Csound Csound-dev Csound-tekno Search About

pyops - trigger execution of an external python script file

Date2015-09-11 05:39
FromTim Mortimer
Subjectpyops - trigger execution of an external python script file
Hi

I am trying to get this happening, but I'm a bit confused exactly what
combination of pyops (particularly on the "import" side of thing...) I am
supposed to use.. 

(& it is the first time i have used the __call__(self) method in python, but
i think i have done it correctly...)

I am trying to follow the basic principles of the example under "pycall" in
the csound manual.

But the statement

"Supposing we have previously defined or imported a function named
get_number_from_pool as: "

leaves me wondering how i actually include this from a separate .py file
with an appropriate statement, relevant to this particular triggerable
application ...

This is just a test, & i hope the nature of it is fairly self explanatory

with both files in the same directory ...

here is my python script "pyops_test.py"

import random

class CallMe():

	def __init__(self):
	
		return
	
	def __call__(self,kkey_number):
	
		print "this is a test"
		print "number in: %d" % (kkey_number)
		
		self.vallist = []
		for i in range(4):
			x = float(random.randint(0,25))
			self.vallist.append(x)
			
		# must return a tuple containing floats
		tup = tuple(self.vallist)
		return tup
		
x = CallMe()


& here is my .csd

many thanks ....




-odac




sr = 48000
ksmps = 128
nchnls = 2
0dbfs = 1.0

instr 01

	pyinit

	;;; write the python as a class
	;;;  use the __call__() method ...
	;;;  instantiate it as an object
	pyexec "pyops_test.py"
	
	kkey, kdown sensekey
		
	kr1, kr2, kr3, kr4  pylcall4t  kdown, "x" , kkey
	
	printk2 kr1
	printk2 kr2
	printk2 kr3
	printk2 kr4

endin




i 1 0 300






--
View this message in context: http://csound.1045644.n5.nabble.com/pyops-trigger-execution-of-an-external-python-script-file-tp5743559.html
Sent from the Csound - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
Csound-users mailing list
Csound-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/csound-users
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2015-09-11 07:24
FromTim Mortimer
SubjectRe: pyops - trigger execution of an external python script file
i think i have basically sorted it.

i had an absolute vs relative paths problem ... my terminal working
directory wasn't actually the directory i was calling the csd from, i had
specified absolute path, so i needed to specify an absolute path to the
python script in this case also...

i have of course altered the code from the posted example in the meantime,
but i am getting input & output from the relevant python script using
pylcall4t by loading the script using pyexeci "your_script_here.py" (a
slight amendment from pyexec as used in my original example from a couple of
hours ago ...)

back to regular programming .... 



--
View this message in context: http://csound.1045644.n5.nabble.com/pyops-trigger-execution-of-an-external-python-script-file-tp5743559p5743560.html
Sent from the Csound - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
Csound-users mailing list
Csound-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/csound-users
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Date2015-09-11 08:19
FromOeyvind Brandtsegg
SubjectRe: pyops - trigger execution of an external python script file
... good that you solved it,
you could also use

pyruni "import pyops_test"

then you would access the functions in pyops_test as
pyops_test.myFunction

I'm not actually sure if it would be different, but it seems more in
line with regular Python practice to use import when you want to ...
well, import,.. the functions or classes from an external file
When importing in Python, the code in the file is executed, so it
might be the same for simple cases. With import you can also do things
like
import melodygenerator as m

come to think of it, maybe running the file with pyexeci actually equals
from pyops_test import *



2015-09-11 8:24 GMT+02:00 Tim Mortimer :
> i think i have basically sorted it.
>
> i had an absolute vs relative paths problem ... my terminal working
> directory wasn't actually the directory i was calling the csd from, i had
> specified absolute path, so i needed to specify an absolute path to the
> python script in this case also...
>
> i have of course altered the code from the posted example in the meantime,
> but i am getting input & output from the relevant python script using
> pylcall4t by loading the script using pyexeci "your_script_here.py" (a
> slight amendment from pyexec as used in my original example from a couple of
> hours ago ...)
>
> back to regular programming ....
>
>
>
> --
> View this message in context: http://csound.1045644.n5.nabble.com/pyops-trigger-execution-of-an-external-python-script-file-tp5743559p5743560.html
> Sent from the Csound - General mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Csound-users mailing list
> Csound-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/csound-users
> Send bugs reports to
>         https://github.com/csound/csound/issues
> Discussions of bugs and features can be posted here


Date2015-09-11 16:13
FromTim Mortimer
SubjectRe: pyops - trigger execution of an external python script file
Thank you Oeyvind,

I agree, that is certainly more in keeping with my standard python practice
at least ... 

I am wondering if __call__() lends Python to other types of API interfacing
(sic?) more easily ... it was a new one for me, so i thought id give it a
try.

moving off on a tangent (& it's late here, excuse the vague-ary...) ... i
wonder if __call__() might also assist in achieving something like this,
which i nutted out recently when i was experimenting with rolling my own
tree like structure ... (simplified version...)

class Node():

    def __init__(self):

        # some object type ...
        self.value = None

        self.branches = []

        return

    def
apply_function_to_value_sequential_return_new_args(self,function,*args):

        '''
        RECURSIVE FUNCTION
        some object type has been assigned to the value
        an associated class member function is called for the object
        & applied recursively
        to all the objects in the tree structure

        '''

        args = function(self.value,*args)

        for n in self.branches:
            args =
n.apply_function_to_value_sequential_return_new_args(function,*args)

        return args



--
View this message in context: http://csound.1045644.n5.nabble.com/pyops-trigger-execution-of-an-external-python-script-file-tp5743559p5743562.html
Sent from the Csound - General mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
Csound-users mailing list
Csound-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/csound-users
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here