Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > loading configuration files that are themselves python

Reply
Thread Tools

loading configuration files that are themselves python

 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-03-2010
Matthew Vernon a écrit :
> Hi,
>
> Is there a more idiomatic way of loading in a configuration file
> that's python code than:
>
> _temp=__import__(path,fromlist='cachestrs')
> cachestrs=_temp.cachestrs
>
> ? I mean, that's pretty ugly...Plain "import" doesn't work in this
> case because 'path' is a variable defined elsewhere


At least you have a way to do it, so you should be happy !-)

Another solution would be to add the path to sys.path, but it wouldn't
necessarily be the best thing to do here.
 
Reply With Quote
 
 
 
 
Matthew Vernon
Guest
Posts: n/a
 
      07-03-2010
Hi,

Is there a more idiomatic way of loading in a configuration file
that's python code than:

_temp=__import__(path,fromlist='cachestrs')
cachestrs=_temp.cachestrs

? I mean, that's pretty ugly...Plain "import" doesn't work in this
case because 'path' is a variable defined elsewhere

TIA,

Matthew

--
Rapun.sel - outermost outpost of the Pick Empire
http://www.pick.ucam.org
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-03-2010
Matthew Vernon wrote:

> Is there a more idiomatic way of loading in a configuration file
> that's python code than:
>
> _temp=__import__(path,fromlist='cachestrs')
> cachestrs=_temp.cachestrs
>
> ? I mean, that's pretty ugly...Plain "import" doesn't work in this
> case because 'path' is a variable defined elsewhere


execfile(path)

in a module with a fixed name that you can import wherever you need access
to your configuration data?

Peter

 
Reply With Quote
 
Matthew Vernon
Guest
Posts: n/a
 
      07-03-2010
Peter Otten <__peter__@web.de> writes:

> Matthew Vernon wrote:
>
> > Is there a more idiomatic way of loading in a configuration file
> > that's python code than:
> >
> > _temp=__import__(path,fromlist='cachestrs')
> > cachestrs=_temp.cachestrs
> >
> > ? I mean, that's pretty ugly...Plain "import" doesn't work in this
> > case because 'path' is a variable defined elsewhere

>
> execfile(path)
>
> in a module with a fixed name that you can import wherever you need access
> to your configuration data?


That looks like what I want, thanks

Matthew

--
Rapun.sel - outermost outpost of the Pick Empire
http://www.pick.ucam.org
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-03-2010
On 7/3/2010 5:15 AM, Matthew Vernon wrote:
> Hi,
>
> Is there a more idiomatic way of loading in a configuration file
> that's python code than:
>
> _temp=__import__(path,fromlist='cachestrs')
> cachestrs=_temp.cachestrs
>
> ? I mean, that's pretty ugly...Plain "import" doesn't work in this
> case because 'path' is a variable defined elsewhere


cachestrs=__import__(path,fromlist='cachestrs').ca chestrs
?

--
Terry Jan Reedy

 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      07-24-2010
In message <>, Matthew Vernon wrote:

> Is there a more idiomatic way of loading in a configuration file
> that's python code ...


Is it really a good idea to have a configuration language that’s Turing-
complete?
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      07-29-2010
Ben Finney wrote:
> Lawrence D'Oliveiro <_zealand> writes:
>
>
>> In message <>, Matthew Vernon wrote:
>>
>>
>>> Is there a more idiomatic way of loading in a configuration file
>>> that's python code ...
>>>

>> Is it really a good idea to have a configuration language that’s Turing-
>> complete?
>>

>
> I think not. Configuration files should be read as data; they should be
> declarative only, not executable languages. That way, a different
> program can read and parse them without having to be a parser for an
> entire programming language.
>


For local non distributed applications, configuration files written in
python are just fine.

JM

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK Mike ASP .Net 5 08-15-2007 08:57 AM
Web Form Designer object and initialize component code disappear by themselves Jonathan Stone ASP .Net 1 08-21-2006 08:33 PM
Loading configuration files in an OS-agnostic way Gavri Savio Fernandez Ruby 2 12-17-2004 08:11 AM
Files renemed by themselves CAS Computer Support 2 09-08-2003 08:35 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57