1

I am trying to optimize code that was originally written using namedtuples and function to modify them. Each function would be called a great number of time for various values of the same namedtuple type.

Basic example would be:

def modifyTuple(record):
  if record.field1 == True:
    record.field2 = "VALUE1"
  elif record.field3 == "SPECIAL":
    record.field2 = "VALUE2"

The analysis of the bottleneck which i was experiencing and it shows that using multiple dictionary lookups every time the function is called leads to lost time. I am now trying to rewrite functions on-the-fly using inspect and basic string replacements, based on simple lists and a map from field names to fields indices.

def modifyTuple(record):
  if record[0] == True:
    record[1] = "VALUE1"
  elif record[2] == "SPECIAL":
    record[3] = "VALUE2"

This code is generated and used (i have a function which returns the modified transformation function, after internally redefining and compiling it using exec). It seems to work well, but i can't seem to find a way to deal with the case when the function actually is a class method, and therefore has a complementary "self" argument.

How should this be dealt with ? Any ideas ?

Benoît
  • 16,019
  • 7
  • 43
  • 64
  • Does this answer the question? http://stackoverflow.com/questions/6478371/assigning-a-function-to-an-object-attribute – Steve Jessop Sep 07 '13 at 21:21

0 Answers0