Searching Sonos Music Library with SoCo in Python - python

I have another SoCo questions and I really hope someone can get me started.
I'm really pulling my hair out here. What am I doing wrong?
>>> from soco.music_library import MusicLibrary
>>> MusicLibrary.get_music_library_information('artists', search_term='Metallica')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: get_music_library_information() missing 1 required positional argument: 'search_type'
I copied the get_music_library_information('artists', search_term='Metallica') straight out of the docs.

Thanks for having a go Bahrom. I got it to take action in the following way:
First i got the list of speakers with a
speakers=soco.discover()
and then I selected one of the speakers, calling it 'speaker'.
>>> for speaker in speakers:
... if speaker.player_name == 'Office':
... break
Then I appended the get command on to the speaker e.g.
>>> from soco.music_library import MusicLibrary
>>> speaker.get_music_library_information('genres')
and this works :)

Haven't tested this, but looking at music_library.py on github, I think you just need to instantiate MusicLibrary first:
>>> from soco.music_library import MusicLibrary
>>> MusicLibrary().get_music_library_information('artists', search_term='Metallica')

Related

Attribute error from SoCo class MusicLibrary that documentation appears to say is not an error

I am doing initial exploration in the SoCo module which is a Python API for Sonos. Following the tutorial I do
>>> speakers = soco.discover()
>>> speaker = speakers.pop()
>>> speaker.player_name
'Portable'
>>> speaker.ip_address
'192.168.0.11'
>>> speaker.get_current_transport_info()['current_transport_state']
'STOPPED'
So far, so good. Now, still following the documentation, I do
>>> speaker.music_library.list_library_shares()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'MusicLibrary' object has no attribute 'list_library_shares'
and, sure enough, dir(speaker.music_library) confirms there is no such method.
But the documentation for this class says:
Listing and deleting music library shares
Music library shares are the local network drive shares connected to Sonos, which host the
audio content in the Sonos Music Library.
To list the shares connected to Sonos, use the list_library_shares()
method as follows:
››› device.music_library.list_library_shares()
['//share_host_01/music', '//share_host_02/music']
The result is a
list of network share locations.
Now either the documentation is badly out of step with the version I just downloaded (0.18.1; it says not), or I need another pair of eyes to point out what I am doing wrong.
I asked this question also on the SoCo Google group and this is the answer I got:
This function is not included in v0.18.1, but will be in the upcoming
v0.19 release. It's unintuitive but you need to look at the v0.18.1
documentation at:
http://docs.python-soco.com/en/v0.18.1/api/soco.music_library.html
... not the 'latest' version of the documentation.
I forbear to remark on the wisdom of pointing readers of the Getting Started page in the wrong direction.

looping on array throws: ValueError: need more than 1 value to unpack

Edit: Issue was with environment. Terminal restart made things work again.
I was given an assignment, write a function get_name(names) , call it with [{"name":"one"},{"name":"two"}] and do an action with the name.
My code is:
def get_name(names):
for name in names:
print name
and when I call it with
get_name([{"name":"one"},{"name":"two"}])
I get an error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 2, in get_name
for name in names:
ValueError: need more than 1 value to unpack
I read all links regarding this error on the first 5 google result pages and a few more directly on stackoverflow - none give a hind to the issue I'm having.
Which version of python are you using?
I have python 3.6.4 and when i try exactly your code i have
{'name': 'one'}
{'name': 'two'}
as output

Error when accessing synonyms in python using nltk?

I have written a very simple piece of code to try and print the synonyms associated with a word.
import nltk
from nltk.corpus import wordnet as wn
wordNetSynset = wn.synsets('small')
for synSet in wordNetSynset:
for synWords in synSet.lemma_names:
synonymList.add(synWords)
print synonymList
However, I get the following error:
Traceback (most recent call last):
File "test.py", line 6, in <module>
for synWords in synSet.lemma_names:
TypeError: 'instancemethod' object is not iterable
Does anyone know what the problem could be?
In Nltk 3, the lemma_names has been changed to a method from an attribute.
So you have to call the method
for synWords in synSet.lemma_names():
Other minor changes required are:
synonymList is not defined
List will not have an add method even if synonymList is defined
You better name your variable synonymSet

How do I generate random text in NLTK 3.0?

The generate method of nltk.text.Text seems to have been removed in NLTK 3.0.
For example:
>>> bible = nltk.corpus.gutenberg.words(u'bible-kjv.txt')
>>> bibleText = nltk.Text(bible)
>>> bibleText.generate()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Text' object has no attribute 'generate'
It may just be that I'm remembering wrongly how to do this, but everything I can find online seems to support the above method. Any ideas what I'm doing wrong?
A note in the first online chapter of the NLTK book says that:
The generate() method is not available in NLTK 3.0 but will be
reinstated in a subsequent version.

python AssertionError in django

I am reading the http://www.djangobook.com/en/2.0/chapter04.html. I'm new to python and django but have experience with php.
I've come across the following:
>>> t = Template("My name is {{ person.first_name }}.")
>>> class PersonClass3:
... def first_name(self):
... raise AssertionError, "foo"
>>> p = PersonClass3()
>>> t.render(Context({"person": p}))
this gives the following error;
Traceback (most recent call last):
...
AssertionError: foo
would someone mind explaining why this error occurs? I'm not sure I follow what the problem is. I understand lines 1,2 and 5 but not the others.
Thank you,
Bill
Your code has done exactly what the example was trying to show. You “raised” an exception which caused your program to halt execution because there was no handler to deal with it.
This guide might be a good place to start.

Categories

Resources