def do_test(s: str, expected: List[str]) -> None: symbols = mana.parse(s) works = symbols == expected if not works: print('\nInput: {s}\nExpected: {expected}\n Actual: {actual}'.format( s=s, expected=expected, actual=symbols)) assert works
def playable_where(term: str) -> str: term = term.upper() try: colors = set(mana.parse(term)) except mana.InvalidManaCostException as e: raise InvalidTokenException(e) from e symbols_without_curlies = colors.copy() # Colorless symbols_without_curlies.add('C') all_colors = ['W', 'U', 'B', 'R', 'G'] # Phyrexian symbols_without_curlies.update(['{c}/P'.format(c=c) for c in all_colors]) # Twobrid symbols_without_curlies.update(['2/{c}'.format(c=c) for c in all_colors]) for color in colors: # Hybrid symbols_without_curlies.update([ '{color}/{other}'.format(color=color, other=other) for other in all_colors if other != color ]) symbols_without_curlies.update([ '{other}/{color}'.format(color=color, other=other) for other in all_colors if other != color ]) where = 'mana_cost' for symbol in symbols_without_curlies: where = "REPLACE({where}, '{{{symbol}}}', '')".format(where=where, symbol=symbol) return "{where} = ''".format(where=where)
def mana_where(operator: str, term: str) -> str: term = term.upper() try: symbols = mana.parse( term ) # Uppercasing input means you can't search for 1/2 or 1/2 white mana but w should match W. symbols = ['{{{symbol}}}'.format(symbol=symbol) for symbol in symbols] except mana.InvalidManaCostException: symbols = [term] if operator == ':': d = collections.Counter( symbols ) # Group identical symbols so that UU checks for {U}{U} not just {U} twice. clause = ' AND '.join( 'mana_cost LIKE {symbol}'.format(symbol=sqllikeescape(symbol * n)) for symbol, n in d.items()) elif operator == '=': joined = ''.join('{symbol}'.format(symbol=symbol) for symbol in symbols) clause = "mana_cost = '{joined}'".format(joined=joined) else: raise InvalidTokenException( 'mana expects `:` or `=` not `{operator}`. Did you want cmc?'. format(operator=operator)) return '({clause})'.format(clause=clause)
def set_colors(d: Deck) -> None: deck_colors: Set[str] = set() deck_colored_symbols: List[str] = [] for c in [entry.card for entry in d.maindeck + d.sideboard]: for cost in c.get('mana_cost') or (): if c.layout == 'split': continue # They might only be using one half so ignore it. card_symbols = mana.parse(cost) card_colors = mana.colors(card_symbols) deck_colors.update(card_colors['required']) card_colored_symbols = mana.colored_symbols(card_symbols) deck_colored_symbols += card_colored_symbols['required'] d.colors = mana.order(deck_colors) d.colored_symbols = deck_colored_symbols
def set_colors(d): deck_colors = set() deck_colored_symbols = [] for card in [c['card'] for c in d.maindeck + d.sideboard]: for cost in card.get('mana_cost') or (): if card.layout == 'split' or card.layout == 'aftermath': continue # They might only be using one half so ignore it. card_symbols = mana.parse(cost) card_colors = mana.colors(card_symbols) deck_colors.update(card_colors['required']) card_colored_symbols = mana.colored_symbols(card_symbols) deck_colored_symbols += card_colored_symbols['required'] d.colors = mana.order(deck_colors) d.colored_symbols = deck_colored_symbols
def test_everything() -> None: rs = database.db().select('SELECT name, mana_cost FROM face') for row in rs: if row['mana_cost']: mana.parse(row['mana_cost'])
def test_everything(): rs = database.db().execute('SELECT name, mana_cost FROM face') for row in rs: if row['mana_cost']: mana.parse(row['mana_cost'])
def do_test(s, expected): symbols = mana.parse(s) assert symbols == expected or print( '\nInput: {s}\nExpected: {expected}\n Actual: {actual}'.format( s=s, expected=expected, actual=symbols))