You are not logged in.

#1 2005-12-30 20:10:56

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Simple python question

I'm learning python from a net tutorial.

The code for the first exercise is here:

      1 def buildConnectionString(params):                                     
      2     """Build a connection string from a dictionary of parameters.      
      3                                                                        
      4                                                                        
      5     Returns string."""                                                 
      6     return ";".join(["%s=%s" % (k, v) for k, v in params.items()])     
      7                                                                        
      8 if __name__ == "__main__":                                             
      9     myParams = {"server":"mpilgrim",                                  
     10             "database":"master",                                      
     11             "uid":"sa",                                               
     12             "pwd":"secret"                                            
     13             }                                                          
     14     print buildConnectionString(myParams)

This should produce "server=mpilgrim;uid=sa;database=master;pwd=secret" according to the tutorial, but mine gives me "pwd=secret;database=master;uid=sa;server=mpilgrim" . Have I made a mistake, or is the tute wrong?


.oO Komodo Dave Oo.

Offline

#2 2005-12-30 20:23:03

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple python question

Komodo wrote:

This should produce "server=mpilgrim;uid=sa;database=master;pwd=secret" according to the tutorial, but mine gives me "pwd=secret;database=master;uid=sa;server=mpilgrim" . Have I made a mistake, or is the tute wrong?

Hmm, the order's just reversed... as for that, I'm not sure if there's a way to control the order in which things are stored inside a dictionary, and frankly, it doesn't matter.  The output is right, the order is wrong, but you should never iterate over a dict anyway and expect the order to be the same...

I'd venture a guess that the hash function / storage order / whatever the crap python uses internally has changed since the tutorial was written... but I may be wrong.

Code seems good to me, I'd give it a thumbs up.

Offline

#3 2005-12-30 20:57:21

tmaynard
Member
Registered: 2005-07-29
Posts: 34

Re: Simple python question

Yes the code is good.  The author explains what is going on in more detail later on in that chapter.  Look at example 3.2  (http://diveintopython.org for those who are following along.)

Note that the new element (key 'uid', value 'sa') appears to be in the middle. In fact, it was just a coincidence that the elements appeared to be in order in the first example; it is just as much a coincidence that they appear to be out of order now.

Note  Dictionaries have no concept of order among elements. It is incorrect to say that the elements are "out of order"; they are simply unordered. This is an important distinction that will annoy you when you want to access the elements of a dictionary in a specific, repeatable order (like alphabetical order by key). There are ways of doing this, but they're not built into the dictionary.

Offline

#4 2005-12-30 21:04:08

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Simple python question

I bang my head against my desk...
dictionary = hash
list = array

why did the python language seek to change the vocabulary?
sillyness I tell you..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#5 2005-12-30 21:21:33

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Simple python question

Thanks a lot for the explanation guys, I appreciate it a lot smile


.oO Komodo Dave Oo.

Offline

#6 2005-12-30 22:16:09

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple python question

cactus wrote:

dictionary = hash
list = array

hash = map
array = vector / deque / list

Offline

#7 2005-12-30 22:22:49

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Simple python question

phrakture! No!
/me rolls up newspaper and hits phrakture with it..
No! Bad!


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#8 2005-12-30 22:44:23

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Simple python question

* /me pisses himself for about 5 minutes *


.oO Komodo Dave Oo.

Offline

#9 2005-12-30 23:00:32

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple python question

Seeeee...
containerchoice.png

Offline

#10 2005-12-30 23:18:53

Kern
Member
From: UK
Registered: 2005-02-09
Posts: 464

Re: Simple python question

silliness mode:
kern looks cactus and then at his shopping array and notices he needs some corned beef for his dictionary ! uh ?

Offline

#11 2005-12-30 23:22:12

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Simple python question

phrak.. a stack or a queue is a higher level structure.
An array "is a fundamental data structure that enables us to store and manipulate potentially huge quantities of data. An array stores an ordered sequence of values." Ordered by access (indexed).

You can use an array to implement a stack or a queue.

And a vector (growable array), is again.. a higher level structure. The compiler/vm manages the array memory for you (often by copy to larger array)...

I think we are having a "language" discrepancy.. When I speak of an "array", I am speaking of a lower level structure.. like a C array.

A "hash", is more or less an array too (this is in fact, one of the slightly higher level structures)... just one where the index is based on a hash algorithm over the contents of the thing being hashed.
The size of the 'array' index is exactly that of the available keyspace for your hash algorithm. you can make a shitty hash and decrease your keysize..

I am pretty sure most languages do 'tricks' to reduce the initial memory map of your hash.. like rehashing and gradually increasing the keysize when a collision is going to occur.

But you know all this already..probably better than I even..being Mr. C++ coder that you are.. (closer to machine code (or C) than I am most of the time) . Like I said..I think we are talking about different things..


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#12 2005-12-30 23:38:20

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple python question

Yes, I was simply trying to goad you.

cactus wrote:

I think we are having a "language" discrepancy.. When I speak of an "array", I am speaking of a lower level structure.. like a C array.

I have attempted to correct you evil ways:
http://www.parashift.com/c++-faq-lite/containers.html

An std::vector is no more functional than if you took the c "char *" and wrapped up all the "str[blah]" functions.  Everyone seems to think "zomg it's a class it will kill us all with its mighty inefficiency".  Fact of the matter is that vectors are more efficient at allocating memory than most humans are, and supports random access complexity just like an array.  Deque's, however, have slightly better access complexity, but most people just default to std::vector when you need an array.

In C++, vectors/deques are *the* low level array, and I dare you to benchmark code size of the vector class vs all the libc string functions, along with performance.

PS For the record, std::vector<char> is internally equivalent to a char[], though it does store the begin/end pointers instead of just the beginning.

]EndRant]>

edit: i was speaking entirely in the context of c++ - that flowchart is for c++ only, not an overall data structures primer

Offline

#13 2005-12-30 23:43:43

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,615
Website

Re: Simple python question

Rawr!
We both win!
/me puts his newspaper away for the time being.

Interesting to hear that a C++ stdlib vector is so "cheap" to use.
I guess if it stores the begin and end pointers, then it should indeed be just as fast...since it only need "walk" the array pointers.
Might make for a very tiny slowdown in contiguous memory reads from the vector..but memory is so damn fast...i doubt it ever matters.. i mean..
how often do you walk a full array all the time...

O(n) baby!


"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍

Offline

#14 2005-12-30 23:59:56

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple python question

cactus wrote:

Interesting to hear that a C++ stdlib vector is so "cheap" to use.

Yeah, an std::sort on an std::vector outperforms C and even, in some cases (i.e. vector<int>), will outperform fortran's sorting capabilities).

cactus wrote:

Might make for a very tiny slowdown in contiguous memory reads from the vector..but memory is so damn fast...i doubt it ever matters.. i mean..
how often do you walk a full array all the time...

Right, that's part of the reason why there's all these different containers - if you need random access ("Need to find Nth element" on the chart) vector/deque is better, but for iterating in order (over the whole thing or a subset), list is best (duh, it's a linked list).

Offline

#15 2005-12-31 00:05:15

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Simple python question

Man, you two are both insane and really knowledgeable at the same time...

Since python's so good at string manipulation, I think my first project will be a gobbledygook-slash-jokes filter that I can apply to all phrakture and cactus posts; then when all the code-related stuff is left in its raw form, I can drink up the goodness big_smile

Btw, check out this post when you've got a spare minute both of you, I'd like to know what you think (plus if you two post thepe, I'm sure the thread will get more attention wink).


.oO Komodo Dave Oo.

Offline

Board footer

Powered by FluxBB