[Tutor] Python and PHP config files

Kalle Svensson kalle@lysator.liu.se
Sat, 1 Jun 2002 15:44:37 +0200


-----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-----