Skip to content

Commit 2759e71

Browse files
authored
Merge pull request #50 from alexei/feature-list_adapter
Implement ListField; close #10
2 parents c5894fa + dc4aae8 commit 2759e71

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

adapters/fields.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'DecimalField',
2020
'FloatField',
2121
'IntField',
22+
'ListField',
2223
'TimeField',
2324
'VerbatimField',
2425
]
@@ -78,10 +79,6 @@ def prepare(self, data):
7879
return Decimal(data)
7980

8081

81-
class VerbatimField(BaseField):
82-
pass
83-
84-
8582
class FloatField(BaseField):
8683
def prepare(self, data):
8784
return float(data)
@@ -92,6 +89,15 @@ def prepare(self, data):
9289
return int(data)
9390

9491

92+
class ListField(BaseField):
93+
def __init__(self, child, **kwargs):
94+
self.child = child
95+
super(ListField, self).__init__(**kwargs)
96+
97+
def prepare(self, data):
98+
return [self.child().adapt(item) for item in data]
99+
100+
95101
class TimeField(BaseField):
96102
def prepare(self, data):
97103
if isinstance(data, datetime.time):
@@ -100,3 +106,7 @@ def prepare(self, data):
100106
return dateutil.parser.parse(data).timetz()
101107
else:
102108
raise ValueError("Invalid time argument")
109+
110+
111+
class VerbatimField(BaseField):
112+
pass

tests/test_fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,8 @@ def test_optional_field_with_default(self):
162162
for entry in data:
163163
actual = field.adapt(entry)
164164
self.assertEqual(actual, expected)
165+
166+
def test_list_field(self):
167+
actual = adapters.ListField(adapters.CharField).adapt([1, 2, 3])
168+
expected = ['1', '2', '3']
169+
self.assertEqual(actual, expected)

0 commit comments

Comments
 (0)