python - Mix of scalars, tuples and numpy arrays as string argument -
i using python generate povray rendering code visualization of computed data. need pass lot parameters python strings of povray code. make scrips cleaner. use tuples , arrays directly arguments string formating. this:
sign = -1 name = "temp1" nu = 0.245 boxmin =(0.01,0.01,0.01) # tuple boxmax =array([0.99,0.99,0.99]) # array povfile.write( ''' isosurface { function { %f*( %f - data3d_%s(x,y,z) ) } contained_by { box { <%f,%f,%f>,<%f,%f,%f> } } }''' %( sign, nu, name, *boxmin, *boxmax ) )
instead of this:
povfile.write( ''' isosurface { function { %f*( %f - data3d_%s(x,y,z) ) } contained_by { box { <%f,%f,%f>,<%f,%f,%f> } } }''' %( sign, nu, name, boxmin[0],boxmin[1],boxmin[2], boxmax[0],boxmax[1],boxmax[2] ) )
supposing every element want write in string list (or iterable) - if constituted 1 element - can use workaround based on list flattening.
consider this
flatten_list = lambda tupleoftuples : [element tupl in tupleoftuples element in tupl] = ['hi',] b = [23,56] c = ['bye',33,35] "{0} {1} {2} {3} {4} {5}".format(*flatten_list([a,b,c]))
result
'hi 23 56 bye 33 35'
you can use smarter algorithms flatten argument list include non-iterable elements (i.e. ones constituted 1 item). (see e.g. this answer).
Comments
Post a Comment