Hi DenesL,

On Dec 17, 7:55 am, DenesL <[email protected]> wrote:

> is there an easy way to get 'db'?

I believe that you're saying that you want to somehow get the string
'db' from the assigned variable db.

This is a bit tricky in Python, but it can be done. Here's a function
that does it:


def getName(obj, namespaces=None, findAllNames=False):
    ''' Returns the name(s) associated with a given object.

        @param obj
            The object whose name you would like to know
        @param namespaces
            An ordered list of namespaces that you would like to
search for
            the inputed object. Valid namespaces include: globals(),
locals(),
            and vars(some_object). If set to None (the default) then
the
            globals() namespace will be assumed. You can also directly
            input a single namespace.
        @param findAllNames
            If False (the default) then this function will return the
first
            valid name that it finds. If True then this function will
return
            a complete list of all names it found in all of the
namespaces.
        @return
            The first valid name that it finds in the given
namespaces. If
            findAllNames is True, then it returns a list of all names
found.
            If nothing is found, returns None.
    '''
    if (namespaces == None):
        namespaces = [globals()]

    #if a dictionary was inputed for namespaces
    if (type(namespaces) == dict):
        #then this is probably because the user only needed to input
        #one namespace, and didn't place it in a list
        namespaces = [namespaces]


    result = []

    for namespace in namespaces:
        for key,value in namespace.iteritems():
            #if the given namespace object matches the inputed object
            if value is obj:
                if (findAllNames == False):
                    return key
                else:
                    result.append(key)

    if (len(result) > 0):
        return result
    else:
        return None



---

Try typing

getName(db)

and it should return the string 'db'

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to