mamweb/dakosdump/helpers.py
2015-05-17 02:07:49 +02:00

65 lines
1.8 KiB
Python

import os
import datetime
import sqlite3
from django.utils.html import escape
from seminar import ovvpfile
from seminar.models import Problem
from seminar.utils import roman, from_roman
# MM_ZADANIA.TYP
typtable={'1': Problem.TYP_ULOHA, '2':Problem.TYP_TEMA, '3':Problem.TYP_SERIAL}
# MM_DOZ.ZARADENIE, MM_AZAD.ZAMERANIE
def dectag(x):
return ["MFIOTKPZD"[bi] for bi in range(0, 9) if (int(x) & (1 << bi))]
# Datum z formatu "DD.MM.YY"
def transdate(s):
if '.' not in s: return None
d,m,r = map(int, s.split('.'))
return datetime.date(r + 2000 if r < 30 else r + 1900, m, d)
class SQLiteRow(object):
def __unicode__(self):
return self.__dict__.__unicode__()
def __str__(self):
return self.__dict__.__str__()
def __repr__(self):
return self.__dict__.__str__()
# sqlite3 helper
def sqget(db, sql, limit=None):
c = db.execute(sql)
rows = c.fetchmany(limit) if limit else c.fetchall()
res = []
for row in rows:
o = SQLiteRow()
for coli in range(len(c.description)):
col = c.description[coli]
o.__setattr__(col[0], row[coli])
res.append(o)
return res
## ovvpfile - based import helpers (old)
def read_all_tables(basedir):
tables = {}
for fn in os.listdir(basedir):
if fn.endswith('.csv'):
print "Reading %s ..." % (fn, )
with open(os.path.join(basedir, fn), 'r') as f:
o = ovvpfile.parse(f, with_headers=False)
tables[fn[:-4]] = o.rows
print " %d lines, columns: %s" % (len(o.rows), ' '.join(o.columns), )
return tables
def matchrows(tab, key, val):
return [r for r in tab if r[key]==val]
def onerow(tab, key, val):
t = matchrows(tab, key, val)
assert(len(t) == 1)
return t[0]