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}
-
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
= ''¶
-
-
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
¶
-
-
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
-
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 ║ ╚════════════╝