class Document:
text = ""
def __str__(self):
return self.text
import abc
class DocumentCommand(abc.ABC):
doc = None
@abc.abstractmethod
def execute(self):
pass
@abc.abstractmethod
def undo(self):
pass
class AddStrCommand(DocumentCommand):
def __init__(self, doc, txt):
self.text = txt
self.doc = doc
def execute(self):
self.doc.text += self.text
return self.doc
def undo(self):
return DeleteStrCommand(self.doc, self.text).execute()
class DeleteStrCommand(DocumentCommand):
def __init__(self, doc, txt):
self.text = txt
self.doc = doc
def execute(self):
idx = self.doc.text.find(self.text)
if idx == -1:
return self.doc
self.doc.text = self.doc.text[:idx]
return self.doc
def undo(self):
return AddStrCommand(self.doc, self.text).execute()
doc = Document()
addingTexts = [AddStrCommand(doc,"hello\n"), AddStrCommand(doc,"My name is Chang\n"), AddStrCommand(doc,"I hope you have a good day")]
for c in addingTexts:
c.execute()
print("#"*5)
print(doc)
print("#"*5)
print(addingTexts[-1].undo())
print("#"*5)
print(addingTexts[-1].undo())
print("#"*5)
print("*"*30)
d = DeleteStrCommand(doc,"My name is Chang\n")
print(d.execute())
print("#"*5)
print(d.undo())
print("#"*5)
All posts by chang
Golang with decoupling S3 Buckets and clients
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
package main
import (
"context"
"fmt"
"strconv"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
panic("configuration error, " + err.Error())
}
client := s3.NewFromConfig(cfg)
// ListBuckets(client, cfg)
// BucketLocation(client, cfg.Region, "dnb-vedi")
// BucketLocation(client, cfg.Region, "ag-glue-shaping-test-code")
// cfg.Region = "us-east-1"
// client = s3.NewFromConfig(cfg)
// ListObjects(client, cfg, "dnb-vedi", "2022-06-10")
// ListObjects(client, cfg, , "")
// if IsObjectExistsInS3Bucket(client, cfg, "ag-glue-shaping-test-code", "experiment-data/top50%_caac_fake_entity_newHash.csv") {
// fmt.Println("%s exists %s", "ag-glue-shaping-test-code", "experiment-data/top50%_caac_fake_entity_newHash.csv")
// } else {
// fmt.Println("%s not exists %s", "ag-glue-shaping-test-code", "experiment-data/top50%_caac_fake_entity_newHash.csv")
// }
// if IsObjectExistsInS3Bucket(client, cfg, "dnb-vedi", "2022-06-10/BEMFAB_FILE1.TXT.gz.pgp") {
// fmt.Println("%s exists %s", "dnb-vedi", "2022-06-10/BEMFAB_FILE1.TXT.gz.pgp")
// } else {
// fmt.Println("%s not exists %s", "dnb-vedi", "2022-06-10/BEMFAB_FILE1.TXT.gz.pgp")
// }
// if IsObjectExistsInS3Bucket(client, cfg, "dnb-vedi", "2022-06-10/") {
// fmt.Println("%s exists %s", "dnb-vedi", "2022-06-10/")
// } else {
// fmt.Println("%s not exists %s", "dnb-vedi", "2022-06-10")
// }
items, err := GetObjects(client, cfg, "dnb-vedi", "2022-06-10")
if err != nil {
panic("configuration error, " + err.Error())
}
for _, item := range items {
for k, v := range item {
fmt.Printf("%s: %s ", k, v)
}
fmt.Println()
}
}
func IsObjectExistsInS3Bucket(client *s3.Client, cfg aws.Config, bucket, objectKey string) bool {
region, _ := GetBucketLocation(client, cfg, bucket)
cfg.Region = region
client = s3.NewFromConfig(cfg)
headObj := s3.HeadObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(objectKey),
}
_, err := client.HeadObject(context.TODO(), &headObj)
return err == nil
}
func GetObjects(client *s3.Client, cfg aws.Config, bucket, prefix string) ([]map[string]string, error) {
region, _ := GetBucketLocation(client, cfg, bucket)
cfg.Region = region
// fmt.Println(cfg.Region)
client = s3.NewFromConfig(cfg)
params := &s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
}
resp, err := client.ListObjectsV2(context.Background(), params)
if err != nil {
fmt.Println("Got error retrieving list of objects:")
return nil, err
}
var returnArray []map[string]string
for _, item := range resp.Contents {
var obj = map[string]string{}
obj["path"] = string(*item.Key)
obj["lastmodified"] = (*item.LastModified).Format("2006-01-02T15:04:05")
obj["size"] = strconv.FormatInt(item.Size, 10)
obj["storageClass"] = string(item.StorageClass)
returnArray = append(returnArray, obj)
}
return returnArray, nil
}
func ListObjects(client *s3.Client, cfg aws.Config, bucket, prefix string) {
region, _ := GetBucketLocation(client, cfg, bucket)
cfg.Region = region
// fmt.Println(cfg.Region)
client = s3.NewFromConfig(cfg)
params := &s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
}
resp, err := client.ListObjectsV2(context.Background(), params)
if err != nil {
fmt.Println("Got error retrieving list of objects:")
fmt.Println(err)
return
}
for _, item := range resp.Contents {
fmt.Println("Name: ", *item.Key)
fmt.Println("Last modified: ", *item.LastModified)
fmt.Println("Size: ", item.Size)
fmt.Println("Storage class: ", item.StorageClass)
fmt.Println("")
}
}
func ListBuckets(client *s3.Client, cfg aws.Config) {
param := &s3.ListBucketsInput{}
result, err := client.ListBuckets(context.TODO(), param)
if err != nil {
fmt.Println("Got an error retrieving buckets:")
fmt.Println(err)
return
}
fmt.Println("Buckets:")
for _, bucket := range result.Buckets {
fmt.Print(*bucket.Name + ": " + bucket.CreationDate.Format("2006-01-02 15:04:05 Monday") + ": ")
re, _ := GetBucketLocation(client, cfg, *bucket.Name)
fmt.Println(re)
}
}
func GetBucketLocation(client *s3.Client, cfg aws.Config, bucket string) (string, error) {
param := &s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
}
locationOutput, err := client.GetBucketLocation(context.Background(), param)
if err != nil {
return "", err
}
// fmt.Println(locationOutput.LocationConstraint)
if locationOutput.LocationConstraint == "" {
return cfg.Region, nil
} else {
return string(locationOutput.LocationConstraint), nil
}
}
Mac Terminal Setup
SetUp Mac OS Terminal
1. Install brew
If you do not have Xcode installed
If you have Xcode installed
Brew comes with git
git version 2.32.0 (Apple Git-132)
2. Install Sublime
3. Install Vim
append the following line to ~/.vimrc
4. Install iTerm 2
5. Check installation of Zsh
6. Better Mac Terminal Window
Based on: https://opensource.com/article/20/8/iterm2-zsh
Install Oh My Zsh
Oh My Zsh uses a .zshrc file to save your customizations instead of a .bash_profile
1.
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
2. Add the following in .zshrc
plugins=( git github zsh-syntax-highlighting zsh-autosuggestions bundler dotenv macos
python pip pyenv virtualenv aws brew docker golang helm
sudo tmux
vi-mode vim-interaction
)
source /usr/local/opt/powerlevel10k/powerlevel10k.zsh-theme
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
#ZSH_THEME="powerlevel10k/powerlevel10k"
ZSH_THEME="robbyrussell"
#ZSH_THEME="agnoster"
3. Install the recommended font
4. In order to re-configfure
5. Recommended plugins
6. Verification of on-my-zsh
Check 'oh-my-zsh' folder
.oh-my-zsh
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE.txt
├── README.md
├── SECURITY.md
├── cache
├── custom
├── lib
├── log
├── oh-my-zsh.sh
├── plugins
├── templates
├── themes
└── tools
Generating an SSH key
Install Python3
AWS setup
Install the AWS CLI using GUI installer
https://docs.aws.amazon.com/cli/latest/userguide/installing.html
Install the awscli tool via python pip:
pip3 install awscli --upgrade --user
Install the aws-iam-authenticator application
https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html
Install kubectl
https://kubernetes.io/docs/tasks/tools/install-kubectl/
Install eksctl (admin only)
https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html
References
https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html
https://github.com/weaveworks/eksctl
https://eksctl.io/
AWS
AWS Access Key ID [None]: XXXX
AWS Secret Access Key [None]: XXXXXXXXX
Default region name [None]: us-west-2
Default output format [None]: json