ソースを参照

Changed Sink.output to a property, fix README

Rather than having one method doing double-duty as getter and setter,
with ugly syntax, why not use a property?  Now the Sink's output can
simply be read and written as a boolean.

The README has been updated to show how to manipulate the new SinkConfig
objects, as well as what the output really looks like now.
Clara Hobbs 7年前
コミット
3226e3efd7
3個のファイルの変更26行の追加29行の削除
  1. 6
    6
      README.rst
  2. 16
    19
      pdbuddy/__init__.py
  3. 4
    4
      test_pdbuddy/__init__.py

+ 6
- 6
README.rst ファイルの表示

@@ -11,6 +11,7 @@ Features
11 11
 -  Configuration is represented as a SinkConfig object
12 12
 -  SinkConfig objects can be manipulated locally and written to the
13 13
    device with one method call
14
+-  Allows control of whether or not the output is enabled
14 15
 
15 16
 Examples
16 17
 --------
@@ -23,7 +24,7 @@ Open the first PD Buddy Sink device and read its configuration
23 24
     >>> import pdbuddy
24 25
     >>> pdbs = pdbuddy.Sink(list(pdbuddy.Sink.get_devices())[0])
25 26
     >>> pdbs.get_cfg()
26
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.NONE, v=5000, i=3000)
27
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.NONE: 0>, v=5000, i=3000)
27 28
     >>> print(pdbs.get_cfg())
28 29
     status: valid
29 30
     flags: (none)
@@ -36,11 +37,10 @@ Locally manipulate a SinkConfig object
36 37
 ::
37 38
 
38 39
     >>> cfg = pdbs.get_cfg()
39
-    >>> cfg.v = 20000
40
-    >>> cfg.i = 2250
41
-    >>> cfg.flags |= pdbuddy.SinkFlags.GIVEBACK
40
+    >>> cfg = cfg._replace(v=20000, i=2250)
41
+    >>> cfg = cfg._replace(flags=cfg.flags | pdbuddy.SinkFlags.GIVEBACK)
42 42
     >>> cfg
43
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.GIVEBACK, v=20000, i=2250)
43
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.GIVEBACK: 1>, v=20000, i=2250)
44 44
 
45 45
 Write the SinkConfig object to flash
46 46
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -50,7 +50,7 @@ Write the SinkConfig object to flash
50 50
     >>> pdbs.set_tmpcfg(cfg)
51 51
     >>> pdbs.write()
52 52
     >>> pdbs.get_cfg()
53
-    SinkConfig(status=SinkStatus.VALID, flags=SinkFlags.GIVEBACK, v=20000, i=2250)
53
+    SinkConfig(status=<SinkStatus.VALID: 2>, flags=<SinkFlags.GIVEBACK: 1>, v=20000, i=2250)
54 54
 
55 55
 Requirements
56 56
 ------------

+ 16
- 19
pdbuddy/__init__.py ファイルの表示

@@ -156,29 +156,25 @@ class Sink:
156 156
         """Blinks the LED quickly"""
157 157
         self.send_command("identify")
158 158
 
159
-    def output(self, state=None):
160
-        """Gets or sets the state of a Sink's output
159
+    @property
160
+    def output(self):
161
+        """The state of the Sink's output
161 162
 
162 163
         Raises KeyError if the ``output`` command is not available on the Sink.
163 164
         Raises ValueError if an invalid output is read.
164
-
165
-        :param state: optional value of the output to set
166
-
167
-        :returns: the output state if state is None, None otherwise
168 165
         """
169
-        # With no parameter, return the output state
170
-        if state is None:
171
-            value = self.send_command("output")
172
-            if value[0] == b"enabled":
173
-                return True
174
-            elif value[0] == b"disabled":
175
-                return False
176
-            else:
177
-                # If unexpected text is returned, raise an exception indicating a
178
-                # firmware error
179
-                raise ValueError("unknown output state")
166
+        value = self.send_command("output")
167
+        if value[0] == b"enabled":
168
+            return True
169
+        elif value[0] == b"disabled":
170
+            return False
171
+        else:
172
+            # If unexpected text is returned, raise an exception indicating a
173
+            # firmware error
174
+            raise ValueError("unknown output state")
180 175
 
181
-        # With a parameter, set the output state
176
+    @output.setter
177
+    def output(self, state):
182 178
         if state:
183 179
             self.send_command("output enable")
184 180
         else:
@@ -218,7 +214,8 @@ class SinkConfig(namedtuple("SinkConfig", "status flags v i")):
218 214
     ``status`` should be a `SinkStatus` object.  ``flags`` should be zero or
219 215
     more `SinkFlags` values.  ``v`` is the voltage in millivolts, and ``i``
220 216
     is the current in milliamperes.  `None` is also an acceptible value for
221
-    any of the fields."""
217
+    any of the fields.
218
+    """
222 219
     __slots__ = ()
223 220
 
224 221
     def __str__(self):

+ 4
- 4
test_pdbuddy/__init__.py ファイルの表示

@@ -103,11 +103,11 @@ class SinkTestCase(unittest.TestCase):
103 103
 
104 104
     def test_output(self):
105 105
         try:
106
-            self.pdbs.output(False)
107
-            self.assertFalse(self.pdbs.output())
106
+            self.pdbs.output = False
107
+            self.assertFalse(self.pdbs.output)
108 108
 
109
-            self.pdbs.output(True)
110
-            self.assertTrue(self.pdbs.output())
109
+            self.pdbs.output = True
110
+            self.assertTrue(self.pdbs.output)
111 111
         except KeyError:
112 112
             self.skipTest("Command output not supported")
113 113
         except ValueError:

読み込み中…
キャンセル
保存