-
Notifications
You must be signed in to change notification settings - Fork 4
Adding Sqlite #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0x6773
wants to merge
4
commits into
numerals:master
Choose a base branch
from
0x6773:sql
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adding Sqlite #17
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +0,0 @@ | ||
| #!/usr/bin/env python2 | ||
|
|
||
| ## | ||
| # rollcall | ||
| # https://github.com/numerals/rollcall.git | ||
| # | ||
| # Copyright (c) 2014 Sartaj Singh, Sumit Sahrawat | ||
| # Licensed under the MIT license. | ||
| ## | ||
|
|
||
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
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
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
File renamed without changes.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #!/usr/bin/env python2 | ||
|
|
||
| """ | ||
| Contains the main GUI Class | ||
| """ | ||
|
|
||
| import os | ||
| import gtk | ||
|
|
||
|
|
||
| def find_file(dire, fName): | ||
| """ | ||
| Generates the complete path of a file | ||
| returns the complete path | ||
| """ | ||
| path = os.path.join(os.path.dirname(dire), fName) | ||
| return path | ||
|
|
||
|
|
||
| def load_interface(dire, fName): | ||
| """ | ||
| Loads the interface | ||
| in particular loads the glade file | ||
| returns the builder | ||
| """ | ||
| fName = find_file(dire, fName) | ||
| builder = gtk.Builder() | ||
| builder.add_from_file(fName) | ||
| return builder | ||
|
|
||
|
|
||
| class rollcallGUIClass: | ||
| """ | ||
| Sets up the GUI interface | ||
| """ | ||
| def __init__(self): | ||
|
|
||
| self.builder = load_interface(__file__, 'glade/rollcallGUI.glade') | ||
| self.save_objects() | ||
| self.builder.connect_signals(self.setup_signals()) | ||
| self.window.show_all() | ||
|
|
||
| def setup_signals(self): | ||
| """ | ||
| Sets up the signals | ||
| """ | ||
| sig = {} | ||
|
|
||
| return sig | ||
|
|
||
| def save_objects(self): | ||
| """ | ||
| Get the required objects | ||
| """ | ||
| pass | ||
|
|
||
| def close(self, *args): | ||
| """ | ||
| Handles Destroy Event | ||
| """ | ||
| gtk.main_quit() | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| gtk.main() |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| #!/usr/bin/env python2 | ||
| #ref : http://zetcode.com/db/sqlitepythontutorial/ | ||
|
|
||
| """ | ||
| SQL Class rollcall | ||
| """ | ||
|
|
||
| from __future__ import print_function | ||
| import os | ||
| import sys | ||
| import rollcall.exce as exce | ||
| from rollcall.func_json import TAGS | ||
| from rollcall.main import pDir, full_path_to | ||
|
|
||
| try: | ||
| import sqlite3 as sql | ||
| except ImportError: | ||
| print("SQLite3 is not found!") | ||
| sys.exit() | ||
|
|
||
| dbNameDef = 'rollcall.db' | ||
| dbPathDef = full_path_to(dbNameDef) | ||
|
|
||
| def connect_db(dbPath = dbPathDef): | ||
| """ | ||
| Connect to DB | ||
| and Return the sqlite3.Connection object | ||
| """ | ||
| try: | ||
| conn = sql.connect(dbPath) | ||
| except: | ||
| raise exce.DatabaseError("Unable to open Database File!") | ||
| return conn | ||
|
|
||
| def close_db_connection(conn): | ||
| """ | ||
| close the connection 'conn' | ||
| where conn is sqlite3.Connection object | ||
| """ | ||
| conn.close() | ||
|
|
||
| def get_subjects(dbPath = dbPathDef): | ||
| """ | ||
| Get a List of All Subjects Present in DB | ||
| yield subject | ||
| """ | ||
| conn = connect_db(dbPath) | ||
| command = "SELECT name FROM sqlite_master WHERE type='table'" | ||
| try: | ||
| subQuery = conn.execute(command) | ||
| subList = subQuery.fetchall() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
| for sub in subList: | ||
| yield sub[0] | ||
|
|
||
| def get_count_subject_tag(subName, tag = 'p', dbPath = dbPathDef): | ||
| """ | ||
| Get the number of classes in Subject with tag | ||
| """ | ||
|
|
||
| if not TAGS.has_key(tag): | ||
| raise exce.UnknownTag("Tag: %s UNKNOWN" %(tag)) | ||
|
|
||
| if not subName in get_subjects(dbPath): | ||
| raise exce.SubjectError("There is no records for %s." %(subName)) | ||
|
|
||
| conn = connect_db(dbPath) | ||
| command = """SELECT COUNT(*) FROM %s | ||
| WHERE class_tag=\"%s\"""" % (subName, TAGS[tag]) | ||
| try: | ||
| count = conn.execute(command) | ||
| count = count.fetchall() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
| return count[0][0] | ||
|
|
||
| def get_total_class_subject(subName, dbPath = dbPathDef): | ||
| """ | ||
| Get the total classes happened till date | ||
| """ | ||
|
|
||
| if not subName in get_subjects(dbPath): | ||
| raise exce.SubjectError("There is no records for %s." %(subName)) | ||
|
|
||
| conn = connect_db(dbPath) | ||
| command = """SELECT COUNT(*) FROM %s""" % (subName) | ||
| try: | ||
| count = conn.execute(command) | ||
| count = count.fetchall() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
| return count[0][0] | ||
|
|
||
| #Todo : Add tests | ||
| def add_subject(subName, dbPath = dbPathDef): | ||
| """ | ||
| Add a Subject to the DB | ||
| """ | ||
|
|
||
| if subName in get_subjects(dbPath): | ||
| raise exce.SubjectExists("Records for %s are already present." %(subName)) | ||
|
|
||
| conn = connect_db(dbPath) | ||
| cur = conn.cursor() | ||
| command = """CREATE TABLE %s | ||
| ( | ||
| class_no INTEGER NOT NULL, | ||
| class_tag VARCHAR(10) NOT NULL | ||
| CONSTRAINT chk_tag CHECK (class_tag IN ("absent", | ||
| "present", "future", "holiday", "other")), | ||
| class_date DATETIME NOT NULL DEFAULT(DATETIME('now', 'localtime')), | ||
| PRIMARY KEY (class_no) | ||
| )""" % subName | ||
| try: | ||
| cur.execute(command) | ||
| conn.commit() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
|
|
||
| #Todo : add tests | ||
| def delete_subject(subName, dbPath = dbPathDef): | ||
| """ | ||
| Delete a Subject if Made by mistake :) | ||
| """ | ||
|
|
||
| if not subName in get_subjects(dbPath): | ||
| raise exce.SubjectError("There is no records for %s." %(subName)) | ||
|
|
||
| conn = connect_db(dbPath) | ||
| cur = conn.cursor() | ||
| command = """DROP TABLE %s""" % subName | ||
| try: | ||
| cur.execute(command) | ||
| conn.commit() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
|
|
||
| def update_subject(subName, tag = 'p', dbPath = dbPathDef): | ||
| """ | ||
| Update Subject with Tag | ||
| """ | ||
|
|
||
| if not TAGS.has_key(tag): | ||
| raise exce.UnknownTag("Tag: %s UNKNOWN" %(tag)) | ||
|
|
||
| if not subName in get_subjects(dbPath): | ||
| raise exce.SubjectError("There is no records for %s." %(subName)) | ||
|
|
||
| conn = connect_db(dbPath) | ||
| cur = conn.cursor() | ||
| command = """INSERT INTO %s (class_tag) | ||
| VALUES (\"%s\")""" %(subName, TAGS[tag]) | ||
| try: | ||
| cur.execute(command) | ||
| conn.commit() | ||
| except: | ||
| conn.rollback() | ||
| close_db_connection(conn) | ||
| raise exce.DatabaseError("Some problem occurred in DB!") | ||
| close_db_connection(conn) | ||
|
|
||
| def get_subject_tag_percent(subName, tag = 'p', dbPath = dbPathDef): | ||
| """ | ||
| Get Percent of tag in subName in total Records till Date | ||
| """ | ||
|
|
||
| if not TAGS.has_key(tag): | ||
| raise exce.UnknownTag("Tag: %s UNKNOWN" %(tag)) | ||
|
|
||
| if not subName in get_subjects(dbPath): | ||
| raise exce.SubjectError("There is no records for %s." %(subName)) | ||
|
|
||
| count_tag = get_count_subject_tag(subName, tag, dbPath) | ||
| count_total = get_total_class_subject(subName, dbPath) | ||
| fract = count_tag * 1.0 / count_total | ||
| return fract*100 | ||
Empty file.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #!/usr/bin/env python2 | ||
|
|
||
| """ | ||
| Helper Module for SQL Tests | ||
| """ | ||
|
|
||
| import os | ||
| from os.path import realpath, dirname | ||
| import sys | ||
|
|
||
| import rollcall.sql.rollcallSQLClass as rcsql | ||
|
|
||
| def hadd_subjects(dbPath): | ||
| """ | ||
| Helper Method which add subjects to DB at dbPath | ||
| """ | ||
| rcsql.add_subject('Maths', dbPath) | ||
| rcsql.add_subject('Physics', dbPath) | ||
| rcsql.add_subject('Chemistry', dbPath) | ||
| rcsql.add_subject('Python', dbPath) | ||
|
|
||
| def hupdate_subjects(dbPath): | ||
| """ | ||
| Helper Method which updates each subject with two 'p' & one 'a' | ||
| """ | ||
| for subject in rcsql.get_subjects(dbPath): | ||
| rcsql.update_subject(subject, 'p', dbPath) | ||
| rcsql.update_subject(subject, 'p', dbPath) | ||
| rcsql.update_subject(subject, 'a', dbPath) |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be explicit here. Which exceptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OperationError I guess