Skip to content

Commit bb8f1a4

Browse files
committed
fix: fixed style in io files
1 parent 676ab40 commit bb8f1a4

File tree

3 files changed

+87
-122
lines changed

3 files changed

+87
-122
lines changed

src/hydrodiy/io/csv.py

+44-56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
''' Module to read and write csv data with comments '''
22

3-
import sys, os, re
3+
import sys
4+
import os
5+
import re
46

57
from datetime import datetime
68
from getpass import getuser
@@ -12,14 +14,8 @@
1214

1315
from hydrodiy import PYVERSION
1416

15-
# Tailor string handling depending on python version
16-
if PYVERSION == 2:
17-
from StringIO import StringIO
18-
UNICODE = unicode
19-
20-
elif PYVERSION == 3:
21-
from io import StringIO
22-
UNICODE = str
17+
from io import StringIO
18+
UNICODE = str
2319

2420
# Check distutils available for python folders
2521
HAS_DISTUTILS = False
@@ -89,8 +85,8 @@ def _csvhead(nrow, ncol, comment, source_file, author=None):
8985
head.append(f'# {key} : {comments[key]}')
9086

9187
now = datetime.now()
92-
head.append('# time_generated : ' + \
93-
datetime.strftime(now, '%Y-%m-%d %H:%M:%S'))
88+
head.append("# time_generated : "
89+
+ f"{datetime.strftime(now, '%Y-%m-%d %H:%M:%S')}")
9490

9591
# seek author
9692
if author is None:
@@ -142,8 +138,8 @@ def _check_name(filename):
142138
filename_full = '{0}.zip'.format(filename)
143139

144140
if not os.path.exists(filename_full):
145-
raise ValueError('Cannot find file {0} (filename={1})'.format(\
146-
filename_full, filename))
141+
errmess = f"Cannot find file {filename} ({filename_full})."
142+
raise ValueError(errmess)
147143

148144
return filename_full
149145

@@ -153,31 +149,31 @@ def write2zip(archive, arcname, txt):
153149

154150
# Check file is not in the archive
155151
zinfo_test = archive.NameToInfo.get(arcname)
156-
if not zinfo_test is None:
157-
raise ValueError('File {0} already exists in archive'.format(arcname))
152+
if zinfo_test is not None:
153+
raise ValueError(f"File {arcname} already exists in archive")
158154

159155
# Create fileinfo object
160156
zinfo = zipfile.ZipInfo(arcname)
161157

162158
now = datetime.now()
163-
zinfo.date_time = (now.year, now.month, now.day, \
164-
now.hour, now.minute, now.second)
159+
zinfo.date_time = (now.year, now.month, now.day,
160+
now.hour, now.minute, now.second)
165161
# File permission
166-
zinfo.external_attr = 33488896 # 0777 << 16
162+
zinfo.external_attr = 33488896 # 0777 << 16
167163

168164
# Write to archive with header
169-
archive.writestr(zinfo, txt, \
170-
compress_type=zipfile.ZIP_DEFLATED)
171-
172-
173-
def write_csv(data, filename, comment,\
174-
source_file,\
175-
author=None,\
176-
write_index=False,\
177-
compress=True,\
178-
float_format='%0.5f',\
179-
archive=None,\
180-
**kwargs):
165+
archive.writestr(zinfo, txt,
166+
compress_type=zipfile.ZIP_DEFLATED)
167+
168+
169+
def write_csv(data, filename, comment,
170+
source_file,
171+
author=None,
172+
write_index=False,
173+
compress=True,
174+
float_format='%0.5f',
175+
archive=None,
176+
**kwargs):
181177
''' write a pandas dataframe to csv with comments in header
182178
183179
Parameters
@@ -218,16 +214,15 @@ def write_csv(data, filename, comment,\
218214
source_file = str(source_file)
219215

220216
# Generate head
221-
head = _csvhead(data.shape[0], data.shape[1],\
222-
comment,\
223-
source_file=source_file,\
224-
author=author)
217+
head = _csvhead(data.shape[0], data.shape[1], comment,
218+
source_file=source_file,
219+
author=author)
225220

226221
# Check source_file exists
227222
if not os.path.exists(source_file):
228223
raise ValueError(source_file + ' file does not exists')
229224

230-
if not archive is None:
225+
if archive is not None:
231226
compress = False
232227

233228
# defines file name
@@ -249,9 +244,8 @@ def write_csv(data, filename, comment,\
249244
fobj.write(line+'\n')
250245

251246
# Write data itself
252-
txt = data.to_csv(fobj, index=write_index,\
253-
float_format=float_format, \
254-
**kwargs)
247+
txt = data.to_csv(fobj, index=write_index,
248+
float_format=float_format, **kwargs)
255249

256250
# Store compressed data
257251
if archive or compress:
@@ -261,9 +255,8 @@ def write_csv(data, filename, comment,\
261255
arcname = filename
262256
if compress:
263257
arcname = os.path.basename(filename)
264-
archive = zipfile.ZipFile(filename_full, \
265-
mode='w', \
266-
compression=zipfile.ZIP_DEFLATED)
258+
archive = zipfile.ZipFile(filename_full, mode='w',
259+
compression=zipfile.ZIP_DEFLATED)
267260

268261
write2zip(archive, arcname, txt)
269262

@@ -274,8 +267,8 @@ def write_csv(data, filename, comment,\
274267
fobj.close()
275268

276269

277-
def read_csv(filename, has_colnames=True, archive=None, \
278-
encoding='utf-8', **kwargs):
270+
def read_csv(filename, has_colnames=True, archive=None,
271+
encoding='utf-8', **kwargs):
279272
''' Read a pandas dataframe from a csv with comments in header
280273
281274
Parameters
@@ -330,9 +323,9 @@ def read_csv(filename, has_colnames=True, archive=None, \
330323

331324
except TypeError:
332325
import warnings
333-
warnings.warn(('Failed to open {0} as a text file. '+\
334-
'Will now try to open it as a buffer').format(filename), \
335-
stacklevel=2)
326+
warnmess = f"Failed to open {filename} as a text file."\
327+
+ "Will now try to open it as a buffer."
328+
warnings.warn(warnmess, stacklevel=2)
336329
fobj = filename
337330

338331
else:
@@ -362,12 +355,12 @@ def read_csv(filename, has_colnames=True, archive=None, \
362355
if 'names' not in kwargs:
363356
# deals with multi-index columns
364357
# reformat columns like (idx1-idx2-...)
365-
search = re.findall('"\([^(]*\)"', line)
358+
search = re.findall('"\\([^(]*\\)"', line)
366359
linecols = line
367360

368361
if search:
369362
for cn in search:
370-
cn2 = re.sub('\(|\)|"|\'', '', cn)
363+
cn2 = re.sub('\\(|\\)|"|\'', '', cn)
371364
cn2 = re.sub(', *', '-', cn2)
372365
linecols = re.sub(re.escape(cn), cn2, linecols)
373366

@@ -377,18 +370,13 @@ def read_csv(filename, has_colnames=True, archive=None, \
377370
kwargs.pop('names')
378371

379372
# Reads data with proper column names
380-
data = pd.read_csv(fobj, names=cns, \
381-
encoding=encoding, **kwargs)
382-
383-
data.columns = [re.sub('\\.', '_', cn)\
384-
for cn in data.columns]
373+
data = pd.read_csv(fobj, names=cns, encoding=encoding, **kwargs)
374+
data.columns = [re.sub('\\.', '_', cn) for cn in data.columns]
385375

386376
else:
387377
# Reads data frame without header
388-
data = pd.read_csv(fobj, header=None, \
389-
encoding=encoding, **kwargs)
378+
data = pd.read_csv(fobj, header=None, encoding=encoding, **kwargs)
390379

391380
fobj.close()
392381

393382
return data, comment
394-

src/hydrodiy/io/html.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
import pandas as pd
32
from pathlib import Path
43

54
FHERE = Path(__file__).resolve().parent
@@ -29,21 +28,19 @@
2928
</html>.
3029
'''
3130

31+
3232
def dataframe2html(df, fhtml, title, comment="", author="", index=False):
3333
# Open CSS
3434
fcss = FHERE / "pandas_dataframe_style.css"
3535
with fcss.open("r") as fo:
3636
css_style = fo.read()
3737

3838
# Format table
39+
tbl = df.to_html(index=index, classes="css_style")
3940
with fhtml.open("w") as fo:
40-
fo.write(HTML_TEMPLATE.format(\
41-
author=author, \
42-
css_style=css_style, \
43-
now=str(datetime.now()), \
44-
comment=comment, \
45-
table=df.to_html(index=index, classes="css_style"),\
46-
title=title
47-
))
48-
49-
41+
fo.write(HTML_TEMPLATE.format(author=author,
42+
css_style=css_style,
43+
now=str(datetime.now()),
44+
comment=comment,
45+
table=tbl,
46+
title=title))

0 commit comments

Comments
 (0)