Compare commits

...

3 Commits

Author SHA1 Message Date
frabad
f015641a11 ménage 2022-05-18 12:05:08 +02:00
frabad
e8c065824d ajout du script add_dates.py 2022-05-18 12:02:06 +02:00
frabad
c68da7af37 ajout de commentaires 2022-05-18 11:52:42 +02:00
5 changed files with 49 additions and 31 deletions

44
LittleBock/add_dates.py Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/python3
import json
import pathlib
import datetime
"""
ajoute une valeur de date lisible par un humain à chaque entrée d'un document JSON
"""
def from_stamp(i: int) -> str:
"""normalise une date UNIX"""
dt = str(i)
if len(dt)>10:
dt = dt[:10]
sdt = str(datetime.datetime.fromtimestamp(int(dt)))
#print(f" {dt} : {sdt}")
return sdt
def jsproc(path_in):
"""traite un document JSON"""
_json = None
with open(path_in,'r') as f:
_json = json.load(f)
for group in _json:
for entry in group["data"]:
entry.update({"date":from_stamp(entry["x"])})
return _json
if __name__ == "__main__":
here = pathlib.Path.cwd()
jsdocs = tuple(here.rglob("*.json"))
if len(jsdocs) == 0:
print("Aucun fichier JSON ('.json') trouvé.")
for i in jsdocs:
if i.exists() and i.stat().st_size > 0:
data = jsproc(i)
if data:
with open(i,'w') as f:
f.write(json.dumps(data,
sort_keys=False, ensure_ascii=False, indent=2))
print(f"INFO: {i.name} modifié")

View File

@ -10,18 +10,19 @@ except ModuleNotFoundError:
sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml") sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml")
def jsproc(data): def jsproc(data):
"""supprime les données inutiles d'une série""" """traite les données JSON"""
unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",)
if data.get("data"): if data.get("data"):
#nettoyage des données
unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",)
for u in unwanted: for u in unwanted:
if u in data.keys(): if u in data.keys():
if u == "tooltip": if u == "tooltip": #rattache une unité à la valeur concernée
tt = data.get(u) tt = data.get(u)
suffix = tt.get("valueSuffix") suffix = tt.get("valueSuffix")
if suffix: data.update({ if suffix: data.update({
"name": " ".join([data["name"], suffix]) "name": " ".join([data["name"], suffix])
}) })
del(data[u]) del(data[u]) #supprime la donnée indésirable
return data return data
def hproc(path_in): def hproc(path_in):

View File

@ -1,27 +0,0 @@
#!/usr/bin/python3
import datetime
""""""
TIMESTAMPS = (
161252911000,
162352411900,
1646927061000,
166691206100000,
)
def from_stamp(i: int) -> str:
dt = str(i)
if len(dt)>10:
dt = dt[:10]
sdt = str(datetime.datetime.fromtimestamp(int(dt)))
print(f" {dt} : {sdt}")
return sdt
if __name__ == "__main__":
print("formatted UNIX dates")
for i in TIMESTAMPS:
from_stamp(i)