User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

make your python talk

Fri Nov 16, 2012 4:33 pm

That's the current "challenge" I posted on my blog, make a program to speak the Fibonacci numbers:

http://raspberry-python.blogspot.com/20 ... nacci.html

There are several ways to skin that cat.

Alternatively, if you are more the visual type, come up with the code to visualize the Fibonacci sequence, without using numbers.

Francois
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

User avatar
LetHopeItsSnowing
Posts: 357
Joined: Sat May 26, 2012 6:40 am
Location: UK
Contact: Website

Re: make your python talk

Fri Nov 16, 2012 6:52 pm

There are several options for implementing text to speech on the pi, there is a good article on the wiki about it.

I used google translate in my python talking twitter client. See my blog post for info and code. http://www.stuffaboutcode.com/2012/10/r ... itter.html

Theres a function in the program called speakPhrase or something like that, that should do what you want.

Lhis
"am I getting slower, or is stuff more complicated; either way I now have to write it down - stuffaboutcode.com"

User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

Re: make your python talk

Fri Nov 16, 2012 8:23 pm

Just to clarify, I'm not looking myself for a solution. It is part of the regular "quiz" and "challenge", "learning something new" that I offer my readers. It is a suggestion of something to figure out to expand Python skills.
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

Re: make your python talk

Tue Nov 20, 2012 2:04 am

And here is one solution. Although the espeak python module is not working correctly on the RPi, we work around that using the os module:

http://raspberry-python.blogspot.com/20 ... speak.html
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

toxibunny
Posts: 1382
Joined: Thu Aug 18, 2011 9:21 pm

Re: make your python talk

Tue Nov 20, 2012 5:57 am

Interesting stuff to think about, thanks. I like the idea of using Google translate - very sneaky... ;)
note: I may or may not know what I'm talking about...

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: make your python talk

Tue Nov 20, 2012 7:31 am

@fdion: `os.system()` has a bag full of problems. For instance it starts a shell and the command / argument you are giving is interpreted by that shell. That means you have to escape everything the shell might see as special syntax. That's why the `subprocess` module was introduced to the standard library. It's possible to run external programs without a shell process in between and giving those programs the arguments as is.

Code: Select all

import subprocess

def say(something, language='en', voice='f2'):
    subprocess.call(['espeak', '-v%s+%s' % (language, voice), something])

Code: Select all

while not self.asleep():
    sheep += 1

User avatar
fdion
Posts: 307
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
Contact: Website

Re: make your python talk

Tue Nov 20, 2012 4:32 pm

BlackJack wrote:@fdion: `os.system()` has a bag full of problems. For instance it starts a shell and the command / argument you are giving is interpreted by that shell.
And it is something that, if I had doubts about a Fibonacci series returning anything but numbers (let's say it could potentially return something evil like "; rm -rf &), would be a valid concern. I'm familiar with PEP324, it's been around for years, but in this case, I chose os.system.

Why? Because a single string is much more readable than multiple strings (subprocess requires a string for the executable and one for each argument). I use os.system, unless it is unfit for the purpose. I see many people trip on subprocess (and sh and other similar modules) because they forget it takes all strings, and they pass integer arguments, unknowingly.

Based on my experience in teaching others, it is best to focus on one thing, keep things as simple as one can, and introduce more complexity, gradually.

It is in fact one of the zen of python (PEP20), simple is better:

Code: Select all

$ python
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
I think it is also prettier, and as such feels more Pythonic. So these are my criteria. No doubt we choose something vs something else based on a complex pattern.

Similarly, you are aware of PEP3101, yet chose the older '%s' % var, no doubt based on your own criterion or criteria. Personally, I prefer .format since I don't have to specify the type and as such is more Pythonic to me.

Apple, Orange. Which is better?

Code: Select all

import subprocess

def say(something, language='en', voice='f2'):
    subprocess.call(['espeak', '-v%s+%s' % (language, voice), something])
Having said that, your above code is not a plug in replacement. It is expecting a string, while the Fibonacci series are numbers. One would have to use either say(str(b)) or modify further the subprocess.call in say().

In conclusion, subprocess.call or os.system are hacks, a workaround around a buggy espeak python module on the Raspberry Pi. Ultimately, a better solution would be to fix the module.

François
Francois
http://raspberry-python.blogspot.com - http://www.3dFutureTech.info - @f_dion

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: make your python talk

Tue Nov 20, 2012 11:13 pm

@fdion: I don't think of `os.system()` as simple. More as complicated — starts a shell in between the calling program and the external one — and complicated to use correctly — needs thinking about escaping for an unknown shell. The return value is useless because its not clear if it is coming from the shell or an external command.

The Fibonacci sequence does not contain anything that needs escaping, but the `say()` function is presented as a function that might look useful to let the Raspi speak anything. No warning about `os.system()`\s drawbacks.

Regarding the Zen: Simple is better than complex. But IMHO simple and robust is better than so simple it's almost broken. Splitting the arguments into a list isn't really complex. Another one is the one obvious way to do something — the `subprocess` module might be the one way to start processes. Citing its documentation: „This module intends to replace several other, older modules and functions, such as: os.system, os.spawn*, os.popen*, popen2.*, commands.*”

My excuse for using the ``%`` operator on strings is the PEPs number which implies Python 3 as the target and I have to write and maintain code for versions as old as 2.5 at work. `subprocess` is around since 2.4. :-)

Code: Select all

while not self.asleep():
    sheep += 1

emyr
Posts: 4
Joined: Thu Dec 06, 2012 5:43 pm

Re: make your python talk

Thu Dec 06, 2012 5:49 pm

you had to correct the python library for espeak, its a common problem with ubuntu too....

correct espeak behavior in python
sudo nano /usr/local/lib/python2.7/dist-packages/pyttsx-1.0.egg/pyttsx/drivers/espeak.py
after
EspeakDriver._defaultVoice = self.getProperty('voice')
add
if EspeakDriver._defaultVoice == None:
EspeakDriver._defaultVoice = "en"

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests