Data sticking around too long

2006-09-06 Thread CedricCicada
Greetings!

Here's my script:

  start of script
class ScannerCommand:
taskName = ''
scanList = []

def __init__(self):
print "Creating a ScannerCommand object; list has " + \
  str(len(self.scanList)) + " objects."

class Scanner:
def Read(self, data):
command = ScannerCommand()
command.scanList.append(data)
return command

class Handler:
def Process(self, dataList):
print "Processing data"
for data in dataList:
print "   " + data
print "Finished processing data."

if __name__ == '__main__':
s = Scanner()
count = 0
for data in ["Zero", "One", "Two", "Three"]:
command = s.Read(data)
handler = Handler()
handler.Process(command.scanList)
##  End of script

Here's the result:

##  Start of result
Processing data
   Zero
Finished processing data.
Creating a ScannerCommand object; list has 1 objects.
Processing data
   Zero
   One
Finished processing data.
Creating a ScannerCommand object; list has 2 objects.
Processing data
   Zero
   One
   Two
Finished processing data.
Creating a ScannerCommand object; list has 3 objects.
Processing data
   Zero
   One
   Two
   Three
Finished processing data.
  End of result

I had expected to get "Zero" by itself, then "One" by itself, and so
on.

Why is the ScannerCommand object being created with a scanList that
contains the data that was in the previously created ScannerCommand
object?  And what do I have to do to ensure that the code that is run
when I call ScannerCommand() gives me an object with an empty scanList?

I am a C++ developer in a shop that uses some Python as well, and I
just ran across this behavior for the first time.  I believe it is due
to the difference between binding a variable to an object, as Python
does, and actually creating the variable, as a similar C++ application
would do.

Thank you very much.

Rob Richardson
RAD-CON, Inc.
Bay Village, OH

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data sticking around too long

2006-09-06 Thread CedricCicada
Skip and Matimus,

Thank you for your replies.  Putting initialization in the constructor
gets me what I want.  But I'd like to understand this a bit more.
Here's another script:

class ScannerCommand:
taskName = ''
scanList = []

def __init__(self, data):
self.scanList = []
self.scanList.append(data)

if __name__ == '__main__':
c1 = ScannerCommand("c1")
c2 = ScannerCommand("c2")
print "C1: "
for data in c1.scanList:
print "   " + data
print "C2: "
for data in c2.scanList:
print "   " + data

And here's the output, which is what I want:
C1:
   c1
C2:
   c2

If scanList is a class variable shared between all instances of the
class, then C1's list should have held "C2" when I printed it, since C2
came along and changed scanList.  But obviously, here it's not a class
variable and the two instances have their own lists.

If I don't initialize scanList in the constructor, then scanList is a
class variable (in C++, it would be a static member of the class) that
is shared among all instances of the class.  If I do initialize
scanList in the constructor, then scanList magically becomes an
instance variable, with every instance of the ScannerCommand object
having its own scanList list???  Is that really the way it works?  I
would have thought the C++ way, with some special syntax to distinguish
a class variable from an instance variable, would be much easier to
work with.

Thanks again!

Rob Richardson
RAD-CON, Inc.
Bay Village, OH

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data sticking around too long

2006-09-06 Thread CedricCicada
Greetings again!

There's something more to determining whether a class member is a class
variable or an instance variable.  Here's a slightly expanded version
of my last script:

class ScannerCommand:
taskName = ''
scanList = []
number = 0

def __init__(self, data):
pass
#self.scanList = []
#self.scanList.append(data)

if __name__ == '__main__':
c1 = ScannerCommand("c1")
c2 = ScannerCommand("c2")
c1.number = 1
c2.number = 2
c1.scanList.append("One")
c2.scanList.append("Two")
print "C1: " + str(c1.number)
for data in c1.scanList:
print "   " + data
print "C2: " + str(c2.number)
for data in c2.scanList:
print "   " + data

And here's the output:
C1: 1
   One
   Two
C2: 2
   One
   Two

Here, the two instances share the scanList list, as expected from
previous discussions, but they have their own copies of the number
variable.  Why is scanList a class variable but number an instance
variable here?

Good night, all!

Rob Richardson
RAD-CON, Inc.
Bay Village, OH

-- 
http://mail.python.org/mailman/listinfo/python-list


My first try using logger didn't work. Why?

2007-01-19 Thread CedricCicada
Greetings!

I want to write messages into the Windows event log.  I found
sevicemanager, but the source is always "Python Service", and I'd like
to be a bit more descriptive.  Poking around on the Internet revealed
the existence of the logging module.  It seems to have easily
understood methods with the power I need.  So I tried it.  Here's my
attempt:

logger = logging.getLogger("TahChung Part 1")
logger.setLevel(logging.INFO)
eventHandler = logging.NTEventLogHandler()
eventHandler.setlevel(logging.INFO)
formatter = logging.Formatter("%(message)s")
eventHandler.setFormatter(formatter)

logger.addHandler(eventHandler)
logger.error("This comes from the logger object.")

I get no error messages from this, but I also don't get anything in my
event log.  What am I doing wrong?

By the way, my source of instructions for how to do this was:
http://www.onlamp.com/pub/a/python/2005/06/02/logging.html

Rob Richardson
RAD-CON, Inc.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My first try using logger didn't work. Why?

2007-01-19 Thread CedricCicada
Beautiful!  Thank you very much!

One of the problems I was laboring under was that I did not know where
to go to find the official documentation.  Thanks for that link too!

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My first try using logger didn't work. Why?

2007-01-19 Thread CedricCicada
Vinay (or anybody else),

Well, now that logging is working, how do I stop it from working?

I'm developing in PythonWin.  When I run my test script, I get one
message.  When I run it a second time, I get two, a third time gets me
three, and so on.

I feel like the Sorceror's Apprentice!

Rob

-- 
http://mail.python.org/mailman/listinfo/python-list