__init__.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """A pure Python package providing the core RDF constructs.
  2. The packages is intended to provide the core RDF types and interfaces
  3. for working with RDF. The package defines a plugin interface for
  4. parsers, stores, and serializers that other packages can use to
  5. implement parsers, stores, and serializers that will plug into the
  6. rdflib package.
  7. The primary interface `rdflib` exposes to work with RDF is
  8. [`rdflib.graph.Graph`][rdflib.graph.Graph].
  9. A tiny example:
  10. ```python
  11. >>> from rdflib import Graph, URIRef, Literal
  12. >>> g = Graph()
  13. >>> result = g.parse("http://www.w3.org/2000/10/swap/test/meet/blue.rdf")
  14. >>> print("graph has %s statements." % len(g))
  15. graph has 4 statements.
  16. >>>
  17. >>> for s, p, o in g:
  18. ... if (s, p, o) not in g:
  19. ... raise Exception("It better be!")
  20. >>> s = g.serialize(format='nt')
  21. >>>
  22. >>> sorted(g) == [
  23. ... (URIRef("http://meetings.example.com/cal#m1"),
  24. ... URIRef("http://www.example.org/meeting_organization#homePage"),
  25. ... URIRef("http://meetings.example.com/m1/hp")),
  26. ... (URIRef("http://www.example.org/people#fred"),
  27. ... URIRef("http://www.example.org/meeting_organization#attending"),
  28. ... URIRef("http://meetings.example.com/cal#m1")),
  29. ... (URIRef("http://www.example.org/people#fred"),
  30. ... URIRef("http://www.example.org/personal_details#GivenName"),
  31. ... Literal("Fred")),
  32. ... (URIRef("http://www.example.org/people#fred"),
  33. ... URIRef("http://www.example.org/personal_details#hasEmail"),
  34. ... URIRef("mailto:fred@example.com"))
  35. ... ]
  36. True
  37. ```
  38. """
  39. import logging
  40. import sys
  41. from importlib import metadata
  42. _DISTRIBUTION_METADATA = metadata.metadata("rdflib")
  43. __docformat__ = "restructuredtext en"
  44. __version__: str = _DISTRIBUTION_METADATA["Version"]
  45. __date__ = "2026-02-13"
  46. __all__ = [
  47. "URIRef",
  48. "BNode",
  49. "IdentifiedNode",
  50. "Literal",
  51. "Node",
  52. "Variable",
  53. "Namespace",
  54. "Dataset",
  55. "Graph",
  56. "ConjunctiveGraph",
  57. "BRICK",
  58. "CSVW",
  59. "DC",
  60. "DCAT",
  61. "DCMITYPE",
  62. "DCTERMS",
  63. "DOAP",
  64. "FOAF",
  65. "ODRL2",
  66. "ORG",
  67. "OWL",
  68. "PROF",
  69. "PROV",
  70. "QB",
  71. "RDF",
  72. "RDFS",
  73. "SDO",
  74. "SH",
  75. "SKOS",
  76. "SOSA",
  77. "SSN",
  78. "TIME",
  79. "VANN",
  80. "VOID",
  81. "XMLNS",
  82. "XSD",
  83. "util",
  84. "plugin",
  85. "query",
  86. "NORMALIZE_LITERALS",
  87. ]
  88. logger = logging.getLogger(__name__)
  89. try:
  90. import __main__
  91. if (
  92. not hasattr(__main__, "__file__")
  93. and sys.stdout is not None
  94. and hasattr(sys.stderr, "isatty")
  95. and sys.stderr.isatty()
  96. ):
  97. # show log messages in interactive mode
  98. logger.setLevel(logging.INFO)
  99. logger.addHandler(logging.StreamHandler())
  100. del __main__
  101. except ImportError:
  102. # Main already imported from elsewhere
  103. import warnings
  104. warnings.warn("__main__ already imported", ImportWarning)
  105. del warnings
  106. del sys
  107. NORMALIZE_LITERALS = True
  108. """
  109. If True - Literals lexical forms are normalized when created.
  110. I.e. the lexical forms is parsed according to data-type, then the
  111. stored lexical form is the re-serialized value that was parsed.
  112. Illegal values for a datatype are simply kept. The normalized keyword
  113. for Literal.__new__ can override this.
  114. For example:
  115. ```python
  116. >>> from rdflib import Literal,XSD
  117. >>> Literal("01", datatype=XSD.int)
  118. rdflib.term.Literal("1", datatype=rdflib.term.URIRef("http://www.w3.org/2001/XMLSchema#integer"))
  119. ```
  120. This flag may be changed at any time, but will only affect literals
  121. created after that time, previously created literals will remain
  122. (un)normalized.
  123. """
  124. DAWG_LITERAL_COLLATION = False
  125. """DAWG_LITERAL_COLLATION determines how literals are ordered or compared
  126. to each other.
  127. In SPARQL, applying the >,<,>=,<= operators to literals of
  128. incompatible data-types is an error, i.e:
  129. `Literal(2)>Literal('cake')` is neither true nor false, but an error.
  130. This is a problem in PY3, where lists of Literals of incompatible
  131. types can no longer be sorted.
  132. Setting this flag to True gives you strict DAWG/SPARQL compliance,
  133. setting it to False will order Literals with incompatible datatypes by
  134. datatype URI
  135. In particular, this determines how the rich comparison operators for
  136. Literal work, eq, `__neq__`, `__lt__`, etc.
  137. """
  138. from rdflib.graph import ConjunctiveGraph, Dataset, Graph
  139. from rdflib.namespace import (
  140. BRICK,
  141. CSVW,
  142. DC,
  143. DCAT,
  144. DCMITYPE,
  145. DCTERMS,
  146. DOAP,
  147. FOAF,
  148. ODRL2,
  149. ORG,
  150. OWL,
  151. PROF,
  152. PROV,
  153. QB,
  154. RDF,
  155. RDFS,
  156. SDO,
  157. SH,
  158. SKOS,
  159. SOSA,
  160. SSN,
  161. TIME,
  162. VANN,
  163. VOID,
  164. XMLNS,
  165. XSD,
  166. Namespace,
  167. )
  168. from rdflib.term import BNode, IdentifiedNode, Literal, Node, URIRef, Variable
  169. from rdflib import plugin, query, util # isort:skip
  170. from rdflib.container import * # isort:skip # noqa: F403