1
Various utility functions
2
=========================
5
camelcase_to_underscore_separated
6
---------------------------------
8
LAZR provides a way of converting TextThatIsWordSeparatedWithInterCaps
9
to text_that_is_word_separated_with_underscores.
11
>>> from lazr.restful.utils import camelcase_to_underscore_separated
12
>>> camelcase_to_underscore_separated('lowercase')
15
>>> camelcase_to_underscore_separated('TwoWords')
18
>>> camelcase_to_underscore_separated('twoWords')
21
>>> camelcase_to_underscore_separated('ThreeLittleWords')
24
>>> camelcase_to_underscore_separated('UNCLE')
27
>>> camelcase_to_underscore_separated('_StartsWithUnderscore')
28
'__starts_with_underscore'
34
LAZR provides a safe_hasattr() that doesn't hide exception from the
35
caller. This behaviour of the builtin hasattr() is annoying because it
36
makes problems harder to diagnose.
38
>>> from lazr.restful.utils import safe_hasattr
40
>>> class Oracle(object):
42
... def is_full_moon(self):
45
>>> hasattr(oracle, 'is_full_moon')
48
>>> safe_hasattr(oracle, 'is_full_moon')
49
Traceback (most recent call last):
51
NameError: global name 'full_moon' is not defined
54
>>> hasattr(oracle, 'is_full_moon')
57
>>> safe_hasattr(oracle, 'is_full_moon')
60
>>> hasattr(oracle, 'weather')
63
>>> safe_hasattr(oracle, 'weather')
70
smartquote() converts pairs of inch marks (") in a string to
71
typographical quotation marks.
73
>>> from lazr.restful.utils import smartquote
77
>>> smartquote('foo "bar" baz')
78
u'foo \u201cbar\u201d baz'
80
>>> smartquote('foo "bar baz')
83
>>> smartquote('foo bar" baz')
86
>>> smartquote('""foo " bar "" baz""')
87
u'""foo " bar "" baz""'
89
>>> smartquote('" foo "')
92
>>> smartquote('"foo".')
95
>>> smartquote('a lot of "foo"?')
96
u'a lot of \u201cfoo\u201d?'
102
This will escape the given text so that it can be used in Javascript
105
>>> from lazr.restful.utils import safe_js_escape
106
>>> print safe_js_escape('John "nasty" O\'Brien')
107
"John "nasty" O'Brien"
109
>>> print safe_js_escape("John O\'Brien")
112
>>> print safe_js_escape("John <strong>O\'Brien</strong>")
113
"John <strong>O'Brien</strong>"