Skip to content

Instantly share code, notes, and snippets.

View pavelpy's full-sized avatar
🎯
Focusing

Pavel pavelpy

🎯
Focusing
View GitHub Profile
@pavelpy
pavelpy / super.py
Last active February 12, 2018 19:55
class Class1(object):
def __init__(self):
print('Class 1 init')
class Class2(Class1):
def __init__(self):
Class1.__init__(self)
super().__init__() # python3 only
super(Class2, self).__init()
def benchmark(func):
import time
def wrapper(*args, **kwargs):
t = time.clock()
res = func(*args, **kwargs)
print(func.__name__, time.clock() - t)
return res
return wrapper
def print_args(func):
# Original source: Raymond's Hettinger tweet
""" #python lets you run for-loops over partially consumed iterators.
This is useful for extracting and skipping over header rows:
"""
it = iter(f)
header = next(it)
for line in it:
...
# Original source: @julienbaley tweet

Due to some reason my top bar in gnome-panel got messed up and it not appearing as intended.

To fix this I tried to restore top bar in gnome-panel by using following command.

$ dconf reset -f /org/gnome/gnome-panel/ $ killall gnome-panel

After running above commands it appeared as intended.

@pavelpy
pavelpy / descriptors.py
Created February 28, 2018 16:01
Descriptors
"""
Descriptors
They're the magic behind a whole bunch of core Python features.
When you use dotted access to look up a member (eg, x.y), Python first looks for the member in the instance dictionary. If it's not found, it looks for it in the class dictionary. If it finds it in the class dictionary, and the object implements the descriptor protocol, instead of just returning it, Python executes it. A descriptor is any class that implements the __get__, __set__, or __delete__ methods.
"""
# Here's how you'd implement your own (read-only) version of property using descriptors:
class Property(object):
def __init__(self, fget):
self.fget = fget
@pavelpy
pavelpy / exploring_encoding_defaults.py
Created March 9, 2018 08:46
Exploring encoding defaults
import sys, locale
expressions = """
locale.getpreferredencoding()
type(my_file)
my_file.encoding
sys.stdout.isatty()
sys.stdout.encoding
sys.stdin.isatty()
sys.stdin.encoding
sys.stderr.isatty()
@pavelpy
pavelpy / .python-startup.py
Created July 13, 2018 08:30
export PYTHONSTARTUP=$HOME/.python-startup.py
import os
import readline
import rlcompleter
import atexit
history_file = os.path.join(os.environ['HOME'], '.python_history')
try:
readline.read_history_file(history_file)
except IOError:
@pavelpy
pavelpy / lru_cache_for_class_decorator.py
Last active April 11, 2024 08:57
python functools lru_cache with class methods
# origin: https://stackoverflow.com/questions/33672412/python-functools-lru-cache-with-class-methods-release-object
import functools
import weakref
def memoized_method(*lru_args, **lru_kwargs):
def decorator(func):
@functools.wraps(func)
def wrapped_func(self, *args, **kwargs):
# We're storing the wrapped method inside the instance. If we had
# a strong reference to self the instance would never die.
@pavelpy
pavelpy / cron_git_pull.sh
Created October 1, 2018 13:06
Task for cron for check if pull needed in Git
#!/bin/sh
# usage: cron_git_pull.sh [optional_branch_name]
git fetch
UPSTREAM=${1:-'@{u}'}
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse "$UPSTREAM")
BASE=$(git merge-base @ "$UPSTREAM")
if [ $LOCAL = $REMOTE ]; then
echo "Up-to-date"
@pavelpy
pavelpy / bypass_arguments_in_other_class_methods.py
Created October 10, 2018 11:39
bypass arguments in other class methods
"""
>>> api_instance = BypassApiKey(RemoteServerApi())
>>> api_instance.method_that_need_access_token(1, 2, 3)
[(1, 2, 3), {'access_token': 'test'}]
"""
import functools
ACCESS_TOKEN = 'test'
class RemoteServerApi: