__dict__ of object, Was: Regular Expression IGNORECASE differentfor findall and split?

Chris c at cdot.de
Thu Sep 8 15:43:26 EDT 2005


Mike Meyer wrote:
> Chris <c at cdot.de> writes:
> 
>>Fredrik Lundh wrote:
>>
>>>Chris <c at cdot.de> wrote:
>>>
>>>
>>>>but more of a basic question following, I was doing the following before:
>>>>
>>>>method = 'split' # came from somewhere else of course
>>>>result = re.__dict__[method].(REGEX, TXT)
>>>>
>>>>precompiling the regex
>>>>
>>>>r = compile(REGEX)
>>>>
>>>>does give an regex object which has the needed methods
>>>>
>>>>print dir(r)
>>>>['__copy__', '__deepcopy__', 'findall', 'finditer', 'match', 'scanner',
>>>>'search', 'split', 'sub', 'subn']
>>>>
>>>>but how do I evaluate them without explicitly calling them?
>>>>
>>>>result = r.__???MAGIC???__[method](TXT)
>>>>
>>>>obviously I am not a Python pro ;)
>>>
>>>I really don't understand why you think you have to write
>>>your RE code that way, but the mechanism you're looking
>>>for is getattr:
>>>    result = getattr(r, method)(TXT)
>>>
>>
>>thanks (also to Steven) for the info, that is exactly what i was
>>looking for.
>>
>>reason is that I built a small UI in which the user may choose if he
>>want to do a split, findall (and maybe later others like match or
>>search). So the method name comes in "from the UI". I could of course
>>use if/elif/else blocks but thought getattr should be shorter and
>>easier. I was not really aware of getattr which I was looking for on
>>other occations before...
> 
> 
> So why is the UI returning strings, instead of code objects of some
> kind?
> 
>         <mike

it is a simple ajax call to a python server doing the re. maybe a bit 
contrived but it was nice to try python/ajax on a for me useful app to 
enable easy tryout of regular expressions. if you are interested check 
out http://cthedot.de/retest/

chris



More information about the Python-list mailing list