Deal with buffer in Python 2.x and 3.x

bluedog

Trying to take a rough hash of a file in Python 2.x and 3.x. Must use this hash function - not built in one.

Using

get_file_hash("my-file.txt")

3.x works. 2.x gives an error because the type of the incoming value is 'str'.

Error says

    value = content[0] << 7
    TypeError: unsupported operand type(s) for <<: 'str' and 'int'

Here's the code

def c_mul(a,b):
    return eval(hex((int(a) * b) & 0xFFFFFFFF)[:-1])

def get_hash(content):
    value = 0
    if len(content) > 0:
        print (type(content))
        print (type(content[0]))
        value = content[0] << 7
        for char in content:
            value = c_mul(1000003, value) ^ char
        value = value ^ len(content)
        if value == -1:
            value = -2
    return value

def get_file_hash(filename):
    with open(filename, "rb") as pyfile:
        return get_hash(pyfile.read())

How can I fix get_hash or get_file_hash so this works on 2.x and 3.x?

falsetru

file.read() for a file open with binary mode return bytes in Python 3, and str (== bytes) in Python 2.

But iteratring bytes objects yields different result in both version:

>>> list(b'123') # In Python 3.x, yields `int`s
[49, 50, 51]
>>> list(b'123') # In Python 2.x, yields `string`s
['1', '2', '3']

Use bytearray. Iterating it will yields ints in both version.

>>> list(bytearray(b'123')) # Python 3.x
[49, 50, 51]
>>> list(bytearray(b'123')) # Python 2.x
[49, 50, 51]

def c_mul(a,b):
    return (a * b) & 0xFFFFFFFF

def get_hash(content):
    content = bytearray(content) # <-----
    value = 0
    if len(content) > 0:
        value = content[0] << 7
        for char in content:
            value = c_mul(1000003, value) ^ char
        value = value ^ len(content)
        if value == -1:
            value = -2
    return value

def get_file_hash(filename):
    with open(filename, "rb") as pyfile:
        return get_hash(pyfile.read())

BTW, I modified c_mul not to use hex, eval. (I assumed that you used it to remove trailing L in Python 2.x).

>>> hex(289374982374)
'0x436017d0e6L'
#            ^

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deal with buffer in Python 2.x and 3.x

From Dev

Python: Eval with undefined variables (2*x+x = 3*x)

From Java

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

From Dev

Script incompatibility for Python 2.x and Python 3.x

From Dev

Geocoding Strategies - Python 2.x to Python 3.x

From Dev

Regex unicode in python 2.x vs 3.x

From Dev

Accessing Python2.x and 3.x in windows

From Java

Using both Python 2.x and Python 3.x in IPython Notebook

From Dev

How to parse Python 2.x with Python 3.x ast module?

From Dev

Can't import turtle module in Python 2.x and Python 3.x

From Dev

string is not same in Python3x as for Python2.x but Still it is working

From Dev

Generator evaluation difference between Python 2.x and Python 3.x

From Dev

random.randint shows different output in Python 2.x and Python 3.x with same seed

From Dev

Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

From Dev

How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

From Dev

Summing a 2d array in Python 3.x

From Dev

Is it possible to create GTK+2.x applications using Python 3?

From Dev

Function with args and default kwargs for Python 2.x and 3.x

From Dev

How can I get 2.x-like sorting behaviour in Python 3.x?

From Dev

Valid syntax in both Python 2.x and 3.x for raising exception?

From Dev

Difference in multithreading overhead between python 2.x and 3.x

From Dev

Differences between input commands in Python 2.x and 3.x

From Dev

Troubleshooting Unicode string behavior when updating Python 2.x->3.x

From Dev

Setting up Django environments for Python2.X and 3.X?

From Dev

install python 2.x and 3.x on the same windows computer

From Dev

Portable ctypes.c_char_p for python 2.x and 3.x

From Dev

PyYAML with Python 3.x

From Dev

Pyinstaller with Python3.x

Related Related

  1. 1

    Deal with buffer in Python 2.x and 3.x

  2. 2

    Python: Eval with undefined variables (2*x+x = 3*x)

  3. 3

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  4. 4

    Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

  5. 5

    Script incompatibility for Python 2.x and Python 3.x

  6. 6

    Geocoding Strategies - Python 2.x to Python 3.x

  7. 7

    Regex unicode in python 2.x vs 3.x

  8. 8

    Accessing Python2.x and 3.x in windows

  9. 9

    Using both Python 2.x and Python 3.x in IPython Notebook

  10. 10

    How to parse Python 2.x with Python 3.x ast module?

  11. 11

    Can't import turtle module in Python 2.x and Python 3.x

  12. 12

    string is not same in Python3x as for Python2.x but Still it is working

  13. 13

    Generator evaluation difference between Python 2.x and Python 3.x

  14. 14

    random.randint shows different output in Python 2.x and Python 3.x with same seed

  15. 15

    Change "Edit with IDLE" default to python 3.X instead of python 2.X on windows

  16. 16

    How to convert a unicode list to a list containing python dictionary in python 2.x and 3.x

  17. 17

    Summing a 2d array in Python 3.x

  18. 18

    Is it possible to create GTK+2.x applications using Python 3?

  19. 19

    Function with args and default kwargs for Python 2.x and 3.x

  20. 20

    How can I get 2.x-like sorting behaviour in Python 3.x?

  21. 21

    Valid syntax in both Python 2.x and 3.x for raising exception?

  22. 22

    Difference in multithreading overhead between python 2.x and 3.x

  23. 23

    Differences between input commands in Python 2.x and 3.x

  24. 24

    Troubleshooting Unicode string behavior when updating Python 2.x->3.x

  25. 25

    Setting up Django environments for Python2.X and 3.X?

  26. 26

    install python 2.x and 3.x on the same windows computer

  27. 27

    Portable ctypes.c_char_p for python 2.x and 3.x

  28. 28

    PyYAML with Python 3.x

  29. 29

    Pyinstaller with Python3.x

HotTag

Archive