X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FKERNEL_PY%2Fsalome_notebook.py;h=27acdc550b5f0532f14d3af288c879edc2e43a51;hb=20c132438f5b8f7337bf35272989b10eb003d71c;hp=d2e2738ef27dc62c57b3a9cf1e0a6fcff270d479;hpb=9749fc1db72bd80e278405114b05ffc69b5031da;p=modules%2Fkernel.git diff --git a/src/KERNEL_PY/salome_notebook.py b/src/KERNEL_PY/salome_notebook.py index d2e2738ef..27acdc550 100644 --- a/src/KERNEL_PY/salome_notebook.py +++ b/src/KERNEL_PY/salome_notebook.py @@ -1,10 +1,10 @@ # -*- coding: iso-8859-1 -*- -# Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE +# Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either -# version 2.1 of the License. +# version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -29,73 +29,76 @@ Module salome_notebook gives access to Salome Notebook. import salome -class PseudoStudyForNoteBook(object): - +class PseudoStudyForNoteBook: + def __init__(self, **kwargs): self.kwargs = kwargs pass - + def GetVariableNames(self): - return self.kwargs.keys() - + return list(self.kwargs.keys()) + def IsVariable(self, variableName): return variableName in self.kwargs - + def IsReal(self, variableName): val = self.kwargs[variableName] try: float(val) return True - except: + except Exception: pass return False - + IsInteger = IsReal IsBoolean = IsReal - + def IsString(self, variableName): return not self.IsReal(variableName) - + def GetString(self, variableName): return self.kwargs[variableName] - + def GetReal(self, variableName): return float(self.kwargs[variableName]) - + GetInteger = GetReal GetBoolean = GetReal - + pass class NoteBook: - - def __init__(self, Study): - self.myStudy = Study - + + def __init__(self, theIsEnablePublish=True): + if theIsEnablePublish: + self.myStudy = salome.myStudy + else: + self.myStudy = PseudoStudyForNoteBook() + def set(self, variableName, variable): """ - Create (or modify) variable with name "variableName" + Create (or modify) variable with name "variableName" and value equal "theValue". """ - if type(variable) == float: + if isinstance(variable, float): self.myStudy.SetReal(variableName, variable) - - elif type(variable) == int: + + elif isinstance(variable, int): self.myStudy.SetInteger(variableName, variable) - - elif type(variable) == bool: + + elif isinstance(variable, bool): self.myStudy.SetBoolean(variableName, variable) - - elif type(variable) == str: + + elif isinstance(variable, str): self.myStudy.SetString(variableName, variable) - + def get(self, variableName): """ Return value of the variable with name "variableName". """ aResult = None if self.myStudy.IsVariable(variableName): - + if self.myStudy.IsReal(variableName): aResult = self.myStudy.GetReal(variableName) @@ -108,38 +111,38 @@ class NoteBook: elif self.myStudy.IsString(variableName): aResult = self.myStudy.GetString(variableName) aResult_orig = aResult - l = self.myStudy.GetVariableNames() - l.remove(variableName) + list_of_variables = self.myStudy.GetVariableNames() + list_of_variables.remove(variableName) # -- # To avoid the smallest strings to be replaced first, # the list is sorted by decreasing lengths # -- - l.sort(key=str.__len__) - l.reverse() - for name in l: + list_of_variables.sort(key=str.__len__) + list_of_variables.reverse() + for name in list_of_variables: if aResult.find(name) >= 0: val = self.get(name) - aResult = aResult.replace(name, "%s"%(val)) + aResult = aResult.replace(name, "%s" % (val)) pass pass try: aResult = eval(aResult) - except Exception, e: + except Exception as e: msg = str(e) msg += "\n" msg += "A problem occurs while parsing " - msg += "the variable %s "%(variableName.__repr__()) - msg += "with value %s ..."%(aResult_orig.__repr__()) + msg += "the variable %s " % (variableName.__repr__()) + msg += "with value %s ..." % (aResult_orig.__repr__()) msg += "\n" msg += "Please, check your notebook !" raise Exception(msg) pass - + return aResult - - def isVariable(self, variableName): + + def isVariable(self, variableName): """ - Return true if variable with name "variableName" + Return true if variable with name "variableName" exists in the study, otherwise return false. """ return self.myStudy.IsVariable(variableName) @@ -149,31 +152,32 @@ class NoteBook: value = float(typ(value)) self.myStudy.SetStringAsDouble(variableName, value) return - + def setAsReal(self, variableName): self.setAs(variableName, float) return - + def setAsInteger(self, variableName): self.setAs(variableName, int) return - + def setAsBool(self, variableName): self.setAs(variableName, bool) return - + def check(self): for variableName in self.myStudy.GetVariableNames(): self.get(variableName) pass return - + pass + def checkThisNoteBook(**kwargs): - study = PseudoStudyForNoteBook(**kwargs) - note_book = NoteBook(study) + note_book = NoteBook(False) note_book.check() return -notebook = NoteBook(salome.myStudy) + +notebook = NoteBook()