Skip to content

Commit 1b4ea0c

Browse files
committed
[Python] Format code of JupyROOT sources with ruff
1 parent 9b7be12 commit 1b4ea0c

File tree

16 files changed

+211
-186
lines changed

16 files changed

+211
-186
lines changed

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#-----------------------------------------------------------------------------
1+
# -----------------------------------------------------------------------------
22
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
33
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
4-
#-----------------------------------------------------------------------------
4+
# -----------------------------------------------------------------------------
55

66
################################################################################
77
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #
@@ -15,18 +15,19 @@
1515

1616
from ROOT._jupyroot.helpers import utils
1717

18-
if 'win32' not in sys.platform:
18+
if "win32" not in sys.platform:
1919
from ROOT._jupyroot.helpers import cppcompleter
2020

2121
# Check if we are in the IPython shell
2222
try:
2323
import builtins
2424
except ImportError:
2525
import __builtin__ as builtins # Py2
26-
_is_ipython = hasattr(builtins, '__IPYTHON__')
26+
_is_ipython = hasattr(builtins, "__IPYTHON__")
2727

2828
if _is_ipython:
29-
if 'win32' not in sys.platform:
29+
if "win32" not in sys.platform:
3030
from IPython import get_ipython
31+
3132
cppcompleter.load_ipython_extension(get_ipython())
3233
utils.iPythonize()

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/helpers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#-----------------------------------------------------------------------------
1+
# -----------------------------------------------------------------------------
22
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
33
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
4-
#-----------------------------------------------------------------------------
4+
# -----------------------------------------------------------------------------
55

66
################################################################################
77
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/helpers/cppcompleter.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding:utf-8 -*-
22

3-
#-----------------------------------------------------------------------------
3+
# -----------------------------------------------------------------------------
44
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
55
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
6-
#-----------------------------------------------------------------------------
6+
# -----------------------------------------------------------------------------
77

88
################################################################################
99
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #
@@ -47,8 +47,9 @@
4747
}
4848
"""
4949

50+
5051
class CppCompleter(object):
51-
'''
52+
"""
5253
Completer which interfaces to the TTabCom of ROOT. It is activated
5354
(deactivated) upon the load(unload) of the load of the extension.
5455
@@ -96,7 +97,7 @@ class CppCompleter(object):
9697
>>> comp.deactivate()
9798
>>> for suggestion in comp._completeImpl("TG"):
9899
... print(suggestion)
99-
'''
100+
"""
100101

101102
def __init__(self):
102103
self.hook = None
@@ -107,7 +108,7 @@ def __init__(self):
107108
def activate(self):
108109
self.active = True
109110
if self.firstActivation:
110-
if platform.system() == 'Windows':
111+
if platform.system() == "Windows":
111112
dlOpenRint = 'gInterpreter->LoadFile("libRint.dll");'
112113
else:
113114
utils.declareCppCode('#include "dlfcn.h"')
@@ -120,22 +121,22 @@ def activate(self):
120121
def deactivate(self):
121122
self.active = False
122123

123-
def _getSuggestions(self,line):
124+
def _getSuggestions(self, line):
124125
if self.active:
125126
return self.hook(line)
126127
return []
127128

128-
def _getLastAccessorPos(self,line):
129+
def _getLastAccessorPos(self, line):
129130
accessorPos = -1
130131
for accessor in self.accessors:
131132
tmpAccessorPos = line.rfind(accessor)
132133
if accessorPos < tmpAccessorPos:
133-
accessorPos = tmpAccessorPos+len(accessor)
134+
accessorPos = tmpAccessorPos + len(accessor)
134135
return accessorPos
135136

136137
def _completeImpl(self, line):
137-
line=line.split()[-1]
138-
suggestions = [ str(s) for s in self._getSuggestions(line) ]
138+
line = line.split()[-1]
139+
suggestions = [str(s) for s in self._getSuggestions(line)]
139140
suggestions = filter(lambda s: len(s.strip()) != 0, suggestions)
140141
suggestions = sorted(suggestions)
141142
if not suggestions:
@@ -144,12 +145,12 @@ def _completeImpl(self, line):
144145
# brackets at the end of a line. Jupyter seems to expect functions
145146
# without these brackets to work properly. The brackets of'operator()'
146147
# must not be removed
147-
suggestions = [sugg[:-2] if sugg[-2:] == '()' and sugg != 'operator()' else sugg for sugg in suggestions]
148-
suggestions = [sugg[:-1] if sugg[-1:] == '(' else sugg for sugg in suggestions]
148+
suggestions = [sugg[:-2] if sugg[-2:] == "()" and sugg != "operator()" else sugg for sugg in suggestions]
149+
suggestions = [sugg[:-1] if sugg[-1:] == "(" else sugg for sugg in suggestions]
149150
# If a function signature is encountered, add an empty item to the
150151
# suggestions. Try to guess a function signature by an opening bracket
151152
# ignoring 'operator()'.
152-
are_signatures = "(" in "".join(filter(lambda s: s != 'operator()', suggestions))
153+
are_signatures = "(" in "".join(filter(lambda s: s != "operator()", suggestions))
153154
accessorPos = self._getLastAccessorPos(line)
154155
if are_signatures:
155156
suggestions = [" "] + suggestions
@@ -158,26 +159,27 @@ def _completeImpl(self, line):
158159
# suggestion already contains the variable name, this can happen if
159160
# e.g. there is only one valid completion
160161
if len(suggestions) > 1 or line[:accessorPos] != suggestions[0][:accessorPos]:
161-
suggestions = [line[:accessorPos]+sugg for sugg in suggestions]
162+
suggestions = [line[:accessorPos] + sugg for sugg in suggestions]
162163
return suggestions
163164

164-
def complete(self, ip, event) :
165-
'''
165+
def complete(self, ip, event):
166+
"""
166167
Autocomplete interfacing to TTabCom. If an accessor of a scope is
167168
present in the line, the suggestions are prepended with the line.
168169
That's how completers work. For example:
169170
myGraph.Set<tab> will return "myGraph.Set+suggestion in the list of
170171
suggestions.
171-
'''
172+
"""
172173
return self._completeImpl(event.line)
173174

174175

175176
_cppCompleter = CppCompleter()
176177

178+
177179
def load_ipython_extension(ipython):
178180
_cppCompleter.activate()
179-
ipython.set_hook('complete_command', _cppCompleter.complete, re_key=r"[(.*)[\.,::,\->](.*)]|(.*)")
181+
ipython.set_hook("complete_command", _cppCompleter.complete, re_key=r"[(.*)[\.,::,\->](.*)]|(.*)")
182+
180183

181184
def unload_ipython_extension(ipython):
182185
_cppCompleter.deactivate()
183-

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/helpers/handlers.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding:utf-8 -*-
2-
#-----------------------------------------------------------------------------
2+
# -----------------------------------------------------------------------------
33
# Authors: Danilo Piparo
44
# Omar Zapata <Omar.Zapata@cern.ch> http://oproject.org
5-
#-----------------------------------------------------------------------------
5+
# -----------------------------------------------------------------------------
66

77
################################################################################
88
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #
@@ -21,7 +21,7 @@
2121

2222

2323
class IOHandler(object):
24-
r'''Class used to capture output from C/C++ libraries.
24+
r"""Class used to capture output from C/C++ libraries.
2525
>>> import sys
2626
>>> h = IOHandler()
2727
>>> h.GetStdout()
@@ -31,7 +31,8 @@ class IOHandler(object):
3131
>>> h.GetStreamsDicts()
3232
(None, None)
3333
>>> del h
34-
'''
34+
"""
35+
3536
def __init__(self):
3637
_lib.JupyROOTExecutorHandler_Ctor()
3738

@@ -51,17 +52,18 @@ def EndCapture(self):
5152
_lib.JupyROOTExecutorHandler_EndCapture()
5253

5354
def GetStdout(self):
54-
return _lib.JupyROOTExecutorHandler_GetStdout()
55+
return _lib.JupyROOTExecutorHandler_GetStdout()
5556

5657
def GetStderr(self):
57-
return _lib.JupyROOTExecutorHandler_GetStderr()
58+
return _lib.JupyROOTExecutorHandler_GetStderr()
5859

5960
def GetStreamsDicts(self):
60-
out = self.GetStdout()
61-
err = self.GetStderr()
62-
outDict = {'name': 'stdout', 'text': out} if out != "" else None
63-
errDict = {'name': 'stderr', 'text': err} if err != "" else None
64-
return outDict,errDict
61+
out = self.GetStdout()
62+
err = self.GetStderr()
63+
outDict = {"name": "stdout", "text": out} if out != "" else None
64+
errDict = {"name": "stderr", "text": err} if err != "" else None
65+
return outDict, errDict
66+
6567

6668
class Poller(Thread):
6769
def __init__(self):
@@ -85,10 +87,11 @@ def run(self):
8587
def Stop(self):
8688
if self.is_alive():
8789
self.queue.put(None)
88-
self.join()
90+
self.join()
91+
8992

9093
class Runner(object):
91-
''' Asynchrously run functions
94+
"""Asynchrously run functions
9295
>>> import time
9396
>>> def f(code):
9497
... print(code)
@@ -112,7 +115,8 @@ class Runner(object):
112115
>>> print(r.HasFinished())
113116
True
114117
>>> p.Stop()
115-
'''
118+
"""
119+
116120
def __init__(self, function, poller):
117121
self.function = function
118122
self.poller = poller
@@ -126,13 +130,14 @@ def AsyncRun(self, argument):
126130

127131
def Wait(self):
128132
while self.poller.is_running:
129-
timeSleep(.1)
133+
timeSleep(0.1)
130134

131135
def HasFinished(self):
132136
return not self.poller.is_running
133137

138+
134139
class JupyROOTDeclarer(Runner):
135-
''' Asynchrously execute declarations
140+
"""Asynchrously execute declarations
136141
>>> import ROOT
137142
>>> p = Poller(); p.start()
138143
>>> d = JupyROOTDeclarer(p)
@@ -141,34 +146,41 @@ class JupyROOTDeclarer(Runner):
141146
>>> ROOT.f()
142147
3
143148
>>> p.Stop()
144-
'''
149+
"""
150+
145151
def __init__(self, poller):
146-
super(JupyROOTDeclarer, self).__init__(_lib.JupyROOTDeclarer, poller)
152+
super(JupyROOTDeclarer, self).__init__(_lib.JupyROOTDeclarer, poller)
153+
147154

148155
class JupyROOTExecutor(Runner):
149-
r''' Asynchrously execute process lines
156+
r"""Asynchrously execute process lines
150157
>>> import ROOT
151158
>>> p = Poller(); p.start()
152159
>>> d = JupyROOTExecutor(p)
153160
>>> d.Run('cout << "Here am I" << endl;')
154161
1
155162
>>> p.Stop()
156-
'''
163+
"""
164+
157165
def __init__(self, poller):
158-
super(JupyROOTExecutor, self).__init__(_lib.JupyROOTExecutor, poller)
166+
super(JupyROOTExecutor, self).__init__(_lib.JupyROOTExecutor, poller)
167+
159168

160169
def display_drawables(displayFunction):
161170
drawers = helpers.utils.GetDrawers()
162171
for drawer in drawers:
163172
for dobj in drawer.GetDrawableObjects():
164173
displayFunction(dobj)
165174

175+
166176
class JupyROOTDisplayer(Runner):
167-
''' Display all canvases'''
177+
"""Display all canvases"""
178+
168179
def __init__(self, poller):
169-
super(JupyROOTDisplayer, self).__init__(display_drawables, poller)
180+
super(JupyROOTDisplayer, self).__init__(display_drawables, poller)
170181

171-
def RunAsyncAndPrint(executor, code, ioHandler, printFunction, displayFunction, silent = False, timeout = 0.1):
182+
183+
def RunAsyncAndPrint(executor, code, ioHandler, printFunction, displayFunction, silent=False, timeout=0.1):
172184
ioHandler.Clear()
173185
ioHandler.InitCapture()
174186
executor.AsyncRun(code)
@@ -179,11 +191,11 @@ def RunAsyncAndPrint(executor, code, ioHandler, printFunction, displayFunction,
179191
ioHandler.Clear()
180192
if executor.HasFinished():
181193
break
182-
timeSleep(.1)
194+
timeSleep(0.1)
183195
executor.Wait()
184196
ioHandler.EndCapture()
185197

198+
186199
def Display(displayer, displayFunction):
187200
displayer.AsyncRun(displayFunction)
188201
displayer.Wait()
189-

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/helpers/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,12 @@ def _getJsCode(self):
610610
options = ""
611611

612612
thisJsCode = _jsCode.format(
613-
jsCanvasWidth=width, jsCanvasHeight=height, jsonLength=len(json), jsonZip=jsonzip, jsDrawOptions=options, jsDivId=divId
613+
jsCanvasWidth=width,
614+
jsCanvasHeight=height,
615+
jsonLength=len(json),
616+
jsonZip=jsonzip,
617+
jsDrawOptions=options,
618+
jsDivId=divId,
614619
)
615620
return thisJsCode
616621

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/html/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#-----------------------------------------------------------------------------
1+
# -----------------------------------------------------------------------------
22
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
33
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
4-
#-----------------------------------------------------------------------------
4+
# -----------------------------------------------------------------------------
55

66
################################################################################
77
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/html/cpphighlighter.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#-----------------------------------------------------------------------------
1+
# -----------------------------------------------------------------------------
22
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
33
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
4-
#-----------------------------------------------------------------------------
4+
# -----------------------------------------------------------------------------
55

66
################################################################################
77
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #
@@ -30,9 +30,9 @@ class CppHighlighter(Preprocessor):
3030
Detects and tags code cells that use the C++ language.
3131
"""
3232

33-
magics = [ '%%cpp' ]
34-
cpp = 'cpp'
35-
python = 'python'
33+
magics = ["%%cpp"]
34+
cpp = "cpp"
35+
python = "python"
3636

3737
def __init__(self, config=None, **kw):
3838
super(CppHighlighter, self).__init__(config=config, **kw)
@@ -52,14 +52,14 @@ def matches(self, source, reg_expr):
5252
def _preprocess_cell_python(self, cell, resources, cell_index):
5353
# Mark %%cpp and %%dcl code cells as cpp
5454
if cell.cell_type == "code" and self.matches(cell.source, self.re_magic_language):
55-
cell['metadata']['magics_language'] = self.cpp
55+
cell["metadata"]["magics_language"] = self.cpp
5656

5757
return cell, resources
5858

5959
def _preprocess_cell_cpp(self, cell, resources, cell_index):
6060
# Mark all code cells as cpp
6161
if cell.cell_type == "code":
62-
cell['metadata']['magics_language'] = self.cpp
62+
cell["metadata"]["magics_language"] = self.cpp
6363

6464
return cell, resources
6565

bindings/pyroot/pythonizations/python/ROOT/_jupyroot/kernel/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#-----------------------------------------------------------------------------
1+
# -----------------------------------------------------------------------------
22
# Author: Omar Zapata <Omar.Zapata@cern.ch> http://oproject.org
33
# Author: Danilo Piparo <Danilo.Piparo@cern.ch> CERN
44
# Author: Enric Tejedor <enric.tejedor.saavedra@cern.ch> CERN
5-
#-----------------------------------------------------------------------------
5+
# -----------------------------------------------------------------------------
66

77
################################################################################
88
# Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. #

0 commit comments

Comments
 (0)