Variable Substitution in commands or funtion names?

fleet at teachout.org fleet at teachout.org
Thu Jul 19 22:46:40 CEST 2001


>   Date: Thu, 19 Jul 2001 11:43:21 -0700
>   From: Chris Barker <chrishbarker at home.net>
>
>fleet at teachout.org wrote:
>
>> I think I may have gotten myself into a box.
>>
>> I have a module "cust.py" that contains several dictionaries - each
>> references a different customer with address info, etc. such as:
>>
>> smith={"busname":"Smitties", "street":"123 West Ave"}
>
>I think this is your problem right here. Having the customer name as a
>variable name is problematic, it means the customer's names are
>hard-coded into your module.

I've created dictionaries for each customer in cust.py.  I don't have a
lot of customers and the information doesn't change often, therefore it's
pretty easy to edit the text file "cust.py" to make corrections or
add/delete customers.

If you'd like to do some charter fishing for rock fish in the Chesapeake,
here's what the actual first entry in my cust.py looks like:

Antonia={
   "name":"Nicholson & Daughters",
   "owner":"Capt. Charles Nicholson",
   "addr":"Bay Forest Rd.",
   "city":"Lexington Park",
   "st":"MD",
   "zip":"20653",
   "phone":"301-863-2767",
   "email":"",
   "web":"http://www.paxp.com/antonia/"}

It looks like all I need to do to convert to your suggestion is add:

"Customers={" before "Antonia," replace the "=" after Antonia with an
opening brace (and ditto for each of the other customers) and add a
closing brace at the end of the customers.

>You can do it, and some others have
>suggested how(you might also want to look at eval and exec),but why not
>use a nested dictionary?
>
>Customers = {smith: {"busname":"Smitties", "street":"123 West Ave"},
>             jones: {"busname":"Joneses", "street":"123 East Ave"},
>              ....}

And how are these NOT hardcoded into a module?  How would I access them
otherwise?

>now you do:
>
>from cust import Customers
>print Customers[CurCust]["street"]
>

Thanks for the input.  I'll try this.

					- fleet -

>--
>Christopher Barker,
>Ph.D.
>ChrisHBarker at home.net                 ---           ---           ---
>http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
>                                   ------@@@     ------@@@     ------@@@
>Oil Spill Modeling                ------   @    ------   @   ------   @
>Water Resources Engineering       -------      ---------     --------
>Coastal and Fluvial Hydrodynamics --------------------------------------







More information about the Python-list mailing list