Python API: Dependencies

This is the tested source code for the snippets used in Managing Data Dependencies. The config file we’re using in this example can be downloaded here. Later on in the script, we use a requirements file. That file can be downloaded here.

Setup

>>> import datafs

We test with the following setup:

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

This assumes that you have the provided config file at the above location.

clean up any previous test failures

>>> try:
...     api.delete_archive('my_archive')
... 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

>>> my_archive = api.create('my_archive')
>>> with my_archive.open('w+',
...     dependencies={'archive2': '1.1', 'archive3': None}) as f:
...
...     res = f.write(u'contents depend on archive 2 v1.1')
...
>>> my_archive.get_dependencies() 
{'archive2': '1.1', 'archive3': None}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> my_archive.get_dependencies() == {
...     'archive2': '1.1', 'archive3': None}
...
True

Example 2

Displayed example 2 code

>>> with my_archive.open('w+') as f:
...
...     res = f.write(u'contents depend on archive 2 v1.2')
...
>>> my_archive.set_dependencies({'archive2': '1.2'})
>>> my_archive.get_dependencies() 
{'archive2': '1.2'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> my_archive.get_dependencies() == {'archive2': '1.2'}
True

Example 3

The following text is displayed to demonstrate the file contents:

1
2
dep1==1.0
dep2==0.4.1a3

The following code creates the actual file contents

Example 4

We test with the following setup:

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

Example:

>>> api = datafs.get_api(
...     requirements = 'requirements_data.txt')
... 
>>>
>>> my_archive = api.get_archive('my_archive')
>>> with my_archive.open('w+') as f:
...     res = f.write(u'depends on dep1 and dep2')
...
>>> my_archive.get_dependencies() 
{'dep1': '1.0', 'dep2': '0.4.1a3'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> my_archive.get_dependencies() == {'dep1': '1.0', 'dep2': '0.4.1a3'}
True

Example 5

>>> my_archive.get_dependencies() 
{'dep1': '1.0', 'dep2': '0.4.1a3'}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> my_archive.get_dependencies() == {'dep1': '1.0', 'dep2': '0.4.1a3'}
True

Example 6

>>> my_archive.get_dependencies(version='0.0.1') 
{'archive2': '1.1', 'archive3': None}

The last line of this test cannot be tested directly (exact dictionary formatting is unstable), so is tested in a second block:

>>> my_archive.get_dependencies(version='0.0.1') == {
...     'archive2': '1.1', 'archive3': None}
True

Teardown

>>> try:
...     api.delete_archive('my_archive')
... except KeyError:
...     pass
...

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