dk package¶
Subpackages¶
Submodules¶
dk.age module¶
Age (date subtraction) routines.
-
class
dk.age.
age
(dob, today=None)[source]¶ Bases:
object
The number of years, months, and days since date of birth.
-
dk.age.
birthday_this_year
(birthday, today=None)[source]¶ Return the date of the birthday in the current year.
-
dk.age.
next_birthday
(birthday, today=None)[source]¶ Return the date of the next birthday for someone born on date birthday.
dk.asciify module¶
Convert unicode strings to visually similar ascii representations.
-
dk.asciify.
ascii_name
(name)[source]¶ Convert name from unicode to a ascii representation that (while surely a grave bastardization) can be used as a filename without (ever!) causing problems.
-
dk.asciify.
asciify
(s, spaces=None, legal=None, replacement='')[source]¶ Convert unicode string s to a similarly looking ascii string.
If spaces is specified, runs of space characters are replaced with exactly one spaces.
If legal is specified (as a string), only characters from legal will be in the result, otherwise all characters from ascii 32 to ascii 127 are allowed.
If replacement is passed, then any characters that are elided will be replaced by replacement.
dk.dkimport module¶
Convenience function for importing a fqdn from a package. (to hide the baroque nature of __import__).
-
dk.dkimport.
defined_symbols
(module, attrfilter=None, itemfilter=None)[source]¶ Return symbols that are defined in module.
-
dk.dkimport.
dkimport
(name)[source]¶ Import and return the item specified by name:
Usage:
>>> item = dkimport('dk.core.dkimport.dkimport') >>> item.__name__ 'dkimport'
-
dk.dkimport.
dkimport_functions
(modname, **kw)[source]¶ Return all functions from all direct sub-modules of modname.
-
dk.dkimport.
dkimport_star
(modname, **kw)[source]¶ Import all names from module modname, similar to:
from modname import *
Available
**kw
arguments:- filefilter A function that receives a filename (with extension
- but without path) that should return True if the filename should be included.
useful for modularly implementing functionality, yet making it dynamically available from the top namespace:
...path/cmds/a.py def a(): ... ...path/cmds/b.py def b(): ... ...path/cmds/c.py def c(): ... ...path/cmds/__init__.py from dk.dkimport import dkimport_star as _dki for _item in _dki('..path.cmds'): if hasattr(_item, '__name__'): globals()[_item.__name__] = _item
dk.dklogger module¶
Convenience function for installing a module level logger:
from dk import dklogger
logger = dklogger.dklogger(__name__, debug=1, info=1)
logger.setLevel(dklogger.DEBUG)
to prevent logging to stdout, pass stream=None
dk.findapps module¶
Module for finding all apps folders.
dk.fstr module¶
dk.grid module¶
A 2D grid with slicing.
Usage:
>>> t = grid(emptyval=0)
>>> t.apply[:5,:5] = lambda v, (y,x):y*x
>>> t
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
>>> t.apply[:5,:5] = lambda v, p:v*2
>>> t
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
0 8 16 24 32
>>> t2 = grid.copy(t, lambda orig, (y,x):orig[y,x] / 2)
>>> t2
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16
-
class
dk.grid.
Empty
(emptyval=None)[source]¶ Bases:
dk.proxy.proxy
-
class
dk.grid.
grid
(rows=0, cols=0, emptyval=None)[source]¶ Bases:
object
This is a tabular object of two dimensions that supports slice notation.
-
Deleted
= <->¶
-
apply
¶
-
apply_iterator
()[source]¶ You can implement the game of life style actions with this iterator:
def average((y,x)): return sum(t[y-1:y+1, x-1:x+1]) / 9.0 t.apply[:2, :2] = lambda value, key: average(key)
-
columns
¶
-
height
¶
-
keys
¶
-
lastcol
¶
-
lastrow
¶
-
reversed
¶
-
rows
¶
-
size
¶
-
values
¶
-
width
¶
-
dk.iplist module¶
Collections of distinct ip-addresses.
dk.proxy module¶
Proxy class that forwards __special__ methods too.
dk.text module¶
-
dk.text.
u
(obj)¶ Return obj as a unicode string. If obj is a (non-)unicode string, then first try to decode it as utf-8, then as iso-8859-1.
-
dk.text.
u8
(obj)¶ Return a utf-8 representation of
obj
.
dk.utidy module¶
Micro tidy.
Usage:
>>> print utidy('''
... <form name="FirmaForm" id="FirmaForm" method="POST" autocomplete="off"
... action="." class="fForm"><input type="hidden" name="__cmd"
... value="FirmaForm"></form>hello
... ''')
...
<form action="." autocomplete="off" class="fForm" id="FirmaForm" method="POST" name="FirmaForm">
<input name="__cmd" type="hidden" value="FirmaForm">
</form>>
hello
Put tags without any nested children on one line, i.e. turn:
<h1> foo </h1>
into:
<h1>foo</h1>
dk.utils module¶
FIXME: many of these really should go in their own modules...
-
class
dk.utils.
Ordered
[source]¶ Bases:
dict
Mapping that maintains insertion order. (Should be removed and the adt versions should be used).
-
dk.utils.
dkpath
(pth=None)[source]¶ Usage
dkpath() => (w:)/xxxxxxx/ => (/home)/xxxxxxxx/ dkpath('app/') => ../xxxxxxxx/app/
-
dk.utils.
identity
(x)[source]¶ Return the argument unchanged. This function is often called identity in programming language and type theory (the type is t -> t, which turns out to be a difficult type for most classical static type systems).
-
dk.utils.
kronestring
(kr)[source]¶ Return a string version of the integer value
kr
, with space as the thousand-separator.
-
dk.utils.
lower_case
(s, encoding='u8')[source]¶ Return a lower cased (byte-)string version of
s
encoded inencoding
.
-
dk.utils.
mk_post
(model)[source]¶ Encode
model
(a dict-like object) into a dict where:- all values are strings
- None values are removed
- date values are expanded into year/month/day parts
- Note:: this function is superceeded by maint.client._encode_date
- which does this transparently for unit tests.
-
dk.utils.
normalize
(v)[source]¶ Return a string version of v such that
normalize(u) == normalize(v) iff not (u != v)e.g.:
normalize(None) == normalize(‘’) == normalize(u’‘)
-
dk.utils.
orestring
(n)[source]¶ Return a string version of the integer
øre
value. Either a two-digit string or a dash (as in 5,-).
-
dk.utils.
srcpath
(base, pth)[source]¶ Return an absolute path based on the relative path pth. Useful in tests, where we don’t know what the absolute path is, and we can’t use relative paths since we don’t know which folder the tests are run from.
In a test file xxxxxxx/foo/test/test_foo.py:
path = 'foo/test' fp = open(srcpath(path, 'data/testdata.txt'))
-
dk.utils.
title_case
(s, encoding='u8')[source]¶ Return a title cased (byte-)string version of
s
encoded inencoding
.
-
dk.utils.
title_case_lastname
(s, encoding='u8')[source]¶ Return a title cased version of
s
encoded inencoding
. If it looks likes
is already title cased, then leave it alone (in case of manual override and complex capitalization rules for last names).
-
dk.utils.
u
(obj)¶ Return obj as a unicode string. If obj is a (non-)unicode string, then first try to decode it as utf-8, then as iso-8859-1.
-
dk.utils.
u8
(obj)¶ Return a utf-8 representation of
obj
.
-
dk.utils.
unhtml
(s, toencoding=None)[source]¶ Convert charrefs for Norwegian vowels to their unicode counterparts.