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
  • 3
    You could collect all those variables in a `dict`. – tobias_k Oct 16 '17 at 13:23
  • What would the keys be? –  Oct 16 '17 at 13:24
  • 1
    This seems like a reasonable approach as is; what's the issue with it? – deceze Oct 16 '17 at 13:25
  • I'm running the same code in four lines which I could do in a simple for loop. –  Oct 16 '17 at 13:25
  • You don't need that list. You can loop over itertools.chan([func1(), func2(), func3()]) – Vinayak Kaniyarakkal Oct 16 '17 at 13:25
  • @VinayakKaniyarakkal Do you mind writing an answer explaining that solution? I'm a little confused on how to do it. –  Oct 16 '17 at 13:28
  • I updated my question. –  Oct 16 '17 at 13:35
  • @dirtysocks45 what exactly is that function doing on the dataframe? Stuff that's accessing a dataframe 4 times that differs only between what appears to be a "direction" smells a bit off... – Jon Clements Oct 16 '17 at 13:41
  • It is fine as it is, other ways will make it more complicated imo. As @tobias_k said, you can wrap it into a dictionary, or a pandas.Series. --- You might want to take out the `read_csv` and put it into a separated data layer, but that might be overkill for now. – Elmex80s Oct 16 '17 at 13:50

2 Answers2

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)])
galaxyan
  • 5,944
  • 2
  • 19
  • 43
  • 1
    Another way to flatten the results without reduce https://stackoverflow.com/questions/3204245/how-do-i-convert-a-tuple-of-tuples-to-a-one-dimensional-list-using-list-comprehe – Reti43 Oct 16 '17 at 13:47
  • Two antipatterns here: don't use `reduce` to reinvent `sum` and don't use `sum` to flatten lists. – juanpa.arrivillaga Oct 16 '17 at 14:50
-1
itertools.chain([fill_gyro_and_acc_data(i, merge) for i in [
    nod_intervals, swipe_right_intervals, swipe_left_intervals, whats_up_intervals
])
Vinayak Kaniyarakkal
  • 1,110
  • 17
  • 23
  • @dirtysocks45: Is it better now? – Vinayak Kaniyarakkal Oct 16 '17 at 13:37
  • Will that work with the logic I have currently in my code? My fill_gyro_and_acc_data returns two variables. –  Oct 16 '17 at 13:39
  • @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 – Vinayak Kaniyarakkal Oct 16 '17 at 13:42
  • so how do I set my local variables like `nod_gyro` to the values returned by this command? –  Oct 16 '17 at 16:02
  • nod_gyro, nod_acc, swipe_left_gyro, swipe_right_acc, etc = itertools.chain([...]) – Vinayak Kaniyarakkal Oct 16 '17 at 16:50
  • I get the error: `TypeError: 'itertools.chain' object has no attribute '__getitem__'` when I try `nod_gest.append(generate_gesture_files(i)[0])` –  Oct 16 '17 at 16:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156874/discussion-between-vinayak-kaniyarakkal-and-dirtysocks45). – Vinayak Kaniyarakkal Oct 17 '17 at 05:11