forked from Griatch/wordtrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordtrack.py
executable file
·429 lines (362 loc) · 13.5 KB
/
wordtrack.py
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
#! /usr/bin/python
"""
Wordtrack (Griatch 2014)
Usage:
wordtrack.py start [days [wordgoal]] | plot | <wordcount>
A wordcount tracker that mimics the word-counting mechanism of
NaNoWriMo (www.nanowrimo.org). No code of theirs were used and this is
not in any way related or endorsed by nanowrimo. I just thought their
display was nice and encouraging and would like to have it for other
times of the year too.
Start a tracking like this:
wordtrack.py start [days [wordgoal]]
This will create a new file wordtrack.txt in the same directory, to
store the tracking information (this is an easily readable file).The
period always starts from the current day and you can only have on
period running at a time (if you run start again, you will start
over). You can define the length of your writing period and how many
words you want to attempt to write in that time. Defaults are 30 days
and 50 000 words.
To update your word count, do:
wordtrack.py <wordcount>
This registers your current word count and displays the current
statistics. You can update as many times as you want in a day, only
the latest will be stored.
You can make a plot of your current progress. For this you need
the matplotlib library:
wordtrack.py plot
You can view the statistics for the current period by calling
wordtrack without arguments.
"""
USAGE = """
Usage:
python wordtrack [start [days [wordgoal]] | plot | <wordcount>]
arguments:
(no args) - show current statistics
start - start a new period, starting from today
days - number of days in period (default 30)
wordgoal - target # of words (default 50 000)
plot - plot your progress (requires matplotlib)
<wordcount> - store a new wordcount
"""
import sys
import math
import datetime
# default config values
DEFAULT_FILE = "./wordtrack.txt"
DEFAULT_TIME_PERIOD = 30
DEFAULT_WORDGOAL = 50000
def _read_wordtrack_file(filename):
"""
Parses a wordcount file. The file is on
the following format:
# wordtrack data file.
----------------------
startdate: YY-MM-DD
enddate: YY-MM-DD
wordgoal: NN
day NN1: NN
day NN2: NN
day NN3: NN
...
-----------------------
...
"""
tracks = []
data = []
try:
with open(filename, "r") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("---"):
# store previous data
if data:
tracks.append(data)
data = []
continue
name, value = [part.strip() for part in line.split(":")]
if name == "startdate":
startdate = [int(part) for part in value.split("-", 3)]
data.append(datetime.date(*startdate))
elif name == "enddate":
enddate = [int(part) for part in value.split("-", 3)]
data.append(datetime.date(*enddate))
elif name == "wordgoal":
data.append(int(value))
elif name.startswith("day"):
data.append(int(value))
except IOError:
pass
if data:
# save any remaining the data
tracks.append(data)
return tracks
def _save_wordtrack_file(filename, tracks):
"""
Save the track information to a file
----------------------
startdate: YY-MM-DD
enddate: YY-MM-DD
wordgoal: NN
day NN1: NN
day NN2: NN
day NN3: NN
...
-----------------------
...
"""
trackstring = """
-----------------------
startdate: %s
enddate: %s
wordgoal: %s
"""
with open(filename, "w") as f:
for track in tracks:
# each track is a list
# (startdate, enddate, wordgoal, day1, day2, ...)
f.write(trackstring % (str(track[0]), str(track[1]), str(track[2])))
daystring = "\n".join("day %i: %i" % (i+1, val) for i, val in enumerate(track[3:]))
f.write(daystring)
def _stats(tracking_data):
"""
Get statistics on the word count over time, as per nanowrimo.
Input:
wordcount_list - a list of integer word counts, one per day
starting_date - a datetime.date object giving the starting point of the period.
target_time_period - the number of days to write
target_word_count - number of words aimed to write during time period
"""
starting_date = tracking_data[0]
ending_date = tracking_data[1]
wordgoal = tracking_data[2]
wordcount_list = tracking_data[3:]
target_time_period = (ending_date - starting_date).days + 1
current_day = (datetime.date.today() - starting_date).days + 1
days_remaining = target_time_period - current_day + 1
total_words_written = wordcount_list[-1] if wordcount_list else 0
target_average_word_count = float(wordgoal) / target_time_period if target_time_period else 0
average_words_per_day = float(total_words_written) / current_day if current_day else 0
words_written_today = 0
if current_day == 1:
words_written_today = wordcount_list[0]
else:
words_written_today = wordcount_list[-1] - wordcount_list[-2]
words_remaining = wordgoal - total_words_written
current_goal_day = current_day + int(math.ceil(words_remaining / average_words_per_day if average_words_per_day else 0))
if current_goal_day != current_day:
finish_date = datetime.date.today() + datetime.timedelta(current_goal_day - current_day)
else:
finish_date = "Not started yet."
needed_words_per_day = float(words_remaining) / days_remaining if days_remaining else 0
# validation
if days_remaining < 0:
days_remaining = "Time passed!"
if words_remaining < 0:
words_remaining = "Goal reached!"
if current_day > target_time_period:
current_day = "Time passed!"
if needed_words_per_day < 0:
finish_date = "Goal reached!"
needed_words_per_day = 0
return {"starting_date": starting_date,
"ending_date": ending_date,
"average_words_per_day": int(average_words_per_day),
"words_written_today": words_written_today,
"target_word_count": wordgoal,
"target_average_word_count": int(math.ceil(target_average_word_count)),
"total_words_written": total_words_written,
"words_remaining": words_remaining,
"current_day": current_day,
"days_remaining": days_remaining,
"finish_day": current_goal_day,
"finish_date": finish_date,
"target_time_period": target_time_period,
"needed_words_per_day": int(math.ceil(needed_words_per_day))}
def _plot(tracking_data):
"""
Use matplotlib to plot histogram and curve
"""
try:
from matplotlib import pyplot
except ImportError:
return "Plotting requires matplotlib, please install matplotlib and try again."
wordcount_list = tracking_data[3:]
stats = _stats(tracking_data)
if isinstance("days_remaining", basestring):
return
counts = wordcount_list + [0 for i in range(stats["days_remaining"] -1 )]
Ncount = len(counts)
days = [1 + i for i in range(Ncount)]
target_average = [i * stats["target_average_word_count"] for i in range(1, Ncount + 1)]
current_average = [i * stats["average_words_per_day"] for i in range(1, Ncount + 1)]
xmin = 0
xmax = Ncount + 2
ymin = 0
ymax = max(stats["total_words_written"] + 1000, stats["target_word_count"] + 1)
# red shajded region (past deadline)
x1, x2, y1, y2 = Ncount, xmax, ymin, ymax
rshade = ((x1, x1, x2, x2), (y1, y2, y2, y1))
deadline = ((x1,x1), (y1,y2))
# green shaded region (faster than finish)
x1, x2, y1, y2 = min(x1, stats["finish_day"]), Ncount, ymin, ymax
gshade = ((x1, x1, x2, x2), (y1, y2, y2, y1))
goal_line = ((x1, x1), (y1, y2))
# horizontal word count target line
x1, x2, y1 = xmin, xmax, stats["target_word_count"]
word_target_line = ((x1, x2), (y1, y1))
# creating the plots
print "plotting ..."
pyplot.close("all")
fig, ax = pyplot.subplots()
ax.set_xlim(xmin + 0.5, xmax)
ax.set_ylim(ymin, ymax)
ax.set_title("Words written %s - %s" % (stats["starting_date"], stats["ending_date"]))
ax.set_xlabel("Day number")
ax.set_ylabel("Words written")
ax.plot(days, target_average, '--', c="r", linewidth=4, label="target average")
ax.plot(days, current_average, '.', c="b", linewidth=4, label="current average")
ax.fill(*gshade, alpha=0.2, color="g")
ax.plot(*goal_line, c="g")
ax.fill(*rshade, alpha=0.2, color="r")
ax.plot(*deadline, c="r")
ax.plot(*word_target_line, c='g', linestyle='--')
ax.bar(days, counts, color="black", align="center")
ax.text(0.03, 0.97, _display(stats, plot=True), transform=ax.transAxes, fontsize=12,
horizontalalignment='left', verticalalignment='top',
bbox={"boxstyle":"round,pad=0.5", "alpha":0.2, "facecolor":"white"})
pyplot.tight_layout()
pyplot.show()
def _display(stats, plot=False):
"""
Take stats dictionary and display on same format as nanowrimo.
plot - make a compact plotting format
"""
if plot:
# this is a more compact format for the plot
output = \
"""
Day {current_day} / {target_time_period}
Words: {total_words_written} / {target_word_count}
Today: {words_written_today} words
Rate for deadline: {needed_words_per_day} wpd
Current Rate: {average_words_per_day} wpd
Current finish: {finish_date}
"""
else:
# same layout as nanowrimo
output = \
"""
Period: {starting_date} - {ending_date}
Your average per day: {average_words_per_day}
Words written today: {words_written_today}
Target word count: {target_word_count}
Target average word
count per day: {target_average_word_count}
Total words written: {total_words_written}
Words remaining: {words_remaining}
Current day: {current_day}
Days remaining: {days_remaining}
At this rate you
will finish on: {finish_date}
Words per day to
finish on time: {needed_words_per_day}
"""
return output.format(**stats)
# access functions
def wordtrack_display(filename=DEFAULT_FILE):
"""
Show the word count in a nice list
"""
wordtrack_update(filename)
tracks = _read_wordtrack_file(filename)
current = tracks[-1] if tracks else []
if current:
return _display(_stats(current))
def wordtrack_plot(filename=DEFAULT_FILE):
"""
Display a plot of the data
"""
wordtrack_update(filename)
tracks = _read_wordtrack_file(filename)
current = tracks[-1] if tracks else []
if current:
return _plot(current)
def wordtrack_update(filename=DEFAULT_FILE, wordcount=0):
"""
Add or update wordcount in the data file
"""
tracks = _read_wordtrack_file(filename)
if not tracks:
return
current = tracks[-1] if tracks else []
wordcount_list = current[3:]
stats = _stats(current)
if isinstance(stats["days_remaining"], basestring):
print "Please start a new time period."
return
iday = max(0, stats["current_day"])
nlist = len(wordcount_list)
diff = nlist - iday
auto_wordcount = max(wordcount, wordcount_list[-1] if wordcount_list else 0)
if wordcount_list and not wordcount:
wordcount = auto_wordcount
if diff == -1:
# new day - add new count
wordcount_list.append(auto_wordcount)
elif diff < -1:
# days with nothing written
for i in range(abs(diff)):
wordcount_list.append(auto_wordcount)
wordcount_list.append(wordcount)
else:
# replace last count
wordcount_list[iday - 1] = wordcount
current = current[:3] + wordcount_list
tracks[-1] = current
_save_wordtrack_file(filename, tracks)
stats = _stats(current)
def wordtrack_start(filename=DEFAULT_FILE, days=DEFAULT_TIME_PERIOD, wordgoal=DEFAULT_WORDGOAL):
"""
Create a new wordcount record (this stops previous count)
"""
startdate = datetime.date.today()
enddate = startdate + datetime.timedelta(days)
tracks = _read_wordtrack_file(filename)
# create the new record and save it
tracks.append([startdate, enddate, wordgoal, 0])
_save_wordtrack_file(filename, tracks)
# Main call
if __name__ == "__main__":
filename = DEFAULT_FILE
argv = sys.argv
nlen = len(argv)
if nlen < 2:
out = wordtrack_display(filename)
print out if out else USAGE
else:
if argv[1].lower() in ("start", "s"):
# start a new period
days = DEFAULT_TIME_PERIOD
wordgoal = DEFAULT_WORDGOAL
if nlen > 2:
days = int(argv[2])
if nlen > 3:
wordgoal = int(argv[3])
wordtrack_start(filename=filename, days=days, wordgoal=wordgoal)
print "Started new wordtrack period (%s days including today) with the goal of writing %s words. Good luck!" % (days, wordgoal)
elif argv[1].lower() in ("plot", "p"):
err = wordtrack_plot(filename=filename)
if err:
print err
elif argv[1] in ("help", "h", "--help"):
print USAGE
elif argv[1].isdigit():
# register a new word count
wordtrack_update(filename, int(argv[1]))
out = wordtrack_display(filename)
print out if out else USAGE
else:
print "Malformed input. Use --help."