Code Monkey home page Code Monkey logo

Comments (4)

pganssle avatar pganssle commented on September 13, 2024 1

@besfahbod How about this?

import pytz

def warm_pytz_cache() -> None:
    for zone in pytz.all_timezones:
        pytz.timezone(zone)

I'll note that if you migrate to zoneinfo, the guarantees about the caching would require you to hold an actual reference to the objects you want to cache if you don't want to have a cache miss:

import zoneinfo

def warm_cache(*, _cache: dict[str: zoneinfo.ZoneInfo] = {}) -> None:
    for zone in zoneinfo.available_timezones():
        _cache[zone] = zoneinfo.ZoneInfo(zone)

That is guaranteed to work as part of the public interface by the PEP. I am not sure that pytz makes any guarantees about its caching behavior, but my understanding is that development on pytz is largely frozen, so I doubt they'll be re-working the cache system any time soon.

from pytz.

stub42 avatar stub42 commented on September 13, 2024 1

pytz won't throw away the timezone info once it is loaded, and @pganssle's warm_pytz_cache() method will work fine for most purposes. The only complication is when the database on disk has drifted, which could happen when pytz has been setup to use the system zoneinfo database (but unlikely; it requires a timezone to be completely removed from the database. To make this extremely unlikely, just warm common_timezones rather than all_timezones, so you don't try warming all the deprecated zones).

def warm_pytz_cache() -> None:
    for zone in pytz.common_timezones:
        try:
            pytz.timezone(zone)
        except Exception:
            pass

Consider the above approach blessed, and I'll try to keep it working when transitioning to Python3.9's internals. I don't want to add a call to pytz to do this. I think it is an infrequent enough usage that use cases will be subtly different (warm all_timezones vs common_timezones for instance).

from pytz.

besfahbod avatar besfahbod commented on September 13, 2024

Thanks, @pganssle. The pytz solution makes sense, and is in fact similar to what I have put in place in the application code right now. And, from observation, looks like the caching behavior is that it doesn't get cleared automatically, fwiw.

And thanks for the extra notes about the zoneinfo caching behavior. Good to know when we migrate to zoneinfo module.

Considering these, @pganssle, do you think it makes sense to add a specific API for these to pytz (and probably suggest the same for zoneinfo)?

from pytz.

pganssle avatar pganssle commented on September 13, 2024

Considering these, @pganssle, do you think it makes sense to add a specific API for these to pytz (and probably suggest the same for zoneinfo)?

Can't speak for pytz, but definitely not for zoneinfo. The caching behavior is well-defined and I don't see a particularly strong justification for this, when it's pretty simple to write your own cache warmer if you really need it.

from pytz.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.