-
Notifications
You must be signed in to change notification settings - Fork 11
Refactoring per request #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
||
class OneWireError(Exception): | ||
"""A class to represent a 1-Wire exception.""" | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for pass if there is a docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i kind of like it there, makes it obvious that i intentionally left it blank, vs. accidentally deleting all the code
adafruit_onewire/bus.py
Outdated
if 'start' in kwargs: | ||
start = kwargs['start'] | ||
if 'end' in kwargs: | ||
end = kwargs['end'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is usually done by having keywords argument defaulting to None
, and checking if there are not None
here:
def readinto(self, buf, start=0, end=None):
if end is None:
end = len(buf)
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!
adafruit_onewire/bus.py
Outdated
if 'start' in kwargs: | ||
start = kwargs['start'] | ||
if 'end' in kwargs: | ||
end = kwargs['end'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice again!
adafruit_onewire/bus.py
Outdated
for i in range(0xff): # pylint: disable=unused-variable | ||
rom, diff = self._search_rom(rom, diff) | ||
if rom: | ||
devices += [OneWireAddress(rom)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
devices.append(OneWireAddress(rom))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep. another left over from re-purposing some code.
adafruit_onewire/bus.py
Outdated
return devices | ||
|
||
def _readbit(self): | ||
return self._ow.read_bit() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do self._readbit = self._ow.read_bit
in the __init__
, then it would be one function call less.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like doing it this way in case i ever want to come back and add additional behavior
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can still do it anyways — you can switch to this way at any moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any reason other than code style?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually a bit worse style-wise, but it's much faster -- function calls are expensive, and you are avoiding one this way, and also one frame on the stack. CircuitPython has pretty low recursion limit, it's easy to hit it with a lot of nesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. that's what i was after. 👍
adafruit_onewire/bus.py
Outdated
next_diff = i | ||
self._writebit(b) | ||
if b: | ||
r_b |= 1 << bit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: this could be faster as r_b |= b << bit
without the if.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. that was borrowed code.
adafruit_onewire/bus.py
Outdated
rom[byte] = r_b | ||
return rom, next_diff | ||
|
||
def crc8(self, data): # pylint: disable=no-self-use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could make this a @staticmethod
if you don't use self
.
adafruit_onewire/device.py
Outdated
:param int end: Index to write up to but not include | ||
""" | ||
self._bus.readinto(buf, **kwargs) | ||
if kwargs is None and len(buf) >= 8: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kwargs
is never None
— if there are no keyword arguments, it will be an empty dict {}
, so you could write if not kwargs and ...
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. that was totally a mistake and a bug on my part!
adafruit_onewire/device.py
Outdated
:param int start: Index to start writing from | ||
:param int end: Index to read up to but not include | ||
""" | ||
return self._bus.write(buf, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also do self.write = self._bus.write
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same comment as above
from micropython import const | ||
|
||
_SEARCH_ROM = const(0xF0) | ||
_MATCH_ROM = const(0x55) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you do _MATCH_ROM = b'\x55'
instead, you won't need to convert it to bytearray
everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, those converts were kind of kluge leftover from changing all calls to be general purpose and pass in arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed in the other module, in this one there are no converts used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here are more comments with a bit of overlap with @deshipu
README.rst
Outdated
>>> for d in devices: | ||
... print("ROM={}\tFamily=0x{:02x}".format(d.rom, d.family_code)) | ||
... | ||
ROM=bytearray(b'(\xff\xc3\x80\xc2\x16\x04\xc6') Family=0x28 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't put the prompt here so its easily copied.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
README.rst
Outdated
================ | ||
|
||
To build this library locally you'll need to install the | ||
`circuitpython-travis-build-tools <https://github.com/adafruit/circuitpython-build-tools>`_ package. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gah, cookiecutter is out of date here. This shouldn't have travis in the name and below it should be installed explicitly. Sent you a PR for it here: adafruit/cookiecutter-adafruit-circuitpython#9
adafruit_onewire/bus.py
Outdated
raise OneWireError | ||
return not reset | ||
|
||
def readinto(self, buf, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**kwargs should be rarely used. Its only really useful when passing kwargs through to a sub-call. Using it otherwise hides incorrect, unused kwargs.
Here simply do readinto(self, buf, *, start=None, end=None)
. The *,
tells Python that everything after it must be a kwarg instead of positional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yah, my first time to try using them. got the idea from here:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/blob/master/adafruit_bus_device/i2c_device.py#L65
adafruit_onewire/bus.py
Outdated
rom[byte] = r_b | ||
return rom, next_diff | ||
|
||
def crc8(self, data): # pylint: disable=no-self-use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of disabling pylint, this can be a static method. That means it works independent of the actual object state. To do so, use the @staticmethod
decorator and drop self
.
@staticmethod
def crc8(data):
adafruit_onewire/bus.py
Outdated
|
||
for byte in data: | ||
crc ^= byte | ||
for bit in range(8): # pylint: disable=unused-variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do for _ in range(8):
. _
is used to designate unused things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Miss this?
api.rst
Outdated
.. automodule:: onewire_bus | ||
:members: | ||
|
||
.. automodule:: onewire_device |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these will need to be adafruit_onewire.bus
and adafruit_onewire.device
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. nice catch. left over from a rename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a couple minor things.
README.rst
Outdated
|
||
.. code-block:: shell | ||
|
||
circuitpython-build-bundles --filename_prefix {% if cookiecutter.library_prefix %}{{ cookiecutter.library_prefix | lower }}-{% endif %}circuitpython-{{ cookiecutter.library_name | lower }} --library_location . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cookiecutter templates need to be replaced. (I think it should be adafruit-circuitpython-onewire
.)
README.rst
Outdated
Introduction | ||
============ | ||
|
||
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-OneWire/badge/?version=latest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think OneWire
should be onewire
adafruit_onewire/bus.py
Outdated
|
||
for byte in data: | ||
crc ^= byte | ||
for bit in range(8): # pylint: disable=unused-variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Miss this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome! Thanks!
Pretty major refactor for this issue:
adafruit/circuitpython#338