python ultrajson: how to use? - python

I've just installed ultrajson (ujson) to see if I can't get the json decoding to go faster (string to object). However, I'm not seeing any examples of how to use it.
with regular json it's just
import json
my_object = json.loads(my_string)

Change the import statement to import ujson as json
Then you can leave the other parts of your program as it is.

Related

Python reading responses and validating the result

currently I am stuck with being able to print out the result gotten from the API, but not being able to alter nor read them without parsing it into a text file.
Furthermore, I wouldn't need all of the information that the API provides and would be great if I can only have the match_id.
The response from the API:Result.
From the result I would only need the match_id and after I have gotten the match_id, I would compare it with a list of string e.g. 3238829394, 3238829395 and more, to check whether does any of the value are similar to mine, and if it's similar, the system would then alert me
I have found a way of doing it by passing the results into a text file, then comparing it with the list that I have.
The code for getting the response:
import dota2api
import json
import requests
api = dota2api.Initialise("[Value API][2]")
reponse = api.get_match_history_by_seq_num(start_at_match_seq_num=2829690055, matches_requested=1)
response = str(hist)
f = open('myfile.txt', 'w')
f.write(response)
f.close()
However I am hoping to find a faster and better way to do this process, as it is very time consuming and unstable. Thank you.
You are getting a JSON file back from that API. In python all data can be accessed directly without parsing it.
The response will be something like (sorry, but in that image I cannot copy paste to read the JSON properly):
for match in response['matches']:
if is_similar(match['match_id']):
do_something_cool_here
I think that should do what you need. If you give the answer as string I can help you building the code properly, but I guess you get the idea of what I am trying to say there :)
Hope it helps!
EDIT:
We talked by private and this works:
import dota2api
import requests
api = dota2api.Initialise("API_KEY")
response = api.get_match_history_by_seq_num(start_at_match_seq_num=SEQ_NUM, matches_requested=1)
match_id_check = MATCH_ID
for match in response['matches']:
if match_id_check == match['match_id']:
print(match)
with API_KEY, SEQ_NUM and MATCH_ID to configure

Access a JSON in Python

I'm new at Python as well as JQuery.
I have the next JSON in JS
var intervalos= {"variables":{
"nombreVariables":nombreVariables,
"extremoInferior":fromarray,
"extremoSuperior":toarray,
"step":steparray,
"random":randomarray
}};//intervalos
I did in Python
parsed_input = json.loads(self.intervalos)
How can I access to the structure? (I know that this is a list/dictionary)
like this
intervalos['variables']['nombreVariables'][i];
Presumably, the JS is remote. Assuming its accessible at http://example.com/json one would import it into python as follows:
import requests
parsed_input = requests.get('http://example.com/json').json()
for i in xrange(0, len(parsed_input['variables']['nombreVariables']):
print parsed_input['variables']['nombreVariables'][i]
You'll need requests to make this work in python2. With python 3.x, your syntax will be slightly different.

Python - JSON Load from file not working

So I am writing a basic multipurpose script which uses json to import a dictionary from a file but for some reason it doesn't save properly. I've looked all over and can't find anything relating to my exact problem.
Here is my code:
import json
dicti = json.loads(open('database.db'))
print(str(dicti))
But then I get this error:
TypeError: JSON object must be str, not TextIOWrapper.
So does anyone have any ideas on what the problem is? Thanks in Advance.
Note: Currently the file only has inside it:
{}
You want json.load for loading a file. json.loads is for loading from a string.

How to save Python in-memory dictionary to a file as a Python source code?

How to get Python source code representation of in-memory Python dictionary?
I decided to ask this question after reading Thomas Kluyver's comment on Rob Galanakis' blog post titled Why bother with python and config files? In his comment Thomas states
But if you want any way to change settings inside the application
(like a preferences dialog), there’s no good way to automatically
write a correct Python file.
Assuming it uses only "basic" Python types, you can write out the repr() of the structure, and then use ast.literal_eval() to read it back in after.
As the article says, you're better off using JSON/YAML or other formats, but if you seriously wanted to use a Python dict and are only using basic Python types...
Writing out (attempt to use pformat to try and make it more human readable):
from pprint import pformat # instead of using repr()
d = dict(enumerate('abcdefghijklmnopqrstuvwxyz'))
open('somefile.py').write(pformat(d))
Reading back:
from ast import literal_eval
d = literal_eval(open('somefile.py').read())

Does converting json to dict with eval a good choice?

I am getting a json object from a remote server, and converting it to a python string like this:
a = eval(response)
Is this stupid in any way, or do I have a better option?
Using eval is not a good way to process JSON:
JSON isn't even valid Python, because of true, false, and null.
eval will execute arbitrary Python code, so you are at the mercy of malicious injection of code.
Use the json module available in the standard library instead:
import json
data = json.loads("[1, 2, 3]")
If you're using a version of Python older than 2.6, you'll need to download the module yourself. It's called simplejson and can be downloaded from PyPi.
Yes, very. Use a json decoder instead:
>>> from simplejson import loads
>>> loads(response)

Categories