Skip to content

Commit b1ae5df

Browse files
author
David Walker
committed
Remove pytest, add documentation
1 parent d73bdd9 commit b1ae5df

7 files changed

Lines changed: 94 additions & 31 deletions

File tree

README.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ in the root of the source tree:
5959

6060
This will automatically install the package for you.
6161

62+
Install with Tornado Support
63+
----------------------------
64+
65+
::
66+
pip install xbee[tornado]
67+
68+
6269
Documentation
6370
=============
6471

@@ -80,7 +87,9 @@ PySerial
8087
Additional Dependencies
8188
-----------------------
8289

83-
To run automated tests: `Nose <https://github.com/nose-devs/nose/>`_
90+
If wanting to use the Tornado IOLoop: `Tornado <http://www.tornadoweb.org/>`_
91+
92+
To run automated tests: `pytest <https://docs.pytest.org>`_
8493

8594
To build the documentation: `Sphinx <http://sphinx-doc.org/>`_
8695

@@ -101,3 +110,4 @@ Contributors
101110
* Amit Synderman
102111
* Marco Sangalli
103112
* James Saunders james@saunders-family.net
113+
* David Walker dwalker@n.io

docs/source/index.rst

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ Usage
4242
must be enabled. For instructions about how to do this, see
4343
the documentation for your XBee device.
4444

45-
Synchonous Mode
45+
.. note::
46+
The default implementation of the python-xbee library is to
47+
use threads. There is an implementation that utilizes the
48+
Tornado IOLoop, if imported.
49+
50+
Threaded Synchonous Mode
4651
~~~~~~~~~~~~~~~
4752

4853
The following code demonstrates a minimal use-case for the
@@ -67,7 +72,7 @@ print out any data frames which arrive from a connected XBee
6772
device. Be aware that wait_read_frame() will block until
6873
a valid frame is received from the associated XBee device.
6974

70-
Asynchronous Mode
75+
Threaded Asynchronous Mode
7176
~~~~~~~~~~~~~~~~~
7277

7378
The xbee package is used only slightly differently when
@@ -110,6 +115,46 @@ to handle receiving and processing incoming data from an
110115
XBee device. This example is functionally equivalent to the non-asyncronous
111116
example above.
112117

118+
Tornado IOLoop
119+
~~~~~~~~~~~~~~~~~~
120+
Tornado provides a simple and easy to use IOLoop for asynchronous listening
121+
for XBee communications. The library usage is seemingly identical to the
122+
threaded implementation, excepting importing and yielding. The example
123+
highlights the key differences::
124+
125+
import serial
126+
from tornado import ioloop, gen
127+
from xbee.tornado import XBee
128+
129+
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
130+
131+
def print_data(data):
132+
"""
133+
This method is called whenever data is received
134+
from the associated XBee device. Its first and
135+
only argument is the data contained within the
136+
frame.
137+
"""
138+
print data
139+
140+
@gen.coroutine
141+
def main():
142+
xbee = XBee(serial_port, callback=print_data)
143+
144+
try:
145+
while True:
146+
yield gen.sleep(0.001)
147+
except KeyboardInterrupt:
148+
ioloop.IOLoop.current().stop()
149+
finally
150+
xbee.halt()
151+
serial_port.close()
152+
153+
ioloop.IOLoop.current().spawn_callback(main)
154+
ioloop.IOLoop.current().start()
155+
ioloop.IOLoop.current().close()
156+
157+
113158
Additional Examples
114159
~~~~~~~~~~~~~~~~~~~
115160

examples/tornado_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def main():
3131
xbee.send('at', frame_id='B', command='DL')
3232
except KeyboardInterrupt:
3333
ioloop.IOLoop.current().stop()
34-
return
3534
finally:
3635
xbee.halt()
3736
ser.close()

xbee/tornado/tests/test_base.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
Tests the XBeeBase superclass module for XBee API conformance.
99
"""
1010

11-
import pytest
12-
pytest.importorskip("tornado")
11+
import unittest
12+
from xbee.tornado import has_tornado
1313

14-
from tornado.testing import AsyncTestCase, gen_test
15-
from tornado.test.util import unittest
16-
from xbee.tornado.base import XBeeBase
17-
from xbee.tests.Fake import Serial
14+
if not has_tornado:
15+
raise unittest.SkipTest("Requires Tornado")
16+
17+
from tornado.testing import AsyncTestCase, gen_test # noqa
18+
from tornado.test.util import unittest # noqa
19+
from xbee.tornado.base import XBeeBase # noqa
20+
from xbee.tests.Fake import Serial # noqa
1821

1922

2023
class TestReadFromDevice(AsyncTestCase):

xbee/tornado/tests/test_digimesh.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
77
Tests the XBee DigiMesh implementation class for API compliance
88
"""
9-
import pytest
10-
pytest.importorskip("tornado")
11-
129
import unittest
13-
from xbee.tornado.digimesh import DigiMesh
10+
from xbee.tornado import has_tornado
11+
12+
if not has_tornado:
13+
raise unittest.SkipTest("Requires Tornado")
14+
15+
from xbee.tornado.digimesh import DigiMesh # noqa
1416

1517

1618
class TestDigiMesh(unittest.TestCase):

xbee/tornado/tests/test_ieee.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
88
Tests the XBee (IEEE 802.15.4) implementation class for XBee API compliance
99
"""
10-
import pytest
11-
pytest.importorskip("tornado")
12-
13-
from xbee.tests.Fake import Serial
14-
from xbee.tornado.ieee import XBee
15-
from xbee.frame import APIFrame
16-
from xbee.python2to3 import intToByte, stringToBytes
17-
from tornado.testing import AsyncTestCase, gen_test
18-
from tornado.test.util import unittest
19-
import sys
20-
import pytest
21-
import traceback
10+
import unittest
11+
from xbee.tornado import has_tornado
12+
13+
if not has_tornado:
14+
raise unittest.SkipTest("Requires Tornado")
15+
16+
from xbee.tests.Fake import Serial # noqa
17+
from xbee.tornado.ieee import XBee # noqa
18+
from xbee.frame import APIFrame # noqa
19+
from xbee.python2to3 import intToByte, stringToBytes # noqa
20+
from tornado.testing import AsyncTestCase, gen_test # noqa
21+
from tornado.test.util import unittest # noqa
22+
import sys # noqa
23+
import traceback # noqa
2224

2325

2426
class InitXBee(AsyncTestCase):

xbee/tornado/tests/test_zigbee.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
77
Tests the XBee ZB (ZigBee) implementation class for API compliance
88
"""
9-
import pytest
10-
pytest.importorskip("tornado")
11-
129
import unittest
13-
from xbee.tornado.zigbee import ZigBee
14-
from xbee.tests.Fake import Serial
10+
from xbee.tornado import has_tornado
11+
12+
if not has_tornado:
13+
raise unittest.SkipTest("Requires Tornado")
14+
15+
from xbee.tornado.zigbee import ZigBee # noqa
16+
from xbee.tests.Fake import Serial # noqa
1517

1618

1719
class TestZigBee(unittest.TestCase):

0 commit comments

Comments
 (0)