Skip to content

Commit

Permalink
Raise pylint compliance. (google#402)
Browse files Browse the repository at this point in the history
  * Audit/update pylintrc.
  * Add pylint plugins for conf and mutablerecords.
  * Raise pylint score to 9.73.
  • Loading branch information
jettisonjoe authored Aug 23, 2016
1 parent e20bf43 commit 6760825
Show file tree
Hide file tree
Showing 68 changed files with 1,362 additions and 1,214 deletions.
30 changes: 15 additions & 15 deletions examples/all_the_things.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@
@plug(example=example_plug.ExamplePlug)
def example_monitor(example):
time.sleep(.2)
return example.Increment()
return example.increment()


@measures(
Measurement('unset_meas'),
Measurement(
'widget_type').MatchesRegex(r'.*Widget$').Doc(
'widget_type').matches_regex(r'.*Widget$').doc(
'''This measurement tracks the type of widgets.'''),
Measurement(
'widget_color').Doc('Color of the widget'),
Measurement('widget_size').InRange(1, 4))
'widget_color').doc('Color of the widget'),
Measurement('widget_size').in_range(1, 4))
@plug(example=example_plug.ExamplePlug)
@plug(prompts=user_input.UserInput)
def hello_world(test, example, prompts):
Expand All @@ -60,7 +60,7 @@ def hello_world(test, example, prompts):
raise Exception()
test.measurements.widget_color = 'Black'
test.measurements.widget_size = 3
test.logger.info('Plug value: %s', example.Increment())
test.logger.info('Plug value: %s', example.increment())


# Timeout if this phase takes longer than 10 seconds.
Expand All @@ -80,9 +80,9 @@ def set_measurements(test):


@measures(
Measurement('unset_dims').WithDimensions(units.HERTZ),
Measurement('dimensions').WithDimensions(units.HERTZ),
Measurement('lots_of_dims').WithDimensions(
Measurement('unset_dims').with_dimensions(units.HERTZ),
Measurement('dimensions').with_dimensions(units.HERTZ),
Measurement('lots_of_dims').with_dimensions(
units.HERTZ, units.SECOND, units.RADIAN))
def dimensions(test):
for dim in range(5):
Expand All @@ -95,7 +95,6 @@ def attachments(test):
test.attach('test_attachment', 'This is test attachment data.')
test.attach_from_file('example_attachment.txt')


def teardown(test):
test.logger.info('Running teardown')

Expand All @@ -106,21 +105,22 @@ def teardown(test):
# but you can include any metadata fields.
test_name='MyTest', test_description='OpenHTF Example Test',
test_version='1.0.0')
test.AddOutputCallbacks(output.OutputToFile(
test.add_output_callbacks(output.OutputToFile(
'./{dut_id}.{metadata[test_name]}.{start_time_millis}.pickle'))
test.AddOutputCallbacks(
test.add_output_callbacks(
json_factory.OutputToJSON(
'./{dut_id}.{metadata[test_name]}.{start_time_millis}.json',
indent=4))
#test.AddOutputCallbacks(mfg_inspector.OutputToTestRunProto(
#test.add_output_callbacks(mfg_inspector.OutputToTestRunProto(
# './{dut_id}.{start_time_millis}.pb'))
# Example of how to upload to mfg-inspector. Replace filename with your
# JSON-formatted private key downloaded from Google Developers Console
# when you created the Service Account you intend to use, or name it
# 'my_private_key.json'.
#if os.path.isfile('my_private_key.json'):
# with open('my_private_key.json', 'r') as json_file:
# test.AddOutputCallbacks(output.UploadToMfgInspector.from_json(
# test.add_output_callbacks(output.UploadToMfgInspector.from_json(
# json.load(json_file)))
test.Configure(teardown_function=teardown)
test.Execute(test_start=user_input.prompt_for_test_start())

#test.configure(teardown_function=teardown)
test.execute(test_start=user_input.prompt_for_test_start())
28 changes: 14 additions & 14 deletions examples/example_plug.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@
import openhtf.plugs as plugs


conf.Declare('example_plug_increment', default_value=1,
description='Increment constant for example plug.')
conf.declare('example_plug_increment_size', default_value=1,
description='increment constant for example plug.')


class ExamplePlug(plugs.BasePlug): # pylint: disable=no-init
"""Example of a simple plug.
This plug simply keeps a value and increments it each time Increment() is
This plug simply keeps a value and increments it each time increment() is
called. You'll notice a few paradigms here:
- @conf.InjectPositionalArgs
- @conf.inject_positional_args
This is generally a good way to pass in any configuration that your
plug needs, such as an IP address or serial port to connect to. If
You want to use your plug outside of the OpenHTF framework, you can
still manually instantiate it, but you must pass the arguments by
keyword (as a side effect of the way InjectPositionalArgs is
keyword (as a side effect of the way inject_positional_args is
implemented).
For example, if you had no openhtf.conf loaded, you could do this:
my_plug = ExamplePlug(example_plug_increment=4)
- TearDown()
- tearDown()
This method will be called automatically by the OpenHTF framework at
the end of test execution. Here is a good place to do any close()
calls or similar resource cleanup that you need to do. In this case,
Expand All @@ -58,26 +58,26 @@ class ExamplePlug(plugs.BasePlug): # pylint: disable=no-init
don't have to do anything special to maintain state within a plug
across phases.
This does imply, however, that if you *want* per-phase TearDown()
This does imply, however, that if you *want* per-phase tearDown()
semantics, you have to implement them manually. The recommended
way to do this is to make your plug support Python's context
manager interface (__enter__ and __exit__), and then access it via
a with: block at the beginning of every phase where it is used.
"""

@conf.InjectPositionalArgs
def __init__(self, example_plug_increment):
self.increment = example_plug_increment
@conf.inject_positional_args
def __init__(self, example_plug_increment_size):
self.increment_size = example_plug_increment_size
self.value = 0

def __str__(self):
return '<%s: %s>' % (type(self).__name__, self.value)

def TearDown(self):
def tearDown(self):
"""Tear down the plug instance."""
self.logger.info('Tearing down %s', self)

def Increment(self):
def increment(self):
"""Increment our value, return the previous value."""
self.value += self.increment
return self.value - self.increment
self.value += self.increment_size
return self.value - self.increment_size
6 changes: 3 additions & 3 deletions examples/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def hello_world(test):
# mechanisms can be implemented, but for now we'll just keep it simple.
# This will always output to the same ./hello_world.json file, formatted
# slightly for human readability.
test.AddOutputCallbacks(
test.add_output_callbacks(
json_factory.OutputToJSON('./{dut_id}.hello_world.json', indent=2))

# PromptForTestStart prompts the operator for a DUT ID, a unique identifier
# prompt_for_test_start prompts the operator for a DUT ID, a unique identifier
# for the DUT (Device Under Test). OpenHTF requires that a DUT ID is set
# each time a test is executed. It may be set programmatically, but the
# simplest way to get one is to prompt the user for it. If test_start is
# not provided, the test will start immediately and assume the DUT ID will
# be set later (OpenHTF will raise an exception when the test completes if
# a DUT ID has not been set).
test.Execute(test_start=user_input.prompt_for_test_start())
test.execute(test_start=user_input.prompt_for_test_start())
22 changes: 11 additions & 11 deletions examples/measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- Measurement names must be valid python variable names. This is mostly for
sanity, but also ensures you can access them via attribute access in phases.
This applies *after* any WithArgs() substitution (not covered in this
This applies *after* any with_args() substitution (not covered in this
tutorial, see the phases.py example for more details).
- You cannot declare the same measurement name multiple times on the same
Expand All @@ -61,13 +61,13 @@

# Simple example of measurement use, similar to hello_world.py usage.
@measures(Measurement('hello_world_measurement'))
def HelloPhase(test):
def hello_phase(test):
test.measurements.hello_world_measurement = 'Hello!'


# An alternative simpler syntax that creates the Measurement for you.
@measures('hello_again_measurement')
def AgainPhase(test):
def again_phase(test):
test.measurements.hello_again_measurement = 'Again!'


Expand All @@ -79,7 +79,7 @@ def AgainPhase(test):
# measurements, and then one or two with more complex declarations (see below).
@measures('first_measurement', 'second_measurement')
@measures(Measurement('third'), Measurement('fourth'))
def LotsOfMeasurements(test):
def lots_of_measurements(test):
test.measurements.first_measurement = 'First!'
# Measurements can also be access via indexing rather than attributes.
test.measurements['second_measurement'] = 'Second :('
Expand All @@ -93,8 +93,8 @@ def LotsOfMeasurements(test):
# describing the measurement. Validators can get quite complex, for more
# details, see the validators.py example.
@measures(Measurement('validated_measurement').InRange(0, 10).Doc(
'This measurement is validated.').WithUnits(units.SECOND))
def MeasureSeconds(test):
'This measurement is validated.').with_units(units.SECOND))
def measure_seconds(test):
# The 'outcome' of this measurement in the test_record result will be a PASS
# because its value passes the validator specified (0 <= 5 <= 10).
test.measurements.validated_measurement = 5
Expand All @@ -108,7 +108,7 @@ def MeasureSeconds(test):
@measures('inline_kwargs', docstring='This measurement is declared inline!',
units=units.HERTZ, validators=[validators.InRange(0, 10)])
@measures('another_inline', docstring='Because why not?')
def InlinePhase(test):
def inline_phase(test):
# This measurement will have an outcome of FAIL, because the set value of 15
# will not pass the 0 <= x <= 10 validator.
test.measurements.inline_kwargs = 15
Expand All @@ -120,17 +120,17 @@ def InlinePhase(test):

if __name__ == '__main__':
# We instantiate our OpenHTF test with the phases we want to run as args.
test = Test(HelloPhase, AgainPhase, LotsOfMeasurements, MeasureSeconds,
InlinePhase)
test = Test(hello_phase, again_phase, lots_of_measurements, measure_seconds,
inline_phase)

# In order to view the result of the test, we have to output it somewhere,
# and a local JSON file is a convenient way to do this. Custom output
# mechanisms can be implemented, but for now we'll just keep it simple.
# This will always output to the same ./measurements.json file, formatted
# slightly for human readability.
test.AddOutputCallbacks(
test.add_output_callbacks(
json_factory.OutputToJSON('./measurements.json', indent=2))

# Unlike hello_world.py, where we prompt for a DUT ID, here we'll just
# use an arbitrary one.
test.Execute(test_start=lambda: 'MyDutId')
test.execute(test_start=lambda: 'MyDutId')
Loading

0 comments on commit 6760825

Please sign in to comment.