This repository was archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmonetize.py
executable file
·108 lines (87 loc) · 2.37 KB
/
monetize.py
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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
import datetime
import decimal
import sys
try:
import numpy
except ImportError:
raise Exception('MonetDBLite requires numpy but import of numpy failed')
PY3 = sys.version_info[0] >= 3
from monetdblite.exceptions import ProgrammingError
if PY3:
def utf8_encode(str):
if str == None:
return None
if type(str) == type(""):
return str.encode('utf-8')
return str
else:
def utf8_encode(str):
return str
def monet_none(data):
"""
returns a NULL string
"""
return "NULL"
def monet_bool(data):
"""
returns "true" or "false"
"""
return ["false", "true"][bool(data)]
def monet_escape(data):
"""
returns an escaped string
"""
data = str(data).replace("\\", "\\\\")
data = data.replace("\'", "\\\'")
return "'%s'" % str(data)
def monet_identifier_escape(data):
"""
returns an escaped identifier
"""
data = str(data).replace("\\", "\\\\")
data = data.replace('"', '\\"')
return '"%s"' % str(data)
def monet_bytes(data):
"""
converts bytes to string
"""
if not PY3:
return monet_escape(data)
else:
return monet_escape(data.decode('utf8'))
def monet_unicode(data):
return monet_escape(data.encode('utf-8'))
mapping = [
(str, monet_escape),
(bytes, monet_bytes),
(int, str),
(numpy.integer, str),
(complex, str),
(float, str),
(decimal.Decimal, str),
(datetime.datetime, monet_escape),
(datetime.time, monet_escape),
(datetime.date, monet_escape),
(datetime.timedelta, monet_escape),
(bool, monet_bool),
(type(None), monet_none),
]
if not PY3:
mapping += ((unicode, monet_unicode),)
mapping_dict = dict(mapping)
def convert(data):
"""
Return the appropriate conversion function based upon the python type.
"""
if type(data) in mapping_dict:
return mapping_dict[type(data)](data)
else:
for type_, func in mapping:
if issubclass(type(data), type_):
return func(data)
raise ProgrammingError("type %s not supported as value" % type(data))