Cassandra: 'unicode' does not have the buffer interface - python

I try to use these both prepared statements in my django app:
READINGS = "SELECT * FROM readings"
READINGS_BY_USER_ID = "SELECT * FROM readings WHERE user_id=?"
I query against the db with:
def get_all(self):
query = self.session.prepare(ps.ALL_READINGS)
all_readings = self.session.execute(query)
return all_readings
def get_all_by_user_id(self, user_id):
query = self.session.prepare(ps.READINGS_BY_USER_ID)
readings = self.session.execute(query, [user_id])
return readings
The first of both works pretty well. But the second gives me:
ERROR 2015-07-08 09:42:56,634 | views::exception_handler 47 | ('Unable to complete the operation against any hosts', {<Host: localhost data1>: TypeError("'unicode' does not have the buffer interface",)})
Can anyone tell me what happened here? I understand, that there must be a unicode string somewhere that does not have a buffer interface. But which string is meant? My prepared statement?
Here is the stacktrace in addition:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/rest_framework/views.py", line 448, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/me/Workspace/project/Readings/views.py", line 36, in get_by_user_id
readings = self.tr_dao.get_all_by_user_id(user_id)
File "/Users/me/Workspace/project/Readings/dao.py", line 22, in get_all_by_user_id
readings = self.session.execute(query, [user_id], timeout=60)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 1405, in execute
result = future.result(timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cassandra/cluster.py", line 2967, in result
raise self._final_exception

if you are on python 2 this will probably fix it
def get_all_by_user_id(self, user_id):
query = self.session.prepare(ps.READINGS_BY_USER_ID)
readings = self.session.execute(query, [str(user_id)])
return readings

This is not working because your user_id is of type unicode. You can check it using
type(user_id)
If that is the case you should encode it to string:
str(user_id)
It will solve the issue.

Related

Not understanding why i am getting this error in python script

Hi I am using a python 3 script to do snmp set operation to one of switch.
The script is as follows.
import pysnmp
from pysnmp import hlapi
def cast(value):
try:
return int(value)
except (ValueError, TypeError):
try:
return float(value)
except (ValueError, TypeError):
try:
return str(value)
except (ValueError, TypeError):
pass
return value
def fetch(handler, count):
result = []
for i in range(count):
try:
error_indication, error_status, error_index, var_binds = next(handler)
if not error_indication and not error_status:
items = {}
for var_bind in var_binds:
items[str(var_bind[0])] = cast(var_bind[1])
result.append(items)
else:
raise RuntimeError('Got SNMP error: {0}'.format(error_indication))
except StopIteration:
break
return result
def construct_value_pairs(list_of_pairs):
pairs = []
for key, value in list_of_pairs.items():
pairs.append(hlapi.ObjectType(hlapi.ObjectIdentity(key), value))
return pairs
def set(target, value_pairs, credentials, port=161, engine=hlapi.SnmpEngine(), context=hlapi.ContextData()):
handler = hlapi.setCmd(
engine,
credentials,
hlapi.UdpTransportTarget((target, port)),
context,
*construct_value_pairs(value_pairs)
)
return fetch(handler, 1)[0]
If i run set('10.23.193.153', {'1.3.6.1.2.1.1.5.0': 'Test1'}, hlapi.CommunityData('public'))
The script is executed and the hostname on switch changes to Test1.
However if i do another set operation using
set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public'))
I am getting the following error.
The above oid changes username of the switch.
> ============ RESTART: C:/Users/regop/Desktop/SNMP Password Reset.py ============ Traceback (most recent call last): File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 53, in <module>
> set('10.23.193.153', {'1.3.6.1.4.1.11.2.14.11.5.1.16.19.1.2.1': 'admin'}, hlapi.CommunityData('public')) File
> "C:/Users/regop/Desktop/SNMP Password Reset.py", line 49, in set
> return fetch(handler, 1)[0] File "C:/Users/regop/Desktop/SNMP Password Reset.py", line 22, in fetch
> error_indication, error_status, error_index, var_binds = next(handler) File
> "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\sync\cmdgen.py",
> line 214, in setCmd
> cmdgen.setCmd(snmpEngine, authData, transportTarget, File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\hlapi\asyncore\cmdgen.py",
> line 239, in setCmd
> return cmdgen.SetCommandGenerator().sendVarBinds( File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\entity\rfc3413\cmdgen.py",
> line 249, in sendVarBinds
> v2c.apiPDU.setVarBinds(reqPDU, varBinds) File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 131, in setVarBinds
> apiVarBind.setOIDVal( File "C:\Users\regop\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pysnmp\proto\api\v1.py",
> line 42, in setOIDVal
> varBind.setComponentByPosition(1).getComponentByPosition(1).setComponentByType(val.getTagSet(),
> val, verifyConstraints=False, matchTags=False, matchConstraints=False,
> innerFlag=True) AttributeError: 'str' object has no attribute
> 'getTagSet'
Not sure what different I am doing here.
This unhandled exception feels like a bug in pysnmp, but that's irrelevant to your question.
My guess is that the problem is caused by your second OID not being resolved at a MIB and therefore the value (admin) not automatically casted into some SNMP type object.
The solution is one of:
Refer to your SNMP objects by their symbolic names (MIB, symbol, instance ID)
Keep using OIDs, but pre-load the MIB where that OID is defined
Keep using OIDs, but pass your values as SNMP objects so that no MIBs would be necessary
I have the same error, Because of my SNMP Device doesn't support 'integer' to write, just 'String'.
I suggest to check your OID address whether it supports 'String' to write or not.

can't pickle dictionary in django

I have a simple dictionary that i am trying to save to cache and looks like it django is trying to pickle:
podcasts = []
for i in items:
s = re.sub('[\s+]', '', str(i))
s2 = re.findall(r'<link/>(.*?)<itunes',s)[0]
item_obj = {}
item_obj['title'] = title
item_obj['url'] = s2
item_obj['created_at'] = created_at
item_obj['duration'] = duration
podcasts.append(item_obj)
This has a very simple format that outputs:
[{'title': "Podcast1", 'url': 'https://example.com\\n', 'created_at': 'Thu, 28 Dec 2017', 'duration': '00:30:34'}]
I am running this from a custom management command like this:
python3 manage.py podcast_job
I attempt to save to cache:
podcasts = get_podcasts()
print(podcasts)
cache.set('podcasts', podcasts)
I get the error:
File "podcast_job.py", line 13, in handle
cache.set('podcasts', podcasts)
File "python3.6/site-packages/django_redis/cache.py", line 33, in _decorator
return method(self, *args, **kwargs)
File "python3.6/site-packages/django_redis/cache.py", line 68, in set
return self.client.set(*args, **kwargs)
File "python3.6/site-packages/django_redis/client/default.py", line 109, in set
nvalue = self.encode(value)
File "python3.6/site-packages/django_redis/client/default.py", line 329, in encode
value = self._serializer.dumps(value)
File "python3.6/site-packages/django_redis/serializers/pickle.py", line 33, in dumps
return pickle.dumps(value, self._pickle_version)
RecursionError: maximum recursion depth exceeded while calling a Python object
If I try to save with a string I get no error and it saves fine:
cache.set('podcasts', str(podcasts))
How can I save the list of dictionaries and not get the error above?
If you are using datetime objects for created_at and duration, make sure you render them to strings.
Pickle does not deal well with functions.
Check out this answer for some insight: https://stackoverflow.com/a/1253813/4225229
You could serialize the result of the function (try json.dumps()) and cache that.
I converted the dictionary with json as Jacob suggested like this:
cache.set('podcasts', json.dumps(podcasts))

python : error handling Ordered dict with unicode data

My script migrates data from MySQL to mongodb. It runs perfectly well when there are no unicode columns included. But throws me below error when OrgLanguages column is added.
mongoImp = dbo.insert_many(odbcArray)
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/collection.py", line 711, in insert_many
blk.execute(self.write_concern.document)
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/bulk.py", line 493, in execute
return self.execute_command(sock_info, generator, write_concern)
File "/home/lrsa/.local/lib/python2.7/site-packages/pymongo/bulk.py", line 319, in execute_command
run.ops, True, self.collection.codec_options, bwc)
bson.errors.InvalidStringData: strings in documents must be valid UTF-8: 'Portugu\xeas do Brasil, ?????, English, Deutsch, Espa\xf1ol latinoamericano, Polish'
My code:
import MySQLdb, MySQLdb.cursors, sys, pymongo, collections
odbcArray=[]
mongoConStr = '192.168.10.107:36006'
sqlConnect = MySQLdb.connect(host = "54.175.170.187", user = "testuser", passwd = "testuser", db = "testdb", cursorclass=MySQLdb.cursors.DictCursor)
mongoConnect = pymongo.MongoClient(mongoConStr)
sqlCur = sqlConnect.cursor()
sqlCur.execute("SELECT ID,OrgID,OrgLanguages,APILoginID,TransactionKey,SMTPSpeed,TimeZoneName,IsVideoWatched FROM organizations")
dbo = mongoConnect.eaedw.mysqlData
tuples = sqlCur.fetchall()
for tuple in tuples:
odbcArray.append(collections.OrderedDict(tuple))
mongoImp = dbo.insert_many(odbcArray)
sqlCur.close()
mongoConnect.close()
sqlConnect.close()
sys.exit()
Above script migraates data perfectly when tried without OrgLanguages column in the SELECT query.
To overcome this, I have tried to use the OrderedDict() in another way but gives me a different type of error
Changed Code:
for tuple in tuples:
doc = collections.OrderedDict()
doc['oid'] = tuple.OrgID
doc['APILoginID'] = tuple.APILoginID
doc['lang'] = unicode(tuple.OrgLanguages)
odbcArray.append(doc)
mongoImp = dbo.insert_many(odbcArray)
Error Received:
Traceback (most recent call last):
File "pymsql.py", line 19, in <module>
doc['oid'] = tuple.OrgID
AttributeError: 'dict' object has no attribute 'OrgID'
Your MySQL connection is returning characters in a different encoding than UTF-8, which is the encoding that all BSON strings must be in. Try your original code but pass charset='utf8' to MySQLdb.connect.

Why peewee coerces select column to integer

I can't use sqlite function group_concat() in peewee. Here is complete snipet. Somehow peewee want to convert result of group_concat() to integer, while it is string ("1,2"). I can't find the way to suppress it.
from peewee import *
db = SqliteDatabase(':memory:')
class Test(Model):
name = CharField()
score = IntegerField()
class Meta:
database = db
db.create_tables([Test])
Test.create(name='A', score=1).save()
Test.create(name='A', score=2).save()
#select name, group_concat(score) from Test group by name
for t in Test.select(Test.name, fn.group_concat(Test.score)).order_by(Test.name):
pass
It produces following error:
Traceback (most recent call last):
File "C:\Users\u_tem0m\Dropbox\Wrk\sgo\broken.py", line 17, in <module>
for t in Test.select(Test.name, fn.group_concat(Test.score)).order_by(Test.name):
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1938, in next
obj = self.qrw.iterate()
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 1995, in iterate
return self.process_row(row)
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 2070, in process_row
setattr(instance, column, func(row[i]))
File "C:\Program Files\Python 3.5\lib\site-packages\peewee.py", line 874, in python_value
return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: '1,2'
Try adding a coerce(False) to your call to group_concat:
query = (Test
.select(Test.name, fn.GROUP_CONCAT(Test.score).coerce(False))
.order_by(Test.name))
for t in query:
pass
Peewee sees that Test.score is an integer field, so whenever a function is called on that column, Peewee will try to convert the result back to an int. The problem is that group_concat returns a string, so we must tell Peewee not to mess with the return value.
Just found what result of fn.group_concat(""+Test.score) don't cast to integer. But I think resulting sql maybe less optimal
SELECT "t1"."name", group_concat(? + "t1"."score") AS allscore FROM "test" AS t1 ORDER BY "t1"."name" ['']
Do anybody knows more elegant way?

Adding Item data to a DynamoDB table using boto does not work

I have been trying to add items to a DynamoDB table using boto, but somehow it doesn't seem to work. I tried using users.Item() and users.put_item but nothing worked. Below is the script that I have in use.
import boto.dynamodb2
import boto.dynamodb2.items
import json
from boto.dynamodb2.fields import HashKey, RangeKey, GlobalAllIndex
from boto.dynamodb2.layer1 import DynamoDBConnection
from boto.dynamodb2.table import Table
from boto.dynamodb2.items import Item
from boto.dynamodb2.types import NUMBER
region = "us-east-1"
con = boto.dynamodb2.connect_to_region(region)
gettables = con.list_tables()
mytable = "my_table"
if mytable not in gettables['TableNames']:
print "The table *%s* is not in the list of tables created. A new table will be created." % req_table
Table.create(req_table,
schema = [HashKey('username'),
RangeKey('ID', data_type = NUMBER)],
throughput = {'read': 1, 'write': 1})
else:
print "The table *%s* exists." % req_table
con2table = Table(req_table,connection=con)
con2table.put_item(data={'username': 'abcd',
'ID': '001',
'logins':'10',
'timeouts':'20'
'daysabsent': '30'
})
I tried this, the table gets created and it is fine. But when I try to put in the items, I get the following error message.
Traceback (most recent call last):
File "/home/ec2-user/DynamoDB_script.py", line 29, in <module>
'daysabsent':'30'
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 821, in put_item
return item.save(overwrite=overwrite)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/items.py", line 455, in save
returned = self.table._put_item(final_data, expects=expects)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/table.py", line 835, in _put_item
self.connection.put_item(self.table_name, item_data, **kwargs)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
body=json.dumps(params))
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
retry_handler=self._retry_handler)
File "/usr/lib/python2.7/dist-packages/boto/connection.py", line 954, in _mexe
status = retry_handler(response, i, next_sleep)
File "/usr/lib/python2.7/dist-packages/boto/dynamodb2/layer1.py", line 2882, in _retry_handler
response.status, response.reason, data)
boto.dynamodb2.exceptions.ValidationException: ValidationException: 400 Bad Request
{u'message': u'One or more parameter values were invalid: Type mismatch for key version expected: N actual: S', u'__type': u'com.amazon.coral.validate#ValidationException'}
Thank you.
From the error message you are getting, it sounds like you are trying to send string values for an attribute that is defined as numeric in DynamoDB.
The specific issue looks to be related to your Range Key ID which is defined as a numeric value N but you are sending it a string value '001'.
Looks like of of the values you are trying to load has empty value.
I got the same error when I was trying to load this. I got exception when partner_name property was a empty string.
try:
item_old = self.table.get_item(hash_key=term)
except BotoClientError as ex:
# if partner alias does not exist then create a new entry!
if ex.message == "Key does not exist.":
item_old = self.table.new_item(term)
else:
raise ex
item_old['partner_code'] = partner_code
item_old['partner_name'] = partner_name
item_old.put()

Categories