Skip to content

Commit

Permalink
Merge pull request Pyomo#1241 from blnicho/faster-dicts
Browse files Browse the repository at this point in the history
Use dict comprehension for constructing dictionaries
  • Loading branch information
blnicho authored Jan 28, 2020
2 parents 69b2e7b + cb234d9 commit 3ee6a01
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 76 deletions.
2 changes: 1 addition & 1 deletion pyomo/core/base/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def __repr__(self):
return a

def __getstate__(self):
return dict((x,getattr(self,x)) for x in ComponentUID.__slots__)
return {x:getattr(self, x) for x in ComponentUID.__slots__}

def __setstate__(self, state):
for key, val in iteritems(state):
Expand Down
4 changes: 2 additions & 2 deletions pyomo/core/base/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ def display(self, prefix="", ostream=None):
# expensive to extract the contents of an expression.
#
def extract_values(self):
return dict((key, expression_data.expr) \
for key, expression_data in iteritems(self))
return {key:expression_data.expr
for key, expression_data in iteritems(self)}

#
# takes as input a (index, value) dictionary for updating this
Expand Down
3 changes: 1 addition & 2 deletions pyomo/core/base/indexed_component_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def __getstate__(self):
"blanket" implementation of :py:meth:`__getattr__`, we need to
explicitly implement these to avoid "accidentally" extending or
evaluating this slice."""
return dict(
(k,getattr(self,k)) for k in self.__dict__)
return {k:getattr(self,k) for k in self.__dict__}

def __setstate__(self, state):
"""Deserialize the state into this object. """
Expand Down
5 changes: 2 additions & 3 deletions pyomo/core/base/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ def __init__(self, preserve, translate, other):
other: the character to return for all characters not in
preserve or translate
"""
self.table = dict(
(k if isinstance(k, int) else ord(k), v)
for k,v in six.iteritems(dict(translate)) )
self.table = {k if isinstance(k, int) else ord(k): v
for k,v in six.iteritems(dict(translate)) }
for c in preserve:
_c = ord(c)
if _c in self.table and self.table[_c] != c:
Expand Down
5 changes: 1 addition & 4 deletions pyomo/core/base/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,7 @@ def extract_values(self):
# Thus, we need to create a temporary dictionary that contains the
# values from the ParamData objects.
#
ans = {}
for key, param_value in self.iteritems():
ans[key] = param_value()
return ans
return {key:param_value() for key,param_value in self.iteritems()}
elif not self.is_indexed():
#
# The parameter is a scalar, so we need to create a temporary
Expand Down
16 changes: 8 additions & 8 deletions pyomo/core/base/piecewise.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,14 @@ def _Branching_Scheme(self,n):
S = range(1,n+1)
# turn the GrayCode into a dictionary indexed
# starting at 1
G = dict(enumerate(_GrayCode(n),start=1))
G = {k:v for k,v in enumerate(_GrayCode(n),start=1)}

L = dict((s,[k+1 for k in xrange(BIGL+1) \
L = {s:[k+1 for k in xrange(BIGL+1) \
if ((k == 0) or (G[k][s-1] == 1)) \
and ((k == BIGL) or (G[k+1][s-1] == 1))]) for s in S)
R = dict((s,[k+1 for k in xrange(BIGL+1) \
and ((k == BIGL) or (G[k+1][s-1] == 1))] for s in S}
R = {s:[k+1 for k in xrange(BIGL+1) \
if ((k == 0) or (G[k][s-1] == 0)) \
and ((k == BIGL) or (G[k+1][s-1] == 0))]) for s in S)
and ((k == BIGL) or (G[k+1][s-1] == 0))] for s in S}

return S,L,R

Expand Down Expand Up @@ -662,9 +662,9 @@ def construct(self,pblock,x_var,y_var):
polytopes = range(1,len_x_pts)

# create constants (using future division)
SLOPE = dict((p,(y_pts[p]-y_pts[p-1])/(x_pts[p]-x_pts[p-1])) \
for p in polytopes)
INTERSEPT = dict((p,y_pts[p-1] - (SLOPE[p]*x_pts[p-1])) for p in polytopes)
SLOPE = {p:(y_pts[p]-y_pts[p-1])/(x_pts[p]-x_pts[p-1])
for p in polytopes}
INTERSEPT = {p:y_pts[p-1] - (SLOPE[p]*x_pts[p-1]) for p in polytopes}

# create vars
pblock.MC_poly_x = Var(polytopes)
Expand Down
3 changes: 1 addition & 2 deletions pyomo/core/base/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1365,8 +1365,7 @@ def sorted_data(self):
def _sort(self):
self._ordered_values = list(self.parent_component()._sort_fcn(
self._ordered_values))
self._values = dict(
(j, i) for i, j in enumerate(self._ordered_values) )
self._values = {j:i for i, j in enumerate(self._ordered_values)}
self._is_sorted = True


Expand Down
2 changes: 1 addition & 1 deletion pyomo/core/base/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def _sort(self):
self.value_list,
key=None if _sorter is Set.SortedOrder else _sorter
)
self.order_dict = dict((j,i) for i,j in enumerate(self.value_list))
self.order_dict = {j:i for i,j in enumerate(self.value_list)}
self._is_sorted = 1

def _clear(self):
Expand Down
2 changes: 1 addition & 1 deletion pyomo/core/base/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class InitializerBase(object):
verified = False

def __getstate__(self):
return dict((k, getattr(self,k)) for k in self.__slots__)
return {k:getattr(self,k) for k in self.__slots__}

def __setstate__(self, state):
for key, val in iteritems(state):
Expand Down
7 changes: 3 additions & 4 deletions pyomo/core/base/var.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,10 @@ def get_values(self, include_fixed_values=True):
Return a dictionary of index-value pairs.
"""
if include_fixed_values:
return dict((idx, vardata.value)
for idx, vardata in iteritems(self._data))
return dict((idx, vardata.value)
return {idx:vardata.value for idx,vardata in iteritems(self._data)}
return {idx:vardata.value
for idx, vardata in iteritems(self._data)
if not vardata.fixed)
if not vardata.fixed}

extract_values = get_values

Expand Down
2 changes: 1 addition & 1 deletion pyomo/core/expr/calculus/diff_with_sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def differentiate(expr, wrt=None, wrt_list=None):
# appear in the expression (so that we can detect wrt combinations
# that are, by definition, 0)
#
partial_derivs = dict((x,None) for x in objectMap.sympyVars())
partial_derivs = {x:None for x in objectMap.sympyVars()}
#
# Setup the WRT list
#
Expand Down
11 changes: 4 additions & 7 deletions pyomo/core/expr/symbol_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ def __getstate__(self):
}

def __setstate__(self, state):
self.byObject = dict(
(id(obj), key) for key, obj in state['bySymbol'] )
self.bySymbol = dict(
(key, weakref_ref(obj)) for key, obj in state['bySymbol'] )
self.aliases = dict(
(key, weakref_ref(obj)) for key, obj in state['aliases'] )
self.byObject = {id(obj):key for key, obj in state['bySymbol']}
self.bySymbol = {key:weakref_ref(obj) for key,obj in state['bySymbol']}
self.aliases = {key:weakref_ref(obj) for key, obj in state['aliases']}

def addSymbol(self, obj, symb):
"""
Expand All @@ -78,7 +75,7 @@ def addSymbols(self, obj_symbol_tuples):
This method assumes that symbol names will not conflict.
"""
tuples = list((obj, symb) for obj,symb in obj_symbol_tuples)
tuples = [(obj, symb) for obj,symb in obj_symbol_tuples]
self.byObject.update((id(obj_), symb_) for obj_,symb_ in tuples)
self.bySymbol.update((symb_, weakref_ref(obj_)) for obj_,symb_ in tuples)

Expand Down
4 changes: 2 additions & 2 deletions pyomo/core/kernel/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ def load_solution(self,
# Generate the list of active import suffixes on
# this top level model
valid_import_suffixes = \
dict((obj.storage_key, obj)
for obj in import_suffix_generator(self))
{obj.storage_key:obj
for obj in import_suffix_generator(self)}

# To ensure that import suffix data gets properly
# overwritten (e.g., the case where nonzero dual
Expand Down
16 changes: 7 additions & 9 deletions pyomo/core/kernel/component_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ def __setstate__(self, state):
# object id() may have changed after unpickling,
# so we rebuild the dictionary keys
self._dict = \
dict((id(obj), (obj,val)) \
for obj, val in itervalues(state['_dict']))
{id(obj):(obj,val) \
for obj, val in itervalues(state['_dict'])}

def __getstate__(self):
# *** Temporary hack to allow this class to be used
Expand All @@ -88,9 +88,7 @@ def __getstate__(self):

def __str__(self):
"""String representation of the mapping."""
tmp = dict()
for c,v in self.items():
tmp[str(c)+" (id="+str(id(c))+")"] = v
tmp = {str(c)+" (id="+str(id(c))+")":v for c,v in self.items()}
return "ComponentMap("+str(tmp)+")"

#
Expand Down Expand Up @@ -133,10 +131,10 @@ def __len__(self):
def __eq__(self, other):
if not isinstance(other, collections_Mapping):
return False
return dict(((type(key), id(key)), val)
for key, val in self.items()) == \
dict(((type(key), id(key)), val)
for key, val in other.items())
return {(type(key), id(key)):val
for key, val in self.items()} == \
{(type(key), id(key)):val
for key, val in other.items()}

def __ne__(self, other):
return not (self == other)
Expand Down
3 changes: 1 addition & 2 deletions pyomo/core/kernel/component_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def __setstate__(self, state):
# object id() may have changed after unpickling,
# so we rebuild the dictionary keys
assert len(state) == 1
self._data = dict((id(obj), obj)
for obj in state['_data'])
self._data = {id(obj):obj for obj in state['_data']}

def __getstate__(self):
return {'_data': tuple(self._data.values())}
Expand Down
8 changes: 4 additions & 4 deletions pyomo/core/kernel/dict_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ def __contains__(self, key):
def __eq__(self, other):
if not isinstance(other, collections_Mapping):
return False
return dict((key, (type(val), id(val)))
for key, val in self.items()) == \
dict((key, (type(val), id(val)))
for key, val in other.items())
return {key:(type(val), id(val))
for key, val in self.items()} == \
{key:(type(val), id(val))
for key, val in other.items()}

def __ne__(self, other):
return not (self == other)
3 changes: 1 addition & 2 deletions pyomo/core/kernel/piecewise_library/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ def __init__(self,
def __getstate__(self):
"""Required for older versions of the pickle
protocol since this class uses __slots__"""
return dict((key, getattr(self, key))
for key in self.__slots__)
return {key:getattr(self, key) for key in self.__slots__}

def __setstate__(self, state):
"""Required for older versions of the pickle
Expand Down
3 changes: 1 addition & 2 deletions pyomo/core/kernel/piecewise_library/transforms_nd.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ def __init__(self,
def __getstate__(self):
"""Required for older versions of the pickle
protocol since this class uses __slots__"""
return dict((key, getattr(self, key))
for key in self.__slots__)
return {key:getattr(self, key) for key in self.__slots__}

def __setstate__(self, state):
"""Required for older versions of the pickle
Expand Down
5 changes: 2 additions & 3 deletions pyomo/core/plugins/transform/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ def _apply_to(self, model, **kwds):
# translate the variable_substitution_map (ComponentMap)
# to variable_substition_dict (key: id() of component)
# ToDo: We should change replace_expressions to accept a ComponentMap as well
variable_substitution_dict = dict()
for k in variable_substitution_map:
variable_substitution_dict[id(k)] = variable_substitution_map[k]
variable_substitution_dict = {id(k):variable_substitution_map[k]
for k in variable_substitution_map}

for component in model.component_objects(ctype=(Constraint, Objective), descend_into=True):
for k in component:
Expand Down
12 changes: 4 additions & 8 deletions pyomo/core/tests/unit/kernel/test_dict_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,15 @@ def test_pickle(self):

def test_keys(self):
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._ctype_factory())
for i in index)
raw_constraint_dict = {i:self._ctype_factory() for i in index}
c = self._container_type(raw_constraint_dict)
self.assertEqual(sorted(list(raw_constraint_dict.keys()),
key=str),
sorted(list(c.keys()), key=str))

def test_values(self):
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._ctype_factory())
for i in index)
raw_constraint_dict = {i:self._ctype_factory() for i in index}
c = self._container_type(raw_constraint_dict)
self.assertEqual(
sorted(list(id(_v)
Expand All @@ -302,8 +300,7 @@ def test_values(self):

def test_items(self):
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._ctype_factory())
for i in index)
raw_constraint_dict = {i:self._ctype_factory() for i in index}
c = self._container_type(raw_constraint_dict)
self.assertEqual(
sorted(list((_i, id(_v))
Expand All @@ -315,8 +312,7 @@ def test_items(self):

def test_update(self):
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._ctype_factory())
for i in index)
raw_constraint_dict = {i:self._ctype_factory() for i in index}
c = self._container_type()
c.update(raw_constraint_dict)
self.assertEqual(sorted(list(raw_constraint_dict.keys()),
Expand Down
12 changes: 4 additions & 8 deletions pyomo/core/tests/unit/test_dict_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,15 @@ def test_model_clone(self):
def test_keys(self):
model = self.model
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._cdatatype(self._arg()))
for i in index)
raw_constraint_dict = {i:self._cdatatype(self._arg()) for i in index}
model.c = self._ctype(raw_constraint_dict)
self.assertEqual(sorted(list(raw_constraint_dict.keys()), key=str),
sorted(list(model.c.keys()), key=str))

def test_values(self):
model = self.model
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._cdatatype(self._arg()))
for i in index)
raw_constraint_dict = {i:self._cdatatype(self._arg()) for i in index}
model.c = self._ctype(raw_constraint_dict)
self.assertEqual(
sorted(list(id(_v)
Expand All @@ -234,8 +232,7 @@ def test_values(self):
def test_items(self):
model = self.model
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._cdatatype(self._arg()))
for i in index)
raw_constraint_dict = {i:self._cdatatype(self._arg()) for i in index}
model.c = self._ctype(raw_constraint_dict)
self.assertEqual(
sorted(list((_i, id(_v))
Expand All @@ -248,8 +245,7 @@ def test_items(self):
def test_update(self):
model = self.model
index = ['a', 1, None, (1,), (1,2)]
raw_constraint_dict = dict((i, self._cdatatype(self._arg()))
for i in index)
raw_constraint_dict = {i:self._cdatatype(self._arg()) for i in index}
model.c = self._ctype()
model.c.update(raw_constraint_dict)
self.assertEqual(sorted(list(raw_constraint_dict.keys()), key=str),
Expand Down

0 comments on commit 3ee6a01

Please sign in to comment.