Improved container classes
The containers module defines improved container classes, such as lists:
List: a subclass of the builtin
listclass, with added methods, such asindex_append;
- Bijectivelist: a replacement for the
 listclass for use with 1-1 relationshipswhere index lookup via
dictmakes sense; and
- ShelveBijectivelist: a replacement for the
 listclass for use with 1-1 relationshipswhere index lookup via
shelvemakes sense. This class usesshelveto cope with cases where adictwould 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.SaveableReplacement for the
listclass with only a few methods, such as__getitem__,index, andindex_append.List lookup for
__getitem__uses a list named_item. Index lookup forindexandindex_appenduses a dict named_index`. This class is used for 1-1 relationships where index lookup via ``dictmakes 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
dictpart 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
listpart 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
dictlookup using_indexinstead of callingindexon the list. If thedictlookup yields aKeyErrorthen raise aValueError.INPUT:
self– the current object.item– the item to look up.
OUTPUT:
A non-negative integer indicating the index of
itemwithinself.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
dictlookup using_indexinstead of callingindexon 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
itemwithinself.EFFECT:
The item
itemmay 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.SaveableSubclass of
listwith 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
indexmethod forselfyields 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
itemwithinself.EFFECT:
The item
itemmay 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.BijectiveListReplacement for the
listclass with only a few methods, such as__getitem__,index, andindex_append.List lookup for
__getitem__uses a list named_item. Index lookup forindexandindex_appenduses ashelvenamed_index. This class is used for 1-1 relationships where index lookup viashelvemakes sense.Note
This class uses
shelveto cope with situations where adictwould 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_dictfirst.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 + "*") []