Thread: optimize!

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    13

    optimize!


    Hi,
    I've this code...cld anyone optimize or think of another way to it!

    Explanation............

    here my getNodeType() will return an integer
    DOM_node_types is a dictionary containing the various constants....
    PHP Code:
    DOM_node_types = {
            
    1"ELEMENT",
            
    2"ATTRIBUTE",
            
    3"TEXT",
            
    4"CDATA_SECTION",
            
    5"ENTITY_REFERENCE",
            
    6"ENTITY",
            
    7"PROCESSING_INSTRUCTION",
            
    8"COMMENT",
            
    9"DOCUMENT",
            
    10"DOCUMENT_TYPE",
            
    11"DOCUMENT_FRAGMENT",
            
    12"NOTATION"
            

    what I want to do is get the key of TEXT ...which is 3

    so here I would compare the integer that I get with the key from my constant dictionary...is there a way out?

    PHP Code:
    if n.getNodeType()==list(DOM_node_types).pop(2):
                 return 
    0
    return 
    thanks,
    Subha
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    what I want to do is get the key of TEXT ...which is 3
    A dictionary is generally one-way, so there is no very quick way to do a reverse lookup.

    Code:
    for key, value in  DOM_node_types.items():
        if value == n.getNodeType():
            return key
    
    # If code gets to here, key not found.
    As you can see, this goes through every item in the dictionary - if you need to do this a lot, you might as well make another dictionary once and use it to do lookups the other way...

    Code:
    newdict = {}
    for key, value in d.items():
        newdict[value] = key
    Code:
    if n.getNodeType()==list(DOM_node_types).pop(2):
                 return 0
    return n
    Not sure I can tell what this does...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    As sfb says, create an inverse dictionary that maps the strings to the integers.

    Your original dictionary DOM_node_types may be better implemented as a simple list, e.g.

    Code:
     DOM_node_types = [
             None,
            "ELEMENT",
            "ATTRIBUTE",
            "TEXT",
            "CDATA_SECTION",
            "ENTITY_REFERENCE",
            "ENTITY",
            "PROCESSING_INSTRUCTION",
            "COMMENT",
            "DOCUMENT",
            "DOCUMENT_TYPE",
            "DOCUMENT_FRAGMENT",
            "NOTATION"
            ]
    You can still access the elements in the same way, and it is more efficient. Note I have added a dummy None element at the start, since the list is indexed from 0.


    Originally Posted by NewPythoner
    so here I would compare the integer that I get with the key from my constant dictionary...is there a way out?

    PHP Code:
    if n.getNodeType()==list(DOM_node_types).pop(2):
    ... 
    This code does not make sense, and does not do what you think it does. It creates a list from the dictionary's keys, but in random order. It then takes the second element in the list (which could be any of the keys), removes it from the list (moving the other elements down one place), then destroys the list. This is a very time consuming way to generate what is effectively a random number. Actually the current implementation of dictionary will probably return the keys in numerical order, but this is not guaranteed and is likely to break when the implementation changes.

    Dave - The Developers' Coach
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    And in fact, if you use a list then the .index method gives you precisely what you wanted.

    Code:
    # Using the list from above
    
    
    >>> DOM_node_types.index("NOTATION")
    12
    >>> DOM_node_types.index("ENTITY")
    6
    >>> DOM_node_types[6]
    'ENTITY'
    >>> DOM_node_types[12]
    'NOTATION'

IMN logo majestic logo threadwatch logo seochat tools logo