Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
3Boolean Operations
4------------------
6is_bool
7.......
9Determine if a given value is a boolean at run time.
11.. code-block:: python
13 from superdjango.utils import is_bool
15 print(is_bool("yes"))
16 print(is_bool(True))
17 print(is_bool("No"))
18 print(is_bool(False))
20.. tip::
21 By default, a liberal number of values are used to test. If you *just* want ``True`` or ``False``, simply pass
22 ``(True, False)`` as ``test_values``.
24to_bool
25.......
27Convert a given value to it's boolean equivalent.
29.. code-block:: python
31 from superdjango.utils import to_bool
33 print(to_bool("yes"))
34 print(to_bool(1))
35 print(to_bool("no"))
36 print(to_bool(0))
38Note that an unrecognized value will raise a value error.
40.. code-block:: python
42 from superdjango.utils import to_bool
44 value = "not a boolean"
45 try:
46 print(to_bool(value))
47 except ValueError:
48 print('"%s" is not a boolean value.' % value)
50File Operations
51---------------
53copy_file
54.........
56Copy a file from one location to another.
58.. code-block:: python
60 from superdjango.utils import copy_file
62 copy_file("readme-template.txt", "path/to/project/readme.txt")
64copy_tree
65.........
67Recursively copy a source directory to a given destination.
69.. code-block:: python
71 from superdjango.utils import copy_tree
73 success = copy_tree("from/path", "to/path")
74 print(success)
76read_csv
77........
79Read the contents of a CSV file.
81.. code-block:: text
83 menu,identifier,sort_order,text,url
84 main,product,10,Product,/product/
85 main,solutions,20,Solutions,/solutions/
86 main,resources,30,Resources,/resources/
87 main,support,40,Support,https://support.example.com
88 main,about,50,About,/about/
89 main,contact,60,Contact,/contact/
91.. code-block:: python
93 from superdjango.utils import read_csv
95 rows = read_csv("path/to/menus.csv", first_row_fields_names=True)
96 for r in rows:
97 print("%s: %s" % (row['identifier'], row['url']
100read_file
101.........
103Read a file and return its contents.
105.. code-block:: python
107 from superdjango.utils import read_file
109 output = read_file("path/to/readme.txt")
110 print(output)
113write_file
114..........
116Write a file.
118.. code-block:: python
120 from superdjango.utils import write_file
122 write_file("path/to/readme.txt", "This is a test.")
124Math Helpers
125------------
127average
128.......
130Calculate the average of a given number of values, taking care to handle zero division.
132.. code-block:: python
134 from superdjango.utils import average
136 values = [1, 2, 3, 4, 5]
137 print(average(values))
139is_integer
140..........
142Indicates whether the given value is an integer. Saves a little typing.
144.. code-block:: python
146 from superdjango.utils import is_integer
148 print(is_integer(17))
149 print(is_integer(17.5))
150 print(is_integer("17"))
151 print(is_integer("17", cast=True))
154percentage
155..........
157Calculate the percentage that a portion makes up of a total.
159.. code-block:: python
161 from superdjango.utils import percentage
163 p = percentage(50, 100)
164 print(p + "%")
166String Operations
167-----------------
169base_convert
170............
172Convert a number between two bases of arbitrary digits.
174.. code-block:: python
176 from superdjango.utils import base_convert
178 print(base_convert(12345))
180camelcase_to_underscore
181.......................
183Convert a string from ``CamelCase`` to ``camel_case``.
185.. code-block:: python
187 from superdjango.utils import camelcase_to_underscore
189 model_name = "ProjectTasks"
190 print(camelcase_to_underscore(model_name))
192indent
193......
195Indent a string.
197.. code-block:: python
199 from superdjango.utils import indent
201 text = "This text will be indented."
202 print(indent(text))
204is_string
205.........
207Indicates whether the given value is a string. Saves a little typing.
209.. code-block:: python
211 from superdjango.utils import is_string
213 print(is_string("testing"))
214 print(is_string("17"))
215 print(is_string(17))
217strip_html_tags
218...............
220Strip HTML tags from a string.
222.. code-block:: python
224 from superdjango.utils import strip_html_tags
226 html = "<p>This string contains <b>HTML</b> tags.</p>"
227 print(strip_html_tags(html))
229truncate
230........
232Get a truncated version of a string if if over the limit.
234.. code-block:: python
236 from superdjango.utils import truncate
238 title = "This Title is Too Long to Be Displayed As Is"
239 print(truncate(title))
241underscore_to_camelcase
242.......................
244Convert a string from ``camel_case`` to ``CamelCase`` .
246.. code-block:: python
248 from superdjango.utils import underscore_to_camelcase
250 pattern_name = "project_detail"
251 print(underscore_to_camelcase(pattern_name))
253underscore_to_title_case
254........................
256Convert a string from ``under_score_case`` to ``Title Case``.
258.. code-block:: python
260 from superdjango.utils import underscore_to_title_case
262 pattern_name = "project_detail"
263 print(underscore_to_title_case(pattern_name))
265Others
266------
268The File Class
269..............
271The :py:class:`File` class is a simple helper for working with the various attributes of a given file path.
273For more robust handling of paths, see `pathlib`_.
275.. _pathlib: https://docs.python.org/3/library/pathlib.html
277.. code-block:: python
279 from superdjango.utils import File
281 f = File("/path/to/config.ini")
282 print("Path: %s" % f.path)
283 print("Directory: %s" % f.directory)
284 print("Name: %s" % f.name
285 print("Name Without Extension: %s" % f.basename)
286 print("Extension: %s" % f.extension)
288smart_cast
289..........
291Intelligently cast the given value to a Python data type.
293.. code-block:: python
295 from superdjango.utils import smart_cast
297 value = "123"
298 print(type(smart_cast(value)), smart_cast(value))
300 value = "yes"
301 print(type(smart_cast(value)), smart_cast(value))
303"""
304__author__ = "Shawn Davis <shawn@superdjango.com>"
305__maintainer__ = "Shawn Davis <shawn@superdjango.com>"
306__version__ = "0.16.0-d"
308from .library import *
310__all__ = (
311 "append_file",
312 "average",
313 "base_convert",
314 "camelcase_to_underscore",
315 "copy_file",
316 "copy_tree",
317 "indent",
318 "is_bool",
319 "is_integer",
320 "is_string",
321 "percentage",
322 "read_csv",
323 "read_file",
324 "smart_cast",
325 "strip_html_tags",
326 "to_bool",
327 "truncate",
328 "underscore_to_camelcase",
329 "underscore_to_title_case",
330 "write_file",
331 "File",
332)