comparison hglib/util.py @ 9:5882a698ad5c

util: rewrite eatlines (faster and simpler version)
author Idan Kamara <idankk86@gmail.com>
date Wed, 10 Aug 2011 01:48:38 +0300
parents 3ac38d500d68
children 19d2c55c3928
comparison
equal deleted inserted replaced
8:3ac38d500d68 9:5882a698ad5c
1 import itertools 1 import itertools, cStringIO
2 2
3 def grouper(n, iterable): 3 def grouper(n, iterable):
4 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' 4 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
5 args = [iter(iterable)] * n 5 args = [iter(iterable)] * n
6 return itertools.izip(*args) 6 return itertools.izip(*args)
7 7
8 def eatlines(s, n): 8 def eatlines(s, n):
9 idx = 0 9 """
10 for i in xrange(n): 10 >>> eatlines("1\\n2", 1)
11 idx = s.find('\n', idx) + 1 11 '2'
12 >>> eatlines("1\\n2", 2)
13 ''
14 >>> eatlines("1\\n2", 3)
15 ''
16 >>> eatlines("1\\n2\\n3", 1)
17 '2\\n3'
18 """
19 cs = cStringIO.StringIO(s)
12 20
13 return s[idx:] 21 for line in cs:
22 n -= 1
23 if n == 0:
24 return cs.read()
25 return ''
14 26
15 def cmdbuilder(name, *args, **kwargs): 27 def cmdbuilder(name, *args, **kwargs):
16 """ 28 """
17 A helper for building the command arguments 29 A helper for building the command arguments
18 30