proxy.py (10195:7d4d0cd3f7e5) | proxy.py (12798:d9fc94b42670) |
---|---|
1# Copyright (c) 2018 ARM Limited 2# All rights reserved. 3# 4# The license below extends only to copyright in the software and shall 5# not be construed as granting a license to any other intellectual 6# property including but not limited to intellectual property relating 7# to a hardware implementation of the functionality of the software 8# licensed hereunder. You may use the software subject to the license 9# terms below provided that you ensure that this notice is replicated 10# unmodified and in its entirety in all distributions of the software, 11# modified or unmodified, in source code or in binary form. 12# |
|
1# Copyright (c) 2004-2006 The Regents of The University of Michigan 2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright --- 21 unchanged lines hidden (view full) --- 30##################################################################### 31# 32# Proxy object support. 33# 34##################################################################### 35 36import copy 37 | 13# Copyright (c) 2004-2006 The Regents of The University of Michigan 14# All rights reserved. 15# 16# Redistribution and use in source and binary forms, with or without 17# modification, are permitted provided that the following conditions are 18# met: redistributions of source code must retain the above copyright 19# notice, this list of conditions and the following disclaimer; 20# redistributions in binary form must reproduce the above copyright --- 21 unchanged lines hidden (view full) --- 42##################################################################### 43# 44# Proxy object support. 45# 46##################################################################### 47 48import copy 49 |
50import params 51 |
|
38class BaseProxy(object): 39 def __init__(self, search_self, search_up): 40 self._search_self = search_self 41 self._search_up = search_up | 52class BaseProxy(object): 53 def __init__(self, search_self, search_up): 54 self._search_self = search_self 55 self._search_up = search_up |
42 self._multiplier = None | 56 self._multipliers = [] |
43 44 def __str__(self): 45 if self._search_self and not self._search_up: 46 s = 'Self' 47 elif not self._search_self and self._search_up: 48 s = 'Parent' 49 else: 50 s = 'ConfusedProxy' 51 return s + '.' + self.path() 52 53 def __setattr__(self, attr, value): 54 if not attr.startswith('_'): 55 raise AttributeError, \ 56 "cannot set attribute '%s' on proxy object" % attr 57 super(BaseProxy, self).__setattr__(attr, value) 58 | 57 58 def __str__(self): 59 if self._search_self and not self._search_up: 60 s = 'Self' 61 elif not self._search_self and self._search_up: 62 s = 'Parent' 63 else: 64 s = 'ConfusedProxy' 65 return s + '.' + self.path() 66 67 def __setattr__(self, attr, value): 68 if not attr.startswith('_'): 69 raise AttributeError, \ 70 "cannot set attribute '%s' on proxy object" % attr 71 super(BaseProxy, self).__setattr__(attr, value) 72 |
59 # support multiplying proxies by constants | 73 # support for multiplying proxies by constants or other proxies to 74 # other params |
60 def __mul__(self, other): | 75 def __mul__(self, other): |
61 if not isinstance(other, (int, long, float)): 62 raise TypeError, "Proxy multiplier must be integer" 63 if self._multiplier == None: 64 self._multiplier = other 65 else: 66 # support chained multipliers 67 self._multiplier *= other | 76 if not (isinstance(other, (int, long, float)) or isproxy(other)): 77 raise TypeError, \ 78 "Proxy multiplier must be a constant or a proxy to a param" 79 self._multipliers.append(other) |
68 return self 69 70 __rmul__ = __mul__ 71 | 80 return self 81 82 __rmul__ = __mul__ 83 |
72 def _mulcheck(self, result): 73 if self._multiplier == None: 74 return result 75 return result * self._multiplier | 84 def _mulcheck(self, result, base): 85 for multiplier in self._multipliers: 86 if isproxy(multiplier): 87 multiplier = multiplier.unproxy(base) 88 # assert that we are multiplying with a compatible 89 # param 90 if not isinstance(multiplier, params.NumericParamValue): 91 raise TypeError, \ 92 "Proxy multiplier must be a numerical param" 93 multiplier = multiplier.getValue() 94 result *= multiplier 95 return result |
76 77 def unproxy(self, base): 78 obj = base 79 done = False 80 81 if self._search_self: 82 result, done = self.find(obj) 83 --- 16 unchanged lines hidden (view full) --- 100 "Can't resolve proxy '%s' of type '%s' from '%s'" % \ 101 (self.path(), self._pdesc.ptype_str, base.path()) 102 103 if isinstance(result, BaseProxy): 104 if result == self: 105 raise RuntimeError, "Cycle in unproxy" 106 result = result.unproxy(obj) 107 | 96 97 def unproxy(self, base): 98 obj = base 99 done = False 100 101 if self._search_self: 102 result, done = self.find(obj) 103 --- 16 unchanged lines hidden (view full) --- 120 "Can't resolve proxy '%s' of type '%s' from '%s'" % \ 121 (self.path(), self._pdesc.ptype_str, base.path()) 122 123 if isinstance(result, BaseProxy): 124 if result == self: 125 raise RuntimeError, "Cycle in unproxy" 126 result = result.unproxy(obj) 127 |
108 return self._mulcheck(result) | 128 return self._mulcheck(result, base) |
109 110 def getindex(obj, index): 111 if index == None: 112 return obj 113 try: 114 obj = obj[index] 115 except TypeError: 116 if index != 0: --- 130 unchanged lines hidden --- | 129 130 def getindex(obj, index): 131 if index == None: 132 return obj 133 try: 134 obj = obj[index] 135 except TypeError: 136 if index != 0: --- 130 unchanged lines hidden --- |