I'm actually curious if there's a way to write a generator function
(not a generator expression) in C, or what the simplest way to do it
is... besides link the Python run-time. 14 1210
On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
I'm actually curious if there's a way to write a generator function
(not a generator expression) in C, or what the simplest way to do it
is... besides link the Python run-time.
The reference implementation of Python is written in C, so obviously there
must be a way to write something like generators in C.
Ciao,
Marc 'BlackJack' Rintsch
En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <bj****@gmx.net escribió:
On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
>I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time.
The reference implementation of Python is written in C, so obviously there
must be a way to write something like generators in C.
Yes and no. Generators are tied to frames, and frames execute Python code, not C. There is no simple way to write generators in C, but there are some generator-like examples in the itertools module.
See this thread http://groups.google.com/group/comp....2f72f2d0e88fc/
--
Gabriel Genellina
On Sun, May 4, 2008 at 1:39 AM, <ca********@gma il.comwrote:
I'm actually curious if there's a way to write a generator function
(not a generator expression) in C, or what the simplest way to do it
is... besides link the Python run-time.
--
Here's the itertools C code: http://svn.python.org/view/python/tr...34&view=markup
David.
On May 4, 12:21*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <bj_...@gmx.net escribió:
On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
I'm actually curious if there's a way to write a generator function
(not a generator expression) in C, or what the simplest way to do it
is... besides link the Python run-time.
The reference implementation of Python is written in C, so obviously there
must be a way to write something like generators in C.
Yes and no. Generators are tied to frames, and frames execute Python code,not C. There is no simple way to write generators in C, but there are some generator-like examples in the itertools module.
See this threadhttp://groups.google.c om/group/comp.lang.pytho n/browse_thread/thread/...
--
Gabriel Genellina
Gabriel,
How did your attempt turn out from last May? At first look, it's
outside the scope of Python, but it is not the scope of C
necessarily. Generators offer a lot of simplicity (which I haven't
read about extensively, but am starting to see) that could gain some
reputation for Python. What is the midpoint at which C could meet
Python?
There is no such thing as a 'frame' per se in C; byte code is
integral. As there is no such thing as suspended state without
frames, and no such thing as generators without suspended state. It's
a hard thing to Google for without knowing the prior terminology for
the work that's already been done on them in C. What work is there?
Are any devs interested in pursuing it?
The frame implementation. http://svn.python.org/projects/pytho.../frameobject.h http://svn.python.org/projects/pytho.../frameobject.c
The generator code. http://svn.python.org/projects/pytho...de/genobject.h http://svn.python.org/projects/pytho...ts/genobject.c
I used Microsoft's search engine (python frame generator
site:svn.python .org , links 3 and 5) to find it.
En Sun, 04 May 2008 08:11:35 -0300, <ca********@gma il.comescribió:
On May 4, 12:21*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <bj_...@gmx.net escribió:
On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
>I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time.
The reference implementation of Python is written in C, so obviously there
must be a way to write something like generators in C.
Yes and no. Generators are tied to frames, and frames execute Python code, not C. There is no simple way to write generators in C, but there are some generator-like examples in the itertools module. See this threadhttp://groups.google.c om/group/comp.lang.pytho n/browse_thread/thread/...
Gabriel,
How did your attempt turn out from last May? At first look, it's
outside the scope of Python, but it is not the scope of C
necessarily. Generators offer a lot of simplicity (which I haven't
read about extensively, but am starting to see) that could gain some
reputation for Python. What is the midpoint at which C could meet
Python?
There is no such thing as a 'frame' per se in C; byte code is
integral. As there is no such thing as suspended state without
frames, and no such thing as generators without suspended state. It's
a hard thing to Google for without knowing the prior terminology for
the work that's already been done on them in C. What work is there?
Are any devs interested in pursuing it?
The frame and generator implementations are very tightly coupled to Python code, they aren't useful for implementing generators in C. Don't bother to read them.
Unfortunately, converting a C function into something like a generator isn't as easy as using the "yield" statement... Although the idea is simple, the implementation may be hard sometimes: You have to wrap the function within an object, maintain all state information into that object, and do all computation in the "next" method. Also, __iter__ should return itself so it can be called as an iterator.
(those objects are sometimes called "functors" <http://en.wikipedia.or g/wiki/Function_object not the same meaning as functors in Mathematics)
All examples that I have at hand are propietary code so I can't post them. The itertools module may be used as reference - "cycle" and "chain" are the easiest I think, although they might be *too* easy to understand the structure. "groupby" is a more complex example but at the same time harder to understand. See http://svn.python.org/projects/pytho...rtoolsmodule.c
Ok, I'll try to use Python code as an example. A generator for Fibonacci numbers:
def fibo():
a = b = 1
while True:
a, b = b, a+b
yield b
We can convert that function into this object; it should be written in C, not Python, but the idea is the same:
class fibo:
def __init__(self):
self.a = 1
self.b = 1
def next(self):
temp = self.a + self.b
self.a = self.b
self.b = temp
return temp
def __iter__(self):
return self
It behaves exactly the same as the generator above; we can even use the same code to test it:
pyfor n in fibo():
.... if n>100: break
.... print n
....
2
3
5
8
13
21
34
55
89
Converting that class into C code should be straightforward . And then you have a generator-like function written in C.
--
Gabriel Genellina
On May 4, 8:11*am, castiro...@gmai l.com wrote:
On May 4, 12:21*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sun, 04 May 2008 01:08:34 -0300, Marc 'BlackJack' Rintsch <bj_...@gmx..ne tescribió:
On Sat, 03 May 2008 16:39:43 -0700, castironpi wrote:
>I'm actually curious if there's a way to write a generator function
>(not a generator expression) in C, or what the simplest way to do it
>is... besides link the Python run-time.
The reference implementation of Python is written in C, so obviously there
must be a way to write something like generators in C.
Yes and no. Generators are tied to frames, and frames execute Python code, not C. There is no simple way to write generators in C, but there are some generator-like examples in the itertools module.
See this threadhttp://groups.google.c om/group/comp.lang.pytho n/browse_thread/thread/...
--
Gabriel Genellina
Gabriel,
How did your attempt turn out from last May? *At first look, it's
outside the scope of Python, but it is not the scope of C
necessarily. *Generators offer a lot of simplicity (which I haven't
read about extensively, but am starting to see) that could gain some
reputation for Python. *What is the midpoint at which C could meet
Python?
There is no such thing as a 'frame' per se in C; byte code is
integral. *As there is no such thing as suspended state without
frames, and no such thing as generators without suspended state. *It's
a hard thing to Google for without knowing the prior terminology for
the work that's already been done on them in C. *What work is there?
Are any devs interested in pursuing it?
The frame implementation.
http://svn.python.org/projects/pytho.../frameobject.c
The generator code.
http://svn.python.org/projects/pytho...ts/genobject.c
I used Microsoft's search engine (python frame generator
site:svn.python .org , links 3 and 5) to find it.
Isn't this guy a bot ? :-) It's learning fast. I believe there is a
"frame" in C, composed of its stack and globals. For generators in C,
you may look for "coroutines ". For example, see: http://www.chiark.greenend.org.uk/~s...oroutines.html
A sample code follows:
#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void) {
static int i;
crBegin;
for (i = 0; i < 10; i++)
crReturn(1, i);
crFinish;
}
The "suspended state" is saved in the static variable. It's not
necessary to save the complete "frame", only the suspended state.
A 'generator function' -- a function that when called returns a generator
object, a specific type of iterator, is a rather Python specific concept.
Better to think, I think, in terms of writing an iterator 'class' (in C,
struct with associated function). Looking at the implementation of two of
the itertools in the link below (previously posted), I could see the
'template' that each was based on and that would be the basis for writing
another. (And I have not programmed C for a decade. Raymond's code is
quite clear.)
=============== =============== ===
[Outlook Express does not 'quote' this post properly]
"Gabriel Genellina" <ga*******@yaho o.com.arwrote in message
news:op******** *******@a98gizw .cpe.telecentro .net.ar...
The itertools module may be used as reference - "cycle" and "chain" are
the easiest I think, although they might be *too* easy to understand the
structure. "groupby" is a more complex example but at the same time harder
to understand. See http://svn.python.org/projects/pytho...rtoolsmodule.c
Ok, I'll try to use Python code as an example. A generator for Fibonacci
numbers:
def fibo():
a = b = 1
while True:
a, b = b, a+b
yield b
We can convert that function into this object; it should be written in C,
not Python, but the idea is the same:
class fibo:
def __init__(self):
self.a = 1
self.b = 1
def next(self):
temp = self.a + self.b
self.a = self.b
self.b = temp
return temp
def __iter__(self):
return self
=============== ==============
Terry again: one can think of a generator function as an abbreviation of an
iterator class. Calling the gen. func. produces an iterator instance just
like calling the class object.
=============== ==============
[back to Gabriel]
It behaves exactly the same as the generator above; we can even use the
same code to test it:
pyfor n in fibo():
.... if n>100: break
.... print n
....
2
3
5
8
13
21
34
55
89
Converting that class into C code should be straightforward . And then you
have a generator-like function written in C.
=============== =============== ====
En Mon, 05 May 2008 00:09:02 -0300, hdante <hd****@gmail.c omescribió:
Isn't this guy a bot ? :-) It's learning fast. I believe there is a
"frame" in C, composed of its stack and globals. For generators in C,
you may look for "coroutines ". For example, see:
http://www.chiark.greenend.org.uk/~s...oroutines.html
A sample code follows:
#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void) {
static int i;
crBegin;
for (i = 0; i < 10; i++)
crReturn(1, i);
crFinish;
}
The "suspended state" is saved in the static variable. It's not
necessary to save the complete "frame", only the suspended state.
Quoting the author himself, "this is the worst piece of C hackery ever seen"
--
Gabriel Genellina
On May 5, 12:28*am, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Mon, 05 May 2008 00:09:02 -0300, hdante <hda...@gmail.c omescribió:
*Isn't this guy a bot ? :-) It's learning fast. I believe there is a
"frame" in C, composed of its stack and globals. For generators in C,
you may look for "coroutines ". For example, see:
http://www.chiark.greenend.org.uk/~s...oroutines.html
*A sample code follows:
#define crBegin static int state=0; switch(state) { case 0:
#define crReturn(i,x) do { state=i; return x; case i:; } while (0)
#define crFinish }
int function(void) {
* * static int i;
* * crBegin;
* * for (i = 0; i < 10; i++)
* * * * crReturn(1, i);
* * crFinish;
}
*The "suspended state" is saved in the static variable. It's not
necessary to save the complete "frame", only the suspended state.
Quoting the author himself, "this is the worst piece of C hackery ever seen"
--
Gabriel Genellina- Hide quoted text -
- Show quoted text -
At some point, code goes "on" and "off" the processor, which knowledge
I do owe to spending money. Thus, if the group news is a localcy
(other dimension of currency), that's bounce check the house dollar.
What do [second person plural] spend on [vernacular]?
More strictly speaking, processor freely goes on and off binaries. Is
Goto a hardware instruction? Perhaps a Linux-Python-binding could
yield a frame, pun intended. Do [you] have access the file system?
Could yield merely be implemented as multiple strings of processses?
Get the variables on disk. Question is, is it worth optimizing away
from flash rom? There's a gig of chips in a bowling ball. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Raymond Hettinger |
last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289.
That led to a number of contributors helping to re-work the pep details
into a form that has been well received on the python-dev list:
http://www.python.org/peps/pep-0289.html
In brief, the PEP proposes a list comprehension style syntax for
creating fast, memory efficient generator expressions on the fly:
sum(x*x for x in roots)
|
by: Francis Avila |
last post by:
A little annoyed one day that I couldn't use the statefulness of
generators as "resumable functions", I came across Hettinger's PEP 288
(http://www.python.org/peps/pep-0288.html, still listed as open, even
though it's at least a year old and Guido doesn't seem very hot on the
idea). I'm not too sure of its ideas on raising exceptions in
generators from outside (although it looks like it might be convenient
in some cases), but being able...
|
by: Mahesh Padmanabhan |
last post by:
Hi,
When list comprehension was added to the language, I had a lot of
trouble understanding it but now that I am familiar with it, I am not
sure how I programmed in Python without it.
Now I see that generator expressions have been added to the language
with 2.4 and I question the need for it. I know that it allows for lazy
evaluation which speeds things up for larger lists but why was it
necessary to add it instead of improving list...
|
by: Joh |
last post by:
hello,
i'm trying to understand how i could build following consecutive sets
from a root one using generator :
l =
would like to produce :
, , , ,
|
by: Jonathan Burd |
last post by:
Greetings everyone,
Here is a random string generator I wrote for an application
and I'm wondering about the thread-safety of this function.
I was told using static and global variables cause
potential problems for thread-safety. So far, I'm only confused.
I need a proper explanation for the concept so I can understand how
to write thread-safe functions in the future.
My apologies for posting a long routine.
| |
by: bahadir.balban |
last post by:
Hi,
What's the best way to implement an overloaded function in C? For
instance if you want to have generic print function for various
structures, my implementation would be with a case statement:
void print_structs(void * struct_argument, enum struct_type stype)
{
switch(stype) {
case STRUCT_TYPE_1:
|
by: David |
last post by:
Greetings all,
I am having a problem with an asp.net application using C# which is a
thumbnail generator. Here is the code used...
http://www.eggheadcafe.com/articles/20030515.asp
It works fine for .jpg images but for some reason it doesn't work with .gif
images. The author of the article says it works fine for him but I can't get
it to work. I was wondering if anyone here can shed some light?
|
by: Petr Jakes |
last post by:
Hello,
I am trying to study/understand OOP principles using Python. I have
found following code http://tinyurl.com/a4zkn about FSM (finite state
machine) on this list, which looks quite useful for my purposes. As
this code was posted long time ago (November 1998) I would like to ask
if the principles used in this code are still valid in the "modern"
Python and if/how it can be improved (revrited) using futures of
current version of...
|
by: TTCEric |
last post by:
This will be original. I promise.
I cannot get the random number generator to work.
I tried seeding with Date.Now.Milliseconds, it still results in the same values.
What I have are arrays of values.
I get a random index value for each array so I can pull the data from them.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |