diff options
author | Graham Higgins <gjh-github@bel-epa.com> | 2013-01-11 07:32:02 +0000 |
---|---|---|
committer | Graham Higgins <gjh-github@bel-epa.com> | 2013-01-11 07:32:02 +0000 |
commit | 9de5a66b43dffb43bddb9a04c2ba639396ff13be (patch) | |
tree | 2ef5aa2c3be7fe076f4bb193dfb3ab5c94f9b577 /rdflib/util.py | |
parent | d40abf267214a5398906a16825368aa51e00cf5c (diff) | |
download | rdflib-9de5a66b43dffb43bddb9a04c2ba639396ff13be.tar.gz |
apply autopep8 standards.
$ flake8 rdflib --exclude=pyRdfa,host,extras,transform,rdfs,pyMicrodata
rdflib/graph.py:192: W801 redefinition of unused 'BytesIO' from line 189
rdflib/graph.py:194: W402 'BytesIO' imported but unused
rdflib/graph.py:680:80: E501 line too long (80 > 79 characters)
rdflib/graph.py:682:80: E501 line too long (80 > 79 characters)
rdflib/graph.py:686:80: E501 line too long (83 > 79 characters)
rdflib/graph.py:690:80: E501 line too long (83 > 79 characters)
rdflib/graph.py:692:80: E501 line too long (83 > 79 characters)
rdflib/graph.py:695:80: E501 line too long (83 > 79 characters)
rdflib/graph.py:698:80: E501 line too long (83 > 79 characters)
rdflib/parser.py:21: W801 redefinition of unused 'BytesIO' from line 19
rdflib/compat.py:12: W801 redefinition of unused 'defaultdict' from line 10
rdflib/py3compat.py:10: W801 redefinition of unused 'wraps' from line 7
rdflib/py3compat.py:81: W806 redefinition of function 'b' from line 44
rdflib/py3compat.py:87: W806 redefinition of function 'format_doctest_out' from line 50
rdflib/py3compat.py:97: W806 redefinition of function 'type_cmp' from line 61
rdflib/term.py:54: W801 redefinition of unused 'md5' from line 52
rdflib/store.py:73: W801 redefinition of unused 'BytesIO' from line 71
rdflib/query.py:10: W801 redefinition of unused 'BytesIO' from line 8
rdflib/__init__.py:73: W402 'plugin' imported but unused
rdflib/__init__.py:74: W402 'query' imported but unused
rdflib/util.py:43: W806 redefinition of function 'sign' from line 50
rdflib/plugins/parsers/hturtle.py:25: W801 redefinition of unused 'html5lib' from line 24
rdflib/plugins/parsers/ntriples.py:141: W402 'BytesIO' imported but unused
rdflib/plugins/parsers/structureddata.py:23: W801 redefinition of unused 'html5lib' from line 22
Diffstat (limited to 'rdflib/util.py')
-rw-r--r-- | rdflib/util.py | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/rdflib/util.py b/rdflib/util.py index 4fac9c1a..55bc3845 100644 --- a/rdflib/util.py +++ b/rdflib/util.py @@ -41,11 +41,14 @@ try: cmp except NameError: def sign(n): - if n < 0: return -1 - if n > 0: return 1 + if n < 0: + return -1 + if n > 0: + return 1 return 0 else: - def sign(n): return cmp(n, 0) + def sign(n): + return cmp(n, 0) from rdflib.exceptions import ContextTypeError from rdflib.exceptions import ObjectTypeError @@ -57,21 +60,27 @@ from rdflib.term import BNode from rdflib.term import Literal from rdflib.term import URIRef -__all__ = ['list2set', 'first', 'uniq', 'more_than', 'to_term', 'from_n3','date_time', 'parse_date_time', 'check_context', 'check_subject', 'check_predicate', 'check_object', 'check_statement', 'check_pattern'] +__all__ = [ + 'list2set', 'first', 'uniq', 'more_than', 'to_term', 'from_n3', + 'date_time', 'parse_date_time', 'check_context', 'check_subject', + 'check_predicate', 'check_object', 'check_statement', 'check_pattern'] + def list2set(seq): """ - Return a new list without duplicates. + Return a new list without duplicates. Preserves the order, unlike set(seq) """ seen = set() - return [ x for x in seq if x not in seen and not seen.add(x)] + return [x for x in seq if x not in seen and not seen.add(x)] + def first(seq): for result in seq: return result return None + def uniq(sequence, strip=0): """removes duplicate strings from the sequence.""" if strip: @@ -79,6 +88,7 @@ def uniq(sequence, strip=0): else: return set(sequence) + def more_than(sequence, number): "Returns 1 if sequence has more items than number and 0 if not." i = 0 @@ -88,9 +98,10 @@ def more_than(sequence, number): return 1 return 0 + def to_term(s, default=None): """ - Creates and returns an Identifier of type corresponding + Creates and returns an Identifier of type corresponding to the pattern of the given positional argument string ``s``: '' returns the ``default`` keyword argument value or ``None`` @@ -114,21 +125,24 @@ def to_term(s, default=None): msg = "Unrecognised term syntax: '%s'" % s raise Exception(msg) + def from_n3(s, default=None, backend=None): r''' - Creates the Identifier corresponding to the given n3 string. - + Creates the Identifier corresponding to the given n3 string. + >>> from_n3('<http://ex.com/foo>') == URIRef('http://ex.com/foo') True >>> from_n3('"foo"@de') == Literal('foo', lang='de') True - >>> from_n3('"""multi\nline\nstring"""@en') == Literal('multi\nline\nstring', lang='en') + >>> from_n3('"""multi\nline\nstring"""@en') == Literal( + ... 'multi\nline\nstring', lang='en') True >>> from_n3('42') == Literal(42) True - + ''' - # TODO: should be able to handle prefixes given as opt. argument maybe: from_n3('rdfs:label') + # TODO: should be able to handle prefixes given as opt. argument maybe: + # from_n3('rdfs:label') if not s: return default if s.startswith('<'): @@ -137,23 +151,24 @@ def from_n3(s, default=None, backend=None): if s.startswith('"""'): quotes = '"""' else: - quotes = '"' + quotes = '"' value, rest = s.rsplit(quotes, 1) - value = value[len(quotes):] # strip leading quotes + value = value[len(quotes):] # strip leading quotes datatype = None language = None - + # as a given datatype overrules lang-tag check for it first dtoffset = rest.rfind('^^') if dtoffset >= 0: # found a datatype # datatype has to come after lang-tag so ignore everything before - # see: http://www.w3.org/TR/2011/WD-turtle-20110809/#prod-turtle2-RDFLiteral - datatype = rest[dtoffset+2:] + # see: http://www.w3.org/TR/2011/WD-turtle-20110809/ + # #prod-turtle2-RDFLiteral + datatype = rest[dtoffset + 2:] else: if rest.startswith("@"): - language = rest[1:] # strip leading at sign - + language = rest[1:] # strip leading at sign + value = value.replace(r'\"', '"').replace('\\\\', '\\') # Hack: this should correctly handle strings with either native unicode # characters, or \u1234 unicode escapes. @@ -175,28 +190,33 @@ def from_n3(s, default=None, backend=None): else: return BNode(s) + def check_context(c): - if not (isinstance(c, URIRef) or \ + if not (isinstance(c, URIRef) or isinstance(c, BNode)): raise ContextTypeError("%s:%s" % (c, type(c))) + def check_subject(s): """ Test that s is a valid subject identifier.""" if not (isinstance(s, URIRef) or isinstance(s, BNode)): raise SubjectTypeError(s) + def check_predicate(p): """ Test that p is a valid predicate identifier.""" if not isinstance(p, URIRef): raise PredicateTypeError(p) + def check_object(o): """ Test that o is a valid object identifier.""" - if not (isinstance(o, URIRef) or \ - isinstance(o, Literal) or \ + if not (isinstance(o, URIRef) or + isinstance(o, Literal) or isinstance(o, BNode)): raise ObjectTypeError(o) + def check_statement(triple): (s, p, o) = triple if not (isinstance(s, URIRef) or isinstance(s, BNode)): @@ -205,11 +225,12 @@ def check_statement(triple): if not isinstance(p, URIRef): raise PredicateTypeError(p) - if not (isinstance(o, URIRef) or \ - isinstance(o, Literal) or \ + if not (isinstance(o, URIRef) or + isinstance(o, Literal) or isinstance(o, BNode)): raise ObjectTypeError(o) + def check_pattern(triple): (s, p, o) = triple if s and not (isinstance(s, URIRef) or isinstance(s, BNode)): @@ -218,11 +239,12 @@ def check_pattern(triple): if p and not isinstance(p, URIRef): raise PredicateTypeError(p) - if o and not (isinstance(o, URIRef) or \ - isinstance(o, Literal) or \ + if o and not (isinstance(o, URIRef) or + isinstance(o, Literal) or isinstance(o, BNode)): raise ObjectTypeError(o) + def date_time(t=None, local_time_zone=False): """http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z @@ -254,9 +276,11 @@ def date_time(t=None, local_time_zone=False): tzd = "Z" year, month, day, hh, mm, ss, wd, y, z = time_tuple - s = "%0004d-%02d-%02dT%02d:%02d:%02d%s" % ( year, month, day, hh, mm, ss, tzd) + s = "%0004d-%02d-%02dT%02d:%02d:%02d%s" % ( + year, month, day, hh, mm, ss, tzd) return s + def parse_date_time(val): """always returns seconds in UTC @@ -282,7 +306,7 @@ def parse_date_time(val): ymd, time = val.split("T") hms, tz_str = time[0:8], time[8:] - if not tz_str or tz_str=="Z": + if not tz_str or tz_str == "Z": time = time[:-1] tz_offset = 0 else: @@ -299,6 +323,7 @@ def parse_date_time(val): t = t + tz_offset return t + def test(): import doctest doctest.testmod() |