0

I am reading a text file with many thousands of records (one per line). Each starts with 5 characters, first is alpha followed by 4 numeric e.g. H0100.

The list of available starting labels is defined so not random although the last numeric value could be anything from 1-9 (sometimes last two could be 1-9).

so a class of record could be any of the following H0100, H0101.....H0109

There are instances when the last to numbers have a range such that there would be 100 combinations.

I would like to create a function to process each record such that if the first 3 or 4 characters in the label are identified the same function is called. The reason being is the records are essentially identical in format and can be processed the same even though the last one or two values in the label changes.

Here is how I am doing it at the moment for the case H0100, H0101.....H0109

    for line in (file):
            #turn the first 5 characters into an object
            obj = eval (line[0:5]) 
            obj (line)

    def H0100(line)
          .....process line

    #Just point all the alternative to the one function
    H0101 = H0102 = H0103 = H0104 = H0105 = H0106 = H0107 = H0108 = H0109 = H0100

This works but it does not look very elegant and I am sure it is not Pythonic! Anyone have any suggestions as to how I could make this simpler/more elegant? I have 40-50 labels with one character change and few with two character changes as such have to point 100 alternatives to one function.

Many thank.

ear9mrn
  • 11
  • 2
  • Per the linked duplicate, make a dict with your function references, and then use a statement like `processed_line = myCommandDict[line_header](line)`, where you use the `line_header` to select the appropriate function in your dict. – tehhowch Aug 22 '18 at 20:38

2 Answers2

2

My personal preference would be to use a dictionary of functions keyed by the prefix that is relevant.

def H0100(line)
      .....process line

funcs = { 'H010' : H0100,
          'H020' : H0200,
          ... etc...
        }

then

for line in file:
    f = funcs[ line[:4] ]
    f(line) 

This works as is if the prefix length is fixed. Variable length prefixes requires a slight modification of this approach. You could implement this same strategy by peeking at the line and determining if it's three-letter prefix identifier or four-letter and then use the appropriate lookup table for that case. It's hard to propose a concrete implementation there without more details.

Jon Sorenson
  • 136
  • 5
1

You can use python's built-in exec function.

for i in range(101,110):
    print("H0%d = H0100" % i)   # Just so you see exactly what gets executed
    exec( "H0%d = H0100" % i)   # Perform each assignment

However, I don't think you want to create a bunch of variables with unique names (it's certainly non-pythonic, and is bad practice in general, for any language). You are better off using a data structure such as a List or a Dictionary.

vasia
  • 1,093
  • 7
  • 18