Skip to content
Snippets Groups Projects
Commit 864c1006 authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

#58 get_codebook sorterer på Word eller Code

parent 685c5d4b
No related branches found
No related tags found
1 merge request!33#58 Lag PDF med kode- og dekodebok
......@@ -2,6 +2,7 @@
import os
import sqlite3
import json
from enum import Enum
import soitool.coder
# Set name and path to (future) database
......@@ -21,6 +22,12 @@ CATEGORYWORDS = (
BYHEART = "CREATE TABLE ByHeart(Word VARCHAR PRIMARY KEY)"
class CodebookSort(Enum):
"""Enumerate with ways to sort codebook."""
WORD = 0
CODE = 1
class Database:
"""
Holds database-connection and related functions.
......@@ -137,7 +144,7 @@ class Database:
return categories
def get_codebook(self, small=False):
def get_codebook(self, small=False, sort=CodebookSort.WORD):
"""
Retrieve the entries belonging to the full or small codebook.
......@@ -145,16 +152,24 @@ class Database:
----------
small : Bool
Full or small codebook to be returned
sort : int, optional
0 or 1 (enumerate value from 'CodebookSort')
Data is sorted by Word if 0 (default), by Code if 1
Returns
-------
codebook : list (of dicts)
[{'word': str, 'type': str, 'category': str, 'code': str}]
"""
stmt = "SELECT * FROM (SELECT * FROM CodeBook"
if sort == CodebookSort.WORD:
stmt = stmt + " ORDER BY Word)"
elif sort == CodebookSort.CODE:
stmt = stmt + " ORDER BY Code)"
# Get either full or small codebook
stmt = "SELECT * FROM CodeBook"
if small:
stmt = stmt + " WHERE Type = ?"
stmt = stmt + " WHERE Type = ?" # T?
queried = self.conn.execute(stmt, (1,)).fetchall()
else:
queried = self.conn.execute(stmt).fetchall()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment