Le Sun, 17 Apr 2005 13:38:09 +0200, Andrew E a écrit : > Hi all > > I've written a python program that adds orders into our order routing > simulation system. It works well, and has a syntax along these lines: > > ./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20 > > etc > > However, I'd like to add a mode that will handle, say: > > ./neworder buy 23 NOKIA at MKT x 20 > > I could enter several orders either by running multiple times, or use a > comma-separated approach, like: > > ./neworder buy 23 NOKIA at MKT on helsinki, sell 20 NOKIA at market on > helsinki It would be simpler to have the rule : one line == one order, action, command wahtever. > > The thing about this is that its a "tolerant" parser, so all of these > should also work: > > # omit words like "at", "on" > ./neworder buy 23 NOKIA mkt helsinki > > # take any symbol for helsinki > ./neworder buy 23 mkt helsinki > > # figure out that market=helsinki > ./neworder buy 23 NOKIA at market price You have some variation of the same « buy » order. If you want to detail the semantics : you arrive to determine the different parameter of a BuyCommand : what, where, how (many) ... This semantic is expressed differently in english, US-english, french ... If you base your parser on regex you see that the same BuyOrder is represented by a number of variant : Please, buy 23 NOKIA at market Helsinki Acheter 40 actions Bouygues à la Bouse de Paris
So you have for a given environment (orders management module) a number of commands expressed in pseudo-natural languages. Each command has a certain number of attributes name, module_name, parameters, variants(list of regexps for instance), description, short description, sample sentence (to be presented to the user in an interactive session if requested). An important attribute is naturally the action taken when the parser/interpreter matches the current line. So you group the description of the Commands in an easy to parse file and write an interpreter which on startup read such a file for the Commands it understands : command to load a module, to list the commands in the currently loaded module or in the interpreter, command to display the sample, short or long description (help about cmd or how to spell cmd)., command to stop the interpreter (and the program) ... command to read/include a file containings commands to parse and execute. Python is dynamic, with setattr() you can easily construct a Command object from its description. Here is what could be the description of a command to compute the equilibrium position in a stability module (using a «here» shell like syntax ): command equilibre << eoc description << eod La commande « equilibre » permet de lancer le calcul de la position d'équilibre du flotteur couramment décrit. position equilibre en partant de pqr 0.0/0.0/1.0 et h=8.500 les mots-clés sont equilibre pqr et h[=] eod sample <<eos equilibre pqr 0.0/0.0/1.0 avec h=8.500 eos # name of function to call action compute_equi # parameters of the command (a dictionary of parameters could be in # action's arguments parameters << eop pqr h eop > # The regexpes variants << eov # the following regexp is with re.I (ignore case flag) : variant I << eovi equilibrer?\spqr\s+(?P<pqr>\d*\.\d*/\d*\.\d*/\d*\.\d*)\s+(?P<h>\d*\.\d*) eovvi # syntax of regexp untested :-) variant << eovi equilibrium\spqr\s+(?P<pqr>\d*\.\d*/\d*\.\d*/\d*\.\d*)\s+(?P<h>\d*\.\d*) eovvi eov eoc > Thanks for any suggestions :) > > Andrew -- http://mail.python.org/mailman/listinfo/python-list