Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Import Python library into an Ada package?

151 views
Skip to first unread message

Roger Mc

unread,
Dec 4, 2020, 2:36:15 AM12/4/20
to
Is it possible to import a Python library, such as graphviz, into an Ada package?
So far I have only been able to find information on exporting Ada to Python.

Dmitry A. Kazakov

unread,
Dec 4, 2020, 5:23:23 AM12/4/20
to
On 2020-12-04 08:36, Roger Mc wrote:
> Is it possible to import a Python library, such as graphviz, into an Ada package?
> So far I have only been able to find information on exporting Ada to Python.

I am not sure what you mean. Python is not a compiled language, so
formally speaking a Python library is not a library and you cannot
import it in the sense of linking it to your application and calling
subprograms from it using certain calling conventions.

If you rather meant whether you could execute a Python script from Ada
while passing parameters to it and taking results from, yes you can. If
that script were a part of some Python module, yes you can load it and
once loaded call (interpret) functions from the module.

P.S. Before you proceed, Python is a huge mess and interfacing it is a
pain in the ... So you should consider if Graphviz worths the efforts.
If you find a GTK or Qt library that doing approximately same, that
would be a wiser choice, IMO.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

Roger Mc

unread,
Dec 4, 2020, 6:37:55 AM12/4/20
to
Many thanks for your prompt response and comments Dmitry; they are well appreciated with some of the contents somewhat expected.
I think that I misused the term " Python library"; I think "Python module" is what I should have used.
In this context, in Python, is a module a script? I'll investigate this.
So I need to see if I can "can load it and once loaded call (interpret) functions from the module".
This seems to the approach that I need to try.
Your comment regarding Graphviz was expected. I used it as an example to provide context and certainly do not intend to actually use it for needed graphics. My plan is to use GTK or write my own Ada code for simpler graphics.
The project that I am embarking on is to use Ada for an on-line course in machine learning that uses Python as its teaching platform.
The importing that I was contemplating concerns special machine learning Python modules used in the course.
Of course, the alternative is for me to translate the Python modules into Ada which is something I've done in the past; generally, in my opinion, yielding much better and more readable code.
Again, thanks for your very helpful comments which, hopefully, have focused my mind on the way ahead.

Regarding your comment that "Python is a huge mess" and my own opinion of Python; I am mortified that Python seems to have become the standard language for teaching computer programming and, particularly, that it seems to be the choice of leading university computer science courses.
It seems that the old well-established rules of quality computer program design have been completely abandoned by these institutions.

Best regards,
Roger Mc Murtrie

Niklas Holsti

unread,
Dec 4, 2020, 8:22:48 AM12/4/20
to
On 2020-12-04 9:36, Roger Mc wrote:
> Is it possible to import a Python library, such as graphviz, into an Ada package?


If you mean the Graphviz tool-set,
https://en.wikipedia.org/wiki/Graphviz, that seems to be written in C
and to be open source. You should be able to call Graphviz functions
from Ada in the same way as one calls any C code from Ada. The Python
module you refer to is probably just a binding from Python to the C code
in Graphviz.

If you want to use Graphviz just to draw automatically laid-out graphs,
there is another way, that I have used: make the Ada program write out
the graph definition as a text file in the "dot" language, and then
invoke the "dot" program from Graphviz to lay out and draw the graph
into some graphical format. However, it may be troublesome to make this
method work interactively -- I was satified with non-interactive
post-processing of the "dot" file generated by my Ada program.

gautier...@hotmail.com

unread,
Dec 4, 2020, 8:41:10 AM12/4/20
to
As a side note, there is a cool utility called DePlo ( https://sites.google.com/site/depplot/ , sources here : https://launchpad.net/deplo ) that creates a dependency graph of Ada units from the .ali files that GNAT produces when building a project.
This graph is in the Graphviz's DOT format.
And indeed, graphviz is not specific to Python. The sources are in C, and the Web site mentions bindings to: guile, perl, python, ruby, C#, tcl .

Simon Wright

unread,
Dec 4, 2020, 8:55:18 AM12/4/20
to
Roger Mc <roge...@gmail.com> writes:

> Regarding your comment that "Python is a huge mess" and my own opinion
> of Python; I am mortified that Python seems to have become the
> standard language for teaching computer programming and, particularly,
> that it seems to be the choice of leading university computer science
> courses.

I'd certainly agree that interfacing to Python from Ada is a huge mess
(specifically, unsupported hand management of garbage collection, as you
have to do if invoking Python objects).

Björn Lundin

unread,
Dec 4, 2020, 1:33:00 PM12/4/20
to
Den 2020-12-04 kl. 14:22, skrev Niklas Holsti:
> If you want to use Graphviz just to draw automatically laid-out graphs,
> there is another way, that I have used: make the Ada program write out
> the graph definition as a text file in the "dot" language, and then
> invoke the "dot" program from Graphviz to lay out and draw the graph
> into some graphical format.

And if you really just want to draw graphs - and can use another tool -
gnuplot can be controlled by spawning it and send commands on stdin via
pipes.


--
Björn

Roger Mc

unread,
Dec 4, 2020, 2:08:57 PM12/4/20
to
Thanks for the many helpful comments regarding Graphviz.
I shall certainly consider using these for my project.
I'm not sure what is meant by "automatically laid-out graphs".
Roger

Niklas Holsti

unread,
Dec 4, 2020, 2:40:46 PM12/4/20
to
Graphviz and in particular its "dot" tool are used to draw nice views of
"graphs" that consist of nodes and arcs, not the simple kind of x-y plot
"graph" that we so often look at nowadays -- showing how the pandemic
curve is growing or flattening -- and that, AIUI, gnuplot is mostly used
for.

In the "dot" language, one defines a node-and-arc graph by listing and
naming all the nodes and then listing all the arcs from one node to
another. The "dot" tool performs an automatic lay-out that places the
nodes on the two-dimensional plane (= drawing surface) so that few arcs
cross other arcs, and most arcs connect nodes that are close together --
in other words, a a visually clear and neat node-arc graphic instead of
an arbitrary spaghetti.

The "dot" language and tool provide many ways to control the lay-out and
to annotate it. Very flexible.

Dmitry A. Kazakov

unread,
Dec 4, 2020, 4:09:53 PM12/4/20
to
If you want to build a GUI you can use Cairo. It has all necessary stuff
for vector graphics. E.g. this one uses GtkAda Cairo bindings:

http://www.dmitry-kazakov.de/ada/aicwl.htm

Per Sandberg

unread,
Dec 4, 2020, 4:12:14 PM12/4/20
to
gnatcoll.python + a lot of binding work

/P

Roger Mc

unread,
Dec 4, 2020, 4:15:57 PM12/4/20
to
Thanks Dmitry,
I am considering using aicwl where appropriate.
Regards,
Roger

Roger Mc

unread,
Dec 4, 2020, 4:19:12 PM12/4/20
to
I have been trying to figure out how to use gnatcoll.python.
Unfortunately it doesn't seem to provide any supporting documentation.

Björn Lundin

unread,
Dec 4, 2020, 4:25:10 PM12/4/20
to
Den 2020-12-04 kl. 20:40, skrev Niklas Holsti:
>
> In the "dot" language, one defines a node-and-arc graph by listing and
> naming all the nodes and then listing all the arcs from one node to
> another. The "dot" tool performs an automatic lay-out that places the
> nodes on the two-dimensional plane (= drawing surface) so that few arcs
> cross other arcs, and most arcs connect nodes that are close together --
> in other words, a a visually clear and neat node-arc graphic instead of
> an arbitrary spaghetti.

Very interesting - I think I'll spend some time with its docs - thanks

--
Björn

Dmitry A. Kazakov

unread,
Dec 4, 2020, 6:17:14 PM12/4/20
to
What about this:

https://docs.adacore.com/gnatcoll-docs/scripting.html

I have a rudimentary Python bindings independent on GNATColl, which I
use to run Python scripts from Ada. They were designed to load Python
dynamically, I did not want to make the application dependent on Python
installed. If you want, you can use them as a template. There is no
documentation, but the code using them. But as I said, better not... (:-))

Roger Mc

unread,
Dec 4, 2020, 6:33:39 PM12/4/20
to
I tried using pdf on a small project.
Great result.
I tried on a complex project.
A real mess.
Then tried using ps with the complex project.
Great result when viewed with PostView.

Roger Mc

unread,
Dec 4, 2020, 6:42:26 PM12/4/20
to

> What about this:
>
> https://docs.adacore.com/gnatcoll-docs/scripting.html
>
> I have a rudimentary Python bindings independent on GNATColl, which I
> use to run Python scripts from Ada. They were designed to load Python
> dynamically, I did not want to make the application dependent on Python
> installed. If you want, you can use them as a template. There is no
> documentation, but the code using them. But as I said, better not... (:-))
> --
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

I would really appreciate seeing your "rudimentary Python bindings ".
In the meantime I must study gnatcoll-docs/scripting.html more closely.
I have made a start using an example I found on the web but unfortunately it crashes with an EXC_SOFTWARE, Software generated exception which I think may be due to an invalid process descriptor.
Thanks,
Roger

Roger Mc

unread,
Dec 4, 2020, 6:57:54 PM12/4/20
to
Some good news.
I now have my example working.
The problem was simply not specifying the correct path to my Python module!

Roger Mc

unread,
Dec 4, 2020, 7:13:26 PM12/4/20
to
On Saturday, December 5, 2020 at 10:57:54 AM UTC+11, Roger Mc wrote:
> On Saturday, December 5, 2020 at 10:42:26 AM UTC+11, Roger Mc wrote:
> > > What about this:
> > >
> > > https://docs.adacore.com/gnatcoll-docs/scripting.html
> > >
> > > I have a rudimentary Python bindings independent on GNATColl, which I
> > > use to run Python scripts from Ada. They were designed to load Python
> > > dynamically, I did not want to make the application dependent on Python
> > > installed. If you want, you can use them as a template. There is no
> > > documentation, but the code using them. But as I said, better not... (:-))
> > > --
> > > Regards,
> > > Dmitry A. Kazakov
> > > http://www.dmitry-kazakov.de

gnatcoll-docs/scripting.html seems mostly concerned with exporting Ada resources.
I can't find information in it for importing Python modules into Ada.
Importing is briefly addressed by Ada Gem #54: Scripting Capabilities in GNAT (Part 2).

Dmitry A. Kazakov

unread,
Dec 5, 2020, 4:38:15 AM12/5/20
to
On 2020-12-05 00:42, Roger Mc wrote:

> I would really appreciate seeing your "rudimentary Python bindings ".

Download sources of this:

http://www.dmitry-kazakov.de/ada/max_home_automation.htm

The project is large. Only these packages are related to Python:

1. Py is the bindings

2. Py.Load_Python_Library is an OS-dependent part for loading Python
dynamically from a DLL (Linux or Windows)

3. Py.ELV_MAX_Cube is an implementation of a Python module in Ada. I.e.
calling Ada from Python.

4. MAX_Control_Page contains a task that periodically runs Python
script. I.e. calling Python from Ada.

196...@googlemail.com

unread,
Dec 12, 2020, 9:38:07 AM12/12/20
to
[,,,]

I'm late to the party as usual.

Why not just code it in SVG? It is very easy and just plain XML.

Roger Mc

unread,
Dec 16, 2020, 6:58:38 PM12/16/20
to
I've no idea how to do this.

In any event I've decided to bite the bullet and try and port the python code to Ada.
Thanks everyone for your valuable advice. Much appreciated.
0 new messages