Python - Files wont download - python

Here's My code, all the urls are in a Config Parser format file. When the button is pressed files will not download. What did go wrong? I used urllib should I have used urllib2? Some of the functions may be there but not used just ignore that.
import wx
import ConfigParser
import urllib
def Download(url):
response = urllib.urlopen(url).read()
doned = wx.MessageDialog("Download Done")
doned.ShowModal()
doned.Destroy()
#First thing i gona do is Parse 98 box data
BoxParser = ConfigParser.RawConfigParser() #Set Raw
BoxParser.read("98.box") #Mount into 98.box
#Get Vars, and print them
WxTitle = BoxParser.get("meta_data","Window_title") #get the window title
Application = BoxParser.get("meta_data","Application") #Get app name
Desc = BoxParser.get("meta_data","Description") #Get the description of the app
Author = BoxParser.get("meta_data","Author") #Of course! I dont wanna be plagurized
Contact = BoxParser.get("meta_data","Contact_Info") #My Contact Info
Date = BoxParser.get("meta_data","Date") #Date when the current update was made
#UpdateUrl = BoxParser.get("meta_data","Update_url") #Url to update
#BoxUp = BoxParser.get("meta_data","Update_box") #Url to update 98.box
# Meta Data loaded
#time to load the firmwares
e660 = BoxParser.get("Firmware_links","660") #6.60
e6602 = False
e660g = BoxParser.get("Firmware_links","660go") #6.60 Go Eboot
e6602g = False
e639 = BoxParser.get("Firmware_links","639") #6.39
e6392 = False
e639g = BoxParser.get("Firmware_links","639go") #6.39 Go Eboot
e6392g = False
e635 = BoxParser.get("Firmware_links","635") #6.35
e6352 = False
e635g = BoxParser.get("Firmware_links","635go") #6.35 Go Eboot
e6352g = False
e620 = BoxParser.get("Firmware_links","620") #6.20
e550 = BoxParser.get("Firmware_links","550") #5.50
e5502 = False
e500 = BoxParser.get("Firmware_links","500") #5.00
e5002 = False
e401 = BoxParser.get("Firmware_links","401") #4.01
e4012 = False
#Firmwares Loaded
def BoxUpdate():
Download(Update_box)
#Check if DD equ true so we can post the MSG
if downloaddone == True:
Done2 = wx.MessageDialog(self,"Download Done, 98.Box Updated!")
Done2.ShowModal()
Done.Destroy()
#Time to get out Gui
class FrameClass(wx.Frame): #Finally making the gui!
def __init__(self,parent,title): #making init!
app = wx.Frame
app.__init__(self,parent,title=WxTitle,size = (340,280)) #set window size
Menu = wx.Menu() #Lets make a menu!
panel = wx.Panel(self) #set the panel var
contact = Menu.Append(wx.ID_NONE,"&Contact Info") #add update thing
self.Bind(wx.EVT_MENU,self.contact1,contact) #Add event for Update
fwMsg = wx.StaticText(panel, label='Firmware', pos=(59,25))
fwlist = wx.ComboBox(panel,pos=(118,22), choices=["6.60","6.60 Go/N1000","6.39","6.39 Go/N1000","6.35 Go/N1000","5.50","5.00","4.01"])
self.Bind(wx.EVT_COMBOBOX, self.getsel, fwlist)
downloadbutton = wx.Button(panel, label="Download FW", pos=(100,76))
self.Bind(wx.EVT_BUTTON, self.DLB, downloadbutton)
#now for the member!
TopM = wx.MenuBar()
TopM.Append(Menu, "Tool Opt")
self.SetMenuBar(TopM)
self.Show(True)
def DLUpdate(self,e):
#Check if DD equ true so we can post the MSG
Download(Update_url)
print "downloading"
Done = wx.MessageDialog(self,"Download Done, download stored in \"DLBOXV$.zip\" file")
Done.ShowModal()
Done.Destroy()
def contact1(self,e):
con = wx.MessageDialog(self,Contact)
con.ShowModal()
con.Destroy()
def getsel(self,e):
i = e.GetString()
if i == "6.60":
e6602 = True
print e6602,"660"
else:
e6602 = False
print e6602,"660"
if i == "6.60 Go/N1000":
e6602g = True
print e6602g,"660 go"
else:
e6602g = False
print e6602g,"660 go"
if i == "6.39":
e6392 = True
print e6392,"639"
else:
e6392 = False
print e6392,"639"
if i == "6.39 Go/N1000":
e6392g = True
print e6392g,"639 go"
else:
e6392g = False
print e6392g,"639 go"
if i == "6.35 Go/N1000":
e6352g = False
print e6352g,"635 go"
else:
e6352g = False
print e6352g,"635 go"
if i == "5.50":
e5502 = True
print e5502,"550"
else:
e5502 = False
print e5502,"550"
if i == "500":
e5002 = True
print e5002,"500"
else:
e5002 = False
print e5002,"500"
if i == "401":
e4012 = True
print e4012,"401"
else:
e4012 = False
print e4012,"401"
def DLB(self,e):
if e6602 == True:
Download(e660)
elif e6602g == True:
Download(e660g)
elif e6392 == True:
Download(e639)
elif e639g == True:
Download(e639g)
elif e6352g == True:
Download(e635g)
elif e5502 == True:
Download(e550)
elif e5002 == True:
Download(e500)
elif e4012 == True:
Download(e401)
G = wx.App(False)
Win = FrameClass(None,WxTitle)
G.MainLoop()
But at the function Download(url) will not function, it will not download
def Download(url):
response = urllib.urlopen(url).read()
doned = wx.MessageDialog("Download Done")
doned.ShowModal()
doned.Destroy()
what triggers Download(url) is a few if and elsif statements
def getsel(self,e):
i = e.GetString()
if i == "6.60":
e6602 = True
print e6602,"660"
else:
e6602 = False
print e6602,"660"
if i == "6.60 Go/N1000":
e6602g = True
print e6602g,"660 go"
else:
e6602g = False
print e6602g,"660 go"
if i == "6.39":
e6392 = True
print e6392,"639"
else:
e6392 = False
print e6392,"639"
if i == "6.39 Go/N1000":
e6392g = True
print e6392g,"639 go"
else:
e6392g = False
print e6392g,"639 go"
if i == "6.35 Go/N1000":
e6352g = False
print e6352g,"635 go"
else:
e6352g = False
print e6352g,"635 go"
if i == "5.50":
e5502 = True
print e5502,"550"
else:
e5502 = False
print e5502,"550"
if i == "500":
e5002 = True
print e5002,"500"
else:
e5002 = False
print e5002,"500"
if i == "401":
e4012 = True
print e4012,"401"
else:
e4012 = False
print e4012,"401"
def DLB(self,e):
if e6602 == True:
Download(e660)
elif e6602g == True:
Download(e660g)
elif e6392 == True:
Download(e639)
elif e639g == True:
Download(e639g)
elif e6352g == True:
Download(e635g)
elif e5502 == True:
Download(e550)
elif e5002 == True:
Download(e500)
elif e4012 == True:
Download(e401)

What did go wrong? I used urllib should I have used urllib2?
Yes. The fact that just adding the 2 to your code fixes the problem is obviously proof of that, but it doesn't explain much.
As the docs for urllib.urlopen say:
Deprecated since version 2.6: The urlopen() function has been removed in Python 3 in favor of urllib2.urlopen().
This doesn't mean that they stopped fixing bugs in urllib as of 2.6 (there was a bug fix as late as 2.7.9), but it does mean that missing functionality will never be added. That includes, among other things, some kinds of HTTP authentication, redirections, HTTPS with non-standard CAs, proxy settings, and proxy authentication.
Without knowing anything at all about the problem besides "the download doesn't happen", or what URL you're trying to download (you seem to be passing in a variable named Update_box that isn't assigned anywhere), or the setup you're running on, it's impossible to know exactly which one of these problems (or any others) is the key.

Related

Python trying to run one Async function and one infinite loop simultaneously

I am trying to monitor a stream of salesforce events to play a ringtone each time a case comes in based on case origin. Initially I was querying the API from a thread and I was able to have the UI run in the main function and the monitoring run on a thread in the background. Now that I have switched to the streaming API I need to use async to monitor for responses when a case is assigned. This has caused issues where if I start the async loop first my UI never starts but if I start the UI first my async loop never starts.... I have attached my code below:
Here is my first python script where I actually define the async function and call my user interface.
import asyncio
from aiosfstream import SalesforceStreamingClient
from win10toast import ToastNotifier
import Ringtone
from threading import Thread
import vlc
def notifier(type, system):
n = ToastNotifier()
n.show_toast("SalesForce Notification Manager", "Incoming " + type + " From" + system, duration=10,
icon_path="ICON.ico")
async def subscribeToSalesforce():
async with SalesforceStreamingClient(
# Connect to Salesforce streaming API
consumer_key='<key>',
consumer_secret='<secret>',
username='<username>',
password='<password>',
sandbox=True) as client:
# Subscribe to one or more topics
apiLink = Ringtone.login()
currentUserID = Ringtone.getUserID(apiLink)
await client.subscribe("/event/NewEmail__e")
# listen for incoming messages
async for message in client:
topic = message["channel"]
data = message["data"]
print(f"{topic}: {data}")
data = data['payload']
try:
print(data['Assigned__c'])
if data['Assigned__c']:
if data['Assigned__c'] == 'Assigned':
if data['OwnerID__c'] == currentUserID:
print(data)
findType(data)
except:
print('Exception')
def findType(data):
with open('Ref/Files/phone.txt') as f:
phone = f.readline()
f.close()
with open('Ref/Files/email.txt') as f:
email = f.readline()
f.close()
with open('Ref/Files/chat.txt') as f:
chat = f.readline()
f.close()
with open('Ref/Files/Volume.txt') as f:
volume = f.readline()
f.close()
with open('Ref/Files/notifs.txt') as f:
notifsRaw = f.readline()
f.close()
notifs = True
if data['Assigned__c']:
if data['Assigned__c'] == 'Assigned':
if data['VM_Offline_Chat__c'] == True:
if notifs:
t2 = Thread(target=notifier, args=("Voicemail/Offline Chat", data['Program__c']))
t2.start()
p = vlc.MediaPlayer()
p.audio_set_volume(int(volume))
p.play()
elif data['Case_Type__c'] == 'Phone':
if notifs:
t3 = Thread(target=notifier, args=("Call", data['Program__c']))
t3.start()
p = vlc.MediaPlayer(phone)
p.audio_set_volume(int(volume))
p.play()
elif data['Case_Type__c'] == 'Email':
if notifs:
t4 = Thread(target=notifier, args=("Email", data['Program__c']))
t4.start()
p = vlc.MediaPlayer(email)
p.audio_set_volume(int(volume))
p.play()
elif data['Case_Type__c'] == 'Chat':
if notifs:
t5 = Thread(target=notifier, args=("Chat", data['Program__c']))
t5.start()
p = vlc.MediaPlayer(chat)
p.audio_set_volume(int(volume))
p.play()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(subscribeToSalesforce())
t1 = Thread(target=Ringtone.userInterface(), args=[])
t1.start()
Here is my other python file where the user interface function is stored:
import PySimpleGUI as sg
import time
import vlc
from simple_salesforce import Salesforce
import distutils.util
_VARS = {'window': False,
'fig_agg': False,
'pltFig': False}
ttk_style = 'Clam'
sg.theme('DarkGrey5')
with open('Ref/Files/Volume.txt') as f:
sliderVal = f.readline()
f.close()
with open('Ref/Files/chatchosen.txt') as f:
chatchosen = f.readline()
f.close()
with open('Ref/Files/emailchosen.txt') as f:
emailchosen = f.readline()
f.close()
with open('Ref/Files/phonechosen.txt') as f:
phonechosen = f.readline()
f.close()
with open('Ref/Files/notifs.txt') as f:
notifs = f.readline()
f.close()
notifs = bool(distutils.util.strtobool(notifs))
command = ['Test Ringtone']
cmd_layout = [[sg.Button(cmd, use_ttk_buttons=True, size=(10, 1))] for cmd in command]
ringtones = ['Classical Piano', 'Mozart', 'Game of Thrones', 'Harry Potter', 'Mandalorian', 'Star Wars', 'Play That Funky Music', 'You Are My Sunshine', 'iPhone', 'Blackberry', 'Minimal 1', 'Minimal 2', 'Minimal 3']
def open_window(name):
Title = 'Enter Salesforce Username:'
Window = 'Username'
LayoutItems = [
[sg.Text(Title)],
[sg.Input(name, key='edit')],
[sg.Button('Okay', use_ttk_buttons=True), sg.Button('Cancel', use_ttk_buttons=True)]
]
window = sg.Window(Window, LayoutItems, resizable=True, modal=True)
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
elif event == "Cancel":
window.close()
return 'Cancel'
elif event == "Okay":
if values['edit'] != '':
window.close()
return values['edit']
else:
window.close()
return values['edit']
def open_password(name, num):
if num == 4:
Title = 'Enter Salesforce Password:'
Window = 'Password'
elif num == 5:
Title = 'Enter Salesforce Security Token:'
Window = 'Token'
LayoutItems = [
[sg.Text(Title)],
[sg.Input(name, key='edit', password_char='*')],
[sg.Button('Okay', use_ttk_buttons=True), sg.Button('Cancel', use_ttk_buttons=True)]
]
window = sg.Window(Window, LayoutItems, resizable=True, modal=True)
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
elif event == "Cancel":
window.close()
return 'Cancel'
elif event == "Okay":
if values['edit'] != '':
window.close()
return values['edit']
else:
window.close()
return values['edit']
def login():
with open('Ref/Files/Info1.txt') as f:
usernameold = f.readline()
with open('Ref/Files/Info2.txt') as f:
passwordold = f.readline()
with open('Ref/Files/Info3.txt') as f:
tokenold = f.readline()
username = open_window(usernameold)
password = open_password(passwordold, 4)
token = open_password(tokenold, 5)
with open('Ref/Files/Info1.txt', 'w') as f:
f.write(username)
with open('Ref/Files/Info2.txt', 'w') as f:
f.write(password)
with open('Ref/Files/Info3.txt', 'w') as f:
f.write(token)
try:
sf = Salesforce(username=username, password=password, security_token=token, domain='test')
except:
sg.popup('Incorrect Username/Password Combination')
return sf
def getUserID(apiLink):
identity_url = API.restful('')['identity']
currentUser = API.User.get(identity_url[-18:])
currentUserID = currentUser['Id']
return currentUserID
employee_list_column = [
[sg.Image('K2.png', size=(280, 100))],
[sg.Listbox(values=ringtones, enable_events=True, size=(40, 20), key="RingtoneList"), sg.Column(cmd_layout)],
[sg.Checkbox('Enable Windows Notifications', default=notifs, key="notifsBox")],
[sg.Button('Save Phone Ringtone Selection', use_ttk_buttons=True, key='PhoneRingtone'), sg.Text(phonechosen, key= 'PhoneChosen')],
[sg.Button('Save Email Ringtone Selection', use_ttk_buttons=True, key='EmailRingtone'), sg.Text(emailchosen, key= 'EmailChosen')],
[sg.Button('Save Chat Ringtone Selection', use_ttk_buttons=True, key='ChatRingtone'), sg.Text(chatchosen, key= 'ChatChosen')],
[sg.Slider(orientation ='horizontal', key='Volume', range=(20,100), default_value= int(sliderVal), enable_events=True)]
]
layout = [
[
sg.Column(employee_list_column)
]
]
def chooseRingtones(chosen):
if chosen == 'Classical Piano':
val = '/Ref/Audio/ClassicalPiano.mp3'
elif chosen == 'Mozart':
val = '/Ref/Audio/Mozart.mp3'
elif chosen == 'Mandalorian':
val = '/Ref/Audio/mandalorian_call.mp3'
elif chosen == 'Game of Thrones':
val = '/Ref/Audio/GOT.mp3'
elif chosen == 'Harry Potter':
val = '/Ref/Audio/HarryPotter.mp3'
elif chosen == 'Star Wars':
val = '/Ref/Audio/Imperial.mp3'
elif chosen == 'Play That Funky Music':
val = '/Ref/Audio/PlayThatFunky.mp3'
elif chosen == 'You Are My Sunshine':
val = '/Ref/Audio/YouAreMySunshine.mp3'
elif chosen == 'ival':
val = '/Ref/Audio/ival.mp3'
elif chosen == 'Minimal 1':
val = '/Ref/Audio/Normal1.mp3'
elif chosen == 'Minimal 2':
val = '/Ref/Audio/Normal2.mp3'
elif chosen == 'Minimal 3':
val = '/Ref/Audio/Normal3.mp3'
return val
def userInterface():
start_time = time.time() - 5
window = sg.Window("Salesforce Notification Manager", layout, resizable=True)
while True:
event, values = window.read()
with open('Ref/Files/Volume.txt') as f:
volume = f.readline()
f.close()
if event == "Exit" or event == sg.WIN_CLOSED:
programClose = True
break
elif event == 'Test Ringtone':
name = values['RingtoneList']
if name:
if start_time + 1 <= time.time():
start_time = time.time()
chosen = name[0]
if chosen == 'Classical Piano':
p = vlc.MediaPlayer("/Ref/Audio/ClassicalPiano.mp3")
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Mozart':
p = vlc.MediaPlayer("/Ref/Audio/Mozart.mp3")
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Mandalorian':
p = vlc.MediaPlayer('/Ref/Audio/mandalorian_call.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Game of Thrones':
p = vlc.MediaPlayer('/Ref/Audio/GOT.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Harry Potter':
p = vlc.MediaPlayer('/Ref/Audio/HarryPotter.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Star Wars':
p = vlc.MediaPlayer('/Ref/Audio/Imperial.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Play That Funky Music':
p = vlc.MediaPlayer('/Ref/Audio/PlayThatFunky.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'You Are My Sunshine':
p = vlc.MediaPlayer('/Ref/Audio/YouAreMySunshine.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'iPhone':
p = vlc.MediaPlayer('/Ref/Audio/iPhone.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Blackberry':
p = vlc.MediaPlayer('/Ref/Audio/Blackberry.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Minimal 1':
p = vlc.MediaPlayer('/Ref/Audio/Normal1.mp3')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Minimal 2':
p = vlc.MediaPlayer('/Ref/Audio/Normal2.m4a')
p.audio_set_volume(int(volume))
p.play()
elif chosen == 'Minimal 3':
p = vlc.MediaPlayer('/Ref/Audio/Normal3.mp3')
p.audio_set_volume(int(volume))
p.play()
else:
sg.Popup('No Ringtone Selected')
elif event == 'PhoneRingtone':
name = values['RingtoneList']
if name:
chosen = name[0]
chosen1 = "Phone Ringtone Chosen: " + chosen
window['PhoneChosen'].update(value=chosen1)
phone = chooseRingtones(chosen)
with open('Ref/Files/phonechosen.txt', 'w') as f:
f.write(str(chosen1))
f.close()
with open('Ref/Files/phone.txt', 'w') as f:
f.write(phone)
f.close()
else:
sg.Popup('No Ringtone Selected')
elif event == 'ChatRingtone':
name = values['RingtoneList']
if name:
chosen = name[0]
chosen1 = " Chat Ringtone Chosen: " + chosen
window['ChatChosen'].update(value=chosen1)
chat = chooseRingtones(chosen)
with open('Ref/Files/emailchosen.txt', 'w') as f:
f.write(str(chosen1))
f.close()
with open('Ref/Files/chat.txt', 'w') as f:
f.write(chat)
f.close()
else:
sg.Popup('No Ringtone Selected')
elif event == 'EmailRingtone':
name = values['RingtoneList']
if name:
chosen = name[0]
chosen1 = "Email Ringtone Chosen: " + chosen
window['EmailChosen'].update(value=chosen1)
email = chooseRingtones(chosen)
with open('Ref/Files/chatchosen.txt', 'w') as f:
f.write(str(chosen1))
f.close()
with open('Ref/Files/email.txt', 'w') as f:
f.write(email)
f.close()
else:
sg.popup('No ringtone is selected')
elif event == 'Volume':
slider = window[event]
volume = int(slider.TKScale.get())
with open('Ref/Files/Volume.txt', 'w') as f:
f.write(str(volume))
f.close()
elif event == 'notifsBox':
notifs = values["notifsBox"]
with open('Ref/Files/notifs.txt', 'w') as f:
f.write(str(notifs))
f.close()
Sorry if my code is sloppy. I'm new to all of this and the async stuff specifically is very confusing for me.

How to have a if statement which is nested within a while statement only complete its function once

I'm trying to have my code only complete its process once. (If res = on only do it once but when it becomes off it detects that and does the off data encode once.) Here is my current code and my function onoff if needed as well
def onoff():
result = firebase.get('/Data', 'Condition')
return result
while True:
res = onoff()
data = res+","
if res == 'on':
ser.write(data.encode())
print(data)
time.sleep(1)
elif ser.write(data.encode()):
print(data)
time.sleep(1)```
def onoff():
result = firebase.get('/Data', 'Condition')
return result
switch = True
while True:
res = onoff()
data = res+","
if res == 'on' and switch:
ser.write(data.encode())
print(data)
switch = False
time.sleep(1)
elif ser.write(data.encode()):
print(data)
time.sleep(1)```
Got it too work, had to include 2 switches to control it
result = firebase.get('/Data', 'Condition')
return result
switch = False
switch2 = True
while True:
res = onoff()
data = res+","
if res == 'on':
if run_once == 0 and switch:
ser.write(data.encode())
print(data)
switch2 = True
switch = False
time.sleep(1)
if res == 'off':
if run_once == 0 and switch2:
ser.write(data.encode())
print(data)
switch = True
switch2 = False
time.sleep(1)

UnboundLocalError: local variable 'validSqaures' referenced before assignment

I'm trying to follow a python tutorial on chess, but when i run the function to check for Checkmates and possible moves, I get the following error:
File "d:\Coding\Projects\Chess\stuff\ChessEngine.py", line 86, in getValidMoves
if not (moves[i].endRow, moves[i].endCol) in validSqaures:
UnboundLocalError: local variable 'validSqaures' referenced before assignment
This is the code I used for the function
def getValidMoves(self):
moves = []
self.inCheck, self.pins, self.checks = self.checkForPinsAndChecks()
if self.whiteToMove:
kingRow = self.whiteKingLocation[0]
kingCol = self.whiteKingLocation[1]
else:
kingRow = self.blackKingLocation[0]
kingCol = self.blackKingLocation[1]
if self.inCheck:
if len(self.checks) == 1: #Only 1 piece checking the King
moves = self.getAllPossibleMoves()
check = self.checks[0]
checkRow = check[0]
checkCol = check[1]
pieceChecking = self.board[checkRow][checkCol]
validSquares = [] #Sqaures pieces can move to
if pieceChecking[1] == "N":
validSqaures = [(checkRow, checkCol)]
else:
for i in range(1, 8):
validSquare = (kingRow + check[2] * i, kingCol + check[3] * i)
validSquares.append(validSquare)
if validSquares[0] == checkRow and validSquares[1] == checkCol:
break
for i in range(len(moves) -1 ,-1, -1):
if moves[i].pieceMoved[1] != 'K':
if len(moves) != 0:
if not (moves[i].endRow, moves[i].endCol) in validSqaures:
moves.remove(moves[i])
else: #Double check, King has to move
self.getKingMoves(kingRow, kingCol, moves)
else: #Not in check so any move is fine
moves = self.getAllPossibleMoves()
if len(moves) == 0: #either CheckMate or Stalemate
if self.inCheck == True:
self.checkMate = True
else:
self.staleMate = True
else:
self.checkMate = False
self.staleMate = False
return moves
When I try placing the list validSqaures anywhere else, it just turns blank and doesnt allow me to make anymore moves after putting the opponent king in check.

Outputting only the objects with "readas" false

Using my current setup on my code; how do i output only the False objects ("hasBeenRead = False")
#An SMS Simulation
SMSStore = []
unreadMessage = []
class SMSMessage(object):
def __init__(self, messageText, fromNumber):
self.hasBeenRead = False
self.messageText = messageText
self.fromNumber = fromNumber
def markAsRead(self, hasBeenRead):
hasBeenRead = True
def add_sms(self):
newMessage = (self.hasBeenRead, self.messageText, self.fromNumber)
return SMSStore.append(newMessage)
def get_count(self):
return len(SMSStore)
def get_message(self, q):
print (SMSStore[q][1])
self.get_hasBeenRead()
def get_hasBeenRead(self):
self.hasBeenRead = True
def get_unread_messages(SMSStore):
counter = 0
if SMSStore[counter][0] == False:
print "From: " + SMSStore[counter][2]
print "Message: " + SMSStore[counter][1]
counter = counter +1
def remove(self, i):
return SMSStore.remove(i)
#sample = SMSMessage("Hello friend!", 0742017560)
userChoice = ""
while userChoice != "quit":
userChoice = raw_input("What would you like to do - read/send/quit?")
if userChoice == "read":
i = int((raw_input("Please enter which messsage number you want to read: ")))
messageText = "null"
fromNumber = "null"
newObject1 = SMSMessage(messageText, fromNumber)
newObject1.get_message(i)
readUnread = raw_input("Would you like to read the unread messages: yes/no?")
if readUnread == "yes":
newObject1.get_unread_messages()
else:
print "All messages have been read"
elif userChoice == "send":
messageText = raw_input("Please type in your message: ")
fromNumber = raw_input("Please type in the number it was sent from ")
newObject = SMSMessage(messageText, fromNumber)
newObject.add_sms()
print str(newObject.get_count()) + " is now the new length of the list"
elif userChoice == "quit":
print "Goodbye"
else:
print "Oops - incorrect input"
As you can see i am quiet a beginner at coding but using what i have setup is it possible to only output objects from the list that have False?

python - 'NoneType' object has no attribute

I'm new with Python/programming and trying to solve little problems to get a hang of it.
I've been struggling with the error below and not too sure how i got it. I understand it is saying the file type is None which is why it's throwing the error. However, I don't get how it is None in the first place? Just wondering if I could get some guidance or hint on the issue? Thank you very much, sorry if the code is messy
line 138, in move
self.ecoList[self.temP].nP = tempAni.p AttributeError: 'NoneType' object has no attribute 'nP'
from random import randint
class Bears:
def __init__(self):
self.p = 0
self.dp = 0
self.nP = 0
self.check = True
def run(self):
self.dp = randint(-1, 1)
self.nP = self.dp + self.p
self.check = False
def __str__(self):
return "Bear_"
def __repr__(self):
return self.__str__()
class Fish:
def __init__(self):
self.p = 0
self.dp = 0
self.nP = 0
self.check = True
def run(self):
self.dp = randint(-1, 1)
self.nP = self.dp + self.p
self.check = False
def __str__(self):
return "Fish|"
def __repr__(self):
return self.__str__()
class EcoSystem:
def __init__(self):
self.ecoList = [None] * 10
self.bearList = []
self.fishList = []
for i in range(2):
self.bearList.append(Bears())
#Adding bear to ecoList
while True:
index = randint(0, 9)
if self.ecoList[index] is None:
self.ecoList[index] = self.bearList[i]
self.ecoList[index].p = index
break
else:
continue
for i in range(2):
self.fishList.append(Fish())
#Adding fish to ecoList
while True:
index = randint(0, 9)
if self.ecoList[index] is None:
self.ecoList[index] = self.fishList[i]
self.ecoList[index].p = index
break
else:
continue
self.move()
def move(self):
#Print out the current eco system
print(*self.ecoList, sep='\n')
anwser = True
while anwser:
#populate next move new position for each object
for i in range(len(self.ecoList)):
if self.ecoList[i] is None:
continue
else:
self.ecoList[i].run()
#run for loop to test next position of the object
for i in range (len(self.ecoList)):
#if [i] item is None skip to next loop
if self.ecoList[i] is None:
continue
elif self.ecoList[i].check == True:
continue
#else check if it is going to move then check adjacent slot is going to be taken
else:
tempAni = None #temp animal to compare with item in loop
#call out new position from item i
newP = self.ecoList[i].nP
#call out direction:
newDP = self.ecoList[i].dp
#do nothing, skip to next slot if it is not going to move
if newDP == 0:
self.ecoList[i].check = True
continue
elif newDP != 0:#test if new position is going to be out of bound
if newP < 0 or newP > (len(self.ecoList)-1):
#set new position back to current
self.ecoList[i].nP = self.ecoList[i].p
self.ecoList[i].dp = 0
self.ecoList[i].check = True
else:
#test if new position is going to be collided
if self.ecoList[newP] is not None:
if self.ecoList[newP].nP == self.ecoList[i].nP:
print("////////////////")
tempAni = self.ecoList[newP]
#test if the next next new position is not None or out of bound
#Assumption - prioritize the closet animal going to move
elif (newP+newDP) > 0 and (newP+newDP) < (len(self.ecoList)-1):
if self.ecoList[newP+newDP] is not None:
#test if this is going to be collided
if self.ecoList[newP+newDP].nP == self.ecoList[i].nP:
print("\\\\\\\\\\\\\\")
tempAni = self.ecoList[newP+newDP]
#if tempAni is not none compare the type
if tempAni is not None:
print ("####")
print (self.ecoList[i].p)
print (self.ecoList[i])
print("-----------")
print (tempAni.p)
print(tempAni)
print ("####")
#test if they are the same type
self.temP = tempAni.p
if tempAni.__class__.__name__ == self.ecoList[i].__class__.__name__:
#if they are, change new position to current position
self.ecoList[i].nP = self.ecoList[i].p
self.ecoList[i].check = True
print("?????")
print(self.temP)
print(tempAni)
print(tempAni.dp)
print(self.ecoList[i])
print(self.ecoList[i].dp)
self.ecoList[self.temP].nP = tempAni.p
self.ecoList[self.temP].check = True
#create new animal of the same type and put it to a random place on the list
#Assumption - if the list is full add do nothing
#Determine tempAni type to create new bear or fish
if isinstance(tempAni, Bears):
#create new bear
newAni = Bears()
else:
#creaete new fish
newAni = Fish()
#while loop if the list is still have available spot add new animal to random spot, otherwise do nothing
while None in self.ecoList:
index = randint(0, 9)
if self.ecoList[index] is None:
self.ecoList.insert(index, newAni)
self.ecoList[index].p = index
print ("*****")
print (self.ecoList[index].p)
print (self.ecoList[index])
print ("*****")
break
#if they are not the same type, kill the fish
else:
#determine if tempAni is the fish or bear
if isinstance(tempAni, Bears):
#if it is bears kill the fish in i
self.ecoList[i].p = -1
self.ecoList[i].check = True
self.ecoList[self.temP].check = True
elif isinstance(tempAni, Fish):
#if it is fish kill it
self.ecoList[self.temP].p = -1
self.ecoList[i].check = True
self.ecoList[self.temP].check = True
#Apply the change after all the checks are finished
#Remove all the fish got killed and apply the moves
for i in range (len(self.ecoList)):
if self.ecoList[i] is not None:
if self.ecoList[i].p == -1:
self.ecoList[i] = None
elif self.ecoList[i].check == False:
self.ecoList[i].check = True
newP = self.ecoList[i].nP
if newP != i:
self.ecoList[newP] = self.ecoList[i]
self.ecoList[newP].p = newP
self.ecoList[i] = None
#Print out the current eco system
print ("---------------------------------------")
for i in range (len(self.ecoList)):
print(self.ecoList[i])
print(i)
#Ask if user want to continue playing
test = True
while test == True:
strAns = input ('Enter y/n to continue or not: ')
if strAns.lower() == "n":
anwser = False
test = False
break
elif strAns.lower() == "y":
test = False
break
def main():
EcoSystem()
main()
The error means that self.ecoList[self.temP] is None, although your code does not expect it to be None. So, this is the offending line:
self.ecoList[self.temP].nP = tempAni.p
Your code actually wants to assign tempAni.p to None.nP. None does not have such an attribute, and that is why you get the error. The line where the code throws the exception is just an indication that something is wrong in your code, somewhere. Finding this bug is your task now.
You need to breathe calmly and step-by-step figure out where your code is wrong. This might be anywhere and nobody here on SO will find this for you. Add print and/or assert statements to your code, and narrow down the issue. This is debugging work, and you have to go through it!

Categories