Created
August 25, 2020 13:49
-
-
Save rvasilevsk/9bc51a03d345026bffcf0d81ee9a0005 to your computer and use it in GitHub Desktop.
[json_rows.py] #python #json #json_rows #cache
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
#!/usr/bin/env python | |
import datetime | |
import json | |
####################################################################################################################### | |
def replace_suffix(s, suffix, replace): | |
""" | |
replace suffix if exists | |
:param s: str | |
:param suffix: str | |
:param replace: str | |
:return: str | |
>>> replace_suffix('file.JPG', '.JPG', '.jpeg') | |
'file.jpeg' | |
>>> replace_suffix('file.txt', '.JPG', '.jpeg') | |
'file.txt' | |
""" | |
if s.endswith(suffix): | |
return s[:-len(suffix)] + replace | |
else: | |
return s | |
####################################################################################################################### | |
def json_serial(obj): | |
"""JSON serializer for objects not serializable by default json code""" | |
if isinstance(obj, (datetime.datetime, datetime.date)): | |
return obj.isoformat() | |
raise TypeError("Type %s not serializable" % type(obj)) | |
def json_unserial(data): | |
key = 'created_at' | |
dt = data.get(key, None) | |
if dt is not None: | |
data[key] = datetime.datetime.fromisoformat(dt) | |
return data | |
def jsr_cache_filename(): | |
return replace_suffix(__file__, '.py', '.cache.jsonrows') | |
def jsr_json_params(): | |
return dict(ensure_ascii=False, default=json_serial) | |
def jsr_rows_iterator(filename): | |
for ln in open(filename, encoding='utf8').readlines(): | |
ln = ln.strip() | |
if ln: | |
data = json.loads(ln) | |
data = json_unserial(data) | |
yield data | |
def jsr_append(filename, data): | |
with open(filename, 'a', encoding='utf8') as f: | |
data = json.dumps(data, **jsr_json_params()) | |
f.write(data) | |
f.write('\n') | |
def jsr_dump(filename): | |
for data in jsr_rows_iterator(filename): | |
print(data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment