Monday, February 26, 2024

Python 練習

 

Shopping Cart Module

Functional Requirements

You are to prototype business logic for an online shopping cart. Eventually, the user will enter information into an HTML form and hit "Submit". This will trigger Python logic processing the order. The results of the order will be converted to JSON for storage in a NoSQL database. 

For this current assignment, you can ignore the presentation layer (the HTML form). Instead, prototype the logic by letting the user (who will be a tester on your team), respond to command line prompts and enter data like "product id", "product name", "price", "customer id", etc. Use at least four data fields - more if you are feeling ambitious!

Likewise, ignore the part of the data layer that involves implementing the actual NoSQL database. For now, use .json files as your data storage mechanism. When you create the .json files, be sure to use some key field like order_number in the file name so you can retrieve each order from your file system. To finish the project, write a some code to search for orders by order number, read the JSON from the selected file, and present complete information from the order on the Python command line. 

ユーザー入力DataをJSON 形式で保存・追加:

import os
import json
input_id = input("Enter order #: ")
input_product_no = input("Enter product #: ")
input_productname = input("Enter product name: ")
input_price = input("Enter price: ")
with open('store_jsonfile.json',
    mode="r",
    encoding="utf-8") as file:
    orders = json.load(file)
    #print(orders)
    orders[input_id] = {"product_sku": input_product_no, "productname": input_productname, "price": input_price}
    #print(orders)
with open('store_jsonfile.json',
    mode="w",
    encoding="utf-8") as file:
    json.dump(orders, file, indent=4)

検索して表示:

import os
import json
input_id = input("Enter order #: ")
with open('store_jsonfile.json', mode="r", encoding="utf-8") as file:
    orders = json.load(file)
    
    order = orders[input_id]
    print("Order #: ", input_id)
    print("Product SKU: ", order['product_sku']) 
    print("Product Name: ", order['productname'])
    print("Price: ", order['price'])


Sunday, February 25, 2024

Python Collection

 Python Collection

Python CollectionsOrdered/IndexedChangeableAllows Duplicates
Setnono(追加・削除は可能)no
Tupleyesnoyes
Dictionaryyesyesno
Listyesyesyes

Sets

Python sets are unchangeable and unordered. They do not allow duplicates. (See the chart below to see how sets compare to other collections).

What the term "unchangeable" means in the context of Python sets is that the elements themselves cannot be changeable.  The string "blue" cannot be changed. Variations like "Blue", or "bleu", or "skyblue" are just different strings, not changes to "blue". So you can make sets out of unchangeable data types like strings, integers, and booleans. 


Tuples

Tuples really are unchangeable. There are no tuple.add() or tuble.remove() methods. If you want to change a tuple, you will need a hack that migrates the tuple into something changeable and then migrates back to the tuple with new and improved membership:


Dictionaries

Python  dictionaries work very well with JSON. Consider the two code samples below. Which is Python? Which is JSON?


Lists

Lists are the closest feature Python has to what other languages call arrays. You can declare a Python list like this:

Thursday, February 1, 2024

EC2上にDockerをインストールしようと Django


EC2上にDockerをインストールしようとしてたが途中 Django を入れたところで挫折。。。

まず、EC2 インスタンス作成(過去の投稿:EC2 インスタンス起動メモ

/home/ec2-user/tutorial/todobackend/src/manage.py

sudo yum update -y && \ sudo yum install -y docker && \ sudo usermod -a -G docker ec2-user && \ sudo systemctl start docker.service && \ sudo systemctl enable docker.service && \ sudo curl -L https://github.com/docker/compose/release/download/1.21.0/cokder-compose-$(shiro-nsc -s)-$(shiro-nsc -m) -o /usr/local/bin/docker-compose && \ sudo chmod +x /usr/local/bin/docker-compose && \ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose && \ sudo yum install -y python3-devel gcc jq tree git && \ git clone https://github.com/AWSCLOUDTECH/tutorial.git && \ cd tutorial/todobackend && \ cd src && \ sudo yum install -y pip pip3 install wheel uwsgi && \ pip3 install -r requirements.txt --user && \ python3 manage.py migrate && \ python3 manage.py runserver 0.0.0.0:8000






Development Server がポート8000で立ち上がっている。




作成したインスタンスにポート8000でアクセスするとDjango のテストページが表示される


現状(まだ Docker を使っていない):




/home/ec2-user/tutorial/todobackend

Wednesday, January 31, 2024

EC2 に Flask をインストール

 

EC2上にDockerをインストールする

まず、EC2 インスタンス作成(過去の投稿:EC2 インスタンス起動メモ

Amazon Linux 2023

sudo yum -y update

sudo yum -y install python3


sudo yum install python3-pip


pip3 install flask


cd /opt sudo mkdir flask sudo chown -R ec2-user:ec2-user flask cd flask mkdir project cd project


app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
  return "世界のみなさん、こんにちは"




flask run



でも、なぜか動かない、、、下記で動いた。


理由はよくわからない。。。






Sunday, January 7, 2024

AWS S3

S3 Bucket を使って静的 Web サイトを表示:

S3 は 5GB まで無料枠で使えるようなので(そんなに使わないけど)、スタティックなサイトであれば S3 で十分と、HTML ファイルを置いて公開してみた。

S3 サービスを選択:

Create bucket:

上記の Bucket name を DNS のサブドメイン名と合しておかないと下記のような 404 Not Found になった、、、ので、今回、Bucket name を アサインする予定のと同じにしておいた。

Created:

Create access point:

Property:

Static website hosting:

Enable:

Permissions -> Bucket policy:

Upload some test files:

Uploaded:

S3 Bucket の Static website hosting address:

DNS レコードにエイリアス(CNAME)を設定:


Thursday, January 4, 2024

Visual Studio Code のインストール

もう 30 年前だろうか、、、学生の時、Visual Studio C++ とか Borland C++ とか、高価だったけど買わなくてはクラス取れないし、生活費を削って無理して買った記憶がありますが、今は Visual Studio Code という簡易版?の IDE が無償利用できるらしいのでインストールしてみた。


インストール後(手順は省略)、作業フォルダー指定:

新規ファイル作成:

Old School の自分だと NotePad や NotePad++ で十分のような気もしますが、ショートカットとか充実しているそうなので、これを HTML エディタとして使っていこうと思います。

ショートカット例(Shift + !):

追記:VSCode で EC2 インスタンスにアクセスする(ターミナル)

PuTTY でアクセスしていたが、VSCodeをターミナルとしてアクセスできる便利エクステンションがあるので利用。




























Wednesday, January 3, 2024