root/branch/UMPA/umpa/utils/bits.py @ 3617

Revision 3617, 2.6 kB (checked in by getxsick, 5 years ago)

Cleaning docstrings.

There are still some warnings while generating documents:
1 -- strange warning if there are @return or @rtype in the method which is used to property
2 -- hidden umpa.protocols.* by umpa.protocols.*.* (importing classes of the same name as modules)

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2008 Adriano Monteiro Marques.
5#
6# Author: Bartosz SKOWRON <getxsick at gmail dot com>
7#
8# This library is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published
10# by the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16# License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this library; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22"""
23Functions related with parsing bits of numbers.
24"""
25
26BYTE = 8
27
28def split_into_chunks(number, number_length, chunks_size=BYTE):
29    """
30    Split the big number into small chunks.
31
32    The number_length argument isn't odd as you may think in the first time.
33    We could calculte it but what if the number is 10 (4 bits) but the number
34    length should be 32 bits? That why, number_length is necessary.
35   
36    @type number: C{int}
37    @param number: the number which will be splitted.
38
39    @type number_length: C{int}
40    @param number_length: length of the number in bits
41
42    @type chunks_size: C{int}
43    @param chunks_size: size of the single chunk (default: 8 (1 byte) )
44
45    @rtype: C{int}
46    @return: splitted chunks.
47    """
48   
49    mask = 2**chunks_size - 1
50    bytes = number_length / chunks_size
51    return [ (number & (mask << (chunks_size*i))) >> chunks_size*i
52                                    for i in reversed(xrange(bytes)) ]
53
54def get_bits(number, bits, offset=0, rev_offset=False):
55    """
56    Return n bits from the number ragarding to offset.
57   
58    By default offset is from the left side.
59    It is recommended to use right side offset (works much faster).
60
61    @type number: C{int}
62    @param number: the number which will be parsed.
63
64    @type bits: C{int}
65    @param bits: number of bits for the return
66
67    @type offset: C{int}
68    @param offset: offset of the bits (default: 0)
69
70    @type rev_offset: C{bool}
71    @param rev_offset: a direction of the offset (default: from left to right).
72
73    @rtype: C{int}
74    @return: grabbed n bits from the number
75    """
76
77    if not rev_offset:
78        length = 0
79        while number >= 2**length:
80            length += 1
81        offset = length - offset - bits
82    return (number & (2**bits-1 << offset)) >> offset
Note: See TracBrowser for help on using the browser.