Through tutorials I had learned that you can define two variables in the same statement, e.g.:
In [15]: a, b = 'hello', 'hi!'
In [16]: a
Out[16]: 'hello'
In [17]: b
Out[17]: 'hi!'
well how does that apply to here?
fh, opened = cbook.to_filehandle(fname, 'w', return_opened = True)
I prodded further:
In [18]: fh
Out[18]: <open file 'attempt.csv', mode 'w' at 0xaac89d0>
In [19]: opened
Out[19]: True
my issue comes really with 'opened'. Well normally if two variables are being defined, there would be a comma and then whatever is there would define 'opened.' This is not the case. Even with that issue looming, 'opened' is equal to True which I assume is because 'return_opened = True.' Well that's weird because I don't remember in any tutorial that you could just add a 'return_' before a variable to affect that variable.
I play with it some more and I change the True to False and I get this:
In [10]: fh, opened = cbook.to_filehandle(fname, 'w', return_opened = False)
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
/home/blahblahblah/Documents/Programming/EXERCISES/piece.py in <module>()
----> 1
2
3
4
5
IOError: [Errno 9] Bad file descriptor
so I guess it only accepts False.
I guess I'd like if someone could explain what is going on here.
Gracias amigos!
here's the entire code:
if missingd is None:
missingd = dict()
def with_mask(func):
def newfunc(val, mask, mval):
if mask:
return mval
else:
return func(val)
return newfunc
formatd = get_formatd(r, formatd)
funcs = []
for i, name in enumerate(r.dtype.names):
funcs.append(with_mask(csvformat_factory(formatd[name]).tostr))
fh, opened = cbook.to_filehandle(fname, 'w', return_opened=True)
writer = csv.writer(fh, delimiter=delimiter)
header = r.dtype.names
writer.writerow(header)
# Our list of specials for missing values
mvals = []
for name in header:
mvals.append(missingd.get(name, missing))
ismasked = False
if len(r):
row = r[0]
ismasked = hasattr(row, '_fieldmask')
for row in r:
if ismasked:
row, rowmask = row.item(), row._fieldmask.item()
else:
rowmask = [False] * len(row)
writer.writerow([func(val, mask, mval) for func, val, mask, mval
in zip(funcs, row, rowmask, mvals)])
if opened:
fh.close()