When the expected vertex or edge is not present in the database, gremlin python raises the exception StopIteration. How to resolve/prevent the exception. Query to return none or empty instead of an error.
Eg:
g.V().hasLabel('employee').has('name', 'Thirumal').elementMap().next()
output when the vertex is not available
def __next__(self):
if self.traversers is None:
self.traversal_strategies.apply_strategies(self)
if self.last_traverser is None:
self.last_traverser = next(self.traversers)
StopIteration
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/gremlin_python/process/traversal.py:50: StopIteration
Instead of using next, use toList instead. Then, if there is no data, you will get back an empty list.
Related
I am attempting to send a message from one process that I spawned to another for an assignment, I feel like I am very close here, but I think my syntax is just a bit off:
-module(assignment6).
-export([start/1, process1/2, process2/0, send_message/2]).
process1(N, Pid) ->
Message = "This is the original Message",
if
N == 1 ->
timer:sleep(3000),
send_message(Pid, Message);
N > 1 ->
timer:sleep(3000),
send_message(Pid, Message),
process1(N-1, Pid);
true ->
io:fwrite("Negative/0, Int/Floating-Point Numbers not allowed")
end.
process2() ->
recieve
Message ->
io:fwrite(Message),
io:fwrite("~n");
end.
send_message(Pid, Message) ->
Pid ! {Message}.
start(N) ->
Pid = spawn(assignment6, process2, []),
spawn(assignment6, process1, [N, Pid]).
The goal of this program is that the Message, will be printed out N times when the function is started, but be delayed enough so that I can hot-swap the wording of the message mid-run. I just can't quite get the Message to process2 for printout.
Four small things:
It's spelled receive, not recieve
Remove the semicolon in process2. The last clause in a receive expression does not have a terminating semicolon. You can see this in the if expression in process1: the first two clauses end with a semicolon, but the third one does not.
In process2, print the message like this:
io:fwrite("~p~n", [Message])
Since Message is a tuple, not a string, passing it as the first argument to io:fwrite causes a badarg error. Let's ask io:fwrite to format it for us instead.
process2 should probably call itself after printing the message. Otherwise, it will receive one message and then exit.
So now you can run the code, and while it's running you can load a new version of the module with a different message (so called "hot code swapping"). Will that change the message being printed? Why / why not?
It won't. process1 does a local call to itself, which means that it stays in the old version of the module. Do an external call instead (explicitly specifying the module: assignment6:process1(N-1, Pid)), and it will switch to the new version.
I am trying to Implement the function makeNeg() that takes a list of numbers and a number representing an index as a parameter and changes the list item at the specified index to be negative. If the number at the specified index is already negative, the function shouldn't change the list. The function should verify that the index is correct for the list. If the index isn't valid for the list, the function shouldn't change the list.
I have been trying to solve this problem but I can not find the way to start it. Can someone guide me in the right direction? Thank you for your help!
You will get some trouble with accessing mylist[n], even after checking the list length...
First you can compare the length of the list with your index:
def MkNeg(mylist, n):
if len(mylist)<n:
print ("Error, this index does not exist")
The thing is, you can't access any value of a list item that doesn't exist. Even if it's in an if/elif/else position, so that it won't be executed. What I'm saying is
def MkNeg(mylist, n):
if len(mylist)<n:
print ("Error, this index does not exist")
elif mylist[n]>0:
#do ur stuff
this doesn't work, because the compiler sees a reference that doesn't exist.
That's why you need to try it:
def MkNeg(mylist, n):
try:
if mylist[n]>0:
mylist[n]=-mylist[n]
except IndexError:
print ("Index doesn't exist for this list.")
return mylist
I didn't check the length, because you will get an IndexError if the index doesn't exist.
What try does is trying to execute the command. If it fails, you can handle the further execution with except. If not, everything is fine.
I tried stackoverflow, gremlin docs. But couldn't find the solution.
I tried:
g.v('class:Employee',[EmpId:'398']);
It created one vertex. But again I tried with different value. I couldn't insert new vertex.
gremlin> g.addVertex('Class:Employee',[EmpId:'589'])
groovysh_parse: 51: expecting '(', found 'g' # line 51, column 1.
g.addVertex('Class:Employee',[EmpId:'589'])
That error usually means that the input buffer is messed up because you had a syntax error from the previous line. In this case it looks like the buffer is expecting an open parenthesis, but you've instead started a new command. These issue can be fixed with a call to clear:
gremlin> clear
I wrote this Parser incorrectly. My professor says that I am using the tokens inappropriately. The problem is that I'm trying to access the tokens from an empty list. How can I possibly fill up the list with tokens in order to make sure that the program analyzes the tokens so the Eiffel code can execute. That is why I am getting this error:
raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : failed precondition from lexical_analyzers.ads:20
The code can be found here:
https://drive.google.com/file/d/0B3ZPyNRv7C3hV3NRUVBnN0prbEE/view?usp=sharing
The error comes from the lexical_analyzers.ads and the parsers.adb classes.
Your procedure Parse begins
procedure parse(p: in out Parser; f: out feature) is
tok: Token;
lex: Lexical_Analyzer;
var: Id;
com: Compound;
begin
so when you say
get_next_token(lex, tok);
which lex is it using? Answer: the empty one that you created in the declarations. You should be using p.lex.
And in the next line but one, your call to get_id doesn’t pass p:
var := get_id(tok);
and get_id repeats the pattern,
function get_id(tok: in Token) return Id is
par: Parser;
lex: Lexical_Analyzer;
tok1: Token := tok;
str: String := String(Tokens.get_lexeme(tok1));
begin
get_next_token(lex, tok1);
In this case, you’ve done it twice; you’ve created a local Parser and Lexical_Analyser instead of passing in the Parser (and its contained Lexical_Analyser).
This is a pattern you seem to have repeated in several places in the code.
I am working on a project in which I need to download and parse an XML file from a network location. I've been trying to use the QtNetwork module to accomplish this, but I'm running into a seemingly simple problem that I've spent many hours trying to solve. My code is as follows:
class NetworkAccessor(QObject):
done = False
def importXml(self):
self.manager = QNetworkAccessManager()
self.manager.finished.connect(self.fileReady(QNetworkReply))
self.manager.get(QNetworkRequest(QUrl("http://192.168.5.243/details.xml")))
def fileReady(self, response):
self.f = QTemporaryFile()
print type(response)
self.f.write(response.readAll())
self.done = True
print "Done"
When I instantiate the NetworkAccessor class and call importXml, I get the following error:
Traceback (most recent call last):
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 40, in updateUi
f = networkAccessor.importXml()
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 14, in importXml
self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady(QNetworkReply))
File "C:/SVN-Local/Thermal/PyQtTestTemplate.py", line 20, in fileReady
self.f.write(response.readAll())
TypeError: QIODevice.readAll(): first argument of unbound method must have type 'QIODevice'
It seems to indicate that the argument passed to the fileReady method is not instantiated. Furthermore, the print type(response) statement above indicates that response is of type PyQt4.QtNetwork.QNetworkReply.
I've tried various ways of connecting the signal to the slot including the "old fashioned" way: self.connect(self.manager,SIGNAL("finished(QNetworkReply*)"),self.fileReady("QNetworkReply")) as well as using short-circuit parameter passing with no success.
Can somebody show me the proper way to pass an instantiated instance of a QNetworkReply object from a signal to a slot?
Thanks.
Just pass it the slot, the QNetworkReply object will get passed through by the signal:
self.manager.finished.connect(self.fileReady)