Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Branch: master
43 lines (34 sloc) 1.394 kB
import popen2
import simplejson as json
class PHP:
"""This class provides a stupid simple interface to PHP code."""
def __init__(self, prefix="", postfix=""):
"""prefix = optional prefix for all code (usually require statements)
postfix = optional postfix for all code
Semicolons are not added automatically, so you'll need to make sure to put them in!"""
self.prefix = prefix
self.postfix = postfix
def __submit(self, code):
(out, inp) = popen2.popen2("php")
print >>inp, "<? "
print >>inp, self.prefix
print >>inp, code
print >>inp, self.postfix
print >>inp, " ?>"
return out
def get_raw(self, code):
"""Given a code block, invoke the code and return the raw result as a string."""
out = self.__submit(code)
def get(self, code):
"""Given a code block that emits json, invoke the code and interpret the result as a Python value."""
out = self.__submit(code)
return json.loads(
def get_one(self, code):
"""Given a code block that emits multiple json values (one per line), yield the next value."""
out = self.__submit(code)
for line in out:
line = line.strip()
if line:
yield json.loads(line)
Jump to Line
Something went wrong with that request. Please try again.