source: mauRepo/CedaMolesModel/trunk/ceda_moles_model/ascore/utils.py @ 8457

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/CedaMolesModel/trunk/ceda_moles_model/ascore/utils.py@8457
Revision 8457, 2.4 KB checked in by mnagni, 7 years ago (diff)

Corrected an error in the generation of the classes "synchronize" method.

  • Property svn:mime-type set to text/plain
Line 
1'''
2Created on 6 Feb 2012
3
4@author: mnagni
5'''
6from sqlalchemy.orm.collections import InstrumentedList
7def has_value(item):
8    if item is not None:
9        if (isinstance(item, list) and len(item) > 0) or not isinstance(item, list):           
10            return True
11        else:
12            return False
13    return False
14
15def collectionProofHash(tupleToHash):
16    try:
17        return hash(tuple(_convertCollectionToList(tupleToHash)))
18    except Exception as e:
19        print e
20
21def _convertCollectionToList(tupleToHash, processedItems = []):
22    toHash = []
23    if tupleToHash is not None and type(tupleToHash) != tuple and type(tupleToHash) != list \
24        and type(tupleToHash) != InstrumentedList and type(tupleToHash) != dict:
25        processedItems.append(tupleToHash)
26        toHash.append(tupleToHash)
27        return toHash
28   
29    for item in tupleToHash:
30        if item in processedItems:
31            continue
32        processedItems.append(item)
33        if type(item) == tuple or type(item) == list or type(item) == InstrumentedList:
34            for innerItem in item:
35                toHash.extend(_convertCollectionToList(innerItem, processedItems))
36        elif type(item) == dict:
37            for innerItem in item.keys():
38                toHash.extend(_convertCollectionToList(innerItem, processedItems))
39            for innerItem in item.values():
40                toHash.extend(_convertCollectionToList(innerItem, processedItems))
41        else:
42            toHash.append(item)
43    return toHash
44
45def _synchAttributes(instance, done = []):
46    """
47        Restores the class's composition elements.
48        This methods should be called before any commit operation
49        @param instance: the instance that has to be restored
50        @param done: the instance that has to be restored       
51    """   
52    if instance in done:
53        return
54    if hasattr(instance, 'synchronize'):
55        instance.synchronize()
56        done.append(instance)
57    for attr in dir(instance):
58        if attr != '__class__' and attr.startswith('_') \
59            and not attr.startswith('__') and hasattr(getattr(instance,attr), 'synchronize'):
60                _synchAttributes(getattr(instance,attr), done)
61
62def synchAttributes(instance):
63    """
64        Restores the class's composition elements.
65        This methods should be called before any commit operation
66        @param instance: the instance that has to be restored         
67    """
68    _synchAttributes(instance,[])
Note: See TracBrowser for help on using the repository browser.