Skip to content

Commit bd18b75

Browse files
author
Jisk Attema
committed
Minor code cleanup
1 parent 7fccab1 commit bd18b75

10 files changed

Lines changed: 44 additions & 26 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Python3 bindings to the ringbuffer implementation in [PSRDada](http://psrdada.so
33

44
It allows you to connect to a ringbuffer, read header info, and read/write ringbuffer pages as numpy arrays.
55
I aim to be interoperable, and bug-for-bug compattible, with PSRDADA.
6-
It also supports reading multiple datasets form a ringbuffer for connecting to dada\_dbevent.
6+
It also supports reading multiple datasets from a ringbuffer for connecting to dada\_dbevent.
77

88
This is a proof-of-concept implementation, only functions from PSRDADA immediately necessary for my project will be implemented.
99
The code is inspired by the example code in the [cython userguide](http://cython.readthedocs.io/en/latest/src/userguide/buffer.html)

examples/read_eod_iterators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
from psrdada import Reader
1010

1111

12-
reader = Reader()
13-
14-
reader.connect(0xdbda)
12+
reader = Reader(0xdbda)
1513

1614
nheader = 0
1715
done = False

examples/read_eod_no_iterators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
from psrdada import Reader
1010

11-
reader = Reader()
12-
13-
reader.connect(0xdbda)
11+
reader = Reader(0xdbda)
1412

1513
nheader = 0
1614
while True:

examples/sum_no_iterator.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
def read_untill_end():
88
"""Connect, read all pages, and disconnect from a ringbuffer."""
9-
# Create a reader instace
10-
reader = Reader()
11-
12-
# Connect to a running ringbuffer with key 'dada'
13-
reader.connect(0xdada)
9+
# Create a reader instace and connect to a running ringbuffer
10+
reader = Reader(0xdada)
1411

1512
# loop over the pages until EOD is encountered
1613
while not reader.isEndOfData:

examples/sum_with_iterator.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
def read_untill_end():
88
"""Connect, read all pages, and disconnect from a ringbuffer."""
9-
# Create a reader instace
10-
reader = Reader()
11-
12-
# Connect to a running ringbuffer with key 'dada'
13-
reader.connect(0xdada)
9+
# Create a reader instace and connect to a running ringbuffer
10+
reader = Reader(0xdada)
1411

1512
# loop over the pages
1613
for page in reader:

examples/write_eod_iterators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def random_string(length=10):
1717
return ''.join(choice(ascii_lowercase) for i in range(length))
1818

1919

20-
writer = Writer()
21-
22-
writer.connect(0xdbda)
20+
writer = Writer(0xdbda)
2321

2422
# send 10 datasets, separated by an EOD
2523
for ndataset in range(10):

examples/write_eod_no_iterators.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ def random_string(length=10):
1717
return ''.join(choice(ascii_lowercase) for i in range(length))
1818

1919

20-
writer = Writer()
21-
22-
writer.connect(0xdbda)
20+
writer = Writer(0xdbda)
2321

2422
# send 10 datasets, separated by an EOD
2523
for ndataset in range(10):

psrdada/reader.pyx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
Reader class.
44
55
Implements reading header and data from a running PSRDada ringbuffer.
6+
Create a new reader instance::
7+
8+
reader = Reader()
9+
10+
Create a new reader instance, and connect it directly to a running ringbuffer::
11+
12+
reader = Reader(0xdada)
13+
614
"""
715
from cpython.buffer cimport PyBUF_READ
816
cimport dada_hdu
@@ -23,9 +31,14 @@ cdef class Reader(Ringbuffer):
2331
2432
Implements reading header and data from a running PSRDada ringbuffer.
2533
"""
26-
def __init__(self):
27-
self.isEndOfData = False
28-
self.isHoldingPage = False
34+
def __init__(self, key=None):
35+
"""
36+
Creates a new Reader instance, and optionally connects to a ringbuffer.
37+
38+
:param key: Optional. Identifier of the ringbuffer, typically 0xdada
39+
"""
40+
if key:
41+
self.connect(key)
2942

3043
def connect(self, key):
3144
"""

psrdada/ringbuffer.pyx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ cdef class Ringbuffer:
1212

1313
self._c_dada_hdu = dada_hdu.dada_hdu_create(self._multilog)
1414
self._key = 0xdada
15+
self.isConnected = False
16+
self.isHoldingPage = False
17+
self.isEndOfData = False
1518
self.header = dict()
1619

1720
def __dealloc__(self):

psrdada/writer.pyx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
Writer class.
44
55
Implements writing header and data from a running PSRDada ringbuffer.
6+
Create a new writer instance::
7+
8+
writer = Writer()
9+
10+
Create a new reader instance, and connect it directly to a running ringbuffer::
11+
12+
writer = Writer(0xdada)
613
"""
714

815
from cpython.buffer cimport PyBUF_WRITE
@@ -25,6 +32,15 @@ cdef class Writer(Ringbuffer):
2532
2633
Implements writing header and data to a running PSRDada ringbuffer.
2734
"""
35+
def __init__(self, key=None):
36+
"""
37+
Creates a new Reader instance, and optionally connects to a ringbuffer.
38+
39+
:param key: Optional. Identifier of the ringbuffer, typically 0xdada
40+
"""
41+
if key:
42+
self.connect(key)
43+
2844
def connect(self, key):
2945
"""
3046
Connect to a PSR DADA ringbuffer with the specified key, and lock it for writing

0 commit comments

Comments
 (0)