In the middle of the desert you can say anything you want
Stumbled yet again1 on mentions of IPython and decided to look into it, prev. assumption being that it’s the same or almost the same thing as Jupyter Notebook. (Also the i
in ipdb
stands for IPython-enabled, apparently).
It’s not., it’s a separate interactive superset of the Python cli that’s runnable by itself through python3 -m IPython
.
Which in turn feels like a better / more interactive shell that can also do magic commands (%xxx
) that I’ve seen in Google Colab / Jupyter; additionally understands bash stuff as-is and does other cool stuff. Definitely worth looking into.
ALSO the same article1 mentions a way of using IPython inside ipdb
, quoting:
ipdb> from IPython import embed
ipdb> embed() # drop into an IPython session.
# Any variables you define or modify here
# will not affect program execution
To run a program with ipdb without editing the source and dropping in an ipdb prompt when if it breaks from shell:
python3 -m ipdb script.py
Took another look at the official docu 26.2. pdb — The Python Debugger — Python 2.7.18 documentation:
p
prints the expression following, pp
pretty-prints it.Pressing u / d moves you through the individual frames of the stack.
Also TODO look into using it to run stuff and debug automatically on fail, without editing the source code.1
An Inverted index - Wikipedia is a mapping from content to its location/name, as opposed to the usual case of name-to-content. One use is searching.
This is part two of 211209-1354 Python testing basics with poetry and pytest. Fixtures scopes work similarly to the various setup/teardown functions of unittest, can be per module/class/etc.
@pytest.mark.xfail(reason="Reason why it's supposed to fail")
def test_...
For a specific exception, you assert that it raises that exception type and then can do asserts on the exception that is raised.
def test_whatever():
with pytest.raises(Exception) as excinfo:
raise Exception("oh no")
assert str(excinfo.value) == "oh no"
Regex also works (example directly from pytest.raises()
API Reference
>>> with pytest.raises(ValueError, match=r'must be \d+$'):
... raise ValueError("value must be 42")
## Services (skipped, see below)
### Creating fixtures that get used automatically
```python
@pytest.fixture(autouse=True)
def skip_servicetest(request, run_services):
if request....
pytest.skip("skipped because X")
pyfakefs
creates a fake filesystem that gets used transparently.
from pyfakefs.fake_filesystem import FakeFilesystem
@pytest.fixture
def common_fs(fs: FakeFilesystem):
fs.create_dir(Path("/tmp/common"))
fs.create_file("/tmp/common")
def test_filesystem_fixture(common_filesystem):
assert os.path.exists("/tmp/common")
assert os.path.exists("/tmp/not_there") == False
A development approach from TDD.
Tests should be:
3A is a common pattern for structuring tests.
In a test this would look like this:
string = "ABc"
result = string.upper()
assert result == "ABC"
import this
A coworker reminded be of this gem; quoting him:
The order is important. My favourite one is ’explicit is better than implciit'
Had a big repo, wanted to clone only some folders.
The setup below automatically fetched the subfolders I added to the sparse-checkout set.
git clone --filter=blob:none --no-checkout --branch main ssh://git@me.me/my/repo.git
cd myrepo
git sparse-checkout init --cone
git sparse-checkout set "activitywatch" "friends" ".task" ".timewarrior"
https://gohugo.io/tools/search/
It boils down to creating an index (json) then using something to search in it client side
Once an index is built, Lunr seems the way to do with this: https://lunrjs.com/docs/lunr.Query.html#~Clause
It seems flexible enough, including ability to search inside taxonomies.
From SO1, if both are JSON serializable objects, you can use json
:
from json import loads, dumps
from collections import OrderedDict
def to_dict(input_ordered_dict):
return loads(dumps(input_ordered_dict))
Scripting Commands — Qtile 0.1.dev50+ga708c8c.d20211209 documentation has a lot more interesting stuff than the ones exposed through “vanilla” config, finally figured out how to use them:
def test(qtile):
qtile.cmd_to_layout_index(0)
# ...
Key([mod, ctrl], "apostrophe", lazy.function(test))
It’s in the docu1 but I missed its significance on first read, then saw hints in a github config2.
The qtile
object passed as the first argument is exactly the QTile
from scripting.
To parametrize it, you have to let it return a callable function:
def switch_to(ly_id: int):
def cb(qtile):
qtile.cmd_to_layout_index(ly_id)
return cb
# ...
Key([mod, ctrl], "apostrophe", lazy.function(switch_to(0))),
I don’t see this mentioned in the docu, but the attributes can be found in the source of libqtile.core.manager — Qtile 0.1.dev50+ga708c8c.d20211209 documentation.
If you mess up config.py
and restart qtile and most of your keybindings aren’t working, if you’re lucky you still have a terminal open. From it, you can fix config.py, then restart via qtile shell
-> restart()
.