This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# So, assuming the lists have the same length, e.g.: | |
dummy_data = { | |
"Date":["01/24/2029","10/28/2027", "01/24/2024","03/24/2024"], | |
"Company Name":["Mcdonalds", "Burger King", "KFC","Popeyes"], | |
"File_name_location":["C/Documents/Files/invoice1.pdf", "C/Documents/Files/invoice1.pdf","C/Documents/Files/invoice1.pdf", "C/Documents/Files/invoice1.pdf"], | |
} | |
# Then, to sort everything, simply get a list of sorted indices. | |
# This can be done easily. Since you don't want `datetime.date` objects |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
import copy | |
def clone_class(klass): | |
def _exec_body(ns): | |
ns_no_slots = { | |
k:v for k,v in vars(klass).items() | |
if not isinstance(v, types.MemberDescriptorType) | |
} | |
ns |= copy.deepcopy(ns_no_slots) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
def is_leap_year(year: int) -> int: | |
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) | |
def add_months(date: datetime.date, months: int) -> datetime.date: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BagOfClass(type): | |
class Value: | |
pass | |
def __iter__(klass): | |
for attr_value in vars(klass).values(): | |
if isinstance(attr_value, type) and issubclass( | |
attr_value, BagOfClass.Value | |
): | |
yield attr_value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleSuper: | |
def __init__(self, cls, instance): | |
self.cls = cls | |
self.instance = instance | |
def __getattr__(self, name): | |
mro = type(self.instance).mro() | |
next_cls = mro[mro.index(self.cls) + 1] | |
attribute = getattr(next_cls, name) | |
if hasattr(attribute, "__get__"): | |
return attribute.__get__(self.instance, self.cls) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import pandas as pd | |
from timeit import timeit | |
setup = """ | |
def list_comp(d): | |
return [ (x*2)/3 for x in d ] | |
def pre_allocate(d): | |
result = [None]*len(d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import matplotlib.pyplot as plt | |
from timeit import timeit | |
from tqdm import tqdm | |
gen_exp = "tuple(x for x in range({}))" | |
list_comp = "tuple([x for x in range({})])" | |
N = range(1, 500_001, 5000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_code(msg): | |
f,l,i=[item for item in msg.split('0') if len(item)>0] | |
return {'first_name':f,'last_name':l,'id':i} | |
def parse_code(msg): | |
result = [] | |
for item in msg.split("0"): | |
if len(item) > 0: | |
result.append(item) | |
f,l,i = result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.read_csv('en_bigram.csv') | |
st = df[df["right"] == "some_text"]["left"] | |
st[st.str.startswith("My")].to_list() | |
# using a dict reader | |
import csv | |
result = [] | |
with open("en_bigram.csv") as f: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Range: | |
def __init__(self, start, stop, step=1): | |
self.start = start | |
self.stop = stop | |
self.step = step | |
def __getitem__(self, index): | |
result = self.start + index*self.step | |
if result >= self.stop: | |
raise IndexError("Index out of range") | |
return result |
NewerOlder