Python API: Tagging

This is the tested source code for the snippets used in Tagging Archives. The config file we’re using in this example can be downloaded here.

Setup

>>> import datafs
>>> from fs.tempfs import TempFS

We test with the following setup:

>>> api = datafs.get_api(
...     config_file='examples/snippets/resources/datafs_mongo.yml')
...

This assumes that you have a config file at the above location. The config file we’re using in this example can be downloaded here.

clean up any previous test failures

>>> try:
...     api.delete_archive('archive1')
... except (KeyError, OSError):
...     pass
...
>>> try:
...     api.delete_archive('archive2')
... except (KeyError, OSError):
...     pass
...
>>> try:
...     api.delete_archive('archive3')
... except (KeyError, OSError):
...     pass
...
>>> try:
...     api.manager.delete_table('DataFiles')
... except KeyError:
...     pass
...

Add a fresh manager table:

>>> api.manager.create_archive_table('DataFiles')

Example 1

Displayed example 1 code:

>>> archive1 = api.create(
...      'archive1',
...      tags=["foo", "Bar"],
...      metadata={'description': 'tag test 1 has bar'})
...
>>> archive1.get_tags()
[u'foo', u'bar']
>>>
>>> archive2 = api.create(
...      'archive2',
...      tags=["foo", "Baz"],
...      metadata={'description': 'tag test 1 has baz'})
...
>>> archive2.get_tags()
[u'foo', u'baz']
>>>
>>> archive2.add_tags(42)
>>> archive2.get_tags()
[u'foo', u'baz', u'42']

Example 2

>>> list(api.search('bar')) 
['archive1']

>>> list(api.search('baz')) 
['archive2']

>>> list(api.search('foo')) 
['archive1', 'archive2']

Actual Example Block 2 Test

>>> assert set(api.search('bar')) == {'archive1'}
>>> assert set(api.search('baz')) == {'archive2'}
>>> assert set(api.search('foo')) == {'archive1', 'archive2'}

Example 3

>>> archive3 = api.create(
...      'archive3',
...      tags=["foo", "bar", "baz"],
...      metadata={'description': 'tag test 3 has all the tags!'})
...

>>> list(api.search('bar', 'foo')) 
['archive1', 'archive3']

>>> list(api.search('bar', 'foo', 'baz')) 
['archive3']

Actual example block 3 search test:

>>> assert set(api.search('bar', 'foo')) == {'archive1', 'archive3'}
>>> assert set(api.search('bar', 'foo', 'baz')) == {'archive3'}

Example 4

>>> list(api.search('qux')) 
[]
>>> list(api.search('foo', 'qux')) 
[]

Actual example block 4 test

>>> assert set(api.search('qux')) == set([])

Example 5

>>> archive1.get_tags() 
['foo', 'bar']

Actual example block 5 test

>>> assert set(archive1.get_tags()) == {'foo', 'bar'}

Example 6

>>> archive1.add_tags('qux')
>>>
>>> list(api.search('foo', 'qux')) 
['archive1']

Actual example block 6 search test

>>> assert set(api.search('foo', 'qux')) == {'archive1'}

Example 7

>>> archive1.delete_tags('foo', 'bar')
>>>
>>> list(api.search('foo', 'bar')) 
['archive3']

Actual example block 7 search test

>>> assert set(api.search('foo', 'bar')) == {'archive3'}

Teardown

>>> archive1.delete()
>>> archive2.delete()
>>> archive3.delete()