In the middle of the desert you can say anything you want
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.
~/.dotfiles for stow to work# essentials
sudo apt install cowsay fortune-mod
sudo apt install vim-gtk3 # for clipboard support
# wm clipboard
sudo apt install i3
sudo apt installxdotool maim xclip zenity
# misc
sudo apt install jq tldr silversearcher-ag rofi xscreensaver
sudo apt install blueman
sudo apt install tree stow
sudo apt install fish
sudo apt install taskwarrior timewarrior
sudo apt install arandr, autorandr
# if needed ensurepip
sudo apt install python3.12-venv
sudo apt install git git-lfs
sudo apt install tig
sudo apt install pavucontrol
#sudo apt install kitty
sudo apt install alacritty
sudo apt install htop
sudo apt install ncal # cal command
uv task installgem install friendsbrew install hugobrew install go-taskbrew install asciiquariumenpx pagefind (for hugo)chsh /usr/bin/fishtlp if it’s a laptop~/.fonts should work, subdirectories possible
fc-cache -v tells you which dirs it goes throughfc-cache -f installssudo apt install fonts-firacodealias cal="ncal -w -A1 -M" # week starts on monday, 1 months from now, show weeks
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().
Get screenshotting working through a hotkey. I need to screenshot an area of the screen, put the screenshot in a folder, and immediately open it.
In i3 had
bindsym Mod3+s --release exec scrot -s -e 'mv $f ~/s/screenshots && eog ~/s/screenshots/$f'
Nothing I tried worked (didn’t do anything weird):
Key([mod], "s", lazy.spawn(CONFIG_LOCATION + "screenshot.sh"))
Tracked it down to two main issues:
scrot works, scrot -s doesn’t. (Running the shell script directly from shell was fine!)# this works
scrot -u -e 'thunar $f' "/tmp/shot.png"
# this doesn't
scrot -u -e 'thunar $f' "$SCREENSHOT_PATH/shot.png"
Decided to leave the first one alone, scrot -u gets the currently selected window, which generally is good enough for me.
The second one - first rewrote the script to get passed the target path as positional variable (surprisingly it worked!), then decided to do it python-only. As a bonus, copies the screenshot url to the clipboard.
# definition
copy_command = 'bash -c "echo {0} | xclip -selection c"'
# ...
def take_screenshot():
SCREENSHOT_FILENAME = datetime.now().strftime("qtile_%y%m%d-%H%M%S%z")+"-$w$h.png"
screenshot_path = D.SCREENSHOT_DIR +"/"+ SCREENSHOT_FILENAME
command = f"scrot -u -e 'thunar $f && {Commands.copy_command.format('$f')}' {screenshot_path}"
return command
#usage
Key([mod], "s", lazy.spawn(Commands.take_screenshot()))
(qtile-dotfiles/config.py at master · justinesmithies/qtile-dotfiles has escrotum as python module, errored out during install in the qtile venv and segfaulted on first run when installed outside of it.)
To add an item for the WM to the options shown on gdm startup:
.desktop file to /usr/share/xsessions:[Desktop Entry]
Name=qtile
Comment=Qtile
Exec=/home/me/.dotfiles/qtile/.config/qtile/startup.sh
Type=Application
X-LightDM-DesktopName=qtile
DesktopNames=qtile
Keywords=tiling;wm;windowmanager;window;manager;
sudo systemctl restart gdm.service1Before that I tried killing gdm3 and X but it didn’t work. ↩︎