Skip to content

Instantly share code, notes, and snippets.

@timmc-edx
Last active April 29, 2021 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmc-edx/38b4c56482fccc24d2ac4b25c01fbd6e to your computer and use it in GitHub Desktop.
Save timmc-edx/38b4c56482fccc24d2ac4b25c01fbd6e to your computer and use it in GitHub Desktop.
#!/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