chula.data – Data helper methods

Functions to make working with data easier.

chula.data.commaify(string)

Generate a number with commas for pretty printing

Parameters:string (int or str) – Data to be commaified
Return type:str
>>> from chula import data
>>> print data.commaify('45000000000')
45,000,000,000
chula.data.date_add(unit, delta, date)

Add or subtract from the date passed

Parameters:
  • unit (str) – Unit of measure (w, d, h, m, s)
  • delta (int) – Offset, amount to adjust the date by
  • date (datetime.datetime) – Date to be added/subtracted to/from
Return type:

datetime.datetime

>>> from chula import data
>>> start = data.str2date('1/1/2005 11:35')
>>> print data.date_add('days', -5, start)
2004-12-27 11:35:00
chula.data.date_diff(start, stop, unit='seconds')

Calculates the difference between two dates.

Parameters:
Return type:

int (defaults to seconds, if unit not passed)

>>> from chula import data
>>> start = data.str2date('1/1/2005')
>>> stop = data.str2date('1/5/2005')
>>> print data.date_diff(start, stop)
345600.0
>>> print data.date_diff(start, stop, 'd')
4.0
chula.data.date_within_range(time, offset, now=None)

The idea is to provide shorthand for “is foobar time within 02:00 + 30 min”. This can be useful for things that look for time periods when different logic applies, like from 2am and the next 30 minutes expect the network to be slow, as backups are taking place. Anything in the past is considered out of range.

Uncertain if this method should stay or be removed

Parameters:
  • time (str representation of time) – Representation of hours:minutes
  • offset (int) – The size of the range or window
  • now (datetime.datetime) – I{Optional} argument to specify time range/window start.
Return type:

bool

>>> from chula import data
>>> data.date_within_range('11:00', 30, data.str2date('1/1/2005 11:25'))
True
>>> data.date_within_range('11:00', 30, data.str2date('1/1/2005 11:35'))
False
chula.data.escape_for_js(string)

Clean the string for use within javascript

Parameters:string (str) – String to have illegal js chars escaped
Return type:str (safe for use in javascript)
chula.data.format_money(amount)

Format a numeric value into commaified dollars and cents (two digits)

Parameters:amount (float, int) – Money to be converted
Return type:str
>>> from chula import data
>>> print data.format_money(15000)
15,000.00
>>> print data.format_money(15000.100030)
15,000.10
chula.data.format_phone(string)

Format a string into a properly formatted telephone number. Accepts a few common patterns.

Parameters:string (str) – Telephone number to format
Return type:str formatted as: (area) exchange-number
>>> from chula import data
>>> print data.format_phone('555-123-1234')
(555) 123-1234
chula.data.isdate(string)

Determines if the value passed is a date.

Parameters:string (str) – Value to evaluate
Return type:bool
>>> from chula import data
>>> print data.isdate('1/1/2005')
True
>>> print data.isdate('1/41/2005')
False
chula.data.isregex(string)

Determines if the value passed is a valid regular rexpression

Parameters:string (str) – Value to evaluate
Return type:bool
>>> from chula import data
>>> print data.isregex(r'.*')
True
>>> print data.isregex(r'[')
False
chula.data.istag(string, regexp=None)

Determines if the value passed is a tag

Parameters:
  • string (str) – Value to evaluate
  • regexp (Valid regular expression str) – Alternate regex to chula.regex.TAG
Return type:

bool

>>> from chula import data
>>> print data.istag('foo')
True
>>> print data.istag('foo!!')
False
chula.data.istags(string, regexp=None)

Determines if the value passed is a collection of tag.

Parameters:
  • string (str) – Value to evaluate
  • regexp (Valid regular expression str) – Alternate regex to chula.regex.TAGS
Return type:

bool

>>> from chula import data
>>> print data.istags('foo bar')
True
>>> print data.istags('foo, bar')
True
>>> print data.istags('foo!! bar')
False
chula.data.none2empty(string)

Convert none to an empty string.

Parameters:string (str) – Value to evaluate
Return type:Empty str or value passed
>>> from chula import data
>>> print data.none2empty([1])
[1]
>>> if data.none2empty(None) == '':
...     print True
True
chula.data.replace_all(subs, string)

Simple text replacement, using an string dictionary against a string.

Parameters:
  • subs (dict) – Dictionary of find/replace values
  • string (str) – String to update
Return type:

str

>>> from chula import data
>>> print data.replace_all({'l':'1', 'o':'0'}, "Hello world")
He110 w0r1d
chula.data.str2bool(string)

Determine if the data passed is either True or False

Parameters:string (str) – Value to evaluate
Return type:bool
>>> from chula import data
>>> data.str2bool(True)
True
>>> data.str2bool('on')
True
chula.data.str2date(string)

Conversion from string to datetime object. Most of the common patterns are currently supported. If None is passed None will be returned

Parameters:string (str, or None) – Date time (of supported pattern)
Return type:datetime.datetime
>>> from chula import data
>>> print data.str2date("10/4/2005 21:45")
2005-10-04 21:45:00

Note

We highly recommend using http://niemeyer.net/python-dateutil instead :)

chula.data.str2tags(string)

Convert a string of tags into a sorted list of tags

Parameters:string (str) – List of comma or space delimited tags
Return type:list
>>> from chula import data
>>> print data.str2tags('linux good ms annoying linux good')
['annoying', 'good', 'linux', 'ms']
chula.data.str2unicode(retval, encoding='utf8', errors='ignore')

Convert a string into a unicode encoded string. If the character set is not specified, utf-8 will be used. If errors are encountered during conversion, by default they will be ignored. This means the invalid characters will be removed.

Parameters:
  • retval (str, unicode) – String
  • encoding (str) – Desired encoding, utf8 by default
  • errors – How to handle unicode conversion errors
Type :

str (valid values: ‘strict’, ‘replace’, ‘ignore’)

Return type:

str

>>> from chula import data
>>> print data.str2unicode('abc')
abc
>>> unicode('a').encode('utf-8') is data.str2unicode('a')
True
chula.data.tags2str(tags)

Convert a list of tags into a tsearch2 compliant string. Note that this string is not single quote padded for use with an sql insert statement. For that, pass the returened value to: db.ctags()

Parameters:tags (list) – List of valid tags
Return type:list
>>> from chula import data
>>> print data.tags2str(['annoying', 'good', 'linux', 'ms'])
annoying good linux ms
chula.data.wrap(string, wrap)

Wrap a string with something

Parameters:
  • string (str) – String to wrap
  • wrap (str) – String to wrap with
Return type:

str

>>> from chula import data
>>> print data.wrap('sql', "'")
'sql'

Previous topic

chula.config – Configuration

Next topic

chula.db.functions – Database helper functions

This Page