'Try to write self as ``exp_polar(2*pi*I*n)*z`` in a nice way.
Return (z, n).
>>> from sympy import exp_polar, I, pi
>>> from sympy.abc import x, y
>>> exp_polar(I*pi).extract_branch_factor()
(exp_polar(I*pi), 0)
>>> exp_polar(2*I*pi).extract_branch_factor()
(1, 1)
>>> exp_polar(-pi*I).extract_branch_factor()
(exp_polar(I*pi), -1)
>>> exp_polar(3*pi*I + x).extract_branch_factor()
(exp_polar(x + I*pi), 1)
>>> (y*exp_polar(-5*pi*I)*exp_polar(3*pi*I + 2*pi*x)).extract_branch_factor()
(y*exp_polar(2*pi*x), -1)
>>> exp_polar(-I*pi/2).extract_branch_factor()
(exp_polar(-I*pi/2), 0)
If allow_half is True, also extract exp_polar(I*pi):
>>> exp_polar(I*pi).extract_branch_factor(allow_half=True)
(1, 1/2)
>>> exp_polar(2*I*pi).extract_branch_factor(allow_half=True)
(1, 1)
>>> exp_polar(3*I*pi).extract_branch_factor(allow_half=True)
(1, 3/2)
>>> exp_polar(-I*pi).extract_branch_factor(allow_half=True)
(1, -1/2)'

def extract_branch_factor(self, allow_half=False):

    from sympy import exp_polar, pi, I, ceiling, Add
    n = S(0)
    res = S(1)
    args = Mul.make_args(self)
    exps = []
    for arg in args:
        if (arg.func is exp_polar):
            exps += [arg.exp]
        else:
            res *= arg
    piimult = S(0)
    extras = []
    while exps:
        exp = exps.pop()
        if exp.is_Add:
            exps += exp.args
            continue
        if exp.is_Mul:
            coeff = exp.as_coefficient((pi * I))
            if (coeff is not None):
                piimult += coeff
                continue
        extras += [exp]
    if (not piimult.free_symbols):
        coeff = piimult
        tail = ()
    else:
        (coeff, tail) = piimult.as_coeff_add(*piimult.free_symbols)
    branchfact = (ceiling(((coeff / 2) - (S(1) / 2))) * 2)
    n += (branchfact / 2)
    c = (coeff - branchfact)
    if allow_half:
        nc = c.extract_additively(1)
        if (nc is not None):
            n += (S(1) / 2)
            c = nc
    newexp = (((pi * I) * Add(*((c,) + tail))) + Add(*extras))
    if (newexp != 0):
        res *= exp_polar(newexp)
    return (res, n)
