Skip to content

Instantly share code, notes, and snippets.

View pavdmyt's full-sized avatar
🛠️
Focusing

Pavel Dmytrenko pavdmyt

🛠️
Focusing
View GitHub Profile
@pavdmyt
pavdmyt / some_factorials.py
Created October 1, 2015 16:31
Few ways to calculate factorial of the number.
"""
Few ways to calculate factorial of the number.
For my article: http://pavdmyt.com/digging-around-factorial-function/
"""
import math as m
def fact(x):
"""
Weird factorial function implementation
@pavdmyt
pavdmyt / last_item.py
Created October 29, 2015 08:51
Iterate over a list of tuples, ignore all but the last item in each tuple.
tuples = [(1, 2), (3, 4, 5), (6, 7, 8, 9)]
for *_, last in tuples:
print(last)
# Prints: 2, 5, 9
@pavdmyt
pavdmyt / infinite_for_loops.py
Created August 29, 2016 15:26
Some implementations of infinite / endless for loops in Python.
"""
Some implementations of infinite / endless for loops in Python.
No practical applications, just pure theoretical interest :)
C/C++ example:
for (;;) {}
src: https://stackoverflow.com/questions/5737196/an-expression-for-an-infinite-generator
"""
import itertools
@pavdmyt
pavdmyt / cached_property.py
Last active September 22, 2016 12:53
Code snippets
"""
Is analogous to @reify
Borrowed from here: https://github.com/aio-libs/yarl/blob/master/yarl/__init__.py
Topic references:
https://www.reddit.com/r/learnpython/comments/2oqusj/pyramids_reify_descriptor_could_it_be_simplified/
http://www.gghh.name/dibtp/2013/06/18/caching-in-python-with-a-descriptor-and-a-decorator.html
"""
@pavdmyt
pavdmyt / clean_requirements.py
Created February 1, 2017 09:55
clean requirements-dev.in
#!/usr/bin/env python
"""
Remove all packages from requirements-dev.txt
except of listed in PACKAGES.
"""
import os
@pavdmyt
pavdmyt / syncer.py
Last active August 15, 2017 09:36
Daemon to sync current dir contents with remote
# -*- coding: utf-8 -*-
"""
A daemon that monitors files mtimes and automaticaly syncs it
with remote if files have changed.
How to use:
~~~~~~~~~~~
1. $ python syncer.py
@pavdmyt
pavdmyt / ipv4_netmasks.py
Last active August 17, 2017 10:26
[Python] Print all valid IPv4 netmasks in dot-decimal notation.
# -*- coding: utf-8 -*-
"""
Print all valid IPv4 netmasks in dot-decimal notation.
Netmask reference chart:
http://unixwiz.net/techtips/netmask-ref.html
"""
@pavdmyt
pavdmyt / core_i3.txt
Last active March 20, 2018 16:18
Benchmarking various factorial function implementations.
Logical CPUs: 4
Value to calculate: 1e+06
---
SimpleFactFast -- elapsed time: 2.909772092s
HalfIterFact -- elapsed time: 1m8.50245915s
SimpleFactIter -- elapsed time: 1m57.486627904s
ConcFactIter :: workers 1 -- elapsed time: 1m54.401843303s
ConcFactIter :: workers 2 -- elapsed time: 42.335188303s
"""
Some func problems
~~~~~~~~~~~~~~~~~~
Provide your solutions and run this module.
"""
def add(a, b):
@pavdmyt
pavdmyt / .pypirc
Created January 27, 2019 22:49
pypirc; upload package to PyPI
# ~/.pypirc upload to PyPI (legacy way)
# https://medium.freecodecamp.org/how-to-publish-a-pyton-package-on-pypi-a89e9522ce24
[distutils]
index-servers=pypi
[pypi]
repository = https://upload.pypi.org/legacy/
username = <pypi-user>
password = <pypi-pwd>