Package: python-contracts
Contracts for Python FunctionsContracts can be used to validate the input or output of a function. Data flow among components can be hard to keep track or maintained, sometimes forcing us to write print statements everywhere trying to catch malformed data. This contracts library solves this by intercepting data that is piped into or out of a function and checking if they satisfy the specified requirements.Getting StartedInstall contracts through pip.$pip3installpython-contractsOnce contracts is installed, you can import the input and output contract decorators, and other convenient qualifier functions.# Imports ic (input contract) and oc (output contract)fromcontractsimportic,oc# Examples of some qualifiersfromcontractsimportnatural,integerRefer to  Qualifiers for list of available qualifiers.ContractsInput contract is a function decorater that takes in kwargs as a mapping of parameter names to their qualifier(s).Output contract is another decorator that takes in a single qualifier and checks the result of calling the decorated function.Using Input Contractfromcontractsimportic,natural@ic(val=natural)deffunc(val):returnvalfunctakes in "val" as a parameter.ic's arguments should correspond to the function's parameter names. In this case, the contract states that arguments for "val" should be natural numbers.iccan take in as many arguments as the number of parameters for the decorated function. All keys should map to the correct parameter names. If the contract is violated, you will get an InputContractException error message listing out the arguments that failed and their given values.contracts.exceptions.InputContractException:
Arguments that failed the contracts
val: -1Using Output Contractfromcontractsimportoc,natural@oc(natural)deffunc(val):returnvalocvalidates the result of calling a function. In this case, it checks that the result is a natural number. The decorator takes a single qualifier. If the contract is violated, the following error will be given.contracts.exceptions.OutputContractException: The result of -1 broke the output contractQualifiersQualifiers are functions that take in a single value and returnTrueif conditions are satisfied, andFalseotherwise.For convenience, the contracts library provides a range of basic qualifiers on some data types.Numbers: number, positive_number, negative_numberIntegers: integer, natural, positive_integer, negative_integerFloats: floating_point, positive_float, negative_floatComposing Multiple QualifiersTo compose multiple qualifiers, you can import thecomposefunction and pass as many qualifiers as needed to make a single qualifier.fromcontractsimportnatural,positive_integer,compose@ic(val=compose(natural,positive_integer))deffunc(val):returnvalThe input contract above will check that the argument for "val" is a natural number and a positive integer.Custom QualifiersUsers of the library can plug in their own qualifiers as long as they adhere to the right data signature. If you're using this library throughout your code base, writing your collection of qualifiers in a separate file will keep things clean.Built WithContracts library is built with Python 3.6. Code has been tested only in this version. Compatibility with other versions has not been tested.Libraries used:inspectfunctoolspytestAuthorGino Jacob -GithubFeedback is very much welcomed. Please write issues.LicenseThis project is licensed under the MIT License - see theLICENSE.mdfile for details