SIC> PYTHON Python 2.5 (r25:51908, Nov 14 2006, 22:44:28) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 Entering interactive session. Type 'Sic()' to go back to SIC. >>> |
The SIC variables are not yet available at this stage, assuming the user may not need them. You have to invoke the .get() method (with no arguments) to import them (see section 2.4 for details):
>>> pysic.get() Importing all SIC variables into Python... ... done. >>> |
SIC> TYPE test.py print "Hello world!" a = 1 a print "a value is", a # This line is a comment for i in xrange(1,4): print i if True: print "True" else: print "False" |
SIC> PYTHON test.py Hello world! a value is 1 1 2 3 True SIC> |
SIC> SIC VERIFY ON I-SIC, VERIFY is ON SIC> PYTHON test.py >>> print "Hello world!" Hello world! >>> a = 1 >>> a >>> print "a value is", a a value is 1 >>> for i in xrange(1,4): ... print i 1 2 3 >>> if True: ... print "True" ... else: ... print "False" True SIC> |
SIC> TYPE test2.py import sys for i in sys.argv: print i, type(i) SIC> PYTHON test2.py 1 qwerty "ABCD" test2.py <type 'str'> 1 <type 'str'> qwerty <type 'str'> "ABCD" <type 'str'> SIC> |
SIC> DEFINE DOUBLE A SIC> LET A 1.234 SIC> py test2.py A "A" 'A' test2.py <type 'str'> A <type 'str'> "A" <type 'str'> 1.234 <type 'str'> SIC> |
SIC> type test3.py import sicparse parser = sicparse.OptionParser() # Declare 1 known option: parser.add_option( "-m", # Short name "--myopt", # Full name dest="optval", # Variable name where option value will be stored nargs=1, # Number of arguments expected type="float", # Kind of arguments default=1.23) # Default value if option is absent # Call again add_options to declare more options # Parse: try: (options, args) = parser.parse_args() except: raise StandardError,"Invalid option" print "Success" print "Command arguments: ",args print "Options: ",options |
SIC> python test3.py ABCD Success Command arguments: ['ABCD'] Options: {'optval': 1.23} SIC> python test3.py ABCD /myopt 4.56 Success Command arguments: ['ABCD'] Options: {'optval': 4.56} SIC> |
SIC> python test3.py /home/me Usage: test3.py [options] test3.py: error: no such option: /home/me Traceback (most recent call last): File "./test3.py", line 19, in <module> raise StandardError,"Invalid option" StandardError: Invalid option SIC> python test3.py "/home/me" Success Command arguments: ['"/home/me"'] Options: {'optval': 1.23} |
SIC> type test4.py print 1/0 SIC> python test4.py Traceback (most recent call last): File "./test4.py", line 1, in <module> print 1/0 ZeroDivisionError: integer division or modulo by zero SIC>
SIC> type test5.py arg = 0 if (arg==0): raise StandardError, "Argument must not be null" else: print 1/arg SIC> python test5.py Traceback (most recent call last): File "./test5.py", line 3, in <module> raise StandardError, "Argument must not be null" StandardError: Argument must not be null SIC>
SIC> type test6.py def main(): try: print 1/0 except: print "E-SIC, Some error when printing 1/0" pysic.sicerror() # Set the SIC\PYTHON command error return # Leave the function and the script if __name__ == "__main__": main() SIC> py test6.py E-SIC, Some error when printing 1/0 SIC>
Note that these 3 methods raise an error in Sic, i.e. the flag SIC%ERROR is true on return, and that procedures will stop in such a case (if ON ERROR is PAUSE).
The last syntax gives the best integration in Sic, because in particular it hides the Python error traceback. However, this requires 1) to catch the error with a try/except clause, 2) to tell Sic an occured thanks to the method sicerror(), and 3) to leave the script right after. This last point can only be achieved by putting the whole code in a (top) function, from which you can return when required. exit is not satisfying because it performs too much cleaning, nor raise since a traceback would be back.
>>> pysic.message(pysic.seve.i,"FOO","Hello world!") I-FOO, Hello world!The seve instance contains the usual elements (f, e, w, r, i, d, t, c) which have to be used to indicate the message severity.
SIC> PYTHON print "Hello world!" Hello world! SIC> |
SIC> SIC VERIFY ON I-SIC, VERIFY is ON SIC> PYTHON print "Hello world!" >>> print "Hello world!" Hello world! SIC> |
SIC> PYTHON print 'Hello world?' >>> print 'Helloworld?' Helloworld? SIC> |