Compare commits
3 Commits
888e1593f9
...
f015641a11
Author | SHA1 | Date | |
---|---|---|---|
|
f015641a11 | ||
|
e8c065824d | ||
|
c68da7af37 |
44
LittleBock/add_dates.py
Executable file
44
LittleBock/add_dates.py
Executable 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é")
|
||||
|
@ -10,18 +10,19 @@ except ModuleNotFoundError:
|
||||
sys.exit("Le module 'lxml' est nécessaire.\n\thttp://pypi.org/lxml")
|
||||
|
||||
def jsproc(data):
|
||||
"""supprime les données inutiles d'une série"""
|
||||
unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",)
|
||||
"""traite les données JSON"""
|
||||
if data.get("data"):
|
||||
#nettoyage des données
|
||||
unwanted = ("color","opacity","yAxis","dashStyle", "tooltip",)
|
||||
for u in unwanted:
|
||||
if u in data.keys():
|
||||
if u == "tooltip":
|
||||
if u == "tooltip": #rattache une unité à la valeur concernée
|
||||
tt = data.get(u)
|
||||
suffix = tt.get("valueSuffix")
|
||||
if suffix: data.update({
|
||||
"name": " ".join([data["name"], suffix])
|
||||
})
|
||||
del(data[u])
|
||||
del(data[u]) #supprime la donnée indésirable
|
||||
return data
|
||||
|
||||
def hproc(path_in):
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user