Last active
February 3, 2020 00:33
-
-
Save anandology/3b73676f030b3dbff9e1ddf844471cf6 to your computer and use it in GitHub Desktop.
Lazily Load Python Modules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import pandas as pd | |
from .heavy_modules import pandas as pd | |
# from google.cloud import bigquery | |
from .heavy_modules import google_cloud_bigquery as bigquery | |
def foo(path): | |
df = pd.read_csv(path) | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
app.heavy_modules | |
~~~~~~~~~~~~~~~~~ | |
This imports all heavy modules lazily. | |
""" | |
from lazy_import import lazy_import | |
numpy = lazy_import("numpy") | |
pandas = lazy_import("pandas") | |
google_cloud_storage = lazy_import("google.cloud.storage") | |
google_cloud_bigquery = lazy_import("google.cloud.bigquery") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import web | |
# db_url = os.getenv("DATABASE_URL", "postgres:///elf") | |
# db = web.database(db_url) | |
# | |
# def get_sites(): | |
# query = "..." | |
# result = db.query(query) | |
# ... | |
_db = None | |
def get_db(): | |
global _db | |
if _db is None: | |
db_url = os.getenv("DATABASE_URL", "postgres:///elf") | |
_db = web.database(db_url) | |
return _db | |
def get_sites(): | |
query = "..." | |
result = get_db().query(query) | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from statsmodels.tsa.holtwinters import SimpleExpSmoothing | |
def forecast(df): | |
"""Forecasts the future sales using the historical sales. | |
""" | |
# importing SimpleExpSmoothing inside the function to avoid loading it on startup | |
from statsmodels.tsa.holtwinters import SimpleExpSmoothing | |
model = SimpleExpSmoothing(df.quantity.values).fit() | |
.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
lazy_import | |
~~~~~~~~~~~ | |
Utilitity to import a module lazily. | |
# import pandas as pd | |
pd = lazy_import("pandas") | |
""" | |
class LazyModule: | |
def __init__(self, name): | |
self.__dict__['_name'] = name | |
self.__dict__['_mod'] = None | |
def load(self): | |
if self._mod is None: | |
import importlib | |
self.__dict__['_mod'] = importlib.import_module(self._name) | |
return self._mod | |
def __getattr__(self, name): | |
return getattr(self.load(), name) | |
def __setattr__(self, name, value): | |
return setattr(self.load(), name, value) | |
def __delattr__(self, name): | |
return delattr(self.load(), name) | |
def __repr__(self): | |
return "<LazyModule: {}>".format(self._name) | |
def lazy_import(module_name): | |
"""Imports a module lazily. | |
The module is loaded when an attribute it is accessed from that module for the first time. | |
pd = lazy_import("pandas") | |
This only works with absolute imports and does not work with relative imports. | |
""" | |
return LazyModule(module_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment