r77588 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r77587‎ | r77588 | r77589 >
Date:17:11, 2 December 2010
Author:diederik
Status:deferred
Tags:
Comment:
* Handle more edge cases
Modified paths:
  • /trunk/tools/editor_trends/utils/utils.py (modified) (history)

Diff [purge]

Index: trunk/tools/editor_trends/utils/utils.py
@@ -215,20 +215,19 @@
216216 fh.write('\n')
217217
218218
219 -def write_dict_to_csv(data, fh, write_key=True, newline=True):
220 - keys = data.keys()
221 - keys.sort()
 219+def write_dict_to_csv(data, fh, keys, write_key=True, newline=True):
222220 for key in keys:
223221 if write_key:
224222 fh.write('%s\t' % key)
225 - if getattr(data[key], '__iter__', False):
 223+ if type(data[key]) == type(list):
 224+ write_list_to_csv(data[key], fh, recursive=False, newline=False)
 225+ elif getattr(data[key], '__iter__', False):
226226 for d in data[key]:
227227 fh.write('%s\t' % d)
228228 else:
229229 fh.write('%s\t' % (data[key]))
230230 if newline:
231231 fh.write('\n')
232 - return False #this prevents the calling function from writing \t
233232
234233
235234 def create_txt_filehandle(location, name, mode, encoding):
@@ -315,15 +314,26 @@
316315 return dict([[v, k] for k, v in dictionary.items()])
317316
318317
319 -def create_dict_from_csv_file(location, filename, encoding):
 318+def create_dict_from_csv_file(location, filename, encoding, keys=None):
320319 '''
321320 Constructs a dictionary from a txtfile
 321+ The first column of the csv file should contain the main key for the dictionary.
 322+ If there are more than one value in the values list, then a @keys variable should
 323+ be supplied and the key sequence should match the value sequence.
322324 '''
323325 d = {}
324326 for line in read_data_from_csv(location, filename, encoding):
325327 line = clean_string(line)
326 - value, key = line.split('\t')
327 - d[key] = value
 328+ line = line.split('\t')
 329+ key = line[0]
 330+ values = line[1:]
 331+ if len(values) == 1:
 332+ d[key] = values
 333+ else:
 334+ assert keys != None
 335+ d[key] = {}
 336+ for k, v in zip(keys, values):
 337+ d[key][k] = v
328338 return d
329339
330340

Status & tagging log