How to create new section from var name?

11 views
Skip to first unread message

SJML

unread,
May 20, 2009, 11:05:55 PM5/20/09
to iniparse-discuss
I'm using the iniparse module (nice work, by the way), and have a
question about a particular use case.

What's the best way to make a new section in the INI file from a
variable name?

If I try to use the dictionary access syntax:

s = 'newsection'
config[s]['foo'] = bar

I get key errors on newsection. But I can't call the dot-syntax with a
variable name. Is there a good syntax for this use case, or do I just
need to mess around with attribute tables and the like?

Paramjit Oberoi

unread,
May 21, 2009, 1:06:59 AM5/21/09
to iniparse...@googlegroups.com
I would call it a bug. I'm surprised something so basic lay
undiscovered or so long.
I'll fix it soon and let you know when a fixed version is available.

The compatibility layer of iniparse has seen a lot more usage and
testing than the "native" interface, so I expect there are more such
issues lurking in there. Let me know if you encounter any others.

Thanks!
-param

SJML

unread,
May 21, 2009, 7:16:49 AM5/21/09
to iniparse-discuss
Thanks for the quick reply! Looking forward to the fix. Do you know of
a workaround in the meanwhile?

On May 21, 1:06 am, Paramjit Oberoi <psobe...@gmail.com> wrote:
> I would call it a bug. I'm surprised something so basic lay
> undiscovered or so long.
> I'll fix it soon and let you know when a fixed version is available.
>
> The compatibility layer of iniparse has seen a lot more usage and
> testing than the "native" interface, so I expect there are more such
> issues lurking in there.  Let me know if you encounter any others.
>
> Thanks!
> -param
>

Paramjit Oberoi

unread,
May 21, 2009, 10:55:52 AM5/21/09
to iniparse...@googlegroups.com
You can work around it for now by calling _new_namespace(newsection)
to create undefined sections. For example:

>>> import iniparse
>>> i=iniparse.INIConfig()
>>> i._new_namespace('foo')
>>> i['foo']['bar'] = 42
>>> print i
[foo]
bar = 42

This strictly a temporary workaround - the _new_namespace() method may
disappear in future versions.

-param

SJML

unread,
May 21, 2009, 11:41:26 PM5/21/09
to iniparse-discuss
Hopefully by the time it disappears there will be a fix for the
initial problem. :-)

Thanks again for your responsiveness on this.


On May 21, 10:55 am, Paramjit Oberoi <psobe...@gmail.com> wrote:
> You can work around it for now by calling _new_namespace(newsection)
> to create undefined sections.  For example:
>
> >>> import iniparse
> >>> i=iniparse.INIConfig()
> >>> i._new_namespace('foo')
> >>> i['foo']['bar'] = 42
> >>> print i
>
> [foo]
> bar = 42
>
> This strictly a temporary workaround - the _new_namespace() method may
> disappear in future versions.
>
> -param
>

Paramjit Oberoi

unread,
Jun 12, 2010, 1:33:45 PM6/12/10
to iniparse...@googlegroups.com
Sorry it took a year... but this should be fixed in iniparse-0.4

-param

> --~--~---------~--~----~------------~-------~--~----~
> To post to this group, send email to iniparse...@googlegroups.com
> To unsubscribe from this group, send email to iniparse-discu...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/iniparse-discuss?hl=en
> -~----------~----~----~----~------~----~------~--~---
>
>

Reply all
Reply to author
Forward
0 new messages