Skip to content

Commit 15db026

Browse files
committed
Clarify style of attribute comments in the Design Guide.
And update the core attributes to match the style.
1 parent 243279f commit 15db026

9 files changed

Lines changed: 138 additions & 85 deletions

File tree

‎docs/design_guide.rst‎

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
Design Guide
22
============
33

4-
MicroPython has created a great foundation to build upon and to make it even
5-
better for beginners we've created CircuitPython. This guide covers a number of
6-
ways the core and libraries are geared towards beginners.
4+
This guide covers a variety of development practices for CircuitPython core and library APIs. Consistency with these practices ensures that beginners can learn a pattern once and apply it throughout the CircuitPython ecosystem.
75

86
Start libraries with the cookiecutter
97
-------------------------------------
@@ -164,61 +162,74 @@ After the license comment::
164162
Class description
165163
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
166164

167-
Documenting what the object does::
165+
At the class level document what class does and how to initialize it::
168166

169167
class DS3231:
170-
"""Interface to the DS3231 RTC."""
168+
"""DS3231 real-time clock.
171169

172-
Renders as:
170+
:param ~busio.I2C i2c_bus: The I2C bus the DS3231 is connected to.
171+
:param int address: The I2C address of the device.
172+
"""
173173

174-
.. py:class:: DS3231
175-
:noindex:
174+
def __init__(self, i2c_bus, address=0x40):
175+
self._i2c = i2c_bus
176176

177-
Interface to the DS3231 RTC.
178177

179-
Data descriptor description
180-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178+
Renders as:
181179

182-
Comment is after even though its weird::
180+
.. py:class:: DS3231(i2c_bus, address=64)
181+
:noindex:
183182

184-
lost_power = i2c_bit.RWBit(0x0f, 7)
185-
"""True if the device has lost power since the time was set."""
183+
DS3231 real-time clock.
186184

187-
Renders as:
185+
:param ~busio.I2C i2c_bus: The I2C bus the DS3231 is connected to.
186+
:param int address: The I2C address of the device.
188187

189-
.. py:attribute:: lost_power
190-
:noindex:
188+
Attributes
189+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
191190

192-
True if the device has lost power since the time was set.
191+
Attributes are state on objects. (See `Getters/Setters` above for more discussion
192+
about when to use them.) They can be defined internally in a number of different
193+
ways. Each approach is enumerated below with an explanation of where the comment
194+
goes.
193195

194-
Method description
195-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
196+
Regardless of how the attribute is implemented, it should have a short
197+
description of what state it represents including the type, possible values and/or
198+
units. It should be marked as ``(read-only)`` or ``(write-only)`` at the end of
199+
the first line for attributes that are not both readable and writeable.
196200

197-
First line after the method definition::
201+
Instance attributes
202+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
198203

199-
def turn_right(self, degrees):
200-
"""Turns the bot ``degrees`` right.
204+
Comment comes from after the assignment::
201205

202-
:param float degrees: Degrees to turn right
206+
def __init__(self, drive_mode):
207+
self.drive_mode = drive_mode
208+
"""
209+
The pin drive mode. One of:
210+
211+
- `digitalio.DriveMode.PUSH_PULL`
212+
- `digitalio.DriveMode.OPEN_DRAIN`
203213
"""
204214

205215
Renders as:
206216

207-
.. py:method:: turn_right(degrees)
208-
:noindex:
217+
.. py:attribute:: drive_mode
218+
:noindex:
209219

210-
Turns the bot ``degrees`` right.
220+
The pin drive mode. One of:
211221

212-
:param float degrees: Degrees to turn right
222+
- `digitalio.DriveMode.PUSH_PULL`
223+
- `digitalio.DriveMode.OPEN_DRAIN`
213224

214225
Property description
215-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
226+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216227

217228
Comment comes from the getter::
218229

219230
@property
220231
def datetime(self):
221-
"""The current date and time"""
232+
"""The current date and time as a `time.struct_time`."""
222233
return self.datetime_register
223234

224235
@datetime.setter
@@ -228,9 +239,65 @@ Comment comes from the getter::
228239
Renders as:
229240

230241
.. py:attribute:: datetime
242+
:noindex:
243+
244+
The current date and time as a `time.struct_time`.
245+
246+
Read-only example::
247+
248+
@property
249+
def temperature(self):
250+
"""
251+
The current temperature in degrees Celcius. (read-only)
252+
253+
The device may require calibration to get accurate readings.
254+
"""
255+
return self._read(TEMPERATURE)
256+
257+
258+
Renders as:
259+
260+
.. py:attribute:: temperature
261+
:noindex:
262+
263+
The current temperature in degrees Celcius. (read-only)
264+
265+
The device may require calibration to get accurate readings.
266+
267+
Data descriptor description
268+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
269+
270+
Comment is after even though its weird::
271+
272+
lost_power = i2c_bit.RWBit(0x0f, 7)
273+
"""True if the device has lost power since the time was set."""
274+
275+
Renders as:
276+
277+
.. py:attribute:: lost_power
278+
:noindex:
279+
280+
True if the device has lost power since the time was set.
281+
282+
Method description
283+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
284+
285+
First line after the method definition::
286+
287+
def turn_right(self, degrees):
288+
"""Turns the bot ``degrees`` right.
289+
290+
:param float degrees: Degrees to turn right
291+
"""
292+
293+
Renders as:
294+
295+
.. py:method:: turn_right(degrees)
231296
:noindex:
232297

233-
The current date and time
298+
Turns the bot ``degrees`` right.
299+
300+
:param float degrees: Degrees to turn right
234301

235302
Use BusDevice
236303
--------------------------------------------------------------------------------

‎shared-bindings/analogio/AnalogIn.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4,
106106

107107
//| .. attribute:: value
108108
//|
109-
//| Read the value on the analog pin and return it. The returned value
110-
//| will be between 0 and 65535 inclusive (16-bit). Even if the underlying
111-
//| analog to digital converter (ADC) is lower resolution, the result will
112-
//| be scaled to be 16-bit.
109+
//| The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
113110
//|
114-
//| :return: the data read
115-
//| :rtype: int
111+
//| Even if the underlying analog to digital converter (ADC) is lower
112+
//| resolution, the value is 16-bit.
116113
//|
117114
STATIC mp_obj_t analogio_analogin_obj_get_value(mp_obj_t self_in) {
118115
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -130,10 +127,8 @@ const mp_obj_property_t analogio_analogin_value_obj = {
130127

131128
//| .. attribute:: reference_voltage
132129
//|
133-
//| The maximum voltage measurable. Also known as the reference voltage.
134-
//|
135-
//| :return: the reference voltage
136-
//| :rtype: float
130+
//| The maximum voltage measurable (also known as the reference voltage) as a
131+
//| `float` in Volts.
137132
//|
138133
STATIC mp_obj_t analogio_analogin_obj_get_reference_voltage(mp_obj_t self_in) {
139134
analogio_analogin_obj_t *self = MP_OBJ_TO_PTR(self_in);

‎shared-bindings/analogio/AnalogOut.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4
105105

106106
//| .. attribute:: value
107107
//|
108-
//| The value on the analog pin. The value must be between 0 and 65535
109-
//| inclusive (16-bit). Even if the underlying digital to analog converter
110-
//| is lower resolution, the input must be scaled to be 16-bit.
111-
//|
112-
//| :return: the last value written
113-
//| :rtype: int
108+
//| The value on the analog pin between 0 and 65535 inclusive (16-bit). (write-only)
114109
//|
110+
//| Even if the underlying digital to analog converter (DAC) is lower
111+
//| resolution, the value is 16-bit.
115112
STATIC mp_obj_t analogio_analogout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
116113
analogio_analogout_obj_t *self = MP_OBJ_TO_PTR(self_in);
117114
raise_error_if_deinited(common_hal_analogio_analogout_deinited(self));

‎shared-bindings/audioio/AudioOut.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(audioio_audioout_stop_obj, audioio_audioout_obj_stop);
187187

188188
//| .. attribute:: playing
189189
//|
190-
//| True when the audio sample is being output.
190+
//| True when the audio sample is being output. (read-only)
191191
//|
192192
STATIC mp_obj_t audioio_audioout_obj_get_playing(mp_obj_t self_in) {
193193
audioio_audioout_obj_t *self = MP_OBJ_TO_PTR(self_in);

‎shared-bindings/digitalio/DigitalInOut.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ const mp_obj_property_t digitalio_digitalinout_value_obj = {
253253

254254
//| .. attribute:: drive_mode
255255
//|
256-
//| Get or set the pin drive mode.
256+
//| The pin drive mode. One of:
257+
//|
258+
//| - `digitalio.DriveMode.PUSH_PULL`
259+
//| - `digitalio.DriveMode.OPEN_DRAIN`
257260
//|
258261
STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) {
259262
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -295,10 +298,13 @@ const mp_obj_property_t digitalio_digitalio_drive_mode_obj = {
295298

296299
//| .. attribute:: pull
297300
//|
298-
//| Get or set the pin pull. Values may be `digitalio.Pull.UP`,
299-
//| `digitalio.Pull.DOWN` or ``None``.
301+
//| The pin pull direction. One of:
302+
//|
303+
//| - `digitalio.Pull.UP`
304+
//| - `digitalio.Pull.DOWN`
305+
//| - `None`
300306
//|
301-
//| :raises AttributeError: if the direction is ~`digitalio.Direction.OUTPUT`.
307+
//| :raises AttributeError: if `direction` is :py:data:`~digitalio.Direction.OUTPUT`.
302308
//|
303309
STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) {
304310
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);

‎shared-bindings/microcontroller/Processor.c‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050

5151
//| .. class:: Processor()
5252
//|
53-
//| You cannot create an instance of `microcontroller.Processor`.
54-
//| Use `microcontroller.cpu` to access the sole instance available.
53+
//| You cannot create an instance of `microcontroller.Processor`.
54+
//| Use `microcontroller.cpu` to access the sole instance available.
5555
//|
5656

57-
//| .. attribute:: frequency
57+
//| .. attribute:: frequency
5858
//|
59-
//| Return the CPU operating frequency as an int, in Hz.
59+
//| The CPU operating frequency as an `int`, in Hertz. (read-only)
6060
//|
6161
STATIC mp_obj_t mcu_processor_get_frequency(mp_obj_t self) {
6262
return mp_obj_new_int_from_uint(common_hal_mcu_processor_get_frequency());
@@ -72,10 +72,11 @@ const mp_obj_property_t mcu_processor_frequency_obj = {
7272
},
7373
};
7474

75-
//| .. attribute:: temperature
75+
//| .. attribute:: temperature
7676
//|
77-
//| Return the on-chip temperature, in Celsius, as a float.
78-
//| If the temperature is not available, return `None`.
77+
//| The on-chip temperature, in Celsius, as a float. (read-only)
78+
//|
79+
//| Is `None` if the temperature is not available.
7980
//|
8081
STATIC mp_obj_t mcu_processor_get_temperature(mp_obj_t self) {
8182
float temperature = common_hal_mcu_processor_get_temperature();
@@ -92,10 +93,9 @@ const mp_obj_property_t mcu_processor_temperature_obj = {
9293
},
9394
};
9495

95-
//| .. attribute:: uid
96+
//| .. attribute:: uid
9697
//|
97-
//| Return the unique id (aka serial number) of the chip.
98-
//| Returns a bytearray object.
98+
//| The unique id (aka serial number) of the chip as a `bytearray`. (read-only)
9999
//|
100100
STATIC mp_obj_t mcu_processor_get_uid(mp_obj_t self) {
101101
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];

‎shared-bindings/pulseio/PulseIn.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(pulseio_pulsein_popleft_obj, pulseio_pulsein_obj_pople
201201

202202
//| .. attribute:: maxlen
203203
//|
204-
//| Returns the maximum length of the PulseIn. When len() is equal to maxlen,
204+
//| The maximum length of the PulseIn. When len() is equal to maxlen,
205205
//| it is unclear which pulses are active and which are idle.
206206
//|
207207
STATIC mp_obj_t pulseio_pulsein_obj_get_maxlen(mp_obj_t self_in) {

‎shared-bindings/touchio/TouchIn.c‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(touchio_touchin___exit___obj, 4, 4, t
107107

108108
//| .. attribute:: value
109109
//|
110-
//| Whether the touch pad is being touched or not.
111-
//| True if `raw_value` > `threshold`.
110+
//| Whether the touch pad is being touched or not. (read-only)
112111
//|
113-
//| :return: True when touched, False otherwise.
114-
//| :rtype: bool
112+
//| True when `raw_value` > `threshold`.
115113
//|
116114
STATIC mp_obj_t touchio_touchin_obj_get_value(mp_obj_t self_in) {
117115
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -130,10 +128,7 @@ const mp_obj_property_t touchio_touchin_value_obj = {
130128

131129
//| .. attribute:: raw_value
132130
//|
133-
//| The raw touch measurement. Not settable.
134-
//|
135-
//| :return: an integer >= 0
136-
//| :rtype: int
131+
//| The raw touch measurement as an `int`. (read-only)
137132
//|
138133
STATIC mp_obj_t touchio_touchin_obj_get_raw_value(mp_obj_t self_in) {
139134
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -153,14 +148,12 @@ const mp_obj_property_t touchio_touchin_raw_value_obj = {
153148

154149
//| .. attribute:: threshold
155150
//|
156-
//| `value` will return True if `raw_value` is greater than than this threshold.
151+
//| Minimum `raw_value` needed to detect a touch (and for `value` to be `True`).
152+
//|
157153
//| When the **TouchIn** object is created, an initial `raw_value` is read from the pin,
158154
//| and then `threshold` is set to be 100 + that value.
159155
//|
160-
//| You can set the threshold to a different value to make the pin more or less sensitive.
161-
//|
162-
//| :return: an integer >= 0
163-
//| :rtype: int
156+
//| You can adjust `threshold` to make the pin more or less sensitive.
164157
//|
165158
STATIC mp_obj_t touchio_touchin_obj_get_threshold(mp_obj_t self_in) {
166159
touchio_touchin_obj_t *self = MP_OBJ_TO_PTR(self_in);

‎shared-bindings/usb_hid/Device.c‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_hid_device_send_report_obj, usb_hid_device_send_re
6767

6868
//| .. attribute:: usage_page
6969
//|
70-
//| The usage page of the device. Can be thought of a category.
71-
//|
72-
//| :return: the device's usage page
73-
//| :rtype: int
70+
//| The usage page of the device as an `int`. Can be thought of a category. (read-only)
7471
//|
7572
STATIC mp_obj_t usb_hid_device_obj_get_usage_page(mp_obj_t self_in) {
7673
usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -87,12 +84,10 @@ const mp_obj_property_t usb_hid_device_usage_page_obj = {
8784

8885
//| .. attribute:: usage
8986
//|
90-
//| The functionality of the device. For example Keyboard is 0x06 within the
91-
//| generic desktop usage page 0x01. Mouse is 0x02 within the same usage
92-
//| page.
87+
//| The functionality of the device as an int. (read-only)
9388
//|
94-
//| :return: the usage within the usage page
95-
//| :rtype: int
89+
//| For example, Keyboard is 0x06 within the generic desktop usage page 0x01.
90+
//| Mouse is 0x02 within the same usage page.
9691
//|
9792
STATIC mp_obj_t usb_hid_device_obj_get_usage(mp_obj_t self_in) {
9893
usb_hid_device_obj_t *self = MP_OBJ_TO_PTR(self_in);

0 commit comments

Comments
 (0)