Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make autocall regexp's configurable. #1713

Merged
merged 4 commits into from May 9, 2012

Conversation

bfroehle
Copy link
Contributor

@bfroehle bfroehle commented May 8, 2012

As discussed in #81, there is some (small) demand that that the autocall regular expressions be configurable. For example:

$ ipython --AutocallChecker.exclude_regexp='^[,&^\\|\\*/]|^is |^not |^in |^and |^or ' --autocall=2
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: def f(*args): print args

In [2]: f -1
------> f(-1)
(-1,)

The attached pull request:

  1. Creates a new trait type "CRegExp" which casts a string or regular expression into a compiled regular expression.
  2. Makes the autocall regular expressions, which are only used in AutocallChecker configurable.

@bfroehle
Copy link
Contributor Author

bfroehle commented May 8, 2012

I should mention that even with this change I cannot get f +1 to convert to f(+1), but that seems beyond the scope here.

In [3]: f +1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home/bfroehle/src/ipython/<ipython-input-3-14b45bdc9b4b> in <module>()
----> 1 f +1

TypeError: unsupported operand type(s) for +: 'function' and 'int'

@takluyver
Copy link
Member

Could you add tests for one or both of these parts? Thanks.

@takluyver
Copy link
Member

Test results for commit d1f107f merged into master
Platform: linux2

  • python2.7: OK
  • python3.1: OK (libraries not available: matplotlib pymongo qt wx wx.aui zmq)
  • python3.2: OK (libraries not available: pymongo wx wx.aui)

Not available for testing: python2.6

@bfroehle
Copy link
Contributor Author

bfroehle commented May 8, 2012

Could you add tests for one or both of these parts? Thanks.

Yes, see attached. There might be an easier way to do so, but I ended up creating a new AutocallChecker instance and adding it to the top of the list of checkers in ip.prefilter_manger.

@bfroehle
Copy link
Contributor Author

bfroehle commented May 8, 2012

Test results for commit 9d723ae merged into master
Platform: linux2

  • python2.7: OK
  • python3.2: OK (libraries not available: matplotlib pymongo wx wx.aui)

Not available for testing: python2.6, python3.1

@takluyver
Copy link
Member

Looks good to me, but I'll let someone else have a quick glance over it (ping @fperez, @ellisonbg, @minrk)

@@ -1411,3 +1411,14 @@ def validate(self, obj, value):
if port >= 0 and port <= 65535:
return value
self.error(obj, value)

class CRegExp(TraitType):
"""A trait for a compiled regular expression."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the right description? If the validation attempts to compile, why is the trait itself called 'compiled'? I'm a little confused.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it should have the word "coerced" in there? That's why there is the C prefix, to be consistent with other traits.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what it needs is a more complete docstring: regexps lingo is often ambiguous, because people often use the word 'regex' to describe either the pattern (which is a plain string) or the compiled regex (which is a very different object from a string). So I'd like the dosctring here to unambigously and explicitly indicate what it refers to, what it validates, what the constructor takes, etc. In other traits we don't need as much info b/c everybody has a reasonable idea of what an Int should be. But I think regexes deserve special consideration to avoid confusion in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This accepts either a compiled expression or a string, but the attribute will always be a compiled regular expression object (re.compile(re.compile(pat)) is allowed). Adding 'casting' to the description would be appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A casting compiled regular expression trait.

Accepts both strings and compiled regular expressions. The resulting
attribute will be a compiled regular expression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very clear, thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's perfect, thanks!

@bfroehle
Copy link
Contributor Author

bfroehle commented May 9, 2012

Now with the improved docstring.

fperez added a commit that referenced this pull request May 9, 2012
Make autocall regexp's configurable.

* Creates a new trait type "CRegExp" which casts a string or regular expression into a compiled regular expression.

* Makes the autocall regular expressions, which are only used in AutocallChecker, configurable.

Closes #81.
@fperez fperez merged commit fdd8826 into ipython:master May 9, 2012
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
…nfigurable

Make autocall regexp's configurable.

* Creates a new trait type "CRegExp" which casts a string or regular expression into a compiled regular expression.

* Makes the autocall regular expressions, which are only used in AutocallChecker, configurable.

Closes ipython#81.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants