Last active
March 15, 2016 19:39
-
-
Save Painted-Fox/01f6c349f8d62dd4b66f to your computer and use it in GitHub Desktop.
Simple script to display time tracking stored in a SQLite database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''View a simple timesheet kept in sqlite''' | |
from __future__ import division # Ensure division is floating | |
import sqlite3 | |
import time | |
from datetime import date, timedelta | |
DB_PATH = 'db.sqlite' | |
class TimeEntry: | |
def __init__(self, timestamp, activity): | |
self.timestamp = timestamp | |
self.activity = activity | |
def __str__(self): | |
return '%s %s' % ( | |
time.strftime('%Y-%m-%d %I:%M %p', time.localtime(self.timestamp)), | |
self.activity) | |
class DayEntries: | |
def __init__(self): | |
self.time_entries = [] | |
def add(self, time_entry): | |
self.time_entries.append(time_entry) | |
def total_hours(self): | |
total = 0 | |
entries = iter(self.time_entries) | |
for entry in entries: | |
try: | |
out = next(entries) | |
except StopIteration: | |
out = TimeEntry(time.time(), 'Out') | |
total += out.timestamp - entry.timestamp | |
return total / 60 / 60 | |
class WeekEntries: | |
def __init__(self): | |
self.day_entries = {} | |
def add(self, time_entry): | |
date_str = date.fromtimestamp(time_entry.timestamp).strftime("%Y-%m-%d") | |
if date_str not in self.day_entries: | |
self.day_entries[date_str] = DayEntries() | |
self.day_entries[date_str].add(time_entry) | |
def display_entries(): | |
time_entries = [] | |
try: | |
db = sqlite3.connect(DB_PATH) | |
cursor = db.cursor() | |
cursor.execute('''Select timestamp, activity From time_entries Order By timestamp''') | |
for row in cursor: | |
time_entries.append(TimeEntry(row[0], row[1])) | |
finally: | |
db.close() | |
for entry in time_entries: | |
print (entry) | |
def week(): | |
week_entries = WeekEntries() | |
today = date.today() | |
days_so_far = today.weekday() + 1 # include Sunday | |
week_start = today - timedelta(days = days_so_far) | |
stamp_start = int(time.mktime(week_start.timetuple())) | |
try: | |
db = sqlite3.connect(DB_PATH) | |
cursor = db.cursor() | |
cursor.execute('''Select timestamp, activity From time_entries Where timestamp >= ? Order By timestamp''', (stamp_start,)) | |
for row in cursor: | |
week_entries.add(TimeEntry(row[0], row[1])) | |
finally: | |
db.close() | |
total = 0 | |
for key, day in week_entries.day_entries.items(): | |
for entry in day.time_entries: | |
print (entry) | |
total += day.total_hours() | |
print ("%.2f" % day.total_hours()) | |
print ("Week total: %.2f" % total) | |
def today(): | |
day_entries = DayEntries() | |
today_start = int(time.mktime(date.today().timetuple())) | |
try: | |
db = sqlite3.connect(DB_PATH) | |
cursor = db.cursor() | |
cursor.execute('''Select timestamp, activity From time_entries Where timestamp >= ? Order By timestamp''', (today_start,)) | |
for row in cursor: | |
day_entries.add(TimeEntry(row[0], row[1])) | |
finally: | |
db.close() | |
for entry in day_entries.time_entries: | |
print (entry) | |
print ("%.2f" % day_entries.total_hours()) | |
if __name__ == '__main__': | |
''' | |
import argparse | |
parser = argparse.ArgumentParser(prog='Time') | |
subparser = parser.add_subparsers(help='sub-command help') | |
parser_log = subparsers.add_parser('log', help='Shows a log of time entries.') | |
''' | |
#display_entries() | |
#today() | |
week() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment