Hello, I have a problem with calling a staticmethod from another staticmethod when I try use dictionary {} to handle.

My code is very basic, but in the future I will add many other static method:

# -*- coding: utf-8 -*-
import sys

class DateFormat():
	"""Class to format and translate date to polish"""
	
	labels = {
			'monday'   : u'poniedziałek',
			'tuesday'  : u'wtorek',
			'wenesday' : u'środa',
			'thursday' : u'czwartek',
			'friday'   : u'piątek',
			'saturday' : u'sobota',
			'sunday'   : u'niedziela',
			}
	
	@staticmethod
	def small(date_string):
		return date_string.lower()
	
        # I know this is very simple example
	@staticmethod
	def big(date_string):
		return date_string.upper()
			
	# handler to format function
	filters = {
				'small' : small,   
			    'big'   : big,     
			   }

	@staticmethod
	def get_date(label, *args):
		try:
			new_format = DateFormat.labels[label]
		except:
			return new_format
		
		for format in args:
			print ('format:'  + str(format))
			new_format = DateFormat.filters[format](new_format)
			
		return new_format

Now when I try use:

translate = DateFormat.get_date('friday')

I get correct answer: 'piątek'

But when I try use this method with some filter I get incorrect answer:

translate = DateFormat.get_date('friday', 'big')

I get: TypeError: 'staticmethod' object is not callable, but I don't know why?

Try this:

filters = {'small': DateFormat.small, 'big': DateFormat.big}

This works in Python 2.3:

import sys

class DateFormat(object):
	"""Class to format and translate date to polish"""
	
	labels = {
			'monday'   : u'poniedziałek',
			'tuesday'  : u'wtorek',
			'wenesday' : u'środa',
			'thursday' : u'czwartek',
			'friday'   : u'piątek',
			'saturday' : u'sobota',
			'sunday'   : u'niedziela',
			}
	
	#@staticmethod
	def small(date_string):
		return date_string.lower()
	small = staticmethod(small)
	
    # I know this is very simple example
	#@staticmethod
	def big(date_string):
		return date_string.upper()
	big = staticmethod(big)
			
	# handler to format function
	filters = {'small': DateFormat.small, 'big': DateFormat.big}

	# @staticmethod
	def get_date(label, *args):
		try:
			new_format = DateFormat.labels[label]
		except:
			return new_format
		
		for format in args:
			print ('format:'  + str(format))
			new_format = DateFormat.filters[format](new_format)
			
		return new_format
	get_date = staticmethod(get_date)

print DateFormat.get_date('friday')
print DateFormat.get_date('friday', 'big')
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.