Skip to content

Commit

Permalink
Compile AgML under python 3 (#646)
Browse files Browse the repository at this point in the history
This is a draft PR for compiling AgML using python 3.

There were only a few types of changes needed to get AgML to run under
python 3.
1) Python 3 requires () in print and presumably assert statements.
2) Exceptions have been moved from the "exceptions" to the "builtins"
module.
3) Python 3 needs the full path to the module, even if we are importing
the module from a module in the same directory.
4) Python 3 requires consistent use of spaces vs tabs for indentation
purposes (showed up in one file... I think when I went on
a quest for a new editor... as if emacs and vi are not enough.)
5) Python3 has changed dictionaries. The iteritems() method is removed.
The iter() method, which is common between python 2 and 3, should be
used instead. (Slightly slower... and return list may now be bound
differently between python2 and 3...)
5a) Minor issue w/ using "pop" to access dictionary keys resulting from
above... get was a perfectly good substitute
6) And there is something in the legacy compat
pams/geometry/tpcegeo/tpcegeo.g file that throws a unicode error.
Ignoring the error seems to fix the problem w/ no observable difference
in the output.

The refactoring above may or may not leave the geometry model invariant.
It is sufficient to show that the output of the AgML codes is identical,
up to whitespace differences and irrelevant re-orderings of declaration
statements.

On the c++ side, we are good. The codes produced using python2 is
equivalent to the python3 code. Some differences are observed in the
order in which (for instance) shape parameters are output. This is a
consequence of the change from iteritems() to iter()... and if I took
the python2 output from after the refactoring, I would probably not even
see this difference.

On the Mortran side, we have additional differences. The rules for
breaking lines at column 72 seem to be broken. Will need to solve this
problem before converting this from draft.

---------

Co-authored-by: Dmitri Smirnov <[email protected]>
Co-authored-by: Yuri Fisyak <[email protected]>
  • Loading branch information
3 people authored Jan 17, 2025
1 parent f8a6955 commit e266c8b
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 221 deletions.
11 changes: 4 additions & 7 deletions mgr/Dyson/Export/AgDL.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Handler import Handler
from Dyson.Export.Handler import Handler
import Dyson.Utils.Shapes

export_comments = False
Expand Down Expand Up @@ -45,7 +45,6 @@ def __init__(self,firstKey=None,keylist=[]):

self.firstKey = firstKey
self.keylist = keylist
# print firstKey

def startElement(self,tag,attr):
out = ''
Expand All @@ -62,7 +61,7 @@ def startElement(self,tag,attr):
if value != None: out += '%s=%s '%( key, value.strip() )
except KeyError:
pass
for key,value in attr.iteritems():
for key,value in attr.items():
if value != None: out += '%s=%s '%( key, value.strip() )
out = sanitize(out)
out = '%s '%( tag.upper() ) + out + ';'
Expand All @@ -77,8 +76,6 @@ def __init__(self,firstKey=None,keylist=[]):
def startElement(self,tag,attr):
global _depth
out = ''
# if ( tag=='Placement' ):
# print "PLACEMENT: 1st=%s"%self.firstKey
if ( self.firstKey != None ):
try:
firstValue = attr.pop(self.firstKey)
Expand All @@ -93,7 +90,7 @@ def startElement(self,tag,attr):
if ( value != None ): out += '%s=%s '%( key, value.strip() )
except KeyError:
pass
for key,value in attr.iteritems():
for key,value in attr.items():
if value != None: out += '%s=%s '%( key, value.strip() )
out = '%s '%tag.upper() + sanitize(out) + '{'

Expand Down Expand Up @@ -144,7 +141,7 @@ def __call__(self,line):
output += _prepend
i+=1
output += line
print output
print(output)


# ====================================================================================================
Expand Down
8 changes: 4 additions & 4 deletions mgr/Dyson/Export/AgML.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ def startElement(self,tag,attr):
if value != None: out += '%s="%s" '%( key, value )
except KeyError:
pass
for key,value in attr.iteritems():
for key,value in attr.items():
if value != None: out += '%s="%s" '%( key, value )
out = sanitize(out)
out = '<%s '%( tag ) + out + ' />'
Expand Down Expand Up @@ -1162,7 +1162,7 @@ def startElement(self,tag,attr):
if ( value != None ): out += '%s="%s" '%( key, value )
except KeyError:
pass
for key,value in attr.iteritems():
for key,value in attr.items():
if value != None: out += '%s="%s" '%( key, value )
out = '<%s '%tag + sanitize(out) + ' >'
#out += '>'
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def __call__(self,line):
output += _prepend
i+=1
output += line
print output
print(output)


# ====================================================================================================
Expand Down Expand Up @@ -1824,7 +1824,7 @@ def startElement(self,tag,attr):
# Perform %var --> material:var and ag_var --> material:var substitution
#
myattr = {}
for key,value in attr.iteritems():
for key,value in attr.items():
myattr[key] = ag_variable( attr[key], 'material' )
Operator.startElement(self,tag,myattr)

Expand Down
5 changes: 4 additions & 1 deletion mgr/Dyson/Export/AgMLExceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
from exceptions import Exception
try:
from exceptions import Exception
except ImportError:
from builtins import Exception


from Dyson.Utils.Shapes import listOfShapes
Expand Down
56 changes: 17 additions & 39 deletions mgr/Dyson/Export/AgROOT.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Handler import Handler
from Dyson.Export.Handler import Handler

import Dyson.Utils.Shapes
from Dyson.Utils.Shapes import shape_params
Expand All @@ -8,7 +8,7 @@
import re

# Exception handling
from AgMLExceptions import ContentError, MissingError, AgmlArrayError, AgmlNameError, AgmlCommentError, AgmlShapeError, AgmlAttributeWarning, AgmlFillMissingVarError, MixtureComponentError, AgmlMissingAttributeWarning
from Dyson.Export.AgMLExceptions import ContentError, MissingError, AgmlArrayError, AgmlNameError, AgmlCommentError, AgmlShapeError, AgmlAttributeWarning, AgmlFillMissingVarError, MixtureComponentError, AgmlMissingAttributeWarning

#enable_warnings = os.getenv('AGML_WARNINGS',False)
#enable_warnings = ( os.getenv('STAR','...adev').find('adev') < 0 )
Expand Down Expand Up @@ -212,7 +212,7 @@ def checkAttributes( tag, attr, mylist, skip=[], warning=True ):
to the expected values provided in mylist. Keys provided in
the except list will not be checked.
"""
for key,value in attr.iteritems():
for key,value in attr.items():
if key in skip: continue
if key.lower() in skip: continue
key = key.lower()
Expand Down Expand Up @@ -353,10 +353,6 @@ def replacements( line ):

for struct in document.structs:

# if struct in myline:
# print "----------------------------------------------------------"
# print ":in: %s"%myline

# Lower case struct to be certain
struct = struct.lower()

Expand All @@ -365,7 +361,7 @@ def replacements( line ):
# myline = myline.replace(',',')(')

# Loop over all variables in this struct.
for key,value in _struct_dims.iteritems(): # isnt this backward?
for key,value in _struct_dims.items(): # isnt this backward?

if ( key not in myline.lower() ): continue # can skip finditer

Expand Down Expand Up @@ -406,21 +402,10 @@ def replacements( line ):
New = Old.replace('(','[int(')
New = New.replace(')',')-1]')

#print match
#print New

myline = myline.replace(Old,New)



# if struct in myline:
# print ":out: %s"%myline







#
# Replace single quote for double quote
Expand Down Expand Up @@ -614,10 +599,6 @@ def characters(self,content):
for flag in content.split(' '):
if len(flag.strip()):
self.flags.append(flag)
## name = flag[:4]
## print '%s::setup();' % flag
## print '%s::Module *%s = new %s::Module();' % ( flag, name, flag )
## print '%s -> ConstructGeometry();' %name


def endElement(self,tag):
Expand Down Expand Up @@ -1696,7 +1677,6 @@ def endElement(self,tag):
if i<len(self.arglist)-1:
mydef += ','
mydef += ') { return %s; }' % self.rvalue
#print mydef

# Add this inline function to the implementation file
document.impl( mydef, unit=self.name )
Expand Down Expand Up @@ -1966,13 +1946,13 @@ def startElement(self,tag,attr):
try:
self.parent.addVar(name,type,dim,value,comment)
except AttributeError:
print 'AttributeError: %s does not implement addVar' % self.parent
print ' : name %s'%name
print ' : type %s'%type
print ' : dim %s'%dim
print ' : value %s'%value
print ' : comment %s'%comment
assert 2+2==5
print('AttributeError: %s does not implement addVar' % self.parent)
print(' : name %s'%name)
print(' : type %s'%type)
print(' : dim %s'%dim)
print(' : value %s'%value)
print(' : comment %s'%comment)
assert(2+2==5)

self.type = type

Expand Down Expand Up @@ -2568,7 +2548,6 @@ def endElement(self,tag):
output += '// %s_docum.%s = "%s";\n'%(name,var,com)

output += "%s.fill();\n"%name.upper()
#print output

document.impl( output, unit=current )

Expand Down Expand Up @@ -3005,7 +2984,7 @@ def startElement(self,tag,attr):
"""
block = attr.get('block')
keys=[]
for key,value in attr.iteritems():
for key,value in attr.items():
keys.append(key)


Expand All @@ -3025,15 +3004,15 @@ def startElement(self,tag,attr):
if count:
document.impl('{ // Paramters passed in via the Create operator', unit=current );
document.impl('AgCreate createin("%s");'%block, unit=current )
for key,value in shape.iteritems():
for key,value in shape.items():
document.impl('createin.par("%s")=%s;'%(key,value), unit=current)
document.impl('_create = createin;', unit=current )
document.impl('}', unit=current );


document.impl( '{', unit=current );
document.impl( 'AgShape myshape; // undefined shape',unit=current )
for key,value in shape.iteritems():
for key,value in shape.items():
value=value.lower()
value=replacements(value)
document.impl( '/// Set shape par: %s = %s'%(key,value), unit=current )
Expand Down Expand Up @@ -3080,7 +3059,7 @@ def startElement(self,tag,attr):

# Check validity of attributes and issue warning if we are provided
# an unknown attribute
for key,value in attr.iteritems():
for key,value in attr.items():
if key=='block': continue
if key=='into' : continue
key = key.lower()
Expand Down Expand Up @@ -3125,7 +3104,6 @@ def startElement(self,tag,attr):
val = attr.get(key,None)
if ( val != None ):
pos = "%s=%s"%(key,val);
#print "Add pos=%s" %pos
self.pos.append(pos)

# And next the position arguements
Expand Down Expand Up @@ -3191,7 +3169,7 @@ def startElement(self,tag,attr):
parent = parent.parent

if into==None:
print 'AgROOT Warning: parent of block %s not in stack WTF?' % block
print('AgROOT Warning: parent of block %s not in stack WTF?' % block)



Expand Down Expand Up @@ -3918,7 +3896,7 @@ def setParent(self,p): self.parent = p


if ( 1 ):
print ag_variable( 'material::dens*vacuum')
print(ag_variable( 'material::dens*vacuum'))


if ( 0 ):
Expand Down
Loading

0 comments on commit e266c8b

Please sign in to comment.