PySide Bugzilla Closed for New Bugs

PySide is now a Qt Add-on and uses the Qt Project's JIRA Bug Tracker instead of this Bugzilla instance. This Bugzilla is left for reference purposes.

Bug 906 - qHash is missing in pyside
: qHash is missing in pyside
Status: CLOSED INVALID
Product: PySide
Classification: Unclassified
Component: QtCore
: HEAD
: PC Linux
: P5 normal
Assigned To: renato filho
:
:
:
  Show dependency treegraph
 
Reported: 2011-06-29 16:41 EEST by Ivan Frade
Modified: 2011-08-23 00:37 EEST (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ivan Frade 2011-06-29 16:41:36 EEST
The Qt/C++ code to calculate the album art of a song looks like this:

 hash = ::qHash(album + artist + QString("album"));

qHash function is not present in pyside, so there is no way to write similar
functionality in python.

This is the documentation of the C++ API:
http://doc.qt.nokia.com/latest/qhash.html#qHashx

Not sure if this is a bug in the bindings... but the functionality is needed,
so at least a workaround is welcome.
Comment 1 renato filho 2011-06-29 16:58:38 EEST
you can do the same thing with python  built-in function "hash".

The qHash functions is a template because of that this is not exported in
PySide bindings.
Comment 2 Ivan Frade 2011-06-30 12:04:53 EEST
[Not an expert on this topic]
The hash of Qt is different from the hash function used in python for strings.
At least doing "hash (someString)" doesn't return the same results as in C++ . 

As a work around, I translated the Qt hash function for strings into python. 
#
# Python translation of :
#
https://qt.gitorious.org/qt/qt/blobs/d12681a4cf1227d0e92fc7cf12aa3977e6ffe3fe/src/corelib/tools/qhash.cpp#line76
#
def qhash (inputstr):

    instr = ""
    if isinstance (inputstr, str):
        instr = inputstr 
    elif isinstance (inputstr, unicode):
        instr = inputstr.encode ("utf8")
    else:
        return -1

    h = 0x00000000
    for i in range (0, len (instr)):
        h = (h << 4) + ord(instr[i])
        h ^= (h & 0xf0000000) >> 23
        h &= 0x0fffffff

    return h
Comment 3 renato filho 2011-06-30 15:26:55 EEST
yes the qhash and python hash funcation, is not the same algorithm but they
have the same propose. 

this is the hash function used on string objects:

class string:
    def __hash__(self):
        if not self:
            return 0 # empty
        value = ord(self[0]) << 7
        for char in self:
            value = c_mul(1000003, value) ^ ord(char)
        value = value ^ len(self)
        if value == -1:
            value = -2
        return value

Why you do not want to use the python version there is some problem with that?
you can use either the python hashlib functions, check:
http://docs.python.org/library/hashlib.html
Comment 4 Ivan Frade 2011-06-30 16:56:54 EEST
The music player in Harmattan is saving the album art in
~/blablabla/qHash(album + artist).png and my code must read the image in that
exact same location. 

Actually, the use-case is very specific and nobody should use qHash for
anything "external" to the application like a file name. I guess we can close
this as INVALID or WONTFIX. Sorry for the noise.
Comment 5 renato filho 2011-06-30 17:02:36 EEST
ok then, I will mark as INVALID since you agree with that.
Comment 6 renato filho 2011-08-23 00:37:19 EEST
Release PySide 1.0.6