format code with `black`

This commit is contained in:
Oystein Kristoffer Tveit 2023-08-29 23:50:34 +02:00
parent 88d855a951
commit c219fa61ba
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
30 changed files with 1514 additions and 1124 deletions

View File

@ -1,8 +1,8 @@
db_url = 'postgresql://robertem@127.0.0.1/pvvvv'
db_url = "postgresql://robertem@127.0.0.1/pvvvv"
quit_allowed = True
stop_allowed = False
show_tracebacks = True
input_encoding = 'utf8'
input_encoding = "utf8"
low_credit_warning_limit = -100
user_recent_transaction_limit = 100

View File

@ -3,4 +3,4 @@
import configparser
config = configparser.ConfigParser()
config = configparser.ConfigParser()

View File

@ -3,5 +3,5 @@ from sqlalchemy.orm import sessionmaker
from dibbler.conf import config
engine = create_engine(config.get('database', 'url'))
Session = sessionmaker(bind=engine)
engine = create_engine(config.get("database", "url"))
Session = sessionmaker(bind=engine)

View File

@ -6,20 +6,20 @@ from brother_ql.devicedependent import label_type_specs
def px2mm(px, dpi=300):
return (25.4 * px)/dpi
return (25.4 * px) / dpi
class BrotherLabelWriter(ImageWriter):
def __init__(self, typ='62', max_height=350, rot=False, text=None):
def __init__(self, typ="62", max_height=350, rot=False, text=None):
super(BrotherLabelWriter, self).__init__()
assert typ in label_type_specs
self.rot = rot
if self.rot:
self._h, self._w = label_type_specs[typ]['dots_printable']
self._h, self._w = label_type_specs[typ]["dots_printable"]
if self._w == 0 or self._w > max_height:
self._w = min(max_height, self._h / 2)
else:
self._w, self._h = label_type_specs[typ]['dots_printable']
self._w, self._h = label_type_specs[typ]["dots_printable"]
if self._h == 0 or self._h > max_height:
self._h = min(max_height, self._w / 2)
self._xo = 0.0
@ -31,36 +31,40 @@ class BrotherLabelWriter(ImageWriter):
super(BrotherLabelWriter, self)._init(code)
def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
x, y = super(BrotherLabelWriter, self).calculate_size(modules_per_line, number_of_lines, dpi)
x, y = super(BrotherLabelWriter, self).calculate_size(
modules_per_line, number_of_lines, dpi
)
self._xo = (px2mm(self._w)-px2mm(x))/2
self._yo = (px2mm(self._h)-px2mm(y))
self._xo = (px2mm(self._w) - px2mm(x)) / 2
self._yo = px2mm(self._h) - px2mm(y)
assert self._xo >= 0
assert self._yo >= 0
return int(self._w), int(self._h)
def _paint_module(self, xpos, ypos, width, color):
super(BrotherLabelWriter, self)._paint_module(xpos+self._xo, ypos+self._yo, width, color)
super(BrotherLabelWriter, self)._paint_module(
xpos + self._xo, ypos + self._yo, width, color
)
def _paint_text(self, xpos, ypos):
super(BrotherLabelWriter, self)._paint_text(xpos+self._xo, ypos+self._yo)
super(BrotherLabelWriter, self)._paint_text(xpos + self._xo, ypos + self._yo)
def _finish(self):
if self._title:
width = self._w+1
width = self._w + 1
height = 0
max_h = self._h - mm2px(self._yo, self.dpi)
fs = int(max_h / 1.2)
font_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Stranger back in the Night.ttf")
font_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"Stranger back in the Night.ttf",
)
font = ImageFont.truetype(font_path, 10)
while width > self._w or height > max_h:
font = ImageFont.truetype(font_path, fs)
width, height = font.getsize(self._title)
fs -= 1
pos = (
(self._w-width)//2,
0 - (height // 8)
)
pos = ((self._w - width) // 2, 0 - (height // 8))
self._draw.text(pos, self._title, font=font, fill=self.foreground)
return self._image

View File

@ -7,31 +7,72 @@ from sqlalchemy import or_, and_
from ..models import User, Product
def search_user(string, session, ignorethisflag=None):
string = string.lower()
exact_match = session.query(User).filter(or_(User.name == string, User.card == string, User.rfid == string)).first()
exact_match = (
session.query(User)
.filter(or_(User.name == string, User.card == string, User.rfid == string))
.first()
)
if exact_match:
return exact_match
user_list = session.query(User).filter(or_(User.name.ilike(f'%{string}%'),
User.card.ilike(f'%{string}%'),
User.rfid.ilike(f'%{string}%'))).all()
user_list = (
session.query(User)
.filter(
or_(
User.name.ilike(f"%{string}%"),
User.card.ilike(f"%{string}%"),
User.rfid.ilike(f"%{string}%"),
)
)
.all()
)
return user_list
def search_product(string, session, find_hidden_products=True):
if find_hidden_products:
exact_match = session.query(Product).filter(or_(Product.bar_code == string, Product.name == string)).first()
exact_match = (
session.query(Product)
.filter(or_(Product.bar_code == string, Product.name == string))
.first()
)
else:
exact_match = session.query(Product).filter(or_(Product.bar_code == string,
and_(Product.name == string, Product.hidden == False))).first()
exact_match = (
session.query(Product)
.filter(
or_(
Product.bar_code == string,
and_(Product.name == string, Product.hidden == False),
)
)
.first()
)
if exact_match:
return exact_match
if find_hidden_products:
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
Product.name.ilike(f'%{string}%'))).all()
product_list = (
session.query(Product)
.filter(
or_(
Product.bar_code.ilike(f"%{string}%"),
Product.name.ilike(f"%{string}%"),
)
)
.all()
)
else:
product_list = session.query(Product).filter(or_(Product.bar_code.ilike(f'%{string}%'),
and_(Product.name.ilike(f'%{string}%'),
Product.hidden == False))).all()
product_list = (
session.query(Product)
.filter(
or_(
Product.bar_code.ilike(f"%{string}%"),
and_(Product.name.ilike(f"%{string}%"), Product.hidden == False),
)
)
.all()
)
return product_list
@ -45,61 +86,21 @@ def system_user_exists(username):
else:
return True
def guess_data_type(string):
if string.startswith('ntnu') and string[4:].isdigit():
return 'card'
if string.startswith("ntnu") and string[4:].isdigit():
return "card"
if string.isdigit() and len(string) == 10:
return 'rfid'
if string.isdigit() and len(string) in [8,13]:
return 'bar_code'
# if string.isdigit() and len(string) > 5:
# return 'card'
return "rfid"
if string.isdigit() and len(string) in [8, 13]:
return "bar_code"
# if string.isdigit() and len(string) > 5:
# return 'card'
if string.isalpha() and string.islower() and system_user_exists(string):
return 'username'
return "username"
return None
# def retrieve_user(string, session):
# # first = session.query(User).filter(or_(User.name==string, User.card==string)).first()
# search = search_user(string,session)
# if isinstance(search,User):
# print "Found user "+search.name
# return search
# else:
# if len(search) == 0:
# print "No users found matching your search"
# return None
# if len(search) == 1:
# print "Found one user: "+list[0].name
# if confirm():
# return list[0]
# else:
# return None
# else:
# print "Found "+str(len(search))+" users:"
# return select_from_list(search)
# def confirm(prompt='Confirm? (y/n) '):
# while True:
# input = raw_input(prompt)
# if input in ["y","yes"]:
# return True
# elif input in ["n","no"]:
# return False
# else:
# print "Nonsense!"
# def select_from_list(list):
# while True:
# for i in range(len(list)):
# print i+1, " ) ", list[i].name
# choice = raw_input("Select user :\n")
# if choice in [str(x+1) for x in range(len(list))]:
# return list[int(choice)-1]
# else:
# return None
def argmax(d, all=False, value=None):
maxarg = None
maxargs = []
@ -117,14 +118,14 @@ def argmax(d, all=False, value=None):
def less(string):
'''
"""
Run less with string as input; wait until it finishes.
'''
"""
# If we don't ignore SIGINT while running the `less` process,
# it will become a zombie when someone presses C-c.
int_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
env = dict(os.environ)
env['LESSSECURE'] = '1'
proc = subprocess.Popen('less', env=env, encoding='utf-8', stdin=subprocess.PIPE)
env["LESSSECURE"] = "1"
proc = subprocess.Popen("less", env=env, encoding="utf-8", stdin=subprocess.PIPE)
proc.communicate(string)
signal.signal(signal.SIGINT, int_handler)

View File

@ -10,35 +10,41 @@ from PIL import Image, ImageDraw, ImageFont
from .barcode_helpers import BrotherLabelWriter
def print_name_label(text, margin=10, rotate=False, label_type="62", printer_type="QL-700",):
def print_name_label(
text,
margin=10,
rotate=False,
label_type="62",
printer_type="QL-700",
):
if not rotate:
width, height = label_type_specs[label_type]['dots_printable']
width, height = label_type_specs[label_type]["dots_printable"]
else:
height, width = label_type_specs[label_type]['dots_printable']
height, width = label_type_specs[label_type]["dots_printable"]
font_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "ChopinScript.ttf")
fs = 2000
tw, th = width, height
if width == 0:
while th + 2*margin > height:
while th + 2 * margin > height:
font = ImageFont.truetype(font_path, fs)
tw, th = font.getsize(text)
fs -= 1
width = tw+2*margin
width = tw + 2 * margin
elif height == 0:
while tw + 2*margin > width:
while tw + 2 * margin > width:
font = ImageFont.truetype(font_path, fs)
tw, th = font.getsize(text)
fs -= 1
height = th+2*margin
height = th + 2 * margin
else:
while tw + 2*margin > width or th + 2*margin > height:
while tw + 2 * margin > width or th + 2 * margin > height:
font = ImageFont.truetype(font_path, fs)
tw, th = font.getsize(text)
fs -= 1
xp = (width//2)-(tw//2)
yp = (height//2)-(th//2)
xp = (width // 2) - (tw // 2)
yp = (height // 2) - (th // 2)
im = Image.new("RGB", (width, height), (255, 255, 255))
dr = ImageDraw.Draw(im)
@ -55,8 +61,14 @@ def print_name_label(text, margin=10, rotate=False, label_type="62", printer_typ
print_image(fn, printer_type, label_type)
def print_bar_code(barcode_value, barcode_text, barcode_type="ean13", rotate=False, printer_type="QL-700",
label_type="62"):
def print_bar_code(
barcode_value,
barcode_text,
barcode_type="ean13",
rotate=False,
printer_type="QL-700",
label_type="62",
):
bar_coder = barcode.get_barcode_class(barcode_type)
wr = BrotherLabelWriter(typ=label_type, rot=rotate, text=barcode_text, max_height=1000)
@ -72,12 +84,12 @@ def print_image(fn, printer_type="QL-700", label_type="62"):
create_label(qlr, fn, label_type, threshold=70, cut=True)
be = backend_factory("pyusb")
list_available_devices = be['list_available_devices']
BrotherQLBackend = be['backend_class']
list_available_devices = be["list_available_devices"]
BrotherQLBackend = be["backend_class"]
ad = list_available_devices()
assert ad
string_descr = ad[0]['string_descr']
string_descr = ad[0]["string_descr"]
printer = BrotherQLBackend(string_descr)

View File

@ -8,411 +8,507 @@ from .helpers import *
from ..models import Transaction
from ..db import Session
def getUser():
while 1:
string = input('user? ')
session = Session()
user = search_user(string, session)
session.close()
if not isinstance(user, list):
return user.name
i=0
if len(user)==0:
print('no matching string')
if len(user)==1:
print('antar: ', user[0].name, '\n')
return user[0].name
if len(user)>10:
continue
for u in user:
print(i, u.name)
i += 1
try:
n = int(input ('enter number:'))
except:
print('invalid input, restarting')
continue
if (n>-1) and (n<i):
return user[n].name
def getProduct():
while 1:
string = input('product? ')
session = Session()
product = search_product(string, session)
session.close()
if not isinstance(product, list):
return product.name
i=0
if len(product)==0:
print('no matching string')
if len(product)==1:
print('antar: ', product[0].name, '\n')
return product[0].name
if len(product)>10:
continue
for u in product:
print(i, u.name)
i += 1
try:
n = int(input ('enter number:'))
except:
print('invalid input, restarting')
continue
if (n>-1) and (n<i):
return product[n].name
while 1:
string = input("user? ")
session = Session()
user = search_user(string, session)
session.close()
if not isinstance(user, list):
return user.name
i = 0
if len(user) == 0:
print("no matching string")
if len(user) == 1:
print("antar: ", user[0].name, "\n")
return user[0].name
if len(user) > 10:
continue
for u in user:
print(i, u.name)
i += 1
try:
n = int(input("enter number:"))
except:
print("invalid input, restarting")
continue
if (n > -1) and (n < i):
return user[n].name
def getProduct():
while 1:
string = input("product? ")
session = Session()
product = search_product(string, session)
session.close()
if not isinstance(product, list):
return product.name
i = 0
if len(product) == 0:
print("no matching string")
if len(product) == 1:
print("antar: ", product[0].name, "\n")
return product[0].name
if len(product) > 10:
continue
for u in product:
print(i, u.name)
i += 1
try:
n = int(input("enter number:"))
except:
print("invalid input, restarting")
continue
if (n > -1) and (n < i):
return product[n].name
class Database:
#for varer
varePersonAntall = defaultdict(dict) #varePersonAntall[Oreo][trygvrad] == 3
vareDatoAntall = defaultdict(list) #dict->array
vareUkedagAntall = defaultdict(list)
#for personer
personVareAntall = defaultdict(dict) #personVareAntall[trygvrad][Oreo] == 3
personVareVerdi = defaultdict(dict) #personVareVerdi[trygvrad][Oreo] == 30 #[kr]
personDatoVerdi = defaultdict(list) #dict->array
personUkedagVerdi = defaultdict(list)
#for global
personPosTransactions = {} # personPosTransactions[trygvrad] == 100 #trygvrad har lagt 100kr i boksen
personNegTransactions = {} # personNegTransactions[trygvrad» == 70 #trygvrad har tatt 70kr fra boksen
globalVareAntall = {}#globalVareAntall[Oreo] == 3
globalVareVerdi = {}#globalVareVerdi[Oreo] == 30 #[kr]
globalPersonAntall = {}#globalPersonAntall[trygvrad] == 3
globalPersonForbruk = {}#globalPersonVerdi == 30 #[kr]
globalUkedagForbruk = []
globalDatoVarer = []
globalDatoForbruk = []
pengebeholdning = []
# for varer
varePersonAntall = defaultdict(dict) # varePersonAntall[Oreo][trygvrad] == 3
vareDatoAntall = defaultdict(list) # dict->array
vareUkedagAntall = defaultdict(list)
# for personer
personVareAntall = defaultdict(dict) # personVareAntall[trygvrad][Oreo] == 3
personVareVerdi = defaultdict(dict) # personVareVerdi[trygvrad][Oreo] == 30 #[kr]
personDatoVerdi = defaultdict(list) # dict->array
personUkedagVerdi = defaultdict(list)
# for global
personPosTransactions = (
{}
) # personPosTransactions[trygvrad] == 100 #trygvrad har lagt 100kr i boksen
personNegTransactions = (
{}
) # personNegTransactions[trygvrad» == 70 #trygvrad har tatt 70kr fra boksen
globalVareAntall = {} # globalVareAntall[Oreo] == 3
globalVareVerdi = {} # globalVareVerdi[Oreo] == 30 #[kr]
globalPersonAntall = {} # globalPersonAntall[trygvrad] == 3
globalPersonForbruk = {} # globalPersonVerdi == 30 #[kr]
globalUkedagForbruk = []
globalDatoVarer = []
globalDatoForbruk = []
pengebeholdning = []
class InputLine:
def __init__(self, u, p, t):
self.inputUser = u
self.inputProduct = p
self.inputType = t
def __init__(self, u, p, t):
self.inputUser = u
self.inputProduct = p
self.inputType = t
def getDateDb(date, inp):
try:
year = inp.partition('-')
month = year[2].partition('-')
return datetime.datetime(int(year[0]), int(month[0]), int(month[2]))
except:
print('invalid date, setting date to date found in db')
print(date)
return date
try:
year = inp.partition("-")
month = year[2].partition("-")
return datetime.datetime(int(year[0]), int(month[0]), int(month[2]))
except:
print("invalid date, setting date to date found in db")
print(date)
return date
def dateToDateNumDb(date, startDate):
deltaDays = date-startDate
return int(deltaDays.days), date.weekday()
deltaDays = date - startDate
return int(deltaDays.days), date.weekday()
def getInputType():
inp = 0
while not (inp == '1' or inp == '2' or inp == '3' or inp == '4'):
print('type 1 for user-statistics')
print('type 2 for product-statistics')
print('type 3 for global-statistics')
print('type 4 to enter loop-mode')
inp = input('')
return int(inp)
inp = 0
while not (inp == "1" or inp == "2" or inp == "3" or inp == "4"):
print("type 1 for user-statistics")
print("type 2 for product-statistics")
print("type 3 for global-statistics")
print("type 4 to enter loop-mode")
inp = input("")
return int(inp)
def getProducts(products):
product = []
products = products.partition('¤')
product.append(products[0])
while (products[1]=='¤'):
products = products[2].partition('¤')
product.append(products[0])
return product
product = []
products = products.partition("¤")
product.append(products[0])
while products[1] == "¤":
products = products[2].partition("¤")
product.append(products[0])
return product
def getDateFile(date, inp):
try:
year = inp.partition('-')
month = year[2].partition('-')
return datetime.date(int(year[0]), int(month[0]), int(month[2]))
except:
print('invalid date, setting date to date found on file file')
print(date)
return datetime.date(int(date.partition('-')[0]), int(date.partition('-')[2].partition('-')[0]), int(date.partition('-')[2].partition('-')[2]))
try:
year = inp.partition("-")
month = year[2].partition("-")
return datetime.date(int(year[0]), int(month[0]), int(month[2]))
except:
print("invalid date, setting date to date found on file file")
print(date)
return datetime.date(
int(date.partition("-")[0]),
int(date.partition("-")[2].partition("-")[0]),
int(date.partition("-")[2].partition("-")[2]),
)
def dateToDateNumFile(date, startDate):
year = date.partition('-')
month = year[2].partition('-')
day = datetime.date(int(year[0]), int(month[0]), int(month[2]))
deltaDays = day-startDate
return int(deltaDays.days), day.weekday()
year = date.partition("-")
month = year[2].partition("-")
day = datetime.date(int(year[0]), int(month[0]), int(month[2]))
deltaDays = day - startDate
return int(deltaDays.days), day.weekday()
def clearDatabase(database):
database.varePersonAntall.clear()
database.vareDatoAntall.clear()
database.vareUkedagAntall.clear()
database.personVareAntall.clear()
database.personVareVerdi.clear()
database.personDatoVerdi.clear()
database.personUkedagVerdi.clear()
database.personPosTransactions.clear()
database.personNegTransactions.clear()
database.globalVareAntall.clear()
database.globalVareVerdi.clear()
database.globalPersonAntall.clear()
database.globalPersonForbruk.clear()
return(database)
database.varePersonAntall.clear()
database.vareDatoAntall.clear()
database.vareUkedagAntall.clear()
database.personVareAntall.clear()
database.personVareVerdi.clear()
database.personDatoVerdi.clear()
database.personUkedagVerdi.clear()
database.personPosTransactions.clear()
database.personNegTransactions.clear()
database.globalVareAntall.clear()
database.globalVareVerdi.clear()
database.globalPersonAntall.clear()
database.globalPersonForbruk.clear()
return database
def addLineToDatabase(database, inputLine):
if abs(inputLine.price)>90000:
return database
#fyller inn for varer
if (not inputLine.product=='') and ((inputLine.inputProduct=='') or (inputLine.inputProduct==inputLine.product)):
database.varePersonAntall[inputLine.product][inputLine.user] = database.varePersonAntall[inputLine.product].setdefault(inputLine.user,0) + 1
if inputLine.product not in database.vareDatoAntall:
database.vareDatoAntall[inputLine.product] = [0]*(inputLine.numberOfDays+1)
database.vareDatoAntall[inputLine.product][inputLine.dateNum] += 1
if inputLine.product not in database.vareUkedagAntall:
database.vareUkedagAntall[inputLine.product] = [0]*7
database.vareUkedagAntall[inputLine.product][inputLine.weekday] += 1
#fyller inn for personer
if (inputLine.inputUser=='') or (inputLine.inputUser==inputLine.user):
if not inputLine.product == '':
database.personVareAntall[inputLine.user][inputLine.product] = database.personVareAntall[inputLine.user].setdefault(inputLine.product,0) + 1
database.personVareVerdi[inputLine.user][inputLine.product] = database.personVareVerdi[inputLine.user].setdefault(inputLine.product,0) + inputLine.price
if inputLine.user not in database.personDatoVerdi:
database.personDatoVerdi[inputLine.user] = [0]*(inputLine.numberOfDays+1)
database.personDatoVerdi[inputLine.user][inputLine.dateNum] += inputLine.price
if inputLine.user not in database.personUkedagVerdi:
database.personUkedagVerdi[inputLine.user] = [0]*7
database.personUkedagVerdi[inputLine.user][inputLine.weekday] += inputLine.price
#fyller inn delt statistikk (genereres uansett)
if (inputLine.product==''):
if (inputLine.price>0):
database.personPosTransactions[inputLine.user] = database.personPosTransactions.setdefault(inputLine.user,0) + inputLine.price
else:
database.personNegTransactions[inputLine.user] = database.personNegTransactions.setdefault(inputLine.user,0) + inputLine.price
elif not (inputLine.inputType==1):
database.globalVareAntall[inputLine.product] = database.globalVareAntall.setdefault(inputLine.product,0) + 1
database.globalVareVerdi[inputLine.product] = database.globalVareVerdi.setdefault(inputLine.product,0) + inputLine.price
#fyller inn for global statistikk
if (inputLine.inputType==3) or (inputLine.inputType==4):
database.pengebeholdning[inputLine.dateNum] += inputLine.price
if not (inputLine.product==''):
database.globalPersonAntall[inputLine.user] = database.globalPersonAntall.setdefault(inputLine.user,0) + 1
database.globalPersonForbruk[inputLine.user] = database.globalPersonForbruk.setdefault(inputLine.user,0) + inputLine.price
database.globalDatoVarer[inputLine.dateNum] += 1
database.globalDatoForbruk[inputLine.dateNum] += inputLine.price
database.globalUkedagForbruk[inputLine.weekday] += inputLine.price
return database
if abs(inputLine.price) > 90000:
return database
# fyller inn for varer
if (not inputLine.product == "") and (
(inputLine.inputProduct == "") or (inputLine.inputProduct == inputLine.product)
):
database.varePersonAntall[inputLine.product][inputLine.user] = (
database.varePersonAntall[inputLine.product].setdefault(inputLine.user, 0) + 1
)
if inputLine.product not in database.vareDatoAntall:
database.vareDatoAntall[inputLine.product] = [0] * (inputLine.numberOfDays + 1)
database.vareDatoAntall[inputLine.product][inputLine.dateNum] += 1
if inputLine.product not in database.vareUkedagAntall:
database.vareUkedagAntall[inputLine.product] = [0] * 7
database.vareUkedagAntall[inputLine.product][inputLine.weekday] += 1
# fyller inn for personer
if (inputLine.inputUser == "") or (inputLine.inputUser == inputLine.user):
if not inputLine.product == "":
database.personVareAntall[inputLine.user][inputLine.product] = (
database.personVareAntall[inputLine.user].setdefault(inputLine.product, 0) + 1
)
database.personVareVerdi[inputLine.user][inputLine.product] = (
database.personVareVerdi[inputLine.user].setdefault(inputLine.product, 0)
+ inputLine.price
)
if inputLine.user not in database.personDatoVerdi:
database.personDatoVerdi[inputLine.user] = [0] * (inputLine.numberOfDays + 1)
database.personDatoVerdi[inputLine.user][inputLine.dateNum] += inputLine.price
if inputLine.user not in database.personUkedagVerdi:
database.personUkedagVerdi[inputLine.user] = [0] * 7
database.personUkedagVerdi[inputLine.user][inputLine.weekday] += inputLine.price
# fyller inn delt statistikk (genereres uansett)
if inputLine.product == "":
if inputLine.price > 0:
database.personPosTransactions[inputLine.user] = (
database.personPosTransactions.setdefault(inputLine.user, 0) + inputLine.price
)
else:
database.personNegTransactions[inputLine.user] = (
database.personNegTransactions.setdefault(inputLine.user, 0) + inputLine.price
)
elif not (inputLine.inputType == 1):
database.globalVareAntall[inputLine.product] = (
database.globalVareAntall.setdefault(inputLine.product, 0) + 1
)
database.globalVareVerdi[inputLine.product] = (
database.globalVareVerdi.setdefault(inputLine.product, 0) + inputLine.price
)
# fyller inn for global statistikk
if (inputLine.inputType == 3) or (inputLine.inputType == 4):
database.pengebeholdning[inputLine.dateNum] += inputLine.price
if not (inputLine.product == ""):
database.globalPersonAntall[inputLine.user] = (
database.globalPersonAntall.setdefault(inputLine.user, 0) + 1
)
database.globalPersonForbruk[inputLine.user] = (
database.globalPersonForbruk.setdefault(inputLine.user, 0) + inputLine.price
)
database.globalDatoVarer[inputLine.dateNum] += 1
database.globalDatoForbruk[inputLine.dateNum] += inputLine.price
database.globalUkedagForbruk[inputLine.weekday] += inputLine.price
return database
def buildDatabaseFromDb(inputType, inputProduct, inputUser):
sdate = input('enter start date (yyyy-mm-dd)? ')
edate = input('enter end date (yyyy-mm-dd)? ')
print('building database...')
session = Session()
transaction_list = session.query(Transaction).all()
inputLine = InputLine(inputUser, inputProduct, inputType)
startDate = getDateDb(transaction_list[0].time, sdate)
endDate = getDateDb(transaction_list[-1].time, edate)
inputLine.numberOfDays = (endDate-startDate).days
database = Database()
database = clearDatabase(database)
sdate = input("enter start date (yyyy-mm-dd)? ")
edate = input("enter end date (yyyy-mm-dd)? ")
print("building database...")
session = Session()
transaction_list = session.query(Transaction).all()
inputLine = InputLine(inputUser, inputProduct, inputType)
startDate = getDateDb(transaction_list[0].time, sdate)
endDate = getDateDb(transaction_list[-1].time, edate)
inputLine.numberOfDays = (endDate - startDate).days
database = Database()
database = clearDatabase(database)
if (inputType==3) or (inputType==4):
database.globalDatoVarer = [0]*(inputLine.numberOfDays+1)
database.globalDatoForbruk = [0]*(inputLine.numberOfDays+1)
database.globalUkedagForbruk = [0]*7
database.pengebeholdning = [0]*(inputLine.numberOfDays+1)
print('wait for it.... ')
for transaction in transaction_list:
if transaction.purchase:
products = [ent.product.name for ent in transaction.purchase.entries]
else:
products = []
products.append('')
inputLine.dateNum, inputLine.weekday = dateToDateNumDb(transaction.time, startDate)
if inputLine.dateNum<0 or inputLine.dateNum>(inputLine.numberOfDays):
continue
inputLine.user=transaction.user.name
inputLine.price=transaction.amount
for inputLine.product in products:
database=addLineToDatabase(database, inputLine )
inputLine.price = 0;
if (inputType == 3) or (inputType == 4):
database.globalDatoVarer = [0] * (inputLine.numberOfDays + 1)
database.globalDatoForbruk = [0] * (inputLine.numberOfDays + 1)
database.globalUkedagForbruk = [0] * 7
database.pengebeholdning = [0] * (inputLine.numberOfDays + 1)
print("wait for it.... ")
for transaction in transaction_list:
if transaction.purchase:
products = [ent.product.name for ent in transaction.purchase.entries]
else:
products = []
products.append("")
inputLine.dateNum, inputLine.weekday = dateToDateNumDb(transaction.time, startDate)
if inputLine.dateNum < 0 or inputLine.dateNum > (inputLine.numberOfDays):
continue
inputLine.user = transaction.user.name
inputLine.price = transaction.amount
for inputLine.product in products:
database = addLineToDatabase(database, inputLine)
inputLine.price = 0
print("saving as default.dibblerlog...", end=" ")
f = open("default.dibblerlog", "w")
line_format = "%s|%s|%s|%s|%s|%s\n"
transaction_list = session.query(Transaction).all()
for transaction in transaction_list:
if transaction.purchase:
products = "¤".join([ent.product.name for ent in transaction.purchase.entries])
description = ""
else:
products = ""
description = transaction.description
line = line_format % (
"purchase",
transaction.time,
products,
transaction.user.name,
transaction.amount,
transaction.description,
)
f.write(line.encode("utf8"))
session.close()
f.close
# bygg database.pengebeholdning
if (inputType == 3) or (inputType == 4):
for i in range(inputLine.numberOfDays + 1):
if i > 0:
database.pengebeholdning[i] += database.pengebeholdning[i - 1]
# bygg dateLine
day = datetime.timedelta(days=1)
dateLine = []
dateLine.append(startDate)
for n in range(inputLine.numberOfDays):
dateLine.append(startDate + n * day)
print("done")
return database, dateLine
print('saving as default.dibblerlog...', end=' ')
f=open('default.dibblerlog','w')
line_format = '%s|%s|%s|%s|%s|%s\n'
transaction_list = session.query(Transaction).all()
for transaction in transaction_list:
if transaction.purchase:
products = '¤'.join([ent.product.name for ent in transaction.purchase.entries])
description = ''
else:
products = ''
description = transaction.description
line = line_format % ('purchase', transaction.time, products, transaction.user.name, transaction.amount, transaction.description)
f.write(line.encode('utf8'))
session.close()
f.close
#bygg database.pengebeholdning
if (inputType==3) or (inputType==4):
for i in range(inputLine.numberOfDays+1):
if i > 0:
database.pengebeholdning[i] +=database.pengebeholdning[i-1]
#bygg dateLine
day=datetime.timedelta(days=1)
dateLine=[]
dateLine.append(startDate)
for n in range(inputLine.numberOfDays):
dateLine.append(startDate+n*day)
print('done')
return database, dateLine
def buildDatabaseFromFile(inputFile, inputType, inputProduct, inputUser):
sdate = input('enter start date (yyyy-mm-dd)? ')
edate = input('enter end date (yyyy-mm-dd)? ')
f=open(inputFile)
try:
fileLines=f.readlines()
finally:
f.close()
inputLine = InputLine(inputUser, inputProduct, inputType)
startDate = getDateFile(fileLines[0].partition('|')[2].partition(' ')[0], sdate)
endDate = getDateFile(fileLines[-1].partition('|')[2].partition(' ')[0], edate)
inputLine.numberOfDays = (endDate-startDate).days
database = Database()
database = clearDatabase(database)
sdate = input("enter start date (yyyy-mm-dd)? ")
edate = input("enter end date (yyyy-mm-dd)? ")
f = open(inputFile)
try:
fileLines = f.readlines()
finally:
f.close()
inputLine = InputLine(inputUser, inputProduct, inputType)
startDate = getDateFile(fileLines[0].partition("|")[2].partition(" ")[0], sdate)
endDate = getDateFile(fileLines[-1].partition("|")[2].partition(" ")[0], edate)
inputLine.numberOfDays = (endDate - startDate).days
database = Database()
database = clearDatabase(database)
if (inputType == 3) or (inputType == 4):
database.globalDatoVarer = [0] * (inputLine.numberOfDays + 1)
database.globalDatoForbruk = [0] * (inputLine.numberOfDays + 1)
database.globalUkedagForbruk = [0] * 7
database.pengebeholdning = [0] * (inputLine.numberOfDays + 1)
for linje in fileLines:
if not (linje[0] == "#") and not (linje == "\n"):
# henter dateNum, products, user, price
restDel = linje.partition("|")
restDel = restDel[2].partition(" ")
inputLine.dateNum, inputLine.weekday = dateToDateNumFile(restDel[0], startDate)
if inputLine.dateNum < 0 or inputLine.dateNum > (inputLine.numberOfDays):
continue
restDel = restDel[2].partition("|")
restDel = restDel[2].partition("|")
products = restDel[0]
restDel = restDel[2].partition("|")
inputLine.user = restDel[0]
inputLine.price = int(restDel[2].partition("|")[0])
for inputLine.product in getProducts(products):
database = addLineToDatabase(database, inputLine)
inputLine.price = 0
# bygg database.pengebeholdning
if (inputType == 3) or (inputType == 4):
for i in range(inputLine.numberOfDays + 1):
if i > 0:
database.pengebeholdning[i] += database.pengebeholdning[i - 1]
# bygg dateLine
day = datetime.timedelta(days=1)
dateLine = []
dateLine.append(startDate)
for n in range(inputLine.numberOfDays):
dateLine.append(startDate + n * day)
return database, dateLine
if (inputType==3) or (inputType==4):
database.globalDatoVarer = [0]*(inputLine.numberOfDays+1)
database.globalDatoForbruk = [0]*(inputLine.numberOfDays+1)
database.globalUkedagForbruk = [0]*7
database.pengebeholdning = [0]*(inputLine.numberOfDays+1)
for linje in fileLines:
if not (linje[0]=='#') and not (linje=='\n') :
#henter dateNum, products, user, price
restDel = linje.partition('|')
restDel = restDel[2].partition(' ')
inputLine.dateNum, inputLine.weekday = dateToDateNumFile(restDel[0], startDate)
if inputLine.dateNum<0 or inputLine.dateNum>(inputLine.numberOfDays):
continue
restDel=restDel[2].partition('|')
restDel=restDel[2].partition('|')
products = restDel[0]
restDel=restDel[2].partition('|')
inputLine.user=restDel[0]
inputLine.price=int(restDel[2].partition('|')[0])
for inputLine.product in getProducts(products):
database=addLineToDatabase(database, inputLine )
inputLine.price = 0;
#bygg database.pengebeholdning
if (inputType==3) or (inputType==4):
for i in range(inputLine.numberOfDays+1):
if i > 0:
database.pengebeholdning[i] +=database.pengebeholdning[i-1]
#bygg dateLine
day=datetime.timedelta(days=1)
dateLine=[]
dateLine.append(startDate)
for n in range(inputLine.numberOfDays):
dateLine.append(startDate+n*day)
return database, dateLine
def printTopDict(dictionary, n, k):
i=0
for key in sorted(dictionary, key=dictionary.get, reverse=k):
print(key, ': ',dictionary[key])
if i<n:
i += 1
else:
break
i = 0
for key in sorted(dictionary, key=dictionary.get, reverse=k):
print(key, ": ", dictionary[key])
if i < n:
i += 1
else:
break
def printTopDict2(dictionary, dictionary2, n):
print('')
print('product : price[kr] ( number )')
i=0
for key in sorted(dictionary, key=dictionary.get, reverse=True):
print(key, ': ',dictionary[key], ' (', dictionary2[key], ') ')
if i<n:
i += 1
else:
break
print("")
print("product : price[kr] ( number )")
i = 0
for key in sorted(dictionary, key=dictionary.get, reverse=True):
print(key, ": ", dictionary[key], " (", dictionary2[key], ") ")
if i < n:
i += 1
else:
break
def printWeekdays(week, days):
if week==[] or days==0:
return
print('mon: ', '%.2f'%(week[0]*7.0/days), ' tue: ', '%.2f'%(week[1]*7.0/days), ' wen: ', '%.2f'%(week[2]*7.0/days), ' thu: ', '%.2f'%(week[3]*7.0/days), ' fri: ', '%.2f'%(week[4]*7.0/days), ' sat: ','%.2f'%( week[5]*7.0/days), ' sun: ', '%.2f'%(week[6]*7.0/days))
print('forbruk per dag (snitt): ', '%.2f'%(sum(week)*1.0/days))
print('')
if week == [] or days == 0:
return
print(
"mon: ",
"%.2f" % (week[0] * 7.0 / days),
" tue: ",
"%.2f" % (week[1] * 7.0 / days),
" wen: ",
"%.2f" % (week[2] * 7.0 / days),
" thu: ",
"%.2f" % (week[3] * 7.0 / days),
" fri: ",
"%.2f" % (week[4] * 7.0 / days),
" sat: ",
"%.2f" % (week[5] * 7.0 / days),
" sun: ",
"%.2f" % (week[6] * 7.0 / days),
)
print("forbruk per dag (snitt): ", "%.2f" % (sum(week) * 1.0 / days))
print("")
def printBalance(database, user):
forbruk = 0
if (user in database.personVareVerdi):
forbruk = sum([i for i in list(database.personVareVerdi[user].values())])
print('totalt kjøpt for: ', forbruk, end=' ')
if (user in database.personNegTransactions):
print('kr, totalt lagt til: ', -database.personNegTransactions[user], end=' ')
forbruk=-database.personNegTransactions[user]-forbruk
if (user in database.personPosTransactions):
print('kr, totalt tatt fra boks: ', database.personPosTransactions[user], end=' ')
forbruk=forbruk-database.personPosTransactions[user]
print('balanse: ', forbruk, 'kr', end=' ')
print('')
forbruk = 0
if user in database.personVareVerdi:
forbruk = sum([i for i in list(database.personVareVerdi[user].values())])
print("totalt kjøpt for: ", forbruk, end=" ")
if user in database.personNegTransactions:
print("kr, totalt lagt til: ", -database.personNegTransactions[user], end=" ")
forbruk = -database.personNegTransactions[user] - forbruk
if user in database.personPosTransactions:
print("kr, totalt tatt fra boks: ", database.personPosTransactions[user], end=" ")
forbruk = forbruk - database.personPosTransactions[user]
print("balanse: ", forbruk, "kr", end=" ")
print("")
def printUser(database, dateLine, user, n):
printTopDict2(database.personVareVerdi[user], database.personVareAntall[user], n)
print('\nforbruk per ukedag [kr/dag],', end=' ')
printWeekdays(database.personUkedagVerdi[user], len(dateLine))
printBalance(database, user)
printTopDict2(database.personVareVerdi[user], database.personVareAntall[user], n)
print("\nforbruk per ukedag [kr/dag],", end=" ")
printWeekdays(database.personUkedagVerdi[user], len(dateLine))
printBalance(database, user)
def printProduct(database, dateLine, product, n):
printTopDict(database.varePersonAntall[product], n, 1)
print('\nforbruk per ukedag [antall/dag],', end=' ')
printWeekdays(database.vareUkedagAntall[product], len(dateLine))
print('Det er solgt: ', database.globalVareAntall[product], product, 'til en verdi av: ', database.globalVareVerdi[product], 'kr')
printTopDict(database.varePersonAntall[product], n, 1)
print("\nforbruk per ukedag [antall/dag],", end=" ")
printWeekdays(database.vareUkedagAntall[product], len(dateLine))
print(
"Det er solgt: ",
database.globalVareAntall[product],
product,
"til en verdi av: ",
database.globalVareVerdi[product],
"kr",
)
def printGlobal(database, dateLine, n):
print('\nmest lagt til: ')
printTopDict(database.personNegTransactions, n, 0)
print('\nmest tatt fra:')
printTopDict(database.personPosTransactions, n, 1)
print('\nstørst forbruk:')
printTopDict(database.globalPersonForbruk, n, 1)
printTopDict2(database.globalVareVerdi, database.globalVareAntall, n)
print('\nforbruk per ukedag [kr/dag],', end=' ')
printWeekdays(database.globalUkedagForbruk, len(dateLine))
print('Det er solgt varer til en verdi av: ', sum(database.globalDatoForbruk), 'kr, det er lagt til', -sum([i for i in list(database.personNegTransactions.values())]), 'og tatt fra', sum([i for i in list(database.personPosTransactions.values())]), end=' ')
print('balansen blir:', database.pengebeholdning[len(dateLine)-1], 'der negative verdier representerer at brukere har kreditt tilgjengelig')
print("\nmest lagt til: ")
printTopDict(database.personNegTransactions, n, 0)
print("\nmest tatt fra:")
printTopDict(database.personPosTransactions, n, 1)
print("\nstørst forbruk:")
printTopDict(database.globalPersonForbruk, n, 1)
printTopDict2(database.globalVareVerdi, database.globalVareAntall, n)
print("\nforbruk per ukedag [kr/dag],", end=" ")
printWeekdays(database.globalUkedagForbruk, len(dateLine))
print(
"Det er solgt varer til en verdi av: ",
sum(database.globalDatoForbruk),
"kr, det er lagt til",
-sum([i for i in list(database.personNegTransactions.values())]),
"og tatt fra",
sum([i for i in list(database.personPosTransactions.values())]),
end=" ",
)
print(
"balansen blir:",
database.pengebeholdning[len(dateLine) - 1],
"der negative verdier representerer at brukere har kreditt tilgjengelig",
)
def alt4menuTextOnly(database, dateLine):
n=10
while 1:
print('\n1: user-statistics, 2: product-statistics, 3:global-statistics, n: adjust amount of data shown q:quit')
inp = input('')
if inp == 'q':
break
elif inp == '1':
try:
printUser(database, dateLine, getUser(), n)
except:
print('\n\nSomething is not right, (last date prior to first date?)')
elif inp == '2':
try:
printProduct(database, dateLine, getProduct(), n)
except:
print('\n\nSomething is not right, (last date prior to first date?)')
elif inp == '3':
try:
printGlobal(database, dateLine, n)
except:
print('\n\nSomething is not right, (last date prior to first date?)')
elif inp == 'n':
n=int(input('set number to show '));
n = 10
while 1:
print(
"\n1: user-statistics, 2: product-statistics, 3:global-statistics, n: adjust amount of data shown q:quit"
)
inp = input("")
if inp == "q":
break
elif inp == "1":
try:
printUser(database, dateLine, getUser(), n)
except:
print("\n\nSomething is not right, (last date prior to first date?)")
elif inp == "2":
try:
printProduct(database, dateLine, getProduct(), n)
except:
print("\n\nSomething is not right, (last date prior to first date?)")
elif inp == "3":
try:
printGlobal(database, dateLine, n)
except:
print("\n\nSomething is not right, (last date prior to first date?)")
elif inp == "n":
n = int(input("set number to show "))
def statisticsTextOnly():
inputType = 4
product = ''
user = ''
print('\n0: from file, 1: from database, q:quit')
inp = input('')
if inp == '1':
database, dateLine = buildDatabaseFromDb(inputType, product, user)
elif inp=='0' or inp == '':
database, dateLine = buildDatabaseFromFile('default.dibblerlog', inputType, product, user)
if not inp == 'q':
alt4menuTextOnly(database, dateLine)
inputType = 4
product = ""
user = ""
print("\n0: from file, 1: from database, q:quit")
inp = input("")
if inp == "1":
database, dateLine = buildDatabaseFromDb(inputType, product, user)
elif inp == "0" or inp == "":
database, dateLine = buildDatabaseFromFile("default.dibblerlog", inputType, product, user)
if not inp == "q":
alt4menuTextOnly(database, dateLine)

View File

@ -13,39 +13,34 @@ parser.add_argument(
)
subparsers = parser.add_subparsers(
title='subcommands',
dest='subcommand',
title="subcommands",
dest="subcommand",
required=True,
)
subparsers.add_parser(
'loop',
help='Run the dibbler loop'
)
subparsers.add_parser(
'create-db',
help='Create the database'
)
subparsers.add_parser(
'slabbedasker',
help='Find out who is slabbedasker'
)
subparsers.add_parser("loop", help="Run the dibbler loop")
subparsers.add_parser("create-db", help="Create the database")
subparsers.add_parser("slabbedasker", help="Find out who is slabbedasker")
def main():
args = parser.parse_args()
config.read(args.config)
if args.subcommand == 'loop':
import dibbler.subcommands.loop as loop
loop.main()
if args.subcommand == "loop":
import dibbler.subcommands.loop as loop
elif args.subcommand == 'create-db':
import dibbler.subcommands.makedb as makedb
makedb.main()
loop.main()
elif args.subcommand == 'slabbedasker':
import dibbler.subcommands.slabbedasker as slabbedasker
slabbedasker.main()
elif args.subcommand == "create-db":
import dibbler.subcommands.makedb as makedb
makedb.main()
elif args.subcommand == "slabbedasker":
import dibbler.subcommands.slabbedasker as slabbedasker
slabbedasker.main()
if __name__ == "__main__":
main()
main()

View File

@ -21,8 +21,8 @@ from .miscmenus import (
)
from .printermenu import PrintLabelMenu
from .stats import (
ProductPopularityMenu,
ProductRevenueMenu,
BalanceMenu,
LoggedStatisticsMenu
ProductPopularityMenu,
ProductRevenueMenu,
BalanceMenu,
LoggedStatisticsMenu,
)

View File

@ -14,10 +14,10 @@ from .helpermenus import Menu