onix.utilities module

Common utilities used across the package

onix.utilities.sanitize_string(input_string)[source]

Strips all non-alphanumeric characters and puts everything in lowercase

Parameters:input_string (str) – string to be sanitized
Returns:the sanitized string
Return type:str

Examples

>>> from onix.utilities import sanitize_string
>>> print(sanitize_string('Hello World'))
helloworld
class onix.utilities.Sanitizer(pokedex, aliases)[source]

Bases: object

An object which normalizes inputs to ensure consistency, removing or replacing invalid characters and de-aliasing

Parameters:
  • pokedex (dict) – the Pokedex to use, scraped from Pokemon Showdown
  • aliases (dict) – the alias lookup to use, scraped from Pokemon Showdown
sanitize(input_object)[source]

Sanitizes the given object, sorting it, removing or replacing invalid characters, and de-aliasing, as required :param input_object: the object to sanitize

Returns:the sanitized object, of the same type as the input
Return type:object
Raises:TypeError – if the type of the input_object is not supported

Examples

>>> import json
>>> from onix import scrapers
>>> from onix import utilities
>>> try:
...    pokedex = json.load(open('.psdata/pokedex.json'))
... except IOError:
...     pokedex = scrapers.scrape_battle_pokedex()
...
>>> try:
...     aliases = json.load(open('.psdata/aliases.json'))
... except IOError:
...     aliases = scrapers.scrape_battle_aliases()
...
>>> sanitizer = utilities.Sanitizer(pokedex, aliases)
>>> sanitizer.sanitize('Wormadam-Trash')
'wormadamtrash'
>>> print(', '.join(sanitizer.sanitize(['Volt Switch', 'Thunder',
...                                     'Giga Drain', 'Web'])))
gigadrain, stickyweb, thunder, voltswitch
onix.utilities.compute_sid(moveset, sanitizer=None)[source]

Computes the Set ID for the given moveset

Parameters:
  • moveset (Moveset) – the moveset to compute the SID for
  • sanitizer (Sanitizer, optional) – if no sanitizer is provided, moveset is assumed to be already sanitized. Otherwise, the provided Sanitizer is used to sanitize the moveset.
Returns:

the corresponding Set ID

Return type:

str

Examples

>>> from onix.model import PokeStats, Forme, Moveset
>>> from onix import utilities
>>> moveset = Moveset([Forme('mamoswine','thickfat',
...                          PokeStats(361,394,197,158,156,259))],
...                   'f', 'lifeorb',
...                   ['earthquake', 'iceshard', 'iciclecrash',
...                    'superpower'], 100, 255)
>>> print(utilities.compute_sid(moveset)) 
ad9a9fa20...
onix.utilities.stats_dict_to_model(stats_dict)[source]

Converts a Pokemon Showdown-style stats dict to a PokeStats object.

Parameters:stats_dict (dict) – the object to convert
Returns:the converted object
Return type:PokeStats
Raises:TypeError – if stats_dict doesn’t have the correct keys

Examples

>>> from onix import utilities
>>> utilities.stats_dict_to_model({'hp': 361, 'atk': 394, 'def': 197,
... 'spa': 158, 'spd' : 156, 'spe': 259})
PokeStats(hp=361, atk=394, dfn=197, spa=158, spd=156, spe=259)
onix.utilities.calculate_stats(base_stats, nature, ivs, evs, level)[source]

Calculate a Pokemon’s battle stats

Parameters:
  • base_stats (PokeStats) – the Pokemon’s base stats
  • nature (dict) – the nature, with plus and minus keys indicating the stats that are boosted and hindered (neutral natures will have neither key)
  • ivs (PokeStats) – the Pokemon’s individual values
  • evs (PokeStats) – the Pokemon’s effort values
  • level (int) – the Pokemon’s level
Returns:

the Pokemon’s battle stats

Return type:

PokeStats

Examples

>>> from onix.model import PokeStats
>>> from onix import utilities
>>> utilities.calculate_stats(PokeStats(108, 130, 95, 80, 85, 102),
... {'name': 'Adamant', 'plus': 'atk', 'minus': 'spa'},
... PokeStats(24, 12, 30, 16, 23, 5),
... PokeStats(74, 195, 86, 48, 84, 23), 78)
PokeStats(hp=289, atk=279, dfn=192, spa=135, spd=171, spe=171)
onix.utilities.load_natures()[source]

Loads the natures dictionary

Returns:the natures dictionary
Return type:dict

Examples

>>> from onix import utilities
>>> natures = utilities.load_natures()
>>> print(natures['mild']['minus'])
dfn
onix.utilities.load_accessible_formes()[source]

Loads the dictionary of accessible formes

Returns:the accessible formes dictionary
Return type:dict

Examples

>>> from onix import utilities
>>> accessible_formes = utilities.load_accessible_formes()
>>> print(accessible_formes['charizardmegax'][0][1][0])
charizardmegay
onix.utilities.load_species_lookup()[source]

Loads the dictionary of sanitized formes (and forme-concatenations) to their display names. This is what handles things like determining whether megas are tiered together or separately or what counts as an “appearance-only” forme.

Returns:the species lookup dictionary
Return type:dict

Examples

>>> from onix import utilities
>>> species_lookup = utilities.load_species_lookup()
>>> print(species_lookup['darmanitanzen'])
Zen-Darmanitan
onix.utilities.parse_ruleset(ruleset)[source]

Extract information from a ruleset dict (an entry from formats.json) that’s relevant to log reading / stat summing, etc.

Parameters:ruleset (dict) – the entry from formats.json corresponding to the format of interest
Returns:
  • (str) : what’s the game type? That is, ‘singles’ vs. ‘doubles’ vs. whatever
  • (bool) : is it a Hackmons metatame?
  • (bool) : are illegal species / ability combos allowed?
  • (bool) : is Rayquaza allowed to mega-evolve in the metagame?

    Note

    If Rayquaza is banned from the metagame, this is trivial (and will probably return True)

  • (int) : the default level of a Pokemon in the metagame
Return type:tuple

Examples

>>> import json
>>> from onix import scrapers
>>> from onix import utilities
>>> commit = '5c14138b54dddf8bc034433eaef950a1c6eaf734'
>>> try:
...     formats = json.load(
...                   open('.psdata/{}/formats.json'.format(commit)))
... except IOError:
...     formats = scrapers.scrape_battle_formats(commit=commit)
>>> print(utilities.parse_ruleset(formats['nu']))
('singles', False, False, True, 100)
>>> print(utilities.parse_ruleset(formats['almostanyability']))
('singles', False, True, True, 100)
>>> print(utilities.parse_ruleset(formats['doublesuu']))
('doubles', False, False, True, 100)
onix.utilities.determine_hidden_power_type(ivs)[source]

Determine a Pokemon’s Hidden Power type from its IVs. One should never need to do this (PS automatically detemines the type and puts it in the moveset), but it’s best to be sure

Parameters:ivs (PokeStats) – The Pokemon’s individual values
Returns:hidden power type
Return type:str

Examples

>>> from onix import utilities as utl
>>> from onix.model import PokeStats
>>> utl.determine_hidden_power_type(PokeStats(31, 31, 31, 31, 31, 31))
'dark'
>>> utl.determine_hidden_power_type(PokeStats(31, 0, 30, 31, 31, 31))
'ice'