I have a file named myfile.txt given below:
&cntrl
pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion = t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc=5,
pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = 'as-1.out',
gfileEnout = 'as-1.en',
gfileInfo = 'as-1.info',
gfileStart = 'init.in',
gfileRst = 'as-1.rst',
gfileTraj = 'as-1.traj',
gfileVeloc = 'as-1.vel',
gfileQmen = 'as-1.qmen'
&end
Using the above given single file I want to create 10 files but I want to manipulate the values of last eight variables in a way that value of the variable in every new file changes as the number of file changes i.e if ten files are created then the value of last eight variables like gfileOut in tength file should be 'as-10.out'.
For doing this I have a code given below:
#!/usr/bin/python3
for i in range(10):
f = open('file' +str(i)+'.txt','w')
f.write("&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0,pion=t,"+"\n"+
"pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,"+"\n"+
"pinit = t, pnstep = 5000, pnscale = 100,"+"\n"+"pnstat = 5, pnout = 5,
pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,"+"\n"+
"msolute = t, nosa = 1, pichrg = t"+"\n"+'gfileOut = as-' +str(i)+ ".out,"+"\n"+
'gfileEnout = as-' +str(i)+ '.en,'+"\n"+'gfileInfo = as-' +str(i)+".info,"+"\n"+
'gfileStart = init' +str(i)+ ".in,"+"\n"+'gfileRst = as' +str(i)+ ".rst,"+"\n"+
'gfileTraj = as' +str(i)+ ".traj,"+"\n"
+'gfileVeloc = as' +str(i)+ ".vel,"+"\n"+'gfileQmen = as' +str(i)+ '.qmen'+"\n"+"&end ")
f.close()
The above given code produces right output but I want a way to read myfile.txt and change the values of last eight variables as mentioned above and then use this file to create ten new files.
str.format handles the inserts into the string to be written to each of the files.
# Write the files.
for i in range(1, 11):
with open('file' +str(i)+ '.txt','w') as f:
f.write(
('&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion=t,\n'
'pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,\n'
'pinit = t, pnstep = 5000, pnscale = 100,\n'
'pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,\n'
'msolute = t, nosa = 1, pichrg = t\n'
'gfileOut = as-{index}.out,\n'
'gfileEnout = as-{index}.en,\n'
'gfileInfo = as-{index}.info,\n'
'gfileStart = init{index}.in,\n'
'gfileRst = as{index}.rst,\n'
'gfileTraj = as{index}.traj,\n'
'gfileVeloc = as{index}.vel,\n'
'gfileQmen = as{index}.qmen\n'
'&end ').format(index=i))
Note: The string contains {index} which is replaced by the value of i that range(1, 10) sets.
Edit: Re-done post due to misunderstanding the details of the question alerted by the 1st comment. Sorry.
Edit: Looked at need to read from file, so this may help.
template.txt:
&cntrl pTopt = 298.15, pdens = 0.997, prcut = 12.0, pion=t,
pihot = t, prQM = 5.5, prSM = 5.3, prQI=3.0, piguess = f,
pinit = t, pnstep = 5000, pnscale = 100,
pnstat = 5, pnout = 5, pnrst = 5, pioutc = t, pioutv = t, pnoutc = 5, pnoutv = 5,
msolute = t, nosa = 1, pichrg = t
gfileOut = as-{index}.out,
gfileEnout = as-{index}.en,
gfileInfo = as-{index}.info,
gfileStart = init{index}.in,
gfileRst = as{index}.rst,
gfileTraj = as{index}.traj,
gfileVeloc = as{index}.vel,
gfileQmen = as{index}.qmen
&end
main script:
# Read template file.
with open('template.txt') as r:
content = r.read()
# Write the files.
for i in range(1, 11):
with open('file' +str(i)+ '.txt','w') as f:
f.write(content.format(index=i))
Related
In this simple calculator GUI, I'm creating a frame template using classes. The frame has 2 labels, 2 entry boxes, and a button. I'd like the button to run a specific command depending on the function_call variable passed when initializing - but this doesn't work. The two_points function should be called for the first object, and one_point should be called for the second. How do I dynamically change which command is called based on which object I'm using? Thank you for taking the time to read this.
from tkinter import *
root = Tk()
root.title("Simple Slope Calculator")
class Slope_Calc:
# Variable info that changes within the frame
def __init__(self, master, num_1, num_2, frame_name, label_1_name, label_2_name, function_call):
self.num_1 = int(num_1)
self.num_2 = int(num_2)
self.frame_name = frame_name
self.label_1_name = label_1_name
self.label_2_name = label_2_name
self.function_call = function_call
# Frame template
self.frame_1 = LabelFrame(master, text = self.frame_name, padx = 5, pady = 5)
self.frame_1.grid(row = self.num_1, column = self.num_2, padx = 10, pady = 10)
self.label_1 = Label(self.frame_1, text = self.label_1_name)
self.label_1.grid(row = 0, column = 0)
self.entry_1 = Entry(self.frame_1)
self.entry_1.grid(row = 0, column = 1)
self.label_2 = Label(self.frame_1, text = self.label_2_name)
self.label_2.grid(row = 1, column = 0)
self.entry_2 = Entry(self.frame_1)
self.entry_2.grid(row = 1, column = 1)
self.calc_button = Button(self.frame_1, text = "Calculate", command = self.function_call) # This is what doesn't work
self.calc_button.grid(row = 1, column = 2, padx = 5)
# Strips string of spaces and parentheses
# Returns a list of relevant ordered pair
def strip_string(self, entry_num):
ordered_pair = entry_num.get().split(", ")
ordered_pair[0] = ordered_pair[0].replace("(", "")
ordered_pair[1] = ordered_pair[1].replace(")", "")
return(ordered_pair)
# Calculates slope based on one point and y-intercept
def one_point(self):
pair_1 = self.strip_string(self.entry_1)
b = int(self.entry_2.get())
m = (int(pair_1[1]) - b)/(float(pair_1[1]))
label_3 = Label(self.frame_1, text = "SLOPE-INTERCEPT EQUATION: y = " + str(m) + "x + " + str(b))
label_3.grid(row = 2, column = 0, columnspan = 2)
# Calculates slope based on two points given
def two_points(self):
pair_1 = self.strip_string(self.entry_1)
pair_2 = self.strip_string(self.entry_2)
m = (int(pair_2[1]) - int(pair_1[1]))/float(int(pair_2[0]) - int(pair_1[0]))
b = (int(pair_1[1])) - (m*int(pair_1[0]))
label_3 = Label(self.frame_1, text = "SLOPE-INTERCEPT EQUATION: y = " + str(m) + "x + " + str(b))
label_3.grid(row = 2, column = 0, columnspan = 2)
# Calling each object
two_p = Slope_Calc(root, 0, 0, "Two Points", "First Ordered Pair", "Second Ordered Pair", "two_points")
one_p = Slope_Calc(root, 0, 1, "One Point and Y-Intercept", "Ordered Pair", "Y-intercept", "one_point")
root.mainloop()
The command keyword argument of the Button constructor is supposed to be a function.
Here you give it instead a string which is the name of the method of self that should be called. So you must first get this method using setattr to be able to call it. This should do it:
def call():
method = getattr(self, self.function_call)
method()
self.calc_button = Button(
self.frame_1,
text = "Calculate",
command = call)
You then have an error in strip_string but that's another story.
Code snippet #1:
d = census_df[census_df.SUMLEV==50].copy()
d['max'] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)
d['min'] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
d['diff'] = d['max'] - d['min']
d[d['diff'] == d['diff'].max()].iloc[0]['CTYNAME']
Code snippet #2:
d = census_df[census_df["SUMLEV"] == 50]
d= d.groupby(["STNAME"])["POPESTIMATE2015'].nlargest(3)
d["max"] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].max(axis=1)
d["min"] = d[['POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].min(axis=1)
d["Diff"] = d["max"] - d["min"]
ans = d[d["Diff"] == d["Diff"]].max().iloc[0]["CTYNAME"]
I'm trying to use an Entry widget to get the user's data and then print it.
Why is Tkinter Entry's get function returning nothing?
This didn't help me.
This is my code
message = ''
# start_chatting function
def start_chatting ():
global message
master2 = tk.Tk()
master2.geometry("1280x720")
master2.title("Messenger")
label = tk.Label(master2, text = "Messenger!!!",bg = '#1e00ff',fg ='yellow',width = 35, height = 5).place(x = 500, y = 0)
username_label = tk.Label(master2,text = usernames[position_counter],bg = '#91806d',fg ='white',width = 10, height = 2).place(x = 0, y = 100)
v = StringVar()
L1 = Label(master2, text = "Type your message : ").place(x=0, y = 680)
e = Entry(master2,textvariable = v)
e.insert(END, '')
e.pack()
e.place(x = 115, y = 680)
submit_button = Button(master2,text = "Submit",command = submit_f).place(x = 200, y = 680)
message = message+ v.get()
master2.mainloop()
#submit_f function
def submit_f ():
global message
print(message)
Keep in mind that this is a part of my code and not all of it.
Thanks in advance!
The function prints nothing beacause you have changed the message value in the current function where the entry box is defined.
So, when you write v.get(), it generally returns an empty text.The message variable needs to call every time when submit button is pressed. Hence, message variable should be changed inside submit_f() function.
Here's the Solution,
import tkinter
from tkinter import *
message = ''
# start_chatting function
def start_chatting ():
global v
master2 = Tk()
master2.geometry("1280x720")
master2.title("Messenger")
label = Label(master2, text = "Messenger!!!",bg = '#1e00ff',fg ='yellow',width = 35, height = 5).place(x = 500, y = 0)
username_label = Label(master2,text = usernames[position_counter],bg = '#91806d',fg ='white',width = 10, height = 2).place(x = 0, y = 100)
L1 = Label(master2, text = "Type your message : ").place(x=0, y = 680)
v = StringVar()
e = Entry(master2,textvariable = v)
e.insert(END, '')
e.pack()
e.place(x = 115, y = 680)
submit_button = Button(master2,text = "Submit",command = submit_f).place(x = 200, y = 680)
master2.mainloop()
#submit_f function
def submit_f ():
global message
message = message + " " + v.get()
print(message)
start_chatting()
The output i am getting from my residual model is an image with small little squares on it ( a very low resolution image), but it is supposed to give me a depth map. The objects in the image are lost and only those small squares are visible. I don't how to improvise it ?
def mini_model(input_shape) :
# Define the input as a tensor with shape input_shape
X_input = Input(input_shape)
# Zero_Padding
X = ZeroPadding2D((3,3))(X_input)
#stage_1
X = Conv2D(64,(7,7),strides = (2,2),name = 'conv1')(X)
X = BatchNormalization(axis = 3,name = 'bn_conv1')(X)
X = Activation('relu')(X)
X = MaxPooling2D((3,3),strides = (2,2))(X)
# Stage 2
X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 1)
X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')
#stage3
X = convolutional_block(X,f = 3 , filters = [128,128,512],stage = 3,block = 'a', s = 2)
X = identity_block(X,3,[128,128,512],stage = 3,block='b')
X = identity_block(X,3,[128,128,512],stage = 3 , block = 'c')
X = identity_block(X,3,[128,128,512],stage = 3 , block = 'd')
#stage 4
X = convolutional_block(X,f = 3 , filters = [256,256,1024],stage = 4,block = 'a', s = 2)
X = identity_block(X,3,[256,256,1024],stage = 4,block='b')
X = identity_block(X,3,[256,256,1024],stage = 4,block='c')
X = identity_block(X,3,[256,256,1024],stage = 4,block='d')
X = identity_block(X,3,[256,256,1024],stage = 4,block='e')
X = identity_block(X,3,[256,256,1024],stage = 4,block='f')
X = identity_block(X,3,[256,256,1024],stage = 4,block='g')
X = identity_block(X,3,[256,256,1024],stage = 4,block='h')
X = identity_block(X,3,[256,256,1024],stage = 4,block='i')
X = identity_block(X,3,[256,256,1024],stage = 4,block='j')
X = identity_block(X,3,[256,256,1024],stage = 4,block='k')
X = identity_block(X,3,[256,256,1024],stage = 4,block='l')
#stage 5
X = convolutional_block(X,f = 3 , filters = [512,512,2048],stage = 5,block = 'a', s = 2)
X = identity_block(X,3,[512,512,2048],stage = 5,block='b')
X = identity_block(X,3,[512,512,2048],stage = 5,block='c')
# AVGPOOL
X = Conv2D(3,kernel_size=(3,3), padding = 'same',use_bias = False)(X)
X = UpSampling2D(size=2)(X)
X = UpSampling2D(size=2)(X)
X = UpSampling2D(size=2)(X)
X = UpSampling2D(size=2)(X)
X = UpSampling2D(size=2)(X)
# Create model
model = Model(inputs = X_input, outputs = X)
return(model)
my residual model!!
input image shape = (480,640,3)
Actual results : The image is made of small squares , with different levels of gray.
Expected results : The image should be an depth map of the same size as input (480,640,3)
You have five upsampling layers in sequence. That's exactly what is expected from that. Big squares of 32 pixels. (2^5 = 32)
You should probably read about U-nets, create more convolutions between the upsamplings and connections from the resnet to the upsampling results.
I have a little problem . I want to do a program that divides two numbers with fixed point . My output looks okay but when I test the program on a
local test site I get some strange results. Can someone help me?Thanks.
module divider(
input[7:0] a,b,
output reg [15:0] q,
output reg [7:0] r,frac
);
reg[7:0] c2, r2;
integer c;
always #(*)
begin
c = 8'b00000000;
r = a;
frac = 8'b00000000;
repeat (30)
begin
if (r >= b )
begin
c = c + 1;
r = r - b;
end
end
q[15:8] = c;
repeat (8)
begin
if(r != 0)
begin
r = r*10;
c2 = 8'b00000000;
repeat(30)
begin
if (r>=b)
begin
c2 = c2 + 1;
r = r-b;
end
end
frac = frac*10 + c2;
end
end
q[7:0] = frac;
end
endmodule
Output local site:
error: a = 1, b = 2, q = 0.019531, expected q = 0.500000
error: a = 1, b = 4, q = 0.097656, expected q = 0.250000
error: a = 1, b = 5, q = 0.007813, expected q = 0.199219
error: a = 1, b = 7, q = 0.570313, expected q = 0.140625
error: a = 1, b = 8, q = 0.488281, expected q = 0.125000
error: a = 1, b = 9, q = 0.777344, expected q = 0.109375
error: a = 1, b = 10, q = 0.003906, expected q = 0.097656
error: a = 1, b = 11, q = 0.363281, expected q = 0.089844
error: a = 1, b = 14, q = 0.785156, expected q = 0.070313
error: a = 1, b = 15, q = 0.664063, expected q = 0.066406
error: a = 1, b = 16, q = 0.441406, expected q = 0.062500
error: a = 1, b = 17, q = 0.937500, expected q = 0.058594
error: a = 1, b = 18, q = 0.386719, expected q = 0.054688
error: a = 1, b = 19, q = 0.207031, expected q = 0.050781
error: a = 1, b = 20, q = 0.019531, expected q = 0.046875
error: a = 1, b = 21, q = 0.187500, expected q = 0.046875
error: a = 1, b = 22, q = 0.679688, expected q = 0.042969
error: a = 1, b = 23, q = 0.695313, expected q = 0.042969
error: a = 1, b = 25, q = 0.015625, expected q = 0.039063
error: a = 1, b = 27, q = 0.589844, expected q = 0.035156
error: a = 1, b = 28, q = 0.890625, expected q = 0.035156
error: a = 1, b = 29, q = 0.824219, expected q = 0.031250
error: a = 1, b = 30, q = 0.832031, expected q = 0.031250
error: a = 1, b = 31, q = 0.804688, expected q = 0.031250
error: a = 1, b = 32, q = 0.207031, expected q = 0.031250
error: a = 1, b = 33, q = 0.121094, expected q = 0.027344
error: a = 1, b = 34, q = 0.437500, expected q = 0.027344
error: a = 1, b = 35, q = 0.570313, expected q = 0.027344
error: a = 1, b = 36, q = 0.914063, expected q = 0.027344
error: a = 1, b = 37, q = 0.000000, expected q = 0.023438
error: a = 1, b = 38, q = 0.339844, expected q = 0.023438
error: a = 1, b = 40, q = 0.097656, expected q = 0.023438
You don't necessarily need radix 10 for division on FPGA. Radix2 division (e.g. shift and subtract) would be much faster and easier to implement. I found this example for radix2 division.