### Navigation

- index
- modules |
- CCHR 1.0 documentation »

# `Business Rules`

class `src.rules.``Rule`(initial, result)

  Default Crosswalk Rule defining a crosswalk conversion.

  When a crosswalk is loaded each line in the crosswalk file is turned into a rule. For this default rule the initial value is the key in the crosswalk and the result is the value in the crosswalk. See crosswalk for more details.

  Public Methods: handle – Returns true if initial matches the value convert – Returns the result

  `convert`(**kw)

class `src.rules.``AllRule`(result)

  Crosswalk rule to handle all values. If result is :copy: will copy the initial value to the result value, otherwise will output the result value.

  example) :all: :copy: This will output exactly what is in the original dataset without doing any conversion.

  All: |
  none: |

  This will output a None value.

  All: | 1111.11

  This will output all values to 1111.11

  `convert`(**kw)

class `src.rules.``RegexRule`(regex, result, type, priority, escape=True, **kw)
  `convert`(**kw)

    Return result.

  `delete_transform`(value)

    Transform the value by removing the regex pattern from value. If a named group called keep is present than remove other but the pattern in the keep group.

  `handle`(value)

    Returns True if rule can handle the value. Convert will only be called if handle returns True.

class `src.rules.``Crosswalk`(filename, db_info, segment, log, **kw)

  A Crosswalk object is used to do variable conversions either directly or by accessing the object in the func.py file for a CCHR variable.

  The Crosswalk object first loads the crosswalk.txt file specified by the filename parameter. Each line of this file is turned into a rule object, see _rules for more information. When crosswalk.convert(value) is called we iterate over the rules list checking to see if a rule can handle the value. If a rule can handle the value the rules result is returned, otherwise we raise a ConvertError. An additional lookup parameter can be defined to provide a list of valid return values.

  The column delimiter is a tab and the row delimiter is a newline. So each line in the crosswalk looks like:

  keyt**value**n

  example)

  100 murder

  parameters:

  filename - Path to file containing crosswalk information. lookup - A list of valid values that can be returned by the convert function.

  `convert`(key, **kw)

    Returns a converted value based on key or raises a ConvertError if key can not be converted.

    Looks to see if there is a rule that can handle a given key. Split keyword returns the converted result as a list of values, using the value of the split keyword as the delimiter for the result list.

  `delete_transform`(key, count=0, **kw)

    Transform the given key by applying the rules in the crosswalk. If a rule can handle the key it will remove the matched text from the key. After the rules are applied the transformed key will be returned.

    The optional argument of count is the maximum number of times a transform will be applied to the key. By default (count=0) all rules will be checked to see if they can handle the key. If count is specified than the crosswalk will incrememnt a counter for each time a rule handles the key. Once the counter = count the crosswalk will return the transformed key.

  `load`(filename, db_info, segment, no_space=False)
  `validate`()

    Validate the ruleset.

class `src.rules.``RegexMathRule`(regex, result, type, priority, escape=True, **kw)
  `convert`(**kw)

    For math regex apply groups extracted from value to arthimetic function within self.result Math: >>> r = RegexMathRule(‘Sent to #Day #Y’, ‘#1 + #2(365.25)’, ‘math’) >>> r.handle(‘Sent to 10Day 2Y’) True >>> r.convert() 740.5

  `handle`(value)

`src.``rules`

  alias of `src.rules`

### Navigation

- index
- modules |
- CCHR 1.0 documentation »