Recipes¶
Short solutions to common tasks. See the Tutorial for the basics.
Accept one item or many¶
import camina
def load(paths):
return [camina.pathlibify(p) for p in camina.listify(paths)]
load("a.csv") # [Path('a.csv')]
load(["a.csv", "b.csv"]) # [Path('a.csv'), Path('b.csv')]
Namespace keys¶
camina.add_prefix({"size": 1, "color": 2}, "widget", divider="_")
# {'widget_size': 1, 'widget_color': 2}
camina.drop_prefix({"widget_size": 1}, "widget", divider="_")
# {'size': 1}
List the public attributes of an object¶
camina.drop_privates(dir(obj)) # no names that start with an underscore
camina.drop_dunders(dir(obj)) # keeps single-underscore names
Parse settings that arrive as strings¶
{k: camina.typify(v) for k, v in {"n": "3", "debug": "yes", "ids": "1, 2"}.items()}
# {'n': 3, 'debug': True, 'ids': [1, 2]}
Compare neighbors in a sequence¶
Make a dataclass use less memory¶
@dataclasses.dataclass
class Point:
x: float = 0.0
y: float = 0.0
Point = camina.add_slots(Point) # instances no longer have a __dict__
Note that the zero-argument form of super() does not work inside methods of
a class returned by add_slots. Alternatively, use
dataclass(slots=True).