, .
, . . , , , , .. , , . , , ( ) , , .
, , . $MY_APP_HOME/dat. my_config_, combo, -. : ( ) my_config_11_fast_but_sloppy.txt, my_config_100_balanced.txt, my_config_3_thorough_but_slow.txt, ( ): Thorough But Slow, Fast But Sloppy, Balanced.
,
The MyConfiguration class below does all the work in only a few lines of code (significantly less than what I needed to explain the purpose :-), and it can be used as follows:
# populate my_config combobox
self.my_config = MyConfiguration()
self.gui.my_config.addItems(self.my_config.get_items())
# get selected file path
index = self.gui.my_config.currentIndex()
self.config_file = self.my_config.get_file_path_by_index(index);
Here is the MyConfiguration class:
import os, re
class MyConfiguration:
def __init__(self):
self.__config_dir = '';
env_name = 'MY_APP_HOME'
if env_name in os.environ:
self.__config_dir = os.environ[env_name] + '/dat/';
else:
raise Exception(env_name + ' environment variable is not set.')
regex = re.compile("^(?P<file_name>my_config_(?P<index>\d+?)_(?P<desc>.*?)[.]txt?)$",re.MULTILINE)
file_names = os.listdir(self.__config_dir)
self.__items = regex.findall("\n".join(file_names))
self.__items.sort(key=lambda x: int(x[1]))
def get_items(self):
items = []
for item in self.__items:
items.append( self.__format_item_text(item[2]))
return items
def get_file_path_by_index(self, index):
return self.__config_dir + self.__items[index][0]
def __format_item_text(self, text):
return text.replace("_", " ").title();
source
share