Improved container classes
The containers
module defines improved container classes, such as lists:
List: a subclass of the builtin
list
class, with added methods, such asindex_append
;
- Bijectivelist: a replacement for the
list
class for use with 1-1 relationshipswhere index lookup via
dict
makes sense; and
- ShelveBijectivelist: a replacement for the
list
class for use with 1-1 relationshipswhere index lookup via
shelve
makes sense. This class usesshelve
to cope with cases where adict
would be too large to store in memory.
AUTHORS:
Paul Leopardi (2016-08-21): initial version
- class boolean_cayley_graphs.containers.BijectiveList(other_list=None)[source]
Bases:
sage.structure.sage_object.SageObject
,boolean_cayley_graphs.saveable.Saveable
Replacement for the
list
class with only a few methods, such as__getitem__
,index
, andindex_append
.List lookup for
__getitem__
uses a list named_item
. Index lookup forindex
andindex_append
uses a dict named_index`. This class is used for 1-1 relationships where index lookup via ``dict
makes sense.Warning
Initialization from a non-empty list can easily break the 1-1 relationship between index and item in a
BijectiveList
.EXAMPLES:
Initialize from a list.
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList(["1","2","3"]) sage: BL.get_list() ['1', '2', '3'] sage: dict(sorted(BL.get_dict().items())) {'1': 0, '2': 1, '3': 2} sage: del BL
TESTS:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: L = BijectiveList([1,2,4]) sage: print(L) BijectiveList(1,2,4) sage: from boolean_cayley_graphs.containers import BijectiveList sage: L = BijectiveList([1,2,4]) sage: latex(L) \text{\texttt{BijectiveList(1,2,4)}}
- close_dict()[source]
Dummy method to match the interface of
ShelveBijectiveList
.TESTS:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList(["1","2","6"]) sage: BL.close_dict() sage: BL.remove_dict()
- get_dict()[source]
Get the
dict
part of theBijectiveList
.INPUT:
self
– the current object.
EXAMPLES:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList([1,2,5]) sage: dict(sorted(BL.get_dict().items())) {1: 0, 2: 1, 5: 2} sage: del BL
- get_list()[source]
Get the
list
part of theBijectiveList
.INPUT:
self
– the current object.
EXAMPLES:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList([1,2,5]) sage: BL.get_list() [1, 2, 5] sage: del BL
- index(item)[source]
Return the index of a given item.
Use a
dict
lookup using_index
instead of callingindex
on the list. If thedict
lookup yields aKeyError
then raise aValueError
.INPUT:
self
– the current object.item
– the item to look up.
OUTPUT:
A non-negative integer indicating the index of
item
withinself
.EXAMPLES:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList([1,2,4]) sage: BL.index(2) 1 sage: BL.get_list() [1, 2, 4] sage: dict(sorted(BL.get_dict().items())) {1: 0, 2: 1, 4: 2} sage: del BL
TESTS:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList([1,2,4]) sage: try: ....: BL.index(3) ....: except ValueError as e: ....: print("ValueError: {0}".format(e.args[0])) ....: finally: ....: del BL ValueError: 3 is not in list
- index_append(item)[source]
Return the index of a given item, appending it if necessary.
Use a
dict
lookup using_index
instead of callingindex
on the list. If the dict lookup yields a KeyError` then set result to the length ofself
, append item toself
, and add result to_index
.INPUT:
self
– the current object.item
– the item to look up, and append if necessary.
OUTPUT:
A non-negative integer indicating the index of
item
withinself
.EFFECT:
The item
item
may be appended toself
.EXAMPLES:
sage: from boolean_cayley_graphs.containers import BijectiveList sage: BL = BijectiveList([1,2,4]) sage: BL.index_append(2) 1 sage: BL.get_list() [1, 2, 4] sage: BL.index_append(3) 3 sage: BL.get_list() [1, 2, 4, 3] sage: dict(sorted(BL.get_dict().items())) {1: 0, 2: 1, 3: 3, 4: 2} sage: del BL
- class boolean_cayley_graphs.containers.List(iterable=(), /)[source]
Bases:
list
,sage.structure.sage_object.SageObject
,boolean_cayley_graphs.saveable.Saveable
Subclass of
list
with added methods, such asindex_append
.TESTS:
sage: from boolean_cayley_graphs.containers import List sage: L = List([1,2,4]) sage: print(L) [1, 2, 4] sage: from boolean_cayley_graphs.containers import List sage: L = List([1,2,4]) sage: latex(L) \text{\texttt{[1,{ }2,{ }4]}}
- index_append(item)[source]
Return the index of a given item, appending it if necessary.
If the inherited list
index
method forself
yields aValueError`, then set result to the length of `self
, and append item toself
.INPUT:
self
– the current object.item
– the item to look up, and append if necessary.
OUTPUT:
A non-negative integer indicating the index of
item
withinself
.EFFECT:
The item
item
may be appended toself
.EXAMPLES:
sage: from boolean_cayley_graphs.containers import List sage: L = List([1,2,4]) sage: L.index_append(2) 1 sage: L [1, 2, 4] sage: L.index_append(3) 3 sage: L [1, 2, 4, 3] sage: del L
- class boolean_cayley_graphs.containers.ShelveBijectiveList(other_list=None)[source]
Bases:
boolean_cayley_graphs.containers.BijectiveList
Replacement for the
list
class with only a few methods, such as__getitem__
,index
, andindex_append
.List lookup for
__getitem__
uses a list named_item
. Index lookup forindex
andindex_append
uses ashelve
named_index
. This class is used for 1-1 relationships where index lookup viashelve
makes sense.Note
This class uses
shelve
to cope with situations where adict
would be too large to fit into memory.Warning
Initialization from a non-empty list works only for lists of strings.
Warning
Initialization from a non-empty list can easily break the 1-1 relationship between index and item in a
ShelveBijectiveList
.EXAMPLES:
Initialize from a list.
sage: from boolean_cayley_graphs.containers import ShelveBijectiveList sage: SBL = ShelveBijectiveList(["1","2","4"]) sage: SBL.get_list() ['1', '2', '4'] sage: dict(sorted(SBL.get_dict().items())) {'1': 0, '2': 1, '4': 2} sage: del SBL
TESTS:
sage: from boolean_cayley_graphs.containers import ShelveBijectiveList sage: L = ShelveBijectiveList(["1","2","4"]) sage: print(L) ShelveBijectiveList('1','2','4') sage: from boolean_cayley_graphs.containers import ShelveBijectiveList sage: L = ShelveBijectiveList(["1","2","4"]) sage: latex(L) \text{\texttt{ShelveBijectiveList('1','2','4')}}
- close_dict()[source]
Synchronize and close the persistent dictionary on disk.
TESTS:
sage: from boolean_cayley_graphs.containers import ShelveBijectiveList sage: SBL = ShelveBijectiveList(["1","2","6"]) sage: SBL.close_dict() sage: SBL.remove_dict()
- remove_dict()[source]
Remove the files used for the persistent dictionary on disk.
Warning
Use
close_dict
first.TESTS:
sage: import glob sage: from boolean_cayley_graphs.containers import ShelveBijectiveList sage: SBL = ShelveBijectiveList(["1","2","6"]) sage: SBL.close_dict() sage: SBL.remove_dict() sage: glob.glob(SBL.shelve_file_name + "*") []