2019/08/04

GCP: GAE/Py から、ElasticSearch(GCE上で稼働)を呼び出す -- Python

  • GOAL: レコメンドエンジンとしてElasticSearchを立ち上げ&セットアップする。その後、Google App Engine(GAE)から、ElasticSearchAPIを呼び出して使う。
  • POINT
    1.GAE
    からの外部HTTPREST API)呼び出しは、POSTで行うこと! GETは使うな!!
    (GAE/Py27の環境で、requestsを利用して呼び出す場合)
    2.Logstash
    は、必ず「Ctrl+D」で終了させること!
    Ctrl+Z
    で終了させていたため、サービスが起動中となりハマりました。
  • HOW
    1.Martket Placeから、ELK(ElasticSearch+Logstash+Kibana)を起動
    2.Logstashを使って、CSVデータをElasticSearchにロード
    3.GAEから、ElasticSearchに検索クエリーを発行して結果を取得
    ----------------
    1.Martket Placeから、ELK(ElasticSearch+Logstash+Kibana)を起動
       ・Bitnamiの仮想マシンを選択
       ・手順に沿って、選択をしていくだけ


    2.Logstashを使って、CSVデータをElasticSearchにロード


    3.GAEから、ElasticSearchに検索クエリーを発行して結果を取得

2019/07/27

GCP: Iphoneから、端末位置情報をIoT分析環境に送信する -- python, GCP, IoTcore

Goal: 「IoTデータ分析環境立ち上げ on GCP」の環境に対して、Iphoneから位置情報データをPushするサンプル。Iphoneをエッジ端末として活用して、色々できそう。

How 

0. 前準備 Pythonista関連
   a)Pythonista のインストール
   https://apps.apple.com/jp/app/pythonista-3/id1085978097
   b)Gitを使いたかったら、stashをインストール
   https://github.com/ywangd/stash
   c)説明サイト ググったらたくさんあると思いますが、私は、以下のリンクを参照させていただきました。ありがとうございました。
   http://hitoriblog.com/?p=42145#Python-5




1. Pythonista を使って、端末の位置情報、スピード等を取得する
  a)Iphone上で、位置情報などを確認してみる
import location
import json
import datetime
address_now=location.get_location()
address_now.update(created=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"))
output = json.dumps(address_now)
print (output)
print(type(output))
print(type(address_now))
results = location.reverse_geocode(address_now)
print (results)




2. GCPのPub/Subにパブリッシュする。これの詳細は、前出のブログを参照

   a)GCP提供のサンプルプログラムをベースに追加加工する。

      https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/iot/api-client/mqtt_example
   をローカルPCにクローンして、Pythonの環境などをセットする。
   cloudiot_mqtt_example.pyをベースに変更を加えたものが以下。

#!/usr/bin/env python
import argparse
import datetime
import os
import random
import ssl
import time
import jwt
# --- 追加(ここから) ---
from jwt.contrib.algorithms.pycrypto import RSAAlgorithm
from jwt.contrib.algorithms.py_ecdsa import ECAlgorithm
try:
jwt.unregister_algorithm('RS256')
jwt.unregister_algorithm('ES256')
except:
pass
jwt.register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256))
jwt.register_algorithm('ES256', ECAlgorithm(ECAlgorithm.SHA256))
import location
import json
# --- 追加(ここまで) ---
import paho.mqtt.client as mqtt
# The initial backoff time after a disconnection occurs, in seconds.
minimum_backoff_time = 1
# The maximum backoff time before giving up, in seconds.
MAXIMUM_BACKOFF_TIME = 32
# Whether to wait with exponential backoff before publishing.
should_backoff = False
# [START iot_mqtt_jwt]
def create_jwt(project_id, private_key_file, algorithm):
token = {
# The time that the token was issued at
'iat': datetime.datetime.utcnow(),
# The time the token expires.
'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60),
# The audience field should always be set to the GCP project id.
'aud': project_id
}
# Read the private key file.
with open(private_key_file, 'r') as f:
private_key = f.read()
print('Creating JWT using {} from private key file {}'.format(
algorithm, private_key_file))
return jwt.encode(token, private_key, algorithm=algorithm)
# [END iot_mqtt_jwt]
# [START iot_mqtt_config]
def error_str(rc):
"""Convert a Paho error to a human readable string."""
return '{}: {}'.format(rc, mqtt.error_string(rc))
def on_connect(unused_client, unused_userdata, unused_flags, rc):
"""Callback for when a device connects."""
print('on_connect', mqtt.connack_string(rc))
# After a successful connect, reset backoff time and stop backing off.
global should_backoff
global minimum_backoff_time
should_backoff = False
minimum_backoff_time = 1
def on_disconnect(unused_client, unused_userdata, rc):
"""Paho callback for when a device disconnects."""
print('on_disconnect', error_str(rc))
# Since a disconnect occurred, the next loop iteration will wait with
# exponential backoff.
global should_backoff
should_backoff = True
def on_publish(unused_client, unused_userdata, unused_mid):
"""Paho callback when a message is sent to the broker."""
print('on_publish')
def on_message(unused_client, unused_userdata, message):
"""Callback when the device receives a message on a subscription."""
payload = str(message.payload)
print('Received message \'{}\' on topic \'{}\' with Qos {}'.format(
payload, message.topic, str(message.qos)))
def get_client(
project_id, cloud_region, registry_id, device_id, private_key_file,
algorithm, ca_certs, mqtt_bridge_hostname, mqtt_bridge_port):
"""Create our MQTT client. The client_id is a unique string that identifies
this device. For Google Cloud IoT Core, it must be in the format below."""
client = mqtt.Client(
client_id=('projects/{}/locations/{}/registries/{}/devices/{}'
.format(
project_id,
cloud_region,
registry_id,
device_id)))
# With Google Cloud IoT Core, the username field is ignored, and the
# password field is used to transmit a JWT to authorize the device.
client.username_pw_set(
username='unused',
password=create_jwt(
project_id, private_key_file, algorithm))
# Enable SSL/TLS support.
client.tls_set(ca_certs=ca_certs, tls_version=ssl.PROTOCOL_TLSv1_2)
# Register message callbacks. https://eclipse.org/paho/clients/python/docs/
# describes additional callbacks that Paho supports. In this example, the
# callbacks just print to standard out.
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.on_message = on_message
# Connect to the Google MQTT bridge.
client.connect(mqtt_bridge_hostname, mqtt_bridge_port)
# This is the topic that the device will receive configuration updates on.
mqtt_config_topic = '/devices/{}/config'.format(device_id)
# Subscribe to the config topic.
client.subscribe(mqtt_config_topic, qos=1)
# The topic that the device will receive commands on.
mqtt_command_topic = '/devices/{}/commands/#'.format(device_id)
# Subscribe to the commands topic, QoS 1 enables message acknowledgement.
print('Subscribing to {}'.format(mqtt_command_topic))
client.subscribe(mqtt_command_topic, qos=0)
return client
# [END iot_mqtt_config]
def parse_command_line_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description=(
'Example Google Cloud IoT Core MQTT device connection code.'))
parser.add_argument(
'--project_id',
default=os.environ.get('GOOGLE_CLOUD_PROJECT'),
help='GCP cloud project name')
parser.add_argument(
'--registry_id', required=True, help='Cloud IoT Core registry id')
parser.add_argument(
'--device_id', required=True, help='Cloud IoT Core device id')
parser.add_argument(
'--private_key_file',
required=True, help='Path to private key file.')
parser.add_argument(
'--algorithm',
choices=('RS256', 'ES256'),
required=True,
help='Which encryption algorithm to use to generate the JWT.')
parser.add_argument(
'--cloud_region', default='us-central1', help='GCP cloud region')
parser.add_argument(
'--ca_certs',
default='roots.pem',
help=('CA root from https://pki.google.com/roots.pem'))
parser.add_argument(
'--num_messages',
type=int,
default=100,
help='Number of messages to publish.')
parser.add_argument(
'--message_type',
choices=('event', 'state'),
default='event',
help=('Indicates whether the message to be published is a '
'telemetry event or a device state message.'))
parser.add_argument(
'--mqtt_bridge_hostname',
default='mqtt.googleapis.com',
help='MQTT bridge hostname.')
parser.add_argument(
'--mqtt_bridge_port',
choices=(8883, 443),
default=8883,
type=int,
help='MQTT bridge port.')
parser.add_argument(
'--jwt_expires_minutes',
default=20,
type=int,
help=('Expiration time, in minutes, for JWT tokens.'))
return parser.parse_args()
# [START iot_mqtt_run]
def main():
global minimum_backoff_time
args = parse_command_line_args()
# Publish to the events or state topic based on the flag.
sub_topic = 'events' if args.message_type == 'event' else 'state'
mqtt_topic = '/devices/{}/{}'.format(args.device_id, sub_topic)
jwt_iat = datetime.datetime.utcnow()
jwt_exp_mins = args.jwt_expires_minutes
client = get_client(
args.project_id, args.cloud_region, args.registry_id, args.device_id,
args.private_key_file, args.algorithm, args.ca_certs,
args.mqtt_bridge_hostname, args.mqtt_bridge_port)
# --- 追加(ここから) ---
location.start_updates()
time.sleep(1)
# --- 追加(ここまで) ---
# Publish num_messages mesages to the MQTT bridge once per second.
for i in range(1, args.num_messages + 1):
# Process network events.
client.loop()
# Wait if backoff is required.
if should_backoff:
# If backoff time is too large, give up.
if minimum_backoff_time > MAXIMUM_BACKOFF_TIME:
print('Exceeded maximum backoff time. Giving up.')
break
# Otherwise, wait and connect again.
delay = minimum_backoff_time + random.randint(0, 1000) / 1000.0
print('Waiting for {} before reconnecting.'.format(delay))
time.sleep(delay)
minimum_backoff_time *= 2
client.connect(args.mqtt_bridge_hostname, args.mqtt_bridge_port)
# --- 修正(ここから) ---
outputdata = location.get_location()
outputdata.update(created=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S"),dev_id=args.device_id)
payload = json.dumps(outputdata)
# --- 修正(ここまで) ---
print('Publishing message {}/{}: \'{}\''.format(
i, args.num_messages, payload))
# [START iot_mqtt_jwt_refresh]
seconds_since_issue = (datetime.datetime.utcnow() - jwt_iat).seconds
if seconds_since_issue > 60 * jwt_exp_mins:
print('Refreshing token after {}s').format(seconds_since_issue)
jwt_iat = datetime.datetime.utcnow()
client = get_client(
args.project_id, args.cloud_region,
args.registry_id, args.device_id, args.private_key_file,
args.algorithm, args.ca_certs, args.mqtt_bridge_hostname,
args.mqtt_bridge_port)
# [END iot_mqtt_jwt_refresh]
# Publish "payload" to the MQTT topic. qos=1 means at least once
# delivery. Cloud IoT Core also supports qos=0 for at most once
# delivery.
client.publish(mqtt_topic, payload, qos=1)
# Send events every second. State should not be updated as often
time.sleep(1 if args.message_type == 'event' else 5)
# --- 追加(ここから) ---
location.stop_updates()
# --- 追加(ここまで) ---
print('Finished.')
# [END iot_mqtt_run]
if __name__ == '__main__':
main()


 
   

2019/06/09

文字列内の検索  str.find() str.rfind() --- Python3

Goal: 文字列の中に、特定の文字列が含まれているか? 含まれている場合は、INDEXを取得する。

How: 文字列メソッド find() 、rfind() を使う。

例)
01234567890123456789012345678901
THIS IS A PEN. THIS IS AN APPLE.

に、文字列が含まれているかどうかを確認する。

1: 'IS' を前から探す →  2が返ってくる
2: 'THIS' を前から探す → 0が返ってくる
3: 'THIS' を後ろから探す → 15が返ってくる
4: 'PINEAPPLE' を探す → 無いから、-1が返ってくる

Source:

# find と rfind メソッドを試す
a = 'THIS IS A PEN. THIS IS AN APPLE.'
# 1: 'IS' を前から探す → 2が返ってくる
print(a.find('IS'))
# 2: 'THIS' を前から探す → 0が返ってくる
print(a.find('THIS'))
# 3: 'THIS' を後ろから探す → 15が返ってくる
print(a.rfind('THIS'))
# 4: 'PINEAPPLE' を探す → 無いから、-1が返ってくる
print(a.find('PINEAPPLE'))
view raw find_rfind.py hosted with ❤ by GitHub


Result:

-> % python test.py
2
0
15
-1

2019/05/27

2次元配列を複数のKeyでソートする --- Python3


  • Goal
    2次元配列を複数のKeyでソートしたい
  • How
    itemgetterを使う。
  • Example
    入力)
    Udon 500 大阪店
    Udon 700 東京店
    Ramen 1000 大阪店
    Ramen 1200 大阪店
    Udon 500 福岡店
    Apple 100 福岡店

    これを、メニュー(ABC順)→値段(高いものから)並べたい。
  • Source

    # -*- coding: utf-8 -*-
    import sys
    from operator import itemgetter
    # input
    a = []
    for i in range(6):
    a1, a2, a3 = map(str, input().split())
    a.append([a1, int(a2), a3])
    # ソート前
    print('Before Sort')
    print(a)
    print('\nSort by Price')
    a.sort(key=itemgetter(1), reverse=True)
    print(a)
    print('\nThen sort by menu')
    a.sort(key=itemgetter(0))
    print(a)

  • Result
    % python sort_itemgetter.py
    Udon 500 大阪店
    Udon 700 東京店
    Ramen 1000 大阪店
    Ramen 1200 大阪店
    Udon 500 福岡店
    Apple 100 福岡店
    Before Sort
    [['Udon', 500, '大阪店'], ['Udon', 700, '東京店'], ['Ramen', 1000, '大阪店'], ['Ramen', 1200, '大阪店'], ['Udon', 500, '福岡店'], ['Apple', 100, '福岡店']]

    Sort by Price
    [['Ramen', 1200, '大阪店'], ['Ramen', 1000, '大阪店'], ['Udon', 700, '東京店'], ['Udon', 500, '大阪店'], ['Udon', 500, '福岡店'], ['Apple', 100, '福岡店']]

    Then sort by menu
    [['Apple', 100, '福岡店'], ['Ramen', 1200, '大阪店'], ['Ramen', 1000, '大阪店'], ['Udon', 700, '東京店'], ['Udon', 500, '大阪店'], ['Udon', 500, '福岡店']]

2019/05/12

素因数分解 (試し割り法) の根本理解  - Python3

競技プログラミングサイトで遊んでいると、素因数分解の知識が必要となり、再勉強をした。

1.素因数分解を思い出す。
小さい素数から、割り算を繰り返して、これ以上どの数字でも割れないとなったら終了という理解。(以下の画像のイメージ)

これを、試し割り法というとの事。

2.試し割り法のアルゴリズムの理解を深める。
以下のサイトを参考にさせて頂いて、試し割り法のアルゴリズムを見た。
Thanks!!
Pythonで素因数分解する
素数と仲良しになる3つのプログラム

が、素人の為、正直良くわからない部分があり、1行づつ理解を深めて見た。

私の理解としては。。。

ある数(素数候補)として、初期値を2とする。

①”ある数(素数候補)”で割り切れるかどうかを見る?
②入力数字を”ある数”で実際に割り、商に置き換える。

①と②を割りきれなくなるまで繰り返す。

割り切れなくなったら、
③次に、”ある数”を1つ大きくして、①と②を繰り返す。

で、④残りの数を、”ある数”で割った商が、”ある数”よりも小さくなったら終わり。
何故なら、2から初めて、1づつ”ある数”を大きくしながらで割っていくので、
これより先、割り切れる事はない。(割り切れるなら、この段階にくるまでに割り切れているはず。)

。。。。ということで、説明は上手くないが、以下を実行して、腹落ちした。

# -*- coding: utf-8 -*-
# ColabのPython3(Python 3.6.7)で実行
################
# 素因数分解   試し割り法
################
def factorization(n):
i = 2
l = []
while i * i <= n:
print(f'CP1: {i}で割り切れるかどうか調べる。') ## CheckPoint1
while n % i == 0:
n /= i
print(f'CP2: {i}で割り切れたから、{i}を出力する。') ## CheckPoint2
l.append(i)
print(f'この時点の商:{int(n)}')
print(f'この時点の出力予定:{l}')
print(f'まだ、{i}で割り切れるかどうかを確認する。\n')
print(f'CP3: {i}では、もう割り切れなくなったので、') ## CheckPoint3
i += 1
print(f'次は、{i}で割り切れるかどうかを試す。\n')
print(f'CP4: {int(n)}を{i}で、割ったとしても、商は{i}より小さくなるので、これ以上は検討不要。') ## CheckPoint4
print(f'残っている数を、最後の約数として追加する。ただし、1の場合は、無視する。')
if n > 1:
l.append(int(n))
print(f'{int(n)}を出力して計算終了。\n')
return l
# input
a = int(input())
#a = 10000000000
print(f'{a}の素因数分解をする。\n')
# calc
o = factorization(a)
# output
print('output----')
print(o)


以下、上記プログラムの実行結果。360を素因数分解している。

-> % python factorization.py
360
360の素因数分解をする。

CP1: 2で割り切れるかどうか調べる。
CP2: 2で割り切れたから、2を出力する。
この時点の商:180
この時点の出力予定:[2]
まだ、2で割り切れるかどうかを確認する。

CP2: 2で割り切れたから、2を出力する。
この時点の商:90
この時点の出力予定:[2, 2]
まだ、2で割り切れるかどうかを確認する。

CP2: 2で割り切れたから、2を出力する。
この時点の商:45
この時点の出力予定:[2, 2, 2]
まだ、2で割り切れるかどうかを確認する。

CP3: 2では、もう割り切れなくなったので、
次は、3で割り切れるかどうかを試す。

CP1: 3で割り切れるかどうか調べる。
CP2: 3で割り切れたから、3を出力する。
この時点の商:15
この時点の出力予定:[2, 2, 2, 3]
まだ、3で割り切れるかどうかを確認する。

CP2: 3で割り切れたから、3を出力する。
この時点の商:5
この時点の出力予定:[2, 2, 2, 3, 3]
まだ、3で割り切れるかどうかを確認する。

CP3: 3では、もう割り切れなくなったので、
次は、4で割り切れるかどうかを試す。

CP4: 5を4で、割ったとしても、商は4より小さくなるので、これ以上は検討不要。
残っている数を、最後の約数として追加する。ただし、1の場合は、無視する。
5を出力して計算終了。

output----
[2, 2, 2, 3, 3, 5]

2019/02/24

MapオブジェクトをObject形式に変換する--- Javascript/ES6


  • Goal
    MapオブジェクトをObject形式に変換する
  • ソース
    const mapToObj = inputMap => {
    const obj = {}
    for (const [key, value] of inputMap) {
    obj[key] = value
    }
    return obj
    }
    const example = new Map([['udon', 500], ['soba', 500], ['ramen', 800], ['mentaipasta', 1000]])
    console.log(JSON.stringify(mapToObj(example)))
    view raw mapToObject.js hosted with ❤ by GitHub



  • 出力結果

  • Thanks!!
    Map and object: converting nested objects
    https://www.xul.fr/javascript/map-and-object.php
    Arrayがネストされているケースの記載もありました。

2019/01/06

round() の四捨五入では上手くいかない! - python3

Python3 では、round()による四捨五入は使えない!!

How: decimal モジュールを使う。
サンプルコード

# -*- coding: utf-8 -*-
#
from decimal import Decimal, ROUND_HALF_UP
i = float(input())
print("入力値")
print(i)
#
print("roundの結果")
ex_round = round(i,1)
print(ex_round)
#
print("decimalの結果")
o = float(Decimal(str(i)).quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))
print(o)

結果

入力値
12.25
roundの結果
12.2
decimalの結果

12.3

2019/01/05

Colaboratory すごい - Colaboratory + Python3 + Pandas

Google の Colaboratory を使ってデータサイエンス、機械学習にチャレンジをしてみる。

Goal:Google Colaboratory を立ち上げて、機械学習用のデータ・セットを読み込みデータをハンドリングする。主に、Pandas を使ってみるという内容。

How
1. Colaboratoryに新しいノートブックを作成する。以下にアクセスして、Python3のノートブックを作成する。

      https://colab.research.google.com/


2. Notebook

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

2019/01/04

買い物: CW-X ジェネレーターモデル

破けたから新調します。

ジェネレーターモデル、差はよくわからないけど、ねじれに強いのでこれ。


2019/01/01

標準入力の取扱 input と print - Python3

Goal:Python3での標準入力をハンドリングする。
#1 複数の値が1行に同時に入力された場合、数値として取り込む
入力値 --- 以下の3つの数値を入力したら、int型として取り込む
100 3 123456

# -*- coding: utf-8 -*-
#1 複数の値が1行に同時に入力された場合、数値として取り込む
# 入力値 --- 以下のように3つの数値が入力されて、int型として取り込む
# 100 3 123456
# input
a, b, c = map(int,input().split())
# output
print('output---')
print('a=%d' % a)
print('b=%d' % b)
print('c=%d' % c)


(py3) toshi$ python python3-input.py
100 3 123456
output---
a=100
b=3
c=123456


#2 入力行数を最初に入力して、その後、複数行に渡る入力値を、配列として取り込む
入力値 --- 以下のように入力したら、3*3の行列として取り込む
3
1 2 3
4 5 6
7 8 9

# -*- coding: utf-8 -*-
#2 入力行数を最初に入力して、その後、複数行に渡る入力値を、配列として取り込む
# 入力値 --- 以下のように入力したら、3*3の行列として取り込む
# 3
# 1 2 3
# 4 5 6
# 7 8 9
# input
n = int(input())
a = []
for i in range(n):
a.append(list(map(int,input().split())))
# output
print('output---')
print(a)


(py3) toshi$ python python3-input2.py
3
1 2 3
4 5 6
7 8 9
output---
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]