def charge_from_formula(formula):
negative = '<STR_LIT:->' in formula<EOL>positive = '<STR_LIT:+>' in formula<EOL>if positive and negative:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif not (positive or negative):<EOL><INDENT>return <NUM_LIT:0><EOL><DEDENT>multiplier, sign = (-<NUM_LIT:1>, '<STR_LIT:->') if negative else (<NUM_LIT:1>, '<STR_LIT:+>')<EOL>hit = False<EOL>if '<STR_LIT:(>' in formula:<EOL><INDENT>hit = bracketed_charge_re.findall(formula)<EOL>if hit:<EOL><INDENT>formula = hit[-<NUM_LIT:1>].replace('<STR_LIT:(>', '<STR_LIT>').replace('<STR_LIT:)>', '<STR_LIT>')<EOL><DEDENT><DEDENT>count = formula.count(sign)<EOL>if count == <NUM_LIT:1>:<EOL><INDENT>splits = formula.split(sign)<EOL>if splits[<NUM_LIT:1>] == '<STR_LIT>' or splits[<NUM_LIT:1>] == '<STR_LIT:)>':<EOL><INDENT>return multiplier<EOL><DEDENT>else:<EOL><INDENT>return multiplier*int(splits[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return multiplier*count<EOL><DEDENT>
r'''Basic formula parser to determine the charge from a formula - given
    that the charge is already specified as one element of the formula.

    Performs no sanity checking that elements are actually elements.

    Parameters
    ----------
    formula : str
        Formula string, very simply formats only, ending in one of '+x',
        '-x', n*'+', or n*'-' or any of them surrounded by brackets but always
        at the end of a formula.

    Returns
    -------
    charge : int
        Charge of the molecule, [faraday]

    Notes
    -----

    Examples
    --------
    >>> charge_from_formula('Br3-')
    -1
    >>> charge_from_formula('Br3(-)')
    -1