4 ms·
I agree that these problems can make you wish Python had decent metaprogramming. However, for most of your examples, there are Python solutions without explicit
by limmeau 17y ago
I agree that these problems can make you wish Python had decent metaprogramming. However, for most of your examples, there are Python solutions without explicit metaprogramming which IMHO are not so painful after all.
Parsing: a recursive-descent parser is straight-forward to implement and suffices for moderate parsing needs.
Regexps: there is re.VERBOSE -- you get multi-line regexps with comments.
Databases: SQLAlchemy got to where it is with only a lot of operator overloading. I can't say how painful it was to write because I've only used, not read SQLAlchemy.
Continuations: well... you can write the interaction as a generator if you can live without yielding in subroutines until PEP380 is implemented. Not ideal.
- gchpaco 17y agoWithout wanting to get into too much back and forth, a quick response to these: Parsing: hand writing lexers is an enormous PIA and the problem with a hand written recursive descent parser is if you want to change the grammar for whatever reason it is more involved than if you had a grammar generator. Regexps: the problem with re.VERBOSE is that you're still writing essentially assembly language for a state machine, in the end, and it doesn't solve problems like "I want to search for this user input string, interpolate it into here and escape it for me please." Or even more interesting ones like "I want to compose these regexps, because I'm having to write a lexer by hand". You can use a separate escape function, and generally build up this layer of essentially scar tissue around the fact that this feature of your language is not really well integrated into the language, but that has its own issues. Better to solve it cleanly IMHO. Databases: I have no experience with SQLAlchemy; it didn't exist when I did the DB project. Continuations: I could probably have gotten away with using coroutines there. But Stackless was still an experiment, and not a particularly promising one, and generators didn't exist in the language at the time. I still think CPS-conversion would have been cleaner.
- limmeau 17y agoNow that you mention composable regexps (or composable grammars), I want some, too. :)
- silentbicycle 17y agoWell, if you're considering Lua, look at LPEG. (http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html)
- limmeau 17y ago(Answering myself,) apparently pyparsing[1] composes grammars from grammar objects in the spirit of parser combinators. [1] http://pyparsing.wikispaces.com/ http://pyparsing.wikispaces.com/