3

I have to evaluate (millions of) Python expressions e.g. (int(a) >> 8 == 4) and b in my OCaml program. There is pycaml but I failed to get it working.

So I turned to another idea: control the input/output of Python interpreter directly.

Ideally I would like to intercept both the input/output of the interpreter itself. By sending a = 3 b = 5 a > b to the interpreter, I would then be able to get the result False, as if I have done this by keyboard..

>>> a = 3
>>> b = 5
>>> a > b
False
>>> 

However, my code doesn't working as expected (while the same code worked for some interactive program)

let (readme, writeme) = Unix.open_process "python -u";; 
let _ = output_string writeme "3 + 5\n" in
let _ = flush writeme in 
let result = input_line readme in
print_endline result;;

I tried changing 3 + 5\n to print 3\n, but it still hangs at input_line. Is there any better way to do this? I would need to evaluate quite a lot of expressions, so I don't really want to do this via a temp file. Any help appreciated, Thanks.

1
  • It seems like python does something weird to read its input. If instead of opening "python" as your process you open "ocaml" it works (and you change what you are writing, obviously). If you run "python > output.txt" from the commandline it will write the output of each expression to the file. However if you do "python < input.txt > output.txt" it doesn't seem to work, which leads me to conclude that python is doing something strange to stdin. Commented Jun 13, 2011 at 17:41

2 Answers 2

3

I'm not going to comment on the weirdness of the entire concept (driving python to evaluate expressions from o'caml) but it seems like you might want to look into writing a python program that is an eval cycle that reads/writes a string from/to a pipe. Look up the eval command.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - Of course the best option would be writing an expression evaluator in OCaml.. however I wanted to keep it simple and it seems to be very complicated especially when floating point arithmetic and type casting are involved.
2

You can supply a command to the interpreter through the command line:

$ python -c 'a = 3; b = 5; print a > b'
False

Is that adequate for your needs?

If you're concerned about opening the interpreter repeatedly, you could generate and evaluate many expressions at once. I'm not sure what the upper limit is, but I was able to evaluate and print 200 concatenated copies of a = 3; b = 5; print a > b; without any problem.

2 Comments

Thank you - This is possible, but I think if I do this for a million times, then a lot of time would be wasted on loading the interpreter. Is it possible to kind of "stay" in the interpreter without re-execute it every time?
Hm... perhaps. But I'm not sure trying to interface with the interpreter is a better idea. See above for another suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.