_c_ Posted January 24, 2021 Share Posted January 24, 2021 I don't understand this. if I use in an external editor (Visual Studio Code, should that be relevant) this code I get bucketloads of errors: h = vs.GetCustomObjectPath( gPio_H ) if h is not None: .... While the same, expressed with operators, doesn't: h = vs.GetCustomObjectPath( gPio_H ) if h != None: .... But only in the external editor. In a doc script both forms possible without any problem. What could cause this? Please read the Python Programming recommendations: www.python.org/dev/peps/pep-0008/#programming-recommendations Quote Comparisons to singletons like None should always be done with is or is not, never the equality operators. Quote Link to comment
twk Posted January 24, 2021 Share Posted January 24, 2021 Possibly the level of error checking in Visual Studio? I use PyCharm and have not much experience in VS. Pycharm recommends the h is not None flavor. Using the h != None, displays a warning to adjust my code. Quote Link to comment
_c_ Posted January 24, 2021 Author Share Posted January 24, 2021 The errors come up in VW, though. I have a lot of problems but perhaps it depends on what I am doing. I was silly enough to begin my belated catching up of Python from.... vs.GetPt. Quote Link to comment
_c_ Posted January 24, 2021 Author Share Posted January 24, 2021 (edited) No, it is definitely weird. I do have it also in the normal VS editor. This causes errors: import vs def LinkIt(obj): recH = vs.GetParametricRecord(obj) if recH is not None: # uses words vs.AlrtDialog(vs.GetName(recH)) def Sub(pt): vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5) h = vs.FSActLayer vs.GetPt( Sub ) While this does not: import vs def LinkIt(obj): recH = vs.GetParametricRecord(obj) if recH != None: # uses operators vs.AlrtDialog(vs.GetName(recH)) def Sub(pt): vs.ForEachObjectAtPoint(LinkIt, 0, 0, pt[0], pt[1], 5) h = vs.FSActLayer vs.GetPt( Sub ) But this never gives problems: import vs def test(h): if h is not None: # uses words vs.AlrtDialog('not NIL') h = vs.FSActLayer test(h) Edited January 25, 2021 by _c_ Quote Link to comment
PatW Posted January 26, 2021 Share Posted January 26, 2021 (edited) You can also directly check "if ! h" the handle class has the boolean method implemented. regards, Patrick Edited January 26, 2021 by PatW Quote Link to comment
_c_ Posted January 26, 2021 Author Share Posted January 26, 2021 Ciao @PatW, the vs.py comes from one of the latest SDK. I don't think that the problem is in the None access, I think that there are underlying problems related to user interaction -or lack thereof. The whole trouble comes from vs.GetPt in some subtle way. Quote Link to comment
JBenghiat Posted January 29, 2021 Share Posted January 29, 2021 I believe the issue is that the handle is actually a class and not a value, so that indeed it’s not None (the variable is assigned to an object), it’s that the object evaluates to nil. So your line 6 is actually succeeding when it shouldn’t and line 7 executes with a null pointer. I suspect that the internal/external editor difference is a red herring — you were just detecting an invalid object. The test I use is != vs.Handle(0), that is, a handle that points to nothing, which is different from a non-existent handle. 1 Quote Link to comment
JBenghiat Posted January 29, 2021 Share Posted January 29, 2021 And to piggy back on @PatW’s comment, it’s possible the class is set to override the = / != operator, so using those compares the value of the handle while is / is not compares the presence of the class. Quote Link to comment
_c_ Posted January 29, 2021 Author Share Posted January 29, 2021 (edited) It has been said before and it is perfectly useless to say it again, but indeed variable declaration and handling, while less powerful, is clearly a no-brainer in Vectorscript Pascal, while in Python it is a major source of headaches. I can't begin even speaking of how much I miss Vector variables in Pascal. And yes, I am using vector classes in Python. Rant off. I'll get fit in this fast enough. Edited January 29, 2021 by _c_ Quote Link to comment
_c_ Posted January 29, 2021 Author Share Posted January 29, 2021 And I am crashing/hanging non stop as soon as I use the GetPt call in PIO from a button event. But one step at a time. Quote Link to comment
matteoluigi Posted February 20, 2022 Share Posted February 20, 2022 did you find any conclusion? actually I tried the following code, understanding that it won't work. However, I want to check wether the worksheet already exists before I create it. if worksheet = nil: however doesn't work with a handle, obviously (and "NIL" doesn't exist in Python) What can I do? switch back to Vectorscript? 😞 worksheet = vs.GetObject('WS_TEST') if worksheet = nil: vs.CreateWS(('WS_TEST'),2,2) Quote Link to comment
twk Posted February 20, 2022 Share Posted February 20, 2022 is there a nil object in python? In any case, for handle error checking I normally use: # based on your example worksheet = vs.GetObject('WS_TEST') if worksheet not in [None, 0]: vs.CreateWS(('WS_TEST'), 2,2) Quote Link to comment
matteoluigi Posted February 20, 2022 Share Posted February 20, 2022 (edited) 😞 that didn't help I just want to prevent, that the worksheet already exists before I create a new one 😉 for a in range (4): # vs.CreateWS('WS_TEST'+a+',2,2') worksheet = vs.GetObject(vs.Concat('WS_TEST',a)) if worksheet not in [None, 0]: vs.CreateWS((vs.Concat('WS_TEST',a)),2,2) vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,50) vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,20) vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),1,1,20,0,0) vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),2,2,20,0,0) vs.SetWSCellFormulaN((vs.Concat('WS_TEST',a)),1,1,1,1,a) create-ws.zip Edited February 20, 2022 by matteoluigi Quote Link to comment
twk Posted February 20, 2022 Share Posted February 20, 2022 (edited) The error is saying your indentations are inconsistent/incorrect. Indentations are important for python. You must either use 4-spaces, or a single TAB, you can't use both in one script. try this: for a in range (4): # vs.CreateWS('WS_TEST'+a+',2,2') worksheet = vs.GetObject(vs.Concat('WS_TEST',a)) if worksheet not in [None, 0]: vs.CreateWS((vs.Concat('WS_TEST',a)),2,2) vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,50) vs.SetWSColumnWidth((vs.Concat('WS_TEST',a)),1,1,20) vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),1,1,20,0,0) vs.SetWSRowHeight((vs.Concat('WS_TEST',a)),2,2,20,0,0) vs.SetWSCellFormulaN((vs.Concat('WS_TEST',a)),1,1,1,1,a) Edited February 20, 2022 by twk 2 Quote Link to comment
matteoluigi Posted February 21, 2022 Share Posted February 21, 2022 thanks @twk, works great!!! create-ws.zip Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.