Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project.get(): Accept non-list args (strings) #2853

Merged
merged 3 commits into from
Jul 19, 2019

Conversation

ferdnyc
Copy link
Contributor

@ferdnyc ferdnyc commented Jul 8, 2019

This is another entry in my award-ineligible series, "Don't make programmers write awkward code" (see OpenShot/libopenshot#266). The target today is project.get() in classes.project_data.

Don't make programmers write awkward code

All over the source tree, there are requests for project data like this:

# Get some settings from the project
fps = project.get(["fps"])
width = project.get(["width"])
height = project.get(["height"])
sample_rate = project.get(["sample_rate"])
channels = project.get(["channels"])
channel_layout = project.get(["channel_layout"])

Unlike any other lookup method, ProjectDataStore.get() requires that it be passed a list, or it will fail with a warning:

# Verify key is valid type
if not isinstance(key, list):
log.warning("get() key must be a list. key: {}".format(key))
return None

But that just makes the calling code awkward. So, with this change, a non-empty, non-list arg is simply promoted to a single-item list by the code, instead of by the coder:

        if not key:
            log.warning("Cannot get empty key.")
            return None
        if not isinstance(key, list):
            key = [key]

That allows project.get("fps"), project.get("width"), etc. to do what's expected, and makes the code easier to write and to read.

ferdnyc added 2 commits July 8, 2019 09:26
previously project.get() required its argument to be a list, or it
would bail with a warning. This created lots of awkward code like:
`get_app().project.get(["layers"])`. Now, if a non-empty, non-list
arg is passed in, it simply promotes it to a list and continues,
making `get_app().project.get("layers")` valid and equivalent.
@ferdnyc
Copy link
Contributor Author

ferdnyc commented Jul 10, 2019

The BEST part of this, the great irony of project.get() requiring that its argument be a list? The whole reason that was done was so that sub-objects could be looked up by path, using the list arg. So, project.get(["fps", "num"]), for example. And, that functionality is made limited use of, by things like the filter() methods in the QueryObject classes.

But project.get() is never, never-EVER called that way, not even once, in the actual code. Not even where it COULD be! Even where sub-keys are looked up, the code doesn't query them using a list. Instead?

fps_num = get_app().project.get(["fps"]).get("num", 24)
fps_den = get_app().project.get(["fps"]).get("den", 1)

# Get FPS from project
fps = get_app().project.get(["fps"])
fps_num = float(fps["num"])
fps_den = float(fps["den"])

If anyone was ever curious how you can tell when your API isn't quite in sync with the needs of its consumers, that's how.

@DylanC
Copy link
Collaborator

DylanC commented Jul 18, 2019

@ferdnyc - Simple but effective. I like this change.

@ferdnyc
Copy link
Contributor Author

ferdnyc commented Jul 19, 2019

@DylanC Thanks! I find the whole idea of the "series" pretty entertaining, actually. I can imagine it really taking off, right down to an awareness campaign featuring awkward novelty t-shirt designs. 😉

This code was what initially inspired it, actually. I ended up tackling libopenshot's logging calls first only because the code they forced was so much more awkward and ugly. This is admittedly minor by comparison. We get to type a whole 2 less characters per call, woo.

@DylanC
Copy link
Collaborator

DylanC commented Jul 19, 2019

@ferdnyc

This is admittedly minor by comparison. We get to type a whole 2 less characters per call, woo.

Any time saving is better than no time saving. 😄

@DylanC DylanC merged commit e7320fd into OpenShot:develop Jul 19, 2019
@ferdnyc ferdnyc deleted the awkward-gets branch July 19, 2019 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants