there has to be a more pythonic way to do this, ri...
# singer-tap-development
s
there has to be a more pythonic way to do this, right?
Copy code
def get_url_params(self, context=None, ...):
    params: dict = {}
    if context:
        if context.get("channel_id"):
            params["channel"] = context["channel_id"]
    return params
v
Copy code
def get_url_params(self, context=None, ...):
    params: dict = {}
    if context:
        params["channel"] = context.get("channel_id", None)
    return params
Makes it a little better
e
context.get("channel_id", None)
is a bit redundant,
context.get("channel_id")
already defaults to
None
v
Even better 😄
a
@stephen_bailey - If you only want to set 'channel' if context has a "channel_id", you can do both of these checks in the same line:
Copy code
def get_url_params(self, context=None, ...):
    params: dict = {}
    if context and "channel_id" in context:
        params["channel"] = context["channel_id"]
    return params
s
@aaronsteers 🙌 🙌 🙌 that's exactly what i was hoping was possible AND i did not know
"key" in my_dict
was a valid thing 🤯
a
Yeah, the "in" operator in Python is kina magical, I think partywizard
s
yeah, two things blowing my mind here: 1) that dicts are iterable like that, and 2) that the chained
if x and y
is conditional and only evaluates the second thing if the first is true. i was thinking that
"channel_id" in context
would throw an error if context was not a dict
v
1. I'm pretty sure under the hood it's a HashSet so it's an O(1) lookup! Like a .contains in Java land for a set 2. Short Circuit Evaluation 😄 https://en.wikipedia.org/wiki/Short-circuit_evaluation The table is interesting to read, haven't thought about this in some time. Fun stuff
a
Gotta love short-circuit evaluation. 🙂