Retrieve an item from a dictionary using an arbitrary object as the key

Birone Lynch bironelynch at gmail.com
Tue Apr 3 13:50:52 EDT 2007


>
>
> > getPerson(19)  ....should return me the Person with name "bob" and age
> > 99.  I am thinking there is some method that is used by the dictionary
> > to know if the key exists, just not sure which.


Were you thinking of get:
a.get(k[, x]) returns a[k] if k in a, else x

But do you want to lookup by the object itself, or by its val attribute?
(Your code suggests the latter, but that's not what your title says...)


---------- Forwarded message ----------
> From: tom at finland.com
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 16:35:13 GMT
> Subject: Re: Stack experiment
> Ok, I got it running. Thank you!
>
> I removed the space and top of that I had foul indentation in return
> statement.
>
> I'll try the approaches you suggest.
>
>
>
> ---------- Forwarded message ----------
> From: Steven Bethard <steven.bethard at gmail.com>
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 10:38:41 -0600
> Subject: Re: getattr/setattr q.
> Steve Holden wrote:
> > You don't need setattr/getattr if you know in advance the name of the
> > attribute you need to access and you can get a reference to the object
> > whose attribute it is. So:
> >
> >  >>> x = "Hello, Paulo"
> >  >>> import sys
> >  >>> sys.modules['__main__'].x
> > 'Hello, Paulo'
>
> a.k.a
>
>     >>> import __main__
>     >>> __main__.x
>     'Hello, Paulo'
>
> STeVe
>
>
>
> ---------- Forwarded message ----------
> From: Steve Holden <steve at holdenweb.com>
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 12:43:17 -0400
> Subject: Re: getattr/setattr q.
> Steven Bethard wrote:
> > Steve Holden wrote:
> >> You don't need setattr/getattr if you know in advance the name of the
> >> attribute you need to access and you can get a reference to the object
> >> whose attribute it is. So:
> >>
> >>  >>> x = "Hello, Paulo"
> >>  >>> import sys
> >>  >>> sys.modules['__main__'].x
> >> 'Hello, Paulo'
> >
> > a.k.a
> >
> >      >>> import __main__
> >      >>> __main__.x
> >      'Hello, Paulo'
> >
> Indeed. Any handle on the right object will do.
>
> regards
>  Steve
> --
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd          http://www.holdenweb.com
> Skype: holdenweb     http://del.icio.us/steve.holden
> Recent Ramblings       http://holdenweb.blogspot.com
>
>
>
>
> ---------- Forwarded message ----------
> From: Steve Holden <steve at holdenweb.com>
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 12:44:53 -0400
> Subject: Re: Stack experiment
> kyosohma at gmail.com wrote:
> [...]
> >
> > Steve,
> >
> > How do you do "tokenList = split(expr)"? There is no builtin called
> > "split".
> >
> > Mike
> >
>
> Sorry, that should have been a call to the .split() method of expr, i.e.:
>
> tokenList = expr.split()
>
> regards
>  Steve
> --
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC/Ltd          http://www.holdenweb.com
> Skype: holdenweb     http://del.icio.us/steve.holden
> Recent Ramblings       http://holdenweb.blogspot.com
>
>
>
>
> ---------- Forwarded message ----------
> From: "olive" <ocollioud at gmail.com>
> To: python-list at python.org
> Date: 3 Apr 2007 10:09:48 -0700
> Subject: Re: XML DTD analysis, diffing ...
> Thank you Stephane,
>
> it is almost what I want.
>
> I'm going to improve it a little and then provide the code back.
> Where is the best place ?
>
> Olive.
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Robert Kern <robert.kern at gmail.com>
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 12:21:44 -0500
> Subject: Re: Numeric compiling problem under QNX 4.25
> bernhard.voigt at gmail.com wrote:
> > Hi,
> >
> > unfortunately I don't have a solution. However, if you're not forced
> > to use Python2.2 and Numeric you should use Numpy (the successor for
> > both, Numeric and Numarray). Numpy requires Python2.3, though.
>
> He's already asked about numpy. He is stuck with 2.2.
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma
>  that is made terrible by our own mad attempt to interpret it as though it
> had
>  an underlying truth."
>  -- Umberto Eco
>
>
>
>
> ---------- Forwarded message ----------
> From: care02 at gmail.com
> To: python-list at python.org
> Date: 3 Apr 2007 10:21:59 -0700
> Subject: How can I kill a running thread when exiting from __main__
> Hi!
>
> I have the following problem: I have written a short Python server
> that creates an indefinite simulation thread that I want to kill when
> quitting (Ctrl-C) from Python. Googling around has not given me any
> hints on how to cleanly kill running threads before exiting. Any help
> is appreciated!
>
> Carl
>
> ### CODE EXTRACT ###
>
> import pythoncom
>
> class QueueThread( threading.Thread):
>    def __init__(self, command):
>        threading.Thread.__init__(self)
>        self.command = command
>
>    def run(self):
>        pythoncom.CoInitialize()
>        try:
>            object = Dispatch('application')
>            execute = getattr(object, 'Execute')
>            execute(self.command )
>        finally:
>            object = None
>            pythoncom.CoUnitialize()
>
> queuethread = QueueThread("queuehandler")
> queuethread.setDaemon(True)
> queuethread.start()
>
> ## How can I kill "queuethread" when exiting (Ctrl-C)?
>
>
>
>
> ---------- Forwarded message ----------
> From: Robert Kern <robert.kern at gmail.com>
> To: python-list at python.org
> Date: Tue, 03 Apr 2007 12:22:50 -0500
> Subject: Re: Numeric compiling problem under QNX 4.25
> ZMY wrote:
> > Dear all,
> >
> > I am a real newbie for both python and QNX, but I am still trying to
> > compile Numeric-24.2 under QNX4.25 with python 2.2. I got following
> > error message:
> >
> >
> > $ sudo python setup.py install
> > Password:
> > running install
> > ...
> > building '_numpy' extension
> > skipping Src/_numpymodule.c (build/temp.qnx-O-PCI-2.2/_numpymodule.o
> > up-to-date)
> > skipping Src/arrayobject.c (build/temp.qnx-O-PCI-2.2/arrayobject.o up-
> > to-date)
> > skipping Src/ufuncobject.c (build/temp.qnx-O-PCI-2.2/ufuncobject.o up-
> > to-date)
> > ld build/temp.qnx-O-PCI-2.2/_numpymodule.o build/temp.qnx-O-PCI-2.2/
> > arrayobject.o build/temp.qnx-O-PCI-2.2/ufuncobject.o -o build/lib.qnx-
> > O-PCI-2.2/_numpy.so
> > unable to execute ld: No such file or directory
> > error: command 'ld' failed with exit status 1
>
> It looks like it can't find the command ld. Can you compile any other
> extension
> modules?
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma
>  that is made terrible by our own mad attempt to interpret it as though it
> had
>  an underlying truth."
>  -- Umberto Eco
>
>
>
>
> ---------- Forwarded message ----------
> From: "Boudreau, Emile" <Emile.Boudreau at cognos.com>
> To: <python-list at python.org>
> Date: Tue, 3 Apr 2007 13:26:54 -0400
> Subject: Extracting a file from a tarball
>
> I am trying to extract one file from a tarball, without success. This is
> the code I'm using to open the tarball and extract the file:
>
> tar = tarfile.open(component+'-win32-app-'+bestVersion+'-dev.tar.gz', 'r')
> extractedFile = tar.extractfile('symbols.xml')
>
> And this is my error:
>
> Traceback (most recent call last):
>   File "C:\CognosInstalls\AutoTest\PollDir.py", line 121, in <module>
>     main()
>   File "C:\CognosInstalls\AutoTest\PollDir.py", line 119, in main
>     extract('c:\\', 'I:\\daily\\'+components[i], components[i])
>   File "C:\CognosInstalls\AutoTest\PollDir.py", line 27, in extract
>     filelike = tar.extractfile('symbols.xml')
>   File "C:\Python25\lib\tarfile.py", line 1488, in extract
>     tarinfo = self.getmember(member)
>   File "C:\Python25\lib\tarfile.py", line 1171, in getmember
>     raise KeyError("filename %r not found" % name)
> KeyError: "filename 'symbols.xml' not found"
>
> I know that the tarball contants this file "symbols.xml" but I don't
> understand why it's not extracting it. Any help will be greatly appreciated.
>
> Thanks,
>
> Emile Boudreau
>
>      This message may contain privileged and/or confidential information.
> If you have received this e-mail in error or are not the intended recipient,
> you may not use, copy, disseminate or distribute it; do not open any
> attachments, delete it immediately from your system and notify the sender
> promptly by e-mail that you have done so.  Thank you.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070403/56379dcf/attachment.html>


More information about the Python-list mailing list