Skip to content

Instantly share code, notes, and snippets.

@arbennett
Created December 9, 2022 00:13
Show Gist options
  • Save arbennett/4c5930d5ebd77a5faeb2969d9149c534 to your computer and use it in GitHub Desktop.
Save arbennett/4c5930d5ebd77a5faeb2969d9149c534 to your computer and use it in GitHub Desktop.
def set_flight_and_condition(name, flight):
if name[-2:] == 'L1':
first_flight = True
condition = flight[0] == flight[-1]
elif name[-2:] == 'L2':
first_flight = False
condition = flight[0] == flight[-2]
return first_flight, condition
#%%
# Using a dictionary to initialize many variable combinations
# saves a lot of typing and boilerplate code. Plus it's much
# easier to add a new category into later if you need to
# (or remove one)
v1 = ['dgscat']
v2 = ['norm', 'vertmean']
v3 = ['coupled', 'decoupled', 'other']
v4 = ['winter', 'spring']
category_dict = {}
for vv1 in v1:
for vv2 in v2:
for vv3 in v3:
for vv4 in v4:
category_dict[f'{vv1}_{vv2}_{vv3}_{vv4}'] = []
print(category_dict)
#%%
# Numpy has tons of built in functionality that is usually
# faster, more reliable, and plays well with arrays (meaning
# less loops for you to write manually).
#
# Here I'm finding the closest value in a list for every
# element of another.
import numpy as np
l1 = np.array([3, 5, 7, 14, 25])
l2 = np.array([4, 9, 22, 27])
for l in l2:
idx = np.argmin(np.abs(l1 - l))
closest = l1[idx]
print(f'Closest value to {l} is at {idx} and has val {closest}')
# %%
# You can write code in multiple files! Putting all of your
# really messy data handling/filtering/etc code into a bunch
# of functions and then saving them in a separate file (what
# python calls a module) will let you import it into your
# main python code so that you see what you are logically
# trying to do rather than all of the nitty-gritty details
# of how you actually accomplish it.
#
# Here the key is whatever you name your python file "name.py"
# then you will just import "name". Additionally, you will need
# to make sure python can find your extra code. The easiest
# way to make sure of this is to just put both files side by side
# in the same folder.
from functions import set_flight_and_condition
all_names = [
'blah_L1',
'blah_L2'
]
all_flights = [
[1, 2, 3],
[2, 2, 2],
]
for i in range(len(all_names)):
name = all_names[i]
flight = all_flights[i]
first_flight, condition = set_flight_and_condition(name, flight)
print(name, flight)
print(first_flight, condition)
print('code block out here')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment