watson.filters.string

class watson.filters.string.Date(format='%Y-%m-%d')[source]

Formats a string representation of a date to a particular format.

Example:

filter = Date()
filter('2013-09-12')  # datetime object for that date
__init__(format='%Y-%m-%d')[source]

Initializes the filter.

Parameters:format (string) – The date format to make.
class watson.filters.string.HtmlEntities[source]

Encodes all html entities.

Example:

filter = HtmlEntities()
filter('<div>test</div>')  # &lt;div&gt;test&lt;/div&gt;
class watson.filters.string.Lower[source]

Converts all characters to lowercase.

Example:

filter = Lower()
filter('ABCD')  # abcd
class watson.filters.string.Numbers(regex='[^0-9]', replacement='', flags=0)[source]

Strips all characters except for numbers.

Example:

filter = Numbers()
filter('abcd1234')  # 1234
__init__(regex='[^0-9]', replacement='', flags=0)[source]
class watson.filters.string.RegEx(regex, replacement='', flags=0)[source]

Uses regular expressions to replace values.

Example:

filter = RegEx('ing', replacement='ed')
filter('Stopping')  # Stopped
__init__(regex, replacement='', flags=0)[source]

Initializes the filter.

Parameters:
  • regex (string|regex) – The pattern to match.
  • replacement (string) – The value to be used in the replacement.
  • flags (int) – The regex flags.
class watson.filters.string.StripTags(regex='</?\S([^=]*=(\s*"[^"]*"|\s*\'[^\']*\'|\S*)|[^>])*?>', flags=2)[source]

Strips all html tags.

Thanks to django for the regex used below.

Example:

filter = StripTags()
filter('test<div>blah</div>')  # testblah
__init__(regex='</?\\S([^=]*=(\\s*"[^"]*"|\\s*\\\'[^\\\']*\\\'|\\S*)|[^>])*?>', flags=2)[source]
class watson.filters.string.Trim[source]

Strips whitespace from value.

class watson.filters.string.Upper[source]

Converts all characters to uppercase.

Example:

filter = Upper()
filter('abcd')  # ABCD