Skip to content

Instantly share code, notes, and snippets.

@raeq
raeq / with_overloading.py
Last active May 2, 2021 12:22
Worked example of overloading
import math
from functools import singledispatch
class Square:
length: int
def __init__(self, length=0):
self.length = length
from functools import singledispatch
@singledispatch
def area(any_object):
raise NotImplementedError
@area.register
def _(any_object: Circle):
return math.pi * (math.pow(any_object.radius, 2))
@raeq
raeq / which_dispatcher.py
Created May 2, 2021 12:26
Which dispatcher will be called
from functools import singledispatch
@singledispatch
def fancy_print(s):
raise NotImplementedError
@fancy_print.register(int)
def _ints(s):
@raeq
raeq / which_dispatcher.py
Created May 2, 2021 12:38
Which Dispatcher
from functools import singledispatch
@singledispatch
def fancy_print(s):
raise NotImplementedError
@fancy_print.register(int)
def _ints(s):
@raeq
raeq / method_overloading.py
Last active May 2, 2021 21:30
method_overloading
from functools import singledispatchmethod
class Dog:
@singledispatchmethod
def bark(self, quality):
raise NotImplementedError
@bark.register
@raeq
raeq / morse.py
Created October 15, 2021 20:14
A package for working with Morse
"""A package for Morse encoding and decoding."""
from collections import UserDict
from time import sleep
from typing import NamedTuple
from pysinewave import SineWave
class MorseDict(UserDict):
def __init__(self, dictionary=None):
@raeq
raeq / scratch_32.py
Created October 29, 2021 15:40
Chemical Structure
from typing import NamedTuple, List
class Chemical_Structure(NamedTuple):
name: str
synonyms: List[str]
iupac_name: str
pubchem_cid: str
inchi: str
inchi_key: str
@raeq
raeq / cards.py
Created November 25, 2021 17:56
Simulate playing cards
import random
from typing import NamedTuple
class Card(NamedTuple):
suit: str
face: str
face_value: int
@property
def description(self):
import random
from collections import Counter
from enum import Enum, IntEnum, unique
@unique
class Outcome(Enum):
COMPUTER = 2
TIED = 0
PLAYER = 1
import more_itertools
def get_data(filename: str) -> list:
with open(filename, 'r') as f:
return [int(line.rstrip('\n')) for line in f]
def make_couples(singles: list) -> list:
return zip(singles, singles[1:])