Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import copy
- import json
- import random
- import warnings
- import unittest
- import numpy as np
- random.seed(42)
- with open("vhod.txt", "w") as f:
- f.write("".join(f"{random.uniform(0.3, 2):.1f}\n" for _ in range(200)))
- with open("izhod.txt", "w") as f:
- f.write("""19.3
- 20.0
- 20.0
- 19.6
- 20.0
- 19.9
- 19.4
- 19.7
- 18.9
- 18.4
- 19.9
- 10.1
- """)
- with open("vhod2.txt", "w") as f:
- f.write("".join(f"{random.uniform(0.3, 2):.1f}\n" for _ in range(5)))
- with open("izhod2.txt", "w") as f:
- f.write("7.2\n")
- with open("vhod3.txt", "w") as f:
- f.write("1.0\n" * 20)
- with open("izhod3.txt", "w") as f:
- f.write("20.0\n")
- t20 = [[20] * 5 for _ in range(7 * 4)]
- # Pretežak stolpec
- tst = copy.deepcopy(t20)
- tst[3][2] = 21
- # Prelahka leva
- tl = copy.deepcopy(t20)
- for n in range(4):
- tl[1 + 7 * n][:2] = [0, 0]
- tl[2 + 7 * n][:2] = [0, 0]
- tl[5 + 7 * n][:2] = [0, 0]
- tl[6 + 7 * n][:2] = [0, 0]
- # Prelahka desna
- td = copy.deepcopy(t20)
- for n in range(4):
- td[1 + 7 * n][-2:] = [0, 0]
- td[3 + 7 * n][-2:] = [0, 0]
- # Prelahko spredaj
- ts = copy.deepcopy(t20)
- for n in range(4):
- for v in range(3):
- ts[7 * n + v][1:4] = [0, 0, 0]
- # Vse OK - ignororaj srednjo :)
- tok = copy.deepcopy(t20)
- for n in range(4):
- for v in range(7):
- tok[7 * n + v][2] = 0
- # Vse OK - razlike niso prevelike
- toks = copy.deepcopy(t20)
- for n in range(4):
- for v in range(3):
- toks[7 * n + v][1] = 0
- for i, tab in enumerate((t20, tst, tl, td, ts, tok, toks), start=1):
- with open(f"varnost{i}.txt", "w") as f:\
- f.write("".join(",".join(map(str, v)) + "\n" for v in tab))
- with open("narocila.json", "w") as f:
- json.dump({"Ana": 1000, "Berta": 2000, "Cilka": 300, "Dani": 1200}, f)
- os.makedirs("posiljke", exist_ok=True)
- for komu, teze in (("Ana", (100, 400, 300, 200)), ("Berta", (200, 1000)), ("Dani", (1000, 200, 80))):
- for teza in teze:
- with open(f"posiljke/N{random.randint(100000, 999999)}.json", "w") as f:
- json.dump({"narocnik": komu, "teza": teza, "koda_prevoznika": random.randint(100, 999)}, f)
- def pakiranje(vlez, izlez):
- sum = 0.0
- res = []
- with open(vlez, "r") as f:
- for line in f:
- broj = float(line)
- if sum + broj > 20.0:
- res.append(f"{sum:.1f}")
- sum = 0.0
- sum += broj
- if sum > 0:
- res.append(f"{sum:.1f}")
- with open(izlez, "w") as f:
- for i in res:
- print(i, file=f)
- print(res)
- def varnost(ime_datoteka):
- vlez = np.genfromtxt(ime_datoteka, delimiter=',')
- vkupno_rows, cols = vlez.shape
- num_levels = 4
- rows = vkupno_rows // num_levels
- kutii = vlez.reshape(num_levels, rows, cols)
- column_weight = kutii.sum(axis=0)
- print(column_weight)
- if np.any(column_weight > 80):
- return False
- total_weight = kutii.sum()
- print(total_weight)
- half_cols = cols // 2
- leva_tezina = column_weight[:, :half_cols].sum()
- desna_tezina = column_weight[:, -half_cols:].sum()
- print(leva_tezina, desna_tezina)
- razlika = abs(leva_tezina - desna_tezina)
- if razlika > total_weight * 0.1:
- return False
- half_rows = rows // 2
- napred_tezina = column_weight[:half_rows, :].sum()
- nazad_tezina = column_weight[-half_rows:, :].sum()
- razlika = abs(napred_tezina - nazad_tezina)
- if razlika > (total_weight * 0.1):
- return False
- return True
- def pregled(orders, dir_name, file_to_print):
- name_dict = {}
- with open(orders, "r") as f:
- name_dict = json.load(f)
- zemena_roba = {}
- names = []
- for ime, vrednost in name_dict.items():
- names.append(ime)
- zemena_roba[ime] = 0
- for file_name in os.listdir(dir_name):
- if file_name.endswith(".json"):
- path = os.path.join(dir_name, file_name)
- with open(path, "r") as f:
- d = json.load(f)
- if "narocnik" in d:
- if d["narocnik"] in zemena_roba and "teza" in d:
- zemena_roba[d["narocnik"]] += d["teza"]
- res = {}
- for ime, tezina in zemena_roba.items():
- if tezina < name_dict[ime]:
- res[ime] = name_dict[ime] - tezina
- with open(file_to_print, "w") as f:
- json.dump(res, f)
- class Test(unittest.TestCase):
- def setUp(self):
- super().setUp()
- warnings.simplefilter("ignore", ResourceWarning)
- def test_01_pakiranje(self):
- pakiranje("vhod.txt", "izhod-moj.txt")
- self.assertEqual(open("izhod-moj.txt").read(), open("izhod.txt").read())
- pakiranje("vhod2.txt", "izhod2-moj.txt")
- self.assertEqual(open("izhod2-moj.txt").read(), open("izhod2.txt").read())
- pakiranje("vhod3.txt", "izhod3-moj.txt")
- self.assertEqual(open("izhod3-moj.txt").read(), open("izhod3.txt").read())
- def test_02_varnost(self):
- self.assertTrue(varnost("varnost1.txt"))
- self.assertFalse(varnost("varnost2.txt")) # pretežak stolpec
- self.assertFalse(varnost("varnost3.txt")) # prelahka leva stran
- self.assertFalse(varnost("varnost4.txt")) # prelahka desna stran
- self.assertFalse(varnost("varnost5.txt")) # prelahka sprednja stran
- self.assertTrue(varnost("varnost6.txt")) # na sredini nič, ampak OK
- self.assertTrue(varnost("varnost7.txt")) # srepdaj malo manjka, vendar razlika ni prevlika
- def test_03_pregled(self):
- pregled("narocila.json", "posiljke", "porocilo-moj.json")
- self.assertEqual({'Berta': 800, 'Cilka': 300}, json.load(open("porocilo-moj.json")))
- def test_04_porocilo(self):
- narocila = json.load(open("narocila.json"))
- dorocila = {'Ana': 1000, 'Berta': 800, 'Dani': 1280}
- self.assertEqual("""Naročnik Naročeno Dostava Razlika
- ----------------------------------------
- Ana 1000 1000 0
- Berta 2000 800 -1200
- Cilka 300 0 -300
- Dani 1200 1280 80
- ----------------------------------------""".strip(), porocilo(narocila, dorocila).strip())
- if __name__ == "__main__":
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment