~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/lazr/doc/utils.txt

  • Committer: William Grant
  • Date: 2011-12-30 02:21:12 UTC
  • mto: (14606.3.6 apocaremains)
  • mto: This revision was merged to the branch mainline in revision 14608.
  • Revision ID: william.grant@canonical.com-20111230022112-1b1pdbwt1suongbu
Drop fatsam.dia. It's more than 6 years out of date.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Various utility functions
 
2
=========================
 
3
 
 
4
 
 
5
camelcase_to_underscore_separated
 
6
---------------------------------
 
7
 
 
8
LAZR provides a way of converting TextThatIsWordSeparatedWithInterCaps
 
9
to text_that_is_word_separated_with_underscores.
 
10
 
 
11
    >>> from lazr.restful.utils import camelcase_to_underscore_separated
 
12
    >>> camelcase_to_underscore_separated('lowercase')
 
13
    'lowercase'
 
14
 
 
15
    >>> camelcase_to_underscore_separated('TwoWords')
 
16
    'two_words'
 
17
 
 
18
    >>> camelcase_to_underscore_separated('twoWords')
 
19
    'two_words'
 
20
 
 
21
    >>> camelcase_to_underscore_separated('ThreeLittleWords')
 
22
    'three_little_words'
 
23
 
 
24
    >>> camelcase_to_underscore_separated('UNCLE')
 
25
    'u_n_c_l_e'
 
26
 
 
27
    >>> camelcase_to_underscore_separated('_StartsWithUnderscore')
 
28
    '__starts_with_underscore'
 
29
 
 
30
 
 
31
safe_hasattr()
 
32
--------------
 
33
 
 
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.
 
37
 
 
38
    >>> from lazr.restful.utils import safe_hasattr
 
39
 
 
40
    >>> class Oracle(object):
 
41
    ...        @property
 
42
    ...        def is_full_moon(self):
 
43
    ...            return full_moon
 
44
    >>> oracle = Oracle()
 
45
    >>> hasattr(oracle, 'is_full_moon')
 
46
    False
 
47
 
 
48
    >>> safe_hasattr(oracle, 'is_full_moon')
 
49
    Traceback (most recent call last):
 
50
      ...
 
51
    NameError: global name 'full_moon' is not defined
 
52
 
 
53
    >>> full_moon = True
 
54
    >>> hasattr(oracle, 'is_full_moon')
 
55
    True
 
56
 
 
57
    >>> safe_hasattr(oracle, 'is_full_moon')
 
58
    True
 
59
 
 
60
    >>> hasattr(oracle, 'weather')
 
61
    False
 
62
 
 
63
    >>> safe_hasattr(oracle, 'weather')
 
64
    False
 
65
 
 
66
 
 
67
smartquote()
 
68
------------
 
69
 
 
70
smartquote() converts pairs of inch marks (") in a string to
 
71
typographical quotation marks.
 
72
 
 
73
    >>> from lazr.restful.utils import smartquote
 
74
    >>> smartquote('')
 
75
    u''
 
76
 
 
77
    >>> smartquote('foo "bar" baz')
 
78
    u'foo \u201cbar\u201d baz'
 
79
 
 
80
    >>> smartquote('foo "bar baz')
 
81
    u'foo \u201cbar baz'
 
82
 
 
83
    >>> smartquote('foo bar" baz')
 
84
    u'foo bar\u201d baz'
 
85
 
 
86
    >>> smartquote('""foo " bar "" baz""')
 
87
    u'""foo " bar "" baz""'
 
88
 
 
89
    >>> smartquote('" foo "')
 
90
    u'" foo "'
 
91
 
 
92
    >>> smartquote('"foo".')
 
93
    u'\u201cfoo\u201d.'
 
94
 
 
95
    >>> smartquote('a lot of "foo"?')
 
96
    u'a lot of \u201cfoo\u201d?'
 
97
 
 
98
 
 
99
safe_js_escape()
 
100
----------------
 
101
 
 
102
This will escape the given text so that it can be used in Javascript
 
103
code.
 
104
 
 
105
    >>> from lazr.restful.utils import safe_js_escape
 
106
    >>> print safe_js_escape('John "nasty" O\'Brien')
 
107
    "John "nasty" O'Brien"
 
108
 
 
109
    >>> print safe_js_escape("John O\'Brien")
 
110
    "John O'Brien"
 
111
 
 
112
    >>> print safe_js_escape("John <strong>O\'Brien</strong>")
 
113
    "John &lt;strong&gt;O'Brien&lt;/strong&gt;"
 
114