term
This module defines the different types of terms. Terms are the kinds of objects that can appear in a quoted/asserted triple. This includes those that are core to RDF:
- Blank Nodes - Blank Nodes
- URI References - URI References
- Literals - Literals (which consist of a literal value, datatype and language tag)
Those that extend the RDF model into N3:
QuotedGraph- FormulaeVariable- Universal Quantifications (Variables)
And those that are primarily for matching against ‘Nodes’ in the underlying Graph:
- REGEX Expressions
- Date Ranges
- Numerical Ranges
Classes:
-
BNode–RDF 1.1’s Blank Nodes Section: https://www.w3.org/TR/rdf11-concepts/#section-blank-nodes
-
IdentifiedNode–An abstract class, primarily defined to identify Nodes that are not Literals.
-
Identifier–See http://www.w3.org/2002/07/rdf-identifer-terminology/
-
Literal–RDF 1.1’s Literals Section: http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal
-
Node–A Node in the Graph.
-
URIRef– -
Variable–A Variable - this is used for querying, or in Formula aware
Functions:
-
bind–register a new datatype<->pythontype binding
XSDToPython
module-attribute
XSDToPython: Dict[Optional[str], Optional[Callable[[str], Any]]] = {None: None, URIRef(_XSD_PFX + 'time'): parse_time, URIRef(_XSD_PFX + 'date'): parse_xsd_date, URIRef(_XSD_PFX + 'dateTime'): parse_datetime, URIRef(_XSD_PFX + 'duration'): parse_xsd_duration, URIRef(_XSD_PFX + 'dayTimeDuration'): parse_xsd_duration, URIRef(_XSD_PFX + 'yearMonthDuration'): parse_xsd_duration, URIRef(_XSD_PFX + 'hexBinary'): _unhexlify, URIRef(_XSD_PFX + 'string'): None, URIRef(_XSD_PFX + 'normalizedString'): None, URIRef(_XSD_PFX + 'token'): None, URIRef(_XSD_PFX + 'language'): None, URIRef(_XSD_PFX + 'boolean'): _parseBoolean, URIRef(_XSD_PFX + 'decimal'): Decimal, URIRef(_XSD_PFX + 'integer'): long_type, URIRef(_XSD_PFX + 'nonPositiveInteger'): long_type, URIRef(_XSD_PFX + 'long'): long_type, URIRef(_XSD_PFX + 'nonNegativeInteger'): long_type, URIRef(_XSD_PFX + 'negativeInteger'): long_type, URIRef(_XSD_PFX + 'int'): int, URIRef(_XSD_PFX + 'unsignedLong'): long_type, URIRef(_XSD_PFX + 'positiveInteger'): long_type, URIRef(_XSD_PFX + 'short'): int, URIRef(_XSD_PFX + 'unsignedInt'): int, URIRef(_XSD_PFX + 'byte'): int, URIRef(_XSD_PFX + 'unsignedShort'): int, URIRef(_XSD_PFX + 'unsignedByte'): int, URIRef(_XSD_PFX + 'float'): float, URIRef(_XSD_PFX + 'double'): float, URIRef(_XSD_PFX + 'base64Binary'): b64decode, URIRef(_XSD_PFX + 'anyURI'): None, _RDF_XMLLITERAL: _parseXML}
__all__
module-attribute
__all__ = ['bind', '_is_valid_uri', 'Node', 'IdentifiedNode', 'Identifier', 'URIRef', 'BNode', 'Literal', 'Variable']
BNode
Bases: IdentifiedNode
RDF 1.1’s Blank Nodes Section: https://www.w3.org/TR/rdf11-concepts/#section-blank-nodes
Blank Nodes are local identifiers for unnamed nodes in RDF graphs that are used in some concrete RDF syntaxes or RDF store implementations. They are always locally scoped to the file or RDF store, and are not persistent or portable identifiers for blank nodes. The identifiers for Blank Nodes are not part of the RDF abstract syntax, but are entirely dependent on particular concrete syntax or implementation (such as Turtle, JSON-LD).
RDFLib’s BNode class makes unique IDs for all the Blank Nodes in a Graph but you
should never expect, or reply on, BNodes’ IDs to match across graphs, or even for
multiple copies of the same graph, if they are regenerated from some non-RDFLib
source, such as loading from RDF data.
Methods:
-
__new__–only store implementations should pass in a value
-
__reduce__– -
__repr__– -
n3– -
skolemize–Create a URIRef “skolem” representation of the BNode, in accordance
Attributes:
__new__
__new__(value: Optional[str] = None, _sn_gen: Optional[Union[Callable[[], str], Generator]] = None, _prefix: str = _unique_id())
only store implementations should pass in a value
Source code in rdflib/term.py
__reduce__
__reduce__() -> Tuple[Type[BNode], Tuple[str]]
__repr__
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
skolemize
skolemize(authority: Optional[str] = None, basepath: Optional[str] = None) -> URIRef
Create a URIRef “skolem” representation of the BNode, in accordance with http://www.w3.org/TR/rdf11-concepts/#section-skolemization
Added in version 4.0
Source code in rdflib/term.py
Genid
IdentifiedNode
Bases: Identifier
An abstract class, primarily defined to identify Nodes that are not Literals.
The name “Identified Node” is not explicitly defined in the RDF specification, but can be drawn from this section: https://www.w3.org/TR/rdf-concepts/#section-URI-Vocabulary
Methods:
-
__getnewargs__– -
n3– -
toPython–
Attributes:
__getnewargs__
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
Identifier
Bases: Node, str
See http://www.w3.org/2002/07/rdf-identifer-terminology/ regarding choice of terminology.
Methods:
-
__eq__–Equality for Nodes.
-
__ge__– -
__gt__–This implements ordering for Nodes.
-
__le__– -
__lt__– -
__ne__– -
__new__– -
eq–A “semantic”/interpreted equality function,
-
neq–A “semantic”/interpreted not equal function,
-
startswith–
Attributes:
__eq__
Equality for Nodes.
>>> BNode("foo")==None
False
>>> BNode("foo")==URIRef("foo")
False
>>> URIRef("foo")==BNode("foo")
False
>>> BNode("foo")!=URIRef("foo")
True
>>> URIRef("foo")!=BNode("foo")
True
>>> Variable('a')!=URIRef('a')
True
>>> Variable('a')!=Variable('a')
False
Source code in rdflib/term.py
__ge__
__gt__
This implements ordering for Nodes.
This tries to implement this: http://www.w3.org/TR/sparql11-query/#modOrderBy
Variables are not included in the SPARQL list, but they are greater than BNodes and smaller than everything else
Source code in rdflib/term.py
__le__
__lt__
Source code in rdflib/term.py
__ne__
__new__
eq
neq
Literal
Bases: Identifier
RDF 1.1’s Literals Section: http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal
Literals are used for values such as strings, numbers, and dates.
A literal in an RDF graph consists of two or three elements:
- a lexical form, being a Unicode string, which SHOULD be in Normal Form C
- a datatype IRI, being an IRI identifying a datatype that determines how the lexical form maps to a literal value, and
- if and only if the datatype IRI is
http://www.w3.org/1999/02/22-rdf-syntax-ns#langString, a non-empty language tag. The language tag MUST be well-formed according to section 2.2.9 ofTags for identifying languages <http://tools.ietf.org/html/bcp47>_.
A literal is a language-tagged string if the third element is present. Lexical representations of language tags MAY be converted to lower case. The value space of language tags is always in lower case.
For valid XSD datatypes, the lexical form is optionally normalized
at construction time. Default behaviour is set by rdflib.NORMALIZE_LITERALS
and can be overridden by the normalize parameter to __new__
Equality and hashing of Literals are done based on the lexical form, i.e.:
>>> from rdflib.namespace import XSD
>>> Literal('01') != Literal('1') # clear - strings differ
True
but with data-type they get normalized:
unless disabled:
Value based comparison is possible:
The eq method also provides limited support for basic python types:
>>> Literal(1).eq(1) # fine - int compatible with xsd:integer
True
>>> Literal('a').eq('b') # fine - str compatible with plain-lit
False
>>> Literal('a', datatype=XSD.string).eq('a') # fine - str compatible with xsd:string
True
>>> Literal('a').eq(1) # not fine, int incompatible with plain-lit
NotImplemented
Greater-than/less-than ordering comparisons are also done in value space, when compatible datatypes are used. Incompatible datatypes are ordered by DT, or by lang-tag. For other nodes the ordering is None < BNode < URIRef < Literal
Any comparison with non-rdflib Node are “NotImplemented” In PY3 this is an error.
>>> from rdflib import Literal, XSD
>>> lit2006 = Literal('2006-01-01',datatype=XSD.date)
>>> lit2006.toPython()
datetime.date(2006, 1, 1)
>>> lit2006 < Literal('2007-01-01',datatype=XSD.date)
True
>>> Literal(datetime.utcnow()).datatype
rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime')
>>> Literal(1) > Literal(2) # by value
False
>>> Literal(1) > Literal(2.0) # by value
False
>>> Literal('1') > Literal(1) # by DT
True
>>> Literal('1') < Literal('1') # by lexical form
False
>>> Literal('a', lang='en') > Literal('a', lang='fr') # by lang-tag
False
>>> Literal(1) > URIRef('foo') # by node-type
True
The > < operators will eat this NotImplemented and throw a TypeError (py3k):
Methods:
-
__abs__–Implements absolute value operation for Literals with numeric values.
-
__add__–```python
-
__bool__–Determines the truth value of the Literal.
-
__eq__–Equality operator for Literals.
-
__ge__– -
__getstate__– -
__gt__–Implements the greater-than comparison for Literals.
-
__hash__–Hash function for Literals to enable their use as dictionary keys.
-
__invert__–Implements bitwise NOT operation for Literals with numeric values.
-
__le__–Less than or equal operator for Literals.
-
__lt__– -
__neg__–Implements unary negation for Literals with numeric values.
-
__new__–Create a new Literal instance.
-
__pos__–Implements unary plus operation for Literals with numeric values.
-
__reduce__– -
__repr__– -
__setstate__– -
__sub__–Implements subtraction between Literals or between a Literal and a Python object.
-
eq–Compare the value of this literal with something else.
-
n3–Returns a representation in the N3 format.
-
neq– -
normalize–Returns a new literal with a normalised lexical representation
-
toPython–Returns an appropriate python datatype derived from this RDF Literal
Attributes:
-
__slots__– -
datatype(Optional[URIRef]) – -
ill_typed(Optional[bool]) –For `recognized datatype IRIs
-
language(Optional[str]) – -
value(Any) –
__slots__
class-attribute
instance-attribute
ill_typed
property
For recognized datatype IRIs
<https://www.w3.org/TR/rdf11-concepts/#dfn-recognized-datatype-iris>,
this value will be True if the literal is ill formed, otherwise it
will be False. Literal.value (i.e. the literal value <https://www.w3.org/TR/rdf11-concepts/#dfn-literal-value>) should always be defined if this property is False, but should not be considered reliable if this property is True.
If the literal’s datatype is None or not in the set of recognized datatype IRIs
<https://www.w3.org/TR/rdf11-concepts/#dfn-recognized-datatype-iris>_ this value will be None.
__abs__
__abs__() -> Literal
Implements absolute value operation for Literals with numeric values.
Example
# Absolute value of a negative integer Literal
abs(Literal(-1))
# rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Using a string with a datatype
from rdflib.namespace import XSD
abs(Literal("-1", datatype=XSD.integer))
# rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# This will raise a TypeError
abs(Literal("1"))
# TypeError: Not a number; rdflib.term.Literal('1')
Source code in rdflib/term.py
__add__
__add__(val: Any) -> Literal
>>> from rdflib.namespace import XSD
>>> Literal(1) + 1
rdflib.term.Literal('2', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
>>> Literal("1") + "1"
rdflib.term.Literal('11')
# Handling dateTime/date/time based operations in Literals
>>> a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
>>> b = Literal('P31D', datatype=XSD.duration)
>>> (a + b)
rdflib.term.Literal('2006-02-01T20:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))
>>> from rdflib.namespace import XSD
>>> a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
>>> b = Literal('P122DT15H58M', datatype=XSD.duration)
>>> (a + b)
rdflib.term.Literal('2006-11-01T12:50:00', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#dateTime'))
Source code in rdflib/term.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
__bool__
Determines the truth value of the Literal.
Used for if statements, bool(literal), etc.
__eq__
Equality operator for Literals.
Literals are only equal to other literals.
Notes
“Two literals are equal if and only if all of the following hold: * The strings of the two lexical forms compare equal, character by character. * Either both or neither have language tags. * The language tags, if any, compare equal. * Either both or neither have datatype URIs. * The two datatype URIs, if any, compare equal, character by character.” – 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)
Example
Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo"))
# True
Literal("1", datatype=URIRef("foo")) == Literal("1", datatype=URIRef("foo2"))
# False
Literal("1", datatype=URIRef("foo")) == Literal("2", datatype=URIRef("foo"))
# False
Literal("1", datatype=URIRef("foo")) == "asdf"
# False
from rdflib import XSD
Literal('2007-01-01', datatype=XSD.date) == Literal('2007-01-01', datatype=XSD.date)
# True
Literal('2007-01-01', datatype=XSD.date) == date(2007, 1, 1)
# False
Literal("one", lang="en") == Literal("one", lang="en")
# True
Literal("hast", lang='en') == Literal("hast", lang='de')
# False
Literal("1", datatype=XSD.integer) == Literal(1)
# True
Literal("1", datatype=XSD.integer) == Literal("01", datatype=XSD.integer)
# True
Source code in rdflib/term.py
__ge__
__getstate__
__gt__
Implements the greater-than comparison for Literals.
This is the base method for ordering comparisons - other comparison methods delegate here.
Implements the ordering rules described in http://www.w3.org/TR/sparql11-query/#modOrderBy
In summary: 1. Literals with compatible data-types are ordered in value space 2. Incompatible datatypes are ordered by their datatype URIs 3. Literals with language tags are ordered by their language tags 4. Plain literals come before xsd:string literals 5. In the node order: None < BNode < URIRef < Literal
Example
from rdflib import XSD
from decimal import Decimal
# Comparing numeric literals in value space
Literal(1) > Literal(2) # int/int
# False
Literal(2.0) > Literal(1) # double/int
# True
Literal(Decimal("3.3")) > Literal(2.0) # decimal/double
# True
Literal(Decimal("3.3")) < Literal(4.0) # decimal/double
# True
# Comparing string literals
Literal('b') > Literal('a') # plain lit/plain lit
# True
Literal('b') > Literal('a', datatype=XSD.string) # plain lit/xsd:str
# True
# Incompatible datatypes ordered by DT
Literal(1) > Literal("2") # int>string
# False
# Langtagged literals ordered by lang tag
Literal("a", lang="en") > Literal("a", lang="fr")
# False
Source code in rdflib/term.py
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 | |
__hash__
Hash function for Literals to enable their use as dictionary keys.
Example
Notes
“Called for the key object for dictionary operations, and by the built-in function hash(). Should return a 32-bit integer usable as a hash value for dictionary operations. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g., using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.” – 3.4.1 Basic customization (Python)
“Two literals are equal if and only if all of the following hold: * The strings of the two lexical forms compare equal, character by character. * Either both or neither have language tags. * The language tags, if any, compare equal. * Either both or neither have datatype URIs. * The two datatype URIs, if any, compare equal, character by character.” – 6.5.1 Literal Equality (RDF: Concepts and Abstract Syntax)
Source code in rdflib/term.py
__invert__
__invert__() -> Literal
Implements bitwise NOT operation for Literals with numeric values.
Example
# Bitwise NOT of a negative integer Literal
~(Literal(-1))
# rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Using a string with a datatype
from rdflib.namespace import XSD
~(Literal("-1", datatype=XSD.integer))
# rdflib.term.Literal('0', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# This will raise a TypeError
~(Literal("1"))
# TypeError: Not a number; rdflib.term.Literal('1')
Source code in rdflib/term.py
__le__
Less than or equal operator for Literals.
Example
Source code in rdflib/term.py
__lt__
Source code in rdflib/term.py
__neg__
__neg__() -> Literal
Implements unary negation for Literals with numeric values.
Example
# Negating an integer Literal
-Literal(1)
# rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Negating a float Literal
-Literal(10.5)
# rdflib.term.Literal('-10.5', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))
# Using a string with a datatype
from rdflib.namespace import XSD
-Literal("1", datatype=XSD.integer)
# rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# This will raise a TypeError
-Literal("1")
# TypeError: Not a number; rdflib.term.Literal('1')
Source code in rdflib/term.py
__new__
__new__(lexical_or_value: Any, lang: Optional[str] = None, datatype: Optional[str] = None, normalize: Optional[bool] = None)
Create a new Literal instance.
Source code in rdflib/term.py
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | |
__pos__
__pos__() -> Literal
Implements unary plus operation for Literals with numeric values.
Example
# Applying unary plus to an integer Literal
+Literal(1)
# rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Applying unary plus to a negative integer Literal
+Literal(-1)
# rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Using a string with a datatype
from rdflib.namespace import XSD
+Literal("-1", datatype=XSD.integer)
# rdflib.term.Literal('-1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# This will raise a TypeError
+Literal("1")
# TypeError: Not a number; rdflib.term.Literal('1')
Source code in rdflib/term.py
__reduce__
__reduce__() -> Tuple[Type[Literal], Tuple[str, Union[str, None], Union[str, None]]]
__repr__
Source code in rdflib/term.py
__setstate__
__sub__
__sub__(val: Any) -> Literal
Implements subtraction between Literals or between a Literal and a Python object.
Example
from rdflib.namespace import XSD
# Basic numeric subtraction
Literal(2) - 1
# rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
Literal(1.1) - 1.0
# rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#double'))
Literal(1.1) - 1
# rdflib.term.Literal('0.1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#decimal'))
Literal(1.1, datatype=XSD.float) - Literal(1.0, datatype=XSD.float)
# rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#float'))
# This will raise a TypeError
Literal("1.1") - 1.0
# TypeError: Not a number; rdflib.term.Literal('1.1')
Literal(1.1, datatype=XSD.integer) - Literal(1.0, datatype=XSD.integer)
# rdflib.term.Literal('0.10000000000000009', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
# Handling dateTime/date/time based operations in Literals
a = Literal('2006-01-01T20:50:00', datatype=XSD.dateTime)
b = Literal('2006-02-01T20:50:00', datatype=XSD.dateTime)
(b - a)
# rdflib.term.Literal('P31D', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
a = Literal('2006-07-01T20:52:00', datatype=XSD.dateTime)
b = Literal('2006-11-01T12:50:00', datatype=XSD.dateTime)
(a - b)
# rdflib.term.Literal('-P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
(b - a)
# rdflib.term.Literal('P122DT15H58M', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#duration'))
Source code in rdflib/term.py
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 | |
eq
Compare the value of this literal with something else.
This comparison can be done in two ways:
-
With the value of another literal - comparisons are then done in literal “value space” according to the rules of XSD subtype-substitution/type-promotion
-
With a Python object:
- string objects can be compared with plain-literals or those with datatype xsd:string
- bool objects with xsd:boolean
- int, long or float with numeric xsd types
- date, time, datetime objects with xsd:date, xsd:time, xsd:datetime
Any other operations returns NotImplemented.
Source code in rdflib/term.py
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 | |
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
Returns a representation in the N3 format.
Strings with newlines or triple-quotes:
>>> Literal("foo\nbar").n3()
'"""foo\nbar"""'
>>> Literal("''\'").n3()
'"\'\'\'"'
>>> Literal('"""').n3()
'"\\"\\"\\""'
Language:
Datatypes:
>>> Literal(1).n3()
'"1"^^<http://www.w3.org/2001/XMLSchema#integer>'
>>> Literal(1.0).n3()
'"1.0"^^<http://www.w3.org/2001/XMLSchema#double>'
>>> Literal(True).n3()
'"true"^^<http://www.w3.org/2001/XMLSchema#boolean>'
Datatype and language isn’t allowed (datatype takes precedence):
Custom datatype:
>>> footype = URIRef("http://example.org/ns#foo")
>>> Literal("1", datatype=footype).n3()
'"1"^^<http://example.org/ns#foo>'
Passing a namespace-manager will use it to abbreviate datatype URIs:
Source code in rdflib/term.py
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 | |
neq
normalize
normalize() -> Literal
Returns a new literal with a normalised lexical representation of this literal
>>> from rdflib import XSD
>>> Literal("01", datatype=XSD.integer, normalize=False).normalize()
rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
Illegal lexical forms for the datatype given are simply passed on
>>> Literal("a", datatype=XSD.integer, normalize=False)
rdflib.term.Literal('a', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer'))
Source code in rdflib/term.py
toPython
Returns an appropriate python datatype derived from this RDF Literal
Node
Bases: ABC
A Node in the Graph.
Methods:
-
n3–
Attributes:
n3
abstractmethod
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
RDFLibGenid
URIRef
Bases: IdentifiedNode
Terminology
Documentation on RDF outside of RDFLib uses the term IRI or URI whereas this class is called URIRef. This is because it was made when the first version of the RDF specification was current, and it used the term URIRef, see RDF 1.0 URIRef
An IRI (Internationalized Resource Identifier) within an RDF graph is a Unicode string that conforms to the syntax defined in RFC 3987.
IRIs in the RDF abstract syntax MUST be absolute, and MAY contain a fragment identifier.
IRIs are a generalization of URIs [RFC3986] that permits a wider range of Unicode characters.
Methods:
-
__add__– -
__mod__– -
__new__– -
__radd__– -
__reduce__– -
__repr__– -
de_skolemize–Create a Blank Node from a skolem URI, in accordance
-
defrag– -
n3–This will do a limited check for valid URIs,
Attributes:
-
__invert__(Callable[[URIRef], InvPath]) – -
__neg__(Callable[[URIRef], NegatedPath]) – -
__or__(Callable[[URIRef, Union[URIRef, Path]], AlternativePath]) – -
__slots__– -
__truediv__(Callable[[URIRef, Union[URIRef, Path]], SequencePath]) – -
fragment(str) –Return the URL Fragment
fragment
property
__add__
__add__(other) -> URIRef
__mod__
__mod__(other) -> URIRef
__new__
Source code in rdflib/term.py
__radd__
__radd__(other) -> URIRef
__reduce__
__reduce__() -> Tuple[Type[URIRef], Tuple[str]]
__repr__
de_skolemize
de_skolemize() -> BNode
Create a Blank Node from a skolem URI, in accordance with http://www.w3.org/TR/rdf11-concepts/#section-skolemization. This function accepts only rdflib type skolemization, to provide a round-tripping within the system.
Added in version 4.0
Source code in rdflib/term.py
defrag
defrag() -> URIRef
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
This will do a limited check for valid URIs,
essentially just making sure that the string includes no illegal
characters (<, >, ", {, }, |, \,, ^`)
Parameters:
-
(namespace_managerOptional[NamespaceManager], default:None) –if not None, will be used to make up a prefixed name
Source code in rdflib/term.py
Variable
Bases: Identifier
A Variable - this is used for querying, or in Formula aware graphs, where Variables can be stored
Methods:
-
__new__– -
__reduce__– -
__repr__– -
n3– -
toPython–
Attributes:
__new__
__reduce__
__reduce__() -> Tuple[Type[Variable], Tuple[str]]
__repr__
n3
n3(namespace_manager: Optional[NamespaceManager] = None) -> str
bind
bind(datatype: str, pythontype: Type[Any], constructor: Optional[Callable[[str], Any]] = None, lexicalizer: Optional[Callable[[Any], Union[str, bytes]]] = None, datatype_specific: bool = False) -> None
register a new datatype<->pythontype binding
Parameters:
-
(constructorOptional[Callable[[str], Any]], default:None) –An optional function for converting lexical forms into a Python instances, if not given the pythontype is used directly
-
(lexicalizerOptional[Callable[[Any], Union[str, bytes]]], default:None) –An optional function for converting python objects to lexical form, if not given object.str is used
-
(datatype_specificbool, default:False) –Makes the lexicalizer function be accessible from the pair (pythontype, datatype) if set to True or from the pythontype otherwise. False by default