예제 #1
0
def pyfun(fun, module=None, *args):
    MDSplus.DEALLOCATE('public _py_exception')
    fun = MDSplus.Data(fun).data()
    module = MDSplus.Data(module).data()

    def getfun(fun):
        fun = str(fun)
        if module is not None:
            return __import__(str(module), globals(), locals(),
                              [fun]).__dict__[fun]
        builtins = __builtins__ if isinstance(__builtins__,
                                              dict) else __builtins__.__dict__
        if fun in builtins:
            return builtins[fun]
        try:
            return MDSplus.__dict__[fun]
        except:
            raise MDSplus.TdiUNKNOWN_VAR(fun)

    fun = getfun(fun)
    args = tuple(map(MDSplus.Data.evaluate, args))
    try:
        return fun(*args)
    except Exception as exc:
        import traceback
        traceback.print_exc()
        MDSplus.String(str(exc)).setTdiVar("_py_exception")
예제 #2
0
def Py(cmds, *arg):
    """
    Execute python commands passed as a string or string array in cmds argument.
    If you want to return the value of a variable defined in the commands pass
    the variable name in the varname argument as a string. For example:

    py(["from MDSplus import Tree","ans=Tree('main',-1).getNode('numeric').record"],"ans")

    If you want to use the global namespace for the variables pass a 1 in the isglobal argument.
    This is unusual but does let you save the variables in a process global name space which
    could be retrieved later. For example in tdi:

    py("x=42",*,1)
    .....
    py("None","x",1)

    The lock argument is no longer used but retained for compatibility.
    """
    varname = arg[0] if len(arg) > 0 else None
    #isglobal= arg[1] if len(arg)>1 else False
    arg = arg[3:] if len(arg) > 3 else []
    MDSplus.DEALLOCATE('public _py_exception')
    cmdlist = list()
    ans = 1
    for cmd in cmds:
        cmdlist.append(MDSplus.version.tostr(cmd))
    cmds = "\n".join(cmdlist)
    env = {"arg": arg, 'MDSplus': MDSplus}
    try:
        exec(compile(cmds, '<string>', 'exec'), globals(), env)
    except Exception as exc:
        _tb.print_exc()
        MDSplus.String(exc).setTdiVar("_py_exception")
        ans = 0
    if varname is not None:
        varname = MDSplus.version.tostr(varname)
        if varname in env:
            ans = env[varname]
        else:
            ans = None
    return ans
예제 #3
0
def pyfun(fun, module=None, *args):
    import MDSplus
    MDSplus.DEALLOCATE('public _py_exception')
    fun = str(fun)
    if module is not None:
        module = __import__(str(module), globals(), locals(), [fun])
        fun = module.__dict__[fun]
    elif fun in __builtins__:
        fun = __builtins__[fun]
    else:
        try:
            module = __import__('MDSplus', globals(), locals(), [fun])
            fun = module.__dict__[fun]
        except:
            raise MDSplus.TdiUNKNOWN_VAR(fun)
    args = tuple(map(MDSplus.Data.evaluate, args))

    try:
        return fun(*args)
    except Exception as exc:
        import traceback
        traceback.print_exc()
        MDSplus.String(str(exc)).setTdiVar("_py_exception")
예제 #4
0
 def tdiFunctions(self):
     m.dTRUE = m.__dict__['$TRUE']
     m.dFALSE = m.__dict__['$FALSE']
     from MDSplus import mdsExceptions as Exc
     """Test Exceptions"""
     self._doExceptionTest('abort()', Exc.TdiABORT)
     self._doExceptionTest('{,}', Exc.TdiSYNTAX)
     self._doExceptionTest('\033[[A', Exc.TdiBOMB)
     self._doExceptionTest('abs()', Exc.TdiMISS_ARG)
     self._doExceptionTest('abs("")', Exc.TdiINVDTYDSC)
     self._doExceptionTest('abs(1,2)', Exc.TdiEXTRA_ARG)
     self._doExceptionTest('"', Exc.TdiUNBALANCE)
     """Test $Missing/NoData/None"""
     self._doTdiTest('', None)
     """Test abs"""
     self._doThreeTest('abs(cmplx(3.0,4.0))', m.ABS(m.Complex64(3. + 4.j)),
                       m.Float32(5.))
     """Test abs1"""
     self._doThreeTest('abs1(cmplx(3.0,4.0))',
                       m.ABS1(m.Complex64(3. + 4.j)), m.Float32(7.))
     """Test abssq"""
     self._doThreeTest('abssq(cmplx(3.0,4.0))',
                       m.ABSSQ(m.Complex64(3. + 4.j)), m.Float32(25.))
     """Test accumulate"""
     self._doThreeTestArray('accumulate([1,2,3])',
                            m.ACCUMULATE(m.makeArray([1, 2, 3])),
                            m.Int32Array([1, 3, 6]))
     self._doThreeTestArray(
         'accumulate([[1,3,5],[2,4,6]])',
         m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]])),
         m.Int32Array([[1, 4, 9], [11, 15, 21]]))
     self._doThreeTestArray(
         'accumulate([[1,3,5],[2,4,6]],0)',
         m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]]), 0),
         m.Int32Array([[1, 4, 9], [2, 6, 12]]))
     #self._doThreeTestArray('accumulate([[1,3,5],[2,4,6]],1)',m.ACCUMULATE([[1,3,5],[2,4,6]],1),m.Int32Array([[1,3,5],[3,7,11]]))  # tdi issue
     self._doUnaryArray(
         m.Data.execute('accumulate([[1,3,5],[2,4,6]],1)'),
         m.ACCUMULATE(m.makeArray([[1, 3, 5], [2, 4, 6]]), 1).getData())
     """Test achar"""
     self._doThreeTest('achar(88)', m.ACHAR(88), m.String('X'))
     """Test ADJUSTL"""
     self._doThreeTest('adjustl(" WORD")', m.ADJUSTL(" WORD"),
                       m.String("WORD "))
     """Test ADJUSTR"""
     self._doThreeTest('adjustr("WORD ")', m.ADJUSTR("WORD "),
                       m.String(" WORD"))
     """Test AIMAG"""
     self._doThreeTest('AIMAG(CMPLX(2.0,3.0))', m.AIMAG(m.CMPLX(2., 3.)),
                       m.Float32(3.0))
     """Test AINT"""
     self._doThreeTest('aint(2.783)', m.AINT(2.783), m.Float32(2.0))
     self._doThreeTest('aint(-2.783)', m.AINT(-2.783), m.Float32(-2.0))
     """Test NE (operates on flattened array, i.e. first 3 values are compared)"""
     A, B = m.makeArray([1, 3, 5]), m.makeArray([[0, 3, 5], [0, 0, 0],
                                                 [0, 4, 8]])
     self._doThreeTestArray(
         '_A=[1,3,5],_B=[[0,3,5],[0,0,0],[0,4,8]],_A ne _B', m.NE(A, B),
         m.Uint8Array([1, 0, 0]))
     """Test NE (operates on flattened array, i.e. first 3 values are compared)"""
     self._doThreeTestArray('_A eq _B', m.EQ(A, B), m.Uint8Array([0, 1, 1]))
     """Test ALL and ANY"""
     self._doThreeTest('all([$TRUE,$FALSE,$TRUE])',
                       m.ALL(m.makeArray([1, 0, 1])), m.Uint8(0))
     self._doThreeTest('any([$TRUE,$FALSE,$TRUE])',
                       m.ANY(m.makeArray([1, 0, 1])), m.Uint8(1))
     A = 0
     self._doThreeTest('_A=0,all(_A eq _B)', m.ALL(m.EQ(A, B)), False)
     self._doThreeTest('any(_A ne _B)', m.ANY(m.NE(A, B)), True)
     self._doThreeTestArray('all(_A ne _B,0)', m.ALL(m.NE(A, B), 0),
                            m.Uint8Array([0, 0, 0]))
     self._doThreeTestArray('any(_A ne _B,0)', m.ANY(m.NE(A, B), 0),
                            m.Uint8Array([1, 0, 1]))
     self._doThreeTestArray('all(_A eq _B,1)', m.ALL(m.EQ(A, B), 1),
                            m.Uint8Array([1, 0, 0]))
     self._doThreeTestArray('any(_A ne _B,1)', m.ANY(m.NE(A, B), 1),
                            m.Uint8Array([0, 1, 1]))
     """Test allocated"""
     self.assertEqual(m.DEALLOCATE('*') >= 2,
                      True)  # deallocates _A and _B and more?
     self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(0))
     self._doTdiTest('_xyz=0,allocated("_xyz")', m.Uint8(1))
     self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(1))
     self.assertEqual(m.DEALLOCATE('*'), m.Uint8(1))
     self.assertEqual(m.ALLOCATED('_xyz'), m.Uint8(0))
     """Test AND"""
     A, B = m.makeArray([0, 0, 1, 1]), m.makeArray([0, 1, 0, 1])
     self._doThreeTestArray('_A=[0,0,1,1],_B=[0,1,0,1],_A && _B',
                            m.AND(A, B), m.Uint8Array([0, 0, 0, 1]))
     """Test AND_NOT"""
     self._doThreeTestArray('_A AND_NOT _B', m.AND_NOT(A, B),
                            m.Uint8Array([0, 0, 1, 0]))
     """Test ANINT"""
     self._doThreeTest('ANINT(2.783)', m.ANINT(2.783), m.Float32(3.0))
     """Test ARG"""
     self._doTdiTest(
         'execute("abs(arg(cmplx(3.0,4.0)) - .9272952) < .000001")',
         m.Uint8(1))
     """Test ARGD"""
     self._doTdiTest(
         'execute("abs(argd(cmplx(3.0,4.0)) - 53.1301) < .000001")',
         m.Uint8(1))
     """Test arg_of"""
     self._doThreeTest('arg_of(pub->foo(42,43))',
                       m.ARG_OF(m.Call('pub', 'foo', 42, 43)), m.Int32(42))
     self._doThreeTest('arg_of(pub->foo(42,43),1)',
                       m.ARG_OF(m.Call('pub', 'foo', 42, 43), 1),
                       m.Int32(43))
     self._doThreeTest('arg_of(1+3,1)', m.ARG_OF(m.ADD(1, 3), 1),
                       m.Int32(3))
     """Test Array"""
     self._doThreeTestArray('array(10)', m.ARRAY(10),
                            m.Float32Array([0] * 10))
     self._doThreeTestArray('array(10,0)', m.ARRAY(10, 0),
                            m.Int32Array([0] * 10))
     self._doThreeTestArray('array(10,0BU)', m.ARRAY(10, m.Uint8(0)),
                            m.Uint8Array([0] * 10))
     self._doThreeTestArray('zero(100)', m.ZERO(100),
                            m.Float32Array([0] * 100))