473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

global name not defined

I added a function 'warn_Admin' and defined it just before another
function 'process_log'. 'process_log' calls this warn_Admin' function.
However, when it gets called i get the following error every time:
---
Traceback (most recent call last):
File "/usr/bin/denyhosts.py", line 202, in ?
first_time, noemail, daemon)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
86, in __init__
last_offset)
File "/usr/lib/python2.3/site-packages/DenyHosts/daemon.py", line 74,
in createDaemon
apply(func, args)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
137, in runDaemon
purge_time, purge_sleep_ratio)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
178, in daemonLoop
last_offset = self.process_log(logfile, last_offset)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
380, in process_log
[warn_Admin(ip) for ip in new_denied_hosts]
NameError: global name 'warn_Admin' is not defined
--
If I take the two functions out of their current environment and store
them in test file and run it, it doesn't complain. I'm new to python
so I'm guessing there is some weird scope rule I am missing. I did try
'self.warn_Admin(ip)' just to be safe but then I got a 'too many
arguments' error?

I'm lost :)

the added function plus the header of the existing function(its too
large):
------------
def warn_Admin(warn_ip):
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: ke***@netkev.com\n")
p.write("Subject: test from denyhosts\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write(warn_ip)
sts = p.close()
if sts != 0:
info("Sendmail exit status: %s", sts)
return sts
def process_log(self, logfile, offset):
-------------

the call to warn_Admin from process_log:
---
if new_denied_hosts:
info("new denied hosts: %s", str(new_denied_hosts))
#[info(ip) for ip in new_denied_hosts]
[warn_Admin(ip) for ip in new_denied_hosts]
else:
debug("no new denied hosts")

-kevin

May 22 '06 #1
5 6680
"NetKev" <ke***@netkev.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
I added a function 'warn_Admin' and defined it just before another
function 'process_log'. 'process_log' calls this warn_Admin' function.
However, when it gets called i get the following error every time:
---
Traceback (most recent call last):
File "/usr/bin/denyhosts.py", line 202, in ?
first_time, noemail, daemon)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
86, in __init__
last_offset)
File "/usr/lib/python2.3/site-packages/DenyHosts/daemon.py", line 74,
in createDaemon
apply(func, args)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
137, in runDaemon
purge_time, purge_sleep_ratio)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
178, in daemonLoop
last_offset = self.process_log(logfile, last_offset)
File "/usr/lib/python2.3/site-packages/DenyHosts/deny_hosts.py", line
380, in process_log
[warn_Admin(ip) for ip in new_denied_hosts]
NameError: global name 'warn_Admin' is not defined
--
If I take the two functions out of their current environment and store
them in test file and run it, it doesn't complain. I'm new to python
so I'm guessing there is some weird scope rule I am missing. I did try
'self.warn_Admin(ip)' just to be safe but then I got a 'too many
arguments' error?

I'm lost :)

the added function plus the header of the existing function(its too
large):
------------
def warn_Admin(warn_ip):
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
p = os.popen("%s -t" % SENDMAIL, "w")
p.write("To: ke***@netkev.com\n")
p.write("Subject: test from denyhosts\n")
p.write("\n") # blank line separating headers from body
p.write("Some text\n")
p.write(warn_ip)
sts = p.close()
if sts != 0:
info("Sendmail exit status: %s", sts)
return sts
def process_log(self, logfile, offset):
-------------

the call to warn_Admin from process_log:
---
if new_denied_hosts:
info("new denied hosts: %s", str(new_denied_hosts))
#[info(ip) for ip in new_denied_hosts]
[warn_Admin(ip) for ip in new_denied_hosts]
else:
debug("no new denied hosts")

-kevin


Sounds like warn_Admin is defined within a class.
a. could not resolve name when call was not qualified with "self."
b. when called as "self.warn_Admin", name was resolved, but got "too many
arguments" - this is because there was no explicit self argument in the
definition of warn_Admin.

It doesn't look like warn_Admin needs to be in the class. Move warn_Admin
to module-level scope, outside of the class containing process_log, and see
if things work better.

-- Paul
-- Paul
May 22 '06 #2
You are probably right and I think I will do so but just for the sake
of my understanding of python...I noticed somthing. process_log takes
two arguments when called but it's definition has 3 and one of them is
"self". So I'm thinking if I modify my warn_Admin definition to
include "self" and then call it from process_log with
self.warn_Admin... it will work. This explains why I was getting the
"too many arguments" error.

May 22 '06 #3
"NetKev" <ke***@netkev.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
You are probably right and I think I will do so but just for the sake
of my understanding of python...I noticed somthing. process_log takes
two arguments when called but it's definition has 3 and one of them is
"self". So I'm thinking if I modify my warn_Admin definition to
include "self" and then call it from process_log with
self.warn_Admin... it will work. This explains why I was getting the
"too many arguments" error.


Yes. When you invoke self.warn_Admin(x), it calls warn_Admin with 2 args,
self and x.

The reason I did not suggest this is becaus it looked like warn_Admin didn't
really use anything inside self, so why make it a method?

Looks like you are getting the method/function concepts straight.

(I'm not trying to confuse you, but you could also make warn_Admin a
staticmethod within the class, using the @staticmethod decorator. Static
methods do not pass the self argument, so making warn_Admin into a static
method would be another way to resolve this problem. But only do this if
your class, whatever it is, has something inherently about it that wants its
own warn_Admin method - otherwise, just make it a global function.)

-- Paul
May 23 '06 #4
NetKev wrote:
(snip)
def process_log(self, logfile, offset):
if new_denied_hosts:
info("new denied hosts: %s", str(new_denied_hosts))
[warn_Admin(ip) for ip in new_denied_hosts]


This uselessly builds a list. List comprehension is meant to create
lists, not to replace for loops.

for ip in new_denied_hosts:
warn_admin(ip)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 23 '06 #5
good point

May 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
3
by: Anjali Lourda | last post by:
Hi, I have defined a function in global.asax file. Could somebody please tell me how i am supposed to call that function from the other files of the same project. Global.asax public function...
12
by: a | last post by:
def fn(): for i in range(l) global count count= .... how do i declare count to be global if it is an array subsequently i should access or define count as an array error:
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
3
by: barronmo | last post by:
I'm getting an error msg I don't understand, "global name EMR_globals is not defined", and could use some help. I've separated the application I'm building into several modules. One of the...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
2
by: pythonnewb | last post by:
I am fairly new to programming but have some very basic Java background. I am just learning python and tried to make a module that would allow me to create a file containing an address book. I was...
0
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no*...
4
by: RgeeK | last post by:
I have a main module doStuff.py and another module utility.py. At the start of doStuff.py I call import utility.py Then I also proceed to initiallize some global variables sName = "" ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.