[MRG +1] bpython support (followup on #270) #1100
Conversation
example: cat <<EOF >> ~/.scrapy.cfg [settings] console = python EOF (closes scrapy#270, scrapy#1100)
is it possible to add tests? and can we use a environment variable? |
Good question. I'm a bit stupid here. What should a test for this look like? |
Yeah but the question is - what output to validate. No? The only thing that changes is the interactive shell. Even if we had an interactive shell in the testsuite, how would the output differ if it went through IPython instead of bpython or plain python? |
/cc @kmike do you know how we can achieve this? |
Regarding tests: one way to test it is to refactor the code. Create functions I'm fine with merging this feature without tests though. To clarify: I haven't checked this PR. |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
This seems sensible, I have tried to make it that way. |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
It is taking better form. Can we decorate the On Don't forget to add documentation. |
Right, the test thingy is ugly. I also don't know if
I was trying to! |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Doc 4th paragraph I don't think explaining why bpython is important. Just with adding a link, mentioning we detect it and the order this is done; would be enough. Decorator: >>> def _embed_regex(regex, text):
... import re
... @wraps(_embed_regex)
... def wrapper(regex=regex, text=text):
... return re.findall(regex, text)
... return wrapper
...
>>> func = _embed_regex(r'a', 'abc')
>>> func.__name__
'_embed_regex'
>>> func()
['a']
>>> func(r'b')
['b']
# ---
>>> def _embed_fail():
... import nonexistent
... @wraps(_embed_fail)
... def wrapper():
... return
... return wrapper
...
>>> func = _embed_fail()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 2, in _embed_fail
ImportError: No module named nonexistent |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Awwright, added some docs! |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
def _embed_ipython_shell(namespace={}, banner='', **kw):
"""Start an IPython Shell"""
try:
from IPython.terminal.embed import InteractiveShellEmbed
from IPython.terminal.ipapp import load_default_config
except ImportError:
from IPython.frontend.terminal.embed import InteractiveShellEmbed
from IPython.frontend.terminal.ipapp import load_default_config
@wraps(_embed_ipython_shell)
def wrapper(namespace=namespace, banner='', **kw):
config = load_default_config()
shell = InteractiveShellEmbed(banner1=banner, user_ns=namespace, config=config)
return shell()
return wrapper Then |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Ah! Thank you so much. In my mind I thought I needed control-flow back after the wrapper, big fail. This one is on me Anything else I failed? |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
Naming the kwarg and the constant the same feels so dangerous Maybe something like this? KNOWN_SHELLS = {...}
def f(shells, known_shells=None):
if known_shells is None:
known_shells = KNOWN_SHELLS.copy() I don't know about environment variables; thoughts @kmike ? |
self.assertEqual(shell, None) | ||
|
||
shell = get_shell_embed_func(['invalid','python']) | ||
assert callable(shell) |
nramirezuy
Jul 17, 2015
Contributor
This should be self.assertTrue(callable(shell))
This should be self.assertTrue(callable(shell))
Oh, this is a mistake from moving |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be used as the interactive python console, if available. # (default is ipython, fallbacks in the order listed above) shell = python EOF (closes scrapy#270, scrapy#1100)
@nyov we usually avoid mutable default arguments due to this: http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be tried as the interactive python console # (in above order, unless set here). shell = python EOF (closes scrapy#270, scrapy#1100, scrapy#1301)
Okay, OrderedDict seemed simpler than using tuples :) Here's the update, |
Looks good, and works good in Mac OS X with python 2.7.6 and pypy 2.6.0. However.. the screen is being cleared at the start and I'm not being able to see the scrapy banner. Can we prevent that happening? Tomorrow will be the sprints at EuroPython 2015 and bpython and scrapy will be there, so maybe I can ask them about that |
Nice, hope to see some video coverage of the event later. To be honest, I didn't give much thought about bpython's screen clearing, but it does make things a bit inconvenient. I couldn't find anything from a quick look about how to maybe pull in custom text into bpython's window at start. |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be tried as the interactive python console # (in above order, unless set here). shell = python EOF (closes #270, #1100, #1301)
Current coverage is
|
I don't know if you have to add more test; so the bot smiles at you. |
I didn't know the correct way to add a commit to a foreign PR, so maybe I did it wrong, sorry! I did another PR (#1444) that takes the help that was being manually printed on console and builds a 'banner' for passing it to the different shells. Tested on standard python, ipython and bpython and works as expected. |
@nramirezuy , yeah I knew that would come once I saw the bot :( @cyberplant, thanks that looks nice! Want me to pull the commit in here, or do you want to keep going with the whole thing in your PR? |
As you wish.. for me, it's the same. |
If you're willing to see it through, I'll be glad to let you have it. (And you have the shorter wire to the merge decision people, looks like, so that's an advantage ;) Too bad I can't hand over the PR or point it at your branch, so yours will be the followup on the followup. |
Understood. So.. good people, come with me, the party continues here |
Adds support for configuration of shells from scrapy.cfg and SCRAPY_PYTHON_SHELL. config snippet: cat <<EOF >> ~/.scrapy.cfg [settings] # shell can be one of ipython, bpython or python; # to be tried as the interactive python console # (in above order, unless set here). shell = python EOF (closes scrapy#270, scrapy#1100, scrapy#1301)
This is a rebase and revamp of #270
I added support for setting the console/interpreter from scrapy.cfg,
hopefully this might make it mergeable now.
Couldn't find a good place to document this in the docs, so I did it in code :P :P :P