Created
July 23, 2012 23:28
-
-
Save farnoy/3166915 to your computer and use it in GitHub Desktop.
learning python
This file contains 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
$ python ./tasker.py | |
Available commands: | |
* quit # quits the program | |
* add {name} # adds a task | |
* list # lists current tasks | |
* mark {id} v|o # mark task as done or not | |
******************************* | |
Enter command: add Watch TV | |
******************************* | |
* [1] (o) Watch TV | |
Enter command: add Sleep | |
******************************* | |
* [2] (o) Sleep | |
Enter command: list | |
******************************* | |
* [1] (o) Watch TV | |
* [2] (o) Sleep | |
Enter command: mark 1 v | |
******************************* | |
Setting status v for task | |
* [1] (v) Watch TV |
This file contains 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
# Task list | |
import pickle | |
storage = open("./tasks", "rb") | |
class Task(object): | |
def __init__(self, name): | |
super(Task, self).__init__() | |
self.__name = name | |
self.__completed = False | |
def get_name(self): | |
return self.__name | |
def set_name(self, new_name): | |
if isinstance(new_name, str): | |
self.__name = new_name | |
def iscompleted(self): | |
return self.__completed | |
def set_completed(self, compl): | |
self.__completed = compl | |
class IOCommander(object): | |
def handle_input(arr): | |
print("Available commands:") | |
print("* quit # quits the program") | |
print("* add {name} # adds a task") | |
print("* list # lists current tasks") | |
print("* mark {id} v|o # mark task as done or not") | |
IOCommander.ruler() | |
while True: | |
x = input("Enter command: ") | |
if x.startswith("quit"): | |
break; | |
elif x.startswith("add"): | |
IOCommander.add(arr, x[4:]) | |
elif x.startswith("list"): | |
IOCommander.list(arr) | |
elif x.startswith("mark"): | |
IOCommander.mark(arr, x[5:6], x[7:8]) | |
def ruler(): | |
print("\n*******************************") | |
def add(arr, name): | |
IOCommander.ruler() | |
arr.append(Task(name)) | |
IOCommander.display_task(len(arr), arr[len(arr)-1]) | |
def mark(arr, id, status): | |
id = int(id) | |
IOCommander.ruler() | |
if status == 'v': | |
completed = True | |
else: | |
completed = False | |
try: | |
arr[id-1].set_completed(completed) | |
print("Setting status %s for task" % status) | |
IOCommander.display_task(id, arr[id-1]) | |
except IndexError: | |
print("No such task") | |
def get_status_symbol(compl): | |
if compl: | |
return 'v' | |
else: | |
return 'o' | |
def list(arr): | |
IOCommander.ruler() | |
for i in range(len(arr)): | |
task = arr[i] | |
IOCommander.display_task(i+1, task) | |
def display_task(id, task): | |
print ("* [%s] (%s) %s" % ( | |
id, IOCommander.get_status_symbol(task.iscompleted()), task.get_name() | |
)) | |
try: | |
tasks = pickle.load(storage) | |
except EOFError: | |
storage.close() | |
storage = open("./tasks", "wb") | |
tasks = [] | |
pickle.dump(tasks, storage) | |
finally: | |
storage.close() | |
storage = open("./tasks", "wb") | |
IOCommander.handle_input(tasks) | |
pickle.dump(tasks, storage) | |
storage.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment