[Tutor] Global var not defined?

2013-08-27 Thread leam hall
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



-- 
Mind on a Mission http://leamhall.blogspot.com/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global var not defined?

2013-08-27 Thread Alan Gauld

On 27/08/13 18:58, leam hall wrote:


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 Python global only applies to the local file.
Thus customers needs to be at the global level in
functions.py.

But since its a bad use of globals it would be better to just get the 
function to return the value and do the append in the top level file...


BTW the line

  host_list[cust] = {}

Makes no sense since you are  adding an empty dictionary  to a variable 
that doesn't exist so you should get an error. I assume you have 
simplified the code somewhat?



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)


   customers.append(functions.get_customer(line_list, customers))

Avoids any need for globals.

Personally I'd move the line split into the function so your loop looks 
like:


for line in input_file:
 line = line.strip()
 if line:
customers.append(functions.get_customer(line_list, customers))

Alternatively I'd put the customers list into a module called customer
and move the customer functions into that module. And then it looks
like I might have a Customer class emerging... And maybe the code above 
would become a class method:

   Customer.readFromFile(aFile) - [cust1, cust2, ...]


Just a thought...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global var not defined?

2013-08-27 Thread Prasad, Ramit
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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global var not defined?

2013-08-27 Thread Alan Gauld

On 27/08/13 19:50, leam hall wrote:

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



You will need to change something because what you have won;t work.

The question is what to change?


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.


So abstracting that you have several customer objects each containing
a list(or dict?) of servers. Servers in turn have numerous attributes:
name, role, ip, environ etc...


When I print these out they will be
sorted by customer but rolled into a single file.


So the customer objects have a method that prints to a file.

That's the OOP approach, but you can do it without classes
if you want, its just a bit more effort on the readability
and coding front.


The line host_list[cust] = {} creates the customer dictionary if that
customer doesn't exist.


It may be better to look at the get() method of dictionaries for that.
But the problem I highlighted was that the top level host_list 
dictionary  didn't exist in your code. You need to initialize

it before you can access the cust key.

host_list = {}
.
.
.
host_list.get(cust, {})


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.


A class is effectively a dictionary inside so its very similar.
Using my OOP suggestion above this would translate to something like:

alan.servers['webserver'].ip = '12.23.34.45

Which you find more readable is a matter of taste!
There are some other advantages to the OOP approach but
they are not critical here.


The data does not get altered, just organized.


That doesn't make much difference in this case.

The simplest solution to get it working is probably just
to move the customers list into the functions module.
In the longer term the other options might prove more beneficial.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Global var not defined?

2013-08-27 Thread Steven D'Aprano

On 28/08/13 03:58, 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


Globals are not globally global, they are global to the module. Otherwise 
variables defined in module X would stomp all over variables defined in module Y, 
unpredictably depending on the order than modules were imported.



In the calling file:

import functions
import sys

customers = []



This cannot work, because it belongs to a different namespace (module). Just pass 
customers as an explicit parameter to append_customer, and then google for Global 
variables considered harmful.

If you absolutely must emulate COBOL programmers of the 1970s and insist on 
using globals, write:

functions.customers = []

instead.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor