Debian Codegraph @

examples/notes_example.py

raw symbols SCIP
1 # A very simple notetaking application that uses Python to load
2 # and save a string in a text file in the user's home directory.
3
4 import os
5 import threading
6 import time
7
8 import pyotherside
9
10 class Notes:
11 def __init__(self):
12 self.filename = os.path.expanduser('~/pyotherside_notes.txt')
13 self.thread = None
14 self.new_text = self.get_contents()
15
16 def save_now(self):
17 print('Saving file right away at exit')
18 self._update_file(now=True)
19
20 def _update_file(self, now=False):
21 if not now:
22 time.sleep(3)
23 print('Saving file now')
24 open(self.filename, 'w').write(self.new_text)
25 self.thread = None
26
27 def get_contents(self):
28 if os.path.exists(self.filename):
29 return open(self.filename).read()
30 else:
31 return '<new file>'
32
33 def set_contents(self, text):
34 self.new_text = text
35 if self.thread is None:
36 print('Scheduling saving of file')
37 self.thread = threading.Thread(target=self._update_file)
38 self.thread.start()
39
40 notes = Notes()
41 pyotherside.atexit(notes.save_now)