How to use the multimethod.multidispatch function in multimethod

To help you get started, we’ve selected a few multimethod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coady / multimethod / tests / test_dispatch.py View on Github external
assert roshambo(r, p) == 'paper covers rock'
    assert roshambo(p, r) == 'paper covers rock'
    assert roshambo(r, s) == 'rock smashes scissors'
    assert roshambo(p, s) == 'scissors cut paper'
    assert roshambo(r, r) == 'tie'
    assert len(roshambo) == 8
    del roshambo[()]
    del roshambo[rock, paper]
    assert len(roshambo) == 5
    with pytest.raises(DispatchError, match="0 methods"):
        roshambo(r, r)


# methods
class cls(object):
    method = multidispatch(lambda self, other: None)

    @method.register(Iterable, object)
    def _(self, other):
        return 'left'

    @method.register(object, Iterable)
    def _(self, other):
        return 'right'


def test_cls():
    obj = cls()
    assert obj.method(None) is cls.method(None, None) is None
    assert obj.method('') == 'right'
    assert cls.method('', None) == 'left'
    with pytest.raises(DispatchError, match="2 methods"):
github coady / multimethod / tests / test_all.py View on Github external
    @multidispatch
    def func(arg):
        return type(arg)
github coady / multimethod / tests / test_dispatch.py View on Github external
@multidispatch
def roshambo(left, right):
    return 'tie'
github openworm / pygeppetto / pygeppetto / model / utils / pointer_utility.py View on Github external
@multidispatch
def get_type(model, path):
    st = iter(path.split('.'))
    lastType = None
    lastVar = None
    library = None
    for token in st:

        #  token can be a library, a type or a variable
        if lastType is not None:
            if isinstance(lastType, CompositeType):
                lastVar = find_variable(get_variable(token), lastType)
            elif isinstance(lastType, ArrayType) and isinstance(lastType.arrayType, CompositeType):
                lastVar = find_variable(get_variable(token), (lastType).arrayType)
            else:
                raise GeppettoModelException(
                    "{} is not of type CompositeType there can't be nested variables".format(lastType.id))
github openworm / pygeppetto / pygeppetto / model / utils / pointer_utility.py View on Github external
@multidispatch
def equals(pointer: Pointer, pointer2: Pointer):
    if not pointer == pointer2:
        if len(pointer.elements) != len(pointer2.elements):
            return False
        for i, pe in enumerate(pointer.elements):
            try:
                pe2 = pointer2.elements[i]
            except:
                return False

            if not pe2 or not equals(pe, pe2):
                return False
    return True
github openworm / pygeppetto / pygeppetto / model / utils / pointer_utility.py View on Github external
@multidispatch
def find_type(type_, variable: Variable) -> Type:
    if type_ is None:
        types = []
        types += variable.anonymousTypes
        types += variable.types
        if len(types) == 1:
            return types[0]
        elif len(types) == 0:
            raise GeppettoModelException("The variable {} has not types".format(variable.id))
        else:
            raise GeppettoModelException(
                "The instance path does not specify a type but more than one type are present for the variable {}".format(
                    variable.id))
    else:
        for t in variable.types:
            if t.id == type_:
github openworm / pygeppetto / pygeppetto / model / utils / pointer_utility.py View on Github external
@multidispatch
def get_variable(pointer: Pointer):
    return pointer.elements[-1].variable
github openworm / pygeppetto / pygeppetto / model / utils / pointer_utility.py View on Github external
@multidispatch
def get_pointer(variable: Variable, type_, index):
    pointerElement = PointerElement()
    pointerElement.index = index
    pointerElement.variable = variable
    pointerElement.type = type_
    pointer = Pointer(elements=(pointerElement,))

    return pointer
github mara / mara-db / mara_db / shell.py View on Github external
@multidispatch
def copy_command(source_db: object, target_db: object, target_table: str,
                 timezone = None, csv_format = None, delimiter_char = None) -> str:
    """
    Creates a shell command that
    - receives a sql query from stdin
    - executes the query in `source_db`
    - writes the results of the query to `target_table` in `target_db`

    Args:
        source_db: The database in which to run the query (either an alias or a `dbs.DB` object
        target_db: The database where to write the query results (alias or db configuration)
        target_table: The table in which to write the query results
        timezone: Sets the timezone of the client, if applicable
        csv_format: double quote 'difficult' strings
        delimiter_char: The character that separates columns, default '\t'