Re: "dumping" a map
- Posted by ghaberek (admin) Feb 20, 2023
- 1479 views
To give more context, I have an existing app which reads a text file and based on its contents generates text output. The existing app is written in gawk and comprises some 3500 lines of code. For some time I have wanted to port it to a multi-platform compilable version, and I'm looking at either OE/phix, or python. The current app relies heavily on gawk's associative arrays, with string keys, so I need an easy way to replicate this in OE. Maps seem to fit the bill, but for development and trouble-shooting purposes it would be great to be able to visualize the contents of a complex multi-level map structure.
Awhile ago I wrote mvc/mapdbg.e for Euphoria MVC (a web framework I started writing for redoing this website). Mapdbg helps trace out what I was doing with maps and to ensure I was properly freeing objects from eumem. It's meant to be a drop-in replacement for std/map.e that uses uses the debugging features in Euphoria 4.1 to parse your code for map allocations and it logs these events to the console using mvc/logger.e. It also includes a print_map() routine that attempts to nicely display the key/value pairs, and an print_maps() routine that will print all of the maps currently allocated. I plan to pull mvc/logger.e into Euphoria 4.2 but if it's useful, I will pull in mvc/mapdbg.e as well. (These would be moved to the std/ include directory.) If you want to try it out, just copy mvc/mapdbg.e and mvc/logger.e into an mvc/ directory in your project directory and then change your include std/map.e line to include mvc/mapdbg.e. You also need to define MAPDBG for it to do its job and LOG_DEBUG for it to show debug logs.
-- enable debug logging. optional. shows mapdbg looking through your code. with define LOG_DEBUG include mvc/logger.e -- enable map debugging. required. tracks map usage and allows print_map() below. with define MAPDBG include mvc/mapdbg.e as map object users = map:new() map:nested_put( users, {"alice","id"}, "alice" ) map:nested_put( users, {"alice","name"}, "Alice" ) map:nested_put( users, {"alice","email"}, "alice@example.com" ) map:nested_put( users, {"bob","id"}, "bob" ) map:nested_put( users, {"bob","name"}, "Bob" ) map:nested_put( users, {"bob","email"}, "bob@example.com" ) map:nested_put( users, {"carol","id"}, "carol" ) map:nested_put( users, {"carol","name"}, "Carol" ) map:nested_put( users, {"carol","email"}, "carol@example.com" ) ifdef MAPDBG then print_map( users, /* nested = */ 1 ) end ifdef
Output:
users@/maptest3.ex:9 (map #3) = { 3: {0x24D62C12,"bob", map #5 = { 1: {0x369D9230,"id","bob"}, 10: {0x36DEAD19,"email","bob@example.com"}, 12: {0x0EAAA6CB,"name","Bob"}, } }, 10: {0x1C0FF609,"carol", map #6 = { 1: {0x369D9230,"id","carol"}, 10: {0x36DEAD19,"email","carol@example.com"}, 12: {0x0EAAA6CB,"name","Carol"}, } }, 15: {0x053F7D1E,"alice", map #4 = { 1: {0x369D9230,"id","alice"}, 10: {0x36DEAD19,"email","alice@example.com"}, 12: {0x0EAAA6CB,"name","Alice"}, } }, }
And this is really just a learning exercise so that I can next leverage my new-found OE or Python knowledge to port an Excel/VBA which also relies heavily on data structures, but this is a much more ambitious project as I need to recreate Excel's tabbed GUI (though I don't need any formulas, just tabbed grids).
I've been revisiting IUP lately and it has a few options for easily creating a tabbed interface with grids of data. I'd be glad to help you out with that when you're ready.
Is there any mechanism to catch runtime errors, like python's Try...Except ?
Nope, sorry. Runtime errors crash. But you can use crash_routine to "catch" that crash before your program exits completely. When your crash routine is called, Euphoria has already written its ex.err so you can open and read that file if you need.
-Greg