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

General code cleanup part #1 #29

Merged
merged 9 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ ENV/
# Rope project settings
.ropeproject

README.rst
# Generated README
README.rst

# Extra development notebooks
Untitled*.ipynb
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ install:
- python -m spylon_kernel install --user

script:
- coverage run run_tests.py -vrsx --capture=sys --color=yes
- coverage report -m
- python run_tests.py -vxrs --capture=sys --color=yes
# Ensure installability
- python setup.py sdist
- pip install --no-use-wheel dist/*.tar.gz
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ release: clean ## Make a pypi release of a tagged build
$(SA) $(ENV) && python setup.py sdist register upload

test: ## Make a test run
$(SA) $(ENV) && coverage run run_tests.py -vrsx --capture=sys --color=yes
$(SA) $(ENV) && python run_tests.py -vxrs --capture=sys --color=yes
26 changes: 20 additions & 6 deletions run_tests.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
#!/usr/bin/env python
import sys
import pytest

if __name__ == '__main__':
# call pytest and exit with the return code from pytest so that
# travis will fail correctly if tests fail
sys.exit(pytest.main(sys.argv[1:]))
import coverage
cov = coverage.Coverage()
cov.start()

# Import required modules after coverage starts
import sys
import pytest

# Call pytest and exit with the return code from pytest so that
# CI systems will fail if tests fail.
ret = pytest.main(sys.argv[1:])

cov.stop()
cov.save()
# Save HTML coverage report to disk
cov.html_report()
# Emit coverage report to stdout
cov.report()

sys.exit(ret)
13 changes: 2 additions & 11 deletions spylon_kernel/__main__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
from tornado.ioloop import IOLoop

"""Entrypoint for running the kernel process."""
from spylon_kernel import SpylonKernel
import sys
from tornado.ioloop import IOLoop

if __name__ == '__main__':

# For testing purposes we want to be able to run our kernel with coverage on.
try:
import coverage
coverage.process_startup()
except ImportError:
pass

IOLoop.configure("tornado.platform.asyncio.AsyncIOLoop")
SpylonKernel.run_as_main()
72 changes: 44 additions & 28 deletions spylon_kernel/init_spark_magic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Metakernel magic for configuring and automatically initializing a Spark session."""
import logging
import spylon.spark

from metakernel import Magic
from spylon_kernel.scala_interpreter import init_spark_session
from .scala_interpreter import init_spark

try:
import jedi
Expand All @@ -12,42 +14,60 @@


class InitSparkMagic(Magic):
"""Cell magic that supports configuration property autocompletion and
initializes a Spark session.

Attributes
----------
env : __builtins__
Copy of the Python builtins
log : logging.Logger
Logger for this instance
"""
def __init__(self, kernel):
super(InitSparkMagic, self).__init__(kernel)
self.env = globals()['__builtins__'].copy()
self.env['application_name'] = None
self.env['launcher'] = spylon.spark.launcher.SparkConfiguration()
self.log = logging.Logger("InitSparkMagic")
self.log = logging.Logger(self.__class__.__name__)

def cell_init_spark(self):
"""
%%init_spark - start up a spark context with a custom configuration
"""Starts a SparkContext with a custom configuration defined
using Python code.

Example:
%%init_spark
application_name = 'My Fancy App'
launcher.jars = ["file://some/jar.jar"]
launcher.master = "local[4]"
launcher.conf.spark.executor.cores = 8
Includes a `spylon.spark.launcher.SparkConfiguration` instance
in the variable `launcher`. Looks for an `application_name`
variable to use as the name of the Spark session.

This will evaluate the launcher args using spylon.
Example
-------
%%init_spark
application_name = "My Fancy App"
launcher.jars = ["file://some/jar.jar"]
launcher.master = "local[4]"

Choose a reason for hiding this comment

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

What in the world is this local[4] syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Spark syntax for how many cores to use. local[4] says "Please use 4 of my cores for the application master." local[*] says "TAKE ALL MY CORES PLZ!"

Choose a reason for hiding this comment

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

Can you add that as a comment in the code?

launcher.conf.spark.executor.cores = 8
"""
if "__builtins__" not in self.env:
# __builtins__ get generated after an eval:
eval("1", self.env)

globals_dict = self.env
exec(self.code, globals_dict)
application_name = globals_dict['application_name']
conf = globals_dict['launcher']
init_spark_session(conf, application_name=application_name)
# Evaluate the cell contents as Python
exec(self.code, self.env)
# Use the launcher and application_name as arguments to spylon to
# initialize a spark session
init_spark(conf=self.env['launcher'],
application_name=self.env['application_name'])
# Do not evaluate the cell contents using the kernel
self.evaluate = False
self.kernel.Display()

def get_completions(self, info):
"""Get Python completions."""
# https://github.com/davidhalter/jedi/blob/master/jedi/utils.py
"""Gets Python completions based on the current cursor position
within the %%init_spark cell.

Based on
https://github.com/Calysto/metakernel/blob/master/metakernel/magics/python_magic.py

Parameters
----------
info : dict
Information about the current caret position
"""
if jedi is None:
return []

Expand All @@ -65,8 +85,4 @@ def get_completions(self, info):
before = text[:len(text) - len(name)]
completions = interpreter.completions()
completions = [before + c.name_with_symbols for c in completions]
return [c[info['start']:] for c in completions]


def register_magics(kernel):
kernel.register_magics(InitSparkMagic)
return [c[info['start']:] for c in completions]
Loading