venmo/business-rules

Missing positional argument in wrapper()

lorddaren opened this issue · 7 comments

I am receiving this error when executing run_all:

Traceback (most recent call last):
  File "ksl.py", line 225, in <module>
    stop_on_first_trigger=False)
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 10, in run_all
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 19, in run
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 31, in check_conditions_recursively
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 46, in check_conditions_recursively
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 54, in check_condition
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 68, in _get_variable_value
TypeError: wrapper() missing 1 required positional argument: 'func'

Variables:

class CarVariables(BaseVariables):
    """ Variables to look for in validating a car """

    def __init__(self, car):
        self.avg_miles_per_year = 20000
        self.car = car

    @numeric_rule_variable
    def mileage(self):
        return self.car.miles

    @numeric_rule_variable
    def price(self):
        return self.car.price

    @numeric_rule_variable
    def year(self):
        return self.car.year

    @string_rule_variable
    def make(self):
        return self.car.make

    @string_rule_variable
    def model(self):
        return self.car.model

    @numeric_rule_variable(label='Mileage over standard yearly miles average.')
    def miles_over_average(self):
        year_diff = datetime.now().year - self.car.year
        avg_miles = self.avg_miles_per_year * year_diff
        avg_diff = self.car.miles - avg_miles
        return avg_diff

Actions:

class CarActions(BaseActions):
    """ Actions that can be taken when rules are met """

    def __init__(self, car):
        logging.getLogger()
        self.car = car

    @rule_action(params={})
    def ready_for_review(self):
        logging.debug('Marking car for review')
        self.car.review = True
        self.car.post = False

    @rule_action(params={})
    def not_for_review(self):
        self.car.review = False
        self.car.post = False

    @rule_action(params={})
    def ready_for_post(self):
        self.car.post = True

Have you tried using @string_rule_variable() instead of @string_rule_variable? And same for the rest of the variable decorators.

New error:

Traceback (most recent call last):
  File "ksl.py", line 225, in <module>
    stop_on_first_trigger=False)
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 10, in run_all
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 19, in run
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 31, in check_conditions_recursively
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 46, in check_conditions_recursively
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 54, in check_condition
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/engine.py", line 68, in _get_variable_value
  File "/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/business_rules-0.1.3-py3.4.egg/business_rules/variables.py", line 67, in memf
TypeError: mileage() missing 1 required positional argument: 'self'

Hmm. This is after fixing @numeric_rule_variable to @numeric_rule_variable()?

Correct.

Could you post an example of how you're actually calling engine.run?

Here ya go:

    rules = """[{"actions":
                    [
                        {"fields": [],
                         "name": "ready_for_review"
                         }
                    ],
                 "conditions":
                    {"all":
                        [
                            {"operator": "less_than",
                             "value": "100000",
                              "name": "mileage"
                            },
                            {"operator": "greater_than",
                             "value": "2000",
                              "name": "year"
                             }
                        ]
                    }
                }
            ]
            """

    for car in k.postings:
        logging.debug('Running business rules on %s' % car )
        run_all(json.loads(rules),
                CarVariables,
                CarActions,
                stop_on_first_trigger=False)

After posting the last output I realized my mistake. I was failing to actually pass the "car" object to the variables and actions.