configuration - How to exclude DEFAULTs from Python ConfigParser .items()? -


I am using ConfigParser to load data into the configuration file:

Test.conf:

  [myfiles] fileone:% (DATADIR) s / somefile.foo filetwo:% (DATADIR) s / nudderfile.foo  

load.py:

  Import Configure Config = Config Piece. Config.read (configurable): config.items ('myfiles') print config.get (' myfiles', 'datadir')  ('' datadir ':' / tmp '}) config.read (' test .conf '  

Output:

  $ dragon load.py [('DATADIR', '/ tmp'), ('filetwo', '/ Tmp / nudderfile.foo '), (' fileone ',' /tmp/somefile.foo ')] / TMP  

I'm afraid that variable replacement (' Dattadir ',' '/ Tmp') are being shown as part of the default . item () and .get () , as if they were values ​​in the configuration file. Is this behavior expected? Any work around, so that I can just restart .items () , without getting the default dictionary values, but still using magic interpolation?

Reference:

Thank you!

Update: It has been indicated that this is expected behavior: the default in the configuration file is like a different name / value pair similarly, name / value pairs in the configuration file Also available for "magic interpolation", so if I define:

  foo: bar: zap:% (foo) snowl  

me < Code> [... ('zap': 'barnowl')] will get

It's very clean, but I'm still wondering if what I want to accomplish is Poo I can do the following: In my configuration files the name / value is reusable in pairs, with the launch of the variable, without defaults

My specific scenario is: I have the config at {atirir: '/ Foo / bar'} because the full path is different for some files. Then I have to pass that configured object around and it must be repeated through the files. I do not want every class to know that the configuration should be read to know that it was started with some omission and it should be ignored because they are not actual files, is it possible? There is a way to hide default from .item () and .get () but still have interpolation? Thanks! Normally, I've found the class very useful, but there is a lack of it.

However, it may be , subclassed and expanded, sometimes well, sometimes it does not do so well (= very implementation dependent)

Here is the solution to your problem, tested in python3:

  class ConfigParser (configparser.ConfigParser): "" "option () can get the" "default" def "option () Self, section, no_defaults = false, ** kwargs): if any_default: try: return list (excluding itself ._section [section]. Key ()) KeyError: raise NoS EctionError (section) Other: Returns Super (). Option (section, ** kwargs)  

This is one of the worst examples, because it is partly copying options () . It would be better if the configparser will provide the internal value of the base class RawConfigParser option _options (self, section) which will include the exception filter, and a option () After that, in the sub-closing we will be using _options () .

Can reuse for 2, I believe the only change is super () code> super (config parser, self) .

You can then use:

  Print Configuration. Use ('myfiles', no_defaults = true)  

and also to repeat that list.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -