pyglet ManagedSoundPlayer on_eos event not working? - python

I am trying to listen to the on_eos event of the pyglet.media.ManagedSoundPlayer following the examples in the pyglet documentation and end up with code like this:
from pyglet.media import load, ManagedSoundPlayer
def on_eos():
print "EOS"
def play(source):
player = source.play()
player.on_eos = on_eos
src = load("beep.mp3")
play(src)
As expected, I do hear a "beep" - but to my surprise and frustration, "EOS" is not printed.
Did I miss something in the docs? Am I doing something wrong? What should I do to make this work?
Thank you!

Accordingly to this old link to pyglet Google group you need some tricks to reuse on_eos. It is still not implemented. Take a look on the sample from documentation how to implement on_eos event: http://www.pyglet.org/doc/programming_guide/media_player.py

Related

Random.Choice always the same result

Some pretext that's maybe useful idk. I use mixer and am trying to do a random music choice but the random.choice always take the last one (song 5). This is my code
if Mamp=='1':
vad=input("[1] Bakground music")
#Mixer shit
mixer.init()
pygame.mixer.get_init
if vad=='1':
commands=[mixer.music.load("song1.mp3"), mixer.music.load("song2.mp3"),mixer.music.load("song3.mp3"),mixer.music.load("song4.mp3"),mixer.music.load("song5.mp3")]
current_command=random.choice(commands)
current_command
mixer.music.play(0)
I looked at many answers for the same problem but all wer're just fixing thier (the uploader's) code and uploaded it so I couldn't use it for my code. And since i'm trying to learn python it would be great if you could also explain what went wrong?
commands=[mixer.music.load("song1.mp3"), ...]
current_command=random.choice(commands)
current_command
You're doing this part all wrong.
It seems like you want to make a list of commands to be executed later, but that's not what you're actually doing. The load() commands are actually executed when the commands list is created.
And because song5.mp3 is the last one, it gives the appearance of being "picked" every time.
Try it this way instead:
songs = ["song1.mp3", "song2.mp3", "song3.mp3", "song4.mp3", "song5.mp3"]
song = random.choice(songs)
mixer.music.load(song)

Use UINotificationFeedbackGenerator in pythonista Objc_util

I'm trying to use UINotifcationFeedbackGenerator in pythonista,
from objc_util import *
feedbackGenerator = ObjCClass('UINotifcationFeedbackGenerator')
feedbackGenerator = feedbackGenerator.alloc().init()
feedbackGenerator.notificationOccurred(0)
but running this causes the app to crash, with the error file saying
called more times than the feedback engine was activated
so searching it up, it seems the feedback Generator isn't tread safe, but using on_main_thread() didn't work either (or i'm just using it wrong). Through strangely enough, adding it to a method called by ui works here
Thanks for your help!
Maybe a little late ;-), but: You are overwriting feedbackGenerator which will cause the crash:
feedbackGenerator = feedbackGenerator.alloc().init()
Try this:
f = feedbackGenerator.alloc().init()
f.notificationOccurred(0)

Getting a selection in 3ds Max into a list in Python

I am writing in Python, sometimes calling certain aspects of maxscript and I have gotten most of the basics to work. However, I still don't understand FPValues. I don't even understand while looking through the examples and the max help site how to get anything meaningful out of them. For example:
import MaxPlus as MP
import pymxs
MPEval = MP.Core.EvalMAXScript
objectList = []
def addBtnCheck():
select = MPEval('''GetCurrentSelection()''')
objectList.append(select)
print(objectList)
MPEval('''
try (destroyDialog unnamedRollout) catch()
rollout unnamedRollout "Centered" width:262 height:350
(
button 'addBtn' "Add Selection to List" pos:[16,24] width:88 height:38
align:#left
on 'addBtn' pressed do
(
python.Execute "addBtnCheck()"
)
)
''')
MP.Core.EvalMAXScript('''createDialog unnamedRollout''')
(I hope I got the indentation right, pretty new at this)
In the above code I successfully spawned my rollout, and used a button press to call a python function and then I try to put the selection of a group of objects in a variable that I can control through python.
The objectList print gives me this:
[<MaxPlus.FPValue; proxy of <Swig Object of type 'Autodesk::Max::FPValue *' at 0x00000000846E5F00> >]
When used on a selection of two objects. While I would like the object names, their positions, etc!
If anybody can point me in the right direction, or explain FPValues and how to use them like I am an actual five year old, I would be eternally grateful!
Where to start, to me the main issue seems to be the way you're approaching it:
why use MaxPlus at all, that's an low-level SDK wrapper as unpythonic (and incomplete) as it gets
why call maxscript from python for things that can be done in python (getCurrentSelection)
why use maxscript to create UI, you're in python, use pySide
if you can do it in maxscript, why would you do it in python in the first place? Aside from faster math ops, most of the scene operations will be orders of magnitude slower in python. And if you want, you can import and use python modules in maxscript, too.
import MaxPlus as MP
import pymxs
mySel = mp.SelectionManager.Nodes
objectList = []
for each in mySel:
x = each.Name
objectList.append(x)
print objectList
The easiest way I know is with the
my_selection = rt.selection
command...
However, I've found it works a little better for me to throw it into a list() function as well so I can get it as a Python list instead of a MAXscript array. This isn't required but some things get weird when using the default return from rt.selection.
my_selection = list(rt.selection)
Once you have the objects in a list you can just access its attributes by looking up what its called for MAXscript.
for obj in my_selection:
print(obj.name)

MPRIS + Python (dbus): reading and writing properties

I have already checked this link: How to handle properties of a dbus interface with python. However, that only lists an API... but I don't know where that API comes from.
I just started working with dbus (pretty excited about this, to be honest ^__^ just not too happy with the documentation I've found) on python and I was wondering if I could just get some sample code.
I'm using MPRIS specifically for Rhythmbox, although it 'should' be the same for all.
I know I can access and have fun witht he methods by doing the following:
import dbus
bus = dbus.SessionBus()
proxy = bus.get_object('org.mpris.MediaPlayer2.rhythmbox','/org/mpris/MediaPlayer2')
player = dbus.Interface(proxy, 'org.mpris.MediaPlayer2.Player')
playlists = dbus.Interface(proxy, 'org.mpris.MediaPlayer2.Playlists')
tracklist = dbus.Interface(proxy, 'org.mpris.MediaPlayer2.TrackList')
However, I wish to know about properties. Some sample code will suffice :) Thanks!
Found how.
proxy = bus.get_object('org.mpris.MediaPlayer2.rhythmbox','/org/mpris/MediaPlayer2')
properties_manager = dbus.Interface(proxy, 'org.freedesktop.DBus.Properties')
properties_manager.Set('org.mpris.MediaPlayer2.Player', 'Volume', 100.0)
curr_volume = properties_manager.Get('org.mpris.MediaPlayer2.Player', 'Volume')
Pretty simple indeed :) I thought it would be simple like this.

How would you adblock using Python?

I'm slowly building a web browser in PyQt4 and like the speed i'm getting out of it. However, I want to combine easylist.txt with it. I believe adblock uses this to block http requests by the browser.
How would you go about it using python/PyQt4?
[edit1] Ok. I think i've setup Privoxy. I haven't setup any additional filters and it seems to work. The PyQt4 i've tried to use looks like this
self.proxyIP = "127.0.0.1"
self.proxyPORT= 8118
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.HttpProxy)
proxy.setHostName(self.proxyIP)
proxy.setPort(self.proxyPORT)
QNetworkProxy.setApplicationProxy(proxy)
However, this does absolutely nothing and I cannot make sense of the docs and can not find any examples.
[edit2] I've just noticed that i'f I change self.proxyIP to my actual local IP rather than 127.0.0.1 the page doesn't load. So something is happening.
I know this is an old question, but I thought I'd try giving an answer for anyone who happens to stumble upon it. You could create a subclass of QNetworkAccessManager and combine it with https://github.com/atereshkin/abpy. Something kind of like this:
from PyQt4.QtNetwork import QNetworkAccessManager
from abpy import Filter
adblockFilter = Filter(file("easylist.txt"))
class MyNetworkAccessManager(QNetworkAccessManager):
def createRequest(self, op, request, device=None):
url = request.url().toString()
doFilter = adblockFilter.match(url)
if doFilter:
return QNetworkAccessManager.createRequest(self, self.GetOperation, QNetworkRequest(QUrl()))
else:
QNetworkAccessManager.createRequest(self, op, request, device)
myNetworkAccessManager = MyNetworkAccessManager()
After that, set the following on all your QWebView instances, or make a subclass of QWebView:
QWebView.page().setNetworkAccessManager(myNetworkAccessManager)
Hope this helps!
Is this question about web filtering?
Then try use some of external web-proxy, for sample Privoxy (http://en.wikipedia.org/wiki/Privoxy).
The easylist.txt file is simply plain text, as demonstrated here: http://adblockplus.mozdev.org/easylist/easylist.txt
lines beginning with [ and also ! appear to be comments, so it is simply a case of sorting through the file, and searching for the correct things in the url/request depending upon the starting character of the line in the easylist.txt file.
Privoxy is solid. If you want it to be completely API based though, check out the BrightCloud web filtering API as well.

Categories

Resources