Skip to content

Commit

Permalink
Set KeyDicts with vectorvar slices (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Dec 2, 2020
1 parent 648df46 commit 9dd245e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions gpkit/keydict.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,20 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
"Overloads __setitem__ and []= to work with all keys"
# pylint: disable=too-many-boolean-expressions
key, idx = self.parse_and_index(key)
# pylint: disable=too-many-boolean-expressions,too-many-branches
try:
key, idx = self.parse_and_index(key)
except KeyError as e: # may be indexed VectorVariable
# NOTE: this try/except takes ~4% of the time in this loop
if not hasattr(key, "shape"):
raise e
if not hasattr(value, "shape"):
value = np.full(key.shape, value)
elif key.shape != value.shape:
raise KeyError("Key and value have different shapes") from e
for subkey, subval in zip(key.flat, value.flat):
self[subkey] = subval
return
value = clean_value(key, value)
if key not in self.keymap:
if not hasattr(self, "_unmapped_keys"):
Expand Down

0 comments on commit 9dd245e

Please sign in to comment.