next up previous contents index
Next: X. Appendices Up: IX. FeResPost Python bindings Previous: IX.0 Introduction   Contents   Index

Subsections


IX.1 Python examples

As the adaptation from ruby to Python is straightforward, the current chapter is also very short. We only highlight some of the differences related to ruby and Python language differences.


IX.1.1 Iterators

The first example of an iterator with Python library is in ``PYTHON/EX02/printGroups.py'' and reads as follows:

for groupName in db.iter_groupName():
    grp = db.getGroupCopy(groupName)
    nodesNbr = grp.NbrElements
    elementsNbr = grp.NbrNodes
    mpcsNbr = grp.NbrMpcs
    coordNbr = grp.NbrCoordSys
    print("%20s%10d%10d%10d%10d"%(groupName,nodesNbr,elementsNbr,
        mpcsNbr,coordNbr))
Note:


IX.1.2 Operators

In ``Group'' and ``ResKeyList'' class, the operators work exactly the same way as with FeResPost ruby extension. See for example, the multiplication operator used in example
``PYTHON/EX03/makeMatGroups.py'':

for panelGroupName in panelGroupNames:
    panelGroup = db.getGroupCopy(panelGroupName)
    for matGrp in matGroups:
        newGrp = panelGroup * matGrp
        newGrp.Name=panelGroupName+"_"+matGrp.Name
        if newGrp.NbrElements!=0:
            db.addGroupCopy(newGrp)
On the other hand, in ``Result'' class, the operators have been repalced by corresponding methods. For example in example `PYTHON/EX05/printStressMax.py'', one calculates honeycomb out-of-plane shear stress as follows:
    targetGrp=db.getGroupCopy("pan_PZ_Honey_72")
    stress=db.getResultCopy("ORBIT_ONE_MS2_Z","Statics",
        "Stress Tensor","ElemCorners",targetGrp,[])
    sXZ=stress.deriveTensorToOneScal("Component XZ")
    sYZ=stress.deriveTensorToOneScal("Component YZ")
    scalar=Post.sqrt((sXZ.opMul(sXZ)).opAdd(sYZ.opMul(sYZ)))


IX.1.3 SQLite examples

The use of SQLite library is explained in Python documentation. With FeResPost, the BLOB object returned by ``toBlob'' method must be converted into a Python buffer using the corresponding function:

for lcName in lcNames:
    for resName in resNames:
        results=db.getAttachmentResults(xdbFileName,lcName,scNames,resName)
        if (results):
            for key,res in results.iteritems():
                print key
                sqldb.execute(
                    "insert or replace into dynam_results_1 values(?,?,?,?,?,?,?,?,?,?)",
                    [lcName,key[1],resName,res.TensorOrder,
                    res.getIntId(0),res.getIntId(1),
                    res.getRealId(0),res.getRealId(1),
                    res.Size,buffer(res.toBlob())])
            sqldb.commit()
        else:
            print "NO FOR" + lcName + resName
Note that the call to ``buffer'' method seems necessary with versions 2.* of Python, but must be removed for version 3.*.


next up previous contents index
Next: X. Appendices Up: IX. FeResPost Python bindings Previous: IX.0 Introduction   Contents   Index
FeResPost User Manual Version 4.4.0 (2017/08/27)