~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Jeroen Vermeulen
  • Date: 2011-09-19 06:57:55 UTC
  • mto: This revision was merged to the branch mainline in revision 13994.
  • Revision ID: jeroen.vermeulen@canonical.com-20110919065755-lgot1hi4xfqrf492
Lint.  Lots of lint.

Show diffs side-by-side

added added

removed removed

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