Spaces:
Runtime error
Runtime error
Upload utils.py with huggingface_hub
Browse files
utils.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
+
from datetime import datetime, date
|
| 3 |
+
|
| 4 |
+
def create_todo(text, priority="Medium", due_date=None):
|
| 5 |
+
"""Create a new todo item"""
|
| 6 |
+
if due_date is None:
|
| 7 |
+
due_date = date.today().strftime('%Y-%m-%d')
|
| 8 |
+
|
| 9 |
+
return {
|
| 10 |
+
'id': str(uuid.uuid4()),
|
| 11 |
+
'text': text,
|
| 12 |
+
'completed': False,
|
| 13 |
+
'priority': priority,
|
| 14 |
+
'due_date': due_date,
|
| 15 |
+
'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def toggle_todo(todos, todo_id):
|
| 19 |
+
"""Toggle the completion status of a todo"""
|
| 20 |
+
for todo in todos:
|
| 21 |
+
if todo['id'] == todo_id:
|
| 22 |
+
todo['completed'] = not todo['completed']
|
| 23 |
+
break
|
| 24 |
+
return todos
|
| 25 |
+
|
| 26 |
+
def delete_todo(todos, todo_id):
|
| 27 |
+
"""Delete a todo item"""
|
| 28 |
+
return [todo for todo in todos if todo['id'] != todo_id]
|
| 29 |
+
|
| 30 |
+
def update_todo(todos, todo_id, new_text, new_priority, new_due_date):
|
| 31 |
+
"""Update an existing todo"""
|
| 32 |
+
for todo in todos:
|
| 33 |
+
if todo['id'] == todo_id:
|
| 34 |
+
todo['text'] = new_text
|
| 35 |
+
todo['priority'] = new_priority
|
| 36 |
+
todo['due_date'] = new_due_date
|
| 37 |
+
break
|
| 38 |
+
return todos
|
| 39 |
+
|
| 40 |
+
def clear_completed_todos(todos):
|
| 41 |
+
"""Remove all completed todos"""
|
| 42 |
+
return [todo for todo in todos if not todo['completed']]
|
| 43 |
+
|
| 44 |
+
def filter_todos(todos, filter_type='all', search_query='', priorities=None):
|
| 45 |
+
"""Filter todos based on completion status and search query"""
|
| 46 |
+
if priorities is None:
|
| 47 |
+
priorities = ['High', 'Medium', 'Low']
|
| 48 |
+
|
| 49 |
+
filtered = todos
|
| 50 |
+
|
| 51 |
+
# Filter by completion status
|
| 52 |
+
if filter_type == 'active':
|
| 53 |
+
filtered = [todo for todo in filtered if not todo['completed']]
|
| 54 |
+
elif filter_type == 'completed':
|
| 55 |
+
filtered = [todo for todo in filtered if todo['completed']]
|
| 56 |
+
|
| 57 |
+
# Filter by priority
|
| 58 |
+
filtered = [todo for todo in filtered if todo['priority'] in priorities]
|
| 59 |
+
|
| 60 |
+
# Filter by search query
|
| 61 |
+
if search_query:
|
| 62 |
+
search_query = search_query.lower()
|
| 63 |
+
filtered = [todo for todo in filtered if search_query in todo['text'].lower()]
|
| 64 |
+
|
| 65 |
+
# Sort by priority and due date
|
| 66 |
+
priority_order = {'High': 0, 'Medium': 1, 'Low': 2}
|
| 67 |
+
filtered.sort(key=lambda x: (x['completed'], priority_order[x['priority']], x['due_date']))
|
| 68 |
+
|
| 69 |
+
return filtered
|
| 70 |
+
|
| 71 |
+
def get_todo_stats(todos):
|
| 72 |
+
"""Get statistics about todos"""
|
| 73 |
+
total = len(todos)
|
| 74 |
+
completed = len([todo for todo in todos if todo['completed']])
|
| 75 |
+
pending = total - completed
|
| 76 |
+
|
| 77 |
+
return {
|
| 78 |
+
'total': total,
|
| 79 |
+
'completed': completed,
|
| 80 |
+
'pending': pending
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
def get_priority_stats(todos):
|
| 84 |
+
"""Get statistics by priority"""
|
| 85 |
+
stats = {'High': 0, 'Medium': 0, 'Low': 0}
|
| 86 |
+
for todo in todos:
|
| 87 |
+
if not todo['completed']:
|
| 88 |
+
stats[todo['priority']] += 1
|
| 89 |
+
return stats
|
| 90 |
+
|
| 91 |
+
def export_todos(todos):
|
| 92 |
+
"""Export todos to JSON format"""
|
| 93 |
+
return json.dumps(todos, indent=2)
|
| 94 |
+
|
| 95 |
+
def import_todos(json_data):
|
| 96 |
+
"""Import todos from JSON data"""
|
| 97 |
+
try:
|
| 98 |
+
return json.loads(json_data)
|
| 99 |
+
except json.JSONDecodeError:
|
| 100 |
+
return None
|