Python - General
This assumes you are using Python 3. (I am also using Git Bash as my shell)
Venv
Setup a virtual environment
It is a good idea to install packages in virtual environment so that we do not clutter our main python install, and we can easily get rid of the packages if we want. First we need to create the virtual enveronment. Navigat to a folder where you want your venv and type cd into it and then run:
winpty python3 -m venv /path/to/new/virtual/environment
Note if you use Git Bash you need to use the winpty utility to run some programs. See here for more info
What this will do is to create a virtual environment (inside a new folder called venv) that will be placed inside the current directory. The next step is to activate the environment, we do this by runnung:
. venv/Scripts/activate
If the venv was activated your terminal should display (venv) in front of the drive letter.
If you wish to deactivate the venv run this:
deactivate
Use the venv in SublimeText
There are probably better ways of running a venv in sublime but this brute force method works fine. Just add a new build system and point it to the python.exe in you venv.
{
"cmd": ["C:/path/to/venv/Scripts/python.exe", "-u", "$file"],
"selector": "source.python",
"file_regex": "^\\s*File \"(...*?)\", line ([0-9]*)"
}
Pip
To see which packages that are already installed we can ran:
pip list
Package Version
---------- -------
pip 20.2.1
setuptools 49.2.1
Dictionary
Sorting
data = {'B':(2, 1), 'C':(0, 2), 'A':(1, 0)}
print(dict(sorted(data.items())))
# {'A': (1, 0), 'B': (2, 1), 'C': (0, 2)}
print(dict(sorted(data.items(), key=lambda item: item[1][0])))
# {'C': (0, 2), 'A': (1, 0), 'B': (2, 1)}
print(dict(sorted(data.items(), key=lambda item: item[1][1])))
{'A': (1, 0), 'B': (2, 1), 'C': (0, 2)}
data = {'B':{'width':2, 'height':1}, 'C':{'width':0, 'height':2}, 'A':{'width':1, 'height':0}}
print(dict(sorted(data.items())))
# {'A': {'width': 1, 'height': 0}, 'B': {'width': 2, 'height': 1}, 'C': {'width': 0, 'height': 2}}
print(dict(sorted(data.items(), key=lambda item: item[1]['width'])))
# {'C': {'width': 0, 'height': 2}, 'A': {'width': 1, 'height': 0}, 'B': {'width': 2, 'height': 1}}
print(dict(sorted(data.items(), key=lambda item: item[1]['height'])))
# {'A': {'width': 1, 'height': 0}, 'B': {'width': 2, 'height': 1}, 'C': {'width': 0, 'height': 2}}
Misc
Environ
import os
for k, v in os.environ.items():
print(f'{k}={v}')
print(os.environ['OCIO'])
print(os.environ.get('OCIO'))
Inspect
MRO
Method Resolution Order
MRO is a concept used in inheritance. It is the order in which a method is searched for in a classes hierarchy and is especially useful in Python because Python supports multiple inheritance.
In Python, the MRO is from bottom to top and left to right. This means that, first, the method is searched in the class of the object. If it’s not found, it is searched in the immediate super class. In the case of multiple super classes, it is searched left to right, in the order by which was declared by the developer. For example:
node = hou.node('/obj/geo1/null1')
node.__class__.__mro__
# (<class 'hou.SopNode'>, <class 'hou.Node'>, <class 'hou.NetworkMovableItem'>, <class 'hou.NetworkItem'>, <class 'object'>)