How do I access classes and get a dir() of available actions?

2024/11/16 17:35:06

I have been trying to get access to available functions for a Match Object from re.search. I am looking for a way to do that similar to how I could do dir(str) and I can find .replace.

This is my dir() for the re module and some of the things I have tried:

>>> import re
>>> m = re.search('x', 'x')
>>> dir(re)
['DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 'M', 'MULTILINE', 
'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 'VERBOSE', 'X', 
'_MAXCACHE', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 
'__package__', '__version__', '_alphanum', '_cache', '_cache_repl', 
'_compile', '_compile_repl', '_expand', '_locale', '_pattern_type', 
'_pickle', '_subx', 'compile', 'copy_reg', 'error', 'escape', 'findall', 
'finditer', 'match', 'purge', 'search', 'split', 'sre_compile', 
'sre_parse', 'sub', 'subn', 'sys', 'template']

I want to get to this menu without having to create the Match Object:

>>> dir(m)
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__doc__', 
'__format__', '__getattribute__', '__hash__', '__init__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', 
'__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 
'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 
'span', 'start', 'string']

Is there a way to go from dir(m) and be able to find out how to go up a level? That way I can trace my way back to the module and functions. Like if I was to do dir(re.search.func_dict), how can I find out what I need to type into dir() to get back a list that included func_dict()?

>>> dir(re.Match)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Match'

I see this thing about _sre.SRE._Match but how do I found out where that lives so I can get more information on it?

>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, 'm': <_sre.SRE_Match 
object at 0xb7b0d8a8>, '__package__': None, 're': <module 're' from 
'/usr/lib/python2.7/re.pyc'>, '__name__': '__main__', '__doc__': None}

I have tried to use the inspect function, but the only function that gives me any info is inspect.getmembers(re) but it's just a ton of stuff I don't understand.

I am a complete newbie following a learn as you program course and I have no bases knowledge in Python other than some programs I have written. I have been trying to use dir() and help() a lot to learn. I greatly appreciate your help.

Answer

In Python 3.7, re.Match is the type of the objects returned by re.match. See bpo30397.

In previous versions, re.Match is not defined. If you wanted a reference to the type of match objects you could get it with

Match = type(re.match('',''))

You can use dir on either the Match type or on a match object to list its attributes and methods.

https://en.xdnf.cn/q/120300.html

Related Q&A

Python - IndexError: list index out of range

Why would data[entities][urls][0][expanded_url] would produce IndexError: list index out of range error? I understand what this error means but cant see why? perhaps too sleepy at 2 am? Please helpd…

Python: Use Regular expression to remove something

Ive got a string looks like thisABC(a =2,b=3,c=5,d=5,e=Something)I want the result to be likeABC(a =2,b=3,c=5)Whats the best way to do this? I prefer to use regular expression in Python.Sorry, somethi…

Python delete row in file after reading it

I python 2.7 I am reading data from file in while loop. When I successfully read row, I would like to delete this row from a file, but I dont know how to do it - Efficient way so i dont waste to much o…

Trying to keep the same type after saving a dataframe in a csv file

When I try to get my dataframe out of the csv file the type of the data changed. Is there a way I can avoid this?

Merge blocks of images to produce new image

Hi is there a way of merging specific blocks from multiple images of same size(say 100x100) and putting them together in a new image. To be more specific, consider I have a set of images which have bee…

Removing Characters from python Output

I did alot of work to remove the characters from the spark python output like u u u" [()/" which are creating problem for me to do the further work. So please put a focus on the same .I have …

How to make a tkinter entry default value permanent

I am writing a program in python that will take in specific formats, a Phone number and dollar/cent values. How can I make tkinter have default value which is permanent, not deletable. For example (XXX…

distribute value in buckets

Consider below DF, I have an input number=4 to be inserted evenly in different hour buckets.p_hourly mins 0 2020-09-10 07:00:00 60.0 1 2020-09-10 08:00:00 60.0 2 2020-09-10 09:00:00 60…

for loop over list break and continue

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entr…

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…