VDBWRAIR / bactpipeline

Bacteria Pipeline modified from MRSN Bacteria Pipeline
GNU General Public License v2.0
0 stars 0 forks source link

Make Contract messages more user-friendly #23

Open averagehat opened 8 years ago

averagehat commented 8 years ago

e.g.

contracts.interface.ContractNotRespected: Breach for argument 'outdir' to run_sample_sheet().
Shouldn't have satisfied the clause exists.
checking: !exists for value: Instance of <type 'str'>: './'
checking: str,!exists for value: Instance of <type 'str'>: './'
Variables bound in inner context:

Could become


contracts.interface.ContractNotRespected: Breach for argument 'outdir' to run_sample_sheet().
=====================
ERROR:  blah blah blah
======================
checking: !exists for value: Instance of <type 'str'>: './'
checking: str,!exists for value: Instance of <type 'str'>: './'
Variables bound in inner context:

Do this by raising a ValueError rather than returning false. Example:

def fails(x):
    raise ValueError("="*9 + "\nERROR: blah blah blah, do xyz\n" + "="*9)
contracts.new_contract("failit", fails)
contracts.decorate(lambda x: x, x="failit")(1)

I would just wrap our current boolean-returning functions

def wrap_validation(func, message):
   def ret_func(arg):
      if not func(arg): 
         raise ValueError(message)
   return ret_func

contracts.new_contract("is_truthy", wrap_validation(lambda x: bool(x), "Not truthy, use truthy input"))
necrolyte2 commented 8 years ago

It needs to be very explicit in what is wrong, that is, it should say exactly what the issue is

Like for the !exists issue, it sould say something like

Error: Directory X already exists
averagehat commented 8 years ago

This is better as a PR against the contracts library.