comparison hglib/util.py @ 159:16496e0f3c09

util: update doctests to work with bytes (issue4520)
author Brett Cannon <brett@python.org>
date Fri, 27 Mar 2015 12:57:58 -0400
parents 2104fc9aa513
children 0f81ed8e147b
comparison
equal deleted inserted replaced
158:2104fc9aa513 159:16496e0f3c09
30 args = [iter(iterable)] * n 30 args = [iter(iterable)] * n
31 return izip(*args) 31 return izip(*args)
32 32
33 def eatlines(s, n): 33 def eatlines(s, n):
34 """ 34 """
35 >>> eatlines("1\\n2", 1) 35 >>> eatlines(b("1\\n2"), 1) == b('2')
36 '2' 36 True
37 >>> eatlines("1\\n2", 2) 37 >>> eatlines(b("1\\n2"), 2) == b('')
38 '' 38 True
39 >>> eatlines("1\\n2", 3) 39 >>> eatlines(b("1\\n2"), 3) == b('')
40 '' 40 True
41 >>> eatlines("1\\n2\\n3", 1) 41 >>> eatlines(b("1\\n2\\n3"), 1) == b('2\\n3')
42 '2\\n3' 42 True
43 """ 43 """
44 cs = BytesIO(s) 44 cs = BytesIO(s)
45 45
46 for line in cs: 46 for line in cs:
47 n -= 1 47 n -= 1
51 51
52 def skiplines(s, prefix): 52 def skiplines(s, prefix):
53 """ 53 """
54 Skip lines starting with prefix in s 54 Skip lines starting with prefix in s
55 55
56 >>> skiplines('a\\nb\\na\\n', 'a') 56 >>> skiplines(b('a\\nb\\na\\n'), b('a')) == b('b\\na\\n')
57 'b\\na\\n' 57 True
58 >>> skiplines('a\\na\\n', 'a') 58 >>> skiplines(b('a\\na\\n'), b('a')) == b('')
59 '' 59 True
60 >>> skiplines('', 'a') 60 >>> skiplines(b(''), b('a')) == b('')
61 '' 61 True
62 >>> skiplines('a\\nb', 'b') 62 >>> skiplines(b('a\\nb'), b('b')) == b('a\\nb')
63 'a\\nb' 63 True
64 """ 64 """
65 cs = BytesIO(s) 65 cs = BytesIO(s)
66 66
67 for line in cs: 67 for line in cs:
68 if not line.startswith(prefix): 68 if not line.startswith(prefix):
172 172
173 class propertycache(object): 173 class propertycache(object):
174 """ 174 """
175 Decorator that remembers the return value of a function call. 175 Decorator that remembers the return value of a function call.
176 176
177 >>> execcount = 0
177 >>> class obj(object): 178 >>> class obj(object):
178 ... def func(self): 179 ... def func(self):
179 ... print 'func' 180 ... global execcount
181 ... execcount += 1
180 ... return [] 182 ... return []
181 ... func = propertycache(func) 183 ... func = propertycache(func)
182 >>> o = obj() 184 >>> o = obj()
183 >>> o.func 185 >>> o.func
184 func
185 [] 186 []
187 >>> execcount
188 1
186 >>> o.func 189 >>> o.func
187 [] 190 []
191 >>> execcount
192 1
188 """ 193 """
189 def __init__(self, func): 194 def __init__(self, func):
190 self.func = func 195 self.func = func
191 self.name = func.__name__ 196 self.name = func.__name__
192 def __get__(self, obj, type=None): 197 def __get__(self, obj, type=None):