I am importing one of my sub functions into my main script and I keep getting the following:
IndexError: list index out of range
I am implementing the same exact function calls in the script as the functions asks for. Does anyone know a way to check to see if the function is properly imported?
Below is a sample of the code I am working with
from file_with_function import function
[output1 output2 output3] = function(roll = 15, pitch = 30,
yaw = 45)
print('Argument List: '+ str(sys.argv))
rolla = int(sys.argv[1])
pitcha = int(sys.argv[2])
yawa = int(sys.argv[3])
def function(roll = 15 , pitch = 30 , yaw = 45):
#script is here
if __name__ =='__main__':
function(
roll = rolla,
pitch = pitcha,
yaw = yawa)
There is either an error in the function you're trying to import, or there is something wrong with the data that you are passing into the function. I don't think it's an import error, but without looking at the code I can't really help you.
Related
I try to generalize an optimization function using scipy.optimize.
Actually I write this function in this way:
def value_to_optimize(data):
data_set = np.genfromtxt('myfilepathinstaticmode', delimiter=',',skip_header=1)
doe = data_set[:,:-1]
new_data_set = np.vstack((np.array(doe),np.array(data)))
return result_of_another_function(new_data_set)
def new_data():
rst = minimize(value_to_optimize,[0,0])
return rst.x
the function I try to optimize is the first one. And to do that I use the second function that use "minimize" and a x0 for starting optimization.
As you can see my problem is comming from 'myfilepathinstaticmode'. I would like to generalize my function, like value-to_optimize(filename,data), but at this moment, I cannot apply optimize() on it because it is only working on numbers.
Any idea on how to write it in a generalized manner ?
I personally would read the data outside of the minimizer function and then hand over only that data into the method:
def value_to_optimize(data, doe):
new_data_set = np.vstack((np.array(doe),np.array(data)))
return result_of_another_function(new_data_set)
def new_data():
data_set = np.genfromtxt('myfilepathinstaticmode', delimiter=',',skip_header=1)
doe = data_set[:,:-1]
rst = minimize(value_to_optimize, ([0,0], doe))
return rst.x
EDIT: I'm not sure if I understood your question correctly. So, alternatively, a more flexible approach would be to use functools.partial to generate a method with the filename as a parameter which you can then hand over to your optimizer.
Something is working in this way. I'm not convinced about the robustess of the code. The solution: I defined the function value_to_optimize() inside the function new_data(). Like this, the parameter 'filename' is out of "value_to_optimize()" but is a kind of "global" assignment inside new_data().
def new_data(filename):
def value_to_optimize(data):
data_set = np.genfromtxt(filename, delimiter=',',skip_header=1)
doe = data_set[:,:-1]
new_data_set = np.vstack((np.array(doe),np.array(data)))
return result_of_another_function(new_data_set)
rst = minimize(value_to_optimize,[0,0])
return rst.x
I was use threading Pool for my script. I have working code for html table to json conversion.
I am using pandas for html table to json.
html_source2 = str(html_source1)
pool = ThreadPool(4)
table = pd.read_html(html_source2)[0]
table= table.loc[:,~table.columns.str.startswith('Unnamed')]
d = (table.to_dict('records'))
print(json.dumps(d,ensure_ascii=False))
results = (json.dumps(d,ensure_ascii=False))
i want something like:
html_source2 = str(html_source1)
pool = ThreadPool(4)
def abcd():
table = pd.read_html(html_source2)[0]
table= table.loc[:,~table.columns.str.startswith('Unnamed')]
d = (table.to_dict('records'))
print(json.dumps(d,ensure_ascii=False))
results = (json.dumps(d,ensure_ascii=False))
You are almost there. You need to make the function take an input argument, here html_str and then have it return the results you need so you can use them outside the function.
html_source2 = str(html_source1)
pool = ThreadPool(4)
def abcd(html_str):
table = pd.read_html(html_str)[0]
table= table.loc[:,~table.columns.str.startswith('Unnamed')]
d = (table.to_dict('records'))
print(json.dumps(d,ensure_ascii=False))
results = (json.dumps(d,ensure_ascii=False))
return results
my_results = abcd(html_source2)
And remove the print call if you don't need to see the output in the function
I guess you don't know much about functions, parameters and how to call functions read here https://www.w3schools.com/python/python_functions.asp
Consider reading it it's a short read.
I am trying to use "odein" in another function to assess some parameters. But the code gave a sign "restart: shell". I cannot understand what is happenning here.
def fun(x,ia,ib):
#### function that returns dy/dt
def model(y, t, a, b):
dydt = np.exp(a-b/R/T)
return dydt
#### initial condition
y0 = 0.0
#### temperature points
t = x/d
#### solve ODEs
a= ia
b = ib
y1 = odeint(modeldydt,y0,t,args=(a,b))
return y1
x=array([279,300,310,320,330,340,350])
y=array([0.0,0.1,0.2,0.3,0.6,0.7,0.8])
calc_model = Model(fun)
pars = TGA_model.make_params(ia=10, ib=1e4)
result = calc_model.fit(y, x=x)
And what I got is
=============================== RESTART: Shell ===============================
I would like to know if it is possible to net the functions together in python. If it is possible, you are very kind to tell me the mistake I have made. If it is not possible, you are still welcome to send me your suggestion. If nothing works in python, I should try some matlab code instead.
Thanks in advance!
I'd like to use multiprocessing in a rescource-heavy computation in a code I write, as shown in this watered-down example:
import numpy as np
import multiprocessing as multiproc
def function(r, phi, z, params):
"""returns an array of the timepoints and the corresponding values
(demanding computation in actual code, with iFFT and stuff)"""
times = np.array([1.,2.,3.])
tdependent_vals = r + z * times + phi
return np.array([times, tdependent_vals])
def calculate_func(rmax, zmax, phi, param):
rvals = np.linspace(0,rmax,5)
zvals = np.linspace(0,zmax,5)
for r in rvals:
func_at_r = lambda z: function(r, phi, z, param)[1]
with multiproc.Pool(2) as pool:
fieldvals = np.array([*pool.map(func_at_r, zvals)])
print(fieldvals) #for test, it's actually saved in a numpy array
calculate_func(3.,4.,5.,6.)
If I run this, it fails with
AttributeError: Can't pickle local object 'calculate_func.<locals>.<lambda>'
What I think the reason is, according to the documentation, only top-level defined functions can be pickled, and my in-function defined lambda can't. But I don't see any way I could make it a standalone function, at least without polluting the module with a bunch of top-level variables: the parameters are unknown before calculate_func is called, and they're changing at each iteration over rvals. This whole multiprocessing thing is very new to me, and I couldn't come up with an alternative. What would be the simplest working way to parallelize the loop over rvals and zvals?
Note: I used this answer as a starting point.
This probably isn't the best answer for this, but it's an answer, so please no hate :)
You can just write a top level wrapper function that can be serialized and have it execute functions... This is kinda like function inception a bit but I solved a similar problem in my code like this.
Here is a brief example
def wrapper(arg_list, *args):
func_str = arg_list[0]
args = arg_list[1]
code = marshal.loads(base64.b64decode(func_str.data))
func = types.FunctionType(code, globals(), "wrapped_func")
return func(*args)
def run_func(func, *args):
func_str = base64.b64encode(marshal.dumps(func.__code__, 0))
arg_list = [func_str, args]
with mp.Pool(2) as pool:
results = pool.map(wrapper, arg_list)
return results
I have three functions 2 of which take a string and return a string. I have a third function that takes two strings and returns a string. I am trying to create a simple Tkinter GUI that would take in any parameters of the functions then based on the button press run my algorithm returning the result. Tkinter is giving me a hard time. I need four input fields for all possible parameters then run the correct function on press of button. Functions will look like:
CalculateStrenghtofBrute(Word, Charset)
CalculateDictionary(Word)
CalculatePassPhrase(Phrase)
All return a string created within the functions.
Below is a Sample Function
def wordTime(Password):
with open('Dics/dict.txt','r') as f:
Words = f.read().splitlines()
found = Words.index(Password)
found += 1
timeSec = found*.1
if(timeSec> 31536000):
time = timeSec/31536000
timeType = 'Years'
elif(timeSec>86400):
time = timeSec/86400
timeType = 'Days'
elif(timeSec>360):
time = timeSec/360
timeType = 'Hours'
elif(timeSec>60):
time = timeSec/60
timeType = 'Minutes'
else:
time = timeSec
timeType ='Seconds'
return ('Cracking',Password,'using dictionary attack will take', round(time, 2), timeType+'.')
Thanks
If you want to take input from the user you need to create an entry box, once you have an entry box you can call the get method on it to get the string which currently resides in the entry box, I've took your example function and made a simple tk GUI for it:
import Tkinter as tk
def wordTime():
password = input_box.get()
with open('Dics/dict.txt','r') as f:
Words = f.read().splitlines()
found = Words.index(Password)
found += 1
timeSec = found*.1
if(timeSec> 31536000):
time = timeSec/31536000
timeType = 'Years'
elif(timeSec>86400):
time = timeSec/86400
timeType = 'Days'
elif(timeSec>360):
time = timeSec/360
timeType = 'Hours'
elif(timeSec>60):
time = timeSec/60
timeType = 'Minutes'
else:
time = timeSec
timeType ='Seconds'
print ('Cracking',Password,'using dictionary attack will take', round(time, 2), timeType+'.')
# Make a top level Tk window
root = tk.Tk()
root.title("Cracker GUI v.01")
# Set up a Label
grovey_label = tk.Label(text="Enter password:")
grovey_label.pack(side=tk.LEFT,padx=10,pady=10)
# Make an input box
input_box = tk.Entry(root,width=10)
input_box.pack(side=tk.LEFT,padx=10,pady=10)
# Make a button which takes wordTime as command,
# Note that we are not using wordTime()
mega_button = tk.Button(root, text="GO!", command=wordTime)
mega_button.pack(side=tk.LEFT)
#Lets get the show on the road
root.mainloop()
If you wanted to take multiple values you could use multiple buttons which set multiple variables, also I'm not sure about your function but that's not really the question at hand.
For reference the following sites have some good basic examples:
http://effbot.org/tkinterbook/entry.htm
http://effbot.org/tkinterbook/button.htm
http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/