0

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?

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by bvdet
0

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')
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.