简体   繁体   中英

python decorator for function argument preprocessing

my original problem is I need to strip a string and then split by a operator.

s = " a & b | c & d "
l = map(lambda x : map(lambda x:x.strip() , x.strip().split('&')), s.strip().split('|'))

this is too cumbersome for people to read, so I am thinking to use decorator to do this strip() preprocessing.

here is my current solution, but it's not working!

Update:

query_AND =lambda wl:  '.*'+'.*'.join(wl)+'.*'

def d_query_AND(query_split):
    def d_query_AND_f(query_split):
        return query_AND(query_split)
        #return query_spli
    return d_query_AND_f


@d_query_AND
def query_split(p):
    return [v for v in p.split('&')]


print query_split("asd & asdsa & sadsa")

I have to admit I don't fully understand why you need a decorator.

One relatively easy way to tokenize these expressions is by using a regex:

In [12]: re.findall(r'[^|&\s]+|&|[|]', ' a & b | c & d ')
Out[12]: ['a', '&', 'b', '|', 'c', '&', 'd']

In [13]: re.findall(r'[^|&\s]+|&|[|]', 'asdasd&sdasd| asdsa & asdsda')
Out[13]: ['asdasd', '&', 'sdasd', '|', 'asdsa', '&', 'asdsda']

The regex consists of three components OR-ed together:

  • [^|&\\s]+ matches a sequence of characters other than & , | and whitespace.
  • & matches & .
  • [|] matches | .

If you don't need to capture the operators, the regex can be simplified to r'[^|&\\s]+' .

edit : If you don't want to split on the spaces, as you imply in the comments, the following should work:

In [18]: map(string.strip, re.findall(r'[^|&]+|&|[|]', 'asdasd&sd  asd| asdsa & asdsda'))
Out[18]: ['asdasd', '&', 'sd  asd', '|', 'asdsa', '&', 'asdsda']

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