Converting String to Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michael

    #1

    Converting String to Class

    Hello,

    I have a script that is called by the shell this way :

    mqtrigger.py "TMC 2TEST.QUEUE LV1871.MQPROCES S"

    So argv[1] is the string "TMC 2TEST.QUEUE LV1871.MQPROCES S"

    I would like to construct a class with that string that contains the
    attributes

    -StrucId
    -VersionId
    -QName
    -ProcessName

    I am thinking about using the following info

    opts = [['StrucId', CMQC.MQTMC_STRU C_ID, '4s'],
    ['Version', CMQC.MQTMC_VERS ION_2, '4s'],
    ['QName', '', '48s'], ## or less :-)
    ['ProcessName', '', '48s']] ## or less :-)

    1. is the attributename
    2. is the default
    3. is the format in the string

    So the string could be parsed with the opts Info and the attributes
    could be inserted into the class. I dont even have a starting point
    how to do that. Could somebody point my into the right direction

    Regards

    Michael
  • Matt Gerrans

    #2
    Re: Converting String to Class

    You didn't specify exactly how the string is parsed, so this is a guess:

    class Thingy(object):
    def __init__(self,r awinfo):
    self.StructId, vq,self.Process Name = rawinfo.split()
    self.Version,se lf.QName = vq.split('.')
    def __str__(self):
    return '<Thingy: StructId: %s, Version: %s, QName: %s, ProcessName: %s[color=blue]
    >' % (self.StructId, self.Version, self.QName, self.ProcessNam e)[/color]

    t = Thingy( "TMC 2TEST.QUEUE LV1871.MQPROCES S" )
    str(t) -> '<Thingy: StructId: TMC, Version: 2TEST, QName: QUEUE,
    ProcessName: LV1871.MQPROCES S >'

    - mfg


    Comment

    Working...