2

I'm trying to make my code cleaner.

I want to do something like:

gesture_sensor_data = [nod_gyro, nod_acc, swipe_left_gyro, swipe_right_acc, etc.]

I have this right now:

nod_gyro, nod_acc = fill_gyro_and_acc_data(nod_intervals, merge)
swipe_right_gyro, swipe_right_acc = fill_gyro_and_acc_data(swipe_right_intervals, merge)
swipe_left_gyro, swipe_left_acc = fill_gyro_and_acc_data(swipe_left_intervals, merge)
whats_up_gyro, whats_up_acc = fill_gyro_and_acc_data(whats_up_intervals, merge)

I want to run a loop through the gesture_sensor_data.

Is there a way to do this? Some kind of structure or something?

EDIT: I'll just show my full code in this function for context.

def generate_gesture_files(i):
    nod_intervals, swipe_left_intervals, swipe_right_intervals, whats_up_intervals = generate_gesture_intervals(i)

    merge = pandas.read_csv(final_user_study_path + "/P" + str(i) + "/DataCollection/data/merge.csv")
    nod_gyro, nod_acc = fill_gyro_and_acc_data(nod_intervals, merge)
    swipe_right_gyro, swipe_right_acc = fill_gyro_and_acc_data(swipe_right_intervals, merge)
    swipe_left_gyro, swipe_left_acc = fill_gyro_and_acc_data(swipe_left_intervals, merge)
    whats_up_gyro, whats_up_acc = fill_gyro_and_acc_data(whats_up_intervals, merge)
    return nod_gyro, nod_acc, swipe_right_gyro, swipe_right_acc, swipe_left_gyro, swipe_right_acc, whats_up_gyro, whats_up_acc
9
  • 3
    You could collect all those variables in a dict. Commented Oct 16, 2017 at 13:23
  • What would the keys be? Commented Oct 16, 2017 at 13:24
  • 1
    This seems like a reasonable approach as is; what's the issue with it? Commented Oct 16, 2017 at 13:25
  • I'm running the same code in four lines which I could do in a simple for loop. Commented Oct 16, 2017 at 13:25
  • You don't need that list. You can loop over itertools.chan([func1(), func2(), func3()]) Commented Oct 16, 2017 at 13:25

2 Answers 2

0

you could change your generate_gesture_intervalsand use partial

def generate_gesture_files(i):
  return reduce(lambda x,y:x+y, [fill_gyro_and_acc_data(arg, merge) for arg in generate_gesture_intervals(i)])
Sign up to request clarification or add additional context in comments.

2 Comments

Another way to flatten the results without reduce stackoverflow.com/questions/3204245/…
Two antipatterns here: don't use reduce to reinvent sum and don't use sum to flatten lists.
-1
itertools.chain([fill_gyro_and_acc_data(i, merge) for i in [
    nod_intervals, swipe_right_intervals, swipe_left_intervals, whats_up_intervals
])

7 Comments

@dirtysocks45: Is it better now?
Will that work with the logic I have currently in my code? My fill_gyro_and_acc_data returns two variables.
@dirtysocks45: It doesn't return 2 variables. It returns an iterable of length 2. You are unpacking it. A function always returns only one object. We are merging multiple iterables using itertools.chain
so how do I set my local variables like nod_gyro to the values returned by this command?
nod_gyro, nod_acc, swipe_left_gyro, swipe_right_acc, etc = itertools.chain([...])
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.