i want call functions don't know how import package. tried follows failed. how can do? (i want use third-party package analyzing each doc, code below test)
please, if know answer...
import couchdb db import datetime couch = db.server() d1 = couch['test'] def map(doc): text = doc['text'] ti = doc['timestamp_ms'] ti = ti[:10] + '.' + ti[10:] datearray = datetime.datetime.utcfromtimestamp(float(time)) if (datearray.time().hour < 12): yield ["am"], text else: yield ["pm"], text row in d1.query(map, descending=true, language='python'): print row.key, row.value
you can use datetime inside map function if import datetime inside map function. example -
def map(doc): text = doc['text'] ti = doc['timestamp_ms'] ti = ti[:10] + '.' + ti[10:] from datetime import datetime datearray = datetime.utcfromtimestamp(float(time)) if (datearray.time().hour
however, cannot load third-party libraries in map function. tried importing nose test library , saw in couch logs -
[[{initial_call, {couch_os_process,init,['argument__1']}}, {pid,<0.14643.4>}, {registered_name,[]}, {error_info, {exit, {function_clause, [{couch_os_process,handle_info, [{#port<0.13927>, {data, {eol, <<"{\"log\": \"traceback (most recent call last):\n file \\"/usr/local/lib/python2.7/dist-packages/couchdb/view.py\\", line 79, in map_doc\n results.append([[key, value] key, value in function(doc)])\n file \\"\\", line 5, in map\nimporterror: no module named nose\n\"}">>}}}, {os_proc,"couchpy",#port<0.13927>, #fun, #fun,5000}]}, {gen_server,handle_msg,5}, {proc_lib,init_p_do_apply,3}]}, [{gen_server,terminate,6}, {proc_lib,init_p_do_apply,3}]}},
you might able if install third party library directly on host python installation, not in virtual environment. however, not recommend practice. use virtual env instead always.
Comments
Post a Comment