In the middle of the desert you can say anything you want
In the context of reading a settings.ini
from python’s decouple
1 config lib, this works as empty string
YAML_CONVERTER_PREFIX=
has to be cast to string though:
D_YAML_CONVERTER_PREFIX = config("YAML_CONVERTER_PREFIX", cast=str)
These don’t, these are strings containing two characters, ""
and ''
respectively.
YAML_CONVERTER_PREFIX=""
YAML_CONVERTER_PREFIX=''
Wooho!
files = list(input_dir.glob("*.md"))[: cs.limit]
if output_path.is_file() and ((l := len(files)) != 1):
raise ValueError(f"Can't write {l} files to single file {output_dir}")
Had to use additional parentheses around the actual assignment. Without that, black fails in an interesting way:
error: cannot format smw_to_hugo/yaml_converter.py: cannot use --safe with this file; failed to parse source file.
Just discovered this! In vim, if I skip the pattern, it’ll take the one last searched for:
/mypattern
:s//newpattern/g
Had weird issues with kitty terminal output being wrong, lines in vim/nvim being in wrong places, usually because it thought the terminal was a different size than it really was (blamed it on nvim initally, but the problem happened in other complex CLI programs too, notably tig
).
$TERMINFO
wasn’t set, and the terminfo file was nowhere to be found. The package kitty-terminfo
was installed though.
In any case, downloaded the terminfo file from the repo and set the env variable manually in zshrc, now it works:
export TERMINFO="$HOME/.config/kitty/xterm-kitty"
After for the nth time writing awkward code like
if limit is None:
limit = len(mylist)
decided to see if there’s a better way. Looked into the walrus operator etc,but decided to test what I get with None
.
Well, mylist[:None]
works! No errors, I’d guess I get a copy of it same as mylist[:]
.
Will save me hundreds of lines in the future!
Docu about slice
1 is terse, says it uses range(start,end,step)
under the hood with start
and step
defaulting to None. But range
doesn’t accept None for all arguments! TODO for later I guess.
Things I can pass to mypy
like mypy --disallow-any-generics
can be configured in pyproject.toml
:
[tool.mypy]
show_error_codes = true
warn_unused_ignores = false
disallow_any_generics = false
ignore_missing_imports = true
Is nice! It transparently got all vim’s configs plugins and they seems to work!
set runtimepath^=~/.vim runtimepath+=~/.vim/after
let &packpath = &runtimepath
source ~/.vimrc
A Complete Guide to Neovim Configuration for Python Development - jdhao’s blog
deoplete for faster completions, jedi-vim for goto and friends.
davidhalter/jedi-vim: Using the jedi autocompletion library for VIM.
Interesting bindings:
let g:jedi#usages_command = "<leader>n"
let g:jedi#goto_command = "<leader>d"
let g:jedi#rename_command = "<leader>r"
let g:jedi#documentation_command = "K"
But it didn’t work for packages not living inside the default python environment, and manually each venv would be tedious. poet-v to the rescue!
let g:poetv_executables = ['poetry']
map <leader>va :PoetvActivate<CR>
Deoplete1 is an autocomplete framework (nvim-only, was my last reason for switching), deoplete-jedi2 makes it use jedi.
To select on enter, had to add this to vimrc/nvimrc:
set completeopt+=noinsert
In general deoplete faq in vim help is much longer than the one on their github repo.
nvie/vim-flake8: Flake8 plugin for Vim, <F7>
to run it on the current buffer.
Found a post1 about it.
But I like much more Click’s way to do this (Options — Click Documentation (8.0.x)):
@click.option(
"--username", prompt=True,
default=lambda: os.environ.get("USER", "")
)
Of course, os.get.environ
can be replaced by python-decouple’s config()
.
Lastly, ini files support interpolation2 (%(whatever)s
)! Final solution:
[settings]
EXPORT=../../exports
CATS_INPUT=%(EXPORT)s/cats.json
@click.option(
"--input-file",
"-i",
type=click.Path(exists=True, path_type=Path),
default=lambda: config("CATS_INPUT"),
)
Also TIL if I use quotes in the ini file, they’ll become part of the final filename.
Stumbled upon python-decouple · PyPI, which seems like a “better” dotenv (supports casting, defaults etc)
For example, this is a settings.ini
in poetry project root:
[settings]
ECHO=True
I can overwrite these parameters like ECHO=False poetry run python myscript.py
Neat!
Python Best Practices for a New Project in 2021 - Alex Mitelman
Describes a setup that uses poetry, black, flake8, pytest, mypy and new to me isort
to sort imports.
The Fast track section has a TL;DR of how to create that setup.
I also really like this intro to poetry: Package Python Projects the Proper Way with Poetry