Commit 80cc954a authored by peastman's avatar peastman
Browse files

Merge pull request #1228 from rmcgibbo/docstrings

Better docstrings
parents d137b536 1db3314e
...@@ -45,7 +45,7 @@ using namespace OpenMM; ...@@ -45,7 +45,7 @@ using namespace OpenMM;
%} %}
%feature("autodoc", "1"); %feature("autodoc", "0");
%nodefaultctor; %nodefaultctor;
%include features.i %include features.i
......
...@@ -49,8 +49,7 @@ def striphtmltags(s): ...@@ -49,8 +49,7 @@ def striphtmltags(s):
s = s.replace('<i>', '_').replace('</i>', '_') s = s.replace('<i>', '_').replace('</i>', '_')
s = s.replace('<b>', '*').replace('</b>', '*') s = s.replace('<b>', '*').replace('</b>', '*')
s = re.sub('\s*(<ul>.*</ul>\s*)', replace_ul_tags, s, flags=re.MULTILINE | re.DOTALL) s = re.sub('\s*(<ul>.*?</ul>\s*)', replace_ul_tags, s, flags=re.MULTILINE | re.DOTALL)
return s return s
def trimToSingleSpace(text): def trimToSingleSpace(text):
...@@ -146,6 +145,20 @@ def getClassMethodList(classNode, skipMethods): ...@@ -146,6 +145,20 @@ def getClassMethodList(classNode, skipMethods):
return methodList return methodList
def docstringTypemap(cpptype):
"""Translate a C++ type to Python for inclusion in the Python docstrings.
This doesn't need to be perfectly accurate -- it's not used for generating
the actual swig wrapper code. It's only used for generating the docstrings.
"""
pytype = cpptype
if pytype.startswith('const '):
pytype = pytype[6:]
if pytype.startswith('std::'):
pytype = pytype[5:]
pytype = pytype.strip('&')
return pytype.strip()
class SwigInputBuilder: class SwigInputBuilder:
def __init__(self, def __init__(self,
inputDirname, inputDirname,
...@@ -568,32 +581,58 @@ class SwigInputBuilder: ...@@ -568,32 +581,58 @@ class SwigInputBuilder:
(shortClassName, memberNode, (shortClassName, memberNode,
shortMethDefinition, methName, shortMethDefinition, methName,
isConstructors, isDestructor, templateType, templateName ) = items isConstructors, isDestructor, templateType, templateName ) = items
if self.fOutDocstring: if self.fOutDocstring:
for dNode in findNodes(memberNode, 'detaileddescription'): signatureParams = findNodes(memberNode, 'param')
dString="" assert len(findNodes(memberNode, 'detaileddescription')) == 1
dNode = findNodes(memberNode, 'detaileddescription')[0]
try: try:
description=getText('para', dNode) description=getText('para', dNode)
description.strip() description.strip()
if description:
dString=description
except IndexError: except IndexError:
pass description = ''
params = findNodes(dNode, 'para/parameterlist/parameteritem') params = findNodes(dNode, 'para/parameterlist/parameteritem')
paramString = ['Parameters', '----------']
returnString = ['Returns', '-------']
if len(params) > 0: if len(params) > 0:
dString="%s\n Parameters:" % dString if len(signatureParams) != len(params):
for pNode in params: raise ValueError('docstring in %s.%s does not match the signature' % (shortClassName, methName))
argName = getText('parameternamelist/parametername', pNode)
for pNode, pSignatureNode in zip(params, signatureParams):
parameterNameNode = findNodes(pNode, 'parameternamelist/parametername')[0]
argDoc = getText('parameterdescription/para', pNode) argDoc = getText('parameterdescription/para', pNode)
dString="%s\n - %s %s" % (dString, argName, argDoc) argName = getNodeText(parameterNameNode)
dString.strip() argType = docstringTypemap(getText('type', pSignatureNode))
isOutput = parameterNameNode.get('direction') == 'out'
if isOutput:
returnString.extend(['%s : %s' % (argName, argType), ' %s' % argDoc])
else:
paramString.extend(['%s : %s' % (argName, argType), ' %s' % argDoc])
returnSection = findNodes(dNode, 'para/simplesect')
if len(returnSection) > 0:
returnNode = returnSection[0]
if returnNode.get('kind') == 'return':
argType = getNodeText(findNodes(memberNode, 'type')[0])
argType = docstringTypemap(argType)
returnString.extend([argType, ' %s' % getNodeText(returnNode).strip()])
dString = '\n'.join(
([description] + [''] if len(description) > 0 else []) +
(paramString + [''] if len(paramString) > 2 else []) +
(returnString if len(returnString) > 2 else [])).strip()
if dString: if dString:
dString=re.sub(r'([^\\])"', r'\g<1>\"', dString) dString = re.sub(r'([^\\])"', r'\g<1>\"', dString)
s = '%%feature("docstring") OpenMM::%s::%s "%s";' \ s = '%%feature("docstring") OpenMM::%s::%s "%s";' \
% (shortClassName, methName, dString) % (shortClassName, methName, dString)
self.fOutDocstring.write("%s\n" % s) self.fOutDocstring.write("%s\n" % s)
self.fOutDocstring.write("\n\n")
#print "Done write Docstring info\n"
self.fOutDocstring.write("\n\n")
def writeSwigFile(self): def writeSwigFile(self):
......
...@@ -51,33 +51,33 @@ ...@@ -51,33 +51,33 @@
%pythoncode %{ %pythoncode %{
def getState(self, def getState(self, getPositions=False, getVelocities=False,
getPositions=False, getForces=False, getEnergy=False, getParameters=False,
getVelocities=False, enforcePeriodicBox=False, groups=-1):
getForces=False, """Get a State object recording the current state information stored in this context.
getEnergy=False,
getParameters=False, Parameters
enforcePeriodicBox=False, ----------
groups=-1): getPositions : bool=False
""" whether to store particle positions in the State
getState(self, getPositions=False, getVelocities=False, getForces=False, getVelocities : bool=False
getEnergy=False, getParameters=False, enforcePeriodicBox=False, whether to store particle velocities in the State
groups=-1) -> State getForces : bool=False
whether to store the forces acting on particles in the State
Get a State object recording the current state information stored in this context. getEnergy : bool=False
whether to store potential and kinetic energy in the State
Parameters: getParameter : bool=False
- getPositions (bool=False) whether to store particle positions in the State whether to store context parameters in the State
- getVelocities (bool=False) whether to store particle velocities in the State enforcePeriodicBox : bool=False
- getForces (bool=False) whether to store the forces acting on particles in the State if false, the position of each particle will be whatever position
- getEnergy (bool=False) whether to store potential and kinetic energy in the State is stored in the Context, regardless of periodic boundary conditions.
- getParameter (bool=False) whether to store context parameters in the State If true, particle positions will be translated so the center of
- enforcePeriodicBox (bool=False) if false, the position of each particle will be whatever position is stored in the Context, regardless of periodic boundary conditions. If true, particle positions will be translated so the center of every molecule lies in the same periodic box. every molecule lies in the same periodic box.
- groups (set={0,1,2,...,31}) a set of indices for which force groups groups : set={0,1,2,...,31}
to include when computing forces and energies. The default value a set of indices for which force groups to include when computing
includes all groups. groups can also be passed as an unsigned integer forces and energies. The default value includes all groups. groups
interpreted as a bitmask, in which case group i will be included if can also be passed as an unsigned integer interpreted as a bitmask,
(groups&(1<<i)) != 0. in which case group i will be included if (groups&(1<<i)) != 0.
""" """
getP, getV, getF, getE, getPa, enforcePeriodic = map(bool, getP, getV, getF, getE, getPa, enforcePeriodic = map(bool,
(getPositions, getVelocities, getForces, getEnergy, getParameters, (getPositions, getVelocities, getForces, getEnergy, getParameters,
...@@ -208,33 +208,32 @@ Parameters: ...@@ -208,33 +208,32 @@ Parameters:
getParameters=False, getParameters=False,
enforcePeriodicBox=False, enforcePeriodicBox=False,
groups=-1): groups=-1):
""" """Get a State object recording the current state information about one copy of the system.
getState(self,
copy, Parameters
getPositions = False, ----------
getVelocities = False, copy : int
getForces = False, the index of the copy for which to retrieve state information
getEnergy = False, getPositions : bool=False
getParameters = False, whether to store particle positions in the State
enforcePeriodicBox = False, getVelocities : bool=False
groups = -1) whether to store particle velocities in the State
-> State getForces : bool=False
whether to store the forces acting on particles in the State
Get a State object recording the current state information about one copy of the system. getEnergy : bool=False
whether to store potential and kinetic energy in the State
Parameters: getParameter : bool=False
- copy (int) the index of the copy for which to retrieve state information whether to store context parameters in the State
- getPositions (bool=False) whether to store particle positions in the State enforcePeriodicBox : bool=False
- getVelocities (bool=False) whether to store particle velocities in the State if false, the position of each particle will be whatever position
- getForces (bool=False) whether to store the forces acting on particles in the State is stored in the Context, regardless of periodic boundary conditions.
- getEnergy (bool=False) whether to store potential and kinetic energy in the State If true, particle positions will be translated so the center of
- getParameter (bool=False) whether to store context parameters in the State every molecule lies in the same periodic box.
- enforcePeriodicBox (bool=False) if false, the position of each particle will be whatever position is stored in the Context, regardless of periodic boundary conditions. If true, particle positions will be translated so the center of every molecule lies in the same periodic box. groups : set={0,1,2,...,31}
- groups (set={0,1,2,...,31}) a set of indices for which force groups a set of indices for which force groups to include when computing
to include when computing forces and energies. The default value forces and energies. The default value includes all groups. groups
includes all groups. groups can also be passed as an unsigned integer can also be passed as an unsigned integer interpreted as a bitmask,
interpreted as a bitmask, in which case group i will be included if in which case group i will be included if (groups&(1<<i)) != 0.
(groups&(1<<i)) != 0.
""" """
getP, getV, getF, getE, getPa, enforcePeriodic = map(bool, getP, getV, getF, getE, getPa, enforcePeriodic = map(bool,
(getPositions, getVelocities, getForces, getEnergy, getParameters, (getPositions, getVelocities, getForces, getEnergy, getParameters,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment