Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit 4e84780

Browse files
committed
Lazy load pandas
1 parent b634cb5 commit 4e84780

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

third_party/lazy_loader.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
""" A LazyLoader class. """
16+
17+
import importlib
18+
import types
19+
20+
21+
class LazyLoader(types.ModuleType):
22+
"""
23+
Lazily import a module, mainly to avoid pulling in large dependencies. `contrib`, and
24+
`ffmpeg` are examples of modules that are large and not always needed, and this allows them to
25+
only be loaded when they are used.
26+
"""
27+
28+
def __init__(self, local_name, parent_module_globals, name):
29+
self._local_name = local_name
30+
self._parent_module_globals = parent_module_globals
31+
32+
super(LazyLoader, self).__init__(name)
33+
34+
def _load(self):
35+
""" Load the module and insert it into the parent's globals. """
36+
37+
# Import the target module and insert it into the parent's namespace
38+
module = importlib.import_module(self.__name__)
39+
self._parent_module_globals[self._local_name] = module
40+
41+
# Update this object's dict so that if someone keeps a reference to the
42+
# LazyLoader, lookups are efficient (__getattr__ is only called on lookups
43+
# that fail).
44+
self.__dict__.update(module.__dict__)
45+
46+
return module
47+
48+
def __call__(self, *args, **kwargs):
49+
module = self._load()
50+
return module(*args, **kwargs)
51+
52+
def __getattr__(self, item):
53+
module = self._load()
54+
return getattr(module, item)
55+
56+
def __dir__(self):
57+
module = self._load()
58+
return dir(module)

torchnlp/datasets/dataset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import pandas as pd
1+
from third_party.lazy_loader import LazyLoader
2+
3+
pd = LazyLoader('pd', globals(), 'pandas')
24

35
from torch.utils import data
46

torchnlp/datasets/simple_qa.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22

3-
import pandas as pd
3+
from third_party.lazy_loader import LazyLoader
4+
5+
pd = LazyLoader('pd', globals(), 'pandas')
46

57
from torchnlp.datasets.dataset import Dataset
68
from torchnlp.download import download_file_maybe_extract

0 commit comments

Comments
 (0)