Getting in to metaprogramming

Rafe rafesacks at gmail.com
Thu Nov 27 06:23:31 CET 2008


On Nov 27, 12:11 pm, Rafe <rafesa... at gmail.com> wrote:
> On Nov 27, 11:41 am, "Hendrik van Rooyen" <m... at microcorp.co.za>
> wrote:
>
>
>
> >  "Steven D'Aprano" <steau> wrote:
>
> > > Well, I don't know about "any problem". And it's not so much about
> > > whether metaprograms can solve problems that can't be solved by anything
> > > else, as whether metaprograms can solve problems more effectively than
> > > other techniques.
>
> > > If you include factory functions, class factories, the builder design
> > > pattern, metaclasses, etc. as "metaprogramming", then I use it all the
> > > time, and find it an excellent technique to use.
>
> > > But if you mean using a Python program to generate Python source code,
> > > then I can't think of any time I used it. Which doesn't mean that others
> > > don't find it helpful, only that I haven't yet.
>
> > I am using the term in the restricted sense of Python writing Python source.
>
> > Given that, can anybody think of an example that you could not do with
> > a class?  (excepting the "stored procedure" aspect)
>
> > Or can I claim a new a new meta - rule - I would call it van Rooyen's folly...
>
> > > Thinking further back, when I was young and programming in Apple's
> > > Hypercard 4GL, I used to frequently use Hypercard scripts to generate new
> > > Hypercard scripts. That was to work around the limitations of the
> > > scripting language.
>
> > What sort of stuff did you do, and would having had simple OO available
> > have rendered it unnecessary?
>
> > > I don't think metaprogramming in the limited sense (programs to output
> > > source code) is a bad idea, but I do think that there are probably better
> > > alternatives in a language like Python.
>
> > True. No argument here - I was just wondering if the relationship holds.
>
> > - Hendrik
>
> "Given that, can anybody think of an example that you could not do
> with a class?"
>
> Generating a template for a specific script application. For example,
> a script with pre-defined callbacks that only require the addition of
> the contents.
>
> I was really interested in exploring the idea of using python output,
> instead of XML, to record something a user did in a GUI. I have seen
> it done and it is really advantageous in the 3D industry because it
> means the script files can be edited directly, in a pinch, to generate
> something slightly different.
>
> For example, say we have code which generates a cube by plotting it's
> points. A user then changes a point position in the GUI. The change is
> saved by outputting the function call to a file with new arguments
> (the new point location). If I wanted to make 100s of copies of the
> cube, but with a slightly different point position, I could edit the
> custom cube's python code and hand it back for creation without using
> the GUI. I could do this with XML, but it can be harder to work with
> in a text editor (though I have seen some XML editors that make it a
> bit easier.) In fact, in most 3D applications, the app prints
> everything the user does to a log. Sometimes in a choice of languages,
> so I guess I am looking to do the same thing with my own custom tools.
>
> In a real situation the generated code file can build some pretty
> complex 3D object hierarchies. It moves beyond simple storage of data
> and becomes a real script that can be hacked as necessary.
>
> It is nice to have everything as python scripts because we always have
> a blend of GUI users and developers to get a job done.
>
> -Rafe

I was just thinking (hopefully i get some time to try this soon) that
it wouldn't be difficult to decorate a function so that when called, a
line of code is output. as long as the arguments can be stored as a
string (strings, numbers, lists, etc. but no 'object' instances) it
should be able to be executed to get the same result. I think it would
just have to:

1) Dynamically write the name with a '('

2) Gather all the args in a list and ", ".join(args)

3) Gather kwargs as a list of ['%s = %s' % key, value] and then and ",
".join(kwlist)

4) Add ')\n'


- Rafe



More information about the Python-list mailing list