def get_pkgs_classification(self, labels): pkgs_percent = {} pkgs_classification = {} pkg_time = PkgTime() pkg_data = pkg_time.get_package_data() for name, time_values in pkg_data.iteritems(): _, access = time_values pkgs_percent[name] = access pkgs = pkgs_percent.keys() pkgs = sorted(pkgs, key=lambda pkg: pkgs_percent[pkg]) pkgs = list(reversed(pkgs)) size = len(pkgs) / len(labels) for index, label in enumerate(labels): index_begin = size * index index_end = index_begin + size classifications = dict.fromkeys(pkgs[index_begin:index_end], label) pkgs_classification.update(classifications) index_begin = size * len(labels) if index_begin < len(labels): classifications = dict.fromkeys(pkgs[index_begin], label[-1]) pkgs_classification.update(classifications) return pkgs_classification
def main(): path = os.path.expanduser( '~/.app-recommender/user_data/pkgs_classifications.txt') if not os.path.exists(path): print 'Could not find file pkgs_classification' print 'Have you run apprec --train ?' exit(-1) pkg_time = PkgTime() pkgs_times = pkg_time.get_package_data() with open(path, 'ra') as data: pkg_classification = pickle.load(data) classifications = {'RU': [], 'U': [], 'NU': []} for pkg, values in pkg_classification.iteritems(): classifications[values[-1]].append(pkg) for classification, pkgs in classifications.iteritems(): print '\n' print 'Classification: {}'.format(classification) print '\n' for pkg in sorted(pkgs): pkg_text = '{} \t' if len(pkg) < 15: pkg_text += '\t' if len(pkg) < 7: pkg_text += '\t' pkg_text += ' {}' print pkg_text.format(pkg, pkgs_times[pkg][1].strip()) print '\nNum pkgs: {}'.format(len(pkg_classification))
def get_pkgs_classification(self, percent_function, labels): pkgs_percent = {} pkgs_classification = {} time_now = calendar.timegm(time.gmtime()) pkg_time = PkgTime() pkg_data = pkg_time.get_package_data() for name, time_values in pkg_data.iteritems(): modify = time_values[0] access = time_values[1] pkgs_percent[name] = percent_function(modify, access, time_now) pkgs = pkgs_percent.keys() pkgs = sorted(pkgs, key=lambda pkg: pkgs_percent[pkg]) pkgs = list(reversed(pkgs)) size = len(pkgs) / len(labels) for index, label in enumerate(labels): index_begin = size * index index_end = index_begin + size classifications = dict.fromkeys(pkgs[index_begin:index_end], label) pkgs_classification.update(classifications) index_begin = size * len(labels) if index_begin < len(labels): classifications = dict.fromkeys(pkgs[index_begin], label[-1]) pkgs_classification.update(classifications) return pkgs_classification
def collect_pkgs_time(): pkg_time = PkgTime() if create_file(PKGS_TIME_PATH): manual_pkgs = [] with open(MANUAL_INSTALLED_PKGS_PATH, 'r') as text: manual_pkgs = [line.strip() for line in text] pkgs_time = pkg_time.get_packages_time(manual_pkgs) pkg_time.save_package_time(pkgs_time, PKGS_TIME_PATH)
class PkgTimeTests(unittest.TestCase): def setUp(self): self.pkg_time = PkgTime() @patch('commands.getoutput') @patch('apprecommender.ml.pkg_time.get_time_from_package') def test_invalid_paths_get_best_time(self, mock_time, mock_command): mock_time.return_value = [10, 10] mock_command.return_value = '/usr/lib/a-b-c/\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_command.return_value = '/usr/lib/a-b-c-d/\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_command.return_value = '/usr/lib/mime/packages/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_command.return_value = '/etc/init.d/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_command.return_value = '/media/files/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) @patch('commands.getoutput') @patch('apprecommender.ml.pkg_time.get_time_from_package') def test_valid_paths_get_best_time(self, mock_time, mock_command): mock_time.return_value = [10, 10] mock_command.return_value = '/usr/lib/a-b/\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 10) self.assertEqual(modify, 10) mock_time.return_value = [20, 20] mock_command.return_value = '/usr/bin/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 20) self.assertEqual(modify, 20) mock_time.return_value = [30, 30] mock_command.return_value = '/usr/game/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 30) self.assertEqual(modify, 30) mock_time.return_value = [40, 40] mock_command.return_value = '/usr/lib/test/test\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 40) self.assertEqual(modify, 40) @patch('commands.getoutput') @patch('apprecommender.ml.pkg_time.get_time_from_package') def test_invalid_files_get_best_time(self, mock_time, mock_command): mock_time.return_value = [10, 10] mock_command.return_value = '/usr/bin/test.desktop\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_time.return_value = [10, 10] mock_command.return_value = '/usr/games/test.conf\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_time.return_value = [10, 10] mock_command.return_value = '/usr/lib/python/test.egg-info\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0) mock_time.return_value = [10, 10] mock_command.return_value = '/usr/lib/test/test.txt\n' access, modify = self.pkg_time.get_best_time('test') self.assertEqual(access, 0) self.assertEqual(modify, 0)
def setUp(self): self.pkg_time = PkgTime()