Skip to content

Commit de37e57

Browse files
committed
Use new traitlets 4.1+ APIs
1 parent 3ff596a commit de37e57

File tree

10 files changed

+111
-81
lines changed

10 files changed

+111
-81
lines changed

ipykernel/comm/comm.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from ipykernel.kernelbase import Kernel
1313

1414
from ipykernel.jsonutil import json_clean
15-
from traitlets import Instance, Unicode, Bytes, Bool, Dict, Any
15+
from traitlets import Instance, Unicode, Bytes, Bool, Dict, Any, default
1616

1717

1818
class Comm(LoggingConfigurable):
@@ -21,15 +21,22 @@ class Comm(LoggingConfigurable):
2121
shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
2222
allow_none=True)
2323
kernel = Instance('ipykernel.kernelbase.Kernel')
24-
def _kernel_default(self):
24+
25+
@default('kernel')
26+
def _default_kernel(self):
2527
if Kernel.initialized():
2628
return Kernel.instance()
2729

2830
iopub_socket = Any()
29-
def _iopub_socket_default(self):
31+
32+
@default('iopub_socket')
33+
def _default_iopub_socket(self):
3034
return self.kernel.iopub_socket
35+
3136
session = Instance('jupyter_client.session.Session')
32-
def _session_default(self):
37+
38+
@default('session')
39+
def _default_session(self):
3340
if self.kernel is not None:
3441
return self.kernel.session
3542

@@ -38,7 +45,9 @@ def _session_default(self):
3845
which to load comm target.""")
3946

4047
topic = Bytes()
41-
def _topic_default(self):
48+
49+
@default('topic')
50+
def _default_topic(self):
4251
return ('comm-%s' % self.comm_id).encode('ascii')
4352

4453
_open_data = Dict(help="data dict, if any, to be included in comm_open")
@@ -49,7 +58,9 @@ def _topic_default(self):
4958

5059
_closed = Bool(True)
5160
comm_id = Unicode()
52-
def _comm_id_default(self):
61+
62+
@default('comm_id')
63+
def _default_comm_id(self):
5364
return uuid.uuid4().hex
5465

5566
primary = Bool(True, help="Am I the primary or secondary Comm?")

ipykernel/comm/manager.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from ipython_genutils.importstring import import_item
1313
from ipython_genutils.py3compat import string_types
14-
from traitlets import Instance, Unicode, Dict, Any
14+
from traitlets import Instance, Unicode, Dict, Any, default
1515

1616
from .comm import Comm
1717

@@ -34,10 +34,15 @@ class CommManager(LoggingConfigurable):
3434
kernel = Instance('ipykernel.kernelbase.Kernel')
3535

3636
iopub_socket = Any()
37-
def _iopub_socket_default(self):
37+
38+
@default('iopub_socket')
39+
def _default_iopub_socket(self):
3840
return self.kernel.iopub_socket
41+
3942
session = Instance('jupyter_client.session.Session')
40-
def _session_default(self):
43+
44+
@default('session')
45+
def _default_session(self):
4146
return self.kernel.session
4247

4348
comms = Dict()

ipykernel/inprocess/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313

1414
# IPython imports
1515
from ipykernel.inprocess.socket import DummySocket
16-
from traitlets import Type, Instance
16+
from traitlets import Type, Instance, default
1717
from jupyter_client.clientabc import KernelClientABC
1818
from jupyter_client.client import KernelClient
1919

2020
# Local imports
2121
from .channels import (
2222
InProcessChannel,
2323
InProcessHBChannel,
24-
2524
)
2625

2726
#-----------------------------------------------------------------------------
@@ -50,11 +49,12 @@ class InProcessKernelClient(KernelClient):
5049
#--------------------------------------------------------------------------
5150
# Channel management methods
5251
#--------------------------------------------------------------------------
53-
54-
def _blocking_class_default(self):
52+
53+
@default('blocking_class')
54+
def _default_blocking_class(self):
5555
from .blocking import BlockingInProcessKernelClient
5656
return BlockingInProcessKernelClient
57-
57+
5858
def get_connection_info(self):
5959
d = super(InProcessKernelClient, self).get_connection_info()
6060
d['kernel'] = self.kernel

ipykernel/inprocess/ipkernel.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from IPython.core.interactiveshell import InteractiveShellABC
1111
from ipykernel.jsonutil import json_clean
12-
from traitlets import Any, Enum, Instance, List, Type
12+
from traitlets import Any, Enum, Instance, List, Type, default
1313
from ipykernel.ipkernel import IPythonKernel
1414
from ipykernel.zmqshell import ZMQInteractiveShell
1515

@@ -52,21 +52,25 @@ class InProcessKernel(IPythonKernel):
5252
control_stream = Any()
5353
_underlying_iopub_socket = Instance(DummySocket, ())
5454
iopub_thread = Instance(IOPubThread)
55-
def _iopub_thread_default(self):
55+
56+
@default('iopub_thread')
57+
def _default_iopub_thread(self):
5658
thread = IOPubThread(self._underlying_iopub_socket)
5759
thread.start()
5860
return thread
59-
61+
6062
iopub_socket = Instance(BackgroundSocket)
61-
def _iopub_socket_default(self):
63+
64+
@default('iopub_socket')
65+
def _default_iopub_socket(self):
6266
return self.iopub_thread.background_socket
63-
67+
6468
stdin_socket = Instance(DummySocket, ())
6569

6670
def __init__(self, **traits):
6771
super(InProcessKernel, self).__init__(**traits)
6872

69-
self._underlying_iopub_socket.on_trait_change(self._io_dispatch, 'message_sent')
73+
self._underlying_iopub_socket.observe(self._io_dispatch, names=['message_sent'])
7074
self.shell.kernel = self
7175

7276
def execute_request(self, stream, ident, parent):
@@ -119,7 +123,7 @@ def _redirected_io(self):
119123

120124
#------ Trait change handlers --------------------------------------------
121125

122-
def _io_dispatch(self):
126+
def _io_dispatch(self, change):
123127
""" Called when a message is sent to the IO socket.
124128
"""
125129
ident, msg = self.session.recv(self.iopub_socket, copy=False)
@@ -128,20 +132,25 @@ def _io_dispatch(self):
128132

129133
#------ Trait initializers -----------------------------------------------
130134

131-
def _log_default(self):
135+
@default('log')
136+
def _default_log(self):
132137
return logging.getLogger(__name__)
133138

134-
def _session_default(self):
139+
@default('session')
140+
def _default_session(self):
135141
from jupyter_client.session import Session
136142
return Session(parent=self, key=b'')
137143

138-
def _shell_class_default(self):
144+
@default('shell_class')
145+
def _default_shell_class(self):
139146
return InProcessInteractiveShell
140147

141-
def _stdout_default(self):
148+
@default('stdout')
149+
def _default_stdout(self):
142150
return OutStream(self.session, self.iopub_thread, u'stdout')
143151

144-
def _stderr_default(self):
152+
@default('stderr')
153+
def _default_stderr(self):
145154
return OutStream(self.session, self.iopub_thread, u'stderr')
146155

147156
#-----------------------------------------------------------------------------

ipykernel/inprocess/manager.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright (c) IPython Development Team.
44
# Distributed under the terms of the Modified BSD License.
55

6-
from traitlets import Instance, DottedObjectName
6+
from traitlets import Instance, DottedObjectName, default
77
from jupyter_client.managerabc import KernelManagerABC
88
from jupyter_client.manager import KernelManager
99
from jupyter_client.session import Session
@@ -24,11 +24,14 @@ class InProcessKernelManager(KernelManager):
2424
allow_none=True)
2525
# the client class for KM.client() shortcut
2626
client_class = DottedObjectName('ipykernel.inprocess.BlockingInProcessKernelClient')
27-
def _blocking_class_default(self):
27+
28+
@default('blocking_class')
29+
def _default_blocking_class(self):
2830
from .blocking import BlockingInProcessKernelClient
2931
return BlockingInProcessKernelClient
3032

31-
def _session_default(self):
33+
@default('session')
34+
def _default_session(self):
3235
# don't sign in-process messages
3336
return Session(key=b'', parent=self)
3437

ipykernel/ipkernel.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,24 @@ def __init__(self, **kwargs):
9191
'text': "pandas",
9292
'url': "http://pandas.pydata.org/pandas-docs/stable/",
9393
},
94-
], config=True)
94+
]).tag(config=True)
9595

9696
# Kernel info fields
9797
implementation = 'ipython'
9898
implementation_version = release.version
9999
language_info = {
100-
'name': 'python',
101-
'version': sys.version.split()[0],
102-
'mimetype': 'text/x-python',
103-
'codemirror_mode': {'name': 'ipython',
104-
'version': sys.version_info[0]},
105-
'pygments_lexer': 'ipython%d' % (3 if PY3 else 2),
106-
'nbconvert_exporter': 'python',
107-
'file_extension': '.py'
108-
}
100+
'name': 'python',
101+
'version': sys.version.split()[0],
102+
'mimetype': 'text/x-python',
103+
'codemirror_mode': {
104+
'name': 'ipython',
105+
'version': sys.version_info[0]
106+
},
107+
'pygments_lexer': 'ipython%d' % (3 if PY3 else 2),
108+
'nbconvert_exporter': 'python',
109+
'file_extension': '.py'
110+
}
111+
109112
@property
110113
def banner(self):
111114
return self.shell.banner

ipykernel/kernelapp.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
from zmq.eventloop.zmqstream import ZMQStream
1717

1818
from IPython.core.application import (
19-
BaseIPythonApplication, base_flags, base_aliases, catch_config_error
19+
BaseIPythonApplication, base_flags, base_aliases, catch_config_error
2020
)
2121
from IPython.core.profiledir import ProfileDir
2222
from IPython.core.shellapp import (
23-
InteractiveShellApp, shell_flags, shell_aliases
23+
InteractiveShellApp, shell_flags, shell_aliases
2424
)
2525
from IPython.utils import io
2626
from ipython_genutils.path import filefind, ensure_dir_exists
27-
from traitlets import (
28-
Any, Instance, Dict, Unicode, Integer, Bool, DottedObjectName, Type,
29-
)
27+
from traitlets import (Any, Instance, Dict, Unicode, Integer, Bool,
28+
DottedObjectName, Type, default)
3029
from ipython_genutils.importstring import import_item
3130
from jupyter_core.paths import jupyter_runtime_dir
3231
from jupyter_client import write_connection_file
@@ -38,7 +37,7 @@
3837
from .ipkernel import IPythonKernel
3938
from .parentpoller import ParentPollerUnix, ParentPollerWindows
4039
from jupyter_client.session import (
41-
Session, session_flags, session_aliases,
40+
Session, session_flags, session_aliases,
4241
)
4342
from .zmqshell import ZMQInteractiveShell
4443

@@ -112,7 +111,7 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,
112111
poller = Any() # don't restrict this even though current pollers are all Threads
113112
heartbeat = Instance(Heartbeat, allow_none=True)
114113
ports = Dict()
115-
114+
116115
subcommands = {
117116
'install': (
118117
'ipykernel.kernelspec.InstallIPythonKernelSpecApp',
@@ -122,7 +121,9 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp,
122121

123122
# connection info:
124123
connection_dir = Unicode()
125-
def _connection_dir_default(self):
124+
125+
@default('connection_dir')
126+
def _default_connection_dir(self):
126127
return jupyter_runtime_dir()
127128

128129
@property
@@ -132,7 +133,6 @@ def abs_connection_file(self):
132133
else:
133134
return self.connection_file
134135

135-
136136
# streams, etc.
137137
no_stdout = Bool(False, help="redirect stdout to the null device").tag(config=True)
138138
no_stderr = Bool(False, help="redirect stderr to the null device").tag(config=True)
@@ -153,7 +153,7 @@ def abs_connection_file(self):
153153

154154
def init_crash_handler(self):
155155
sys.excepthook = self.excepthook
156-
156+
157157
def excepthook(self, etype, evalue, tb):
158158
# write uncaught traceback to 'real' stderr, not zmq-forwarder
159159
traceback.print_exception(etype, evalue, tb, file=sys.__stderr__)
@@ -241,7 +241,7 @@ def init_sockets(self):
241241
self.control_socket.linger = 1000
242242
self.control_port = self._bind_socket(self.control_socket, self.control_port)
243243
self.log.debug("control ROUTER Channel on port: %i" % self.control_port)
244-
244+
245245
self.init_iopub(context)
246246

247247
def init_iopub(self, context):
@@ -253,7 +253,6 @@ def init_iopub(self, context):
253253
self.iopub_thread.start()
254254
# backward-compat: wrap iopub socket API in background thread
255255
self.iopub_socket = self.iopub_thread.background_socket
256-
257256

258257
def init_heartbeat(self):
259258
"""start the heart beating"""
@@ -312,9 +311,9 @@ def init_io(self):
312311
if self.displayhook_class:
313312
displayhook_factory = import_item(str(self.displayhook_class))
314313
sys.displayhook = displayhook_factory(self.session, self.iopub_socket)
315-
314+
316315
self.patch_io()
317-
316+
318317
def patch_io(self):
319318
"""Patch important libraries that can't handle sys.stdout forwarding"""
320319
try:
@@ -388,7 +387,7 @@ def init_shell(self):
388387
self.shell = getattr(self.kernel, 'shell', None)
389388
if self.shell:
390389
self.shell.configurables.append(self)
391-
390+
392391
def init_extensions(self):
393392
super(IPKernelApp, self).init_extensions()
394393
# BEGIN HARDCODED WIDGETS HACK
@@ -434,7 +433,6 @@ def initialize(self, argv=None):
434433
def start(self):
435434
if self.subapp is not None:
436435
return self.subapp.start()
437-
438436
if self.poller is not None:
439437
self.poller.start()
440438
self.kernel.start()

0 commit comments

Comments
 (0)