Basic use of glom requires only a glance, not a whole tutorial. The
case study below takes a wider look at day-to-day data and object
manipulation, helping you develop an eye for writing robust,
declarative data transformations.

Well that’s no good. We didn’t get our value. We got a TypeError, a
type of error that doesn’t help us at all. The error message doesn’t
even tell us which access failed. If data2 had been passed to us,
we wouldn’t know if 'a', 'b', or 'c' had been set to
None.

We can react to changing data requirements as fast as the data itself can change, naturally restructuring our results,
despite the input’s nested nature. Like a list comprehension, but for nested data, our code mirrors our output.

Most other implementations are limited to a particular data format or pure model, be it jmespath or XPath/XSLT.
glom makes no such sacrifices of practicality, harnessing the full power of Python itself.

Going back to our example, let’s say we wanted to get an aggregate moon count:

glom is a practical tool for production use. To best demonstrate how
you can use it, we’ll be building an API response. We’re implementing
a Contacts web service, like an address book, but backed by an
ORM/database and compatible with web and mobile frontends.

As you can see, the Contact object has fields for primary_email,
defaulting to the first email in the email list, and add_date, to
track the date the contact was added. And as the unique,
autoincrementing id suggests, there appear to be a few other
contacts already in our system.

>>> len(Contact.objects.all())5

Sure enough, we’ve got a little address book going here. But right now
it consists of plain Python objects, not very API friendly:

But at least we know our data, so let’s get to building the API
response with glom.

First, let’s set our source object, conventionally named target:

>>> target=Contact.objects.all()# here we could do filtering, etc.

Next, let’s specify the format of our result. Remember, the processing
is not happening here, this is just declaring the format. We’ll be
going over the specifics of what each line does after we get our
results.

As we can see, our response looks a lot like our glom
specification. This type of WYSIWYG code is one of glom’s most
important features. After we’ve appreciated that simple fact, let’s
look at it line by line.

For id and name, we’re just doing simple copy-overs. For
add_date, we use a tuple to denote repeated gloms; we access
add_date and pass the result to str to convert it to a string.

For emails we need to serialize a list of subobjects. Good news, glom
subgloms just fine, too. We use a tuple to access emails, iterate
over that list, and from each we copy over id and email. Note
how email_type is easily remapped to simply type.

For primary_email we see our first usage of glom’s Coalesce
feature. Much like SQL’s keyword of the same name, Coalesce
returns the result of the first spec that returns a valid value. In
our case, primary_email can be None, so a further access of
primary_email.email would, outside of glom, result in an
AttributeError or TypeError like the one we described before the
Contact example. Inside of a glom Coalesce, exceptions are caught
and we move on to the next spec. glom raises a CoalesceError when
no specs match, so we use default to tell it to return None
instead.

Some Contacts have nicknames or other names they prefer to go by, so
for pref_name, we want to return the stored pref_name, or fall
back to the normal name. Again, we use Coalesce, but this time we
tell it not only to ignore the default GlomError exceptions, but
also ignore empty string values, and finally default to empty string
if all specs result in empty strings or GlomError.

And finally, for our last field, detail, we want to conjure up a
bit of info that’ll help jog the user’s memory. We’re going to include
the location, or company, or year the contact was added. You can see
an example of this feature as implemented by GitHub, here:
https://github.com/mahmoud/glom/stargazers

We’ve seen a crash course in how glom can tame your data and act as a
powerful source of code coherency. glom transforms not only your data,
but also your code, bringing it in line with the data itself.

glom tamed our nested data, avoiding tedious, bug-prone lines,
replacing what would have been large sections with code that was
declarative, but flexible, an ideal balance for maintainability.