From 6671090e31ab30711f169abfb61d880b50ba27d8 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 4 Feb 2022 18:07:47 +0000 Subject: Dictionary class. Reformat. --- dictionary.py | 38 ++++++++++++++++++++++++++++++++++++++ errors.py | 16 ++++++++++++++++ main.py | 5 +++-- sql.py | 4 ++-- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 dictionary.py diff --git a/dictionary.py b/dictionary.py new file mode 100644 index 0000000..32c4e73 --- /dev/null +++ b/dictionary.py @@ -0,0 +1,38 @@ +import errors + +# Recursive merge sort algorithm +# https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Python +def merge(x, y, i): + if x == []: + return y + if y == []: + return x + return ( + [x[0]] + merge(x[1:], y, i) + if x[0][i] < y[0][i] + else [y[0]] + merge(x, y[1:], i) + ) + + +def sort(a, n, i): + m = n // 2 + return ( + a + if n <= 1 + else merge(sort(a[:m], m, i), sort(a[m:], n - m, i), i) + ) + + +class dictionary: + def __init__(self): + self.data = [] + self.size = 0 + + def add(self, key, value): + if type(value) != int: + self.data.append([key, value]) + self.size += 1 + + # Sort dictionary by value + def sort_by_value(self): + self.data = sort_v(self.data, self.size, 1) diff --git a/errors.py b/errors.py index 5e7394b..b367263 100644 --- a/errors.py +++ b/errors.py @@ -1,3 +1,6 @@ +import os + + def device_error(): print( """ @@ -7,4 +10,17 @@ def device_error(): - Raspberry Pi 4 """ ) + os._exit(1) + quit() + + +def argument_error(data, datatype): + print( + """ + Invalid data type {} for {} + """.format( + data, datatype + ) + ) + os._exit(1) quit() diff --git a/main.py b/main.py index df6d919..7559888 100644 --- a/main.py +++ b/main.py @@ -83,9 +83,10 @@ def export(opt): for i in data: o += str(i) + "\n" con.close() - with open('static/export.txt', 'w') as file: + # writing data to a file + with open("static/export.txt", "w") as file: file.write(o) - return redirect('/static/export.txt') + return redirect("/static/export.txt") else: return render_template( "error.html", error="Invalid export type" diff --git a/sql.py b/sql.py index d78c84c..38d40e1 100644 --- a/sql.py +++ b/sql.py @@ -19,9 +19,9 @@ class db: con = sqlite3.connect(self.dbfile) cur = con.cursor() if param == None: - cur.execute(cmd) + cur.execute(cmd) else: - cur.execute(cmd, param) + cur.execute(cmd, param) con.commit() con.close() -- cgit v1.2.3