Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3 as sqlite, argparse
- class HourTracker:
- class CursorInstance:
- def __init__(self, con):
- self.cursor = con.cursor()
- self.con = con
- def __call__(self,*a,**k):return self.cursor.execute(*a,**k)
- def __enter__(self):return self
- def __exit__(self,et,ev,tb):
- self.cursor.close()
- self.con.commit()
- def __init__(self):
- self.connection = sqlite.connect("hours.sql")
- self.connection.row_factory = sqlite.Row
- def cursor(self):return self.CursorInstance(self.connection)
- def __enter__(self):
- with self.cursor() as cursor:
- jq = """CREATE TABLE if not exists "jobs" (
- "id" INTEGER PRIMARY KEY AUTOINCREMENT,
- "name" TEXT NOT NULL
- )"""
- sq = """CREATE TABLE if not exists shifts (
- "job" INTEGER NOT NULL,
- "minutes" TEXT NOT NULL,
- "date" DATE default (date('now','localtime'))
- )"""
- wq = """REATE TABLE if not exists"wages" (
- "job" INTEGER NOT NULL,
- "wage" INTEGER NOT NULL,
- "start" DATE default (date('now','localtime'))
- )"""
- asv = """CREATE VIEW if not exists "active_shifts" AS select
- sum(shifts.minutes) / 60.0 as 'hours',
- shifts.job as 'job'
- from shifts
- where shifts.date >= date('now','start of month')
- and shifts.date < date('now','start of month',"+1 month")
- group by(shifts.job)
- """
- epv = """CREATE VIEW "expected_pay" AS select
- (select jobs.name from jobs where jobs.id==active_shifts.job) as 'job',
- active_shifts.hours as 'hours',
- (select wages.wage from wages where wages.job==active_shifts.job)
- * active_shifts.hours as 'expected_pay'
- from active_shifts
- """
- cursor(jq)
- cursor(sq)
- cursor(wq)
- cursor(asv)
- cursor(epv)
- return self
- def __exit__(self,et,eb,tb):
- self.connection.commit()
- def add_job(self, name, wage):
- with self.cursor() as cursor:
- cursor("insert into jobs(name) values(?)",(name,))
- cursor("insert into wages(job,wage) values(last_insert_rowid(),?)",(wage,))
- def job_id(self, name):
- with self.cursor() as cursor:
- q="select id from jobs where name like ?"
- r=[row['id']for row in cursor(q,(name,))]
- if len(r)>1:return r
- return r[0] if len(r)>0 else None
- def log_time(self, job, minutes):
- with self.cursor() as cursor:
- if isinstance(job,str):
- jt=self.job_id(job)
- if jt is None:
- raise RuntimeError("No such job as '{}'".format(job))
- job=jt
- q="insert into shifts(job,minutes) values(?,?)"
- cursor(q,(job,minutes))
- def wage_report(self):
- q="select * from expected_pay"
- with self.cursor() as cursor:
- for row in cursor(q):
- yield (row['job'],row['hours'],row['expected_pay'])
- class Main(argparse.ArgumentParser):
- def __init__(self):
- d="Hour Logging"
- super().__init__(description=d)
- self.tracker = None
- self.add_argument('--display', dest="mode", action='store_const',
- help="Displays expected payments from all jobs for this month.",
- const=self.display_wages, default=self.log_minutes)
- self.add_argument('--time', dest='time', action='store',type=int, default="0")
- self.add_argument('--job', dest='job', action='store', default=None)
- self.add_argument("--hire", dest='mode', action='store_const',
- const=self.get_hired, default=self.log_minutes)
- self.add_argument("--wage", dest="wage", action='store', default="7.25", type=float)
- def __enter__(self):
- self.tracker = HourTracker()
- return self
- def __exit__(self,et,ev,tb):pass
- def __call__(self):
- ns = self.parse_args()
- return ns.mode(ns)
- def display_wages(self,parsed):
- job = parsed.job
- report = self.tracker.wage_report()
- job = job.lower() if isinstance(job,str) else None
- s = "Currently expecting {:.4n}$ from {} after {:.3n} hours."
- for rjob, hours, pay in report:
- if (job is not None and job==rjob.lower()) or job is None:
- print(s.format(pay, rjob, hours))
- def log_minutes(self, parsed):
- job, time = parsed.job, parsed.time
- if job is not None and time > 0:
- self.tracker.log_time(job,time)
- print("Successfully logged {} minutes for {}".format(time,job))
- def get_hired(self, parsed):
- job,wage = parsed.job,parsed.wage
- s = "Successfully added {} to jobs index, setting wage to {:.4}$"
- if job is not None:
- self.tracker.add_job(job,wage)
- print(s.format(job,wage))
- if __name__=="__main__":
- with Main() as prog:prog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement