Skip to content

Instantly share code, notes, and snippets.

@santiagobasulto
Last active February 8, 2019 01:04
Show Gist options
  • Save santiagobasulto/257c01d938a5ca598a979e60da591d4a to your computer and use it in GitHub Desktop.
Save santiagobasulto/257c01d938a5ca598a979e60da591d4a to your computer and use it in GitHub Desktop.
{
"category_name": "General",
"todos": [
{
"task": "My TODO Task",
"due_on": null,
"status": "pending",
"description": null
},
{
"task": "My TODO Task",
"due_on": "2018-03-05",
"status": "pending",
"description": "Much description, wow"
}
]
}
"""
def __init__(self, base_todos_path, create_dir=True):
self.base_todos_path = base_todos_path
self.path = Path(self.base_todos_path)
# continue here
if not self.path.exists():
# create the directory
self.path.mkdir()
def list(self, status=STATUS_ALL, category=CATEGORY_GENERAL):
json_files = self.path.glob('*')
"""
import json
from pathlib import Path
{
'Programming': [{
'task': 'Practice Pathlib',
'description': 'Investigate Pathlib and file creation',
'due_on': '2018-03-25',
'status': 'pending'
}, {
'task': 'Finish rmotrgram',
'description': 'Finish before class to start reviewing',
'due_on': '2018-03-21',
'status': 'done'
}],
'Reviews': [{
'task': 'Review rmotrgram',
'description': 'Finish review before weekend',
'due_on': '2018-03-28',
'status': 'pending'
}]
}
p = Path('todos')
json_files = p.glob('*.json')
result = {}
for json_file in json_files:
with json_file.open() as fp:
todo = json.loads(fp.read())
print(todo['category_name'])
{
"category_name": "Reviews",
"todos": [
{
"task": "First review todo",
"due_on": null,
"status": "pending",
"description": null
}
]
}
import json
from pathlib import Path
from datetime import date
class TodoManager(object):
STATUS_ALL = 'all'
STATUS_DONE = 'done'
STATUS_PENDING = 'pending'
CATEGORY_GENERAL = 'general'
def __init__(self, base_todos_path, create_dir=True):
self.base_todos_path = base_todos_path
self.path = Path(self.base_todos_path)
# continue here
if self.path.exists() and self.path.is_file():
raise ValueError("There's a file with that name")
if not self.path.exists():
# create the directory
self.path.mkdir()
def list(self, status=STATUS_ALL, category=CATEGORY_GENERAL):
json_files = self.path.glob('*.json')
result = {}
for json_file in json_files:
with json_file.open() as fp:
todo_file_doc = json.loads(fp.read())
cat_name = todo_file_doc['category_name']
todos = todo_file_doc['todos']
result[cat_name] = []
for todo in todos:
if todo['status'] == status:
result[cat_name].append(todo)
return result
def new(self, task, category=CATEGORY_GENERAL, description=None,
due_on=None):
if due_on:
if type(due_on) == date:
due_on = due_on.isoformat()
elif type(due_on) == str:
# all good
pass
else:
raise ValueError('Invalid due_on type. Must be date or str')
# continue here
# if file doesn't exist:
# create file with category name
# add the todo
todo_path = self.path / (category + '.json')
if not todo_path.exists():
# create it...
empty_document = {
'category_name': category.title(),
'todos': []
}
with todo_path.open('w') as fp:
json_document = json.dumps(empty_document)
fp.write(json_document)
# S2
with todo_path.open() as fp:
todos_doc = json.loads(fp.read())
todos_doc['todos'].append({
"task": task,
"due_on": due_on,
"status": "pending",
"description": description
})
with todo_path.open('w') as fp:
json_document = json.dumps(todos_doc)
fp.write(json_document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment