Last active
March 17, 2022 12:57
-
-
Save connormcmk/bd92ef084585fe1bd48d7f8326f70863 to your computer and use it in GitHub Desktop.
To show the total number of todo items, just call `len(api.list_items(listname, user))`
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
from typing import Sequence, Callable, Iterable, Option | |
from uuid import uuid4 as uuid | |
import time | |
class State: | |
pass | |
class User: | |
def __init__(s, authentication): | |
try: s.authenticate(authentication): | |
s._authenticated = True | |
except AuthenticationError as e: | |
raise e | |
def get_friends(s): | |
return s.friends | |
done = State | |
new = State | |
in_progress = State | |
under_review = State | |
blocked = State | |
class ShareRule: | |
pass | |
owner_only = ShareRule() | |
friends = ShareRule() | |
class Item: | |
def __init__(s, name: str, id: int, owner: User): | |
s.name = name | |
s.owner = owner | |
s.state = new | |
s.created = time.now() | |
s.id = uuid() | |
s.priority = 0 | |
s.sticker: str = None | |
s.due = Option(None, int) | |
s.share = owner_only | |
return s | |
def complete(s): | |
s.state = done | |
def set_state(s, state: State): | |
s.state = state | |
def set_priority(val: int): | |
s.priority = int | |
def set_sticker(s, sticker_href): | |
s.sticker = sticker_href | |
def set_due(s, due: int): | |
s.due = due | |
def set_share(s, rule: ShareRule): | |
s.share = rule | |
def __repr__(s) -> Item: | |
return f'{s.name} {s.state} created {s.created}. \n priority: {s.priority} due: {s.due}' | |
class MissingItem(KeyError): | |
pass | |
class ItemList: | |
def __init__(s): | |
s.itemlist = dict[int, Item] | |
s.max_id = 0 | |
return s | |
def get_item(s, id: str): | |
if item := s.itemlist.get(id, None) == None: | |
raise MissingItem(f'No item id {id}') | |
return item | |
def add(s, name: str, owner: User): | |
s.itemlist[s.max_id]: Item(name, s.max_id, owner) | |
s.max_id += 1 | |
def del(s, id: str): | |
if s.itemlist.get(int, None) == None: | |
pass | |
else: | |
del s.itemlist[id] | |
def __iter__(s) -> Iterable: | |
# makes ItemList iterable | |
def __repr__(s): | |
return f'ItemList at {s}' | |
class ExistingList(ValueError): | |
pass | |
class MissingList(ValueError): | |
pass | |
@singleton | |
class API: | |
def __init__(s) -> API: | |
s.lists = dict[str, ItemList] | |
return s | |
def _listexists(s, listname): | |
if s.lists.get(name, False): | |
True | |
else: | |
False | |
def get_list(s, listname: str) -> ItemList: | |
if not s._listexists(listname): | |
raise MissingList(f'No list named {listname}') | |
return s.lists.get(listname) | |
def new_list(s): | |
listname = uuid() | |
if s._listexists(listname): | |
raise ExistingList(f'list id {listname} already exists.') | |
s.lists[listname] = ItemList() | |
return s.lists[listname] | |
def lists(s) -> dict[str, ItemList]: | |
return s.lists.toJSON() | |
def add_item(s, listname: str, name: str): | |
list_ = get_list(listname) | |
list_.add(name) | |
def list_items(s, listname: str, user: User, filter: Callable = lambda x: True, sort_key: Callable = None, reverse = False): | |
list_ = get_list(listname) | |
return sorted([item for item in list_ if fn(item) and (item.share = friends and user in item.owner.get_friends())], key = None, reverse = reverse) | |
def del_item(s, listname: str, item_id: str): | |
list_ = get_list(listname) | |
list_.del(item_id) | |
def set_state(s, listname: str, item_id: str, state: State): | |
list_ = get_list(listname) | |
item = list_.get_item(item_id) | |
item.set_state(state) | |
def set_priority(s, listname: str, item_id: str, val: int): | |
list_ = get_list(listname) | |
item = list_.get_item(item_id) | |
item.set_priority(val) | |
def set_sticker(s, listname: str, item_id: str, sticker_href: str): | |
list_ = get_list(listname) | |
item = list_.get_item(item_id) | |
item.set_sticker(sticker_href) | |
def set_due(s, listname: str, item_id: str, due: int): | |
list_ = get_list(listname) | |
item = list_.get_item(item_id) | |
item.set_due(sticker_href) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are a bunch of small bugs throughout. For example, on line 51 I set
priority
toint
when I meant to set it toval
and similarly all the invocations ofState
near line 19 should have been instance creations,State()
.But my understanding is that what's more important is the structure of the code not necessarily whether it will run, so consider this some strangely precise pseudocode rather than something that would actually run