libbe.util.utility

Assorted utility functions that don’t fit in anywhere else.

class libbe.util.utility.Dir

A temporary directory for testing use.

Make sure you run cleanup() after you’re done using the directory.

Methods

cleanup()
cleanup()
exception libbe.util.utility.InvalidXML(type, element, error)

Invalid XML while parsing for a *.from_xml() method.

Parameters :

type : str

String identifying *, e.g. “bug”, “comment”, ...

element : ElementTree.Element

ElementTree.Element instance which caused the error.

error : str

Error description.

libbe.util.utility.RFC_2822_TIME_FMT = '%a, %d %b %Y %H:%M:%S +0000'

RFC 2822 [1] format string for time.strftime() and time.strptime().

[1]See RFC 2822, sections 3.3 and A.1.1.
libbe.util.utility.handy_time(time_val)

Convert a time number into a useful localtime.

Where time_to_str() returns GMT +0000, handy_time returns a string in local time. This may be more accessible for the user.

Parameters :

time_val : float

Float seconds since the Epoc, see time.time().

libbe.util.utility.iterable_full_of_strings(value, alternative=None)

Require an iterable full of strings.

This is useful, for example, in validating *.extra_strings. See libbe.bugdir.BugDir.extra_strings

Parameters :

value : list or None

The potential list of strings.

alternative :

Allow a default (e.g. None), such that:

iterable_full_of_strings(value=x, alternative=x) -> True

Examples

>>> iterable_full_of_strings([])
True
>>> iterable_full_of_strings(["abc", "def", u"hij"])
True
>>> iterable_full_of_strings(["abc", None, u"hij"])
False
>>> iterable_full_of_strings(None, alternative=None)
True
libbe.util.utility.search_parent_directories(path, filename)

Find the file (or directory) named filename in path or in any of path’s parents. For example:

search_parent_directories("/a/b/c", ".be")

will return the path to the first existing file from:

/a/b/c/.be
/a/b/.be
/a/.be
/.be

or None if none of those files exist.

libbe.util.utility.str_to_time(str_time)

Convert an RFC 2822-fomatted string into a time value.

Parameters :

str_time : str

An RFC 2822-formatted string.

See also

time_to_str
inverse

Examples

>>> str_to_time("Thu, 01 Jan 1970 00:00:00 +0000")
0
>>> q = time.time()
>>> str_to_time(time_to_str(q)) == int(q)
True
>>> str_to_time("Thu, 01 Jan 1970 00:00:00 -1000")
36000
libbe.util.utility.time_to_gmtime(str_time)

Convert an RFC 2822-fomatted string to a GMT string.

Parameters :

str_time : str

An RFC 2822-formatted string.

Examples

>>> time_to_gmtime("Thu, 01 Jan 1970 00:00:00 -1000")
'Thu, 01 Jan 1970 10:00:00 +0000'
libbe.util.utility.time_to_str(time_val)

Convert a time number into an RFC 2822-formatted string.

Parameters :

time_val : float

Float seconds since the Epoc, see time.time(). Note that while time_val may contain sub-second data, the output string will not.

See also

str_to_time
inverse
handy_time
localtime string

Examples

>>> time_to_str(0)
'Thu, 01 Jan 1970 00:00:00 +0000'
libbe.util.utility.underlined(string, char='=')

Produces a version of a string that is underlined.

Parameters :

string : str

The string to underline

char : str

The character to use for the underlining.

Examples

>>> underlined("Underlined String")
'Underlined String\n================='

This Page