Changeset 2385


Ignore:
Timestamp:
Feb 22, 2010 1:14:18 AM (10 years ago)
Author:
wehart
Message:

Adding a work-around when importing 'bidict' fails. This occurs with
Python 2.5 and earlier.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pyomo/trunk/coopr/pyomo/base/sets.py

    r2369 r2385  
    1818import types
    1919import copy
    20 import bidict
     20try:
     21    import bidict
     22    using_bidict=True
     23except:
     24    using_bidict=False
    2125
    2226log = pyutilib.component.core.PluginGlobals.env().log
     
    7175        self.ordered=False
    7276        self.order=[]
    73         self.order_dict = bidict.bidict()
     77        if using_bidict:
     78            self.order_dict = bidict.bidict()
     79        else:
     80            self.order_dict = {}
     81            self.order_dict_inv = {}
    7482        self.domain=None
    7583        self.dimen = None
     
    290298           raise TypeError, "Cannot clear virtual Set object `"+self.name+"'"
    291299        self.value.clear()
    292         self.order_dict=bidict.bidict()
     300        if using_bidict:
     301            self.order_dict = bidict.bidict()
     302        else:
     303            self.order_dict = {}
     304            self.order_dict_inv = {}
    293305
    294306    def check_values(self):
     
    315327                    raise ValueError, "Element "+str(tmp)+" already exists in ordered set "+self.name
    316328               self.order.append(tmp)
    317                self.order_dict[tmp:] = len(self.order)
     329               if using_bidict:
     330                    self.order_dict[tmp:] = len(self.order)
     331               else:
     332                    self.order_dict[tmp] = len(self.order)
     333                    self.order_dict_inv[len(self.order)] = tmp
    318334               
    319335
     
    333349            id = self.order_dict[element]-1
    334350            for i in xrange(id+1,len(self.order)):
    335                 self.order_dict[self.order[i]] = i-1
     351                if using_bidict:
     352                    self.order_dict[self.order[i]] = i-1
     353                else:
     354                    self.order_dict[self.order[i]] = i-1
     355                    self.order_dict_inv[i-1] = self.order[i]
    336356            del self.order[id]
    337357
     
    361381    def member(self, key):
    362382        if self.ordered:
    363             return self.order_dict.inv[key]
     383            return self.order_dict_inv[key]
    364384        #
    365385        # If the set is not ordered, then we use the intrinsic ordering imposed by the set.
Note: See TracChangeset for help on using the changeset viewer.