Tree view from json Django - python

I have a backend that gives me a json response like this
{
"compiler": {
"type": "GCC",
"version": "5.4"
},
"cpu": {
"architecture": "x86_64",
"count": 4
}
}
I need to visualize this response in the form of a tree. What should I do?
Maybe try to transform it to django-model? Or something else?

If you just want it printed with indentation, the json module can already do this using dumps(), as shown here. Alternatively, you can use pprint.

Related

PyMongo: Is there a way to add data to a existing document in MongoDB using python?

I have a database 'Product'. Which contains a collection name 'ProductLog'. Inside this collection , there are 2 documents in the following format:
{
"environment": "DevA",
"data": [
{
"Name": "ABC",
"Stream": "Yes"
},
{
"Name": "ZYX",
"Stream": "Yes"
}
]
},
{
"environment": "DevB",
"data": [
{
"Name": "ABC",
"Stream": "Yes"
},
{
"Name": "ZYX",
"Stream": "Yes"
}
]
}
This gets added as 2 documents in collection. I want to append more data in the already existing document's 'data' field in MongoDB using python. Is there a way for that? I guess update would remove the existing fields in "data" field or may update a whole document.
For example: Adding one more array in EmployeeDetails field, while the earlier data in EmployeeDetail also remains.
I want to show how you can append more data in the already existing document 'data' field in MongoDB using python:
First install pymongo:
pip install mongoengine
Now let's get our hands dirty:
from pymongo import MongoClient
mongo_uri = "mongodb://user:pass#mongosrv:27017/"
client = MongoClient(mongo_uri)
database = client["Product"]
collection = "ProductLog"
database[collection].update_one({"environment": "DevB"}, {
"$push": {
"data": {"Name": "DEF", "Stream": "NO"}
}
})
There is a SQL library in Python language through which you can insert/add your data in your desired database. For more information, check out the tutorial

How can I format my Python dictionary to match the below JSON format using json.dumps()

I am using python to complete some scripting tasks and I have to send some data to another program using my scripts stdout. In order to communicate, I have a set example of how the JSON needs to be formatted but I am struggling to replicate the format:
{
"Bookmarks": [{
"BookmarkPath": "path/foo/bar",
"HtmlColor": "#7FCC99",
"Comment": "AAA",
"Sha1": "xxx"
}]
}
My code is using the standard json library and the method .dumps to take my dictionary that looks like:
dict = {
'Bookmarks':{
'BookmarkPath': "path/foo/bar",
'HtmlColor': "#7FCC99",
'Comment': "AAA",
'Sha1': "xxx"
}
}
Ultimately it's formatting as such, which doesn't work:
{
"Bookmarks": {
"BookmarkPath": "path/foo/bar",
"HtmlColor": "#7FCC99",
"Comment": "AAA",
"Sha1": "xxx"
}
}
It's a subtle difference (the sqaure brackets being missing is the issue) but I am not sure to fix it. I am new to dictionaries in Python so please be kind :)
Thanks for any suggestions.
The square brackets mean that it's a list. Do the same in the dictionary:
my_dict = {
'Bookmarks':[{
'BookmarkPath': "path/foo/bar",
'HtmlColor': "#7FCC99",
'Comment': "AAA",
'Sha1': "xxx"
}]
}

How should I add a geojson in URL for static image api?

I have this geojson (all this code is in python)
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"stroke": "#000000",
"fill": "#005776",
"fill-opacity": 1.0
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-81.26581704096897, 28.37974376331988],
[-81.26601725837781, 28.37977498699149],
[-81.26613780689904, 28.37940694447166],
[-81.26594365491499, 28.3793572200485],
[-81.26581704096897, 28.37974376331988]
]
]
}
}
]
}
I am encoding it like this:
geojson_string = json.dumps(geojson, separators=(',', ':'))
geojson_encoded = urllib.parse.quote(f"{{{geojson_string}}}")
And getting this string:
%7B%22type%22%3A%22FeatureCollection%22%2C%22features%22%3A%5B%7B%22type%22%3A%22Feature%22%2C%22properties%22%3A%7B%22stroke%22%3A%22%23000000%22%2C%22fill%22%3A%22%23005776%22%2C%22fill-opacity%22%3A1%7D%2C%22geometry%22%3A%7B%22type%22%3A%22Polygon%22%2C%22coordinates%22%3A%5B%5B%5B-81.26581704096897%2C28.37974376331988%5D%2C%5B-81.26601725837781%2C28.37977498699149%5D%2C%5B-81.26613780689904%2C28.37940694447166%5D%2C%5B-81.26594365491499%2C28.3793572200485%5D%2C%5B-81.26581704096897%2C28.37974376331988%5D%5D%5D%7D%7D%7D
Then I am making the url like this:
url = f"https://api.mapbox.com/styles/v1/{user}/{style}/static/geojson(geojson_encoded)/auto/640x360?{access_token}"
But I am getting this error:
message: "Failed parsing geojson"
Can someone help me to know what I am doing wrong ?
The problem appears to lie with how your Python code is encoding and/or making the request against the API, so I would recommend you double check that the request being made is what you expect it to be.
If I take your unencoded geojson object, and include it directly within a cURL request (making sure to encode just the necessary forbidden use of # for colors), then I don't have any issues receiving a valid response:
https://api.mapbox.com/styles/v1/mapbox/light-v10/static/geojson({"type":"FeatureCollection","features":[{"type":"Feature","properties":{"stroke":"%23000000","fill":"%23005776","fill-opacity":1},"geometry":{"type": "Polygon","coordinates":[[[-81.26581704096897,28.37974376331988],[-81.26601725837781,28.37977498699149],[-81.26613780689904,28.37940694447166],[-81.26594365491499,28.3793572200485],[-81.26581704096897,28.37974376331988]]]}}]})/auto/630x360?access_token=my.token
The above request responds with your desired image (albeit, without the custom user style):
⚠️ disclaimer: I currently work for Mapbox ⚠️

JIRA attachment with REST API

I want to upload attachment while creating JIRA ticket, I tried the format below but its not working with syntax error given for the file. Do you have any idea how to create this in a json file ?
data = {
"fields": {
"project": {
"key": project
},
"attachments" : [
"file": "C:\data.txt"
],
"summary": task_summary,
"issuetype": {
"name": "Task"
}
}
}
You can only set issue fields during creation: https://docs.atlassian.com/jira/REST/latest/#d2e2786
You can add attachments to already existing issues by posting the content itself: https://docs.atlassian.com/jira/REST/latest/#d2e2035
Sou you have to do it in two steps.
It works now, i was not using rest/api/2/issue/DEV-290/attachments

ElasticSearch-Haystack: Spanish Tokenizer "Fails"

I'm using:
Haystack - 2.1.0
ElasticSearch - 0.90.3
pyelasticsearch - 0.6
I've configured a custom backend to change default Elasticsearch settings and use Spanish analyzer.
I'm using this settings for Elasticsearch:
"settings" : {
"index": {
"uuid": "IPwcMthwRpSJzpjtarc9eQ",
"analysis": {
"analyzer": {
"default": {
"filter": ["standard", "lowercase", "asciifolding", ],
"tokenizer": "standard"
}
}
},
"number_of_replicas": "1",
"number_of_shards": "10",
}
},
"analyzer": {
"spanish": {
"tokenizer": "standard",
"filter": [
"lowercase",
"spanish_stop",
"spanish_keywords",
"spanish_stemmer"
]
}
}
I read this settings in some answer here. When I apply this settings to ElasticSearch and reindex my models I get a behaviour that I'm not sure I understand.
I have some objects with names like "Ciencias" and others like "Ciéncies" When I do a search like "ciencias" I receive objects with names like "Ciencias" and "Ciéncies", and the same happens when I search for "ciencies" or "ciéncies".
I want ElasticSearch to ignore accents, that's why I'm using asciifolding, and using spanish tokenizer because most of text is in spanish. I don't understand why using different words like "cienciAs" and "cienciEs" receive same results.
Why is this happening ? Is because a default ngram analyzer that is splitting the words ?
Why searching for "cienciAs" I get object with name like "ciénciEs" as results ?
Probably because the stemmer is doing its job. If you want to find out what happens while tokenising or stemming, install the inquisitor plugin and go to the Analyzers tab (see here)
Finally I removed the Spanish analyzer and everything began to work as expected.
Now I'm using only Asciifolding and Lowercase filters and accents and ñ's are being indexed well, and I don't have the issue with ciencias and ciencies.

Categories

Resources