Permalink
Browse files

CBD-603: Parse numeric field as number

Except id field, we need to parse numeric field value as number
instead of string

Change-Id: I7e67afc291220472c2af2230bc709b91cf6d4ede
Reviewed-on: http://review.couchbase.org/21199
Tested-by: Bin Cui <bin.cui@gmail.com>
Reviewed-by: Pavel Paulau <pavel.paulau@gmail.com>
  • Loading branch information...
1 parent 73bd2a2 commit c82db287eab652d25116b042d4627a6931722a8e @bcui6611 bcui6611 committed with Peter Wansch Oct 13, 2012
Showing with 13 additions and 1 deletion.
  1. +13 −1 pump_csv.py
View
@@ -9,6 +9,15 @@
import memcacheConstants
import pump
+def number_try_parse(str):
+ for func in (int, float):
+ try:
+ return func(str)
+ except ValueError:
+ pass
+
+ return str
+
class CSVSource(pump.Source):
"""Reads csv file, where first line is field names and one field
should be 'id'."""
@@ -68,7 +77,10 @@ def provide_batch(self):
vals = self.r.next()
doc = {}
for i, field in enumerate(self.fields):
- doc[field] = vals[i]
+ if field == 'id':
+ doc[field] = vals[i]
+ else:
+ doc[field] = number_try_parse(vals[i])
doc_json = json.dumps(doc)
msg = (cmd, vbucket_id, doc['id'], flg, exp, cas, '', doc_json)
batch.append(msg, len(doc))

0 comments on commit c82db28

Please sign in to comment.