Welcome | Get started | Dive into Lino | Contribute | Topics | Reference | More

lino.utils

lino.utils (the top-level module) contains a few often-used function for general use. It has also many subpackages and submodules.

addressable

See The lino.utils.addressable module.

ajax

This middleware is automatically being installed on every Lino site.

choosers

Extends the possibilities for defining choices for fields of a Django model.

code

Defines some utilities to inspect Python code.

config

This defines the ConfigDirCache which Lino instantiates and installs as SITE.confdirs.

cycler

Turns a list of items into an endless loop.

dates

Some general utilities for dates.

daemoncommand

Copyright (c) 2009, Sean Creeley All rights reserved.

dataserializer

YAML serializer.

dbfreader

What's the format of a Clipper .dbf file? http://www.the-oasis.net/clipper-12.html#ss12.4

dblogger

Just a shortcut and encapsulation. Instead of writing::.

diag

Some diagnostic utilities.

djangotest

Two TestCase classes for writing tests be run using Django's test runner (i.e. manage.py test).

dpy

Defines Lino's Python serializer and deserializer.

html2odf

This module contains mainly a utility function html2odf() which converts an ElementTree object generated using etgen.html to a fragment of ODF.

html2xhtml

Defines the html2xhtml() function which converts HTML to valid XHTML.

mytidylib

This is used only when libtidy is not available.

instantiator

Defines the Instantiator class and some other utilities used for generating database objects in python fixtures.

jinja

This defines the Counter class, a utility used in Jinja templates to generate self-incrementing counters for sections, subsections and any other sequences.

jscompressor

http://code.activestate.com/recipes/496882/ Author: Michael Palmer 13 Jul 2006 a regex-based JavaScript code compression kludge

jsgen

A framework for generating Javascript from Python.

latex

Based on http://www.djangosnippets.org/snippets/102/

mdbtools

This is for writing fixtures that import data from an MS-Access database (.mdb) into Lino.

media

Defines the MediaFile class.

mldbc

Generic support for Multilingual database content.

mti

A collection of tools around multi-table inheritance.

odsreader

OdsReader uses odfpy to extract data from an .ods document (OpenOffice.org spreadsheet).

pdf

pythontest

An extended TestCase for the plain python tests of a Lino project.

pyuca

Preliminary implementation of the Unicode Collation Algorithm.

quantities

See Quantities.

ranges

The lino.utils.ranges module contains utility methods for working with "ranges".

requests

restify

Just a copy & paste of the docutils.examples module (as instructed there).

screenshots

sqllog

A middleware for sending SQL statements to the Lino logger.

ssin

See ssin : Belgian national identification numbers.

test

Defines some TestCase classes that are meant for writing general Python test cases (not Django test cases, which are defined in lino.utils.djangotest).

textfields

This is taken from Helio Perroni Filho's answer at https://stackoverflow.com/questions/328356/extracting-text-from-html-file-using-python/3987802#3987802

ucsv

report

Functions

buildurl(root, *args, **kw)

call_on_bases(cls, name, *args, **kw)

Doesn't work.

call_optional_super(cls, self, name, *args, **kw)

Doesn't work.

camelize(s)

Convert the given string into a camel-case string.

curry(func, *args, **kw)

d2iso(d)

Supports also dates before 1900.

date_offset(ref[, days])

Compute a date using a "reference date" and an offset.

get_class_attr(cl, name)

get_client_ip_address(request)

hex2str(value)

Convert the hexadecimal representation of a string to the original string.

html2text(html, **kwargs)

iif(condition, true_value[, false_value])

"Inline If" : an if statement as a function.

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.

join_words(*words)

Remove any empty item (None or ''), call unicode on each and join the remaining word using a single space.

last_day_of_month(d)

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

logging_disabled(level)

moneyfmt(value[, places, curr, sep, dp, ...])

Convert Decimal to a money formatted string.

puts(s)

A simplistic replacement for the puts function of clint which has the problem of not supporting unicode strings.

read_exception(excinfo)

str2hex(s)

Convert a string to its hexadecimal representation.

uncamel(s)

Thanks to nickl in Stackoverflow

unicode_string(x)

When we want unicode strings (e.g. translated exception messages) to appear in an Exception, we must first encode them using a non-strict errorhandler.

workdays(start, end)

Return the number of workdays (Monday to Friday) between the given two dates.

Classes

AttrDict

Dictionary-like helper object.

IncompleteDate(year, month, day)

Naive representation of a potentially incomplete gregorian date.

MissingRow(msg)

Represents a database row that is expected to exist but doesn't.

SimpleSingleton(*args, **kwargs)

SumCollector()

A dictionary of sums to be collected using an arbitrary key.

class lino.utils.AttrDict

Bases: dict

Dictionary-like helper object.

See usage example in Lino utilities

define(*args)

args must be a series of names followed by the value

resolve(name, default=None)

return an attribute with dotted name

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

Compute a date using a "reference date" and an offset.

Usage examples in Manipulating dates and time.

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

"Inline If" : an if statement as a function.

Examples in Lino utilities.

lino.utils.last_day_of_month(d)

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

Examples in Manipulating dates and time.

Thanks to stackoverflow.com.

lino.utils.isiterable(x)

Returns True if the specified object is iterable.

lino.utils.is_string(s)

Return True if the specified value is a string.

TODO: remove this.

lino.utils.isidentifier(s)

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

TODO: remove this.

lino.utils.join_words(*words)

Remove any empty item (None or ''), call unicode on each and join the remaining word using a single space.

TODO: move this to etgen.html ?

See examples in Lino utilities.

lino.utils.d2iso(d)

Supports also dates before 1900.

lino.utils.call_optional_super(cls, self, name, *args, **kw)

Doesn't work. See 20110914.

lino.utils.call_on_bases(cls, name, *args, **kw)

Doesn't work. See 20110914. This is necessary because we want to call setup_report on the model and all base classes of the model. We cannot use super() for this because the setup_report method is optional.

lino.utils.str2hex(s)

Convert a string to its hexadecimal representation.

See examples in About formatting.

lino.utils.hex2str(value)

Convert the hexadecimal representation of a string to the original string.

See also str2hex().

Examples in About formatting.

class lino.utils.IncompleteDate(year, month, day)

Bases: object

Naive representation of a potentially incomplete gregorian date.

Where incomplete means that maybe only the year is known and day and month is unknown or similarly otherwise. Such a date may not always be convertible to datetime.date.

Some examples are in Manipulating dates and time.

classmethod parse(s)

Parse the given string and return an IncompleteDate object.

Examples in Manipulating dates and time.

get_age(today)

Return age in years as integer.

lino.utils.moneyfmt(value, places=2, curr='', sep=',', dp='.', pos='', neg='-', trailneg='')

Convert Decimal to a money formatted string.

places: required number of places after the decimal point
curr: optional currency symbol before the sign (may be blank)
sep: optional grouping separator (comma, period, space, or blank)
dp: decimal point indicator (comma or period)
only specify as blank when places is zero
pos: optional sign for positive numbers: '+', space or blank
neg: optional sign for negative numbers: '-', '(', space or blank
trailneg: optional trailing minus indicator: '-', ')', space or blank

See examples in About formatting.

lino.utils.unicode_string(x)

When we want unicode strings (e.g. translated exception messages) to appear in an Exception, we must first encode them using a non-strict errorhandler. Because the message of an Exception may not be a unicode string.

lino.utils.workdays(start, end)

Return the number of workdays (Monday to Friday) between the given two dates. Is not aware of holidays.

Both dates start and end are included. For example if you specify a Monday as start and Monday of the following week as end, then you get 6 (not 5). See examples in: Manipulating dates and time.

lino.utils.camelize(s)

Convert the given string into a camel-case string.

Usage examples in About formatting.

lino.utils.uncamel(s)

Thanks to nickl in Stackoverflow

Usage examples in About formatting.

lino.utils.puts(s)

A simplistic replacement for the puts function of clint which has the problem of not supporting unicode strings.

This method is meant for issuing to the interactive console messages which do not need to be logged because they just give information about what's going on.

Currently this just prints the string to stdout using print. I prefer to use this over a plain print statement because I guess that there will be problems (mainly thinking about the fact that writing to stdout is considered an error in a wsgi application).

class lino.utils.SumCollector

Bases: object

A dictionary of sums to be collected using an arbitrary key.

This is also included in the default context used by the Jinja renderer (lino.modlib.jinja) when rendering templates, which makes it a more complete solution for a problem asked also elsewhere, e.g. on Stackoverflow.

See examples in Lino utilities.

collect(k, value)

Add the given value to the sum at the given key k.

class lino.utils.MissingRow(msg)

Bases: object

Represents a database row that is expected to exist but doesn't.

Used for example by lino.core.choicelists.PointingChoice, or internally for reporting why the master instance for an action request could not be found.