mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2024-11-23 12:23:04 +00:00
Refactored comments
Followed best practices by putting comments beneath the method declaration and not above.
This commit is contained in:
parent
13063b112b
commit
d4161f77d2
5 changed files with 39 additions and 39 deletions
|
@ -6,9 +6,9 @@ import os
|
|||
import sys
|
||||
|
||||
|
||||
# Convert a Pokemon name to a number.
|
||||
# Example: Pikachu returns 25. If the Pokemon does not exist, return -1.
|
||||
def to_number(pokemon_name):
|
||||
# Convert a Pokemon name to a number.
|
||||
# Example: Pikachu returns 25. If the Pokemon does not exist, return -1.
|
||||
pokemon_names = extractor.load_names()
|
||||
pokemon_name = pokemon_name.lower()
|
||||
for pokemon in pokemon_names:
|
||||
|
@ -18,9 +18,9 @@ def to_number(pokemon_name):
|
|||
return -1
|
||||
|
||||
|
||||
# Find Pokemon who's name starts with a word.
|
||||
# Example: If user_input is "pika", the result will be Pikachu.
|
||||
def guess_by_startswith(user_input):
|
||||
# Find Pokemon who's name starts with a word.
|
||||
# Example: If user_input is "pika", the result will be Pikachu.
|
||||
guessed_pokemon = extractor.pokemon_starting_with(user_input)
|
||||
if len(guessed_pokemon) == 0:
|
||||
return False
|
||||
|
@ -33,9 +33,9 @@ def guess_by_startswith(user_input):
|
|||
return True
|
||||
|
||||
|
||||
# Find Pokemon who's name contains a word.
|
||||
# Example: If user input is "chu", the result will be Pikachu, Raichu, Pichu and Smoochum.
|
||||
def guess_by_contains(user_input):
|
||||
# Find Pokemon who's name contains a word.
|
||||
# Example: If user input is "chu", the result will be Pikachu, Raichu, Pichu and Smoochum.
|
||||
guessed_pokemon = extractor.pokemon_containing(user_input)
|
||||
if len(guessed_pokemon) == 0:
|
||||
return False
|
||||
|
@ -48,17 +48,17 @@ def guess_by_contains(user_input):
|
|||
return True
|
||||
|
||||
|
||||
# If no Pokemon is found by the user input then try to guess what Pokemon they meant.
|
||||
# Return false if it failed to make any guesses.
|
||||
def guess(user_input):
|
||||
# If no Pokemon is found by the user input then try to guess what Pokemon they meant.
|
||||
# Return false if it failed to make any guesses.
|
||||
if len(user_input) < 3:
|
||||
return guess_by_startswith(user_input)
|
||||
else:
|
||||
return guess_by_contains(user_input)
|
||||
|
||||
|
||||
# Logic for dealing with user input when a Pokemon index is specified.
|
||||
def digit_handler(user_input):
|
||||
# Logic for dealing with user input when a Pokemon index is specified.
|
||||
user_input = int(user_input)
|
||||
if user_input < 1 or user_input > 493:
|
||||
print("Only pokemon 1 through 493 are supported.")
|
||||
|
@ -66,8 +66,8 @@ def digit_handler(user_input):
|
|||
filegen.create_applescript(user_input)
|
||||
|
||||
|
||||
# Logic for dealing with user input when a Pokemon name is specified.
|
||||
def other_handler(user_input):
|
||||
# Logic for dealing with user input when a Pokemon name is specified.
|
||||
number = to_number(user_input)
|
||||
if number == -1:
|
||||
guessed = guess(user_input)
|
||||
|
@ -81,9 +81,9 @@ def other_handler(user_input):
|
|||
filegen.create_applescript(number)
|
||||
|
||||
|
||||
# Changes the background image in the terminal.
|
||||
# The parameter is the Pokemon name or number.
|
||||
def change_background(user_input):
|
||||
# Changes the background image in the terminal.
|
||||
# The parameter is the Pokemon name or number.
|
||||
if str(user_input).isdigit():
|
||||
digit_handler(user_input)
|
||||
else:
|
||||
|
|
12
extractor.py
12
extractor.py
|
@ -4,16 +4,16 @@ import printer
|
|||
import os
|
||||
|
||||
|
||||
# Load all the Pokemon and their corresponding numbers into a list.
|
||||
def load_names():
|
||||
# Load all the Pokemon and their corresponding numbers into a list.
|
||||
names_file = open(os.get_exec_path()[0] + "/Data/pokemon.txt", "r+")
|
||||
content = names_file.readlines()
|
||||
return content
|
||||
|
||||
|
||||
# Extract the name from a Pokemon extracted from the data file.
|
||||
# Example: it will be read in from the data file as "150 Mewtwo\n" but it should just be "Mewtwo".
|
||||
def trim_name(pokemon):
|
||||
# Extract the name from a Pokemon extracted from the data file.
|
||||
# Example: it will be read in from the data file as "150 Mewtwo\n" but it should just be "Mewtwo".
|
||||
front_trim = 0
|
||||
for char in pokemon:
|
||||
if not char.isalpha():
|
||||
|
@ -23,8 +23,8 @@ def trim_name(pokemon):
|
|||
return pokemon[front_trim:-1]
|
||||
|
||||
|
||||
# Find all the Pokemon who's names begin with a key word.
|
||||
def pokemon_starting_with(word):
|
||||
# Find all the Pokemon who's names begin with a key word.
|
||||
all_pokemon = load_names()
|
||||
result = []
|
||||
for pokemon in all_pokemon:
|
||||
|
@ -34,8 +34,8 @@ def pokemon_starting_with(word):
|
|||
return result
|
||||
|
||||
|
||||
# Find all the Pokemon who's names begin with a key word.
|
||||
def pokemon_containing(word):
|
||||
# Find all the Pokemon who's names begin with a key word.
|
||||
all_pokemon = load_names()
|
||||
result = []
|
||||
for pokemon in all_pokemon:
|
||||
|
@ -45,8 +45,8 @@ def pokemon_containing(word):
|
|||
return result
|
||||
|
||||
|
||||
# Get the current Pokemon that is in the background.
|
||||
def current_pokemon():
|
||||
# Get the current Pokemon that is in the background.
|
||||
content = open(os.get_exec_path()[0] + "/Scripts/background.scpt", "r+").readlines()
|
||||
pokemon_id = content[2][len(content[2])-9:-6]
|
||||
all_pokemon = load_names()
|
||||
|
|
12
filegen.py
12
filegen.py
|
@ -4,8 +4,8 @@
|
|||
import os
|
||||
|
||||
|
||||
# Determine which region a Pokemon is from.
|
||||
def get_region(pokemon_number):
|
||||
# Determine which region a Pokemon is from.
|
||||
if pokemon_number < 1:
|
||||
raise Exception("Pokemon number must be at least 1.")
|
||||
if pokemon_number < 152:
|
||||
|
@ -20,9 +20,9 @@ def get_region(pokemon_number):
|
|||
raise Exception("Pokemon number must be less than 494.")
|
||||
|
||||
|
||||
# Append zeros at the front if necessary and add the extension.
|
||||
# Example: 5 would become 005.png.
|
||||
def get_filename(pokemon_number):
|
||||
# Append zeros at the front if necessary and add the extension.
|
||||
# Example: 5 would become 005.png.
|
||||
if pokemon_number < 1:
|
||||
raise Exception("Pokemon number must be at least 1.")
|
||||
elif pokemon_number < 10:
|
||||
|
@ -35,8 +35,8 @@ def get_filename(pokemon_number):
|
|||
raise Exception("Pokemon number must be less than 494.")
|
||||
|
||||
|
||||
# Create the script that will change the terminal background image.
|
||||
def create_applescript_content(region, filename):
|
||||
# Create the script that will change the terminal background image.
|
||||
content = "tell application \"iTerm\"\n"
|
||||
content += "\ttell current session of current window\n"
|
||||
content += "\t\tset background image to \"" + os.get_exec_path()[0] + "/Images/" + region + "/" + filename + "\"\n"
|
||||
|
@ -45,8 +45,8 @@ def create_applescript_content(region, filename):
|
|||
return content
|
||||
|
||||
|
||||
# Create and save the script for changing the terminal background image.
|
||||
def create_applescript(pokemon_number):
|
||||
# Create and save the script for changing the terminal background image.
|
||||
region = get_region(pokemon_number)
|
||||
filename = get_filename(pokemon_number)
|
||||
content = create_applescript_content(region, filename)
|
||||
|
@ -55,8 +55,8 @@ def create_applescript(pokemon_number):
|
|||
file.close()
|
||||
|
||||
|
||||
# Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist.
|
||||
def create_bash_run():
|
||||
# Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist.
|
||||
content = "#!/bin/bash\n" + "osascript " + os.get_exec_path()[0] + "/Scripts/background.scpt"
|
||||
if open(os.get_exec_path()[0] + "/Scripts/run.sh", 'r').read() == content:
|
||||
return
|
||||
|
|
4
main.py
4
main.py
|
@ -11,8 +11,8 @@ import sys
|
|||
import time
|
||||
|
||||
|
||||
# Test each Pokemon in order, one by one.
|
||||
def debug():
|
||||
# Test each Pokemon in order, one by one.
|
||||
for x in range(1, 494):
|
||||
backchanger.change_background(x)
|
||||
try:
|
||||
|
@ -22,8 +22,8 @@ def debug():
|
|||
sys.exit()
|
||||
|
||||
|
||||
# Entrance to the program.
|
||||
if __name__ == "__main__":
|
||||
# Entrance to the program.
|
||||
if len(argv) == 1:
|
||||
print("No command line arguments specified. Try typing in a Pokemon name or number.")
|
||||
elif len(argv) == 2:
|
||||
|
|
26
printer.py
26
printer.py
|
@ -3,8 +3,8 @@
|
|||
import extractor
|
||||
|
||||
|
||||
# Print the instructions of usage.
|
||||
def print_usage():
|
||||
# Print the instructions of usage.
|
||||
print(
|
||||
'''
|
||||
Usage:
|
||||
|
@ -25,15 +25,15 @@ Other Parameters:
|
|||
''')
|
||||
|
||||
|
||||
# Print all the items in a list. Used for printing each Pokemon from a particular region.
|
||||
def print_list(list_of_items):
|
||||
# Print all the items in a list. Used for printing each Pokemon from a particular region.
|
||||
for item in list_of_items:
|
||||
print(item)
|
||||
|
||||
|
||||
# Add zeros to the front so that it begins with 3 digits.
|
||||
# Example: "2 Ivysaur" -> "002 Ivysaur"
|
||||
def add_zeroes(pokemon):
|
||||
# Add zeros to the front so that it begins with 3 digits.
|
||||
# Example: "2 Ivysaur" -> "002 Ivysaur"
|
||||
zeroes = ""
|
||||
if int(pokemon.split(' ')[0]) < 10:
|
||||
zeroes = "00"
|
||||
|
@ -42,8 +42,8 @@ def add_zeroes(pokemon):
|
|||
return zeroes + pokemon
|
||||
|
||||
|
||||
# Print a list as multiple columns instead of just one.
|
||||
def print_columns(items, i, j):
|
||||
# Print a list as multiple columns instead of just one.
|
||||
rows = []
|
||||
items_per_column = int((j - i) / 3) + 1
|
||||
|
||||
|
@ -62,44 +62,44 @@ def print_columns(items, i, j):
|
|||
print_list(rows)
|
||||
|
||||
|
||||
# Helper method for printing all the Pokemon in a particular region.
|
||||
def print_region(i, j):
|
||||
# Helper method for printing all the Pokemon in a particular region.
|
||||
print_columns(extractor.load_names(), i, j)
|
||||
|
||||
|
||||
# Print each Kanto region Pokemon and its corresponding number.
|
||||
def print_kanto():
|
||||
# Print each Kanto region Pokemon and its corresponding number.
|
||||
print_region(0, 151)
|
||||
|
||||
|
||||
# Print each Johto region Pokemon and its corresponding number.
|
||||
def print_johto():
|
||||
# Print each Johto region Pokemon and its corresponding number.
|
||||
print_region(151, 251)
|
||||
|
||||
|
||||
# Print each Hoenn region Pokemon and its corresponding number.
|
||||
def print_hoenn():
|
||||
# Print each Hoenn region Pokemon and its corresponding number.
|
||||
print_region(251, 386)
|
||||
|
||||
|
||||
# Print each Sinnoh region Pokemon and its corresponding number.
|
||||
def print_sinnoh():
|
||||
# Print each Sinnoh region Pokemon and its corresponding number.
|
||||
print_region(386, 493)
|
||||
|
||||
|
||||
# Print all the Pokemon for all the regions supported.
|
||||
def print_all():
|
||||
# Print all the Pokemon for all the regions supported.
|
||||
print_region(0, 493)
|
||||
|
||||
|
||||
# Print all the Pokemon who's names begin with a particular letter.
|
||||
def print_pokemon_starting_with(char):
|
||||
# Print all the Pokemon who's names begin with a particular letter.
|
||||
pokemon = extractor.pokemon_starting_with(char)
|
||||
print_columns(pokemon, 0, len(pokemon))
|
||||
|
||||
|
||||
# Print all the supported regions.
|
||||
def print_regions():
|
||||
# Print all the supported regions.
|
||||
print("Kanto")
|
||||
print("Johto")
|
||||
print("Hoenn")
|
||||
|
|
Loading…
Reference in a new issue