From 701ae7999bdecd06414d92d2e4b8f825afea83ee Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Tue, 8 Jan 2019 07:06:03 +0100 Subject: [PATCH 1/1] autopep8 our python examples --- examples/python/actor-create/actor-create.py | 153 ++++++++++-------- examples/python/actor-daemon/actor-daemon.py | 16 +- examples/python/actor-join/actor-join.py | 49 +++--- .../python/actor-migrate/actor-migrate.py | 38 +++-- .../python/actor-suspend/actor-suspend.py | 38 +++-- examples/python/actor-yield/actor-yield.py | 14 +- examples/python/exec-basic/exec-basic.py | 9 +- 7 files changed, 185 insertions(+), 132 deletions(-) diff --git a/examples/python/actor-create/actor-create.py b/examples/python/actor-create/actor-create.py index dd4e4d4291..c8719b7293 100644 --- a/examples/python/actor-create/actor-create.py +++ b/examples/python/actor-create/actor-create.py @@ -19,83 +19,100 @@ import sys from simgrid import * -# Our first class of actors is simply implemented with a function, that takes a single string as parameter. -# -# Later, this actor class is instantiated within the simulation. + def receiver(mailbox_name): - mailbox = Mailbox.by_name(mailbox_name) + """ + Our first class of actors is simply implemented with a function, that takes a single string as parameter. + Later, this actor class is instantiated within the simulation. + """ + mailbox = Mailbox.by_name(mailbox_name) - this_actor.info("Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.name)) + this_actor.info( + "Hello s4u, I'm ready to get any message you'd want on {:s}".format(mailbox.name)) + + msg1 = mailbox.get() + msg2 = mailbox.get() + msg3 = mailbox.get() + this_actor.info( + "I received '{:s}', '{:s}' and '{:s}'".format(msg1, msg2, msg3)) + this_actor.info("I'm done. See you.") - msg1 = mailbox.get() - msg2 = mailbox.get() - msg3 = mailbox.get() - this_actor.info("I received '{:s}', '{:s}' and '{:s}'".format(msg1, msg2, msg3)) - this_actor.info("I'm done. See you.") -# Our second class of actors is also a function def forwarder(*args): - if len(args) < 2: raise AssertionError("Actor forwarder requires 2 parameters, but got only {:d}".format(len(args))) - mb_in = Mailbox.by_name(args[0]) - mb_out = Mailbox.by_name(args[1]) + """Our second class of actors is also a function""" + if len(args) < 2: + raise AssertionError( + "Actor forwarder requires 2 parameters, but got only {:d}".format(len(args))) + mb_in = Mailbox.by_name(args[0]) + mb_out = Mailbox.by_name(args[1]) + + msg = mb_in.get() + this_actor.info("Forward '{:s}'.".format(msg)) + mb_out.put(msg, len(msg)) - msg = mb_in.get() - this_actor.info("Forward '{:s}'.".format(msg)) - mb_out.put(msg, len(msg)) -# Declares a third class of actors which sends a message to the mailbox 'mb42'. -# The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default) -# -# Later, this actor class is instantiated twice in the simulation. class Sender: - mbox = "mb42" - msg = "GaBuZoMeu"; - def __init__(self, *args): - if len(args) > 0: self.msg = args[0]; - if len(args) > 1: self.mbox = args[1]; - if len(args) > 2: raise AssertionError("Actor sender requires 2 parameters, but got only {:d}".format(len(args))) + """ + Declares a third class of actors which sends a message to the mailbox 'mb42'. + The sent message is what was passed as parameter on creation (or 'GaBuZoMeu' by default) + + Later, this actor class is instantiated twice in the simulation. + """ + mbox = "mb42" + msg = "GaBuZoMeu" + + def __init__(self, *args): + if len(args) > 0: + self.msg = args[0] + if len(args) > 1: + self.mbox = args[1] + if len(args) > 2: + raise AssertionError( + "Actor sender requires 2 parameters, but got only {:d}".format(len(args))) + + def __call__(self): + this_actor.info("Hello s4u, I have something to send") + mailbox = Mailbox.by_name(self.mbox) - def __call__(self): - this_actor.info("Hello s4u, I have something to send") - mailbox = Mailbox.by_name(self.mbox) + mailbox.put(self.msg, len(self.msg)) + this_actor.info("I'm done. See you.") - mailbox.put(self.msg, len(self.msg)) - this_actor.info("I'm done. See you.") -# Here comes the main function of your program if __name__ == '__main__': - # When your program starts, you have to first start a new simulation engine, as follows - e = Engine(sys.argv) - - # Then you should load a platform file, describing your simulated platform - e.load_platform("../../platforms/small_platform.xml"); - - # And now you have to ask SimGrid to actually start your actors. - # - # The easiest way to do so is to implement the behavior of your actor in a single function, - # as we do here for the receiver actors. This function can take any kind of parameters, as - # long as the last parameters of Actor::create() match what your function expects. - Actor.create("receiver", Host.by_name("Fafard"), receiver, "mb42") - - # If your actor is getting more complex, you probably want to implement it as a class instead, - # as we do here for the sender actors. The main behavior goes into operator()() of the class. - # - # You can then directly start your actor, as follows: - Actor.create("sender1", Host.by_name("Tremblay"), Sender()) - # If you want to pass parameters to your class, that's very easy: just use your constructors - Actor.create("sender2", Host.by_name("Jupiter"), Sender("GloubiBoulga")); - - # But starting actors directly is considered as a bad experimental habit, since it ties the code - # you want to test with the experimental scenario. Starting your actors from an external deployment - # file in XML ensures that you can test your code in several scenarios without changing the code itself. - # - # For that, you first need to register your function or your actor as follows. - e.register_actor("sender", Sender) - e.register_actor("forwarder", forwarder) - # Once actors and functions are registered, just load the deployment file - e.load_deployment("actor-create_d.xml") - - # Once every actors are started in the engine, the simulation can start - e.run(); - - # Once the simulation is done, the program is ended + """Here comes the main function of your program""" + + # When your program starts, you have to first start a new simulation engine, as follows + e = Engine(sys.argv) + + # Then you should load a platform file, describing your simulated platform + e.load_platform("../../platforms/small_platform.xml") + + # And now you have to ask SimGrid to actually start your actors. + # + # The easiest way to do so is to implement the behavior of your actor in a single function, + # as we do here for the receiver actors. This function can take any kind of parameters, as + # long as the last parameters of Actor::create() match what your function expects. + Actor.create("receiver", Host.by_name("Fafard"), receiver, "mb42") + + # If your actor is getting more complex, you probably want to implement it as a class instead, + # as we do here for the sender actors. The main behavior goes into operator()() of the class. + # + # You can then directly start your actor, as follows: + Actor.create("sender1", Host.by_name("Tremblay"), Sender()) + # If you want to pass parameters to your class, that's very easy: just use your constructors + Actor.create("sender2", Host.by_name("Jupiter"), Sender("GloubiBoulga")) + + # But starting actors directly is considered as a bad experimental habit, since it ties the code + # you want to test with the experimental scenario. Starting your actors from an external deployment + # file in XML ensures that you can test your code in several scenarios without changing the code itself. + # + # For that, you first need to register your function or your actor as follows. + e.register_actor("sender", Sender) + e.register_actor("forwarder", forwarder) + # Once actors and functions are registered, just load the deployment file + e.load_deployment("actor-create_d.xml") + + # Once every actors are started in the engine, the simulation can start + e.run() + + # Once the simulation is done, the program is ended diff --git a/examples/python/actor-daemon/actor-daemon.py b/examples/python/actor-daemon/actor-daemon.py index c8dc581d17..46aa28b846 100644 --- a/examples/python/actor-daemon/actor-daemon.py +++ b/examples/python/actor-daemon/actor-daemon.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017-2018. The SimGrid Team. All rights reserved. +# Copyright (c) 2017-2018. The SimGrid Team. All rights reserved. # # This program is free software you can redistribute it and/or modify it # under the terms of the license (GNU LGPL) which comes with this package. @@ -6,26 +6,32 @@ from simgrid import * import sys -# The worker process, working for a while before leaving + def worker(): + """The worker process, working for a while before leaving""" this_actor.info("Let's do some work (for 10 sec on Boivin).") this_actor.execute(980.95e6) this_actor.info("I'm done now. I leave even if it makes the daemon die.") -# The daemon, displaying a message every 3 seconds until all other processes stop + def my_daemon(): + """The daemon, displaying a message every 3 seconds until all other processes stop""" Actor.self().daemonize() while True: this_actor.info("Hello from the infinite loop") this_actor.sleep_for(3.0) - this_actor.info("I will never reach that point: daemons are killed when regular processes are done") + this_actor.info( + "I will never reach that point: daemons are killed when regular processes are done") + if __name__ == '__main__': e = Engine(sys.argv) - if len(sys.argv) < 2: raise AssertionError("Usage: actor-daemon.py platform_file [other parameters]") + if len(sys.argv) < 2: + raise AssertionError( + "Usage: actor-daemon.py platform_file [other parameters]") e.load_platform(sys.argv[1]) Actor.create("worker", Host.by_name("Boivin"), worker) diff --git a/examples/python/actor-join/actor-join.py b/examples/python/actor-join/actor-join.py index f0aba2af6b..0a59caf324 100644 --- a/examples/python/actor-join/actor-join.py +++ b/examples/python/actor-join/actor-join.py @@ -6,43 +6,48 @@ from simgrid import * import sys + def sleeper(): this_actor.info("Sleeper started") this_actor.sleep_for(3) this_actor.info("I'm done. See you!") + def master(): - this_actor.info("Start sleeper") - actor = Actor.create("sleeper from master", Host.current(), sleeper) - this_actor.info("Join the sleeper (timeout 2)") - actor.join(2) + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 2)") + actor.join(2) + + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 4)") + actor.join(4) - this_actor.info("Start sleeper") - actor = Actor.create("sleeper from master", Host.current(), sleeper) - this_actor.info("Join the sleeper (timeout 4)") - actor.join(4) + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Join the sleeper (timeout 2)") + actor.join(2) - this_actor.info("Start sleeper") - actor = Actor.create("sleeper from master", Host.current(), sleeper) - this_actor.info("Join the sleeper (timeout 2)") - actor.join(2) + this_actor.info("Start sleeper") + actor = Actor.create("sleeper from master", Host.current(), sleeper) + this_actor.info("Waiting 4") + this_actor.sleep_for(4) + this_actor.info("Join the sleeper after its end (timeout 1)") + actor.join(1) - this_actor.info("Start sleeper") - actor = Actor.create("sleeper from master", Host.current(), sleeper) - this_actor.info("Waiting 4") - this_actor.sleep_for(4) - this_actor.info("Join the sleeper after its end (timeout 1)") - actor.join(1) + this_actor.info("Goodbye now!") - this_actor.info("Goodbye now!") + this_actor.sleep_for(1) - this_actor.sleep_for(1) + this_actor.info("Goodbye now!") - this_actor.info("Goodbye now!") if __name__ == '__main__': e = Engine(sys.argv) - if len(sys.argv) < 2: raise AssertionError("Usage: actor-join.py platform_file [other parameters]") + if len(sys.argv) < 2: + raise AssertionError( + "Usage: actor-join.py platform_file [other parameters]") e.load_platform(sys.argv[1]) diff --git a/examples/python/actor-migrate/actor-migrate.py b/examples/python/actor-migrate/actor-migrate.py index 48b4b4931f..cf95683d80 100644 --- a/examples/python/actor-migrate/actor-migrate.py +++ b/examples/python/actor-migrate/actor-migrate.py @@ -18,43 +18,51 @@ from simgrid import * import sys + def worker(first_host, second_host): flop_amount = first_host.speed * 5 + second_host.speed * 5 - this_actor.info("Let's move to {:s} to execute {:.2f} Mflops (5sec on {:s} and 5sec on {:s})".format(first_host.name, flop_amount / 1e6, first_host.name, second_host.name)) + this_actor.info("Let's move to {:s} to execute {:.2f} Mflops (5sec on {:s} and 5sec on {:s})".format( + first_host.name, flop_amount / 1e6, first_host.name, second_host.name)) this_actor.migrate(first_host) this_actor.execute(flop_amount) - this_actor.info("I wake up on {:s}. Let's suspend a bit".format(this_actor.get_host().name)) + this_actor.info("I wake up on {:s}. Let's suspend a bit".format( + this_actor.get_host().name)) this_actor.suspend() this_actor.info("I wake up on {:s}".format(this_actor.get_host().name)) this_actor.info("Done") + def monitor(): - boivin = Host.by_name("Boivin") - jacquelin = Host.by_name("Jacquelin") - fafard = Host.by_name("Fafard") + boivin = Host.by_name("Boivin") + jacquelin = Host.by_name("Jacquelin") + fafard = Host.by_name("Fafard") - actor = Actor.create("worker", fafard, worker, boivin, jacquelin) + actor = Actor.create("worker", fafard, worker, boivin, jacquelin) - this_actor.sleep_for(5) + this_actor.sleep_for(5) - this_actor.info("After 5 seconds, move the process to {:s}".format(jacquelin.name)) - actor.migrate(jacquelin) + this_actor.info( + "After 5 seconds, move the process to {:s}".format(jacquelin.name)) + actor.migrate(jacquelin) + + this_actor.sleep_until(15) + this_actor.info( + "At t=15, move the process to {:s} and resume it.".format(fafard.name)) + actor.migrate(fafard) + actor.resume() - this_actor.sleep_until(15) - this_actor.info("At t=15, move the process to {:s} and resume it.".format(fafard.name)) - actor.migrate(fafard) - actor.resume() if __name__ == '__main__': e = Engine(sys.argv) - if len(sys.argv) < 2: raise AssertionError("Usage: actor-migration.py platform_file [other parameters]") + if len(sys.argv) < 2: + raise AssertionError( + "Usage: actor-migration.py platform_file [other parameters]") e.load_platform(sys.argv[1]) Actor.create("monitor", Host.by_name("Boivin"), monitor) e.run() - diff --git a/examples/python/actor-suspend/actor-suspend.py b/examples/python/actor-suspend/actor-suspend.py index 7d53f81c97..5fecb46c6f 100644 --- a/examples/python/actor-suspend/actor-suspend.py +++ b/examples/python/actor-suspend/actor-suspend.py @@ -6,13 +6,15 @@ from simgrid import * import sys -# The Lazy guy only wants to sleep, but can be awaken by the dream_master process. + def lazy_guy(): + """The Lazy guy only wants to sleep, but can be awaken by the dream_master process""" this_actor.info("Nobody's watching me ? Let's go to sleep.") - this_actor.suspend() # - Start by suspending itself + this_actor.suspend() # - Start by suspending itself this_actor.info("Uuuh ? Did somebody call me ?") - this_actor.info("Going to sleep...") # - Then repetitively go to sleep, but get awaken + # - Then repetitively go to sleep, but get awaken + this_actor.info("Going to sleep...") this_actor.sleep_for(10) this_actor.info("Mmm... waking up.") @@ -25,25 +27,27 @@ def lazy_guy(): this_actor.info("Mmmh, I'm done now. Goodbye.") -# The Dream master + def dream_master(): + """The Dream master""" this_actor.info("Let's create a lazy guy.") # Create a lazy_guy process lazy = Actor.create("Lazy", this_actor.get_host(), lazy_guy) this_actor.info("Let's wait a little bit...") - this_actor.sleep_for(10) # Wait for 10 seconds + this_actor.sleep_for(10) # Wait for 10 seconds this_actor.info("Let's wake the lazy guy up! >:) BOOOOOUUUHHH!!!!") if lazy.is_suspended(): - lazy.resume() # Then wake up the lazy_guy + lazy.resume() # Then wake up the lazy_guy else: - this_actor.error("I was thinking that the lazy guy would be suspended now") + this_actor.error( + "I was thinking that the lazy guy would be suspended now") - this_actor.sleep_for(5) # Repeat two times: + this_actor.sleep_for(5) # Repeat two times: this_actor.info("Suspend the lazy guy while he's sleeping...") - lazy.suspend() # Suspend the lazy_guy while he's asleep + lazy.suspend() # Suspend the lazy_guy while he's asleep this_actor.info("Let him finish his siesta.") - this_actor.sleep_for(10) # Wait for 10 seconds + this_actor.sleep_for(10) # Wait for 10 seconds this_actor.info("Wake up, lazy guy!") - lazy.resume() # Then wake up the lazy_guy again + lazy.resume() # Then wake up the lazy_guy again this_actor.sleep_for(5) this_actor.info("Suspend again the lazy guy while he's sleeping...") @@ -54,7 +58,8 @@ def dream_master(): lazy.resume() this_actor.sleep_for(5) - this_actor.info("Give a 2 seconds break to the lazy guy while he's working...") + this_actor.info( + "Give a 2 seconds break to the lazy guy while he's working...") lazy.suspend() this_actor.sleep_for(2) this_actor.info("Back to work, lazy guy!") @@ -62,12 +67,15 @@ def dream_master(): this_actor.info("OK, I'm done here.") + if __name__ == '__main__': e = Engine(sys.argv) - if len(sys.argv) < 2: raise AssertionError("Usage: actor-suspend.py platform_file [other parameters]") + if len(sys.argv) < 2: + raise AssertionError( + "Usage: actor-suspend.py platform_file [other parameters]") - e.load_platform(sys.argv[1]) # Load the platform description + e.load_platform(sys.argv[1]) # Load the platform description list = e.get_all_hosts() Actor.create("dream_master", list[0], dream_master) - e.run() # Run the simulation + e.run() # Run the simulation diff --git a/examples/python/actor-yield/actor-yield.py b/examples/python/actor-yield/actor-yield.py index ce2bbc68f8..dc108bd049 100644 --- a/examples/python/actor-yield/actor-yield.py +++ b/examples/python/actor-yield/actor-yield.py @@ -14,22 +14,28 @@ from simgrid import * # # It can also be used to benchmark our context-switching mechanism. -# Main function of the Yielder process + class Yielder: + """Main function of the Yielder process""" number_of_yields = 0 + def __init__(self, *args): self.number_of_yields = int(args[0]) + def __call__(self): for _ in range(self.number_of_yields): this_actor.yield_() - this_actor.info("I yielded {:d} times. Goodbye now!".format(self.number_of_yields)) + this_actor.info("I yielded {:d} times. Goodbye now!".format( + self.number_of_yields)) + if __name__ == '__main__': e = Engine(sys.argv) e.load_platform(sys.argv[1]) # Load the platform description - e.register_actor("yielder", Yielder) # Register the class representing the actors + # Register the class representing the actors + e.register_actor("yielder", Yielder) e.load_deployment(sys.argv[2]) - e.run() # - Run the simulation + e.run() # - Run the simulation diff --git a/examples/python/exec-basic/exec-basic.py b/examples/python/exec-basic/exec-basic.py index 6577bd54cb..fa001b9397 100644 --- a/examples/python/exec-basic/exec-basic.py +++ b/examples/python/exec-basic/exec-basic.py @@ -6,6 +6,7 @@ import sys from simgrid import * + def executor(): # execute() tells SimGrid to pause the calling actor until # its host has computed the amount of flops passed as a parameter @@ -13,20 +14,22 @@ def executor(): this_actor.info("Done.") # This simple example does not do anything beyond that + def privileged(): # You can also specify the priority of your execution as follows. # An execution of priority 2 computes twice as fast as a regular one. # # So instead of a half/half sharing between the two executions, # we get a 1/3 vs 2/3 sharing. - this_actor.execute(98095, priority = 2); - this_actor.info("Done."); + this_actor.execute(98095, priority=2) + this_actor.info("Done.") # Note that the timings printed when executing this example are a bit misleading, # because the uneven sharing only last until the privileged actor ends. # After this point, the unprivileged one gets 100% of the CPU and finishes # quite quickly. + i = 0 if "--" in sys.argv: i = sys.argv.index("--") @@ -36,4 +39,4 @@ e.load_platform(sys.argv[i+1]) Actor.create("executor", Host.by_name("Tremblay"), executor) Actor.create("privileged", Host.by_name("Tremblay"), privileged) -e.run() \ No newline at end of file +e.run() -- 2.20.1