Home > other >  Express a function with a lambda function functionally equivalent to it, and pass the return of that
Express a function with a lambda function functionally equivalent to it, and pass the return of that

Time:10-18

import re, datetime

def modify_function(match):
    input_text_substring = match.group()
    current_year = datetime.datetime.today().strftime('%Y')
    input_text_substring = re.sub(r"[\s|]*de[\s|]*"   current_year, "", input_text_substring, 1)
    return input_text_substring

#Example input cases
input_text = '[26 -- 31] de 10 del 200 de 2022' #example 1
input_text = '[26 -- 31] de 12 del 206 del 2022' #example 2
input_text = '[06 -- 11] del 09 del ano 2020 del 2022' #example 3

#modular regex parts
possible_year_num = r"\d*" #I need one or more numbers (one or more numeric digits but never any number)
current_year = datetime.datetime.today().strftime('%Y')
month_context_regex = r"[\s|]*(?:del[\s|]*mes|de[\s|]*el[\s|]*mes|de[\s|]*mes|del|de[\s|]*el|de)[\s|]*"
year_context_regex = r"[\s|]*(?:del[\s|]*año|de[\s|]*el[\s|]*año|de[\s|]*año|del[\s|]*ano|de[\s|]*el[\s|]*ano|de[\s|]*ano|del|de[\s|]*el|de)[\s|]*"

#I combine those modular regex expressions to build a regex that serves to identify the substring in which the replacement must be performed
identity_replacement_case_regex = r"\[\d{2}"   " -- "   r"\d{2}]"   month_context_regex   r"\d{2}"   year_context_regex   possible_year_num   year_context_regex   current_year

input_text = re.sub(identity_replacement_case_regex, modify_function, input_text)

print(repr(input_text))  # --> output

The correct outputs should look like this:

'[26 -- 31] de 10 del 200' #for example 1
'[26 -- 31] de 12 del 206' #for example 2
'[06 -- 11] del 09 del ano 2020' #for example 3

I want to achieve the same thing but instead of having to call an independent in this case, the function called modify_function, would require that within the same line input_text = re.sub(identity_replacement_case_regex, modify_function, input_text) the result from a lambda function can be passed as a replacement argument.

enter image description here

The code of the program that is in the question works perfectly but... for practical reasons it would need not to have to resort to an external function to the main code, so that argument would need to be obtained from a lambda function lambda input_text, current_year: expression that is equivalent running to the function called modify_function

CodePudding user response:

It looks like you want:

lambda m: re.sub(r"[\s|]*de[\s|]*"   datetime.datetime.today().strftime('%Y'), "", m.group(), 1)

You would use that in your code as:

input_text = re.sub(identity_replacement_case_regex, 
                    lambda m: re.sub(r"[\s|]*de[\s|]*"   datetime.datetime.today().strftime('%Y'), "", m.group(), 1),
                    input_text)
  • Related