Use generators, now that Cython supports them
[pycomedi.git] / pycomedi / device.pyx
index e8329d1423a9a987b5bd7267dbb448ce145cd76b..420bf4afbe0b22df215ab15da29742815720cb1d 100644 (file)
@@ -1,4 +1,6 @@
+# -*- coding: utf-8 -*-
 # Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
+#                         Éric Piel <piel@delmic.com>
 #
 # This file is part of pycomedi.
 #
 import os as _os
 cimport libc.stdlib as _stdlib
 
-from pycomedi import LOG as _LOG
-from pycomedi import PyComediError as _PyComediError
-cimport _comedi_h
-cimport _comedilib_h
-import _error
-from calibration import Calibration as _Calibration
-from instruction cimport Insn as _Insn
-from instruction import Insn as _Insn
-from subdevice import Subdevice as _Subdevice
+from . import LOG as _LOG
+from . import PyComediError as _PyComediError
+from pycomedi cimport _comedi_h
+from pycomedi cimport _comedilib_h
+from . import _error
+from . import calibration as _calibration
+from pycomedi.device_holder cimport DeviceHolder as _DeviceHolder
+from . import device_holder as _device_holder
+from pycomedi cimport instruction as _instruction
+from . import instruction as _instruction
+from . import subdevice as _subdevice
 
 
-cdef class Device (object):
+cdef class Device (_DeviceHolder):
     """A Comedi device
 
     >>> from . import constant
@@ -97,7 +101,6 @@ cdef class Device (object):
     >>> d.close()
     """
     def __cinit__(self):
-        self.device = NULL
         self.file = None
         self.filename = None
 
@@ -226,7 +229,7 @@ cdef class Device (object):
         Returns the number of successfully completed instructions.
         """
         cdef _comedi_h.comedi_insnlist il
-        cdef _Insn i
+        cdef _instruction.Insn i
         il.n_insns = len(insnlist)
         if il.n_insns == 0:
             return
@@ -248,7 +251,7 @@ cdef class Device (object):
             _error.raise_error(function_name='comedi_do_insnlist', ret=ret)
         return ret
 
-    cpdef do_insn(self, _Insn insn):
+    cpdef do_insn(self, _instruction.Insn insn):
         """Preform a single instruction.
 
         Returns an instruction-specific integer.
@@ -281,23 +284,19 @@ cdef class Device (object):
         """
         if path is None:
             path = self.get_default_calibration_path()
-        c = _Calibration(device=self)
+        c = _calibration.Calibration(device=self)
         c.from_file(path)
         return c
 
     # extensions to make a more idomatic Python interface
 
     def insn(self):
-        return _Insn()
+        return _instruction.Insn()
 
     def subdevices(self, **kwargs):
         "Iterate through all available subdevices."
-        ret = []
         for i in range(self.get_n_subdevices()):
-            #yield self.subdevice(i, **kwargs)
-            # Generators are not supported in Cython 0.14.1
-            ret.append(self.subdevice(i, **kwargs))
-        return ret
+            yield self.subdevice(i, **kwargs)
 
-    def subdevice(self, index, factory=_Subdevice, **kwargs):
+    def subdevice(self, index, factory=_subdevice.Subdevice, **kwargs):
         return factory(device=self, index=index, **kwargs)