def test_to_dict(self): """ Note: data file with column headers are tested in test_filters.py """ r = pe.Reader(self.testfile) result = OrderedDict() result.update({"Series_1": [1, 2, 3, 4]}) result.update({"Series_2": [ 5, 6, 7, 8, ]}) result.update({"Series_3": [9, 10, 11, 12]}) actual = pe.to_dict(r.rows()) assert actual.keys() == result.keys() assert result == actual result = { "Series_1": 1, "Series_2": 2, "Series_3": 3, "Series_4": 4, "Series_5": 5, "Series_6": 6, "Series_7": 7, "Series_8": 8, "Series_9": 9, "Series_10": 10, "Series_11": 11, "Series_12": 12 } actual = pe.to_dict(r.enumerate()) assert result == actual
def test_to_dict(self): """ Note: data file with column headers are tested in test_filters.py """ r = pe.Reader(self.testfile) result = OrderedDict() result.update({"Series_1": [1, 2, 3, 4]}) result.update({"Series_2": [5, 6, 7, 8, ]}) result.update({"Series_3": [9, 10, 11, 12]}) actual = pe.to_dict(r.rows()) assert actual.keys() == result.keys() self.assertEqual(result, actual) result = { "Series_1": 1, "Series_2": 2, "Series_3": 3, "Series_4": 4, "Series_5": 5, "Series_6": 6, "Series_7": 7, "Series_8": 8, "Series_9": 9, "Series_10": 10, "Series_11": 11, "Series_12": 12 } actual = pe.to_dict(r.enumerate()) self.assertEqual(result, actual)
def test_orthogonality(self): r = pe.ColumnSeriesReader(self.test_tuple) r.add_filter(pe.sheets.filters.EvenRowFilter()) r.add_filter(pe.sheets.filters.OddColumnFilter()) actual = pe.to_dict(r) result = {"X": [2, 4], "Z": [22, 42]} assert result == actual
def test_orthogonality(self): r = pe.SeriesReader(self.testfile) r.add_filter(pe.sheets.filters.EvenRowFilter()) r.add_filter(pe.sheets.filters.OddColumnFilter()) actual = pe.to_dict(r) result = {"Y": [11, 31, 51]} assert result == actual
def test_orthogonality(self): r = pe.SeriesReader(self.testfile) r.add_filter(pe.filters.EvenRowFilter()) r.add_filter(pe.filters.OddColumnFilter()) actual = pe.to_dict(r) result = { "Y": [11, 31, 51] } assert result == actual
def test_orthogonality(self): r = pe.ColumnSeriesReader(self.test_tuple) r.add_filter(pe.filters.EvenRowFilter()) r.add_filter(pe.filters.OddColumnFilter()) actual = pe.to_dict(r) result = { "X": [2, 4], "Z": [22, 42] } assert result == actual
def test_orthogonality(self): r = pe.ColumnSeriesReader(self.test_tuple) r.add_filter(pe.filters.EvenRowFilter()) r.add_filter(pe.filters.OddColumnFilter()) actual = pe.to_dict(r) result = {"X": [2, 4], "Z": [22, 42]} assert result == actual # test removing the filter, it prints the original one r.clear_filters() actual = pe.utils.to_array(r.rows()) assert actual == pe.transpose(self.content[1:])
def test_orthogonality(self): r = pe.SeriesReader(self.testfile) r.add_filter(pe.filters.EvenRowFilter()) r.add_filter(pe.filters.OddColumnFilter()) actual = pe.to_dict(r) result = {"Y": [11, 31, 51]} assert result == actual # test removing the filter, it prints the original one r.clear_filters() actual = pe.utils.to_array(r.rows()) assert actual == self.content[1:]
def read_excel(file): """ Takes file and returns dict """ if file: filename = file.filename extension = filename.split(".")[1] sheet = pe.get_sheet(file_type=extension, file_stream=file.read()) data = pe.to_dict(sheet) #skip first since it is going to be header if len(data)>= 1: data.pop('Series_1') for key in data: print str(key), str(data[key][0]), str(data[key][1]) return data
def _excel_file_to_dict(file): """ Loads an Excel file and transforms it into a dictionary. It uses the first row's cells as keys for the dictionary and the columns are the contents. :param file: The Excel file to transform. :return: A dictionary of arrays. """ valid_extensions = ['.xls', '.xlsx'] validate_file_extension(file, valid_extensions) file_extension = file.name.split(".")[1] sheet = pyexcel.load_from_memory(file_extension, file.read(), name_columns_by_row=0, name_rows_by_column=-1) return pyexcel.to_dict(sheet)
def test_book_reader_to_dict(self): r = pe.BookReader(self.testfile) actual = pe.to_dict(r) assert actual == self.content
def client_list_import(request): if request.method == 'POST' and 'file' in request.FILES: filename = request.FILES['file'].name extension = filename.split(".")[1] sheet = pyexcel.load_from_memory(extension, request.FILES['file'].read()) data = pyexcel.to_dict(sheet) user = request.user error = [] success = [] i = 1 for row in data: if row != 'Series_1': city = data[row][0] name = data[row][1] kind_of_activity = data[row][2] actual_address = data[row][3] site = data[row][4] contact_name = data[row][5] contact_function = data[row][6] contact_phone = data[row][7] contact_email = data[row][8] try: # проверяем город city_instance = City.objects.get(name__iexact=city) try: incomingclient = IncomingClient.objects.get(name__iexact=name) error.append(u'Строка: %s .Клиент %s уже есть в системе' % (i, incomingclient.name)) except: incomingclient = IncomingClient( city=city_instance, name=name, manager=user.manager, ) if kind_of_activity: incomingclient.kind_of_activity = kind_of_activity if actual_address: incomingclient.actual_address = actual_address if site: incomingclient.site = site incomingclient.save() success.append(u'Клиент %s добавлен в систему' % name) if contact_name: incomingcontact = IncomingClientContact( incomingclient=incomingclient, name=contact_name ) if contact_email: incomingcontact.email = contact_email if contact_phone: incomingcontact.phone = contact_phone if contact_function: incomingcontact.function = contact_function incomingcontact.save() incomingclientmanager = IncomingClientManager( manager=user.manager, incomingclient=incomingclient ) incomingclientmanager.save() except: error.append(u'Строка %s, Город %s не доступен для модератора %s' % (i, city, user.manager.moderator)) i += 1 context = { 'error_list': error, 'error_count': len(error), 'success_list': success, 'success_count': len(success) } return render(request, 'incoming/import_error.html', context) else: return HttpResponseRedirect(reverse('incoming:list'))
def address_list_import(request): if request.method == "POST" and "file" in request.FILES: filename = request.FILES["file"].name extension = filename.split(".")[1] sheet = pyexcel.load_from_memory(extension, request.FILES["file"].read()) data = pyexcel.to_dict(sheet) for row in data: # print row if row != "Series_1": city = data[row][0] area = data[row][1].strip() street = data[row][2].strip() house_number = "" point_flag = False for i in unicode(data[row][3]): if i == ".": point_flag = True if point_flag: if i != "0" and i != ".": house_number += i else: house_number += i try: # пробуем получить город city_instance = City.objects.get(name__iexact=city) # print 'has city %s' % city_instance try: # пробуем получить район area_instance = CityArea.objects.get(city=city_instance, name__iexact=area) # print 'has area %s' % area_instance except: # создаём новый район area_instance = CityArea(city=city_instance, name=area) area_instance.save() # print 'create area %s' % area_instance try: # пробуем получить улицу street_instance = CityStreet.objects.get( city=city_instance, area=area_instance, name__iexact=street ) # print 'has street %s' % street_instance except: # создаём новую улицу street_instance = CityStreet(city=city_instance, area=area_instance, name=street) street_instance.save() # print 'create street %s' % street_instance try: # пробуем получить поверхность house_instance = CityHouse.objects.get( city=city_instance, area=area_instance, street=street_instance, number=house_number ) # print 'has house %s' % house_instance except: # создаём поверхность house_instance = CityHouse( city=city_instance, area=area_instance, street=street_instance, number=house_number ) house_instance.save() # print 'create house %s' % house_instance except: pass return HttpResponseRedirect(reverse("admin-index")) return HttpResponseRedirect("/admin/city/cityhouse/")
def address_list_import(request): if request.method == 'POST' and 'file' in request.FILES: filename = request.FILES['file'].name extension = filename.split(".")[1] sheet = pyexcel.load_from_memory(extension, request.FILES['file'].read()) data = pyexcel.to_dict(sheet) for row in data: if row != 'Series_1': city = data[row][0] area = data[row][1] street = data[row][2] house_number = '' point_flag = False for i in unicode(data[row][3]): if i == '.': point_flag = True if point_flag: if i != '0' and i != '.': house_number += i else: house_number += i # try: # porch_list = [int(data[row][4])] # except: # r_porch_list = data[row][4].strip().split(',') # porch_list = [] # for porch in r_porch_list: # try: # porch_list.append(int(porch)) # except: # pass raw_porch_list = str(data[row][4]).replace('.', ',').strip().split(',') # import pdb; pdb.set_trace() porch_list = [] for i in raw_porch_list: if i.strip() != '0' or i.strip() != '0': porch_list.append(int(i.strip())) try: # пробуем получить город city_instance = City.objects.get(name__iexact=city) try: # пробуем получить район area_instance = Area.objects.get(city=city_instance, name__iexact=area) except: # создаём новый район area_instance = Area(city=city_instance, name=area) area_instance.save() try: # пробуем получить улицу street_instance = Street.objects.get(city=city_instance, area=area_instance, name__iexact=street) except: # создаём новую улицу street_instance = Street(city=city_instance, area=area_instance, name=street) street_instance.save() try: # пробуем получить поверхность surface_instance = Surface.objects.get(city=city_instance, street=street_instance, house_number=house_number) except: # создаём поверхность surface_instance = Surface(city=city_instance, street=street_instance, house_number=house_number) surface_instance.save() for i in porch_list: # пробегаемся по списку подъездов try: Porch.objects.get(surface=surface_instance, number=i) except: porch = Porch(surface=surface_instance, number=i) porch.save() except: pass # print u'Город не найден' return HttpResponseRedirect(reverse('surface:list'))
def address_list_import(request): if request.method == 'POST' and 'file' in request.FILES: filename = request.FILES['file'].name extension = filename.split(".")[1] sheet = pyexcel.load_from_memory(extension, request.FILES['file'].read()) data = pyexcel.to_dict(sheet) for row in data: if row != 'Series_1': city = data[row][0] area = data[row][1] street = data[row][2] house_number = '' point_flag = False for i in unicode(data[row][3]): if i == '.': point_flag = True if point_flag: if i != '0' and i != '.': house_number += i else: house_number += i # try: # porch_list = [int(data[row][4])] # except: # r_porch_list = data[row][4].strip().split(',') # porch_list = [] # for porch in r_porch_list: # try: # porch_list.append(int(porch)) # except: # pass raw_porch_list = str(data[row][4]).replace( '.', ',').strip().split(',') # import pdb; pdb.set_trace() porch_list = [] for i in raw_porch_list: if i.strip().isdigit( ) and i.strip() != '0' or i.strip() != '': porch_list.append(int(i.strip())) try: # пробуем получить город city_instance = City.objects.get(name__iexact=city) try: # пробуем получить район area_instance = Area.objects.get(city=city_instance, name__iexact=area) except: # создаём новый район area_instance = Area(city=city_instance, name=area) area_instance.save() try: # пробуем получить улицу street_instance = Street.objects.get( city=city_instance, area=area_instance, name__iexact=street) except: # создаём новую улицу street_instance = Street(city=city_instance, area=area_instance, name=street) street_instance.save() try: # пробуем получить поверхность surface_instance = Surface.objects.get( city=city_instance, street=street_instance, house_number=house_number) except: # создаём поверхность surface_instance = Surface(city=city_instance, street=street_instance, house_number=house_number) surface_instance.save() for i in porch_list: # пробегаемся по списку подъездов try: Porch.objects.get(surface=surface_instance, number=i) except: porch = Porch(surface=surface_instance, number=i) porch.save() except: pass # print u'Город не найден' return HttpResponseRedirect(reverse('surface:list'))
def test_book_reader_to_dict(self): r = pe.BookReader(self.testfile) actual = pe.to_dict(r) self.assertEqual(self.content, actual)
def client_list_import(request): if request.method == 'POST' and 'file' in request.FILES: filename = request.FILES['file'].name extension = filename.split(".")[1] sheet = pyexcel.load_from_memory(extension, request.FILES['file'].read()) data = pyexcel.to_dict(sheet) user = request.user error = [] success = [] i = 1 for row in data: if row != 'Series_1': city = data[row][0] name = data[row][1] kind_of_activity = data[row][2] actual_address = data[row][3] site = data[row][4] contact_name = data[row][5] contact_function = data[row][6] contact_phone = data[row][7] contact_email = data[row][8] try: # проверяем город city_instance = City.objects.get(name__iexact=city) try: incomingclient = IncomingClient.objects.get( name__iexact=name) error.append( u'Строка: %s .Клиент %s уже есть в системе' % (i, incomingclient.name)) except: incomingclient = IncomingClient( city=city_instance, name=name, manager=user.manager, ) if kind_of_activity: incomingclient.kind_of_activity = kind_of_activity if actual_address: incomingclient.actual_address = actual_address if site: incomingclient.site = site incomingclient.save() success.append(u'Клиент %s добавлен в систему' % name) if contact_name: incomingcontact = IncomingClientContact( incomingclient=incomingclient, name=contact_name) if contact_email: incomingcontact.email = contact_email if contact_phone: incomingcontact.phone = contact_phone if contact_function: incomingcontact.function = contact_function incomingcontact.save() incomingclientmanager = IncomingClientManager( manager=user.manager, incomingclient=incomingclient) incomingclientmanager.save() except: error.append( u'Строка %s, Город %s не доступен для модератора %s' % (i, city, user.manager.moderator)) i += 1 context = { 'error_list': error, 'error_count': len(error), 'success_list': success, 'success_count': len(success) } return render(request, 'incoming/import_error.html', context) else: return HttpResponseRedirect(reverse('incoming:list'))