Pyspotify - periodic "the track cannot be played error" - python

I have a problem with pyspotify. I'm trying to search for an artist name, get a list of their top tracks and then play one of them. This is my code segment:
search_string = "artist:"+artist_to_find
result = session.search(search_string)
result.load()
print result.track_total
browser = result.artists[0].browse()
browser.load()
print browser.tophit_tracks
for track in browser.tophit_tracks:
print track.name, track
toptracks = browser.tophit_tracks
print "error check"
if toptracks!=-1:
print "------------------"
tracktoplay=choice(toptracks)
rand = randint(0,10)
print "random track number = %s" %rand
tracktoplay = browser.tophit_tracks[rand]
print tracktoplay.link
print tracktoplay.name
print "------------------"
session.player.load(tracktoplay)
session.player.play()
I frequently call this code without the previous track finishing. Perodically (every few tracks) I get the following error:
random track number = 7
spotify:track:6vQN2a9QSgWcm74KEZYfDL
Take A Chance On Me
------------------
Traceback (most recent call last):
File "../jj.py", line 146, in <module>
app.run()
File "../jj.py", line 116, in run
conversation.handleForever()
File "/home/mh/Projects/jjo/client/conversation.py", line 44, in handleForever
listen(self)
File "/home/mh/Projects/jjo/client/new_play.py", line 110, in listen
play(self,response)
File "/home/mh/Projects/jjo/client/new_play.py", line 194, in play
session.player.load(tracktoplay)
File "/usr/local/lib/python2.7/dist-packages/spotify/player.py", line 45, in load
self._session._sp_session, track._sp_track))
File "/usr/local/lib/python2.7/dist-packages/spotify/error.py", line 30, in maybe_raise
raise LibError(error_type)
spotify.error.LibError: The track cannot be played
Can anyone advise on what I'm doing wrong?
Thanks

I don't have experiece on pyspotify, but I was looking at it out of curiosity.
If I was you I would check the PlayerState is LOADED before I call play()
Hope it helps.

Related

Python unitesting Html-test runner index out of range

Hi guys I'm new to automation and trying out to automate my ui and generate the HTML test report for it. I'm using Html-test runner for it.
Please refer the following code:
#invalid Password
def test1(self):
rva = signUp_invalidpassword(self.driver)
self.assertEqual("Error : Invalid password",rva)
time.sleep(6)
# self.driver.refresh()
# time.sleep(10)
#Password Mismatch
def test2(self):
rvb = signUp_passwordmismatch(self.driver)
self.assertEqual("Error : Confirm Password is not matching",rvb)
time.sleep(5)
#Signup Succesfully
def test3(self):
time.sleep(5)
rvc = signUp(self.driver)
self.assertEqual("Device Monitoring and Analysis Tool",rvc)
time.sleep(8)
#Default RF parameter
def test4(self):
rvd = SignIn(self.driver)
self.assertEqual("RSRP",rvd)
time.sleep(5)
#Default Zoom_level
def test5(self):
rve = zoomLevel(self.driver)
self.assertEqual("Zoom level: 4.0",rve)
time.sleep(5)
#Chance RF parameter
def test6(self):
rvf = changeRF(self.driver)
self.assertEqual("RSRQ",rvf)
time.sleep(4)
#Empty Search State
def test7(self):
rvg = noState(self.driver)
self.assertEqual("Please enter a search term.",rvg)
time.sleep(4)
#Polygon Functionality
def test8(self):
rvj = polygon(self.driver)
self.assertEqual("CLOSE",rvj)
if __name__=='__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='Dmat_dir'))
But I'm getting the following output:
Generating HTML reports...
Traceback (most recent call last):
File "C:/Users/inswadhwa/PycharmProjects/automation/assertion.py", line 99, in <module>
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(output='Dmat_dir'))
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\unittest\main.py", line 256, in runTests
self.result = testRunner.run(self.test)
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\HtmlTestRunner\runner.py", line 95, in run
result.generate_reports(self)
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\HtmlTestRunner\result.py", line 339, in generate_reports
testRunner)
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\HtmlTestRunner\result.py", line 316, in _report_tests
tests = self.sort_test_list(tests)
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\HtmlTestRunner\result.py", line 303, in sort_test_list
return sorted(test_list, key=self.get_test_number)
File "C:\Users\inswadhwa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\HtmlTestRunner\result.py", line 295, in get_test_number
test_number = int(test_name.split('_')[1])
IndexError: list index out of range
Process finished with exit code 1
Can any one help me with this?
Try changing the name from test1 to test_1 for all test. From the error it seems the runner assumes there will be _ in the name
To control the order use test names like test_001, test_002 and so on.
Also it seems HtmlTestRunner is quite basic and not a great option. So look for alternatives

Multiprocess, various process reading the same file

I am trying to simulate some dna-sequencing reads, and,in order to speed-up the code, I am in need to run it on parallel.
Basically, what I am trying to do is the following:I am sampling reads from the human genome, and I think that one the two process from multiprocessing module try to get data from the same file (the human genome) the processes gets corrupted and it is not able to get the desired DNA sequence. I have tried different things, but I am very new to parallel programming and I cannot solve my problem
When I run the script with one core it works fine.
This is the way I am calling the function
if __name__ == '__main__':
jobs = []
# init the processes
for i in range(number_of_cores):
length= 100
lock = mp.Manager().Lock()
p = mp.Process(target=simulations.sim_reads,args=(lock,FastaFile, "/home/inigo/msc_thesis/genome_data/hg38.fa",length,paired,results_dir,spawn_reads[i],temp_file_names[i]))
jobs.append(p)
p.start()
for p in jobs:
p.join()
And this is the function I am using to get the reads, were each process writes the data to a different file.
def sim_single_end(lc,fastafile,chr,chr_pos_start,chr_pos_end,read_length, unique_id):
lc.acquire()
left_split_read = fastafile.fetch(chr, chr_pos_end - (read_length / 2), chr_pos_end)
right_split_read = fastafile.fetch(chr, chr_pos_start, chr_pos_start + (read_length / 2))
reversed_left_split_read = left_split_read[::-1]
total_read = reversed_left_split_read + right_split_read
seq_id = "id:%s-%s|left_pos:%s-%s|right:%s-%s " % (unique_id,chr, int(chr_pos_end - (read_length / 2)), int(chr_pos_end), int(chr_pos_start),int(chr_pos_start + (read_length / 2)))
quality = "I" * read_length
fastq_string = "#%s\n%s\n+\n%s\n" % (seq_id, total_read, quality)
lc.release()
new_record = SeqIO.read(StringIO(fastq_string), "fastq")
return(new_record)
Here is the traceback:
Traceback (most recent call last):
File "/usr/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
self.run()
File "/usr/lib/python3.5/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "/home/inigo/Dropbox/PycharmProjects/circ_dna/simulations.py", line 107, in sim_ecc_reads
new_read = sim_single_end(lc,fastafile, chr, chr_pos_start, chr_pos_end, read_length, read_id)
File "/home/inigo/Dropbox/PycharmProjects/circ_dna/simulations.py", line 132, in sim_single_end
new_record = SeqIO.read(StringIO(fastq_string), "fastq")
File "/usr/local/lib/python3.5/dist-packages/Bio/SeqIO/__init__.py", line 664, in read
first = next(iterator)
File "/usr/local/lib/python3.5/dist-packages/Bio/SeqIO/__init__.py", line 600, in parse
for r in i:
File "/usr/local/lib/python3.5/dist-packages/Bio/SeqIO/QualityIO.py", line 1031, in FastqPhredIterator
for title_line, seq_string, quality_string in FastqGeneralIterator(handle):
File "/usr/local/lib/python3.5/dist-packages/Bio/SeqIO/QualityIO.py", line 951, in FastqGeneralIterator
% (title_line, seq_len, len(quality_string)))
ValueError: Lengths of sequence and quality values differs for id:6-chr1_KI270707v1_random|left_pos:50511537-50511587|right:50511214-50511264 (0 and 100).
I am the OP of this answer that I did almost a year ago. The problem was that the package that I was using for reading the human genome file (pysam) was failing. The issue was a typo when calling multiprocessing.
From the authors respose, this should work:
p = mp.Process(target=get_fasta, args=(genome_fa,))
note the ',' to ensure you pass a tuple
See https://github.com/pysam-developers/pysam/issues/409 for more details

'RuntimeError: maximum recursion depth exceeded in cmp' when collecting tweets using Tweepy

Im trying to collect a large number of tweets from the twitter api using tweepy. I have a text file containing around ten thousand tweet IDs. My program reads through the file, grabbing each tweet, as well as the tweet that it is replying to. It then saves the text of each tweet, as well as the usernames of each author, in respective text files. Here's the code:
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
api = tweepy.API(auth)
tweetsFile = open("srcstic.txt", "r")
tweets_seen = set() # holds tweets already seen
def getNextLine():
while True:
tweetID = tweetsFile.readline()
getTweetObj(tweetID)
if not tweetID:
break
def getTweetObj(tweetID):
try:
tweetObj = api.get_status(tweetID)
#sleep(11)
except tweepy.error.TweepError:
getNextLine()
else:
pass
tweet = tweetObj.text.encode("utf8")
idnum = tweetObj.in_reply_to_status_id_str
try:
former = api.get_status(idnum)
except tweepy.error.TweepError:
getNextLine()
else:
printFiles(former, tweetObj, tweet)
def printFiles(former, tweetObj, tweet):
callUserName = former.user.screen_name
responseUserName = tweetObj.user.screen_name
if tweet not in tweets_seen:
with open("callauthors.txt", "a") as callauthors:
cauthors = callUserName + "\n"
callauthors.write(cauthors)
with open("responseauthors.txt", "a") as responseauthors:
rauthors = responseUserName + "\n"
responseauthors.write(rauthors)
with open("response_tweets.txt", "a") as responsetweets:
output = (tweetObj.text.encode('utf-8')+"\n")
responsetweets.write(output)
with open("call_tweets.txt", "a") as calltweets:
output = (former.text.encode('utf-8')+"\n")
calltweets.write(output)
tweets_seen.add(tweet)
getNextLine()
However, all works fine for a while then i get the following errors:
File "gettweets2.py", line 68, in <module>
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 40, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
File "gettweets2.py", line 21, in getNextLine
getTweetObj(tweetID)
File "gettweets2.py", line 31, in getTweetObj
getNextLine()
........
........
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_ abcoll.py", line 540, in update
if isinstance(other, Mapping):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py", line 141, in __instancecheck__
subtype in cls._abc_negative_cache):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_weakrefset.py", line 73, in __contains__
return wr in self.data
RuntimeError: maximum recursion depth exceeded in cmp
any ideas whats going wrong here?
Thanks.
You can only call a function recursively 999 times after that you get that error. You could instead call from outside the function using a conditional statement or create a generator.
As best I can read this, a read error can throw you into infinite recursion, as each routine calls the other. If fetching the next line doesn't break you out of the error condition, you'll exceed the stack limit in less than a second.
If nothing else, do a quick check with a couple of print statements: print out the tweetIDs as you encounter them, labeled to identify the print location. A direct repair may well include writing a second routine that grabs the original tweet, without having the capability to recur. This assumes that you need only the immediate parent of the current tweet, rather than the entire chain.

How to catch and manage exceptions for GPS module?

I have a raspberry pi using a GPS module. To use the module, I am running a code such as this:
##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps
if __name__ == '__main__':
gps = microstacknode.hardware.gps.l80gps.L80GPS()
while True:
data = gps.get_gpgga()
List = [list(data.values())[x] for x in [7, 9, 12]]
string=str(List)
string = string[1:-1]
text_file = open("/home/pi/fyp/gps.txt","a")
text_file.write(string + "\n")
time.sleep(1)
However, every now and then it gives this error because it cannot find my location:
Traceback (most recent call last):
File "gps.py", line 8, in <module>
data = gps.get_gpgga()
File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 119, in get_gpgga
pkt = self.get_nmea_pkt('GPGGA')
File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 293, in get_nmea_pkt
"Timed out before valid '{}'.".format(pattern))
microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError: Timed out before valid 'GPGGA'.
It's alright to have that error. The trouble I have is that the program stops running if it occurs. Is there a way to catch that error and get the program to loop back and try again even if it encounters this error?
UPDATE
if I try Stefan_Reinhardt's method, I would get the following error instead:
Traceback (most recent call last):
File "gps.py", line 9, in <module>
data = gps.get_gpgga()
File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 119, in get_gpgga
pkt = self.get_nmea_pkt('GPGGA')
File "/usr/lib/python3/dist-packages/microstacknode/hardware/gps/l80gps.py", line 293, in get_nmea_pkt
"Timed out before valid '{}'.".format(pattern))
microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError: Timed out before valid 'GPGGA'.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "gps.py", line 10, in <module>
except NMEAPacketNotFoundError:
NameError: name 'NMEAPacketNotFoundError' is not defined
I agree to the answer of Oisin,
but i'd suggest to put the try-except clause only arround the line where it could happen, and pass the rest of the while-loop with a continue statement so it would look like
##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps
if __name__ == '__main__':
gps = microstacknode.hardware.gps.l80gps.L80GPS()
while True:
try:
data = gps.get_gpgga()
except NMEAPacketNotFoundError:
continue
List = [list(data.values())[x] for x in [7, 9, 12]]
string=str(List)
string = string[1:-1]
text_file = open("/home/pi/fyp/gps.txt","a")
text_file.write(string + "\n")
time.sleep(1)
This should work but it could get stuck in an infinite recursion loop.
##Prints the latitude and longitude every second.
import time
import microstacknode.hardware.gps.l80gps
if __name__ == '__main__':
getPos()
def getPos():
try:
while True:
gps = microstacknode.hardware.gps.l80gps.L80GPS()
data = gps.get_gpgga()
List = [list(data.values())[x] for x in [7, 9, 12]]
string=str(List)
string = string[1:-1]
text_file = open("/home/pi/fyp/gps.txt","a")
text_file.write(string + "\n")
time.sleep(1)
except microstacknode.hardware.gps.l80gps.NMEAPacketNotFoundError:
getPos()

Python isn't catching KeyError properly

I've tried two different versions of the same function:
def position_of(self, table_name, column_name):
positions = self.heading_positions()
position = positions['{t}.{c}'.format(t=table_name, c=column_name)]
return position
-
def position_of(self, table_name, column_name):
positions = self.heading_positions()
try:
position = positions['{t}.{c}'.format(t=table_name, c=column_name)]
except KeyError:
raise RuntimeError('No heading found for {t}.{c} in import profile "{ip}"'.format(t=table_name, c=column_name, ip=self))
return position
With the first version, I get the following error, which is fine:
Traceback (most recent call last):
File "./import.py", line 15, in <module>
g.process()
File "/home/jason/projects/mcifdjango/mcif/models/generic_import.py", line 39, in process
row.process()
File "/home/jason/projects/mcifdjango/mcif/models/csv_row.py", line 18, in process
self.save()
File "/home/jason/projects/mcifdjango/mcif/models/csv_row.py", line 26, in save
self.output("Phone: " + self.value('customer', 'phone'));
File "/home/jason/projects/mcifdjango/mcif/models/csv_row.py", line 60, in value
print self.generic_import.import_profile.position_of(table_name, column_name)
File "/home/jason/projects/mcifdjango/mcif/models/import_profile.py", line 22, in position_of
position = positions['{t}.{c}'.format(t=table_name, c=column_name)]
KeyError: 'customer.phone'
But the second version - the one that has the more informative error description - fails silently. Why is this?
The second version of position_of works fine for me. I have turned it into a minimal complete program as follows:
class Test(object):
def heading_positions(self):
return {}
def position_of(self, table_name, column_name):
positions = self.heading_positions()
try:
position = positions['{t}.{c}'.format(t=table_name, c=column_name)]
except KeyError:
raise RuntimeError('No heading found for {t}.{c} in import profile "{ip}"'.format(t=table_name, c=column_name, ip=self))
return position
a = Test()
a.position_of('customer', 'phone')
When I run this (using Python 2.6.6 on MacOS X) I get the following error message as expected:
Traceback (most recent call last):
File "./a.py", line 17, in <module>
a.position_of('customer', 'phone')
File "./a.py", line 13, in position_of
raise RuntimeError('No heading found for {t}.{c} in import profile "{ip}"'.format(t=table_name, c=column_name, ip=self))
RuntimeError: No heading found for customer.phone in import profile "<__main__.Test object at 0x100426ad0>"
This shows that catching the KeyError and turning it into a RuntimeError works fine. Does this example work for you? As Sven already writes, a possible explanation would be if you catch RuntimeError but not KeyError somewhere in the call chain.

Categories

Resources