PaferaPy Async 0.1
ASGI framework focused on simplicity and efficiency
Loading...
Searching...
No Matches
types.py
Go to the documentation of this file.
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# This file contains conversion functions that allow the same code to be
5# used in both python 2 and python 3 with minimum modifications.
6
7import sys
8import subprocess
9import inspect
10
11PY2 = sys.version_info[0] == 2
12PY3 = sys.version_info[0] == 3
13
14# =====================================================================
15if PY2:
16 def S(v):
17 """Converts the value into a byte string.
18 """
19 if isinstance(v, unicode):
20 return v.encode(sys.getfilesystemencoding())
21
22 return str(v)
23
24 def U(v):
25 """Converts the value into a unicode string.
26 """
27 if isinstance(v, str):
28 return v.decode(sys.getfilesystemencoding())
29
30 return unicode(v)
31
32 def Print(v, *args):
33 """Print a unicode string with arguments.
34 """
35 print(Printf(v, *args))
36
37 def PrintB(v, *args):
38 """Print a byte string with arguments.
39 """
40 print(S(Printf(v, *args)))
41
42 def Printf(v, *args):
43 """Returns a formatted unicode string.
44 """
45 if args:
46 if isinstance(args, tuple):
47 args = [U(a) for a in args]
48 return U(v).format(*args)
49 else:
50 return U(v).format(U(args))
51
52 return v
53
54 def PrintL(v, localdict = None):
55 """Print a string formatted with local variables.
56 """
57
58 if not localdict:
59 localdict = inspect.currentframe().f_back.f_locals
60
61 print(PrintLf(v, localdict))
62
63 def PrintBL(v, localdict = None):
64 """Print a byte string formatted with local variables.
65 """
66
67 if not localdict:
68 localdict = inspect.currentframe().f_back.f_locals
69
70 print(S(PrintLf(v, localdict)))
71
72 def PrintLf(s, localdict = None):
73 """Returns a string formatted with local variables.
74 """
75 if not localdict:
76 localdict = inspect.currentframe().f_back.f_locals
77
78 udict = {}
79
80 for k, v in localdict.iteritems():
81 if isinstance(v, str):
82 udict[k] = v.decode(sys.getfilesystemencoding())
83 else:
84 udict[k] = v
85
86 return U(s).format(**udict)
87
88# =====================================================================
89elif PY3:
90 def S(v):
91 """Converts the value into a byte string.
92 """
93 if not isinstance(v, bytes):
94 return bytes(v, sys.getfilesystemencoding())
95
96 return v
97
98 def U(v):
99 """Converts the value into a unicode string.
100 """
101 if not isinstance(v, str):
102 return str(v, sys.getfilesystemencoding())
103
104 return v
105
106 def Print(v, *args):
107 """Prints the string with arguments.
108 """
109 print(Printf(v, *args))
110
111 def Printf(v, *args):
112 """Returns a string with converted arguments.
113 """
114 if args:
115 if isinstance(args, tuple):
116 return v.format(*args)
117 else:
118 return v.format(args)
119
120 return v
121
122 def PrintL(v, localdict = None):
123 """Prints a string formatted with local variables.
124 """
125 if not localdict:
126 localdict = inspect.currentframe().f_back.f_locals
127
128 print(PrintLf(v, localdict))
129
130 def PrintLf(v, localdict = None):
131 """Returns a string formatted with local variables.
132 """
133 if not localdict:
134 localdict = inspect.currentframe().f_back.f_locals
135
136 return v.format(**localdict)
137
138 def RunCommand(cmdlist):
139 """Runs a command and returns unicode output
140 """
141 cmdlist = [U(t) for t in cmdlist]
142 return U(subprocess.check_output(cmdlist))
143
144 def RunDaemon(cmdlist):
145 """Runs a command in the background
146 """
147 cmdlist = [U(t) for t in cmdlist]
148 subprocess.Popen(cmdlist)
149
150# =====================================================================
151def V(d, k, default = None):
152 """Utility function to get a value from a dict or object given its
153 name. Will return default if it can't be found.
154
155 The V functions are convenience functions designed to ensure that
156 a value is the correct type before being used for any work.
157 """
158 try:
159 return d[k]
160 except:
161 try:
162 return getattr(d, k)
163 except:
164 pass
165
166 return default
167
168# =====================================================================
169def IntV(d, k, min = None, max = None, default = 0):
170 """Utility function to get an int from a dict or object given its
171 name. Will return default if it can't be found.
172
173 The V functions are convenience functions designed to ensure that
174 a value is the correct type before being used for any work.
175 """
176 v = default
177
178 try:
179 v = int(d[k])
180 except:
181 try:
182 v = int(getattr(d, k))
183 except:
184 return default
185
186 if min is not None and v < min:
187 v = min
188
189 if max is not None and v > max:
190 v = max
191
192 return v
193
194# =====================================================================
195def FloatV(d, k, min = None, max = None, default = 0.0):
196 """Utility function to get a float from a dict or object given its
197 name. Will return default if it can't be found.
198
199 The V functions are convenience functions designed to ensure that
200 a value is the correct type before being used for any work.
201 """
202 v = default
203
204 try:
205 v = float(d[k])
206 except:
207 try:
208 v = float(getattr(d, k))
209 except:
210 return default
211
212 if min is not None and v < min:
213 v = min
214
215 if max is not None and v > max:
216 v = max
217
218 return v
219
220# =====================================================================
221def StrV(d, k, default = ''):
222 """Utility function to get a string from a dict or object given its
223 name. Will return default if it can't be found.
224
225 The V functions are convenience functions designed to ensure that
226 a value is the correct type before being used for any work.
227 """
228 v = default
229
230 try:
231 v = str(d[k])
232 except:
233 try:
234 v = str(getattr(d, k))
235 except:
236 return default
237
238 return v
239
240# =====================================================================
241def ArrayV(d, k, default = []):
242 """Utility function to get an array from a dict or object given its
243 name. Will return default if it can't be found.
244
245 The V functions are convenience functions designed to ensure that
246 a value is the correct type before being used for any work.
247 """
248 v = default
249
250 try:
251 v = d[k]
252 except:
253 try:
254 v = getattr(d, k)
255 except:
256 return default
257
258 if not isinstance(v, list) and not isinstance(v, tuple):
259 v = []
260
261 return v
262
263# =====================================================================
264def DictV(d, k, default = {}):
265 """Utility function to get a dict from a dict or object given its
266 name. Will return default if it can't be found.
267
268 The V functions are convenience functions designed to ensure that
269 a value is the correct type before being used for any work.
270 """
271 v = default
272
273 try:
274 v = d[k]
275 except:
276 try:
277 v = getattr(d, k)
278 except:
279 return default
280
281 if not isinstance(v, dict):
282 v = {}
283
284 return v
def PrintLf(s, localdict=None)
Returns a string formatted with local variables.
Definition: types.py:72
def S(v)
Converts the value into a byte string.
Definition: types.py:16
def FloatV(d, k, min=None, max=None, default=0.0)
Utility function to get a float from a dict or object given its name.
Definition: types.py:195
def StrV(d, k, default='')
Utility function to get a string from a dict or object given its name.
Definition: types.py:221
def PrintL(v, localdict=None)
Print a string formatted with local variables.
Definition: types.py:54
def U(v)
Converts the value into a unicode string.
Definition: types.py:24
def Print(v, *args)
Print a unicode string with arguments.
Definition: types.py:32
def PrintBL(v, localdict=None)
Print a byte string formatted with local variables.
Definition: types.py:63
def DictV(d, k, default={})
Utility function to get a dict from a dict or object given its name.
Definition: types.py:264
def ArrayV(d, k, default=[])
Utility function to get an array from a dict or object given its name.
Definition: types.py:241
def RunCommand(cmdlist)
Runs a command and returns unicode output.
Definition: types.py:138
def IntV(d, k, min=None, max=None, default=0)
Utility function to get an int from a dict or object given its name.
Definition: types.py:169
def PrintB(v, *args)
Print a byte string with arguments.
Definition: types.py:37
def Printf(v, *args)
Returns a formatted unicode string.
Definition: types.py:42
def RunDaemon(cmdlist)
Runs a command in the background.
Definition: types.py:144
def V(d, k, default=None)
Utility function to get a value from a dict or object given its name.
Definition: types.py:151