osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Better Regex and exception handling for this small code


On 2017-07-12 23:49, Nick Mellor wrote:
> On Wednesday, 12 July 2017 02:32:29 UTC+10, Ganesh Pal  wrote:
>> Dear Python friends
>> 
>> I am trying to open a file and check if there is a pattern  has  changed
>> after the task got completed?
>> 
>> file data:
>> ........................................................
>> 
>> #tail -f /file.txt
>> ..........................................
>> Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = b6b20a65,
>> journal_crc = d2097b00
>> Note: Task completed successfully.
>> Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e,
>> journal_crc = a176af10
>> 
>> 
>>  I  have the below piece of code  but would like to make this better more
>> pythonic , I found regex pattern and exception handling poor here , any
>> quick suggestion in your spare time is welcome.
>> 
>> 
>> #open the existing file if the flag is set and check if there is a match
>> 
>> log_file='/file.txt'
>> flag_is_on=1
>> 
>> data = None
>> with open(log_file, 'r') as f:
>>      data = f.readlines()
>> 
>> 
>> if flag_is_on:
>>     logdata = '\n'.join(data)
>>     reg = "initiator_crc =(?P<ini_crc>[\s\S]*?), journal_crc"
>>     crc = re.findall(re.compile(reg), logdata)
>>     if not crc:
>>         raise Exception("Pattern not found in  logfile")
>> 
>>     checksumbefore = crc[0].strip()
>>     checksumafter = crc[1].strip()
>>     logging.info("checksumbefore :%s and  checksumafter:%s"
>>                       % (checksumbefore, checksumafter))
>> 
>>     if checksumbefore == checksumafter:
>>        raise Exception("checksum not macthing")
>> 
>> I am  on Linux and Python 2.7
>> 
>> Regards,
>> Ganesh
> 
> There's not much need to compile regexes unless you've got *a lot* of them in your code. The first ones are automatically compiled and cached:
> 
> https://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile
> 
I think this is the first time that I've seen someone pass a compiled 
pattern into re.findall.

The usual way is to pass the pattern as a string:

     crc = re.findall(reg, logdata)

If you have a lot of them, or it's in a loop that'll iterate many times, 
it'll be quicker if you compile it first (outside the loop):

     pattern = re.compile(reg)

and then use the compiled pattern's .findall method:

     crc = pattern.findall(logdata)