atelier.utils

Defines a series of utility classes and functions.

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

Functions

assert_pure(s)

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

cd(path)

confirm([prompt, default])

Ask for user confirmation from the console.

date_offset(ref[, days])

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

dict_py2(old_dict)

Convert the given dict so that it’s repr is the same for both Python 2 and 3.

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:

is_string(s)

Return True if the specified value is a string.

isidentifier(s)

Check whether the given string can be used as a Python identifier.

isiterable(x)

Returns True if the specified object is iterable.

ispure(s)

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

last_day_of_month(d)

Return the last day of the month of the given date.

list_py2(old_list)

Convert the given list so that it’s repr is the same for both Python 2 and 3.

rmu(x)

Remove the ‘u’ prefix from unicode strings under Python 2 in order to produce Python 3 compatible output in a doctested code snippet.

sixprint(*args)

Like print, but simulating PY3 output under PY2.

tuple_py2(old_tuple)

Convert the given tuple so that it’s repr is the same for both Python 2 and 3.

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

Bases: dict

Dictionary-like helper object.

Usage example:

>>> from atelier.utils import AttrDict
>>> a = AttrDict()
>>> a.define('foo', 1)
>>> a.define('bar', 'baz', 2)
>>> a == {"bar": {"baz": 2}, "foo": 1}
True
>>> 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

atelier.utils.iif(condition, true_value, false_value=None)

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

Examples:

>>> from atelier.utils import iif
>>> print("Hello, %s world!" % iif(1+1==2, "real", "imaginary"))
Hello, real world!
>>> iif(True, "true")
'true'
>>> iif(False, "true")
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.last_day_of_month(d)

Return the last day of the month of the given date.

>>> from atelier.utils import i2d
>>> last_day_of_month(i2d(20160212))
datetime.date(2016, 2, 29)
>>> last_day_of_month(i2d(20161201))
datetime.date(2016, 12, 31)
>>> last_day_of_month(i2d(20160123))
datetime.date(2016, 1, 31)
>>> last_day_of_month(i2d(20161123))
datetime.date(2016, 11, 30)

Thanks to stackoverflow.com.

atelier.utils.isiterable(x)

Returns True if the specified object is iterable.

atelier.utils.is_string(s)

Return True if the specified value is a string.

atelier.utils.isidentifier(s)

Check whether the given string can be used as a Python identifier.

atelier.utils.ispure(s)

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

atelier.utils.assert_pure(s)

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

atelier.utils.confirm(prompt=None, default='y')

Ask for user confirmation from the console.

atelier.utils.indentation(s)

Examples:

>>> from atelier.utils import indentation
>>> indentation("")
0
>>> indentation("foo")
0
>>> indentation(" foo")
1
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
class atelier.utils.SubProcessParent

Bases: object

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

run_subprocess(args, **kw)

Run a subprocess, wait until it terminates, fail if the returncode is not 0.

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.dict_py2(old_dict)

Convert the given dict so that it’s repr is the same for both Python 2 and 3.

Deprecated. Use rmu() instead.

atelier.utils.list_py2(old_list)

Convert the given list so that it’s repr is the same for both Python 2 and 3.

Deprecated. Use rmu() instead.

atelier.utils.tuple_py2(old_tuple)

Convert the given tuple so that it’s repr is the same for both Python 2 and 3.

Deprecated. Use rmu() instead.

atelier.utils.rmu(x)

Remove the ‘u’ prefix from unicode strings under Python 2 in order to produce Python 3 compatible output in a doctested code snippet.

>>> lst = [123, "123", u"Äöü"]
>>> print(rmu(lst))
[123, '123', '\xc4\xf6\xfc']
>>> print(rmu(tuple(lst)))
(123, '123', '\xc4\xf6\xfc')
>>> dct = {i: i for i in lst}
>>> print(rmu(dct)) 
{...'\xc4\xf6\xfc': '\xc4\xf6\xfc'...}
atelier.utils.sixprint(*args)

Like print, but simulating PY3 output under PY2.