from tabulate import tabulate
req = [
{
"meta": {
"href": '2ab2a8e1-06fc-11eb-0a80-09950026157e',
"type": "product",
"mediaType": "application/json",
},
"stockByStore": [
{
"meta": {
"href": "cdf4e4d9-7960-11ea-0a80-02cc00015063",
"type": "store",
"mediaType": "application/json"
},
"name": "КБ Советская",
"stock": 12.075,
"reserve": 0.0,
"inTransit": 0.0
},
{
"meta": {
"href": "7acec394-7b04-11ea-0a80-01940002ba4f",
"type": "store",
"mediaType": "application/json"
},
"name": "Кулой",
"stock": 8.609,
"reserve": 0.0,
"inTransit": 0.0
},
{
"meta": {
"href": "89e15a69-7950-11ea-0a80-043900008c70",
"type": "store",
"mediaType": "application/json"
},
"name": "Основной склад",
"stock": 83.0,
"reserve": 0.0,
"inTransit": 0.0
}
]
},
{
"meta": {
"href": "8ec23fae-904e-11ea-0a80-05660005a116",
"type": "product",
"mediaType": "application/json"
},
"stockByStore": [
{
"meta": {
"href": "cdf4e4d9-7960-11ea-0a80-02cc00015063",
"type": "store",
"mediaType": "application/json"
},
"name": "КБ Советская",
"stock": 1.1,
"reserve": 0.0,
"inTransit": 0.0
},
{
"meta": {
"href": "7acec394-7b04-11ea-0a80-01940002ba4f",
"type": "store",
"mediaType": "application/json"
},
"name": "Кулой",
"stock": 0.0,
"reserve": 0.0,
"inTransit": 0.0
},
{
"meta": {
"href": "89e15a69-7950-11ea-0a80-043900008c70",
"type": "store",
"mediaType": "application/json"
},
"name": "Основной склад",
"stock": 23.0,
"reserve": 0.0,
"inTransit": 0.0
}
]
}
]
req2 = [
{
"meta": {
"href": "2ab2a8e1-06fc-11eb-0a80-09950026157e",
"type": "product",
"mediaType": "application/json"
},
"stock": 103.684,
"inTransit": 0.0,
"reserve": 0.0,
"quantity": 103.684,
"name": "арахис в хрустящей корочке",
"code": "00268",
"price": 18000.0,
"salePrice": 25000.0,
"uom": {
"meta": {
"type": "uom",
"mediaType": "application/json"
},
"name": "кг"
},
"externalCode": "WOywSqXtgrq3Wz0egCiQB2",
"stockDays": 57.78
},
{
"meta": {
"href": "8ec23fae-904e-11ea-0a80-05660005a116",
"type": "product",
"mediaType": "application/json"
},
"stock": 24.1,
"inTransit": 0.0,
"reserve": 0.0,
"quantity": 24.1,
"name": "арахис соленый",
"code": "00166",
"article": "арахис",
"price": 16141.078838174273,
"salePrice": 21500.0,
"uom": {
"meta": {
"href": "2ec1170c-3f69-4409-87bb-c68e0011b275",
"type": "uom",
"mediaType": "application/json"
},
"name": "кг"
},
"externalCode": "vLXA07ARjLeF3vvs2LmeF2",
"stockDays": 150.79
}
]
#функция принимает данные из "req2",возвращает словарь,где ключ продукта значение"href"
def create_dict_products(req):
new_dict = {}
for tag in req:
key = tag['meta']['href']
new_dict[key] = tag
return new_dict
products = create_dict_products(req2)
#функция принимает "req",возвращает словарь где ключ "продукт"
def process_req(req):
out = {}
for d in req:
product_key = d['meta']['href']
product_name = products[product_key]['name']
temp = {}
for store in d['stockByStore']:
temp[store['name']]=store['stock']
key = product_name
out[key] = temp
return out
res = process_req(req)
#функция принимает "req",возвращает словарь где ключ "склад"
def process_req2(req):
out = {}
for store in req[0]['stockByStore']:
key = store['name']
out[key]={}
for d in req:
product_key = d['meta']['href']
product_name = products[product_key]['name']
for store in d['stockByStore']:
key = store['name']
out[key].update({product_name:store['stock']})
return out
res2 = process_req2(req)
#функция принимает свой словарь,и заголовки,выводит в консоль таблицу
def show_table(res,headers=[]):
list_for_table =[]
for k,v in res.items():
for k2,v2 in v.items():
list_for_table.append([k,k2,v2])
print(tabulate(list_for_table,headers=headers,tablefmt="grid"))
print('пример 1')
show_table(res,['Продукт','Склад','Остаток'])
print('\n\n\Пример2')
show_table(res2,['Склад','Продукт','Остаток'])