Package: ducktype
********Ducktype********.. image:: https://pypip.in/v/ducktype/badge.png:target: https://pypi.python.org/pypi/ducktypeProvides *isducktype*, a simple function to check ducktype.Routine* 'A' is ducktype of 'B' when:- 'A' number of arguments is compatible with 'B' (names not checked)- OR A or B have varargs (*args) or keywords (**kwargs)Arguments comparison between A and B is made within:- the number of required arguments to call A is between minimum and maximum number of B arguments- OR the number maximum of arguments to call A is between minimum and maximum number of B argumentsrequired arguments means all arguments without defaultsObject/Class 'A' is ducktype of 'B' when:- it has at least same public members names (public if they don't start by '_')- AND its methods are ducktypes of corresponding B methods.If B has a method __ducktypecheck__, *isducktype* returns B.__ducktypecheck__(A)Like *isinstance* or *issubclass*, *isducktype* second argument can be either an objet or a tuple of objects.Routine* in the sense: a user defined or builtins, function, method, or lambda.---------------------------------------------------------------------**Table of Contents**.. contents:::local::depth: 1:backlinks: none=============Installation=============Install it from pypi::pip install ducktypeor from sources::git clone git@github.com:apieum/ducktype.gitcd ducktypepython setup.py install=====Usage=====--------------------------------Example 1 - comparing functions:--------------------------------.. code-block:: pythonfrom ducktype import isducktypefunc_a = lambda a1, a2, a3=None: Nonefunc_b = lambda *b1: Nonefunc_c = lambda **c2: Nonefunc_d = lambda d1, d2: Nonefunc_e = lambda e1: Nonefunc_f = lambda f1, f2, f3, f4: Noneassert isducktype(func_b, func_a)assert isducktype(func_c, func_a)assert isducktype(func_d, func_a)assert isducktype(func_a, func_e) is Falseassert isducktype(func_e, func_a) is Falseassert isducktype(func_f, func_a) is False# with a tuple (== Or)assert isducktype(func_e, (func_a, func_b))assert isducktype(func_e, (func_a, func_d)) is False--------------------------------Example 2 - comparing objects:--------------------------------.. code-block:: pythonfrom ducktype import isducktypeclass A(object):_protected = 'hidden'__private  = 'hidden'attr1 = Nonedef method1(self, arg, kwargs=True):return kwargsdef method2(self, arg):return argclass B(object):attr1 = Nonedef method1(self, **kwargs):return kwargsdef method2(self, arg1, arg2=None):return Noneclass C(object):attr1 = Falsedef method1(self, arg, kwarg):return argclass D(object):attr1 = Falsemethod1 = Nonemethod2 = Noneclass E(object):def method1(self, **kwargs):return kwargsdef method2(self, arg1, arg2=None):return None# it doesn't care if it's an instance or a classassert isducktype(A, B)assert isducktype(A(), B)assert isducktype(A, B())assert isducktype(A(), B())# You can call each A method as if it was Cassert isducktype(A, C)# Reverse is not Trueassert isducktype(C, A) is False# Whereas D as same members as A, two are not functionsassert isducktype(A, D) is Falseassert isducktype(D, A) is False# E need attribute "attr1" to ducktype Aassert isducktype(A, E)assert isducktype(E, A) is False--------------------------------Example 3 - overriding default:--------------------------------.. code-block:: pythonfrom ducktype import isducktypeclass A(object):attr1 = Noneclass B(object):attr1 = Noneattr2 = Noneclass C(B):@classmethoddef __ducktypecheck__(cls, maybe_duck):return hasattr(maybe_duck, 'attr1')class D(B):def __ducktypecheck__(self, maybe_duck):return hasattr(maybe_duck, 'attr1')# A must not ducktype Bassert isducktype(A, B) is False# Returns A.__ducktypecheck__(C)assert isducktype(A, C)assert isducktype(A, D) is Falseassert isducktype(A, D())===========Development===========Any feedback or help is welcome.You can contact me by mail: apieum [at] gmail [dot] comLaunch test::git clone git@github.com:apieum/ducktype.gitcd ducktypenosetests --with-spec --spec-color ./.. image:: https://secure.travis-ci.org/apieum/ducktype.png?branch=master:target: https://travis-ci.org/apieum/ducktype