重构一个缓冲的类...... [英] Refactor a buffered class...
问题描述
你好,
我正在寻找这种行为,我写了一段有效的代码,
但它看起来很奇怪。有人可以帮我重构一下吗?
我想通过一系列N(N = 3以下)
来浏览一系列物品。我有明确的序列结束标记(这里是''。'')
,可以是任意长度,由单词组成。
for:s =" this。是一个。测试到。检查一下。工作。好 。看起来很像
..喜欢。
输出应该是(如果按3分组),如:
=这个。
=这个。是一个。
=这个。是一个。测试到。
=是。测试到。检查一下。
=测试到。检查一下。工作。
=检查一下。工作。好吧。
=工作。好 。它看起来。
=好吧。它看起来 。喜欢。
我的代码:
import sys
class MyBuffer:
def __init __(自我):
self.acc = []
self.sentries = [0,]
def append(self,item):
self.acc.append(item)
def addSentry(self):
self.sentries。追加(len(self.acc))
打印> sys.stderr," \t",self.sentries
def checkSentry(self,size,keepFirst ):
n = len(self.sentries) - 1
if keepFirst且n<尺寸:
返回self.acc
如果n%尺寸== 0:
result = self.acc
first = self.sentries [1]
self.acc = self.acc [first:]
self.sentries = [x - self.sentries中x的第一个]
self.sentries.pop(0)
返回结果
s =" this。是一个。测试到。检查一下。工作。好 。它看起来。
喜欢。"
l = s.split()
打印l
mb = MyBuffer()
n = 0
for x in l:
mb.append(x)
如果x ==''。'':
#end of something
print" +",n
n + = 1
mb.addSentry()
current = mb.checkSentry(3,True)#GROUPING BY 3
if current:
print" =>",current
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
推荐答案
lh ***** @ yahoo.fr 写道:
您好,
我正在寻找这种行为,我写了一段有效的代码,
但它看起来很奇怪。有人可以帮我重构一下吗?
我想通过一系列N(N = 3以下)
来浏览一系列物品。我有明确的序列结束标记(这里是''。'')
,可以是任意长度,由单词组成。
for:s =" this。是一个。测试到。检查一下。工作。好 。看起来好像是b $ b。比如。
输出应该是(如果分组为3),如:
=这个。
=这个。是一个。
=这个。是一个。测试到。
=是。测试到。检查一下。
=测试到。检查一下。工作。
=检查一下。工作。好吧。
=工作。好 。它看起来。
=好吧。它看起来 。喜欢。
我的代码:
import sys
class MyBuffer:
def __init __(自我):
self.acc = []
self.sentries = [0,]
def append(self,item):
self.acc.append(item)
def addSentry(self):
self.sentries。追加(len(self.acc))
打印> sys.stderr," \t",self.sentries
def checkSentry(self,size,keepFirst ):
n = len(self.sentries) - 1
if keepFirst且n<尺寸:
返回self.acc
如果n%尺寸== 0:
result = self.acc
first = self.sentries [1]
self.acc = self.acc [first:]
self.sentries = [x - self.sentries中x的第一个]
self.sentries.pop(0)
返回结果
s =" this。是一个。测试到。检查一下。工作。好 。它看起来。
喜欢。"
l = s.split()
打印l
mb = MyBuffer()
n = 0
for x in l:
mb.append(x)
如果x ==''。'':
#end of something
print" +",n
n + = 1
mb.addSentry()
current = mb.checkSentry(3,True)#GROUPING BY 3
if current:
print" =>",current
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
如果你只是需要遍历项目列表,那么你的缓冲类并且
辅助函数似乎不必要复杂。一个生成器可以做到这一点,
类似于:
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:
>> def chunker(s,chunk_size = 3,sentry ="。"):
>>def chunker(s, chunk_size=3, sentry="."):
.... buffer = []
.... sentry_count = 0
....对于s中的项目:
.... buffer。追加(项目)
.... if item == sentry:
.... sentry_count + = 1
.... if sentry_count chunk_size:
.... del buffer [:buffer.index(sentry)+1]
.... yield buffer
....
.... buffer=[]
.... sentry_count = 0
.... for item in s:
.... buffer.append(item)
.... if item == sentry:
.... sentry_count += 1
.... if sentry_count chunk_size:
.... del buffer[:buffer.index(sentry)+1]
.... yield buffer
....
>> s =" this 。是一个。测试到。检查一下。工作。好 。它看起来。喜欢。
for p in chunker(s.split()):print" " .join(p)
>>s = "this . is a . test to . check if it . works . well . it looks. like ."
for p in chunker(s.split()): print " ".join(p)
....
this。
这个。这是一个。
。是一个。测试到。
是一个。测试到。检查一下。
测试到。检查一下。工作。
检查一下。工作。好吧。
有效。好 。它看起来。喜欢 。
....
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 .
>>>
>>>
HTH
Michael
HTH
Michael
Michael Spenceraécrit:
Michael Spencer a écrit :
如果你只是需要浏览一个项目列表,那么你的缓冲类和
辅助函数似乎不必要复杂。一个发电机可以做到这一点,
类似于:
If you just need to ''walk across a list of items'', then your buffer classand
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.
>
这是另一个版本,
类ChunkeredBuffer:
def __init __(个体经营):
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]
如果不是keepFirst和len(self.sentries)< chunkSize:
返回
如果忘了!= []:
last =忘记[-1]
self .buffer = self.buffer [last:]
self.sentries = [x - 最后x为self.sentries [1:]]
print> sys。 stderr,self.sentries,len(self.sentries),忘了
返回self.buffer
但是我想知道我怎么能添加,最后的项目如果需要:
看起来。喜欢。
喜欢。
到上一个:
这个。
这个 。这是一个。
。是一个。测试到。
是一个。测试到。检查一下。
测试到。检查一下。工作。
检查一下。工作。好吧。
有效。好 。它看起来像。
有:
这个。
这个。这是一个。
。是一个。测试到。
是一个。测试到。检查一下。
测试到。检查一下。工作。
检查一下。工作。好吧。
有效。好 。它看起来像。
看起来。喜欢。
喜欢。
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 .
这篇关于重构一个缓冲的类......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!