decorates a method to detect overrides in subclasses method isdefault true return method def isdefaultmethod serializes a userdefined function args func the function to serialize returns a tuple code defaults closure deserializes a user defined function args code bytecode of the function defaults defaults of the function closure closure of the function globs dictionary of global objects returns a function object ensures that a value is converted to a python cell object args value any value that needs to be casted to the cell type returns a value wrapped as a cell object see function funcload normalizes a listtensor into a list if a tensor is passed we return a list of size 1 containing the tensor args x target object to be normalized returns a list removes sequences that exceed the maximum length args maxlen int maximum length of the output sequences seq list of lists where each sublist is a sequence label list where each element is an integer returns newseq newlabel shortened lists for seq and label backport of removeprefix from pep616 python 3 9 if lenprefix 0 and x startswithprefix return xlenprefix else return x def removesuffixx suffix remove a value from a list by id for i v in enumeratelst if idv idvalue del lsti return decorates a method to detect overrides in subclasses check if a method is decorated with the default wrapper serializes a user defined function args func the function to serialize returns a tuple code defaults closure deserializes a user defined function args code bytecode of the function defaults defaults of the function closure closure of the function globs dictionary of global objects returns a function object unpack previous dump ensures that a value is converted to a python cell object args value any value that needs to be casted to the cell type returns a value wrapped as a cell object see function func_load just access it so it gets captured in __closure__ normalizes a list tensor into a list if a tensor is passed we return a list of size 1 containing the tensor args x target object to be normalized returns a list removes sequences that exceed the maximum length args maxlen int maximum length of the output sequences seq list of lists where each sublist is a sequence label list where each element is an integer returns new_seq new_label shortened lists for seq and label backport of removeprefix from pep 616 python 3 9 backport of removesuffix from pep 616 python 3 9 remove a value from a list by id
import binascii
import codecs
import marshal
import os
import types as python_types


def default(method):
    
    method._is_default = True
    return method


def is_default(method):
    
    return getattr(method, "_is_default", False)


def func_dump(func):
    
    if os.name == "nt":
        raw_code = marshal.dumps(func.__code__).replace(b"\\", b"/")
        code = codecs.encode(raw_code, "base64").decode("ascii")
    else:
        raw_code = marshal.dumps(func.__code__)
        code = codecs.encode(raw_code, "base64").decode("ascii")
    defaults = func.__defaults__
    if func.__closure__:
        closure = tuple(c.cell_contents for c in func.__closure__)
    else:
        closure = None
    return code, defaults, closure


def func_load(code, defaults=None, closure=None, globs=None):
    
    if isinstance(code, (tuple, list)):  
        code, defaults, closure = code
        if isinstance(defaults, list):
            defaults = tuple(defaults)

    def ensure_value_to_cell(value):
        

        def dummy_fn():
            value  

        cell_value = dummy_fn.__closure__[0]
        if not isinstance(value, type(cell_value)):
            return cell_value
        return value

    if closure is not None:
        closure = tuple(ensure_value_to_cell(_) for _ in closure)
    try:
        raw_code = codecs.decode(code.encode("ascii"), "base64")
    except (UnicodeEncodeError, binascii.Error):
        raw_code = code.encode("raw_unicode_escape")
    code = marshal.loads(raw_code)
    if globs is None:
        globs = globals()
    return python_types.FunctionType(
        code, globs, name=code.co_name, argdefs=defaults, closure=closure
    )


def to_list(x):
    
    if isinstance(x, list):
        return x
    return [x]


def remove_long_seq(maxlen, seq, label):
    
    new_seq, new_label = [], []
    for x, y in zip(seq, label):
        if len(x) < maxlen:
            new_seq.append(x)
            new_label.append(y)
    return new_seq, new_label


def removeprefix(x, prefix):
    

    if len(prefix) > 0 and x.startswith(prefix):
        return x[len(prefix) :]
    else:
        return x


def removesuffix(x, suffix):
    

    if len(suffix) > 0 and x.endswith(suffix):
        return x[: -len(suffix)]
    else:
        return x


def remove_by_id(lst, value):
    
    for i, v in enumerate(lst):
        if id(v) == id(value):
            del lst[i]
            return