Printing key:value pairs from pyDataset?

#    data passed in from user dataset and converted with system.dataset.toPyDataSet()
for row in pyDataset:
    for key, value in row:
        message = 'key: ' + key + ', value: ' + value

doesn't work. Error_Configuration. this is a JSON object! where's the error!?!?

A PyDataset contains PyRows. PyRows are list-like objects, not dictionary-like objects. So you can't unpack it like a tuple.

You probably want something like this You can translate your code into something that works like this:

for row in pyDataset:
    for key, value in zip(pyDataset.columnNames, row):
        message = 'key: ' + key + ', value: ' + value

But let's avoid the XY problem and maybe ask what you're actually trying to do :slight_smile:

1 Like

same as with every data structure: SEE how it's laid out, see the keys and values, see the nesting. just like a simple unpacking of a JSON object or a dict. some of these scripts are making no sense because they are targeting things that don't exist in the dataset in the user's tag. eg. row['visibility'] when there is no such column. and if we change it to isVisible, the actual name, the script breaks. so i want to just see the raw data without all the UI. show me keys, values, and indicies. like this:

    'docs_url': None,
    'download_url': 'UNKNOWN',
    'downloads': {...},
    'home_page': '',
    'keywords': 'sample setuptools development',
    'license': 'MIT',

so now i know thatdocs_url has a value of None, downloads is going to be a dict, and that they aren't zero-based indexed, like a list would be. moreover, i know that keywords comes AFTER downloads, and not before. this is basic, basic stuff in just about any coding language: Py, JS, PHP.

EDIT: your script also fails, btw: cannot concatenate 'unicode' and 'bool'

Default Java and python2 dictionaries are not order-preserving. This creates, along with Perspective's property wrapper quirks, a number of fundamental issues that cause programmers from other disciplines to pull their hair out.

I'm not aware of any learning curve shortcuts for someone on a deadline. Keep asking questions here--we'll keep helping.

Much of what you are struggling with is documented, but the java way, and therefore time-consuming to absorb. :man_shrugging:

1 Like

Well, that's a bog-standard Python error. Using a better formatting construct should help:

for row in pyDataset:
    for key, value in zip(pyDataset.columnNames, row):
        system.perspective.print('key: {}, value: {}'.format(key, value))

Note that this wouldn't work for dicts either, you'd need to use row.items() to extract both the key and values.


wait... Python 2 !?!? how did i not know this? um, yeah. back to unordered everything... oh, wow. ok. that actually explains many other things i thought were odd elsewhere in the project. can't believe i didn't check that sooner.

1 Like

Specifically 2.7, and it's Jython which can sometimes make a difference, like not supporting C extensions (e.g. numpy)

Jython still has not made the leap to v3, mainly because the key motivators for the 2=>3 breaking change aren't such big deals in Jython. Specifically:

  • Jython strings are already internationalized, as they are wrappers for java strings (which are 16-bit wide characters under the hood). Java bytes and byte arrays, used and delivered by java's filesystem and stream libraries, already provide the basis for the distinction between bytes and characters that Python3 introduced.

  • CPython's global interpreter lock doesn't exist in Jython. Jython is fully multithreaded out of the box, and all of the core Python data structures (sequences, mapping types) are built on Java Concurrent collections--a thread-safe foundation. This doesn't mean an algorithm implemented in jython will be thread-safe, but that such algorithms do not suffer the interpreter contention of CPython.

  • CPython's async and await keywords are the stop-gap alternative for full, high-performance multi-threading. Jython doesn't need these, partly because its multi-threading doesn't suck, but mainly because java provides a complete set of functional programming tools that a jython application can leverage. And are preferred, as they will interoperate with native java objects.

On top of that, jython makes interaction with java libraries almost trivially easy. Java types can be imported by name, and java classes are implicitly treated as constructors, python style. When any java object is encapsulated for use by the jython interpreter, it is wrapped in a jython type that automatically recognizes NetBeans-compliant getters and setters, and inserts python property handlers for them. So, any .getSomething() can (and should) be shortened to just .something. Likewise, any .setSomething(rhs) can be written as the statement .something = rhs.

Finally, note that java has high-performance ordered mappings that can (and should) be used in place of python's orderedDict. I tend to use LinkedHashMap for throw-away computations, and apply Collections.synchronizedMap() if I need a thread-safe version.