import re


def dpd_digit(arg):
    """Calculates the Checksum for DPD Packets.

    See http://static.23.nu/md/Pictures/BIC3_DPDPaketscheinspez_Neu_D_v101.pdf page 22 for an explanation.

    Exaple:
    >>> dpd_digit('400599871650')
    'E'
    >>> dpd_digit('007110601632532948375179276')
    'A'
    """

    if not re.compile('[A-Z0-9]*').match(arg):
        raise ValueError("Not a valid argument: %r" % arg)

    # to calculate the code values - position equals code value
    _chartable = list("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

    mod = 36
    cdigit = mod
    for char in arg:
        cdigit = cdigit + _chartable.index(char.upper())
        if cdigit > mod:
            cdigit = cdigit - mod
        cdigit = 2 * cdigit
        if cdigit > mod:
            cdigit = cdigit - mod - 1
    cdigit = mod + 1 - cdigit
    if cdigit == mod:
        cdigit = 0
    return _chartable[cdigit]