Hide keyboard shortcuts

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""" 

2 

3Boolean Operations 

4------------------ 

5 

6is_bool 

7....... 

8 

9Determine if a given value is a boolean at run time. 

10 

11.. code-block:: python 

12 

13 from superdjango.utils import is_bool 

14 

15 print(is_bool("yes")) 

16 print(is_bool(True)) 

17 print(is_bool("No")) 

18 print(is_bool(False)) 

19 

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``. 

23 

24to_bool 

25....... 

26 

27Convert a given value to it's boolean equivalent. 

28 

29.. code-block:: python 

30 

31 from superdjango.utils import to_bool 

32 

33 print(to_bool("yes")) 

34 print(to_bool(1)) 

35 print(to_bool("no")) 

36 print(to_bool(0)) 

37 

38Note that an unrecognized value will raise a value error. 

39 

40.. code-block:: python 

41 

42 from superdjango.utils import to_bool 

43 

44 value = "not a boolean" 

45 try: 

46 print(to_bool(value)) 

47 except ValueError: 

48 print('"%s" is not a boolean value.' % value) 

49 

50File Operations 

51--------------- 

52 

53copy_file 

54......... 

55 

56Copy a file from one location to another. 

57 

58.. code-block:: python 

59 

60 from superdjango.utils import copy_file 

61 

62 copy_file("readme-template.txt", "path/to/project/readme.txt") 

63 

64copy_tree 

65......... 

66 

67Recursively copy a source directory to a given destination. 

68 

69.. code-block:: python 

70 

71 from superdjango.utils import copy_tree 

72 

73 success = copy_tree("from/path", "to/path") 

74 print(success) 

75 

76read_csv 

77........ 

78 

79Read the contents of a CSV file. 

80 

81.. code-block:: text 

82 

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/ 

90 

91.. code-block:: python 

92 

93 from superdjango.utils import read_csv 

94 

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'] 

98 

99 

100read_file 

101......... 

102 

103Read a file and return its contents. 

104 

105.. code-block:: python 

106 

107 from superdjango.utils import read_file 

108 

109 output = read_file("path/to/readme.txt") 

110 print(output) 

111 

112 

113write_file 

114.......... 

115 

116Write a file. 

117 

118.. code-block:: python 

119 

120 from superdjango.utils import write_file 

121 

122 write_file("path/to/readme.txt", "This is a test.") 

123 

124Math Helpers 

125------------ 

126 

127average 

128....... 

129 

130Calculate the average of a given number of values, taking care to handle zero division. 

131 

132.. code-block:: python 

133 

134 from superdjango.utils import average 

135 

136 values = [1, 2, 3, 4, 5] 

137 print(average(values)) 

138 

139is_integer 

140.......... 

141 

142Indicates whether the given value is an integer. Saves a little typing. 

143 

144.. code-block:: python 

145 

146 from superdjango.utils import is_integer 

147 

148 print(is_integer(17)) 

149 print(is_integer(17.5)) 

150 print(is_integer("17")) 

151 print(is_integer("17", cast=True)) 

152 

153 

154percentage 

155.......... 

156 

157Calculate the percentage that a portion makes up of a total. 

158 

159.. code-block:: python 

160 

161 from superdjango.utils import percentage 

162 

163 p = percentage(50, 100) 

164 print(p + "%") 

165 

166String Operations 

167----------------- 

168 

169base_convert 

170............ 

171 

172Convert a number between two bases of arbitrary digits. 

173 

174.. code-block:: python 

175 

176 from superdjango.utils import base_convert 

177 

178 print(base_convert(12345)) 

179 

180camelcase_to_underscore 

181....................... 

182 

183Convert a string from ``CamelCase`` to ``camel_case``. 

184 

185.. code-block:: python 

186 

187 from superdjango.utils import camelcase_to_underscore 

188 

189 model_name = "ProjectTasks" 

190 print(camelcase_to_underscore(model_name)) 

191 

192indent 

193...... 

194 

195Indent a string. 

196 

197.. code-block:: python 

198 

199 from superdjango.utils import indent 

200 

201 text = "This text will be indented." 

202 print(indent(text)) 

203 

204is_string 

205......... 

206 

207Indicates whether the given value is a string. Saves a little typing. 

208 

209.. code-block:: python 

210 

211 from superdjango.utils import is_string 

212 

213 print(is_string("testing")) 

214 print(is_string("17")) 

215 print(is_string(17)) 

216 

217strip_html_tags 

218............... 

219 

220Strip HTML tags from a string. 

221 

222.. code-block:: python 

223 

224 from superdjango.utils import strip_html_tags 

225 

226 html = "<p>This string contains <b>HTML</b> tags.</p>" 

227 print(strip_html_tags(html)) 

228 

229truncate 

230........ 

231 

232Get a truncated version of a string if if over the limit. 

233 

234.. code-block:: python 

235 

236 from superdjango.utils import truncate 

237 

238 title = "This Title is Too Long to Be Displayed As Is" 

239 print(truncate(title)) 

240 

241underscore_to_camelcase 

242....................... 

243 

244Convert a string from ``camel_case`` to ``CamelCase`` . 

245 

246.. code-block:: python 

247 

248 from superdjango.utils import underscore_to_camelcase 

249 

250 pattern_name = "project_detail" 

251 print(underscore_to_camelcase(pattern_name)) 

252 

253underscore_to_title_case 

254........................ 

255 

256Convert a string from ``under_score_case`` to ``Title Case``. 

257 

258.. code-block:: python 

259 

260 from superdjango.utils import underscore_to_title_case 

261 

262 pattern_name = "project_detail" 

263 print(underscore_to_title_case(pattern_name)) 

264 

265Others 

266------ 

267 

268The File Class 

269.............. 

270 

271The :py:class:`File` class is a simple helper for working with the various attributes of a given file path. 

272 

273For more robust handling of paths, see `pathlib`_. 

274 

275.. _pathlib: https://docs.python.org/3/library/pathlib.html 

276 

277.. code-block:: python 

278 

279 from superdjango.utils import File 

280 

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) 

287 

288smart_cast 

289.......... 

290 

291Intelligently cast the given value to a Python data type. 

292 

293.. code-block:: python 

294 

295 from superdjango.utils import smart_cast 

296 

297 value = "123" 

298 print(type(smart_cast(value)), smart_cast(value)) 

299 

300 value = "yes" 

301 print(type(smart_cast(value)), smart_cast(value)) 

302 

303""" 

304__author__ = "Shawn Davis <shawn@superdjango.com>" 

305__maintainer__ = "Shawn Davis <shawn@superdjango.com>" 

306__version__ = "0.16.0-d" 

307 

308from .library import * 

309 

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)