> In case it helps, off in one of my applications I've got the
> following bit of code for creating RDF Collections from a list of
> strings. If anyone wants to generalize the code and submit it in the
> form of a patch that'd be great. Else I'll look to do that this
> weekend. Think the only change needed is changing it to directly add
> the items from source (and not create a Literal from them). And a
> corresponding test case would be ideal.

It would probably make sense for this to be promoted to a top level
utility class in Graph (Collection, perhaps) which has a constructor
that mimics this  (with the default being an empty Collection) and
overiddes for *all* Python container methods:

__getitem__(self, k)
__setitem__(self, k, value)
_delitem__(self, k) <-- this could probably use an index function
(same as lists)
__contains__(self, elt)
not __contains__(self, elt)
__iter__(self)  <-- could probably port over Graph.items
__len__(self)

As well as append and extend.  I could write a test suite for such a
class :) and it would be incredibly useful once implemented.d

>
> def make_collection(source, type, context):
>      items = list()
>      for item in source:
>          b = BNode()
>          context.add((b, RDFS.label, Literal(item)))
>          context.add((b, RDF.type, type))
>          items.append(b)
>      items.reverse()
>      rest = RDF.nil
>      for item in items:
>          l = BNode()
>          context.add((l, RDF.first, item))
>          context.add((l, RDF.rest, rest))
>          rest = l
>      return l
_______________________________________________
Dev mailing list
[email protected]
http://rdflib.net/mailman/listinfo/dev

Reply via email to