atelier.utils

(This module’s source code is available here.)

Defines a series of utility classes and functions.

Functions

assert_pure(s) raise an Exception if the given string is not ispure().
confirm([prompt]) Ask for user confirmation from the console.
date_offset(ref[, days]) Compute a date using a “reference date” and an offset.
i2d(i) Convert int to date.
i2t(s) Convert int to time.
iif(condition, true_value, false_value) “Inline If” : an if statement as a function.
indentation(s) Examples:
ispure(s) Returns True if the specified string s is either a unicode string or contains only ASCII characters.
unindent(s) Reduces indentation of a docstring to the minimum.

Classes

AttrDict Dictionary-like helper object.
SubProcessParent Base class for atelier.test.TestCase.
class atelier.utils.AttrDict

Dictionary-like helper object.

Usage example:

>>> from atelier.utils import AttrDict
>>> a = AttrDict()
>>> a.define('foo',1)
>>> a.define('bar','baz',2)
>>> print a
{'foo': 1, 'bar': {'baz': 2}}
>>> print a.foo
1
>>> print a.bar.baz
2
>>> print a.resolve('bar.baz')
2
>>> print a.bar
{'baz': 2}
define(*args)

args must be a series of names followed by the value

resolve(name, default=None)

return an attribute with dotted name

class atelier.utils.SubProcessParent

Base class for atelier.test.TestCase. Also used standalone by lino.management.commands.makescreenshots.

build_environment()

Contructs and return a dict with the environment variables for the future subprocess.

open_subprocess(args, **kw)

Additional keywords will be passed to the Popen constructor. They can be e.g. cwd : the working directory

atelier.utils.assert_pure(s)

raise an Exception if the given string is not ispure().

atelier.utils.confirm(prompt=None)

Ask for user confirmation from the console.

atelier.utils.date_offset(ref, days=0, **offset)

Compute a date using a “reference date” and an offset.

>>> r = i2d(20140222)

In 10 days: >>> date_offset(r, 10) datetime.date(2014, 3, 4)

Four hundred days ago: >>> date_offset(r, -400) datetime.date(2013, 1, 18)

atelier.utils.i2d(i)

Convert int to date. Examples:

>>> i2d(20121224)
datetime.date(2012, 12, 24)
atelier.utils.i2t(s)

Convert int to time. Examples:

>>> i2t(815)
datetime.time(8, 15)
>>> i2t(1230)
datetime.time(12, 30)
>>> i2t(12)
datetime.time(12, 0)
>>> i2t(1)
datetime.time(1, 0)
atelier.utils.iif(condition, true_value, false_value)

“Inline If” : an if statement as a function.

Examples:

>>> import six
>>> from atelier.utils import iif
>>> six.print_("Hello, %s world!" % iif(1+1==2,"real","imaginary"))
Hello, real world!
atelier.utils.indentation(s)

Examples:

>>> from atelier.utils import indentation
>>> indentation("")
0
>>> indentation("foo")
0
>>> indentation(" foo")
1
atelier.utils.ispure(s)

Returns True if the specified string s is either a unicode string or contains only ASCII characters.

atelier.utils.unindent(s)

Reduces indentation of a docstring to the minimum. Empty lines don’t count.

Examples:

>>> from atelier.utils import unindent
>>> unindent('')
''
>>> print unindent('''
...   foo
...     foo
... ''')

foo
  foo
>>> print unindent('''
... foo
...     foo
... ''')

foo
    foo