Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
O(1)
bool IsFirstElementNull(IList<String> elements)
{
return elements[0] == null;
}
O(N)
bool ContainsValue(IEnumerable<string> elements, string value)
{
foreach (var element in elements)
{
if (element == value) return true;
}
return false;
}
O(N^2)
bool ContainsDuplicates(IList<string> elements)
{
for (var outer = 0; outer < elements.Count; outer++)
{
for (var inner = 0; inner < elements.Count; inner++)
{
// Don't compare with self
if (outer == inner) continue;
if (elements[outer] == elements[inner]) return true;
}
}
return false;
}
O(2^N)
int Fibonacci(int number)
{
if (number <= 1) return number;
return Fibonacci(number - 2) + Fibonacci(number - 1);
}
O(log N)
x = 23
=> x is assigned the value of 23Code executes in order from top to bottom
x = 23
y = x
x = 12
print(y) //=> y = 23
print(x) //=> x = 12
ints, floats, strings, tuples
rebinding
x = x + 1
x
to the value of x + 1
mutating
nums.append(7)
7
to the end of the listint
: int
is immutablefor
loop, we constantly assign new values to x
as we go through the sequencecurl https://pyenv.run | bash
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev zlib1g-dev libssl-dev openssl libgdbm-dev libgdbm-compat-dev liblzma-dev libreadline-dev libncursesw5-dev libffi-dev uuid-dev
pyenv install VERSION_YOU_WOULD_LIKE_TO_INSTALL
curl
curl -sSL
https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
pip
# Create a virtual environment called tools that is based on 3.7.5
pyenv virtualenv 3.7.5 tools
# Install poetry into the tools virtual env
pyenv activate tools
pip install poetry
# Check installed poetry version
poetry --version
# Leave the virtual env
pyenv deactivate
# This does not work yet
poetry --version
# Add your tools virtual env to the globally available ones
pyenv global 3.7.5 tools
# Now this works and you can start using poetry
poetry --version
Configure so that the virtual environment is created inside your project folder
# That seems to be peotry prior to 1.0.0
poetry config settings.virtualenvs.in-project true
# That is poetry since 1.0.0
poetry config virtualenvs.in-project true
Initializing a new project
# Initialze a new project
poetry new dsexample
cd dsexample
# Add modules and create virtual environment.
poetry add pandas=0.25 fastapi --extras all
# As an example of how you could add a git module
poetry add tf2-utils --git git@github.com:Shawe82/tf2-utils.git
Installation
# We add black as a development dependency with --dev as we don't
# need it when it comes to production
poetry add --dev black=19.3b0
# Assume we are inside the current toplevel dsexample folder
poetry run black .
Install
# We add mypy as a development dependency with --dev as we don't
# need it when it comes to production
poetry add --dev mypy
# Assume we are inside the current toplevel dsexample folder
poetry run mypy .
# Install pre-commit into the tools virtual env
pyenv activate tools
pip install pre-commit
# Leave the virtual env
pyenv deactivate
# As we have already added the tool venv, it will work directly
pre-commit --version
Example
repos:
- repo: https://github.com/ambv/black
rev: 19.3b0
hooks:
- id: black
language_version: python3.7
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.740
hooks:
- id: mypy
Next, you must tell pre-commit to set up the hooks by executing
# I assume your are in the top level folder
pre-commit install
To run, execute:
pre-commit run --all-files