• _stranger_@lemmy.world
    link
    fedilink
    arrow-up
    31
    ·
    9 days ago

    Ugh, terrible memory unlocked.

    I asked someone a similar question in an interview once. They weren’t doing well and I wanted to end on a positive at least. I asked them to make a dict in python and for extra credit, turn it into a string using whatever library or method they want.

    This was not for a junior position.

    5 minutes of for loop insanity later they disconnected crying. I always felt bad for that guy.

        • mel ♀@jlai.lu
          link
          fedilink
          arrow-up
          1
          ·
          9 days ago

          I think json.dumps would not work if there is a datetime in the dict, right?

          • _stranger_@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            8 days ago

            Yeah there’s other edge cases too, like Decimal, but as long as you have those things imported when you send it through dumps it should work.

            • mel ♀@jlai.lu
              link
              fedilink
              arrow-up
              1
              ·
              8 days ago

              Just tried it in an ipython shell

              import datetime
              import json
              
              json.dumps({"a": datetime.datetime(1970,1,1)})
              

              And I got the TypeError: datetime not JSON serializable though.

              • _stranger_@lemmy.world
                link
                fedilink
                arrow-up
                1
                ·
                8 days ago

                If you don’t care you can pass in default=str (I think) but if you do care you can define a function for what the string representation should look like and pass that in, but that’s a happy path case to be honest. I usually only run into this problem when something is logging unknown data (otherwise I prefer to model data like this so the IDE is happy) so I pass in the default=str just to get it functional.

                Duck typing usually works but, datetime man, datetime is the warp from Warhammer 40K, it will drive you mad.