0

I have an application that can be started using some user preferences. To do so, I use the ConfigParser class from the built-in package ConfigParser. I derived a subclass of ConfigParser that implements the __getitem__ and __setitem__ methods in order to get/set some preferences items without having to specify each time their respective section, subsections and option.

As some of those preferences (working directory, logging level ...) are quite sensible for my code, I would like to check them every time one of their items is modified. To do so, I currently think about two approaches:

  1. writing a method that check every item of my preferences each time one of them is modified. Quite simple but not very efficient as every time one item is changed the whole preferences are checked.

  2. writing a check_item method for each preferences item (check_logger, check_working_directory, check_blabla ...) and each time an item is set the corresponding checker will be called using getattr(self,'check_%s' % item)(item)

I would tend to think that the second approach is better than the first one.

Would you see some alternatives to those designs ?

thank you very much

Eric

Eurydice
  • 8,001
  • 4
  • 24
  • 37
  • "I would like to check them every time one of their items is modified" What did you understand by "one item is modified"? Modified externally in (one of) the configuration file(s)? Or modified from within the Python program? – Sylvain Leroux Jun 22 '13 at 14:31
  • I mean that the user change one item either by editing the preferences file or through the GUI – Eurydice Jun 22 '13 at 16:25
  • For modifications done using your application GUI -- your program's logic should handle that easely. On the other hand, it seems rather difficult to track "in real time" the modifications of the config file. Or do you just want to track them at application startup? – Sylvain Leroux Jun 22 '13 at 17:00
  • The preferences will be first checked at start up. In that context, I decided to be quite strict regarding their contents. No correct preferences, no start. After start up, when the user opens the Preferences settings on the GUI, and press the OK button, the whole set of preferences will be checked and if one is wrong the user will have to make it correct as the dialog will be modal for that purpose. – Eurydice Jun 23 '13 at 07:55
  • Ah, ok! You will check for consistency/correctness. I assume you will have a set of rules at some point. If the configuration pass the rules, it is accepted. Rejected otherwise. I'm not sure you could really check item by item -- since I assume some items could be valid or not depending the value of other items. – Sylvain Leroux Jun 23 '13 at 08:23

1 Answers1

1

Depending on the complexity of your rules (and who wrote/will write them), you could either:

  • implement them as pure Python;
  • use a (business) rule language like BPEL. Python has support for this with the PyPEL project;
  • use constraint based system to check it the set of values does not violate directly or indirectly any rules. You could take a look at python-constraint for example. This is more a kind of "solver" -- but it will inform you if there is no solution given your set of "preferences".

For simple to moderately complex constraints the first choice would have my favors. Note however, like I explained it in comments, I don't think the "check one item at a time" approach to be valid -- except for simplest cases. That's because some item values might or might not be valid depending on the value of one or more other items. And it would be not that easy to track which rule(s) a change in a value might indirectly broke.

Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125