diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-03-11 15:25:56 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-03-11 15:25:56 +0200 |
commit | 0e6d874c4abb734a915f719f0a507724c7a754fa (patch) | |
tree | 03c1d24cba09bc2d1216f45e930c2d877e269a7b | |
parent | 9568147aefac22f8928460220560c62bbd972c99 (diff) | |
download | cs-y13-0e6d874c4abb734a915f719f0a507724c7a754fa.tar.gz cs-y13-0e6d874c4abb734a915f719f0a507724c7a754fa.tar.bz2 cs-y13-0e6d874c4abb734a915f719f0a507724c7a754fa.zip |
feat: add ESQs
-rw-r--r-- | notebooks/esq_ch_26.py | 36 | ||||
-rw-r--r-- | notebooks/exception_handling.py | 34 | ||||
-rw-r--r-- | notebooks/random_access.py | 1 | ||||
-rw-r--r-- | notebooks/thing.py | 22 |
4 files changed, 93 insertions, 0 deletions
diff --git a/notebooks/esq_ch_26.py b/notebooks/esq_ch_26.py new file mode 100644 index 0000000..1ef4dc7 --- /dev/null +++ b/notebooks/esq_ch_26.py @@ -0,0 +1,36 @@ +import pickle +import random +DATA_FILE = "CustomerData.DAT" +class CustomerRecord: + def __str__(self) -> str: + return f"{self.CustomerID}" + def __init__(self): + self.CustomerID = random.randint(100001, 1000000) + self.CustomerName = "" + self.TelephoneNumber = "" + self.OrderValueTotal = 0 +customers = [CustomerRecord() for i in range(0, 1000)] +def Hash(CustomerID: int) -> int: + return CustomerID % 1000 +CustomerData = [None for i in range(0, 1000)] +def AddRecord(Customer: CustomerRecord): + offset = 0 + while CustomerData[Hash(Customer.CustomerID) + offset] != None: + offset += 1 + CustomerData[Hash(Customer.CustomerID) + offset] = Customer +def FindRecord(customer_id: int) -> CustomerRecord: + starting = Hash(customer_id) + offset = 0 + while CustomerData[starting + offset].CustomerID != customer_id: + offset += 1 + return CustomerData[starting + offset] +def StoreRecords(filename = DATA_FILE): + with open(filename, 'wb') as file: + for customer in CustomerData: + pickle.dump(customer, file) + file.close() +for i in range(20): + AddRecord(CustomerRecord()) +for i in range(len(CustomerData)): + if CustomerData[i] != None: + print(f"[{i}] {CustomerData[i]} {'-> Collision!' if Hash(CustomerData[i].CustomerID) != i else ''}")
\ No newline at end of file diff --git a/notebooks/exception_handling.py b/notebooks/exception_handling.py new file mode 100644 index 0000000..50bf830 --- /dev/null +++ b/notebooks/exception_handling.py @@ -0,0 +1,34 @@ +# Exception handling +# - programming errors +# - user errors +# TRY +# <statement> +# EXCEPT +# <statement> +# ENDTRY + +def safe_student_open(filename): + try: + sfile = open(filename, 'rb') + sfile.close() + except OSError: + print("wtf is this file man") + +def test_input_integer(): + val = input("sample input: ") + try: + value = int(val) + print(value) + print("It is an integer") + except ValueError: + print("Noooo bro it isn't an integer cmon man get good") +# test_input_integer() + +def integer(num): + try: + b = int(num) + print(b) + print("is integer") + except: + print("not") +safe_student_open("thing.data")p
\ No newline at end of file diff --git a/notebooks/random_access.py b/notebooks/random_access.py new file mode 100644 index 0000000..ae6fed7 --- /dev/null +++ b/notebooks/random_access.py @@ -0,0 +1 @@ +# Accessing a file and its records through random hash
\ No newline at end of file diff --git a/notebooks/thing.py b/notebooks/thing.py new file mode 100644 index 0000000..a580117 --- /dev/null +++ b/notebooks/thing.py @@ -0,0 +1,22 @@ +import datetime +import pickle +import random # data generator +class Car: + def __init__(self, car_id): + self.VehicleID = car_id + self.__Registration = "" + self.__Date = datetime.datetime.now() + self.__EngineSize = -1 + self.__PurchasePrice = 5900_00 +def data_generator(): + cars = [] + for i in range(40): + s = "ID" + for i in range(10): + s += str(random.randint(0, 9)) + cars.append(Car(s)) + return cars +with open("cars.dat", 'wb+') as carfile: + for car in data_generator(): + print(car.VehicleID) + pickle.dump(car, carfile)
\ No newline at end of file |