[Tutor] Global var not defined?

leam hall leamhall at gmail.com
Tue Aug 27 20:50:52 CEST 2013


Well, I'm happy to change things but my python is only so good. And much of
that is based off of shell programming.

What the data looks like is fairly simple. I have a spreadsheet of host
information. Customer 'Alan' may have a dozen or so servers and customer
Ramit has another dozen or two. When I print these out they will be sorted
by customer but rolled into a single file.

The line "host_list[cust] = {}" creates the customer dictionary if that
customer doesn't exist. Then there's a host key with multiple layers:

   host_list['alan']['webserver']['ip'] = '12.23.34.45'
   host_list['alan']['webserver']['environ'] = 'Dev'

Make sense? As I do not know a lot about classes I'm not sure they are
better in this case than a multi-level dictionary. The data does not get
altered, just organized.

Leam


On Tue, Aug 27, 2013 at 2:34 PM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> leam hall wrote:
> > Could use some help with this. Python 2.4.3 on RHEL 5.x.
> >
> > In the functions file that gets imported:
> >
> > def append_customer(line_list):
> >         global customers
> >         cust = line_list[0]     // list with Customer info in [0]
> >         cust = clean_word(cust)  // Trims white space
> >
> >         if len(cust) and cust not in customers:
> >                 host_list[cust] = {}
> >                 customers.append(cust)
> >
> > In the calling file:
> >
> >
> > import functions
> > import sys
> >
> > customers = []
> >
> > .
> > .
> > .
> > for line in input_file:
> >     line = line.strip()
> >     if not len(line):
> >         continue
> >     line_list = line.split(',')
> >     functions.append_customer(line_list)
> >
> > Error message:
> > Traceback (most recent call last):
> >   File "./host_tools.py", line 55, in ?
> >     functions.append_customer(line_list)
> >   File "/home/lhall/lang/functions.py", line 27, in append_customer
> >     if len(cust) and cust not in customers:
> > NameError: global name 'customers' is not defined
> >
> >
>
> The problem is because "customers" needs to be defined
> in the module with the append_customers. Global as
> written refers to module level variables.
>
> Some (possible and untested) methods to get around this are:
> 1. pass in customers as an argument
> 2. use globals()?
> 3. add it to functions module `functions.customers = customers`.
>
>
> ~Ramit
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of securities,
> accuracy and completeness of information, viruses, confidentiality, legal
> privilege, and legal entity disclaimers, available at
> http://www.jpmorgan.com/pages/disclosures/email.
>



-- 
Mind on a Mission <http://leamhall.blogspot.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130827/7db3ced9/attachment-0001.html>


More information about the Tutor mailing list