diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2019-10-24 20:11:14 -0700 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2019-10-24 22:11:14 -0500 |
commit | f73e2571fb643a2afdde365eeee0fe0f3f4f5300 (patch) | |
tree | f9015586cee7efc5e60eee78a8ebcbaa4e9e953d /examples/statemachine | |
parent | 696808023f10207461d7b22dc1d02cbed44e2bfa (diff) | |
download | pyparsing-git-f73e2571fb643a2afdde365eeee0fe0f3f4f5300.tar.gz |
Use pyupgrade to upgrade the code to use Python3 conventions (#138)
The pyupgrade project is available at
https://github.com/asottile/pyupgrade and can be installed through pip.
The pyupgrade tool automatically upgrades syntax for newer versions of
the language. As pyparsing is now Python 3 only, can apply some cleanups
and simplifications. Ran the tool using the following command:
$ find . -name \*.py -exec pyupgrade --py3-plus {} \;
For now, pyparsing.py was skipped while it is refactored to a package.
Diffstat (limited to 'examples/statemachine')
-rw-r--r-- | examples/statemachine/libraryBookDemo.py | 4 | ||||
-rw-r--r-- | examples/statemachine/statemachine.py | 16 | ||||
-rw-r--r-- | examples/statemachine/trafficLightDemo.py | 2 | ||||
-rw-r--r-- | examples/statemachine/vending_machine.py | 6 |
4 files changed, 14 insertions, 14 deletions
diff --git a/examples/statemachine/libraryBookDemo.py b/examples/statemachine/libraryBookDemo.py index a5e018d..98f0b2b 100644 --- a/examples/statemachine/libraryBookDemo.py +++ b/examples/statemachine/libraryBookDemo.py @@ -15,7 +15,7 @@ class Book(librarybookstate.BookStateMixin): class RestrictedBook(Book): def __init__(self): - super(RestrictedBook, self).__init__() + super().__init__() self._authorized_users = [] def authorize(self, name): @@ -26,7 +26,7 @@ class RestrictedBook(Book): if user in self._authorized_users: super().checkout() else: - raise Exception("{0} could not check out restricted book".format(user if user is not None else "anonymous")) + raise Exception("{} could not check out restricted book".format(user if user is not None else "anonymous")) def run_demo(): diff --git a/examples/statemachine/statemachine.py b/examples/statemachine/statemachine.py index f318ee5..4c8ee1d 100644 --- a/examples/statemachine/statemachine.py +++ b/examples/statemachine/statemachine.py @@ -72,10 +72,10 @@ def expand_state_definition(source, loc, tokens): ]) # define all state classes - statedef.extend("class {0}({1}): pass".format(s, baseStateClass) for s in states) + statedef.extend("class {}({}): pass".format(s, baseStateClass) for s in states) # define state->state transitions - statedef.extend("{0}._next_state_class = {1}".format(s, fromTo[s]) for s in states if s in fromTo) + statedef.extend("{}._next_state_class = {}".format(s, fromTo[s]) for s in states if s in fromTo) statedef.extend([ "class {baseStateClass}Mixin:".format(baseStateClass=baseStateClass), @@ -178,20 +178,20 @@ def expand_named_state_definition(source, loc, tokens): for tn in transitions) # define all state classes - statedef.extend("class %s(%s): pass" % (s, baseStateClass) + statedef.extend("class {}({}): pass".format(s, baseStateClass) for s in states) # define state transition methods for valid transitions from each state for s in states: trns = list(fromTo[s].items()) # statedef.append("%s.tnmap = {%s}" % (s, ", ".join("%s:%s" % tn for tn in trns))) - statedef.extend("%s.%s = classmethod(lambda cls: %s())" % (s, tn_, to_) + statedef.extend("{}.{} = classmethod(lambda cls: {}())".format(s, tn_, to_) for tn_, to_ in trns) statedef.extend([ "{baseStateClass}.transitions = classmethod(lambda cls: [{transition_class_list}])".format( baseStateClass=baseStateClass, - transition_class_list = ', '.join("cls.{0}".format(tn) for tn in transitions) + transition_class_list = ', '.join("cls.{}".format(tn) for tn in transitions) ), "{baseStateClass}.transition_names = [tn.__name__ for tn in {baseStateClass}.transitions()]".format( baseStateClass=baseStateClass @@ -236,7 +236,7 @@ namedStateMachine.setParseAction(expand_named_state_definition) # ====================================================================== # NEW STUFF - Matt Anderson, 2009-11-26 # ====================================================================== -class SuffixImporter(object): +class SuffixImporter: """An importer designed using the mechanism defined in :pep:`302`. I read the PEP, and also used Doug Hellmann's PyMOTW article `Modules and Imports`_, as a pattern. @@ -279,7 +279,7 @@ class SuffixImporter(object): # it probably isn't even a filesystem path finder = sys.path_importer_cache.get(dirpath) if isinstance(finder, (type(None), importlib.machinery.FileFinder)): - checkpath = os.path.join(dirpath, '{0}.{1}'.format(fullname, self.suffix)) + checkpath = os.path.join(dirpath, '{}.{}'.format(fullname, self.suffix)) yield checkpath def find_module(self, fullname, path=None): @@ -337,4 +337,4 @@ class PystateImporter(SuffixImporter): PystateImporter.register() if DEBUG: - print("registered {0!r} importer".format(PystateImporter.suffix)) + print("registered {!r} importer".format(PystateImporter.suffix)) diff --git a/examples/statemachine/trafficLightDemo.py b/examples/statemachine/trafficLightDemo.py index a8fac8c..5ff94b1 100644 --- a/examples/statemachine/trafficLightDemo.py +++ b/examples/statemachine/trafficLightDemo.py @@ -18,7 +18,7 @@ class TrafficLight(trafficlightstate.TrafficLightStateMixin): light = TrafficLight() for i in range(10): - print("{0} {1}".format(light, ("STOP", "GO")[light.cars_can_go])) + print("{} {}".format(light, ("STOP", "GO")[light.cars_can_go])) light.crossing_signal() light.delay() print() diff --git a/examples/statemachine/vending_machine.py b/examples/statemachine/vending_machine.py index f48d2f9..d218608 100644 --- a/examples/statemachine/vending_machine.py +++ b/examples/statemachine/vending_machine.py @@ -46,7 +46,7 @@ class VendingMachine(VendingMachineStateMixin): def press_alpha_button(self): try: - super(VendingMachine, self).press_alpha_button() + super().press_alpha_button() except VendingMachineState.InvalidTransitionException as ite: print(ite) else: @@ -54,7 +54,7 @@ class VendingMachine(VendingMachineStateMixin): def press_digit_button(self): try: - super(VendingMachine, self).press_digit_button() + super().press_digit_button() except VendingMachineState.InvalidTransitionException as ite: print(ite) else: @@ -63,7 +63,7 @@ class VendingMachine(VendingMachineStateMixin): def dispense(self): try: - super(VendingMachine, self).dispense() + super().dispense() except VendingMachineState.InvalidTransitionException as ite: print(ite) else: |