New to Python: Do we have the concept of Hash in Python?

A

A.M

Hi,



I am new to Python, with C#/Java background



Is there any built-in Hash implementation in Python? I am looking for a
container that I can access to it's items by name. Something like this:



Print container["memeberName"]





I am asking this because I learned that DB-API in Python doesn't offer
access to cursor columns by name. The only option is access by index. I hope
that I've got it wrong!



I learned Ruby perfectly supports that.





Thank you,

Alan
 
I

infidel

Is there any built-in Hash implementation in Python? I am looking for a
container that I can access to it's items by name. Something like this:

Print container["memeberName"]

You obviously haven't read the tutorial, they're called "dictionaries"
in Python
I am asking this because I learned that DB-API in Python doesn't offer
access to cursor columns by name. The only option is access by index. I hope
that I've got it wrong!

That's because the DB-API uses tuples to represent records.
I learned Ruby perfectly supports that.

Yay for Ruby.
 
B

Brian Quinlan

A.M said:
Is there any built-in Hash implementation in Python? I am looking for a
container that I can access to it's items by name. Something like this:

Print container["memeberName"]

d = {"memberName" : "some value"}
print d["memberName"]
I am asking this because I learned that DB-API in Python doesn't offer
access to cursor columns by name. The only option is access by index. I hope
that I've got it wrong!

I learned Ruby perfectly supports that.

Python does not use column names by default because they are not
portable across database implementations. That being said, you can get
them easily, if you want. Look at the "description" attribute of your
cursor instance.

Cheers,
Brian
 
J

Jeffrey Froman

A.M said:
I am asking this because I learned that DB-API in Python doesn't offer
access to cursor columns by name. The only option is access by index. I
hope that I've got it wrong!

While it's not part of the DB-API as far as I know, the MySQLdb package (and
perhaps other DB access modules as well, I don't know) provides
a "DictCursor" class that returns query results as a tuple of column-keyed
dictionaries (hashes, already demonstrated by Brian).


Jeffrey
 
F

Fredrik Lundh

A.M said:
I am new to Python, with C#/Java background

that's not really much of an excuse for not reading *any* Python tutorial before
you jump in...
I am asking this because I learned that DB-API in Python doesn't offer access to cursor columns by
name. The only option is access by index. I hope that I've got it wrong!

several people have already told you how to deal with this in Python. please see
the followups to your earlier post.
I learned Ruby perfectly supports that.

Ruby's DB interface, perhaps. Python's standard DB-API doesn't support this
by default, for portability and performance reasons. that doesn't mean that Python
don't have "hashes", just that the DB-API doesn't provide dictionary-style access
to columns.

see the followups to your earlier post for more details, and ways to handle this.

(a much better way to deal with this is of course to *name* the variables in your
select statements, so 1) the database engine can spot errors early on, and 2) it
won't have to fetch data that you don't really need).

</F>
 
A

A.M

Fredrik Lundh said:
that's not really much of an excuse for not reading *any* Python tutorial
before
you jump in...

Hi Fredrik,

1st of all, I am really impressed by this Python community. Answers are
helpful and I am having excellent progress. I appreciate everybody's help.



This is my 1st day that I am seriously diving into Python and I have to
finish this application by the end of today. Maybe it wasn't a good idea to
choose the language that I don't know when I have to deliver my work in such
short time. I understand that my question might seems very trivial to you,
but please consider the fact that I am in time pressure and I cannot go
through a 400 book today. I promises I'll do that this weekend ;) Wish luck
for me!



Thank you for your post,

Alan
 
G

gregarican

Lemme see, starting *and* finishing a project in a language you've
never practically used before within a day's time? Sounds like a clip
from next season's opener of the TV show '24' to me.

I came from using Ruby about a year or so and even then it took a
couple of days of browsing through the Python docs and playing around
until I could consider myself somewhat useful. Coming from Java and C#
might make the departure a little steeper. Good luck!
 
S

skip

Alan> but please consider the fact that I am in time pressure and I
Alan> cannot go through a 400 book today.

The tutorial is online and not 400 pages long:

http://www.python.org/doc/current/tut/tut.html

It's got a pretty comprehensive table of contents, looking at which you
might jump quickly to Chapter 5, "Data Structures", and quickly eliminate
lists, sets and maybe tuples as possible synonyms for "hash" and investigate
Section 5.5, "Dictionaries" more closely. The first sentence of that
section should ring bells:

Another useful data type built into Python is the
dictionary. Dictionaries are sometimes found in other languages as
``associative memories'' or ``associative arrays''.

I suspect that was what Fredrik was thinking you might reasonably have done
before asking on the list. In addition, you could have probably answered
your own question faster by using the online documentation instead of
waiting for the Q&A round-trip to comp.lang.python.

Skip
 
F

Fredrik Lundh

A.M said:
This is my 1st day that I am seriously diving into Python and I have to
finish this application by the end of today. Maybe it wasn't a good idea to
choose the language that I don't know when I have to deliver my work in such
short time.

are your boss aware of this ?

</F>
 
A

A.M

Fredrik Lundh said:
are your boss aware of this ?

</F>


are your boss aware of this ?

What is wrong with *this*?
Yes, they are aware of *this*.
The application is not mission critical system. It is just a simple
reporting tool.
 
A

A.M

Fredrik Lundh said:
are your boss aware of this ?

</F>


are your boss aware of this ?

In fact my boss is quite impressed with my progress so far.



I am a little confused about the fact that you got frustrated with my posts
today. I am not asking for a big tutorial or

deepest philosophy behind the concepts. The answer to this post could be
just the word "Dictionary" which is 10 key stroke !

Does this hurt?
 
G

gregarican

Dear A.M.,

The day is complete. My TPS reports still aren't on my desk. Either
with or without cover sheets. Not a good time to try to teach yourself
a new language. Please gather your belongings and move down to basement
storage C.

That'd be great,
Bill Lumberg
 
A

A.M

Well the work is done now. I arrived home at 10:30 PM.



The application takes data from Oracle 10g stored procedures and renders
data to both HTML and CSV formats. The HTML reports have summary and
grouping sections and the HTML files are Excel friendly. The application is
modular and consists of reusable modules. The application is called every
night by scheduled task. The CSV and HTML files are being deployed to Apache
for internal employee access.



This isn't a complex and big system. Tomorrow, I'll add the email
functionality to the application and re-use it's components for more
reports.



Now that I finished the 1st part of the application, I have to admit that
choosing Python was an excellent decision. Python is an awesome programming
language with comprehensive library and great community support.



I am not that new to Python anymore.. I had a productive day. Thanks
everybody for help.
 
P

Piet van Oostrum

A.M said:
AM> This is my 1st day that I am seriously diving into Python and I have
AM> to finish this application by the end of today.

Are you serious?
 
S

Sion Arrowsmith

gregarican said:
I came from using Ruby about a year or so [ ... ]

That's an interesting way round. Why did you consider Python if
you already knew Ruby, and which is now your preferred language?
(I've no interest in learning Ruby, but from what I've seen of it
I similarly can't imagine what would motivate me to learn Python.)
 
A

A.M

Sion Arrowsmith said:
gregarican said:
I came from using Ruby about a year or so [ ... ]

That's an interesting way round. Why did you consider Python if
you already knew Ruby, and which is now your preferred language?
(I've no interest in learning Ruby, but from what I've seen of it
I similarly can't imagine what would motivate me to learn Python.)

--
\S -- (e-mail address removed) -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the
other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

My decision wasn't based on which language I am most comfortable with. I can
do the same application in Java in one hour. We are looking for a scripting
language to use across our organization. This is a strategic decision. Out
1st choice is Python, the second one is Perl and the last one is Ruby.
Yesterday I proof that Python is way to go.



The programming language aspects of Ruby is a brilliant, but the language
itself is not everything. I encountered some problem with Ruby and I found
it is not there yet. Here is some of problems I have with ruby: (consider
the fact that it is my personal opinion)



1) Ruby community feel that there is strong database access support. I
personally didn't find DBI distribution and documentation very organized and
at the production level. For example, DBI:OCI8 is at experimental state.
http://www.jiubao.org/ruby-oci8/ Oracle data access support is very
important for me and DBI:OCI8 is the only way you can get data from Oracle
stored procedures.

2) Komodo (my favorite scripting IDE) crashes when I debug certain type
of Ruby scripts. Moreover there is no ActiveRuby from ActiveState **yet**.

3) You can find lots of excellent Python books from OREILLY. You don't
have much option for Ruby **yet**.

4) Ruby became famous because of Rails. Rails is a great idea, but I am
so focused on ASP.NET and J2EE and I am going to stay there. Rails doesn't
have a production level support for IIS. So I am not very interested in
Rails.



In essence, Ruby language is the best, but Ruby platform is too young for
me. I'll give Ruby another two years and come back to it again.



I found the Python language quite powerful and easy. With mature and strong
community support.
 
R

Ravi Teja

A.M said:
In fact my boss is quite impressed with my progress so far.



I am a little confused about the fact that you got frustrated with my posts
today. I am not asking for a big tutorial or

deepest philosophy behind the concepts. The answer to this post could be
just the word "Dictionary" which is 10 key stroke !

Does this hurt?

IRC is a better place to request 10 key stroke answers. And it is
faster for you to get answers there too. Usenet is archived these days
and it adds value to all of us that it is filled with discussions of
questions that are more intellectual than those which can be looked up
at a glance at the documentation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top