From 01c7d786e36e30dca88479afe81a9df5ca94d6ee Mon Sep 17 00:00:00 2001 From: dherrada Date: Fri, 20 Mar 2020 12:13:58 -0400 Subject: [PATCH] Ran black, updated to pylint 2.x --- .github/workflows/build.yml | 2 +- adafruit_dht.py | 43 +++++++----- docs/conf.py | 121 ++++++++++++++++++++------------- examples/dht_simpletest.py | 7 +- examples/dht_to_led_display.py | 6 +- setup.py | 50 ++++++-------- 6 files changed, 129 insertions(+), 100 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fff3aa9..1dad804 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,7 +40,7 @@ jobs: source actions-ci/install.sh - name: Pip install pylint, black, & Sphinx run: | - pip install --force-reinstall pylint==1.9.2 black==19.10b0 Sphinx sphinx-rtd-theme + pip install --force-reinstall pylint black==19.10b0 Sphinx sphinx-rtd-theme - name: Library version run: git describe --dirty --always --tags - name: PyLint diff --git a/adafruit_dht.py b/adafruit_dht.py index 2956cc7..4c9083f 100644 --- a/adafruit_dht.py +++ b/adafruit_dht.py @@ -31,17 +31,20 @@ import array import time from digitalio import DigitalInOut, Pull, Direction + _USE_PULSEIO = False try: from pulseio import PulseIn + _USE_PULSEIO = True except ImportError: - pass # This is OK, we'll try to bitbang it! + pass # This is OK, we'll try to bitbang it! __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DHT.git" + class DHTBase: """ base support for DHT11 and DHT22 devices """ @@ -88,7 +91,7 @@ def _pulses_to_binary(self, pulses, start, stop): bit = 0 if pulses[bit_inx] > self.__hiLevel: bit = 1 - binary = binary<<1 | bit + binary = binary << 1 | bit hi_sig = not hi_sig @@ -104,7 +107,7 @@ def _get_pulses_pulseio(self): transition times starting with a low transition time. Normally pulses will have 81 elements for the DHT11/22 type devices. """ - pulses = array.array('H') + pulses = array.array("H") if _USE_PULSEIO: # The DHT type device use a specialize 1-wire protocol # The microprocessor first sends a LOW signal for a @@ -133,7 +136,7 @@ def _get_pulses_bitbang(self): transition times starting with a low transition time. Normally pulses will have 81 elements for the DHT11/22 type devices. """ - pulses = array.array('H') + pulses = array.array("H") with DigitalInOut(self._pin) as dhtpin: # we will bitbang if no pulsein capability transitions = [] @@ -143,19 +146,19 @@ def _get_pulses_bitbang(self): time.sleep(0.1) dhtpin.value = False time.sleep(0.001) - timestamp = time.monotonic() # take timestamp - dhtval = True # start with dht pin true because its pulled up + timestamp = time.monotonic() # take timestamp + dhtval = True # start with dht pin true because its pulled up dhtpin.direction = Direction.INPUT dhtpin.pull = Pull.UP while time.monotonic() - timestamp < 0.25: if dhtval != dhtpin.value: dhtval = not dhtval # we toggled - transitions.append(time.monotonic()) # save the timestamp + transitions.append(time.monotonic()) # save the timestamp # convert transtions to microsecond delta pulses: # use last 81 pulses transition_start = max(1, len(transitions) - 81) for i in range(transition_start, len(transitions)): - pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i-1])) + pulses_micro_sec = int(1000000 * (transitions[i] - transitions[i - 1])) pulses.append(min(pulses_micro_sec, 65535)) return pulses @@ -171,20 +174,24 @@ def measure(self): # Initiate new reading if this is the first call or if sufficient delay # If delay not sufficient - return previous reading. # This allows back to back access for temperature and humidity for same reading - if (self._last_called == 0 or - (time.monotonic()-self._last_called) > delay_between_readings): + if ( + self._last_called == 0 + or (time.monotonic() - self._last_called) > delay_between_readings + ): self._last_called = time.monotonic() if _USE_PULSEIO: pulses = self._get_pulses_pulseio() else: pulses = self._get_pulses_bitbang() - #print(len(pulses), "pulses:", [x for x in pulses]) + # print(len(pulses), "pulses:", [x for x in pulses]) if len(pulses) >= 80: - buf = array.array('B') + buf = array.array("B") for byte_start in range(0, 80, 16): - buf.append(self._pulses_to_binary(pulses, byte_start, byte_start+16)) + buf.append( + self._pulses_to_binary(pulses, byte_start, byte_start + 16) + ) if self._dht11: # humidity is 1 byte @@ -193,10 +200,10 @@ def measure(self): self._temperature = buf[2] else: # humidity is 2 bytes - self._humidity = ((buf[0]<<8) | buf[1]) / 10 + self._humidity = ((buf[0] << 8) | buf[1]) / 10 # temperature is 2 bytes # MSB is sign, bits 0-14 are magnitude) - raw_temperature = (((buf[2] & 0x7f)<<8) | buf[3]) / 10 + raw_temperature = (((buf[2] & 0x7F) << 8) | buf[3]) / 10 # set sign if buf[2] & 0x80: raw_temperature = -raw_temperature @@ -207,7 +214,7 @@ def measure(self): chk_sum += b # checksum is the last byte - if chk_sum & 0xff != buf[4]: + if chk_sum & 0xFF != buf[4]: # check sum failed to validate raise RuntimeError("Checksum did not validate. Try again.") @@ -217,6 +224,7 @@ def measure(self): else: # Probably a connection issue! raise RuntimeError("DHT sensor not found, check wiring") + @property def temperature(self): """ temperature current reading. It makes sure a reading is available @@ -237,11 +245,13 @@ def humidity(self): self.measure() return self._humidity + class DHT11(DHTBase): """ Support for DHT11 device. :param ~board.Pin pin: digital pin used for communication """ + def __init__(self, pin): super().__init__(True, pin, 18000) @@ -251,5 +261,6 @@ class DHT22(DHTBase): :param ~board.Pin pin: digital pin used for communication """ + def __init__(self, pin): super().__init__(False, pin, 1000) diff --git a/docs/conf.py b/docs/conf.py index caed051..b628bfa 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,7 +2,8 @@ import os import sys -sys.path.insert(0, os.path.abspath('..')) + +sys.path.insert(0, os.path.abspath("..")) # -- General configuration ------------------------------------------------ @@ -10,39 +11,47 @@ # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.viewcode', + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.viewcode", ] autodoc_mock_imports = ["pulseio"] -intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None), - 'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None), - 'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None), - 'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} +intersphinx_mapping = { + "python": ("https://docs.python.org/3.4", None), + "BusDevice": ( + "https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", + None, + ), + "Register": ( + "https://circuitpython.readthedocs.io/projects/register/en/latest/", + None, + ), + "CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None), +} # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = u'Adafruit CircuitPython DHT Library' -copyright = u'2017 Mike McWethy' -author = u'Mike McWethy' +project = u"Adafruit CircuitPython DHT Library" +copyright = u"2017 Mike McWethy" +author = u"Mike McWethy" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = u'1.0' +version = u"1.0" # The full version, including alpha/beta/rc tags. -release = u'1.0' +release = u"1.0" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -54,7 +63,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', '.env', 'CODE_OF_CONDUCT.md'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"] # The reST default role (used for this markup: `text`) to use for all # documents. @@ -66,7 +75,7 @@ add_function_parentheses = True # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False @@ -80,59 +89,62 @@ # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -on_rtd = os.environ.get('READTHEDOCS', None) == 'True' +on_rtd = os.environ.get("READTHEDOCS", None) == "True" if not on_rtd: # only import and set the theme if we're building docs locally try: import sphinx_rtd_theme - html_theme = 'sphinx_rtd_theme' - html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), '.'] + + html_theme = "sphinx_rtd_theme" + html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."] except: - html_theme = 'default' - html_theme_path = ['.'] + html_theme = "default" + html_theme_path = ["."] else: - html_theme_path = ['.'] + html_theme_path = ["."] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] # The name of an image file (relative to this directory) to use as a favicon of # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. # -html_favicon = '_static/favicon.ico' +html_favicon = "_static/favicon.ico" # Output file base name for HTML help builder. -htmlhelp_basename = 'AdafruitCircuitPythonDHTLibrarydoc' +htmlhelp_basename = "AdafruitCircuitPythonDHTLibrarydoc" # -- Options for LaTeX output --------------------------------------------- latex_elements = { - # The paper size ('letterpaper' or 'a4paper'). - # - # 'papersize': 'letterpaper', - - # The font size ('10pt', '11pt' or '12pt'). - # - # 'pointsize': '10pt', - - # Additional stuff for the LaTeX preamble. - # - # 'preamble': '', - - # Latex figure (float) alignment - # - # 'figure_align': 'htbp', + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'AdafruitCircuitPythonDHTLibrary.tex', u'Adafruit CircuitPython DHT Library Documentation', - author, 'manual'), + ( + master_doc, + "AdafruitCircuitPythonDHTLibrary.tex", + u"Adafruit CircuitPython DHT Library Documentation", + author, + "manual", + ), ] # -- Options for manual page output --------------------------------------- @@ -140,8 +152,13 @@ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'adafruitCircuitPythonDHTlibrary', u'Adafruit CircuitPython DHT Library Documentation', - [author], 1) + ( + master_doc, + "adafruitCircuitPythonDHTlibrary", + u"Adafruit CircuitPython DHT Library Documentation", + [author], + 1, + ) ] # -- Options for Texinfo output ------------------------------------------- @@ -150,7 +167,13 @@ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'AdafruitCircuitPythonDHTLibrary', u'Adafruit CircuitPython DHT Library Documentation', - author, 'AdafruitCircuitPythonDHTLibrary', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "AdafruitCircuitPythonDHTLibrary", + u"Adafruit CircuitPython DHT Library Documentation", + author, + "AdafruitCircuitPythonDHTLibrary", + "One line description of project.", + "Miscellaneous", + ), ] diff --git a/examples/dht_simpletest.py b/examples/dht_simpletest.py index e07c078..9a054d1 100644 --- a/examples/dht_simpletest.py +++ b/examples/dht_simpletest.py @@ -11,8 +11,11 @@ temperature_c = dhtDevice.temperature temperature_f = temperature_c * (9 / 5) + 32 humidity = dhtDevice.humidity - print("Temp: {:.1f} F / {:.1f} C Humidity: {}% " - .format(temperature_f, temperature_c, humidity)) + print( + "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( + temperature_f, temperature_c, humidity + ) + ) except RuntimeError as error: # Errors happen fairly often, DHT's are hard to read, just keep going diff --git a/examples/dht_to_led_display.py b/examples/dht_to_led_display.py index d3cb3a9..41baaa5 100644 --- a/examples/dht_to_led_display.py +++ b/examples/dht_to_led_display.py @@ -19,7 +19,7 @@ display = bcddigits.BCDDigits(spi, cs, nDigits=8) display.brightness(5) -#initial the dht device +# initial the dht device dhtDevice = adafruit_dht.DHT22(D2) while True: @@ -27,11 +27,11 @@ # show the values to the serial port temperature = dhtDevice.temperature * (9 / 5) + 32 humidity = dhtDevice.humidity - #print("Temp: {:.1f} F Humidity: {}% ".format(temperature, humidity)) + # print("Temp: {:.1f} F Humidity: {}% ".format(temperature, humidity)) # now show the values on the 8 digit 7-segment display display.clear_all() - display.show_str(0, '{:5.1f}{:5.1f}'.format(temperature, humidity)) + display.show_str(0, "{:5.1f}{:5.1f}".format(temperature, humidity)) display.show() except RuntimeError as error: diff --git a/setup.py b/setup.py index 2817391..834fba2 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ # Always prefer setuptools over distutils from setuptools import setup, find_packages + # To use a consistent encoding from codecs import open from os import path @@ -14,47 +15,38 @@ here = path.abspath(path.dirname(__file__)) # Get the long description from the README file -with open(path.join(here, 'README.rst'), encoding='utf-8') as f: +with open(path.join(here, "README.rst"), encoding="utf-8") as f: long_description = f.read() setup( - name='adafruit-circuitpython-dht', - + name="adafruit-circuitpython-dht", use_scm_version=True, - setup_requires=['setuptools_scm'], - - description='CircuitPython support for DHT11 and DHT22 type temperature/humidity devices', + setup_requires=["setuptools_scm"], + description="CircuitPython support for DHT11 and DHT22 type temperature/humidity devices", long_description=long_description, - long_description_content_type='text/x-rst', - + long_description_content_type="text/x-rst", # The project's main homepage. - url='https://github.com/adafruit/Adafruit_CircuitPython_DHT', - + url="https://github.com/adafruit/Adafruit_CircuitPython_DHT", # Author details - author='Adafruit Industries', - author_email='circuitpython@adafruit.com', - - install_requires=['Adafruit-Blinka'], - + author="Adafruit Industries", + author_email="circuitpython@adafruit.com", + install_requires=["Adafruit-Blinka"], # Choose your license - license='MIT', - + license="MIT", # See https://pypi.python.org/pypi?%3Aaction=list_classifiers classifiers=[ - 'Development Status :: 3 - Alpha', - 'Intended Audience :: Developers', - 'Topic :: Software Development :: Libraries', - 'Topic :: System :: Hardware', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries", + "Topic :: System :: Hardware", + "License :: OSI Approved :: MIT License", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", + "Programming Language :: Python :: 3.5", ], - # What does your project relate to? - keywords='adafruit dht hardware sensors temperature humidity micropython circuitpython', - + keywords="adafruit dht hardware sensors temperature humidity micropython circuitpython", # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). - py_modules=['adafruit_dht'], + py_modules=["adafruit_dht"], )