[Tutor] sqlite3 module
ThreeBlindQuarks
threesomequarks at proton.me
Tue Apr 18 13:07:58 EDT 2023
Roel,
It sounds like there are two scenarios. One is about a static dictionary that is guaranteed not to change during an interval either because only one thread of execution is running or can change it, or because you are working on a copy.
In that scenario, I would think it is safe to get keys and then values and they should be returned in the same order. If that is not guaranteed, I assume you can get the items back as a data structure of pairs and break them apart yourself.
That seems straightforward enough and I shudder at any implementation that would not return keys and values synchronized, albeit older versions in no guaranteed order.
In the second scenario, hardly anything works. If a dictionary is having additions, modifications and deletions as you work on it, you either need a sort of atomic snapshot to work with, and ignore changes made after your copy is set aside, or put some kind of lock on the dictionary and block anyone else while you work after making sure you have access.
For the purposes of the OP, I doubt this matters. The discussion as I recall was that you had a fairly small dictionary containing a subset of the names of table columns as keys and their corresponding values, with some perhaps being set to some form of NULL. To get around one SQL insertion method that seemed to require even optional fields to be described even as they should default to NULL, it was suggested he use an alternate insertion method where he presents matched lists of sorts with one containing just the names of the fields and the other containing the values IN THE SAME ORDER. The actual order becomes not relevant and only that it be the same.
So my question was whether any implementation can be expected to not return keys and values in a static dictionary in the same order. Or, if this was a deal breaker, should the OP (if using this idea) use some variant with guarantees, such as a sorted dictionary.
Sent with Proton Mail secure email.
------- Original Message -------
On Tuesday, April 18th, 2023 at 4:02 AM, Roel Schroeven <roel at roelschroeven.net> wrote:
> ThreeBlindQuarks via Tutor schreef op 18/04/2023 om 4:17:
>
> > Dumb question. Is there a reason to assume the keys are not presented in the same order as the values?
>
> Even in the older version of dictionaries, the order was arbitrary but
> not random. Adding or deleting items had a good chance of changing the
> order, but I'm pretty sure order was preserved in the absence of changes
> to the dictionary. That can get tricky when using multithreading of
> course, since then another thread can possibly cause changes.
>
> --
> "Cheer up," they said, "things could be worse." So I cheered up, and
> sure enough, things got worse.
> -- Paraphrased from James C. Hagerty
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list