Re: passing object between classes

2009-09-22 Thread Duncan Booth
daved170 daved...@gmail.com wrote:

 Is there any existing python Log object that do so?

There is. See Jean-Michel Pichavant's reply.

 I no, I created my
 own Log object that only open file and write a line to it,

 how can I make it be global?

You could assign it to a global name.

 Should I use it as a static object?

If you wish. It depends on exactly what you want. Usually static
objects are fine for logging unless you want to be able to modify the
logging in particular situations (e.g. when running your unit tests). In 
that case you could just assign it to an attribute of your object much as 
you were failing to do in your original post: there is no reason at all why 
that shouldn't work also. 

 will it work?

There is no reason why not.

 (offcurse in the case of the threads I'll use utex)

With the built-in logging module you don't need to worry about that.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing object between classes

2009-09-21 Thread Duncan Booth
daved170 daved...@gmail.com wrote:

 Hi everybody,
 I built my owen log obj as a class.
 I'm passing it to another object (actually to a thread).
 When I run my app it raise error at the line when I'm using that log
 obj. is there any problem with the concept of passing object as I do
 it?
 How can I do that?
 
 class A:
 def foo1:
 myLog = cLog()
 myYhread = cThread(myLog)
 myThread.start()
 
 class cThread:
 def __init__(self,in_myLog):
 sel.LogObj = in_myLog
 
 def run():
sel.LogObj.writeLine(HI)
 
 thanks
 Dave
 

Please always post real code and state the exact error you get. That will 
maximise the chance that you get a useful answer.

The code you posted will fail for numerous reasons and I can't guess 
whether you have a problem with indentation, a problem because you 
misspelled 'self' as 'sel', or because you missed out the 'self' parameter 
altogether on a method, or perhaps your LogObj simply doesn't have a 
writeLine method.



-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing object between classes

2009-09-21 Thread daved170
On Sep 21, 1:44 pm, Duncan Booth duncan.bo...@invalid.invalid wrote:
 daved170 daved...@gmail.com wrote:
  Hi everybody,
  I built my owen log obj as a class.
  I'm passing it to another object (actually to a thread).
  When I run my app it raise error at the line when I'm using that log
  obj. is there any problem with the concept of passing object as I do
  it?
  How can I do that?

  class A:
  def foo1:
      myLog = cLog()
      myYhread = cThread(myLog)
      myThread.start()

  class cThread:
  def __init__(self,in_myLog):
      sel.LogObj = in_myLog

  def run():
     sel.LogObj.writeLine(HI)

  thanks
  Dave

 Please always post real code and state the exact error you get. That will
 maximise the chance that you get a useful answer.

 The code you posted will fail for numerous reasons and I can't guess
 whether you have a problem with indentation, a problem because you
 misspelled 'self' as 'sel', or because you missed out the 'self' parameter
 altogether on a method, or perhaps your LogObj simply doesn't have a
 writeLine method.

 --
 Duncan Boothhttp://kupuguy.blogspot.com- Hide quoted text -

 - Show quoted text -

Hi Duncan,
You are right, I should have put the entire code but my question was
more theroretical and I know that the code that I posted won't work.

Let me simplified my question, I need to be able to write to the same
log file from different classes. and even more, from different
threads.
Is there any existing python Log object that do so? I no, I created my
own Log object that only open file and write a line to it, how can I
make it be global? Should I use it as a static object? will it work?
(offcurse in the case of the threads I'll use utex)

Thanks again,
DaveD
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: passing object between classes

2009-09-21 Thread Jean-Michel Pichavant

daved170 wrote:

On Sep 21, 1:44 pm, Duncan Booth duncan.bo...@invalid.invalid wrote:
  

daved170 daved...@gmail.com wrote:


Hi everybody,
I built my owen log obj as a class.
I'm passing it to another object (actually to a thread).
When I run my app it raise error at the line when I'm using that log
obj. is there any problem with the concept of passing object as I do
it?
How can I do that?
  
class A:

def foo1:
myLog = cLog()
myYhread = cThread(myLog)
myThread.start()
  
class cThread:

def __init__(self,in_myLog):
sel.LogObj = in_myLog
  
def run():

   sel.LogObj.writeLine(HI)
  
thanks

Dave
  

Please always post real code and state the exact error you get. That will
maximise the chance that you get a useful answer.

The code you posted will fail for numerous reasons and I can't guess
whether you have a problem with indentation, a problem because you
misspelled 'self' as 'sel', or because you missed out the 'self' parameter
altogether on a method, or perhaps your LogObj simply doesn't have a
writeLine method.

--
Duncan Boothhttp://kupuguy.blogspot.com- Hide quoted text -

- Show quoted text -



Hi Duncan,
You are right, I should have put the entire code but my question was
more theroretical and I know that the code that I posted won't work.

Let me simplified my question, I need to be able to write to the same
log file from different classes. and even more, from different
threads.
Is there any existing python Log object that do so? I no, I created my
own Log object that only open file and write a line to it, how can I
make it be global? Should I use it as a static object? will it work?
(offcurse in the case of the threads I'll use utex)

Thanks again,
DaveD
  

I have a theoretical answer to your theoretical question :

Use the standard python module logging for logging activities.
http://docs.python.org/library/logging.html

It's very powerful, standard, thread safe and time-saving.

cheers,

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