Re: Extendable Enum like Type?
On 18/07/19 18:14, Ethan Furman wrote:
> On 07/18/2019 06:04 AM, Antoon Pardon wrote:
>
>> I am experimenting with writing an Earley Parser. Now I would like to
>> have the non-terminals from the grammer I am reading in, be represented
>> bye an enum like type. So that if the grammer contains the following
>> production: Term -> Term '+' Factor I can reprensent the right hand side
>> with a list that gets printed something like the following:
>> [, '+', ] I am a bit at a
>> loss right now on how to start. Can someone point me in the right
>> direction?
>
> The basic method is:
>
> from enum import Enum # `from aenum` [1][2] if less than Python 3.4
>
> Class NonTerminal(Enum):
> Term = 1
> Factor = 2
> ...
>
> The docs [3] also have a lot of information.
>
> Does that answer your question?
I don't seem to have made myself clear. The grammar with its Terminals
and NonTerminals is read in from a file. The program doesn't know what
they will be.
For the moment what I am thinking about is something as follows:
grammar = LoadGrammer(GrammarFile)
EnumList = ['class NonTerminal(Enum):\n']
for Index, NonTerminal in enumerate(grammar.NonTerminals):
EnumList.append('%s = %d\n' % (NonTerminal, Index))
exec(''.join(EnumList), ..., ...)
The problem with this approach is that I can't use these values until
the whole grammer is loaded. I would prefer some way to add values
during the loading of the grammar.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
On Thursday, July 18, 2019 at 2:01:39 PM UTC-6, Chris Angelico wrote: > On Fri, Jul 19, 2019 at 5:51 AM Christian Gollwitzer wrote: > > Once you can do this, you can proceed to call a Python function, which > > in C means that you invoke the function PyObject_CallObject(). A basic > > example is shown here: > > > > https://docs.python.org/2/extending/embedding.html#pure-embedding > > > > Or, better: > > https://docs.python.org/3/extending/embedding.html#pure-embedding > > ChrisA I need to call a .so file, but I don't know I way to do that with PyObject. I can only seem to bring in .py files -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
On Sat, Jul 20, 2019 at 12:16 AM Jesse Ibarra wrote: > > On Thursday, July 18, 2019 at 2:01:39 PM UTC-6, Chris Angelico wrote: > > On Fri, Jul 19, 2019 at 5:51 AM Christian Gollwitzer > > wrote: > > > Once you can do this, you can proceed to call a Python function, which > > > in C means that you invoke the function PyObject_CallObject(). A basic > > > example is shown here: > > > > > > https://docs.python.org/2/extending/embedding.html#pure-embedding > > > > > > > Or, better: > > > > https://docs.python.org/3/extending/embedding.html#pure-embedding > > > > ChrisA > > I need to call a .so file, but I don't know I way to do that with PyObject. I > can only seem to bring in .py files I have vastly insufficient context to be able to even attempt to answer this. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
On Thursday, July 18, 2019 at 1:46:05 PM UTC-6, Christian Gollwitzer wrote:
> Am 18.07.19 um 16:18 schrieb Jesse Ibarra:
> > On Wednesday, July 17, 2019 at 2:20:51 PM UTC-6, Christian Gollwitzer wrote:
> >> What level of integration do you want to achieve? Do you want
> >>
> >> a) to call Python functions from Smalltalk
> >> b) call Smalltalk functions from Python
> >> c) pass callbacks around, e.g. use a Smalltalk function within a Python
> >> list comprehension, and if so, which way
> >> d) integrate the class systems - derive a Python class from a Smalltalk
> >> base or the other way round
> >>
>
> >
> > For right now, I need to call a .so file from Smalltalk. I can't explicitly
> > use Python libraries since Smalltalk does not support Python. I need to use
> > the C/Python API (in Smalltalk) to create a bridge where I can call a .so
> > and a function in that .so file with a PyObject (This will be called back
> > to Smalltalk from the .so file). I can't figure out a way to do that since
> > the C/API can't call C or .so files.
> >
> > sorry for the confusion on my part
> >
>
> I still don't get it, sorry. To me it is unclear which part of the
> integration you manage to do so far, and which part is the problem.
>
> Which Smalltalk interpreter are you using? The answer to the following
> will heavily depend on that.
>
>
> Suppose I'd give you a C file with the following simple function:
>
>
> double add(double a, double b) {
> return a+b;
> }
>
> Do you know how to compile this code and make it usable from Smalltalk?
>
> One level up, consider a C function working on an array:
>
> double arrsum(int n, double *arr) {
> double sum=0.0;
> for (int i=0; i return sum;
> }
>
>
> How would you compile and link this with your Smalltalk implementation,
> such that you can pass it a Smalltalk array?
>
> Once you can do this, you can proceed to call a Python function, which
> in C means that you invoke the function PyObject_CallObject(). A basic
> example is shown here:
>
> https://docs.python.org/2/extending/embedding.html#pure-embedding
>
> Christian
I am using Visualworks 8.3. I can write that double arrsum function in
Smalltalk. No Problem.
I can also make shared library and bring those function into Smalltalk with C
(Since VW 8.3 supports C and C types). No Problem.
Smalltalk does not support Python. So I can use C/API to bring in Python
libs,types and files. No Problem
Now ,I need to bring in shared libraries using C/Python API using Smalltalk. It
seems like I can't directly bring in C shared libraries (.so files). PROBLEM.
HOW CAN I BRING THESE IN DIRECTLY?
Why do I want to call a C shared lib (.so file) using C/Python API from
Smalltalk?
Because I want to make C shared libs that bring in individual Python libraries
into Smalltalk, such as as (Scipy,NumPy,etc). That can be called using that
C/Python API in Smalltalk
--
https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
On Friday, July 19, 2019 at 8:17:43 AM UTC-6, Chris Angelico wrote: > On Sat, Jul 20, 2019 at 12:16 AM Jesse Ibarra > wrote: > > > > On Thursday, July 18, 2019 at 2:01:39 PM UTC-6, Chris Angelico wrote: > > > On Fri, Jul 19, 2019 at 5:51 AM Christian Gollwitzer > > > wrote: > > > > Once you can do this, you can proceed to call a Python function, which > > > > in C means that you invoke the function PyObject_CallObject(). A basic > > > > example is shown here: > > > > > > > > https://docs.python.org/2/extending/embedding.html#pure-embedding > > > > > > > > > > Or, better: > > > > > > https://docs.python.org/3/extending/embedding.html#pure-embedding > > > > > > ChrisA > > > > I need to call a .so file, but I don't know I way to do that with PyObject. > > I can only seem to bring in .py files > > I have vastly insufficient context to be able to even attempt to answer this. > > ChrisA Thank you for your help. If this helps. I need PyImport_Import to bring in a C shared lib. (.so file) -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
Am 19.07.19 um 16:26 schrieb Jesse Ibarra:
On Friday, July 19, 2019 at 8:17:43 AM UTC-6, Chris Angelico wrote:
On Sat, Jul 20, 2019 at 12:16 AM Jesse Ibarra
wrote:
On Thursday, July 18, 2019 at 2:01:39 PM UTC-6, Chris Angelico wrote:
On Fri, Jul 19, 2019 at 5:51 AM Christian Gollwitzer wrote:
Once you can do this, you can proceed to call a Python function, which
in C means that you invoke the function PyObject_CallObject(). A basic
example is shown here:
https://docs.python.org/2/extending/embedding.html#pure-embedding
Or, better:
https://docs.python.org/3/extending/embedding.html#pure-embedding
ChrisA
I need to call a .so file, but I don't know I way to do that with PyObject. I
can only seem to bring in .py files
I have vastly insufficient context to be able to even attempt to answer this.
ChrisA
Thank you for your help.
If this helps. I need PyImport_Import to bring in a C shared lib. (.so file)
So your question is which CPython function is used to load a compiled
Python extension (.so file). I haven't done this, but it will also be
not sufficient to import numpy or other packages. These larger packages
typically have a layer of Python code which acts as a loader and upon
import loads the corresponding .so files at runtime.
After browsing the C-API, I would try either:
PyImport_ImportModule("numpy")
or even
PyRun_SimpleString("import numpy")
If this does not work, then maybe you need to setup the correct
PYTHONPATH to point to the places where these packages are installed?
Christian
--
https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
Jesse Ibarra schrieb am 17.07.19 um 20:39: > My options seem rather limited, I need to make a Pipeline from > (Smalltalk -> C -> Python) then go back (Smalltalk <- C <- Python). > Since Smalltalk does not support Python directly I have to settle with > the C/Python API > (https://docs.python.org/3.6/extending/embedding.html#beyond-very-high-level-embedding-an-overview). > Any suggestions? First of all: don't use the C-API! :-) Use Cython instead. It's a Python-to-C compiler that covers up all the ugly little details of talking to Python from C (importing a module is just "import module", and it even adapts to different Python versions automatically). You can keep writing Python code, and at the same time trivially use external C code. https://cython.org/ http://docs.cython.org/en/latest/src/tutorial/ For embedding Python in an external program (in case you really need to do that and can't live with starting Python instead of Smalltalk), here's an example: https://github.com/cython/cython/tree/master/Demos/embed It uses the "--embed" argument to Cython that generates a C (main) function to start up the CPython runtime. Stefan -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
Sorry, I am not understanding. Smalltlak VW 8.3 does not support Python. I can only call Pyhton code through C/Python API. -- https://mail.python.org/mailman/listinfo/python-list
Re: Embedding Python in C
Jesse Ibarra writes:
> ...
> Now ,I need to bring in shared libraries using C/Python API using Smalltalk.
> It seems like I can't directly bring in C shared libraries (.so files).
> PROBLEM.
With Python, you typically do not load ("bring in") shared libraries
explicitly; instead, you simply import a module; should this module
be implemented by a shared library, it will be loaded implicitly.
Thus, once you are in the Python world, shared libraries are no
longer a problem. You told us that you already know how to
build shared libraries and call them from Smalltalk.
Thus, do this: ensure that the activated shared library initializes
the [C]Python runtime (Stefan posted a link for this), use
"Cython" to interface Python with C.
--
https://mail.python.org/mailman/listinfo/python-list
