python read data from file and calculate result -


i want store values in python file blew here abc.py file

{   21:"this field required",   22:"value can't null",   23:"other custom message", } 

then want calculate code given string in python function. example:

def calculate_code(error_message):     #process error_message , read data abc.py file     #calculate error_code     return eror_code 

like if have error_message = "value can't null" ll error_code = 22.

what best way it?

a sensible solution define cache class (based on dictionary) messages error codes. not complete solution, provides skeleton:

from datetime import datetime os import stat import time

class cache(object):     def __init__(self, filename):         self._filename = filename         self._populate_cache()      def __getitem__(self, name):         if self._check_file_updated():            self._populate_cache()         return self._cache[name]      def _populate_cache(self):         self._populate_time = time.mktime(datetime.now().timetuple())         self._cache = read_file_as_dict(self._filename)         print "debug: updated cache"      def _check_file_updated(self):         return stat(self._filename).st_mtime > self._populate_time   def read_file_as_dict(filename):     # needs implementation: simulating     return { "blown up": 25, "warning": 31 } 

usage:

cache = cache("test") >>> cache["blown up"] 25 >>> cache["blown up"] 25 

now touch "test" file:

>>> cache["blown up"] debug: updated cache 25 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -