Strange JSON keyerror python 3 - json

I have been getting some strange key errors when trying to load a value after a few game ticks in my case, there is no modification going on cause most of it gets loaded from class internally at initiation, here is an quick example of the error im getting when trying to access.
also link to source is here, im pretty new and just have been coding for aint to long so please no judgement.
{'calleas': {'player': {'x': 5, 'y': 5, 'id': 'npc_1'}, '1': {'x': 6, 'y': 8, 'id': 'npc_2'}, '2': {'x': 5, 'y': 8, 'id': 'npc_3'}}, 'calleas_forest': {'player': {'x': 55, 'y': 5, 'id': 'npc_1'}}}
{'calleas': {'0': {'object_id': 7, 'x': 0, 'y': 5, 'walkable': True, 'gamestate': 'callaes_forest'}, '1': {'object_id': 7, 'x': 0, 'y': 4, 'walkable': True, 'gamestate': 'callaes_forest'}}, 'callaes_forest': {'0': {'object_id': 7, 'x': 56, 'y': 5, 'walkable': True, 'gamestate': 'callaes_forest'}, '1': {'object_id': 7, 'x': 56, 'y': 4, 'walkable': True, 'gamestate': 'callaes_forest'}}}
None
Traceback (most recent call last):
File "C:/Users/Jannick/PycharmProjects/NewRemake/Game.py", line 39, in <module>
g.start_game()
File "C:/Users/Jannick/PycharmProjects/NewRemake/Game.py", line 33, in start_game
self.update()
File "C:\Users\Jannick\PycharmProjects\NewRemake\GameHandler.py", line 106, in update
self.entity_handler.update()
File "C:\Users\Jannick\PycharmProjects\NewRemake\Entity\EntityHandler.py", line 21, in update
self.game.player.update()
File "C:\Users\Jannick\PycharmProjects\NewRemake\Entity\Entity.py", line 35, in update
self.move(x=1)
File "C:\Users\Jannick\PycharmProjects\NewRemake\Entity\Player.py", line 47, in move
self.game.map_handler.load(self.game.map_handler.new_map_name(dest_x, dest_y))
File "C:\Users\Jannick\PycharmProjects\NewRemake\World\MapBuilder.py", line 26, in load
self.get_map_configuration(map_config)
File "C:\Users\Jannick\PycharmProjects\NewRemake\World\MapBuilder.py", line 46, in get_map_configuration
"entitys": self.all_map_entitys[map_config]
KeyError: 'callaes_forest'
{'calleas': {'player': {'x': 5, 'y': 5, 'id': 'npc_1'}, '1': {'x': 6, 'y': 8, 'id': 'npc_2'}, '2': {'x': 5, 'y': 8, 'id': 'npc_3'}}, 'calleas_forest': {'player': {'x': 55, 'y': 5, 'id': 'npc_1'}}}

Related

Semantic segmentation labeling

I'm trynna make a scratch code of Semantic segmentation through U-Net. I'll use Cityscapes Dataset. I'm trying to make a dictionary(python) composed of the key(car, train, human, etc) and the value(rgb info). How can I match the dictionary with my ground_truth data?
example of labeling dictionary is like below
color_map = {
'0': [0, 0, 0], # unlabelled
'1': [128, 64, 128], # road
'2': [244, 35, 232], # sidewalk
'3': [70, 70, 70], # building
'4': [102, 102, 156], # wall
'5': [190, 153, 153], # fence
'6': [153, 153, 153], # pole
'7': [250,170, 30], # traffic_light
'8': [220, 220, 0], # traffic_sign
'9': [107, 142, 35], # vegetation
'10': [152, 251, 152], # terrain
'11': [0, 130, 180], # sky
'12': [220, 20, 60], # person
'13': [255, 0, 0], # rider
'14': [0, 0, 142], # car
'15': [0, 0, 70], # truck
'16': [0, 60, 100], # bus
'17': [0, 80, 100], # train
'18': [0, 0, 230], # motorcycle
'19': [119, 11, 32] # bicycle
}

Django combine two json objects to form a new array

I have two json objects as follow
Id:1
{"points":[{"x":109,"y":286,"r":1,"color":"black"},{"x":108,"y":285,"r":1,"color":"black"},{"x":106,"y":282,"r":1,"color":"black"},{"x":103,"y":276,"r":1,"color":"black"},],"lines":[{"x1":109,"y1":286,"x2":108,"y2":285,"strokeWidth":"2","strokeColor":"black"},{"x1":108,"y1":285,"x2":106,"y2":282,"strokeWidth":"2","strokeColor":"black"},{"x1":106,"y1":282,"x2":103,"y2":276,"strokeWidth":"2","strokeColor":"black"}]}
Id-2
{"points":[{"x":524,"y":343,"r":1,"color":"black"},{"x":523,"y":342,"r":1,"color":"black"},{"x":521,"y":339,"r":1,"color":"black"},{"x":520,"y":334,"r":1,"color":"black"},{"x":514,"y":319,"r":1,"color":"black"}],"lines":[{"x1":524,"y1":343,"x2":523,"y2":342,"strokeWidth":"2","strokeColor":"black"},{"x1":523,"y1":342,"x2":521,"y2":339,"strokeWidth":"2","strokeColor":"black"},{"x1":521,"y1":339,"x2":520,"y2":334,"strokeWidth":"2","strokeColor":"black"},{"x1":520,"y1":334,"x2":514,"y2":319,"strokeWidth":"2","strokeColor":"black"}]}
I am trying to merge these two data onto a canvas
I am able to retrieve a single file but combining them i am not able to do
def loadDrawing(request):
""" Function to load the drawing with drawingID if it exists."""
try:
# Getting JSON object string of saved drawing.
drawingJSONData = Drawing.objects.get(id = 1).drawingJSONText
# drawingJSONData1 = Drawing.objects.get(id=1).drawingJSONText
# drawingJSONData2 = Drawing.objects.get(id=2).drawingJSONText
# Seding context with appropriate information
context = {
"loadIntoJavascript" : True,
"JSONData" : drawingJSONData
}
# Editing response headers and returning the same
response = modifiedResponseHeaders(render(request, 'MainCanvas/index.html', context))
return response
My model in django
class Drawing(models.Model):
drawingJSONText = models.TextField(null = True)
My .js file to load the drawing and parsing it from server to JSON object and pushing the loaded points into an array
// Checking if the drawing to be loaded exists
if (document.getElementById('JSONLoadData') != null)
{
// Parsing the loaded drawing from server to a JSON Object
var loadedData = JSON.parse(JSONLoadData.value)
// Iterating through all the points in the loaded drawing
for(let i = 0; i < loadedData['points'].length; i++)
{
// Saving the point and drawing the same in the svg canvas
const point = svg.append('circle')
.attr('cx', loadedData['points'][i]['x'])
.attr('cy', loadedData['points'][i]['y'])
.attr('r', loadedData['points'][i]['r'])
.style('fill', loadedData['points'][i]['color']);
// Pushing the point inside points array
points.push(point);
}
// Iterating through all the lines in the loaded drawing
for(let i = 0; i < loadedData['lines'].length; i++)
{
// Saving the line and drawing the same in the svg canvas
const line = svg.append('line')
.attr('x1', loadedData['lines'][i]['x1'])
.attr('y1', loadedData['lines'][i]['y1'])
.attr('x2', loadedData['lines'][i]['x2'])
.attr('y2', loadedData['lines'][i]['y2'])
.attr('stroke-width', loadedData['lines'][i]['strokeWidth'])
.style('stroke', loadedData['lines'][i]['strokeColor']);
// Pushing the line inside lines array
lines.push(line);
}
}
});
Edited :
If my model is as follows
class Drawing(models.Model):
drawingJSONText = models.TextField(null=True)
project = models.CharField(max_length=250)
How can i filter data based on project
Lets say i have three datasets
1st one contains project = a
2nd one contains project = b
3rd one contains project = a
4th one contains project = a
How can i add datapoints like above by filtering data Drawing.objects.filter(project=a)
then based on the queryset i have three data points and corresponding data are plotted on canvas as above
I'm not entirely sure this is what you want, but are you trying to combine id-1 and id-2? If I think I understand what you are trying to do, will using the + operator work for you in this case?
drawingJSONData1 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData2 = json.loads(Drawing.objects.get(id=1).drawingJSONText)
drawingJSONData = dict()
drawingJSONData["points"] = drawingJSONData1["points"]+drawingJSONData1["points"]
drawingJSONData["lines"] = drawingJSONData2["lines"]+drawingJSONData2["lines"]
With your example above, you'd end up with:
{'points': [{'x': 109, 'y': 286, 'r': 1, 'color': 'black'},
{'x': 108, 'y': 285, 'r': 1, 'color': 'black'},
{'x': 106, 'y': 282, 'r': 1, 'color': 'black'},
{'x': 103, 'y': 276, 'r': 1, 'color': 'black'},
{'x': 524, 'y': 343, 'r': 1, 'color': 'black'},
{'x': 523, 'y': 342, 'r': 1, 'color': 'black'},
{'x': 521, 'y': 339, 'r': 1, 'color': 'black'},
{'x': 520, 'y': 334, 'r': 1, 'color': 'black'},
{'x': 514, 'y': 319, 'r': 1, 'color': 'black'}],
'lines': [{'x1': 109,
'y1': 286,
'x2': 108,
'y2': 285,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 108,
'y1': 285,
'x2': 106,
'y2': 282,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 106,
'y1': 282,
'x2': 103,
'y2': 276,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 524,
'y1': 343,
'x2': 523,
'y2': 342,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 523,
'y1': 342,
'x2': 521,
'y2': 339,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 521,
'y1': 339,
'x2': 520,
'y2': 334,
'strokeWidth': '2',
'strokeColor': 'black'},
{'x1': 520,
'y1': 334,
'x2': 514,
'y2': 319,
'strokeWidth': '2',
'strokeColor': 'black'}]}
EDIT: added the gets wrapped in json.loads to convert string to Python object as I don't know what kind of field that is and given the error being seen.

Convert Nested JSON to Pandas Column

I have a Json file as below:
[{
'instrument_token': 12335618, 'last_price': 31584.6,
'ohlc': {'open': 31080.1, 'high': 31590.0, 'low': 31049.05, 'close': 31114.7},
'depth': {'buy': [{'quantity': 40, 'price': 31576.4, 'orders': 1}, {'quantity': 160, 'price': 31576.0, 'orders': 1}], 'sell': [{'quantity': 200, 'price': 31584.6, 'orders': 2}, {'quantity': 60, 'price': 31584.65, 'orders': 1}]}
}]
I have tried as below:
df = json_normalize(ticks)
print(df)
This gives me a result as:
instrument_token last_price ohlc.open ohlc.high ohlc.low ohlc.close depth.buy depth.sell
0 12335618 31584.6 31080.1 31590.0 31049.05 31114.7 [{'quantity': 40, 'price': 31576.4, 'orders': ... [{'quantity': 200, 'price': 31584.6, 'orders':...
I want to further normalize the data of depth.buy & depth.sell columns in separate columns with column name as:
depth.buy.quantity1, depth.buy.price1, depth.buy.orders1,
depth.buy.quantity2, depth.buy.price2, depth.buy.orders2,
depth.sell.quantity1, depth.sell.price1, depth.sell.orders1,
depth.sell.quantity2, depth.sell.price2, depth.sell.orders2,
Is it possible to normalise further?
for this example dataset,you can do it like this.
from pandas.io.json import json_normalize
data = [{
'instrument_token': 12335618, 'last_price': 31584.6,
'ohlc': {'open': 31080.1, 'high': 31590.0, 'low': 31049.05, 'close': 31114.7},
'depth': {'buy': [{'quantity': 40, 'price': 31576.4, 'orders': 1},
{'quantity': 160, 'price': 31576.0, 'orders': 1}],
'sell': [{'quantity': 200, 'price': 31584.6, 'orders': 2},
{'quantity': 60, 'price': 31584.65, 'orders': 1}]
}
}]
df = json_normalize(data)
cols = ["depth.buy","depth.sell"]
for c in cols:
postdf = pd.DataFrame()
tmp = df[c].values
for i,val in enumerate(tmp):
vals = list(val.values())
keys = [f"{c}.{k}{i+1}"for k in val.keys()]
tmpdf =pd.DataFrame([vals],columns=keys)
postdf = pd.concat([postdf,tmpdf],axis=1)
df = pd.concat([df,postdf],axis=1)
df = df.drop(columns=[c])
note that,
tmp = df[c].values take the 0th element of the list.if you have multiple elements you have to loop over the elements.i assumed that all data inside one list.
if you need to get column names list (["depth.buy","depth.sell"]) dynamically,you can do it by checking dtypes of the df and get object type column names.

How to show only ticks that are in the array google charts

I have a line chart where user can see where they got in a competition over the years. There is up to 60th place. I will be filling this data from the database.
The problem is that all 60 places are shown on the y axis when I only want it to show 1, 15, 30, 45 , 60
I believe this is happening because I have the ticks changed from 1 to 1st place on the y axis, as this is better for user experience.
but since I am pulling from the database all 60 ticks need to be there. Though on the graph I actually want ticks to show every 15.
Heres my code:
// Callback that draws line chart for progress report
function drawLineChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Orouke', 'feis nara'],
['2014', 1, 4],
['2015', 11, 46],
['2016', 60, 11],
['2017', 10, 5]
]);
var options = {
title: 'Progress Report',
width: 600,
height: 550,
legend: { position: 'bottom' },
vAxis: { title: 'Competition Placement',
direction: -1,
gridlines: {count: 10},
ticks: [{ v: 1, f: '1st Place'}, {v: 2, f: '2nd Place'}, {v: 3, f: '3rd Place'}, {v: 4, f: '4th Place'}, {v: 5, f: '5th Place'}, {v: 6, f: '6th Place'}, {v: 7, f: '7th Place'}, {v: 8, f: '8th Place'}, {v: 9, f: '9th Place'}, {v: 10, f: '10th Place'}, {v: 11, f: '11th Place'}, { v: 12, f: '12th Place'}, {v: 13, f: '13th Place'}, {v: 14, f: '14th Place'}, {v: 15, f: '15th Place'}, {v: 16, f: '16th Place'}, {v: 17, f: '17th Place'}, {v: 18, f: '18th Place'}, {v: 19, f: '19th Place'}, {v: 20, f: '20th Place'}, {v: 21, f: '21st Place'}, {v: 22, f: '22nd Place'}, { v: 23, f: '23rd Place'}, {v: 24, f: '24th Place'}, {v: 25, f: '25th Place'}, {v: 26, f: '26th Place'}, {v: 27, f: '27th Place'}, {v: 28, f: '28th Place'}, {v: 29, f: '29th Place'}, {v: 30, f: '30th Place'}, {v: 31, f: '31st Place'}, {v: 32, f: '32nd Place'}, {v: 33, f: '33rd Place'}, { v: 34, f: '34th Place'}, {v: 35, f: '35th Place'}, {v: 36, f: '36th Place'}, {v: 37, f: '37th Place'}, {v: 38, f: '38th Place'}, {v: 39, f: '39th Place'}, {v: 40, f: '41st Place'}, {v: 42, f: '42nd Place'}, {v: 43, f: '43rd Place'}, {v: 44, f: '44th Place'}, {v: 45, f: '45th Place'}, {v: 46, f: '46th Place'}, {v: 47, f: '47th Place'}, {v: 48, f: '48th Place'}, {v: 49, f: '49th Place'}, {v: 50, f: '51st Place'}, {v: 52, f: '52nd Place'}, {v: 53, f: '53rd Place'}, {v: 54, f: '54th Place'}, {v: 55, f: '55th Place'}, {v: 56, f: '56th Place'}, {v: 57, f: '57th Place'}, {v: 58, f: '58th Place'}, {v: 59, f: '59th Place'}, {v: 60, f: '60th Place'}] }
};
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(data, options);
}
NOTE: I have not used php yet because I decided to set up the chart first and then fill in php after.
you can build the ticks dynamically, using a for loop...
for (var i = 0; i <= 60; i = i + 15) {
addTick(i);
}
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(function () {
var data = google.visualization.arrayToDataTable([
['Date', 'Orouke', 'feis nara'],
['2014', 1, 4],
['2015', 11, 46],
['2016', 60, 11],
['2017', 10, 5]
]);
// build ticks
var ticks = [];
for (var i = 0; i <= 60; i = i + 15) {
addTick(i);
}
function addTick(i) {
var place;
var digit;
if (i === 0) {
i = 1;
}
digit = i.toString().substr(i.toString().length - 1);
switch (digit) {
case '1':
place = 'st';
break;
case '2':
place = 'nd';
break;
case '3':
place = 'rd';
break;
default:
place = 'th';
}
ticks.push({
v: i,
f: i + place
});
}
var options = {
title: 'Progress Report',
width: 600,
height: 550,
legend: {
position: 'bottom'
},
vAxis: {
title: 'Competition Placement',
direction: -1,
gridlines: {count: 10},
ticks: ticks
}
};
var chart = new google.visualization.LineChart(document.getElementById('line_chart'));
chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="line_chart"></div>

Reading numeric keys from JSON file

Let's say I store a dictionary's values in JSON file. Here is the simplified code:
test = {}
for i in range(10):
for j in range(15):
test['{},{}'.format(i, j)] = i * j
with open('file1.json', 'w') as f:
json.dump(test, f)
I have hard time reading back from this file. How can I read back from this file into a dictionary with elements like key as [i,j] and value as i*j?
I use simple
with open('file1.json', 'r') as f:
data2 = json.load(f)
Do you mean something like this?
with open('file1.json', 'r') as f:
data2 = {tuple(int(x) for x in k.split(',')): v
for (k, v) in json.load(f).items()}
Your code will returns a dictionary contain unicode key and values if you want to get a dictionary contains the integer values you can use json.dumps after loading the file :
import json
test = {}
for i in range(10):
for j in range(15):
test['{},{}'.format(i, j)] = i * j
with open('file1.json', 'w') as f:
json.dump(test, f)
with open('file1.json', 'r') as f:
data2 = json.load(f)
print json.dumps(data2)
result :
{"1,8": 8, "1,9": 9, "1,6": 6, "1,7": 7, "1,4": 4, "1,5": 5, "1,2": 2, "1,3": 3, "1,0": 0, "1,1": 1, "7,6": 42, "7,7": 49, "5,8": 40, "5,9": 45, "3,8": 24, "3,9": 27, "5,2": 10, "5,3": 15, "5,0": 0, "3,7": 21, "3,0": 0, "5,7": 35, "3,2": 6, "3,3": 9, "3,14": 42, "3,12": 36, "3,13": 39, "3,10": 30, "3,11": 33, "2,8": 16, "5,14": 70, "5,10": 50, "5,11": 55, "5,12": 60, "5,13": 65, "0,8": 0, "4,8": 32, "0,13": 0, "0,12": 0, "0,11": 0, "0,10": 0, "0,14": 0, "6,9": 54, "6,8": 48, "6,1": 6, "6,0": 0, "6,3": 18, "6,2": 12, "6,5": 30, "6,4": 24, "6,7": 42, "6,6": 36, "6,14": 84, "6,11": 66, "6,10": 60, "6,13": 78, "6,12": 72, "8,9": 72, "8,8": 64, "8,7": 56, "8,6": 48, "8,5": 40, "8,4": 32, "8,3": 24, "8,2": 16, "8,1": 8, "8,0": 0, "5,1": 5, "2,13": 26, "3,4": 12, "3,5": 15, "3,6": 18, "0,7": 0, "0,6": 0, "0,5": 0, "0,4": 0, "0,3": 0, "0,2": 0, "0,1": 0, "0,0": 0, "5,6": 30, "0,9": 0, "3,1": 3, "1,10": 10, "1,11": 11, "1,12": 12, "1,13": 13, "2,9": 18, "5,4": 20, "2,5": 10, "2,4": 8, "2,7": 14, "2,6": 12, "2,1": 2, "2,0": 0, "2,3": 6, "2,2": 4, "4,3": 12, "4,2": 8, "4,1": 4, "4,0": 0, "4,7": 28, "4,6": 24, "4,5": 20, "4,4": 16, "2,11": 22, "2,10": 20, "4,9": 36, "2,12": 24, "2,14": 28, "1,14": 14, "5,5": 25, "8,13": 104, "8,12": 96, "8,11": 88, "8,10": 80, "8,14": 112, "7,12": 84, "7,13": 91, "7,10": 70, "7,11": 77, "7,14": 98, "4,14": 56, "4,13": 52, "4,12": 48, "4,11": 44, "4,10": 40, "7,8": 56, "7,9": 63, "9,4": 36, "9,5": 45, "9,2": 18, "9,3": 27, "9,0": 0, "9,1": 9, "7,0": 0, "7,1": 7, "7,2": 14, "7,3": 21, "7,4": 28, "7,5": 35, "9,8": 72, "9,9": 81, "9,6": 54, "9,7": 63, "9,10": 90, "9,11": 99, "9,12": 108, "9,13": 117, "9,14": 126}