~leonardr/lazr.restful/use-bleedthrough

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Various utility functions
*************************

implement_from_dict
===================

This function takes an interface and a dictionary, and returns a class
that implements as much of the interface as possible.

    >>> from zope.schema import TextLine
    >>> from zope.interface import Interface
    >>> from StringIO import StringIO
    >>> class ITwoFields(Interface):
    ...     field_1 = TextLine(title=u"field 1", default=u"field_1 default")
    ...     field_2 = TextLine(title=u"field 2")
    ...     def a_method():
    ...         pass

Values present in the dictionary become methods and attributes of the
implementing class.

    >>> from lazr.restful.utils import implement_from_dict
    >>> def result(self):
    ...     return "result"
    >>> implementation = implement_from_dict(
    ...     'TwoFields', ITwoFields,
    ...     {'field_1': 'foo', 'field_2': 'bar', 'a_method': result})

    >>> print implementation.__name__
    TwoFields
    >>> ITwoFields.implementedBy(implementation)
    True
    >>> print implementation.field_1
    foo
    >>> print implementation.field_2
    bar
    >>> print implementation().a_method()
    result

If one of the interface's attributes is not defined in the dictionary,
but the interface's definition of that attribute includes a default
value, the generated class will define an attribute with the default
value.

    >>> implementation = implement_from_dict(
    ...     'TwoFields', ITwoFields, {})

    >>> print implementation.field_1
    field_1 default

If an attribute is not present in the dictionary and its interface
definition does not include a default value, the generated class will
not define that attribute.

    >>> implementation.field_2
    Traceback (most recent call last):
    ...
    AttributeError: type object 'TwoFields' has no attribute 'field_2'

    >>> implementation.a_method
    Traceback (most recent call last):
    ...
    AttributeError: type object 'TwoFields' has no attribute 'a_method'

The 'superclass' argument lets you specify a superclass for the
generated class.

    >>> class TwoFieldsSuperclass(object):
    ...     def a_method(self):
    ...         return "superclass result"

    >>> implementation = implement_from_dict(
    ...     'TwoFields', ITwoFields, {}, superclass=TwoFieldsSuperclass)

    >>> print implementation().a_method()
    superclass result

make_identifier_safe
====================

LAZR provides a way of converting an arbitrary string into a similar
string that can be used as a Python identifier.

    >>> from lazr.restful.utils import make_identifier_safe
    >>> print make_identifier_safe("already_a_valid_IDENTIFIER_444")
    already_a_valid_IDENTIFIER_444

    >>> print make_identifier_safe("!starts_with_punctuation")
    _starts_with_punctuation

    >>> print make_identifier_safe("_!contains!pu-nc.tuation")
    __contains_pu_nc_tuation

    >>> print make_identifier_safe("contains\nnewline")
    contains_newline

    >>> print make_identifier_safe("")
    _

    >>> print make_identifier_safe(None)
    Traceback (most recent call last):
    ...
    ValueError: Cannot make None value identifier-safe.

camelcase_to_underscore_separated
=================================

LAZR provides a way of converting TextThatIsWordSeparatedWithInterCaps
to text_that_is_word_separated_with_underscores.

    >>> from lazr.restful.utils import camelcase_to_underscore_separated
    >>> camelcase_to_underscore_separated('lowercase')
    'lowercase'
    >>> camelcase_to_underscore_separated('TwoWords')
    'two_words'
    >>> camelcase_to_underscore_separated('twoWords')
    'two_words'
    >>> camelcase_to_underscore_separated('ThreeLittleWords')
    'three_little_words'
    >>> camelcase_to_underscore_separated('UNCLE')
    'u_n_c_l_e'
    >>> camelcase_to_underscore_separated('_StartsWithUnderscore')
    '__starts_with_underscore'

safe_hasattr()
==============

LAZR provides a safe_hasattr() that doesn't hide exception from the
caller. This behaviour of the builtin hasattr() is annoying because it
makes problems harder to diagnose.

    >>> from lazr.restful.utils import safe_hasattr

    >>> class Oracle(object):
    ...     @property
    ...     def is_full_moon(self):
    ...         return full_moon
    >>> oracle = Oracle()
    >>> hasattr(oracle, 'is_full_moon')
    False
    >>> safe_hasattr(oracle, 'is_full_moon')
    Traceback (most recent call last):
      ...
    NameError: global name 'full_moon' is not defined

    >>> full_moon = True
    >>> hasattr(oracle, 'is_full_moon')
    True
    >>> safe_hasattr(oracle, 'is_full_moon')
    True

    >>> hasattr(oracle, 'weather')
    False
    >>> safe_hasattr(oracle, 'weather')
    False

smartquote()
============

smartquote() converts pairs of inch marks (") in a string to typographical
quotation marks.

    >>> from lazr.restful.utils import smartquote
    >>> smartquote('')
    u''
    >>> smartquote('foo "bar" baz')
    u'foo \u201cbar\u201d baz'
    >>> smartquote('foo "bar baz')
    u'foo \u201cbar baz'
    >>> smartquote('foo bar" baz')
    u'foo bar\u201d baz'
    >>> smartquote('""foo " bar "" baz""')
    u'""foo " bar "" baz""'
    >>> smartquote('" foo "')
    u'" foo "'
    >>> smartquote('"foo".')
    u'\u201cfoo\u201d.'
    >>> smartquote('a lot of "foo"?')
    u'a lot of \u201cfoo\u201d?'

safe_js_escape()
================

This will escape the given text so that it can be used in Javascript
code.

    >>> from lazr.restful.utils import safe_js_escape
    >>> print safe_js_escape('John "nasty" O\'Brien')
    "John "nasty" O'Brien"
    >>> print safe_js_escape("John O\'Brien")
    "John O'Brien"
    >>> print safe_js_escape("John <strong>O\'Brien</strong>")
    "John &lt;strong&gt;O'Brien&lt;/strong&gt;"