0

With python and 'type()', I need to reproduce this 'structure', but dynamically:

class Class1(BaseClass):

    def items(self):
        return [
                'abc',
                'def',
                'hij'
                ]

    def location(self, obj):
        return obj


class Class2(BaseClass):

    def items(self):
        return [
                'klm',
                'nop',
                'qrs'
                ]

    def location(self, obj):
        return obj


final_dict = {
    'Class1': Class1,
    'Class2': Class2,
}

Here's what I came up with:

def get_final_dict():
    list1 = ['abc', 'def', 'hij']
    list2 = ['klm', 'nop', 'qrs']
    final_dict = {}
    final_dict.update({'Class1': type("Class1", (BaseClass,), {"items": lambda self: list1, "location": lambda self, obj: obj})})
    final_dict.update({'Class2': type("Class2", (BaseClass,), {"items": lambda self: list2, "location": lambda self, obj: obj})})
    return final_dict

final_dict = get_final_dict()

Note that I didn't include the 'lists' as part of the lambda, as these are dynamically generated from a method in my 'real life' situation.

This works with this 'prototype', but apparently doesn't work with my 'real life scenario'.

Here's what I'm trying to do with Django's sitemap and 'django-static-sitemaps':

def get_sitemaps_dicts():

    nb_of_items_sql = 'SELECT count(id) FROM table'
    cursor = connection.cursor()
    cursor.execute(nb_of_items_sql)
    nb_of_items = cursor.fetchone()[0]

    if nb_of_items < 50000:
        nb_of_items = 50000

    all_classes = {}
    for i in range(nb_of_items / 50000):
        print 'item #' + str(i * 50000)

        sql = 'SELECT url FROM table ' \
              'LIMIT 50000 OFFSET %s'

        cursor.execute(sql, [i * 50000])
        items = cursor.fetchall()
        all_items = []
        for item in items:
            all_items.append(item[0])

        all_classes.update({'Items' + str(i): type("Items" + str(i), (Sitemap,), {"items": lambda self: all_items, "location": lambda self, obj: obj})})

    return all_classes

When I set 'sitemaps=get_sitemaps_dicts()', I get all the sitemaps but they are all empty (no URL at all). But, if I set it to my 'prototype', the sitemaps are generated correctly with the URLs.

The reason I'm doing this 'dynamic list' is because the static sitemap plugin will iterate over all the collection for each group of 50k items. When you have 4+ millions rows, it gets a bit long to generate.

4

0 回答 0