Functions to make working with data easier.
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
Add or subtract from the date passed
Parameters: |
|
---|---|
Return type: |
>>> 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
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
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: |
|
---|---|
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
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) |
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
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
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
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
Determines if the value passed is a tag
Parameters: |
|
---|---|
Return type: | bool |
>>> from chula import data
>>> print data.istag('foo')
True
>>> print data.istag('foo!!')
False
Determines if the value passed is a collection of tag.
Parameters: |
|
---|---|
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
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
Simple text replacement, using an string dictionary against a string.
Parameters: |
|
---|---|
Return type: | str |
>>> from chula import data
>>> print data.replace_all({'l':'1', 'o':'0'}, "Hello world")
He110 w0r1d
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
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 :)
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']
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: |
|
---|---|
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
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
Wrap a string with something
Parameters: |
|
---|---|
Return type: | str |
>>> from chula import data
>>> print data.wrap('sql', "'")
'sql'