├── README.md ├── aws ├── aws-policies │ └── iam │ │ ├── allow_user_change_passwd.json │ │ ├── s3-bucket_enforce_server_side_encryption.json │ │ └── s3_allow_ses_to_write_to_s3.json ├── bash │ ├── rds.md │ └── ssm.md └── python │ ├── dynamodb │ ├── batch_get_item_with_limit.py │ └── create_and_tag_table.py │ ├── kms │ └── encrypt_decrypt.py │ ├── rds │ ├── describe_rds_instance.py │ ├── list_rds_instances.py │ └── list_rds_tags_per_db.py │ └── s3 │ └── put_object.py ├── concourse ├── 01-basic-task-ubuntu.yml ├── 02-inputs-upload-list.yml └── 03-task-scripts.yml ├── elasticsearch └── python │ ├── es_bulk_ingest.py │ ├── es_index_document.py │ └── es_show_info_no_auth.py ├── ethereum └── python │ └── create_address.py ├── golang ├── basics │ ├── readfile_into_memory.go │ ├── sleep.go │ └── while.go └── robotgo │ └── get_cursor_coordinates.go ├── mongodb ├── golang │ └── examples.go └── python │ └── insert-document-mongodb.py ├── mysql └── python │ ├── batch-executemany-writes.py │ ├── insert-batch-generated-mysql.py │ └── read-mysql-data.py ├── python-flask ├── README.md └── api │ └── flask-api.py ├── ruby └── basics │ ├── for_loop.rb │ ├── if_statement_int.rb │ └── if_statement_str.rb └── sqlite └── python ├── random-write_to_sqlite.py ├── read_from_sqlite.py └── read_from_sqlite_groupby.py /README.md: -------------------------------------------------------------------------------- 1 | # code-examples 2 | 3 | This repository is for example code that I write and for referencing: 4 | 5 | ### MongoDB 6 | - [MongoDB: Python](mongodb/python) 7 | 8 | ### MySQL 9 | - [MySQL: Python](mysql/python) 10 | -------------------------------------------------------------------------------- /aws/aws-policies/iam/allow_user_change_passwd.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Action": [ 7 | "iam:ChangePassword" 8 | ], 9 | "Resource": [ 10 | "arn:aws:iam::*:user/${aws:username}" 11 | ] 12 | }, 13 | { 14 | "Effect": "Allow", 15 | "Action": [ 16 | "iam:GetAccountPasswordPolicy" 17 | ], 18 | "Resource": "*" 19 | } 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /aws/aws-policies/iam/s3-bucket_enforce_server_side_encryption.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Sid": "EnforceEncryption", 6 | "Effect": "Deny", 7 | "Principal": "*", 8 | "Action": "s3:PutObject", 9 | "Resource": "arn:aws:s3:::my-secure-bucket/*", 10 | "Condition": { 11 | "StringNotEquals": { 12 | "s3:x-amz-server-side-encryption": "AES256" 13 | } 14 | } 15 | } 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /aws/aws-policies/iam/s3_allow_ses_to_write_to_s3.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Sid": "AllowSESPutsToS3", 6 | "Effect": "Allow", 7 | "Principal": { 8 | "Service": "ses.amazonaws.com" 9 | }, 10 | "Action": "s3:PutObject", 11 | "Resource": "arn:aws:s3:::bucket-name/path/to/drop/*", 12 | "Condition": { 13 | "StringEquals": { 14 | "aws:Referer": "123456789012" 15 | } 16 | } 17 | } 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /aws/bash/rds.md: -------------------------------------------------------------------------------- 1 | 2 | ## Describe All RDS DB Instances: 3 | 4 | ``` 5 | $ aws --profile prod rds describe-db-instances --query 'DBInstances[*].[DBInstanceArn,DBInstanceIdentifier,DBInstanceClass,Endpoint]' 6 | ``` 7 | 8 | ## Describe a RDS DB Instance with a dbname: 9 | 10 | ``` 11 | $ aws --profile prod rds describe-db-instances --query 'DBInstances[?DBInstanceIdentifier==`db-staging`].[DBInstanceArn,DBInstanceIdentifier,DBInstanceClass,Endpoint]' 12 | [ 13 | [ 14 | "arn:aws:rds:eu-west-1::db:db-staging", 15 | "db-staging", 16 | "db.t2.micro", 17 | { 18 | "HostedZoneId": "ASKDJSAKDJBA", 19 | "Port": 5432, 20 | "Address": "db-staging.asdkjahsd.eu-west-1.rds.amazonaws.com" 21 | } 22 | ] 23 | ] 24 | ``` 25 | 26 | ## List all RDS DB Instances and limit output: 27 | 28 | ``` 29 | $ aws --profile prod rds describe-db-instances --query 'DBInstances[*].[DBInstanceArn,DBInstanceIdentifier,DBInstanceClass,Endpoint]' 30 | [ 31 | [ 32 | "arn:aws:rds:eu-west-1::db:db-name", 33 | "db-name", 34 | "db.t2.micro", 35 | { 36 | "HostedZoneId": "ABCDEFGHILKL", 37 | "Port": 5432, 38 | "Address": "db-name.abcdefg.eu-west-1.rds.amazonaws.com" 39 | } 40 | ], 41 | ``` 42 | 43 | ## List all RDS DB Instances that has backups enabled, and limit output: 44 | 45 | ``` 46 | $ aws --profile prod rds describe-db-instances --query 'DBInstances[?BackupRetentionPeriod>`0`].[DBInstanceArn,DBInstanceIdentifier,DBInstanceClass,Endpoint]' 47 | [ 48 | [ 49 | "arn:aws:rds:eu-west-1::db:db-name", 50 | "db-name", 51 | "db.t2.micro", 52 | { 53 | "HostedZoneId": "ABCDEFGHILKL", 54 | "Port": 5432, 55 | "Address": "db-name.abcdefg.eu-west-1.rds.amazonaws.com" 56 | } 57 | ], 58 | ``` 59 | 60 | ## Describe DB Snapshots for DB Instance Name: 61 | 62 | ``` 63 | $ aws --profile prod rds describe-db-snapshots --db-instance-identifier db --query 'DBSnapshots[?DBInstanceIdentifier==`db`].[DBInstanceIdentifier,DBSnapshotIdentifier,SnapshotCreateTime,Status]' 64 | [ 65 | [ 66 | "db", 67 | "rds:db-2018-05-16-04-08", 68 | "2018-05-16T04:08:53.696Z", 69 | "available" 70 | ], 71 | ``` 72 | 73 | ## Events for the last 24 Hours: 74 | 75 | ``` 76 | $ aws --profile prod rds describe-events --source-identifier "rds:db-2018-05-16-04-08" --source-type db-snapshot --duration 1440 --query 'Events[*]' 77 | [ 78 | { 79 | "EventCategories": [ 80 | "creation" 81 | ], 82 | "SourceType": "db-snapshot", 83 | "SourceArn": "arn:aws:rds:eu-west-1::snapshot:rds:db-2018-05-16-04-08", 84 | "Date": "2018-05-16T04:08:40.264Z", 85 | "Message": "Creating automated snapshot", 86 | "SourceIdentifier": "rds:db-2018-05-16-04-08" 87 | }, 88 | { 89 | "EventCategories": [ 90 | "creation" 91 | ], 92 | "SourceType": "db-snapshot", 93 | "SourceArn": "arn:aws:rds:eu-west-1::snapshot:rds:db-2018-05-16-04-08", 94 | "Date": "2018-05-16T04:32:04.047Z", 95 | "Message": "Automated snapshot created", 96 | "SourceIdentifier": "rds:db-2018-05-16-04-08" 97 | } 98 | ] 99 | ``` 100 | ## List Public RDS Instances: 101 | 102 | ``` 103 | $ aws --profile prod rds describe-db-instances --query 'DBInstances[?PubliclyAccessible==`true`].[DBInstanceIdentifier,Endpoint.Address]' 104 | 105 | [ 106 | [ 107 | "name", 108 | "name.abcdef.eu-west-1.rds.amazonaws.com" 109 | ] 110 | ] 111 | ``` 112 | -------------------------------------------------------------------------------- /aws/bash/ssm.md: -------------------------------------------------------------------------------- 1 | # AWS SSM 2 | 3 | ## Get Parameters By Path: 4 | 5 | ``` 6 | $ aws ssm get-parameters-by-path --path '/global/devops/app01/' | jq '.Parameters[]' | jq -r '.Name' 7 | /team/devops/app01/fqdn-hostname 8 | /team/devops/app01/app-username 9 | ``` 10 | 11 | ## Get Paramaters: 12 | 13 | ``` 14 | $ aws ssm get-parameters --names '/team/devops/app01/fqdn-hostname' --with-decryption | jq -r '.Parameters[]' | jq -r '.Value' 15 | app01.domain.com 16 | ``` 17 | 18 | ## Describe Parameters: 19 | 20 | ``` 21 | $ aws prod ssm describe-parameters --filters "Key=Name,Values='/team/devops/app01/fqdn-hostname'" 22 | { 23 | "Parameters": [ 24 | { 25 | "KeyId": "alias/devops-app01-prod-ssm", 26 | "Name": "/team/devops/app01/fqdn-hostname", 27 | "LastModifiedDate": 1520853940.496, 28 | "Version": 1, 29 | "LastModifiedUser": "arn:aws:iam::0123456789012:user/ruanb", 30 | "Type": "SecureString" 31 | } 32 | ] 33 | } 34 | ``` 35 | -------------------------------------------------------------------------------- /aws/python/dynamodb/batch_get_item_with_limit.py: -------------------------------------------------------------------------------- 1 | # Duplicated HashKey to the metadata table and duplicated timestamp attribute 2 | # Scan and Filter metadata table, get HK's 3 | # Execute BatchGet on the Base Table 4 | 5 | import boto3 6 | import time 7 | from boto3.dynamodb.conditions import Key 8 | 9 | base_table = 'test_base' 10 | meta_table = 'test_20181029' 11 | 12 | session = boto3.Session(region_name='eu-west-1', profile_name='test') 13 | resource = session.resource('dynamodb') 14 | table = resource.Table(meta_table) 15 | filtering_expression = Key('timestamp').gt(1540841144) 16 | 17 | response = table.scan(FilterExpression=filtering_expression, Limit=100) 18 | 19 | all_items = [] 20 | finished=False 21 | while finished != True: 22 | if 'LastEvaluatedKey' in response.keys(): 23 | print("Getting {} Items".format(response['Count'])) 24 | items = resource.batch_get_item(RequestItems={base_table: {'Keys': response['Items']}}) 25 | #print(items['Responses'][base_table]) 26 | all_items += items['Responses'][base_table] 27 | time.sleep(2) 28 | response = table.scan(FilterExpression=filtering_expression, Limit=100, ExclusiveStartKey=response['LastEvaluatedKey']) 29 | else: 30 | print("Getting {} Items".format(response['Count'])) 31 | items = resource.batch_get_item(RequestItems={base_table: {'Keys': response['Items']}}) 32 | #print(items['Responses'][base_table]) 33 | all_items += items['Responses'][base_table] 34 | finished=True 35 | 36 | print(len(all_items)) 37 | # Output: 38 | # Getting 100 Items 39 | # Getting 100 Items 40 | # Getting 54 Items 41 | # 254 42 | -------------------------------------------------------------------------------- /aws/python/dynamodb/create_and_tag_table.py: -------------------------------------------------------------------------------- 1 | # Creates a DynamoDB Table: test_20181030 and Tags it 2 | 3 | import boto3 4 | import time 5 | 6 | session = boto3.Session(region_name='eu-west-1', profile_name='test') 7 | resource = session.resource('dynamodb') 8 | client = session.client('dynamodb') 9 | 10 | def create_table(): 11 | table_name = "test_{0}".format(time.strftime("%Y%m%d")) 12 | response = resource.create_table( 13 | TableName=table_name, 14 | KeySchema=[{ 15 | 'AttributeName': 'uuid', 16 | 'KeyType': 'HASH' 17 | }], 18 | AttributeDefinitions=[{ 19 | 'AttributeName': 'uuid', 20 | 'AttributeType': 'S' 21 | }], 22 | ProvisionedThroughput={ 23 | 'ReadCapacityUnits': 1, 24 | 'WriteCapacityUnits': 1 25 | } 26 | ) 27 | 28 | resource.Table(table_name).wait_until_exists() 29 | 30 | arn = client.describe_table(TableName=table_name)['Table']['TableArn'] 31 | client.tag_resource( 32 | ResourceArn=arn, 33 | Tags=[ 34 | {'Key': 'Name','Value': 'yes'}, 35 | {'Key': 'Environment','Value': 'yes'}, 36 | {'Key': 'Owner','Value': 'yes'} 37 | ] 38 | ) 39 | 40 | return resource.Table(table_name).table_status 41 | -------------------------------------------------------------------------------- /aws/python/kms/encrypt_decrypt.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | session = boto3.Session(region_name='us-east-1', profile_name='default') 3 | kms = session.client('kms') 4 | 5 | def encrypt(plaintext): 6 | ciphertext = kms.encrypt(KeyId='alias/mykey', Plaintext=plaintext) 7 | encoded_ciphertext = base64.b64encode(ciphertext["CiphertextBlob"]) 8 | return encoded_ciphertext.decode('utf-8') 9 | 10 | def decrypt(encoded_ciphertext): 11 | decoded_ciphertext = base64.b64decode(encoded_ciphertext) 12 | plaintext = kms.decrypt(CiphertextBlob=bytes(decoded_ciphertext)) 13 | return plaintext['Plaintext'].decode('utf-8') 14 | 15 | """ 16 | >>> a = encrypt('hello') 17 | >>> a 18 | 'AQICAHgQYMmngPUi9lcJeng2A12tVdu[shortened]2XY1wT3t1zreJg2KEF8vZmYykJBc8g==' 19 | 20 | >>> b = decrypt(a) 21 | >>> b 22 | 'hello' 23 | """ 24 | -------------------------------------------------------------------------------- /aws/python/rds/describe_rds_instance.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | session = boto3.Session( 4 | region_name='eu-west-1', 5 | profile_name='test' 6 | ) 7 | 8 | rds = session.client('rds') 9 | response = rds.describe_db_instances(DBInstanceIdentifier='MyRDS_DB_Name') 10 | 11 | print(response) 12 | -------------------------------------------------------------------------------- /aws/python/rds/list_rds_instances.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | session = boto3.Session(region_name='eu-west-1', profile_name='test') 4 | rds = session.client('rds') 5 | 6 | response = rds.describe_db_instances() 7 | 8 | for rds_dbs in response['DBInstances']: 9 | print(rds_dbs['DBInstanceIdentifier']) 10 | -------------------------------------------------------------------------------- /aws/python/rds/list_rds_tags_per_db.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | session = boto3.Session( 4 | region_name='eu-west-1', 5 | profile_name='test' 6 | ) 7 | 8 | missing_tag_value = 'N/A' 9 | rds = session.client('rds') 10 | response = rds.describe_db_instances() 11 | rds_resource_arns = [] 12 | 13 | for rds_db_id in response['DBInstances']: 14 | rds_resource_arns.append(rds_db_id['DBInstanceArn']) 15 | 16 | def get_resource_id(arn_string): 17 | resource_name = arn_string.split(":")[6] 18 | resource_id = rds.describe_db_instances(DBInstanceIdentifier=resource_name)['DBInstances'][0]['DbiResourceId'] 19 | 20 | return resource_id 21 | 22 | def list_rds_tags(arn_string): 23 | tag_name = missing_tag_value 24 | tag_owner = missing_tag_value 25 | tag_classification = missing_tag_value 26 | tag_list = rds.list_tags_for_resource(ResourceName=arn_string) 27 | for tags in tag_list['TagList']: 28 | if tags['Key'] == 'Name': 29 | tag_name = tags['Value'] 30 | if tags['Key'] == 'Owner': 31 | tag_owner = tags['Value'] 32 | 33 | return tag_name, tag_owner 34 | 35 | for arn in rds_resource_arns: 36 | db_id = get_resource_id(arn) 37 | tag_name, tag_owner = list_rds_tags(arn) 38 | print( 39 | "{0}, {1}, {2}, {3}".format( 40 | "RDS", db_id, tag_name, tag_owner 41 | ) 42 | ) 43 | -------------------------------------------------------------------------------- /aws/python/s3/put_object.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | s3 = boto3.client('s3') 3 | 4 | with open('file.txt', 'r') as file_content: 5 | s3.put_object(Bucket='my-bucket-name', Key=testfolder/file.txt, Body=file_content.read()) 6 | -------------------------------------------------------------------------------- /concourse/01-basic-task-ubuntu.yml: -------------------------------------------------------------------------------- 1 | --- 2 | platform: linux 3 | 4 | image_resource: 5 | type: docker-image 6 | source: 7 | repository: ubuntu 8 | 9 | run: 10 | path: uname 11 | args: [-a] 12 | 13 | ## Running: 14 | # fly -t tutorial execute -c task_whoami.yml 15 | # executing build 12 at http://192.168.100.4:8080/builds/12 16 | # initializing 17 | # running uname -a 18 | # Linux 278dd7ee-4edb-430f-5c87-5885a67516b1 4.4.0-101-generic #124~14.04.1-Ubuntu SMP Fri Nov 10 19:05:36 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 19 | # succeeded 20 | -------------------------------------------------------------------------------- /concourse/02-inputs-upload-list.yml: -------------------------------------------------------------------------------- 1 | --- 2 | platform: linux 3 | 4 | image_resource: 5 | type: docker-image 6 | source: {repository: busybox} 7 | 8 | inputs: 9 | - name: my-input-dir 10 | 11 | run: 12 | path: ls 13 | args: ['-alR'] 14 | 15 | ## Run: 16 | # fly -t tutorial e -c my-inputs-conf.yml -i my-input-dir=./my-dir 17 | # executing build 24 at http://192.168.100.4:8080/builds/24 18 | # initializing 19 | # running ls -alR 20 | # .: 21 | # total 12 22 | # drwxr-xr-x 3 root root 4096 Jan 10 11:34 . 23 | # drwxr-xr-x 3 root root 4096 Jan 10 11:34 .. 24 | # drwxr-xr-x 1 root root 4096 Jan 10 11:34 my-input-dir 25 | 26 | # ./my-input-dir: 27 | # total 8 28 | # drwxr-xr-x 1 root root 4096 Jan 10 11:34 . 29 | # drwxr-xr-x 3 root root 4096 Jan 10 11:34 .. 30 | # -rw-r--r-- 1 501 20 0 Jan 10 11:32 file-1.txt 31 | # -rw-r--r-- 1 501 20 0 Jan 10 11:32 file-2.txt 32 | # succeeded 33 | 34 | # when the working dir is the same name as the inputs-name, then '-i my-input-dir' is not needed. 35 | -------------------------------------------------------------------------------- /concourse/03-task-scripts.yml: -------------------------------------------------------------------------------- 1 | --- 2 | platform: linux 3 | 4 | image_resource: 5 | type: docker-image 6 | source: {repository: busybox} 7 | 8 | inputs: 9 | - name: task-scripts 10 | 11 | run: 12 | path: ./task-scripts/task_show_uname.sh 13 | 14 | # Run: 15 | # fly -t tutorial e -c task_show_uname.yml 16 | # executing build 25 at http://192.168.100.4:8080/builds/25 17 | # initializing 18 | # running ./task-scripts/task_show_uname.sh 19 | # Linux ff5d67fb-4e7f-464a-6d6b-6f0622185803 4.4.0-101-generic #124~14.04.1-Ubuntu SMP Fri Nov 10 19:05:36 UTC 2017 x86_64 GNU/Linux 20 | # succeeded 21 | 22 | # This was running inside the task-scripts directory and the script needs to be executable (755) 23 | # https://concoursetutorial.com/basics/task-scripts/ 24 | -------------------------------------------------------------------------------- /elasticsearch/python/es_bulk_ingest.py: -------------------------------------------------------------------------------- 1 | from elasticsearch import Elasticsearch, helpers 2 | 3 | es = Elasticsearch( 4 | hosts = [{'host': 'es-domain.es.amazonaws.com', 'port': 443}], 5 | use_ssl=True, verify_certs=True 6 | ) 7 | 8 | bulk_docs = [] 9 | list_of_dicts = [{"name": "ruan", "age": 32}, {"name": "stefan", "age": 31}] 10 | 11 | for doc in list_of_dicts: 12 | doc['_index'] = 'my-index' 13 | bulk_docs.append(doc) 14 | 15 | helpers.bulk(es, bulk_docs) 16 | -------------------------------------------------------------------------------- /elasticsearch/python/es_index_document.py: -------------------------------------------------------------------------------- 1 | from elasticsearch import Elasticsearch 2 | 3 | es = Elasticsearch( 4 | hosts = [{'host': 'es-domain.es.amazonaws.com', 'port': 443}], 5 | use_ssl=True, verify_certs=True 6 | ) 7 | 8 | document = {"name": "ruan", "surname": "bekker"} 9 | 10 | response = es.index(index='my-index', doc_type='_doc', body=document) 11 | 12 | # response 13 | # {'_index': 'my-index', '_type': '_doc', '_id': 'Mlq9FHMB6HS5JLZeF5Rs', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1} 14 | -------------------------------------------------------------------------------- /elasticsearch/python/es_show_info_no_auth.py: -------------------------------------------------------------------------------- 1 | from elasticsearch import Elasticsearch 2 | 3 | es = Elasticsearch( 4 | hosts = [{'host': 'es.id.eu-west-1.es.amazonaws.com', 'port': 443}], 5 | use_ssl=True, verify_certs=True 6 | ) 7 | 8 | es.info() 9 | -------------------------------------------------------------------------------- /ethereum/python/create_address.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | pip install eth_keys 5 | pip install eth-hash[pycryptodome] 6 | """ 7 | 8 | import os 9 | from eth_keys import keys 10 | 11 | private_key = keys.PrivateKey(os.urandom(32)) 12 | 13 | public_key = private_key.public_key 14 | eth_address = public_key.to_checksum_address() 15 | 16 | print(f"Private key: {private_key}") 17 | print(f"Public key: {public_key}") 18 | print(f"Ethereum address: {eth_address}") 19 | 20 | """ 21 | Validate: 22 | curl -XPOST -H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0x746C9474d98C8A99280fC0E9DA7d706B647163DE", "latest"],"id":1}' http://localhost:8545 23 | """ 24 | -------------------------------------------------------------------------------- /golang/basics/readfile_into_memory.go: -------------------------------------------------------------------------------- 1 | // https://kgrz.io/reading-files-in-go-an-overview.html 2 | package main 3 | 4 | import "fmt" 5 | import "os" 6 | 7 | func main(){ 8 | 9 | file, err := os.Open("data.txt") 10 | if err != nil { 11 | fmt.Println(err) 12 | return 13 | } 14 | 15 | defer file.Close() 16 | 17 | fileinfo, err := file.Stat() 18 | if err != nil { 19 | fmt.Println(err) 20 | return 21 | } 22 | 23 | filesize := fileinfo.Size() 24 | buffer := make([]byte, filesize) 25 | 26 | bytesread, err := file.Read(buffer) 27 | if err != nil { 28 | fmt.Println(err) 29 | return 30 | } 31 | 32 | fmt.Println("bytes read: ", bytesread) 33 | fmt.Println("bytestream to string: ", string(buffer)) 34 | } 35 | -------------------------------------------------------------------------------- /golang/basics/sleep.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | import "time" 5 | import "strconv" 6 | 7 | func main(){ 8 | time.Sleep(2 * time.Second) 9 | unixtime := strconv.FormatInt(time.Now().UTC().Unix(), 10) 10 | 11 | fmt.Println("Unix Time is:", time.Now().Unix()) 12 | fmt.Println("Unix Time using string conversion:", unixtime) 13 | } 14 | -------------------------------------------------------------------------------- /golang/basics/while.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | import "time" 5 | 6 | func main() { 7 | count := 0 8 | for count <= 10 { 9 | fmt.Println("Count is:", count) 10 | time.Sleep(1 * time.Second) 11 | count++ 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /golang/robotgo/get_cursor_coordinates.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "github.com/go-vgo/robotgo" 7 | ) 8 | 9 | func get() { 10 | time.Sleep(2 * time.Second) 11 | x, y := robotgo.GetMousePos() 12 | fmt.Println("pos:", x, y) 13 | } 14 | 15 | func main() { 16 | mouse() 17 | } 18 | -------------------------------------------------------------------------------- /mongodb/golang/examples.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | 8 | "go.mongodb.org/mongo-driver/bson" 9 | "go.mongodb.org/mongo-driver/mongo" 10 | "go.mongodb.org/mongo-driver/mongo/options" 11 | ) 12 | 13 | type Person struct { 14 | Name string 15 | Age int 16 | City string 17 | } 18 | 19 | func main() { 20 | 21 | // Set client options 22 | clientOptions := options.Client().ApplyURI("mongodb://mongodb:27017") 23 | 24 | // Connect to MongoDB 25 | client, err := mongo.Connect(context.TODO(), clientOptions) 26 | if err != nil { 27 | log.Fatal(err) 28 | } 29 | 30 | // Check the connection 31 | err = client.Ping(context.TODO(), nil) 32 | if err != nil { 33 | log.Fatal(err) 34 | } 35 | 36 | fmt.Println("Connected to MongoDB!") 37 | 38 | // Get a handle for your collection 39 | collection := client.Database("testdb").Collection("people") 40 | 41 | // Some dummy data to add to the Database 42 | ruan := Person{"Ruan", 32, "Cape Town"} 43 | james := Person{"James", 32, "Nairobi"} 44 | frankie := Person{"Frankie", 31, "Nairobi"} 45 | 46 | // Insert a single document 47 | insertResult, err := collection.InsertOne(context.TODO(), ash) 48 | if err != nil { 49 | log.Fatal(err) 50 | } 51 | fmt.Println("Inserted a single document: ", insertResult.InsertedID) 52 | 53 | // Insert multiple documents 54 | trainers := []interface{}{misty, brock} 55 | 56 | insertManyResult, err := collection.InsertMany(context.TODO(), trainers) 57 | if err != nil { 58 | log.Fatal(err) 59 | } 60 | fmt.Println("Inserted multiple documents: ", insertManyResult.InsertedIDs) 61 | 62 | // Update a document 63 | filter := bson.D{{"name", "Ash"}} 64 | 65 | update := bson.D{ 66 | {"$inc", bson.D{ 67 | {"age", 1}, 68 | }}, 69 | } 70 | 71 | updateResult, err := collection.UpdateOne(context.TODO(), filter, update) 72 | if err != nil { 73 | log.Fatal(err) 74 | } 75 | fmt.Printf("Matched %v documents and updated %v documents.\n", updateResult.MatchedCount, updateResult.ModifiedCount) 76 | 77 | // Find a single document 78 | var result Person 79 | 80 | err = collection.FindOne(context.TODO(), filter).Decode(&result) 81 | if err != nil { 82 | log.Fatal(err) 83 | } 84 | 85 | fmt.Printf("Found a single document: %+v\n", result) 86 | 87 | findOptions := options.Find() 88 | findOptions.SetLimit(2) 89 | 90 | var results []*Person 91 | 92 | // Finding multiple documents returns a cursor 93 | cur, err := collection.Find(context.TODO(), bson.D{{}}, findOptions) 94 | if err != nil { 95 | log.Fatal(err) 96 | } 97 | 98 | // Iterate through the cursor 99 | for cur.Next(context.TODO()) { 100 | var elem Person 101 | err := cur.Decode(&elem) 102 | if err != nil { 103 | log.Fatal(err) 104 | } 105 | 106 | results = append(results, &elem) 107 | } 108 | 109 | if err := cur.Err(); err != nil { 110 | log.Fatal(err) 111 | } 112 | 113 | // Close the cursor once finished 114 | cur.Close(context.TODO()) 115 | 116 | fmt.Printf("Found multiple documents (array of pointers): %+v\n", results) 117 | 118 | // Delete all the documents in the collection 119 | deleteResult, err := collection.DeleteMany(context.TODO(), bson.D{{}}) 120 | if err != nil { 121 | log.Fatal(err) 122 | } 123 | 124 | fmt.Printf("Deleted %v documents in the trainers collection\n", deleteResult.DeletedCount) 125 | 126 | // Close the connection once no longer needed 127 | err = client.Disconnect(context.TODO()) 128 | 129 | if err != nil { 130 | log.Fatal(err) 131 | } else { 132 | fmt.Println("Connection to MongoDB closed.") 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /mongodb/python/insert-document-mongodb.py: -------------------------------------------------------------------------------- 1 | from pymongo import MongoClient 2 | import datetime 3 | 4 | client = MongoClient('mongodb://localhost:27017/') 5 | mydb = client['mydb'] 6 | 7 | myrecord = { 8 | "author": "James", 9 | "title" : "PyMongo 101", 10 | "tags" : ["MongoDB", "PyMongo", "Tutorial"], 11 | "date" : datetime.datetime.utcnow() 12 | } 13 | 14 | record_id = mydb.collection01.insert(myrecord) 15 | 16 | print record_id 17 | print mydb.collection_names() 18 | -------------------------------------------------------------------------------- /mysql/python/batch-executemany-writes.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import random 3 | import MySQLdb 4 | 5 | host="192.168.0.6" 6 | user="root" 7 | password="password" 8 | dbname="db1" 9 | 10 | db = MySQLdb.connect(host, user, password, dbname) 11 | 12 | names = ['Ruan', 'Stefan', 'James', 'Peter', 'Silver', 'Frank', 'Michelle', 'Samantha', 'Jennifer'] 13 | surnames = ['Smith', 'Jacobs', 'James', 'Phillips', 'Anderson'] 14 | countries = ['South Africa', 'Italy', 'Sweden', 'England', 'Somoa', 'New York'] 15 | job = ['Doctor', 'Scientist', 'Teacher', 'Police Officer', 'Waiter', 'Banker', 'IT'] 16 | os = ['Linux', 'Windows', 'Mac'] 17 | car = ['Volkswagen', 'Ford', 'Nissan', 'Toyota', 'BMW', 'Audi', 'Mercedez-Benz', 'Mazda', 'Hyundai'] 18 | 19 | cur = db.cursor() 20 | cur.execute("DROP TABLE IF EXISTS myusers") 21 | cur.execute("CREATE TABLE myusers(name VARCHAR(50), surname VARCHAR(50), countries VARCHAR(50), job VARCHAR(20), os VARCHAR(20), car VARCHAR(20))") 22 | 23 | bunch = [] 24 | for x in range(100000): 25 | a = random.choice(names) 26 | b = random.choice(surnames) 27 | c = random.choice(countries) 28 | d = random.choice(job) 29 | e = random.choice(os) 30 | f = random.choice(car) 31 | 32 | bunch.append((a, b, c, d, e, f)) 33 | 34 | # https://mysqlclient.readthedocs.io/user_guide.html 35 | cur.executemany( 36 | """INSERT INTO myusers(name, surname, countries, job, os, car) VALUES(%s, %s, %s, %s, %s, %s)""", 37 | bunch 38 | ) 39 | 40 | db.commit() 41 | db.close() 42 | -------------------------------------------------------------------------------- /mysql/python/insert-batch-generated-mysql.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import random 3 | import MySQLdb 4 | 5 | host="192.168.1.1" 6 | user="root" 7 | password="password" 8 | dbname="db1" 9 | 10 | db = MySQLdb.connect(host, user, password, dbname) 11 | 12 | names = ['Ruan', 'Stefan', 'James', 'Peter', 'Silver', 'Frank', 'Michelle', 'Samantha', 'Jennifer'] 13 | surnames = ['Smith', 'Jacobs', 'James', 'Phillips', 'Anderson'] 14 | countries = ['South Africa', 'Italy', 'Sweden', 'England', 'Somoa', 'New York'] 15 | job = ['Doctor', 'Scientist', 'Teacher', 'Police Officer', 'Waiter', 'Banker', 'IT'] 16 | os = ['Linux', 'Windows', 'Mac'] 17 | car = ['Volkswagen', 'Ford', 'Nissan', 'Toyota', 'BMW', 'Audi', 'Mercedez-Benz', 'Mazda', 'Hyundai'] 18 | 19 | cur = db.cursor() 20 | cur.execute("DROP TABLE IF EXISTS myusers") 21 | cur.execute("CREATE TABLE myusers(name VARCHAR(50), surname VARCHAR(50), countries VARCHAR(50), job VARCHAR(20), os VARCHAR(20), car VARCHAR(20))") 22 | 23 | for x in range(1000000): 24 | a = random.choice(names) 25 | b = random.choice(surnames) 26 | c = random.choice(countries) 27 | d = random.choice(job) 28 | e = random.choice(os) 29 | f = random.choice(car) 30 | 31 | cur.execute("""INSERT INTO myusers(name, surname, countries, job, os, car) VALUES(%s, %s, %s, %s, %s, %s)""", (a, b, c, d, e, f)) 32 | 33 | db.commit() 34 | db.close() 35 | -------------------------------------------------------------------------------- /mysql/python/read-mysql-data.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import random 3 | import MySQLdb 4 | import time 5 | 6 | db = MySQLdb.connect('192.168.1.1', 'root', 'password', 'test-db') 7 | 8 | con = db.cursor() 9 | con.execute("SELECT * from myusers") 10 | 11 | rows = con.fetchall() 12 | 13 | for row in rows: 14 | a_name = row[0] 15 | a_surnames = row[1] 16 | a_country = row[2] 17 | a_gender = row[3] 18 | a_os = row[4] 19 | a_car = row[5] 20 | 21 | print a_name, a_surnames, a_country, a_gender, a_os, a_car 22 | -------------------------------------------------------------------------------- /python-flask/README.md: -------------------------------------------------------------------------------- 1 | ## Templating (Jinja): 2 | 3 | ### For Loops: 4 | 5 | - `app.py` 6 | 7 | ```python 8 | @app.route('/list/groups') 9 | def list_groups(): 10 | groups = get_groups_function() 11 | return render_template('show.html', title='My Title', groups=groups) 12 | ``` 13 | 14 | - `templates/show.html` 15 | 16 | ```html 17 | 18 | 19 |

{{ title }}

20 |

    21 | {% for item in groups %} 22 |
  • {{ item }}
  • 23 | {% endfor %} 24 |
25 | 26 | 27 | ``` 28 | -------------------------------------------------------------------------------- /python-flask/api/flask-api.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify, request 2 | from multiprocessing import Value 3 | 4 | counter = Value('i', 0) 5 | app = Flask(__name__) 6 | 7 | a = [] 8 | help_message = """ 9 | API Usage: 10 | 11 | - GET /api/list 12 | - POST /api/add data={"key": "value"} 13 | - GET /api/get/ 14 | - PUT /api/update/ data={"key": "value_to_replace"} 15 | - DELETE /api/delete/ 16 | 17 | """ 18 | 19 | def id_generator(): 20 | with counter.get_lock(): 21 | counter.value += 1 22 | return counter.value 23 | 24 | @app.route('/api', methods=['GET']) 25 | def help(): 26 | return help_message 27 | 28 | @app.route('/api/list', methods=['GET']) 29 | def list(): 30 | return jsonify(a) 31 | 32 | @app.route('/api/add', methods=['POST']) 33 | def index(): 34 | payload = request.json 35 | payload['id'] = id_generator() 36 | a.append(payload) 37 | return "Created: {} \n".format(payload) 38 | 39 | @app.route('/api/get', methods=['GET']) 40 | def get_none(): 41 | return 'ID Required: /api/get/ \n' 42 | 43 | @app.route('/api/get/', methods=['GET']) 44 | def get(_id): 45 | for user in a: 46 | if _id == user['id']: 47 | selected_user = user 48 | return jsonify(selected_user) 49 | 50 | @app.route('/api/update', methods=['PUT']) 51 | def update_none(): 52 | return 'ID and Desired K/V in Payload required: /api/update/ -d \'{"name": "john"}\' \n' 53 | 54 | @app.route('/api/update/', methods=['PUT']) 55 | def update(_id): 56 | update_req = request.json 57 | key_to_update = update_req.keys()[0] 58 | update_val = (item for item in a if item['id'] == _id).next()[key_to_update] = update_req.values()[0] 59 | update_resp = (item for item in a if item['id'] == _id).next() 60 | return "Updated: {} \n".format(update_resp) 61 | 62 | @app.route('/api/delete/', methods=['DELETE']) 63 | def delete(_id): 64 | deleted_user = (item for item in a if item['id'] == _id).next() 65 | a.remove(deleted_user) 66 | return "Deleted: {} \n".format(deleted_user) 67 | 68 | if __name__ == '__main__': 69 | app.run() 70 | -------------------------------------------------------------------------------- /ruby/basics/for_loop.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | for x in 0..5 4 | puts "Number is: #{x}" 5 | end 6 | 7 | # or 8 | 9 | (0..5).each do |x| 10 | puts "Number is #{x}" 11 | end 12 | -------------------------------------------------------------------------------- /ruby/basics/if_statement_int.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | num = 1 4 | 5 | if num > 2 6 | puts "number is bigger than 2" 7 | elsif num <= 2 and num !=0 8 | puts "number is 1" 9 | else 10 | puts "not getting the number" 11 | end 12 | -------------------------------------------------------------------------------- /ruby/basics/if_statement_str.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | name = "ruan" 4 | 5 | if name == "stefan" 6 | puts "not the right name" 7 | elsif name == "ruan" 8 | puts "Correct name" 9 | else 10 | puts "dunno" 11 | end 12 | -------------------------------------------------------------------------------- /sqlite/python/random-write_to_sqlite.py: -------------------------------------------------------------------------------- 1 | import sqlite3, random 2 | 3 | names = ['ruan', 'stefan', 'philip', 'norman', 'frank', 'pete', 'johnny', 'peter', 'adam'] 4 | cities = ['cape town', 'johannesburg', 'pretoria', 'dublin', 'kroonstad', 'bloemfontein', 'port elizabeth', 'auckland', 'sydney'] 5 | lastnames = ['smith', 'bekker', 'admams', 'phillips', 'james', 'adamson'] 6 | words = ['some really random text', 'this is more random text with stupid words', 'this is a hello world string', 'this is going to be fun - bhla blah blah blah blah bhla blah blah blah blahbhla blah blah blah blahbhla blah blah blah blahbhla blah blah blah blahbhla blah blah blah blahbhla blah blah blah blah'] 7 | 8 | conn = sqlite3.connect('database-large.db') 9 | conn.execute('CREATE TABLE IF NOT EXISTS people (name STRING, age INTEGER, surname STRING, city STRING, favorite_words STRING)') 10 | 11 | conn = sqlite3.connect('database-large.db') 12 | for x in range(1,1000000): 13 | conn.execute('INSERT INTO people VALUES("{}", "{}", "{}", "{}", "{}")'.format(random.choice(names), random.randint(18,40), random.choice(lastnames), random.choice(cities), random.choice(words))) 14 | conn.execute('INSERT INTO people VALUES("{}", "{}", "{}", "{}", "{}")'.format(random.choice(names), random.randint(18,40), random.choice(lastnames), random.choice(cities), random.choice(words))) 15 | conn.execute('INSERT INTO people VALUES("{}", "{}", "{}", "{}", "{}")'.format(random.choice(names), random.randint(18,40), random.choice(lastnames), random.choice(cities), random.choice(words))) 16 | conn.execute('INSERT INTO people VALUES("{}", "{}", "{}", "{}", "{}")'.format(random.choice(names), random.randint(18,40), random.choice(lastnames), random.choice(cities), random.choice(words))) 17 | conn.execute('INSERT INTO people VALUES("{}", "{}", "{}", "{}", "{}")'.format(random.choice(names), random.randint(18,40), random.choice(lastnames), random.choice(cities), random.choice(words))) 18 | 19 | conn.commit() 20 | conn.close() 21 | -------------------------------------------------------------------------------- /sqlite/python/read_from_sqlite.py: -------------------------------------------------------------------------------- 1 | import sqlite3 as sql 2 | 3 | product_name = 'guitar' 4 | 5 | db_connection = sql.connect('db.sql') 6 | c = db_connection.cursor() 7 | 8 | try: 9 | c.execute('select product_description from products where product_name = "{k}"'.format(k=product_name)) 10 | data = c.fetchone()[0] 11 | db_connection.close() 12 | print("=> Product: {p}, Description: {d}".format(p=product_name, d=data)) 13 | except: 14 | print('issue occured') 15 | 16 | # to create the data: 17 | # $ sqlite3 db.sql -header -column 18 | # import sqlite3 as sql 19 | # SQLite version 3.16.0 2016-11-04 19:09:39 20 | # Enter ".help" for usage hints. 21 | # 22 | # sqlite> create table products (product_name STRING(32), product_description STRING(32)); 23 | # sqlite> insert into products values('apple', 'fruit called apple'); 24 | # sqlite> insert into products values('guitar', 'musical instrument'); 25 | # sqlite> select * from products; 26 | # product_name product_description 27 | # ------------ ------------------- 28 | # apple fruit called apple 29 | # guitar musical instrument 30 | # sqlite> .exit 31 | -------------------------------------------------------------------------------- /sqlite/python/read_from_sqlite_groupby.py: -------------------------------------------------------------------------------- 1 | import sqlite3 2 | conn = sqlite3.connect('database.db') 3 | conn.row_factory = sqlite3.Row 4 | cur = conn.cursor() 5 | cur.execute('select count(*) as num, city from people group by city') 6 | rows = cur.fetchall() 7 | 8 | for row in rows: 9 | print("{}, {}".format(row['num'], row['city'])) 10 | 11 | # output: 12 | # 555346, auckland 13 | # 556127, bloemfontein 14 | # 555049, cape town 15 | # 555350, dublin 16 | # 556027, johannesburg 17 | # 555961, kroonstad 18 | # 555740, port elizabeth 19 | # 555645, pretoria 20 | # 554750, sydney 21 | --------------------------------------------------------------------------------