[Python-il] Fwd: Is there a Python module for handling Python object addresses?
cool-rr at cool-rr.com
Sat Sep 11 22:23:39 IDT 2010
On Sat, Sep 11, 2010 at 9:07 PM, Amit Aronovitch <aronovitch at gmail.com>wrote:
>> Please specify a clear use case.
>> 1. Django settings module.
>> 2. `repr`. Especially if you want to shorten the address, according to
>> point 3 of my first message. It's a lot nicer to have:
>> <garlicsim.Project containing 101 nodes and employing 4 crunchers
>> at 0x1f668d0>
>> Then to have
>> <garlicsim.asynchronous_crunching.project.Project containing 101 nodes and
>> employing 4 crunchers at 0x1f668d0>
>> (Since `Project` is already available in the `garlicsim` namespace.)
> The fully qualified name is the only one you can find in O(1), because this
> is the information which is stored in the object.
The time that finding the name will take does not matter at all. When you
use `repr`, do you care if it will take x100 the time it usually takes? It
will still be instantaneous. Besides, I cache it.
> To find other modules which import it, you'd have to search, and also the
> result might not be constant.
> >>> from django.contrib import auth as a
> >>> a.__name__
> >>> import sys
> >>> sys.auth # auth is NOT available in module sys
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: 'module' object has no attribute 'auth'
> >>> sys.auth = a # from now on, it is
> >>> sys.auth.__name__ # but the name stays the same
> If you want to shorten the name that appears in the repr, you can always
> change the __name__ (or __module__ in case it is a class), in your
> garlicsim.__init__, right after you import it (however, this might confuse
> debuggers etc. that need to find the source file in which your
> class/function/module was defined).
> >>> a.__name__ = "kuku.auth"
> >>> sys.auth
> <module 'kuku.auth' from
You're making this really complicated. It doesn't have to be that
bulletproof! The mechanism in `repr` is not bulletproof, and Django's
mechanism for getting the object from the string is not bulletproof either.
And I definitely don't want to be changing `__name__` of anything.
> 3. I'm making a GUI in which the user will be able to manipulate Python
>> functions, and I want to display the shortest name possible on the screen.
> I think you'd have to do a nasty search in sys.modules.
> I'm almost certain I wrote code that does something like that, but that was
> ages ago and I do not have access to it any more. Sorry.
> Note that if you want your GUI to be able to find the *source file* where
> the object is defined, the original fully-qualified-name
> would be more useful.
Searching `sys.modules` is not needed. By "rooting" (as described in point 4
in my original message) and shortening (as described in point 3) I can
shorten the address to the minimum. (Not 100% guaranteed, but almost 100%.)
Thanks for your help Amit. The module I made for this, called
`address_tools.py`, already works for me. (I don't want to show it yet
because it's not documented.) It'll be released documented and tested with
GarlicSim 0.6, in a few months.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-il