forked from dropbox/stone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_helpers.py
More file actions
74 lines (58 loc) · 1.32 KB
/
python_helpers.py
File metadata and controls
74 lines (58 loc) · 1.32 KB
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
from __future__ import absolute_import, division, print_function, unicode_literals
import pprint
from stone.data_type import (
Boolean,
Bytes,
Float32,
Float64,
Int32,
Int64,
List,
String,
Timestamp,
UInt32,
UInt64,
)
from stone.target.helpers import (
fmt_pascal,
fmt_underscores,
)
_type_table = {
Boolean: 'bool',
Bytes: 'bytes',
Float32: 'float',
Float64: 'float',
Int32: 'int',
Int64: 'long',
List: 'list',
String: 'str',
Timestamp: 'datetime',
UInt32: 'long',
UInt64: 'long',
}
_reserved_keywords = {
'break',
'class',
'continue',
'for',
'pass',
'while',
}
def _rename_if_reserved(s):
if s in _reserved_keywords:
return s + '_'
else:
return s
def fmt_class(name, check_reserved=False):
s = fmt_pascal(name)
return _rename_if_reserved(s) if check_reserved else s
def fmt_func(name, check_reserved=False):
s = fmt_underscores(name)
return _rename_if_reserved(s) if check_reserved else s
def fmt_obj(o):
return pprint.pformat(o, width=1)
def fmt_type(data_type):
return _type_table.get(data_type.__class__, fmt_class(data_type.name))
def fmt_var(name, check_reserved=False):
s = fmt_underscores(name)
return _rename_if_reserved(s) if check_reserved else s