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?
-
2you can write in simple file, JSON file, etc. or in pickle or similar (it save in file too). Other solution is database.furas– furas2016年12月05日 15:39:16 +00:00Commented Dec 5, 2016 at 15:39
1 Answer 1
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.
1 Comment
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.