[Beginner] Calling a function by its name in a string
Hi all: Is there a metalanguage capability in Python (I know there are many) to call a function having its name in a string? Something like: __call__("foo") instead of: foo() Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Calling a function by its name in a string
Thank you both for your quick answers. What I wanted is to parameterize a function with another member function, like this: def printFunctionForEach(collection, functionName): for elem in collection: print eval("elem." + functionName + "()") Moreover, I wanted to do it with a property: def printPropertyForEach(collection, propertyName): for elem in collection: print eval("elem." + propertyName) Is there another approach to do it? Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Calling a function by its name in a string
> Thank you both for your quick answers. Thank you *all* for your quick answers. -- http://mail.python.org/mailman/listinfo/python-list
Re: [Beginner] Calling a function by its name in a string
Once again: thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: What are modules really for?
N.Davis wrote: > Functions existing in a module? Surely if "everything is an object" (OK > thats Java-talk but supposedly Python will eventually follow this too) > then there should be nothing in a module thats not part of a class. Well, all data in a Python program are objects, in the sense that they have an identity on their own, and variables are only references to them. Why is it necessary to have all code into classes for meeting the "everything is an object" panacea? > Even > a static method is simply a class function that operates on the > "collection of all instances" rather than a single instance. That is not true. A static method knows nothing about the instances of a class, unless you do it your own. Besides, it will work whether you have created instances of the class or not. So, a static method is just a global method declared withing a class, which just serves as a namespace for it. > Related classes in the same file? Be careful. Doesn't anything "knowing" > about anything else compromise encapsulation? Why would > properly-designed classes have such a close relationship? Question back: why do you think having classes defined in the same file compromises encapsulation? Classes don't know more about each other for the fact of being written into the same file. Anyway, in Python, classes know always too much from each other, don't they?, as there are no access modifiers for class attributes. > Having back in the day worked on big real-time systems where being very > strict about encapsulation was a god-send for fighting complexity, I > feel unnerved by Perl and Python's laid-back OO culture of "you can do > it if you feel like it but don't have to". Well, that is the case with whatever general-purpose programming language. You have virtually an infinite number of ways to do things, and most of them are not appropriate. The thing with Python is, in my opinion, that it wants to put all the power in your hands to do whatever you want in the fastest way possible. If I want to do something, I don't declare a class that knows how to do it, then create it and invoke the right method, I just put the code to do what I want to do. If, in between, I find that one class would make my life easier, I declare it just in place and go on. If I find repetitive tasks I can declare functions, but I won't go for a class immediately. Why do you think putting code into functions is not encapsulating? > While you could do all manner > of nasty hacks in C++ I worked with people who carefully avoided this. Well done, but messes you can do in whatever language. Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: What are modules really for?
Neil Benn wrote: > Tito wrote: >> N.Davis wrote: >>> Functions existing in a module? Surely if "everything is an object" >>> (OK thats Java-talk but supposedly Python will eventually follow this >>> too) then there should be nothing in a module thats not part of a class. >> >> Well, all data in a Python program are objects, in the sense that they >> have an identity on their own, and variables are only references to them. >> >> Why is it necessary to have all code into classes for meeting the >> "everything is an object" panacea? >> > If you don't have a class how can you combine functionality with data > and hold state - that is one of the points of a class. Yes, of course. You don't have OO if you cannot define your own classes. I only said that I don't find it necessary to have all code confined in classes. > Python has the > added concept that if you don;t need this you can create functions with > no associations - lots of languages do that but Java, Eiffel, C#, etc > don't have that concept. You need to make a difference between > everything is an object and everything is a class here. A programmer > coming from Java/C# wouldn't think about that. >> >>> Even a static method is simply a class function that operates on the >>> "collection of all instances" rather than a single instance. >> >> That is not true. A static method knows nothing about the instances of >> a class, unless you do it your own. Besides, it will work whether you >> have created instances of the class or not. >> >> So, a static method is just a global method declared withing a class, >> which just serves as a namespace for it. >> > In java, a static method is the same as a class method in Python, you > can then use the cls param to access class attributes (that is what teh > OP implied). However a static method can help with namespacing. What > looks better : > > initialise_local_message_router() > objPublisher = get_publisher_from_local_router('bob') > > or > > LocalRouter.initialise() > objPublisher = LocalRouter.getPublisher('bob') > >IMHO, the second case makes much more sense that a floating function > which makes things less expressive. Yes. So we agree on the fact that the class' name serves as a namespace. >>> Related classes in the same file? Be careful. Doesn't anything >>> "knowing" about anything else compromise encapsulation? Why would >>> properly-designed classes have such a close relationship? >> >> Question back: why do you think having classes defined in the same >> file compromises encapsulation? Classes don't know more about each >> other for the fact of being written into the same file. Anyway, in >> Python, classes know always too much from each other, don't they?, as >> there are no access modifiers for class attributes. >> > If I want to change one class and replace the file on the install then I > need to put a whole bunch of classes on - increasing the change of > making a mistake. Sorry, I don't understand the previous sentence. What is meant by "replace on the install"? And by "to put a whole bunch of classes on"? > Module scoping exists with globals, also you have the > convetnion of creating classes with the name of _XXX to mean module > level only. Do you mean that _XXX server as an access modifier among modules? Yes, that can be a reason to split definitions of classes into different modules. >>> Having back in the day worked on big real-time systems where being >>> very strict about encapsulation was a god-send for fighting >>> complexity, I feel unnerved by Perl and Python's laid-back OO culture >>> of "you can do it if you feel like it but don't have to". >>> >> >> >> Well, that is the case with whatever general-purpose programming >> language. You have virtually an infinite number of ways to do things, >> and most of them are not appropriate. >> >> > Some languages are more strict than others - yes you need covnentions > but you will need more conventions in a less strict language. The > upside is that, if you are careful, you can leverage the added oosness > to implement features which would be a pain in other languages. > Enterprise systems have different objectives than a cgi script or single > client install stuff. It's a judgement call. > >> The thing with Python is, in my opinion, that it wants to put all the >> power in your hands to do whatever
Re: What are modules really for?
> [1] 'aName' => public, '_aName' => protected, '__aName' => private I didn't know this one, as I am quite new to Python. Is it really general use? Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: What are modules really for?
bruno modulix wrote: > Magnus Lycka wrote: > >>N.Davis wrote: >> >> >>>Functions existing in a module? Surely if "everything is an object" >>>(OK thats Java-talk but supposedly Python will eventually follow this >>>too) >> >> >>int too? ;) > > > Yes, int too. I think he is talking about *Java* int. Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Can't simultaneously read/write from ossaudio dsp device
For an internet telephone application, I need to be able to read and write data to and from /dev/dsp simultaneously. I wrote some code and its not working. Anyone have any working code to do this? I am assuming my card is full duplex, it is a built-in sound card on a new dell 600m laptop, but I am not sure how to tell for sure. But I think the problem is not so much my sound card, but that I am making some fundamentally wrong assumption on the way to do this ;) Also I am definitely a newbie when it comes to audio coding, so any corrections or tips are welcome. Here is some test code that is failing for me from twisted.internet.task import LoopingCall from twisted.internet import reactor import os, sys, wave, audioop """ While playing the contents of test1.wav, talk into the mic and have the audio recorded into /tmp/out.wav """ def playnlisten_out(): audio = wavin.readframes(1024) stereoaudio = audioop.tostereo(audio, 2, 1, 1) dsp.write(stereoaudio) def playnlisten_in(): audio = dsp.read(640) wavout.write(audio) def both(): playnlisten_out() playnlisten_in() dsp = ossaudiodev.open('/dev/dsp', 'rw') wavin = wave.open("test1.wav", "r") wavout = wave.open("/tmp/out.wav", "w") both_loop = LoopingCall(both) both_loop.start(0.02) reactor.run() Actual behavior It fails with an error: dsp.write(stereoaudio) exceptions.IOError: [Errno 19] No such device If I comment either playnlisten_out() or playnlisten_in() then the other function will work. They just don't work at the same time. Sys info box:~# lsmod | grep -i audio i810_audio 30356 1 ac97_codec 16908 1 i810_audio soundcore 9824 3 snd,i810_audio box:~# lsmod | grep -i snd snd_pcm_oss48168 0 snd_mixer_oss 16640 1 snd_pcm_oss snd_intel8x0m 18632 0 snd_intel8x0 33068 0 snd_ac97_codec 59268 2 snd_intel8x0m,snd_intel8x0 snd_pcm85412 3 snd_pcm_oss,snd_intel8x0m,snd_intel8x0 snd_timer 23172 1 snd_pcm snd_page_alloc 11144 3 snd_intel8x0m,snd_intel8x0,snd_pcm gameport4736 1 snd_intel8x0 snd_mpu401_uart 7296 1 snd_intel8x0 snd_rawmidi23232 1 snd_mpu401_uart snd_seq_device 7944 1 snd_rawmidi snd50148 10 snd_pcm_oss,snd_mixer_oss,snd_intel8x0m,snd_intel8x0,snd_ac97_codec,snd_pcm,snd_timer,snd_mpu401_uart,snd_rawmidi,snd_seq_device soundcore 9824 3 snd,i810_audio box:~# lspci :00:1f.5 Multimedia audio controller: Intel Corp. 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) AC'97 Audio Controller (rev 01) box:~# uname -a Linux box 2.6.7-1-386 #1 Thu Jul 8 05:08:04 EDT 2004 i686 GNU/Linux -- http://mail.python.org/mailman/listinfo/python-list
Re: Can't simultaneously read/write from ossaudio dsp device
I got it working! There was another thread http://tinyurl.com/pebqc on this group where someone had the same problem. I changed my code to the following: from twisted.internet.task import LoopingCall from twisted.internet import reactor import os, sys, wave, audioop """ While playing the contents of test1.wav, talk into the mic and have the audio recorded into /tmp/out.wav """ def playnlisten_out(): audio = wavin.readframes(1024) stereoaudio = audioop.tostereo(audio, 2, 1, 1) dspout.write(stereoaudio) def playnlisten_in(): audio = dspin.read(640) wavout.write(audio) def both(): playnlisten_out() playnlisten_in() dspout = ossaudiodev.open('/dev/dsp', 'w')# 2 file handles dspin = ossaudiodev.open('/dev/dsp', 'r') wavin = wave.open("test1.wav", "r") wavout = wave.open("/tmp/out.wav", "w") both_loop = LoopingCall(both) both_loop.start(0.02) reactor.run() and it worked as expected. I did not need to mess around with /dev/mixer. -- http://mail.python.org/mailman/listinfo/python-list
linear algebric equations
Regarding the solution of linear algebraic equations I noticed a big difference in the computation time in Python compared to the old fortran language. I have compared both the linelg and lapack.dgesv-lapack.zgesv modules with the fortan: dgelg and f04adf. The difference in computation time is enormous: for example for 430 degrees of freedom it is about 24 min in Python versus about 1 sec in fortran. Is it possible to get better performance in Python? Thanks in advance Tito Sano Roma Italy Cell: 339 6903895 -- https://mail.python.org/mailman/listinfo/python-list