How to retrieve property values of a node in py2neo - py2neo

I,m creating a graph database of geometric shapes and nodes contain a property called "type" to indicate whether that shape is a Triangle, Circle or square.I want to retrieve the type of every node and use it in IF condition as follow.
Results = g.run("START n=node(*) RETURN n.type as type")
for Result in Results:
if Result.type == 'TRINGLE':
print("It is a Triange")
elif Result.type == 'CIRCLE':
print("It is a Circle")
else:
print("It is a Square")
when run this code it is showing an error like this-
if Result.type == 'TRINGLE':
AttributeError: 'Record' object has no attribute 'type'
can anyone please tell me that are there any way to solve this.

You need record["type"] instead of record.type.

Related

Back4app, filter query based on null value or undefined value

In database seems we have two types of non existing values:
(undefined) and (null)
If I do query
if paidDateDoesNotExist == true {
query.whereKeyDoesNotExist("paidDate")
}
looks like it returns only undefined but no null records which is seems correct.
If I do query
if paidDateDoesNotExist == true {
query.whereKey("paidDate", equalTo: NSNull())
}
it returns the all undefined and null records.
So for some cases I need to clear value from client code. Let's say I have paidDate column and I want to clear paidDate which was set to some Date already.
in my client code I just simple assign NSNull() to PFObject
like pfObject["paidDate"] = NSNull()
in this cases query query.whereKeyDoesNotExist("paidDate") will not work.
So the thing I would like to understand - do I need to assign pfObject["paidDate"] = (undefined) value somehow or I just need to use another query which is uses NSNull() as filter.
Which is more fun when I use similar query form the admin panel, admin panel returns records which are equal to null only ignoring undefined records. But for client code query:
if paidDateDoesNotExist == true {
query.whereKey("paidDate", equalTo: NSNull())
}
returns undefined records as well as null records.

How to make where condition include nil value

I find ActiveRecord, funnel value is not "input" value
and I make where condition
here is my code
#calls = Call.searchable.where.not(funnel: "input")
these command work good, but result is also except nil value
I wanna only funnel is not 'input'
how to add OR condition in where statement
SQL Statement is
select * from call where is_visible = 1 and (funnel != 'input or funnel is null)
The blow code should help you.
#calls = Call.searchable.where("funnel != 'input' or funnel is null")
This will also work if values are dynamic
Call.searchable.where("funnel != ? OR funnel is ?”,#value1, nil)

How can I see what the default value of a visit is?

Assume I have:
visit(p) {
case ...
default:
println("This should not happen. All elements should be catched. Check: <x>");
};
How can I print out (in this case as x) what could not be matched?
I tried:
x:default:
\x:default:
default:x:
\default:x:
Tx,
Jos
We have a library named Traversal that allows you to get back the context of a match. So, you can do something like this:
import Traversal;
import IO;
void doit() {
m = (1:"one",2:"two",3:"three");
bottom-up visit(m) {
case int n : println("<n> is an int");
default: {
tc = getTraversalContext();
println("Context is: <tc>");
println("<tc[0]> is not an int");
if (str s := tc[0]) {
println("<s> is a string");
}
}
}
}
tc is then a list of all the nodes back to the top of the term -- in this case, it will just be the current value, like "three", and the entire value of map m (or the entire map, which will also be a match for the default case). If you had something structured as a tree, such as terms formed using ADTs or nodes, you would get all the intervening structure from the point of the match back to the top (which would be the entire term).
For some reason, though, default is matching the same term multiple times. I've filed this as bug report https://github.com/cwi-swat/rascal/issues/731 on GitHub.
You could also try this idiom:
visit(x) {
case ...
case ...
case value x: throw "default case should not happen <x>";
}
The value pattern will catch everything but only after the others are tried.

LINQ query with omitted user input

so I have a form with several fields which are criteria for searching in a database.
I want to formulate a query using LINQ like so:
var Coll = (from obj in table where value1 = criteria1 && value2 = criteria2...)
and so on.
My problem is, I don't want to write it using If statements to check if every field has been filled in, nor do I want to make separate methods for the various search cases (criteria 1 and criteria 5 input; criteria 2 and criteria 3 input ... etc.)
So my question is: How can I achieve this without writing an excessive amount of code? If I just write in the query with comparison, will it screw up the return values if the user inputs only SOME values?
Thanks for your help.
Yes, it will screw up.
I would go with the ifs, I don't see what's wrong with them:
var query = table;
if(criteria1 != null)
query = query.Where(x => x.Value1 == criteria1);
if(criteria2 != null)
query = query.Where(x => x.Value2 == criteria2);
If you have a lot of criteria you could use expressions, a dictionary and a loop to cut down on the repetitive code.
In an ASP.NET MVC app, chances are your user input is coming from a form which is being POSTed to your server. In that case, you can make use of strongly-typed views, using a viewmodel with [Required] on the criteria that MUST be provided. Then you wrap your method in if (ModelState.IsValid) { ... } and you've excluded all the cases where the user hasn't given you something they need.
Beyond that, if you can collect your criteria into a list, you can filter it. So, you could do something like this:
filterBy = userValues.Where(v => v != null);
var Coll = (from obj in table where filterBy.Contains(value1) select obj);
You can make this more complex by having a Dictionary (or Lookup for non-unique keys) that contains a user-entered value along with some label (an enum, perhaps) that tells you which field they're filtering by, and then you can group them by that label to separate out the filters for each field, and then filter as above. You could even have a custom SearchFilter object that contains other info, so you can have filters with AND, NOT and OR conditions...
Failing that, you can remember that until you trigger evaluation of an IQueryable, it doesn't hit the database, so you can just do this:
var Coll = (from obj in table where value1 == requiredCriteria select obj);
if(criteria1 != null)
{
query = query.Where(x => x.Value1 == criteria1);
}
//etc...
if(criteria5 != null)
{
query = query.Where(x => x.Value5 == criteria5);
}
return query.ToList();
That first line applies any criteria that MUST be there; if there aren't any mandatory ones then it could just be var Coll = table;.
That will add any criteria that are provided will be applied, any that aren't will be ignored, you catch all the possible combinations, and only one query is made at the end when you .ToList() it.
As I understand of your question you want to centralize multiple if for the sake of readability; if I were right the following would be one of some possible solutions
Func<object, object, bool> CheckValueWithAnd = (x, y) => x == null ? true : x==y;
var query = from obj in table
where CheckValue(obj.value1, criteria1) &&
CheckValue(obj.value2, criteria2) &&
...
select obj;
It ls flexible because in different situations or scenarios you can change the function in the way that fulfill your expectation and you do not need to have multiple if.
If you want to use OR operand in your expression you need to have second function
Func<object, object, bool> CheckValueWithOr = (x, y) => x == null ? false : x==y;

Can not parse XML when getting Empty tag in xml

While parsing an XML in Blackberry using DOM i got an empty node like </image>.
while parsing is uses condition if ("image".equals(node.getNodeName().toString())) to check whether this is image node or not. but does not get inside the condition.
if i uses xyz.jpg then image name returned. so please tell me condition by which i can classify whether this is empty node or not.
NodeList childNodes = node.getChildNodes();
int numChildren = childNodes.getLength();
Node firstChild = childNodes.item(0);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE) {
if ("headline".equals(node.getNodeName().toString())) {
vecHeadline.addElement(firstChild.getNodeValue());
}
}
}
but when i get empty tag like this will not parse. actually i want to add a string where node is empty
As you are not providing the exception I'll have to guess.
node.getNodeName().toString() is where it is crashing.
Try checking first is node.getNodeName() is not null

Resources