Repository navigation
Expand file tree
/
Copy paththinker_app.py
More file actions
467 lines (398 loc) Β· 18.6 KB
/
Copy paththinker_app.py
File metadata and controls
467 lines (398 loc) Β· 18.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
"""
Python Thinker App - A tool to help organize thoughts, brainstorm ideas, and structure thinking
"""
import json
import os
import datetime
from typing import List, Dict, Any
from dataclasses import dataclass, asdict
import uuid
@dataclass
class Thought:
"""Represents a single thought or idea"""
id: str
content: str
category: str
priority: int # 1-5 scale
tags: List[str]
created_at: str
updated_at: str
is_completed: bool = False
@dataclass
class ThinkingSession:
"""Represents a thinking session with multiple thoughts"""
id: str
title: str
description: str
thoughts: List[Thought]
created_at: str
updated_at: str
class ThinkerApp:
"""Main application class for the Python Thinker"""
def __init__(self, data_file: str = "thoughts.json"):
self.data_file = data_file
self.sessions: List[ThinkingSession] = []
self.current_session: ThinkingSession = None
self.load_data()
def load_data(self):
"""Load existing thinking sessions from file"""
if os.path.exists(self.data_file):
try:
with open(self.data_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.sessions = []
for session_data in data:
thoughts = [Thought(**thought) for thought in session_data['thoughts']]
session = ThinkingSession(
id=session_data['id'],
title=session_data['title'],
description=session_data['description'],
thoughts=thoughts,
created_at=session_data['created_at'],
updated_at=session_data['updated_at']
)
self.sessions.append(session)
except (json.JSONDecodeError, KeyError) as e:
print(f"Error loading data: {e}")
self.sessions = []
def save_data(self):
"""Save thinking sessions to file"""
try:
data = []
for session in self.sessions:
session_dict = asdict(session)
data.append(session_dict)
with open(self.data_file, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print("β
Data saved successfully!")
except Exception as e:
print(f"β Error saving data: {e}")
def create_session(self, title: str, description: str = ""):
"""Create a new thinking session"""
session_id = str(uuid.uuid4())[:8]
timestamp = datetime.datetime.now().isoformat()
session = ThinkingSession(
id=session_id,
title=title,
description=description,
thoughts=[],
created_at=timestamp,
updated_at=timestamp
)
self.sessions.append(session)
self.current_session = session
print(f"π§ Created new thinking session: '{title}' (ID: {session_id})")
return session
def list_sessions(self):
"""List all thinking sessions"""
if not self.sessions:
print("π No thinking sessions found. Create one to get started!")
return
print("\nπ Your Thinking Sessions:")
print("-" * 50)
for i, session in enumerate(self.sessions, 1):
status = "π’ Active" if session == self.current_session else "βͺ Inactive"
thought_count = len(session.thoughts)
completed_thoughts = len([t for t in session.thoughts if t.is_completed])
print(f"{i}. {session.title} ({session.id})")
print(f" {status} | {thought_count} thoughts ({completed_thoughts} completed)")
print(f" Created: {session.created_at[:19]}")
if session.description:
print(f" Description: {session.description}")
print()
def select_session(self, session_id: str):
"""Select a session to work with"""
for session in self.sessions:
if session.id == session_id:
self.current_session = session
print(f"π― Selected session: '{session.title}'")
return session
print(f"β Session with ID '{session_id}' not found")
return None
def add_thought(self, content: str, category: str = "general", priority: int = 3, tags: List[str] = None):
"""Add a new thought to the current session"""
if not self.current_session:
print("β No active session. Create or select a session first.")
return
if tags is None:
tags = []
thought_id = str(uuid.uuid4())[:8]
timestamp = datetime.datetime.now().isoformat()
thought = Thought(
id=thought_id,
content=content,
category=category,
priority=priority,
tags=tags,
created_at=timestamp,
updated_at=timestamp
)
self.current_session.thoughts.append(thought)
self.current_session.updated_at = timestamp
print(f"π‘ Added thought: '{content[:50]}...' (ID: {thought_id})")
def list_thoughts(self, category: str = None, completed: bool = None):
"""List thoughts in the current session"""
if not self.current_session:
print("β No active session selected")
return
thoughts = self.current_session.thoughts
# Filter by category
if category:
thoughts = [t for t in thoughts if t.category.lower() == category.lower()]
# Filter by completion status
if completed is not None:
thoughts = [t for t in thoughts if t.is_completed == completed]
if not thoughts:
print("π€ No thoughts found with the specified criteria")
return
print(f"\nπ Thoughts in '{self.current_session.title}':")
print("-" * 60)
# Sort by priority (high to low)
thoughts.sort(key=lambda x: x.priority, reverse=True)
for thought in thoughts:
status = "β
" if thought.is_completed else "β"
priority_stars = "β" * thought.priority
print(f"{status} {thought.content}")
print(f" ID: {thought.id} | Category: {thought.category} | Priority: {priority_stars}")
if thought.tags:
print(f" Tags: {', '.join(thought.tags)}")
print(f" Created: {thought.created_at[:19]}")
print()
def complete_thought(self, thought_id: str):
"""Mark a thought as completed"""
if not self.current_session:
print("β No active session selected")
return
for thought in self.current_session.thoughts:
if thought.id == thought_id:
thought.is_completed = True
thought.updated_at = datetime.datetime.now().isoformat()
self.current_session.updated_at = thought.updated_at
print(f"β
Marked thought as completed: '{thought.content[:50]}...'")
return
print(f"β Thought with ID '{thought_id}' not found")
def delete_thought(self, thought_id: str):
"""Delete a thought from the current session"""
if not self.current_session:
print("β No active session selected")
return
for i, thought in enumerate(self.current_session.thoughts):
if thought.id == thought_id:
deleted_thought = self.current_session.thoughts.pop(i)
self.current_session.updated_at = datetime.datetime.now().isoformat()
print(f"ποΈ Deleted thought: '{deleted_thought.content[:50]}...'")
return
print(f"β Thought with ID '{thought_id}' not found")
def brainstorm_session(self):
"""Interactive brainstorming session"""
if not self.current_session:
print("β No active session. Create or select a session first.")
return
print(f"\nπ§ Starting brainstorming session for: '{self.current_session.title}'")
print("π‘ Enter your thoughts (type 'done' to finish, 'help' for commands)")
print("-" * 60)
while True:
try:
user_input = input("\nπ Your thought: ").strip()
if user_input.lower() == 'done':
break
elif user_input.lower() == 'help':
self.show_brainstorm_help()
continue
elif user_input.lower().startswith('priority:'):
# Handle priority setting: "priority:5 This is important"
parts = user_input.split(' ', 1)
if len(parts) == 2:
try:
priority = int(parts[0].split(':')[1])
content = parts[1]
self.add_thought(content, priority=min(max(priority, 1), 5))
except ValueError:
print("β Invalid priority format. Use: priority:1-5 your thought")
continue
elif user_input.lower().startswith('category:'):
# Handle category setting: "category:ideas This is an idea"
parts = user_input.split(' ', 1)
if len(parts) == 2:
category = parts[0].split(':')[1]
content = parts[1]
self.add_thought(content, category=category)
continue
elif not user_input:
continue
# Regular thought
self.add_thought(user_input)
except KeyboardInterrupt:
print("\n\nπ Brainstorming session interrupted")
break
print(f"\nπ Brainstorming session completed! Added thoughts to '{self.current_session.title}'")
def show_brainstorm_help(self):
"""Show help for brainstorming session"""
print("\nπ Brainstorming Commands:")
print(" β’ Just type your thought and press Enter")
print(" β’ 'priority:1-5 your thought' - Set priority (1=low, 5=high)")
print(" β’ 'category:name your thought' - Set category")
print(" β’ 'help' - Show this help")
print(" β’ 'done' - Finish brainstorming session")
def export_session(self, session_id: str = None, format: str = "txt"):
"""Export a session to a file"""
session = self.current_session
if session_id:
session = next((s for s in self.sessions if s.id == session_id), None)
if not session:
print("β No session to export")
return
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"thinking_session_{session.id}_{timestamp}.{format}"
try:
if format == "txt":
self._export_to_txt(session, filename)
elif format == "md":
self._export_to_markdown(session, filename)
else:
print("β Unsupported format. Use 'txt' or 'md'")
return
print(f"π Session exported to: {filename}")
except Exception as e:
print(f"β Error exporting session: {e}")
def _export_to_txt(self, session: ThinkingSession, filename: str):
"""Export session to plain text"""
with open(filename, 'w', encoding='utf-8') as f:
f.write(f"Thinking Session: {session.title}\n")
f.write("=" * 50 + "\n\n")
if session.description:
f.write(f"Description: {session.description}\n\n")
f.write(f"Created: {session.created_at[:19]}\n")
f.write(f"Updated: {session.updated_at[:19]}\n")
f.write(f"Total Thoughts: {len(session.thoughts)}\n\n")
# Group thoughts by category
categories = {}
for thought in session.thoughts:
if thought.category not in categories:
categories[thought.category] = []
categories[thought.category].append(thought)
for category, thoughts in categories.items():
f.write(f"\n{category.upper()}\n")
f.write("-" * len(category) + "\n\n")
# Sort by priority
thoughts.sort(key=lambda x: x.priority, reverse=True)
for thought in thoughts:
status = "[β]" if thought.is_completed else "[ ]"
priority = "β
" * thought.priority
f.write(f"{status} {thought.content}\n")
f.write(f" Priority: {priority} | Tags: {', '.join(thought.tags)}\n")
f.write(f" Created: {thought.created_at[:19]}\n\n")
def _export_to_markdown(self, session: ThinkingSession, filename: str):
"""Export session to markdown"""
with open(filename, 'w', encoding='utf-8') as f:
f.write(f"# {session.title}\n\n")
if session.description:
f.write(f"**Description:** {session.description}\n\n")
f.write(f"- **Created:** {session.created_at[:19]}\n")
f.write(f"- **Updated:** {session.updated_at[:19]}\n")
f.write(f"- **Total Thoughts:** {len(session.thoughts)}\n\n")
# Group thoughts by category
categories = {}
for thought in session.thoughts:
if thought.category not in categories:
categories[thought.category] = []
categories[thought.category].append(thought)
for category, thoughts in categories.items():
f.write(f"\n## {category.title()}\n\n")
# Sort by priority
thoughts.sort(key=lambda x: x.priority, reverse=True)
for thought in thoughts:
checkbox = "- [x]" if thought.is_completed else "- [ ]"
priority = "β" * thought.priority
f.write(f"{checkbox} **{thought.content}**\n")
f.write(f" - Priority: {priority}\n")
if thought.tags:
f.write(f" - Tags: {', '.join(thought.tags)}\n")
f.write(f" - Created: {thought.created_at[:19]}\n\n")
def main():
"""Main function to run the Thinker App"""
app = ThinkerApp()
print("π§ Welcome to the Python Thinker App!")
print("π Organize your thoughts, brainstorm ideas, and structure your thinking")
print("Type 'help' for available commands\n")
while True:
try:
command = input("π€ thinker> ").strip().lower()
if command == 'help':
show_help()
elif command == 'quit' or command == 'exit':
app.save_data()
print("π Thanks for using Python Thinker! Your thoughts have been saved.")
break
elif command == 'sessions':
app.list_sessions()
elif command.startswith('create '):
title = command[7:].strip()
if title:
app.create_session(title)
else:
print("β Please provide a session title")
elif command.startswith('select '):
session_id = command[7:].strip()
app.select_session(session_id)
elif command.startswith('add '):
content = command[4:].strip()
if content:
app.add_thought(content)
else:
print("β Please provide thought content")
elif command == 'thoughts':
app.list_thoughts()
elif command.startswith('complete '):
thought_id = command[9:].strip()
app.complete_thought(thought_id)
elif command.startswith('delete '):
thought_id = command[7:].strip()
app.delete_thought(thought_id)
elif command == 'brainstorm':
app.brainstorm_session()
elif command.startswith('export'):
parts = command.split()
format_type = parts[1] if len(parts) > 1 else "txt"
app.export_session(format=format_type)
elif command == 'save':
app.save_data()
elif command == '':
continue
else:
print("β Unknown command. Type 'help' for available commands.")
except KeyboardInterrupt:
print("\n\nπ Use 'quit' to exit properly and save your data.")
except Exception as e:
print(f"β An error occurred: {e}")
def show_help():
"""Display help information"""
print("\nπ Python Thinker App Commands:")
print("β" * 50)
print("ποΈ Session Management:")
print(" sessions - List all thinking sessions")
print(" create <title> - Create a new thinking session")
print(" select <session_id> - Select a session to work with")
print()
print("π Thought Management:")
print(" add <thought> - Add a thought to current session")
print(" thoughts - List thoughts in current session")
print(" complete <thought_id> - Mark a thought as completed")
print(" delete <thought_id> - Delete a thought")
print()
print("π§ Thinking Tools:")
print(" brainstorm - Start interactive brainstorming")
print(" export [txt|md] - Export current session to file")
print()
print("πΎ Data Management:")
print(" save - Save all data to file")
print(" quit/exit - Save and exit the application")
print()
print("π‘ Tips:")
print(" β’ Use descriptive session titles")
print(" β’ Set priorities (1-5) during brainstorming: 'priority:5 important idea'")
print(" β’ Categorize thoughts: 'category:goals finish the project'")
print(" β’ Use tags to organize related thoughts")
if __name__ == "__main__":
main()