Python supercharged for fastai development
56
Powerful collection classes with functional programming methods, lazy evaluation support, and advanced indexing capabilities. The core is the L class which extends Python lists with extensive functional programming operations.
The L class provides a feature-rich alternative to Python's built-in list with functional programming methods, advanced indexing, and seamless integration with numpy arrays and pandas DataFrames.
class L(GetAttr, CollBase):
"""
Enhanced list class with functional programming methods and advanced indexing.
Features:
- Functional operations: map, filter, reduce
- Advanced indexing with masks and lists of indices
- Method chaining for fluent APIs
- Integration with numpy arrays and pandas DataFrames
- Lazy evaluation where possible
Parameters:
- items: Initial items for the list
- *rest: Additional items to include
- use_list: bool, force list conversion
- match: Object to match length against
"""
def __init__(self, items=None, *rest, use_list=False, match=None): ...
# Enhanced indexing
def __getitem__(self, idx):
"""
Retrieve items by index, mask, or list of indices.
Parameters:
- idx: int, slice, boolean mask, or list of indices
Returns:
Single item or new L instance
"""
def __setitem__(self, idx, o):
"""
Set items at specified indices with broadcasting support.
Parameters:
- idx: int, list of indices, or boolean mask
- o: Value(s) to set (broadcast if not iterable)
"""
# Functional operations
def map(self, f, *args, **kwargs):
"""
Apply function to all items, return new L.
Parameters:
- f: Function to apply
- *args: Arguments passed to f
- **kwargs: Keyword arguments passed to f
Returns:
L: New L with function applied to all items
"""
def filter(self, f=noop, negate=False, **kwargs):
"""
Filter items by predicate function.
Parameters:
- f: Predicate function (default: noop)
- negate: bool, invert the filter condition
- **kwargs: Additional arguments
Returns:
L: New L with filtered items
"""
def argwhere(self, f, negate=False, **kwargs):
"""
Get indices where predicate function returns True.
Parameters:
- f: Predicate function
- negate: bool, invert condition
- **kwargs: Additional arguments
Returns:
L: Indices where condition is true
"""
def argfirst(self, f, negate=False):
"""
Get first index where predicate returns True.
Parameters:
- f: Predicate function
- negate: bool, invert condition
Returns:
Index of first matching item or None
"""Helper functions for working with collections and creating specialized iterators.
def mask2idxs(mask):
"""
Convert boolean mask or index list to integer indices.
Parameters:
- mask: Boolean mask, list of indices, or slice
Returns:
List of integer indices or original slice
"""
def is_indexer(idx):
"""
Test whether index will select a single item from a list.
Parameters:
- idx: Index to test
Returns:
bool: True if idx selects single item
"""
def cycle(o):
"""
Like itertools.cycle but handles empty collections gracefully.
Creates infinite cycle of [None] if input is empty.
Parameters:
- o: Collection to cycle through
Returns:
Iterator that cycles through items
"""
def zip_cycle(x, *args):
"""
Like itertools.zip_longest but cycles through all arguments except first.
Parameters:
- x: First iterable (not cycled)
- *args: Additional iterables (cycled)
Returns:
Iterator of tuples
"""
def is_bool(x):
"""
Check whether object is boolean or None.
Handles numpy boolean types correctly.
Parameters:
- x: Object to check
Returns:
bool: True if x is boolean-like
"""
def coll_repr(c, max_n=20):
"""
Create string representation of collection with truncation.
Parameters:
- c: Collection to represent
- max_n: Maximum number of items to show
Returns:
str: Truncated string representation
"""Foundation class for creating custom collection types.
class CollBase:
"""
Base class for composing collections with standard list interface.
Features:
- Standard sequence protocol (__len__, __getitem__, etc.)
- Automatic delegation to internal items storage
- Foundation for building custom collection types
Parameters:
- items: Internal storage for collection items
"""
def __init__(self, items): ...
def __len__(self): ...
def __getitem__(self, k): ...
def __setitem__(self, k, v): ...
def __delitem__(self, i): ...
def __repr__(self): ...
def __iter__(self): ...Classes for handling configuration files and settings.
class Config:
"""
Configuration management class with file I/O support.
Features:
- Read/write configuration files
- Attribute-style access to settings
- Support for multiple configuration formats
"""
def save_config_file(file, d, **kwargs):
"""
Write settings dictionary to configuration file.
Parameters:
- file: Path to configuration file
- d: Dictionary of settings to save
- **kwargs: Additional options for file writing
"""
def read_config_file(file, **kwargs):
"""
Read configuration file into dictionary.
Parameters:
- file: Path to configuration file
- **kwargs: Additional options for file reading
Returns:
dict: Configuration settings
"""Extended functionality for the L class including mathematical operations, transformations, and aggregations.
# Class methods for L creation
@classmethod
def L.split(cls, s, sep=None, maxsplit=-1):
"""Create L by splitting string. Same as str.split but returns L."""
@classmethod
def L.splitlines(cls, s, keepends=False):
"""Create L from string lines. Same as str.splitlines but returns L."""
@classmethod
def L.range(cls, a, b=None, step=None):
"""Create L from range. Same as range but returns L."""
# Transformation methods
def sorted(self, key=None, reverse=False, cmp=None, **kwargs):
"""Return new sorted L. If key is str use attrgetter, if int use itemgetter."""
def unique(self, sort=False, bidir=False, start=None):
"""Return L with unique items in stable order."""
def shuffle(self):
"""Return new L with items shuffled (not in-place)."""
def enumerate(self):
"""Return L of (index, item) tuples."""
def renumerate(self):
"""Return L of (reversed_index, item) tuples."""
# Attribute and item access
def itemgot(self, *idxs):
"""Create new L with item at specified indices from all items."""
def attrgot(self, k, default=None):
"""Create new L with attribute k (or dict key k) from all items."""
# Combining and transformations
def starmap(self, f, *args, **kwargs):
"""Apply function using itertools.starmap (unpacking arguments)."""
def zip(self, cycled=False):
"""Create new L with zip of all items. Use cycled=True for zip_cycle."""
def zipwith(self, *rest, cycled=False):
"""Zip self with additional iterables."""
def map_zip(self, f, *args, cycled=False, **kwargs):
"""Combine zip and starmap operations."""
def map_zipwith(self, f, *rest, cycled=False, **kwargs):
"""Combine zipwith and starmap operations."""
def concat(self):
"""Concatenate all elements (flatten one level)."""
# Aggregations
def reduce(self, f, initial=None):
"""Apply functools.reduce with optional initial value."""
def sum(self):
"""Sum of all numeric items."""
def product(self):
"""Product of all numeric items."""
# Utilities
def val2idx(self):
"""Create dictionary mapping values to their indices."""
def groupby(self, key, val=noop):
"""Group items by key function."""
def map_dict(self, f=noop, *args, **kwargs):
"""Create dictionary mapping items to function results."""
def map_first(self, f=noop, g=noop, *args, **kwargs):
"""Get first element after applying map and optional filter g."""
def setattrs(self, attr, val):
"""Set attribute on all items in the collection."""from fastcore.foundation import L
# Create enhanced list
data = L([1, 2, 3, 4, 5])
# Functional operations with chaining
result = (data
.filter(lambda x: x % 2 == 0) # Keep even numbers: [2, 4]
.map(lambda x: x ** 2) # Square them: [4, 16]
.sum()) # Sum: 20
# Advanced indexing
numbers = L(range(10))
evens = numbers.filter(lambda x: x % 2 == 0) # L([0, 2, 4, 6, 8])
subset = numbers[[1, 3, 5]] # L([1, 3, 5]) - index by list
masked = numbers[numbers > 5] # L([6, 7, 8, 9]) - boolean maskfrom fastcore.foundation import L
# Working with objects
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f"Person('{self.name}', {self.age})"
people = L([
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 35)
])
# Extract attributes
names = people.attrgot('name') # L(['Alice', 'Bob', 'Charlie'])
adults = people.filter(lambda p: p.age >= 30) # L([Person('Alice', 30), Person('Charlie', 35)])
# Working with dictionaries
data = L([
{"name": "Alice", "score": 95},
{"name": "Bob", "score": 87},
{"name": "Charlie", "score": 92}
])
high_scores = data.filter(lambda x: x['score'] > 90)
names = data.attrgot('name') # Works with dicts toofrom fastcore.foundation import L
# String operations
text = "hello world how are you"
words = L.split(text) # L(['hello', 'world', 'how', 'are', 'you'])
# Mathematical operations
numbers = L.range(1, 11) # L([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
factorial = numbers.reduce(lambda a, b: a * b) # 3628800
# Grouping and aggregation
data = L([
('A', 1), ('B', 2), ('A', 3), ('B', 4), ('A', 5)
])
grouped = data.groupby(lambda x: x[0]) # Group by first element
# Returns: {'A': [('A', 1), ('A', 3), ('A', 5)], 'B': [('B', 2), ('B', 4)]}
# Zipping operations
x = L([1, 2, 3])
y = L(['a', 'b'])
paired = x.zipwith(y, cycled=True) # L([(1, 'a'), (2, 'b'), (3, 'a')])from fastcore.foundation import L
import numpy as np
import pandas as pd
# Works with numpy arrays
arr = np.array([1, 2, 3, 4, 5])
l_arr = L(arr)
filtered = l_arr.filter(lambda x: x > 2) # L([3, 4, 5])
# Works with pandas Series
series = pd.Series([10, 20, 30, 40, 50])
l_series = L(series)
doubled = l_series.map(lambda x: x * 2) # L([20, 40, 60, 80, 100])
# Boolean indexing like numpy
mask = l_arr > 2
result = l_arr[mask] # L([3, 4, 5])Install with Tessl CLI
npx tessl i tessl/pypi-fastcoredocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10