Check ducktyping
Project description
********
Ducktype
********
.. image:: https://pypip.in/v/ducktype/badge.png
:target: https://pypi.python.org/pypi/ducktype
Provides *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 arguments
required arguments means all arguments without defaults
Object/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 ducktype
or from sources::
git clone git@github.com:apieum/ducktype.git
cd ducktype
python setup.py install
=====
Usage
=====
--------------------------------
Example 1 - comparing functions:
--------------------------------
.. code-block:: python
from ducktype import isducktype
func_a = lambda a1, a2, a3=None: None
func_b = lambda *b1: None
func_c = lambda **c2: None
func_d = lambda d1, d2: None
func_e = lambda e1: None
func_f = lambda f1, f2, f3, f4: None
assert isducktype(func_b, func_a)
assert isducktype(func_c, func_a)
assert isducktype(func_d, func_a)
assert isducktype(func_a, func_e) is False
assert isducktype(func_e, func_a) is False
assert 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:: python
from ducktype import isducktype
class A(object):
_protected = 'hidden'
__private = 'hidden'
attr1 = None
def method1(self, arg, kwargs=True):
return kwargs
def method2(self, arg):
return arg
class B(object):
attr1 = None
def method1(self, **kwargs):
return kwargs
def method2(self, arg1, arg2=None):
return None
class C(object):
attr1 = False
def method1(self, arg, kwarg):
return arg
class D(object):
attr1 = False
method1 = None
method2 = None
class E(object):
def method1(self, **kwargs):
return kwargs
def method2(self, arg1, arg2=None):
return None
# it doesn't care if it's an instance or a class
assert 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 C
assert isducktype(A, C)
# Reverse is not True
assert isducktype(C, A) is False
# Whereas D as same members as A, two are not functions
assert isducktype(A, D) is False
assert isducktype(D, A) is False
# E need attribute "attr1" to ducktype A
assert isducktype(A, E)
assert isducktype(E, A) is False
--------------------------------
Example 3 - overriding default:
--------------------------------
.. code-block:: python
from ducktype import isducktype
class A(object):
attr1 = None
class B(object):
attr1 = None
attr2 = None
class C(B):
@classmethod
def __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 B
assert isducktype(A, B) is False
# Returns A.__ducktypecheck__(C)
assert isducktype(A, C)
assert isducktype(A, D) is False
assert isducktype(A, D())
===========
Development
===========
Any feedback or help is welcome.
You can contact me by mail: apieum [at] gmail [dot] com
Launch test::
git clone git@github.com:apieum/ducktype.git
cd ducktype
nosetests --with-spec --spec-color ./
.. image:: https://secure.travis-ci.org/apieum/ducktype.png?branch=master
:target: https://travis-ci.org/apieum/ducktype
Ducktype
********
.. image:: https://pypip.in/v/ducktype/badge.png
:target: https://pypi.python.org/pypi/ducktype
Provides *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 arguments
required arguments means all arguments without defaults
Object/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 ducktype
or from sources::
git clone git@github.com:apieum/ducktype.git
cd ducktype
python setup.py install
=====
Usage
=====
--------------------------------
Example 1 - comparing functions:
--------------------------------
.. code-block:: python
from ducktype import isducktype
func_a = lambda a1, a2, a3=None: None
func_b = lambda *b1: None
func_c = lambda **c2: None
func_d = lambda d1, d2: None
func_e = lambda e1: None
func_f = lambda f1, f2, f3, f4: None
assert isducktype(func_b, func_a)
assert isducktype(func_c, func_a)
assert isducktype(func_d, func_a)
assert isducktype(func_a, func_e) is False
assert isducktype(func_e, func_a) is False
assert 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:: python
from ducktype import isducktype
class A(object):
_protected = 'hidden'
__private = 'hidden'
attr1 = None
def method1(self, arg, kwargs=True):
return kwargs
def method2(self, arg):
return arg
class B(object):
attr1 = None
def method1(self, **kwargs):
return kwargs
def method2(self, arg1, arg2=None):
return None
class C(object):
attr1 = False
def method1(self, arg, kwarg):
return arg
class D(object):
attr1 = False
method1 = None
method2 = None
class E(object):
def method1(self, **kwargs):
return kwargs
def method2(self, arg1, arg2=None):
return None
# it doesn't care if it's an instance or a class
assert 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 C
assert isducktype(A, C)
# Reverse is not True
assert isducktype(C, A) is False
# Whereas D as same members as A, two are not functions
assert isducktype(A, D) is False
assert isducktype(D, A) is False
# E need attribute "attr1" to ducktype A
assert isducktype(A, E)
assert isducktype(E, A) is False
--------------------------------
Example 3 - overriding default:
--------------------------------
.. code-block:: python
from ducktype import isducktype
class A(object):
attr1 = None
class B(object):
attr1 = None
attr2 = None
class C(B):
@classmethod
def __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 B
assert isducktype(A, B) is False
# Returns A.__ducktypecheck__(C)
assert isducktype(A, C)
assert isducktype(A, D) is False
assert isducktype(A, D())
===========
Development
===========
Any feedback or help is welcome.
You can contact me by mail: apieum [at] gmail [dot] com
Launch test::
git clone git@github.com:apieum/ducktype.git
cd ducktype
nosetests --with-spec --spec-color ./
.. image:: https://secure.travis-ci.org/apieum/ducktype.png?branch=master
:target: https://travis-ci.org/apieum/ducktype
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ducktype-0.2.tar.gz
(7.6 kB
view details)
Built Distributions
ducktype-0.2-py3.3.egg
(11.6 kB
view details)
ducktype-0.2-py2.7.egg
(11.0 kB
view details)
File details
Details for the file ducktype-0.2.tar.gz
.
File metadata
- Download URL: ducktype-0.2.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d33f3401391acda266afd39b36a768638972a61ad10c0fe54473bf0c9859b2d8 |
|
MD5 | 7586b525ebfe6692221af2006e2d0462 |
|
BLAKE2b-256 | 896f7fcde267e1f3e94c5e8575698f39513ec76486656337d01e83870988830e |
File details
Details for the file ducktype-0.2-py3.3.egg
.
File metadata
- Download URL: ducktype-0.2-py3.3.egg
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58cbb59e7ec66631ac01e46d94749b86a355373541e004ffdb600107435c871d |
|
MD5 | 4bf652ca81e7869ada643822452191c3 |
|
BLAKE2b-256 | 8da59ba8998dcca628eff221777b8bf03dc7bb21e31d737985107d2d99348ccb |
File details
Details for the file ducktype-0.2-py2.7.egg
.
File metadata
- Download URL: ducktype-0.2-py2.7.egg
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62e0c0c6748705bfd9169b42042817d0838f2a91256e76ebfc493a9d3511e095 |
|
MD5 | 40fd7ea73d7175aeced0f6e1c6944b22 |
|
BLAKE2b-256 | 95b7774777e4866cbc8cb83927f7d946fa0ee9d4bc60487760f9acd0e7307115 |