11import os
22import csv
33import warnings
4- from glob import glob
4+ from pathlib import Path
55
66import pandas as pd
77import numpy as np
@@ -83,7 +83,7 @@ def read_csv(filepath_or_buffer, nb_axes=None, index_col=None, sep=',', headerse
8383 Examples
8484 --------
8585 >>> csv_dir = get_example_filepath('examples')
86- >>> fname = csv_dir + '/ population.csv'
86+ >>> fname = csv_dir / ' population.csv'
8787
8888 >>> # The data below is derived from a subset of the demo_pjan table from Eurostat
8989 >>> read_csv(fname)
@@ -97,7 +97,7 @@ def read_csv(filepath_or_buffer, nb_axes=None, index_col=None, sep=',', headerse
9797
9898 Missing label combinations
9999
100- >>> fname = csv_dir + '/ population_missing_values.csv'
100+ >>> fname = csv_dir / ' population_missing_values.csv'
101101 >>> # let's take a look inside the CSV file.
102102 >>> # they are missing label combinations: (Paris, male) and (New York, female)
103103 >>> with open(fname) as f:
@@ -129,7 +129,7 @@ def read_csv(filepath_or_buffer, nb_axes=None, index_col=None, sep=',', headerse
129129
130130 Specify the number of axes of the output array (useful when the name of the last axis is implicit)
131131
132- >>> fname = csv_dir + '/ population_missing_axis_name.csv'
132+ >>> fname = csv_dir / ' population_missing_axis_name.csv'
133133 >>> # let's take a look inside the CSV file.
134134 >>> # The name of the last axis is missing.
135135 >>> with open(fname) as f:
@@ -164,7 +164,7 @@ def read_csv(filepath_or_buffer, nb_axes=None, index_col=None, sep=',', headerse
164164
165165 Read array saved in "narrow" format (wide=False)
166166
167- >>> fname = csv_dir + '/ population_narrow_format.csv'
167+ >>> fname = csv_dir / ' population_narrow_format.csv'
168168 >>> # let's take a look inside the CSV file.
169169 >>> # Here, data are stored in a 'narrow' format.
170170 >>> with open(fname) as f:
@@ -260,46 +260,44 @@ def __init__(self, fname, overwrite_file=False, sep=','):
260260 self .sep = sep
261261 self .axes = None
262262 self .groups = None
263- if fname is None :
263+ if self . fname is None :
264264 self .pattern = None
265265 self .directory = None
266- elif '.csv' in fname or '*' in fname or '?' in fname :
267- self .pattern = fname
268- self .directory = os . path . dirname ( fname )
266+ elif self . fname . suffix == '.csv' or '*' in self . fname . name or '?' in self . fname . name :
267+ self .pattern = self . fname . name
268+ self .directory = fname . parent
269269 else :
270270 # assume fname is a directory.
271- # Not testing for os.path.isdir(fname ) here because when writing, the directory might not exist.
272- self .pattern = os . path . join ( fname , '*.csv' )
273- self .directory = fname
271+ # Not testing for fname.is_dir( ) here because when writing, the directory might not exist.
272+ self .pattern = '*.csv'
273+ self .directory = self . fname
274274
275275 def _get_original_file_name (self ):
276276 pass
277277
278- def _to_filepath (self , key : str ) -> str :
278+ def _to_filepath (self , key ) -> Path :
279279 if self .directory is not None :
280- return os . path . join ( self .directory , f'{ key } .csv' )
280+ return self .directory / f'{ key } .csv'
281281 else :
282- return key
282+ return Path ( key )
283283
284284 def _open_for_read (self ):
285- if self .directory and not os . path . isdir ( self .directory ):
285+ if self .directory and not self .directory . is_dir ( ):
286286 raise ValueError (f"Directory '{ self .directory } ' does not exist" )
287287
288288 def _open_for_write (self ):
289289 if self .directory is not None :
290290 try :
291291 os .makedirs (self .directory )
292292 except OSError :
293- if not os . path . isdir ( self .directory ):
293+ if not self .directory . is_dir ( ):
294294 raise ValueError (f"Path { self .directory } must represent a directory" )
295295
296296 def list_items (self ) -> List [Tuple [str , str ]]:
297- fnames = glob (self .pattern ) if self .pattern is not None else []
298- # drop directory
299- fnames = [os .path .basename (fname ) for fname in fnames ]
300- # strip extension from files
301- # XXX: unsure we should use sorted here
302- fnames = sorted ([os .path .splitext (fname )[0 ] for fname in fnames ])
297+ fnames = self .directory .glob (self .pattern ) if self .pattern is not None else []
298+ # stem = filename without extension
299+ # FIXME : not sure sorted is required here
300+ fnames = sorted ([fname .stem for fname in fnames ])
303301 return [(name , 'Array' ) for name in fnames if name != '__metadata__' ]
304302
305303 def _read_item (self , key , type , * args , ** kwargs ) -> Array :
@@ -316,7 +314,7 @@ def _dump_item(self, key, value, *args, **kwargs):
316314
317315 def _read_metadata (self ) -> Metadata :
318316 filepath = self ._to_filepath ('__metadata__' )
319- if os . path . isfile ( filepath ):
317+ if filepath . is_file ( ):
320318 meta = read_csv (filepath , wide = False )
321319 return Metadata .from_array (meta )
322320 else :
0 commit comments