Skip to content

Commit

Permalink
Use snake_case gflags APIs. (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
yilei authored and gareth-ferneyhough committed Apr 11, 2017
1 parent 0254d50 commit 9e130a4
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 81 deletions.
2 changes: 1 addition & 1 deletion perfkitbenchmarker/configs/benchmark_config_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def Decode(self, value, component_full_name, flag_values):
self._GetOptionFullName(component_full_name), key))
if not merged_flag_values[key].present:
try:
merged_flag_values[key].Parse(value)
merged_flag_values[key].parse(value)
except flags.IllegalFlagValue as e:
raise errors.Config.InvalidValue(
'Invalid {0}.{1} value: "{2}" (of type "{3}").{4}{5}'.format(
Expand Down
19 changes: 10 additions & 9 deletions perfkitbenchmarker/flag_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __iter__(self):
yield val

def __str__(self):
return IntegerListSerializer().Serialize(self)
return IntegerListSerializer().serialize(self)

def _CreateXrangeFromTuple(self, input_tuple):
start = input_tuple[0]
Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(self, on_nonincreasing=None):

self.on_nonincreasing = on_nonincreasing

def Parse(self, inp):
def parse(self, inp):
"""Parse an integer list.
Args:
Expand Down Expand Up @@ -189,17 +189,18 @@ def HandleNonIncreasing():

return IntegerList(result)

def Type(self):
def flag_type(self):
return 'integer list'


class IntegerListSerializer(flags.ArgumentSerializer):

def _SerializeRange(self, val):
if len(val) == 2:
return '%s-%s' % (val[0], val[1])
return '%s-%s-%s' % (val[0], val[1], val[2])

def Serialize(self, il):
def serialize(self, il):
return ','.join([str(val) if isinstance(val, int) or isinstance(val, long)
else self._SerializeRange(val)
for val in il.groups])
Expand Down Expand Up @@ -263,15 +264,15 @@ def __init__(self, convertible_to):
convertible_to: Either an individual unit specification or a series of
unit specifications, where each unit specification is either a string
(e.g. 'byte') or a units.Unit. The parser input must be convertible to
at least one of the specified Units, or the Parse() method will raise
at least one of the specified Units, or the parse() method will raise
a ValueError.
"""
if isinstance(convertible_to, (basestring, units.Unit)):
self.convertible_to = [units.Unit(convertible_to)]
else:
self.convertible_to = [units.Unit(u) for u in convertible_to]

def Parse(self, inp):
def parse(self, inp):
"""Parse the input.
Args:
Expand Down Expand Up @@ -311,7 +312,7 @@ def Parse(self, inp):


class UnitsSerializer(flags.ArgumentSerializer):
def Serialize(self, units):
def serialize(self, units):
return str(units)


Expand Down Expand Up @@ -409,7 +410,7 @@ class YAMLParser(flags.ArgumentParser):

syntactic_help = 'A YAML expression.'

def Parse(self, inp):
def parse(self, inp):
"""Parse the input.
Args:
Expand Down Expand Up @@ -437,7 +438,7 @@ def Parse(self, inp):

class YAMLSerializer(flags.ArgumentSerializer):

def Serialize(self, val):
def serialize(self, val):
return yaml.dump(val)


Expand Down
4 changes: 2 additions & 2 deletions perfkitbenchmarker/linux_benchmarks/multichase_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self):
super(_MemorySizeParser, self).__init__(convertible_to=(units.byte,
units.percent))

def Parse(self, inp):
def parse(self, inp):
"""Parse the input.
Args:
Expand All @@ -85,7 +85,7 @@ def Parse(self, inp):
ValueError: If the input cannot be parsed, or if it parses to a value that
does not meet the requirements described in self.syntactic_help.
"""
size = super(_MemorySizeParser, self).Parse(inp)
size = super(_MemorySizeParser, self).parse(inp)
if size.units != units.percent:
size_byte_count = size.to(units.byte).magnitude
if size_byte_count != int(size_byte_count):
Expand Down
2 changes: 1 addition & 1 deletion perfkitbenchmarker/pkb.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def _LogCommandLineFlags():
for name in FLAGS:
flag = FLAGS[name]
if flag.present:
result.append(flag.Serialize())
result.append(flag.serialize())
logging.info('Flag values:\n%s', '\n'.join(result))


Expand Down
4 changes: 2 additions & 2 deletions perfkitbenchmarker/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, *args, **kwargs):
super(RunStageParser, self).__init__(*args, **kwargs)
self.syntactic_help = _SYNTACTIC_HELP

def Parse(self, argument):
def parse(self, argument):
"""Parses a list of stages.
Args:
Expand All @@ -62,7 +62,7 @@ def Parse(self, argument):
ValueError: If argument does not conform to the guidelines explained in
syntactic_help.
"""
stage_list = super(RunStageParser, self).Parse(argument)
stage_list = super(RunStageParser, self).parse(argument)

if not stage_list:
raise ValueError('Unable to parse {0}. Stage list cannot be '
Expand Down
10 changes: 5 additions & 5 deletions tests/background_cpu_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def _CheckVMFromSpec(self, spec, working_groups=(), non_working_groups=()):

def testWindowsVMCausesError(self):
""" windows vm with background_cpu_threads raises exception """
self._mocked_flags['background_cpu_threads'].Parse(1)
self._mocked_flags['os_type'].Parse(os_types.WINDOWS)
self._mocked_flags['background_cpu_threads'].parse(1)
self._mocked_flags['os_type'].parse(os_types.WINDOWS)
spec = self._CreateBenchmarkSpec(ping_benchmark.BENCHMARK_CONFIG)
spec.ConstructVirtualMachines()
with self.assertRaisesRegexp(Exception, 'NotImplementedError'):
Expand All @@ -128,7 +128,7 @@ def testWindowsVMCausesError(self):

def testBackgroundWorkloadVM(self):
""" Check that the background_cpu_threads causes calls """
self._mocked_flags['background_cpu_threads'].Parse(1)
self._mocked_flags['background_cpu_threads'].parse(1)
spec = self._CreateBenchmarkSpec(ping_benchmark.BENCHMARK_CONFIG)
spec.ConstructVirtualMachines()
self._CheckVMFromSpec(spec, working_groups=(_GROUP_1, _GROUP_2))
Expand All @@ -144,7 +144,7 @@ def testBackgroundWorkloadVanillaConfig(self):

def testBackgroundWorkloadWindows(self):
""" Test that nothing happens with the vanilla config """
self._mocked_flags['os_type'].Parse(os_types.WINDOWS)
self._mocked_flags['os_type'].parse(os_types.WINDOWS)
spec = self._CreateBenchmarkSpec(ping_benchmark.BENCHMARK_CONFIG)
spec.ConstructVirtualMachines()
for vm in spec.vms:
Expand All @@ -154,7 +154,7 @@ def testBackgroundWorkloadWindows(self):

def testBackgroundWorkloadVanillaConfigFlag(self):
""" Check that the background_cpu_threads flags overrides the config """
self._mocked_flags['background_cpu_threads'].Parse(2)
self._mocked_flags['background_cpu_threads'].parse(2)
spec = self._CreateBenchmarkSpec(ping_benchmark.BENCHMARK_CONFIG)
spec.ConstructVirtualMachines()
for vm in spec.vms:
Expand Down
16 changes: 8 additions & 8 deletions tests/background_network_workload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def makeSpec(self, yaml_benchmark_config=ping_benchmark.BENCHMARK_CONFIG):

def testWindowsVMCausesError(self):
""" windows vm with background_network_mbits_per_sec raises exception """
self.mocked_flags['background_network_mbits_per_sec'].Parse(200)
self.mocked_flags['os_type'].Parse(os_types.WINDOWS)
self.mocked_flags['background_network_mbits_per_sec'].parse(200)
self.mocked_flags['os_type'].parse(os_types.WINDOWS)
spec = self.makeSpec()
with self.assertRaisesRegexp(Exception, 'NotImplementedError'):
spec.Prepare()
Expand All @@ -165,7 +165,7 @@ def testWindowsVMCausesError(self):

def testBackgroundWorkloadVM(self):
""" Check that the vm background workload calls work """
self.mocked_flags['background_network_mbits_per_sec'].Parse(200)
self.mocked_flags['background_network_mbits_per_sec'].parse(200)
spec = self.makeSpec()
self._CheckVMFromSpec(spec, working_groups=(_GROUP_1, _GROUP_2))

Expand All @@ -189,7 +189,7 @@ def testBackgroundWorkloadWindows(self):

def testBackgroundWorkloadVanillaConfigFlag(self):
""" Check that the flag overrides the config """
self.mocked_flags['background_network_mbits_per_sec'].Parse(200)
self.mocked_flags['background_network_mbits_per_sec'].parse(200)
spec = self.makeSpec()
for vm in spec.vms:
self.assertEqual(vm.background_network_mbits_per_sec, 200)
Expand All @@ -198,8 +198,8 @@ def testBackgroundWorkloadVanillaConfigFlag(self):

def testBackgroundWorkloadVanillaConfigFlagIpType(self):
""" Check that the flag overrides the config """
self.mocked_flags['background_network_mbits_per_sec'].Parse(200)
self.mocked_flags['background_network_ip_type'].Parse('INTERNAL')
self.mocked_flags['background_network_mbits_per_sec'].parse(200)
self.mocked_flags['background_network_ip_type'].parse('INTERNAL')
spec = self.makeSpec()
for vm in spec.vms:
self.assertEqual(vm.background_network_mbits_per_sec, 200)
Expand All @@ -208,8 +208,8 @@ def testBackgroundWorkloadVanillaConfigFlagIpType(self):

def testBackgroundWorkloadVanillaConfigBadIpTypeFlag(self):
""" Check that the flag overrides the config """
self.mocked_flags['background_network_mbits_per_sec'].Parse(200)
self.mocked_flags['background_network_ip_type'].Parse('IAMABADFLAGVALUE')
self.mocked_flags['background_network_mbits_per_sec'].parse(200)
self.mocked_flags['background_network_ip_type'].parse('IAMABADFLAGVALUE')
config = configs.LoadConfig(ping_benchmark.BENCHMARK_CONFIG, {}, NAME)
with self.assertRaises(errors.Config.InvalidValue):
benchmark_config_spec.BenchmarkConfigSpec(
Expand Down
8 changes: 4 additions & 4 deletions tests/disk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ def testNonPresentFlagsDoNotOverrideConfigs(self):

def testPresentFlagsOverrideConfigs(self):
flags = mock_flags.MockFlags()
flags['data_disk_size'].Parse(100)
flags['data_disk_type'].Parse('flag_disk_type')
flags['num_striped_disks'].Parse(3)
flags['scratch_dir'].Parse('/flag_scratch_dir')
flags['data_disk_size'].parse(100)
flags['data_disk_type'].parse('flag_disk_type')
flags['num_striped_disks'].parse(3)
flags['scratch_dir'].parse('/flag_scratch_dir')
spec = disk.BaseDiskSpec(
_COMPONENT, flags, device_path='config_device_path', disk_number=1,
disk_size=75, disk_type='config_disk_type', mount_point='/mountpoint',
Expand Down
52 changes: 26 additions & 26 deletions tests/flag_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ def setUp(self):
self.ilp = flag_util.IntegerListParser()

def testOneInteger(self):
self.assertEqual(list(self.ilp.Parse('3')), [3])
self.assertEqual(list(self.ilp.parse('3')), [3])

def testIntegerRange(self):
self.assertEqual(list(self.ilp.Parse('3-5')), [3, 4, 5])
self.assertEqual(list(self.ilp.parse('3-5')), [3, 4, 5])

def testIntegerRangeWithStep(self):
self.assertEqual(list(self.ilp.Parse('2-7-2')), [2, 4, 6])

def testIntegerList(self):
self.assertEqual(list(self.ilp.Parse('3-5,8,10-12')),
self.assertEqual(list(self.ilp.parse('3-5,8,10-12')),
[3, 4, 5, 8, 10, 11, 12])

def testIntegerListWithRangeAndStep(self):
Expand All @@ -92,31 +92,31 @@ def testIntegerListWithRangeAndStep(self):

def testNoInteger(self):
with self.assertRaises(ValueError):
self.ilp.Parse('a')
self.ilp.parse('a')

def testBadRange(self):
with self.assertRaises(ValueError):
self.ilp.Parse('3-a')
self.ilp.parse('3-a')

def testBadList(self):
with self.assertRaises(ValueError):
self.ilp.Parse('3-5,8a')
self.ilp.parse('3-5,8a')

def testTrailingComma(self):
with self.assertRaises(ValueError):
self.ilp.Parse('3-5,')
self.ilp.parse('3-5,')

def testNonIncreasingEntries(self):
ilp = flag_util.IntegerListParser(
on_nonincreasing=flag_util.IntegerListParser.EXCEPTION)
with self.assertRaises(ValueError):
ilp.Parse('3,2,1')
ilp.parse('3,2,1')

def testNonIncreasingRange(self):
ilp = flag_util.IntegerListParser(
on_nonincreasing=flag_util.IntegerListParser.EXCEPTION)
with self.assertRaises(ValueError):
ilp.Parse('3-1')
ilp.parse('3-1')

def testNonIncreasingRangeWithStep(self):
ilp = flag_util.IntegerListParser(
Expand All @@ -130,7 +130,7 @@ def testSerialize(self):
ser = flag_util.IntegerListSerializer()
il = flag_util.IntegerList([1, (2, 5), 9])

self.assertEqual(ser.Serialize(il),
self.assertEqual(ser.serialize(il),
'1,2-5,9')

def testSerializeWithStep(self):
Expand Down Expand Up @@ -180,50 +180,50 @@ def setUp(self):
self.up = flag_util.UnitsParser('byte')

def testParser(self):
self.assertEqual(self.up.Parse('10KiB'), 10 * 1024 * units.byte)
self.assertEqual(self.up.parse('10KiB'), 10 * 1024 * units.byte)

def testQuantity(self):
quantity = 1.0 * units.byte
self.assertEqual(self.up.Parse(quantity), quantity)
self.assertEqual(self.up.parse(quantity), quantity)

def testBadExpression(self):
with self.assertRaises(ValueError):
self.up.Parse('asdf')
self.up.parse('asdf')

def testUnitlessExpression(self):
with self.assertRaises(ValueError):
self.up.Parse('10')
self.up.parse('10')

def testBytes(self):
q = self.up.Parse('1B')
q = self.up.parse('1B')
self.assertEqual(q.magnitude, 1.0)
self.assertEqual(q.units, {'byte': 1.0})

def testBytesWithPrefix(self):
q = self.up.Parse('2KB').to(units.byte)
q = self.up.parse('2KB').to(units.byte)
self.assertEqual(q.magnitude, 2000.0)
self.assertEqual(q.units, {'byte': 1.0})

def testWrongUnit(self):
with self.assertRaises(ValueError):
self.up.Parse('1m')
self.up.parse('1m')

def testConvertibleToUnit(self):
up = flag_util.UnitsParser(convertible_to=units.byte)
self.assertEqual(up.Parse('10KiB'), 10 * 1024 * units.byte)
self.assertEqual(up.parse('10KiB'), 10 * 1024 * units.byte)

def testConvertibleToSeries(self):
up = flag_util.UnitsParser(convertible_to=(units.byte, 'second'))
self.assertEqual(up.Parse('10 MB'), 10 * units.Unit('megabyte'))
self.assertEqual(up.Parse('10 minutes'), 10 * units.Unit('minute'))
self.assertEqual(up.parse('10 MB'), 10 * units.Unit('megabyte'))
self.assertEqual(up.parse('10 minutes'), 10 * units.Unit('minute'))
with self.assertRaises(ValueError):
up.Parse('1 meter')
up.parse('1 meter')

def testPercent(self):
up = flag_util.UnitsParser(convertible_to=units.percent)
self.assertEqual(up.Parse('100%'), 100 * units.percent)
self.assertEqual(up.parse('100%'), 100 * units.percent)
with self.assertRaises(ValueError):
up.Parse('10KiB')
up.parse('10KiB')


class TestStringToBytes(unittest.TestCase):
Expand Down Expand Up @@ -280,16 +280,16 @@ def setUp(self):
self.parser = flag_util.YAMLParser()

def testValidString(self):
self.assertEqual(self.parser.Parse('[1, 2, 3]'),
self.assertEqual(self.parser.parse('[1, 2, 3]'),
[1, 2, 3])

def testPreParsedYAML(self):
self.assertEqual(self.parser.Parse([1, 2, 3]),
self.assertEqual(self.parser.parse([1, 2, 3]),
[1, 2, 3])

def testBadYAML(self):
with self.assertRaises(ValueError):
self.parser.Parse('{a')
self.parser.parse('{a')


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 9e130a4

Please sign in to comment.