The surf.util Module

class surf.util.Single(attr)[source]

Bases: object

Descriptor for easy access to attributes with single value.

surf.util.attr2rdf(attr_name)[source]

Convert an attribute name in the form:

# direct predicate
instance1.foaf_name
# inverse predicate
instance2.if_foaf_title_of

to

<!-- direct predicate -->
<http://xmlns.com/foaf/spec/#term_name>
<!-- inverse predicate -->
<http://xmlns.com/foaf/spec/#term_title>
Parameters:attr_name (str) – the attribute name to convert to RDF
Returns:a (uri representation, True if it’s a direct predicate or False if its an inverse predicate) tuple.
Return type:tuple
surf.util.de_camel_case(camel_case, delim=' ', method=2)[source]

Adds spaces to a camel case string. Failure to space out string returns the original string.

Parameters:
  • camel_case (str) – the camel cased string
  • delim (str) – the delimiter
  • method (int) – the method
Returns:

the normalized string

Return type:

str

surf.util.is_attr_direct(attr_name)[source]

Checks whether this is a direct or inverse edge / property. The naming convention defined by the pattern_direct and pattern_inverse regex patterns.

>>> is_attr_direct('foaf_name')
True
>>> is_attr_direct('is_foaf_name_of')
False
Parameters:attr_name (str) – the attribute name to convert to RDF
Returns:True if attr_name is a direct edge / property
Return type:bool
surf.util.is_uri(uri)[source]

Checks whether the given uri is a URI reference

Parameters:uri (str) – the given uri
Returns:True if a URI reference
Return type:bool
surf.util.json_to_rdflib(json_object)[source]

Convert a json result entry to an rdfLib type.

Parameters:json_object (dict) – the JSON object
Returns:the converted value (if possible)
Return type:rdflib.term.Literal or rdflib.term.BNode or rdflib.term.URIRef or None
surf.util.namespace_split(uri)[source]

Same as uri_split(), but instead of the base of the uri, returns the registered namespace for this uri

>>> print namespace_split('http://mynamespace/ns#some_property')
(rdflib.URIRef('http://mynamespace/ns#'), 'some_property')
Parameters:uri (str) – the uri
Returns:a (namespace, predicate) tuple. Types: (rdflib.term.URIRef, str)
Return type:tuple
surf.util.pattern_direct = <_sre.SRE_Pattern object>

the attribute regex pattern representing a direct edge or property: {{ATTRIBUTE_NAME}}

surf.util.pattern_inverse = <_sre.SRE_Pattern object>

the attribute regex pattern representing an inverse edge or property: is_{{ATTRIBUTE_NAME}}_of

surf.util.pretty_rdf(uri)[source]

Returns a string of the given URI under the form namespace:symbol, if namespace is registered, else returns an empty string

Parameters:uri (str) – the given uri
Returns:the python prettified uri representation
Return type:str
surf.util.rdf2attr(uri, direct)[source]

Inverse of attr2rdf, return the attribute name, given the uri and whether it is direct or not.

>>> print rdf2attr('http://xmlns.com/foaf/spec/#term_name',True)
foaf_name
>>> print rdf2attr('http://xmlns.com/foaf/spec/#term_title',False)
if_foaf_title_of
Parameters:
  • uri (rdflib.term.URIRef or str) – the given uri
  • direct (bool) – whether this is a direct or inverse edge or property
Returns:

the python attribute name

Return type:

str

surf.util.single(attr)[source]

alias for Single

Parameters:attr (rdflib.term.URIRef or str) – the given attribute
Returns:a Single instance
Return type:Single
surf.util.string_conforms_to_base64(string)[source]

check whether the given string conforms to the base64 encoding.

Parameters:string (str) – the string
Returns:True if strings conforms to base64 encoding
Return type:bool
surf.util.uri_split(uri)[source]

Split the uri into base path and remainder, the base is everything that comes before the last #‘ or / including it

>>> print uri_split('http://mynamespace/ns#some_property')
('NS1', 'some_property')
Parameters:uri (rdflib.term.URIRef or basestring) – the uri
Returns:a (base, remainder) tuple. Types: (str, str)
Return type:tuple
surf.util.uri_to_class(uri)[source]

returns a class object from the supplied uri. A valid class name is retrieved using the uri_to_classname() method.

>>> print uri_to_class('http://mynamespace/ns#some_class')
surf.util.Ns1some_class
Parameters:uri (str) – the given uri
Returns:the python class for the given uri
Return type:type
surf.util.uri_to_classname(uri)[source]

Handy function to convert a uri to a Python valid class name

>>> # prints Ns1some_class, where Ns1 is the namespace (not registered, assigned automatically)
>>> print uri_to_classname('http://mynamespace/ns#some_class')
Ns1some_class
Parameters:uri (str) – the uri
Returns:a valid python class name for the given uri
Return type:str
surf.util.uuid_subject(namespace=None)[source]

This function generates a unique subject in the provided namespace based on the uuid.uuid4() method, If namespace is not specified than the default SURF namespace is used

>>> from surf import namespace as ns
>>> print uuid_subject(ns.SIOC)
http://rdfs.org/sioc/ns#1b6ca1d5-41ed-4768-b86a-42185169faff
Parameters:namespace (None or rdflib.namespace.Namespace or str or unicode) – the given namespace
Returns:the RDF subject identifier in the specified namespace
Return type:rdflib.term.URIRef
surf.util.value_to_rdf(value)[source]

Convert the value to an rdflib compatible type if appropriate.

Parameters:value (object) – the value
Returns:the converted value (if possible)
Return type:rdflib.term.Literal or rdflib.term.BNode or rdflib.term.URIRef or object