Skip to content

Instantly share code, notes, and snippets.

View pavelpy's full-sized avatar
🎯
Focusing

Pavel pavelpy

🎯
Focusing
View GitHub Profile
@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 / 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 / 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

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.

# 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
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):
@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()