Pythonic: Difference between revisions

1,527 bytes added ,  17 April 2016
→‎Python from Octave: idea of a Python object viewed from Octave
(→‎Python from Octave: idea of a Python object viewed from Octave)
Line 45: Line 45:
This would be the default behavior. We will extend pyeval to receive an optional argument specifying the Octave type that should be used as the output of the conversion, e.g. when the dict uses continuos numbers as keys one could do
This would be the default behavior. We will extend pyeval to receive an optional argument specifying the Octave type that should be used as the output of the conversion, e.g. when the dict uses continuos numbers as keys one could do


<!-- {{SyntaxHighlight| -->
{{Code|pyeval optional argument|<syntaxhighlight lang="octave" style="font-size:13px">
{{Code|pyeval optional argument|<syntaxhighlight lang="octave" style="font-size:13px">
> x = pyeval ("{1:'one',2:'two'}",@cell);
> x = pyeval ("{1:'one',2:'two'}",@cell);
Line 76: Line 75:


The optional argument could be the constructor of an Octave class.
The optional argument could be the constructor of an Octave class.
==== Octave view of Python ====
Currently we can do things like
{{Code|Objects in python exists across calls to pyexec and pyeval|<syntaxhighlight lang="octave" style="font-size:13px">
pyexec ("import networkx as nx")
pyexec ("G=nx.complete_graph(10)")
x = pyeval ("G.nodes()")
</syntaxhighlight>}}
But we cannot get a representation of ''G'' within Octave.
The idea is to have an object that maintains a pointer to an Python object and that has all the methods to convert that object to Octave.
The python object can be manipulated by python calls like {{Codeline|pycall}} and {{Codeline|pyexec}} without needing to do an explicit conversion. For example
{{Code|Octave object that points to a python object|<syntaxhighlight lang="octave" style="font-size:13px">
pyexec ("import networkx as nx")
pyexec ("G=nx.complete_graph(10)")
G      = pyobj ("G") ## G is an object without Octave representation but with methods to do the conversion if required
nodes  = pycall ("nx.nodes",G) # Python nx.nodes(G)
nodes2 = pyeval ("G.nodes()")  # Equivalent
</syntaxhighlight>}}
'''Comment:''' It seems that pyexec and pycall do not share workspace, so the code above wont work because ''nx'' doesn't exist.
The conversion methods should provide different output objects to Octave. The idea is to be able to call something like
{{Code|Octave view of Python object with conversion methods|<syntaxhighlight lang="octave" style="font-size:13px">
G_cell  = cell(G);  # G converted to a cell
G_struct = struct(G); # G converted to a struct
</syntaxhighlight>}}
657

edits