apply some codeql recommendations

- remove unused imports
- avoid overlaps of variable names with keywords
- make some exception processing more explicit
This commit is contained in:
Axel Kohlmeyer
2021-05-09 19:52:31 -04:00
parent 6d9309b76d
commit 69a92a3e00
4 changed files with 94 additions and 97 deletions

View File

@ -18,9 +18,6 @@ from __future__ import print_function
import os import os
import sys import sys
import traceback
import types
import warnings
from ctypes import * from ctypes import *
from os.path import dirname,abspath,join from os.path import dirname,abspath,join
from inspect import getsourcefile from inspect import getsourcefile
@ -47,7 +44,7 @@ class ExceptionCheck:
def __enter__(self): def __enter__(self):
pass pass
def __exit__(self, type, value, traceback): def __exit__(self, exc_type, exc_value, traceback):
if self.lmp.has_exceptions and self.lmp.lib.lammps_has_error(self.lmp.lmp): if self.lmp.has_exceptions and self.lmp.lib.lammps_has_error(self.lmp.lmp):
raise self.lmp._lammps_exception raise self.lmp._lammps_exception
@ -310,7 +307,7 @@ class lammps(object):
# tested to work with mpi4py versions 2 and 3 # tested to work with mpi4py versions 2 and 3
self.has_mpi4py = mpi4py_version.split('.')[0] in ['2','3'] self.has_mpi4py = mpi4py_version.split('.')[0] in ['2','3']
except: except:
pass self.has_mpi4py = None
# if no ptr provided, create an instance of LAMMPS # if no ptr provided, create an instance of LAMMPS
# don't know how to pass an MPI communicator from PyPar # don't know how to pass an MPI communicator from PyPar
@ -878,71 +875,71 @@ class lammps(object):
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
def extract_compute(self,id,style,type): def extract_compute(self,cid,cstyle,ctype):
"""Retrieve data from a LAMMPS compute """Retrieve data from a LAMMPS compute
This is a wrapper around the :cpp:func:`lammps_extract_compute` This is a wrapper around the :cpp:func:`lammps_extract_compute`
function of the C-library interface. function of the C-library interface.
This function returns ``None`` if either the compute id is not This function returns ``None`` if either the compute id is not
recognized, or an invalid combination of :ref:`style <py_style_constants>` recognized, or an invalid combination of :ref:`cstyle <py_style_constants>`
and :ref:`type <py_type_constants>` constants is used. The and :ref:`ctype <py_type_constants>` constants is used. The
names and functionality of the constants are the same as for names and functionality of the constants are the same as for
the corresponding C-library function. For requests to return the corresponding C-library function. For requests to return
a scalar or a size, the value is returned, otherwise a pointer. a scalar or a size, the value is returned, otherwise a pointer.
:param id: compute ID :param cid: compute ID
:type id: string :type cid: string
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants` :param cstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
:type style: int :type cstyle: int
:param type: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants` :param ctype: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
:type type: int :type ctype: int
:return: requested data as scalar, pointer to 1d or 2d double array, or None :return: requested data as scalar, pointer to 1d or 2d double array, or None
:rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType :rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType
""" """
if id: id = id.encode() if cid: cid = cid.encode()
else: return None else: return None
if type == LMP_TYPE_SCALAR: if ctype == LMP_TYPE_SCALAR:
if style == LMP_STYLE_GLOBAL: if style == LMP_STYLE_GLOBAL:
self.lib.lammps_extract_compute.restype = POINTER(c_double) self.lib.lammps_extract_compute.restype = POINTER(c_double)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr[0] return ptr[0]
elif style == LMP_STYLE_ATOM: elif style == LMP_STYLE_ATOM:
return None return None
elif style == LMP_STYLE_LOCAL: elif style == LMP_STYLE_LOCAL:
self.lib.lammps_extract_compute.restype = POINTER(c_int) self.lib.lammps_extract_compute.restype = POINTER(c_int)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr[0] return ptr[0]
elif type == LMP_TYPE_VECTOR: elif ctype == LMP_TYPE_VECTOR:
self.lib.lammps_extract_compute.restype = POINTER(c_double) self.lib.lammps_extract_compute.restype = POINTER(c_double)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr return ptr
elif type == LMP_TYPE_ARRAY: elif ctype == LMP_TYPE_ARRAY:
self.lib.lammps_extract_compute.restype = POINTER(POINTER(c_double)) self.lib.lammps_extract_compute.restype = POINTER(POINTER(c_double))
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr return ptr
elif type == LMP_SIZE_COLS: elif ctype == LMP_SIZE_COLS:
if style == LMP_STYLE_GLOBAL \ if cstyle == LMP_STYLE_GLOBAL \
or style == LMP_STYLE_ATOM \ or cstyle == LMP_STYLE_ATOM \
or style == LMP_STYLE_LOCAL: or cstyle == LMP_STYLE_LOCAL:
self.lib.lammps_extract_compute.restype = POINTER(c_int) self.lib.lammps_extract_compute.restype = POINTER(c_int)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr[0] return ptr[0]
elif type == LMP_SIZE_VECTOR or type == LMP_SIZE_ROWS: elif ctype == LMP_SIZE_VECTOR or ctype == LMP_SIZE_ROWS:
if style == LMP_STYLE_GLOBAL \ if cstyle == LMP_STYLE_GLOBAL \
or style == LMP_STYLE_LOCAL: or cstyle == LMP_STYLE_LOCAL:
self.lib.lammps_extract_compute.restype = POINTER(c_int) self.lib.lammps_extract_compute.restype = POINTER(c_int)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_compute(self.lmp,id,style,type) ptr = self.lib.lammps_extract_compute(self.lmp,cid,cstyle,ctype)
return ptr[0] return ptr[0]
return None return None
@ -952,25 +949,25 @@ class lammps(object):
# in case of global data, free memory for 1 double via lammps_free() # in case of global data, free memory for 1 double via lammps_free()
# double was allocated by library interface function # double was allocated by library interface function
def extract_fix(self,id,style,type,nrow=0,ncol=0): def extract_fix(self,fid,fstyle,ftype,nrow=0,ncol=0):
"""Retrieve data from a LAMMPS fix """Retrieve data from a LAMMPS fix
This is a wrapper around the :cpp:func:`lammps_extract_fix` This is a wrapper around the :cpp:func:`lammps_extract_fix`
function of the C-library interface. function of the C-library interface.
This function returns ``None`` if either the fix id is not This function returns ``None`` if either the fix id is not
recognized, or an invalid combination of :ref:`style <py_style_constants>` recognized, or an invalid combination of :ref:`fstyle <py_style_constants>`
and :ref:`type <py_type_constants>` constants is used. The and :ref:`ftype <py_type_constants>` constants is used. The
names and functionality of the constants are the same as for names and functionality of the constants are the same as for
the corresponding C-library function. For requests to return the corresponding C-library function. For requests to return
a scalar or a size, the value is returned, also when accessing a scalar or a size, the value is returned, also when accessing
global vectors or arrays, otherwise a pointer. global vectors or arrays, otherwise a pointer.
:param id: fix ID :param fid: fix ID
:type id: string :type fid: string
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants` :param fstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
:type style: int :type fstyle: int
:param type: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants` :param ftype: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
:type type: int :type ftype: int
:param nrow: index of global vector element or row index of global array element :param nrow: index of global vector element or row index of global array element
:type nrow: int :type nrow: int
:param ncol: column index of global array element :param ncol: column index of global array element
@ -979,53 +976,53 @@ class lammps(object):
:rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType :rtype: c_double, ctypes.POINTER(c_double), ctypes.POINTER(ctypes.POINTER(c_double)), or NoneType
""" """
if id: id = id.encode() if fid: fid = fid.encode()
else: return None else: return None
if style == LMP_STYLE_GLOBAL: if fstyle == LMP_STYLE_GLOBAL:
if type in (LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY): if ftype in (LMP_TYPE_SCALAR, LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
self.lib.lammps_extract_fix.restype = POINTER(c_double) self.lib.lammps_extract_fix.restype = POINTER(c_double)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol) ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
result = ptr[0] result = ptr[0]
self.lib.lammps_free(ptr) self.lib.lammps_free(ptr)
return result return result
elif type in (LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS): elif ftype in (LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
self.lib.lammps_extract_fix.restype = POINTER(c_int) self.lib.lammps_extract_fix.restype = POINTER(c_int)
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol) ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
return ptr[0] return ptr[0]
else: else:
return None return None
elif style == LMP_STYLE_ATOM: elif fstyle == LMP_STYLE_ATOM:
if type == LMP_TYPE_VECTOR: if ftype == LMP_TYPE_VECTOR:
self.lib.lammps_extract_fix.restype = POINTER(c_double) self.lib.lammps_extract_fix.restype = POINTER(c_double)
elif type == LMP_TYPE_ARRAY: elif ftype == LMP_TYPE_ARRAY:
self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double)) self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double))
elif type == LMP_SIZE_COLS: elif ftype == LMP_SIZE_COLS:
self.lib.lammps_extract_fix.restype = POINTER(c_int) self.lib.lammps_extract_fix.restype = POINTER(c_int)
else: else:
return None return None
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol) ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
if type == LMP_SIZE_COLS: if ftype == LMP_SIZE_COLS:
return ptr[0] return ptr[0]
else: else:
return ptr return ptr
elif style == LMP_STYLE_LOCAL: elif fstyle == LMP_STYLE_LOCAL:
if type == LMP_TYPE_VECTOR: if ftype == LMP_TYPE_VECTOR:
self.lib.lammps_extract_fix.restype = POINTER(c_double) self.lib.lammps_extract_fix.restype = POINTER(c_double)
elif type == LMP_TYPE_ARRAY: elif ftype == LMP_TYPE_ARRAY:
self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double)) self.lib.lammps_extract_fix.restype = POINTER(POINTER(c_double))
elif type in (LMP_TYPE_SCALAR, LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS): elif ftype in (LMP_TYPE_SCALAR, LMP_SIZE_VECTOR, LMP_SIZE_ROWS, LMP_SIZE_COLS):
self.lib.lammps_extract_fix.restype = POINTER(c_int) self.lib.lammps_extract_fix.restype = POINTER(c_int)
else: else:
return None return None
with ExceptionCheck(self): with ExceptionCheck(self):
ptr = self.lib.lammps_extract_fix(self.lmp,id,style,type,nrow,ncol) ptr = self.lib.lammps_extract_fix(self.lmp,fid,fstyle,ftype,nrow,ncol)
if type in (LMP_TYPE_VECTOR, LMP_TYPE_ARRAY): if ftype in (LMP_TYPE_VECTOR, LMP_TYPE_ARRAY):
return ptr return ptr
else: else:
return ptr[0] return ptr[0]

View File

@ -176,7 +176,7 @@ class AvgChunkFile:
current[data_column] = [value] current[data_column] = [value]
chunks_read += 1 chunks_read += 1
assert (chunk == chunks_read) assert chunk == chunks_read
else: else:
# do not support changing number of chunks # do not support changing number of chunks
if not (num_chunks == int(parts[1])): if not (num_chunks == int(parts[1])):

View File

@ -142,7 +142,7 @@ class numpy_wrapper:
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
def extract_compute(self, cid, style, type): def extract_compute(self, cid, cstyle, ctype):
"""Retrieve data from a LAMMPS compute """Retrieve data from a LAMMPS compute
This is a wrapper around the This is a wrapper around the
@ -150,50 +150,50 @@ class numpy_wrapper:
It behaves the same as the original method, but returns NumPy arrays It behaves the same as the original method, but returns NumPy arrays
instead of ``ctypes`` pointers. instead of ``ctypes`` pointers.
:param id: compute ID :param cid: compute ID
:type id: string :type cid: string
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants` :param cstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
:type style: int :type cstyle: int
:param type: type of the returned data (scalar, vector, or array), see :ref:`py_type_constants` :param ctype: type of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
:type type: int :type ctype: int
:return: requested data either as float, as NumPy array with direct access to C data, or None :return: requested data either as float, as NumPy array with direct access to C data, or None
:rtype: float, numpy.array, or NoneType :rtype: float, numpy.array, or NoneType
""" """
value = self.lmp.extract_compute(cid, style, type) value = self.lmp.extract_compute(cid, cstyle, ctype)
if style in (LMP_STYLE_GLOBAL, LMP_STYLE_LOCAL): if cstyle in (LMP_STYLE_GLOBAL, LMP_STYLE_LOCAL):
if type == LMP_TYPE_VECTOR: if ctype == LMP_TYPE_VECTOR:
nrows = self.lmp.extract_compute(cid, style, LMP_SIZE_VECTOR) nrows = self.lmp.extract_compute(cid, cstyle, LMP_SIZE_VECTOR)
return self.darray(value, nrows) return self.darray(value, nrows)
elif type == LMP_TYPE_ARRAY: elif ctype == LMP_TYPE_ARRAY:
nrows = self.lmp.extract_compute(cid, style, LMP_SIZE_ROWS) nrows = self.lmp.extract_compute(cid, cstyle, LMP_SIZE_ROWS)
ncols = self.lmp.extract_compute(cid, style, LMP_SIZE_COLS) ncols = self.lmp.extract_compute(cid, cstyle, LMP_SIZE_COLS)
return self.darray(value, nrows, ncols) return self.darray(value, nrows, ncols)
elif style == LMP_STYLE_ATOM: elif cstyle == LMP_STYLE_ATOM:
if type == LMP_TYPE_VECTOR: if ctype == LMP_TYPE_VECTOR:
nlocal = self.lmp.extract_global("nlocal") nlocal = self.lmp.extract_global("nlocal")
return self.darray(value, nlocal) return self.darray(value, nlocal)
elif type == LMP_TYPE_ARRAY: elif ctype == LMP_TYPE_ARRAY:
nlocal = self.lmp.extract_global("nlocal") nlocal = self.lmp.extract_global("nlocal")
ncols = self.lmp.extract_compute(cid, style, LMP_SIZE_COLS) ncols = self.lmp.extract_compute(cid, cstyle, LMP_SIZE_COLS)
return self.darray(value, nlocal, ncols) return self.darray(value, nlocal, ncols)
return value return value
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
def extract_fix(self, fid, style, type, nrow=0, ncol=0): def extract_fix(self, fid, fstyle, ftype, nrow=0, ncol=0):
"""Retrieve data from a LAMMPS fix """Retrieve data from a LAMMPS fix
This is a wrapper around the :py:meth:`lammps.extract_fix() <lammps.lammps.extract_fix()>` method. This is a wrapper around the :py:meth:`lammps.extract_fix() <lammps.lammps.extract_fix()>` method.
It behaves the same as the original method, but returns NumPy arrays It behaves the same as the original method, but returns NumPy arrays
instead of ``ctypes`` pointers. instead of ``ctypes`` pointers.
:param id: fix ID :param fid: fix ID
:type id: string :type fid: string
:param style: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants` :param fstyle: style of the data retrieve (global, atom, or local), see :ref:`py_style_constants`
:type style: int :type fstyle: int
:param type: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants` :param ftype: type or size of the returned data (scalar, vector, or array), see :ref:`py_type_constants`
:type type: int :type ftype: int
:param nrow: index of global vector element or row index of global array element :param nrow: index of global vector element or row index of global array element
:type nrow: int :type nrow: int
:param ncol: column index of global array element :param ncol: column index of global array element
@ -202,22 +202,22 @@ class numpy_wrapper:
:rtype: integer or double value, pointer to 1d or 2d double array or None :rtype: integer or double value, pointer to 1d or 2d double array or None
""" """
value = self.lmp.extract_fix(fid, style, type, nrow, ncol) value = self.lmp.extract_fix(fid, fstyle, ftype, nrow, ncol)
if style == LMP_STYLE_ATOM: if fstyle == LMP_STYLE_ATOM:
if type == LMP_TYPE_VECTOR: if ftype == LMP_TYPE_VECTOR:
nlocal = self.lmp.extract_global("nlocal") nlocal = self.lmp.extract_global("nlocal")
return self.darray(value, nlocal) return self.darray(value, nlocal)
elif type == LMP_TYPE_ARRAY: elif ftype == LMP_TYPE_ARRAY:
nlocal = self.lmp.extract_global("nlocal") nlocal = self.lmp.extract_global("nlocal")
ncols = self.lmp.extract_fix(fid, style, LMP_SIZE_COLS, 0, 0) ncols = self.lmp.extract_fix(fid, fstyle, LMP_SIZE_COLS, 0, 0)
return self.darray(value, nlocal, ncols) return self.darray(value, nlocal, ncols)
elif style == LMP_STYLE_LOCAL: elif fstyle == LMP_STYLE_LOCAL:
if type == LMP_TYPE_VECTOR: if ftype == LMP_TYPE_VECTOR:
nrows = self.lmp.extract_fix(fid, style, LMP_SIZE_ROWS, 0, 0) nrows = self.lmp.extract_fix(fid, fstyle, LMP_SIZE_ROWS, 0, 0)
return self.darray(value, nrows) return self.darray(value, nrows)
elif type == LMP_TYPE_ARRAY: elif ftype == LMP_TYPE_ARRAY:
nrows = self.lmp.extract_fix(fid, style, LMP_SIZE_ROWS, 0, 0) nrows = self.lmp.extract_fix(fid, fstyle, LMP_SIZE_ROWS, 0, 0)
ncols = self.lmp.extract_fix(fid, style, LMP_SIZE_COLS, 0, 0) ncols = self.lmp.extract_fix(fid, fstyle, LMP_SIZE_COLS, 0, 0)
return self.darray(value, nrows, ncols) return self.darray(value, nrows, ncols)
return value return value

View File

@ -23,7 +23,6 @@ from __future__ import print_function
import os import os
import re import re
import select import select
import sys
from collections import namedtuple from collections import namedtuple
from .core import lammps from .core import lammps
@ -41,7 +40,7 @@ class OutputCapture(object):
os.dup2(self.stdout_pipe_write, self.stdout_fd) os.dup2(self.stdout_pipe_write, self.stdout_fd)
return self return self
def __exit__(self, type, value, tracebac): def __exit__(self, exc_type, exc_value, traceback):
os.dup2(self.stdout, self.stdout_fd) os.dup2(self.stdout, self.stdout_fd)
os.close(self.stdout) os.close(self.stdout)
os.close(self.stdout_pipe_read) os.close(self.stdout_pipe_read)
@ -351,6 +350,7 @@ def get_thermo_data(output):
for i, col in enumerate(columns): for i, col in enumerate(columns):
current_run[col].append(values[i]) current_run[col].append(values[i])
except ValueError: except ValueError:
# cannot convert. must be a non-thermo output. ignore.
pass pass
return runs return runs