I am trying to get the audio devices names from using PyQt4 framework in Python.
The code is as follows:
from PyQt4 import QtCore, QtGui, QtMultimedia
temp_list=()
x=()
for i in range(5):
temp_list=QtMultimedia.QAudioDeviceInfo.availableDevices(QtMultimedia.QAudio.AudioInput)
x=QtMultimedia.QAudioDeviceInfo.deviceName
print temp_list
print x
yet somehow I can't get the device names. Not too sure how to proceed from here.
It seems from your example code that you don't have much experience of using Python. I would advise you to work through some tutorials before proceeding any further. The Beginners Guide on the Python wiki is a good place to start.
I would also advise you to make full use of Python's interactive mode when trying to work out how the Qt APIs work. With the use of print, you can answer basic questions very quickly and easily. Here's an example session:
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtMultimedia
>>> devices = QtMultimedia.QAudioDeviceInfo.availableDevices(QtMultimedia.QAudio.AudioInput)
>>> print(devices)
[<PyQt4.QtMultimedia.QAudioDeviceInfo object at 0x00CC5C30>, <PyQt4.QtMultimedia.QAudioDeviceInfo object at 0x00CC5C70>]
>>> for device in devices:
... print(device.deviceName())
...
Intel(r) Integrated Audio
default
Just another way of doing it also, sounddevice lib is nice and easy to use !
py -m pip install sounddevice
import sounddevice as sd
def query():
a = sd.query_devices()
print(a)
query()
Related
In Python 2.7, how can I make the IDLE app use \__future__ division without typing from \__future__ import division manually every time I start IDLE?
If I put from \__future__ import division at the top of my .idlestartup file it is ignored, even though the other things in .idlestartup get executed. For example:
~> cat >.idlestartup
from __future__ import division
print("Executing .idlestartup")
~> idle -s
Here's what my IDLE window looks like after I try dividing:
Python 2.7.8 |Anaconda 2.1.0 (x86_64)| (default, Aug 21 2014, 15:21:46)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>>
Executing .idlestartup
>>> 2/3
0
>>>
I am using Mac OS X 10.9.5 Mavericks (also had the same problem on earlier versions of OS X). Note that the command line version above was included to make it easier to show what I'm talking about, but the version I'm more interested in is running the IDLE app from the GUI.
The solution suggested by Ashwini Chaudhary below worked for running the Anaconda version from the command line but not for running the IDLE app.
I was finally able to get future division working automatically in the IDLE app by adding "sys.argv.insert(1, '-Qnew')" to /Applications/IDLE.app/Contents/MacOS/IDLE. Both that and Ashwini Chaudhary's solution below seem brittle. I wonder if there is a cleaner way.
Adding the __future__ statement at the top of /usr/lib/python2.7/idlelib/PyShell.py did the job for me.
I am on Ubuntu, the path may vary for other OS:
>>> import idlelib
>>> idlelib.PyShell.__file__
'/usr/lib/python2.7/idlelib/PyShell.py'
I use Windows 7, 64bit, with installed Python 2.7 and Oracle instant client 10.2.0.3.
I try to set up connection with Oracle database from python. In order to do this, I download cx_Oracle-5.1.2-10g.win32-py2.7.msi and install it.
upd. it's an error. i meant cx_Oracle-5.1.2-10g.win-amd64-py2.7.msi
Then I try to connect use code like this
import cx_Oracle
ad = cx_Oracle.makedsn('127.0.0.1', '1521', 'XE')
con = cx_Oracle.connect('user', 'password', ad)
And check that connection is set up correctly by selecting some rows from database table.
And at this moment happens something interesting.
I perform described actions in three different environments: Sublime Text 3, Python Console and ipython.
The problem is in ST3 and in ipython this chunk of code silently crashes on line with cx_Oracle.connect (I checked that with print statement in different places).
But in python console and idle it works just fine. Moreover sometimes it works properly in ipython, but I cannot understand why and when. In ST3 it never works.
ST shows message [Finished in 0.4s with exit code 3221226356]
To demonstrate behavior in python and ipython console I attach copypaste of simple case from cmd. It just exits from ipython.
C:\Users\Alexey>python
Python 2.7.7 (default, Jun 1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> cx_Oracle.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: ORA-12560: TNS:protocol adapter error
>>> exit()
C:\Users\Alexey>ipython
Python 2.7.7 (default, Jun 1 2014, 14:21:57) [MSC v.1500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 2.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import cx_Oracle
In [2]: cx_Oracle.connect()
C:\Users\Alexey>
Where is the problem? How can I solve it?
Thanks in advance.
PS. I tried to handle exception in ST3 and ipython such as
try:
cx_Oracle.connect()
except Exception as e:
print e
but script terminates on cx_Oracle.connect() and no message appears.
I tried this environment myself and did not experience the same behavior. Perhaps use faulthandler or gdb or some equivalent to figure out where the crash is taking place? cx_Oracle is still maintained (by me) so if you can find a bug I'll happily fix it! It may also be a problem with IPython or ST3 or in some interaction between these two and cx_Oracle. Since I can't replicate the problem, however, you'll need to provide a stack trace of some sort so we can proceed further.
I am trying to access the value of pi in Python 2.7, but it seems as if Python doesn't recognize math.pi. I am using IDLE, and when I try to print the value of math.pi, it says that "Math is not defined" or "math is not defined". I can't upgrade to the next version without risk, so is there a way to access pi in Python 2.7? Alternatively, is there another way to convert degrees to radians in Python 2.7 without dealing with Pi?
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.pi
3.141592653589793
Check out the Python tutorial on modules and how to use them.
As for the second part of your question, Python comes with batteries included, of course:
>>> math.radians(90)
1.5707963267948966
>>> math.radians(180)
3.141592653589793
To have access to stuff provided by math module, like pi. You need to import the module first:
import math
print (math.pi)
I'm new to python, and I'm trying to get a basic working understanding of changing the working directory and working with .py files. At the moment, I've changed the directory to where a couple of my .py files are, but when I go to import them, I get a time lag of over a minute for each one. Is that to be expected? Any idea what's going on? Here's what I'm doing:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import os
>>> os.getcwd()
'C:\\Python33'
>>> os.chdir("C:/Users/MarketResearch")
>>> import EarningsDownload
>>> import RatingsChanges
>>>
Sounds like your module is actually doing work when it is imported. This is (usually) a bad idea. What you should do instead is put your setup code into a function, say setup() or initialize(), and then call it after importing:
import earnings_download
earnings_download.initialize()
I'm trying to register the Firefox browser to run on Windows. According to the documentation for Webbrowser, "If the environment variable BROWSER exists, it is interpreted to override the platform default list of browsers, as a os.pathsep-separated list of browsers to try in order". I tried setting it, but it had no impact.
Z:\>SET BROWSER=C:\Program Files (x86)\Mozilla Firefox\firefox.exe %s
Z:\>python3
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (I
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import webbrowser
>>>
>>> webbrowser.open('http://google.com')
True
>>>
webbrowser.get("firefox") doesn't work either
How can I make webbrowser launch Firefox?
You might need to set static envionment viraibles, this you can do in the properties of my computer... Whether or not it will help is for you to figure out (worked over here..).
Another way to do this:
import webbrowser
webbrowser.get("open -a C:\\Program F~\\Mozilla Firefox\\firefox.exe %s")
webbrowser.open('http://google.com')