Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
vlad-bezden / cleanup.md
Last active April 5, 2019 20:35
How to delete all installed python packages

To uninstall all packages in current venv

On Linux:

$ pip freeze > args | pip uninstall -y -r args | rm args

On Windows:

@vlad-bezden
vlad-bezden / setup.py
Created March 24, 2019 14:50
Minimum setup.py requirements if project is structured with "src" directory
"""
Project structure:
demo_reader
src
demo_reader
compressed
__init__.py
bzipped.py
gzipped.py
@vlad-bezden
vlad-bezden / flatten.py
Created March 22, 2019 15:36
Flatten list of lists of etc to one dimensional list
"""
Flatten a List
There is a list which contains integers or other nested lists which may contain yet
more lists and integers which then may contain more lists ...
You should put all of the integer values into one flat list. The order should be as it
was in the original list with string representation from left to right.
Your code should be shorter than 140 characters (with whitespaces).
@vlad-bezden
vlad-bezden / setup.bat
Last active March 21, 2019 16:51
New Python project setup files, virtual environment (venv), activate venv, install required library and open VS Code on Windows platform
: create virtual environment
python -m venv ./venv
: create activate.bat file
echo .\venv\scripts\activate.bat > activate.bat
: activate virtual env
call activate.bat
: update pip
@vlad-bezden
vlad-bezden / file_loader.py
Created March 17, 2019 14:47
Different ways to load data from the file
file = "some_file_path.csv"
# Option #1 using readlines(). It will create list of lines
with open(file, "r") as reader:
lines = reader.readlines()
# Option #2. Read line by line and add to the list
with open(file, "r") as reader:
lines = []
for line in reader:
@vlad-bezden
vlad-bezden / pi.ipynb
Created March 9, 2019 11:23
Calculating pi using Leibniz formula
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vlad-bezden
vlad-bezden / classic.ipynb
Created March 8, 2019 15:52
Factory Method Design Pattern in Python and it's Implementations
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@vlad-bezden
vlad-bezden / inheritance_and_cls_annotation.py
Created February 18, 2019 17:02
An example of how to annotate cls return type class in inheritance using mypy in order to escape error `AttributeError: type object 'A' has no attribute 'bazz'`
"""
An example of how to annotate cls return type class in inheritance using mypy
in order to escape error `AttributeError: type object 'A' has no attribute 'bazz'`
Here is what we done:
* The type variable TA is used to denote that return values might be an
instances of subclasses of A.
* Specify that A is an upper bound for TA.
Specifying bound means that TA will only be A or one of its subclasses.
This is needed to properly restrict the types that are allowed.
@vlad-bezden
vlad-bezden / matrix.py
Created January 21, 2019 17:42
Creates 2D array (matrix)
"""
Creates 2D matrix
"""
def matrix(rows, cols, start=0):
return [[c + start + r * cols for c in range(cols)] for r in range(rows)]
assert matrix(2, 3, 1) == [[1, 2, 3], [4, 5, 6]]
@vlad-bezden
vlad-bezden / memory_alloc_performance.py
Last active January 19, 2019 19:56
Performance results for different type of memory allocation
"""
List allocation with teh same value using different techniques
The winner is [value] * times by between 15-25 times
"""
from timeit import timeit
from itertools import repeat