zoom.utils module¶
zoom.utils
-
class
zoom.utils.Bunch(**kwargs)¶ Bases:
objecta handy bunch of variables
-
class
zoom.utils.Config(filename)¶ Bases:
objectConfig File Reader
A Config with a handy get method.
>>> config = Config(zoom.tools.zoompath('web','sites','default','site.ini')) >>> config.get('site', 'name') 'ZOOM'
>>> config.has_option('site', 'name') True
>>> config.get('site', 'size', 100) 100
>>> try: ... config.get('site', 'size') ... except (configparser.NoOptionError, configparser.NoSectionError): ... error = True >>> error True
-
get(section, option, default=None)¶ Get a config file value supplying an optional defalt value.
-
has_option(section, option)¶ Return True if config file option exists.
-
has_section(section)¶ Return True if config file section exists.
-
-
class
zoom.utils.DefaultRecord¶ Bases:
zoom.utils.RecordA Record with default values
>>> class Foo(DefaultRecord): pass >>> foo = Foo(name='Sam') >>> foo.name 'Sam' >>> foo.phone ''
-
class
zoom.utils.ItemList(*args, **kwargs)¶ Bases:
listlist of data items
>>> items = ItemList() >>> items.append(['Joe', 12, 125]) >>> items [['Joe', 12, 125]] >>> print(items) Column 0 Column 1 Column 2 -------- -------- -------- Joe 12 125
>>> items.insert(0, ['Name', 'Score', 'Points']) >>> print(items) Name Score Points ---- ----- ------ Joe 12 125
>>> data = [ ... ['Joe', 12, 125], ... ['Sally', 13, 1354], ... ] >>> items = ItemList(data) >>> print(items) Column 0 Column 1 Column 2 -------- -------- -------- Joe 12 125 Sally 13 1,354
>>> data = [ ... ['Joe', 12, 125], ... ['Sally', 13, 135], ... ] >>> items = ItemList(data, labels=['Name', 'Score', 'Points']) >>> print(items) Name Score Points ----- ----- ------ Joe 12 125 Sally 13 135
-
class
zoom.utils.OrderedSet(iterable=None)¶ Bases:
collections.abc.MutableSetA set that preserves the order of the elements
>>> s = OrderedSet('abracadaba') >>> t = OrderedSet('simsalabim') >>> print(s | t) OrderedSet(['a', 'b', 'r', 'c', 'd', 's', 'i', 'm', 'l']) >>> print(s & t) OrderedSet(['a', 'b']) >>> print(s - t) OrderedSet(['r', 'c', 'd']) >>> print(OrderedSet(reversed(s - t))) OrderedSet(['d', 'c', 'r']) >>> OrderedSet(['d', 'c', 'd']) == OrderedSet(['c', 'd', 'd']) False
credit: http://code.activestate.com/recipes/576694/ Licensed under MIT License
-
add(key)¶ add an item
>>> s = OrderedSet([1, 2, 3]) >>> s.add(4) >>> s OrderedSet([1, 2, 3, 4])
-
discard(key)¶ discard an item by key
>>> s = OrderedSet([1, 2, 3]) >>> s.discard(1) >>> s OrderedSet([2, 3])
-
pop(last=True)¶ pop an item
>>> s = OrderedSet([1, 2, 3]) >>> s.pop(2) 3 >>> s OrderedSet([1, 2])
-
-
class
zoom.utils.Record¶ Bases:
zoom.utils.StorageA dict with attribute access to items, attributes and properties
>>> class Foo(Record): ... full = property(lambda a: a.fname + ' ' + a.lname) ... >>> f = Foo(fname='Joe', lname='Smith') >>> f.full 'Joe Smith' >>> f['full'] 'Joe Smith' >>> 'The name is %(full)s' % f 'The name is Joe Smith' >>> print(f) Foo fname ...............: 'Joe' lname ...............: 'Smith' full ................: 'Joe Smith'
>>> f.attributes() ['fname', 'lname', 'full']
>>> class FooBar(Record): ... full = property(lambda a: a.fname + ' ' + a.lname) ... >>> o = FooBar(a=2) >>> kind(o) 'foo_bar' >>> o.a 2 >>> o['a'] 2 >>> o.double = property(lambda o: 2*o.a) >>> o.double 4 >>> o['double'] 4 >>> del o.a >>> print(o.a) None
>>> class Foo(Record): ... full = property(lambda a: a.fname + ' ' + a.lname) ... >>> f = Foo(fname='Joe', lname='Smith') >>> f.full 'Joe Smith' >>> f['full'] 'Joe Smith' >>> 'The name is %(full)s' % f 'The name is Joe Smith' >>> getattr(f,'full') 'Joe Smith'
>>> print(Foo(_id=1, fname='Jane', lname='Smith')) Foo fname ...............: 'Jane' lname ...............: 'Smith' full ................: 'Jane Smith'
>>> o = Record(a=2) >>> o.a 2 >>> o.valid() 1 >>> o.attributes() ['a'] >>> o['a'] 2 >>> o.double = property(lambda o: 2*o.a) >>> o.double 4 >>> o['double'] 4 >>> del o.a >>> o.a
-
allows(user, action)¶
-
attributes()¶
-
get(name, *default)¶ Return the value for key if key is in the dictionary, else default.
-
save()¶ save record
-
valid()¶
-
-
class
zoom.utils.RecordList(*a, **k)¶ Bases:
lista list of Records
-
class
zoom.utils.Storage¶ Bases:
dictA Storage object is like a dictionary except obj.foo can be used in addition to obj[‘foo’].
>>> o = Storage(a=1) >>> o.a 1 >>> o['a'] 1 >>> o.a = 2 >>> o['a'] 2 >>> del o.a >>> o.a
-
zoom.utils.dedup(seq)¶ Remove duplicates while retaining order
-
zoom.utils.dictify(item)¶ prepare an object for transmission by marshalling it’s members
Marshals only members that json can handle.
>>> class Thing(object): pass >>> pp(dictify(Bunch(name='Terry', age=21, funky_type=Thing()))) { "age": 21, "name": "Terry" }
-
zoom.utils.existing(path, subdir=None)¶ Returns existing directories only
-
zoom.utils.generate_key()¶ make a new key
>>> len(generate_key()) 40
-
zoom.utils.get_attributes(obj)¶
-
zoom.utils.get_config(filename)¶ load a config file into a Config object
>>> get_config('doesnt_exist.conf')
-
zoom.utils.id_for(*args)¶ Calculates a valid HTML tag id given an arbitrary string.
>>> id_for('Test 123') 'test-123' >>> id_for('New Record') 'new-record' >>> id_for('New "special" Record') 'new-special-record' >>> id_for("hi", "test") 'hi~test' >>> id_for("hi test") 'hi-test' >>> id_for("hi-test") 'hi-test' >>> id_for(1234) '1234' >>> id_for('this %$&#@^is##-$&*!it') 'this-is-it' >>> id_for('test-this') 'test-this'
-
zoom.utils.kind(o)¶ returns a suitable table name for an object based on the object class
-
zoom.utils.locate_config(filename='zoom.conf', start='.')¶ locate a config file
First look in the current directory or above and then look in the user root directory and above.
-
zoom.utils.matches(item, terms)¶ Returns True if an item matches search terms
-
zoom.utils.name_for(text)¶ Calculates a valid HTML field name given an arbitrary string.
>>> name_for('Test 123') 'test_123' >>> name_for('New Record') 'new_record' >>> name_for('New "special" Record') 'new_special_record' >>> name_for("hi test") 'hi_test' >>> name_for("hi-test") 'hi_test' >>> name_for(1234) '1234' >>> name_for('this %$&#@^is##-$&*!it') 'this_is_it' >>> name_for('test-this') 'test_this'
-
zoom.utils.parents(path)¶
-
zoom.utils.pp(obj)¶ pretty print an object
>>> obj = dict(name='Joe', age=25)
>>> pp(obj) { "age": 25, "name": "Joe" }
-
zoom.utils.pretty(obj)¶ return an object in a pretty form
>>> obj = dict(name='Joe', age=25) >>> pretty(obj) '{\n "age": 25,\n "name": "Joe"\n}'
-
zoom.utils.search(items, text)¶ Returns items that match search terms
>>> items = [ ... {'name': 'Terry', 'instrument': 'guitar, drums, picolo', 'age': 25}, ... {'name': 'Pat', 'instrument': 'drums, vocals', 'age': 29}, ... {'name': 'Francis', 'instrument': 'saxophone, piano', 'age': 35}, ... ]
>>> pp(list(search(items, 'drums'))) [ { "age": 25, "instrument": "guitar, drums, picolo", "name": "Terry" }, { "age": 29, "instrument": "drums, vocals", "name": "Pat" } ]
>>> pp(list(search(items, 'drums pat'))) [ { "age": 29, "instrument": "drums, vocals", "name": "Pat" } ]
>>> pp(list(search(items, ''))) [ { "age": 25, "instrument": "guitar, drums, picolo", "name": "Terry" }, { "age": 29, "instrument": "drums, vocals", "name": "Pat" }, { "age": 35, "instrument": "saxophone, piano", "name": "Francis" } ]
>>> pp(list(search(items, None))) [ { "age": 25, "instrument": "guitar, drums, picolo", "name": "Terry" }, { "age": 29, "instrument": "drums, vocals", "name": "Pat" }, { "age": 35, "instrument": "saxophone, piano", "name": "Francis" } ]
>>> sorted(list(search((list(item.values()) for item in items), '35'))[0], key=str) [35, 'Francis', 'saxophone, piano']
-
zoom.utils.sorted_column_names(names)¶
-
zoom.utils.trim(text)¶ Remove the left most spaces for markdown
>>> trim('remove right ') 'remove right'
>>> trim(' remove left') 'remove left'
>>> print(trim(' remove spaces\n from block\n of text')) remove spaces from block of text
>>> print( ... trim( ... ' \n' ... ' remove spaces\n' ... ' from block\n' ... ' of text\n' ... ' \n' ... '\n' ... ) ... ) remove spaces from block of text
>>> print(trim(' remove spaces\n from block\n of text\n ')) remove spaces from block of text
>>> print(trim(' remove spaces\n from block\n of text')) remove spaces from block of text
>>> print(trim('\n remove spaces\n from block\n of text')) remove spaces from block of text
>>> text = '\nremove spaces \n from block\nof text' >>> print('\n'.join(repr(t) for t in trim(text).splitlines())) 'remove spaces ' ' from block' 'of text'
>>> text = ( ... '\nremove spaces' ... '\n from block' ... ) >>> print(trim(text)) remove spaces from block