Merge ssh://192.168.66.221/~vitor/Yap/yap-6.3
This commit is contained in:
21
packages/python/examples/namedtuples.yap
Normal file
21
packages/python/examples/namedtuples.yap
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
:- use_module( library(python) ).
|
||||
|
||||
:- := import( collections ).
|
||||
:- := import( yap ).
|
||||
:- e := yap.'YAPEngine'().
|
||||
|
||||
main :-
|
||||
system_predicate(N/A),
|
||||
args(0,A,L),
|
||||
N := namedtuple( N, L),
|
||||
fail.
|
||||
main :-
|
||||
:= e.call( writeln( 1 ) ).
|
||||
|
||||
args(N, N, []) :- !.
|
||||
args(I0,IF,[AI|Ais]) :-
|
||||
I is I0+1,
|
||||
number_string(I, IS),
|
||||
string_concat("A", IS, AI),
|
||||
args(I, IF, Ais).
|
0
packages/python/examples/untitled
Normal file
0
packages/python/examples/untitled
Normal file
48
packages/python/images.py
Normal file
48
packages/python/images.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import base64
|
||||
import imghdr
|
||||
import os
|
||||
|
||||
#from IPython.
|
||||
|
||||
_TEXT_SAVED_IMAGE = "yap_kernel: saved image data to:"
|
||||
|
||||
image_setup_cmd = """
|
||||
display () {
|
||||
TMPFILE=$(mktemp ${TMPDIR-/tmp}/yap_kernel.XXXXXXXXXX)
|
||||
cat > $TMPFILE
|
||||
echo "%s $TMPFILE" >&2
|
||||
}
|
||||
""" % _TEXT_SAVED_IMAGE
|
||||
|
||||
def display_data_for_image(filename):
|
||||
with open(filename, 'rb') as f:
|
||||
image = f.read()
|
||||
os.unlink(filename)
|
||||
|
||||
image_type = imghdr.what(None, image)
|
||||
if image_type is None:
|
||||
raise ValueError("Not a valid image: %s" % image)
|
||||
|
||||
image_data = base64.b64encode(image).decode('ascii')
|
||||
content = {
|
||||
'data': {
|
||||
'image/' + image_type: image_data
|
||||
},
|
||||
'metadata': {}
|
||||
}
|
||||
return content
|
||||
|
||||
|
||||
def extract_image_filenames(output):
|
||||
output_lines = []
|
||||
image_filenames = []
|
||||
|
||||
for line in output.split("\n"):
|
||||
if line.startswith(_TEXT_SAVED_IMAGE):
|
||||
filename = line.rstrip().split(": ")[-1]
|
||||
image_filenames.append(filename)
|
||||
else:
|
||||
output_lines.append(line)
|
||||
|
||||
output = "\n".join(output_lines)
|
||||
return image_filenames, output
|
92
packages/python/kernel
Normal file
92
packages/python/kernel
Normal file
@@ -0,0 +1,92 @@
|
||||
from __future__ import print_function
|
||||
|
||||
from metakernel import MetaKernel
|
||||
|
||||
from metakernel import register_ipython_magics
|
||||
register_ipython_magics()
|
||||
|
||||
class MetaKernelyap(MetaKernel):
|
||||
implementation = 'MetaKernel YAP'
|
||||
implementation_version = '1.0'
|
||||
language = 'text'
|
||||
language_version = '0.1'
|
||||
banner = "MetaKernel YAP"
|
||||
language_info = {
|
||||
'mimetype': 'text/plain',
|
||||
'name': 'text',
|
||||
# ------ If different from 'language':
|
||||
'codemirror_mode': {
|
||||
"version": 2,
|
||||
"name": "prolog"
|
||||
}
|
||||
'pygments_lexer': 'language',
|
||||
'version' : "0.0.1",
|
||||
'file_extension': '.yap',
|
||||
'help_links': MetaKernel.help_links,
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
MetaKernel.__init__(self, **kwargs)
|
||||
self._start_yap()
|
||||
self.qq = None sq
|
||||
|
||||
def _start_yap(self):
|
||||
# Signal handlers are inherited by forked processes, and we can't easily
|
||||
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
|
||||
# message handlers, we need to temporarily reset the SIGINT handler here
|
||||
# so that yap and its children are interruptible.
|
||||
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
||||
try:
|
||||
engine = yap.YAPEngine()
|
||||
engine.query("load_files(library(python), [])").command()
|
||||
banner = "YAP {0} Kernel".format(self.engine.version())
|
||||
|
||||
finally:
|
||||
signal.signal(signal.SIGINT, sig)
|
||||
|
||||
# Register Yap function to write image data to temporary file
|
||||
#self.yapwrapper.run_command(image_setup_cmd)
|
||||
|
||||
def get_usage(self):
|
||||
return "This is the YAP kernel."
|
||||
|
||||
def do_execute_direct(self, code):
|
||||
if not code.strip():
|
||||
return {'status': 'ok', 'execution_count': self.execution_count,
|
||||
'payload': [], 'user_expressions': {}}
|
||||
|
||||
interrupted = False
|
||||
try:
|
||||
print self.q
|
||||
if self.q is None:
|
||||
self.q = self.engine.query(code.rstrip())
|
||||
if self.q.next():
|
||||
vs = self.q.namedVars()
|
||||
if vs.length() > 0:
|
||||
l = []
|
||||
while vs.length() > 0:
|
||||
eq = vs.car()
|
||||
l.append(' '.join([getArg(1).text(), '=', eq.getArg(2).text())
|
||||
vs = vs.cdr()
|
||||
l.append(';')
|
||||
o = '\n'.join(l)
|
||||
else:
|
||||
return 'yes'
|
||||
self.q = None
|
||||
|
||||
else:
|
||||
return 'no'
|
||||
self.q = None
|
||||
|
||||
|
||||
|
||||
def repr(self, data):
|
||||
return repr(data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
from ipykernel.kernelapp import IPKernelApp
|
||||
except ImportError:
|
||||
from IPython.kernel.zmq.kernelapp import IPKernelApp
|
||||
IPKernelApp.launch_instance(kernel_class=MetaKernelyap)
|
61
packages/python/plot.py
Normal file
61
packages/python/plot.py
Normal file
@@ -0,0 +1,61 @@
|
||||
%matplotlib inline
|
||||
import numpy as np
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
sns.set(style="white", context="talk")
|
||||
rs = np.random.RandomState(7)
|
||||
|
||||
pos={0:(0,0),
|
||||
1:(1,0),
|
||||
2:(0,1),
|
||||
3:(1,1),
|
||||
4:(0.1,0.9),
|
||||
5:(0.3,1.1),
|
||||
6:(0.9,0.9)
|
||||
}
|
||||
|
||||
names={4:'MMM',
|
||||
5:'XXX',
|
||||
6:'ZZZ'}
|
||||
|
||||
def plot1(y10,y20):
|
||||
def gen(f,f0):
|
||||
return [f[0],f[1],-f[2]]/max(f,f0)
|
||||
ax1 = plt.subplot2grid((1,2), (0,0), colspan=2)
|
||||
ax2 = plt.subplot2grid((1,2), (0,1), colspan=2)
|
||||
ax3 = plt.subplot2grid((2,2), (2,0), colspan=2, rowspan=2)
|
||||
|
||||
xs = ["+-","++","--"]
|
||||
y1 = gen(y10, y20)
|
||||
sns.barplot(xs, y1, palette="RdBu_r", ax=ax1)
|
||||
y2 = gen(y20,y10)
|
||||
sns.barplot(xs, y2, palette="Set3", ax=ax2)
|
||||
# Finalize the plot
|
||||
# sns.despine(bottom=True)
|
||||
|
||||
|
||||
G=nx.Graph()
|
||||
i=0
|
||||
G.pos={} # location
|
||||
G.pop={} # size
|
||||
lpos={0:(0,0),1:(0,0),2:(0,0),3:(0,0)}
|
||||
last=len(pos)-1
|
||||
for i in range(4,len(pos)):
|
||||
G.pos[i]=pos[i]
|
||||
G.pop[i]=2000
|
||||
(x,y) = pos[i]
|
||||
lpos[i] = (x,y-0.05)
|
||||
if i > 4:
|
||||
G.add_edge(i-1,i)
|
||||
else:
|
||||
G.add_edge(2,i)
|
||||
G.add_edge(3,last)
|
||||
nx.draw_networkx_nodes(G,pos,nodelist=range(4,len(pos)),ax=ax3)
|
||||
nx.draw_networkx_nodes(G,pos,nodelist=[0,1,2,3],node_color='b',ax=ax3)
|
||||
nx.draw_networkx_edges(G,pos,alpha=0.5,ax=ax3)
|
||||
nx.draw_networkx_labels(G,lpos,names,alpha=0.5,ax=ax3)
|
||||
plt.axis('off')
|
||||
plt.tight_layout(h_pad=3)
|
||||
plt.savefig("house_with_colors.png") # save as png
|
||||
|
||||
plot1([20,30,10],[30,30,5])
|
1283
packages/python/prolog.js
Normal file
1283
packages/python/prolog.js
Normal file
File diff suppressed because it is too large
Load Diff
@@ -185,9 +185,9 @@ foreign_t python_to_term(PyObject *pVal, term_t t) {
|
||||
Py_ssize_t i, sz = PyTuple_Size(pVal);
|
||||
functor_t f;
|
||||
const char *s;
|
||||
if ((s = (Py_TYPE(pVal)->tp_name)))
|
||||
if ((s = (Py_TYPE(pVal)->tp_name))) {
|
||||
f = PL_new_functor(PL_new_atom(s), sz);
|
||||
else
|
||||
} else
|
||||
f = PL_new_functor(ATOM_t, sz);
|
||||
if (!PL_unify_functor(t, f))
|
||||
return FALSE;
|
||||
|
@@ -28,7 +28,7 @@ static foreign_t python_f(term_t tmod, term_t fname, term_t tf) {
|
||||
char *s;
|
||||
size_t len;
|
||||
PyObject *pF, *pModule;
|
||||
|
||||
|
||||
/* if an atom, fetch again */
|
||||
if (PL_is_atom(tmod)) {
|
||||
PyObject *pName;
|
||||
@@ -42,40 +42,59 @@ static foreign_t python_f(term_t tmod, term_t fname, term_t tf) {
|
||||
pName = PyUnicode_FromString(s);
|
||||
#endif
|
||||
if (pName == NULL) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pModule = PyImport_Import(pName);
|
||||
PyErr_Clear();
|
||||
} else if (!(pModule = term_to_python(tmod, true))) {
|
||||
PyErr_Clear();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pF = PyObject_GetAttrString(pModule, s);
|
||||
PyErr_Print();
|
||||
Py_DECREF(pModule);
|
||||
if (pF == NULL || !PyCallable_Check(pF)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
foreign_t rc = python_to_ptr(pF, tf);
|
||||
return rc;
|
||||
}
|
||||
return python_to_ptr(pF, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_o(term_t tmod, term_t fname, term_t tf) {
|
||||
char *s;
|
||||
size_t len;
|
||||
PyObject *pO, *pModule;
|
||||
|
||||
|
||||
pModule = term_to_python(tmod, true);
|
||||
if (!PL_get_nchars(fname, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pO = PyObject_GetAttrString(pModule, s);
|
||||
if (pO == NULL) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
foreign_t rc = python_to_ptr(pO, tf);
|
||||
;
|
||||
return rc;
|
||||
}
|
||||
return python_to_ptr(pO, tf);
|
||||
}
|
||||
|
||||
static foreign_t python_len(term_t tobj, term_t tf) {
|
||||
@@ -83,8 +102,9 @@ static foreign_t python_len(term_t tobj, term_t tf) {
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
if (o == NULL) {
|
||||
return false;
|
||||
}
|
||||
len = PyObject_Length(o);
|
||||
return PL_unify_int64(tf, len);
|
||||
}
|
||||
@@ -94,40 +114,54 @@ static foreign_t python_dir(term_t tobj, term_t tf) {
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
if (o == NULL) {
|
||||
return false;
|
||||
}
|
||||
dir = PyObject_Dir(o);
|
||||
return python_to_ptr(dir, tf);
|
||||
{
|
||||
foreign_t rc = python_to_ptr(dir, tf);
|
||||
;
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_index(term_t tobj, term_t tindex, term_t val) {
|
||||
PyObject *i;
|
||||
PyObject *o;
|
||||
PyObject *f;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (o == NULL)
|
||||
return false;
|
||||
if (!PySequence_Check(o))
|
||||
if (o == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (!PySequence_Check(o)) {
|
||||
return false;
|
||||
}
|
||||
i = term_to_python(tindex, true);
|
||||
if (i == NULL)
|
||||
if (i == NULL) {
|
||||
return false;
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
f = PyObject_CallMethodObjArgs(o, PyString_FromString("getitem"), i);
|
||||
#else
|
||||
f = PyObject_CallMethodObjArgs(o, PyUnicode_FromString("getitem"), i);
|
||||
#endif
|
||||
return python_to_ptr(f, val);
|
||||
{
|
||||
foreign_t rc = python_to_ptr(f, val);
|
||||
;
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_is(term_t tobj, term_t tf) {
|
||||
PyObject *o;
|
||||
|
||||
o = term_to_python(tobj, true);
|
||||
if (!o)
|
||||
return FALSE;
|
||||
|
||||
return python_to_ptr(o, tf);
|
||||
if (!o) {
|
||||
return false;
|
||||
}
|
||||
foreign_t rc = python_to_ptr(o, tf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static foreign_t python_assign_item(term_t parent, term_t indx, term_t tobj) {
|
||||
@@ -145,23 +179,31 @@ static foreign_t python_assign_item(term_t parent, term_t indx, term_t tobj) {
|
||||
pF = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else if (PyObject_SetItem(p, pI, pF)) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return true;
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** assign a tuple to something:
|
||||
*/
|
||||
static foreign_t python_assign_tuple(term_t t_lhs, term_t t_rhs) {
|
||||
PyObject *e = term_to_python(t_rhs, true);
|
||||
PyObject *e;
|
||||
Py_ssize_t sz;
|
||||
functor_t f;
|
||||
|
||||
e = term_to_python(t_rhs, true);
|
||||
if (!e || !PyTuple_Check(e)) {
|
||||
return -1;
|
||||
}
|
||||
@@ -224,12 +266,18 @@ static foreign_t python_item(term_t parent, term_t indx, term_t tobj) {
|
||||
return false;
|
||||
} else if ((pF = PyObject_GetItem(p, pI)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return address_to_term(pF, tobj);
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(pF, tobj);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
@@ -244,15 +292,23 @@ static foreign_t python_slice(term_t parent, term_t indx, term_t tobj) {
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!pI || !p) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else if ((pF = PySequence_GetSlice(p, 0, 0)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Py_DecRef(pI);
|
||||
Py_DecRef(p);
|
||||
|
||||
return address_to_term(pF, tobj);
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(pF, tobj);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
@@ -268,14 +324,18 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
pF = term_to_python(tin, true);
|
||||
PyErr_Clear();
|
||||
if (pF == NULL) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (PL_is_atom(targs)) {
|
||||
pArgs = NULL;
|
||||
} else {
|
||||
|
||||
if (!PL_get_name_arity(targs, &aname, &arity)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (arity == 1 && PL_get_arg(1, targs, targ) && PL_is_variable(targ)) {
|
||||
/* ignore (_) */
|
||||
@@ -283,15 +343,18 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
} else {
|
||||
|
||||
pArgs = PyTuple_New(arity);
|
||||
if (!pArgs)
|
||||
return FALSE;
|
||||
if (!pArgs) {
|
||||
return false;
|
||||
}
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, targs, targ))
|
||||
return FALSE;
|
||||
if (!PL_get_arg(i + 1, targs, targ)) {
|
||||
return false;
|
||||
}
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
if (pArg == NULL) {
|
||||
return false;
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
}
|
||||
@@ -319,13 +382,16 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
}
|
||||
} else {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (pArgs)
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL)
|
||||
return FALSE;
|
||||
if (pValue == NULL) {
|
||||
return false;
|
||||
}
|
||||
out = python_to_ptr(pValue, tf);
|
||||
return out;
|
||||
}
|
||||
@@ -333,21 +399,23 @@ static foreign_t python_apply(term_t tin, term_t targs, term_t keywds,
|
||||
static foreign_t python_assign(term_t name, term_t exp) {
|
||||
PyObject *e = term_to_python(exp, true);
|
||||
|
||||
if (e == NULL)
|
||||
return FALSE;
|
||||
if (e == NULL) {
|
||||
return false;
|
||||
}
|
||||
return assign_python(py_Main, name, e) >= 0;
|
||||
}
|
||||
|
||||
static foreign_t python_assign_field(term_t source, term_t name, term_t exp) {
|
||||
PyObject *e = term_to_python(exp, true), *root = term_to_python(source, true);
|
||||
|
||||
if (e == NULL)
|
||||
return FALSE;
|
||||
if (e == NULL) {
|
||||
return false;
|
||||
}
|
||||
return assign_python(root, name, e) >= 0;
|
||||
}
|
||||
|
||||
static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
PyObject *pI, *pArgs, *pOut;
|
||||
PyObject *pI, *pArgs, *pOut;
|
||||
PyObject *env;
|
||||
atom_t name;
|
||||
char *s;
|
||||
@@ -356,36 +424,47 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
|
||||
if ((env = py_Builtin) == NULL) {
|
||||
// no point in even trying
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (PL_get_name_arity(caller, &name, &arity)) {
|
||||
if (!(s = PL_atom_chars(name)))
|
||||
if (!(s = PL_atom_chars(name))) {
|
||||
return false;
|
||||
}
|
||||
if ((pI = PyObject_GetAttrString(env, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Prolog should make sure this never happens.
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, caller, targ))
|
||||
return FALSE;
|
||||
if (!PL_get_arg(i + 1, caller, targ)) {
|
||||
return false;
|
||||
}
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
pArg = Py_None;
|
||||
} else {
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
if (pArg == NULL) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
if (PyTuple_SetItem(pArgs, i, pArg)) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
pOut = PyObject_CallObject(pI, pArgs);
|
||||
@@ -393,9 +472,15 @@ static foreign_t python_builtin_eval(term_t caller, term_t dict, term_t out) {
|
||||
Py_DECREF(pI);
|
||||
if (pOut == NULL) {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
foreign_t rc = python_to_ptr(pOut, out);
|
||||
;
|
||||
return rc;
|
||||
}
|
||||
return python_to_ptr(pOut, out);
|
||||
}
|
||||
|
||||
static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
@@ -405,57 +490,76 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
int i, arity;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (o == NULL)
|
||||
return FALSE;
|
||||
if (o == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (PL_is_atom(f)) {
|
||||
if (!PL_get_atom_chars(f, &s))
|
||||
return FALSE;
|
||||
if (!PL_get_atom_chars(f, &s)) {
|
||||
return false;
|
||||
}
|
||||
if ((pValue = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/* follow chains of the form a.b.c.d.e() */
|
||||
while (name == ATOM_dot && arity == 2) {
|
||||
term_t tleft = PL_new_term_ref();
|
||||
PyObject *lhs;
|
||||
|
||||
if (!PL_get_arg(1, f, tleft))
|
||||
return FALSE;
|
||||
if (!PL_get_arg(1, f, tleft)) {
|
||||
return false;
|
||||
}
|
||||
lhs = term_to_python(tleft, true);
|
||||
if ((o = PyObject_GetAttr(o, lhs)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!PL_get_arg(2, f, f)) {
|
||||
return false;
|
||||
}
|
||||
if (!PL_get_arg(2, f, f))
|
||||
return FALSE;
|
||||
if (!PL_get_name_arity(f, &name, &arity)) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (!s)
|
||||
if (!s) {
|
||||
return false;
|
||||
}
|
||||
if ((pF = PyObject_GetAttrString(o, s)) == NULL) {
|
||||
PyErr_Print();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pArgs = PyTuple_New(arity);
|
||||
for (i = 0; i < arity; i++) {
|
||||
PyObject *pArg;
|
||||
if (!PL_get_arg(i + 1, f, targ))
|
||||
return FALSE;
|
||||
if (!PL_get_arg(i + 1, f, targ)) {
|
||||
return false;
|
||||
}
|
||||
/* ignore (_) */
|
||||
if (i == 0 && PL_is_variable(targ)) {
|
||||
pArgs = Py_None;
|
||||
}
|
||||
pArg = term_to_python(targ, true);
|
||||
if (pArg == NULL)
|
||||
return FALSE;
|
||||
if (pArg == NULL) {
|
||||
return false;
|
||||
}
|
||||
/* pArg reference stolen here: */
|
||||
PyTuple_SetItem(pArgs, i, pArg);
|
||||
}
|
||||
@@ -463,9 +567,13 @@ static foreign_t python_access(term_t obj, term_t f, term_t out) {
|
||||
Py_DECREF(pArgs);
|
||||
Py_DECREF(pF);
|
||||
if (pValue == NULL) {
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
return python_to_term(pValue, out);
|
||||
}
|
||||
|
||||
static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
@@ -475,7 +583,9 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
int arity;
|
||||
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
PyObject *p;
|
||||
|
||||
@@ -484,39 +594,61 @@ static foreign_t python_field(term_t parent, term_t att, term_t tobj) {
|
||||
p = term_to_python(parent, true);
|
||||
// Exp
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
if (arity == 1 && !strcmp(s, "()")) {
|
||||
if (!PL_get_arg(1, att, att))
|
||||
if (!PL_get_arg(1, att, att)) {
|
||||
return false;
|
||||
}
|
||||
if (!PL_get_name_arity(att, &name, &arity)) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
s = PL_atom_chars(name);
|
||||
}
|
||||
if (!s || !p) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else if ((pF = PyObject_GetAttrString(p, s)) == NULL) {
|
||||
PyErr_Clear();
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return address_to_term(pF, tobj);
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(pF, tobj);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_main_module(term_t mod) {
|
||||
return address_to_term(py_Main, mod);
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(py_Main, mod);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_function(term_t tobj) {
|
||||
PyObject *obj = term_to_python(tobj, true);
|
||||
foreign_t rc = PyFunction_Check(obj);
|
||||
|
||||
return PyFunction_Check(obj);
|
||||
return rc;
|
||||
}
|
||||
|
||||
foreign_t python_builtin(term_t out) {
|
||||
return address_to_term(py_Builtin, out);
|
||||
{
|
||||
foreign_t rc;
|
||||
rc = address_to_term(py_Builtin, out);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_run_file(term_t file) {
|
||||
@@ -530,24 +662,37 @@ static foreign_t python_run_file(term_t file) {
|
||||
PyRun_SimpleFileEx(PyFile_AsFile(PyFileObject), "test.py", 1);
|
||||
#else
|
||||
FILE *f = fopen(s, "r");
|
||||
if (f == NULL)
|
||||
if (f == NULL) {
|
||||
return false;
|
||||
}
|
||||
PyRun_SimpleFileEx(f, s, 1);
|
||||
#endif
|
||||
return TRUE;
|
||||
{
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
extern PyThreadState *YAP_save;
|
||||
|
||||
|
||||
static foreign_t python_run_command(term_t cmd) {
|
||||
char *s;
|
||||
bool rc = false;
|
||||
size_t len;
|
||||
char si[256];
|
||||
|
||||
s = si;
|
||||
if (PL_get_nchars(cmd, &len, &s, CVT_ALL | CVT_EXCEPTION)) {
|
||||
PyRun_SimpleString(s);
|
||||
rc = true;
|
||||
}
|
||||
return TRUE;
|
||||
return rc;
|
||||
}
|
||||
|
||||
static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
@@ -587,7 +732,9 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
Py_DECREF(pModule);
|
||||
PyErr_Print();
|
||||
fprintf(stderr, "Call failed\n");
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (PyErr_Occurred())
|
||||
@@ -598,11 +745,17 @@ static foreign_t python_run_script(term_t cmd, term_t fun) {
|
||||
Py_DECREF(pModule);
|
||||
} else {
|
||||
PyErr_Print();
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static foreign_t python_export(term_t t, term_t pl) {
|
||||
@@ -611,10 +764,12 @@ static foreign_t python_export(term_t t, term_t pl) {
|
||||
void *ptr;
|
||||
term_t targ = PL_new_term_ref();
|
||||
|
||||
if (!PL_get_arg(1, t, targ))
|
||||
if (!PL_get_arg(1, t, targ)) {
|
||||
return false;
|
||||
if (!PL_get_pointer(targ, &ptr))
|
||||
}
|
||||
if (!PL_get_pointer(targ, &ptr)) {
|
||||
return false;
|
||||
}
|
||||
Py_INCREF((PyObject *)ptr);
|
||||
/* return __main__,s */
|
||||
rc = python_to_term((PyObject *)ptr, pl);
|
||||
@@ -622,9 +777,7 @@ static foreign_t python_export(term_t t, term_t pl) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
static foreign_t p_python_within_python(void) {
|
||||
return python_in_python;
|
||||
}
|
||||
static foreign_t p_python_within_python(void) { return python_in_python; }
|
||||
|
||||
static int python_import(term_t mname, term_t mod) {
|
||||
PyObject *pName, *pModule;
|
||||
@@ -638,14 +791,17 @@ static int python_import(term_t mname, term_t mod) {
|
||||
if (PL_is_pair(mname)) {
|
||||
char *sa;
|
||||
if (!PL_get_arg(1, mname, arg) || !PL_get_atom_chars(arg, &sa) ||
|
||||
!PL_get_arg(2, mname, mname))
|
||||
!PL_get_arg(2, mname, mname)) {
|
||||
return false;
|
||||
}
|
||||
s = stpcpy(s, sa);
|
||||
*s++ = '.';
|
||||
s[0] = '\0';
|
||||
} else if (!PL_get_nchars(mname, &len, &s,
|
||||
CVT_ALL | CVT_EXCEPTION | REP_UTF8)) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -656,7 +812,9 @@ static int python_import(term_t mname, term_t mod) {
|
||||
pName = PyUnicode_FromString(s0);
|
||||
#endif
|
||||
if (pName == NULL) {
|
||||
return false;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pModule = PyImport_Import(pName);
|
||||
PyErr_Clear();
|
||||
@@ -667,12 +825,32 @@ static int python_import(term_t mname, term_t mod) {
|
||||
PyErr_Print();
|
||||
PyErr_Clear();
|
||||
#endif
|
||||
return FALSE;
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
ActiveModules[active_modules++] = pModule;
|
||||
return python_to_ptr(pModule, mod);
|
||||
{ foreign_t rc = python_to_ptr(pModule, mod);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
static PyThreadState *_saveP;
|
||||
|
||||
static YAP_Int
|
||||
p_python_get_GIL(void)
|
||||
{
|
||||
PyEval_AcquireThread(_saveP);
|
||||
return true;
|
||||
}
|
||||
|
||||
static YAP_Int
|
||||
p_python_release_GIL(void)
|
||||
{
|
||||
_saveP = PyEval_SaveThread();
|
||||
return true;
|
||||
}
|
||||
|
||||
install_t install_pypreds(void) {
|
||||
PL_register_foreign("python_builtin_eval", 3, python_builtin_eval, 0);
|
||||
PL_register_foreign("python_builtin", 1, python_builtin, 0);
|
||||
@@ -699,5 +877,6 @@ install_t install_pypreds(void) {
|
||||
PL_register_foreign("python_main_module", 1, python_main_module, 0);
|
||||
PL_register_foreign("python_import", 2, python_import, 0);
|
||||
PL_register_foreign("python_access", 3, python_access, 0);
|
||||
PL_register_foreign("python_within_python", 0, p_python_within_python, 0);
|
||||
PL_register_foreign("release_GIL", 0, p_python_release_GIL, 0);
|
||||
PL_register_foreign("acquire_GIL", 0, p_python_get_GIL, 0);
|
||||
}
|
||||
|
@@ -93,6 +93,7 @@ X_API bool init_python(void) {
|
||||
// wait for YAP_Init
|
||||
return false;
|
||||
}
|
||||
PyGILState_STATE gstate = PyGILState_Ensure();
|
||||
term_t t = PL_new_term_ref();
|
||||
if (!Py_IsInitialized()) {
|
||||
python_in_python = true;
|
||||
@@ -111,5 +112,6 @@ X_API bool init_python(void) {
|
||||
PL_reset_term_refs(t);
|
||||
install_pypreds();
|
||||
install_pl2pl();
|
||||
PyGILState_Release(gstate);
|
||||
return !python_in_python;
|
||||
}
|
||||
|
@@ -27,6 +27,8 @@
|
||||
array_to_python_tuple/4,
|
||||
array_to_python_view/5,
|
||||
python/2,
|
||||
acquire_GIL/0,
|
||||
release_GIL/0,
|
||||
(:=)/2,
|
||||
(:=)/1,
|
||||
% (<-)/2,
|
||||
|
44
packages/python/yap_kernel/#install.py#
Normal file
44
packages/python/yap_kernel/#install.py#
Normal file
@@ -0,0 +1,44 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
from jupyter_client.kernelspec import install_kernel_spec
|
||||
except ImportError:
|
||||
from IPython.kernel.kernelspec import install_kernel_spec
|
||||
from IPython.utils.tempdir import TemporaryDirectory
|
||||
|
||||
|
||||
kernel_json = {
|
||||
"argv": [sys.executable,
|
||||
"-m", "yap_kernel",
|
||||
"-f", "{connection_file}"],
|
||||
"display_name": "yap",
|
||||
"mimetype": "text/x-prolog",
|
||||
"language": "prolog",
|
||||
"name": "yap",
|
||||
}
|
||||
|
||||
def install_my_kernel_spec(user=False):
|
||||
with TemporaryDirectory() as td:
|
||||
os.chmod(td, 0o755) # Starts off as 700, not user readable
|
||||
with open(os.path.join(td, 'kernel.json'), 'w') as f:
|
||||
json.dump(kernel_json, f, sort_keys=True)
|
||||
# TODO: Copy resources once they're specified
|
||||
|
||||
print('Installing IPython kernel spec')
|
||||
install_kernel_spec(td, 'yap', user=False, replace=True)
|
||||
|
||||
def _is_root():
|
||||
return True
|
||||
try:
|
||||
return os.geteuid() == 0
|
||||
except AttributeError:
|
||||
return False # assume not an admin on non-Unix platforms
|
||||
|
||||
def main(argv=[]):
|
||||
user = '--user' in argv or not _is_root()
|
||||
install_my_kernel_spec(user=user)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(argv=sys.argv)
|
30
packages/python/yap_kernel/clause.py
Normal file
30
packages/python/yap_kernel/clause.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
Paired Density and Scatterplot Matrix
|
||||
=====================================
|
||||
|
||||
_thumb: .5, .5
|
||||
"""
|
||||
import seaborn as sns
|
||||
import matplotlib.pyplot as plt
|
||||
sns.set(style="white")
|
||||
|
||||
df = sns.load_dataset("iris")
|
||||
|
||||
g = sns.PairGrid(df, diag_sharey=False)
|
||||
g.map_lower(sns.kdeplot, cmap="Blues_d")
|
||||
g.map_upper(plt.scatter)
|
||||
g.map_diag(sns.kdeplot, lw=3)
|
||||
|
||||
"""
|
||||
Grouped barplots
|
||||
================
|
||||
|
||||
_thumb: .45, .5
|
||||
"""
|
||||
sns.set(style="whitegrid")
|
||||
|
||||
# Draw a nested barplot to show survival for class and sex
|
||||
g = sns.factorplot(x="class", y="survived", hue="sex", data=[15,30,5],
|
||||
size=3, kind="bar", palette="muted")
|
||||
g.despine(left=True)
|
||||
g.set_ylabels("survival probability")
|
Reference in New Issue
Block a user