In the middle of the desert you can say anything you want
From a conversation with a colleague at work about #py/logging
Logger names can be used to cleanly output and separate them.
Assuming one has a package with multiple files/subfolders in it, it’s possible to give each one their own logger, like this:
In the main file of the package:
logger = logging.getLogger(__package__)
In all the other files:
logger = logging.getLogger(__name__)
That way paths ./package/my_module.py
lead to loggers named like package.my_module
that map the semantical and the directory structure.
In a setup above, one can then easily change the settings of the loggers referring to them by their names.
Configuring logging: Logging HOWTO — Python 3.10.0 documentation
Changing loglevel is easy from code,
if args.debug:
logger.setLevel(logging.DEBUG)
logging.config
allows to change the config from ini-like config files. Two main ways:
logging.config.fileConfig
reads ini-like config files,
logging.config.dictConfig
1 from dictionaries.
Sample .yaml that when converted to dict would change the loglevel of different loggers:
version: 1
loggers:
packageName.mymodule1:
level: DEBUG
packageName.mymodule2:
level: DEBUG
These loggers can even include external ones!
Short notes about #py/poetry for package management
poetry new packagename
creates a poetry project
From within the folder with the package:
poetry install
== pip3 install -r requierements.txt
poetry shell
== source .venv/bin/activate
exit
== deactivate
Basic usage | Documentation | Poetry - Python dependency management and packaging made easy:
{cache-dir}/virtualenvs
, which on my box is /home/me/.cache/pypoetry/virtualenvs/ptest-eeSDLvcF-py3.6/bin/activate
poetry.lock
caches the resolved packages once we install things once.
pyproject.toml
, a warning will be shown otherwisepoetry update
updates everything to the latest versions, overwriting poetry.lock
poetry init
initializes a project and creates a pyproject.toml
interactively, allowing even to search for packages etc!Adding packages:
poetry add yaml
adds a packagepoetry search yaml
looks for packages in remote repos! Will tell you that you actually want pyyaml
Providing a __main__.py
along with __init__.py
makes the package itself executable:
$ python -m module_name
__main__.py
would have an usual if __name__ == "__main__" block
and run stuff imported from other files of that package.
(From a python riddle at work)
Things declared in if __name__ == '__main__'
are in global scope. Not because it’s special, but because ..global scope. All these bugs go away if you move main()
to a separate function.
Code from SO answer:[^2]
In main:
>>> if __name__ == '__main__':
... x = 1
... print 'x' in globals()
True
Inside a function:
>>> def foo():
... if __name__ == '__main__':
... bar = 1
... foo()
... print 'bar' in globals()
False
Python doesn’t have block-local scope, so any variables you use inside an if block will be added to the closest enclosing “real” scope.
Someone mentioned that if __name__ == '__main__'
can happen anywhere in the code. Never thought about this
If sync is enabled, in settings -> Sync there’s a “Deleted files” with versions and actions.
If not, unless a setting is set to delete to Obsidian’s trash, it’s left to the filesystem, so trash can or extundelete
in my case or whatever.
Link by M.O.: nix-community/nix-data-science: Standard set of packages and overlays for data-scientists [maintainer=@tbenst]
Detectron’s Instances object gets created like this, creating attributes with names unknown initially:
def __init__(self, image_size: Tuple[int, int], **kwargs: Any):
"""
Args:
image_size (height, width): the spatial size of the image.
kwargs: fields to add to this `Instances`.
"""
self._image_size = image_size
self._fields: Dict[str, Any] = {}
for k, v in kwargs.items():
self.set(k, v)
Which is neat.
To create an Instances object for unit tests I did:
pred_boxes = Boxes(tensor(
[
[ 143.8892, 1166.6632, 1358.7292, 1411.6588],
[ 131.3727, 864.3126, 1355.7804, 1144.3668],
[ 585.6373, 747.7184, 922.6433, 815.9998]
]))
scores = tensor(
[0.9971, 0.9967, 0.9938]
)
pred_classes = tensor([3, 3, 3])
instances = Instances(
image_size=(2122, 1500),
scores=scores,
pred_classes=pred_classes,
pred_boxes=pred_boxes
)
Found this in old markdown code from my old blog, I guess I forgot about this:
<what@ever.com>
<https://example.com>
When opening a lot of files as vim -p *.md*
only 10 kept being opened, finally googled it.
Solution: adding set tabpagemax=50
to ~/.vimrc
From SO1:
find . -name '*.php' -exec sed -i -e 's/www.fubar.com/www.fubar.ftw.com/g' {} \;