veekun_pokedex/_sources/usage.txt

194 lines
5.2 KiB
Text
Raw Normal View History

2011-11-18 23:36:39 +00:00
Using pokedex
=============
The pokédex is, first and foremost, a Python library. To get the most of it,
you'll need to learn `Python`_ and `SQLAlchemy`_.
Here is a small example of using pokedex:
.. testcode::
from pokedex.db import connect, tables, util
session = connect()
pokemon = util.get(session, tables.PokemonSpecies, 'bulbasaur')
print u'{0.name}, the {0.genus} Pokemon'.format(pokemon)
Running this will give you some Bulbasaur info:
.. testoutput::
Bulbasaur, the Seed Pokemon
Connecting
----------
To get information out of the Pokédex, you will need to create a
:class:`Session <pokedex.db.multilang.MultilangSession>`. To do that, use
:func:`pokedex.db.connect`. For simple uses, you don't need to give it any
arguments: it the database that ``pokedex load`` fills up by default. If you
need to select another database, give its URI as the first argument.
The object :func:`~pokedex.db.connect` gives you is actually a
:class:`SQLAlchemy session <sqlalchemy.orm.session.Session>`, giving you the
full power of SQLAlchemy for working with the data. We'll cover some basics
here, but if you intend to do some serious work, do read SQLAlchemy's docs.
2012-02-12 23:14:24 +00:00
Pokédex tables
--------------
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Data in the pokédex is organized in tables, defined in
:mod:`pokedex.db.tables`.
There is quite a few or them. To get you started, here are a few common ones:
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
* :class:`~pokedex.db.tables.PokemonSpecies`
* :class:`~pokedex.db.tables.Move`
* :class:`~pokedex.db.tables.Item`
* :class:`~pokedex.db.tables.Type`
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Getting things
--------------
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
If you know what you want from the pokédex, you can use the
:func:`pokedex.db.util.get` function. It looks up a thing in a table, based on
its identifier, name, or ID, and returns it.
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testcode::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
def print_pokemon(pokemon):
print u'{0.name}, the {0.genus} Pokemon'.format(pokemon)
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
print_pokemon(util.get(session, tables.PokemonSpecies, identifier='eevee'))
print_pokemon(util.get(session, tables.PokemonSpecies, name=u'Ho-Oh'))
print_pokemon(util.get(session, tables.PokemonSpecies, id=50))
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
def print_item(item):
print u'{0.name}: ${0.cost}'.format(item)
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
print_item(util.get(session, tables.Item, identifier='great-ball'))
print_item(util.get(session, tables.Item, name='Potion'))
print_item(util.get(session, tables.Item, id=30))
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testoutput::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Eevee, the Evolution Pokemon
Ho-Oh, the Rainbow Pokemon
Diglett, the Mole Pokemon
Great Ball: $600
Potion: $300
Fresh Water: $200
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Querying
--------
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
So, how do you get data from the session? You use the session's
:meth:`~sqlalchemy.orm.session.Session.query` method, and give it a pokédex
Table as an argument. This will give you a :class:`SQLAlchemy query
<sqlalchemy.orm.query.Query>`.
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Ordering
^^^^^^^^
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
As always with SQL, you should not rely on query results being in some
particular order unless you have ordered the query first. This means that
you'll want to sort just about every query you will make.
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
For example, you can get a list of all pokémon species, sorted by their
:attr:`~pokedex.db.tables.PokemonSpecies.id`, like so:
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testcode::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
for pokemon in session.query(tables.PokemonSpecies).order_by(tables.PokemonSpecies.id):
print pokemon.name
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testoutput::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Bulbasaur
Ivysaur
Venusaur
Charmander
Charmeleon
...
Keldeo
Meloetta
Genesect
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Or to order by :attr:`~pokedex.db.tables.PokemonSpecies.name`:
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testcode::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
for pokemon in session.query(tables.PokemonSpecies).order_by(tables.PokemonSpecies.name):
print pokemon.name
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testoutput::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Abomasnow
...
Zweilous
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Filtering
^^^^^^^^^
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Another major operation on queries is filtering, using the query's
:meth:`~sqlalchemy.orm.query.Query.filter` or
:meth:`~sqlalchemy.orm.query.Query.filter_by` methods:
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testcode::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
for move in session.query(tables.Move).filter(tables.Move.power > 200):
print move.name
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testoutput::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Explosion
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
Joining
^^^^^^^
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
The final operation we'll cover here is joining other tables to the query,
using the query's :meth:`~sqlalchemy.orm.query.Query.join`.
You will usually want to join on a relationship, such as in the following
example:
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testcode::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
query = session.query(tables.Move)
query = query.join(tables.Move.type)
query = query.filter(tables.Type.identifier == 'grass')
query = query.filter(tables.Move.power >= 100)
query = query.order_by(tables.Move.power)
query = query.order_by(tables.Move.name)
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
print 'The most powerful Grass-type moves:'
for move in query:
print u'{0.name} ({0.power})'.format(move)
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. testoutput::
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
The most powerful Grass-type moves:
Petal Dance (120)
Power Whip (120)
Seed Flare (120)
SolarBeam (120)
Wood Hammer (120)
Leaf Storm (140)
Frenzy Plant (150)
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
That concludes our brief tutorial.
If you need to do more, consult the `SQLAlchemy documentation`_.
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
API documentation
-----------------
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. autofunction:: pokedex.db.connect
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
See :class:`sqlalchemy.orm.session.Session` for more documentation on the
returned object.
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. autofunction:: pokedex.db.util.get
2011-11-18 23:36:39 +00:00
2012-02-12 23:14:24 +00:00
.. _Python: http://www.python.org
.. _SQLAlchemy: http://www.sqlalchemy.org
.. _`SQLAlchemy documentation`: http://www.sqlalchemy.org/docs/orm/tutorial.html