Skip to content

Commit cbd4353

Browse files
committed
Fix P_xcfg.toLines function.
Do not generate formatting function with exec. Use the Python `format` function and its mini-language instead.
1 parent be7005a commit cbd4353

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/diffpy/Structure/Parsers/P_xcfg.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,36 +338,31 @@ def toLines(self, stru):
338338
if p_allUzero:
339339
pass
340340
elif p_allUiso:
341-
p_auxiliaries.append( ('Uiso', 'a.U[0,0]') )
341+
p_auxiliaries.append(('Uiso', 'uflat[0]'))
342342
else:
343-
p_auxiliaries.extend([ ('U11', 'a.U[0,0]'),
344-
('U22', 'a.U[1,1]'),
345-
('U33', 'a.U[2,2]') ])
343+
p_auxiliaries.extend([('U11', 'uflat[0]'),
344+
('U22', 'uflat[4]'),
345+
('U33', 'uflat[8]')])
346346
# check if there are off-diagonal elements
347347
allU = numpy.array([a.U for a in stru])
348348
if numpy.any(allU[:,0,1] != 0.0):
349-
p_auxiliaries.append( ('U12', 'a.U[0,1]') )
349+
p_auxiliaries.append(('U12', 'uflat[1]'))
350350
if numpy.any(allU[:,0,2] != 0.0):
351-
p_auxiliaries.append( ('U13', 'a.U[0,2]') )
351+
p_auxiliaries.append(('U13', 'uflat[2]'))
352352
if numpy.any(allU[:,1,2] != 0.0):
353-
p_auxiliaries.append( ('U23', 'a.U[1,2]') )
353+
p_auxiliaries.append(('U23', 'uflat[5]'))
354354
# count entries
355-
p_entry_count = 6 - 3*p_NO_VELOCITY + len(p_auxiliaries)
355+
p_entry_count = (3 if p_NO_VELOCITY else 6) + len(p_auxiliaries)
356356
lines.append("entry_count = %d" % p_entry_count)
357357
# add auxiliaries
358358
for i in range(len(p_auxiliaries)):
359359
lines.append("auxiliary[%d] = %s [au]" % (i, p_auxiliaries[i][0]))
360-
# now define p_entry_line function for representing atom properties
361-
p_exprs = [ "def p_entry_line(a, p_A, p_dxyz):",
362-
" fields = list( a.xyz/p_A+p_dxyz )" ]
360+
# now define entry format efmt for representing atom properties
361+
fmwords = ["{pos[0]:.8g}", "{pos[1]:.8g}", "{pos[2]:.8g}"]
363362
if not p_NO_VELOCITY:
364-
p_exprs.append( \
365-
" fields += [ a.v[0], a.v[1], a.v[2] ]" )
366-
p_exprs += [" fields += [ " +
367-
",".join([e for p,e in p_auxiliaries]) + " ]",
368-
" line = ' '.join([ '%.8g' % x for x in fields ])",
369-
" return line" ]
370-
exec "\n".join(p_exprs)
363+
fmwords += ["{v[0]:.8g}", "{v[1]:.8g}", "{v[2]:.8g}"]
364+
fmwords += (('{' + e + ':.8g}') for p, e in p_auxiliaries)
365+
efmt = ' '.join(fmwords)
371366
# we are ready to output atoms:
372367
lines.append("")
373368
p_element = None
@@ -376,7 +371,11 @@ def toLines(self, stru):
376371
p_element = a.element
377372
lines.append("%.4f" % AtomicMass.get(p_element, 0.0))
378373
lines.append(p_element)
379-
lines.append(p_entry_line(a, p_A, p_dxyz))
374+
pos = a.xyz / p_A + p_dxyz
375+
v = None if p_NO_VELOCITY else a.v
376+
uflat = numpy.ravel(a.U)
377+
entry = efmt.format(pos=pos, v=v, uflat=uflat, a=a)
378+
lines.append(entry)
380379
return lines
381380
# End of toLines
382381

0 commit comments

Comments
 (0)