Difference between revisions of "Python"
From Hawk Wiki
m |
|||
Line 20: | Line 20: | ||
<pre> | <pre> | ||
mypy testtype.py | mypy testtype.py | ||
+ | </pre> | ||
+ | |||
+ | == install pyenv == | ||
+ | |||
+ | <pre> | ||
+ | |||
+ | sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \ | ||
+ | libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ | ||
+ | libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev | ||
+ | |||
+ | curl https://pyenv.run | bash | ||
+ | |||
+ | vim ~/.bashrc | ||
+ | |||
+ | # add | ||
+ | # pyenv | ||
+ | export PATH="$HOME/.pyenv/bin:$PATH" | ||
+ | eval "$(pyenv init --path)" | ||
+ | eval "$(pyenv virtualenv-init -)" | ||
+ | |||
+ | |||
+ | pyenv install 3.8.16 | ||
+ | pyenv global 3.8.16 | ||
+ | |||
</pre> | </pre> |
Latest revision as of 03:14, 14 August 2023
Python 3.6 type checking
Make sure python is 3.6 or above
python3 -m pip install -U mypy
Example code with typecheck
from typing import List Vector = List[float] def scale(scalar: float, vector: Vector) -> Vector: return [scalar * num for num in vector] # typechecks; a list of floats qualifies as a Vector. new_vector = scale(2.0, [1.0, -4.2, 5.4]) print(new_vector)
To test typing, just run
mypy testtype.py
install pyenv
sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev curl https://pyenv.run | bash vim ~/.bashrc # add # pyenv export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)" pyenv install 3.8.16 pyenv global 3.8.16