2012年12月31日月曜日

あけましておめでとうございます。

とりあえずの任期は残り半年ですが、力の続く限りがんばりますので、個人的に知ってる方は密かに応援していてくださると嬉しいです ;)


2012年12月17日月曜日

web2pyをバックグラウンドにする方法

こうしろと書いてある。なんか、ちょっと、いいのか?という気がしないでもない。

nohup python web2py.py -a '<recycle>' -i 127.0.0.1 -p 8000 &

ルートに成れるなら、もっと賢い方法がありそう。
https://sites.google.com/site/web2pyjapan/book/11/linux-unix

2012年12月14日金曜日

web2pyでdatatablesを使う。ついでに、mongodb

staticフォルダにdatatablesのアーカイブを解凍した物をおいてある。

==ビューの部分ここから==

<html>
<head>
<script src="{{=URL(r=request,c='static',f='DataTables-1.9.3/media/js/jquery.js')}}" type="text/javascript"></script>
<script src="{{=URL(r=request,c='static',f='DataTables-1.9.3/media/js/jquery.dataTables.js')}}" type="text/javascript"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" type="text/css">

<style type="text/css" title="currentStyle">
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/media/css/demo_page.css')}}";
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/media/css/jquery.dataTables_themeroller.css')}}";
    @import "{{=URL(r=request,c='static',f='DataTables-1.9.3/examples/examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css')}}";
</style>

<script type="text/javascript" charset="utf-8">
         $(document).ready(function() {
            oTable = $('#example').dataTable({
               "bJQueryUI": true,
               "sPaginationType": "full_numbers",
               "aaSorting": [[0,"desc"]]
            });
         });
</script>
</head>
<body>
<table width="100%" class="display" id="example" border="0" cellpadding="5" cellspacing="5">
   <thead>
      <tr>
        <th>Collection stop date</th>
        <th>Collection stop time</th>
        <th>Category</th>
        <th>ID</th>
        <th>Station</th>
        <th>Detected Nuclide</th>
        <th>Link data</th>
     </tr>
  </thead>
<tbody>
{{
    import datetime
    
    old_id = 0
    for col in records:
      current_id = col["rn_sampleid"]
      utime=col["rn_unixtime"]
      dt=datetime.datetime.fromtimestamp(utime)
    if current_id != old_id:
       old_id = current_id
}}
<tr>
    <td>{{=dt.strftime('%Y/%m/%d')}}</td>
    <td>{{=dt.strftime('%H:%M:%S')}}</td>
    <td>{{=col["rn_category"]}}</td>
    <td>{{=col["rn_sampleid"]}}</td>
    <td>{{=col["rn_station"]}}</td>
    <td>{{=col["rn_nucl"].replace('+',' ')}}</td>
    <td>{{="S"}}</td>
</tr>{{pass}}{{pass}}
</tbody>
  <tfoot>
    <tr>
       <th>Collection stop date</th>
       <th>Collection stop time</th>
       <th>Category</th>
       <th>ID</th>
       <th>Station</th>
       <th>Detected Nuclide</th>
       <th>Link data</th>
    </tr>
  </tfoot>
</table>
</body>
</html>
==ビューの部分ここまで==
==pythonの部分ここから==
import pymongo
import time

def index():

    conn = pymongo.Connection('ip.to.your.machine',27017)
    db   = conn.datafusion
    col  = db.overlaps

    start_time_tuple = (2012,9 ,1,0,0,0,0,0,0)
    end_time_tuple   = (2012,12,1,0,0,0,0,0,0)
    start_unixtime = time.mktime(start_time_tuple)
    end_unixtime   = time.mktime(end_time_tuple)

    data_full = col.find({"rn_unixtime":{"$gte":start_unixtime,"$lt":end_unixtime}})
    conn.disconnect()
        
    return dict(records=data_full)
==pythonの部分ここまで==

2012年12月10日月曜日

Python3でWSGIサーバをスレッド化する


メモ

return [b'hello']
がわからなくてはまった。

from wsgiref import simple_server
from socketserver import ThreadingMixIn

class ThreadingWSGIServer(ThreadingMixIn, simple_server.WSGIServer):
   pass

def myapp(environ, start_response):
   start_response('200 OK', [('Content-type', 'text/plain')])
   return [b'hello']

server = simple_server.make_server('0.0.0.0',8000,myapp,ThreadingWSGIServer)
server.serve_forever()
~

2012年12月5日水曜日

路上でピアノ

この寒いのに。


楽友協会

ウィーンフィルの立ち見のチケットもらえたので行って来ましてん。
一度前の方の席に行かねばと思った次第。


2012年12月4日火曜日

MongoDBのメモ

わたし用MongoDBをCドライバを使って動かすときのメモ。

  1. サーバーインストール
    http://www.mongodb.org/downloads
    から適当にアーカイブ拾ってくる。
    わたしは、Linux64bitなので、以降はそれが前提。

    mongod.confを適当に作る。私は、ここから拾ってきた。
    https://github.com/mongodb/mongo/blob/master/debian/mongodb.conf

    サーバー起動用スクリプト(バックグラウンドになる)

    #!/bin/sh -f
    numactl --interleave=all ./mongodb-linux-x86_64-static-legacy-2.2.2/bin/mongod --config ./mongodb-linux-x86_64-static-legacy-2.2.2/mongod.conf --fork
  2. C API (driver)のインストール
    http://www.mongodb.org/display/DOCS/C+Language+Center
    から、アーカイブ拾ってくる。makeしてできたlibとかヘッダーを適当な場所にコピーする
  3. プログラムする
    基本はチュートリアル、
    http://api.mongodb.org/c/current/tutorial.html
    を参考にすればいいんだけど、C driver v0.7から write concernという新しい変数が必要になる。プログラムは末尾に。コンパイル時に-std=c99するか-DMONGO_HAVE_STDINTしないといけない。私は、後者にしている。色々理由があって。
  4. mongo shellで中身を確認する時は、
    mongo SERVER_FQDN
    して、shellのなかで、
    use COLLECTION_NAME #プログラムの中のmongo_insertで決めてる
    db.DOCUMENTS.find()
    すると、登録した物が見えるはず。
  #include "mongo.h"
  int status;
  mongo mongo_conn[1];
  mongo_write_concern mongo_wc[1];

  mongo_init(mongo_conn);
  mongo_write_concern_init(mongo_wc);
  mongo_wc->w=1;
  mongo_write_concern_finish(mongo_wc);

  mongo_set_op_timeout(mongo_conn,1000);
  status = mongo_client(mongo_conn,"SERVER_FQDN",27017);
  if( status != MONGO_OK ) {
    fprintf(stderr,"error\n");
 }
  
  bson b[1];
  bson_init(b);
  bson_append_new_oid(b,"_id");
  bson_append_int(b,"unixtime",time_integer);
  bson_finish(b);
  status = mongo_insert(mongo_conn,"COLLECTION_NAME.DOCOMENTS",b,mongo_wc);
  if(status != MONGO_OK){
      fprintf(stderr,"%s\n",msg_mongo(status));
  }
  bson_destroy(b);
  mongo_write_concern_destroy(mongo_wc);
  mongo_destroy(mongo_conn);

2012年12月3日月曜日

UNWGバザー

VICの横の会場で、UNWG主催のバザーがありました。
いろいろな国が出店していて、楽しゅうございました。
写真は、同室の人の出身のカザフスタンの人が民族衣装をまとっていたので、撮らせてもらいました。