def test_load_to_object(self): test_stations = util.read_csv(self.test_station_file, 17) a_station = create_station(test_stations[1]) self.assertEqual(1.1, a_station.get_flow(0)) self.assertEqual(31.31, a_station.get_flow(30)) self.assertEqual( datetime.strptime('1/2/2006', '%d/%m/%Y').date(), a_station.date) self.assertEqual(1, a_station.get_flow_status(0)) self.assertEqual(1, a_station.get_flow_status(1)) self.assertEqual(0, a_station.get_flow_status(2)) self.assertEqual(0, a_station.get_flow_status(3)) self.assertEqual(1, a_station.get_flow_status(27)) self.assertEqual(0, a_station.get_flow_status(28)) self.assertEqual(1, a_station.get_flow_status(29)) self.assertEqual(0, a_station.get_flow_status(30)) a_station.save() a_station = create_station(test_stations[2]) a_station.save() all_csv_stations = util.read_csv(self.ana_station_file, 17) for a__csv_station in all_csv_stations: a_station = create_station(a__csv_station) self.assertIsNotNone(a_station) a_station.save()
def test_load_to_object(self): test_stations = util.read_csv(self.test_station_file, 17) a_station = create_station(test_stations[1]) self.assertEqual(1.1, a_station.get_flow(0)) self.assertEqual(31.31, a_station.get_flow(30)) self.assertEqual(datetime.strptime('1/2/2006', '%d/%m/%Y').date(), a_station.date) self.assertEqual(1, a_station.get_flow_status(0)) self.assertEqual(1, a_station.get_flow_status(1)) self.assertEqual(0, a_station.get_flow_status(2)) self.assertEqual(0, a_station.get_flow_status(3)) self.assertEqual(1, a_station.get_flow_status(27)) self.assertEqual(0, a_station.get_flow_status(28)) self.assertEqual(1, a_station.get_flow_status(29)) self.assertEqual(0, a_station.get_flow_status(30)) a_station.save() a_station = create_station(test_stations[2]) a_station.save() all_csv_stations = util.read_csv(self.ana_station_file, 17) for a__csv_station in all_csv_stations: a_station = create_station(a__csv_station) self.assertIsNotNone(a_station) a_station.save()
def load_csv_for_Z(fname, parallel=False, **kwargs): Z = [] if not parallel: with read_csv(fname) as f: Z = [map_int(row) for row in f] else: n_jobs = kwargs.get('n_jobs', 4) verbose = kwargs.get('verbose', 1) backend = kwargs.get('backend', 'multiprocessing') with read_csv(fname) as f: Z = Parallel(n_jobs=n_jobs, verbose=verbose, backend=backend)(delayed(map_int)(row) for row in f) return Z
def station_new(request, format=None): print('encontrou!!') print(request.FILES) try: data = request.FILES['file'] path = default_storage.save('tmp/stations_temp.txt', ContentFile(data.read())) tmp_file = os.path.join(settings.MEDIA_ROOT, path) test_stations = util.read_csv(tmp_file, 17) print("Reading "+str(len(test_stations))) #TODO muito lento, trocar por um 'bulk insert' ou algo do tipo for a_line in test_stations: station = create_station(a_line) station.save() print("deu certo") except: print("deu pau") print( sys.exc_info()) msg = {'name': 'um nome do servidor', 'desc': 'sucesso'} return Response(msg)
def test_serializer(self): test_stations = util.read_csv(self.test_station_file, 17) a_station = create_station(test_stations[1]) a_station.save() print(a_station.pk) ser = StationSerializer(a_station) content = JSONRenderer().render(ser.data) self.assertIsNotNone(content) stream = BytesIO(content) data = JSONParser().parse(stream) ser = StationSerializer(data=data) self.assertTrue(ser.is_valid())
def station_new(request, format=None): print('encontrou!!') print(request.FILES) try: data = request.FILES['file'] path = default_storage.save('tmp/stations_temp.txt', ContentFile(data.read())) tmp_file = os.path.join(settings.MEDIA_ROOT, path) test_stations = util.read_csv(tmp_file, 17) print("Reading " + str(len(test_stations))) #TODO muito lento, trocar por um 'bulk insert' ou algo do tipo for a_line in test_stations: station = create_station(a_line) station.save() print("deu certo") except: print("deu pau") print(sys.exc_info()) msg = {'name': 'um nome do servidor', 'desc': 'sucesso'} return Response(msg)
def test_read_csv(self): strpath = os.path.join(os.path.dirname(__file__), 'resources', '44630000.TXT') util.read_csv(strpath, 17) pass