Skip to content

Instantly share code, notes, and snippets.

@robgolding
Last active November 7, 2021 07:06
Show Gist options
  • Save robgolding/65c1d10c610c7a90e417 to your computer and use it in GitHub Desktop.
Save robgolding/65c1d10c610c7a90e417 to your computer and use it in GitHub Desktop.
Python Collections ABC
import collections
class LazyLoadedList(collections.Sequence):
def __init__(self, url):
self.url = url
self.page = 0
self.num_items = 0
self.store = []
def load_data(self):
data = requests.get(self.url, params={
'page': self.page,
}).json()
self.num_items = data['total']
objects = data.get('objects', [])
self.store += objects
return len(objects)
def __getitem__(self, index):
while index >= len(self):
self.page += 1
if not self.load_data():
break
return self.store[index]
def __len__(self):
return self.num_items
import collections
class ListBasedSet(collections.MutableSet):
store = None
def __init__(self, items):
self.store = list(items) or []
def __contains__(self, item):
return item in self.store
def __iter__(self):
return iter(self.store)
def __len__(self):
return len(self.store)
def add(self, item):
if item not in self.store:
self.store.append(item)
def discard(self, item):
try:
self.store.remove(item)
except ValueError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment