Skip to content

Instantly share code, notes, and snippets.

View gwsu2008's full-sized avatar

Guang gwsu2008

View GitHub Profile
@gwsu2008
gwsu2008 / changeAwsKeysSecretText.groovy
Created January 3, 2020 04:52
changeAwsKeysSecretText.groovy
import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.*
import org.jenkinsci.plugins.plaincredentials.*
import org.jenkinsci.plugins.plaincredentials.impl.*
import hudson.util.Secret
def changeAwsKeys = { cred_id, access_key, secret_key ->
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
@gwsu2008
gwsu2008 / python-read-file-content.py
Last active January 3, 2020 05:36
Python read files into array list stripping line break
with open('myfile.txt') as f:
lines = [ line.rstrip() for line in f ]
score = np.array([70, 60, 50, 10, 90, 40, 80])
name = np.array(['Ada', 'Ben', 'Charlie', 'Danny', 'Eden', 'Fanny', 'George'])
sorted_name = name[np.argsort(score)] # an array of names in ascending order of their scores
print(sorted_name) # ['Danny' 'Fanny' 'Charlie' 'Ben' 'Ada' 'George' 'Eden']
original_name = sorted_name[np.argsort(np.argsort(score))]
print(original_name) # ['Ada' 'Ben' 'Charlie' 'Danny' 'Eden' 'Fanny' 'George']
%timeit name[np.argsort(score)]
@gwsu2008
gwsu2008 / numpy-ma.py
Created December 30, 2019 05:17 — forked from edenau/numpy-ma.py
import math
def is_prime(n):
assert n > 1, 'Input must be larger than 1'
if n % 2 == 0 and n > 2:
return False
return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
arr = np.array(range(2,100))
non_prime_mask = [not is_prime(n) for n in a]
prime_arr = np.ma.MaskedArray(data=arr, mask=non_prime_mask)
arr = np.array(range(1000)).reshape(2,5,2,10,-1)
print(arr[:,:,:,3,2] == arr[...,3,2])
# [[[ True, True],
# [ True, True],
# [ True, True],
# [ True, True],
# [ True, True]],
# [[ True, True],
# [ True, True],
# [ True, True],
@gwsu2008
gwsu2008 / python-site-package-path
Created December 26, 2019 06:48
python-site-package-path
python3 -m site
@gwsu2008
gwsu2008 / python-staticmethod.py
Last active January 3, 2020 05:37
Python sample staticmethod in class
# app.py
# Defining a class named Sum
class Sum:
# defining a function sumoftwo
# this function will be called in a static method
# Creating static method here only
@staticmethod
def sumoftwo(a, b):
@gwsu2008
gwsu2008 / MacOS-clear-dns-cache
Created December 24, 2019 17:27
MacOS-clear-dns-cache
#!/bin/bash +x
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
@gwsu2008
gwsu2008 / ssh port forwarding
Created December 16, 2019 17:56
SSH port forwarding
Host $NAME
Hostname $EC2_IP
User $EC2_USERNAME
ForwardAgent yes
ProxyCommand ssh <internal ip> nc %h %p 2> /dev/null
IdentityFile $LOCATION_OF_AWS_PEM_KEY
access jenkins
ssh -f <internal ip> -L 8443:<external ip>:443 -N
@gwsu2008
gwsu2008 / replace.py
Created November 25, 2019 00:22 — forked from carlsmith/replace.py
A Python function that does multiple string replace ops in a single pass.
import re
def replace(string, substitutions):
substrings = sorted(substitutions, key=len, reverse=True)
regex = re.compile('|'.join(map(re.escape, substrings)))
return regex.sub(lambda match: substitutions[match.group(0)], string)