0

I have some useful scripts written in python that helps me with testing of the project (some complicated price calculation system). Before starting price calculation of any product i need to parse server for values that are related only to required product. This parsing requires time, about 5 sec. Given, that I know when i need to update values from server, I don't need to wait these 5 seconds each time when i launch my method that calculates price for me. In this case i need to store parsed info inside variable and use it for further price calculation. But sometimes i also need to update my info with server. simplified code example:

class Some_scripts(object):
    def __init__(self):
        pass

    def parser(self, endpoint, additional_requirements):
        some_parsing_code
        return required_info  # example of returned data [{"name":"John"}, {"name":"Jack"}...]

    def car_price_calc(self):
        self.required_values = self.parser(endpoint, additional_requirements)  # this one i need to store
        futher_work_with
        self.required_value

The first thing that comes to my mind, simply write self.required_valuesinto file and add some additional flag that indicates method about updating self.required_values from server or just taking it from file. But is it the most correct, reliable way of solving my question?

1
  • 2
    you can write in simple file, JSON file, etc. or in pickle or similar (it save in file too). Other solution is database. Commented Dec 5, 2016 at 15:39

1 Answer 1

5

Depends on you use case. Pickling is the nicest way of writing python variables to file, if you intent to read it with python again. https://docs.python.org/3/library/pickle.html

Other solutions would be plain text files, or somewhat structured text format, like JSON or YAML. Those are more useful for human reading, or using your values in a different language.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Made it with saving file as json.dump() and opening it as json.loads(). Just for my growth. I can also save my list with dictionaries into pickle and then open it as variable and iterate through dictionaries of this list? If yes can you please show example of code how to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.