This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapping.py: Added a new mapping class
This makes things so much easier than deeply, deeply nested code.
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from abc import ABCMeta, abstractmethod | ||
|
||
|
||
# XXX: Is it recommended I inherit from object? | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
class Mapping(object): | ||
""" | ||
This class handles mappings between our raw input, and what we generate. | ||
""" | ||
|
||
# We're an abstract class | ||
__metaclass__ = ABCMeta | ||
|
||
# This was originally called 'value', but apparently that's a | ||
# special method name. | ||
This comment has been minimized.
Sorry, something went wrong.
ncoghlan
|
||
@abstractmethod | ||
def emit(self, row): | ||
""" | ||
Given a row of data, emits the value calculated by | ||
this transform. | ||
""" | ||
raise NotImplementedError | ||
|
||
|
||
class Literal(Mapping): | ||
"""A literal string that's always the same.""" | ||
|
||
def __init__(self, config): | ||
self.value = config["value"] | ||
|
||
def emit(self, row): | ||
return self.value | ||
|
||
|
||
class Map(Mapping): | ||
"""The most common mapping. Turns a field in our data into a field in our output.""" | ||
|
||
def __init__(self, config): | ||
self.field = config["field"] | ||
self.optional = config.get("optional", False) | ||
|
||
def emit(self, row): | ||
value = row[self.field] | ||
|
||
if not self.optional and len(value) == 0: | ||
raise ValueError("Required field {} missing in data".format(self.field)) | ||
|
||
return value | ||
|
||
|
||
class Concat(Map): | ||
"""A map, but concatenated with static strings.""" | ||
|
||
def __init__(self, config): | ||
super(self.__class__, self).__init__(config) | ||
self.pre = config.get("prefix", "") | ||
self.post = config.get("postfix", "") | ||
|
||
def emit(self, row): | ||
return self.pre + super(self.__class__, self).emit(row) + self.post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from taas.mapping import Literal, Map, Concat | ||
import unittest | ||
|
||
|
||
class TestMapping(unittest.TestCase): | ||
|
||
row = { | ||
"id": "3", | ||
"foo": "bar", | ||
"baz": "bazza" | ||
} | ||
|
||
def test_literal(self): | ||
|
||
mystr = "foo" | ||
|
||
lit = Literal({"value": mystr}) | ||
|
||
value = lit.emit(self.row) | ||
|
||
self.assertEqual(value, mystr) | ||
|
||
def test_map(self): | ||
mymap = Map({"field": "foo"}) | ||
|
||
self.assertEqual( | ||
mymap.emit(self.row), | ||
"bar" | ||
) | ||
|
||
self.assertEqual( | ||
mymap.emit({"foo": "baz"}), | ||
"baz" | ||
) | ||
|
||
# TODO: Test *optional* maps. | ||
|
||
def test_concat(self): | ||
concat = Concat({ | ||
"field": "id", | ||
"prefix": "https://example.com/", | ||
"postfix": "/self" | ||
}) | ||
|
||
self.assertEqual( | ||
concat.emit(self.row), | ||
"https://example.com/3/self" | ||
) |
This is Python 2.7, so yes. If you don't, you'll get an old-style class, and find a lot of things don't work the way articles and other documentation suggest they should.