Coding and Decoding in Python

Discussion in 'Python' started by Wanderer, Mar 17, 2011.

  1. Wanderer

    Wanderer Guest

    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)
    'qinfBitDepth' : 5, # Maximum bit depth
    'qinfCooled' : 6, # Returns 1 if
    cooler is available, 0 if not
    'qinfReserved1' : 7, # Reserved
    'qinfImageWidth ' : 8, # Width of the ROI (in
    pixels)
    'qinfImageHeight' : 9, # Height of the ROI (in
    pixels)
    'qinfImageSize' : 10, # Size of returned
    image (in bytes)
    'qinfCcdType' : 11, # CDD type (see
    QCam_qcCcdType)
    'qinfCcdWidth' : 12, # CCD maximum width
    'qinfCcdHeight' : 13, # CCD maximum height
    'qinfFirmwareBuild' : 14, # Build number of the
    firmware
    'qinfUniqueId' : 15, # Same as uniqueId
    in QCam_CamListItem
    'qinfIsModelB' : 16, # Cameras
    manufactured after March 1, 2004 return 1, otherwise 0
    'qinfIntensifierModel' : 17, # Intensifier tube
    model (see QCam_qcIntensifierModel)
    'qinfExposureRes' : 18, # Exposure time
    resolution (nanoseconds)
    'qinfTriggerDelayRes' : 19, # Trigger delay
    Resolution (nanoseconds)
    'qinfStreamVersion' : 20, # Streaming version
    'qinfNormGainSigFigs' : 21, # Normalized Gain
    Significant Figures resolution
    'qinfNormGaindBRes' : 22, # Normalized Gain dB
    resolution (in micro units)
    'qinfNormITGainSigFigs' : 23, # Normalized Intensifier
    Gain Significant Figures
    'qinfNormITGaindBRes' : 24, # Normalized Intensifier
    Gain dB resolution (micro units)
    'qinfRegulatedCooling' : 25, # 1 if camera has
    regulated cooling
    'qinfRegulatedCoolingLock' : 26, # 1 if camera is at
    regulated temperature, 0 otherwise
    'qinfFanControl' : 29, # 1 if camera can
    control fan speed
    'qinfHighSensitivityMode' : 30, # 1 if camera has high
    sensitivity mode available
    'qinfBlackoutMode' : 31, # 1 if camera has
    blackout mode available
    'qinfPostProcessImageSize' : 32, # Returns the size (in
    bytes) of the post-processed image
    'qinfAsymmetricalBinning' : 33, # 1 if camera has
    asymmetrical binning (ex: 2x4)
    'qinfEMGain' : 34, # 1 if EM gain is
    supported, 0 if not
    'qinfOpenDelay' : 35, # 1 if shutter open
    delay controls are available, 0 if not
    'qinfCloseDelay' : 36, # 1 if shutter close
    delay controls are available, 0 if not
    'qinfColorWheelSupported' : 37, # 1 if color wheel is
    supported, 0 if not
    'qinfReserved2' : 38,
    'qinfReserved3' : 39,
    'qinfReserved4' : 40,
    'qinfReserved5' : 41,
    'qinfEasyEmModeSupported' : 42, # 1 if camera supports
    Easy EM mode
    'qinfLockedGainModeSupported' : 43,
    'qinf_last' : 44,
    '_qinf_force32' : 0xFFFFFFFF
    }
    Wanderer, Mar 17, 2011
    #1
    1. Advertising

  2. Wanderer

    Mel Guest

    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.
    Mel, Mar 17, 2011
    #2
    1. Advertising

  3. Wanderer

    John Gordon Guest

    In <> Wanderer <> writes:

    > But when the program returns the value I can't get the key.


    What happens when two keys have the same value? How would you know which
    key to return?

    In your sample code all the values are different, but surely that won't
    always be the case with real data.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Mar 17, 2011
    #3
  4. Wanderer

    Wanderer Guest

    On Mar 17, 11:44 am, John Gordon <> wrote:
    > In <> Wanderer <> writes:
    >
    > > But when the program returns the value I can't get the key.

    >
    > What happens when two keys have the same value?  How would you know which
    > key to return?
    >
    > In your sample code all the values are different, but surely that won't
    > always be the case with real data.
    >
    > --
    > John Gordon                   A is for Amy, who fell down the stairs
    >              B is for Basil, assaulted by bears
    >                                 -- EdwardGorey, "The Gashlycrumb Tinies"


    I guess two keys having the same value is why dictionaries don't
    return keys for values, but this is a code. Each value has a unique
    meaning to both sender and receiver. The text part is for making the
    program understandable and printing understandable error messages.
    Wanderer, Mar 17, 2011
    #4
  5. Wanderer

    John Gordon Guest

    In <> Wanderer <> writes:

    > I guess two keys having the same value is why dictionaries don't
    > return keys for values, but this is a code. Each value has a unique
    > meaning to both sender and receiver. The text part is for making the
    > program understandable and printing understandable error messages.


    I see. You're storing integer equivalents for the labels themselves,
    not the actual data associated with the labels.

    Then Mel's solution is a good one -- construct a second dict which
    has the keys and values swapped.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Mar 17, 2011
    #5
  6. On Thu, 17 Mar 2011 08:07:28 -0700, 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.


    If you only have a few keys:

    def find_key(value, d):
    """Return the key in dictionary d that has the given value.
    If there are multiple keys with the same value, return an
    arbitrarily chosen one."""
    for k, v in d.items():
    if v == value: return k
    raise KeyError('no such value found')


    If you have many keys/values, then simply create a reverse dictionary:

    Rev_QCam_Info = {}
    for key, value in QCam_Info.items():
    Rev_QCam_Info[value] = key

    and then search that.




    --
    Steven
    Steven D'Aprano, Mar 18, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Roberts
    Replies:
    0
    Views:
    952
    Tim Roberts
    Jun 25, 2003
  2. Nemesis

    X-faces decoding with Python

    Nemesis, Aug 24, 2003, in forum: Python
    Replies:
    0
    Views:
    290
    Nemesis
    Aug 24, 2003
  3. calmar
    Replies:
    11
    Views:
    756
    calmar
    Feb 21, 2006
  4. Robocop

    Python barcode decoding

    Robocop, Oct 24, 2008, in forum: Python
    Replies:
    5
    Views:
    931
    Wolfgang Draxinger
    Oct 28, 2008
  5. Johannes Bauer

    Python 3.0 automatic decoding of UTF16

    Johannes Bauer, Dec 5, 2008, in forum: Python
    Replies:
    25
    Views:
    3,801
    John Machin
    Dec 7, 2008
Loading...

Share This Page