1
0

2 Commits 3387a33aff ... 9ecad93aae

Autor SHA1 Mensagem Data
  Nerenex 9ecad93aae dokončení optimalizace kodu, zbavil jsem se pandas. há 10 meses atrás
  Nerenex b7d0ea3f04 dokončení optimalizace kodu, zbavil jsem se pandas. há 10 meses atrás
2 ficheiros alterados com 107 adições e 28 exclusões
  1. 53 28
      Prepis.py
  2. 54 0
      csv_check.py

+ 53 - 28
Prepis.py

@@ -1,30 +1,55 @@
-import pandas as pd
-import sys
 import json
+import sys
+import csv
+
+
+def nacti(path):
+    data = []
+    with open(path, 'r', encoding='utf8') as fp:
+        reading = csv.DictReader(fp)
+        for row in reading:
+            data.append(row['JSON'])
+        return data
+
+def ulozit(saving_csv, name_csv):
+    with open(name_csv + ".csv", "w", encoding="utf8", newline='') as f:
+        writer = csv.DictWriter(f, fieldnames=['JSON'])
+        writer.writeheader()
+        for radek in saving_csv:
+            writer.writerow({'JSON': radek})
+
+def odstranit(csv_json, clear_json):
+    for s in csv_json:
+        if isinstance(s, str) and ',' in s:
+            idx = s.rfind(',')
+            clear_json.add(s[:idx])
+        else:
+            clear_json.add(s)
+
+def nactenicsv(pattern_csv, target_csv):
+    missing = set()
+    overstay = set()
+    pattern_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in pattern_csv)
+    target_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in target_csv)
+    missing_jsons = pattern_jsons - target_jsons
+    overstay_jsons = target_jsons - pattern_jsons
+    odstranit(missing_jsons, missing)
+    odstranit(overstay_jsons, overstay)
+    ulozit(missing, "pridat")
+    ulozit(overstay, "odebrat")
+
+def main():
+    if len(sys.argv) < 3:
+        print('Chybi cesta k csv souborum.', file=sys.stderr)
+        print('Zadejte takto: <nazev_programu.py> <cesta k vzorove.csv> <cesta k cilove.csv>', file=sys.stderr)
+        sys.exit(1)
+    path_pattern = sys.argv[1] 
+    path_target = sys.argv[2]
+    pattern = nacti(path_pattern)
+    target = nacti(path_target)
+
+    nactenicsv(pattern, target)
 
-vzor = None
-cil = None
-
-def nactenicsv():
-    cestaksouboru_vzor = sys.argv[1]
-    vzor = pd.read_csv(cestaksouboru_vzor)
-    cestaksouboru_cil = sys.argv[2]
-    cil = pd.read_csv(cestaksouboru_cil)
-    vzor_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in vzor['JSON'])
-    cil_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in cil['JSON'])
-
-def ulozit(ukladane_csv, nazev_csv):
-    pd.DataFrame(list(ukladane_csv), columns=['JSON']).to_csv(nazev_csv, index=False)
-
-#main
-#def main() -> None:
-nactenicsv()
-vzor_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in vzor['JSON'])
-cil_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in cil['JSON'])
-chybi_jsons =  vzor_jsons - cil_jsons
-ulozit(chybi_jsons, "pridat")
-prebiva_jsons = cil_jsons - vzor_jsons
-ulozit(prebiva_jsons, "odebrat")
-
-#if __name__ == '__main__':
-#    sys.exit(main()) 
+if __name__ == '__main__':
+    main()
+    sys.exit(0) 

+ 54 - 0
csv_check.py

@@ -0,0 +1,54 @@
+import json
+import sys
+import csv
+
+def load(path):
+    data = []
+    with open(path, 'r', encoding='utf8') as fp:
+        reading = csv.DictReader(fp)
+        for row in reading:
+            data.append(row['JSON'])
+        return data
+
+def save(saving_csv, name_csv):
+    with open(name_csv + '.csv', 'w', encoding= 'utf8', newline='') as f:
+        writer = csv.DictWriter(f, fieldnames=['JSON'])
+        writer.writeheader()
+        for radek in saving_csv:
+            writer.writerow({'JSON': radek})
+
+def delete(csv_json, clear_json):
+    for s in csv_json:
+        if isinstance(s, str) and ',' in s:
+            idx = s.rfind(',')
+            clear_json.add(s[:idx])
+        else:
+            clear_json.add(s)
+
+def loadcsv(pattern_csv, target_csv):
+    missing = set()
+    overstay = set()
+    pattern_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in pattern_csv)
+    target_jsons = set(json.dumps(json.loads(x), sort_keys=True) for x in target_csv)
+    missing_jsons = pattern_jsons - target_jsons
+    overstay_jsons = target_jsons - pattern_jsons
+    delete(missing_jsons, missing)
+    delete(overstay_jsons, overstay)
+    save(missing, 'pridat')
+    save(overstay, 'odebrat')
+
+def main():
+    if len(sys.argv) < 3:
+        print('Chybi cesta k csv souborum.', file=sys.stderr)
+        print('Zadejte takto: <nazev_programu.py> <cesta k vzorove.csv> <cesta k cilove.csv>', file=sys.stderr)
+        sys.exit(1)
+    path_pattern = sys.argv[1] 
+    path_target = sys.argv[2]
+    pattern = load(path_pattern)
+    target = load(path_target)
+
+    loadcsv(pattern, target)
+
+if __name__ == '__main__':
+    main()
+    sys.exit(0)