views:

43

answers:

1

I've got a problem to solve in Jython. The function I've got looks like this:

ok = whatever1(x, ...)
self.assertTrue("whatever1 failed: "+x...(), ok)

ok = whatever2(x, ...)
self.assertTrue("whatever2 failed: "+x...(), ok)

[ many many lines ] ...

There are many tests that look like this, they contain mostly ok=... tests, but there are some other things done too. I know which functions are testable, because they come from only one namespace (or I can leave the "ok = " part). The question is - how to transform the source automatically, so that I write only:

ok = whatever1(x, ...) # this is transformed
ok = whatever2(x, ...) # this too
something_else(...) # this one isn't

and the rest is generated automatically?

I know about unparse and ast - is there any better way to approach this problem? (yeah, I know - Maybe-like monad) I'm looking at the rope library too and cannot decide... which way is the best one to choose here? The transformation I described is the only one I need and I don't mind creating a temporary file that will get included in the real code.

+2  A: 

Are you sure you need an AST? If the only lines of interest are the ones starting with "ok = ", then maybe simple string work on the source files would be enough?

Ned Batchelder
True - that's a good idea... although some more interesting syntax might come into play, so I'll wait for more answers.
viraptor