Changeset 01d3cf in indico


Ignore:
Timestamp:
03/26/12 16:49:50 (14 months ago)
Author:
Jose Benito <jose.benito.gonzalez@…>
Branches:
master, hello-world-walkthrough, ipv6, v0.98-series, v0.98.2, v0.98.3, v0.99, 051b2622c51afb171a1dedb46a0df4fbb0cbd02e, 0da0c1403bae8e51d8229f460181c71b9e6dda72
Children:
13903d
Parents:
6a90d1
Message:

[OPT] Minor optimizations to fossils

File:
1 edited

Legend:

Unmodified
Added
Removed
  • indico/util/fossilize/__init__.py

    r4fd80c r01d3cf  
    9595    __methodNameCache = {} 
    9696    __fossilNameCache = {} 
     97    __fossilInterfaceCache = {} 
    9798    __fossilAttrsCache = {} # Attribute Cache for Fossils with 
    9899                            # fields that are repeated 
     
    272273        result = {} 
    273274 
    274         for method in interface: 
    275  
    276             tags = interface[method].getTaggedValueTags() 
     275        # cache method names for each interface 
     276        names = cls.__fossilInterfaceCache.get(interface) 
     277        if names is None: 
     278            names = interface.names(all=True) 
     279            cls.__fossilInterfaceCache[interface] = names 
     280        ### 
     281 
     282        for methodName in names: 
     283 
     284            method = interface[methodName] 
     285 
     286            tags = method.getTaggedValueTags() 
    277287            isAttribute = False 
    278288 
     
    282292            if useAttrCache: 
    283293                try: 
    284                     methodResult = cls.__fossilAttrsCache[obj._p_oid][method] 
     294                    methodResult = cls.__fossilAttrsCache[obj._p_oid][methodName] 
    285295                    cacheUsed = True 
    286296                except KeyError: 
     
    290300                # there is almost always a more elegant and modular solution! 
    291301                if 'produce' in tags: 
    292                     methodResult = interface[method].getTaggedValue('produce')(obj) 
     302                    methodResult = method.getTaggedValue('produce')(obj) 
    293303                else: 
    294                     attr = getattr(obj, method) 
     304                    attr = getattr(obj, methodName) 
    295305                    if callable(attr): 
    296306                        try: 
     
    305315 
    306316                if hasattr(obj, "_p_oid"): 
    307                     try: 
    308                         cls.__fossilAttrsCache[obj._p_oid] 
    309                     except KeyError: 
    310                         cls.__fossilAttrsCache[obj._p_oid] = {} 
    311                     cls.__fossilAttrsCache[obj._p_oid][method] = methodResult 
     317                    cls.__fossilAttrsCache.setdefault(obj._p_oid, {})[methodName] = methodResult 
    312318 
    313319            if 'filterBy' in tags: 
    314320                if 'filters' not in kwargs: 
    315321                    raise Exception('No filters defined!') 
    316                 filterName = interface[method].getTaggedValue('filterBy') 
     322                filterName = method.getTaggedValue('filterBy') 
    317323 
    318324                if filterName in kwargs['filters']: 
     
    325331            # Result conversion 
    326332            if 'result' in tags: 
    327                 targetInterface = interface[method].getTaggedValue('result') 
     333                targetInterface = method.getTaggedValue('result') 
    328334                #targetInterface = globals()[targetInterfaceName] 
    329335 
     
    333339            # Conversion function 
    334340            if 'convert' in tags: 
    335                 convertFunction = interface[method].getTaggedValue('convert') 
     341                convertFunction = method.getTaggedValue('convert') 
    336342                converterArgNames = inspect.getargspec(convertFunction)[0] 
    337343                converterArgs = dict((name, kwargs[name]) 
     
    342348                except: 
    343349                    logging.getLogger('indico.fossilize').error("Problem fossilizing '%r' with '%s' (%s)" % 
    344                                                                 (obj, interfaceArg, method)) 
     350                                                                (obj, interfaceArg, methodName)) 
    345351                    raise 
    346352 
     
    348354            # Re-name the attribute produced by the method 
    349355            if 'name' in tags: 
    350                 attrName = interface[method].getTaggedValue('name') 
     356                attrName = method.getTaggedValue('name') 
    351357            elif isAttribute: 
    352                 attrName = method 
    353             else: 
    354                 attrName = cls.__extractName(method) 
     358                attrName = methodName 
     359            else: 
     360                attrName = cls.__extractName(methodName) 
    355361 
    356362            # In case the name contains dots, each of the 'domains' but the 
     
    367373            while len(attrList) > 1: 
    368374                attr = attrList.pop(0) 
    369                 try: 
    370                     current = current[attr] 
    371                 except KeyError: 
     375                if attr not in current: 
    372376                    current[attr] = {} 
    373                     current = current[attr] 
     377                current = current[attr] 
    374378 
    375379            # For the last attribute level 
Note: See TracChangeset for help on using the changeset viewer.