dak ブログ

python、rubyなどのプログラミング、MySQL、サーバーの設定などの備忘録。レゴの写真も。

python で BigQuery の検索結果を取得

2024-06-24 23:50:50 | SVG
python で BigQuery の検索結果を取得する方法のメモ。
from google.cloud import bigquery

client = bigquery.Client()

query = """
    SELECT *
    FROM prj.ds.tbl
    LIMIT 1
"""

bq_job = client.query(query)
rows = bq_job.result()

for row in rows:
    for k, v in row.items():
        print(f'{k}: {v}')

semaphore による同時実行制御

2024-05-10 00:02:32 | SVG
semaphore による同時実行制御の例。 以下は new Semaphore(1) で同時実行数を1にした実行例です。 ■プログラム
import { setTimeout } from 'timers/promises';
import { Semaphore } from 'await-semaphore';

(() => {
  const s = new Semaphore(1);

  s.use(async () => {
    console.log('before wait 1');
    await setTimeout(5000);
    console.log('after wait 1');
  });

  s.use(async () => {
    console.log('before wait 2');
    await setTimeout(1000);
    console.log('after wait 2');
  });
})();
■実行結果
before wait 1
after wait 1
before wait 2
after wait 2

TypeScript で Cloud Storage のファイルをダウンロード

2024-03-23 00:13:03 | SVG
TypeScript で Cloud Storage のファイルをダウンロードする方法のメモ。
■ライブラリのインストール
npm install @google-cloud/storage
以下のプログラムで cloud-storage-foo/bar/baz.html をダウンロード。
■プログラム
import { Bucket, Storage } from '@google-cloud/storage';

(async () => {
  const target_storage = 'cloud-storage-foo';
  const target_file = 'bar/baz.html';

  const storage = new Storage();
  const bucket = storage.bucket(target_storage);
  const file = bucket.file(target_file);

  const contents = await file.download();
  for (const content of contents) {
    console.log(content.toString('utf-8'));
  }
})();

WordPress.com のアプリケーションパスワード

2024-03-20 23:29:53 | SVG
WordPress.com の ASP サービスでのアプリケーションパスワードの設定方法のメモ。

WordPress.com では、2段階認証の設定を行うとアプリケーションパスワードを設定することができます。 しかし、ここで作成したアプリケーションパスワードでは、REST API の認証がとおりません。

REST API の認証がとおるアプリケーションパスワードは、サイトのドメインが xxx.com であれば、
https://xxx.com/wp-admin/profile.php
にブラウザで直接アクセスします。
「アプリケーションパスワード」欄で「新しいアプリケーションパスワード名」にアプリケーションパスワード名を入力し、 「新しいアプリケーションパスワードを追加」をクリックして、表示されたアプリケーションパスワードを使います。

TypeScript では、以下のようなプログラムで投稿することができます。
imoprt WPAPI from 'wpapi';
(async () => {
  const config = {
    endpoint: 'https://xxx.yyy.zzz/wp-json',
    username: '{user name}',
    password: '{application password}', // uuuu vvvv wwww xxxx yyyy zzzz 形式
  };
  const wpapi = new WPAPI(config);

  const article = {
    title: 'application password test'
  };
  const res = await wapapi.posts().create(article);
	console.log(res);
})();

python で svg を png に変換

2024-02-24 13:21:43 | SVG
python で svg ファイルを png ファイルに変換する方法のメモ。

■ライブラリなどのインストール
pip install svglib
pip install reportlab

sudo yum install cairo cairo-devel
pip install rlpycairo

■プログラム
import sys
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPM

def main():
    in_file = sys.argv[1]
    out_file = sys.argv[2]

    img = svg2rlg(in_file)
    renderPM.drawToFile(img, out_file, fmt='png')

    return 0

exit(main())


SVG の要素にイベントを設定

2023-10-22 21:28:23 | SVG
SVG の要素にイベントを設定

SVG の要素にイベントを設定する方法のメモ。
<html>
<head>

<title></title>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200" style="background-color:#aaaaaa;"> <rect id="id-1" type="target" x="10" y="20" width="30" height="20" fill="#00ff00"/> <rect id="id-2" type="target" x="50" y="60" width="20" height="10" fill="#0000ff"/> </svg>
message area
<script language="javascript"> // クリックイベントハンドラ function click_event_handler(e) { const msg_area = document.getElementById('message_area'); msg_area.innerHTML += `clicked: id="${e.srcElement.id}"`; console.log(e); } // クリックイベントハンドラを設定 const tgts = document.querySelectorAll("[type='target']"); for (let tgt of tgts) { tgt.onclick = (e) => { click_event_handler(e); } } </script> </body> </html>

rect 要素をクリックすると、以下のように rect の id が表示されます。
clicked: id="id-1"
clicked: id="id-2"