From ce1e98d805b0cff425a0fea0e8a7f6853e107a4a Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Fri, 7 Dec 2012 00:04:16 -0800 Subject: [PATCH] Show move methods in form-differences.py --- scripts/form-differences.py | 50 ++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/scripts/form-differences.py b/scripts/form-differences.py index f89e196..88a7628 100644 --- a/scripts/form-differences.py +++ b/scripts/form-differences.py @@ -5,6 +5,8 @@ This is not a one-shot script! It is probably unmaintained though. """ +import itertools + from pokedex.db import connect import pokedex.db.tables as t from sqlalchemy.orm import lazyload, joinedload, subqueryload @@ -22,25 +24,32 @@ def getpokeathlon(stats): return dict((stat.pokeathlon_stat_id, (stat.minimum_stat, stat.base_stat, stat.maximum_stat)) for stat in stats) def getitems(items): - return list(sorted((item.version_id, item.item, item.rarity) for item in items)) - -def getmoves(moves): - return list(sorted((move.version_group_id, move.move_id, move.pokemon_move_method_id, move.level, move.order) for move in moves)) + return sorted((item.version_id, item.item_id, item.rarity) for item in items) def getform(form): return { 'ability': form.pokemon.all_abilities, 'items': getitems(form.pokemon.items), - 'moves': getmoves(form.pokemon.pokemon_moves), 'pokeathlon': getpokeathlon(form.pokeathlon_stats), 'stats': getstats(form.pokemon.stats), 'effort': geteffort(form.pokemon.stats), - 'types': form.pokemon.types, + 'type': form.pokemon.types, 'exp': form.pokemon.base_experience, 'weight': form.pokemon.weight, 'height': form.pokemon.height, } +def groupdict(it, key): + """Group an iterable by a key and return a dict.""" + + return dict((group_key, list(group)) for group_key, group in itertools.groupby(it, key)) + +def getmoves(form): + all_moves = sorted((move.method.identifier, move.version_group_id, move.move_id, move.level, move.order) for move in form.pokemon.pokemon_moves) + + return groupdict(all_moves, lambda x: x[0]) + + def gcd(a, b): keys = set(a.keys()) & set(b.keys()) result = {} @@ -49,6 +58,14 @@ def gcd(a, b): result[k] = a[k] return result +def find_uncommon_keys(dicts): + keys = set(dicts[0]) + + common_keys = set(reduce(gcd, dicts).keys()) + unique_keys = keys - common_keys + + return unique_keys + q = session.query(t.PokemonSpecies) q = q.options( lazyload('default_pokemon'), @@ -67,21 +84,30 @@ for species in q.all(): .join(t.Pokemon) .filter(t.Pokemon.species_id==species.id) .options( + subqueryload('pokeathlon_stats'), + joinedload('pokemon'), joinedload('pokemon.all_abilities'), joinedload('pokemon.items'), - joinedload('pokemon.stats'), joinedload('pokemon.types'), - joinedload('pokeathlon_stats'), + joinedload('pokemon.pokemon_moves.method'), + subqueryload('pokemon.stats'), subqueryload('pokemon.pokemon_moves'), + + lazyload('pokemon.default_form'), + lazyload('pokemon.forms'), + lazyload('pokemon.items.item'), lazyload('pokemon.pokemon_moves.pokemon'), lazyload('pokemon.pokemon_moves.move'), + lazyload('pokemon.species'), ) .all() ) - keys = set(getform(forms[0]).keys()) - common_keys = set(reduce(gcd, map(getform, forms)).keys()) - unique_keys = keys - common_keys + uncommon = sorted(find_uncommon_keys(map(getform, forms))) + uncommon_moves = sorted(find_uncommon_keys(map(getmoves, forms))) - print "{}: {}".format(species.name, ", ".join(sorted(unique_keys))) + if uncommon_moves: + uncommon.append("moves ({})".format(", ".join(sorted(uncommon_moves)))) + + print "{}: {}".format(species.name, ", ".join(uncommon))