There is a class:

class Conception: def __init__(self, n): self.name = n self.delivery = ['Доставка', {'sum': 0, 'chq': 0, 'avg': 0}] self.notdelivery = ['Прилавок', {'sum': 0, 'chq': 0, 'avg': 0}] def setname(self, newname): self.name = newname def set_values_is_delivery(self, s, c, a): self.delivery[1]['sum'] = s self.delivery[1]['chq'] = c self.delivery[1]['avg'] = a def set_values_is_not_delivery(self, s, c, a): self.notdelivery[1]['sum'] = s self.notdelivery[1]['chq'] = c self.notdelivery[1]['avg'] = a def __str__(self): return 'Концепция: {0}\n''Доставка: {1}\n''Прилавок: {2}'. \ format(self.name, self.delivery, self.notdelivery) 

There is json with data:

 { "data":[ { "Conception":null, "Delivery.IsDelivery":"ORDER_WITHOUT_DELIVERY", "DishDiscountSumInt":0, "DishDiscountSumInt.average":0, "OpenDate.Typed":"2016-12-20T00:00:00", "UniqOrderId.OrdersCount":1 }, { "Conception":"One", "Delivery.IsDelivery":"DELIVERY_ORDER", "DishDiscountSumInt":35125, "DishDiscountSumInt.average":5017.86, "OpenDate.Typed":"2016-12-20T00:00:00", "UniqOrderId.OrdersCount":7 }, { "Conception":"One", "Delivery.IsDelivery":"ORDER_WITHOUT_DELIVERY", "DishDiscountSumInt":59594, "DishDiscountSumInt.average":425.67, "OpenDate.Typed":"2016-12-20T00:00:00", "UniqOrderId.OrdersCount":140 }, { "Conception":"Two", "Delivery.IsDelivery":"ORDER_WITHOUT_DELIVERY", "DishDiscountSumInt":240, "DishDiscountSumInt.average":120, "OpenDate.Typed":"2016-12-20T00:00:00", "UniqOrderId.OrdersCount":2 }, { "Conception":"Three", "Delivery.IsDelivery":"DELIVERY_ORDER", "DishDiscountSumInt":49422, "DishDiscountSumInt.average":4942.2, "OpenDate.Typed":"2016-12-20T00:00:00", "UniqOrderId.OrdersCount":10 } ] } 

There are json messages for Slack Messenger:

 [ { "fallback": "Required plain-text summary of the attachment.", "color": "good", "pretext": "__" }, { "fallback": "Required plain-text summary of the attachment.", "color": "#4B1924", "author_name": "One", "author_icon": "http://unicodey.com/emoji-data/img-twitter-64/1f4e2.png", "fields": [ { "title": "No delivery", "short": "true" }, { "title": "Delivery", "short": "true" }, { "title": "Выручка", "value": "24 000", "short": "true" }, { "title": "Выручка", "value": "24 000 руб.", "short": "true" }, { "title": "Чеки", "value": "24", "short": "true" }, { "title": "Чеки", "value": "24", "short": "true" }, { "title": "Средний чек", "value": "2 400", "short": "true" }, { "title": "Средний чек", "value": "1 000 руб.", "short": "true" }, { "title": "Итого", "value": "100000", "short": "false" } ] }, { "fallback": "Required plain-text summary of the attachment.", "color": "#9887C2", "author_name": "Two", "author_icon": "http://unicodey.com/emoji-data/img-apple-64/1f680.png", "fields": [ { "title": "No delivery", "short": "true" }, { "title": "Delivery", "short": "true" }, { "title": "Выручка", "value": "24 000", "short": "true" }, { "title": "Выручка", "value": "24 000 руб.", "short": "true" }, { "title": "Чеки", "value": "24", "short": "true" }, { "title": "Чеки", "value": "24", "short": "true" }, { "title": "Средний чек", "value": "2 400", "short": "true" }, { "title": "Средний чек", "value": "1 000 руб.", "short": "true" }, { "title": "Итого", "value": "100000", "short": "false" } ], "footer": "__", "ts": 123456789 } ] 

It is necessary to substitute data from one json to another, where:

 ... "author_name": "Conception.name()" ... { "title": "Выручка", "value": "Conception.set_values_is_delivery.s()", "short": "true" }, { "title": "Чеки", "value": "Conception.set_values_is_delivery.c()", "short": "true" }, { "title": "Conception.set_values_is_delivery.a()", "value": "2 400", "short": "true" ... 

I sit here and think, is there a need for a class Conception at all? Maybe you can somehow do without it? Who can offer a simpler solution to the problem?

  • 3
  • In python, after parsing json, standard python structures and types are obtained - dict, list, int, float, str and None. You can work with this data and then generate a string from them json - gil9red

0