本文整理匯總了Python中requests_cache.install_cache方法的典型用法代碼示例。如果您正苦於以下問題:Python requests_cache.install_cache方法的具體用法?Python requests_cache.install_cache怎麽用?Python requests_cache.install_cache使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類requests_cache
的用法示例。
在下文中一共展示了requests_cache.install_cache方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: import_places
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def import_places(self):
if self.options['cached']:
requests_cache.install_cache('tprek')
queryset = Place.objects.filter(data_source=self.data_source)
if self.options.get('single', None):
obj_id = self.options['single']
obj_list = [self.pk_get('unit', obj_id)]
queryset = queryset.filter(id=obj_id)
else:
logger.info("Loading units...")
obj_list = self.pk_get('unit')
logger.info("%s units loaded" % len(obj_list))
syncher = ModelSyncher(queryset, lambda obj: obj.origin_id, delete_func=self.mark_deleted,
check_deleted_func=self.check_deleted)
for idx, info in enumerate(obj_list):
if idx and (idx % 1000) == 0:
logger.info("%s units processed" % idx)
self._import_unit(syncher, info)
syncher.finish(self.options.get('remap', False))
示例2: setup
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def setup(self):
self.tprek_data_source = DataSource.objects.get(id='tprek')
ds_args = dict(id=self.name)
ds_defaults = dict(name='City of Espoo')
self.data_source, _ = DataSource.objects.get_or_create(defaults=ds_defaults, **ds_args)
org_args = dict(origin_id='kaupunki', data_source=self.data_source)
org_defaults = dict(name='Espoon kaupunki')
self.organization, _ = Organization.objects.get_or_create(defaults=org_defaults, **org_args)
self._build_cache_places()
self._cache_yso_keywords()
if self.options['cached']:
requests_cache.install_cache('espoo')
self.cache = requests_cache.get_cache()
else:
self.cache = None
示例3: setup_requests_cachedir
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def setup_requests_cachedir():
"""Sets up local caching for faster remote HTTP requests.
Caching directory will be set up in the user's home directory under
a .nfcore_cache subdir.
"""
# Only import it if we need it
import requests_cache
pyversion = '.'.join(str(v) for v in sys.version_info[0:3])
cachedir = os.path.join(os.getenv("HOME"), os.path.join('.nfcore', 'cache_'+pyversion))
if not os.path.exists(cachedir):
os.makedirs(cachedir)
requests_cache.install_cache(
os.path.join(cachedir, 'github_info'),
expire_after=datetime.timedelta(hours=1),
backend='sqlite',
)
示例4: init_requests_cache
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def init_requests_cache(refresh_cache=False):
"""
Initializes a cache which the ``requests`` library will consult for
responses, before making network requests.
:param refresh_cache: Whether the cache should be cleared out
"""
# Cache data from external sources; used in some checks
dirs = AppDirs("stix2-validator", "OASIS")
# Create cache dir if doesn't exist
try:
os.makedirs(dirs.user_cache_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
requests_cache.install_cache(
cache_name=os.path.join(dirs.user_cache_dir, 'py{}cache'.format(
sys.version_info[0])),
expire_after=datetime.timedelta(weeks=1))
if refresh_cache:
clear_requests_cache()
示例5: shodan_query
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def shodan_query(query, api_key, cache=True, verbose=False):
if verbose:
logging.basicConfig(level=logging.INFO, format='%(message)s')
if cache:
homedir = Path(os.path.expanduser('~'))
requests_cache.install_cache(str(homedir / '.habu_requests_cache'), expire_after=3600)
url = 'https://api.shodan.io/shodan/host/search?key={}&query={}'.format(api_key, query)
r = requests.get(url)
if r.status_code not in [200, 404]:
logging.error(str(r))
return {}
if r.status_code == 404:
return {}
data = r.json()
return data
示例6: main
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def main():
if os.path.isfile(BASEDIR):
sys.exit('Please remove your old configuration file at {}'.format(BASEDIR))
os.makedirs(BASEDIR, exist_ok=True)
global CONFIG
CONFIG = read_configuration(CONFFILE)
locale.setlocale(locale.LC_MONETARY, CONFIG['locale'].get('monetary', ''))
requests_cache.install_cache(cache_name='api_cache', backend='memory',
expire_after=int(CONFIG['api'].get('cache', 10)))
curses.wrapper(mainc)
示例7: setup
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def setup(self):
defaults = dict(name='Matkailu- ja kongressitoimisto')
self.data_source, _ = DataSource.objects.get_or_create(id=self.name, defaults=defaults)
self.tprek_data_source = DataSource.objects.get(id='tprek')
ytj_ds, _ = DataSource.objects.get_or_create(defaults={'name': 'YTJ'}, id='ytj')
org_args = dict(origin_id='0586977-6', data_source=ytj_ds)
defaults = dict(name='Helsingin Markkinointi Oy')
self.organization, _ = Organization.objects.get_or_create(
defaults=defaults, **org_args)
place_list = Place.objects.filter(data_source=self.tprek_data_source, deleted=False)
deleted_place_list = Place.objects.filter(data_source=self.tprek_data_source,
deleted=True)
# Get only places that have unique names
place_list = place_list.annotate(count=Count('name_fi')).filter(count=1).values('id', 'origin_id', 'name_fi')
deleted_place_list = deleted_place_list.annotate(count=Count('name_fi')).\
filter(count=1).values('id', 'origin_id', 'name_fi', 'replaced_by_id')
self.tprek_by_name = {p['name_fi'].lower(): (p['id'], p['origin_id']) for p in place_list}
self.deleted_tprek_by_name = {
p['name_fi'].lower(): (p['id'], p['origin_id'], p['replaced_by_id'])
for p in deleted_place_list}
if self.options['cached']:
requests_cache.install_cache('matko')
示例8: __init__
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def __init__(self, username, password, verify=True, debug=False):
assert username is not None
assert password is not None
assert verify is not None
assert debug is not None
self._username = username
self._password = password
self._verify = verify
self._debug = debug
if self._request_caching_enabled:
self.request_count = self.get_cached_request_count()
if not self._verify:
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
if self._debug:
self.enable_request_debugging()
else:
self.enable_error_logging()
# cache requests for 24 hours
if self._request_caching_enabled:
requests_cache.install_cache('capiq_cache', backend='sqlite', expire_after=86400, allowable_methods=('POST',))
# This function retrieves a single data point for a point in time value for a mnemonic either current or
# historical. Default inputs include a Mnemonic and a Security/Entity Identifier
#
# Returns a nested dictionary, where the primary key is the identifier and the secondary key is the mnemonic.
# In case of an error, a None value is returned for that mnemonic and Cap IQ's error is logged
示例9: enable_cache
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def enable_cache():
"""Enable requests library cache."""
try:
import requests_cache
except ImportError as err:
sys.stderr.write("Failed to enable cache: {0}\n".format(str(err)))
return
if not os.path.exists(CACHE_DIR):
os.makedirs(CACHE_DIR)
requests_cache.install_cache(CACHE_FILE)
示例10: setup_paasta_api
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def setup_paasta_api():
if os.environ.get("PAASTA_API_DEBUG"):
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.WARNING)
# pyinotify is a better solution than turning off file caching completely
service_configuration_lib.disable_yaml_cache()
settings.system_paasta_config = load_system_paasta_config()
if os.environ.get("PAASTA_API_CLUSTER"):
settings.cluster = os.environ.get("PAASTA_API_CLUSTER")
else:
settings.cluster = settings.system_paasta_config.get_cluster()
settings.marathon_clients = marathon_tools.get_marathon_clients(
marathon_tools.get_marathon_servers(settings.system_paasta_config)
)
settings.marathon_servers = marathon_tools.get_marathon_servers(
system_paasta_config=settings.system_paasta_config
)
settings.marathon_clients = marathon_tools.get_marathon_clients(
marathon_servers=settings.marathon_servers, cached=False
)
try:
settings.kubernetes_client = kubernetes_tools.KubeClient()
except FileNotFoundError:
log.info("Kubernetes not found")
settings.kubernetes_client = None
except Exception:
log.exception("Error while initializing KubeClient")
settings.kubernetes_client = None
# Set up transparent cache for http API calls. With expire_after, responses
# are removed only when the same request is made. Expired storage is not a
# concern here. Thus remove_expired_responses is not needed.
requests_cache.install_cache("paasta-api", backend="memory", expire_after=5)
示例11: use_requests_cache
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def use_requests_cache(
cache_name: str, backend: str = "memory", **kwargs: Any
) -> Callable[[_UseRequestsCacheFuncT], _UseRequestsCacheFuncT]:
def wrap(fun: _UseRequestsCacheFuncT) -> _UseRequestsCacheFuncT:
def fun_with_cache(*args: Any, **kwargs: Any) -> Any:
requests_cache.install_cache(cache_name, backend=backend, **kwargs)
result = fun(*args, **kwargs)
requests_cache.uninstall_cache()
return result
return cast(_UseRequestsCacheFuncT, fun_with_cache)
return wrap
示例12: fetch
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def fetch(outfile):
"""The main function for downloading all scripts from github."""
if not os.path.exists(REQUESTS_CACHE):
os.makedirs(REQUESTS_CACHE)
requests_cache.install_cache(REQUESTS_CACHE)
result = []
label_counts = defaultdict(int)
print('Fetching scripts')
for label, url in DATA_URLS.items():
print(url)
scripts = fetch_scripts(url)
for script in scripts:
try:
result.append({
'tree': build_tree(script), 'metadata': {'label': label}
})
label_counts[label] += 1
except Exception as err:
print(err)
print('Label counts: ', label_counts)
print('Dumping scripts')
with open(outfile, 'wb') as file_handler:
pickle.dump(result, file_handler)
示例13: set_cache
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def set_cache(refresh=False):
""" install the static Requests cache """
if refresh:
expire_after = datetime.timedelta(seconds=0)
else:
expire_after = datetime.timedelta(days=30)
requests_cache.install_cache(
cache_name=os.path.join(os.path.dirname(__file__), "cache"),
allowable_methods=('GET', 'POST'), expire_after=expire_after)
requests_cache.core.remove_expired_responses()
示例14: pytest_configure
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def pytest_configure(config):
if config.getoption('--use-cache'):
import requests_cache
requests_cache.install_cache('test_cache')
api = Api()
pytest.game_ids = api.GetSeasonGameIDs('2009-10', 'Regular Season')[:2] # Hack to carry the gameids to tests
pytest.game_ids = ['0020900292']
示例15: use_requests_cache
# 需要導入模塊: import requests_cache [as 別名]
# 或者: from requests_cache import install_cache [as 別名]
def use_requests_cache():
import requests_cache
requests_cache.install_cache('test_cache')