Problems with incrementing location of widget creation when function is called - python

I am trying to make this function create a label and 2 buttons respectively, and each time this function is called, 3the widgets will be created on the next row(directly under the previous 3 widgets).
Howver, I am not sure why the items keep being created on the same row (effectively overlapping over the same one when the function is called) despite the counter being incremented.
def fetch_quick(self, entries):
for entry in entries:
text = entry[1].get()
print(text)
exec("app._framea" + str(self._qqq+7) + "= tk.Frame(app._master, bg='white')")
exec("app._framea" + str(self._qqq+7) + ".grid(row=" + str(self._qqq+6) + ")")
exec("self.queue_entry_quick" + str(self._qqq) + " = Label(app._framea" + str(self._qqq+7) + ", text='1 '+text +' 0 a few seconds ago')")
exec("self.queue_entry_quick" + str(self._qqq) + ".grid(row=" + str(self._qqq) + ")")
exec("self._Button" + str(self._qqq) + " = Button(app._framea" + str(self._qqq+7) + ", text = self._qqq, width = 2, command=app._framea" + str(self._qqq+7) + ".destroy, bg='red')")
exec("self._Buttonb" + str(self._qqq) + " = Button(app._framea" + str(self._qqq+7) + ", text = self._qqq, width = 2, command=app._framea" + str(self._qqq+7) + ".destroy, bg='green')")
exec("self._Button" + str(self._qqq) + ".grid(row=" + str(self._qqq) + ", column=1)")
exec("self._Button" + str(self._qqq) + ".bind('<Button-1>',self.call)")
exec("self._Buttonb" + str(self._qqq) + ".grid(row=" + str(self._qqq) + ", column=2)")
exec("self._Buttonb" + str(self._qqq) + ".bind('<Button-1>',self.call)")
abcd.append(text)
self._qqq += 1
print(self._qqq)
I think it might be the issue of the widgets' creation location(on the grid) being pre-set to row 0 for all of the widgets and thus it will not update self._qqq for each time the function is called. If this is the case, I am still unsure of what to do about it

Related

What transformation does `simplify` apply when both `powsimp` and `collect` do nothing?

I have the following expression and want to group it by powers of the variable e:
from sympy import symbols
e = symbols('e')
expr = (
0.363635891123757*e
+ 1.27444227529689*(0.006290933064*e + 0.526290933064)*(0.12408152*e + 0.12408152)
+ 2.76494336639746*(0.0483917928*e + 1.0483917928)*(0.12408152*e + 0.12408152)
+ 1.27444227529689*(0.12408152*e + 0.12408152)*(
0.012581866128*e
+ 0.39*(0.006290933064*e + 0.526290933064)*(0.12408152*e + 0.12408152)
+ 0.922581866128
)
+ 2.76494336639746*(0.12408152*e + 0.12408152)*(
0.0967835856*e
+ 0.39*(0.0483917928*e + 1.0483917928)*(0.12408152*e + 0.12408152)
+ 1.0967835856
)
+ 1.63807816642065
)
From their descriptions both powsimp and collect should work, however they return the expression unmodified. When I apply simplify then the expression gets simplified as expected. So I'm wondering what other type of transformation gets applied during the call to simplify?
from sympy.simplify import simplify, powsimp, collect
print(f'{powsimp(expr) = }')
print(f'{collect(expr, e) = }')
print(f'{simplify(expr) = }')
This produces the following output:
powsimp(expr) = 0.363635891123757*e + (0.0080174310478246*e + 0.670727414202207)*(0.12408152*e + 0.12408152) + (0.12408152*e + 0.12408152)*(0.13380056649044*e + 2.8987439328879) + (0.158134734671097*e + 0.158134734671097)*(0.012581866128*e + (0.00245346389496*e + 0.20525346389496)*(0.12408152*e + 0.12408152) + 0.922581866128) + (0.343078375616514*e + 0.343078375616514)*(0.0967835856*e + (0.018872799192*e + 0.408872799192)*(0.12408152*e + 0.12408152) + 1.0967835856) + 1.63807816642065
collect(expr, e) = 0.363635891123757*e + (0.0080174310478246*e + 0.670727414202207)*(0.12408152*e + 0.12408152) + (0.12408152*e + 0.12408152)*(0.13380056649044*e + 2.8987439328879) + (0.158134734671097*e + 0.158134734671097)*(0.012581866128*e + (0.00245346389496*e + 0.20525346389496)*(0.12408152*e + 0.12408152) + 0.922581866128) + (0.343078375616514*e + 0.343078375616514)*(0.0967835856*e + (0.018872799192*e + 0.408872799192)*(0.12408152*e + 0.12408152) + 1.0967835856) + 1.63807816642065
simplify(expr) = 0.000851550024540092*e**3 + 0.0759270659579648*e**2 + 1.42522479477876*e + 2.62459155414223
P.S.: For my use case, this expression is part of a larger expression which I cannot transform via simplify because I get the error described here.
$ conda list | grep sympy
sympy 1.8 py39hf3d152e_0 conda-forge
In general you need to use expand before collect although in this case expand is enough:
In [7]: expr.expand()
Out[7]:
3 2
0.000851550024540092⋅e + 0.0759270659579648⋅e + 1.42522479477876⋅e + 2.62459155414223

How to retrieve only those elements of list which matches user input? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
i need to take input from user and only that group of words should return to me where the input string occurs. For example if i search for people then only those group of words where people appears should be retrieved as output.
here is my sample output:
[(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"'), (4, '0.675*"unto" + -0.425*"shall" + -0.335*"indeed" + 0.214*"thou" + 0.180*"thee" + 0.161*"lord" + -0.105*"said" + 0.099*"hath" + -0.075*"upon"'), (5, '-0.760*"said" + 0.356*"indeed" + 0.261*"upon" + 0.157*"would" + -0.130*"shall" + 0.109*"earth" + -0.108*"allah" + 0.105*"lord" + 0.100*"truth" + 0.096*"good"')
Here is my expected output:
[(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"')]
Use a function with two parameter , one is your desired string and
second is your list :
Data is :
data=[(0,
'0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'),
(1,
'0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'),
(2,
'-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'),
(3,
'-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"'),
(4,
'0.675*"unto" + -0.425*"shall" + -0.335*"indeed" + 0.214*"thou" + 0.180*"thee" + 0.161*"lord" + -0.105*"said" + 0.099*"hath" + -0.075*"upon"'),
(5,
'-0.760*"said" + 0.356*"indeed" + 0.261*"upon" + 0.157*"would" + -0.130*"shall" + 0.109*"earth" + -0.108*"allah" + 0.105*"lord" + 0.100*"truth" + 0.096*"good"')]
Detailed solution :
def search_strin(stri,list_1):
final_list=[]
for tup in list_1:
for item in tup:
if isinstance(item,str):
if stri in item:
final_list.append(tup)
return final_list
print(search_strin('people',data))
output:
Its returning only those group which have 'people' in string.
[(0, '0.897*"allah" + 0.120*"indeed" + 0.117*"lord" + 0.110*"said" + 0.101*"people" + 0.093*"upon" + 0.083*"shall" + 0.082*"unto" + 0.072*"believe" + 0.070*"earth"'), (1, '0.495*"lord" + 0.398*"said" + -0.377*"allah" + 0.252*"shall" + 0.241*"people" + 0.236*"unto" + 0.195*"indeed" + 0.131*"upon" + 0.117*"come" + 0.109*"thou"'), (2, '-0.682*"lord" + 0.497*"shall" + 0.350*"unto" + 0.125*"thee" + 0.125*"thou" + -0.098*"indeed" + -0.092*"said" + 0.092*"come" + 0.091*"people" + 0.080*"truth"'), (3, '-0.615*"shall" + 0.520*"people" + -0.395*"lord" + 0.259*"said" + 0.227*"indeed" + 0.103*"would" + 0.081*"sent" + 0.078*"among" + -0.059*"deeds" + -0.053*"good"')]
Just for fun one line solution if you want to try:
search='people'
print([tup for tup in data for item in tup if isinstance(item,str) if search in item])
As you commented you are getting empty list , You should check that you are passing correct list. You can check here live running code :

Python comma and divide an integer

def SetHP(self, hpPercentage, curHP, maxHP):
if not self.hpGauge.IsShow():
self.SetSize(200 + 7*self.nameLength, 70)
self.hpGauge.Show()
self.UpdatePosition()
self.hpGauge.SetPercentage(hpPercentage, 100)
strCurHP = str(curHP)
strMaxHP = str(maxHP)
self.broadCastHP.SetText(strCurHP + " / " + strMaxHP)
Example output is: 8993 / 18782
I see some questions like that, but all of them was about "float".
I want to make these integers like that:
8,9K / 18,7K
What is the "proper" way to do that?
Try this function:
def HPformat(str):
if len(str)==5:
newstr=str[0]+str[1] + ','+ str[2] + 'K'
return newstr
elif len(str)==4:
newstr=str[0]+','+ str[1] + 'K'
return newstr
And replace your final line of code with the function:
def SetHP(self, hpPercentage, curHP, maxHP):
if not self.hpGauge.IsShow():
self.SetSize(200 + 7*self.nameLength, 70)
self.hpGauge.Show()
self.UpdatePosition()
self.hpGauge.SetPercentage(hpPercentage, 100)
strCurHP = str(curHP)
strMaxHP = str(maxHP)
self.broadCastHP.SetText(HPformat(strCurHP) + " / " + HPformat(strMaxHP))
Also, if you don't want to add a new function you could just do:
def SetHP(self, hpPercentage, curHP, maxHP):
if not self.hpGauge.IsShow():
self.SetSize(200 + 7*self.nameLength, 70)
self.hpGauge.Show()
self.UpdatePosition()
self.hpGauge.SetPercentage(hpPercentage, 100)
strCurHP = str(curHP)
strMaxHP = str(maxHP)
newCurHP = strCurHP[0] + ',' + strCurHP [1] + 'K'
newMaxHP = strMaxHP[0] + strMaxHP[1] + ',' + strMaxHP[2] + 'K'
self.broadCastHP.SetText(newCurHP + " / " + newMaxHP)

Python - key error when using "if in dict"

I am receiving the following error when running a script to parse contents of an XML file.
if iteration.findtext("Iteration_query-def") in ecdict:
KeyError: 'XLOC_000434'
I was under the impression that using "if in dict" would mean that if the key is not found in the dictionary, the script will continue past the if statement and proceed with the rest of the code. Below is the problematic section of the code I am using. I realise this is quite a basic question, but I am unsure what else I can say, and I don't understand why I am receiving this error.
import xml.etree.ElementTree as ET
tree = ET.parse('507.FINAL_14.2.14_2_nr.out_fmt5.out')
blast_iteration = tree.find("BlastOutput_iterations")
for iteration in blast_iteration.findall("Iteration"):
query = iteration.findtext("Iteration_query-def").strip().strip("\n")
if query in score:
continue
if iteration.findtext("Iteration_message") == "No hits found":
if iteration.findtext("Iteration_query-def") in tair:
tairid = tair[iteration.findtext("Iteration_query-def")][0]
tairdes = tair[iteration.findtext("Iteration_query-def")][1]
else:
tairid = "-"
tairdes = "-"
goterms = ""
ecterms = ""
if iteration.findtext("Iteration_query-def") in godict:
for x in godict[iteration.findtext("Iteration_query-def")][:-1]:
goterms = goterms + x + ";"
goterms = goterms + godict[iteration.findtext("Iteration_query-def")][-1]
else:
goterms = "-"
if iteration.findtext("Iteration_query-def") in ecdict:
for x in ecdict[iteration.findtext("Iteration_query-def")][:-1]:
ecterms = ecterms + x + ";"
ecterms = ecterms + ecdict[iteration.findtext("Iteration_query-def")][-1]
else:
ecterms = "-"
if iteration.findtext("Iteration_query-def") in godescr:
desc = godescr[iteration.findtext("Iteration_query-def")]
else:
desc = "-"
n += 1
p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
print p
Hope you can help,
Thanks.
edit: I should say that godict and ecdict were previously created as follows - I can submit the entire code if needs be:
godict = {}
ecdict = {}
godescr = {}
f = open("507.FINAL_14.2.14_2_nr.out_fmt5.out.annot")
for line in f:
line = line.split("\t")
if len(line) > 2:
godescr[line[0]] = line[2]
line[1] = line[1].strip("\n")
if line[1].startswith("EC"):
if line[0] in ecdict:
a = ecdict[line[0]]
a.append(line[1])
ecdict[line[0]] = a
else:
ecdict[line[0]] = [line[1]]
else:
if line[0] in godict:
a = godict[line[0]]
a.append(line[1])
godict[line[0]] = a
else:
godict[line[0]] = [line[1]]
Traceback:
Traceback (most recent call last):
File "2d.test.py", line 170, in <module>
p = "PvOAK_up"+str(n) + "\t" + tranlen[iteration.findtext("Iteration_query-def")] + "\t" + orflen[iteration.findtext("Iteration_query-def")] + "\t" + "-" + "\t" + "-" + "\t" + tairid + "\t" + tairdes + "\t" + goterms + "\t" + ecterms + "\t" + desc + "\t" + str(flower[query][2]) + "\t" + str('{0:.2e}'.format(float(flower[query][1]))) + "\t" + str('{0:.2f}'.format(float(flower[query][0]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][2]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][1]))) + "\t" + str('{0:.2f}'.format(float(leaf[query][0])))
KeyError: 'XLOC_000434'

integrate cos(x)*cos(2x)*...*cos(mx) via SAGE

I'm going to find $I_m=\int_0^{2\pi} \prod_{k=1}^m cos(kx){}dx$, where $m=1,2,3\ldots$
Simple SAGE code:
x=var('x')
f = lambda m,x : prod([cos(k*x) for k in range(1,m+1)])
for m in range(1,15+1):
print m, numerical_integral(f(m,x), 0, 2*pi)[0],integrate(f(m,x),x,0,2*pi).n()
Output:
1 -1.47676658757e-16 0.000000000000000
2 -5.27735962315e-16 0.000000000000000
3 1.57079632679 1.57079632679490
4 0.785398163397 0.785398163397448
5 -2.60536121164e-16 0.000000000000000
6 -1.81559273097e-16 0.000000000000000
7 0.392699081699 0.392699081698724
8 0.343611696486 0.147262155637022
9 -1.72448482421e-16 0.294524311274043
10 -1.8747663502e-16 0.196349540849362
11 0.214757310304 0.312932080728671
12 0.190213617698 0.177941771394734
13 -1.30355375996e-16 0.208621387152447
14 -1.25168280013e-16 0.0859029241215959
15 0.138441766107 0.134223318939994
As you can see numerical answer is right, but result of integrate(...) is right for $m=1,2,\ldots,7$ and then there is some bug.
We can print indefinite integral:
for m in range(7,11+1):
print 'm=',m
print 'Indef_I_m=',integrate(f(m,x),x)
And Output:
m = 7
Indef_I_m = 1/16*x + 1/16*sin(2*x) + 1/32*sin(4*x) + 7/384*sin(6*x) +
7/512*sin(8*x) + 3/320*sin(10*x) + 5/768*sin(12*x) + 5/896*sin(14*x) +
1/256*sin(16*x) + 1/384*sin(18*x) + 1/640*sin(20*x) + 1/704*sin(22*x) +
1/1536*sin(24*x) + 1/1664*sin(26*x) + 1/1792*sin(28*x)
m = 8
Indef_I_m = 3/128*x + 5/256*sin(2*x) + 1/32*sin(3*x) + 5/512*sin(4*x) +
5/768*sin(6*x) + 1/256*sin(8*x) + 1/256*sin(10*x) + 1/256*sin(12*x) +
1/256*sin(14*x) + 1/256*sin(16*x) + 7/2304*sin(18*x) + 3/1280*sin(20*x)
+ 5/2816*sin(22*x) + 1/768*sin(24*x) + 3/3328*sin(26*x) +
1/1792*sin(28*x) + 1/1920*sin(30*x) + 1/4096*sin(32*x) +
1/4352*sin(34*x) + 1/4608*sin(36*x) + 3/32*sin(x)
m = 9
Indef_I_m = 3/64*x + 3/128*sin(2*x) + 23/768*sin(3*x) + 3/256*sin(4*x) +
3/640*sin(5*x) + 1/128*sin(6*x) + 5/1792*sin(7*x) + 5/2304*sin(9*x) +
3/2816*sin(11*x) + 1/832*sin(13*x) + 1/1280*sin(15*x) + 3/4352*sin(17*x)
+ 5/4864*sin(19*x) + 1/1344*sin(21*x) + 3/2944*sin(23*x) +
7/6400*sin(25*x) + 1/1152*sin(27*x) + 3/3712*sin(29*x) +
5/7936*sin(31*x) + 1/2112*sin(33*x) + 3/8960*sin(35*x) +
1/4736*sin(37*x) + 1/4992*sin(39*x) + 1/10496*sin(41*x) +
1/11008*sin(43*x) + 1/11520*sin(45*x) + 23/256*sin(x)
m = 10
Indef_I_m = 1/32*x + 1/64*sin(2*x) + 17/512*sin(3*x) + 1/128*sin(4*x) +
7/2560*sin(5*x) + 1/192*sin(6*x) + 3/1792*sin(7*x) + 1/1152*sin(9*x) +
5/5632*sin(11*x) + 3/6656*sin(13*x) + 1/2560*sin(15*x) +
5/8704*sin(17*x) + 3/9728*sin(19*x) + 1/2688*sin(21*x) +
1/2944*sin(23*x) + 1/6400*sin(25*x) + 1/4608*sin(27*x) +
3/14848*sin(29*x) + 3/15872*sin(31*x) + 5/16896*sin(33*x) +
3/8960*sin(35*x) + 3/9472*sin(37*x) + 1/3328*sin(39*x) +
5/20992*sin(41*x) + 1/5504*sin(43*x) + 1/7680*sin(45*x) +
1/12032*sin(47*x) + 1/12544*sin(49*x) + 1/26112*sin(51*x) +
1/27136*sin(53*x) + 1/28160*sin(55*x) + 13/128*sin(x)
m = 11
Indef_I_m = 51/1024*x + 53/2048*sin(2*x) + 13/768*sin(3*x) + 53/4096*sin(4*x) +
13/1536*sin(6*x) + 1/2048*sin(8*x) + 1/2560*sin(10*x) + 1/3072*sin(12*x)
+ 5/14336*sin(14*x) + 1/4096*sin(16*x) + 5/18432*sin(18*x) +
1/4096*sin(20*x) + 1/5632*sin(22*x) + 5/24576*sin(24*x) +
5/26624*sin(26*x) + 5/28672*sin(28*x) + 1/5120*sin(30*x) +
3/16384*sin(32*x) + 5/34816*sin(34*x) + 1/9216*sin(36*x) +
5/38912*sin(38*x) + 1/10240*sin(40*x) + 1/10752*sin(42*x) +
3/22528*sin(44*x) + 3/23552*sin(46*x) + 1/8192*sin(48*x) +
3/25600*sin(50*x) + 5/53248*sin(52*x) + 1/13824*sin(54*x) +
3/57344*sin(56*x) + 1/29696*sin(58*x) + 1/30720*sin(60*x) +
1/63488*sin(62*x) + 1/65536*sin(64*x) + 1/67584*sin(66*x) +
13/256*sin(x)
so for $m=7$ answer is right compare with Indef_I_7 via WolframAlpha
and for $m=8$ answer is incorrect Indef_I_8 via WolframAlpha
There should be Indef_I_8=$\frac{7x}{128}+\ldots$ and no $\sin(x)$, $\sin(3x)$ in summation, only $\sin(2k)$ for $k=1,2,3,\ldots 18$
Sorry for volumetric calculations !
The question is - Am I right that it is the bug in the symbolic integration?
Well, apparently setting algorithm='mathematica_free' solved the issue; this is probably a bug in the default algorithm used bye SAGE ('maxima').
So the answer is - Yes. It is a bug in algorithm='maxima', so use algorithm='mathematica_free' (def new function to find definite integral) or simplify_full() for such product of cos(kx) and than integrate.
f(8,x).simplify_full().integral(x,0,2pi) == 7/64pi and
(7/64*pi).n() == 0.343611696486384 is correct

Categories

Resources