Skip to content

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

Merged
merged 13 commits into from
Dec 15, 2017
Merged

Refactoring per request #1

merged 13 commits into from
Dec 15, 2017

Conversation

caternuson
Copy link
Contributor

Pretty major refactor for this issue:
adafruit/circuitpython#338

@tannewt tannewt self-requested a review December 13, 2017 23:15

class OneWireError(Exception):
"""A class to represent a 1-Wire exception."""
pass
Copy link

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

Copy link
Contributor Author

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

if 'start' in kwargs:
start = kwargs['start']
if 'end' in kwargs:
end = kwargs['end']
Copy link

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)
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

if 'start' in kwargs:
start = kwargs['start']
if 'end' in kwargs:
end = kwargs['end']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice again!

for i in range(0xff): # pylint: disable=unused-variable
rom, diff = self._search_rom(rom, diff)
if rom:
devices += [OneWireAddress(rom)]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devices.append(OneWireAddress(rom))

Copy link
Contributor Author

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.

return devices

def _readbit(self):
return self._ow.read_bit()
Copy link

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.

Copy link
Contributor Author

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

Copy link

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.

Copy link
Contributor Author

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?

Copy link

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.

Copy link
Contributor Author

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. 👍

next_diff = i
self._writebit(b)
if b:
r_b |= 1 << bit
Copy link

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.

Copy link
Contributor Author

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.

rom[byte] = r_b
return rom, next_diff

def crc8(self, data): # pylint: disable=no-self-use
Copy link

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.

:param int end: Index to write up to but not include
"""
self._bus.readinto(buf, **kwargs)
if kwargs is None and len(buf) >= 8:
Copy link

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 ....

Copy link
Contributor Author

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!

: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)
Copy link

@deshipu deshipu Dec 13, 2017

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.

Copy link
Contributor Author

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)
Copy link

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Member

@tannewt tannewt left a 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
Copy link
Member

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.

Copy link
Contributor Author

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.
Copy link
Member

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

raise OneWireError
return not reset

def readinto(self, buf, **kwargs):
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rom[byte] = r_b
return rom, next_diff

def crc8(self, data): # pylint: disable=no-self-use
Copy link
Member

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):


for byte in data:
crc ^= byte
for bit in range(8): # pylint: disable=unused-variable
Copy link
Member

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.

Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@tannewt tannewt left a 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 .
Copy link
Member

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
Copy link
Member

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


for byte in data:
crc ^= byte
for bit in range(8): # pylint: disable=unused-variable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Miss this?

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome! Thanks!

@tannewt tannewt merged commit bb71e82 into adafruit:master Dec 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants