for any info/changes follow me: @nickmilon

twtPyCurl.py.utilities module

some useful utilities used in multiple places

twtPyCurl.py.utilities.format_header(frmt)[source]

creates a header string from a new style format string useful when printing dictionaries

Parameters:frmt (str) – a new style format string
Returns:a table header string
assumptions for frmt specs:
  • all have a separator = ‘|’ and include a key size format directive i.e.: ‘{key:size}’
  • no other character allowed in frmt except separator
Example:
>>> frmt = '|{count:12,d}|{percent:7.2f}|{depth:5d}|'
>>> data_dict = {'count': 100, 'percent': 10.5, 'depth': 10}
>>> print(format_header(frmt)); print(frmt.format(**data_dict))
............................
|   count    |percent|depth|
............................
|         100|  10.50|   10|
class twtPyCurl.py.utilities.DotDot[source]

Bases: dict

A dictionary that can handle dot notation to access its members (useful when parsing JSON content), to keep casting to it cheap it doesn’t handle creating multilevel keys using dot notation. For this functionality look for easydict or addict

Example:
>>> dd = DotDot()
>>> dd.a = 1
>>> dd
{'a': 1}
>>> dd.b.c = 100
'AttributeError ...  '
>>> dd.b = {'b1': 21, 'b2': 22}
>>> dd.b.b3 = 23
>>> dd
{'a': 1, 'b': {'b1': 21, 'b2': 22}, 'b3': 23}

Warning

don’t try to delete a nested key using dot notation i.e: del dd.a.b.b1 (it will fail silently)

__setattr__

x.__setitem__(i, y) <==> x[i]=y

__delattr__

x.__delitem__(y) <==> del x[y]

class twtPyCurl.py.utilities.AdHocTree(parent=None, name='root')[source]

Bases: object

builds an arbitrary tree structure using object attributes

Usage:
>>> aht = AdHocTree().foo.bar
>>> aht
<AdHocTree: root/foo/bar>
    - can be extended:
>>> newtree = newtree = aht.new_foo.new_bar
>>> newtree
<AdHocTree: root/foo/bar/new_foo/new_bar>
__slots__ = ['parent', 'name']
__init__(parent=None, name='root')[source]
Parameters:
  • parent (obj) – parent object, defaults to None
  • name (str) – name of the Tree, defaults to root
parent
name
__call__(*args, **kwargs)[source]

calls _adHocCmd_ method on root’s parent if exists

__reduce__()[source]

its pickle-able

__iter__()[source]

iterates breadth-first up to root

path(separator='/')[source]
Returns:a string representing the path to root element separated by separator
root_and_path()[source]
Returns:a tuple (parent, [members,... ]
twtPyCurl.py.utilities.seconds_to_DHMS(seconds, as_string=True)[source]

converts seconds to Days, Hours, Minutes, Seconds

Parameters:
  • seconds (int) – number of seconds
  • as_string (bool) – to return a formated string defaults to True
Returns:

a formated string if as_string else a dictionary

Example:
>>> seconds_to_DHMS(60*60*24)
001-00:00:00
>>> seconds_to_DHMS(60*60*24, False)
{'hours': 0, 'seconds': 0, 'minutes': 0, 'days': 1}
twtPyCurl.py.utilities.dict_encode(in_dict)[source]

returns a new dictionary with encoded values useful for encoding http queries (python < 3)

twtPyCurl.py.utilities.dict_copy(a_dict, exclude_keys_lst=[], exclude_values_lst=[])[source]

a sallow copy of a dict excluding items in exclude_keys_lst and exclude_values_lst useful for copying locals etc...

Parameters:
  • a_dict (dict) – a dictionary
  • exclude_keys_lst (list) – a list of dictionary keys to exclude from copying
  • exclude_values_lst (list) – a list of dictionary values to exclude from copying
Returns:

a dictionary

Warning

remember returned dict it is NOT a deep copy of original