예제 #1
0
    def table_sizes(self, db=None, sortby=None):

        if db is None:
            db = self.get_dbs()[0]

        if sortby is None:
            sortby = 'total'

        mp = {
            'free': lambda x: x['Data_free'],
            'used': lambda x: x['Data_length'],
            'total': lambda x: x['Data_free'] + x['Data_length'],
        }
        sort_key = mp[sortby]

        addr = {}
        addr.update(self.mysql_addr)
        addr.update({
            'db': db,
        })

        pool = mysqlconnpool.make(addr)
        rsts = pool.query('show table status')

        for rst in rsts:
            self._rst_to_number(rst)

        rsts = list(rsts)
        rsts.sort(key=sort_key, reverse=True)

        rsts = [('{Data_length:>6} free-able: {Data_free:>6} {Name}'.format(
            **humannum.humannum(x)), x) for x in rsts]

        return rsts
예제 #2
0
    def test_limit_keys(self):

        cases = (
                (({'a': 10240, 'b': 10240}, {'include': ['a', 'inexistent']}),
                 {'a': '10K', 'b': 10240},
                 ''
                 ),
                (({'a': 10240, 'b': 10240}, {'exclude': ['b', 'inexistent']}),
                 {'a': '10K', 'b': 10240},
                 ''
                 ),
        )

        for _in, _out, _msg in cases:

            rst = humannum.humannum(_in[0], **_in[1])

            msg = 'in: {_in} expect: {_out}, rst: {rst}; {_msg}'.format(
                _in=repr(_in),
                _out=repr(_out),
                rst=rst,
                _msg=_msg
            )

            self.assertEqual(_out, rst, msg)
            self.assertTrue(_in is not rst, 'result must not be input')
예제 #3
0
    def test_non_primitive(self):

        cases = (
            ({
                'a': 10240
            }, {
                'a': '10K'
            }, ''),
            ([{
                'a': 10240
            }, 1024432], [{
                'a': '10K'
            }, '1000.4K'], ''),
            ([{
                'a': 'xp'
            }, 1024432], [{
                'a': 'xp'
            }, '1000.4K'], ''),
        )

        for _in, _out, _msg in cases:

            rst = humannum.humannum(_in)

            msg = 'in: {_in} expect: {_out}, rst: {rst}; {_msg}'.format(
                _in=repr(_in), _out=repr(_out), rst=rst, _msg=_msg)

            self.assertEqual(_out, rst, msg)
            self.assertTrue(_in is not rst, 'result must not be input')
예제 #4
0
    def test_get_path_usage(self):

        path = '/'
        rst = fsutil.get_path_usage(path)

        # check against os.statvfs.
        st = os.statvfs(path)

        dd(humannum.humannum(rst))

        # space is changing..

        self.assertAlmostEqual(st.f_frsize * st.f_bavail,
                               rst['available'],
                               delta=4 * 1024**2)
        self.assertAlmostEqual(st.f_frsize * st.f_blocks,
                               rst['total'],
                               delta=4 * 1024**2)
        self.assertAlmostEqual(st.f_frsize * (st.f_blocks - st.f_bavail),
                               rst['used'],
                               delta=4 * 1024**2)
        self.assertAlmostEqual((st.f_blocks - st.f_bavail) * 100 / st.f_blocks,
                               int(rst['percent'] * 100),
                               delta=4 * 1024**2)

        if st.f_bfree > st.f_bavail:
            self.assertLess(rst['available'], st.f_frsize * st.f_bfree)
        else:
            dd('st.f_bfree == st.f_bavail')

        # check against df

        rc, out, err = proc.shell_script('df -m / | tail -n1')
        # Filesystem 1M-blocks   Used Available Capacity  iused    ifree %iused  Mounted on
        # /dev/disk1    475828 328021    147556    69% 84037441 37774557   69%   /
        dd('space of "/" from df')
        total_mb = out.strip().split()[1]

        # "123.8M", keep int only
        rst_total_mb = humannum.humannum(rst['total'], unit=humannum.M)
        rst_total_mb = rst_total_mb.split('.')[0].split('M')[0]

        dd('total MB from df:', total_mb, 'result total MB:', rst_total_mb)

        self.assertAlmostEqual(int(total_mb), int(rst_total_mb), delta=4)
예제 #5
0
    def test_humanize_number(self):

        cases = (
            (0, '0', ''),
            (1, '1', ''),
            (1.0, '1.00', ''),
            (1.01, '1.01', ''),
            (1.001, '1.00', ''),
            (1023, '1023', ''),
            (1024, '1K', ''),
            (1024 + 1, '1.00K', ''),
            (1024 + 10, '1.01K', ''),
            (1024132, '1000.1K', ''),
            (1024**2, '1M', ''),
            (1024**2 + 10240, '1.01M', ''),
            (1024**3, '1G', ''),
            (1024**4, '1T', ''),
            (1024**5, '1P', ''),
            (1024**6, '1E', ''),
            (1024**7, '1Z', ''),
            (1024**8, '1Y', ''),
            (1024**9, '1024Y', ''),
            (1024**10, '1048576Y', ''),
            (1048996, '1.00M', ''),
        )

        for _in, _out, _msg in cases:

            rst = humannum.humannum(_in)

            msg = 'humanize: in: {_in} expect: {_out}, rst: {rst}; {_msg}'.format(
                _in=repr(_in), _out=repr(_out), rst=rst, _msg=_msg)

            self.assertEqual(_out, rst, msg)

            rst = humannum.humannum(-_in)

            if _in > 0:
                _out = '-' + _out
            self.assertEqual(_out, rst, msg + ': negative')

        self.assertIs(True, humannum.humannum(True))
예제 #6
0
    def test_specified_unit(self):

        cases = (
                ((1024 ** 2, {'unit': 1024}),      '1024K',   ''),
                ((1024 ** 2, {'unit': 1024**3}),   '0.001G',  ''),
        )

        for _in, _out, _msg in cases:

            rst = humannum.humannum(_in[0], **_in[1])

            msg = 'in: {_in} expect: {_out}, rst: {rst}; {_msg}'.format(
                _in=repr(_in),
                _out=repr(_out),
                rst=rst,
                _msg=_msg
            )

            self.assertEqual(_out, rst, msg)
예제 #7
0
 def _table_status_str(self, rst):
     hum = humannum.humannum(rst)
     return ('`{Name}` Data_free:{Data_free} / Data_length:{Data_length}'
             ' = {rate:.3f}').format(rate=float(rst['Data_free']) /
                                     rst['Data_length'],
                                     **hum)
예제 #8
0
from pykit import humannum


if __name__ == '__main__':
    num = 1024 ** 3 * 100
    print humannum.humannum(num)
    print humannum.humannum(num, humannum.K)
    string = humannum.humannum(num, humannum.T)
    print string
    print humannum.parsenum(string)
    print

    num1 = 1024 ** 2 * 2
    num1 = str(num1)
    print humannum.humannum(num1)
    print

    num2 = [1024214, 1515161, 616171]
    string = humannum.humannum(num2)
    print string
    # can't analysis list
    # print humannum.parseint(string)
    print

    num3 = {'1': 1024 ** 3 * 10, '2': 1024 ** 2, '3': 1024 ** 4}
    print humannum.humannum(num3,exclude=['1'])
    print humannum.humannum(num3, include=['1','2'])
    print

    num4 = '14%'
    print humannum.parsenum(num4)