~saurabhanandiit/gtg/exportFixed

« back to all changes in this revision

Viewing changes to GTG/tools/twokeydict.py

Merge of my work on liblarch newbase and all the backends ported to liblarch
(which mainly means porting the datastore).
One failing test, will check it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
#
 
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
 
9
# version.
 
10
#
 
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
 
14
# details.
 
15
#
 
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
# -----------------------------------------------------------------------------
 
19
 
 
20
'''
 
21
Contains TwoKeyDict, a Dictionary which also has a secondary key
 
22
'''
 
23
 
 
24
from GTG.tools.bidict import BiDict
 
25
 
 
26
 
 
27
 
 
28
class TwoKeyDict(object):
 
29
    '''
 
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
 
33
    it as:
 
34
        twokey['2'] ==> 'two'
 
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.
 
39
    '''
 
40
 
 
41
 
 
42
    def __init__(self, *triplets):
 
43
        '''
 
44
        Creates the TwoKeyDict and optionally populates it with some data
 
45
 
 
46
        @oaram triplets: tuples for populating the TwoKeyDict. Format:
 
47
                         ((key1, key2, data_to_store), ...)
 
48
        '''
 
49
        super(TwoKeyDict, self).__init__()
 
50
        self._key_to_key_bidict = BiDict()
 
51
        self._primary_to_value = {}
 
52
        for triplet in triplets:
 
53
            self.add(triplet)
 
54
 
 
55
    def add(self, triplet):
 
56
        '''
 
57
        Adds a new triplet to the TwoKeyDict
 
58
 
 
59
        @param triplet: a tuple formatted like this:
 
60
                        (key1, key2, data_to_store)
 
61
        '''
 
62
        self._key_to_key_bidict.add((triplet[0], triplet[1]))
 
63
        self._primary_to_value[triplet[0]] = triplet[2]
 
64
 
 
65
    def _get_by_primary(self, primary):
 
66
        '''
 
67
        Gets the stored data given the primary key
 
68
 
 
69
        @param primary: the primary key
 
70
        @returns object: the stored object
 
71
        '''
 
72
        return self._primary_to_value[primary]
 
73
 
 
74
    def _get_by_secondary(self, secondary):
 
75
        '''
 
76
        Gets the stored data given the secondary key
 
77
 
 
78
        @param secondary: the primary key
 
79
        @returns object: the stored object
 
80
        '''
 
81
        primary = self._key_to_key_bidict._get_by_second(secondary)
 
82
        return self._get_by_primary(primary)
 
83
 
 
84
    def _remove_by_primary(self, primary):
 
85
        '''
 
86
        Removes a triplet given the rpimary key.
 
87
 
 
88
        @param primary: the primary key
 
89
        '''
 
90
        del self._primary_to_value[primary]
 
91
        self._key_to_key_bidict._remove_by_first(primary)
 
92
 
 
93
    def _remove_by_secondary(self, secondary):
 
94
        '''
 
95
        Removes a triplet given the rpimary key.
 
96
 
 
97
        @param secondary: the primary key
 
98
        '''
 
99
        primary = self._key_to_key_bidict._get_by_second(secondary)
 
100
        self._remove_by_primary(primary)
 
101
 
 
102
    def _get_secondary_key(self, primary):
 
103
        '''
 
104
        Gets the secondary key given the primary
 
105
 
 
106
        @param primary: the primary key
 
107
        @returns object: the secondary key
 
108
        '''
 
109
        return self._key_to_key_bidict._get_by_first(primary)
 
110
 
 
111
    def _get_primary_key(self, secondary):
 
112
        '''
 
113
        Gets the primary key given the secondary
 
114
 
 
115
        @param secondary: the secondary key
 
116
        @returns object: the primary key
 
117
        '''
 
118
        return self._key_to_key_bidict._get_by_second(secondary)
 
119
 
 
120
    def _get_all_primary_keys(self):
 
121
        '''
 
122
        Returns all primary keys
 
123
        
 
124
        @returns list: list of all primary keys
 
125
        '''
 
126
        return self._key_to_key_bidict._get_all_first()
 
127
 
 
128
    def _get_all_secondary_keys(self):
 
129
        '''
 
130
        Returns all secondary keys
 
131
        
 
132
        @returns list: list of all secondary keys
 
133
        '''
 
134
        return self._key_to_key_bidict._get_all_second()
 
135