From 2dd308e0979e7af5e505bb076772161eae76b9a4 Mon Sep 17 00:00:00 2001 From: Gilles DAVID Date: Thu, 27 Jul 2017 13:56:54 +0200 Subject: [PATCH] Use isinstance() instead of type() and remove useless list(....keys()) --- bin/addToKillList.py | 4 ++-- src/Container/SALOME_ContainerPy.py | 8 ++------ src/KERNEL_PY/kernel/datamodeler.py | 18 +++++++++--------- src/KERNEL_PY/kernel/enumerate.py | 15 ++++++--------- src/KERNEL_PY/salome_notebook.py | 8 ++++---- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/bin/addToKillList.py b/bin/addToKillList.py index 4bb8827e3..d9b580d8f 100755 --- a/bin/addToKillList.py +++ b/bin/addToKillList.py @@ -61,7 +61,7 @@ def addToKillList(command_pid, command, port=None): # check if PID is already in dictionary already_in=False for process_id in process_ids: - for pid in list(process_id.keys()): + for pid in process_id: if int(pid) == int(command_pid): already_in=True break @@ -72,7 +72,7 @@ def addToKillList(command_pid, command, port=None): # add process to the dictionary if not already_in: import types - if type(command) == list: command=" ".join([str(c) for c in command]) + if isinstance(command, list): command=" ".join([str(c) for c in command]) command=command.split()[0] try: if verbose(): print("addToKillList: %s : %s" % ( str(command_pid), command )) diff --git a/src/Container/SALOME_ContainerPy.py b/src/Container/SALOME_ContainerPy.py index 1e24534dc..fafcc92a6 100755 --- a/src/Container/SALOME_ContainerPy.py +++ b/src/Container/SALOME_ContainerPy.py @@ -230,15 +230,11 @@ class SALOME_ContainerPy_i (Engines__POA.Container): def find_component_instance(self, registeredName): anEngine = None - keysList = list(self._listInstances_map.keys()) - i = 0 - while i < len(keysList): - instance = keysList[i] + for instance in self._listInstances_map: if find(instance,registeredName) == 0: anEngine = self._listInstances_map[instance] return anEngine._this() - i = i + 1 - return anEngine._this() + return anEngine #------------------------------------------------------------------------- diff --git a/src/KERNEL_PY/kernel/datamodeler.py b/src/KERNEL_PY/kernel/datamodeler.py index eec05050d..b2f12bc5a 100644 --- a/src/KERNEL_PY/kernel/datamodeler.py +++ b/src/KERNEL_PY/kernel/datamodeler.py @@ -105,18 +105,18 @@ class DataModeler: # Default initialization (if any) if defaultmap is not None: self._defaultmap.update(defaultmap) - for name in list(self._defaultmap.keys()): + for name in self._defaultmap: self.__setattr__(name,self._defaultmap[name]) ## %A None argument means that no entry is created in the associated maps. - def addAttribute(self, name, type=None, range=None, default=None, void=None): + def addAttribute(self, name, a_type=None, a_range=None, default=None, void=None): """ A None argument means that no entry is created in the associated maps. """ - self._typemap[name] = type + self._typemap[name] = a_type - if range is not None: - self._rangemap[name] = range + if a_range is not None: + self._rangemap[name] = a_range if void is not None: self._voidmap[name] = void @@ -135,7 +135,7 @@ class DataModeler: if name == "_typemap": print("WARNING WARNING WARNING : changing value of _typemap by ",val) - if name not in list(self._typemap.keys()): + if name not in self._typemap: raise DevelException("The class "+str(self.__class__)+" has no attribute "+str(name)) if val is None: @@ -158,7 +158,7 @@ class DataModeler: if name in UNCHECKED_ATTRIBUTES: return self.__dict__[name] - if name not in list(self._typemap.keys()): + if name not in self._typemap: raise DevelException("The class "+str(self.__class__)+" has no attribute "+str(name)) # The attribute coulb be requested while it has not been created yet (for # example if we did't call the setter before). @@ -232,8 +232,8 @@ def TEST_addAttribute(): ref_value = 1.3 data.addAttribute( name = "myAttr", - type = TypeDouble, - range = None, + a_type = TypeDouble, + a_range = None, default = ref_value, void = False) diff --git a/src/KERNEL_PY/kernel/enumerate.py b/src/KERNEL_PY/kernel/enumerate.py index 5111c916f..ad04448af 100644 --- a/src/KERNEL_PY/kernel/enumerate.py +++ b/src/KERNEL_PY/kernel/enumerate.py @@ -53,6 +53,7 @@ class Enumerate(object): value = offset + number setattr(self, key, value) self._dict_keynumbers[key] = value + self._dict_numberkeys = {v: k for k, v in self._dict_keynumbers.items()} ## Return true if this enumerate contains the specified key string # \param key a key string to test @@ -61,7 +62,7 @@ class Enumerate(object): Return true if this enumerate contains the specified key string @key a key string to test """ - return (key in list(self._dict_keynumbers.keys())) + return key in self._dict_keynumbers ## Returns true if the specified integer value is defined as an identifier # in this enumarate. @@ -72,25 +73,21 @@ class Enumerate(object): in this enumarate. @value a value to test """ - return (value in list(self._dict_keynumbers.values())) + return value in self._dict_numberkeys ## Returns the list of keys in this enumerate. def listkeys(self): """ Returns the list of keys in this enumerate. """ - list = list(self._dict_keynumbers.keys()) - list.sort() - return list + return sorted(self._dict_keynumbers) ## Returns the list of values specified to initiate this enumerate. def listvalues(self): """ Returns the list of values specified to initiate this enumerate. """ - list = list(self._dict_keynumbers.values()) - list.sort() - return list + return sorted(self._dict_numberkeys) ## Returns the symbolic key string associated to the specified identifier value. # \param value an integer value whose associated key string is requested. @@ -104,7 +101,7 @@ class Enumerate(object): return None # _MEM_ We assume here that the keys and associated values are in the # same order in their list. - return list(self._dict_keynumbers.keys())[list(self._dict_keynumbers.values()).index(value)] + return self._dict_numberkeys[value] # If not, weshould use a longer implementation such that: #for key in self._dict_keynumbers.keys(): diff --git a/src/KERNEL_PY/salome_notebook.py b/src/KERNEL_PY/salome_notebook.py index fec578e57..92857da24 100644 --- a/src/KERNEL_PY/salome_notebook.py +++ b/src/KERNEL_PY/salome_notebook.py @@ -80,16 +80,16 @@ class NoteBook: 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): -- 2.39.2