This file contains 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
""" | |
Enable correctly annotated properties with @dataclass | |
>>> @dataclasses.dataclass | |
... class Person: | |
... first: str | |
... last: str | |
... | |
... @property_field | |
... def name(self) -> str: |
This file contains 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 sys | |
import inspect | |
def get_outer_frame(): | |
current = inspect.currentframe() | |
outers = inspect.getouterframes(current, 0) | |
return outers[2].frame | |
This file contains 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 json | |
import slicer | |
from slicer.ScriptedLoadableModule import ScriptedLoadableModuleLogic | |
_NODE = "__parameter_node__" | |
def parameterProperty(name): | |
"""Create a property backed by a parameter in a parameter node. |
This file contains 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 inspect | |
def curry(func): | |
sig = inspect.signature(func) | |
num = len(sig.parameters) | |
def make(frozen_args): | |
def wrap(e): | |
args = frozen_args + (e,) | |
This file contains 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 timedec(*args, func=None, repeat=5, number=1000000): | |
import timeit | |
from functools import partial | |
import statistics | |
def dec(f): | |
p = partial(f, *args) | |
pname = f"{f.__name__}({', '.join(map(str, args))})" | |
result = timeit.Timer(p).repeat(repeat, number) |
This file contains 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 CM: | |
mode = 0 | |
def __init__(self, m): | |
self.m = m | |
def __enter__(self): | |
self.old = CM.mode | |
CM.mode = self.m |
This file contains 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 asyncio | |
class CM: | |
mode = 0 | |
def __init__(self, m): | |
self.m = m | |
def __enter__(self): | |
self.old = CM.mode |
This file contains 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
units = { | |
1: 'I', | |
5: 'V', | |
10: 'X', | |
50: 'L', | |
100: 'C', | |
500: 'D', | |
1000: 'M' | |
} |
This file contains 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
# Example values convert '8DA5' from hexidecimal to decimal. | |
# Get valid digits in our base. | |
# input '0123456789ABCDEF' | |
digits = raw_input('Enter valid digits for this number system.\nRepeats are allowed, but will cause unexpected results.\n> ') | |
# Get the number we want to convert and reverse it (so the 1's place is on the left) | |
# input '8DA5' (becomes '5AD8') | |
number = raw_input('Enter a number in base %s\n> '%len(digits))[::-1] |