FluidDB Tags in 10 Minutes
July 7, 2010
So, with your namespaces structured it’s now time to add tags.
I’ll assume you already have a FluidDB session open. To add a tag to your current namespace:
>>> namespace.tag_names
[]
>>> namespace.create_tag( "foo", "behold: the foo tag", False )
<FluidResponse (201, 'application/json', None, {'id': 'cac795ce-777f-4d64-94cc-6df5356eb651', 'URI': 'http://fluiddb.fluidinfo.com/tags/metaljoe/foo'})>
>>> namespace.tag_names
['foo']
Okay, let’s take a look at that in more detail. The create_tag() method has three arguments: the tag name, the tag description and a mysterious boolean value at the end. The last value specifies whether the tag should be indexed – in this case, I have left it unindexed. At present, I’m not sure how the indexing works or if it gives any noticeable advantages at present. I should really make an effort to find out….
When my tag has been created, FOM returns a response from FluidDB. For those not fluent in HTTP, 201 is the Created response status: my tag was successfully created. You can also see that FluidDB has assigned my tag a unique GUID, and that the URI of my tag is http://fluiddb.fluidinfo.com/tags/metaljoe/foo – everything in FluidDB can be referenced by a URL, FluidDB’s RESTful API, and everything has a GUID.
>>> namespace.tag_paths ['metaljoe/foo']
Yup, that looks pretty conclusive.
Having a tag defined is one thing, the power of that tag comes when you apply it to objects in the system. Let’s find a suitable object first:
>>> from fom.mapping import Object >>> mars = Object(about="planet:Mars") >>> mars <Object planet:Mars>
Tagging the object is trivial:
>>> mars.set( "metaljoe/foo", None )
As is retrieving the value of that tag:
>>> mars.get( "metaljoe/foo" ) (None, 'application/vnd.fluiddb.value+json')
Now, setting the value to None is pretty dull. How about we set it to something more interesting?
>>> mars.set( "metaljoe/foo", "The red planet" )
>>> mars.get( "metaljoe/foo" )
('The red planet', 'application/vnd.fluiddb.value+json')
Actually, we might not want the value set in the system as a FluidDB JSON value. Let’s try setting it to the text/plain MIME type:
>>> mars.set( "metaljoe/foo", "The red planet", "text/plain" )
>>> mars.get( "metaljoe/foo" )
('The red planet', 'text/plain')
Python lists can also be stored easily enough:
>>> mars.set( "metaljoe/foo", ["The red planet", "My favourite planet"] ) >>> mars.get( "metaljoe/foo" ) (['My favourite planet', 'The red planet'], 'application/vnd.fluiddb.value+json')
Finally, tags have their own class in FOM:
>>> from fom.mapping import Tag >>> tag = Tag( "metaljoe/foo" ) >>> tag <fom.mapping.Tag object at 0x5b62d0> >>> tag.description 'behold: the foo tag'