mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

what kind of data is this?

Mon Aug 25, 2014 11:03 pm

I'm using mpd and a python wrapper. When I use status() I get this it starts with the ___ bracket. I'm trying to retrieve this data to find out if it is playing or not.
>>> data
{'songid': '28', 'playlistlength': '1', 'playlist': '32', 'repeat': '0', 'consume': '0', 'mixrampdb': '0.000000', 'random': '0', 'state': 'play', 'xfade': '0', 'volume': '-1', 'single': '1', 'mixrampdelay': 'nan', 'time': '228:429', 'song': '0', 'elapsed': '228.473', 'bitrate': '128', 'audio': '44100:24:2'}

My current workaround is turning it into a string and using the split(',') to retrieve my information.

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: what kind of data is this?

Mon Aug 25, 2014 11:18 pm

Looks like it is a dictionary.
There are 10 types of people: those who understand binary and those who don't.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13099
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: what kind of data is this?

Mon Aug 25, 2014 11:59 pm

looks like the internal list of strings used in an music player program that has a "playlist", and organized so that internationalizing (translating the program) is made easier.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Tue Aug 26, 2014 3:01 am

any easy way to extract the data? into a variable I want to know if it is playing or not..

User avatar
Douglas6
Posts: 4860
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: what kind of data is this?

Tue Aug 26, 2014 3:33 am

I believe kusti8 is correct, but you can verify by reading the documentation regarding how you got the data variable. Assuming it is a Python dictionary, you can access the 'state' property so:

Code: Select all

if data['state'] == 'play':
    print('playing')
Very basic, very well documented Python code.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: what kind of data is this?

Tue Aug 26, 2014 6:15 am

https://docs.python.org/2/library/json.html

It's JSON so simple to decode.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Fri Aug 29, 2014 8:55 pm

okay how to I use json.... I want to check what state it is on...

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: what kind of data is this?

Fri Aug 29, 2014 8:58 pm

Code: Select all

import json
decoded = json.loads(jsondata)
print decoded['songid'], decoded['state']
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

gkreidl
Posts: 6326
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: what kind of data is this?

Fri Aug 29, 2014 9:17 pm

DougieLawson wrote:

Code: Select all

import json
decoded = json.loads(jsondata)
print decoded['songid'], decoded['state']
Sorry, Dougie, but this is simply nonsense. Why should anybody use Json inside Python to "decode" a simple Python dictionary,
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Fri Aug 29, 2014 9:22 pm

oh sorry I didn't read the first parts. Yes that works perfectly =)

if data['state'] == 'play':
print('playing')

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Fri Aug 29, 2014 9:43 pm

Thanks again that code worked perfectly. I want to do one more thing

i'm doing data['time']
i get this outputted
'21:429'
is it possible to some how extract compare the numbers in a %.

e.g. 21/429 = 0.05 = 5%

I want to created an LED bar that will show the progress of the audio.

EDIT: I tried just replacing the : to a / but that didn't work =/ lol

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Fri Aug 29, 2014 9:55 pm

Nevermind figured it out good old 'eval'

User avatar
Douglas6
Posts: 4860
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: what kind of data is this?

Fri Aug 29, 2014 9:59 pm

mrteach wrote:I'm using mpd and a python wrapper. When I use status() I get this it starts with the ___ bracket. I'm trying to retrieve this data to find out if it is playing or not.
>>> data
{'songid': '28', 'playlistlength': '1', 'playlist': '32', 'repeat': '0', 'consume': '0', 'mixrampdb': '0.000000', 'random': '0', 'state': 'play', 'xfade': '0', 'volume': '-1', 'single': '1', 'mixrampdelay': 'nan', 'time': '228:429', 'song': '0', 'elapsed': '228.473', 'bitrate': '128', 'audio': '44100:24:2'}
Gotta admit, that data looks a bit 'pantsed'.
data['time'] = 228:429
data['elapsed'] = 228.473

So not only is the format different (colon vs. dot), but the elapsed time is greater than the song time (assuming that's what those are.) Again assuming they represent seconds and milliseconds, you could split out the first part of each and divide elapsed by (total) time, but, I'm not feeling good about the data.

What is the 'python wrapper' you're using? Isn't there a fairly standard Python library for MPD?

[EDIT: Ok, if that works, clearly my assumptions were wrong.]

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: what kind of data is this?

Fri Aug 29, 2014 11:00 pm

gkreidl wrote:
DougieLawson wrote:

Code: Select all

import json
decoded = json.loads(jsondata)
print decoded['songid'], decoded['state']
Sorry, Dougie, but this is simply nonsense. Why should anybody use Json inside Python to "decode" a simple Python dictionary,
It walks like JSON, it quacks like JSON, why not decode it like JSON?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mrteach
Posts: 181
Joined: Sun May 26, 2013 6:49 am

Re: what kind of data is this?

Sat Aug 30, 2014 2:01 am

Douglas6 wrote:
mrteach wrote:I'm using mpd and a python wrapper. When I use status() I get this it starts with the ___ bracket. I'm trying to retrieve this data to find out if it is playing or not.
>>> data
{'songid': '28', 'playlistlength': '1', 'playlist': '32', 'repeat': '0', 'consume': '0', 'mixrampdb': '0.000000', 'random': '0', 'state': 'play', 'xfade': '0', 'volume': '-1', 'single': '1', 'mixrampdelay': 'nan', 'time': '228:429', 'song': '0', 'elapsed': '228.473', 'bitrate': '128', 'audio': '44100:24:2'}
Gotta admit, that data looks a bit 'pantsed'.
data['time'] = 228:429
data['elapsed'] = 228.473

So not only is the format different (colon vs. dot), but the elapsed time is greater than the song time (assuming that's what those are.) Again assuming they represent seconds and milliseconds, you could split out the first part of each and divide elapsed by (total) time, but, I'm not feeling good about the data.

What is the 'python wrapper' you're using? Isn't there a fairly standard Python library for MPD?

[EDIT: Ok, if that works, clearly my assumptions were wrong.]
Yeah I had to take a close look (I didn't think they had a song length) they did it by saying 228(current duration of song):429(length of the whole song).

Using python-mpd2 by the way.

Thanks again. I think the only reason I wouldn't use json is because you have to import something else(not sure if this acutally takes memory or anything) and it won't decode right away you have to convert it from ' to " using json.dumps(data) first.

so I would have to do

Code: Select all

import json
decoded = json.loads(json.dumps(data))
print decoded['songid'], decoded['state']
and oddly it would output u'play or something like that instead of just play (unless I messed up with the code). I tried without dumping it (converting ' to ") but it gave me an error.

this did help me learn a bit about json.. not sure what it is for but will be in the back in my memory banks. Thanks.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: what kind of data is this?

Sat Aug 30, 2014 3:04 am

DougieLawson wrote:
gkreidl wrote:
DougieLawson wrote:

Code: Select all

import json
decoded = json.loads(jsondata)
print decoded['songid'], decoded['state']
Sorry, Dougie, but this is simply nonsense. Why should anybody use Json inside Python to "decode" a simple Python dictionary,
It walks like JSON, it quacks like JSON, why not decode it like JSON?
Because it's a dict. It's already 'decoded' and trivial to access.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: what kind of data is this?

Sat Aug 30, 2014 8:27 am

DougieLawson wrote:
gkreidl wrote:
DougieLawson wrote:

Code: Select all

import json
decoded = json.loads(jsondata)
print decoded['songid'], decoded['state']
Sorry, Dougie, but this is simply nonsense. Why should anybody use Json inside Python to "decode" a simple Python dictionary,
It walks like JSON, it quacks like JSON, why not decode it like JSON?
http://json.org/

However JSON quacks twice, this quacks only once . JSON insists on " rather than '. I got caught by that one only yesterday.

Its a printout of a python dictionary
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: what kind of data is this?

Sat Aug 30, 2014 12:30 pm

similar but different:
1. data is a dictionary object
>>> data
{'songid': '28', 'playlistlength': '1', 'playlist': '32', 'repeat': '0', 'consume': '0', 'mixrampdb': '0.000000', 'random': '0', 'state': 'play', 'xfade': '0', 'volume': '-1', 'single': '1', 'mixrampdelay': 'nan', 'time': '228:429', 'song': '0', 'elapsed': '228.473', 'bitrate': '128', 'audio': '44100:24:2'}
2. data is json.dumps(dictionary object)
>>> data
'{"songid": "28", "playlistlength": "1", "playlist": "32", "repeat": "0", "consume": "0", "mixrampdb": "0.000000", "random": "0", "state": "play", "xfade": "0", "volume": "-1", "single": "1", "mixrampdelay": "nan", "time": "228:429", "song": "0", "elapsed": "228.473", "bitrate": "128", "audio": "44100:24:2"}'
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”