1
0
mirror of https://github.com/jbranchaud/til synced 2026-03-03 22:48:45 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud
f20428b06a Add Keep A Tally With collections.Counter as a Python TIL 2026-02-22 14:28:37 -06:00
jbranchaud
e41802653d Add Inspect EXIF Data For An Image File as a Unix TIL 2026-02-22 11:52:47 -06:00
3 changed files with 90 additions and 1 deletions

View File

@@ -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).
_1743 TILs and counting..._
_1745 TILs and counting..._
See some of the other learning resources I work on:
@@ -1039,6 +1039,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Dunder Methods](python/dunder-methods.md)
- [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md)
- [Iterate First N Items From Enumerable](python/iterate-first-n-items-from-enumerable.md)
- [Keep A Tally With collections.Counter](python/keep-a-tally-with-collections-counter.md)
- [Load A File Into The Python REPL](python/load-a-file-into-the-python-repl.md)
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
@@ -1710,6 +1711,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Ignore A Directory During ripgrep Search](unix/ignore-a-directory-during-ripgrep-search.md)
- [Ignore The Alias When Running A Command](unix/ignore-the-alias-when-running-a-command.md)
- [Include Ignore Files In Ripgrep Search](unix/include-ignore-files-in-ripgrep-search.md)
- [Inspect EXIF Data For An Image File](unix/inspect-exif-data-for-an-image-file.md)
- [Interactively Browse Available Node Versions](unix/interactively-browse-availabile-node-versions.md)
- [Interactively Switch asdf Package Versions](unix/interactively-switch-asdf-package-versions.md)
- [Interpret Cron Schedule From The CLI](unix/interpret-cron-schedule-from-the-cli.md)

View File

@@ -0,0 +1,40 @@
# Keep A Tally With collections.Counter
Python's `collections` module comes with a
[`Counter`](https://docs.python.org/3/library/collections.html#collections.Counter)
object which is a specialized dict subclass focussed on tallying counts of keys.
> It is a collection where elements are stored as dictionary keys and their
> counts are stored as dictionary values. Counts are allowed to be any integer
> value including zero or negative counts.
I used it recently while doing an exploratory implementation of a Byte-Pair
Encoding (BPE):
```python
from collections import Counter
def get_pair_counts(token_ids: list[int]) -> Counter:
"""Count how often each adjacent pair appears"""
counts = Counter()
for i in range(len(token_ids) - 1):
pair = (token_ids[i], token_ids[i + 1])
counts[pair] += 1
return counts
```
Here I'm able to count the number of occurrences of each pair of bytes from the
input text. A tuple of `int` values is hashable, so they work great as keys for
a `Counter`.
The count value of any key will default to `0`. That makes it straightforward to
increment from there as you iterating over occurrences.
```python
>>> counts = Counter()
>>> counts['hello']
0
>>> count['hello'] += 1
>>> count['hello']
1
```

View File

@@ -0,0 +1,47 @@
# Inspect EXIF Data For An Image File
The `exiftool` CLI (which can be downloaded via `brew`) is a useful tool for
inspecting all the EXIF data attached to a media file. A media file like an
image has a bunch of additional details embedded in it like timestamps, image
metadata, and sometimes location information.
Here is all the data attached to a screenshot I found on my desktop:
```bash
exiftool ~/Desktop/CleanShot\ 2025-11-17\ at\ 11.22.18@2x.png
ExifTool Version Number : 13.50
File Name : CleanShot 2025-11-17 at 11.22.18@2x.png
Directory : /Users/lastword/Desktop
File Size : 1194 kB
File Modification Date/Time : 2025:11:17 11:22:21-06:00
File Access Date/Time : 2025:12:15 10:43:55-06:00
File Inode Change Date/Time : 2025:12:05 15:37:48-06:00
File Permissions : -rw-r--r--
File Type : PNG
File Type Extension : png
MIME Type : image/png
Image Width : 2502
Image Height : 1232
Bit Depth : 8
Color Type : RGB with Alpha
Compression : Deflate/Inflate
Filter : Adaptive
Interlace : Noninterlaced
XMP Toolkit : XMP Core 6.0.0
Y Resolution : 144
Resolution Unit : inches
X Resolution : 144
Exif Image Width : 2502
Color Space : sRGB
User Comment : Screenshot
Exif Image Height : 1232
SRGB Rendering : Perceptual
Image Size : 2502x1232
Megapixels : 3.1
```
This works with other kinds of media files. For instance, I ran this against an
MP4 screen recording file which contained even more metadata.
In addition to reading data, `exiftool` can also write it. See `man exiftool`
for more details on what else it can do.