def toPython_decimal_test(): """docstring for toPython""" # test a normal iso d = Literal('.1', datatype=XSD.decimal).toPython() assert isinstance(d, Decimal) payments = [ Literal(s, datatype=XSD.decimal) for s in '.1 .1 .1 -.3'.split() ] assert sum([payment.toPython() for payment in payments]) == 0
def filter_by(cls, **kwargs): """Class method returns a generator over classs instances meeting the kwargs conditions. Each keyword must be a class descriptor filter by RDF.type == cls.rdf_type is implicit Order helps, the first keyword should be the most restrictive """ filters = [] for key, value in kwargs.items(): pred = cls._getdescriptor(key).pred # try to make the value be OK for the triple query as an object if isinstance(value, Identifier): obj = value else: obj = Literal(value) filters.append((pred, obj)) # make sure we filter by type if not (RDF.type, cls.rdf_type) in filters: filters.append((RDF.type, cls.rdf_type)) pred, obj = filters[0] log.debug("Checking %s, %s" % (pred, obj)) for sub in cls.db.subjects(pred, obj): log.debug("maybe %s" % sub) for pred, obj in filters[1:]: log.debug("Checking %s, %s" % (pred, obj)) try: cls.db.triples((sub, pred, obj)).next() except: log.warn("No %s" % sub) break else: yield cls(sub)
def get_by(cls, **kwargs): """Class Method, returns a single instance of the class by a single kwarg. the keyword must be a descriptor of the class. example: .. code-block:: python bigBlue = Company.get_by(symbol='IBM') :Note: the keyword should map to an rdf predicate that is of type owl:InverseFunctional""" if len(kwargs) != 1: raise ValueError("get_by wanted exactly 1 but got %i args\n" + "Maybe you wanted filter_by" % (len(kwargs))) key, value = kwargs.items()[0] if isinstance(value, (URIRef, BNode, Literal)): o = value else: o = Literal(value) pred = cls._getdescriptor(key).pred uri = cls.db.value(None, pred, o) if uri: return cls(uri) else: raise LookupError("%s = %s not found" % (key, value))
def toPython_datetime_test(): """docstring for toPython""" # test a normal iso d = Literal('2008-02-09T10:46:29', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) d = Literal('2008-02-09T10:46:29Z', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) d = Literal('2008-02-09T10:46:29-07:00', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) d = Literal('2008-02-09T10:46:29.1', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) d = Literal('2008-02-09T10:46:29.123', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) d = Literal('2008-02-09T10:46:29.123456', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime) # test a normal iso with fractional seconds d = Literal('2008-02-09 10:46:29', datatype=XSD.dateTime).toPython() assert isinstance(d, datetime)