Exercise: Basic “Sensor”

Specification, And Setup

Implement a class ConstantSensor, such that the following works.

  • A program, read-sensor-const.py. Download that into the programs/ directory of your project structure.

    #!/usr/bin/env python
    
    from sensors.sensor_const import ConstantSensor
    
    sensor = ConstantSensor(36.7)
    temperature = sensor.get_temperature()
    print(temperature)
    
  • A unit test; you download that into the tests/ subdirectory of you project.

    from sensors.sensor_const import ConstantSensor
    import pytest
    
    def test_basic():
        sensor = ConstantSensor(36.7)
        temperature = sensor.get_temperature()
        assert temperature == pytest.approx(36.7)
    
  • To set paths, follow the instructions in Project Setup

Verification

Program

Run the program,

$ python programs/read-sensor-const.py
36.7

Unit Test

In you project’s root directory, unit tests like follows …

$ python -m pytest
============================= test session starts ==============================
platform linux -- Python 3.10.7, pytest-7.2.0, pluggy-1.0.0
rootdir: <your project root>
collected 1 item

tests/test_sensor_const.py .                                             [100%]

============================== 1 passed in 0.01s ===============================

Dependencies

cluster_python Python Programming cluster_python_basics Python: The Language Fundamentals cluster_python_exercises Exercises cluster_python_exercises_sensors Sensors, And Data Acquisition (Exercise Series) cluster_python_advanced Python: More Language Features cluster_python_advanced_oo Object Oriented Programming python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0120_helloworld Hello World python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_basics_python_0110_blahblah Blahblah python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0140_variables Variables python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_exercises_sensors_sensor_const Exercise: Basic “Sensor” python_exercises_sensors_project_setup Project Setup python_exercises_sensors_sensor_const->python_exercises_sensors_project_setup python_advanced_oo_methods Methods python_exercises_sensors_sensor_const->python_advanced_oo_methods python_exercises_sensors_project_setup->python_exercises_sensors_sensor_const python_advanced_oo_classes_and_dicts Classes And Dictionaries python_advanced_oo_methods->python_advanced_oo_classes_and_dicts python_advanced_oo_classes_and_dicts->python_basics_python_0150_datatypes_overview_compound