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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • A.M

    #1

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

    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["memeberNam e"]





    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


  • infidel

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

    > Is there any built-in Hash implementation in Python? I am looking for a[color=blue]
    > container that I can access to it's items by name. Something like this:
    >
    > Print container["memeberNam e"][/color]

    You obviously haven't read the tutorial, they're called "dictionari es"
    in Python
    [color=blue]
    > 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![/color]

    That's because the DB-API uses tuples to represent records.
    [color=blue]
    > I learned Ruby perfectly supports that.[/color]

    Yay for Ruby.

    Comment

    • Brian Quinlan

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

      A.M wrote:[color=blue]
      > 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["memeberNam e"][/color]

      d = {"memberName " : "some value"}
      print d["memberName "]
      [color=blue]
      > 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.[/color]

      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 "descriptio n" attribute of your
      cursor instance.

      Cheers,
      Brian

      Comment

      • Jeffrey Froman

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

        A.M wrote:
        [color=blue]
        > 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![/color]

        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

        Comment

        • Fredrik Lundh

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

          "A.M" wrote:
          [color=blue]
          > I am new to Python, with C#/Java background[/color]

          that's not really much of an excuse for not reading *any* Python tutorial before
          you jump in...
          [color=blue]
          > 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![/color]

          several people have already told you how to deal with this in Python. please see
          the followups to your earlier post.
          [color=blue]
          > I learned Ruby perfectly supports that.[/color]

          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>



          Comment

          • A.M

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


            "Fredrik Lundh" <fredrik@python ware.com> wrote in message
            news:mailman.64 14.1149178232.2 7775.python-list@python.org ...[color=blue]
            > "A.M" wrote:
            >[color=green]
            >> I am new to Python, with C#/Java background[/color]
            >
            > that's not really much of an excuse for not reading *any* Python tutorial
            > before
            > you jump in...[/color]

            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



            Comment

            • gregarican

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

              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!

              A.M wrote:[color=blue]
              > "Fredrik Lundh" <fredrik@python ware.com> wrote in message
              > news:mailman.64 14.1149178232.2 7775.python-list@python.org ...[color=green]
              > > "A.M" wrote:
              > >[color=darkred]
              > >> I am new to Python, with C#/Java background[/color]
              > >
              > > that's not really much of an excuse for not reading *any* Python tutorial
              > > before
              > > you jump in...[/color]
              >
              > 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[/color]

              Comment

              • skip@pobox.com

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


                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:

                The official home of the Python Programming Language


                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, "Dictionari es" 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.pytho n.

                Skip

                Comment

                • Fredrik Lundh

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

                  A.M wrote:
                  [color=blue]
                  > 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.[/color]

                  are your boss aware of this ?

                  </F>

                  Comment

                  • gregarican

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

                    We have sort of a .... problem .... here .... uh .... yeah
                    (http://www.luminomagazine.com/2004.0...lumbergh1.jpg)...

                    Fredrik Lundh wrote:[color=blue]
                    > A.M wrote:
                    >[color=green]
                    > > 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.[/color]
                    >
                    > are your boss aware of this ?
                    >
                    > </F>[/color]

                    Comment

                    • A.M

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



                      "Fredrik Lundh" <fredrik@python ware.com> wrote in message
                      news:mailman.64 24.1149190330.2 7775.python-list@python.org ...[color=blue]
                      > A.M wrote:
                      >[color=green]
                      >> 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.[/color]
                      >
                      > are your boss aware of this ?
                      >
                      > </F>
                      >[/color]

                      [color=blue]
                      > are your boss aware of this ?[/color]

                      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.


                      Comment

                      • A.M

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


                        "Fredrik Lundh" <fredrik@python ware.com> wrote in message
                        news:mailman.64 24.1149190330.2 7775.python-list@python.org ...[color=blue]
                        > A.M wrote:
                        >[color=green]
                        >> 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.[/color]
                        >
                        > are your boss aware of this ?
                        >
                        > </F>
                        >[/color]

                        [color=blue]
                        > are your boss aware of this ?[/color]

                        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?






                        Comment

                        • John Machin

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


                          A.M wrote:
                          [color=blue]
                          > The application is not mission critical system. It is just a simple
                          > reporting tool.[/color]

                          Famous last words.

                          Comment

                          • gregarican

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

                            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

                            John Machin wrote:[color=blue]
                            > A.M wrote:
                            >[color=green]
                            > > The application is not mission critical system. It is just a simple
                            > > reporting tool.[/color]
                            >
                            > Famous last words.[/color]

                            Comment

                            • A.M

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



                              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.





                              "gregarican " <greg.kujawa@gm ail.com> wrote in message
                              news:1149212030 .851030.257180@ c74g2000cwc.goo glegroups.com.. .[color=blue]
                              > 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
                              >
                              > John Machin wrote:[color=green]
                              >> A.M wrote:
                              >>[color=darkred]
                              >> > The application is not mission critical system. It is just a simple
                              >> > reporting tool.[/color]
                              >>
                              >> Famous last words.[/color]
                              >[/color]


                              Comment

                              Working...