basic
- python float is usually implemented with c double: 64 bit
.format
esacpe{}
by doubling it, ``- use a
f'string {replace}'
in python3
- use a
http://bokeh.org/
inplace operation
__add__
vs__iadd__
a = b + c
vsb += c
- note that new PyObject is created when operation is not inplace
asyncio
- move blocking function to another process:
debug
import debugpy
# 5678 is the default attach port in the VS Code
debugpy.listen(5678)
debugpy.wait_for_client()
debugpy.breakpoint()
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
numpy
indexing
Ellipsis
expands to the number of : objects needed for the selection tuple to index all dimensions.- Each
newaxis
object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimensionnewaxis
is a alias ofNone
- operation on certain axis means to increment index along that axis.
>>> x = np.array([[[1],[2],[3]], [[4],[5],[6]]])
>>> x.shape
(2, 3, 1)
>>> x[...,0]
array([[1, 2, 3],
[4, 5, 6]])
>>> x[:,np.newaxis,:,:].shape
(2, 1, 3, 1)
boardcast
- Two dimensions are compatible when
- they are equal, or
- one of them is 1
- Lining up the sizes of the trailing axes according to the broadcast rules, will shows if arrays are compatible
- Dimensions with size 1 are stretched or “copied” to match the other
env
- override python malloc at cmd:
PYTHONMALLOC=malloc
- the regular pymalloc seems to cause false positive for valgrind
https://docs.python.org/3/using/cmdline.html
edit conda environment
- install conda with
-p <path>
Create activate and deactivate file as suggested in document.
# addition
export LD_LIBRARY_PATH=<value>${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# removal
export LD_LIBRARY_PATH=$(echo ${LD_LIBRARY_PATH} | sed -e 's|<value>:\?||')
module / package
Module serves as an organizational unit of Python code. Modules have a namespace containing arbitrary Python objects. Modules are loaded into Python by the process of importing.
Python has only one type of module object, and all modules are of this type, regardless of whether the module is implemented in Python, C, or something else. To help organize modules and provide a naming hierarchy, Python has a concept of packages. It’s important to keep in mind that all packages are modules, but not all modules are packages.
import <>
: import a module into local namespacefrom <> import <>
:- find the module specified in the
from
clause - import the
attribute
with that name- attribute can be a function, class or variable
- find the module specified in the
sample package
sound/ Top-level package
__init__.py Initialize the sound package
formats/ Subpackage for file format conversions
__init__.py
wavread.py
wavwrite.py
effects/ Subpackage for sound effects
__init__.py
echo.py
surround.py
- The
__init__.py
files are required to make Python treat directories containing the file as packages. - Absolute imports: import start from the toplevel pacakge
- may use either the
import <>
orfrom <> import <>
syntax - ex
from sound.effects import echo
- may use either the
- Relative import:
- may only use the form
from <> import <>
-
import XXX.YYY.ZZZ should expose XXX.YYY.ZZZ as a usable expression, but .moduleY is not a valid expression
-
-
Two or more leading dots indicate a relative import to the parent(s) of the current package, one level per dot after the first
- ex for surround.py
from . import echo
from .. import formats
from ..formats import wavwrite
- may only use the form
- consideration for
main
relative imports are based on the name of the current module. Since the name of the main module is always
__main__
, modules intended for use as the main module of a Python application must always use absolute imports.
import at runtime
my_module = importlib.import_module('os.path')
extending python
There are multiple ways to extend python.
- ctypes: Load library and map python types to C when calling
- cffi: Generate function interface based on source code?
- extension module: specific to Cython, let library register itself
- pybind11 is a very convenient wrapper around this interface
- module search path
- site
pybind11
py::object hold reference to pyobject py::handle does not reference
# links to follow
https://github.com/pybind/cmake_example/blob/master/setup.py
https://pybind11.readthedocs.io/en/stable/advanced/cast/strings.html
https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html?highlight=bytes%20to%20vector#making-opaque-types
https://pybind11.readthedocs.io/en/stable/advanced/functions.html#return-value-policies
https://pybind11.readthedocs.io/en/stable/advanced/misc.html?highlight=py%3A%3Afunction#global-interpreter-lock-gil
https://pybind11.readthedocs.io/en/stable/faq.html
package
schedule queue.work_done() queue.join()
trio?
python-config –cflags: do we need this to be binary compatible? –ldflags: do we need this at all? –includes : seems to be helpful
https://docs.python.org/3/distutils/setupscript.html#installing-package-data
python3 -m sysconfig | less |
https://pypi.org/project/accelerate/
simple http server
python3 -m http.server 8000
https://nuitka.net/doc/user-manual.html