7

In the Python code using numpy 1.18.1

` def printBoard(self): current = self.player other = self.player % 2 + 1

    currBin   = '{:049b}'.format(self.current_position)
    currRev = currBin[::-1]

    cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

    other_position = self.current_position^self.mask
    othBin   = '{:049b}'.format(other_position)
    othRev = othBin[::-1]

    oArr = (np.fromstring(othRev,'u1') - ord('0'))*other

    tArr =  oArr+cArr

    brd = np.reshape(tArr,(7,7),order = 'F')
    for y in range(bitBoard.HEIGHT,-1,-1):
        for x in range(bitBoard.WIDTH):
            print(brd[y,x],end = ' ')
        print()
    print()
    `

the line :

cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

gives the following warning:

DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on    unicode inputs. Use frombuffer instead
  cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

Replacing 'fromstring' with 'frombuffer' gives the following error :

cArr = (np.frombuffer(currRev,'u1') - ord('0'))*current

TypeError: a bytes-like object is required, not 'str'

Despite some Googling I cannot find what I should use instead. Can anybody help?

Thank you.

Alan

2
  • Explain what those lines do conceptually. Your example is not minimal, and 90% of the code you posted is irrelevant to the problem. Commented Feb 20, 2020 at 13:30
  • I know I have given too much but often when I ask a question I get requested for the whole code! This time I gave the function hoping that would suffice. The lines take a column of a connect4 column encoded in binary form and convert it to 0 and 1 or 0 and 2 depending on the player. it adds the resulting arrays together and outputs the positions of the pieces in that column Commented Feb 20, 2020 at 15:26

1 Answer 1

7

The relevant part of your code is that which produces currRev. From that I can construct this example:

In [751]: astr = '{:049b}'.format(123)[::-1]                                                   
In [752]: astr                                                                                 
Out[752]: '1101111000000000000000000000000000000000000000000'

your warning:

In [753]: np.fromstring(astr, 'u1')                                                            
/usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
  #!/usr/bin/python3
Out[753]: 
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
      dtype=uint8)

frombuffer wants a bytestring, so let's create one:

In [754]: astr.encode()                                                                        
Out[754]: b'1101111000000000000000000000000000000000000000000'
In [755]: np.frombuffer(astr.encode(),'u1')                                                    
Out[755]: 
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
      dtype=uint8)

And the rest of the line:

In [756]: _-ord('0')                                                                           
Out[756]: 
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0], dtype=uint8)

Another way to get the same array:

In [758]: np.array(list(astr),'uint8')                                                         
Out[758]: 
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0], dtype=uint8)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much. That worked. I do not play with bits very much so I was unsure what to do.
In py2, default strings were byte size. In py3 unicode is the default, and bytestrings are marked with the b'...'
Thanks for the explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.