onix.backend.sql.sinks module

Sink implementations for SQL backend

onix.backend.sql.sinks.compute_tid(team, sanitizer=None)[source]

Computes the Team ID for the given group of movesets

Parameters:
  • team (iterable of Moveset or str) – the team for which to compute the TID, represented either by their movesets or by their SIDs
  • sanitizer (onix.utilities.Sanitizer, optional) – if no sanitizer is provided, movesets are assumed to be already sanitized. Otherwise, the provided Sanitizer is used to sanitize the movesets.
Returns:

the corresponding Team ID

Return type:

str

Examples

>>> from onix.model import Moveset, Forme, PokeStats
>>> from onix.backend.sql.sinks import compute_tid
>>> delphox = Moveset([Forme('delphox', 'magician',
...                    PokeStats(282, 158, 222, 257, 220, 265))],
...                   'f', 'lifeorb', ['calmmind', 'psychic'], 100, 255)
>>> ditto = Moveset([Forme('ditto', 'imposter',
...                  PokeStats(259, 164, 98, 134, 126, 123))],
...                  'u', 'focussash', ['transform'], 100, 255)
>>> print(compute_tid([delphox, ditto])) 
4e49b0eb...
onix.backend.sql.sinks.compute_fid(forme, sanitizer=None)[source]

Computes the Forme ID for a given forme

Parameters:
  • forme (Forme) – the forme to compute the FID for.
  • sanitizer (onix.utilities.Sanitizer, optional) – if no sanitizer is provided, the forme is assumed to be already sanitized. Otherwise, the provided Sanitizer is used to sanitize the forme.
Returns:

the corresponding Forme ID

Return type:

str

Examples

>>> from onix.model import Forme, PokeStats
>>> from onix.backend.sql.sinks import compute_fid
>>> forme = Forme('stunfisk', 'static',
...               PokeStats(369, 168, 177, 258, 225, 73))
>>> print(compute_fid(forme)) 
2220c1624d...
onix.backend.sql.sinks.convert_forme(forme)[source]

Converts a Forme object to a row of values in an insert expression into the formes table

Parameters:forme (Forme) – the forme to convert. Is assumed to be sanitized.
Returns:the corresponding row for an insert expression into the formes table
Return type:tuple

Examples

>>> from onix.model import Forme, PokeStats
>>> from onix.backend.sql.sinks import convert_forme
>>> forme = Forme('heatmor', 'gluttony',
...               PokeStats(333, 241, 170, 253, 150, 204))
>>> row = convert_forme(forme)
>>> print(row['ability'])
gluttony
>>> print(row['hp'])
333
onix.backend.sql.sinks.convert_moveset(sid, moveset)[source]

Converts a Moveset DTO to rows of values for insert expressions

Parameters:
  • sid (str) – the SID of the moveset
  • moveset (dto.Moveset) – the moveset to convert. Is assumed to be sanitized.
Returns:

the corresponding insert rows. The keys are the table names, the values are the rows to insert.

Return type:

dict of sa.Table to list of tuple

Examples

>>> from onix.model import Moveset, Forme, PokeStats
>>> from onix.backend.sql.sinks import convert_moveset
>>> from onix.backend.sql import schema
>>> moveset = Moveset([Forme('diglett', 'sandveil',
...                    PokeStats(17, 11, 9, 11, 10, 17))],
...                   'm', 'leftovers',
...                   ['earthquake', 'rockslide', 'shadowclaw',
...                    'substitute'], 5, 255)
>>> rows = convert_moveset('f4ce673a1', moveset)
>>> print(rows[schema.movesets][0]['item'])
leftovers
>>> print(sum(map(lambda x:len(x), rows.values())))
7
onix.backend.sql.sinks.convert_team(team_sids)[source]

Converts a list of SIDs specifying a player’s team into the corresponding rows of values for an insert expression into the teams table

Parameters:team_sids (list of str) – the SIDs corresponding to a player’s pokemon
Returns:The corresponding rows for an insert expression into the teams table
Return type:list of tuple

Examples

>>> from onix.backend.sql.sinks import convert_team
>>> rows = convert_team(['ghi', 'abc', 'def'])
>>> print(rows[0]['tid']) #doctest +ELLIPSIS
8711a93...
>>> print(rows[1]['sid'])
def
onix.backend.sql.sinks.convert_player(player, bid, side, tid)[source]

Converts a Players DTO to a row of values in an insert expression into the battle_players table

Parameters:
  • player (dto.Player) – the Player to convert
  • bid (int) – the battle ID
  • side (int) –

    the player’s “index” in the battle.

    Note

    this index is one-based rather than zero-based, so as to maintain consistency with log references (i.e. “player 1 vs. player 2”).

  • tid (str) – the TID for the player’s team
Returns:

the corresponding row for an insert expression into the battle_players table

Return type:

tuple

Examples

>>> from onix.model import Player
>>> from onix.backend.sql.sinks import convert_player
>>> player = Player(id='chaos', rating={'elo': 1311.1479745117863,
...                                     'rpr': None,
...                                      'r': 1227.7501280633721,
...                                      'l': 83, 'rprd': None,
...                                      'rd': 129.53915739500627,
...                                      'w': 40})
>>> row = convert_player(player, 5134, 1, 'aac491ca1')
>>> print(row['w'])
40
>>> print(row['t'])
None
>>> print(row['rpr'])
None
onix.backend.sql.sinks.convert_battle_info(battle_info)[source]

Converts a Moveset DTO to rows of values for insert expressions

Parameters:battle_info (dto.BattleInfo) – the BattleInfo to convert
Returns:the corresponding insert rows. The keys are the table names, the values are the rows to insert.
Return type:dict of sa.Table to list of tuple

Examples

>>> import datetime
>>> from onix.model import BattleInfo, Player
>>> from onix.backend.sql import schema
>>> from onix.backend.sql.sinks import convert_battle_info
>>> battle_info = BattleInfo(5776, 'randombattle',
...                          datetime.date(2016, 9, 21),
...                          [Player('echad', {'w': 1, 'l': 0}),
...                           Player('shtaymin', {'w': 0, 'l': 1})],
...                          [['abc', 'cab', 'bac'],
...                           ['123', '312', '213']], 16, 'forfeit')
>>> rows = convert_battle_info(battle_info)
>>> print(rows[schema.battle_players][0]['side'])
1
>>> print(rows[schema.battle_players][0]['tid']) #doctest +ELLIPSIS
267e429f...
>>> print(rows[schema.teams][0]['tid']) #doctest +ELLIPSIS
267e429f...
class onix.backend.sql.sinks.MovesetSink(connection, batch_size=1000)[source]

Bases: onix.collection.sinks.MovesetSink

SQL implementation of the MovesetSink interface

Parameters:
  • connection (sqlalchemy.engine.base.Connection) – connection to the SQL backend
  • batch_size (int, optional) – the number of movesets to go through before actually committing to the database, defaults to 1000
class onix.backend.sql.sinks.BattleInfoSink(connection, batch_size=100)[source]

Bases: onix.collection.sinks.BattleInfoSink

SQL implementation of the MovesetSink interface

Parameters:
  • connection (sqlalchemy.engine.base.Connection) – connection to the SQL backend
  • batch_size (int, optional) –
    the number of battles to go through before actually committing
    to the database, defaults to 100