mirror of
https://github.com/jbranchaud/til
synced 2026-09-02 17:51:47 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8eaa193281 | ||
|
|
e9e7ab1fc2 | ||
|
|
21132e9640 |
@@ -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).
|
||||||
|
|
||||||
_1825 TILs and counting..._
|
_1827 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -1104,6 +1104,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Sort Normalized Version Of Data](python/sort-normalized-version-of-data.md)
|
- [Sort Normalized Version Of Data](python/sort-normalized-version-of-data.md)
|
||||||
- [Start The Debugger When A Test Errors](python/start-the-debugger-when-a-test-errors.md)
|
- [Start The Debugger When A Test Errors](python/start-the-debugger-when-a-test-errors.md)
|
||||||
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
|
||||||
|
- [Strictly Separate Positional And Keyword Arguments](python/strictly-separate-positional-and-keyword-arguments.md)
|
||||||
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
- [Test A Function With Pytest](python/test-a-function-with-pytest.md)
|
||||||
- [Turn Method Into Cached Property On Class Instance](python/turn-method-into-cached-property-on-class-instance.md)
|
- [Turn Method Into Cached Property On Class Instance](python/turn-method-into-cached-property-on-class-instance.md)
|
||||||
- [Use pipx To Install End User Apps](python/use-pipx-to-install-end-user-apps.md)
|
- [Use pipx To Install End User Apps](python/use-pipx-to-install-end-user-apps.md)
|
||||||
@@ -1839,6 +1840,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Rename A Bunch Of Files By Constructing mv Commands](unix/rename-a-bunch-of-files-by-constructing-mv-commands.md)
|
- [Rename A Bunch Of Files By Constructing mv Commands](unix/rename-a-bunch-of-files-by-constructing-mv-commands.md)
|
||||||
- [Repeat Yourself](unix/repeat-yourself.md)
|
- [Repeat Yourself](unix/repeat-yourself.md)
|
||||||
- [Replace Pattern Across Many Files In A Project](unix/replace-pattern-across-many-files-in-a-project.md)
|
- [Replace Pattern Across Many Files In A Project](unix/replace-pattern-across-many-files-in-a-project.md)
|
||||||
|
- [Restart Specific Overmind Process](unix/restart-specific-overmind-process.md)
|
||||||
- [Reverse Each Line Of A File](unix/reverse-each-line-of-a-file.md)
|
- [Reverse Each Line Of A File](unix/reverse-each-line-of-a-file.md)
|
||||||
- [Run A Command Repeatedly Several Times](unix/run-a-command-repeatedly-several-times.md)
|
- [Run A Command Repeatedly Several Times](unix/run-a-command-repeatedly-several-times.md)
|
||||||
- [Run A cURL Command Without The Progress Meter](unix/run-a-curl-command-without-the-progress-meter.md)
|
- [Run A cURL Command Without The Progress Meter](unix/run-a-curl-command-without-the-progress-meter.md)
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
# Strictly Separate Positional And Keyword Arguments
|
||||||
|
|
||||||
|
Typically when I define a function with arguments in Python, I can choose pass
|
||||||
|
the arguments to that function as positional or keyword arguments. I can even
|
||||||
|
mix and match as long as all positional arguments come before all keyword
|
||||||
|
arguments.
|
||||||
|
|
||||||
|
But what if I want to enforce a strict boundary between what arguments are
|
||||||
|
positional and what arguments are keyword?
|
||||||
|
|
||||||
|
By combining the _positional-only marker_ (`/`) and _keyword-only marker_ (`*`),
|
||||||
|
I can get exactly that effect.
|
||||||
|
|
||||||
|
Here I have defined a `connect` function that takes `host` and `port` arguments.
|
||||||
|
Because both appear before the `/` marker, they must be passed as positional. I
|
||||||
|
also have a `timeout` argument with a default. Because `timeout` comes after the
|
||||||
|
`*` marker, it must be passed as a keyword argument.
|
||||||
|
|
||||||
|
```python
|
||||||
|
def connect(host, port, /, *, timeout=30):
|
||||||
|
print(f"Connecting to #{host}:#{port}")
|
||||||
|
print(f" Timeout: {timeout}s")
|
||||||
|
|
||||||
|
# ...
|
||||||
|
```
|
||||||
|
|
||||||
|
Let's see it in action.
|
||||||
|
|
||||||
|
```python
|
||||||
|
>>> connect("localhost", 3000, timeout=20)
|
||||||
|
Connecting to #localhost:#3000
|
||||||
|
Timeout: 20s
|
||||||
|
|
||||||
|
>>> connect(host="localhost", port=4000)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
File "/Users/lastword/dev/misc/python-experiments/arguments.py", line 37, in <module>
|
||||||
|
connect(host="localhost", port=4000)
|
||||||
|
TypeError: connect() got some positional-only arguments passed as keyword arguments: 'host, port'
|
||||||
|
```
|
||||||
|
|
||||||
|
This second attempt of calling `connect` with `host` and `port` as keyword
|
||||||
|
arguments presents as a runtime `TypeError` because they were expected as
|
||||||
|
"positional-only arguments".
|
||||||
|
|
||||||
|
In my editor that line also presents with two static typing errors `call-arg:
|
||||||
|
Unexpected keyword argument "port" for "connect"` (and the equivalent for
|
||||||
|
`host`).
|
||||||
|
|
||||||
|
Note: I did some additional reading just now in [Python in a Nutshell, 4th
|
||||||
|
Edition](https://www.oreilly.com/library/view/python-in-a/9781098113544/) and I
|
||||||
|
see that it is more appropriate to call them _Named Arguments_ instead of
|
||||||
|
_Keyword Arguments_.
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
# Restart Specific Overmind Process
|
||||||
|
|
||||||
|
I have been in the bad habit of fully killing and re-running the entire
|
||||||
|
[`overmind`](https://github.com/DarthSim/overmind) process for a project
|
||||||
|
whenever I have a new JavaScript file that I need `vite` to pick up and bundle.
|
||||||
|
`vite` is only one of three processes, so I'm restarting two processes
|
||||||
|
unnecessarily.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ overmind status
|
||||||
|
PROCESS PID STATUS
|
||||||
|
web 50897 running
|
||||||
|
vite 50898 running
|
||||||
|
worker 50899 running
|
||||||
|
```
|
||||||
|
|
||||||
|
Instead, what I can do is specifically target the `vite` process for a
|
||||||
|
`restart`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ overmind restart vite
|
||||||
|
```
|
||||||
|
|
||||||
|
This will restart that specific process, ensuring that the new JS files get
|
||||||
|
picked up and bundled.
|
||||||
|
|
||||||
|
I can clearly see that only `vite` was restarted by looking at the `status`
|
||||||
|
subcommand again.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ overmind status
|
||||||
|
PROCESS PID STATUS
|
||||||
|
web 50897 running
|
||||||
|
vite 84163 running
|
||||||
|
worker 50899 running
|
||||||
|
```
|
||||||
|
|
||||||
|
Notice the `web` and `worker` processes still have the same PIDs, but `vite` has
|
||||||
|
a new much higher PID.
|
||||||
|
|
||||||
|
See `overmind restart --help` for more details.
|
||||||
Reference in New Issue
Block a user