method chains: keep
Method chains are an amazing programming pattern if you're building an API that needs to be expressive in how it changes data. In this series of video's we will make a pandas-like datacontainer for lists of dictionaries.
We'll load in the data via;
import json import pathlib poke_dict = json.loads(pathlib.Path("pokemon.json").read_text())
Next well define a new class called
Clumper. The idea is that this
class will allow us to more flexibly analyse the list of json objects for us.
class Clumper: def __init__(self, blob): self.blob = blob def keep(self, func): return [d for d in self.blob if func(d)]
You can see how it works by running.
Clumper(poke_dict).keep(lambda d: 'Grass' in d['type'])
Can you see a downside with this current approach though?
Feedback? See an issue? Something unclear? Feel free to mention it here.
If you want to be kept up to date, consider signing up for the newsletter.