Neo.ClientError.Statement.ParameterMissing} {message: Expected parameter(s): username_} with Python - python

I have the following function and the following call (with the connection setup before it)
from neo4j import GraphDatabase
from pypher import Pypher
# from WebScraper import *
py = Pypher()
# server connection link
uri = "bolt://localhost:7687"
driver = GraphDatabase.driver(uri, auth=("neo4j", "cs411fpl"))
session = driver.session()
username = 'a'
varray = []
# adds a user node
def add_user(username_):
q1 = "CREATE (u:User) SET u.name= $username_"
nodes = session.run(q1)
add_user(username)
This leads to the error:
File "UserHandler.py", line 37, in <module>
add_user(username)
File "UserHandler.py", line 14, in add_user
nodes = session.run(q1)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/neo4j/work/simple.py", line 217, in run
self._autoResult._run(query, parameters, self._config.database, self._config.default_access_mode, self._bookmarks, **kwparameters)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/neo4j/work/result.py", line 101, in _run
self._attach()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/neo4j/work/result.py", line 202, in _attach
self._connection.fetch_message()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/neo4j/io/_bolt4.py", line 363, in fetch_message
response.on_failure(summary_metadata or {})
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/neo4j/io/_common.py", line 179, in on_failure
raise Neo4jError.hydrate(**metadata)
neo4j.exceptions.ClientError: {code: Neo.ClientError.Statement.ParameterMissing} {message: Expected parameter(s): username_}
Any suggestions would be great. Thanks!

You are missing the connectiong between Cypher and Python (a second argument to run). It is not enough to have spelled username_ the same way both places.
def add_user(username_):
q1 = "CREATE (u:User) SET u.name= $username_"
nodes = session.run(q1, username_=username_)
I think the following would work as well, notice how the second argument for run must describe the coupling between the Cypher and Python:
def add_user(username_):
q1 = "CREATE (u:User) SET u.name= $login"
nodes = session.run(q1, login=username_)
You might be able to find more here:
https://neo4j.com/developer/python/

Related

Python - Error in configuring config sync in clusters registered with Anthos

I am trying to configure Anthos Config Management feature in one of the GKE cluster registered with Google Anthos using python SDK. But unable to figure out the CreateFeatureRequest object to create the request. Can anyone help?
Python Version - 3.8.5
Libraries used - google-cloud-gke-hub==1.4.1
And getting the below error
Traceback (most recent call last):
File "list_config.py", line 62, in <module>
create_feature(client, parent, membership_name)
File "list_config.py", line 28, in create_feature
member_spec = MembershipFeatureSpec(
File "pythonProjectAnthos\venv\lib\site-packages\proto\message.py", line 585, in __init__
super().__setattr__("_pb", self._meta.pb(**params))
TypeError: Parameter to MergeFrom() must be instance of same class: expected google.cloud.gkehub.configmanagement.v1.MembershipSpec got MembershipSpec.
Python Code
from google.cloud import gkehub_v1
from google.oauth2 import service_account
from google.cloud.gkehub_v1.types import MembershipFeatureSpec
from google.cloud.gkehub_v1.types import feature
from google.cloud.gkehub_v1 import configmanagement_v1
projectId = 'xxxxxx'
location = 'global'
def create_feature(client, parent, name):
git_config = configmanagement_v1.GitConfig(
sync_repo="https://github.com/GoogleCloudPlatform/anthos-config-management-samples",
sync_branch="main",
policy_dir="multi-environments-kustomize/config-source/overlays/dev",
secret_type=None
)
config_sync = configmanagement_v1.ConfigSync(
git=git_config,
source_format='unstructured'
)
configmanagement = configmanagement_v1.MembershipSpec(
config_sync=config_sync,
version='1.11.2'
)
member_spec = MembershipFeatureSpec(
configmanagement=configmanagement
)
feature_spec = feature.Feature(
name=name,
membership_specs={
name: member_spec
}
)
logger.info("Creating feature")
# Initialize request argument(s)
request = gkehub_v1.CreateFeatureRequest(
parent=parent,
feature_id='configmanagement',
resource=feature_spec
)
# Make the request
operation = client.create_feature(request=request)
print("Waiting for operation to complete...")
response = operation.result()
print(response)
# Handle the response
print(response.pb)
cluster_name = 'xxxxxxxxxx'
credentials = service_account.Credentials.from_service_account_file('credentials.json')
client = gkehub_v1.GkeHubClient(credentials=credentials)
parent = 'projects/%s/locations/%s' % (projectId, location)
membership_name = '%s/memberships/%s' % (parent, cluster_name)
create_feature(client, parent, membership_name)

Why am I receiving this error using BackTrader on Python?

I am trying to learn how to use the backtrader module on Python. I copied the code directly from the website but receiving an error message.
Here is the website: https://www.backtrader.com/docu/quickstart/quickstart/
I downloaded S&P500 stock data from Yahoo Finance and saved it into an excel file named 'SPY'. Here is the code so far:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
# Import the backtrader platform
import backtrader as bt
if __name__ == '__main__':
# Create a cerebro entity
cerebro = bt.Cerebro()
# Datas are in a subfolder of the samples. Need to find where the script is
# because it could have been called from anywhere
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
datapath = os.path.join(modpath, 'C:\\Users\\xboss\\Desktop\\SPY.csv')
# Create a Data Feed
data = bt.feeds.YahooFinanceCSVData(
dataname=datapath,
# Do not pass values before this date
fromdate=datetime.datetime(2000, 1, 1),
# Do not pass values after this date
todate=datetime.datetime(2000, 12, 31),
reverse=False)
# Add the Data Feed to Cerebro
cerebro.adddata(data)
# Set our desired cash start
cerebro.broker.setcash(100000.0)
# Print out the starting conditions
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
# Run over everything
cerebro.run()
# Print out the final result
print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Here is the error that I am receiving:
C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\Scripts\python.exe C:/Users/xboss/PycharmProjects/BackTraderDemo/backtrader_quickstart.py
Traceback (most recent call last):
File "C:/Users/xboss/PycharmProjects/BackTraderDemo/backtrader_quickstart.py", line 39, in <module>
cerebro.run()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
data.preload()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 688, in preload
while self.load():
Starting Portfolio Value: 100000.00
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 479, in load
_loadret = self._load()
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 710, in _load
return self._loadline(linetokens)
File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feeds\yahoo.py", line 129, in _loadline
dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
ValueError: invalid literal for int() with base 10: '1/29'
Process finished with exit code 1
Does anyone have any suggestions? Any help would be greatly appreciated. Thank you so much for your time!
You get the error because of using custom csv with YahooFinanceCSVData method.
You should import them using GenericCSVData method.
data = btfeed.GenericCSVData(
dataname='SPY.csv',
fromdate=datetime.datetime(2000, 1, 1),
todate=datetime.datetime(2000, 12, 31),
nullvalue=0.0,
dtformat=('%Y-%m-%d'),
datetime=0,
high=1,
low=2,
open=3,
close=4,
volume=5,
openinterest=-1
)
For more information you can see the instruction here

D-Bus method GetAll not found?

As far as I'm aware, org.freedesktop.DBus.Properties.GetAll should work to get properties of an interface. For some reason, this doesn't seem to work on org.freedesktop.NetworkManager.Connections.Active. Any suggestions on how to make this code work?
The code:
import dbus
from gi.repository import GObject
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
loop = GObject.MainLoop()
def handle_nm_change(o):
if 'ActiveConnections' in o:
# This is a connection change, let's see if it's in our SSID list
# First, get the ActiveConnection that changed:
for c in o['ActiveConnections']:
# Get the active connection
dev = system_bus.get_object('org.freedesktop.NetworkManager', c)
# Attempt to get the properties of the connection.
devprops_iface = dbus.Interface(dev, dbus_interface='org.freedesktop.DBus.Properties')
devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
# if not devprops['Default']:
# ii = input('Device not default: ' + c)
# if ii == 'n':
# exit(0)
appath = devprops['SpecificObject']
if appath.startswith('/org/freedesktop/NetworkManager/AccessPoint'):
ap = system_bus.get_object('org.freedesktop.NetworkManager', appath)
ssid = ap.Get('org.freedesktop.NetworkManager.AccessPoint', 'Ssid',
dbus_interface=dbus.PROPERTIES_IFACE
)
print(ssid)
if __name__ == '__main__':
system_bus.add_signal_receiver(handle_nm_change,
'PropertiesChanged',
'org.freedesktop.NetworkManager'
)
loop.run()
The error:
ERROR:dbus.connection:Exception in handler for D-Bus signal:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 230, in maybe_handle_message
self._handler(*args, **kwargs)
File "so-mockup.py", line 18, in handle_nm_change
devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python3/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist
Signal 'PropertiesChanged' is sent also when a connection is deactivated. Then the object path for the "deactivated" connection does not exist anymore. That's why you are receiving the UnknownMethod exception.
Before getting properties of the ActiveConnection make sure it still exists.
Try the changes below:
# Get ActiveConnection upon receiving a PropertiesChanged signal
nm = system_bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
nm_iface = dbus.Interface(nm, dbus_interface='org.freedesktop.DBus.Properties')
nms = nm_iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
# Check current active connections
for ac in nms:
print("ActiveConnection: "+ac)
# This is a connection change, let's see if it's in our SSID list
# First, get the ActiveConnection that changed:
for c in o['ActiveConnections']:
# Do whatever if c is in "ActiveConnections"
if c in nms:

ETE2: adding image to nodes

I have been trying to add different images to nodes in a phylogenetic tree using the ete2 software in Python, but have no success.
from ete2 import Tree, TreeStyle, NodeStyle, TextFace, faces, add_face_to_node, AttrFace
ts.show_leaf_name = True
ts.show_branch_support = True
nw = """
(((Dre:0.008339,Dme:0.300613)1.000000:0.596401,
(Cfa:0.640858,Hsa:0.753230)1.000000:0.182035)1.000000:0.106234,
((Dre:0.271621,Cfa:0.046042)1.000000:0.953250,
(Hsa:0.061813,Mms:0.110769)1.000000:0.204419)1.000000:0.973467);
"""
t = Tree(nw)
img_path = "/home/leonard/Desktop/img_faces/"
humanFace = faces.ImgFace(img_path+"human.png")
mouseFace = faces.ImgFace(img_path+"mouse.png")
def my_layout(node):
if name.startswith("Dre"):
faces.add_face_to_node(humanface, node, column=1)
t.show(my_layout)
ts = TreeStyle()
t.render("img_faces.png", w=600, tree_style = ts)
These are error messages that I have been getting:
File "abc1.py", line 34, in <module>
t.show(my_layout)
File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/coretype/tree.py", line 1283, in show
drawer.show_tree(self, layout=layout, tree_style=tree_style)
File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/drawer.py", line 84, in show_tree
tree_item, n2i, n2f = render(t, img)
File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 258, in render
set_style(n, layout_fn)
File "/usr/local/lib/python2.7/dist-packages/ete2-2.1rev544-py2.7.egg/ete2/treeview/qt4_render.py", line 746, in set_style
layout_func(n)
File "abc1.py", line 29, in my_layout
if name.startswith("Dre"):
NameError: global name 'name' is not defined
Any help is much appreciated!
This is the solution from Jaime Huerta Cepas in google group, and I quote:
"There is a general Python programming error in your script ("name" variable does not exist). I guess that what you meant is "node.name.startswith()" instead of "name.startswith()""
It worked.
According to the documentation (ete2) you need to create a faces ready to read the name attribute of nodes.
Try to add the following to your code:
#nameFace = faces.TextFace(open("text").readline().strip(), fsize=20, fgcolor="#009000")
nameFace = faces.AttrFace("name", fsize=20, fgcolor="#009000")

Making ORM with Python's Storm

The question is based on the thread, since I observed that Storm allows me reuse my SQL-schemas.
How can you solve the following error message in Storm?
The code is based on Jason's answer and on Storm's manual.
import os, pg, sys, re, psycopg2, storm
from storm.locals import *
from storm import *
class Courses():
subject = Unicode()
database = create_database("postgres://naa:123#localhost:5432/tk")
store = Store(database)
course = Courses()
course.subject = 'abcd'
store.add(course)
It gives you
Traceback (most recent call last):
File "<stdin>", line 13, in <module>
File "/usr/lib/python2.6/dist-packages/storm/store.py", line 245, in add
obj_info = get_obj_info(obj)
File "/usr/lib/python2.6/dist-packages/storm/info.py", line 40, in get_obj_info
obj_info = ObjectInfo(obj)
File "/usr/lib/python2.6/dist-packages/storm/info.py", line 162, in __init__
self.cls_info = get_cls_info(type(obj))
File "/usr/lib/python2.6/dist-packages/storm/info.py", line 51, in get_cls_info
cls.__storm_class_info__ = ClassInfo(cls)
File "/usr/lib/python2.6/dist-packages/storm/info.py", line 69, in __init__
raise ClassInfoError("%s.__storm_table__ missing" % repr(cls))
storm.exceptions.ClassInfoError: <type 'instance'>.__storm_table__ missing
This suggests to me that some module is missing. There is no module instance in Storm.
I'll leave out the connection details because I'm not terribly familiar with Postgres.
from storm.locals import *
class Courses(object):
__storm_table__ = 'courses'
pkey = Int(primary=True)
course_nro = Unicode()
course = Courses()
course.course_nro = 'abcd'
store.add(course)
store.commit()
Of course, if you want to do the constructor and initialization on one line, you can use pysistence's expandos:
from storm.locals import *
from pysistence import Expando
class Courses(Expando):
__storm_table__ = 'courses'
pkey = Int(primary=True)
course_nro = Unicode()
course = Courses(course_nro='abcd')
store.add(course)
store.commit()

Categories