Author Topic: PyO2Bas - a Python wrapper for Oxygen  (Read 4872 times)

0 Members and 1 Guest are viewing this topic.

Mark0

  • Guest
PyO2Bas - a Python wrapper for Oxygen
« on: March 05, 2011, 09:46:04 AM »
I just coded a simple class to use Oxygen from Python:

pyo2bas.py
Code: Python
  1. """PyO2Bas - a Python module that wrap Oxygen"""
  2.  
  3. import ctypes
  4.  
  5. MODE_ASCIIZ = 0
  6.  
  7.  
  8. class Error(Exception):
  9.     """pyo2bas generic exception."""
  10.     def __init__(self, s):
  11.         Exception.__init__(self, s)
  12.  
  13.  
  14. class O2bas(object):
  15.     """Oxygen wrapper class."""
  16.     def __init__(self):
  17.         """Initialize the library and its functions."""
  18.         try:
  19.             self.dll = ctypes.WinDLL("oxygen.dll")
  20.         except:
  21.             raise Error("Can't load oxygen.dll!")
  22.         INT, PCHAR = ctypes.c_int, ctypes.c_char_p
  23.         self._o2_mode = self._get_proc("o2_mode", None, [INT])
  24.         self._o2_asmo = self._get_proc("o2_asmo", None, [PCHAR])
  25.         self._o2_basic = self._get_proc("o2_basic", None, [PCHAR])
  26.         self._o2_error = self._get_proc("o2_error", PCHAR, [])
  27.         self._o2_exec = self._get_proc("o2_exec", INT, [INT])
  28.         self._o2_mode(MODE_ASCIIZ)
  29.  
  30.     def _get_proc(self, name, ret, args):
  31.         """Helper to set the various functions parameters & ret values."""
  32.         func = self.dll[name]
  33.         func.restype = ret
  34.         func.argtypes = args
  35.         return func
  36.  
  37.     def geterror(self):
  38.         """Return an error string from Oxygen."""
  39.         res = self._o2_error()
  40.         if res:
  41.             return res
  42.         else:
  43.             return ""
  44.  
  45.     def asm(self, code):
  46.         """Compile Assembly code."""
  47.         self._o2_asmo(code)
  48.  
  49.     def basic(self, code):
  50.         """Compile BASIC code."""
  51.         self._o2_basic(code)
  52.  
  53.     def execute(self):
  54.         """Execute compiled code."""
  55.         return self._o2_exec(0)
  56.  

Then executing some Oxygen code is as simple as:

Code: Python
  1. import pyo2bas
  2.  
  3. o2 = pyo2bas.O2bas()
  4.  
  5. o2.basic('print "Hello, World!"')
  6. o2.execute()

Next thing when I'll have some time will be looking for a nice way to pass some vars between Oxygen & Python.


Charles Pegge

  • Guest
Re: PyO2Bas - a Python wrapper for Oxygen
« Reply #1 on: March 05, 2011, 09:20:51 PM »
Many thanks Marco, and welcome to our group.

I don't know much about Python but I think some kind of binding interface will be needed to map Python variables into Oxygen space or vice versa, as we do with thinBasic_Oxygen.

Please let me know if you need any additional functionality from Oxygen.dll.

Charles

kryton9

  • Guest
Re: PyO2Bas - a Python wrapper for Oxygen
« Reply #2 on: March 06, 2011, 02:57:44 PM »
I don't know how you guys figure this stuff out. That is really cool.

Maxim

  • Guest
Re: PyO2Bas - a Python wrapper for Oxygen
« Reply #3 on: July 20, 2011, 05:18:58 AM »
Thanks Marco.
I think it can gives the acceleration of the execution of some algorithms into Python.
My small modified version is attached as one file.