0

I have written a generic framework in python for some particular type of task. It is a webserver which serves different requests and operations. This framework can be used by many projects and each one has a different set of validation rules. Right now, I'm just updating my script for each project.

I'm thinking of externalizing this validation part, how do I go about this? The validations are more than mere field content validations; I'm thinking of having a config file which maps incoming request <-> validationModule something like /site1/a/b.xml=validateSite1.py and importing this module in an if condition if the request is for site1. So I'll have generic framework scripts + individual scripts for each site.

Is there a cleaner way to do this?

1 Answer 1

1

I think it'd be better to use Python itself as the top-level mapping from URL paths to validation modules. A configuration might look like this:

import site1 import site2 def dispatch(uri): if uri.startswith('/site1/'): return site1.validate(uri) elif uri.startswith('/site2/): return site2.validate(uri) 

This simple example might tempt you to "abstract" it out into a more "generic framework" that turns strings into filenames to use as validation scripts. Here are some advantages of the doing the above instead:

  1. Performance: site modules are imported just once, we don't look up filenames per request.
  2. Flexibility: if you decide later that the dispatching logic is more complicated, you can easily use arbitrary Python code to deal with it. There will never be a need to extend your mapping system itself--only the config files that require more complexity.
  3. Single language.
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this method has a lot of flexibility; only hitch I feel is that the number of import modules will keep on increasing, the imports need to be tracked, remove obsolete ones in the future etc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.