How to use the cachecontrol.heuristics.LastModified function in CacheControl

To help you get started, we’ve selected a few CacheControl examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ionrock / cachecontrol / tests / test_expires_heuristics.py View on Github external
def setup(self):
        self.sess = Session()
        self.cached_sess = CacheControl(self.sess, heuristic=LastModified())
github ionrock / cachecontrol / tests / test_expires_heuristics.py View on Github external
def setup(self):
        self.heuristic = LastModified()
        self.time_now = time.time()
        day_in_seconds = 86400
        self.year_ago = self.last_modified(day_in_seconds * 365)
        self.week_ago = self.last_modified(day_in_seconds * 7)
        self.day_ago = self.last_modified(day_in_seconds)
        self.now = self.last_modified(0)

        # NOTE: We pass in a negative to get a positive... Probably
        #       should refactor.
        self.day_ahead = self.last_modified(-day_in_seconds)
github plotdevice / plotdevice / plotdevice / util / readers.py View on Github external
def csv_dialect(fd):
    snippet = fd.read(1024).encode('utf-8') if PY2 else fd.read(1024)
    fd.seek(0)
    return csv.Sniffer().sniff(snippet)


### HTTP utils ###

try:
    import requests
    from cachecontrol import CacheControl, CacheControlAdapter
    from cachecontrol.caches import FileCache
    from cachecontrol.heuristics import LastModified

    cache_dir = '%s/Library/Caches/PlotDevice'%os.environ['HOME']
    HTTP = CacheControl(requests.Session(), cache=FileCache(cache_dir), heuristic=LastModified())
except ImportError:
    class Decoy(object):
        def get(self, url):
            unsupported = 'could not find the "requests" library (try running "python setup.py build" first)'
            raise RuntimeError(unsupported)
    HTTP = Decoy()

def binaryish(content, format):
    bin_types = ('pdf','eps','png','jpg','jpeg','gif','tiff','tif','zip','tar','gz')
    bin_formats = ('raw','bytes','img','image')
    if any(b in content for b in bin_types):
        return True
    if format:
        return any(b in format for b in bin_types+bin_formats)
    return False
github gateway4labs / labmanager / labmanager / rlms / caches.py View on Github external
from functools import wraps

import requests
from cachecontrol import CacheControl
from cachecontrol.caches import FileCache
from cachecontrol.heuristics import LastModified, TIME_FMT

from email.utils import formatdate, parsedate, parsedate_tz

from flask import g, request
from sqlalchemy.exc import IntegrityError
from labmanager.db import db
from labmanager.application import app
from labmanager.models import RLMSTypeCache, RLMSCache

class LastModifiedNoDate(LastModified):
    """ This takes the original LastModified implementation of 
    cachecontrol, but defaults the date in case it is not provided.
    """
    def __init__(self, require_date = True, error_margin = None):
        if error_margin is None:
            if require_date:
                self.error_margin = 0.1
            else:
                self.error_margin = 0.2
        else:
            self.error_margin = error_margin
        self.require_date = require_date

    def update_headers(self, resp):
        headers = resp.headers
        if 'expires' in headers:
github plotdevice / plotdevice / plotdevice / util / __init__.py View on Github external
def csv_dialect(fd):
    snippet = fd.read(1024).encode('utf-8') if PY2 else fd.read(1024)
    fd.seek(0)
    return csv.Sniffer().sniff(snippet)



import requests
from cachecontrol import CacheControl, CacheControlAdapter
from cachecontrol.caches import FileCache
from cachecontrol.heuristics import LastModified
from urlparse import urlparse

cache_dir = '%s/Library/Caches/PlotDevice'%os.environ['HOME']
HTTP = CacheControl(requests.Session(), cache=FileCache(cache_dir), heuristic=LastModified())

def binaryish(content, format):
    bin_types = ['pdf','eps','png','jpg','jpeg','gif','tiff','tif','zip','tar','gz']
    bin_formats = ['raw','bytes','img','image']
    if any(b in content for b in bin_types):
        return True
    if format:
        return any(b in format for b in bin_types+bin_formats)
    return False

def read(pth, format=None, encoding=None, cols=None, **kwargs):
    """Returns the contents of a file into a string or format-dependent data
    type (with special handling for json and csv files).

    The format will either be inferred from the file extension or can be set
    explicitly using the `format` arg. Text will be read using the specified
github NikhilNarayana / FRC-YouTube-Uploader / tbaAPI.py View on Github external
# Written by Wes Jordan and found here: Python TBA API Layer (https://github.com/Thing342/pyTBA)
import simplejson as json
import numpy
import requests
import hashlib
import re

from cachecontrol import CacheControl
from cachecontrol.heuristics import LastModified

app_id = {'X-TBA-App-Id': ""}
trusted_auth = {'X-TBA-Auth-Id': "", 'X-TBA-Auth-Sig': ""}

s = requests.Session()
s = CacheControl(s, heuristic=LastModified())
s.headers.update(app_id)


class Event:
	def __init__(self, info, teams, matches, awards, rankings):
		self.key = info['key']
		self.info = info
		self.teams = list(filter(lambda team: len(list(filter(
			lambda match: team['key'] in match['alliances']['red']['teams'] or team['key'] in
																			   match['alliances']['blue']['teams'],
			matches))) > 0, teams))
		self.matches = sorted(matches, key=match_sort_key)
		self.awards = awards
		self.rankings = rankings

	def get_match(self, match_key):