diff options
| author | root <root@turin.home> | 2022-02-04 18:07:47 +0000 |
|---|---|---|
| committer | root <root@turin.home> | 2022-02-04 18:07:47 +0000 |
| commit | 6671090e31ab30711f169abfb61d880b50ba27d8 (patch) | |
| tree | 8952c13c358694c423f67e5e1e09223ed090a326 | |
| parent | 0e23673e015a5261019a151f22b419f4c80b7e8d (diff) | |
Dictionary class. Reformat.
| -rw-r--r-- | dictionary.py | 38 | ||||
| -rw-r--r-- | errors.py | 16 | ||||
| -rw-r--r-- | main.py | 5 | ||||
| -rw-r--r-- | sql.py | 4 |
4 files changed, 59 insertions, 4 deletions
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) @@ -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() @@ -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" @@ -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() |
