473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another Python rookie trying to port to C/C++ question.


A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...

Am I understanding/interpreting correctly when I go with the idea that a
"dict" variable can be looked at as (effectively) two parallel arrays?
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}

Where "ValType" would be likely be a union, to allow
assigning/retrieving the actual values correctly depending on what their
individiual types were, and "<somenumbe r>" is just that: Some
more-or-less arbitrarily selected value that provides enough space to
handle all expected numbers of key/value pairs?

Similarly, would something like:

struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}

(again, ValType would probably be a union to cover each type of value
that might be wanted in the dictionary - but in this case, Key directly
contains the Key's name) then building a double-linked list of struct
DictStructs end up behaving at least reasonably like a Python "dict"?

I expect that either will be a reasonable facsimile of how Python
handles dicts, with, of course, the corresponding overhead (which may be
surprisingly little, I'm thinking... but I have yet to code that part,
so I may find out it's going to be a nightmare) of "We want the value
that goes with key, so we've got to walk the KeyArray[] until we find a
match, then extract the value from the other array", or a similar
procedure for the doubly-linked list.

Or am I way out in left field with the way I'm RTFM?

(Note 1)
Along with quite a bit of entirely unwelcome "Python evangelism" --
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of
you who took it upon yourselves to preach (including the idiot who
decided to spew a relatively impressive (as such things go) string of
insults about how stupid I am, how far up my ass my head is, and how
pathetic my family all the way back to Adam must be for producing such a
throwback because I've got no interest in adopting "the perfect
language...") at me on the merits of Python, both on group and
in-mailbox?

--
Don Bruder - da****@sonic.ne t <--- Preferred Email - SpamAssassinate d.
Hate SPAM? See <http://www.spamassassi n.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #1
12 2818
Don Bruder <da****@sonic.n et> writes:

[rewriting a Python program in C/C++]
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}
[...] Similarly, would something like:

struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}

[...] end up behaving at least reasonably like a Python "dict"?


Yes.

Except that it would be less flexible, *much* slower, and
probably much more buggy.

Thomas
Jul 18 '05 #2
Don Bruder wrote:
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...
As you seem to feel at home with C, you could have a look into
Objects/dictobject.c and Include/dictobject.h of the source distribution.
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of


Smart people use efficient tools and build on existing libraries. If you are
serious about the C++ part of your subject line, the Standard library aka
STL has a map template that resembles Python's dict and might enhance both
development and execution speed.

Off topic (or not):
A single person can start a discussion but not limit it, I think that's a
feature of newsgroups rather than an annoyance.
Peter

Jul 18 '05 #3
Don Bruder wrote:
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...
As you seem to feel at home with C, you could have a look into
Objects/dictobject.c and Include/dictobject.h of the source distribution.
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of


Smart people use efficient tools and build on existing libraries. The
Standard library aka STL has a <map> template that might enhance both
development and execution speed.

Off topic (or not):
A single person can start a discussion but not end it, I think that's a
feature of newsgroups rather than an annoyance.
Peter

Jul 18 '05 #4
"Don Bruder" <da****@sonic.n et> schrieb im Newsbeitrag
news:GS******** ************@ty phoon.sonic.net ...
I expect that either will be a reasonable facsimile of how Python
handles dicts, with, of course, the corresponding overhead (which may be
surprisingly little, I'm thinking... but I have yet to code that part,
so I may find out it's going to be a nightmare) of "We want the value
that goes with key, so we've got to walk the KeyArray[] until we find a
match, then extract the value from the other array", or a similar
procedure for the doubly-linked list.
Python hashes the keys for fast lookup.

(Note 1)
Along with quite a bit of entirely unwelcome "Python evangelism" --
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of
you who took it upon yourselves to preach (including the idiot who
decided to spew a relatively impressive (as such things go) string of
insults about how stupid I am, how far up my ass my head is, and how
pathetic my family all the way back to Adam must be for producing such a
throwback because I've got no interest in adopting "the perfect
language...") at me on the merits of Python, both on group and
in-mailbox?


You are treated as you treat others.
It might suprise you but this is a newsgroup not your personal information
lookup place.
Jul 18 '05 #5
In article <bk************ *@news.t-online.com>,
Peter Otten <__*******@web. de> wrote:
Don Bruder wrote:
A week or two ago, I asked here about porting Python to C. Got some good
answers (Note 1) but now I've got another question. Actually, more a
request for clarification of a topic that both the Python tutorial and
docs leave a touch murky to my understanding.

Dictionaries/"dict" types...
As you seem to feel at home with C, you could have a look into
Objects/dictobject.c and Include/dictobject.h of the source distribution.


I may just go poking around there for more details.
Someone (via email) suggests that dicts are "supposed to be" (my
synopsis, not his words) a bit "murky", because all that's supposed to
be relevant is stuffing things into them, and pulling things out. That's
fine, completely admirable, and fully acceptable. *IF* You're working
directly in Python. I'm not. I'm working in a combination of C and C++,
attempting to duplicate the functionality of a program originally
written in Python. As such, the details *MUST* get clear if I'm to
create equivalent functionality. If only because I need to read data
that was written (presumably...) by a Python program. Hence, it becomes
neccesary to get down "in the muck" and actually know what's going on
behind the nice, tidy "dict.whosit.ge t()" (or whatever other operators
might be involved) interface that Python provides.
Which part of "I don't care how good, bad, or indifferent Python is
compared to C/C++, or any other language. I want this Python code to be
written in C/C++, end of discussion." wasn't clear enough for those of
Smart people use efficient tools and build on existing libraries.


There's a statement that needed saying.... Not...

If you are
serious about the C++ part of your subject line, the Standard library aka
STL has a map template that resembles Python's dict and might enhance both
development and execution speed.
I'm *REASONABLY* serious. This port started life as a "Do you really
know as much about C++ as you think you do? Prove it! Take this Python
program and make it C++!" sort of "mid-term" test in my self-taught C++
course. It's rapidly degenerating (due to some technicalities of C++
that I've never before encountered, and therefore had no expectation of
trouble from) into "Let's just get the damn thing working in straight C,
then worry about trying to turn it into C++ later."

Off topic (or not):
A single person can start a discussion but not limit it, I think that's a
feature of newsgroups rather than an annoyance.


Perhaps, and perhaps not. Guess it depends on your perspective. Having a
stream of insult and invective dumped into your mailbox because you've
made it plain that you don't consider somebody's "pet" language the
be-all and end-all of computer programming is hardly what I'd call a
"feature".. .

Be that as it may, fortunately I have a thick enough hide to
figuratively "flip off" the jerk that was involved, and go on with life
knowing full well that in at least one respect, I'm so far superior to
him that words can't describe the gulf between us. Doesn't mean I'm not
going to mention that I think he's a hopeless putz, but I'm sure not
going to let it get me down to the point of giving up.

--
Don Bruder - da****@sonic.ne t <--- Preferred Email - SpamAssassinate d.
Hate SPAM? See <http://www.spamassassi n.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #6
In article <8y**********@p ython.net>,
Thomas Heller <th*****@python .net> wrote:
Don Bruder <da****@sonic.n et> writes:

[rewriting a Python program in C/C++]
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}

[...]
Similarly, would something like:

struct DictStruct
{
char Key[25];
ValType Value;
DictStruct *PrevDictEntry;
DictStruct *NextDictEntry;
}

[...] end up behaving at least reasonably like a Python "dict"?


Yes.

Except that it would be less flexible, *much* slower, and
probably much more buggy.

Thomas


Well, since I'm not worried about "flexible", have my doubts about the
"much slower" part, and have no reason to believe that choice of
language automatically makes a routine buggy, I'll go with the tenative
plan I had. Seems you're confirming that I'm understanding things
properly when it comes to "This is what a dict is, and here's how it
works." Now to put finishing touches on my understanding of exactly what
goes on inside that "black box" that is the "dict"
class/type/whatever-it-is-you-Python-types-like-to-call-such-things, and
make some working code out of it...

--
Don Bruder - da****@sonic.ne t <--- Preferred Email - SpamAssassinate d.
Hate SPAM? See <http://www.spamassassi n.org> for some seriously great info.
I will choose a path that's clear: I will choose Free Will! - N. Peart
Fly trap info pages: <http://www.sonic.net/~dakidd/Horses/FlyTrap/index.html>
Jul 18 '05 #7
On Thu, 25 Sep 2003 18:55:34 GMT, Don Bruder <da****@sonic.n et> wrote:
Dictionaries/"dict" types...

Am I understanding/interpreting correctly when I go with the idea that a
"dict" variable can be looked at as (effectively) two parallel arrays?
Something like this C construct:

struct DictStruct
{
char *KeyArray[<somenumber>];
ValType ValArray[<somenumber>];
}


Sort of, in a sense, but not quite. The types of items within the
dictionary (both key and value) are not constrained. This is no
problem as all Python objects are held using a reference scheme. So
(assuming that a pair-object scheme is used in the dictionary) the
struct will be defined much like...

struct DictPair
{
PyObject *Key;
PyObject *Data;
}

The items will almost certainly be held in a single 'array' of pair
objects.

The Python dictionaries use hashing. I don't know the details of the
hashing used. Anyway, there are many ways to handle dictionary-like
data structures.

C++ has the std::map template, which uses a form of binary tree called
a red-black tree (the red-black refers to a partial balancing system).

Tree-based mappings can be slightly more flexible than hashing (for
instance, a tree can be 'walked' in sorted order - the hashing process
does not preserve relative ordering) but hashing tends to be faster.

An interesting approach to dictionary-like objects is the ternary
tree. This is rather like cascading binary trees - each subtree
identifies one character and cascades into the subtree that finds the
next character. The third link in the 'ternary' is the 'I've
identified one character and moving on to the next' link.

A ternary tree can be faster than hashing - in particular at deciding
that a particular key is not present (hashing needs to calculate a
hash over the whole tree before looking up the result - a ternary tree
search may fail at the first character, without looking at the whole
key).

Digital trees (or tries, IIRC) can be even faster still - but wastes a
lot of memory. In a digital tree, each node has an array subscripted
by character/digit code of pointers to the next subtree.

Multiway trees (most often used on disk for database indexes - B trees
and B+ trees being types of multiway trees) may well be appropriate in
main store on modern desktop machines with caching and virtual memory.

If these options are just too complicated, sorted arrays can be
extremely effective as long as you don't have too many items. The C++
library includes std::vector (a resizable array) and binary search
algorithms which can ease the implementation.
Odds are, if you are translating Python to C++, you should use an
std::map to replace a dictionary. There are some different
assumptions, though. Dictionaries assume that the key is hashable, but
don't require relative comparisons ('<', '<=' etc). The std::map
requires relative comparisons.

This simply arises out of the different approaches - Python using
hashing and C++ using binary trees. Going from Python to C++, 99 times
out of 100 this is not a problem. From C++ to Python there is the
minor issue that Python dictionaries can't be _directly_ iterated in
sorted order, though there are certainly easy ways around that.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #8
On Thu, 25 Sep 2003 15:19:00 -0700, Brian Quinlan <br***@sweetapp .com>
wrote:
Well, since I'm not worried about "flexible", have my doubts about the
"much slower" part,


Your implementation is O(n) with the number of entries while the Python
implementati on is O(log(n)). If n is small then your implementation
might be acceptably fast. Can't you just use the STL hash_map template
class?


Are you sure Python dictionaries are O(log n)? - Remember, they are
based on hashing.

Simple hashing is O(1). Handling of collisions and stuff will affect
that (and I'm no expert on hashing techniques) but I can't help
wondering if you're confusing Pythons hashing with tree-based
techniques.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #9
On Thu, 25 Sep 2003, Don Bruder wrote:
[...] As such, the details *MUST* get clear if I'm to create equivalent
functionality. If only because I need to read data that was written
(presumably...) by a Python program. Hence, it becomes neccesary to get
down "in the muck" and actually know what's going on behind the nice,
tidy "dict.whosit.ge t()" (or whatever other operators might be involved)
interface that Python provides.


is this data in 'pickle' format (as opposed to text, XML, or some specific
binary format)? if it is, reading it in C/C++ is going to be very hard
indeed; you might consider writing a small python program to read it and
convert it to something more tractable. if it isn't, you don't need to
'get down in the muck'; you just need to write a C program to do the job
that needs to be done.

if pickling is entirely new to you, then your data probably isn't pickled,
so you don't need to worry about it.
Off topic (or not): A single person can start a discussion but not
limit it, I think that's a feature of newsgroups rather than an
annoyance.


Perhaps, and perhaps not. Guess it depends on your perspective. Having a
stream of insult and invective dumped into your mailbox because you've
made it plain that you don't consider somebody's "pet" language the
be-all and end-all of computer programming is hardly what I'd call a
"feature".. .


i'm sorry that happened; i hope you won't think badly of python and
pythoneers because of one moron. discreetly let the relevant people know
who he is and we'll fetch the Comfy Chair ... 8)

tom

--
Things fall apart - it's scientific

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1515
by: Don Bruder | last post by:
Greetings, oh scaly ones... :) I'm a Mac user with a fairly high level of computer literacy, including reasonable programming skills in BASIC, C, Pascal, and several flavors of ML, just trying to run a "canned" Python... Uhhh... Program? Script? Module? - Yeesh... I'm such a rookie at Python I'm not even sure of the right terminology to use...
3
2271
by: Michael Sparks | last post by:
Hi, I'm posting a link to this since I hope it's of interest to people here :) I've written up the talk I gave at ACCU Python UK on the Kamaelia Framework, and it's been published as a BBC R&D White Paper and is available here: * http://www.bbc.co.uk/rd/pubs/whp/whp113.shtml
8
1303
by: Daniel Bickett | last post by:
This post started as an incredibly long winded essay, but halfway through I decided that was a terribly bad idea, so I've trimmed it down dramatically, and put it in the third person (for humor's sake). Once upon a time a boy named Hypothetical programmed in PHP and made many a web application. It would be a long while before he would...
7
1713
by: clintonG | last post by:
I'm puzzled and don't think this is possible but if an application that is running on websiteA generates a file can FTP be used from websiteA to transfer that file to websiteB which would be located on another server? -- <%= Clinton Gallagher NET csgallagher AT metromilwaukee.com URL http://www.metromilwaukee.com/clintongallagher/
12
6983
by: Arash Partow | last post by:
Hi all, I've ported various hash functions to python if anyone is interested: def RSHash(key): a = 378551 b = 63689 hash = 0
206
8176
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of...
3
1872
by: chernevik | last post by:
Here is a newbie question: how do I get the server examples in the Preview chapter of "Progamming Python" (Lutz) to run? The code is supposed to be a little webserver on which to run examples, but when I run it it I get "permission denied". Running it as root gets "address already in use". Here is the code (it's example 2.32); comments...
15
3192
by: kyosohma | last post by:
Hi, I am trying to get a small group of volunteers together to create Windows binaries for any Python extension developer that needs them, much like the package/extension builders who volunteer their time to create Linux RPMs. The main thing I need are people willing to test the binaries to make sure the extension is stable. This would...
31
1756
by: raylopez99 | last post by:
I went through a bunch of Regex examples, and indeed it's quite powerful, including 'groups' using 'matches', word boundaries, lookahead matches, replacing and splitting text,etc--apparently there's a whole book on it, though I just used the chapter in Albahari et al C#3 in a Nutshell (a great book). You can indeed 'massage' a string into a...
0
7475
marktang
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...
0
7664
Oralloy
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. ...
0
7918
jinu1996
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...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
agi2029
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...
1
5341
isladogs
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...
0
4958
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...
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.