Examples on how to use the API

The API allows to create objects to be used such as pokemon and moves. At the beginning of your scripts add the import to Move and Pokemon classes


In [1]:
from pokelife.move import Move
from pokelife.pokemon import Pokemon
from pokelife.nature import Nature

The two classess connect to the REST API at pokeapi.co to collect data. To create a new move you can either use its id or its name


In [2]:
move_1 = Move(1)
move_2 = Move('razor-wind')

The print functions are overridden to show all the collected attributes from the database


In [3]:
print(move_1)


accuracy: 100
stat_changes: []
flinch_chance: 0
min_turns: None
ailment_chance: 0
damage_class: physical
healing: 0
name: pound
max_hits: None
stat_chance: 0
priority: 0
id: 1
ailment: none
max_turns: None
type: normal
effect_chance: None
crit_rate: 0
pp: 35
power: 40
drain: 0
min_hits: None

In [4]:
print(move_2)


accuracy: 100
stat_changes: []
flinch_chance: 0
min_turns: None
ailment_chance: 0
damage_class: special
healing: 0
name: razor-wind
max_hits: None
stat_chance: 0
priority: 0
id: 13
ailment: none
max_turns: None
type: normal
effect_chance: None
crit_rate: 1
pp: 10
power: 80
drain: 0
min_hits: None

All the attributes can be accessed individually


In [5]:
move_1.type


Out[5]:
'normal'

You can instatiate pokemons in the exact same way


In [6]:
pokemon = Pokemon(12)

In [7]:
print(pokemon)


_Pokemon__ability: compound-eyes
available_abilities: ['tinted-lens', 'compound-eyes']
_Pokemon__moves: [<pokelife.move.Move object at 0x7fbdd41ccfd0>, <pokelife.move.Move object at 0x7fbdd7c9d668>, <pokelife.move.Move object at 0x7fbdd41ea978>, <pokelife.move.Move object at 0x7fbdd4190358>]
speed: {'base_stat': 70, 'effort': 0}
attack: {'base_stat': 45, 'effort': 0}
special-defense: {'base_stat': 80, 'effort': 1}
_Pokemon__nature: sassy
name: butterfree
types: ['flying', 'bug']
special-attack: {'base_stat': 90, 'effort': 2}
defense: {'base_stat': 50, 'effort': 0}
available_moves: ['razor-wind', 'gust', 'whirlwind', 'take-down', 'double-edge', 'supersonic', 'psybeam', 'hyper-beam', 'mega-drain', 'solar-beam', 'poison-powder', 'stun-spore', 'sleep-powder', 'string-shot', 'toxic', 'confusion', 'psychic', 'rage', 'teleport', 'mimic', 'double-team', 'reflect', 'bide', 'swift', 'dream-eater', 'flash', 'psywave', 'rest', 'substitute', 'thief', 'nightmare', 'snore', 'curse', 'protect', 'giga-drain', 'endure', 'swagger', 'attract', 'sleep-talk', 'return', 'frustration', 'safeguard', 'sweet-scent', 'hidden-power', 'twister', 'rain-dance', 'sunny-day', 'psych-up', 'shadow-ball', 'facade', 'skill-swap', 'secret-power', 'air-cutter', 'silver-wind', 'signal-beam', 'aerial-ace', 'roost', 'natural-gift', 'tailwind', 'u-turn', 'bug-buzz', 'energy-ball', 'giga-impact', 'defog', 'captivate', 'bug-bite', 'ominous-wind', 'venoshock', 'rage-powder', 'quiver-dance', 'round', 'acrobatics', 'struggle-bug', 'electroweb', 'confide', 'infestation']
id: 12
hp: {'base_stat': 60, 'effort': 0}

At creation, nature, abilities and moves are choosen randomly. You can change those element by simply setting them to another value


In [8]:
print('nature: %s' % pokemon.nature)
print('ability: %s' % pokemon.ability)
print('\n'.join("move %d: %s" % (i, move.name) for i, move in enumerate(pokemon.moves)))


nature: sassy
ability: compound-eyes
move 0: struggle-bug
move 1: captivate
move 2: take-down
move 3: signal-beam

In [9]:
pokemon.ability = 'compound-eyes'
print(pokemon.ability)


compound-eyes

Accepted abilities are only those present in the list of available ones


In [12]:
pokemon.ability = 'stench'


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-868678c10b76> in <module>()
----> 1 pokemon.ability = 'stench'

/home/buschbapti/Documents/PokeLife/src/pokelife/pokemon.py in ability(self, ability)
     32     @ability.setter
     33     def ability(self, ability):
---> 34         assert ability in self.available_abilities, "%r is not in the list of available abilities" % ability
     35         self.__ability = ability
     36 

AssertionError: 'stench' is not in the list of available abilities

You can also set an ability using set_ability_by_index(index) function. This function set as active ability the ability from the available list at selected index


In [13]:
pokemon.set_ability_by_index(0)

To change the list of active moves, you need to provide a list of four moves id. Move id can be either move names or corresponding integer id. The class automatically change it to the corresponding Move object


In [14]:
move_pool = ['mimic', 13, 'supersonic', 'toxic']

In [15]:
pokemon.moves = move_pool

In [16]:
print('\n'.join("move %d: %s" % (i, move.name) for i, move in enumerate(pokemon.moves)))


move 0: mimic
move 1: razor-wind
move 2: supersonic
move 3: toxic

You can also set a move at a specific indec using the function set_move_at_index(move_number, id_move)


In [17]:
pokemon.set_move_at_index(3, 'reflect')

In [18]:
print('\n'.join("move %d: %s" % (i, move.name) for i, move in enumerate(pokemon.moves)))


move 0: mimic
move 1: razor-wind
move 2: supersonic
move 3: reflect

In [ ]: