简体   繁体   中英

How to generate this code dynamically in Python?

I have a large list of data and another list of patterns. I'm trying to filter data using the patterns. Here's my code using some sample data:

dataList = [ '4334 marked val 5656 bin', 
    '76-67-34 done this 99', 
    'bin ket AZZ3R434 pid' 
]

for data in dataList:
    regexList = [ re.search(r'val ([\d]+) bin', data),
            re.search(r'bin ket ([A-Z\d-]+)\b', data)
        ]

    for reg in regexList:
        if reg:                   #If there's a match
            #...do something...
            break

In the above code in regexlist the 're.search()' part is getting repeated again and again. I want to keep only a list of patterns, something like below:

regexList = [ 'val ([\d]+) bin',
        'bin ket ([A-Z\d-]+)\b'
    ]

And use these patterns one by one with re.search() later. I tried using eval() and exec() both, but just kept getting errors.

I would also like to know whether 'regexList' is getting created again and again under for loop?

I don't see why you would need to do this with eval/exec. Just pass the pattern to re.search inside the loop:

regexList = [
    r'val ([\d]+) bin',
    r'bin ket ([A-Z\d-]+)\b'
]
for pattern in regexList:
    if re.search(pattern, data):
        ...
dataList = [ '4334 marked val 5656 bin', 
    '76-67-34 done this 99', 
    'bin ket AZZ3R434 pid' 
]

regexList = [
         r'val ([\d]+) bin',
         r'bin ket ([A-Z\d-]+'
]
for data in dataList:
    for reg in regexList:
        if  re.search(reg,data):                   #If there's a match
            #...do something...
            break

i would suggest use a "router" (for example Dromeo , i'm author), the point is that a router matches patterns and executes actions if pattern matches, so it is what you need exactly.

Example:

import Dromeo

router = Dromeo()

router.on('4334 marked val {%INT%:val} bin', my_handler)

router.route('4334 marked val 5656 bin')

def my_handler( params ):
   # val is already typecasted to integer
   print(params['data']['val'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM