Skip to content

Commit

Permalink
Merge pull request #79 from gisce/fix_version_check_f1_f1qh
Browse files Browse the repository at this point in the history
Mejorar versionado de ficheros F1 y F1QH
  • Loading branch information
davidmunoznovoa authored Oct 15, 2024
2 parents ab8e7bb + c2d22e5 commit 1d060a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
38 changes: 23 additions & 15 deletions mesures/f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ def filename(self):

@property
def zip_filename(self):
return "{prefix}_{distributor}_{measures_date}_{timestamp}.zip".format(
return "{prefix}_{distributor}_{measures_date}_{timestamp}.{version}.zip".format(
prefix=self.prefix, distributor=self.distributor, measures_date=self.measures_date[:10].replace('/', ''),
timestamp=self.generation_date.strftime('%Y%m%d')
timestamp=self.generation_date.strftime('%Y%m%d'), version=self.version
)

@property
Expand Down Expand Up @@ -146,25 +146,33 @@ def writer(self):
daymin = self.file['timestamp'].min()
daymax = self.file['timestamp'].max()
self.measures_date = daymin

existing_files = os.listdir('/tmp')
if existing_files:
versions = [int(f.split('.')[1]) for f in existing_files if self.zip_filename.split('.')[0] in f]
if versions:
self.version = max(versions) + 1

zipped_file = ZipFile(os.path.join('/tmp', self.zip_filename), 'w')
while daymin <= daymax:
di = daymin
df = (datetime.strptime(daymin, DATETIME_MASK) + timedelta(days=1)).strftime(DATETIME_MASK)
self.measures_date = di
dataf = self.file[(self.file['timestamp'] >= di) & (self.file['timestamp'] < df)]
# dataf['timestamp'] = dataf['timestamp'].apply(lambda x: x.strftime('%Y/%m/%d %H:%M'))
file_path = os.path.join('/tmp', self.filename)
kwargs = {'sep': ';',
'header': False,
'columns': self.columns,
'index': False,
check_line_terminator_param(): ';\n'
}
if self.default_compression:
kwargs.update({'compression': self.default_compression})

dataf.to_csv(file_path, **kwargs)
# Avoid to generate file if dataframe is empty
if len(dataf):
file_path = os.path.join('/tmp', self.filename)
kwargs = {'sep': ';',
'header': False,
'columns': self.columns,
'index': False,
check_line_terminator_param(): ';\n'
}
if self.default_compression:
kwargs.update({'compression': self.default_compression})
dataf.to_csv(file_path, **kwargs)
zipped_file.write(file_path, arcname=os.path.basename(file_path))

daymin = df
zipped_file.write(file_path, arcname=os.path.basename(file_path))
zipped_file.close()
return zipped_file.filename
32 changes: 20 additions & 12 deletions mesures/f1qh.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,33 @@ def writer(self):
daymin = self.file['timestamp'].min()
daymax = self.file['timestamp'].max()
self.measures_date = daymin

existing_files = os.listdir('/tmp')
if existing_files:
versions = [int(f.split('.')[1]) for f in existing_files if self.zip_filename.split('.')[0] in f]
if versions:
self.version = max(versions) + 1

zipped_file = ZipFile(os.path.join('/tmp', self.zip_filename), 'w')
while daymin <= daymax:
di = daymin
df = (datetime.strptime(daymin, DATETIME_HOUR_MASK) + timedelta(days=1)).strftime(DATETIME_HOUR_MASK)
self.measures_date = di
dataf = self.file[(self.file['timestamp'] >= di) & (self.file['timestamp'] < df)]
# dataf['timestamp'] = dataf['timestamp'].apply(lambda x: x.strftime('%Y/%m/%d %H:%M'))
file_path = os.path.join('/tmp', self.filename)
kwargs = {'sep': ';',
'header': False,
'columns': self.columns,
'index': False,
check_line_terminator_param(): ';\n'
}
if self.default_compression:
kwargs.update({'compression': self.default_compression})
# Avoid to generate file if dataframe is empty
if len(dataf):
file_path = os.path.join('/tmp', self.filename)
kwargs = {'sep': ';',
'header': False,
'columns': self.columns,
'index': False,
check_line_terminator_param(): ';\n'
}
if self.default_compression:
kwargs.update({'compression': self.default_compression})
dataf.to_csv(file_path, **kwargs)
zipped_file.write(file_path, arcname=os.path.basename(file_path))

dataf.to_csv(file_path, **kwargs)
daymin = df
zipped_file.write(file_path, arcname=os.path.basename(file_path))
zipped_file.close()
return zipped_file.filename
6 changes: 3 additions & 3 deletions spec/generation_files_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ def get_sample_obcups_data():
f = F1(data)
assert isinstance(f, F1)

with it('a zip of raw Files'):
with it('is a zip of raw files'):
data = SampleData().get_sample_data()
f = F1(data)
res = f.writer()
Expand Down Expand Up @@ -1009,7 +1009,7 @@ def get_sample_obcups_data():
expected = 'ES0012345678912345670F;11;2022/01/01 01:00:00;0;10.1;10.2;10.3;10.4;10.5;10.6;0;0;1;1'
assert f.file[f.columns].to_csv(sep=';', header=None, index=False).split('\n')[0] == expected

with it('truncate decimals if not allow_decimals parameter is specified'):
with it('truncates decimals if not allow_decimals parameter is specified'):
data = SampleData().get_sample_data_with_decimals()
f = F1(data)
res = f.writer()
Expand All @@ -1022,7 +1022,7 @@ def get_sample_obcups_data():
f = F1QH(data)
assert isinstance(f, F1QH)

with it('a zip of raw Files'):
with it('is a zip of raw files'):
data = SampleData().get_sample_f1qh_data()
f = F1QH(data)
res = f.writer()
Expand Down

0 comments on commit 1d060a9

Please sign in to comment.