[Tutor] Python and PHP config files
Lloyd Kvam
lkvam@venix.com
Sat, 01 Jun 2002 18:43:55 -0400
An alternative approach would be to put the config information into a
dictionary. Picking up from your code:
...
config = datafile.readlines()
config_dict = {}
for con in config: #line by line
name,value = con.split('=') #split as Kalle did
config_dict[name] = value
...
now
db = config_dict["db_name"]
When I can, I like to save my config info directly in a dictionary that I
simply import.
Kalle Svensson wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> [Michael A. Peters]
>
>>For example, the lines in the file i need are-
>>
>>$db_name="foldingathome";
>>$db_host="localhost";
>>$db_port="3306";
>>$db_user="samwise";
>>$db_password="xyzzy";
>>
>>I need to find where the php variable for db_name is defined and assign a
>>python variable for that- such as
>>
>>db_name = somefunction(config, db_name)
>>where
>>somefunction(config, db_name)
>>would find $db_name= in the config list and then return what's in their.
>>
>
> A rough beginning might look something like this:
>
> def find_parameter(config_lines, parameter):
> for line in config_lines:
> if line.find(parameter) >= 0:
> return line.split("=")[1]
>
> It can be used like this:
>
>
>>>>config = ['foo\n', '$db_host="host";\n', '$db_user="user";\n']
>>>>find_parameter(config, "$db_user")
>>>>
> '"user";\n'
>
> See http://python.org/doc/current/lib/string-methods.html for more
> information about string methods like find and split.
>
> Peace,
> Kalle
> - --
> Kalle Svensson, http://www.juckapan.org/~kalle/
> Student, root and saint in the Church of Emacs.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.0.7 (GNU/Linux)
> Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>
>
> iD8DBQE8+M+odNeA1787sd0RAhNBAKC0WIDuM8Utz7UtDKAytDc/8z6+HgCeKeDL
> UgwMzmTkaqyIkbrspapR4Hs=
> =7UWR
> -----END PGP SIGNATURE-----
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582