Beispiel #1
0
class OrderedDictTest(ATTestCase):

    def afterSetUp(self):
        self.d = OrderedDict()
        self.d['a'] = '1'
        self.d['b'] = '2'
        self.d['c'] = '3'

    def test_order(self):
        self.assertTrue(self.d.keys() == ['a', 'b', 'c'])
        self.assertTrue(self.d.values() == ['1', '2', '3'])
        self.assertTrue(self.d.items() == [('a', '1'), ('b', '2'), ('c', '3')])
        self.assertTrue(self.d.reverse() == [('c', '3'), ('b', '2'), ('a', '1')])

    def test_setitem(self):
        self.d['d'] = '4'
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', '2', '3', '4'])
        self.assertTrue(self.d.items() == [('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [('d', '4'), ('c', '3'), ('b', '2'), ('a', '1')])

        self.d['c'] = 'C'
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', '2', 'C', '4'])
        self.assertTrue(self.d.items() == [('a', '1'), ('b', '2'), ('c', 'C'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [('d', '4'), ('c', 'C'), ('b', '2'), ('a', '1')])

    def test_del(self):
        del self.d['b']
        self.assertTrue(self.d.keys() == ['a', 'c'])
        self.assertTrue(self.d.values() == ['1', '3'])
        self.assertTrue(self.d.items() == [('a', '1'), ('c', '3')])
        self.assertTrue(self.d.reverse() == [('c', '3'), ('a', '1')])
        self.assertTrue('b' not in self.d)
        self.assertTrue(self.d.get('b', None) == None)

    def test_clear(self):
        self.d.clear()
        self.assertTrue(self.d.keys() == [])
        self.assertTrue(self.d.values() == [])
        self.assertTrue(self.d.items() == [])
        self.assertTrue(self.d.reverse() == [])

    def test_update(self):
        d2 = {'b': 'B', 'd': '4'}
        self.d.update(d2)
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', 'B', '3', '4'])
        self.assertTrue(self.d.items() == [('a', '1'), ('b', 'B'), ('c', '3'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [('d', '4'), ('c', '3'), ('b', 'B'), ('a', '1')])

    def test_popitem(self):
        (k, v) = self.d.popitem()
        self.assertTrue(k == 'c')
        self.assertTrue(v == '3')
class AlphaBatch(object):
    """Object used to batch results alphabetically.
    """

    security = ClassSecurityInfo()

    JOKER = '*'

    vocab = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
        'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', JOKER
    ]

    threshold = 30

    def __init__(self, results, context, request):
        """Take the results and the request.
        """
        self.context = context
        self.results = results
        self.currentresults = []
        self.pagemap = OrderedDict()
        self.request = request
        self.showBatch = True

        if len(results) < self.threshold:
            self.showBatch = False
            self.currentresults = results
        else:
            self.initialize()

    def initialize(self):
        """Initialize this batch object.
        """
        current = self.request.get('currentPage', None)
        pointer = 0
        hasResults = len(self.results)
        nonresults = []

        for term in self.vocab:

            # full init for term
            self.pagemap[term] = dict()
            self.pagemap[term]['value'] = term
            self.pagemap[term]['visible'] = False
            self.pagemap[term]['current'] = False

            # special handling for joker
            if term == self.JOKER:
                continue

            # assume alpha sorted results here
            for result in self.results[pointer:]:
                title = safe_unicode(result['fullname']).upper()
                # title.replace(u'ü',u'Ü')
                # title.replace(u'ö',u'Ö')
                # title.replace(u'ä',u'Ä')

                currentTerm = title and title[0] or None
                if currentTerm is None or currentTerm not in self.vocab:
                    nonresults.append(result)
                    pointer += 1
                    continue

                if title.startswith(term):
                    self.pagemap[term]['visible'] = True
                    if current is None:
                        current = term
                    if term == current:
                        self.currentresults.append(result)
                    pointer += 1
                else:
                    break

            # check for current after processing
            if term == current:
                self.pagemap[term]['current'] = True

        if nonresults:
            self.pagemap[self.JOKER]['visible'] = True
        if current == '*':
            self.currentresults = nonresults
            self.pagemap[self.JOKER]['current'] = True

    security.declarePublic('showBatch')

    def showBatch(self):
        """Return True if results reaches threshold.
        """
        return self.showBatch

    security.declarePublic('getPages')

    def getPages(self):
        """Return a list of dicts containing page definitions.
        """
        return self.pagemap.values()

    security.declarePublic('getResults')

    def getResults(self):
        """Return the current result.
        """
        return self.currentresults
class AlphaBatch(object):
    """Object used to batch results alphabetically.
    """
    
    security = ClassSecurityInfo()
    
    JOKER = '*'

    vocab = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
             JOKER]
             
    threshold = 30
    
    
    def __init__(self, results, context, request):
        """Take the results and the request.
        """
        self.context = context
        self.results = results
        self.currentresults = []
        self.pagemap = OrderedDict()
        self.request = request
        self.showBatch = True
                
        if len(results) < self.threshold:
            self.showBatch = False
            self.currentresults = results
        else:
            self.initialize()
    
    def initialize(self):
        """Initialize this batch object.
        """
        current = self.request.get('currentPage', None)
        pointer = 0
        hasResults = len(self.results)
        nonresults = []

        for term in self.vocab:
            
            # full init for term
            self.pagemap[term] = dict()
            self.pagemap[term]['value'] = term
            self.pagemap[term]['visible'] = False
            self.pagemap[term]['current'] = False
            
            # special handling for joker
            if term == self.JOKER:
                continue

            # assume alpha sorted results here
            for result in self.results[pointer:]:                
                title = safe_unicode(result['fullname']).upper()
                # title.replace(u'ü',u'Ü')
                # title.replace(u'ö',u'Ö')
                # title.replace(u'ä',u'Ä')
                
                currentTerm = title and title[0] or None
                if currentTerm is None or currentTerm not in self.vocab:
                    nonresults.append(result)
                    pointer += 1
                    continue
                
                if title.startswith(term):
                    self.pagemap[term]['visible'] = True
                    if current is None:
                        current = term
                    if term == current:
                        self.currentresults.append(result)
                    pointer += 1
                else:
                    break
            
            # check for current after processing
            if term == current:
                self.pagemap[term]['current'] = True

        if nonresults:
            self.pagemap[self.JOKER]['visible'] = True
        if current == '*':
            self.currentresults = nonresults
            self.pagemap[self.JOKER]['current'] = True
            
    
    security.declarePublic('showBatch') 
    def showBatch(self):
        """Return True if results reaches threshold.
        """
        return self.showBatch
    
    security.declarePublic('getPages')    
    def getPages(self):
        """Return a list of dicts containing page definitions.
        """
        return self.pagemap.values()
    
    security.declarePublic('getResults')
    def getResults(self):
        """Return the current result.
        """
        return self.currentresults
Beispiel #4
0
class OrderedDictTest(ATTestCase):

    def afterSetUp(self):
        self.d = OrderedDict()
        self.d['a'] = '1'
        self.d['b'] = '2'
        self.d['c'] = '3'

    def test_order(self):
        self.assertTrue(self.d.keys() == ['a', 'b', 'c'])
        self.assertTrue(self.d.values() == ['1', '2', '3'])
        self.assertTrue(self.d.items() == [('a', '1'), ('b', '2'), ('c', '3')])
        self.assertTrue(self.d.reverse() == [
                        ('c', '3'), ('b', '2'), ('a', '1')])

    def test_setitem(self):
        self.d['d'] = '4'
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', '2', '3', '4'])
        self.assertTrue(self.d.items() == [
                        ('a', '1'), ('b', '2'), ('c', '3'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [
                        ('d', '4'), ('c', '3'), ('b', '2'), ('a', '1')])

        self.d['c'] = 'C'
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', '2', 'C', '4'])
        self.assertTrue(self.d.items() == [
                        ('a', '1'), ('b', '2'), ('c', 'C'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [
                        ('d', '4'), ('c', 'C'), ('b', '2'), ('a', '1')])

    def test_del(self):
        del self.d['b']
        self.assertTrue(self.d.keys() == ['a', 'c'])
        self.assertTrue(self.d.values() == ['1', '3'])
        self.assertTrue(self.d.items() == [('a', '1'), ('c', '3')])
        self.assertTrue(self.d.reverse() == [('c', '3'), ('a', '1')])
        self.assertTrue('b' not in self.d)
        self.assertTrue(self.d.get('b', None) == None)

    def test_clear(self):
        self.d.clear()
        self.assertTrue(self.d.keys() == [])
        self.assertTrue(self.d.values() == [])
        self.assertTrue(self.d.items() == [])
        self.assertTrue(self.d.reverse() == [])

    def test_update(self):
        d2 = {'b': 'B', 'd': '4'}
        self.d.update(d2)
        self.assertTrue(self.d.keys() == ['a', 'b', 'c', 'd'])
        self.assertTrue(self.d.values() == ['1', 'B', '3', '4'])
        self.assertTrue(self.d.items() == [
                        ('a', '1'), ('b', 'B'), ('c', '3'), ('d', '4')])
        self.assertTrue(self.d.reverse() == [
                        ('d', '4'), ('c', '3'), ('b', 'B'), ('a', '1')])

    def test_popitem(self):
        (k, v) = self.d.popitem()
        self.assertTrue(k == 'c')
        self.assertTrue(v == '3')