Open main menu

Changes

Relax source design

386 bytes added, 17:10, 6 November 2015
Added the Category:Infobox templates category.
{{lowercase title}}
 
{{stub}}
 
The following is a set of notes which will be used to design the layout of functions, modules, and packages in relax.
== The check_*() functions ==
These functions are for performing checks for certain data being present. They can return True or False, throw RelaxWarnings, or raise RelaxErrorsThe idea uses the [https://en.wikipedia.org/wiki/Strategy_pattern strategy design pattern] which is implemented in the lib.checks.Check class. Therefore these are really function-like objects.
=== Packages ===
=== Design ===
The check_*() functions should have , via the Check object __call__() method, accept the 'escalate' keyword argument which can have the following values:
* 0 - This will simply cause the function to return True or False.
* 1 - In addition to returning True or False, the function will throw a RelaxWarning for better user feedback.
* 2 - This will cause a RelaxError to be raised if the data is missing, not set up, etc. Otherwise the function returns True.
 
The default value is 2.
=== Implementation ===
Here is a prototype functionfor implementing the check object:
<source lang="python">
 
# Python module imports.
from warnings import warn
# relax module imports.
from lib.checks import Check
from lib.errors import RelaxError
from lib.warnings import RelaxWarning
def check_xxxcheck_aaa_func(escalatea, b=0None): """Check if xxxaaa.
@keyword escalateparam a: The feedback to give if the Some check fails. This can be 0 for no printouts, 1 to throw a RelaxWarning, or 2 to raise a RelaxErrorspecific argument. @type escalatea: int str @raises RelaxErrorkeyword b: If escalate is set to 2 and the Some check failsspecific keyword argument. @type b: int @return: True if the check passes, False otherwise The initialised RelaxError object or nothing. @rtype: bool None or RelaxError instance
"""
# InitCheck that... flag if not something(a, b): return RelaxError("Some text") # Create the checking object.check_aaa = TrueCheck(check_aaa_func) msg </source> In the module where the check is performed, the code would be: <source lang= ''"python">
# Check that..relax module imports. if not somethingfrom aaa import check_aaa  def bbb(): flag = False msg = """Something is missingSome function."""
# Warnings and errorsChecks. if not flag and escalate a == 1: warn(RelaxWarning(msg))'600 MHz' elif not flag and escalate b == 2:600 raise RelaxError check_aaa(msga, b=b)
# Return the answer.
return flag
</source>
 
Note that the lib.checks.Check.__call__() method will take the escalate argument for itself and pass 'a' and 'b' into the 'check_aaa_func' function as arguments. The escalate argument defaults to 2.
 
== See also ==
 
[[Category:Development]]
Trusted, Bureaucrats
4,223

edits