Blame view

chap1/common.py 1.6 KB
e5dbe416   Chunk   staged.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Common utils.

@author: chunk
chunkplus@gmail.com
2014 Dec
"""
__author__ = 'hadoop'

import os, sys
import time
import StringIO
import ConfigParser

import numpy as np

package_dir_imager = os.path.dirname(os.path.abspath(__file__))


class Timer():
    def __init__(self):
        self.__newtime = time.time()
        self.__oldtime = self.__newtime

    def mark(self):
        self.__oldtime = self.__newtime
        self.__newtime = time.time()
        return self.__newtime - self.__oldtime

    def report(self):
        print "%-24s%fs" % ("time elapsed:", self.mark())


def ttimer():
    newtime = time.time()
    while True:
        oldtime = newtime
        newtime = time.time()
        yield newtime - oldtime


def ctimer():
    newtime = time.clock()
    while True:
        oldtime = newtime
        newtime = time.clock()
        yield newtime - oldtime


def bytes2bits(arry_bytes):
    """
    :param arry_bytes: 1-D np.unit8 array
    :return: 1-D 0/1 array
    """
    hid_data_bits = [map(int, '{0:08b}'.format(byte)) for byte in arry_bytes]
    return np.array(hid_data_bits).ravel()


def bits2bytes(arry_bits):
    """
    :param arry_bits: 1-D 0/1 array
    :return: 1-D np.unit8 array
    """
    str_bits = ''.join(map(str, arry_bits))
    arry_bytes = [int(str_bits[i:i + 8], 2) for i in range(0, len(str_bits), 8)]
    return np.array(arry_bytes, dtype=np.uint8).ravel()


def test_grammer():
    a = 'fsaf'
    b = ['dasf', 'dff']
    c = 'dgfsfdg'
    # print a + b
    print [a] + b  # ['fsaf', 'dasf', 'dff']
    print [a] + [b]  # ['fsaf', ['dasf', 'dff']]
    print [a] + [c]  # ['fsaf', 'dgfsfdg']