Skip to content
Snippets Groups Projects
Commit 5002eee9 authored by Hock, Martin's avatar Hock, Martin
Browse files

Fix & complete json reading examples.

parent be94a720
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
# import functions.lego_classes as lego_classes
from functions.lego_classes import LegoItem
from functions.lego_classes import LegoComponent
```
%% Cell type:code id: tags:
``` python
# Test manually creating some item and components
item1 = LegoItem(1,0.10,10)
item2 = LegoItem(3,0.30,30)
item3 = LegoItem(2, 0.2,20)
component1 = LegoComponent()
# component1.add_item(item1)
component2 = LegoComponent(item2)
componentall = LegoComponent([component1,component2]) # This will be saved as items for some reasaon
# component3 = LegoComponent(item3,component1)
# component3.add(item2) # Can't really use an item twice
# component3.add(component2)
print(componentall.items)
```
%% Output
[<functions.lego_classes.LegoComponent object at 0x000002720F4BF7F0>, <functions.lego_classes.LegoComponent object at 0x000002720F4BF790>]
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
## Let's test the json stuff
import json
# import tkinter as tk
```
%% Cell type:code id: tags:
``` python
# First level list handling
def read_json(file_path):
with open(file_path, 'r') as f:
data = json.load(f)
return data
json_data = read_json('example_item_data_list.json')
json_list_data = read_json('example_item_data_list.json')
# that produces a list, we'd prefer a dict
with open('example_item_data_dict.json') as json_file:
json_dict = json.load(json_file)
print(type(json_dict))
print(json_dict["1"])
```
%% Cell type:code id: tags:
``` python
# Clever way to just create an item for each listed element
def create_all_items(data):
items = []
for item_data in data:
item_number = item_data.get('item_number')
mass = item_data.get('mass')
delivery_time = item_data.get('delivery_time')
# specific_data = item_data.get('specific_data', {})
item = LegoItem(item_number, mass, delivery_time)
items.append(item)
return items
all_items = create_all_items(json_data)
all_items = create_all_items(json_list_data)
# How to get entries for a specific item
def get_item_by_number(data, item_number):
datasheet = [item for item in data if item['item_number' ] == item_number]
return datasheet
sheet1 = get_item_by_number(json_data, 2) # this is a list? meh
dict1 =
# Create an item for this:
def create_item_from_sheet(dict):
item_number = dict("")
item = LegoItem(item_number, mass, delivery_time)
return item
sheet1 = get_item_by_number(json_list_data, 2) # this is a list? meh
```
%% Cell type:code id: tags:
``` python
print(sheet1['item_number'])
```
# vs first level dict handling
%% Cell type:code id: tags:
``` python
print(json_data)
print(all_items[1])
print(sheet1)
```
with open('example_item_data_dict.json') as json_file:
json_dict = json.load(json_file)
print(type(json_dict))
print(json_dict["1"])
%% Cell type:code id: tags:
sheet2_from_dict = json_dict["2"]
# Create an item for this:
def create_item_from_sheet(dict):
item_number = dict["item_number"]
mass = dict["mass"]
delivery_time =["delivery_time"]
item = LegoItem(item_number, mass, delivery_time)
return item
``` python
# Lets build some items
# lets say item 2 twice and one of item 3
itemfromdict = create_item_from_sheet(sheet2_from_dict)
```
%% Output
gearbox = LegoComponent()
gearbox.add_item()
```
<class 'dict'>
{'item_number': 1, 'mass': 10, 'delivery_time': 1}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment