Difference between revisions of "Python"
From Hawk Wiki
(Created page with "## Python 3.6 type checking make sure python is 3.6 or above <pre> python3 -m pip install -U mypy</pre> Example code with typecheck <pre class="brush:python"> from typing imp...") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | = Python 3.6 type checking = | |
− | + | ||
+ | Make sure python is 3.6 or above | ||
<pre> python3 -m pip install -U mypy</pre> | <pre> python3 -m pip install -U mypy</pre> | ||
Line 16: | Line 17: | ||
</pre> | </pre> | ||
− | + | To test typing, just run | |
<pre> | <pre> | ||
− | mypy testtype. | + | mypy testtype.py |
+ | </pre> | ||
+ | |||
+ | == install pyenv == | ||
+ | |||
<pre> | <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> |
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