Skip to content

Commit d737390

Browse files
committed
Add a safer type conversion and using repr instead of deprecated backquotes
1 parent e56bfa4 commit d737390

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

bigml/fields.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@
2424
import sys
2525
from bigml.util import invert_dictionary
2626

27-
type_map = {
27+
TYPE_MAP = {
2828
"categorical": str,
2929
"numeric": float,
3030
"text": str
3131
}
3232

33+
34+
def map_type(value):
35+
"""Maps a BigML type to a Python type.
36+
37+
"""
38+
if value in TYPE_MAP:
39+
return TYPE_MAP[value]
40+
else:
41+
return str
42+
43+
3344
class Fields(object):
3445
"""A class to deal with BigML auto-generated ids.
3546
@@ -94,17 +105,17 @@ def pair(self, row, objective_field=None, objective_field_present=None):
94105
if objective_field_present:
95106
if index != objective_field:
96107
pair.update({self.field_id(index):
97-
type_map[self.fields[self.field_id(index)]['optype']]
98-
(row[index])})
108+
map_type(self.fields[self.field_id(index)]
109+
['optype'])(row[index])})
99110
else:
100111
if index >= objective_field:
101112
pair.update({self.field_id(index + 1):
102-
type_map[self.fields[self.field_id(index +
103-
1)]['optype']](row[index])})
113+
map_type(self.fields[self.field_id(index + 1)]
114+
['optype'])(row[index])})
104115
else:
105116
pair.update({self.field_id(index):
106-
type_map[self.fields[self.field_id(index)]['optype']]
107-
(row[index])})
117+
map_type(self.fields[self.field_id(index)]
118+
['optype'])(row[index])})
108119

109120
return pair
110121

bigml/model.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
from bigml.api import FINISHED
5858
from bigml.util import invert_dictionary, slugify, split
59-
from bigml.fields import type_map
6059

6160
# Map operator str to its corresponding function
6261
OPERATOR = {
@@ -238,10 +237,10 @@ def python_body(self, depth=1, cmv=False):
238237
(INDENT * depth,
239238
self.fields[child.predicate.field]['slug'],
240239
PYTHON_OPERATOR[child.predicate.operator],
241-
`child.predicate.value`))
240+
repr(child.predicate.value)))
242241
body += child.python_body(depth + 1)
243242
else:
244-
body = "%s return %s\n" % (INDENT * depth, `self.output`)
243+
body = "%s return %s\n" % (INDENT * depth, repr(self.output))
245244
return body
246245

247246
def python(self, out):

0 commit comments

Comments
 (0)