12
chabad360
54d

I hate this line with my soul.

The fact that I need to convert a dict to a string and then invoke the python parser to read it, feels so wrong!

Comments
  • 2
    Might be stupid now, buteh, why?
  • 3
    Why would you need to do that? What is the source of the dict? Also, literal_eval is safe, it won't execute arbitrary code like eval will.

    This is extremely useful when storing things in text files. You can store literals as defined by converting a data object to a string.
  • 2
    @millido @Demolishun I have a dict that has numpy.unit8 objects (I didn't write any of that, and I don't want to touch it) in it, and I need to store it as JSON due to DBus. But JSON doesn't like uint8, so I realized as I was sorting this out in the console, that a simple print the dict, c/p the dict, Bob's your uncle. So I just needed to figure out how to do that in python itself.

    Also @Demolishun good to know, now I don't feel as bad.
  • 1
    Why not do something like this instead?

    @highlight
    result = {k: int(v) for k, v in result.items()}
  • 0
  • 1
    @millido cause it's a tuple that's returned, I wrote this at midnight, it works like a charm.
  • 2
    Can you give an example of your dict?
  • 0
    show us ur dict!
  • 0
    @-pthread @Wisecrack

    Except the numbers in the tuple are uint8

    @highlight
    dict = {
    1: {
    1: (255, 255, 255)
    }
    }
  • 0
Add Comment