Executing Python Code in IPython Kernel - ipython

I want to duplicate ipython notebook capability in Emacs / Pymacs; and I need some direction for a simple code that can 1) send python / "magics" code to a ipython kernel 2) receive the display output, as a string. I found this comment by minrk, the "ipython kernel" example did not work, it gave "ImportError: No module named zmq.blockingkernelmanager".
I had better luck with one his other pointers, finally I landed at ipython-1.1.0/IPython/kernel/inprocess/tests/test_kernel.py, I ripped out a minimal part, and coded an Emacs extension called pytexipy-notebook. It's on Github
goo.gl/kQzJW1
If anyone knows of better examples, such as connecting to an existing (out of process), I'd like to hear about these.
Thanks in advance,

Here is a sample for ipython 3.0.
from IPython.testing.globalipapp import get_ipython
from IPython.utils.io import capture_output
ip = get_ipython()
def run_cell(cmd):
with capture_output() as io:
res = ip.run_cell(content)
print 'suc', res.success
print 'res', res.result
res_out = io.stdout
print 'res out', res_out
content = "print (111+222)"
run_cell(content)
content = "alsdkjflajksf"
run_cell(content)
I will soon update
https://github.com/burakbayramli/emacs-ipython

Related

How to get clickable links to functions and classes on VS code for Mac

Sorry if the question is not entirely clear. Will add details if unclear.
PrintPlugin is a class from another python file I have imported. I used to get clickable links on this on VS code. Meaning, if I cmd+click on PrintPlugin it would go to that class on the source Python file.
My bottom bar used to look like this when it was working.
Now it looks like this
The difference is that I was having the Python 3.9.5 64-bit ('base':conda) earlier.
Everything else works fine. I am on my virtual environment too. I am not sure what else could be causing this. Will add any details required.
Edit:
I have added some reproducible code. This differs from the original image because the original was work related and I can't post it here. But I'm still running this in the same folder and in the same virtual environment, so it has the same problem (that I cannot cmd+click on Multiply() in process.py and go to the class on plugin.py)
File process.py
import numpy as np
from plugin import Multiply
a = np.array([[2,3],
[3,4]])
b = np.array([[5,6],
[7,8]])
result = Multiply()
print(result.multiply(a,b))
File plugin.py
import numpy as np
class Multiply():
def __init__(
self,
a = np.array([[1,1],
[1,1]]),
b = np.array([[1,1],
[1,1]])
):
return
def multiply(self,a,b):
c = a # b
return c
Output when I do python process.py
>>> [[31 36]
[43 50]]

How to get PyTest fixtures to autocomplete in PyCharm (type hinting)

I had a bear of a time figuring this out, and it was really bugging me, so I thought I'd post this here in case anyone hit the same problem...
(and the answer is so dang simple it hurts :-)
The Problem
The core of the issue is that sometimes, not always, when dealing with fixtures in PyTest that return objects, when you use those fixtures in a test in PyCharm, you don't get autocomplete hints. If you have objects with large numbers of methods you want to reference while writing a test, this can add a lot of overhead and inconvenience to the test writing process.
Here's a simple example to illustrate the issue:
Let's say I've got a class "event_manager" that lives in:
location.game.events
Let's further say that in my conftest.py file (PyTest standard thing for the unfamiliar), I've got a fixture that returns an instance of that class:
from location.game.events import event_manager
...
#pytest.fixture(scope="module")
def event_mgr():
"""Creates a new instance of event generate for use in tests"""
return event_manager()
I've had issues sometimes, (but not always - I can't quite figure out why) with classes like this where autocomplete will not work properly in the test code where I use the fixture, e.g.
def test_tc10657(self, evt_mgr):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This does not offer autocomplete hints when you type ".")
So the answer is actually quite simple, once you review type hinting in PyCharm:
http://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html
Here's how to fix the above test code so that autocomplete works properly:
from location.game.events import event_manager
...
def test_tc10657(self, evt_mgr: event_manager):
"""Generates a Regmod and expects filemod to be searchable on server"""
evt_mgr.(This DOES offer hints when you type "." Yay!)
Notice how I explicitly type the fixture as an input parameter of type event_manager.
Also if you add a docstring to a function and specify the type of the the parameters, you will get the code completion for those parameters.
For example using pytest and Selenium:
# The remote webdriver seems to be the base class for the other webdrivers
from selenium.webdriver.remote.webdriver import WebDriver
def test_url(url, browser_driver):
"""
This method is used to see if IBM is in the URL title
:param WebDriver browser_driver: The browser's driver
:param str url: the URL to test
"""
browser_driver.get(url)
assert "IBM" in browser_driver.title
Here's my conftest.py file as well
import pytest
from selenium import webdriver
# Method to handle the command line arguments for pytest
def pytest_addoption(parser):
parser.addoption("--driver", action="store", default="chrome", help="Type in browser type")
parser.addoption("--url", action="store", default='https://www.ibm.com', help="url")
#pytest.fixture(scope='module', autouse=True)
def browser_driver(request):
browser = request.config.getoption("--driver").lower()
# yield the driver to the specified browser
if browser == "chrome":
driver = webdriver.Chrome(executable_path='/path/to/chromedriver')
else:
raise Exception("No driver for browser " + browser)
yield driver
driver.quit()
#pytest.fixture(scope="module")
def url(request):
return request.config.getoption("--url")
Tested using Python 2.7 and PyCharm 2017.1. The docstring format is reStructuredText and the "Analyze Python code in docstrings" checkbox is checked in settings.

python: Imported class does not change, when edited, saved and reimported

I'm new to Python and trying to understand classes. Not sure the following error is coming from the use of my IDE, which is Spyder, or if it is intended behaviour.
I define a class message in the file C:\mydir\class_def.py. Here is what the file contains:
class message:
def __init__(self,msg1,msg2):
self.msg1 = msg1
self.msg2 = msg2
I have another script were I want to execute code, called execute.py. In this script I import the class and make an instance of the class object. Here is the code from the script execute.py:
import os
os.chdir('C:\mydir')
from class_def import message
message_obj = message('Hello','world')
So far no problems!
Then I edit class_def.py to the following:
class message:
def __init__(self,msg1):
self.msg1 = msg1
and edit execute.py to match the new class, so removing one input tomessage:
import os
os.chdir('C:\mydir')
from class_def import message
message_obj = message('Hello')
and I get the following error:
TypeError: __init__() takes exactly 3 arguments (2 given)
It seems like Python keeps the old version of class_def.py and does not import the new one, even though it is saved.
Is this normal behaviour or is Spyder doing something funny?
If you have a .pyc file such as class_def.pyc, delete it.
I'd remove all .pyc files in your working directory and then try again. If that doesn't work, maybe you're not using the module you think you are? To be certain try something like:
import myModule
print myModule.__file__ #This will give you the path to the .pyc file your program loaded
#or
import myModule
import os
print os.path.dirname(myModule.__file__)
try those out so you can be certain that you're actually using the file you're modifying. Hope that helps!

Scrapy cmdline.execute stops script

When I call
cmdline.execute("scrapy crawl website".split())
print "Hello World"
it stops the script after cmdline.execute, and doesn't run the rest of the script and print "Hello World". How do I fix this?
By taking a look at the execute function in Scrapy's cmdline.py, you'll see the final line is:
sys.exit(cmd.exitcode)
There really is no way around this sys.exit call if you call the execute function directly, at least not without changing it. Monkey-patching is one option, albeit not a good one! A better option is to avoid calling the execute function entirely, and instead use the custom function below:
from twisted.internet import reactor
from scrapy import log, signals
from scrapy.crawler import Crawler as ScrapyCrawler
from scrapy.settings import Settings
from scrapy.xlib.pydispatch import dispatcher
from scrapy.utils.project import get_project_settings
def scrapy_crawl(name):
def stop_reactor():
reactor.stop()
dispatcher.connect(stop_reactor, signal=signals.spider_closed)
scrapy_settings = get_project_settings()
crawler = ScrapyCrawler(scrapy_settings)
crawler.configure()
spider = crawler.spiders.create(name)
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run()
And you can call it like this:
scrapy_crawl("your_crawler_name")
I have just tried the following code, and it works for me:
import os
os.system("scrapy crawl website")
print("Hello World")
One can run subprocess.call. For example on Windows with powershell:
import subprocess
subprocess.call([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'-ExecutionPolicy',
'Unrestricted',
'scrapy crawl website -o items.json -t json'])

Turn off auto-closing parentheses in ipython

I stay up-to-date with ipython's dev branch (because ipython is pretty much the most awesome thing ever). Fairly recently (before yesterday's awesome ipython 2.0 release) I noticed that it has started to automatically close parentheses, brackets, quotes, etc., as I type them. It happens in both terminal [nothing else I use in terminal does it] and notebook sessions, so I assume it was an intentional choice on the part of the developers. I can respect that other people might like this feature, but it drives me completely nuts.
I can't find any option for it in the configuration files. I can't even google for it, because I don't know what it's called. The only thing that comes up is the different feature of automatic parentheses. I did actually find this question, but that's old, and suggests that the behavior I'm seeing can't happen.
How can I turn this feature off?
[I mostly just use the notebook interface anyway, so just turning it off there would be fine, but I'd prefer to turn it off in both notebooks and ipython sessions at the terminal.]
#minrk's answer is the meat and bones of the fix, but you'll need to wrap it in an initialization callback, at least with IPython-3.1.0. In your custom.js:
require(['base/js/namespace', 'base/js/events'], function(IPython, events) {
events.on('app_initialized.NotebookApp', function() {
IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
});
});
Thanks #Mike for your comment about IPython's RequireJS dependency loading and the pointer to a better formulation at IPython/Jupyter Installing Extensions.
Edit for Jupyter 4.0.x:
The current IPython notebook implementation, Jupyter 4.0.0, revamped JS customizations. It now uses ~/.jupyter/custom/custom.js by default, and you'll need to replace that whole require(... events.on(...)) snippet with just the following in global scope:
IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
Likewise, if you want to use jQuery to manipulate anything, just use the jQuery global directly. For example, I like to hide the fixed header by default, which gives me another 40px of space for my code, which I find a bit more valuable than looking at the Jupyter logo all the time:
jQuery('#header-container').hide();
Edit for Jupyter ≥ 4.0.6 (but < Jupyter Lab):
If the custom.js solution above doesn't work, try adding the following to your ~/.jupyter/nbconfig/notebook.json:
{
"CodeCell": {
"cm_config": {
"autoCloseBrackets": false
}
}
}
The notebook behavior is the result of the CodeMirror autoCloseBrackets plugin. You can turn this off by editing (create it with ipython profile create if you haven't already) ~/.ipython/profile_default/static/custom/custom.js and adding:
if (IPython.CodeCell) {
IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
}
As for the terminal, I don't see the parenthesis behavior you describe. Do you perhaps have a PYTHONSTARTUP defined? IPython executes this file by default, which you can disable by adding to ~/.ipython/profile_default/ipython_config.py:
c.InteractiveShellApp.exec_PYTHONSTARTUP = False
If you want to do it just from python:
from notebook.services.config import ConfigManager
c = ConfigManager()
c.update('notebook', {"CodeCell": {"cm_config": {"autoCloseBrackets": False}}})
This is what works for me in Jupyter 4.0.6:
require(['notebook/js/codecell'], function (codecell) {
codecell.CodeCell.options_default.cm_config.autoCloseBrackets = false;
})
in ~/.jupyter/custom/custom.js.
BTW, If you additionally want to switch off the syntax higlighting of matching parentheses:
codecell.CodeCell.options_default.cm_config.matchBrackets = false;
In the JupyterLab Notebook you can turn off the autoClosingBrackets plugin in the settings menu. Go to Settings --> Advanced Settings Editor and add the following in the User Overrides section:
{
"codeCellConfig": {
"autoClosingBrackets": false
}
}
Screenshot
This worked with JupyterLab 0.32.1 and jupyter_core 4.4.0
The above suggestions didn't worked for me in Jupyter 4.3.0 with Jupyter Notebook 5.0.0
I found that I needed to create a file called ~/.jupyter/custom/custom.js with the following contents:
var cell = Jupyter.notebook.get_selected_cell();
var patch = {
CodeCell: {
cm_config: {
autoCloseBrackets: false,
}
}
}
cell.config.update(patch);
Note that the directory ~/.juypter/custom didn't exist before I did this.
This was hacked together from suggestions in v5.0 docs, and for future readers these are the latest
For Jupyter Notebook 5.1 use the same thing as for 4.2, i.e. put the following snippet into ~/.jupyter/custom/custom.js:
require(['notebook/js/codecell'], function (codecell) {
codecell.CodeCell.options_default.cm_config.autoCloseBrackets = false;
})
I found it was not mentioned in other answers. In my case(OS X, Jupyter 4.2.0), custom.js is located in
~/anaconda/lib/python3.5/site-packages/notebook/static/custom/custom.js
I think it may help somebody like me.
We can do that from jupyter console, try it.