question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

improve docs about snake case and camel case

See original GitHub issue

A Marshmallow user I’m supporting writes:

I’m running into an issue with a marshmallow/swagger/javascript interaction. In javascript (and serialized json in general), field names are expected to be camelCase. In python they’re supposed to be snake_case. So to work around this, I need to dump/load in python to do snake_case to camelCase translation.

Here is what we came up with:

from marshmallow import Schema as OriginalSchema, fields

def camel_from_snake(s):
    parts = iter(s.split("_"))
    return next(parts) + "".join(i.title() for i in parts)

class Schema(OriginalSchema):
    """Override marshmallow.Schema for snake_case/camelCase marshalling."""

    def on_bind_field(self, field_name, field_obj):
        # field_obj.data_key = camel_from_snake(field_name)  # TODO: marshmallow v3
        field_obj.load_from = camel_from_snake(field_name)
        field_obj.dump_to = camel_from_snake(field_name)

Searching the docs for “camelCase” currently only gives a single result in the “upgrading” section (in which, to be nitpicky, the example confuses PascalCase with camelCase).

This seems like such a common need that it deserves at least its own dedicated section in the docs, if not actual dedicated API support. Would you be open to a PR addressing this?

Thanks for your work on marshmallow!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
deckar01commented, Jul 9, 2019

https://marshmallow.readthedocs.io/en/3.0/extending.html#pre-processing-and-post-processing-methods

You can make a schema mixin that converts the data to snakecase before loading it and converts it back to camelcase after dumping it.

from marshmallow import Schema, fields, pre_load, post_dump
from stringcase import camelcase, snakecase

class JavaScriptMixin:
    @pre_load
    def to_snakecase(self, data, **kwargs):
        return {snakecase(key): value for key, value in data.items()}
    
    @post_dump
    def to_camelcase(self, data, **kwargs):
        return {camelcase(key): value for key, value in data.items()}

class TestSchema(Schema, JavaScriptMixin):
    foo_bar = fields.String()
    boo_baz = fields.String()

schema = TestSchema()
data = schema.load({'fooBar': 'test', 'booBaz': 'done'})
# {'foo_bar': 'test', 'boo_baz': 'done'}
schema.dump(data)
# {'fooBar': 'test', 'booBaz': 'done'}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Camel case vs. snake case: What's the difference?
The use of white space is what separates camel case and snake case. Camel case has none, while snake case uses underscores.
Read more >
Case Styles: Camel, Pascal, Snake, and Kebab Case
The commonly used strategies for combining words are: camel case, pascal case, snake case, and kebab case. We'll go over those here.
Read more >
Make camelCase variable naming preferred over snake_case
Variables should be named using lowercase, and words should be separated either with uppercase characters (example: $lowerCamelCase) or with an ...
Read more >
Camel Case vs. Snake Case vs. Pascal Case - Khalil Stemmler
The three most common are camel case, pascal case, and snake case. Continue reading for more information on usage best practices.
Read more >
Snake case - Wikipedia
One study has found that readers can recognize snake case values more quickly than camel case. (However, “(...) subjects were trained mainly in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found