Create / search / create table with PynamoDB

Thing you want to do

--Touch DynamoDB from MAC --Table operations with pynamoDB

Prerequisites

--AWS Access Key ID and AWS Secret Access Key have been GET

Let's go!

Touch DynamoDB from MAC

Install AWS CLI version 2 from the official website

  1. In your browser, download the macOS pkg file: https://awscli.amazonaws.com/AWSCLIV2.pkg
  2. Double-click the downloaded file to launch the installer.
  3. Follow the on-screen instructions. You can install AWS CLI version 2 in the following ways: (reference) https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/install-cliv2-mac.html You can now touch AWS with commands.

Works with AWS with aws configure

(venv) mbp:wanted user$ aws configure
AWS Access Key ID [None]: ABCDEFGHIJGLMNOPQRSTUVWXYZ
AWS Secret Access Key [None]: SECRETKEYSECRETKEYSECRETKEYSECRETKEY
Default region name [None]: ap-northeast-1
Default output format [None]: 

Interactively, enter the access key and secret key to complete the cooperation

Creating a table

from datetime import datetime

from pynamodb.attributes import UnicodeAttribute, UTCDateTimeAttribute, MapAttribute, NumberAttribute
from pynamodb.models import Model


class AccountMap(MapAttribute):
    """
account information
    """
    price = NumberAttribute(null=True)
    search_limit_num = NumberAttribute(null=True)
    url = UnicodeAttribute(null=True)


class Users(Model):
    """
User information
    """

    class Meta:
        table_name = "Users"
        region = 'ap-northeast-1'

    id = UnicodeAttribute(hash_key=True)
    password = UnicodeAttribute()
    login_date = UTCDateTimeAttribute()
    start_date = UTCDateTimeAttribute(default=datetime.now())
    account = AccountMap()


#Creating a user table
if not Users.exists():
    Users.create_table(read_capacity_units=1, write_capacity_units=1, wait=True)

You can make it like this. Django feeling, odd ^ ^

Search

query Specify the key and then search

def get_user_info(user_id, password, month_password):
    """
Password authentication
    :param user_id:
    :param password:
    :param month_password:
    :return:
    """
    #Get TODO token
    token = "local"

    #User ID and password and this month's password and token
    for user_data in Users.query(user_id, (Users.password == password) & (Users.month_password == month_password) & (
            Users.token == token)):
        return user_data

    return None

Search with query

scan Search without specifying a key

Users.scan(Users.id==user_id)

Save

    #Data creation
    users = Users('onehundred')
    users.password = "password"
    users.token = "local"
    users.month_password = "month_password"
    users.login_date = datetime.now()
    users.start_date = datetime.now()
    users.account = {
        "price": 1000,
        "search_limit_num": 10,
        "pay_pal_url": "https://paypal.com",
    }
    users.save()

Recommended Posts

Create / search / create table with PynamoDB
Create a table of contents with IPython notebook
Sequential search with Python
Binary search with python
Create games with Pygame
Binary search with Python3
Create filter with scipy
Database search with db.py
I tried to create a table only with Django
Why not create a stylish table easily with Python?
Get table dynamically with sqlalchemy
Create Cloud TPU with tf-nightly
Create an API with Django
Create 3d gif with python3
Bit full search with Go
Create graph with plotly button
Create a homepage with django
Create Image Viewer with Tkinter
Full bit search with Python
Create custom rules with ElastAlert
Create patent map with Streamlit
Create a heatmap with pyqtgraph
I can't search with # google-map. ..
Search engine work with python
Search twitter tweets with python
Create a directory with python
Create xlsx file with XlsxWriter
Table scraping with Beautiful Soup
Streamline web search with python
Create plot animation with Python + Matplotlib
Grid search of hyperparameters with Scikit-learn
Create Awaitable with Python / C API
Create screen scroll with Pythonista + scene
[AWS] Create API with API Gateway + Lambda
Create folders from '01' to '12' with python
Learn search with Python # 2bit search, permutation search
Create a virtual environment with Python!
Create an Excel file with Python3
First OSMnx ~ With shortest path search ~
Create LGTM images with GIMP! (Python-fu)
Dynamically create new dataframes with pandas
Create noise-filled audio data with SoX
Create basic algorithms with Smart Trade
Search for homeomorphic idioms with opencv
Create API using hug with mod_wsgi
Create an age group with pandas
Generate fake table data with GAN
Create Splunk custom search command Part 2
Create a poisson stepper with numpy.random
Create github pages with lektor Part 1
Twitter search client made with bottle
Create a file uploader with Django