From 9d02080db8e975ac42e952f0fb221e24e6c2398d Mon Sep 17 00:00:00 2001 From: sleepinghungry Date: Tue, 31 Jan 2017 15:03:50 -0800 Subject: [PATCH 1/2] Added some parentheses to print statements. --- advent.py | 60 +++++++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/advent.py b/advent.py index 4288fad..961791b 100644 --- a/advent.py +++ b/advent.py @@ -530,7 +530,7 @@ def print_output(self, text, message_type = 0): if self.http_output: self.http_text += self.style_text(text, message_type) + "\n" else: - print self.style_text(text, message_type) + print(self.style_text(text, message_type)) # checks to see if the inventory in the items list is in the user's inventory def inventory_contains(self, items): @@ -1079,7 +1079,7 @@ def go(self, actor, way): def debug(self): for key in self.exits: - print "exit: %s" % key + print("exit: %s" % key) # A "connection" connects point A to point B. Connections are @@ -1420,7 +1420,7 @@ def start_running(self): def start_checking(self): assert self.running assert not self.recording - print "check_responses on" + print("check_responses on") self.check_responses = True self.current_response = "" @@ -1513,10 +1513,10 @@ def set_next_response(self, response): def print_script(self): i = 0 for command in self.commands: - print "> " + command + print("> " + command) if command == "end": break - print self.responses[i] + print(self.responses[i]) i = i + 1 def save_file(self): @@ -1606,11 +1606,11 @@ def act_start_recording(self, actor, noun, words): def act_run_script(self, actor, noun, words): if self.current_script: - print "You must stop \"%s\" first." % (self.current_script.name) + print("You must stop \"%s\" first." % (self.current_script.name)) script_name = self.parse_script_name(noun) if not script_name in self.scripts: - print "%s can't find script \"%s\" in its memory." % (self.name, - script_name) + print("%s can't find script \"%s\" in its memory." % (self.name, + script_name)) return True; @@ -1631,30 +1631,30 @@ def act_check_script(self, actor, noun, words): def act_print_script(self, actor, noun, words): script_name = self.parse_script_name(noun) if not script_name in self.scripts: - print "%s can't find script \"%s\" in its memory." % (self.name, - script_name) + print("%s can't find script \"%s\" in its memory." % (self.name, + script_name)) return True - print "----------------------8<-------------------------\n" - print "# Paste the following into your game code in order" - print "# to be able to run this script in the game:" - print "%s_script = Script(\"%s\"," % (script_name, script_name) - print "\"\"\"" + print("----------------------8<-------------------------\n") + print("# Paste the following into your game code in order") + print("# to be able to run this script in the game:") + print("%s_script = Script(\"%s\"," % (script_name, script_name)) + print("\"\"\"") self.scripts[script_name].print_script() - print "\"\"\")" - print "\n# Then add the script to a player, or a robot" - print "# with code like the following:" - print "player.add_script(%s_script)" % script_name - print "\n# Now you can run the script from within the game" - print "# by typing \"run %s\"" % script_name - print "\n---------------------->8-------------------------" + print("\"\"\")") + print("\n# Then add the script to a player, or a robot") + print("# with code like the following:") + print("player.add_script(%s_script)" % script_name) + print("\n# Now you can run the script from within the game") + print("# by typing \"run %s\"" % script_name) + print("\n---------------------->8-------------------------") return True def act_save_file(self, actor, noun, words): script_name = self.parse_script_name(noun) if not script_name in self.scripts: - print "%s can't find script \"%s\" in its memory." % (self.name, - script_name) + print("%s can't find script \"%s\" in its memory." % (self.name, + script_name)) return True self.scripts[script_name].save_file() return True @@ -1676,7 +1676,7 @@ def set_think_time(self, actor, noun, words): self.script_think_time = t return True - print "\"think\" requires a number of seconds (0.0000-60.0000) as an argument" + print("\"think\" requires a number of seconds (0.0000-60.0000) as an argument") return True def get_next_script_command(self): @@ -1684,23 +1684,23 @@ def get_next_script_command(self): return None line = self.current_script.get_next_command() if not line: - print "%s %s done running script \"%s\"." % (self.name, + print("%s %s done running script \"%s\"." % (self.name, self.isare, - self.current_script.name) + self.current_script.name)) self.current_script = None return None if self.script_think_time > 0: time.sleep(self.script_think_time) line = self.name + ": " + line - print "> %s" % line + print("> %s" % line) return line def set_next_script_command(self, command): if not self.current_script: return True if not self.current_script.set_next_command(command): - print "%s finished recording script \"%s\"." % (self.name, - self.current_script.name) + print("%s finished recording script \"%s\"." % (self.name, + self.current_script.name)) self.current_script = None return False return True From f88cd50601841fc0cee7f5b54744a61ab0cbec65 Mon Sep 17 00:00:00 2001 From: sleepinghungry Date: Wed, 1 Feb 2017 21:00:58 -0800 Subject: [PATCH 2/2] Port to python 3.5.2. --- advent.py | 12 ++++++------ bwx-game.py | 34 +++++++++++++++++----------------- graphviz_writer.py | 8 ++++---- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/advent.py b/advent.py index 961791b..271f0c0 100644 --- a/advent.py +++ b/advent.py @@ -4,8 +4,8 @@ # vim: et sw=2 ts=2 sts=2 # for Python3, use: -# import urllib.request as urllib2 -import urllib2 +import urllib.request as urllib2 +#import urllib2 import random import string @@ -619,7 +619,7 @@ def run_step(self, cmd = None): # get input from the user try: self.output("") # add a blank line - user_input = raw_input("> ") + user_input = input("> ") except EOFError: return False @@ -661,9 +661,9 @@ def run_step(self, cmd = None): if words[0].lower() == 'tell' and len(words) > 2: (target_name, words) = get_noun(words[1:], actor.location.actors.values()) - things = actor.inventory.values() + \ - actor.location.contents.values() + \ - actor.location.exits.values() + \ + things = list(actor.inventory.values()) + \ + list(actor.location.contents.values()) + \ + list(actor.location.exits.values()) + \ list(actor.location.actors.values()) + \ [actor.location] + \ [actor] diff --git a/bwx-game.py b/bwx-game.py index ddfc86b..537c5f6 100755 --- a/bwx-game.py +++ b/bwx-game.py @@ -118,7 +118,7 @@ def reception_description(self): # function. "def" defines a function in Python. def scream(self, actor, noun, words): all_words = [noun] + words - print "You hear a scream '%s'." % ' '.join(all_words) + print("You hear a scream '%s'." % ' '.join(all_words)) return True sidewalk.add_verb(Verb(scream, 'scream')) @@ -159,10 +159,10 @@ def scream(self, actor, noun, words): # add a custom actor verb (in this case for the hero) def throw(self, actor, noun, words): if noun and actor.get_verb('drop').act(actor, noun, words): - print 'The %s bounces and falls to the floor' % noun + print('The %s bounces and falls to the floor' % noun) return True else: - print 'You hurt your arm.' + print('You hurt your arm.') return False hero.add_verb(Verb(throw, 'throw')) @@ -207,7 +207,7 @@ def useless_hit_with_object(self, actor, noun, words): # SESSION: available to the specific palyer in the specific adventure in the specific session def scribble(self, actor, noun, words): if not noun or words: - print "You can only scrible a single word." + print("You can only scrible a single word.") return False share.put(share.ADVENTURE, 'crumb.' + actor.location.name, noun.strip()) return True @@ -218,9 +218,9 @@ def scribble(self, actor, noun, words): def peek(self, actor, noun, words): v = share.get(share.ADVENTURE, 'crumb.' + actor.location.name) if not v: - print 'Nothing here.' + print('Nothing here.') return False - print 'Someone scribbled "%s" here.' % v + print('Someone scribbled "%s" here.' % v) return True hero.add_verb(Verb(peek, 'peek')) @@ -229,19 +229,19 @@ def peek(self, actor, noun, words): def more(self, actor, noun, words): loc_name = "_".join(actor.location.name.split(' ')) share.increment(share.ADVENTURE, 'count.' + loc_name) - print 'The count is %s!' % share.get(share.ADVENTURE, 'count.' + loc_name) + print('The count is %s!' % share.get(share.ADVENTURE, 'count.' + loc_name)) return True def fewer(self, actor, noun, words): loc_name = "_".join(actor.location.name.split(' ')) share.decrement(share.ADVENTURE, 'count.' + loc_name) - print 'The count is %s!' % share.get(share.ADVENTURE, 'count.' + loc_name) + print('The count is %s!' % share.get(share.ADVENTURE, 'count.' + loc_name)) return True def reset(self, actor, noun, words): loc_name = "_".join(actor.location.name.split(' ')) share.delete(share.ADVENTURE, 'count.' + loc_name) - print 'The count is reset!' + print('The count is reset!') return True hero.add_verb(Verb(more, 'more')) @@ -256,7 +256,7 @@ def flip(self, actor, noun, words): self.unset_flag('switch_on') else: self.set_flag('switch_on') - print "You flip the switch." + print("You flip the switch.") return True vestibule.add_verb(Verb(flip, 'flip')) @@ -307,7 +307,7 @@ def push(self, actor, noun, words): return False w = "_".join(words) share.push(share.ADVENTURE, 'reception_messages', w) - print "You left a message on the stack of messages." + print("You left a message on the stack of messages.") return True reception.add_verb(Verb(push, 'push')) @@ -317,10 +317,10 @@ def pop(self, actor, noun, words): return False w = share.pop(share.ADVENTURE, 'reception_messages') if not w: - print "There are no messages on the stack." + print("There are no messages on the stack.") return False words = w.split('_') - print "You pull the top message from the stack and read '%s'." % " ".join(words) + print("You pull the top message from the stack and read '%s'." % " ".join(words)) return True reception.add_verb(Verb(pop, 'pop')) @@ -346,9 +346,9 @@ def top(self, actor, noun, words): return False w = share.ztop(share.ADVENTURE, 'highscore', 10) if w: - print "Top Players" + print("Top Players") for x in w: - print " %s" % x + print(" %s" % x) return True def scores(self, actor, noun, words): @@ -356,9 +356,9 @@ def scores(self, actor, noun, words): return False w = share.ztop_with_scores(share.ADVENTURE, 'highscore', 10) if w: - print "High Scores" + print("High Scores") for x in w: - print " %s %s" % (x[0], x[1]) + print(" %s %s" % (x[0], x[1])) return True reception.add_verb(Verb(top, 'top')) diff --git a/graphviz_writer.py b/graphviz_writer.py index 23e6eb5..2a120b3 100644 --- a/graphviz_writer.py +++ b/graphviz_writer.py @@ -52,7 +52,7 @@ def write_as_dot (game, file_name=None): # define helper for printing to the .dot file f = open(file_name, 'w') if file_name else None def _dot (line): - print >> f, line + print(line, file=f) # get node names by munging loc names (bail if any are dups) node_name_set = set() node_name_by_loc = {} @@ -95,7 +95,7 @@ def _dot (line): f.close() def dump_node_name_by_loc (node_name_by_loc): - print "node_name_by_loc = {" + print("node_name_by_loc = {") for (loc, node_name) in node_name_by_loc.items(): - print ' Location("%s") : "%s"' % (loc.name, node_name) - print "}" + print(' Location("%s") : "%s"' % (loc.name, node_name)) + print("}")