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