Refactor a buffered class...
Hello, i'm looking for this behaviour and i write a piece of code which works, but it looks odd to me. can someone help me to refactor it ? i would like to walk across a list of items by series of N (N=3 below) of these. i had explicit mark of end of a sequence (here it is '.') which may be any length and is composed of words. for: s = "this . is a . test to . check if it . works . well . it looks . like ." the output should be (if grouping by 3) like: => this . => this . is a . => this . is a . test to . => is a . test to . check if it . => test to . check if it . works . => check if it . works . well . => works . well . it looks . => well . it looks . like . my piece of code : import sys class MyBuffer: def __init__(self): self.acc = [] self.sentries = [0, ] def append(self, item): self.acc.append(item) def addSentry(self): self.sentries.append(len(self.acc)) print >> sys.stderr, "\t", self.sentries def checkSentry(self, size, keepFirst): n = len(self.sentries) - 1 if keepFirst and n < size: return self.acc if n % size == 0: result = self.acc first = self.sentries[1] self.acc = self.acc[first:] self.sentries = [x - first for x in self.sentries] self.sentries.pop(0) return result s = "this . is a . test to . check if it . works . well . it looks . like ." l = s.split() print l mb = MyBuffer() n = 0 for x in l: mb.append(x) if x == '.': # end of something print "+", n n += 1 mb.addSentry() current = mb.checkSentry(3, True) # GROUPING BY 3 if current: print "=>", current -- http://mail.python.org/mailman/listinfo/python-list
Re: Refactor a buffered class...
Michael Spencer a écrit : > If you just need to 'walk across a list of items', then your buffer class and > helper function seem unnecessary complex. A generator would do the trick, > something like: actually for the example i have used only one sentry condition by they are more numerous and complex, also i need to work on a huge amount on data (each word are a line with many features readed from a file) but generators are interesting stuff that i'm going to look closer. thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Refactor a buffered class...
Here is another version, class ChunkeredBuffer: def __init__(self): self.buffer = [] self.sentries = [] def append(self, item): self.buffer.append(item) def chunk(self, chunkSize, keepFirst = False): self.sentries.append(len(self.buffer)) forget = self.sentries[:-chunkSize] if not keepFirst and len(self.sentries) < chunkSize: return if forget != []: last = forget[-1] self.buffer = self.buffer[last:] self.sentries = [x - last for x in self.sentries[1:]] print >> sys.stderr, self.sentries, len(self.sentries), forget return self.buffer but i was wondering how i could add, the last items if needed: it looks . like . like . to the previous: this . this . is a . this . is a . test to . is a . test to . check if it . test to . check if it . works . check if it . works . well . works . well . it looks like . to have: this . this . is a . this . is a . test to . is a . test to . check if it . test to . check if it . works . check if it . works . well . works . well . it looks like . it looks . like . like . -- http://mail.python.org/mailman/listinfo/python-list
Re: Refactor a buffered class...
oops > to have: > > this . > this . is a . > this . is a . test to . > is a . test to . check if it . > test to . check if it . works . > check if it . works . well . > works . well . it looks like . well . it looks like . it looks like . -- http://mail.python.org/mailman/listinfo/python-list
Re: Refactor a buffered class...
thanks a lot to all, i help me to learn a lot ! (i finally use the generator trick, it is great...) best regards. -- http://mail.python.org/mailman/listinfo/python-list