mirror of
https://github.com/jbranchaud/til
synced 2026-07-04 00:28:23 +00:00
Compare commits
2 Commits
3402428aad
...
facc606014
| Author | SHA1 | Date | |
|---|---|---|---|
| facc606014 | |||
| be103b52dd |
@@ -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).
|
||||
|
||||
_1787 TILs and counting..._
|
||||
_1789 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -406,6 +406,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Move The Latest Commit To A New Branch](git/move-the-latest-commit-to-a-new-branch.md)
|
||||
- [Override The Global Git Ignore File](git/override-the-global-git-ignore-file.md)
|
||||
- [Pick Specific Changes To Stash](git/pick-specific-changes-to-stash.md)
|
||||
- [Programmatically Grab SHA For Head Commit](git/programmatically-grab-sha-for-head-commit.md)
|
||||
- [Pulling In Changes During An Interactive Rebase](git/pulling-in-changes-during-an-interactive-rebase.md)
|
||||
- [Push To A Branch On Another Remote](git/push-to-a-branch-on-another-remote.md)
|
||||
- [Quicker Commit Fixes With The Fixup Flag](git/quicker-commit-fixes-with-the-fixup-flag.md)
|
||||
@@ -1063,6 +1064,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Dunder Methods](python/dunder-methods.md)
|
||||
- [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md)
|
||||
- [Get Absolute Seconds From `timedelta` Object](python/get-absolute-seconds-from-timedelta-object.md)
|
||||
- [Get Quotient And Remainder In One Operation](python/get-quotient-and-remainder-in-one-operation.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)
|
||||
- [Iterate Over A Dictionary](python/iterate-over-a-dictionary.md)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Programmatically Grab SHA For Head Commit
|
||||
|
||||
When I use `gh browse path/to/some-file.txt`, it opens the browser to that file
|
||||
in GitHub. However, it targets the default branch (`main`) by default which is
|
||||
not very useful as a permalink because what that file looks like on `main` is
|
||||
liable to change.
|
||||
|
||||
There is a `--commit` flag you can use to have it instead open to that file at a
|
||||
specific commit SHA.
|
||||
|
||||
So what SHA do I pass as an argument to that flag?
|
||||
|
||||
Often what I would like to grab is a reference to the current version of the
|
||||
file which is whatever it looks like for the `HEAD` commit. But `HEAD` is
|
||||
another moving target reference. The `git rev-parse` command can translate
|
||||
`HEAD` into a specific SHA though.
|
||||
|
||||
```bash
|
||||
❯ git rev-parse --short HEAD
|
||||
3402428
|
||||
|
||||
❯ git rev-parse HEAD
|
||||
3402428aadc02cfdc9825c8feb593443e72f50cd
|
||||
```
|
||||
|
||||
Either of those will work. I can use a bash command substitution then to tie it
|
||||
all together into a single command:
|
||||
|
||||
```bash
|
||||
❯ gh browse path/to/some-file.txt --commit=$(git rev-parse --short HEAD)
|
||||
```
|
||||
|
||||
See `man git-rev-parse` for more details.
|
||||
@@ -0,0 +1,42 @@
|
||||
# Get Quotient And Remainder In One Operation
|
||||
|
||||
While writing some custom code to transform a number of seconds into the
|
||||
constituent hours, minutes, and seconds, I found myself needing to get both the
|
||||
quotient and remainder from a division between two numbers.
|
||||
|
||||
```python
|
||||
>>> import math
|
||||
>>> math.floor(3666 / 3600)
|
||||
1
|
||||
>>> 3666 % 3600
|
||||
66
|
||||
```
|
||||
|
||||
Instead, I can use Python's built-in
|
||||
[`divmod`](https://docs.python.org/3/library/functions.html#divmod) function to
|
||||
compute both values in one statement.
|
||||
|
||||
```python
|
||||
>>> divmod(3666, 3600)
|
||||
(1, 66)
|
||||
```
|
||||
|
||||
The result is a tuple with the first value being my quotient (in this case, the
|
||||
number of hours) and the remainder (the remaining number of seconds).
|
||||
|
||||
This kind of operation is known as [Euclidian
|
||||
Division](https://en.wikipedia.org/wiki/Euclidean_division).
|
||||
|
||||
Here is a snippet of some actual code where I use this in
|
||||
[`py-vmt`](https://github.com/jbranchaud/py-vmt/blob/b9eae8b258e9fd720cfa3bb63b601225df352051/src/py_vmt/time_helpers.py#L14-L16):
|
||||
|
||||
```python
|
||||
def format_time_delta(diff: timedelta) -> str:
|
||||
total_seconds = int(diff.total_seconds())
|
||||
|
||||
hours, remainder = divmod(total_seconds, 3600)
|
||||
minutes, remainder = divmod(remainder, 60)
|
||||
seconds = remainder
|
||||
|
||||
# ...
|
||||
```
|
||||
Reference in New Issue
Block a user