aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/transforms/entities.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2019-03-11 16:38:51 -0700
committerBryan Newbold <bnewbold@robocracy.org>2019-03-11 16:38:51 -0700
commit655f7060eb5b5e711a8a892cb1085639c4aa8fd2 (patch)
treeffa1139e0c56b6510ec71d1aa8cc426423449f11 /python/fatcat_tools/transforms/entities.py
parentc937447f894cfde54628fecf3fa71127cb769f0c (diff)
downloadfatcat-655f7060eb5b5e711a8a892cb1085639c4aa8fd2.tar.gz
fatcat-655f7060eb5b5e711a8a892cb1085639c4aa8fd2.zip
refactor transforms into sub-dir
Diffstat (limited to 'python/fatcat_tools/transforms/entities.py')
-rw-r--r--python/fatcat_tools/transforms/entities.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/python/fatcat_tools/transforms/entities.py b/python/fatcat_tools/transforms/entities.py
new file mode 100644
index 00000000..b67df12d
--- /dev/null
+++ b/python/fatcat_tools/transforms/entities.py
@@ -0,0 +1,31 @@
+
+
+import collections
+from fatcat_client import ApiClient
+
+def entity_to_dict(entity, api_client=None):
+ """
+ Hack to take advantage of the code-generated serialization code.
+
+ Initializing/destroying ApiClient objects is surprisingly expensive
+ (because it involves a threadpool), so we allow passing an existing
+ instance. If you already have a full-on API connection `api`, you can
+ access the ApiClient object as `api.api_client`. This is such a speed-up
+ that this argument may become mandatory.
+ """
+ if not api_client:
+ api_client = ApiClient()
+ return api_client.sanitize_for_serialization(entity)
+
+def entity_from_json(json_str, entity_type, api_client=None):
+ """
+ Hack to take advantage of the code-generated deserialization code
+
+ See not on `entity_to_dict()` about api_client argument.
+ """
+ if not api_client:
+ api_client = ApiClient()
+ thing = collections.namedtuple('Thing', ['data'])
+ thing.data = json_str
+ return api_client.deserialize(thing, entity_type)
+