[Tutor] Inherit from SyntaxError?

Cameron Simpson cs at cskk.id.au
Thu Mar 23 19:06:35 EDT 2023


On 23Mar2023 09:17, Albert-Jan Roskam <sjeik_appie at hotmail.com> wrote:
>   Sometimes catching MemoryError might come in handy with a
>   "inside/outside RAM" strategy. In-memory is faster, but not scalable.
>   For instance, something like:
>   try:
>       d = {}
>       for record in records:
>           d[record.id] = record.something
>   except MemoryError:
>       logger.warning("Using slower shelve route")
>       d = shelve.open("/tmp/lookup.shelve", writeback=True)
>       atexit.register(d.close)
>       for record in records:
>           d[record.id] = record.something
>   If you know that "records" is always huge, this approach is needlessly
>   complicated. But if it *might* be too big for some machines, it might
>   be nice not having to choose the slow approach, just to be on the safe
>   side.

Ah. Is the above taken from working code? I've always felt that when a 
MemoryError files things will be comprimised enough that a lot of stuff 
won't subsequently work.

Taking the above as the example, would you have enough memory to do 
logging or use shelve.open? Even if you explicitly discarded the 
incomplete "d" before trying that (bearing in mind that without invoking 
the garbage collector the memory might not yet be really freed). But 
this opinion is speculation. Have you had joy with the above approach?

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list