I have a function that return a class and I save it as a object. When I try to call on a class method I'll get the error
location = file_type.route(env['REQUEST_URI']) # Get the location
TypeError: 'int' object is not callable
I have changed name on the object to make sure it is no naming collision.
On the first line I call the the method get_file_type in module route.
file_type = route.get_file_type(env['REQUEST_URI']) # Get File_type object
location = file_type.route(env['REQUEST_URI']) # Get the location
If I print out the file_type I get output <route.File_type instance at 0x7f96e3e90950>
I store the File_type classes in a dictionary and the method get_file_type returns a File_type based on the request.
path = {'html' : File_type('html', 1, 1),
'css' : File_type('css', 1, 0),
'ttf' : File_type('ttf', 0, 0),
}
def get_file_type(request): # return a File_type object
extension = request.rsplit('.', 1)[-1]
if extension in path:
return path[extension]
return path['html']
File_type class
class File_type:
def __init__(self, type_, parse, route):
self.type_ = type_
self.parse = parse
self.route = route
def route(resource):
if self.route == 1:
for pattern, value in routes:
if re.search(pattern, resource):
return value
return None
else:
return resource.rsplit('.', 1)[0][1:]