Python,; KeyError; Volumeup

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Bartman
Plugin Developer
Posts: 881
Joined: Sun Feb 12, 2006 9:03 am

Re: Python,; KeyError; Volumeup

Post by Bartman » Wed Dec 30, 2009 2:27 pm

I mixed it up with the smooth volume change. If you only change volume once (just not linear) everything will be fine.

User avatar
jitterjames
Experienced User
Posts: 677
Joined: Thu Aug 13, 2009 4:36 pm
Location: Quebec, Canada
Contact:

Re: Python,; KeyError; Volumeup

Post by jitterjames » Wed Dec 30, 2009 3:13 pm

Actually I meant 20th century... it has been like this since the 90s at least! I am not trying to offend you, but perhaps trying to use a bit of force to help you see reason. Ultimately I am trying to help, and admittedly, I am no expert, although I have used eventGhost quite a bit and written some simple plugins.

It's just that eventGhost has taken care of all this for you already and you are making life difficult for yourself for no reason. I applaud your effort and determination though. It is the best way to learn, and plowing ahead despite what others may tell you will probably be to your ultimate benefit in the long run.

I strongly recommend staying away from threading until you have a firmer grasp of the basics. Threading is a very messy business and often leads to trouble even for experienced programmers. I had to use threading for my smooth volume plugin and it was very tricky. To this day I am not 100% convinced that the code is "air tight".

even if you use threading and use timeouts and have a multicore processor, endlessly looping is a very ineffecient way to handle what events do very nicely.

Good luck in your efforts.

Kindt.nick
Posts: 16
Joined: Mon Dec 28, 2009 6:21 pm

Re: Python,; KeyError; Volumeup

Post by Kindt.nick » Wed Dec 30, 2009 4:24 pm

Hi,

No offense taken, it's just that, I would like to write everything in python code.
I'm used to reading code and it's a lot easier and has more overview for me than the tree view
that eventghost supplies. Although I think it's a good way for starting programmers or small jobs.
Not that we are advanced or something like that, but still.

So as I understand is no way to write the attached picture in python code?

In embedded programming, for things like this, I work with interrupts.
This way there is never any code running that is not neccessary.
I think the "event" in evenghost works kinda the same way.

Thx
Nick
Attachments
event_example.png
event_example.png (1.1 KiB) Viewed 4643 times

User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Python,; KeyError; Volumeup

Post by Bitmonster » Wed Dec 30, 2009 6:12 pm

EG uses hash-table-lookups for the event processing and since Python has one of the best hash-table implementations of all programming languages, this is also very resource friendly. Doing an if-then-elif for every possible event will use multiple powers more CPU cycles if you have some more events to filter. And normally the unused events will use the most, as they have to go through every comparison.

You can handle every event with a script if you like, if you use MceRemote.* as event strings or even just * as the wildcard for every event from every plugin. But believe it or not, you will later hate yourself if you do everything from a central script. The first EventGhost was just this and you end in one big unreadable and inflexible script file.

Adding a new target application to control or new events to a big script really gets a lot of work and error prone. Adding a new folder with all actions of a plugin and assigning events to them is a matter of some few minutes and you can find everything you want to modify by simply double clicking the processed action in the log, as EG will then highlight the corresponding tree item. You will not find code in a source file any faster even with good tracing tools.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!

Kindt.nick
Posts: 16
Joined: Mon Dec 28, 2009 6:21 pm

Re: Python,; KeyError; Volumeup

Post by Kindt.nick » Wed Dec 30, 2009 6:57 pm

Thx for the insight on how the event processing works.

"Doing an if-then-elif for every possible event will use multiple powers more CPU cycles", that is actually true.
As for debugging, it is true that the implemented auto-visualisation helps you allot.
Except, it also depends on how how you write your code.
I usually work with lots of "print" and "ifdef", it all depends on what you started from I guess.
Python does not have the ifdef which creates no extra cpu cycles for the code between it not defined,
this because it simply does not get compiled. At least I didn't find somehting like that.
Nor does it has a case implemented, which generally needs less cpu cycles then an if-then-etc...

This sais, I guess the writen version of "event processing" is not implemented in eventghost. Does it?

I said it before and I'll say it again, I just started out with pythong and eventghost.
I've already read a couple artickels about pythong but I'm still trying to find out what is and isn't possible.
Same with finding out which loops and functions that are most effective to use.

Regards,
Nick

Bartman
Plugin Developer
Posts: 881
Joined: Sun Feb 12, 2006 9:03 am

Re: Python,; KeyError; Volumeup

Post by Bartman » Wed Dec 30, 2009 7:08 pm

Using a lot of prints will further slow you down. With a real IDE there is really no need for prints as you can use the Debugger.

What advantages do you expect from writing the event processing (which is the core of EG) yourself? If you want real effencieny then you should not use python or any scripting language at all.

Although I wished sometimes EG would have been written in C# which I would choose for most windows applications.

User avatar
Bitmonster
Site Admin
Posts: 2239
Joined: Mon Feb 06, 2006 10:28 pm

Re: Python,; KeyError; Volumeup

Post by Bitmonster » Wed Dec 30, 2009 7:44 pm

First: there is nearly nothing that can be done in one language, that can't be done in Python. And Python can do a lot more. You only have to wait till you get this "dynamic" in "dynamic language" into your head and you start thinking and dreaming this way. This is the problematic point and actually the more classic experience a programmer has, the longer it takes sometimes. Somebody without any experience will often get this enlightenment much faster.

In a language like Python you can change everything at any time. Not just before you invoke a compiler. That's the reason why you don't need an "#IFDEF" in Python. You can simply use the normal "if". But you let it process at module level and the "then" case is a function/class/object definition.

Code: Select all

if eg.debugLevel:
   def Debug(msg):
      print msg
else:
   def Debug(msg):
      pass
And this will only get processed once, in the moment the module is loaded. Not every time your code comes to a point where a print statement might be used (if debugging is requested). And even if a module is used from many different modules it only gets loaded once by the Python interpreter. So you just write:

Code: Select all

    ...
    Debug("my message")
    ...
instead of:

Code: Select all

    ...
    if eg.debugLevel:
         print "my message"
    ...
Even though this concrete example might be questionable from a performance point of view, this technique (dynamic function definitions) is one concept how extreme performance gains can be archived if used in the right places.

The event processing is implemented inside EG (EventGhostEvent.Execute() to be exactly) but it relies heavily on features of the Python language, like dictionary lookups and dynamic function invocation. Of course you can do the same in a Python script (like dynamic function lookup and invocation) to archive the same performance and your code might even get more readable as a flat if-then-elif chain, if done right.

But it will not help you as you are just re-implementing the wheel, while EG already has implemented the jet engine. Better delay the building of your vehicle till you have understood the jet engine also by using it and then you might be able to aim a rocket engine and be the first man on the moon.
Please post software-related questions in the forum - PMs will only be answered, if really private, thanks!

Kindt.nick
Posts: 16
Joined: Mon Dec 28, 2009 6:21 pm

Re: Python,; KeyError; Volumeup

Post by Kindt.nick » Sun Jan 03, 2010 4:43 pm

Bitmonster wrote: And this will only get processed once, in the moment the module is loaded. Not every time your code comes to a point where a print statement might be used (if debugging is requested).

Indeed, that seems quite odd to me, but hey, if it works, why not

Post Reply