Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
quality-kpi
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Quiring, Ole
quality-kpi
Commits
5002eee9
Commit
5002eee9
authored
2 years ago
by
Hock, Martin
Browse files
Options
Downloads
Patches
Plain Diff
Fix & complete json reading examples.
parent
be94a720
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test-legoclasses.ipynb
+42
-52
42 additions, 52 deletions
test-legoclasses.ipynb
with
42 additions
and
52 deletions
test-legoclasses.ipynb
+
42
−
52
View file @
5002eee9
...
...
@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
1
,
"execution_count":
2
,
"metadata": {},
"outputs": [],
"source": [
...
...
@@ -53,32 +53,30 @@
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
9
,
"metadata": {},
"outputs": [],
"source": [
"## Let's test the json stuff\n",
"import json\n",
"# import tkinter as tk\n",
"\n",
"def read_json(file_path):\n",
" with open(file_path, 'r') as f:\n",
" data = json.load(f)\n",
" return data\n",
"json_data = read_json('example_item_data_list.json')\n",
"# that produces a list, we'd prefer a dict\n",
"\n",
"with open('example_item_data_dict.json') as json_file:\n",
" json_dict = json.load(json_file)\n",
" print(type(json_dict))\n",
" print(json_dict[\"1\"])"
"# import tkinter as tk\n"
]
},
{
"cell_type": "code",
"execution_count":
null
,
"execution_count":
10
,
"metadata": {},
"outputs": [],
"source": [
"# First level list handling\n",
"\n",
"def read_json(file_path):\n",
" with open(file_path, 'r') as f:\n",
" data = json.load(f)\n",
" return data\n",
"json_list_data = read_json('example_item_data_list.json')\n",
"# that produces a list, we'd prefer a dict\n",
"\n",
"# Clever way to just create an item for each listed element\n",
"def create_all_items(data):\n",
" items = []\n",
...
...
@@ -91,58 +89,50 @@
" items.append(item)\n",
" return items\n",
"\n",
"all_items = create_all_items(json_data) \n",
"all_items = create_all_items(json_
list_
data) \n",
"\n",
"# How to get entries for a specific item\n",
"def get_item_by_number(data, item_number):\n",
" datasheet = [item for item in data if item['item_number' ] == item_number] \n",
" return datasheet\n",
"\n",
"sheet1 = get_item_by_number(json_data, 2) # this is a list? meh\n",
"\n",
"dict1 = \n",
"# Create an item for this:\n",
"def create_item_from_sheet(dict):\n",
" item_number = dict(\"\")\n",
"\n",
" item = LegoItem(item_number, mass, delivery_time)\n",
" return item\n",
"sheet1 = get_item_by_number(json_list_data, 2) # this is a list? meh\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(sheet1['item_number'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"print(json_data)\n",
"print(all_items[1])\n",
"print(sheet1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'dict'>\n",
"{'item_number': 1, 'mass': 10, 'delivery_time': 1}\n"
]
}
],
"source": [
"# Lets build some items\n",
"# lets say item 2 twice and one of item 3\n",
"# vs first level dict handling\n",
"\n",
"with open('example_item_data_dict.json') as json_file:\n",
" json_dict = json.load(json_file)\n",
" print(type(json_dict))\n",
" print(json_dict[\"1\"])\n",
"\n",
"sheet2_from_dict = json_dict[\"2\"]\n",
"# Create an item for this:\n",
"def create_item_from_sheet(dict):\n",
" item_number = dict[\"item_number\"]\n",
" mass = dict[\"mass\"]\n",
" delivery_time =[\"delivery_time\"]\n",
" item = LegoItem(item_number, mass, delivery_time)\n",
" return item\n",
"\n",
"
gearbox = LegoComponent(
)\n",
"
gearbox.add_item()
"
"
itemfromdict = create_item_from_sheet(sheet2_from_dict
)\n",
"
\n
"
]
}
],
...
...
%% 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}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment