Here is the json variable
jsonout = [{"city": "Springfield", "id": 1, "name": "Moes Tavern"}, {"city": "Springfield", "id": 2, "name": "Springfield Power Plant"}, {"city": "Fountain Lakes", "id": 3, "name": "Kath and Kim Pty Ltd"}]
The following command i am using to import json variable
es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')
The following is the error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-10faf5c5bb89> in <module>()
1 docs = [{'id': 2, 'name': 'Jessica Coder', 'age': 32, 'title': 'Programmer'}, {'id': 3, 'name': 'Freddy Tester', 'age': 29, 'title': 'Office Assistant'}]
----> 2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')
d:\nvk\USER\Anaconda2\lib\site-packages\pyelasticsearch\client.pyc in decorate(*args, **kwargs)
91 elif k in convertible_args:
92 query_params[k] = kwargs.pop(k)
---> 93 return func(*args, query_params=query_params, **kwargs)
94 return decorate
95 return decorator
d:\nvk\USER\Anaconda2\lib\site-packages\pyelasticsearch\client.pyc in bulk(self, actions, index, doc_type, query_params)
445 response = self.send_request('POST',
446 [index, doc_type, '_bulk'],
--> 447 body='\n'.join(actions) + '\n',
448 query_params=query_params)
449
<ipython-input-14-10faf5c5bb89> in <genexpr>((doc,))
1 docs = [{'id': 2, 'name': 'Jessica Coder', 'age': 32, 'title': 'Programmer'}, {'id': 3, 'name': 'Freddy Tester', 'age': 29, 'title': 'Office Assistant'}]
----> 2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')
TypeError: 'str' object is not callable
doc is likely the uncallable string. Usually jsonout doesn't sound like it should have functions.
You are missing something in your question - in the error example you have this code:
1 docs = [{'id': 2, 'name': 'Jessica Coder', ...}, {...}]
2 es.bulk((es.index_op(doc, id=doc('id')) for doc in jsonout), index='dbmysql', doc_type='person')
You have the docs variable in the line 1, but you have jsonout in the line 2.
And, if you put the docs variable instead of jsonout into the second line, you should get an error like 'dict' object is not callable because you have doc('id') (instead of doc['id']) and doc is a dictionary.
So I suspect that also something is wrong with your actual jsonout variable value - it is probably a list of strings instead of list of dictionaries.
Found the solution finally.
We can use json.loads to convert str object to json object.
json.loads(jsonout)
Related
Given the following API response from pinecone (https://www.pinecone.io/docs/api/operation/query/)
results = {'matches': [{'id': 'yral5m',
'metadata': {'subreddit': '2qkq6',
'text': 'Black Friday SaaS Deals - 2022'},
'score': 0.772717535,
'sparseValues': {},
'values': []},
{'id': 'yqypa5',
'metadata': {'subreddit': '2qkq6',
'text': 'B2B sales interface.'},
'score': 0.74192214,
'sparseValues': {},
'values': []}],
'namespace': ''}
i am simply trying to return the JSON results from a function. (from a service to a controller) and getting a range of errors:
doing so with:
return results yields RecursionError: maximum recursion depth exceeded in comparison
return json.dumps(results) yeilds TypeError: Object of type QueryResponse is not JSON serializable
*QueryResponse is the type returned from pinecone
return jsonpickle.encode(query_results) yeilds "null"
Very lost any advice would be appreciated!!
full code example:
Controller:
#router.post("/query/")
async def semantic_search(query: str, ):
result = await TopicQueryService.query(query)
return result
Service method:
#staticmethod
async def query(query) -> str:
index = PineConeService.get_or_create_index("openai")
embed = GPT3Service.get_embedding(query)
query_results = index.query(
vector=embed,
top_k=2,
include_metadata=True
)
return json.dumps(query_results)
Replacing Service Method query results with the logged response from index. query works fine eg below. Leading me to believe it is due to the QueryResponse object pinecone returns.
#staticmethod
async def query(query) -> str:
index = PineConeService.get_or_create_index("openai")
embed = GPT3Service.get_embedding(query)
logger.info(embed)
query_results = {'matches': [{'id': 'yral5m',
'metadata': {'subreddit': '2qkq6',
'text': 'Black Friday SaaS Deals - 2022'},
'score': 0.772717535,
'sparseValues': {},
'values': []},
{'id': 'yqypa5',
'metadata': {'subreddit': '2qkq6',
'text': 'B2B sales interface.'},
'score': 0.74192214,
'sparseValues': {},
'values': []}],
'namespace': ''}
return json.dumps(query_results)
Somewhat of a solution -> just iterate over and build an object to return, not ideal though
``blah = []
for x in query_results.matches:
blah.append({
"id": x.id,
"metadata": x.metadata,
"score": x.score
})
json.dumps(blah)``
You can use the method .to_dict() from the QueryResponse object:
#staticmethod
async def query(query) -> str:
index = PineConeService.get_or_create_index("openai")
embed = GPT3Service.get_embedding(query)
query_results = index.query(
vector=embed,
top_k=2,
include_metadata=True
)
return json.dumps(query_results.to_dict())
I am trying to access JSON data but getting the above error. My code is:
with open(filepath+"decompressed_twitter_lot1file1.txt", 'rb') as fh:
for line in fh:
object = json.loads(line)
urls_in_tweet = object['entities']['urls']
domains_in_tweet = []
print(urls_in_tweet)
for url in urls_in_tweet:
for key, value in url.items():
print(key,value)
domain = tldextract.extract(value).registered_domain
print("domain")
My output:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[{
'display_url': 'msnbc.com/rachel-maddow/…',
'indices': [67, 90],
'expanded_url': '//www.msnbc.com/rachel-maddow/watch/trump-admin-coverage-maxim-watch-what-they-do-not-what-they-say-66934341943',
'url': '//t/zHmMchTCIf'
}]
display_url msnbc.com / rachel - maddow / …
domain
indices[67, 90]
After this, I get this error.I dont understand why after indices key it is not printing anything.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-90-cb728568a4f1> in <module>
8 for key, value in url.items():
9 print(key,value)
---> 10 domain = tldextract.extract(value).registered_domain
11 print("domain")
~/.local/lib/python3.8/site-packages/tldextract/tldextract.py in extract(url, include_psl_private_domains)
294 url, include_psl_private_domains=False
295 ): # pylint: disable=missing-function-docstring
--> 296 return TLD_EXTRACTOR(url, include_psl_private_domains=include_psl_private_domains)
297
298
~/.local/lib/python3.8/site-packages/tldextract/tldextract.py in __call__(self, url, include_psl_private_domains)
214
215 netloc = (
--> 216 SCHEME_RE.sub("", url)
217 .partition("/")[0]
218 .partition("?")[0]
TypeError: expected string or bytes-like object
This is data is small part of Twitter API data. How can I access every key-value pair of this JSON data and load value in domain list?
json.loads expects a str hence the error
If you want to get the key-value pairs you can do this:
fs = [{'display_url': 'eonli.ne/33XF5V1', 'indices': [90, 113], 'expanded_url': 'eonli.ne/33XF5V1', 'url': 't.co/flhUdZcUzB'}]
for k,v in fs[0].items():
print(f"{k}, {v}")
fs[0] is a dictionary, get the items with items()
There is not need for json.loads here
Hi I would like to update some documents that match a query. So for each document I would like to update the field 'parent_id' if and only if this document have an ID greater then i.e. 6
for result in results:
db.update(set('parent_id', current_element_id),
result.get('id') > current_element_id )
error:
Traceback (most recent call last):
File "debug.py", line 569, in <module>
convertxml=parse(xmlfile, force_list=('interface',))
File "debug.py", line 537, in parse
parser.Parse(xml_input, True)
File "..\Modules\pyexpat.c", line 468, in EndElement
File "debug.py", line 411, in endElement
db.update(set('parent_id', current_element_id), result.get('id') > current_element_id )
File "C:\ProgramData\Miniconda3\lib\site-packages\tinydb\database.py", line 477, in update
cond, doc_ids
File "C:\ProgramData\Miniconda3\lib\site-packages\tinydb\database.py", line 319, in process_elements
if cond(data[doc_id]):
TypeError: 'bool' object is not callable
example of document that should be update:
...,
{'URI': 'http://www.john-doe/',
'abbr': 'IDD',
'affiliation': 'USA',
'closed': False,
'created': '2018-06-01 22:49:02.927347',
'element': 'distrbtr',
'id': 7,
'parent_id': None
},...
In the documentation of tinydb I see that I can use set. Otherwise if I don't use Set it will update all the document db.update(dict) which I don't want to.
Using the Docs using write_back to replace part of a document is better
>>> docs = db.search(User.name == 'John')
[{name: 'John', age: 12}, {name: 'John', age: 44}]
>>> for doc in docs:
... doc['name'] = 'Jane'
>>> db.write_back(docs) # Will update the documents we retrieved
>>> docs = db.search(User.name == 'John')
[]
>>> docs = db.search(User.name == 'Jane')
[{name: 'Jane', age: 12}, {name: 'Jane', age: 44}]
implementing it to my situation
for result in results:
if result['parent_id'] != None:
result['parent_id'] = current_element_id
db.write_back(results)
I want to save an array of objects passed from javascript through ajax to me database. This is my view code:
data2 = json.loads(request.raw_get_data)
for i in data2:
print(key)
obj = ShoppingCart(quantity = i.quantity , user_id = 3, datetime = datetime.now(), product_id = i.pk)
obj.save()
return render_to_response("HTML.html",RequestContext(request))
After the first line, i get this in my dictionary:
[{'model': 'Phase_2.product', 'fields': {'name': 'Bata', 'category': 2, 'quantity': 1, 'subcategory': 1, 'count': 2, 'price': 50}, 'imageSource': None, 'pk': 1}]
(Only one object in the array right now)
I want to be able access individual fields like quantity, id, etc in order to save the data to my database. When i debug this code, it gives a name error on 'i'. I also tried accessing the fields like this: data2[0].quantity but it gives this error: {AttributeError}dict object has no attribute quantity.
Edited code:
for i in data2:
name = i["fields"]["name"]
obj = ShoppingCart(quantity = i["fields"]["quantity"] , user_id = 3, datetime = datetime.now(), product_id = i["fields"]["pk"])
obj.save()
It might help you to visualise the returned dict with proper formatting:
[
{
'model': 'Phase_2.product',
'fields': {
'name': 'Bata',
'category': 2,
'quantity': 1,
'subcategory': 1,
'count': 2,
'price': 50
},
'imageSource': None,
'pk': 1
}
]
The most likely reason for your error is that you are trying to access values of of the inner 'fields' dictionary as if they belong to the outer i dictionary.
i.e.
# Incorrect
i["quantity"]
# Gives KeyError
# Correct
i["fields"]["quantity"]
Edit
You have the same problem in your update:
# Incorrect
i["fields"]["pk"]
# Correct
i["pk"]
The "pk" field is in the outer dictionary, not the inner "fields" dictionary.
You may try:
i['fields']['quantity']
The json.loads() returns you a dictionary, which should be accessed by key.
I want to add lines to the object account.bank.statement.line through other object But I get following error:
"dictionary update sequence element #0 has length 3; 2 is required"
Here is my code:
def action_account_line_create(self, cr, uid, ids):
res = False
cash_id = self.pool.get('account.bank.statement.line')
for exp in self.browse(cr, uid, ids):
company_id = exp.company_id.id
#statement_id = exp.statement_id.id
lines = []
for l in exp.line_ids:
lines.append((0, 0, {
'name': l.name,
'date': l.date,
'amount': l.amount,
'type': l.type,
'statement_id': exp.statement_id.id,
'account_id': l.account_id.id,
'account_analytic_id': l.analytic_account_id.id,
'ref': l.ref,
'note': l.note,
'company_id': l.company_id.id
}))
inv_id = cash_id.create(cr, uid, lines,context=None)
res = inv_id
return res
I changed it on that but then I ran into this error:
File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\workflow\wkf_expr.py", line 68, in execute
File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\workflow\wkf_expr.py", line 58, in _eval_expr
File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\tools\safe_eval.py", line 241, in safe_eval
File "C:\Program Files (x86)\OpenERP 6.1-20121029-003136\Server\server\.\openerp\tools\safe_eval.py", line 108, in test_expr
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
Code:
def action_account_line_create(self, cr, uid, ids, context=None):
res = False
cash_id = self.pool.get('account.bank.statement.line')
for exp in self.browse(cr, uid, ids):
company_id = exp.company_id.id
lines = []
for l in exp.line_ids:
res = cash_id.create ( cr, uid, {
'name': l.name,
'date': l.date,
'amount': l.amount,
'type': l.type,
'statement_id': exp.statement_id.id,
'account_id': l.account_id.id,
'account_analytic_id': l.analytic_account_id.id,
'ref': l.ref,
'note': l.note,
'company_id': l.company_id.id
}, context=None)
return res
This error raised up because you trying to update dict object by using a wrong sequence (list or tuple) structure.
cash_id.create(cr, uid, lines,context=None) trying to convert lines into dict object:
(0, 0, {
'name': l.name,
'date': l.date,
'amount': l.amount,
'type': l.type,
'statement_id': exp.statement_id.id,
'account_id': l.account_id.id,
'account_analytic_id': l.analytic_account_id.id,
'ref': l.ref,
'note': l.note,
'company_id': l.company_id.id
})
Remove the second zero from this tuple to properly convert it into a dict object.
To test it your self, try this into python shell:
>>> l=[(0,0,{'h':88})]
>>> a={}
>>> a.update(l)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
a.update(l)
ValueError: dictionary update sequence element #0 has length 3; 2 is required
>>> l=[(0,{'h':88})]
>>> a.update(l)
I was getting this error when I was updating the dictionary with the wrong syntax:
Try with these:
lineItem.values.update({attribute,value})
instead of
lineItem.values.update({attribute:value})
Not really an answer to the specific question, but if there are others, like me, who are getting this error in fastAPI and end up here:
It is probably because your route response has a value that can't be JSON serialised by jsonable_encoder. For me it was WKBElement: https://github.com/tiangolo/fastapi/issues/2366
Like in the issue, I ended up just removing the value from the output.
One of the fast ways to create a dict from equal-length tuples:
>>> t1 = (a,b,c,d)
>>> t2 = (1,2,3,4)
>>> dict(zip(t1, t2))
{'a':1, 'b':2, 'c':3, 'd':4, }
I got dictionary update sequence element #0 has length 3; 2 is required
When I was trying to convert a dict to a list using .values()
Solved it by using .items()
list(dict(new_row.items()))
To anyone following patrick collins video[1] on smart contract development who runs into this error code; in the brownie-config.yaml file I had an '=' instead of a '-' in the following line(s)
...
compiler:
solc:
remappings:
= '#openzeppelin=OpenZeppelin/openzeppelin- contracts#4.2.0'
The first '=' should be '-'. The second one is as it should be.
reference:
[1]: https://www.youtube.com/watch?v=M576WGiDBdQ