@staticmethods called more than once

2013-05-21 Thread Christian
Hi,

i'm somewhat confused working with @staticmethods. My logger and configuration  
methods are called n times, but I have only one call.  
n is number of classes which import the loger and configuration class
in the subfolder mymodule. What might be my mistake mistake?

Many thanks
Christian



### __init__.py ###

from  mymodule.MyLogger import MyLogger
from  mymodule.MyConfig import MyConfig


 
# my_test.py ##
from mymodule import MyConfig,MyLogger

#Both methods are static
key,logfile,loglevel = MyConfig().get_config('Logging')
log = MyLogger.set_logger(key,logfile,loglevel)
log.critical(time.time())

#Output
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19

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


Re: @staticmethods called more than once

2013-05-21 Thread Skip Montanaro
Don't confuse the use of static in Python with its use in C/C++.  From a
post on StackOverflow:

A staticmethod is a method that knows nothing about the class or instance
 it was called on. It just gets the arguments that were passed, no implicit
 first argument. It is basically useless in Python -- you can just use a
 module function instead of a staticmethod.


That is, the @staticmethod decorator doesn't mean, call this function
once.

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


Re: @staticmethods called more than once

2013-05-21 Thread John Gordon
In 02f0123d-2f9e-4287-b983-cfa1db9db...@googlegroups.com Christian 
mining.fa...@gmail.com writes:

 Hi,

 i'm somewhat confused working with @staticmethods. My logger and 
 configuration  methods are called n times, but I have only one call.  
 n is number of classes which import the loger and configuration class
 in the subfolder mymodule. What might be my mistake mistake?

 Many thanks
 Christian

 ### __init__.py ###

 from  mymodule.MyLogger import MyLogger
 from  mymodule.MyConfig import MyConfig

 # my_test.py ##
 from mymodule import MyConfig,MyLogger

 #Both methods are static
 key,logfile,loglevel = MyConfig().get_config('Logging')
 log = MyLogger.set_logger(key,logfile,loglevel)
 log.critical(time.time())

 #Output
 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19

You haven't given us the code for your MyLogger class, so it's difficult
to say exactly what the problem is.

However, I have a guess.  Does MyLogger.set_logger() contain a call to
addHandler()?  Each call to addHandler() adds another handler to your
logger, and when you call log.critical() [or any other log function] you
get one line of output for each handler.

You should only call addHandler() once.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, The Gashlycrumb Tinies

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


Re: @staticmethods called more than once

2013-05-21 Thread John Gordon
In kng7n6$efc$1...@reader1.panix.com John Gordon gor...@panix.com writes:

 You should only call addHandler() once.

...for each intended logging output destination, of course.  If you want
logging output to appear in a file and on-screen, then you would call
addHandler() once with a file handler and once with a screen handler.

But I think you may be calling addHandler multiple times for the same
file handler, which is causing the duplicate logging output.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, The Gashlycrumb Tinies

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


Re: @staticmethods called more than once

2013-05-21 Thread Ethan Furman

On 05/21/2013 08:39 AM, Skip Montanaro wrote:

Don't confuse the use of static in Python with its use in C/C++.  From a post 
on StackOverflow:

A staticmethod is a method that knows nothing about the class or instance 
it was called on. It just gets the
arguments that were passed, no implicit first argument. It is basically 
useless in Python -- you can just use a
module function instead of a staticmethod.


For there record, staticmethod is useful when you want to make it possible for 
subclasses to change behavior.

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


Re: @staticmethods called more than once

2013-05-21 Thread Christian
Am Dienstag, 21. Mai 2013 18:48:07 UTC+2 schrieb John Gordon:
 In kng7n6$efc$1...@reader1.panix.com John Gordon gor...@panix.com writes:
 
 
 
  You should only call addHandler() once.
 
 
 
 ...for each intended logging output destination, of course.  If you want
 
 logging output to appear in a file and on-screen, then you would call
 
 addHandler() once with a file handler and once with a screen handler.
 
 
 
 But I think you may be calling addHandler multiple times for the same
 
 file handler, which is causing the duplicate logging output.
 
 
 
 -- 
 
 John Gordon   A is for Amy, who fell down the stairs
 
 gor...@panix.com  B is for Basil, assaulted by bears
 
 -- Edward Gorey, The Gashlycrumb Tinies

Yes you're right.
Many thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: @staticmethods called more than once

2013-05-21 Thread 88888 Dihedral
Ethan Furman於 2013年5月22日星期三UTC+8上午12時30分22秒寫道:
 On 05/21/2013 08:39 AM, Skip Montanaro wrote:
 
  Don't confuse the use of static in Python with its use in C/C++.  From a 
  post on StackOverflow:
 
 
 
  A staticmethod is a method that knows nothing about the class or 
  instance it was called on. It just gets the
 
  arguments that were passed, no implicit first argument. It is basically 
  useless in Python -- you can just use a
 
  module function instead of a staticmethod.
 
 
 
 For there record, staticmethod is useful when you want to make it possible 
 for subclasses to change behavior.
 
 
 
 --
 
 ~Ethan~

I prefer objects in classes with slimer figures not heavily weighted
with trivial methods in each instance construction and clone.

 But this is only my personal style of classes in python.
-- 
http://mail.python.org/mailman/listinfo/python-list