1
0
mirror of https://github.com/jbranchaud/til synced 2026-07-03 08:08:24 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
jbranchaud 16082177aa Add Create A Range Of Descending Values as a Python TIL 2026-03-19 00:25:32 -05:00
jbranchaud 2276a57445 Add Look Inside Pytest tmp_path as a Python TIL 2026-03-18 21:03:51 -05:00
3 changed files with 88 additions and 1 deletions
+3 -1
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).
_1758 TILs and counting..._
_1760 TILs and counting..._
See some of the other learning resources I work on:
@@ -1047,6 +1047,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Check If Package Is Installed With Pip](python/check-if-package-is-installed-with-pip.md)
- [Control Passing Of Time In Tests](python/control-passing-of-time-in-tests.md)
- [Create A Dummy DataFrame In Pandas](python/create-a-dummy-dataframe-in-pandas.md)
- [Create A Range Of Descending Values](python/create-a-range-of-descending-values.md)
- [Dunder Methods](python/dunder-methods.md)
- [Easy Key-Value Aggregates With defaultdict](python/easy-key-value-aggregates-with-defaultdict.md)
- [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md)
@@ -1054,6 +1055,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Iterate Over A Dictionary](python/iterate-over-a-dictionary.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)
- [Look Inside Pytest tmp_path](python/look-inside-pytest-tmp-path.md)
- [Override The Boolean Context Of A Class](python/override-the-boolean-context-of-a-class.md)
- [Parse Relative Time To datetime Object](python/parse-relative-time-to-datetime-object.md)
- [Store And Access Immutable Data In A Tuple](python/store-and-access-immutable-data-in-a-tuple.md)
@@ -0,0 +1,44 @@
# Create A Range Of Descending Values
A typical use of `range` looks something like this:
```python
>>> list(range(1, 5))
[1, 2, 3, 4]
```
Which is equivalent to this one where we give a `step` value of `1`.
```python
>>> list(range(1, 5, 1))
[1, 2, 3, 4]
```
If we try to create a _negative range_, that is, a range of values in decreasing
order, we get an empty list.
```python
>>> list(range(0, -7))
[]
```
That's because the `step` value still defaults to `1`. And there are no positive
steps between `0` and `-7`. So, let's give `range` a `step` value of `-1`.
```python
>>> list(range(0,-7, -1))
[0, -1, -2, -3, -4, -5, -6]
```
One practical use case of a negative range like this is using a list
comprehension to transform it into a list of the _last seven days_.
```python
>>> from datetime import datetime, timedelta
>>> [datetime.now().date() + timedelta(days=days) for days in range(0,-7, -1)]
[datetime.date(2026, 3, 19), datetime.date(2026, 3, 18), datetime.date(2026, 3, 17), datetime.date(2026, 3, 16), datetime.date(2026, 3, 15), datetime.date(2026, 3, 14), datetime.date(2026, 3, 13)]
```
Of course this could have been written with a positive range and then
subtracting the `timedelta`. I like that I have the option of doing this in
whatever way makes the code most readable.
+41
View File
@@ -0,0 +1,41 @@
# Look Inside Pytest tmp_path
In [Isolate and Debug File Side-Effects with Pytest
`tmp_path`](https://www.visualmode.dev/isolate-and-debug-file-side-effects-with-pytest-tmp-path),
I wrote about how I use
[`tmp_path`](https://docs.pytest.org/en/stable/reference/reference.html#std-fixture-tmp_path)
in a Pytest fixture to test [my `py-vmt` CLI](https://github.com/jbranchaud/py-vmt). During testing of the CLI interface
via [`click`'s testing utilities](https://click.palletsprojects.com/en/stable/testing/), `vmt` creates,
modifies, and reads from files. Isolating that behavior with the `tmp_path`
fixture is useful because it prevents individual test cases from conflicting
with one another.
Here is what the fixture looks like at the top of my test file:
```python
# auto fixture for all test cases that monkeypatches the platform dirs to a tmp
# path so that test side-effects don't persist between runs
@pytest.fixture(autouse=True)
def use_tmp_platform_dirs(tmp_path, monkeypatch):
data_dir = tmp_path / "data"
config_dir = tmp_path / "config"
data_dir.mkdir()
config_dir.mkdir()
monkeypatch.setattr(CliContext, "get_data_dir", staticmethod(lambda: data_dir))
monkeypatch.setattr(CliContext, "get_config_dir", staticmethod(lambda: config_dir))
```
The root of the temp directory is located at `tempfile.gettempdir()` and the
directories from there are organized with this structure:
```
{temproot}/pytest-of-{user}/pytest-{num}/{testname}/
```
So, in the case of `vmt`, I can find the `config` and `data` dirs for a specific
test run here:
```bash
ls /var/folders/zc/q6gnvbgx6kq77828jn38716r0000gn/T/pytest-of-lastword/pytest-2/test_start_status_stop_flow0
config data
```