Various utilities

Property sugar

iktomi.utils.cached_property(method, name=None)

Turns decorated method into caching property (method is called once on first access to property).

iktomi.utils.cached_class_property(method, name=None)

Turns decorated method into caching class property (method is called once on first access to property of class or any of its instances).

Versioned Storage

Versioned storage, a classes for env and data objects.

class iktomi.utils.storage.VersionedStorage(cls=<class 'iktomi.utils.storage.StorageFrame'>, *args, **kwargs)

Storage implements state managing interface, allowing to safely set attributes for env and data objects.

Safity means that state of the storage is rolled back every time routing case returns ContinueRoute signal (None).

Be very accurate defining methods or properties for storage, choose correct method or property type depending on what do you want to achieve.

Regular methods will hold the state of storage frame they are added to. If you want to have an access to actual value, use storage property and method decorators.

as_dict()

Returns attributes of storage as dict

class iktomi.utils.storage.StorageFrame(_parent_storage=None, **kwargs)

A single frame in the storage

class iktomi.utils.storage.storage_property(method, name=None)

Turns decorated method into storage property (method is called with VersionedStorage as self).

class iktomi.utils.storage.storage_cached_property(method, name=None)

Turns decorated method into storage cached property (method is called only once with VersionedStorage as self).

iktomi.utils.storage.storage_method(func)

Calls decorated method with VersionedStorage as self

Internationalization

iktomi.utils.N_(msg)

Single translatable string marker. Does nothing, just a marker for *.pot file compilers.

Usage:

n = N_('translate me')
translated = env.gettext(n)
class iktomi.utils.M_(single, plural, count_field='count', format_args=None)

Marker for translatable string with plural form. Does not make a translation, just incapsulates a data about the translatable string.

Parameters:
  • single – a single form
  • plural – a plural form. Count can be included in %-format syntax
  • count_field – a key used to format

Usage:

message = M_(u'max length is %(max)d symbol',
             u'max length is %(max)d symbols',
             count_field="max")
m = message % {'max': 10}
trans = env.ngettext(m.single,
                     m.plural,
                     m.count
                     ) % m.format_args
count

A count based on count_field and format_args.

Paginator

Other

iktomi.utils.quoteattr(value)

Works like quoteattr from saxutils (returns escaped string in quotes), but is safe for HTML

iktomi.utils.quoteattrs(data)

Takes dict of attributes and returns their HTML representation

iktomi.utils.quote_js(text)

Quotes text to be used as JavaScript string in HTML templates. The result doesn’t contain surrounding quotes.

iktomi.utils.weakproxy(obj)

Safe version of weakref.proxy.

iktomi.utils.deprecation.deprecated(comment=None)

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used. Usage:

@deprecated()
def foo():
    pass

or:

@deprecated('Use bar() instead.')
def foo():
    pass
iktomi.utils.dt.strftime(dt, fmt)

strftime implementation working before 1900

Utils