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 923 - Make QScriptValue (or QScriptValueIterator) implement the Python iterator protocol
: Make QScriptValue (or QScriptValueIterator) implement the Python iterator pro...
Status: CLOSED FIXED
Product: PySide
Classification: Unclassified
Component: QtScript
: HEAD
: PC Linux
: P4 enhancement
Assigned To: Hugo Parente Lima
:
:
:
  Show dependency treegraph
 
Reported: 2011-07-10 12:17 EEST by Thomas Perl
Modified: 2011-08-23 00:35 EEST (History)
8 users (show)

See Also:


Attachments
Test to check if QScriptValue is working. (1.38 KB, text/plain)
2011-07-28 22:38 EEST, Hugo Parente Lima
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Perl 2011-07-10 12:17:43 EEST
In order to enumerate the properties of a QScriptValue, one needs to use the
"QScriptValueIterator" class, and manually call its methods like this:

it = QScriptValueIterator(some_script_value)
while it.hasNext():
    it.next()
    key = it.name()
    value = it.value()
    yield key, value

It would be good if we could have a Pythonic way of iterating over a
QScriptValue, either like this (some_script_value is of type QScriptValue):

for key, value in some_script_value:
    print key, '->', value

or like this:

for key, value in QScriptValueIterator(some_script_value):
    print key, '->', value

Alternatively, we could just iterate over the keys and get the value via
.property() on the script value, i.e.:

for key in some_script_value:
    print key, '->', some_script_value.property(key)

On the other hand, having the tuple-based iterator allows the user to directly
convert the script value into a dict like this (depending on which variant of
the above suggestions is chosen):

print dict(some_script_value) or
print dict(QScriptValueIterator(some_script_value))
Comment 1 Hugo Parente Lima 2011-07-11 21:01:46 EEST
I prefer the key/value option, but better ask people on mailing list about it.
Comment 2 Hugo Parente Lima 2011-07-28 22:38:03 EEST
Created attachment 383 [details]
Test to check if QScriptValue is working.

As we get no answers on mailing list about this, I implemented it to pass in
the attached test case, if you have any suggestion about the feature behavior,
now is the time to speak =]
Comment 3 Thomas Perl 2011-07-29 12:13:14 EEST
(In reply to comment #2)
> As we get no answers on mailing list about this, I implemented it to pass in
> the attached test case, if you have any suggestion about the feature behavior,
> now is the time to speak =]

Due to your calls to sort(), it is not guaranteed that the keys match the
corresponding values. Maybe this additional test would take care of that?

def testDictionaryBehaviour(self):
    engine = QScriptEngine()
    value = engine.evaluate('x = {"a": 1, "b": 2}')
    self.assertEqual(dict(value), {'a': 1, 'b': 2})

This should work, because the dict constructor takes an iterable as argument.

For the repr test, shouldn't there be an (additional) test where the test
itself specifies the format of the repr string? Is the format of the repr
string specified somewhere? If so, it would be good if there was a test that
tested if the correct format is returned.
Comment 4 Hugo Parente Lima 2011-07-29 16:41:42 EEST
(In reply to comment #3)
> (In reply to comment #2)
> > As we get no answers on mailing list about this, I implemented it to pass in
> > the attached test case, if you have any suggestion about the feature behavior,
> > now is the time to speak =]
> 
> Due to your calls to sort(), it is not guaranteed that the keys match the
> corresponding values. Maybe this additional test would take care of that?
> 
> def testDictionaryBehaviour(self):
>     engine = QScriptEngine()
>     value = engine.evaluate('x = {"a": 1, "b": 2}')
>     self.assertEqual(dict(value), {'a': 1, 'b': 2})

I changed it to:

    d = {}
    for k, v in QScriptValueIterator(value):
        d[k] = v
    self.assertEqual(d, {'a': 1, 'b': 2})

As dict supports other types in their constructor, better write things
explicitly.

> This should work, because the dict constructor takes an iterable as argument.
> 
> For the repr test, shouldn't there be an (additional) test where the test
> itself specifies the format of the repr string? Is the format of the repr
> string specified somewhere? If so, it would be good if there was a test that
> tested if the correct format is returned.

This isn't related to this bug =], but I added another check:

    self.assertEqual(value.toVariant(), value2.toVariant())
Comment 5 Hugo Parente Lima 2011-07-29 16:46:06 EEST
Fixed in commit:

pyside/20af27cac5c59ff891cd01c4902a3575f7f92241
Comment 6 renato filho 2011-08-23 00:35:39 EEST
Released on PySide 1.0.6