Esempio n. 1
0
    def populate_geoaddresses(cls, addresses):
        for addresses in iter_as_slices(addresses, 50):
            create = []
            update = []

            # TODO: regroup queries ?
            for address in addresses:
                try:
                    geoaddress = address.geoaddress

                    if geoaddress.latitude is None:
                        update.append(geoaddress)
                except GeoAddress.DoesNotExist:
                    create.append(GeoAddress(address=address))

            towns = Town.search_all([geo.address for geo in chain(create, update)])

            for geoaddress, town in zip(chain(create, update), towns):
                geoaddress.set_town_position(town)

            GeoAddress.objects.bulk_create(create)

            # TODO: only if has changed
            # TODO: bulk_update() ?
            with atomic():
                for geoaddress in update:
                    geoaddress.save(force_update=True)
Esempio n. 2
0
    def test_iter_as_slices02(self):
        assert len(self.DATA_UNIX) % 5 == 0
        chunks = [*chunktools.iter_as_slices(self.DATA_UNIX, 5)]

        self.assertEqual(16, len(chunks))

        for i, chunk in enumerate(chunks):
            self.assertEqual(5, len(chunk), f'Bad size for chunk {i}: {chunk}')

        self.assertEqual(self.DATA_UNIX, ''.join(chunks))
Esempio n. 3
0
    def _do_action(cls, sending, request, action, step):
        ws = cls._connect(sending)

        if not ws:
            return

        # TODO: use FlowPaginator instead
        for chunk in chunktools.iter_as_slices(request, 256):
            action(ws, sending, chunk)

        cls._disconnect(ws)
Esempio n. 4
0
    def test_iter_as_slices02(self):
        assert len(self.DATA_UNIX) % 5 == 0
        chunks = list(chunktools.iter_as_slices(self.DATA_UNIX, 5))

        self.assertEqual(16, len(chunks))

        for i, chunk in enumerate(chunks):
            self.assertEqual(5, len(chunk),
                             'Bad size for chunk {}: {}'.format(i, chunk))

        self.assertEqual(self.DATA_UNIX, ''.join(chunks))
Esempio n. 5
0
    def test_iter_as_slices03(self):
        data = self.DATA_UNIX + '9'
        assert len(data) % 5 == 1
        chunks = [*chunktools.iter_as_slices(data, 5)]

        self.assertEqual(17, len(chunks))

        for i, chunk in enumerate(chunks[:-1]):
            self.assertEqual(5, len(chunk), f'Bad size for chunk {i}: {chunk}')

        self.assertEqual('9', chunks[-1])
        self.assertEqual(data, ''.join(chunks))
Esempio n. 6
0
    def test_iter_as_slices03(self):
        data = self.DATA_UNIX + '9'
        assert len(data) % 5 == 1
        chunks = list(chunktools.iter_as_slices(data, 5))

        self.assertEqual(17, len(chunks))

        for i, chunk in enumerate(chunks[:-1]):
            self.assertEqual(5, len(chunk),
                             'Bad size for chunk {}: {}'.format(i, chunk))

        self.assertEqual('9', chunks[-1])
        self.assertEqual(data, ''.join(chunks))
Esempio n. 7
0
    def test_iter_as_slices01(self):
        chunks = [*chunktools.iter_as_slices(self.DATA_UNIX, 1000)]

        self.assertEqual(1, len(chunks))
        self.assertEqual(self.DATA_UNIX, ''.join(chunks))