Package schrodinger :: Package utils :: Module sea :: Class Map
[hide private]
[frames] | no frames]

Class Map

object --+    
         |    
       Sea --+
             |
            Map

This class represents the "map" parameters in a config file. This class' behavior is very similar to that of the buildin dict class.

Instance Methods [hide private]
 
__init__(self, ark={}, parent=None)
Constructs a 'Map' object with a given 'ark'.
 
__setattr__(self, key, value)
Sets an attribute called 'key' with 'value'.
 
__str__(self, ind='', tag=set([]), pre='', is_list_elem=False)
Converts this 'Map' object into a string representation.
 
__eq__(self, rhs)
Compares this 'Map' object with another 'Map' object 'rhs', returns True if the keys and values in both are equal, or False if otherwise.
 
__contains__(self, key)
This is the support for the 'in' operation, and it returns True if 'key' is already in this 'Map' object, False if otherwise.
 
__iter__(self)
This is the iterator fr the support for the 'for ...
 
__getitem__(self, key)
Returns the value associated with the 'key'.
 
__setitem__(self, key, value)
Associates the given 'value' with the given 'key'.
 
__delitem__(self, key)
Removes a key and the associated value from the 'Map' object.
 
__deepcopy__(self, memo={})
 
__set_val(self, val)
This function sets the value of the 'val' property (see below).
 
__get_raw_val(self)
This function returns the value of the 'raw_val' property (see below).
 
__get_bval(self)
Returns a new 'Sea' object, which has all macros expanded and references dereferenced.
 
__get_dval(self)
Returns a new `Sea' object, which has all references dereferenced but macros kept as is.
 
__get_val(self)
Returns the value of this 'Map' object.
 
keys(self, tag=set([]))
Returns references of all keys in a list.
 
values(self, tag=set([]))
Returns references of all values in a list.
 
key_value(self, tag=set([]), should_sort=False)
Returns the key and associated value in a list.
 
clone(self, orig)
Lets this 'Map' object become a deep copy of the 'orig' 'Map' object.
 
apply(self, op)
Recursively applies the operation as given by 'op' to all 'Sea' subobjects of this 'Sea' object.
 
update(self, ark=None, file=None, tag=set([]))
Updates this 'Map' object with the given 'ark' or with the given 'file'.
 
has_key(self, key)
Returns True if this 'Map' object has the 'key'.
 
get_value(self, key)
Returns the value of the given 'key'.
 
_set_value_helper(self, key_index_list, value, tag)
This is the actual implmentation of the 'set_value' function.
 
set_value(self, key, value, tag=set([]))
Associates the given value with the given key.
 
_dump_impl(self, tag)

Inherited from Sea: __getstate__, __ne__, __setstate__, add_tag, clear_all_tag, dump, has_tag, parent, pmode, remove_tag, reset_tag, set_parent, set_pmode, tag

Inherited from Sea (private): _add_tag_impl, _remove_tag_impl, _reset_tag_impl

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __sizeof__, __subclasshook__

Static Methods [hide private]
 
_get_key_index(s)
Parses a string that represents a key-index object in the form of "key[index]".
 
_get_index(s)
Parses a string that represents an index of a list in the form of "[index]".
 
_parse_composite_key(key)
Parses a composite key in the form of, e.g., "keyword1.keyword2[1][2].keyword3", and returns a list, e.g., ["keyword1", "keyword2", 1, 2, "keyword3",].

Inherited from Sea (private): _gen_sea, _normalize_tag

Class Variables [hide private]
  INDEX_PATTERN = re.compile(r'([^\[]*)\[ *([\+-]*[1234567890]*)...
  INDEX_PATTERN2 = re.compile(r'\[ *[\+-]*([1234567890]*) *\]')
Properties [hide private]
  raw_val
Readwrite.
  bval
Readonly.
  dval
Readonly.
  val
Readwrite.

Inherited from Sea: sval

Inherited from object: __class__

Method Details [hide private]

_get_key_index(s)
Static Method

 

Parses a string that represents a key-index object in the form of "key[index]". Note that in this form, there is only one key, but may be more than 1 indices (e.g., key[index1][index2]). This function returns a list, where the first element is the key (a string object), and remaining are indices (integer objects). For example, _get_key_index( "key[1][2]" ) should return ["key", 1, 2,].

_get_index(s)
Static Method

 

Parses a string that represents an index of a list in the form of "[index]". Note that in this form, there may be more than 1 indices (e.g., [index1][index2]). This function returns a list, where the elements are the indices (integer objects). For example, _get_index( "[1][2]" ) should return [1, 2,]. This function is used by '_get_key_index' function (see above).

__init__(self, ark={}, parent=None)
(Constructor)

 

Constructs a 'Map' object with a given 'ark'. The 'ark' can be of the following types of objects:

  • dict The 'Map' object will be constructed consistent with the dict object.
  • Map The 'ark' will be deep-copied.
  • str The string will be parsed and the 'Map' object will be constructed for the parsed string.
  • list The elements of the list object must be of the above types. A new 'Map' object will be constructed using the first elements, and then the 'Map' object will be updated with the remaining objects in the list.

If 'ark' is not provided, an empty 'Map' will be constructed. User can optionally specify the 'parent' parameter, which will set the parent of this 'Map' object to the given value of the 'parent' parameter.

Parameters:
  • parent - This paremeter provides the parent of this 'Sea' object. It can be None.
Overrides: object.__init__

__setattr__(self, key, value)

 

Sets an attribute called 'key' with 'value'.
- 'key' can be a str object or a 'Key' object.
- 'value' can be a 'Sea' object or other types of object (including None).
- If 'key' already exists in this 'Map' object and its value is a 'Sea' object, then 'value' must be either None or a
- 'Sea' object. In other words, you cannot change a 'Sea' value to a non-'Sea' value except for None.
- The parent of the 'value', if it is a 'Sea' object, will reset to this 'Map' object after this function.

Overrides: object.__setattr__

__str__(self, ind='', tag=set([]), pre='', is_list_elem=False)
(Informal representation operator)

 

Converts this 'Map' object into a string representation.
- 'ind' specifies the indentation of the converted string.
- 'tag' specifies the tags. And only the keys with the specified tags will be converted.
  'tag' can be a string (representing a single tag) or a 'list' or 'set' of strings (representing multiple tags).
- 'pre' specifies the prefix.

Overrides: object.__str__

__eq__(self, rhs)
(Equality operator)

 

Compares this 'Map' object with another 'Map' object 'rhs', returns True if the keys and values in both are equal, or False if otherwise. Note if '__CHECK_SEA_DEBUG' is set to True, difference in detail will be printed to the stdout.

Overrides: Sea.__eq__

__contains__(self, key)
(In operator)

 

This is the support for the 'in' operation, and it returns True if 'key' is already in this 'Map' object, False if
otherwise.
- 'key' can be either a string or a 'Key' object.

__iter__(self)

 

This is the iterator fr the support for the 'for ... in ...' syntax. The element yielded is the key of this 'Map' object.

__getitem__(self, key)
(Indexing operator)

 

Returns the value associated with the 'key'.
- 'key' should be string or 'Key' object.
- If 'key' does not exist in this 'Map' object, the 'KeyError' exception will be raised.

__setitem__(self, key, value)
(Index assignment operator)

 

Associates the given 'value' with the given 'key'.
- 'key' should be string or 'Key' object.
- If 'key' does not exist in this 'Map' object before, the 'key' will be put into the 'Map' object.
- 'value' will be processed first by the '_val_filter' function (see above), the 'Sea' object returned by the
  '_val_filter' function will be the actual object associated with the key. So any object acceptable by the
  '_val_filter' function can be used here.

__deepcopy__(self, memo={})

 
Overrides: Sea.__deepcopy__

__set_val(self, val)

 

This function sets the value of the 'val' property (see below).

Parameters:
  • val - 'val' must be either a dict or a 'Map' object, otherwise a 'ValueError' exception will be raised.

__get_raw_val(self)

 

This function returns the value of the 'raw_val' property (see below). Returns the raw value of this 'Map' object. The value will be in the form of a dict object, and all macros will be kept as is.

__get_bval(self)

 

Returns a new 'Sea' object, which has all macros expanded and references dereferenced. This function is used by the 'bval' property (see below).

__get_dval(self)

 

Returns a new `Sea' object, which has all references dereferenced but macros kept as is. This function is used by the 'dval' property (see below).

__get_val(self)

 

Returns the value of this 'Map' object. The value will be in the form of a dict object, and all macros will be expended and references dereferenced.

keys(self, tag=set([]))

 

Returns references of all keys in a list. Note each element in the returned list will be of the 'Key' type.

values(self, tag=set([]))

 

Returns references of all values in a list. Note each element in the returned list will be of the 'Sea' type.

key_value(self, tag=set([]), should_sort=False)

 

Returns the key and associated value in a list. Note each element in the returned list will be a 2-tuple object. The first element of the tuple is a reference of the key, and the second element is a reference of the value. User can optionally set the 'should_sort' parameter to True, which will let the function return a sorted list. The sorting will be based on the alphanumeric order of 'key'.

apply(self, op)

 

Recursively applies the operation as given by 'op' to all 'Sea' subobjects of this 'Sea' object.

Overrides: Sea.apply

update(self, ark=None, file=None, tag=set([]))

 

Updates this 'Map' object with the given 'ark' or with the given 'file'.

Parameters:
  • file - If 'file' is not None, it must be the name of a file in the ark file format. If 'file' is given, the 'ark' parameter will be ignored.
  • ark - ark can be a string or a dict or a 'Map' object. Or ark can be list of the previous objects.

has_key(self, key)

 

Returns True if this 'Map' object has the 'key'. Returns False if otherwise.

get_value(self, key)

 

Returns the value of the given 'key'.

Parameters:
  • key - The 'key' can be a composite key (i.e., the pathway notation), such as, e.g., "key[1][2].key2.key3[2]".

_set_value_helper(self, key_index_list, value, tag)

 

This is the actual implmentation of the 'set_value' function.

Parameters:
  • key_index_list - A list returned by the 'Map._parse_composite_key' function.
  • value - Same as the 'value' parameter in the 'set_value' function.
  • tag - Same as the 'tag' parameter in the 'set_value' function.

set_value(self, key, value, tag=set([]))

 

Associates the given value with the given key. The difference between this function and the __setitem__ operator is that the former allows us to reset the tag of the value.

Parameters:
  • key - The 'key' can be a composite key (i.e., the pathway notation), e.g., "key[1].key2[0].key3".
  • tag - If the "tag" parameter is specified, the value of 'tag' will be used to tag the 'value'.

_dump_impl(self, tag)

 
Overrides: Sea._dump_impl

Class Variable Details [hide private]

INDEX_PATTERN

Value:
re.compile(r'([^\[]*)\[ *([\+-]*[1234567890]*) *\]')

Property Details [hide private]

raw_val

Readwrite. When read, this returns the current raw value (references and macros kept as is).

Get Method:
__get_raw_val(self) - This function returns the value of the 'raw_val' property (see below).
Set Method:
__set_val(self, val) - This function sets the value of the 'val' property (see below).

bval

Readonly. Returns a new `Map' object, which has all macros expanded and references dereferenced.

Get Method:
__get_bval(self) - Returns a new 'Sea' object, which has all macros expanded and references dereferenced.

dval

Readonly. Returns a new `Map' object with dereferenced values.

Get Method:
__get_dval(self) - Returns a new `Sea' object, which has all references dereferenced but macros kept as is.

val

Readwrite. When read, this returns the current value (macros will be expanded, and references will be dereferenced.

Get Method:
__get_val(self) - Returns the value of this 'Map' object.
Set Method:
__set_val(self, val) - This function sets the value of the 'val' property (see below).