1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Gettings Things Gnome! - a personal organizer for the GNOME desktop
4
# Copyright (c) 2008-2009 - Lionel Dricot & Bertrand Rousseau
6
# This program is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU General Public License as published by the Free Software
8
# Foundation, either version 3 of the License, or (at your option) any later
11
# This program is distributed in the hope that it will be useful, but WITHOUT
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
# You should have received a copy of the GNU General Public License along with
17
# this program. If not, see <http://www.gnu.org/licenses/>.
18
# -----------------------------------------------------------------------------
21
Contains TwoKeyDict, a Dictionary which also has a secondary key
24
from GTG.tools.bidict import BiDict
28
class TwoKeyDict(object):
30
It's a standard Dictionary with a secondary key.
31
For example, you can add an element ('2', 'II', two'), where the
32
first two arguments are keys and the third is the stored object, and access
35
twokey['II'] ==> 'two'
36
You can also request the other key, given one.
37
Function calls start with _ because you'll probably want to rename them when
38
you use this dictionary, for the sake of clarity.
42
def __init__(self, *triplets):
44
Creates the TwoKeyDict and optionally populates it with some data
46
@oaram triplets: tuples for populating the TwoKeyDict. Format:
47
((key1, key2, data_to_store), ...)
49
super(TwoKeyDict, self).__init__()
50
self._key_to_key_bidict = BiDict()
51
self._primary_to_value = {}
52
for triplet in triplets:
55
def add(self, triplet):
57
Adds a new triplet to the TwoKeyDict
59
@param triplet: a tuple formatted like this:
60
(key1, key2, data_to_store)
62
self._key_to_key_bidict.add((triplet[0], triplet[1]))
63
self._primary_to_value[triplet[0]] = triplet[2]
65
def _get_by_primary(self, primary):
67
Gets the stored data given the primary key
69
@param primary: the primary key
70
@returns object: the stored object
72
return self._primary_to_value[primary]
74
def _get_by_secondary(self, secondary):
76
Gets the stored data given the secondary key
78
@param secondary: the primary key
79
@returns object: the stored object
81
primary = self._key_to_key_bidict._get_by_second(secondary)
82
return self._get_by_primary(primary)
84
def _remove_by_primary(self, primary):
86
Removes a triplet given the rpimary key.
88
@param primary: the primary key
90
del self._primary_to_value[primary]
91
self._key_to_key_bidict._remove_by_first(primary)
93
def _remove_by_secondary(self, secondary):
95
Removes a triplet given the rpimary key.
97
@param secondary: the primary key
99
primary = self._key_to_key_bidict._get_by_second(secondary)
100
self._remove_by_primary(primary)
102
def _get_secondary_key(self, primary):
104
Gets the secondary key given the primary
106
@param primary: the primary key
107
@returns object: the secondary key
109
return self._key_to_key_bidict._get_by_first(primary)
111
def _get_primary_key(self, secondary):
113
Gets the primary key given the secondary
115
@param secondary: the secondary key
116
@returns object: the primary key
118
return self._key_to_key_bidict._get_by_second(secondary)
120
def _get_all_primary_keys(self):
122
Returns all primary keys
124
@returns list: list of all primary keys
126
return self._key_to_key_bidict._get_all_first()
128
def _get_all_secondary_keys(self):
130
Returns all secondary keys
132
@returns list: list of all secondary keys
134
return self._key_to_key_bidict._get_all_second()