... this seems like it works even if the initial object isn't named '_something'.... as long as its not '_3' (or some other number) this still looks good... will get you a name before an internal reference.
On Wed, Dec 17, 2008 at 5:06 PM, Yarko Tymciurak <yark...@gmail.com> wrote: > Maybe something on the order of this might reliably get you what you want > (I don't check for no objects case - you can add that): > > def ngetName(obj): > ....: n=[k for k,v in globals().items() if v is obj] > ....: n.sort(reverse=True) > ....: return n[0] > > > > > On Wed, Dec 17, 2008 at 5:00 PM, Yarko Tymciurak <yark...@gmail.com>wrote: > >> I'd left my last python instance up; here's your "copy" version (which >> just copies the current references; it _does_ seem to get rid of the >> transient, underscore-only (one, two, three) references ... but that seems >> little. >> >> Instead of copy, you could do k.sort(reverse=True), and then return k[0] >> - that would at least be more reliable. >> >> Here's my results with your latest suggestion: >> >> In [50]: import copy >> >> In [51]: def cgetName(obj): >> ....: g=copy.copy(globals()) >> ....: return([k for k,v in g.items() if v is obj]) # don't need >> 'None' to return list; empty list o.k. >> ....: >> >> In [52]: cgetName(f._db) >> Out[52]: ['db', '_5', '_31'] >> >> In [53]: cgetName(db) >> Out[53]: ['db', '_5', '_31'] >> >> In [54]: getName(f._db) >> Out[54]: ['db', '_5', '_31'] >> >> In [55]: getName(db) >> Out[55]: ['db', '_5', '_31'] >> >> -------- >> Regards, >> Yarko >> >> >> On Wed, Dec 17, 2008 at 4:52 PM, Yarko Tymciurak <yark...@gmail.com>wrote: >> >>> depending on what you've done / referenced before you get the copy, your >>> list will differ... >>> >>> The underscores (single, double, triple) do appear to be transient; the >>> others are internal references. >>> >>> No matter what, that ****[0] just won't work. I've already tried this >>> on a couple of different machines, and I _can_ get to where 'foobar' is >>> item[0] in the list - sometimes, and not reliably. More often, it's >>> _sometimes_ item[0]; an activity (like a reference) disrupts that list. >>> >>> Anyway, this is interesting - good luck. I think you'll have to get the >>> list, and manually remove the '_names' (including '__); >>> >>> Look at my first "test results" post of this thread - '_5' was at the >>> head of that list (due to what activity happened, and how references were >>> generated) ---- it stayed at the head of the list; '_5' does not seem like >>> intermediate results, rather internally generated reference(s). Those >>> references seem to "stay around" ... don't know for what length of time, but >>> certainly the duration of my tests (gc might clean them up). >>> >>> Another time, '__' was at the head, but (as you noticed) that's >>> transient, and went away. >>> >>> I think you have _incidentally_ hit on cases where item[0] just happens >>> to be what you want. It doesn't look to me like you can count on that. >>> >>> Regards, >>> Yarko >>> >>> >>> On Wed, Dec 17, 2008 at 4:30 PM, DenesL <denes1...@yahoo.ca> wrote: >>> >>>> >>>> Yarko, >>>> >>>> I think those underscored keys come from intermediate results. >>>> Not sure about those double and triple ones. >>>> An improved version (of either J's or M's) would have to work with a >>>> copy of globals: >>>> >>>> import copy >>>> def getName(obj): >>>> g=copy.copy(globals()) >>>> return([k for k,v in g.items() if v is obj]+[None])[0] >>>> >>>> I don't get any underscores this way, how about you?. >>>> >>>> >>>> >>>> >>> >> > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py Web Framework" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---