Is it possible to turn the access_logs block on and off via the environment_name variable?

One way to achieve this with TF 0.12 onwards is to use dynamic blocks: dynamic “access_logs” { for_each = var.environment_name == “production” ? [var.environment_name] : [] content { bucket = “my-bucket” prefix = “${var.environment_name}-alb” } } This will create one or zero access_logs blocks depending on the value of var.environment_name.

SQL conditional SELECT

In SQL, you do it this way: SELECT CASE WHEN @selectField1 = 1 THEN Field1 ELSE NULL END, CASE WHEN @selectField2 = 1 THEN Field2 ELSE NULL END FROM Table Relational model does not imply dynamic field count. Instead, if you are not interested in a field value, you just select a NULL instead and … Read more

What is the syntactical equivalent to switch/case in Python? [duplicate]

TL;DR As of Python 3.10.0 (alpha6 released March 30, 2021), Python has an official syntactical equivalent called match. The basic syntax is: match value: case condition: action(s) … For older Python versions, there are only workarounds if you don’t want to resort to if–elif–else. See this excellent community post for a collection of some. Example … Read more

How do I assign values based on multiple conditions for existing columns?

numpy.select This is a perfect case for np.select where we can create a column based on multiple conditions and it’s a readable method when there are more conditions: conditions = [ df[‘gender’].eq(‘male’) & df[‘pet1’].eq(df[‘pet2’]), df[‘gender’].eq(‘female’) & df[‘pet1’].isin([‘cat’, ‘dog’]) ] choices = [5,5] df[‘points’] = np.select(conditions, choices, default=0) print(df) gender pet1 pet2 points 0 male dog … Read more

How to conditionally add widgets to a list?

EDIT: Since Dart 2.2, new syntaxes supports this natively: Column( children: [ if (foo != null) Text(foo), Bar(), ], ); This problem is currently debated on github here. But for now, you can use dart sync* functions: Row( children: toList(() sync* { if (foo == 42) { yield Text(“foo”); } }), ); where toList is: … Read more

How can I check the existence of attributes and tags in XML before parsing?

If a tag doesn’t exist, .find() indeed returns None. Simply test for that value: for event in root.findall(‘event’): party = event.find(‘party’) if party is None: continue parties = party.text children = event.get(‘value’) You already use .get() on event to test for the value the attribute; it returns None as well if the attribute does not … Read more

Pandas add column with value based on condition based on other columns

Use the timeits, Luke! Conclusion List comprehensions perform the best on smaller amounts of data because they incur very little overhead, even though they are not vectorized. OTOH, on larger data, loc and numpy.where perform better – vectorisation wins the day. Keep in mind that the applicability of a method depends on your data, the … Read more