def push_to_device(device_or_devices):
    # get an iterable list of devices even if one wasn't specified
    try:
        iter(device_or_devices)
    except __HOLE__:
        devices = (device_or_devices, )
    else:
        devices = device_or_devices

    # keyed access to topics for which we'll have an APNs connection for each
    topic_frames = {}

    for device in devices:

        if device.topic in apns_cxns:
            if device.topic not in topic_frames:
                # create our keyed topic reference if it doesn't exist
                topic_frames[device.topic] = Frame()

            # decode from as-stored base64 into hex encoding for apns library
            token_hex = device.token.decode('base64').encode('hex')

            mdm_payload = MDMPayload(device.push_magic)

            # add a frame for this topic
            topic_frames[device.topic].add_item(token_hex, mdm_payload, random.getrandbits(32), 0, 10)
        else:
            # TODO: configure and use real logging
            print 'Cannot send APNs to device: no APNs connection found (by device topic)'

    # loop through our by-topic APNs Frames and send away
    for topic in topic_frames.keys():
        apns_cxns[topic].gateway_server.send_notification_multiple(topic_frames[topic])
dataset/ETHPy150Open jessepeterson/commandment/commandment/push.py/push_to_device