[Python-ideas] dir with a glob?

Mathias Panzenböck grosser.meister.morti at gmx.net
Thu Jun 30 15:09:46 CEST 2011


I would rather add a grep method to regular expression objects:

	def grep(self,sequence):
		for s in sequence:
			if self.search(s):
				yield s

And a global function to the re module:

	def grep(regex,sequence,flags=0):
		return re.compile(regex,flags).grep(sequence)


Then you could do:

	re.grep("^i", dir(sp.fft))

Yes, it would be more to type but it would be more general.

OT: I think it would be good if regular expressions would be callable with this method:

	def __call__(self,s):
		return self.search(s) is not None

Then one could use it in a filter expression:

	filter(re.compile("^i"), dir(sp.fft))

On 06/30/2011 01:38 PM, Sturla Molden wrote:
> Often when exploring an object with the 'dir' function, particularly in large packages like SciPy, I
> find that I need to filter the outout. Since a dir reminds me of a dos 'dir' or linux 'ls', a glob
> feels like the most natural to use.
>
> For example, none of these would work:
>
>  >>> dir(sp.fft.i*) # syntax error
>  >>> dir('sp.fft.i*') # returns the attributes a string
>
> I believe a new dir functions is needed, or a change in the behviour of the current version.
>
> Of course a 'glob aware dir function' can be implemented by monitoring the call stack (cf.
> sys._getframe().f_back), but I think the problem is general enough to warrant an official sultion.
>
> Sturla




More information about the Python-ideas mailing list