This question already has answers here:
What causes a Python segmentation fault?
(8 answers)
Closed 9 years ago.
How can I run the following program in python 2.7.3
import sys
sys.setrecursionlimit(2 ** 20)
def f(x):
if (x==0): return 0
else: return f(x-1)+1
print f(200000)
This code receives segmentation fault in Ubuntu.
The Python interpreter runs out of stack space. Like any other process in the same situation, it is getting killed by the operating system.
You could try increasing the OS stack size limit (ulimit -c).
A better approach might be to rewrite your code so that it does not require recursion this deep (your particular example can be trivially converted into iteration).
Related
This question already has answers here:
Using QProcess.finished() in Python 3 and PyQt
(1 answer)
Qt No such slot for the QProcess::finished() signal
(3 answers)
Closed 2 years ago.
I am currently trying to create an app with PyQt5 and the GUI keeps on hanging.
There were a couple of backend exe files that needed to be executed in the app using command prompt and I found out about the fact that waitForFinished blocks the entire GUI.
I tried going searching it everywhere but no luck. I just found that most of the related questions had just one answer which was to use signals instead of waitForFinished(-1). I wanted to learn how to use signals but nowhere could I find a tutorial of any sorts that could help me.
The code is:
process = QProcess()
cd = "babel.exe"
tem = " "
if not file.endswith("mol"):
tem = re.sub('\..*', '.mol', file)
filech = tem
ar = [file, filech, "-rcpb"]
process.start(cd, ar)
process.waitForFinished(-1)
process.kill
Thanks in advance..
This question already has an answer here:
Python - AttributeError: 'int' object has no attribute 'randint'
(1 answer)
Closed 5 years ago.
I am a beginner Python user. I was wondering why I am unable to use two randint() functions. I've done a little research on the issue, and it seems as though "I can only have one random module active at any time". Is there any way to get around this so I can have two "random values"?
Also, this is my first question ever, so criticism towards the question, sample code and answer are all welcome.
import random
random = random.randint(1, 5)
random_2 = random.randint(7, 11)
print(random)
print(random_2)
What's happening is that Python is confusing random (the number) with random (the module). Also, don't name the file random.py because it's going to search the module in the local directory, first.
And it's not a bug.
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 6 years ago.
I'm running files obtained from the web in my desktop's Python 3.4 install, and getting syntax errors, such as:
class MyClient(IBCpp.IBClient):
def setup(self):
self.stime=None
self.nextId=0
# self.symbol='STK.AAPL'
# self.symbol='CASH.EUR.USD'
self.symbol='FUT.ES.USD.201506'
self.state='first'
skipping a few defs which aren't pre-requisite, there's:
def orderStatus(self,orderId, status, filled, remaining, avgFillPrice,
permId, parentId, lastFillPrice, clientId, whyHeld):
"""
call back function of IB C++ API which update status or certain order
indicated by orderId
"""
print self.symbol, status, 'filled=',filled, 'remaining=', remaining'
for which the interpreter responds
print self.symbol, status, 'filled=',filled, 'remaining=', remaining
^
SyntaxError: invalid syntax
Is this perhaps due to a difference of 2.7 vs 3.4? I'm not sure what version the source file was created in. Source is from IBridgePy github repo
Check the Python 3 docs.
Python 3 uses () around the print contents - print ('python')
This question already has an answer here:
What's with the integer cache maintained by the interpreter?
(1 answer)
Closed 8 years ago.
While trying to answer a question about the use of the is keyword, I figured out that this code:
Script:
a = 123456
b = 123456
print a is b # True
Interactive mode:
>>> a = 123456
>>> b = 123456
>>> a is b
False
was giving different outputs on Python Interactive mode and when it was ran from a script.
From this answer:
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object.
So, I would expect that a is b returned True only for integers in the range [-5, 256]. But it is only true on Interactive mode, not when it is ran from a script.
Question: Why does a is b behaves differently on Interactive mode and when it's ran from script?
Note: Tested in Python 2.7 and Python 3
The difference is, how constants are handled. In interactive mode, there is no way to say, if a number constant is already there or not. But for compiled code, every constant is internally saved to a table, and duplicates are removed. But this is a implementation detail, and need not be true for every python version.
This question already has answers here:
Assigning Multiple Cores to a Python Program
(3 answers)
Closed 6 years ago.
I wrote this code in Python for the kinematic synthesis of a 4 bar linkage using Shu Radcliffe method. As you can see there are a couple of for intended loops that soon will become 3 o 4. It now takes about 40 minutes to complete the execution of the code on a workstation with 16 CPUs (32thread) and the CPU usage is very low while Python is running.
I am using numpy and math.
I was wondering if there is a way of running for loops using multiple CPUs and/or GPU (CUDA) in Python.
for th_12 in th_12_range:
for th_13 in th_13_range:
r_2=x_2-x_1*cos(th_12)+y_1*sin(th_12)
r_3=x_3-x_1*cos(th_13)+y_1*sin(th_13)
s_2=y_2-x_1*sin(th_12)-y_1*cos(th_12)
s_3=y_3-x_1*sin(th_13)-y_1*cos(th_13)
c_X_1_2_A=r_2*cos(th_12)+s_2*sin(th_12)-X_0_A*cos(th_12)\
-Y_0_A*sin(th_12)+X_0_A
c_Y_1_2_A=s_2*cos(th_12)-r_2*sin(th_12)+X_0_A*sin(th_12)\
-Y_0_A*cos(th_12)+Y_0_A
c_X_1_3_A=r_3*cos(th_13)+s_3*sin(th_13)-X_0_A*cos(th_13)\
-Y_0_A*sin(th_13)+X_0_A
c_Y_1_3_A=s_3*cos(th_13)-r_3*sin(th_13)+X_0_A*sin(th_13)\
-Y_0_A*cos(th_13)+Y_0_A
noto_2_A=r_2*X_0_A+s_2*Y_0_A-0.5*(r_2**2+s_2**2)
noto_3_A=r_3*X_0_A+s_3*Y_0_A-0.5*(r_3**2+s_3**2)
coeff_A = array ([[c_X_1_2_A,c_Y_1_2_A],[c_X_1_3_A,c_Y_1_3_A]])
v_noti_A = array ([noto_2_A,noto_3_A])
A=linalg.solve(coeff_A,v_noti_A)
c_X_1_2_B=r_2*cos(th_12)+s_2*sin(th_12)-X_0_B\
*cos(th_12)-Y_0_B*sin(th_12)+X_0_B
c_Y_1_2_B=s_2*cos(th_12)-r_2*sin(th_12)+X_0_B\
*sin(th_12)-Y_0_B*cos(th_12)+Y_0_B
c_X_1_3_B=r_3*cos(th_13)+s_3*sin(th_13)-X_0_B*cos(th_13)\
-Y_0_B*sin(th_13)+X_0_B
c_Y_1_3_B=s_3*cos(th_13)-r_3*sin(th_13)+X_0_B*sin(th_13)\
-Y_0_B*cos(th_13)+Y_0_B
noto_2_B=r_2*X_0_B+s_2*Y_0_B-0.5*(r_2**2+s_2**2)
noto_3_B=r_3*X_0_B+s_3*Y_0_B-0.5*(r_3**2+s_3**2)
coeff_B = array ([[c_X_1_2_B,c_Y_1_2_B],[c_X_1_3_B,c_Y_1_3_B]])
v_noti_B = array ([noto_2_B,noto_3_B])
B=linalg.solve(coeff_B,v_noti_B)
AC_i=((A[0]-x_1)**2+(A[1]-y_1)**2)**0.5
BC_i=((B[0]-x_1)**2+(B[1]-y_1)**2)**0.5
r1_i=((X_0_A-A[0])**2+(Y_0_A-A[1])**2)**0.5
r2_i=((A[0]-B[0])**2+(A[1]-B[1])**2)**0.5
r3_i=((B[0]-X_0_B)**2+(B[1]-Y_0_B)**2)**0.5
r4_i=((X_0_A-X_0_B)**2+(Y_0_A-Y_0_B)**2)**0.5
r=array([r1,r2,r3,r4])
g_1=amax(r)+amin(r)
g_2=sum(r)-g_1
if g_1<=g_2:
if amin(r) == (r1 or r3):
quad_iesimo=[r1,r2,r3,r4,th_12_t,th_13_t,AC,BC]
quad_gra.append(quad_iesimo)
have you looked at pycuda to access Nvidia‘s CUDA parallel computation ?