2

This is about Python. I am creating a BitCode class, and the string can only consist of 0's and 1's, if it does not, it will raise a ValueError with a message.

For example,

b = BitCode('1001')

is valid.

I was wondering after I write:

class Bitcode:

The test part of the code is:

class TestBitCode(unittest.TestCase):

    def test_constructor_value_error_bits(self):
        with self.assertRaises(ValueError):
            b = BitList('FE')

Where can I put the code that checks to make sure the parameter of each object contains only 0 and 1?

martineau
  • 119,623
  • 25
  • 170
  • 301
hexdec123
  • 45
  • 7

2 Answers2

5
class BitCode:
    def __init__(self, s):
        if len(set(s + "01")) != 2:
            raise ValueError
        ...

s + "01" add "0" and "1" to the resulting string, and as a result we get a set of characters among which there are at least "0" and "1". with set we keep only unique elements. Among them there will be 0 and 1, as well as all elements that are not 0 and 1, we find out the length and if it is not equal to 2, then we throw an exception

Danis
  • 344
  • 2
  • 13
  • 1
    Nice. I'll remember that trick for checking a binary string! I'm not sure what you should say, but code-only answers seem to be frowned upon here, no matter how simple and obvious they are. – CryptoFool Feb 08 '21 at 19:52
2

Your code can both check for a valid binary input string, and convert that string to a number in the same operation with int(str, 2). The call will raise a ValueError if the input string is not a valid string representation of a binary value. Whitespace padding is ignored.

You can do this in your object's constructor as follows:

class BitCode:
    def __init__(self, bitsString):
        # Strip the input so we can later use the string representation and know it contains no whitespace padding
        self.bitsString = bitsString.strip() 
        self.bits = int(self.bitsString, 2)

bc = BitCode('1111')
print(bc.bits)

Result:

15

Changing the call to the constructor to:

bc = BitCode('1234')

results in:

Traceback (most recent call last):
  File "/Users/steve/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py", line 5, in <module>
    bc = BitCode('1234')
  File "/Users/steve/Library/Application Support/JetBrains/PyCharm2020.3/scratches/scratch.py", line 3, in __init__
    self.bits = int(bitsString, 2)
ValueError: invalid literal for int() with base 2: '1234'
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Thank you. So if in the later functions within this class, I want to test if the input string contains only 0 and 1 first, do I have to write any additional lines in the function? – hexdec123 Feb 08 '21 at 20:14
  • If you want to make sure there's no whitespace padding, add a call to `strip()` as I now show in my edited answer above. – CryptoFool Feb 08 '21 at 20:21
  • Note that if you really only care about the string representation containing exactly `0`s and `1`s right off the bat, then you might consider using @Danis's fine answer. I just felt it appropriate to give the more common alternative. It's more common because in many cases, you want to convert the incoming string to a numeric value, and so you don't care if it contains some padding. If you don't need to do that, either of these solutions will work, and maybe you like the other one better. – CryptoFool Feb 08 '21 at 20:28