### Install glom Source: https://github.com/mahmoud/glom/blob/master/docs/index.md Install the glom library using pip. This command is used to add glom to your Python environment. ```bash pip install glom ``` -------------------------------- ### MatchError Example Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Demonstrates a MatchError raised when a Match or M check fails due to a key mismatch. ```pycon >>> glom({123: 'a'}, Match({'id': int})) Traceback (most recent call last): ... MatchError: key 123 didn't match any of ['id'] ``` -------------------------------- ### Chaining specs() Calls for Multiple Arguments Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Chain multiple specs() calls to provide multiple arguments that will be resolved from the target. This example provides 'start', 'end', and 'step' to range. ```pycon >>> spec = Invoke(range).specs('start').specs('end', 'step') >>> target = {'start': 2, 'end': 10, 'step': 2} >>> glom(target, (spec, list)) [2, 4, 6, 8] ``` -------------------------------- ### T Roundtripping Example Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates that T objects can be used in specifications and will be preserved as T objects when the specification itself is evaluated or represented. ```python >>> T['a'].b['c']('success') T['a'].b['c']('success') ``` -------------------------------- ### Customize Sum initialization Source: https://github.com/mahmoud/glom/blob/master/docs/grouping.md To change the starting value for a Sum operation, provide a callable to the 'init' parameter. This is useful for non-zero starting points or different numerical types like floats. ```python >>> glom(range(5), Sum(init=lambda: 5.0)) 15.0 ``` -------------------------------- ### PathAccessError Example Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates a PathAccessError when trying to access a non-existent key in nested dictionaries. ```python >>> target = {'a': {'b': None}} >>> glom(target, 'a.b.c') Traceback (most recent call last): ...PathAccessError: could not access 'c', part 2 of Path('a', 'b', 'c'), got error: ... ``` -------------------------------- ### Mixing constants() and specs() with Invoke Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Combine constants() for fixed arguments and specs() for dynamic arguments. This example multiplies a value by 3. ```pycon >>> multiply = lambda x, y: x * y >>> times_3 = Invoke(multiply).constants(y=3).specs(x='value') >>> glom({'value': 5}, times_3) 15 ``` -------------------------------- ### glom CLI Usage Source: https://github.com/mahmoud/glom/blob/master/README.md Example usage of the glom command-line interface for data access and restructuring. It supports specifying target data and spec files in various formats. ```bash Usage: glom [FLAGS] [spec [target]] Command-line interface to the glom library, providing nested data access and data restructuring with the power of Python. Flags: --help / -h show this help message and exit --target-file TARGET_FILE path to target data source (optional) --target-format TARGET_FORMAT format of the source data (json, python, toml, or yaml) (defaults to 'json') --spec-file SPEC_FILE path to glom spec definition (optional) --spec-format SPEC_FORMAT format of the glom spec definition (json, python, python-full) (defaults to 'python') --indent INDENT number of spaces to indent the result, 0 to disable pretty-printing (defaults to 2) --debug interactively debug any errors that come up --inspect interactively explore the data ``` -------------------------------- ### Evaluating Zero-Argument Functions with Invoke Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Invoke can evaluate zero-argument functions. This example shows invoking the int() constructor. ```pycon >>> glom(target={}, spec=Invoke(int)) 0 ``` -------------------------------- ### Define Target Data Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Example of defining a list of dictionaries to be used as target data for matching. ```default >>> target = [ ... {'id': 1, 'email': 'alice@example.com'}, ... {'id': 2, 'email': 'bob@example.com'}] ``` -------------------------------- ### Chained Iter methods for transformation and deduplication Source: https://github.com/mahmoud/glom/blob/master/docs/streaming.md Shows an alternative way to achieve the same result as the previous example by chaining Iter methods: map and unique. ```python >>> glom(['1', '2', '1', '3'], (Iter().map(int).unique(), tuple)) (1, 2, 3) ``` -------------------------------- ### Handle Path Access Errors Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md When data is missing, glom raises a PathAccessError. This example shows how to catch and interpret such errors. ```pycon >>> glom(data2, 'a.b.c') Traceback (most recent call last): ... PathAccessError: could not access 'c', index 2 in path Path('a', 'b', 'c'), got error: ... ``` -------------------------------- ### Custom `Fill` Mode Implementation in Python Source: https://github.com/mahmoud/glom/blob/master/docs/modes.md An example of implementing a custom `Fill` mode for `glom`. This mode prioritizes filling containers rather than iterating or chaining. It requires a `glomit` method and a helper function to handle recursive calls. ```python class Fill(object): def __init__(self, spec): self.spec = spec def glomit(self, target, scope): scope[MODE] = _fill return scope[glom](target, self.spec, scope) def _fill(target, spec, scope): recurse = lambda val: scope[glom](target, val, scope) if type(spec) is dict: return {recurse(key): recurse(val) for key, val in spec.items()} if type(spec) in (list, tuple, set, frozenset): result = [recurse(val) for val in spec] if type(spec) is list: return result return type(spec)(result) if callable(spec): return spec(target) return spec ``` -------------------------------- ### ElementTree to Tag-Children Tuples Example Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Demonstrates the usage of the etree2tuples spec to convert an HTML string into nested tag-children tuples using Glom. ```python >>> etree = ElementTree.fromstring(html_text) >>> glom(etree, etree2tuples) ('html', [('head', [('title', [])]), ('body', [('p', [])])]) ``` -------------------------------- ### TypeMatchError Example Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Illustrates a TypeMatchError, a subtype of MatchError, raised when a Match fails a type check. ```pycon >>> glom({'id': 'a'}, Match({'id': int})) Traceback (most recent call last): ... TypeMatchError: error raised while processing. Target-spec trace, with error detail (most recent last): - Target: {'id': 'a'} - Spec: Match({'id': }) - Spec: {'id': } - Target: 'a' - Spec: int TypeMatchError: expected type int, not str ``` -------------------------------- ### Using Invoke star() with args Spec Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use the star() method with an 'args' spec to unpack a list of values as positional arguments to a callable. This example zips multiple lists. ```pycon >>> spec = Invoke(zip).star(args='lists') >>> target = {'lists': [[1, 2], [3, 4], [5, 6]]} >>> list(glom(target, spec)) [(1, 3, 5), (2, 4, 6)] ``` -------------------------------- ### UnregisteredTarget Error Example Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Shows an UnregisteredTarget error when attempting an unsupported operation (iteration) on an unregistered target type. ```python >>> glom(object(), ['a.b.c']) Traceback (most recent call last): ...UnregisteredTarget: target type 'object' not registered for 'iterate', expected one of registered types: (...) ``` -------------------------------- ### Using constants() for Keyword Arguments Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use constants() to specify keyword arguments for a callable. This example rounds a float to two decimal places. ```pycon >>> round_2 = Invoke(round).constants(ndigits=2).specs(T) >>> glom(3.14159, round_2) 3.14 ``` -------------------------------- ### Glom PathAccessError Example Source: https://github.com/mahmoud/glom/blob/master/docs/debugging.md Demonstrates how a `PathAccessError` occurs when a spec attempts to access a non-existent key in the target data. The output includes a data traceback showing the target and spec at each step of the evaluation, culminating in the specific `KeyError`. ```python target = {'planets': [{'name': 'earth', 'moons': 1}]} glom(target, ('planets', ['rings'])) ``` -------------------------------- ### Using Invoke specs() with a Single Argument Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use specs() to specify an argument that will be resolved from the target. This example uses 'value' to provide the argument to range. ```pycon >>> spec = Invoke(range).specs('value') >>> glom({'value': 5}, (spec, list)) [0, 1, 2, 3, 4] ``` -------------------------------- ### Chaining constants() Calls Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Chaining multiple constants() calls appends arguments. This example passes two fixed arguments to a callable. ```pycon >>> spec = Invoke(T).constants(2).constants(10, 2) >>> glom(range, (spec, list)) [2, 4, 6, 8] ``` -------------------------------- ### Handle PathAssignError when assigning out of range Source: https://github.com/mahmoud/glom/blob/master/docs/mutation.md This example demonstrates a PathAssignError that occurs when attempting to assign a value to an index beyond the bounds of a list. ```default >>> assign(["short", "list"], Path(5), 'too far') Traceback (most recent call last): ...PathAssignError: could not assign 5 on object at Path(), got error: IndexError(... ``` -------------------------------- ### Alternative Parallel Evaluation with Dict and T.values() Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Demonstrates an alternative method to achieve parallel evaluation of sub-specs using a dictionary spec and the T.values() accessor. This approach is presented as the simplest way to get sequential results without a custom extension. ```python glom('1', ({1: float, 2: int}, T.values())) ``` -------------------------------- ### CheckError Example Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Shows a CheckError raised when target data fails to pass a Check's specified validation, such as a type mismatch. ```default >>> target = {'a': {'b': 'c'}} >>> glom(target, {'b': ('a.b', Check(type=int))}) Traceback (most recent call last): ... CheckError: target at path ['a.b'] failed check, got error: "expected type to be 'int', found type 'str'" ``` -------------------------------- ### Debugging a Coalesce Error Tree Source: https://github.com/mahmoud/glom/blob/master/docs/debugging.md This example demonstrates how to read the error tree generated by a Coalesce spec when it encounters exceptions. Pay attention to the '+' and 'X' markers indicating branching and failures, and '|' characters for nesting. ```python >>> target = {'n': 'nope', 'xxx': {'z': {'v': 0}}} >>> glom(target, Coalesce(('xxx', 'z', 'n'), 'yyy')) Traceback (most recent call last): File "tmp.py", line 9, in _make_stack glom(target, spec) File "/home/mahmoud/projects/glom/glom/core.py", line 2029, in glom raise err glom.core.CoalesceError: error raised while processing, details below. Target-spec trace (most recent last): - Target: {'n': 'nope', 'xxx': {'z': {'v': 0}}} + Spec: Coalesce(('xxx', 'z', 'n'), 'yyy') |\ Spec: ('xxx', 'z', 'n') || Spec: 'xxx' || Target: {'z': {'v': 0}} || Spec: 'z' || Target: {'v': 0} || Spec: 'n' |X glom.core.PathAccessError: could not access 'n', part 0 of Path('n'), got error: KeyError('n') |\ Spec: 'yyy' |X glom.core.PathAccessError: could not access 'yyy', part 0 of Path('yyy'), got error: KeyError('yyy') glom.core.CoalesceError: no valid values found. Tried (('xxx', 'z', 'n'), 'yyy') and got (PathAccessError, PathAccessError) (at path ['xxx', 'z']) ``` -------------------------------- ### Glom Or Operator Example Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md The Or specifier tries child specs sequentially and returns the result of the first one that succeeds. If all children raise GlomError, a MatchError is raised. ```python >>> switch_spec = Match(Switch([(Or('a', 'e', 'i', 'o', 'u'), Val('vowel')), ... (And(str, M, M(T[2:]) == ''), Val('consonant'))])) ``` -------------------------------- ### glom.register_op Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Allows extension authors to add custom operations beyond the built-in ones ('get', 'iterate', 'keys', 'assign', 'delete') to the default scope. ```APIDOC ## glom.register_op(op_name, **kwargs) ### Description For extension authors needing to add operations beyond the builtin ‘get’, ‘iterate’, ‘keys’, ‘assign’, and ‘delete’ to the default scope. See TargetRegistry for more details. ``` -------------------------------- ### Filtering GitHub API Events with glom Source: https://github.com/mahmoud/glom/blob/master/docs/cli.md Installs glom and uses it to filter a GitHub API response, extracting specific fields into a more readable format. Data is piped from curl to glom via stdin. ```bash $ pip install glom $ curl -s https://api.github.com/repos/mahmoud/glom/events \ | glom '[{"type": "type", "date": "created_at", "user": "actor.login"}]' ``` -------------------------------- ### Clamp Values Above Threshold Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Shows a Glom idiom for clamping values within a range. This specific example clamps any value less than 7 to 7, effectively setting a maximum. It utilizes pattern matching with 'M' (the target value) and the OR operator '|' with 'Val()'. ```python glom(range(10), [(M < 7) | Val(7)]) # [0, 1, 2, 3, 4, 5, 6, 7, 7, 7] ``` -------------------------------- ### Get the first truthy item with Iter().first() Source: https://github.com/mahmoud/glom/blob/master/docs/streaming.md Use Iter().first() to lazily yield a single truthy item from an iterable. Accepts an optional condition (key) and a default value. ```python >>> target = [False, 1, 2, 3] >>> glom(target, Iter().first()) 1 ``` -------------------------------- ### Register Django ORM Handlers Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Registers glom to automatically handle Django ORM Managers and QuerySets by iterating over their .all() method. Call this early in application setup. ```python import glom import django.db.models glom.register(django.db.models.Manager, iterate=lambda m: m.all()) glom.register(django.db.models.QuerySet, iterate=lambda qs: qs.all()) ``` -------------------------------- ### Instantiate and Use Glommer Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates creating a Glommer instance and using its glom method for data retrieval with default values or custom transformations. ```python from glom import Glommer glommer = Glommer() glommer.glom({}, 'a.b.c', default='d') Glommer().glom({'vals': list(range(3))}, ('vals', len)) ``` -------------------------------- ### Using Assign vs. assign() convenience function Source: https://github.com/mahmoud/glom/blob/master/docs/faq.md Demonstrates the equivalence between using the Assign specifier with the glom() function and using the dedicated assign() convenience function for data mutation. ```python glom({}, Assign('a'), 'b') # is equivalent to assign({}, 'a', 'b') ``` -------------------------------- ### Basic M Comparisons Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Demonstrates basic comparison operations using M. If the comparison succeeds, the target is returned unchanged. If it fails, MatchError is thrown. ```python >>> glom(1, M > 0) 1 >>> glom(0, M == 0) 0 >>> glom('a', M != 'b') == 'a' True ``` -------------------------------- ### Create and Save a Contact Object Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md Initializes a Contact object with email and location, then saves it. Shows the resulting primary email and ID. ```pycon >>> from glom.tutorial import * >>> contact = Contact('Julian', ... emails=[Email(email='jlahey@svtp.info')], ... location='Canada') >>> contact.save() >>> contact.primary_email Email(id=5, email='jlahey@svtp.info', email_type='personal') >>> contact.add_date datetime.datetime(...) >>> contact.id 5 ``` -------------------------------- ### Implementing a Custom HelloWorldSpec Source: https://github.com/mahmoud/glom/blob/master/docs/custom_spec_types.md Shows how to create a custom specifier type by implementing the `glomit` method. Instances of this class can be used directly in glom specs. ```python class HelloWorldSpec(object): def glomit(self, target, scope): print("Hello, world!") return target ``` ```python from glom import glom target = {'example': 'object'} glom(target, HelloWorldSpec()) # prints "Hello, world!" and returns target ``` -------------------------------- ### glom CLI Help Message Source: https://github.com/mahmoud/glom/blob/master/docs/cli.md Displays the usage instructions and available flags for the glom CLI. ```text $ glom --help Usage: glom [FLAGS] [spec [target]] Command-line interface to the glom library, providing nested data access and data restructuring with the power of Python. Flags: --help / -h show this help message and exit --target-file TARGET_FILE path to target data source (optional) --target-format TARGET_FORMAT format of the source data (json, python, toml, or yaml) (defaults to 'json') --spec-file SPEC_FILE path to glom spec definition (optional) --spec-format SPEC_FORMAT format of the glom spec definition (json, python, python-full) (defaults to 'python') --indent INDENT number of spaces to indent the result, 0 to disable pretty-printing (defaults to 2) --debug interactively debug any errors that come up --inspect interactively explore the data ``` -------------------------------- ### Basic Iter usage for transformation and deduplication Source: https://github.com/mahmoud/glom/blob/master/docs/streaming.md Demonstrates converting strings to integers, deduplicating, and converting to a tuple using Iter with basic specifiers. ```python >>> glom(['1', '2', '1', '3'], (Iter(int), set, tuple)) (1, 2, 3) ``` -------------------------------- ### Creating Invoke Instance with specfunc Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use the class method specfunc to create an Invoke instance where the function itself is specified by a string key. ```pycon >>> spec = Invoke.specfunc('func').constants(5) >>> glom({'func': range}, (spec, list)) [0, 1, 2, 3, 4] ``` -------------------------------- ### Combining M with Other Types using '&' Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Demonstrates combining M expressions with other glom types like 'float' using the '&' operator to construct And instances. Parentheses are required due to operator precedence. ```python >>> glom(1.0, (M > 0) & float) 1.0 ``` -------------------------------- ### Handle PathDeleteError when deleting out of range Source: https://github.com/mahmoud/glom/blob/master/docs/mutation.md This example demonstrates a PathDeleteError that occurs when attempting to delete an element at an index beyond the bounds of a list. ```default >>> delete(["short", "list"], Path(5)) Traceback (most recent call last): ...PathDeleteError: could not delete 5 on object at Path(), got error: IndexError(... ``` -------------------------------- ### Update Scope with A (Shortcut) Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Illustrates using 'A' as a shortcut to assign the current target to a location in the scope. This is useful when only the target needs to be assigned. ```default >>> target = {'data': {'val': 9}} >>> spec = ('data.val', A.value, {'val': S.value}) >>> glom(target, spec) {'val': 9} ``` -------------------------------- ### Inspecting the glom Scope Source: https://github.com/mahmoud/glom/blob/master/docs/custom_spec_types.md Demonstrates how to inspect the glom scope within a custom specifier by printing its contents. The scope provides access to runtime state like target, spec, and path. ```python from glom import glom from pprint import pprint class ScopeInspectorSpec(object): def glomit(self, target, scope): pprint(dict(scope)) return target glom(target, ScopeInspectorSpec()) ``` -------------------------------- ### Basic Callable Usage in glom Source: https://github.com/mahmoud/glom/blob/master/docs/custom_spec_types.md Demonstrates using a built-in Python callable like `sum` directly within a glom spec for simple data transformations. ```default glom({'nums': range(5)}, ('nums', sum)) # 10 ``` -------------------------------- ### Using Coalesce for Fallback Behavior in Glom Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md Illustrates how to use `Coalesce` to specify fallback paths for data extraction, enabling graceful handling of schema changes. This is useful when a key might sometimes be present and sometimes absent, or when dealing with evolving data structures. ```python >>> from glom import Coalesce >>> target = { ... 'system': { ... 'planets': [ ... {'name': 'earth', 'moons': 1}, ... {'name': 'jupiter', 'moons': 95} ... ] ... } ... } >>> spec = { ... 'planets': (Coalesce('system.planets', 'system.dwarf_planets'), ['name']), ... 'moons': (Coalesce('system.planets', 'system.dwarf_planets'), ['moons']) ... } >>> pprint(glom(target, spec)) {'moons': [1, 95], 'planets': ['earth', 'jupiter']} ``` ```python >>> target = { ... 'system': { ... 'dwarf_planets': [ ... {'name': 'pluto', 'moons': 5}, ... {'name': 'ceres', 'moons': 0} ... ] ... } ... } >>> pprint(glom(target, spec)) {'moons': [5, 0], 'planets': ['pluto', 'ceres']} ``` -------------------------------- ### Construct Instance: List of Integers to Decimals Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Converts a list of integers to a list of decimal.Decimal objects using a simple type specifier. ```python glom([1, 2, 3], [Decimal]) ``` -------------------------------- ### Basic Attribute and Key Access with T Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates using T for chained attribute and key lookups to access nested data. This is an alternative to the dot-separated string shorthand. ```python >>> spec = T['a']['b']['c'] >>> target = {'a': {'b': {'c': 'd'}}} >>> glom(target, spec) 'd' ``` -------------------------------- ### Basic Glom Function Call Source: https://github.com/mahmoud/glom/blob/master/docs/faq.md The fundamental way to use the glom library is by calling the `glom()` function with a target data structure and a specification. ```default output = glom(target, spec) ``` -------------------------------- ### Delete items from a dictionary using Glom Source: https://github.com/mahmoud/glom/blob/master/docs/mutation.md Use the Delete specifier to remove items from nested dictionaries. This example shows deleting a specific element from a list within a dictionary. ```python >>> target = {'dict': {'x': [5, 6, 7]}} >>> glom(target, Delete('dict.x.1')) {'dict': {'x': [5, 7]}} ``` ```python >>> glom(target, Delete('dict.x')) {'dict': {}} ``` -------------------------------- ### glom.Sum Source: https://github.com/mahmoud/glom/blob/master/docs/grouping.md The Sum specifier type is used to aggregate integers and other numericals using addition, much like the sum() builtin. It takes an optional init parameter to set the starting value. ```APIDOC ## class glom.Sum(subspec=T, init=) The Sum specifier type is used to aggregate integers and other numericals using addition, much like the `sum()` builtin. ```pycon >>> glom(range(5), Sum()) 10 ``` Note that this specifier takes a callable *init* parameter like its friends, so to change the start value, be sure to wrap it in a callable: ```default >>> glom(range(5), Sum(init=lambda: 5.0)) 15.0 ``` To “sum” lists and other iterables, see the [`Flatten`](#glom.Flatten) spec. For other objects, see the [`Fold`](#glom.Fold) specifier type. ``` -------------------------------- ### Object Construction with T and Call Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Illustrates using T in conjunction with glom.Call to construct objects, passing target data as keyword arguments to the object's constructor. ```python >>> class ExampleClass(object): ... def __init__(self, attr): ... self.attr = attr ... >>> target = {'attr': 3.14} >>> glom(target, Call(ExampleClass, kwargs=T)).attr 3.14 ``` -------------------------------- ### glom.Inspect Source: https://github.com/mahmoud/glom/blob/master/docs/debugging.md The Inspect specifier provides a way to get visibility into glom's evaluation of a specification, enabling debugging of tricky problems with unexpected data. It can be inserted into a spec as a wrapper or as an argument-less placeholder. ```APIDOC ## class glom.Inspect(*a, **kw) ### Description The `Inspect` specifier type provides a way to get visibility into glom's evaluation of a specification, enabling debugging of those tricky problems that may arise with unexpected data. It can be inserted into an existing spec in one of two ways: as a wrapper around the spec in question, or as an argument-less placeholder wherever a spec could be. ### Parameters * **echo** (*bool*) – Whether to print the path, target, and output of each inspected glom. Defaults to True. * **recursive** (*bool*) – Whether or not the Inspect should be applied at every level, at or below the spec that it wraps. Defaults to False. * **breakpoint** (*bool*) – This flag controls whether a debugging prompt should appear before evaluating each inspected spec. Can also take a callable. Defaults to False. * **post_mortem** (*bool*) – This flag controls whether exceptions should be caught and interactively debugged with `pdb` on inspected specs. All arguments above are keyword-only to avoid overlap with a wrapped spec. ### Example ```pycon >>> target = {'a': {'b': {}}} >>> val = glom(target, Inspect('a.b')) # wrapping a spec --- path: ['a.b'] target: {'a': {'b': {}}} output: {} --- ``` ### NOTE Just like `pdb.set_trace()`, be careful about leaving stray `Inspect()` instances in production glom specs. ``` -------------------------------- ### Using the built-in Sum Specifier Source: https://github.com/mahmoud/glom/blob/master/docs/custom_spec_types.md Illustrates the usage of glom's built-in `Sum` specifier to calculate the sum of elements in a list. ```python from glom import glom, Sum glom([1, 2, 3], Sum()) ``` -------------------------------- ### Method Calls and List Conversion with T Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Shows how T can be used to call methods on target objects and process their results, such as iterating over dictionary items and converting them to a list. ```python >>> spec = ('a', (T['b'].items(), list)) # reviewed below >>> glom(target, spec) [('c', 'd')] ``` -------------------------------- ### Basic Match Spec for Data Structure Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Create a Match spec to ensure a list of dictionaries conforms to a specific structure with integer IDs and string emails. ```pycon >>> spec = Match([{'id': int, 'email': str}]) ``` -------------------------------- ### Construct Instance: List to deque with Max Size Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Converts a list to a collections.deque with a specified maximum size using Call or lambda. ```python glom([1, 2, 3], Call(deque, args=[T, 10])) glom([1, 2, 3], lambda t: deque(t, 10)) ``` -------------------------------- ### Using Invoke constants() with a Single Argument Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates using constants() to pass a fixed argument to a callable. The target is passed as the first argument. ```pycon >>> spec = Invoke(T).constants(5) >>> glom(range, (spec, list)) [0, 1, 2, 3, 4] ``` -------------------------------- ### Accessing nested data with glom Source: https://github.com/mahmoud/glom/blob/master/README.md Shows how to access nested data using glom's path-based access. It handles potential errors more gracefully than direct access. ```python >>> glom(data, 'a.b.c') 'd' >>> glom(data2, 'a.b.c') Traceback (most recent call last): ... PathAccessError: could not access 'c', index 2 in path Path('a', 'b', 'c'), got error: ... ``` -------------------------------- ### Path Indexing and Slicing Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Path objects support indexing and slicing, with each operation returning a new Path object. ```python >>> path = Path('a', 'b', 1, 2) >>> path[0] Path('a') >>> path[-2:] Path(1, 2) ``` -------------------------------- ### Apply Basic Match Spec Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Use glom() with a basic Match spec to validate and retrieve data that conforms to the expected pattern. ```pycon >>> result = glom(target, spec) >>> assert result == \ ...[{'id': 1, 'email': 'alice@example.com'}, {'id': 2, 'email': 'bob@example.com'}] ``` -------------------------------- ### Deep-get with Wildcard Selectors Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Access values from a list of dictionaries using wildcard selectors. ```python >>> target = {'a': [{'k': 'v1'}, {'k': 'v2'}]} >>> glom(target, 'a.*.k') ['v1', 'v2'] ``` -------------------------------- ### Mixing Positional and Keyword Arguments with Invoke Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use Invoke to sort a list of strings numerically in reverse order, by specifying key and reverse arguments. ```pycon >>> spec = Invoke(sorted).specs(T).constants(key=int, reverse=True) >>> target = ['10', '5', '20', '1'] >>> glom(target, spec) ['20', '10', '5', '1'] ``` -------------------------------- ### Sum integers with Glom Source: https://github.com/mahmoud/glom/blob/master/docs/grouping.md Use the Sum specifier to aggregate integers and other numerical types using addition. The default initialization value is 0. ```python >>> glom(range(5), Sum()) 10 ``` -------------------------------- ### Generate and Print API Response using Glom Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md Applies the glom specification to the target data and prints the resulting JSON response with indentation and sorted keys. ```pycon >>> target = Contact.objects.all() >>> resp = glom(target, spec) >>> print(json.dumps(resp, indent=2, sort_keys=True)) ``` -------------------------------- ### Aggregate iterable into a list with Iter().all() Source: https://github.com/mahmoud/glom/blob/master/docs/streaming.md Use Iter().all() as a convenience method to convert an iterable into a list. This spec consumes the entire iterable. ```python >>> glom(range(5), Iter(lambda t: t * 2).all()) [0, 2, 4, 6, 8] ``` -------------------------------- ### Update Scope with S() Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Shows how to dynamically update the scope using the 'S' object. Keyword arguments to 'S' are evaluated as specs and saved to the scope under the keyword name. ```default >>> target = {'data': {'val': 9}} >>> spec = (S(value=T['data']['val']), {'val': S['value']}) >>> glom(target, spec) {'val': 9} ``` -------------------------------- ### Glom Switch Specifier - Consonant Match Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Demonstrates using the Switch specifier with And to match consonant characters. The target 'z' is processed by the And specifier, resulting in 'consonant'. ```python >>> glom('z', switch_spec) 'consonant' ``` -------------------------------- ### Data-Driven Assignment with Glom Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md Handles dictionaries with dynamic keys by extracting specific values like moon counts. Uses Iter, Coalesce, and Merge for complex data structures. ```python >>> from glom import glom, T, Merge, Iter, Coalesce >>> target = { ... "pluto": {"moons": 5, "population": None}, ... "venus": {"population": {"aliens": 5}}, ... "earth": {"moons": 1, "population": {"humans": 7700000000, "aliens": 1}}, ... } >>> spec = { ... "moons": ( ... T.items(), ... Iter({T[0]: (T[1], Coalesce("moons", default=0))}), ... Merge(), ... ) ... } >>> pprint(glom(target, spec)) {'moons': {'earth': 1, 'pluto': 5, 'venus': 0}} ``` -------------------------------- ### Reverse List using Built-in and T object Source: https://github.com/mahmoud/glom/blob/master/docs/snippets.md Demonstrates two methods for reversing a list: using Python's built-in 'reversed' function and the glom T object's slicing. ```python glom([1, 2, 3], (reversed, list)) ``` ```python glom([1, 2, 3], T[::-1]) ``` -------------------------------- ### glom.Glommer Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Provides a way to encapsulate type registration context, allowing for localized control over type handling without affecting the global glom() behavior. Glommer instances have their own `glom()` method. ```APIDOC ## class glom.Glommer(**kwargs) ### Description The `Glommer` type mostly serves to encapsulate type registration context so that advanced uses of glom don’t need to worry about stepping on each other. Glommer objects are lightweight and, once instantiated, provide a `glom()` method. Instances also provide `register()` method for localized control over type handling. ### Parameters * **register_default_types** (*bool*) – Whether or not to enable the handling behaviors of the default `glom()`. These default actions include dict access, list and iterable iteration, and generic object attribute access. Defaults to True. ``` -------------------------------- ### Ignore missing paths during deletion with Glom Source: https://github.com/mahmoud/glom/blob/master/docs/mutation.md When using Delete, set `ignore_missing=True` to prevent errors if the target path does not exist in the data structure. ```python >>> glom(target, Delete('does_not_exist', ignore_missing=True)) {'dict': {}} ``` -------------------------------- ### Persistent State with S.globals Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Demonstrates using the pre-created global Vars object at S.globals to store state that persists throughout a glom() call. Values saved here are accessible across different parts of the spec. ```default >>> last_spec = ([A.globals.last], S.globals.last) >>> glom([3, 1, 4, 1, 5], last_spec) 5 ``` -------------------------------- ### Basic Nested Data Access Source: https://github.com/mahmoud/glom/blob/master/docs/tutorial.md Demonstrates direct access to nested dictionary values. This can lead to TypeErrors if intermediate values are None. ```default >>> data = {'a': {'b': {'c': 'd'}}} >>> data['a']['b']['c'] 'd' ``` -------------------------------- ### Basic Data Access with glom Source: https://github.com/mahmoud/glom/blob/master/docs/index.md Use the glom function to access nested data within a dictionary using a string path. This is a fundamental usage pattern for retrieving specific values. ```python from glom import glom target = {'a': {'b': {'c': 'd'}}} glom(target, 'a.b.c') # returns 'd' ``` -------------------------------- ### Skipping Specific Exceptions with Default Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Configure glom to ignore specific exceptions and return a default value instead of raising an error. ```python >>> glom({}, lambda x: 100.0 / len(x), default=0.0, skip_exc=ZeroDivisionError) 0.0 ``` -------------------------------- ### Coalesce with fallback values Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use Coalesce to try multiple subspecs in order and return the first successful result. If no subspec matches, a CoalesceError is raised. ```pycon >>> target = {'c': 'd'} >>> glom(target, Coalesce('a', 'b', 'c')) 'd' ``` -------------------------------- ### glom.Call Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Specifies when a target should be passed to a function. It is designed for readability and is similar to partial() but offers a better repr. ```APIDOC ## class glom.Call(func=None, args=None, kwargs=None) ### Description Specifies when a target should be passed to a function, `func`. This is similar to `partial()` but is designed for better readability and a better `repr`. ### Parameters * **func** (*callable*) – A function or other callable to be called with the target. ### Example ```pycon >>> class ExampleClass(object): ... def __init__(self, attr): ... self.attr = attr ... >>> target = {'attr': 3.14} >>> glom(target, Call(ExampleClass, kwargs=T)).attr 3.14 ``` #### NOTE `Call` is mostly for functions. Use a `T` object if you need to call a method. #### WARNING `Call` has a successor with a fuller-featured API, new in 19.10.0: the `Invoke` specifier type. ``` -------------------------------- ### glom.Ref Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Names a part of a spec and refers to it elsewhere in the same spec, which is useful for trees and other self-similar data structures. ```APIDOC ## class glom.Ref(name, subspec=Sentinel('_MISSING')) ### Description Names a part of a spec and refers to it elsewhere in the same spec. This is useful for trees and other self-similar data structures. ### Parameters * **name** (*str*) – The name of the spec to reference. * **subspec** – Pass a spec to name it *name*, or leave unset to refer to an already-named spec. ``` -------------------------------- ### Glom Switch Specifier - Vowel Match Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Demonstrates using the Switch specifier with Or to match vowel characters. The target 'a' is processed by the Or specifier, resulting in 'vowel'. ```python >>> glom('a', switch_spec) 'vowel' ``` -------------------------------- ### Constructing Nested Data Structures Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Create a new nested data structure by specifying the output structure and mapping values from the target using a spec. ```python >>> target = {'a': {'b': 'c', 'd': 'e'}, 'f': 'g', 'h': [0, 1, 2]} >>> spec = {'a': 'a.b', 'd': 'a.d', 'h': ('h', [lambda x: x * 2])} >>> output = glom(target, spec) >>> pprint(output) {'a': 'c', 'd': 'e', 'h': [0, 2, 4]} ``` -------------------------------- ### Inspect a Spec with Glom Source: https://github.com/mahmoud/glom/blob/master/docs/debugging.md Use Inspect as a wrapper around a spec to see the evaluation state. This is useful for understanding how glom processes data at a specific point in the specification. ```pycon >>> target = {'a': {'b': {}}} >>> val = glom(target, Inspect('a.b')) # wrapping a spec --- path: ['a.b'] target: {'a': {'b': {}}} output: {} --- ``` -------------------------------- ### Check if Target Matches Pattern Source: https://github.com/mahmoud/glom/blob/master/docs/matching.md Use the matches() method on a Match instance to check if a target value conforms to the defined pattern without raising an error. ```pycon >>> Match(int).matches(-1.0) False ``` -------------------------------- ### Auto-create Missing Structures with assign() Source: https://github.com/mahmoud/glom/blob/master/docs/mutation.md The `assign()` function can automatically create missing dictionary structures along the assignment path by providing a callable to the `missing` parameter. ```pycon >>> target = {} >>> assign(target, 'a.b.c', 'hi', missing=dict) {'a': {'b': {'c': 'hi'}}} ``` -------------------------------- ### Basic Callable Invocation Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Use a single-argument function directly in a glom spec. The function receives the target value. ```pycon >>> glom(['1', '3', '5'], [int]) [1, 3, 5] ``` -------------------------------- ### Deep-get with Recursive Wildcards Source: https://github.com/mahmoud/glom/blob/master/docs/api.md Access values recursively throughout the target structure using double wildcards. ```python >>> target = {'a': [{'k': 'v3'}, {'k': 'v4'}], 'k': 'v0'} >>> glom(target, '**.k') ['v0', 'v3', 'v4'] ```