I'm trying to track how many times a selection was made in a 2D array and how many times it was played in another array (it has to deal with having multiple mp3's in a directory). Not even sure I'm describing it well, but hopefully my example will show my issue.
If you take the code below (and don't have the "PlayHistory.csv" file created), run it, type "A1" you will see two 2D arrays print out. Specifically look at entries for "A1". The Selection Count is 0 and Allowed Selection Count is 1. This is what I'd expect. Now, close the program (or IDE in my case) the "PlayHistory.csv" file is present and notice A1 entry is 0). Run the program again and use "A1" as your input and you'll see both the Selection Count is 1 and the Allowed Selection Count is 1. I'm not even updating the SelectionCount array in the attached code. How is this happening?
#!/usr/bin/env python3
#
import os, sys, csv, vlc, time, serial
from pynput.keyboard import Key, Listener
#
USBDrive = None
Action = None
Playlist = []
SelectionCount = []
AllowedCount = []
Sel_char = None
Queue_counter = 0
Play_counter = 0
PlayHistory="PlayHistory.csv"
#
# Create/Find/Open PlayHistory.csv
# Create media folders as needed if new PlayHistory file is created
#
USBDrive = os.path.join("/media/pi", "USB30FD")
if os.path.isfile(os.path.join(USBDrive, PlayHistory)):
datafile = open(os.path.join(USBDrive, PlayHistory), 'r+')
datareader = csv.reader(datafile, delimiter=',')
for row in datareader:
SelectionCount.append(row)
AllowedCount.append(row)
else:
datafile = open(os.path.join(USBDrive, PlayHistory), 'w+')
datawriter = csv.writer(datafile, delimiter=',')
datawriter.writerow(['Selection', 'Count'])
SelectionCount.append(['Selection', 'Count'])
AllowedCount.append(['Selection', 'Count'])
for x in range (65, 87):
if x == 73 or x == 79:
continue
for y in range (1,11):
if y == 10:
datawriter.writerow([chr(x) + str(0),0])
SelectionCount.append([chr(x) + str(0),0])
AllowedCount.append([chr(x) + str(0),0])
# if not os.path.isdir(os.path.join(USBDrive, chr(x) + str(0))):
# os.makedirs(os.path.join(USBDrive, chr(x) + str(0)))
else:
datawriter.writerow([chr(x) + str(y),0])
SelectionCount.append([chr(x) + str(y),0])
AllowedCount.append([chr(x) + str(y),0])
# if not os.path.isdir(os.path.join(USBDrive, chr(x) + str(y))):
# os.makedirs(os.path.join(USBDrive, chr(x) + str(y)))
datafile.flush()
datafile.close()
#AllowedCount == SelectionCount
#
# Find location of Selection in SelectionCount 2D array
#
def find(l, elem):
for row, i in enumerate(l):
try:
column = i.index(elem)
except ValueError:
continue
return row, column
return -1
#
# MediaPlayer function
#
def QueueTracker(tracker, selection):
global Queue_counter, Play_counter
if tracker == "Queue":
Queue_counter = Queue_counter + 1
elif tracker == "Play":
print("Play Counter")
Play_counter = Play_counter + 1
count1,count2=find(SelectionCount, selection)
#SelectionCount[count1][1] = int(SelectionCount[count1][1]) + 1
print("SelectionCount: " + str(SelectionCount[count1][1]))
elif tracker == "Allowed":
print("Allowed Counter")
acount1,acount2=find(AllowedCount, selection)
print(AllowedCount[acount1][1])
AllowedCount[acount1][1] = int(AllowedCount[acount1][1]) + 1
print("AllowedSelectionCount: " + str(AllowedCount[acount1][1]))
print("Selected Count")
print(SelectionCount)
print("Allowed Selection Count")
print(AllowedCount)
#
# Define keyboard actions
#
def on_press(key):
global Action
try:
Sel_char = int(key.char)
except:
try:
Sel_char = str(key.char)
Sel_char = Sel_char.upper()
except:
Sel_char = None
if Sel_char == "Z":
return False
elif Sel_char == "Y":
print("Play Track")
QueueTracker("Played", "A1")
elif type(Sel_char) == str:
Action = Sel_char
elif type(Sel_char) == int:
Action = Action + str(Sel_char)
QueueTracker("Allowed", "A1")
else:
pass
# Read keyboard input
#
print("Ready...")
with Listener(on_press=on_press) as listener:
listener.join()
#
# Program is shutting down
#
print ("")
print ("Writing Play Counts to PlayHistory file")
datafile = open(os.path.join(USBDrive, PlayHistory), 'w+')
datawriter = csv.writer(datafile, delimiter=',')
datawriter.writerows(SelectionCount)
datafile.flush()
datafile.close()
print ("")
print ("Have a nice day!")
print ("")
sys.exit()
Any help would be greatly appreciated.
Thanks! Brian