Skip to content

Instantly share code, notes, and snippets.

View gwsu2008's full-sized avatar

Guang gwsu2008

View GitHub Profile
@gwsu2008
gwsu2008 / docker
Created January 12, 2020 02:04
docker
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04
@gwsu2008
gwsu2008 / resign-apk.sh
Created January 3, 2020 06:48
Android APK decompile and update properties
#!/bin/bash +x
info() {
echo -e "\033[34m[INFO]\033[0m $1"
}
error() {
echo -e "\033[31m[ERROR] $1\033[0m"
}
success() {
@gwsu2008
gwsu2008 / python-abstract-class.py
Created January 3, 2020 05:34
Python - Abstract Class
"""
Python - Abstract Class
Yes. Python has Abstract Base Class module allows to enforce that a derived class implements a particular method using a special @abstractmethod decorator on that method.
"""
from abc import ABCMeta, abstractmethod
class Animal:
__metaclass__ = ABCMeta
@abstractmethod
@gwsu2008
gwsu2008 / python-search-content.py
Last active January 3, 2020 05:34
Python search content in files
# Build lists of matched lines - several flavors:
def lines_that_equal(line_to_match, fp):
return [line for line in fp if line == line_to_match]
def lines_that_contain(string, fp):
return [line for line in fp if string in line]
def lines_that_start_with(string, fp):
return [line for line in fp if line.startswith(string)]
@gwsu2008
gwsu2008 / python-boto3client-exception.py
Last active January 3, 2020 05:35
Python boto3 client exception
"""
===========================================
['Error']['Code'] e.g. 'EntityAlreadyExists' or 'ValidationError'
['ResponseMetadata']['HTTPStatusCode'] e.g. 400
['ResponseMetadata']['RequestId'] e.g. 'd2b06652-88d7-11e5-99d0-812348583a35'
['Error']['Message'] e.g. "An error occurred (EntityAlreadyExists) ..."
['Error']['Type'] e.g. 'Sender'
""""
import boto3
from botocore.exceptions import ClientError
@gwsu2008
gwsu2008 / python-boto3-s3-upload.py
Last active January 3, 2020 06:03
python-boto3-s3-upload.py
import boto3
client = boto3.client('s3', 'us-west-2')
transfer = boto3.s3.transfer.S3Transfer(client=client)
transfer.upload_file(file_name, bucket, key_name, extra_args={'ServerSideEncryption':'aws:kms', 'SSEKMSKeyId':'alias/aws/s3'})
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file(file_name, bucket, key_name, ExtraArgs={'ServerSideEncryption':'aws:kms','SSEKMSKeyId':'alias/aws/s3'})
@gwsu2008
gwsu2008 / python-stream-file-s3.py
Last active January 3, 2020 06:06
python-stream-file-s3.py
import json
import os
import requests
import boto3
from datetime import datetime
from botocore.exceptions import ClientError
import io
import contextlib
import urllib3
@gwsu2008
gwsu2008 / python-zip-file.py
Created January 3, 2020 05:23
python-zip-file.py
def zipDir(dirPath, zipPath):
"""
Generate zip package with full path
"""
zipf = zipfile.ZipFile(zipPath , mode='w')
lenDirPath = len(dirPath)
for root, _ , files in os.walk(dirPath):
for file in files:
if re.search('setup.cfg', file):
logger.info('Skip file {}'.format(file))
@gwsu2008
gwsu2008 / python-s3-upload-download.py
Last active January 3, 2020 06:10
Python S3 upload / download gz file
from io import BytesIO
import gzip
import shutil
def upload_gzipped(bucket, key, fp, compressed_fp=None, content_type='text/plain'):
"""
Compress and upload the contents from fp to S3. If compressed_fp is None, the compression is performed in memory.
"""
if not compressed_fp:
compressed_fp = BytesIO()
@gwsu2008
gwsu2008 / python-url-encoding.py
Last active January 3, 2020 05:38
Python URL encoding
#urllib3
import urllib.parse
test_string = 'refs/heads/feature/Hotfix-test-123-214'
print(urllib.parse.quote_plus(test_string))