kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

add a pad(length) convenience function? #112

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It would be nice to have a pad(length,value) function that would pad the 
bitstring on the left (or that could be made into an argument too) with 0s or 
1s to the specified length.

About like this, from my wrapper class:

def pad(bitstr,length,value=0):
    length_diff = length - len(bitstr)
    if length_diff==0:
        pass
    elif length_diff < 0:
        raise ValueError("Can't pad the codeword to a length lower than its current length!")
    else:
        bitstr.prepend('0b' + length_diff * str(int(value)))

Original issue reported on code.google.com by pat...@gmail.com on 21 Jun 2011 at 7:34

GoogleCodeExporter commented 9 years ago
Hi, thanks for the suggestion. I've had similar thoughts before, and your pad 
method is similar to the fill method in Issue 98 (so I'm merging the issues).

Incidentally these are almost equivalent:

pad(s, length, value)

s.prepend((length-s.len)*[value])

Original comment by dr.scott...@gmail.com on 21 Jun 2011 at 8:59

GoogleCodeExporter commented 9 years ago
Oh, they are!  Thanks.  I'll keep an eye on Issue 98.

Original comment by pat...@gmail.com on 21 Jun 2011 at 11:11