Deleting trail/retain of a moving displayObject - vpython

How do you delete a trail from a moving object (the trail exists out of points)?
I thought that if you,
sphere.retain = 0
sphere.make_trail = False
It would instantly delete the trail. But this ins't true because if you put a:
sleep(50)
after it. You can see that the trail is still there.

Setting s.make_trail = False just means "stop adding points to the trail".
In GlowScript VPython or Jupyter VPython you can say s.clear_trail().
In Classic VPython, s.trail_object is the curve or points object, and you can delete elements of these objects.

Related

How to merge nodes and relationships using py2neo v4 and Neo4j

I am trying to perform a basic merge operation to add nonexistent nodes and relationships to my graph by going through a csv file row by row. I'm using py2neo v4, and because there is basically no documentation or examples of how to use py2neo, I can't figure out how to actually get it done. This isn't my real code (it's very complicated to handle many different cases) but its structure is basically like this:
import py2neo as pn
graph = pn.Graph("bolt://localhost:###/", user="neo4j", password="py2neoSux")
matcher = pn.NodeMatcher(graph)
tx = graph.begin()
if (matcher.match("Prefecture", name="foo").first()) == None):
previousNode = pn.Node("Type1", name="fo0", yc=1)
else:
previousNode = matcher.match("Prefecture", name="foo").first())
thisNode = pn.Node("Type2", name="bar", yc=1)
tx.merge(previousNode)
tx.merge(thisNode)
theLink = pn.Relationship(thisNode, "PARTOF", previousNode)
tx.merge(theLink)
tx.commit()
Currently this throws the error
ValueError: Primary label and primary key are required for MERGE operation
the first time it needs to merge a node that it hasn't found (i.e., when creating a node). So then I change the line to:
tx.merge(thisNode,primary_label=list(thisNode.labels)[0], primary_key="name")
Which gives me the error IndexError: list index out of range from somewhere deep in the py2neo source code (....site-packages\py2neo\internal\operations.py", line 168, in merge_subgraph at node = nodes[i]). I tried to figure out what was going wrong there, but I couldn't decipher where the nodes list come from through various connections to other commands.
So, it currently matches and creates a few nodes without problem, but at some point it will match until it needs to create and then fails in trying to create that node (even though it is using the same code and doing the same thing under the same circumstances in a loop). It made it through all 20 rows in my sample once, but usually stops on the row 3-5.
I thought it had something to do with the transactions (see comments), but I get the same problem when I merge directly on the graph. Maybe it has to do with the py2neo merge function finding more identities for nodes than nodes. Maybe there is something wrong with how I specified my primarily label and/or key.
Because this error and code are opaque I have no idea how to move forward.
Anybody have any advice or instructions on merging nodes with py2neo?
Of course I'd like to know how to fix my current problem, but more generally I'd like to learn how to use this package. Examples, instructions, real documentation?
I am having a similar problem and just got done ripping my hair out to figure out what was wrong! SO! What I learned was that at least in my case.. and maybe yours too since we got similar error messages and were doing similar things. The problem lied for me in that I was trying to create a Node with a __primarykey__ field that had a different field name than the others.
PSEUDO EXAMPLE:
# in some for loop or complex code
node = Node("Example", name="Test",something="else")
node.__primarykey__ = "name"
<code merging or otherwise creating the node>
# later on in the loop you might have done something like this cause the field was null
node = Node("Example", something="new")
node.__primarykey__ = "something"
I hope this helps and was clear I'm still recovering from wrapping my head around things. If its not clear let me know and I'll revise.
Good luck.

making objects constantly in a while loop

I'm doing a project for a year 11 physics class and I'm trying to make a battery that generates electrons. This is the code:
electron = sphere(radius = 1, color = color.yellow, vel = vec(-1,0,0));
while battery.voltage > 0:
eb = electron.clone(pos=vec(0,0,0), vel = vec(-1,0,0));
I'm trying to make "eb" constantly, but it only applies eb.pos = eb.pos + eb.vel * deltat; apply to a the first electron. Is there any way to do this without making 600 different electron objects?
You could change the attribute that is modified directly to the electron object instead of creating it all the time. Apply the modifications to electron and add the computing action in the while. Is it what you meant?
You definitely need to make and move 600 sphere objects in order to have 600 sphere objects move. Your variable "eb" is just the name of the most recently made clone of the original sphere.
I'll advertise that a better place to pose VPython questions is in the VPython forum at
https://groups.google.com/forum/?fromgroups&hl=en#!forum/vpython-users

too much error correction made the code more worse

This is my first time with python and I have written this program to simulate a node trying to find mobile phones in an area.
first i am taking the distance converting them to RSS and based on that deciding the direction of the node to find all mobile phones.
the code worked fine till 2 days ago but when i expanded to area and no of nodes, some errors started to come and i kept correcting them.
now it doesn't run and I have made it more worse:
is there any hint or guidance I can get from the experts?
and yes I have overwritten the code without saving it to some other place.
Or maybe someone can really help me by having a look at it.
its really messy though.
thanks
Here is a part of the code i am having the most trouble with.
def pos_y(uav_current_cord,area,uav_step_size,flag):
detected_y = 0
detected_nodes_pos_y = 0
if flag == 1:
span = area+uav_step_size - uav_current_cord[1]
#print(span)
for y in range(uav_current_cord[1],area+uav_step_size,uav_step_size):
global distance_covered
distance_covered += uav_step_size
UAV_new = [uav_current_cord[0],y]
y_last_pos = UAV_new
plot((*UAV_new), marker='o', color='r', ls='')
distance_new=[]
for i in nodes:
temp_x_axis = euclid_dist(UAV_new,node_cord[i])
ss_x_axis = dist_to_ss(temp_x_axis)
if (ss_x_axis > threshold):
detected_nodes_pos_y += 1
detected_y = (detected_nodes_pos_y)
if y >= area+uav_step_size:
uav_current_cord = UAV_new
flag = 1
keep_moving = neg_x(uav_current_cord,span,uav_step_size,flag,y_last_pos)
uav_current_new = keep_moving[0]
distance_covered_back = keep_moving[1]
nodes_detected_final = keep_moving[2]
uav_current_new = keep_moving[3]
#return [uav_current_new, distance_covered, nodes_detected_final, uav_current_new]
#uav_current_new = y_last_pos
return [y_last_pos, distance_covered, detected_y, y_last_pos]
if flag == 1:
area1 = area*2
for y in range(uav_current_cord[1],area,uav_step_size):
UAV_new = [uav_current_cord[0],y]
#print('Pos_Y movement of UAV',UAV_new)
#print('ye loop chal raha hai')
y_last_pos = UAV_new
plot((*UAV_new), marker='o', color='g', ls='')
distance_new=[]
for i in nodes:
temp_x_axis = euclid_dist(UAV_new,node_cord[i])
ss_x_axis = dist_to_ss(temp_x_axis)
if (ss_x_axis > threshold):
detected_nodes_pos_y += 1
detected_y = (detected_nodes_pos_y)
#print('nodes detected in pos Y ',detected_y)
#print('last position in pos y =', y_last_pos)
for y in range(UAV_new[1],uav_current_cord[1],-uav_step_size):
# print('ab ye chala hai')
UAV_new=[uav_current_cord[0],y]
plot((*UAV_new), marker='o', color='g', ls='')
distance_covered_back = area*2
return [UAV_new, distance_covered_back, detected_y, y_last_pos]
As others have said, use git, but know the alternatives.
If you're brand new to revision control, starting with Git will either be a big help or hindrance in the time being, but learning Git will be valuable down the line regardless.
If you have an MSDN account, you can also use Visual Studio's built-in revision control. It's good for rapid prototyping, but has no real edge over git.
Another common choice that is pretty simple would be TortoiseSVN. It's very easy to use.
Alternatively, if you don't mind your code being public for a free account or a small monthly fee for private, you can do all your commits via browser using GitHub. This is hand's down the simplest option for revision control newbies. It's major downside is limited flexibility for multi-file commits.
I have little to no meaningful experience with BitBucket and some other common alternatives to GitHub.
I'm sorry, but we cannot help you... You're not the first with this problem and unfortunately not the last.
To help prevent this kind of problems, software like Git exists. It let's you do the following (image reference):
The root file is the master. This is your working script. If you want to change/add/remove something, you make a so-called branch where you do whatever you want. If you have tested it, you can commit the changes (that is, store this as a new version of the script with a comment) and merge it with the master (that is, add the changes to the master script). If something seems to be wrong, you can always go back to a previous version of your script.
The documentation of Git is very good: read it and use it wisely!

Side-Step Application.MsgBox in VBA (Excel)

In order to head off a storm of "comment it out" replies, here is my situation:
I have a process is normally run 1 iteration by 1 iteration. A user manually hits a button that calls a macro which, upon completion, pops up a message box that reports the total time length the macro ran for. It's pretty handy for diagnosing issues. This code is locked down and I cannot modify it.
I am trying to do this at scale. Because the code in the main spreadsheet and workbook are locked, I have a separate workbook open in the same instance of excel with a macro that operates the locked down workbook. Rather than 1 by 1, I've got a set of 300 I'm trying to run through. Right now I have to babysit the thing and hit space to get past the MsgBox. Does anyone know of any tricks to prevent me having to monitor the thing? Either disabling the pop-ups or some way to make them non-modal. Maybe a trick to make the mouse click?
You're right in knowing that the best way to fix the issue is to correct the code. In which case you would probably make the pop-ups toggle-able.
However, I wrote this for you which could be used as a potential work around. It utilizes VBScript to "sort-of" simulate multithreading so that you can send a key to the modal Msgbox. Assuming you can do what you want to do via code, simply call SendDelayedKeys before the action that will cause a Msgbox. You may have to tinker with the Delay based upon your circumstances as 100 milliseconds may not be enough. To change the Delay, just call like this: SendDelayedKeys 500 for 500 milliseconds.
Sub SendDelayedKeys(Optional Delay As Long = 100, Optional keys As String = """ """)
Dim oFSO As Object
Dim oFile As Object
Dim sFile As String
sFile = "C:\SendKeys.vbs" 'Make this a valid path to which you can write.
'Check for the .vbs file.
If Not Len(Dir$(sFile)) Then
'Create the vbs file.
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(sFile)
oFile.WriteLine "Set WshShell = WScript.CreateObject(""WScript.Shell"")"
oFile.WriteLine "WScript.Sleep CLng(WScript.Arguments(0))"
oFile.WriteLine "WshShell.SendKeys WScript.Arguments(1)"
oFile.Close
End If
Shell "wscript C:\SendKeys.vbs " & Delay & " " & keys
End Sub
Sub ProofOfConcept()
'Using default parameters which sends a space after 100 milliseconds
SendDelayedKeys
MsgBox "I disappear on my own!"
End Sub
A word of warning: Any solution that utilizes SendKeys is a fragile solution and should be avoided when possible. However, when your options are limited and you need to avoid a manual process, sometimes it's your only option.
Since SiddhartRout rightly pointed out that this could be solved using API calls: here's a link with C# code that would close your msgbox every second.
The problem here really isn't strictly a problem more code can (or indeed should) solve.
There are a great many things to consider and any solution will be more complex AND less reliable than the problem it is initially trying to solve. But lets look at your options...
SendKeys is not reliable for that kind of use, what happens if the dialogue says "would you like me to save this workbook?" just after making a change that was meant to be temporary or "would you like to play global thermonuclear war?" Plus with a batch process like that you want to get on with something else while you wait, even if it's only to come here to downvote trolls. If nothing else you may not be in control of this code so what kind of mess will it cause when the maintainers realise msgbox is bad UX and kill it?
FindWindow API calls would let you check the content in the window to make sure it says what you're expecting but then you're potentially asking some bit of quick & dirty vbscript to go into a race condition until the right window comes up. Can you guarantee that the threads won't lock up?. What about platform issues - what happens if someone wants to run your code on their shiny new surface? What happens when your 64 bit modal Excel dialogue window can't be seen by the 32-bit api calls you were making? What about a new version of office that doesn't present modal dialogues in the same way? None of those problems are insurmountable but each adds complexity (aka opportunity for failure.)
The best solution is fix the actual problem you have identified from the outset which is that the original code throws up an unnecessary modal dialogue. Someone needs to fix that - it doesn't have to be you but if you estimate how much time that modal dialogue wastes in lost productivity that should get you a solid business case for getting it sorted.

Load Excel add-in

I tried:
$sAddIn = "H:\prog\essxleqd.xla"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Add
$oAddIn = $oExcel.AddIns.Add($sAddIn)
$oAddIn.Installed = True
$oExcel.WorkBooks.Open("H:\Balance_Inquiry.xls")
When I do:
Run('"c:\pathtoexcel\excel.exe" "c:\pathtoaddin\addin.xla"')
It will work. But I prefer the former solution because I need two different add-ins.
$sAddIn = "H:\prog\essxleqd.xla"
$oExcel = ObjCreate("Excel.Application")
$oExcel.Visible = 1
$oExcel.WorkBooks.Add
$oAddIn = $oExcel.AddIns.Add($sAddIn)
$oAddIn.Installed = False
$oAddIn.Installed = True
$oExcel.WorkBooks.Open("H:\Balance_Inquiry.xls")
This seems to do the trick. First time it got installed and worked. Afterwards it didn't. Even when it is installed it doesn't execute the second time. Maybe someone can explain or provide a more elegant solution?
Strange problem indeed. It seems that the addon will only do its operations when you install it? That is not how Excel addons should behave, and that is why you are seeing strange behavior when trying to automate it.
If the addon needs to be reinstalled to work, then indeed the only solution is to reinstall the addon.
$oAddIn.Installed = False
$oAddIn.Installed = True
If this is an addon that is written by you or someone in your own business, then you might want to identify the problem in the addon itself. I can safely assure you that the normal mode of operation works well. :))
As a bonus tip, you might want to do this:
$oAddIn = $oExcel.AddIns.Add($sAddIn, True)
It will copy the file to the appropriate location if that is necessary, otherwise it is left alone.

Resources