def test_unique_items_list_not_flat(self): list_with_duplicates = [ 'foo', 'bar', 'baz', 'foo', 2, ['b', 'c', 'a'], 6, 10, 2, ['b', 'c', 'a'] ] expected_result = ['foo', 'bar', 'baz', 2, ['b', 'c', 'a'], 6, 10] self.assertEqual(expected_result, utils.unique_items(list_with_duplicates))
def handle(self, *args, **options): seen = [] while True: number_merged = 0 queryset = Gear.objects.exclude( Q(make=None) | Q(make='') | Q(make__in=seen)) all_makes = [ x.make for x in Gear.objects.exclude(Q(make=None) | Q(make='')) ] item = queryset[0] matches = unique_items( difflib.get_close_matches(item.make, all_makes)) if item.make not in seen: seen.append(item.make) if len(matches) == 1 and matches[0] == item.make: continue print(item.make) for i in range(0, len(matches)): if matches[i] != item.make: print('\t%d. %s' % (i, matches[i])) to_merge = raw_input( "Which ones do I merge [space separated, or q]? ") if to_merge == 'q': return elif to_merge == '': continue new_make = raw_input("New make name [%s]? " % item.make).decode( sys.stdin.encoding) if new_make == '': new_make = item.make masters = Gear.objects.filter(make=item.make) for master in masters: master.make = new_make master.save() number_merged += 1 for i in shlex.split(to_merge): slaves = Gear.objects.filter(make=matches[int(i)]) for slave in slaves: slave.make = new_make slave.save() number_merged += 1 print("Merged %d makes." % number_merged)
def handle(self, *args, **options): seen = [] all_makes = sorted( unique_items( Gear.objects.exclude( Q(make=None) | Q(make='') | Q(make__in=seen)).values_list( 'make', flat=True))) print("Total makes: %d." % len(all_makes)) if args: matches = unique_items( difflib.get_close_matches(args[0], all_makes, cutoff=0.2)) all_makes = sorted( unique_items( Gear.objects.filter( Q(make__in=matches) | Q(make__icontains=args[0])).values_list('make', flat=True))) print("Restricted to %d makes." % len(all_makes)) for make in all_makes: if make in seen: # Should not be necessary, actually. continue to_update = Gear.objects.filter(make=make) count = to_update.count() print("[%s]:%d" % (make, count)) new_make = raw_input("") seen.append(make) if new_make == '': continue print("Will update %d items." % count) to_update.update(make=new_make)
def test_unique_items(self): list_with_duplicates = ['foo', 'bar', 'baz', 'foo', 2, 6, 10, 2] expected_result = ['foo', 'bar', 'baz', 2, 6, 10] self.assertEqual(sorted(expected_result), sorted(utils.unique_items(list_with_duplicates)))
def prepare_camera_types(self, obj): l = [] for i in Image.objects.filter(user=obj): l += _prepare_camera_types(i) return unique_items(l)
def prepare_camera_types(self, obj): l = [] for i in Image.objects.filter(user = obj): l += _prepare_camera_types(i) return unique_items(l)