Вопрос

I have a class I and another class X

class X:

class X(I):
    userUrls = self.loadGivenDomainPaths

    def compute_path_amount(self):
        if count(userUrls) > 10:
            #try:
                #Slice urls into list of 10s from userUrls
            #except:
                #error occurred.
        else:
            #blah

How do code the try() block above to Slice the results of loadGivenDomainPaths as defined as:

PathsOfDomain.objects.filter(TheFK=user, a=True)

I want to slice results into sets of 10 results (each result is a string) a dictionary could possibly work, just at a bit of loss for syntax here.

Update I've also thought about some way to use a for() loop to count 10 objects (from the django query like:

def get_10_paths(self):
    for obj in domainPaths:
        return objs

The problem is this would only return an object at a time, not a set of 10. I'm rather confused on how to approach this still

Any helpful code snippets, suggestions, and/or links would be helpful.

Thank you!

Это было полезно?

Решение

Well, you can combine slicing with for loop:

def compute_path_amount(self):
    return [
        self.__class__.userUrls[i:i+9] for i in xrange(
            0, len(self.__class__.userUrls), 10
        )
    ]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top