mirror of
https://github.com/jbranchaud/til
synced 2026-11-05 02:13:27 +00:00
Add Print Out A Calendar For Current Year as a Python TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||||
|
|
||||||
_1897 TILs and counting..._
|
_1898 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -1142,6 +1142,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
|
||||||
- [Override Timezone That All Tests Run In](python/override-timezone-that-all-tests-run-in.md)
|
- [Override Timezone That All Tests Run In](python/override-timezone-that-all-tests-run-in.md)
|
||||||
- [Parse Relative Time To datetime Object](python/parse-relative-time-to-datetime-object.md)
|
- [Parse Relative Time To datetime Object](python/parse-relative-time-to-datetime-object.md)
|
||||||
|
- [Print Out A Calendar For Current Year](python/print-out-a-calendar-for-current-year.md)
|
||||||
- [Publish A Package To A Test Env As A Dry Run](python/publish-a-package-to-a-test-env-as-a-dry-run.md)
|
- [Publish A Package To A Test Env As A Dry Run](python/publish-a-package-to-a-test-env-as-a-dry-run.md)
|
||||||
- [Reclassify Certain Packages As Dev Dependencies](python/reclassify-certain-packages-as-dev-dependencies.md)
|
- [Reclassify Certain Packages As Dev Dependencies](python/reclassify-certain-packages-as-dev-dependencies.md)
|
||||||
- [Register SQLite Adapter To Serialize Datetimes](python/register-sqlite-adapter-to-serialize-datetimes.md)
|
- [Register SQLite Adapter To Serialize Datetimes](python/register-sqlite-adapter-to-serialize-datetimes.md)
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
# Print Out A Calendar For Current Year
|
||||||
|
|
||||||
|
While reading through Simon Willison's [CLI tools hidden in the Python standard
|
||||||
|
library](https://til.simonwillison.net/python/stdlib-cli-tools), I learned that
|
||||||
|
you can print out the entire calendar for the current year with a simple Python
|
||||||
|
one-liner. This uses the
|
||||||
|
[`calendar`](https://docs.python.org/3/library/calendar.html) module in Python's
|
||||||
|
standard library.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ python -m calendar
|
||||||
|
2026
|
||||||
|
|
||||||
|
January February March
|
||||||
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
||||||
|
1 2 3 4 1 1
|
||||||
|
5 6 7 8 9 10 11 2 3 4 5 6 7 8 2 3 4 5 6 7 8
|
||||||
|
12 13 14 15 16 17 18 9 10 11 12 13 14 15 9 10 11 12 13 14 15
|
||||||
|
19 20 21 22 23 24 25 16 17 18 19 20 21 22 16 17 18 19 20 21 22
|
||||||
|
26 27 28 29 30 31 23 24 25 26 27 28 23 24 25 26 27 28 29
|
||||||
|
30 31
|
||||||
|
|
||||||
|
April May June
|
||||||
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
||||||
|
1 2 3 4 5 1 2 3 1 2 3 4 5 6 7
|
||||||
|
6 7 8 9 10 11 12 4 5 6 7 8 9 10 8 9 10 11 12 13 14
|
||||||
|
13 14 15 16 17 18 19 11 12 13 14 15 16 17 15 16 17 18 19 20 21
|
||||||
|
20 21 22 23 24 25 26 18 19 20 21 22 23 24 22 23 24 25 26 27 28
|
||||||
|
27 28 29 30 25 26 27 28 29 30 31 29 30
|
||||||
|
|
||||||
|
July August September
|
||||||
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
||||||
|
1 2 3 4 5 1 2 1 2 3 4 5 6
|
||||||
|
6 7 8 9 10 11 12 3 4 5 6 7 8 9 7 8 9 10 11 12 13
|
||||||
|
13 14 15 16 17 18 19 10 11 12 13 14 15 16 14 15 16 17 18 19 20
|
||||||
|
20 21 22 23 24 25 26 17 18 19 20 21 22 23 21 22 23 24 25 26 27
|
||||||
|
27 28 29 30 31 24 25 26 27 28 29 30 28 29 30
|
||||||
|
31
|
||||||
|
|
||||||
|
October November December
|
||||||
|
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
|
||||||
|
1 2 3 4 1 1 2 3 4 5 6
|
||||||
|
5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13
|
||||||
|
12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20
|
||||||
|
19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27
|
||||||
|
26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31
|
||||||
|
30
|
||||||
|
```
|
||||||
|
|
||||||
|
You cannot tell in this markdown code block, but in the terminal the current day
|
||||||
|
gets highlighted.
|
||||||
|
|
||||||
|
There is also an _output type_ flag (`-t`) where `html` can be specified instead
|
||||||
|
of the default of `text`. The `html` version will spit a bunch of markup out
|
||||||
|
into the terminal. Instead, I can encode that output with `base64` and then open
|
||||||
|
it as a `date:text` URL in Chrome like so:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ open -a "Google Chrome" "data:text/html;base64,$(python -m calendar -t html | base64)"
|
||||||
|
```
|
||||||
|
|
||||||
|
Unfortunately, that gets no default styles. Everything is scrunched together
|
||||||
|
making it hard to read. What if I were to use `sed` to inject some styles that
|
||||||
|
make for much better default styling?
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ open -a "Google Chrome" "data:text/html;base64,$(
|
||||||
|
python -m calendar -t html \
|
||||||
|
| sed 's|<link[^>]*/>|<style>body{font-family:system-ui;margin:2rem}table.year{border-spacing:1.5rem;margin:auto}table.month td{text-align:right;padding:.35rem .5rem}</style>|' \
|
||||||
|
| base64
|
||||||
|
)"
|
||||||
|
```
|
||||||
|
|
||||||
|
That's easier to read and fits nicely on a single sheet of paper if printed.
|
||||||
|
|
||||||
|
See `python -m calendar -h` for more details.
|
||||||
Reference in New Issue
Block a user