title: Python Rule Based Engine
body: <p>I am looking to design a system that will essentially need to make decisions based on input. The input will be a person. </p>
  
  <pre><code>class Person:
      def __init__(self, name, age, sex, weight, height, nationality):
          self.name = name
          self.age = age
          self.sex = sex
          self.weight = weight
          self.height = height
          self.nationality = nationality
  </code></pre>
  
  <p>We want to assign each person to a school class based on certain rules. </p>
  
  <p>For example:</p>
  
  <p>Women from the UK between 22-25 should go to class B. 
  Men over 75 should go to class A. 
  Women over 6ft should go to class C.</p>
  
  <p>We will have approximately 400 different rules and the first rule that is met should be applied - we need to maintain the order of the rules. </p>
  
  <p>I am thinking about how to store/represent the rules here. Obviously, you could just have a veeeery long <code>if, elif, elif</code> statement but this isn't efficient. Another option would be storing the rules in a database and maybe having an in memory table. </p>
  
  <p>I would like to be able to edit the rules without doing a release - possibly having a front end to allow non tech people to add, remove and reorder rules. </p>
  
  <p>Everything is on the table here - the only certain requirement is the actually programming language must be Python. </p>
  
  <p><strong>Added for further context</strong></p>
  
  <p>I suppose my question is how to store the rules. At the moment it is one huge long <code>if elif elif</code> statement so anytime there is a change to the business logic the PM does up the new rules and I then convert them to the if statement. </p>
  
  <p>All inputs to the system will be sent through the same list of rules and the first rule that matches will be applied. Multiple rules can apply to each input but it's always the first that is applied. </p>
  
  <p>e.g. </p>
  
  <blockquote>
    <p>Women over 25 go to Class B<br>
    Women go to Class A. </p>
  </blockquote>
  
  <p>Any women over 25 will be sent to class B even though the second rule also applies.</p>
  
  <p>Input will always contain the same format input - haven't decided where it will be an object or a dict but some of the values may be <code>None</code>. Some Persons may not have a weight associated with them. </p>