# Import pandas and numpy for illustration purposes.
import pandas as pd
import numpy as np
class TestData(object):
def selfDataFrame(self):
return pd.DataFrame(np.random.rand(10), columns=['A'])
@classmethod
def classDataFrame(cls):
return pd.DataFrame(np.random.rand(10), columns=['A'])
@staticmethod
def staticDataFrame():
return pd.DataFrame(np.random.rand(10), columns=['A'])
# myTest = TestData()
# TestData.selfDataFrame(object)
# TestData.classDataFrame()
try:
TestData.classDataFrame(object)
except TypeError as err:
print("Awww dang... @classmethod can'")
# TestData.staticDataFrame()
Awww dang... @classmethod can'
class Test(object):
@classmethod
def method_one(cls):
print("Called method_one")
@staticmethod
def method_two():
print("Called method_two")
# a_test = Test()
# a_test.method_one()
# a_test.method_two()
Test.method_two()
Called method_two
def my_decorator(some_function):
def wrapper():
print("Something is happening before some_function() is called.")
some_function()
print("Something is happening after some_function() is called.")
return wrapper
def just_some_function():
print("Wheee!")
just_some_function = my_decorator(just_some_function)
just_some_function()
Something is happening before some_function() is called. Wheee! Something is happening after some_function() is called.
def conditional_kwargs(**nkwargs):
"""Returns a function that will define a keywords if they have not been not supplied.
"""
def decorator(some_function):
def wrapper(nkwargs=nkwargs, *args, **kwargs):
for k,v in nkwargs.items():
if k not in kwargs:
kwargs[k]=v
else:
pass
return some_function(*args, **kwargs)
return wrapper
return decorator
@conditional_kwargs(thing=42, greetings='hello world')
def some_function(**options):
print(options['greetings'])
print(options['thing'])
# Here I fail to supply the keyword arguments so they will be supplied by the decorator.
some_function()
# Here I have defined one of the keyword arguments.
some_function(greetings='hi')
hello world 42 hi 42
def check_authorization(f):
def wrapper(*args):
print(args[0].url)
return f(*args)
return wrapper
class Client(object):
def __init__(self, url):
self.url = url
@check_authorization
def get(self):
print('get')
Client('http://www.google.com').get()
http://www.google.com get
def check(f):
def wrapper(*args):
print(args[0].url)
return f(*args)
return wrapper
class Client(object):
def __init__(self, url):
self.url = url
@check_authorization
def get(self):
print('get')
Client('http://www.google.com').get()
http://www.google.com get
import functools
def decorator(**dkwargs):
def _decorate(function):
@functools.wraps(function)
def wrapped_function(*args, **kwargs):
return
return wrapped_function
if original_function:
return _decorate(original_function)
return _decorate
def parametrized(dec):
def layer(*args, **kwargs):
def repl(f):
return dec(f, *args, **kwargs)
return repl
return layer
@parametrized
def multiply(f, n):
def aux(*xs, **kws):
return n * f(*xs, **kws)
return aux
@multiply(2)
def function(a):
return 10 + a
print(function(3))
26
class MyDec(object):
def __init__(self,flag):
self.flag = flag
def __call__(self, original_func):
decorator_self = self
def wrappee( *args, **kwargs):
print('in decorator before wrapee with flag ',decorator_self.flag)
original_func(*args,**kwargs)
print('in decorator after wrapee with flag ',decorator_self.flag)
return wrappee
@MyDec('foo de fa fa')
def bar(a,b,c):
print('in bar',a,b,c)
bar('x','y','z')
in decorator before wrapee with flag foo de fa fa in bar x y z in decorator after wrapee with flag foo de fa fa
from threading import Lock
def synchronized(function):
"""
Given a method, return a new method that acquires a
lock, calls the given method, and then releases the
lock.
"""
def wrapper(self, *args, **kwargs):
"""A synchronized wrapper."""
with self._lock:
return function(self, *args, **kwargs)
return wrapper
class ExampleSynchronizedClass:
def __init__(self):
self._lock = Lock()
self._items = []
# Problematic usage:
def add(self, item):
"""Add a new item."""
self._items.append(item)
add = synchronized(add)
esc = ExampleSynchronizedClass()
esc.add(1)
class ExampleSynchronizedClass:
def __init__(self):
self._lock = Lock()
self._items = []
# Nicer decorator usage:
@synchronized
def add(self, item):
"""Add a new item."""
self._items.append(item)
esc = ExampleSynchronizedClass()
esc.add()
import wrapt
from threading import Lock
@wrapt.decorator
def synchronized(function, self, args, kwargs):
"""
Given a method, return a new method that acquires a
lock, calls the given method, and then releases the
lock.
"""
with self._lock:
return function(*args, **kwargs)
class ExampleSynchronizedClass:
def __init__(self):
self._lock = Lock()
self._items = []
# Nicer decorator usage:
@synchronized
def add(self, item):
"""Add a new item."""
self._items.append(item)
esc = ExampleSynchronizedClass()
esc.add(1)
import wrapt
@wrapt.decorator
def basic(function, self, args, kwargs):
'A basic decorator.'
self.hello = 'hello'
class Decked(object):
@basic
def __init__(self):
pass
d = Decked()
d.hello
'hello'
def optional_arg_decorator(fn):
def wrapped_decorator(*args, **kwargs):
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
return fn(args[0])
else:
def real_decorator(decoratee):
return fn(decoratee, *args, **kwargs)
return real_decorator
return wrapped_decorator
class MetaCls(type):
def __getitem__(cls, index):
print("Using meta __getitem__ on classes that have my type")
# metaclass is defined in header:
class Base(metaclass=MetaCls):
pass
Base[0]
Using meta __getitem__ on classes that have my type
class MyMetaClass(type):
def __getitem__(cls, x):
return getattr(cls, x)
def __new__(cls, name, parents, dct):
dct["__getitem__"] = cls.__getitem__
return super().__new__(cls, name, parents, dct)
class Dog(metaclass=MyMetaClass):
x = 10
d = Dog()
print(d['x'])
10
class Base(object):
x = 0
Base.__getitem__
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-117-f9e37aaa013d> in <module>() 1 class Base(object): 2 x = 0 ----> 3 Base.__getitem__ AttributeError: type object 'Base' has no attribute '__getitem__'
class Base(object):
x = 0
def __getitem__(self, value):
return self.x.__dict__[value]
Base['x']
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-116-0244595410db> in <module>() ----> 1 Base['x'] TypeError: 'type' object is not subscriptable
from itertools import islice
class Sliceable(object):
"""Sliceable(iterable) is an object that wraps 'iterable' and
generates items from 'iterable' when subscripted. For example:
>>> from itertools import count, cycle
>>> s = Sliceable(count())
>>> list(s[3:10:2])
[3, 5, 7, 9]
>>> list(s[3:6])
[13, 14, 15]
>>> next(Sliceable(cycle(range(7)))[11])
4
>>> s['string']
Traceback (most recent call last):
...
KeyError: 'Key must be non-negative integer or slice, not string'
"""
def __init__(self, iterable):
self.iterable = iterable
def __getitem__(self, key):
if isinstance(key, int) and key >= 0:
return islice(self.iterable, key, key + 1)
elif isinstance(key, slice):
return islice(self.iterable, key.start, key.stop, key.step)
else:
raise KeyError("Key must be non-negative integer or slice, not {}"
.format(key))
from itertools import count, cycle
s = Sliceable(count())
list(s[3:10:2])
[3, 5, 7, 9]
list(s[0])
[10]
from itertools import islice
class Sliceable(object):
"""Sliceable(iterable) is an object that wraps 'iterable' and
generates items from 'iterable' when subscripted. For example:
>>> from itertools import count, cycle
>>> s = Sliceable(count())
>>> list(s[3:10:2])
[3, 5, 7, 9]
>>> list(s[3:6])
[13, 14, 15]
>>> next(Sliceable(cycle(range(7)))[11])
4
>>> s['string']
Traceback (most recent call last):
...
KeyError: 'Key must be non-negative integer or slice, not string'
"""
def __init__(self, iterable):
self.iterable = iterable
def __getitem__(self, key):
if isinstance(key, int) and key >= 0:
return islice(self.iterable, key, key + 1)
elif isinstance(key, slice):
return islice(self.iterable, key.start, key.stop, key.step)
elif isinstance(key, str):
if int(key) >= 0:
return islice(self.iterable, int(key), int(key) + 1)
else:
raise KeyError("Key must be non-negative integer or slice, not {}"
.format(key))
from itertools import count, cycle
s = Sliceable(count())
list(s[3:10:2])
[3, 5, 7, 9]
list(s[0])
[14]
list(s['0'])
[15]
class Container(object):
def __init__(self,*args,**kwargs):
self.value = 0
def __getitem__(self, key):
return self.__dict__[key]
def __setitem__(self, key, value):
self.__dict__[key] = value
c = Container()
c['value'] = 1
c.value
1
import wrapt
@wrapt.decorator
def basic(function, self, args, kwargs):
'A basic decorator.'
self.hello = 'hello'
import wrapt
@wrapt.decorator
def dict_like(function, self, *args, **kwargs):
def __getitem__(self, key):
return self.__dict__[key]
def __setitem__(self, key, value):
self.__dict__[key] = value
setattr(self, '__getitem__', __getitem__)
class Decked(object):
@dict_like
def __init__(self):
self.hello = 0
pass
d = Decked()
d['hello']
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-217-496f2ae749ce> in <module>() ----> 1 d['hello'] TypeError: 'Decked' object is not subscriptable
class F(object):
def __init__(self, fn):
self.__dict__['fn'] = fn
def __call__(self, *args, **kwargs):
return self.fn(*args, **kwargs)
def __getitem__(self, name):
return name
def __getattr__(self, name):
return name
def foo():
print('hello')
foo = F(foo)
import time
from functools import wraps
def function_timer(some_function):
"""Return the time a function takes to execute."""
@wraps(some_function)
def wrapper():
t1 = time.time()
some_function()
t2 = time.time()
return '{}: {}'.format(some_function.__name__, str((t2 - t1)))
return wrapper
@function_timer
def my_function():
num_list = []
for num in (range(0, 100000)):
num_list.append(num)
print("\nSum of all the numbers: " + str((sum(num_list))))
print(my_function())
Sum of all the numbers: 4999950000 my_function: 0.015599727630615234
my_function.__name__
'my_function'
def class_dec(self):
setattr(self, 'hello', 1)
return self
@class_dec
class DecoratedClass(object):
pass
dc = DecoratedClass()
dc.hello
1
from tkinter import Frame, ttk
from functools import partial
def button_entry(self, attrname):
setattr(self, '_'.join([attrname, 'button']), ttk.Button())
return self
button_entry_dec = partial(button_entry, attrname='thing')
@button_entry_dec
class DecoratedTk(object):
def __init__(self):
pass
dtk = DecoratedTk()
dtk.thing_button
<tkinter.ttk.Button object .!button4>
from tkinter import Frame, ttk
from functools import partial
def button_entry(self):
setattr(self, '_'.join([attrname, 'button']), ttk.Button())
partial(button_entry, attrname='thing')
return self
button_entry_dec = partial(button_entry, attrname='thing')
@button_entry_dec
class DecoratedTk(object):
def __init__(self):
pass
dtk = DecoratedTk()
dtk.thing_button
<tkinter.ttk.Button object .!button4>
def button_dec(prop):
def wrapper(self, *args, **kwargs):
return wrapper
class ExampleClass:
def __init__(self):
pass
@button_dec
def add(self):
pass
esc = ExampleClass()
from tkinter import Tk
dir(Tk)
['_Misc__winfo_getint', '_Misc__winfo_parseitem', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_bind', '_configure', '_displayof', '_getboolean', '_getconfigure', '_getconfigure1', '_getdoubles', '_getints', '_grid_configure', '_gridconvvalue', '_last_child_ids', '_loadtk', '_nametowidget', '_noarg_', '_options', '_register', '_report_exception', '_root', '_subst_format', '_subst_format_str', '_substitute', '_tclCommands', '_w', '_windowingsystem', 'after', 'after_cancel', 'after_idle', 'anchor', 'aspect', 'attributes', 'bbox', 'bell', 'bind', 'bind_all', 'bind_class', 'bindtags', 'cget', 'client', 'clipboard_append', 'clipboard_clear', 'clipboard_get', 'colormapwindows', 'columnconfigure', 'command', 'config', 'configure', 'deiconify', 'deletecommand', 'destroy', 'event_add', 'event_delete', 'event_generate', 'event_info', 'focus', 'focus_displayof', 'focus_force', 'focus_get', 'focus_lastfor', 'focus_set', 'focusmodel', 'forget', 'frame', 'geometry', 'getboolean', 'getdouble', 'getint', 'getvar', 'grab_current', 'grab_release', 'grab_set', 'grab_set_global', 'grab_status', 'grid', 'grid_anchor', 'grid_bbox', 'grid_columnconfigure', 'grid_location', 'grid_propagate', 'grid_rowconfigure', 'grid_size', 'grid_slaves', 'group', 'iconbitmap', 'iconify', 'iconmask', 'iconname', 'iconphoto', 'iconposition', 'iconwindow', 'image_names', 'image_types', 'keys', 'lift', 'loadtk', 'lower', 'mainloop', 'manage', 'maxsize', 'minsize', 'nametowidget', 'option_add', 'option_clear', 'option_get', 'option_readfile', 'overrideredirect', 'pack_propagate', 'pack_slaves', 'place_slaves', 'positionfrom', 'propagate', 'protocol', 'quit', 'readprofile', 'register', 'report_callback_exception', 'resizable', 'rowconfigure', 'selection_clear', 'selection_get', 'selection_handle', 'selection_own', 'selection_own_get', 'send', 'setvar', 'size', 'sizefrom', 'slaves', 'state', 'title', 'tk_bisque', 'tk_focusFollowsMouse', 'tk_focusNext', 'tk_focusPrev', 'tk_setPalette', 'tk_strictMotif', 'tkraise', 'transient', 'unbind', 'unbind_all', 'unbind_class', 'update', 'update_idletasks', 'wait_variable', 'wait_visibility', 'wait_window', 'waitvar', 'winfo_atom', 'winfo_atomname', 'winfo_cells', 'winfo_children', 'winfo_class', 'winfo_colormapfull', 'winfo_containing', 'winfo_depth', 'winfo_exists', 'winfo_fpixels', 'winfo_geometry', 'winfo_height', 'winfo_id', 'winfo_interps', 'winfo_ismapped', 'winfo_manager', 'winfo_name', 'winfo_parent', 'winfo_pathname', 'winfo_pixels', 'winfo_pointerx', 'winfo_pointerxy', 'winfo_pointery', 'winfo_reqheight', 'winfo_reqwidth', 'winfo_rgb', 'winfo_rootx', 'winfo_rooty', 'winfo_screen', 'winfo_screencells', 'winfo_screendepth', 'winfo_screenheight', 'winfo_screenmmheight', 'winfo_screenmmwidth', 'winfo_screenvisual', 'winfo_screenwidth', 'winfo_server', 'winfo_toplevel', 'winfo_viewable', 'winfo_visual', 'winfo_visualid', 'winfo_visualsavailable', 'winfo_vrootheight', 'winfo_vrootwidth', 'winfo_vrootx', 'winfo_vrooty', 'winfo_width', 'winfo_x', 'winfo_y', 'withdraw', 'wm_aspect', 'wm_attributes', 'wm_client', 'wm_colormapwindows', 'wm_command', 'wm_deiconify', 'wm_focusmodel', 'wm_forget', 'wm_frame', 'wm_geometry', 'wm_grid', 'wm_group', 'wm_iconbitmap', 'wm_iconify', 'wm_iconmask', 'wm_iconname', 'wm_iconphoto', 'wm_iconposition', 'wm_iconwindow', 'wm_manage', 'wm_maxsize', 'wm_minsize', 'wm_overrideredirect', 'wm_positionfrom', 'wm_protocol', 'wm_resizable', 'wm_sizefrom', 'wm_state', 'wm_title', 'wm_transient', 'wm_withdraw']
%gui
from sneakers.ttk import window, button, showInfo
def ex0():
# Tk windows
with window() as w:
# Change window properties like normal tkinter.
w.attributes("-topmost", True)
# Give a custom size and or postion.
w.geometry('200x300+100+100')
# Create buttons using decorators.
@button("Create a popup box")
# Attach a function to the button by defining it below the decorator.
def makePopupBox():
showInfo(message = "Hello World!")
ex0()
%gui
from sneakers.ttk import window, button, showInfo
def ex0():
with window() as w:
# Change window properties like normal tkinter.
w.attributes("-topmost", True)
# Give a custom size and or postion.
w.geometry('200x300+100+100')
# Create buttons using decorators.
@button("Create a popup box")
# Attach a function to the button by defining it below the decorator.
def makePopupBox():
showInfo(message = "Hello World!")
ex0()
from tkinter import filedialog
n = filedialog.askopenfilename()
from tkinter import Tk, filedialog
def select_files():
root = Tk()
root.attributes('-topmost', True)
root.withdraw()
return filedialog.askopenfilenames(parent=root)
select_files()
('C:/Users/t440p/Documents/JupyterNotebooks/DMPI/Muoio_Lab/S3vDS3vDKOSkm_10plex/pd22_result_data/Ashley Williams_S3vDS3vDKOSkm_10plex_Acetyl.Phospho.Input.v2_PeptideGroups.txt',)
from tkinter import filedialog
# dir(filedialog)
from tkinter import ttk
# dir(ttk)
# dir(ttk.tkinter)
# from tkinter.ttk.tkinter import filedialog
# dir(filedialog)
--------------------------------------------------------------------------- ModuleNotFoundError Traceback (most recent call last) <ipython-input-17-c949df42b7e6> in <module>() 3 # dir(ttk) 4 # dir(ttk.tkinter) ----> 5 from tkinter.ttk.tkinter import filedialog 6 dir(filedialog) ModuleNotFoundError: No module named 'tkinter.ttk.tkinter'; 'tkinter.ttk' is not a package
%gui
from sneakers.ttk import window, button, filedialog
def ex0():
with window() as w:
# Change window properties like normal tkinter.
# w.attributes("-topmost", True)
w.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)
return file_path
ex0()
print('hi')
hi
# from tkinter import simpledialog
# dir(simpledialog)
# dir(simpledialog.filedialog)