API

Data Management

class gems.composite(data)

Data structure for traversing object relationships via attributes instead of keys and indices.

Parameters:data (tuple, list, dict) – Data to build composite datastructure from.

Example

>>> data = composite({
>>>     'one': 1,
>>>     'two': [1, 2, 3],
>>>     'three': ['one', 2, {'three': 'four'}],
>>>     'four': {'five': [6, 7, 8], 'nine': 10, 'eleven': 'twelve'}
>>> })
>>> data.four.five[1] == 6
True
>>> data.two[0] == 1
True
append(item)

Append to object, if object is list.

difference(other, recursive=True)

Recursively compute difference of data. For dictionaries, items for specific keys will be reduced to differences. For lists, items will be reduced to differences. This method is meant to be analogous to set.difference for composite objects.

Parameters:
  • other (composite) – Other composite object to difference with.
  • recursive (bool) – Whether or not to perform the operation recursively, for all nested composite objects.
extend(item)

Extend list from object, if object is list.

classmethod from_json(fh)

Load json from file handle.

Parameters:fh (file) – File handle to load from.
Examlple:
>>> with open('data.json', 'r') as json:
>>>    data = composite.load(json)
classmethod from_string(string)

Load data from string.

Parameters:string (str) – String to load from.
Examlple:
>>> with open('data.json', 'r') as json:
>>>     jdat = json.read()
>>> data = composite.from_string(jdat)
classmethod from_yaml(fh)

Load yaml from file handle.

Parameters:fh (file) – File handle to load from.
Examlple:
>>> with open('data.yml', 'r') as json:
>>>    data = composite.load(json)
get(*args, **kwargs)

Return item or None, depending on if item exists. This is meant to be similar to dict.get() for safe access of a property.

index(item)

Return index containing value.

intersection(other, recursive=True)

Recursively compute intersection of data. For dictionaries, items for specific keys will be reduced to unique items. For lists, items will be reduced to unique items. This method is meant to be analogous to set.intersection for composite objects.

Parameters:
  • other (composite) – Other composite object to intersect with.
  • recursive (bool) – Whether or not to perform the operation recursively, for all nested composite objects.
items()

Return keys for object, if they are available.

json()

Return JSON representation of object.

keys()

Return keys for object, if they are available.

classmethod load(fh)

Load json or yaml data from file handle.

Parameters:fh (file) – File handle to load from.
Examlple:
>>> with open('data.json', 'r') as json:
>>>    jsdata = composite.load(json)
>>>
>>> with open('data.yml', 'r') as yml:
>>>    ymldata = composite.load(yml)
pop(*args, **kwargs)

Return item or None, depending on if item exists. This is meant to be similar to dict.pop() for safe access of a property.

union(other, recursive=True, overwrite=False)

Recursively compute union of data. For dictionaries, items for specific keys will be combined into a list, depending on the status of the overwrite= parameter. For lists, items will be appended and reduced to unique items. This method is meant to be analogous to set.union for composite objects.

Parameters:
  • other (composite) – Other composite object to union with.
  • recursive (bool) – Whether or not to perform the operation recursively, for all nested composite objects.
  • overwrite (bool) – Whether or not to overwrite entries with the same key in a nested dictionary.
update(other)

Update internal dictionary object. This is meant to be an analog for dict.update().

values()

Return keys for object, if they are available.

write(fh, pretty=True)

API niceness defaulting to composite.write_json().

write_json(fh, pretty=True)

Write composite object to file handle in JSON format.

Parameters:
  • fh (file) – File handle to write to.
  • pretty (bool) – Sort keys and indent in output.
write_yaml(fh)

Write composite object to file handle in YAML format.

Parameters:fh (file) – File handle to write to.

Filesystem Management

class gems.filetree(directory, ignore='^[._]', regex='.*')

Data structure for traversing directory structure and creating object for accessing relative file paths.

Parameters:
  • directory (str) – Directory to build filetree from.
  • ignore (str) – Regular expression with items to ignore. If you wish to recurse through all directories (including hidden directories), set ignore=None. By default, this is set to “^[._]” (i.e. any files beginning with “.” or “_”).

Example

>>> data = filetree('mydir')
>>> print data
mydir/
     one/
        two.txt
        three.json
    two/
        three/
              four.txt
        five six/
                 seven.txt
        eight.config
>>> print data.one['two.txt']
/full/path/to/mydir/one/two.txt
>>> print data.two.three['four.txt']
/full/path/to/mydir/two/three/four.txt
>>> print data.two['five six']['eight.config']
/full/path/to/mydir/two/five six/eight.config
__str__()

Note

This needs to be completed – print filetree

filelist()

Return list of files in filetree.

get(item)

Safe way to get items, similar to __dict__.get().

Parameters:item (str) – Item to get in file tree.
json()

Return JSON representation of object.

prune(regex='.*')

Prune leaves of filetree according to specified regular expression.

Parameters:regex (str) – Regular expression to use in pruning tree.