D
D
Dmitry2016-12-19 17:08:20
Python
Dmitry, 2016-12-19 17:08:20

What is the best way to design an application?

I decided to write a small application in python in order to consolidate the acquired knowledge. While writing the code, I read about OOP and decided to make everything more abstract, in which I actually ask for help.
The cron application accesses the service, logs in, receives json with data. json contains data:

{
    "data": [{
        "Conception": 'Shop1',
        "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY",
        "DishDiscountSumInt": 0,
        "DishDiscountSumInt.average": 0,
        "OpenDate.Typed": "2016-01-11T00:00:00",
        "UniqOrderId.OrdersCount": 2
    }, {
        "Conception": "Shop2",
        "Delivery.IsDelivery": "DELIVERY_ORDER",
        "DishDiscountSumInt": 18895,
        "DishDiscountSumInt.average": 2699.29,
        "OpenDate.Typed": "2016-01-11T00:00:00",
        "UniqOrderId.OrdersCount": 7
    }, {
        "Conception": "Shop2",
        "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY",
        "DishDiscountSumInt": 63376,
        "DishDiscountSumInt.average": 358.06,
        "OpenDate.Typed": "2016-01-11T00:00:00",
        "UniqOrderId.OrdersCount": 177
    }],
    "summary": [
        [{
            "Conception": null,
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 0,
            "DishDiscountSumInt.average": 0,
            "UniqOrderId.OrdersCount": 2
        }],
        [{
            "Conception": "Shop2",
            "Delivery.IsDelivery": "DELIVERY_ORDER"
        }, {
            "DishDiscountSumInt": 18895,
            "DishDiscountSumInt.average": 2699.29,
            "UniqOrderId.OrdersCount": 7
        }],
        [
            {}, {
                "DishDiscountSumInt": 82271,
                "DishDiscountSumInt.average": 442.32,
                "UniqOrderId.OrdersCount": 186
            }
        ],
        [{
            "Conception": "Shop2",
            "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY"
        }, {
            "DishDiscountSumInt": 63376,
            "DishDiscountSumInt.average": 358.06,
            "UniqOrderId.OrdersCount": 177
        }],
        [{
            "Conception": null,
            "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY"
        }, {
            "DishDiscountSumInt": 0,
            "DishDiscountSumInt.average": 0,
            "UniqOrderId.OrdersCount": 2
        }],
        [{
            "Conception": "Shop2"
        }, {
            "DishDiscountSumInt": 82271,
            "DishDiscountSumInt.average": 447.13,
            "UniqOrderId.OrdersCount": 184
        }],
        [{
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 82271,
            "DishDiscountSumInt.average": 442.32,
            "UniqOrderId.OrdersCount": 186
        }],
        [{
            "Conception": null
        }, {
            "DishDiscountSumInt": 0,
            "DishDiscountSumInt.average": 0,
            "UniqOrderId.OrdersCount": 2
        }],
        [{
            "Conception": "Shop2",
            "Delivery.IsDelivery": "DELIVERY_ORDER",
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 18895,
            "DishDiscountSumInt.average": 2699.29,
            "UniqOrderId.OrdersCount": 7
        }],
        [{
            "Conception": "Shop2",
            "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY",
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 63376,
            "DishDiscountSumInt.average": 358.06,
            "UniqOrderId.OrdersCount": 177
        }],
        [{
            "Conception": "Shop2",
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 82271,
            "DishDiscountSumInt.average": 447.13,
            "UniqOrderId.OrdersCount": 184
        }],
        [{
            "Conception": null,
            "Delivery.IsDelivery": "ORDER_WITHOUT_DELIVERY",
            "OpenDate.Typed": "2016-01-11T00:00:00"
        }, {
            "DishDiscountSumInt": 0,
            "DishDiscountSumInt.average": 0,
            "UniqOrderId.OrdersCount": 2
        }]
    ]
}

Next, the application needs to parse the whole thing, form a message and send it to Slack.
At the moment, I have everything implemented like this:
there is a Conceptions 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_d(self, s, c, a):
        self.delivery[1]['sum'] = s
        self.delivery[1]['chq'] = c
        self.delivery[1]['avg'] = a

    def set_values_nd(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 are functions:
getReport() - receives data from the service and parses json;
setValue() - generates a list of concepts with values;
sentMsg() - generates the required json and sends a message to Slack.
Questions:
1) Did I design everything correctly?
2) Can anything be improved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Konovalov, 2016-12-26
@Madique

  1. pep8 , as mentioned.
  2. Class for "Concept" - yes, a good idea if you need to do some separate methods, but sometimes you can get by with a simple dictionary. Why are the delivery and notdelivery attributes lists, if they are not expanded anywhere in the code, there are no iterations over them? Obviously, you forgot to specify some part of the business logic here.
  3. Setname method - really needed and used?
  4. In the set_values_d and set_values_nd methods, you assign values ​​to the first index. It precisely is at the moment of a call of these methods? Maybe all this could be done in the constructor?
  5. Constants in code are bad. Then you yourself will forget, for example, why self.notdelivery[1] and not self.notdelivery[0] or you will typo and assign a value by the key 'cng' instead of 'chg', etc.
  6. But what if the service changes the data output format? IMHO, validation is needed - are all mandatory fields "arrived", are there any errors in the values, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question