ssk’s posterous

ssk’s posterous

Nov 21 / 12:01am

prefix を外すとどうなるか

by ssk
>> :~/src/thrudb/tutorial/py# python BookmarkExample.py
>> Traceback (most recent call last):
>> File "BookmarkExample.py", line 194, in
>> bm.load_tsv_file("../bookmarks.tsv")
>> File "BookmarkExample.py", line 82, in load_tsv_file
>> self.add_bookmark(b)
>> File "BookmarkExample.py", line 89, in add_bookmark
>> bid = self.store_bookmark(b)
>> File "BookmarkExample.py", line 100, in store_bookmark
>> bid = self.thrudoc.putValue(THRUDOC_BUCKET, b_str)
>> File "../gen-py/Thrudoc/Thrudoc.py", line 242, in putValue
>> return self.recv_putValue()
>> File "../gen-py/Thrudoc/Thrudoc.py", line 266, in recv_putValue
>> raise result.e
>> Thrudoc.ttypes.ThrudocException: ThrudocException(what='S3Backend
error', type=1)

S3Backend error になるのでなんか入れないとだめ

Filed under  //  amazonEC2   amazonS3   python  

Comments (0)

Nov 20 / 11:39pm

Thrudb error message

by ssk

s3 buffer:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>NoSuchKey</Code><Message>The specified key does not exist.</Message><Key>td_bookmarks</Key><RequestId>1FFCA809BE735AF4</RequestId><HostId>+xGw8qmEXQ1Ep2q0jHjFbrXHurwp/YNAN8Yxzfvsmkmy45VirPLSv8tdIdmaEekv</HostId></Error>izati�Traceback (most recent call last):
  File "BookmarkExample.py", line 197, in <module>
    bm.remove_all()
  File "BookmarkExample.py", line 125, in remove_all
    for ids in chunker(seed, 100, self.thrudoc.scan):
  File "BookmarkExample.py", line 28, in chunker
    chunk = func(THRUDOC_BUCKET, seed, size)
  File "../gen-py/Thrudoc/Thrudoc.py", line 277, in scan
    return self.recv_scan()
  File "../gen-py/Thrudoc/Thrudoc.py", line 290, in recv_scan
    (fname, mtype, rseqid) = self._iprot.readMessageBegin()
  File "usr/lib/python2.5/site-packages/thrift/protocol/TBinaryProtocol.py", line 126, in readMessageBegin
  File "usr/lib/python2.5/site-packages/thrift/protocol/TBinaryProtocol.py", line 203, in readI32
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 58, in readAll
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 267, in read
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 271, in readFrame
  File "usr/lib/python2.5/site-packages/thrift/transport/TTransport.py", line 58, in readAll
  File "usr/lib/python2.5/site-packages/thrift/transport/TSocket.py", line 94, in read
thrift.transport.TTransport.TTransportException: TSocket read 0 bytes

 

エラーが出るが s3 バケットは出来ていた。

Filed under  //  amazonEC2   amazonS3   python  

Comments (0)

Nov 9 / 8:07pm

PythonでURIエンコードするには - HM python - pythonグループ

by ssk

URIエンコードとデコードにはurllibモジュールのquote()とunquote()が使へる。

# -*- coding: utf-8; -*-

import urllib

print urllib.quote('いろはにほへと')
print urllib.quote(u'いろはにほへと'.encode('euc-jp'))
print urllib.quote(u'いろはにほへと'.encode('shift_jis'))
print urllib.quote(u'いろはにほへと'.encode('iso-2022-jp'))
print urllib.quote(u'いろはにほへと')
print urllib.unquote('%E3%81%84%E3%82%8D%E3%81%AF%E3%81%AB%E3%81%BB%E3%81%B8%E3%81%A8')

實行結果:

%E3%81%84%E3%82%8D%E3%81%AF%E3%81%AB%E3%81%BB%E3%81%B8%E3%81%A8
%A4%A4%A4%ED%A4%CF%A4%CB%A4%DB%A4%D8%A4%C8
%82%A2%82%EB%82%CD%82%C9%82%D9%82%D6%82%C6
%1B%24B%24%24%24m%24O%24K%24%5B%24X%24H%1B%28B
%3044%308D%306F%306B%307B%3078%3068
いろはにほへと

クエリ文字列用のエンコード・デコードにはquote_plus()とunquote_plus()。空白が+に、+が空白になる。

# -*- coding: utf-8; -*-

import urllib

print urllib.quote_plus('Python URIエンコード')
print urllib.unquote_plus('Python+URI%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%89')

實行結果:

Python+URI%E3%82%A8%E3%83%B3%E3%82%B3%E3%83%BC%E3%83%89
Python URIエンコード

Filed under  //  python  

Comments (1)

Oct 29 / 1:04am

pythonとperlとphpとas3とjavascriptでJSON « taichino.com

by ssk

python
2.6系だとJSONが標準で組み込まれてるらしいんですが、手元は2.5系なのでsimplejsonを使っています。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, codecs
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
 
import simplejson
 
data = { 'items':[
  {'name':'iPhone',  'price':50000},
  {'name':'macbook', 'price':100000},
  {'name':"マクド",  'price':100},
]}
text = simplejson.dumps(data)  # encode
copy = simplejson.loads(text)  # decode
 
print "data = " + str(data)
print "text = " + text
print "copy = " + str(copy)
for item in copy['items']:
  print item["name"]

Filed under  //  json   python  

Comments (0)

Oct 25 / 9:27pm

arbiter's dream place in Utah: Python and MySQL technique - Render JSON ouput based on MySQLdb to MySQL

by ssk
Here is the code.
import MySQLdb
import simplejson as json
db = MySQLdb.connect(host='xxx', user='xx', passwd='xx', db='xxx')
cursor = db.cursor()
def getSQLJSON(hostName,dbName,id,pw,sql):
global db,cursor
resultArray =[]
cursor.execute(sql)
tableDescription = cursor.description
result = cursor.fetchall()
for e in result:
resultRaw ={}
columnIndex =0
for ee in e:
vcoulumnName = tableDescription [columnIndex][0]
resultRaw[coulumnName]=ee
columnIndex +=1
resultArray.append(resultRaw)
jsonResult = json.dumps(resultArray, separators=(',',':'))
return jsonResult

mysql の出力を JSON 形式へ?

Filed under  //  json   mysql   python  

Comments (0)

Sep 26 / 1:36am

2606-36J: Amazon EBS Snapshotスクリプト

by ssk
cronなんかに
0 0 * * * /path/to/snapshot.py 7 vol-00000000
という感じで登録しておく事を前提に作りました。

  1. #!/usr/bin/env python  
  2.   
  3. import sys  
  4. from boto.ec2.connection import EC2Connection  
  5.   
  6. if(len(sys.argv) != 3):  
  7.     print "Usage: snapshot.py <num> <volume-id>"  
  8.     quit()  
  9.   
  10. conn = EC2Connection('aws_access_key',' aws_secret_access_key');  
  11. conn.create_snapshot(sys.argv[2])  
  12. snapshot = {}  
  13. for x in conn.get_all_snapshots():  
  14.     if(x.volume_id == sys.argv[2]):  
  15.         tmp = {x.id:x.start_time}  
  16.         snapshot.update(tmp)  
  17. snapshot = sorted(snapshot.items(), key=lambda (k, v): (v, k), reverse=True)  
  18. for i in range(int(sys.argv[1]), len(snapshot)):  
  19.     conn.delete_snapshot(snapshot[i][0])  

amazon ec2

Filed under  //  amazonEC2   python  

Comments (0)

Sep 9 / 2:16am

sK1 を windows python でインストールしようとしたらエラー

by ssk
error: Python was built with Visual Studio 2003;
extensions must be built with a compiler than can generate compatible binaries.
Visual Studio 2003 was not found on this system. If you have Cygwin installed,
you can try compiling with MingW32, by passing "-c mingw32" to setup.py.

VS でビルドした python ではダメだそうな。

Filed under  //  python   windows  

Comments (0)

Jul 16 / 1:50am

Frank Wierzbicki's Weblog: SQLAlchemy, MySQL, and Jython

by ssk
from sqlalchemy import *

db = create_engine('mysql://username:password@localhost/mydb')
metadata = BoundMetaData(db)

users_table = Table('users', metadata, autoload=True)

s = users_table.select()
r = s.execute()
print r.fetchall()

Filed under  //  python   sqlalchemy  

Comments (0)

Jul 8 / 10:52pm

Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip

by ssk
For the modern Python programmer, some of the most important tools to aid in reduced complexity and repetition are virtualenv, Fabric, and pip. Although these tools have no strict relationship (in the sense that many people may use one or two of these tools often, yet aren't even aware of the others), they form a powerful suite when combined. An excellent example is the following:

モダンな pythonista 向け。

Filed under  //  python   virtualenv  

Comments (0)