Last active
April 29, 2021 19:22
-
-
Save timmc-edx/38b4c56482fccc24d2ac4b25c01fbd6e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import collections | |
import datetime | |
import os | |
import sys | |
tz = datetime.timezone(offset=datetime.timedelta(hours=-5)) | |
logfile = os.path.expanduser("~/context-switches.log") | |
def record(): | |
"""Record one context switch in the log file.""" | |
with open(logfile, 'a') as cs: | |
cs.write(datetime.datetime.now(tz=tz).isoformat() + '\n') | |
def stats(): | |
"""Print how many context switches you had on each day.""" | |
with open(logfile, 'r') as cs: | |
dates = [line[:10] for line in cs.readlines()] | |
for d, c in sorted(collections.Counter(dates).items()): | |
print(f"{d}: {c}") | |
if __name__ == '__main__': | |
if sys.argv[1] == 'record': | |
record() | |
elif sys.argv[1] == 'stats': | |
stats() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment