Design pattern in Python with a lot of customer specific exceptions
<p>At this moment we are working on a large project. </p>

<p>This project is supposed to create EDIFACT messages. This is not so hard at first but the catch is there are a lot of customers that have their own implementation of the standard. </p>

<p>On top of that we are working with several EDIFACT standards (D96A and D01B in our case.)</p>

<p>Some customer exceptions might be as small as having a divergent field length, but some have made their own implementation completely different.</p>

<p>At this moment we have listed the customer exceptions in a list (Just to keep them consistent) and in the code we use something like:</p>

<pre><code>if NAME_LENGTH_IS_100 in customer_exceptions:
    this.max_length = 100
else:
    this.max_length = 70
</code></pre>

<p>For a couple of simple exceptions this works just fine, but at this moment the code starts to get really cluttered and we are thinking about refactoring the code. </p>

<p>I am thinking about some kind of factory pattern, but I am not sure about the implementation. 
Another option would be to create a base package and make a separate implementation for every customer that is diverging from the standard.</p>

<p>I hope someone can help me out with some advice. </p>

<p>Thanks in advance. </p>

<p>I think your question is too broad to be answered properly (I was up to click the close button because of this but decided otherwise).  The reason for this is the following:</p>

<p>There is nothing wrong the code snippet you provided.  It should be part of some kind of initialization routine, then this is just fine the way it is.  It also doesn't hurt to have things like this in a large amount.</p>

<p>But how to handle more complex cases depends greatly on the cases themselves.</p>

<ul>
<li>For lots of situations it might be sufficient to have such variables which represent the customer's special choices.</li>
<li>For other aspects I'd propose to have a <code>Customer</code> base class with subclasses thereof, for each customer one (or maybe the customers can even be hierarchically grouped, then a nice inheritance tree could reflect this).</li>
<li>For other cases again I'd propose aspect-oriented programming by use of Python decorators to tweak the behavior of methods, functions, and classes.</li>
</ul>

<p>Since this depends greatly on your concrete usecases, I think this question cannot be answered more concretely than this.</p>

python