[Tutor] printing columns

Rob rob@uselesspython.com
Fri, 26 Jul 2002 07:45:21 -0500


Using the two tabs does create two columns after a fashion, they're just not
evenly laid out. Quite functional, of course, but part of our grade in this
class is based on how the code looks to someone who has to read it.

Of course this would have been far easier to just type than to script in
Python in this case, but I felt like a bit of a diversion from the studies.

As far as using #define instead of const, these are preprocessor directives
in a header file for a Windows GUI in a class dedicated simultaneously to
the Win32 API and to more advanced OOP concepts in C++ (probably more
intermediate than advanced, but I'm not quibbling over symantics too much in
this case). The instructor wants to see it coded this way. Also, and I'm not
sure how ultimately relevant this is, but the header file consists of
nothing but this list of #define statements. This is then included in a .rc
resource file.

Rob
http://uselesspython.com

> -----Original Message-----
> From: alan.gauld@bt.com [mailto:alan.gauld@bt.com]
> Sent: Friday, July 26, 2002 6:07 AM
> To: rob@uselesspython.com; tutor@python.org
> Subject: RE: [Tutor] printing columns
>
>
> > code that produces a header file for a C++ program, but I've
> > got a simple bit of polish I'd like to put on it.
>
> > >>> menuItems = ['NEW', 'OPEN', 'CLOSE', 'SAVE', 'SAVEAS',
> > ...
> > 'CUSTOMTB', 'CASCADE', 'TILE', 'SPLIT']
>
> > >>> myInt = 1
> > >>> for item in menuItems:
> > 	print '#define ROB_' + item + '\t\t' + str(myInt)
> > 	myInt = myInt + 1
> >
> > Now I'm wondering how I could do this a little more neatly,
> > organizing the output into two columns.
>
> Doesn't your two tabs do exactly that? The other way of
> course would be to create a format string with fixed
> length fields...
>
>
> The other issue of course is why you are using #defines
> in a C++ program? Surely const would be better?
>
> Or since you are assigning a series of integer values
> use an enum which will do that automatically.
>
> enum codes {NEW=1,
>            OPEN,  // automatically assigned 2 etc...
>            ...
>            TILE,
>            SPLIT};
>
> Saves a mite of typing and you can now add new values
> easily without worrying about maintaining the numeric
> values etc. And its typesafe, nobody can kid the compiler
> by passing a raw integer pretending to be a code to a
> function which expects a code as input...
>
> Altogether safer and more idiomatic C++.
>
> Alan G.
>