First time I use Python Script on Grasshopper. I can’t find where are my mistakes. I am almost at the end but now I have this Error message. Can anyone help me to find the mistake?
import rhinoscriptsyntax as rs
def koch(v1,v2) :
dist=rs.Distance(v1,v2)
p1=v2-v1
p1=rs.VectorUnitize(p1)
p1*=dist*dist1
p1+=v1
p2=v2-v1
cross=v2-v1
cross=rs.VectorUnitize(cross)
cross=rs.VectorRotate(cross, 90, (0,0,1))
cross*=dist*dist4
p2=rs.VectorUnitize(p2)
p2*=dist*dist2
p2+=v1+cross
p3=v2-v1
p3=rs.VectorUnitize(p3)
p3*=dist*dist3
p3+=v1
return (v1,p1,p2,p3,v2)
def recursive(v1,v2,gens, lineList):
if(gens>0):
newPts = koch(v1,v2)
l = rs.AddPolyline([newPts(0),newPts(1),newPts(2),newPts(3),newPts(4)])
lineList.append(l)
recursive(v1,newPts(0),gens-1)
return lineList
allLines=()
a=recursive(pt1,pt2,2,allLines)
screenshot
Your line l = rs.AddPolyline([newPts(0), newPts(1), newPts(2), newPts(3), newPts(4)]) is incorrect.
Accessing an item in a tuple requires square brackets. Replace with the following line:
l = rs.AddPolyline([newPts[0], newPts[1], newPts[2], newPts[3], newPts[4]])
Related
I am trying to create a function in python 3 that builds a dataframe from a csv file. However, I keep getting a syntax error when I call
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
This line of code is not running, because I never actually call the function. Here is all of my code.
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = np.array(data_df[features].values#.tolist())
y = (data_df["Status"].replace("underperform",0).replace("outperform",1).values.tolist())
return X,y
How should I go about fixing this error?
You're missing a closing parenthesis in your X = np.array(data_df[features].values#.tolist()) - it's there, but it's commented out of the code with the # sign.
Your python interpreter does not know that you actually wanted to end that line there and continues to search for a closing parenthesis. Before it finds one, it stumbles over the assignment in the next line, which is illegal and causes your syntax error.
You can simply do:
def Build_Data_Set(features = ["DE Ratio","Trailing P/E"]):
data_df = pd.read_csv("key_stats.csv") #created in other file
X = data_df[features].values # already returns an array
y = data_df["Status"].replace({"underperform": 0, "outperform":1}).values
return X,y
df = Build_Data_Set()
I'm trying to build a simulation for DTN network the idea is to read a file with 3 elements each one represents time, source address, and destination address. The problem is that with this code that I have so far I get an error message. I don't know anything about python or any other coding program I'm just following tutorials.
import xlsxwriter as xlwt
#xbook = xlwt.Workbook("Test.xlsx")
#xsheet1 = xbook.add_worksheet("singlehop")
base_time = 1390911881273
M = []
def epidemic(_sa, _da):
global base_time, M
M = set([_sa])
for line in open("C:/simulator/new_contact.txt").readlines():
t, sa, da = line.strip().split()
if sa in M:
M.add(da)
if _da in M:
return (float(t) - base_time)/1000.0
t = epidemic(1, 10)
print (M)
# xsheet1.write(a, 0, sa)
# xsheet1.write(a, 1, da)
# xsheet1.write(a, 2, t)
#xbook.close()
Thanks to your help guys now I don't have the iterate problem anymore. However, the result of this code gives me M = {1}, It's not adding the hops from 1 to 10 to M.
As the error message shows you the error is in line 17, so let's check this line.
if sa in M:
m.append(da)
The message says that an integer is not iterable and you want to iterate over M. So could M be an integer?
Let's look at the point where M changes the value the last time.
M = (_sa)
The parenthesis dont make a different in python if you use this brackets [] you get a list, so maybe this is the mistake? _sa is the parameter given to this function and this is indeed an integer and this is not iterable.
First can you put the link to the tutorial?
Second I see a couple of mistakes:
1) m.append(da) will not work because you did not declare m
2) M = (_sa) is an integer not a collection
From what I can see, M get 1 as integer (which is not iterable), but the code will fail when it hits m.append(da) anyway.
I am having an issue with using the median function in numpy. The code used to work on a previous computer but when I tried to run it on my new machine, I got the error "cannot perform reduce with flexible type". In order to try to fix this, I attempted to use the map() function to make sure my list was a floating point and got this error message: could not convert string to float: .
Do some more attempts at debugging, it seems that my issue is with my splitting of the lines in my input file. The lines are of the form: 2456893.248202,4.490 and I want to split on the ",". However, when I print out the list for the second column of that line, I get
4
.
4
9
0
so it seems to somehow be splitting each character or something though I'm not sure how. The relevant section of code is below, I appreciate any thoughts or ideas and thanks in advance.
def curve_split(fn):
with open(fn) as f:
for line in f:
line = line.strip()
time,lc = line.split(",")
#debugging stuff
g=open('test.txt','w')
l1=map(lambda x:x+'\n',lc)
g.writelines(l1)
g.close()
#end debugging stuff
return time,lc
if __name__ == '__main__':
# place where I keep the lightcurve files from the image subtraction
dirname = '/home/kuehn/m4/kepler/subtraction/detrending'
files = glob.glob(dirname + '/*lc')
print(len(files))
# in order to create our lightcurve array, we need to know
# the length of one of our lightcurve files
lc0 = curve_split(files[0])
lcarr = np.zeros([len(files),len(lc0)])
# loop through every file
for i,fn in enumerate(files):
time,lc = curve_split(fn)
lc = map(float, lc)
# debugging
print(fn[5:58])
print(lc)
print(time)
# end debugging
lcm = lc/np.median(float(lc))
#lcm = ((lc[qual0]-np.median(lc[qual0]))/
# np.median(lc[qual0]))
lcarr[i] = lcm
print(fn,i,len(files))
I'm trying to build a function for fitting of my data. I'm using CurveExpert and the lines are in python. I'm no expert so after trying, it says I have an error one line after the end of the return (last line). Can you please help me? I want to note that I tried to copy an existing format of another function.
Thanks in advance!
name = ur"Diffusion"
nindvar = 1
equation = r"8.4*erfc(a/(2*sqrt(13.6094*x*b)))"
latexequation = r"8.4\mathrm{erfc}{\left(a/{left(2\sqrt{13.6094 x b\right)}\right)}"
def evaluate(x,a,b):
"""
"""
y = 8.4*erfc(a/(2*sqrt(13.6094*x*b)))
return y
def initialize(x,y):
"""
"""
try:
a = 74
b = 0.37
return a,b
You can't have try without except.
I'm currently attempting to create a program that sorts logs based on time stamp. The logs are in the following format:
# 2014 00:05:34 Jason "login" 0.01
# 2014 00:10:34 Jason "firefox" 0.01
#server crashed
# 2014 00:11:54 Jason "logoff" 0.01
I'm reading these logs from files on the computer named log.1 and log.2.
My plan is to first remove all lines start with "#" as the commented lines are not going to be needed to be sorted... unless there is a better way to be doing this.
In order to remove all lines that start with "#", my plan was to use list comprehension. Here is what I have so far:
def main():
Logs = []
getLogs(Logs)
sortLogs(Logs)
def getLogs(L):
Log1 = sys.argv[1]
Log2 = sys.argv[2]
L1 = read(Log1)
L2 = read(Log2)
L.append(L1)
L.append(L2)
def sortLogs(L):
sLog = [i for i in L if i.startswith('#')]
print(sLog)
def read(fName):
return open(fName, "r").readlines()
main()
Unfortunately, I get the error of "AttributeError: 'list' object has no attribute 'startswith'". Can anyone provide some insight as to why I'm getting this error or my technique isn't working?
I've also done the following in Python IDE and it has worked:
>>> newlist = ['#test','new','#no','pleasework']
>>> finallist = [x for x in newlist if x.startswith('#')]
>>> finallist
['#test']
Consider the following small piece of code:
First I have two lists
lst1 = [1,2,3,4]
lst2 = [2,3,4,5]
Then I am creating a new list and appending the above twos like you did in your code:
l = list()
l.append(lst1)
l.append(lst2)
Now, I am printing the type of the first element from the new list.
print type(l[0])
It says <type 'list'> and this is what your problem. You are having two lists in your new list, and that's why the startswith() didn't work for you.
So, whats the solution? replace append() with extend() and it will do.