parameters aren't being read in py2neo - python-2.7

I'm trying to query based on node number and return a list of properties. The following cypher query works in the neo4j browser but when I try to pass the same cypher query via py2neo I get:
"Expected a property container or number here, but got:91"
where "91" is the node number I'm querying on.
an excerpt from my code:
def neighbor_finder(a):
try:
graph_db = neo4j.GraphDatabaseService(url)
query = neo4j.CypherQuery(graph_db,
"""CYPHER 2.0
MATCH (n)-[r]-(m)
WHERE ID(n) = {t}
WITH collect (Distinct m.size) as sizes, collect (Distinct m.weight) as weights, collect (Distinct m.color) as colors, collect (distinct n.label) as node_
RETURN sizes, weights, colors, node_
""")
result = query.execute(t=a)
for r in result:
column = [str(item) for item in r.columns]
value = [str(item) for item in r.values]
db_dict={k: v for k, v in zip(column, value)}
for x, y in db_dict.items():
print x, y
except Exception as e:
print e

Can you provide details of the type and value of the a argument that is passed into this function? Without it I cannot see what py2neo is passing into the query parameters.

argument "a" isn't being read because it needs to be declared as an integer, cypher/py2neo are currently reading it as a string.

Related

Creating efficiently a dictionary in pyspark using list derived from df columns

I created this function in python using pandas dataframe, and I'd like to use it also in spark.
What I'm doing with this function is :
converting the df column to a list ( t1 )
converting the unique values of the column to a list ( t2 )
creating a list for each unique value of each feature ( t ). this list takes value 1 when the unique value is present in t1, 0 otherwise.
at the end the result is a dictionary with the unique values of each feature as key and as argument a list with value 1 when the key (the unique value) appears and 0 otherwise.
feat_list is just a list with all the column names.
def binary_dict(pandas_df, feat_list):
dict_feature = dict()
for col in feat_list:
t1 = pandas_df[col].tolist()
t2 = pandas_df[col].unique().tolist()
for value in t2:
t = []
for i in range (0, len(t1)):
if value == t1[i]:
t.append(1)
else:
t.append(0)
cc = str(col)
vv = "_" + str(value)
cv = cc + vv
dict_feature[cv] = t
return dict_feature
I tried using
t1 = df.select("col_name").rdd.flatMap(list).collect()
for creating t1 but it took over 20 minutes to create the list for a single column. I got something like 100 columns. Is there a way to convert this function to spark efficiently?
Thanks everyone for the answers!
PS: I'm using synapse analytics by azure/microsoft, Python 3.8 and pyspark 3.1.

Calling a PL/SQL procedure from django with callproc

I need to call a procedure in PL/SQL from an API in Django. I use the callproc and the right values, but get the error:
"PLS-00306: wrong number or types of arguments in call"
In Oracle I have:
PROCEDURE new_payment(pv_id IN VARCHAR2,
parr_user IN OWA.vc_arr,
parr_date_reg IN OWA.vc_arr,
parr_d_value IN OWA.vc_arr,
parr_descr IN OWA.vc_arr,
parr_type IN OWA.vc_arr,
pi_gerar_ref_mb IN PLS_INTEGER DEFAULT 0,
pv_data_limite_ref_mb IN VARCHAR2 DEFAULT NULL)
In models.py I have:
class PAYMENT():
def new_payment(self, id, user, date_reg, value, descr, type):
cur = connection.cursor()
ref = cur.callproc("PAYMENT.new_payment", [id, user, date_reg, value,
descr, type])
cursor.close()
return ref
In views.py:
pay=PAYMENT()
x=pay.new_payment('123', '111', '2019-07-23', '10', 'test1', 'teste2')
At this point, i get the error:
"ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'NEW_PAYMENT'"`
Any tip in what am I doing wrong?
OWA.vc_arr looks like a collection, so you need to pass a collection as variable.
in the cx_Oracle documentation you can find the function Cursor.arrayvar(dataType, value\[, size\])
Create an array variable associated with the cursor of the given type and size and return a variable object. The value is either an integer specifying the number of elements to allocate or it is a list and the number of elements allocated is drawn from the size of the list. If the value is a list, the variable is also set with the contents of the list. If the size is not specified and the type is a string or binary, 4000 bytes is allocated. This is needed for passing arrays to PL/SQL (in cases where the list might be empty and the type cannot be determined automatically) or returning arrays from PL/SQL.
so your code could looks like:
class PAYMENT():
def new_payment(self, id, user, date_reg, value, descr, type):
cur = connection.cursor()
u = cur.arrayvar(cx_Oracle.STRING,user)
ds = cur.arrayvar(cx_Oracle.STRING,date_reg)
v = cur.arrayvar(cx_Oracle.STRING,value)
d = cur.arrayvar(cx_Oracle.STRING,descr)
t = cur.arrayvar(cx_Oracle.STRING,type)
ref = cur.callproc("PAYMENT.new_payment", [id, u, ds, v,
d, t])
cursor.close()
return ref

PostgreSQL empty list VALUES expression

I am trying to take a list of points, and query a geospatial database, to find all matching rows.
I have a computed SQL statement that looks like this:
cursor = connection.cursor()
cursor.execute(
'''
SELECT g.ident
FROM (VALUES %s) AS v (lon, lat)
LEFT JOIN customers g
ON (ST_Within(ST_SetSRID(ST_MakePoint(v.lon, v.lat), %s), g.poly_home));
''', [AsIs(formatted_points), SRID]
)
Here is an example of what the formatted_points variable looks like:
(-115.062,38.485), (-96.295,43.771)
So, when that is inserted into the SQL expression, then VALUES expression reads:
(VALUES (-115.062,38.485), (-96.295,43.771)) AS v (lon, lat)
So far so good. However, when the list of points is empty, the VALUES expression looks like this:
(VALUES ) AS v (lon, lat)
.. which causes me to get this error:
django.db.utils.ProgrammingError: syntax error at or near ")"
In other words, (VALUES ) is not legal SQL.
Here's the question: How do I represent an empty list using VALUES? I could special case this, and just return an empty list when this function is passed an empty list, but that doesn't seem very elegant.
I have looked at the PostgreSQL manual page for VALUES, but I don't understand how to construct an empty VALUES expression.
If you can put your lons and lats in separate arrays, you could use arrays with unnest:
select * from unnest(ARRAY[1, 2, 3]::int[], ARRAY[4, 5, 6]::int[]) as v(lon, lat);
lon | lat
-----+-----
1 | 4
2 | 5
3 | 6
(3 rows)
select * from unnest(ARRAY[]::int[], ARRAY[]::int[]) as v(lon, lat);
lon | lat
-----+-----
(0 rows)
You'll have to cast the arrays to the appropriate type (probably not int[]). Postgres will guess the type if the arrays aren't empty, but it will throw an error if they are empty and you don't cast them to a specific type.

Can I assign position of item in list?

ex = ['$5','Amazon','spoon']
I want to re-order this list, website - item - price.
Can I assign the index, for instance, ex.index('Amazon') = 1?
I'd like the result to be ['Amazon','spoon','$5']
I found information on how to swap positions, but I would like to know if I can assign an index for each item myself.
You cannot assign an index to an item, but you can build a permuted list according to a permutation pattern:
ex = ['$5','Amazon','spoon']
order = [1, 2, 0]
ex_new = [ex[i] for i in order]
print(ex_new)
#['Amazon', 'spoon', '$5']
Alternatively, you can overwrite the original list in place:
ex[:] = [ex[i] for i in order]
print(ex)
#['Amazon', 'spoon', '$5']

In Spark, does the filter function turn the data into tuples?

Just wondering does the filter turn the data into tuples? For example
val filesLines = sc.textFile("file.txt")
val split_lines = filesLines.map(_.split(";"))
val filteredData = split_lines.filter(x => x(4)=="Blue")
//from here if we wanted to map the data would it be using tuple format ie. x._3 OR x(3)
val blueRecords = filteredData.map(x => x._1, x._2)
OR
val blueRecords = filteredData.map(x => x(0), x(1))
No, all filter does is take a predicate function and uses it such that any of the datapoints in the set that return a false when passed through that predicate, then they are not passed back out to the resultant set. So, the data remians the same:
filesLines //RDD[String] (lines of the file)
split_lines //RDD[Array[String]] (lines delimited by semicolon)
filteredData //RDD[Array[String]] (lines delimited by semicolon where the 5th item is Blue
So, to use filteredData, you will have to access the data as an array using parentheses with the appropriate index
filter will not change the RDD - filtered data would still be RDD(Array[String])