comparison hglib/util.py @ 150:b94e1263836c

util: introduce strtobytes() (issue4520) The strtobytes() function takes an object, gets its string representation, and then convert that to bytes.
author Brett Cannon <brett@python.org>
date Thu, 19 Mar 2015 17:42:26 -0400
parents 958307b30af3
children 0808bb03add5
comparison
equal deleted inserted replaced
149:958307b30af3 150:b94e1263836c
11 return s.encode('latin-1') 11 return s.encode('latin-1')
12 else: 12 else:
13 def b(s): 13 def b(s):
14 """Encode the string as bytes.""" 14 """Encode the string as bytes."""
15 return s 15 return s
16
17 def strtobytes(s):
18 """Return the bytes of the string representation of an object."""
19 return str(s).encode('latin-1')
16 20
17 def grouper(n, iterable): 21 def grouper(n, iterable):
18 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] ''' 22 ''' list(grouper(2, range(4))) -> [(0, 1), (2, 3)] '''
19 args = [iter(iterable)] * n 23 args = [iter(iterable)] * n
20 return itertools.izip(*args) 24 return itertools.izip(*args)