MIME::Lite::HTMLで図書館の新着を追いかける
図書館も新着のRSSとか出すようにしたほうがいいと思うんだけどなぁ。良い本との出会いは心を豊かにするんだったら、出会う頻度をあげるような努力をするのもまぁ図書館の役割なんじゃないかなとか。
と、本当はそんなことはどうでもよくてMIME::Lite::HTMLってのを使えばURLを指定するだけで、HTMLをメールで送ってくれるってのが便利そうだったので、最近使い勝手の悪いサービスの中から適当なサンプルとして図書館の新着検索が選択されただけでした。
プルをプッシュに的な。
use MIME::Lite::HTML;
MIME::Lite::HTML->new(
From => 'kzfm@***.jp',
To => '***@gmail.com',
HTMLCharset => 'utf8',
Subject => '図書館新着',
Url => 'http://library.fujishi.jp/opw/******');
検索結果のURLを渡してHTMLCharsetをきちんと指定すればOK。あとはcronに登録。
Chemistry::Mol
Chemistry::Molでちょっとはまった。
Can't call method "symbol" on an undefined value
というエラーでつまづいた。
my $mol2 = $mol->clone;
Makes a copy of a molecule. Note that this is a deep copy; if your molecule has a pointer to the rest of the universe, the entire universe will be cloned! my $mol2 = $mol->safe_clone;
Like clone, it makes a deep copy of a molecule. The difference is that the copy is not "exact" in that new molecule and its atoms and bonds get assigned new IDs. This makes it safe to combine cloned molecules. For example, this is an error:
# XXX don't try this at home!
my $mol2 = Chemistry::Mol->combine($mol1, $mol1);
# the atoms in $mol1 will clash
But this is ok:
# the "safe clone" of $mol1 will have new IDs
my $mol2 = Chemistry::Mol->combine($mol1, $mol1->safe_clone);
perlでOASAライブラリを使いたい
Inline::Pythonを使うだけ。お手軽
use Inline Python => <<'END';
import pybel
def draw_png(smiles,file):
mymol = pybel.readstring('smi',smiles)
mymol.draw(filename=file, show=False)
return True
END
draw_png("CCCc1ccccc1OC","xxx.png");
TODO: あとでクックブックに追加しておく
肥大化するリスト
こんな感じで肥大化するリストをつくると(実際にやりたいことは実行途中のあるタイミングでリストの先頭に戻ってループさせながらリストを大きくしたい)
my @l = (0);
for my $n (@l) {
push @l,$n+1;
print $n,"\n";
}
数百万件のデータを処理したいので、これをDBIx::Classを使ってやろうと。
で、こんな風に書きました。
#!/usr/bin/perl
#-- tables
#
#create table compounds (
# id integer PRIMARY KEY not null,
# num integer not null
#);
#insert into compounds values(null,1);
package TestSchema;
use strict;
use base qw/DBIx::Class::Schema::Loader/;
__PACKAGE__->loader_options(
debug => 1,
exclude => qr/^sqlite_/,
);
package Main;
my $schema = TestSchema->connect('dbi:SQLite:dbname=test.db');
my $rs = $schema->resultset('Compounds')->search;
while (my $row = $rs->next) {
my $n = $row->num;
$schema->resultset('Compounds')->create({num => $n+1});
printf "* %s\n", $n;
}
この場合、データの行数が一つだとうまくいかない(next一回で終了してしまう。2個以上データが入っていればリストの時のコードの様に動く)。でも、初期データは一つしかないので困る。ダミーでなんか入れとけばいいのだけど、BFSっぽく繰り返したい場合ダミーのデータは邪魔だし、なんか無駄なことやっている気がする。
うまいやり方ないもんか。
「すぐわかる オブジェクト指向 Perl」を読んだ
いくつかあやふやな知識が解消されてよかった。
- Class::Data::Inheritableで変数の継承が可能になる
- (perlはメソッドの継承しかしない)
- use overloadの '""'はpythonの__str__
- Storableのnstoreはネットワークオーダー
- perl - 勝手に添削 - Storableの理由をきちんと理解した。
続・初めてのPerlと一緒に読むか併せて読めばよいのではなかろうか?
続・初めてのPerl 改訂版Randal L. Schwartz,brian d foy,Tom Phoenix,吉川 英興,伊藤 直也,田中 慎司,株式会社ロングテール/長尾 高弘
オライリー・ジャパン / ¥ 3,360 ()
通常24時間以内に発送
CPAN Testerからメールが来た
CPANって地味に凄いなと。
An automated check of reports sent to CPAN Testers detected some issues with
your CPAN Testers configuration. Please check your configuration and upgrade
out-of-date modules to help ensure your test reports are consistent with
the latest bug fixes and CPAN Testers standards.
Issues are listed in YAML format with number of occurrences by architecture and
Perl version since the last notification. If you upgraded recently, this list
may cover reports since the notice, but prior to your upgrade. If so, please
disregard those items.
---
i386-linux:
5.8.8:
CPAN::Reporter out-of-date (1.08 < 1.17): 1
Test::Reporter out-of-date (1.38 < 1.5): 1
早速アップデート
ケモインフォクックブックはじめました
bioinformaticsの学習道路はそこそこ充実している。
でも、chemoinformaticsだってオープンソースで色々できるし、独学でも結構いけるよ!なんて思うんだけど情報が足りないなぁと思った。
というわけで、ちょっと小道でも。
特にperl,pythonでちょっとした化学情報をいじるコードをクックブック形式で書いていけたらなぁなんて思ってる。
週に一回くらいは更新するつもりで。
setuidされたCGIを実行するためのperl
ごくたまにこんなことやりたかったりすることありますよね。
use File::Tail;
sub get_ip{
my $line = File::Tail->new(name => "/var/log/httpd/access_log", tail => 1)->read();
my ($ip) = split / /, $line;
return $ip;
}
print "Content-Type: text/plain\n\n", get_ip();
rootじゃないとアクセスできないのでsetuidする必要がありますが、それだけだとエラー吐いてうごかないので、fedora8もそれ用のperlが必要。
yum install perl-suidperl
スクリプトも
#!/usr/bin/suidperl
ではじめる必要がある。
Inline::Python
Inline::Pythonのバージョンがあがったのでmacbookに入れて遊んでみた。
generatorが使いたかったのだけど、nextを呼ぼうとすると
Can't call method "next" without a package or object reference at test.pl line 3.
と怒られてしまうのであった。
Web::Scraperではてブスクレイピング
自分のブログをブックマークしたひとのはてダをLDRで購読する事が多いので、scrapeしてURLを表示させてみる
use URI;
use Web::Scraper;
use List::MoreUtils qw/uniq/;
my $uri = URI->new('http://b.hatena.ne.jp/bookmarklist?url=http://blog.kzfmix.com');
my $scraper = scraper {
process 'dt.bookmarker a',
'users[]'=> '@href';
};
my $res = $scraper->scrape($uri);
print join "\n", map { s/b.hatena/d.hatena/;$_} uniq(@{$res->{users}});
あとはこの結果をGmailに送ればよいような。



すぐわかる オブジェクト指向 Perl