Source

aodag.tasks / aodag / tasks / __init__.py

"""
>>> from datetime import datetime, time
>>> tasks = list()
>>> t1 = create_task("たすく1", datetime(2012, 4, 1), time(0, 25), 1)
>>> add_task(tasks, t1)
>>> t2 = create_task("たすく2", datetime(2012, 4, 1), time(0, 25), 2)
>>> add_task(tasks, t2)
>>> from tempfile import NamedTemporaryFile
>>> with NamedTemporaryFile(delete=False) as f:
...     filename= f.name
...     save_tasks(tasks, f)
>>> with open(filename, "rb") as f2:
...     loaded = load_tasks(f2)
>>> [f['name'] for f in filter(is_active_task, loaded)]
['たすく2']
"""

import sys
from datetime import datetime, time
import pickle
import functools

STATE_NEW = 1
STATE_ACTIVE = 2
STATE_FINISHED = 3
state_str = {
        STATE_NEW: "新規",
        STATE_ACTIVE: "実行中",
        STATE_FINISHED: "完了",
        }


def create_task(name, due_date, required_time, state):
    """
    >>> from datetime import datetime, time
    >>> create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    {'due_date': datetime.datetime(2012, 4, 1, 0, 0), 'state': 1, 'required_time': datetime.time(0, 25), 'name': 'たすく'}
    """
    task = dict(name=name, due_date=due_date, required_time=required_time, state=state)
    return task

def add_task(task_list, task):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> tasks = []
    >>> add_task(tasks, t)
    >>> tasks
    [{'due_date': datetime.datetime(2012, 4, 1, 0, 0), 'state': 1, 'required_time': datetime.time(0, 25), 'name': 'たすく'}]
    """

    task_list.append(task)

def format_task(task):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> format_task(t)
    '新規   たすく: 2012-04-01まで 予定所要時間00時間25分'
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 2)
    >>> format_task(t)
    '実行中  たすく: 2012-04-01まで 予定所要時間00時間25分'
    
    """
    state = state_str.get(task['state'], '未定義の状態')
    format = "{state:4} {task[name]}: {task[due_date]:%Y-%m-%d}まで 予定所要時間{task[required_time]:%H}時間{task[required_time]:%M}分"
    return format.format(task=task, state=state)

def add_task(task_list, task):
    task_list.append(task)

def is_new_task(task):
    return task.get('state') == STATE_NEW

def is_active_task(task):
    return task.get('state') == STATE_ACTIVE

def is_finished_task(task):
    return task.get('state') == STATE_FINISHED

def is_past(due_date, task):
    """
    >>> due_date = datetime(2012, 4, 30)
    >>> t = create_task("たすく", datetime(2012, 4, 29), time(0, 25), 1)
    >>> is_past(due_date, t)
    True
    >>> t = create_task("たすく", datetime(2012, 4, 30), time(0, 25), 1)
    >>> is_past(due_date, t)
    False
    >>> t = create_task("たすく", datetime(2012, 5, 1), time(0, 25), 1)
    >>> is_past(due_date, t)
    False
    """

    return task['due_date'] < due_date


def filter_many(funcs, task):
    """
    >>> f1 = lambda t: t['name'] == 'タスク'
    >>> f2 = lambda t: t['due_date'] == datetime(2012, 4, 1)
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> g = filter_many([f1, f2], t)
    >>> next(g)
    False
    >>> next(g)
    True
    """

    for f in funcs:
        yield f(task)

def query_factory(*funcs):
    return functools.partial(filter_many, funcs=funcs)

def activate_task(task):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> activate_task(t)
    >>> is_active_task(t)
    True
    """
    return task.update(state=2)

def finish_task(task):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> finish_task(t)
    >>> is_finished_task(t)
    True
    """
    return task.update(state=3)


def save_tasks(task, file):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> tasks = []
    >>> add_task(tasks, t)
    >>> from io import BytesIO
    >>> out = BytesIO()
    >>> save_tasks(tasks, out)
    >>> out.seek(0)
    0
    >>> pickle.load(out)
    [{'due_date': datetime.datetime(2012, 4, 1, 0, 0), 'state': 1, 'required_time': datetime.time(0, 25), 'name': 'たすく'}]

    """
    pickle.dump(task, file)


def load_tasks(file):
    """
    >>> from datetime import datetime, time
    >>> t = create_task("たすく", datetime(2012, 4, 1), time(0, 25), 1)
    >>> tasks = []
    >>> add_task(tasks, t)
    >>> from io import BytesIO
    >>> out = BytesIO()
    >>> pickle.dump(tasks, out)
    >>> out.seek(0)
    0
    >>> load_tasks(out)
    [{'due_date': datetime.datetime(2012, 4, 1, 0, 0), 'state': 1, 'required_time': datetime.time(0, 25), 'name': 'たすく'}]

    """
    return pickle.load(file)


def input_task(in_=input):

    name = in_("name: ")
    due_date = in_("due date [%Y-%m-%d]: ")
    due_date = datetime.strptime(due_date, '%Y-%m-%d')
    required_time = in_("required time [%H:%M]: ")
    required_time = time.strptie(required_time, '%H:%M')
    return create_task(name=name, due_date=due_date, required_time=required_time, state=1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.