No macros in Python

Mel Wilson mwilson at the-wire.com
Mon Dec 16 14:30:20 EST 2002


In article <Pine.GSO.4.44_heb2.09.0212160242440.16160-100000 at techunix.technion.ac.il>,
Beni Cherniavsky <cben at techunix.technion.ac.il> wrote:
>On 2002-12-15, Mel Wilson wrote:
>
>>  If you must have such a thing as C macros, it's
>> available.I tried this as a way of simplifying the
>> creation of a lot of edit boxes with wxPython:
>>
>> new_edit_box_macro = """\
>> hs = wxBoxSizer (wxHORIZONTAL)
>> hs.Add (wxStaticText (self, -1, '%(label)s', wxDefaultPosition, labelsize)
>> self.%(field)s_ctrl = wxTextCtrl (self, -1, '', wxDefaultPosition)
                 ^
   later..
>> hs.Add (self, %(field)s_ctrl, 1)
>> vs.Add (hs, 0, wxGROW)"""
>>
>> exec (new_edit_box_macro % {'label':'Key No.', 'field':'person_id'})
>> exec (new_edit_box_macro % {'label':'First Name', 'field':'fname'})
>> exec (new_edit_box_macro % {'label':'Last Name', 'field':'lname'})
>>
>> and so on.
>>
># Untested
>for label, field in [('Key No.', 'person_id'),
>                     ('First Name', 'fname'),
>                     ('Last Name', 'lname')]:
>    hs = wxBoxSizer (wxHORIZONTAL)
>    hs.Add (wxStaticText (self, -1, label, wxDefaultPosition,
>                          labelsize))
>    ctrl = wxTextCtrl (self, -1, '', wxDefaultPosition)

     ^
   The original named this `person_id_ctrl`, etc.  The
original made this detail hard to spot.  Could of course be
fixed in the `for` list.  C macros in actual C have quite
a tough time with string splicing like this.

>    hs.Add (self, ctrl, 1)
>    vs.Add (hs, 0, wxGROW)
>    self.__dict__[field + '_ctrl'] = ctrl
>
>Variable assignment is not the real point of macros IMHO.

   True.  Creating source code from other strings is the real
point of C macros.

   My comeuppance will be when this gets converted to Visual
Basic per customer spec.  All that ersatz macro stuff will
have to be taken down.

        Regards.        Mel.



More information about the Python-list mailing list