tag:blogger.com,1999:blog-4170332600441687813.post7405574070599648086..comments2013-03-23T09:45:51.933-07:00Comments on Advances in Pencil Science: Why Python?Michael Tobishttp://www.blogger.com/profile/08229460438349093944noreply@blogger.comBlogger14125tag:blogger.com,1999:blog-4170332600441687813.post-36290831813919266712008-03-25T09:15:00.000-07:002008-03-25T09:15:00.000-07:00Some poor sod showed up here looking for how to do...Some poor sod showed up here looking for how to do int.tostring in Python; in case it comes up again for somebody, str() will convert anything to a string representation in Python.Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-74296387799226224362008-01-27T20:47:00.000-08:002008-01-27T20:47:00.000-08:00Oh, I am sure there are cases where C++ is not a ...Oh, I am sure there are cases where C++ is not a good solution. I am not arguing for C++ instead of python. The both have their value.<BR/>What I was saying is that you can write readable OO code in C++ and you also get a lot of libraries for C++. One reason I am using C++ at work is that we have to produce operational application and the static typing gives us some additional security. For, say, climate modelling that would not necessarily be an issue. Speed can be more of a problem. But that's where inter-language calls come in handy.<BR/><BR/>CheersAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-73033976393214200212008-01-27T19:20:00.000-08:002008-01-27T19:20:00.000-08:00I haven't worked with them myself, but as I unders...I haven't worked with them myself, but as I understand it C++ templates (Blitz, etc.) are an impractical solution to a class of problem that dynamic languages actually solve.<BR/><BR/>I have heard of compile times for very high level physics models in C++ in the hours, often to fail because the *compiler* ran out of core!Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-55961065124754284352008-01-27T17:29:00.000-08:002008-01-27T17:29:00.000-08:00Hi Michael,There might well be such a library avai...Hi Michael,<BR/><BR/>There might well be such a library available. I don't know.<BR/>Since C++ is also quite popular, there is also a myriad of libraries available. One project to provide a industrial standard library is boost (http://www.boost.org). They have a parser, but I am not sure if it is as "nifty" as what pyparse does.<BR/>Another library to do numerical (OO style) computing en-par with fortran is blitz (http://www.oonumerics.org/blitz/)<BR/><BR/>I have started to play around with f2py to see if I can get the OSS model plasim (http://www.mi.uni-hamburg.de/Theoretische-Meteorologie.6.0.html)<BR/>"ported" to python. I don't want to re-write the whole model, so I start by wrapping core functionality into python and only replacing the higher level code.<BR/>I hope that bit-by-bit this will allow me to develop a framework where people can quickly and easily inject (or replace) python code.<BR/><BR/>We'll see.<BR/><BR/>I believe you can fascinate (at least some) of the computational scientist by showing them how much more fun and productive it can be to write OO code. <BR/><BR/>When I came into the group I am in at the moment, I brought with me some of that enthusiam. Now people that have before written perl + fotran suites tell me they feel "insecure" if they can't write unit-tests along with their (Python + C++) code:)<BR/><BR/>CheersAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-53423507556681022292008-01-27T17:04:00.000-08:002008-01-27T17:04:00.000-08:00D, OK, fair enough as far as it goes. On the other...D, OK, fair enough as far as it goes. <BR/><BR/>On the other hand, has someone actually written NiftyParsing? Else your argument is to an extent somewhat theoretical. PyParsing is under 4 KLOC and written by a hobbyist. The advantage of Python is that a very wide array of such libraries exist, and that it's such fun to create them that people do so voluntarily.<BR/><BR/>All that said, what fascinates me most about the story is that most practicing computational scientists, certainly in the climate and weather domains, don't find the sort of elegance we are discussing interesting at all. <BR/><BR/>It's how to make the case for OOP to them that really matters, and it's for that reason that I display this approach to roman numerals.<BR/><BR/>I make very little headway in convincing them that such matters are other than an irrelevant curiosity, although the relevance of parsing to modeling complex systems to me is obvious. <BR/><BR/>I think a working prototype may be more valuable than any proposals or verbiage I can pull together.Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-44235189543744041412008-01-27T16:40:00.000-08:002008-01-27T16:40:00.000-08:00Hi,Though I like python, I don't think this a very...Hi,<BR/><BR/>Though I like python, I don't think this a very strong case for it's superior readability over C++.<BR/><BR/>Using templates you could write a simmilar parsing library and your code would look along these lines:<BR/><BR/>#include "NiftyParsing.h"<BR/><BR/>NiftyParsing::Literal romanNumeralLiteral(const string& numeralString, const int value)<BR/>{<BR/> return NiftyParsing::Literal(numeralString).setParseAction( NiftyParsing::ReplaceWith<int, value>() );<BR/>}<BR/><BR/>one = romanNumeralLiteral("I",1)<BR/>four = romanNumeralLiteral("IV",4)<BR/>//etc.<BR/><BR/>NiftyParser::SomeClass numeral = ( onethousand | ninehundred | fivehundred | fourhundred |<BR/> onehundred | ninety | fifty | forty | ten | nine | five |<BR/> four | one ).leaveWhitespace()<BR/><BR/>OneOrMore romanNumeral(numeral, ParseAction<int, Summation>())<BR/><BR/><BR/>cout << romanToInteger("XLII") << endl;<BR/><BR/>Validation could be as easily done as in python, though ,regardless of the language- you would not really use exceptions (whose behaviour in C++ is very similar to pyhton's).<BR/><BR/>Cheers<BR/><BR/>P.S.<BR/>If you don't like the NiftyParsing:: scoping, you could import the symbols into you namespace.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-25882451089490976362008-01-26T13:53:00.000-08:002008-01-26T13:53:00.000-08:00Aha. Then copied from ML's exceptions, which appe...Aha. Then copied from ML's exceptions, which appear to be almost identical.<BR/><BR/>However, as a matter of style, in ML exceptions should be actually exceptional and not used routinely for terminations, etc. Of course, nothing enforces this other than a sense of clean, clear programming.David B. Bensonhttps://www.blogger.com/profile/02917182411282836875noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-84777314455702388132008-01-26T12:25:00.000-08:002008-01-26T12:25:00.000-08:00Mine was even easier. I just lifted it from the py...Mine was even easier. I just lifted it from the pyparsing website.<BR/><BR/>Python exceptions are **vastly** more useful and convenient than Java exceptions. <BR/><BR/>In Java you have to enumerate all possible exceptions futher down the call stack. In Python you just assert the ones you intend to handle at the place you handle them. They propagate up the stack until caught, or as a last resort they terminate the Python process. <BR/><BR/>Exceptions are sufficiently lightweight that loop terminations are (usually? always?) handled as exceptions.Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-76864493479396620412008-01-26T12:09:00.000-08:002008-01-26T12:09:00.000-08:00Yes, the greedyParser does some of that: If the pa...Yes, the <I>greedyParser</I> does some of that: If the parse cannot continue to the end it raises the exception <I>Domain</I>.<BR/>What it does not do is check that the roman numerals occur in numerically non-increasing order nor that forms such as "IV" are not repeated.<BR/><BR/>I opine that Python borrowed exceptions from Standard ML (or maybe Java(?)).<BR/><BR/>I'll just mention that developing the SML version required exactly three compiles and no run-time executions: the first compile showed that I forgot that string have <I>size</I>, not <I>length</I> (which arrays have); the second compile printed the answer, but I forgot the new-line character. The third produced the answer shown.David B. Bensonhttps://www.blogger.com/profile/02917182411282836875noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-15199865251256597232008-01-26T08:29:00.000-08:002008-01-26T08:29:00.000-08:00Beware! The pyparsing bug bites deep!Have fun with...Beware! The pyparsing bug bites deep!<BR/><BR/>Have fun with it!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-60839520362702701912008-01-25T17:58:00.000-08:002008-01-25T17:58:00.000-08:00mean, the natural extension is to validate that th...mean, the natural extension is to validate that the string is a valid roman numeral else throw an exception.<BR/><BR/>Python's exception handling is another thing of beauty by the way.<BR/><BR/>Of course, Fortran wins for sheer minimalism: it usually either ignores the exception or gives you a mysterious segfault...Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-58814240036468729182008-01-25T15:25:00.000-08:002008-01-25T15:25:00.000-08:00Michael --- You'll have to explain (or give a link...Michael --- You'll have to explain (or give a link) to what yoou mean by "validation" in this context. Otherwise I don't know how to answer.David B. Bensonhttps://www.blogger.com/profile/02917182411282836875noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-35316976137070835772008-01-25T14:45:00.000-08:002008-01-25T14:45:00.000-08:00David, I can't deny that is even nicer as far as i...David, I can't deny that is even nicer as far as it goes. Does it extend neatly to validation?Michael Tobishttps://www.blogger.com/profile/08229460438349093944noreply@blogger.comtag:blogger.com,1999:blog-4170332600441687813.post-36647233362562116932008-01-25T14:31:00.000-08:002008-01-25T14:31:00.000-08:00Here is what is looks like in Standard ML (except ...Here is what is looks like in Standard ML (except that I don't know how to produce (what I consider to be) the proper indenting):<BR/><BR/>val romanNumeralLiteralsWithValues =<BR/> [<BR/> ("I",1),<BR/> ("IV",4),<BR/> ("V",5),<BR/> ("IX",9),<BR/> ("X",10),<BR/> ("XL",40),<BR/> ("L",50)<BR/> ]<BR/> fun arabicize input = Int.toString(greedyParse romanNumeralLiteralsWithValues (op+) 0 input)<BR/> (*example*)<BR/> val () = print("XLII ~~> "^(arabicize "XLII")^"\n")<BR/><BR/><BR/>which at the end prints<BR/><BR/>XLII ~~> 42<BR/><BR/>on a separate line.David B. Bensonhttps://www.blogger.com/profile/02917182411282836875noreply@blogger.com