for any info/changes follow me: @nickmilon

Hellas.Sparta module

Defines very basic constants, classes and methods named after Sparta . We keep this module simple and Laconic so No imports in this module

Hellas.Sparta.seconds_to_DHMS(seconds, as_str=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_str 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}
Hellas.Sparta.elapsed_seconds(dt_start, dt_end)[source]
exception Hellas.Sparta.Error[source]

Bases: exceptions.Exception

class Hellas.Sparta.DotDot[source]

Bases: dict

A dictionary that can handle dot notation to access its members (useful when parsing JSON content), although it can perform write operations using dot notation on single level dictionary its mainly use is for reads also 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 use for write operations on a nested key using dot notation i.e: del dd.a.b or dd.a.b = 1 or dd.a.b +=1 (it will fail silently !)

__setattr__

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

__delattr__

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

class Hellas.Sparta.DictDK(val)[source]

Bases: dict

a dictionary with a single predefined key subclasses can redefine the key as: >>> class FOO (DictDK): >>> key = ‘bar’ or create an instance with new default key: >>> f = type(‘FOO’, (DictDK,), dict(key=’foo’))

key = ''
__init__(val)[source]
Hellas.Sparta.dict_encode(in_dict)[source]

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

class Hellas.Sparta.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,... ]
class Hellas.Sparta.EnumLabels[source]

Bases: object

A simple class for enumerating labels to values descendants are easily auto documented with sphinx

Example:
>>> class EnumColors(EnumLabels):
>>>    RED = 1
>>>    GREEN = 2
>>> EnumColors.GREEN
2
classmethod value_name(value)[source]

Returns the label from a value if label exists otherwise returns the value since method does a reverse look up it is slow

Hellas.Sparta.relations_dict(rel_lst)[source]

constructs a relation’s dictionary from a list that describes amphidromus relations between objects

Parameters:

rel_lst (list) – a relationships list of the form [[a,b],[c, a, b]] # can include duplicates

Returns:

a dictionary

Example:
>>> rl = [('a', 'b', 'c'), ('a', 'x', 'y'), ('x', 'y', 'z')]
>>> relations_dict(rl)
{'a': ['x', 'c', 'b', 'y'], 'c': ['a', 'b'], 'b': ['a', 'c'], 'y': ['a', 'x', 'z'], 'x': ['a', 'z', 'y'],
 'z': ['y', 'x']}
Hellas.Sparta.chunks(sliceable, n)[source]

returns a list of lists of any sliceable object each of max lentgh n

Parameters:

-sliceable: (string|list|tuple) any sliceable object - n max elements of ech chunk

Example:
>>> chunksn([1,2,3,4,5,6,7,8,9,'x'], 4)
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 'x']]
>>> chunksn('123456789X', 3)
['123', '456', '789', 'X']
Hellas.Sparta.chunks_str(str, n, separator='\n', fill_blanks_last=True)[source]

returns lines with max n characters

Example:
>>> print (chunks_str('123456X', 3))
123
456
X
Hellas.Sparta.chunks_str_frame(a_str, n=None, center=True)[source]

places a frame around a string :Parameters:

  • a_str: string to frame
  • n: number of chars in each line
  • center: center string in frame if True and n > len(str)
Example:
>>> print(chunks_str_frame('the quick brown fox', 44))
╔════════════════════════════════════════════╗
║            the quick brown fox             ║
╚════════════════════════════════════════════╝
>>> print(chunks_str_frame('the quick brown fox',12, False))
╔════════════╗
║the quick br║
║own fox     ║
╚════════════╝
Hellas.Sparta.unicode_available()[source]

checks to see if unicode is available basically distinguishes between python 2.x and 3.x

Hellas.Sparta.unicode_or_str()[source]