示例#1
0
  def test_valid_grouping(self):
    got = functional.chunks("aaaabbbbccccdddd", 4)
    expected = ["aaaa", "bbbb", "cccc", "dddd"]
    self.assertEqual(list(got), expected)

    got = functional.chunks([1, 1, 1, 2, 2, 2, 3, 3, 3], 3)
    expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
    self.assertEqual(list(got), expected)
示例#2
0
  def test_valid_grouping(self):
    got = chunks("aaaabbbbccccdddd", 4)
    expected = ["aaaa", "bbbb", "cccc", "dddd"]
    self.assertEqual(list(got), expected)

    got = chunks([1, 1, 1, 2, 2, 2, 3, 3, 3], 3)
    expected = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
    self.assertEqual(list(got), expected)
示例#3
0
  def test_filler_None(self):
    got = chunks("aaaabbbccccddd", 4, None)
    self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd"])

    self.assertEqual(tuple(chunks((1, 1, 1, 2, 2), 3, None)),
      ((1, 1, 1, ), (2, 2, None)))

    self.assertEqual(tuple(chunks([1, 1, 1, 2, 2], 3, None)),
      ([1, 1, 1, ], [2, 2, None]))
示例#4
0
  def test_filler_None(self):
    got = functional.chunks("aaaabbbccccddd", 4, None)
    self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd"])

    self.assertEqual(tuple(functional.chunks((1, 1, 1, 2, 2), 3, None)),
      ((1, 1, 1, ), (2, 2, None)))

    self.assertEqual(tuple(functional.chunks([1, 1, 1, 2, 2], 3, None)),
      ([1, 1, 1, ], [2, 2, None]))
示例#5
0
文件: __init__.py 项目: RoboTeddy/mom
def bin_decode(encoded):
  """
  Decodes binary-encoded bytes into raw bytes.

  :param encoded:
      Binary representation.
  :returns:
      Raw bytes.
  """
  if not is_bytes(encoded):
    raise TypeError("argument must be bytes: got %r" %
                    type(encoded).__name__)
  return binascii.a2b_hex(EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble]
  for nibble in chunks(encoded, 4)))
示例#6
0
def bin_decode(encoded):
    """
  Decodes binary-encoded bytes into raw bytes.

  :param encoded:
      Binary representation.
  :returns:
      Raw bytes.
  """
    if not builtins.is_bytes(encoded):
        raise TypeError("argument must be bytes: got %r" %
                        type(encoded).__name__)
    return binascii.a2b_hex(
        EMPTY_BYTE.join(_BIN_TO_HEX_LOOKUP[nibble]
                        for nibble in functional.chunks(encoded, 4)))
示例#7
0
 def test_odd_ball_grouping(self):
   got = chunks("aaabb", 3)
   self.assertEqual(list(got), ["aaa", "bb"])
示例#8
0
 def test_returns_generator_object(self):
   self.assertEqual(type(chunks("aaaabbbb", 4)).__name__, "generator")
示例#9
0
 def test_filler_iterable_not_same_type_as_filler(self):
   #self.assertRaises(TypeError, list, chunks("aaaabbbccccddd", 4, None))
   self.assertRaises(TypeError, tuple, chunks((1, 1, 1, 2, 2), 3, [None, ]))
示例#10
0
  def test_filler(self):
    got = chunks("aaaabbbccccddd", 4, "-")
    self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd--"])

    self.assertEqual(tuple(chunks((1, 1, 1, 2, 2), 3, (True,))),
      ((1, 1, 1, ), (2, 2, True)))
示例#11
0
 def test_odd_ball_grouping(self):
   got = functional.chunks("aaabb", 3)
   self.assertEqual(list(got), ["aaa", "bb"])
示例#12
0
 def test_returns_generator_object(self):
   self.assertEqual(type(functional.chunks("aaaabbbb", 4)).__name__, "generator")
示例#13
0
 def test_filler_iterable_not_same_type_as_filler(self):
   #self.assertRaises(TypeError, list, functional.chunks("aaaabbbccccddd", 4, None))
   self.assertRaises(TypeError, tuple, functional.chunks((1, 1, 1, 2, 2), 3, [None, ]))
示例#14
0
  def test_filler(self):
    got = functional.chunks("aaaabbbccccddd", 4, "-")
    self.assertEqual(list(got), ["aaaa", "bbbc", "cccd", "dd--"])

    self.assertEqual(tuple(functional.chunks((1, 1, 1, 2, 2), 3, (True,))),
      ((1, 1, 1, ), (2, 2, True)))