2020from Orange .canvas .gui .utils import OSX_NSURL_toLocalFile
2121from Orange .data import Table
2222from Orange .base import Learner , Model
23- from Orange .widgets import widget , gui
23+ from Orange .util import interleave
24+ from Orange .widgets import gui
2425from Orange .widgets .utils import itemmodels
2526from Orange .widgets .settings import Setting
2627from Orange .widgets .utils .widgetpreview import WidgetPreview
@@ -78,15 +79,15 @@ def __init__(self, parent=None):
7879 super ().__init__ (parent )
7980
8081 def highlightBlock (self , text ):
81- for pattern , format in self .rules :
82+ for pattern , fmt in self .rules :
8283 exp = QRegExp (pattern )
8384 index = exp .indexIn (text )
8485 while index >= 0 :
8586 length = exp .matchedLength ()
8687 if exp .captureCount () > 0 :
87- self .setFormat (exp .pos (1 ), len (str (exp .cap (1 ))), format )
88+ self .setFormat (exp .pos (1 ), len (str (exp .cap (1 ))), fmt )
8889 else :
89- self .setFormat (exp .pos (0 ), len (str (exp .cap (0 ))), format )
90+ self .setFormat (exp .pos (0 ), len (str (exp .cap (0 ))), fmt )
9091 index = exp .indexIn (text , index + length )
9192
9293 # Multi line strings
@@ -172,9 +173,12 @@ def pasteFile(self, url):
172173
173174
174175class PythonConsole (QPlainTextEdit , code .InteractiveConsole ):
176+ # `locals` is reasonably used as argument name
177+ # pylint: disable=redefined-builtin
175178 def __init__ (self , locals = None , parent = None ):
176179 QPlainTextEdit .__init__ (self , parent )
177180 code .InteractiveConsole .__init__ (self , locals )
181+ self .newPromptPos = 0
178182 self .history , self .historyInd = ["" ], 0
179183 self .loop = self .interact ()
180184 next (self .loop )
@@ -185,7 +189,7 @@ def setLocals(self, locals):
185189 def updateLocals (self , locals ):
186190 self .locals .update (locals )
187191
188- def interact (self , banner = None ):
192+ def interact (self , banner = None , _ = None ):
189193 try :
190194 sys .ps1
191195 except AttributeError :
@@ -223,9 +227,9 @@ def interact(self, banner=None):
223227 self .resetbuffer ()
224228 more = 0
225229
226- def raw_input (self , prompt ):
227- input = str (self .document ().lastBlock ().previous ().text ())
228- return input [len (prompt ):]
230+ def raw_input (self , prompt = "" ):
231+ input_str = str (self .document ().lastBlock ().previous ().text ())
232+ return input_str [len (prompt ):]
229233
230234 def new_prompt (self , prompt ):
231235 self .write (prompt )
@@ -328,25 +332,7 @@ def insertFromMimeData(self, source):
328332 return
329333
330334
331- def interleave (seq1 , seq2 ):
332- """
333- Interleave elements of `seq2` between consecutive elements of `seq1`.
334-
335- >>> list(interleave([1, 3, 5], [2, 4]))
336- [1, 2, 3, 4, 5]
337-
338- """
339- iterator1 , iterator2 = iter (seq1 ), iter (seq2 )
340- leading = next (iterator1 )
341- for element in iterator1 :
342- yield leading
343- yield next (iterator2 )
344- leading = element
345-
346- yield leading
347-
348-
349- class Script (object ):
335+ class Script :
350336 Modified = 1
351337 MissingFromFilesystem = 2
352338
@@ -358,10 +344,8 @@ def __init__(self, name, script, flags=0, filename=None):
358344
359345
360346class ScriptItemDelegate (QStyledItemDelegate ):
361- def __init__ (self , parent ):
362- super ().__init__ (parent )
363-
364- def displayText (self , script , locale ):
347+ @staticmethod
348+ def displayText (script , _locale ):
365349 if script .flags & Script .Modified :
366350 return "*" + script .name
367351 else :
@@ -376,14 +360,17 @@ def paint(self, painter, option, index):
376360 option .palette .setColor (QPalette .Highlight , QColor (Qt .darkRed ))
377361 super ().paint (painter , option , index )
378362
379- def createEditor (self , parent , option , index ):
363+ @staticmethod
364+ def createEditor (parent , _option , _index ):
380365 return QLineEdit (parent )
381366
382- def setEditorData (self , editor , index ):
367+ @staticmethod
368+ def setEditorData (editor , index ):
383369 script = index .data (Qt .DisplayRole )
384370 editor .setText (script .name )
385371
386- def setModelData (self , editor , model , index ):
372+ @staticmethod
373+ def setModelData (editor , model , index ):
387374 model [index .row ()].name = str (editor .text ())
388375
389376
@@ -396,7 +383,7 @@ def select_row(view, row):
396383 QItemSelectionModel .ClearAndSelect )
397384
398385
399- class OWPythonScript (widget . OWWidget ):
386+ class OWPythonScript (OWWidget ):
400387 name = "Python Script"
401388 description = "Write a Python script and run it on input data or models."
402389 icon = "icons/PythonScript.svg"
@@ -421,6 +408,8 @@ class Outputs:
421408
422409 signal_names = ("data" , "learner" , "classifier" , "object" )
423410
411+ libraryListSource : list
412+
424413 libraryListSource = \
425414 Setting ([Script ("Hello world" , "print('Hello world')\n " )])
426415 currentScriptIndex = Setting (0 )
@@ -576,30 +565,30 @@ def restoreScriptText(self):
576565 def saveScriptText (self ):
577566 self .scriptText = self .text .toPlainText ()
578567
579- def handle_input (self , obj , id , signal ):
580- id = id [0 ]
568+ def handle_input (self , obj , sig_id , signal ):
569+ sig_id = sig_id [0 ]
581570 dic = getattr (self , signal )
582571 if obj is None :
583- if id in dic .keys ():
584- del dic [id ]
572+ if sig_id in dic .keys ():
573+ del dic [sig_id ]
585574 else :
586- dic [id ] = obj
575+ dic [sig_id ] = obj
587576
588577 @Inputs .data
589- def set_data (self , data , id ):
590- self .handle_input (data , id , "data" )
578+ def set_data (self , data , sig_id ):
579+ self .handle_input (data , sig_id , "data" )
591580
592581 @Inputs .learner
593- def set_learner (self , data , id ):
594- self .handle_input (data , id , "learner" )
582+ def set_learner (self , data , sig_id ):
583+ self .handle_input (data , sig_id , "learner" )
595584
596585 @Inputs .classifier
597- def set_classifier (self , data , id ):
598- self .handle_input (data , id , "classifier" )
586+ def set_classifier (self , data , sig_id ):
587+ self .handle_input (data , sig_id , "classifier" )
599588
600589 @Inputs .object
601- def set_object (self , data , id ):
602- self .handle_input (data , id , "object" )
590+ def set_object (self , data , sig_id ):
591+ self .handle_input (data , sig_id , "object" )
603592
604593 def handleNewSignals (self ):
605594 self .commit ()
@@ -614,11 +603,11 @@ def selectedScriptIndex(self):
614603 def setSelectedScript (self , index ):
615604 select_row (self .libraryView , index )
616605
617- def onAddScript (self , * args ):
606+ def onAddScript (self , * _ ):
618607 self .libraryList .append (Script ("New script" , self .text .toPlainText (), 0 ))
619608 self .setSelectedScript (len (self .libraryList ) - 1 )
620609
621- def onAddScriptFromFile (self , * args ):
610+ def onAddScriptFromFile (self , * _ ):
622611 filename , _ = QFileDialog .getOpenFileName (
623612 self , 'Open Python Script' ,
624613 os .path .expanduser ("~/" ),
@@ -632,18 +621,18 @@ def onAddScriptFromFile(self, *args):
632621 self .libraryList .append (Script (name , contents , 0 , filename ))
633622 self .setSelectedScript (len (self .libraryList ) - 1 )
634623
635- def onRemoveScript (self , * args ):
624+ def onRemoveScript (self , * _ ):
636625 index = self .selectedScriptIndex ()
637626 if index is not None :
638627 del self .libraryList [index ]
639628 select_row (self .libraryView , max (index - 1 , 0 ))
640629
641- def onSaveScriptToFile (self , * args ):
630+ def onSaveScriptToFile (self , * _ ):
642631 index = self .selectedScriptIndex ()
643632 if index is not None :
644633 self .saveScript ()
645634
646- def onSelectedScriptChanged (self , selected , deselected ):
635+ def onSelectedScriptChanged (self , selected , _deselected ):
647636 index = [i .row () for i in selected .indexes ()]
648637 if index :
649638 current = index [0 ]
@@ -655,7 +644,7 @@ def onSelectedScriptChanged(self, selected, deselected):
655644 self .currentScriptIndex = current
656645
657646 def documentForScript (self , script = 0 ):
658- if type (script ) != Script :
647+ if not isinstance (script , Script ) :
659648 script = self .libraryList [script ]
660649 if script not in self ._cachedDocuments :
661650 doc = QTextDocument (self )
@@ -668,7 +657,7 @@ def documentForScript(self, script=0):
668657 self ._cachedDocuments [script ] = doc
669658 return self ._cachedDocuments [script ]
670659
671- def commitChangesToLibrary (self , * args ):
660+ def commitChangesToLibrary (self , * _ ):
672661 index = self .selectedScriptIndex ()
673662 if index is not None :
674663 self .libraryList [index ].script = self .text .toPlainText ()
@@ -681,7 +670,7 @@ def onModificationChanged(self, modified):
681670 self .libraryList [index ].flags = Script .Modified if modified else 0
682671 self .libraryList .emitDataChanged (index )
683672
684- def onSpliterMoved (self , pos , ind ):
673+ def onSpliterMoved (self , _pos , _ind ):
685674 self .splitterState = bytes (self .splitCanvas .saveState ())
686675
687676 def restoreSaved (self ):
@@ -728,7 +717,6 @@ def initial_locals_state(self):
728717
729718 def commit (self ):
730719 self .Error .clear ()
731- self ._script = str (self .text .toPlainText ())
732720 lcls = self .initial_locals_state ()
733721 lcls ["_script" ] = str (self .text .toPlainText ())
734722 self .console .updateLocals (lcls )
@@ -746,7 +734,8 @@ def commit(self):
746734 out_var = None
747735 getattr (self .Outputs , signal ).send (out_var )
748736
749- def dragEnterEvent (self , event ):
737+ @staticmethod
738+ def dragEnterEvent (event ):
750739 urls = event .mimeData ().urls ()
751740 if urls :
752741 # try reading the file as text
0 commit comments