Coding and Decoding in Python

Mel mwilson at the-wire.com
Thu Mar 17 11:14:56 EDT 2011


Wanderer wrote:

> I have a dll that to communicate with I need to send numeric codes. So
> I created a dictionary. It works in one direction in that I can
> address the key and get the value. But when the program returns the
> value I can't get the key. This code is very simple and I could use a
> list and the index except for the last value. Is there a better way to
> handle coding and decoding values to strings?
> 
> QCam_Info = {
>         'qinfCameraType'             : 0,    # Camera model (see
> QCam_qcCameraType)
>         'qinfSerialNumber'            : 1,    # Deprecated
>         'qinfHardwareVersion'       : 2,    # Hardware version
>         'qinfFirmwareVersion'       : 3,    # Firmware version
>         'qinfCcd'                          : 4,    # CCD model (see
> QCam_qcCcd)
[ ... ]
     '_qinf_force32'               : 0xFFFFFFFF
>     }

I handled this problem in a kind of cheap, nasty way with (untested)

for k, v in QCam_Info.items():
    QCam_Info[v] = k

Then the dictionary lookups work both ways.

	Mel.



More information about the Python-list mailing list