|
From: Thomas Sturges-A. <zi...@ho...> - 2011-10-26 22:16:32
|
Sorry to spam, but i'm guessing this can be answered easily.
Was really glad to discover the configobj module as I had previously been restricted by ConfigParser.
The only important feature I have yet to find is an equivalent of " configfile.has_section('Blah') " and " configfile.has_option ('Section1', 'Option1') " which are listed here: http://docs.python.org/library/configparser.html?highlight=config%20parser#configparser-objects . Do such things exist?
I guess I could use ConfigParser as well just for this feature but it would not support nested sections. I had a fiddle with exceptions in classes as a way of making a feature myself but I can't seem to get it to work for KeyError exceptions (i.e. they crash the interpreter anyway). The validation system is not practical in this instance as what will be checked and what it will be checked for is reasonably dynamic.
I am using Python 2.72 and configobj 7.72 on Windows XP with Notepad ++
Thanks!
Thomas Sturges-Allardhttp://zig13.termisoc.org |
|
From: Michael F. <fuz...@vo...> - 2011-10-28 00:22:24
|
Hello Thomas,
Your email isn't spam, it's entirely on topic!
ConfigObj doesn't have those methods specifically - *however* the
information is easy to find out.
The following tells you if an entry is in a ConfigObj instance (but not
whether it is a section or a value):
'foo' in configfile
For just checking for the presence of a section you can do:
'foo' in configfile.sections
For values you can do:
'foo' in configfile.scalars
That code works on individual sections as well as the main ConfigObj
instance.
You could also do:
result = configfile.get('foo')
if result is None:
# entry is not present
elif isinstance(result, dict):
# entry is present and a section
else:
# entry is present and a value
All the best,
Michael Foord
On 26/10/2011 23:03, Thomas Sturges-Allard wrote:
> Sorry to spam, but i'm guessing this can be answered easily.
>
> Was really glad to discover the configobj module as I had previously
> been restricted by ConfigParser.
>
> The only important feature I have yet to find is an equivalent of "
> configfile.has_section('Blah') " and " configfile.has_option
> ('Section1', 'Option1') " which are listed here:
> http://docs.python.org/library/configparser.html?highlight=config%20parser#configparser-objects
> . Do such things exist?
>
> I guess I could use ConfigParser as well just for this feature but it
> would not support nested sections. I had a fiddle with exceptions in
> classes as a way of making a feature myself but I can't seem to get it
> to work for KeyError exceptions (i.e. they crash the interpreter
> anyway). The validation system is not practical in this instance as
> what will be checked and what it will be checked for is reasonably
> dynamic.
>
> I am using Python 2.72 and configobj 7.72 on Windows XP with Notepad ++
>
> Thanks!
>
> Thomas Sturges-Allard
> http://zig13.termisoc.org
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities.
> http://p.sf.net/sfu/cisco-dev2dev
>
>
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
|
|
From: David H. <neg...@gm...> - 2011-10-28 14:09:20
|
Hey Michael,
Unless I'm mistaken, your suggestions don't completely address Thomas'
question.
configfile.sections and configfile.scalars don't expose nested entries. So
yes, he can use those, but they'll be misleading unless he's using
configuration data that he can guarantee isn't nested, in which case he's
not any better off than when he was using ConfigParser.
I *think* that he was hoping there was some super-convenient way to ask a
ConfigObj instance to be deeply introspective and test for a given
section-name/key-name anywhere in the cfg hierarchy.
Not terribly difficult to do with a smidge of recursion, but not natively
offered by the ConfigObj interface as far as I know.
cheers,
-hoss
David Hostetler
neg...@gm...
On Thu, Oct 27, 2011 at 19:51, Michael Foord <fuz...@vo...>wrote:
> Hello Thomas,
>
> Your email isn't spam, it's entirely on topic!
>
> ConfigObj doesn't have those methods specifically - *however* the
> information is easy to find out.
>
> The following tells you if an entry is in a ConfigObj instance (but not
> whether it is a section or a value):
>
> 'foo' in configfile
>
> For just checking for the presence of a section you can do:
>
> 'foo' in configfile.sections
>
> For values you can do:
>
> 'foo' in configfile.scalars
>
> That code works on individual sections as well as the main ConfigObj
> instance.
>
> You could also do:
>
> result = configfile.get('foo')
> if result is None:
> # entry is not present
> elif isinstance(result, dict):
> # entry is present and a section
> else:
> # entry is present and a value
>
> All the best,
>
> Michael Foord
>
>
> On 26/10/2011 23:03, Thomas Sturges-Allard wrote:
>
> Sorry to spam, but i'm guessing this can be answered easily.
>
> Was really glad to discover the configobj module as I had previously been
> restricted by ConfigParser.
>
> The only important feature I have yet to find is an equivalent of "
> configfile.has_section('Blah') " and " configfile.has_option ('Section1',
> 'Option1') " which are listed here:
> http://docs.python.org/library/configparser.html?highlight=config%20parser#configparser-objects. Do such things exist?
>
> I guess I could use ConfigParser as well just for this feature but it
> would not support nested sections. I had a fiddle with exceptions in classes
> as a way of making a feature myself but I can't seem to get it to work for
> KeyError exceptions (i.e. they crash the interpreter anyway). The validation
> system is not practical in this instance as what will be checked and what it
> will be checked for is reasonably dynamic.
>
> I am using Python 2.72 and configobj 7.72 on Windows XP with Notepad ++
>
> Thanks!
>
> Thomas Sturges-Allard
> http://zig13.termisoc.org
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities. http://p.sf.net/sfu/cisco-dev2dev
>
>
>
> _______________________________________________
> Configobj-develop mailing lis...@li...://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
>
> -- http://www.voidspace.org.uk/
>
> May you do good and not evil
> May you find forgiveness for yourself and forgive others
> May you share freely, never taking more than you give.
> -- the sqlite blessing http://www.sqlite.org/different.html
>
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities.
> http://p.sf.net/sfu/cisco-dev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
|
|
From: Michael F. <fuz...@vo...> - 2011-10-28 14:18:21
|
On 28/10/2011 15:08, David Hostetler wrote:
> Hey Michael,
>
> Unless I'm mistaken, your suggestions don't completely address Thomas'
> question.
>
> configfile.sections and configfile.scalars don't expose nested
> entries. So yes, he can use those, but they'll be misleading unless
> he's using configuration data that he can guarantee isn't nested, in
> which case he's not any better off than when he was using ConfigParser.
>
Well he is because you can't have nested sections *at all* with
ConfigParser (and there are lots of other reasons to prefer ConfigObj...).
> I *think* that he was hoping there was some super-convenient way to
> ask a ConfigObj instance to be deeply introspective and test for a
> given section-name/key-name anywhere in the cfg hierarchy.
>
> Not terribly difficult to do with a smidge of recursion, but not
> natively offered by the ConfigObj interface as far as I know.
The code I suggested was an exact equivalent of "has_section" and
"has_option" in ConfigParser - but you're right, this code only checks
the current section and doesn't recurse. You could write a recursive
version easily, but what would that be useful for? Knowing that
*somewhere* inside a nested data structure the section/option exists
isn't useful, because you still have to write the recursive code to find
it. By the time you've done that you've written the code to check as well...
(It may be useful for configfile of *known* structure - but in that case
you can just go to the section that should contain the option /
sub-section and use the code I suggested.)
That aside, recursively checking sections is easy, here's an example
that can be modified based on the exact use case required:
def contains_entry(section, entry):
if entry in section:
return True
for sub in section.sections:
if contains_entry(section[sub], entry):
return True
You call it with:
contains_entry(configfile, 'some_key')
All the best,
Michael Foord
>
>
> cheers,
>
> -hoss
>
> David Hostetler
> neg...@gm... <mailto:neg...@gm...>
>
>
>
> On Thu, Oct 27, 2011 at 19:51, Michael Foord
> <fuz...@vo... <mailto:fuz...@vo...>> wrote:
>
> Hello Thomas,
>
> Your email isn't spam, it's entirely on topic!
>
> ConfigObj doesn't have those methods specifically - *however* the
> information is easy to find out.
>
> The following tells you if an entry is in a ConfigObj instance
> (but not whether it is a section or a value):
>
> 'foo' in configfile
>
> For just checking for the presence of a section you can do:
>
> 'foo' in configfile.sections
>
> For values you can do:
>
> 'foo' in configfile.scalars
>
> That code works on individual sections as well as the main
> ConfigObj instance.
>
> You could also do:
>
> result = configfile.get('foo')
> if result is None:
> # entry is not present
> elif isinstance(result, dict):
> # entry is present and a section
> else:
> # entry is present and a value
>
> All the best,
>
> Michael Foord
>
>
> On 26/10/2011 23:03, Thomas Sturges-Allard wrote:
>> Sorry to spam, but i'm guessing this can be answered easily.
>>
>> Was really glad to discover the configobj module as I had
>> previously been restricted by ConfigParser.
>>
>> The only important feature I have yet to find is an equivalent of
>> " configfile.has_section('Blah') " and " configfile.has_option
>> ('Section1', 'Option1') " which are listed here:
>> http://docs.python.org/library/configparser.html?highlight=config%20parser#configparser-objects
>> . Do such things exist?
>>
>> I guess I could use ConfigParser as well just for this feature
>> but it would not support nested sections. I had a fiddle with
>> exceptions in classes as a way of making a feature myself but I
>> can't seem to get it to work for KeyError exceptions (i.e. they
>> crash the interpreter anyway). The validation system is not
>> practical in this instance as what will be checked and what it
>> will be checked for is reasonably dynamic.
>>
>> I am using Python 2.72 and configobj 7.72 on Windows XP with
>> Notepad ++
>>
>> Thanks!
>>
>> Thomas Sturges-Allard
>> http://zig13.termisoc.org
>>
>>
>> ------------------------------------------------------------------------------
>> The demand for IT networking professionals continues to grow, and the
>> demand for specialized networking skills is growing even more rapidly.
>> Take a complimentary Learning@Cisco Self-Assessment and learn
>> about Cisco certifications, training, and career opportunities.
>> http://p.sf.net/sfu/cisco-dev2dev
>>
>>
>> _______________________________________________
>> Configobj-develop mailing list
>> Con...@li... <mailto:Con...@li...>
>> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
> --
> http://www.voidspace.org.uk/
>
> May you do good and not evil
> May you find forgiveness for yourself and forgive others
> May you share freely, never taking more than you give.
> -- the sqlite blessinghttp://www.sqlite.org/different.html
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities.
> http://p.sf.net/sfu/cisco-dev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> <mailto:Con...@li...>
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities.
> http://p.sf.net/sfu/cisco-dev2dev
>
>
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
--
http://www.voidspace.org.uk/
May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html
|
|
From: David H. <neg...@gm...> - 2011-10-28 14:45:05
|
On Fri, Oct 28, 2011 at 10:18, Michael Foord <fuz...@vo...>wrote: > On 28/10/2011 15:08, David Hostetler wrote: > > Hey Michael, > > Unless I'm mistaken, your suggestions don't completely address Thomas' > question. > > configfile.sections and configfile.scalars don't expose nested entries. So > yes, he can use those, but they'll be misleading unless he's using > configuration data that he can guarantee isn't nested, in which case he's > not any better off than when he was using ConfigParser. > > > Well he is because you can't have nested sections *at all* with > ConfigParser (and there are lots of other reasons to prefer ConfigObj...). > > Of course - lots of really good reasons to prefer ConfigObj. :) I certainly didn't mean for that to sound disparaging. Just that if he migrated to ConfigObj because he wanted nested data, then artificially constraining himself to not be nested defeats the purpose. > > I *think* that he was hoping there was some super-convenient way to ask a > ConfigObj instance to be deeply introspective and test for a given > section-name/key-name anywhere in the cfg hierarchy. > > Not terribly difficult to do with a smidge of recursion, but not natively > offered by the ConfigObj interface as far as I know. > > > The code I suggested was an exact equivalent of "has_section" and > "has_option" in ConfigParser - but you're right, this code only checks the > current section and doesn't recurse. You could write a recursive version > easily, but what would that be useful for? Knowing that *somewhere* inside a > nested data structure the section/option exists isn't useful, because you > still have to write the recursive code to find it. By the time you've done > that you've written the code to check as well... > > All true. If I were to write something addressing this concept, I'd have it actually return the entity that it was testing for. I.e. if it returns None, the desired item doesn't exist, otherwise it does and here it is for your convenience. configob (and validate) rock, as always. :) regards, -hoss David Hostetler neg...@gm... |
|
From: Thomas Sturges-A. <zi...@ho...> - 2011-10-28 17:04:52
|
Thanks for all your input. I did quite quickly notice the limitations of the code but I think I have managed to work round it for my purposes. I am building a adventure game engine for fun/honing my Python knowledge and so I wanted to be able to check if, for example a given choice had requirements or effects (which would be stored in a sub-section) so that code relating to processing these could be bypassed. As such I am mostly going to be checking for a subsection in a section that is already known to be there (so no chance of a KeyError). The code I will probably use is 'Basics' in character.sections #for checking for primary sections 'name' in character['Basics'].scalars #for checking for options in a (known) primary section 'stuff' in character['Basics'].sections #for checking for sub-sections in a (known) primary section 'blah' in character['Basics']['stuff'].scalars #for checking for options in a (known) sub-section in a (known) primary section etc if need be.... I won't be using this for a bit though as I have only got just (today) finished off the adventure and character loading system but I didn't want to start building configobj into the code if then it would screw me over down the line. The methods above are simple enough so it looks like everything should be cool :) . Is somewhat of a shame they can't be used in a class thougth (as single quotes are needed inside and out of the class which I don't think is possible) because it would have been a great entry for me into classes (believe it or not I haven't used them yet). The GitHun Gist (I haven't got my head round Git yet) can be found here: https://gist.github.com/1322726 if you're interested. Thanks again! For a great module and help using it! Thomas Sturges-Allard From: neg...@gm... Date: Fri, 28 Oct 2011 10:44:32 -0400 Subject: Re: [Configobj-develop] has_section function? To: fuz...@vo... CC: con...@li...; zi...@ho... On Fri, Oct 28, 2011 at 10:18, Michael Foord <fuz...@vo...> wrote: On 28/10/2011 15:08, David Hostetler wrote: Hey Michael, Unless I'm mistaken, your suggestions don't completely address Thomas' question. configfile.sections and configfile.scalars don't expose nested entries. So yes, he can use those, but they'll be misleading unless he's using configuration data that he can guarantee isn't nested, in which case he's not any better off than when he was using ConfigParser. Well he is because you can't have nested sections *at all* with ConfigParser (and there are lots of other reasons to prefer ConfigObj...). Of course - lots of really good reasons to prefer ConfigObj. :) I certainly didn't mean for that to sound disparaging. Just that if he migrated to ConfigObj because he wanted nested data, then artificially constraining himself to not be nested defeats the purpose. I *think* that he was hoping there was some super-convenient way to ask a ConfigObj instance to be deeply introspective and test for a given section-name/key-name anywhere in the cfg hierarchy. Not terribly difficult to do with a smidge of recursion, but not natively offered by the ConfigObj interface as far as I know. The code I suggested was an exact equivalent of "has_section" and "has_option" in ConfigParser - but you're right, this code only checks the current section and doesn't recurse. You could write a recursive version easily, but what would that be useful for? Knowing that *somewhere* inside a nested data structure the section/option exists isn't useful, because you still have to write the recursive code to find it. By the time you've done that you've written the code to check as well... All true. If I were to write something addressing this concept, I'd have it actually return the entity that it was testing for. I.e. if it returns None, the desired item doesn't exist, otherwise it does and here it is for your convenience. configob (and validate) rock, as always. :) regards, -hoss David Hostetler neg...@gm... |
|
From: David H. <neg...@gm...> - 2011-10-28 18:27:12
|
Hey Thomas,
I had a comment that isn't configobj-specific, but I'll keep this on-list in
case it might benefit others.
You mentioned KeyErrors... One way to use dicts without risking KeyError
exceptions is to use the get() function instead of the [] syntax. I.e.:
>>> x = {'a':42}
>>> x.get('a')
42
>>> x.get('b')
>>>
>>> x.get('b') is None
True
>>> x.get('b', 'somedefault')
>>> 'somedefault'
So if a key exists, the value is returned, as expected. If the key doesn't
exist, you just get None, or a default value if you provide one in the
call. The concept is similar to the getattr() function, but you don't get
an exception if the entry doesn't exist even if you don't provide a default.
Not that you couldn't wrap dictionary access in a try/except block, but
using get() is a cleaner implementation when you know you're dealing with
something that may or may not exist.
cheers,
-hoss
David Hostetler
neg...@gm...
p.s. - I didn't understand your class comment regarding single quotes and am
curious what you mean (since I think classes probably can be used - there's
almost no circumstance in which they can't).
On Fri, Oct 28, 2011 at 13:04, Thomas Sturges-Allard <zi...@ho...>wrote:
> Thanks for all your input. I did quite quickly notice the limitations of
> the code but I think I have managed to work round it for my purposes. I am
> building a adventure game engine for fun/honing my Python knowledge and so I
> wanted to be able to check if, for example a given choice had requirements
> or effects (which would be stored in a sub-section) so that code relating to
> processing these could be bypassed. As such I am mostly going to be checking
> for a subsection in a section that is already known to be there (so no
> chance of a KeyError). The code I will probably use is
>
> 'Basics' *in* character.sections #for checking for primary sections
> 'name' *in* character*[*'Basics'*]*.scalars #for checking for options in a
> (known) primary section
> 'stuff' *in* character*[*'Basics'*]*.sections #for checking for
> sub-sections in a (known) primary section
> 'blah' *in* character*[*'Basics'*][*'stuff'*]*.scalars #for checking for
> options in a (known) sub-section in a (known) primary section
> etc if need be....
>
> I won't be using this for a bit though as I have only got just (today)
> finished off the adventure and character loading system but I didn't want to
> start building configobj into the code if then it would screw me over down
> the line. The methods above are simple enough so it looks like everything
> should be cool :) . Is somewhat of a shame they can't be used in a class
> thougth (as single quotes are needed inside and out of the class which I
> don't think is possible) because it would have been a great entry for me
> into classes (believe it or not I haven't used them yet). The GitHun Gist (I
> haven't got my head round Git yet) can be found here:
> https://gist.github.com/1322726 if you're interested.
>
> Thanks again! For a great module and help using it!
>
> Thomas Sturges-Allard
>
> ------------------------------
> From: neg...@gm...
> Date: Fri, 28 Oct 2011 10:44:32 -0400
> Subject: Re: [Configobj-develop] has_section function?
> To: fuz...@vo...
> CC: con...@li...; zi...@ho...
>
>
>
>
> On Fri, Oct 28, 2011 at 10:18, Michael Foord <fuz...@vo...>wrote:
>
> On 28/10/2011 15:08, David Hostetler wrote:
>
> Hey Michael,
>
> Unless I'm mistaken, your suggestions don't completely address Thomas'
> question.
>
> configfile.sections and configfile.scalars don't expose nested entries. So
> yes, he can use those, but they'll be misleading unless he's using
> configuration data that he can guarantee isn't nested, in which case he's
> not any better off than when he was using ConfigParser.
>
>
> Well he is because you can't have nested sections *at all* with
> ConfigParser (and there are lots of other reasons to prefer ConfigObj...).
>
>
>
> Of course - lots of really good reasons to prefer ConfigObj. :) I
> certainly didn't mean for that to sound disparaging. Just that if he
> migrated to ConfigObj because he wanted nested data, then artificially
> constraining himself to not be nested defeats the purpose.
>
>
>
>
> I *think* that he was hoping there was some super-convenient way to ask a
> ConfigObj instance to be deeply introspective and test for a given
> section-name/key-name anywhere in the cfg hierarchy.
>
> Not terribly difficult to do with a smidge of recursion, but not natively
> offered by the ConfigObj interface as far as I know.
>
>
> The code I suggested was an exact equivalent of "has_section" and
> "has_option" in ConfigParser - but you're right, this code only checks the
> current section and doesn't recurse. You could write a recursive version
> easily, but what would that be useful for? Knowing that *somewhere* inside a
> nested data structure the section/option exists isn't useful, because you
> still have to write the recursive code to find it. By the time you've done
> that you've written the code to check as well...
>
>
>
> All true. If I were to write something addressing this concept, I'd have
> it actually return the entity that it was testing for. I.e. if it returns
> None, the desired item doesn't exist, otherwise it does and here it is for
> your convenience.
>
>
> configob (and validate) rock, as always. :)
>
>
>
> regards,
>
> -hoss
>
> David Hostetler
> neg...@gm...
>
>
> ------------------------------------------------------------------------------
> The demand for IT networking professionals continues to grow, and the
> demand for specialized networking skills is growing even more rapidly.
> Take a complimentary Learning@Cisco Self-Assessment and learn
> about Cisco certifications, training, and career opportunities.
> http://p.sf.net/sfu/cisco-dev2dev
> _______________________________________________
> Configobj-develop mailing list
> Con...@li...
> https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
|