예제 #1
0
 def __init__(self, count, freq, cachedir='', pairs=[]):
     self.count = max(count, 1)
     self.d = {}
     self.lastmutex = Semaphore(1)
     self.first = None
     self.last = None
     for key, value in pairs:
         self[key] = value
     # Set the frequency to something like 1/100th of
     # the expected dictionary final size to achieve
     # best performance.
     self.diskcache = DictCache(freq, cachedir)
예제 #2
0
 def __init__(self,
              router_config: str='uniswap',
              name: Optional[str]=None,
              filter_address: Sequence[str]=None):
     script_dir = os.path.dirname(os.path.realpath(__file__))
     self.name = type(self).__name__ if name is None else name
     with open(os.path.join(script_dir, 'config.json')) as f:
         self._config = json5.load(f)
     self._uniswap: Dict[str, UniswapV2Client] = {}
     self.router_config = router_config
     self._web3: Optional[Web3] = None
     self._web3_write: Optional[Web3] = None
     self._abi_cache: Dict[str, str] = {}
     self.wait_async = 0.1
     self.token_cache = DictCache("token-" + self.name)
     self.pair_cache = DictCache("pair-" + self.name)
     self.reserves_cache = DictCache("reserves-" + self.name)
     self.deadline = 1800.0
     self.default_gas_price = \
         self.web3().toWei(15, "gwei")
     self._weth_address: Optional[str] = None
     self.execute_trade = False
     self.filter_address = [ self.uniswap().router_address.lower() ] \
         if filter_address is None else [x.lower() for x in filter_address]
예제 #3
0
class LRU2(object):
    """
    Implementation of a length-limited O(1) LRU queue
    with disk caching. 
    """

    # This LRU drops off items to a disk dictionary cache
    # when older items are dropped.
    def __init__(self, count, freq, cachedir='', pairs=[]):
        self.count = max(count, 1)
        self.d = {}
        self.lastmutex = Semaphore(1)
        self.first = None
        self.last = None
        for key, value in pairs:
            self[key] = value
        # Set the frequency to something like 1/100th of
        # the expected dictionary final size to achieve
        # best performance.
        self.diskcache = DictCache(freq, cachedir)

    def __copy__(self):
        lrucopy = LRU(self.count)
        lrucopy.first = copy.copy(self.first)
        lrucopy.last = copy.copy(self.last)
        lrucopy.d = self.d.copy()
        for key, value in self.iteritems():
            lrucopy[key] = value

        return lrucopy

    def __contains__(self, obj):
        return obj in self.d

    def __getitem__(self, obj):
        try:
            a = self.d[obj].me
            self[a[0]] = a[1]
            return a[1]
        except (KeyError, AttributeError):
            return self.diskcache[obj]

    def __setitem__(self, obj, val):
        if obj in self.d:
            del self[obj]
        nobj = Node(self.last, (obj, val))
        if self.first is None:
            self.first = nobj
        self.lastmutex.acquire()
        try:
            if self.last:
                self.last.next = nobj
            self.last = nobj
        except:
            pass
        self.lastmutex.release()
        self.d[obj] = nobj
        if len(self.d) > self.count:
            self.lastmutex.acquire()
            try:
                if self.first == self.last:
                    self.first = None
                    self.last = None
                    self.lastmutex.release()
                    return
            except:
                pass
            self.lastmutex.release()
            a = self.first
            if a:
                if a.next:
                    a.next.prev = None
                self.first = a.next
                a.next = None
            try:
                key, val = a.me[0], self.d[a.me[0]]
                del self.d[a.me[0]]
                del a
                self.diskcache[key] = val.me[1]
            except (KeyError, AttributeError):
                pass

    def __delitem__(self, obj):
        nobj = self.d[obj]
        if nobj.prev:
            nobj.prev.next = nobj.next
        else:
            self.first = nobj.next
        if nobj.next:
            nobj.next.prev = nobj.prev
        else:
            self.last = nobj.prev
        del self.d[obj]

    def __iter__(self):
        cur = self.first
        while cur != None:
            cur2 = cur.next
            yield cur.me[1]
            cur = cur2

    def iteritems(self):
        cur = self.first
        while cur != None:
            cur2 = cur.next
            yield cur.me
            cur = cur2

    def iterkeys(self):
        return iter(self.d)

    def itervalues(self):
        for i, j in self.iteritems():
            yield j

    def keys(self):
        return self.d.keys()

    def clear(self):
        self.d.clear()
        self.diskcache.clear()

    def __len__(self):
        return len(self.d)

    def get_stats(self):
        """ Return statistics as a dictionary """

        return self.diskcache.get_stats()

    def test(self, N):

        # Test to see if the diskcache works. Pass
        # the total number of items added to this
        # function...

        flag = True

        for x in range(N):
            if self[x] == None:
                flag = False
                break

        return flag