/examples/capsule/utils.py

http://echo-nest-remix.googlecode.com/ · Python · 24 lines · 6 code · 3 blank · 15 comment · 3 complexity · dccd910da256aed8dae538f8ebe7ee7f MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. utils.py
  5. Created by Jason Sundram, on 2010-04-05.
  6. Copyright (c) 2010 The Echo Nest. All rights reserved.
  7. """
  8. def flatten(l):
  9. """ Converts a list of tuples to a flat list.
  10. e.g. flatten([(1,2), (3,4)]) => [1,2,3,4]
  11. """
  12. return [item for pair in l for item in pair]
  13. def tuples(l, n=2):
  14. """ returns n-tuples from l.
  15. e.g. tuples(range(4), n=2) -> [(0, 1), (1, 2), (2, 3)]
  16. """
  17. return zip(*[l[i:] for i in range(n)])
  18. def rows(m):
  19. """returns the # of rows in a numpy matrix"""
  20. return m.shape[0]