python data structures
John Rickman (71) 645 posts |
Given some string data comprising a series of headings |
Steve Fryatt (216) 2103 posts |
A dictionary of lists might be a possibility? |
Jean-Michel BRUCK (3009) 351 posts |
With C++ containers, I would have used a map < string, vector |
John Rickman (71) 645 posts |
I looked at using list of lists and dictionary of lists as suggested by Steve, but would prefer something a bit more specifically tailored to tree-structure data. There is a PIP installable package called Bigtree which looks hopeful. Unfortunately it reports an error when it trying to draw a tree at the terminal because of missing Unicode characters. |
David Gee (1833) 268 posts |
If it is actually a file system you are concerned with, Python has a number of existing routines for handling such things. But they’re for querying an existing structure rather than for modelling one in memory. Does your data structure need to handle a hierarchical structure, I.e. where directories can contain other directories? |
John Rickman (71) 645 posts |
The structure has only two layers. First layer directory names, second layer leaf names of runable programs The purpose of the program is to list all the RISC OS executables. |
Steve Fryatt (216) 2103 posts |
If you need to go beyond just storing filenames, I would suggest investigating DataClasses (I’m assuming that the RISC OS port supports them). |
Paolo Fabio Zaino (28) 1853 posts |
@ John If i understand your question correctly then, in Python, the best approach you have should be to use nested dictionary:
What the above means:
HTH @ Jean
In many ways, yes, a C++ std::map is similar to a Python dictionary, but there are also some differences. So, I would not really call it synonymous, but more like similar concept if I am making sense. @ Steve
Good idea, and I would say: I would invest in learning DataClasses anyway. It should work fine on Python for RO as long as a library is pure python and has dependecies that are pure python it should work straight out of the box (some lib may have some issues if they make certain assumptions like with syscalls and/or file paths with ENV variables etc., but DataClasses should not have any of this IIRC) |
John Rickman (71) 645 posts |
Thanks Steve and Paolo. I really did not did not want to hear that:-) I usually start writing top down linear procedural code, and after 10 lines or so start thinking about defining a function or two. For this particual program I had achieved 90 percent of what I needed in 20 lines code. Unfortunately it was at this point that I began to realize that further development would require a rethink about the approach I had taken. The data needed structure. But you have convinced me. I know it it the right way to go so will bite the bullet, study Data Classes, light a smoky torch and climb down the OOP hole. Version 1.0 release may be some time. |
K J Wong (13876) 2 posts |
Hi, creator of bigtree here. May I know how you are creating a tree and if this error can be replicated? Feel free to raise an issue here as well! |
John Rickman (71) 645 posts |
Hello Kay It is a pleasant surprise to hear from the developer, but I am surprised you managed to find your way into this little RISC OS backwater:) The problem is reproducible on a machine running RISC OS with python 3.8 installed.
Running the above gives error output:
I don’t think the problem is in your code but rather in the RISC OS Python implementation. RISC OS Python is still a bit raw and RISC OS does not have comprehensive UniCode support. |
K J Wong (13876) 2 posts |
I was just googling around to see how I can support my own package haha I see.. I do not have RISC OS so I am unable to replicate the error on my machine, unfortunately. I see that printing out the characters is the issue – there is another function that can extract the string – and from there you can perform any encoding as you wish. In the example below, I tried using utf-8 encoding but the results are not “pretty” though since they got encoded to byte format
Hope this can be a workaround :) |