Skip to content

Instantly share code, notes, and snippets.

@pavelanni
pavelanni / parseForm.go
Created July 11, 2021 01:39
My naive attempt to fill a struct from form data -- similar to gorilla/schema, but much simpler
func newHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
files := []string{
templateDir + "/bootstrap.go.html",
templateDir + "/header.go.html",
templateDir + "/new.go.html",
templateDir + "/footer.go.html",
}
tmpl, err := template.ParseFiles(files...)
if err != nil {
package main
import "fmt"
func inner(s []int) {
s[len(s)-1] = 999
}
func inner2(s []int) {
s[0] = 9
@pavelanni
pavelanni / gist:4cc93619ed882b8f9a24ac3f06c4cb2a
Created March 16, 2021 17:22
You have many RPMs in one dir; you want to know which repos they belong to (BaseOS, AppStream, etc.)
for r in *rpm ; do pkgname=$(rpm -q --queryformat "%{NAME}" -p ${r}); dnf -q repoquery --qf "%{name} %{reponame}" ${pkgname} ; done
@pavelanni
pavelanni / gist:800a1de2d7c85881b751868208f1d5e3
Created March 12, 2021 15:54
Python: Add items to a dict from a second dict without changing the existing ones
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'d': 4, 'e': 5, 'a': 6}
d1.update({k:v for (k,v) in d2.items() if k not in d1})
d1
# Out: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
#!/usr/bin/env python
import sys
import sqlite3
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} <year_start> <year_end>")
sys.exit(1)
year_start = int(sys.argv[1])
year_end = int(sys.argv[2])
@pavelanni
pavelanni / gist:bc3aba097ffd30647125602f3cba5153
Created July 22, 2020 16:10
List packages in each RPM repo and store in files
for r in $(dnf repolist | egrep -v "^repo" | awk '{print $1}') ; do dnf repository-packages ${r} list > ${r}.txt ; done
@pavelanni
pavelanni / s3_glacier_archive.py
Last active June 7, 2018 17:32
Lambda function to automatically copy objects uploaded to S3 bucket to a Glacier vault. It also creates a record in DynamoDB for each archived object so they can be retrieved later
import urllib.parse
import boto3
import tempfile
from datetime import datetime
account_id = 'XXXXXXXXXX'
db_name = 'archive-db'
vault = 'archive-vault'
@pavelanni
pavelanni / russian_decor.py
Created February 26, 2018 15:26
Fun with decorators and Russian language in Python
печать = print
def пожалуйста(func):
def wrapper():
func()
печать(', пожалуйста ', end='')
return wrapper
@pavelanni
pavelanni / bashrc.sh
Created February 26, 2018 15:15
This changes PS1 on the AWS EC2 instance to show the instance's name (instead of the meaningless internal hostname)
# Borrowed from here: https://stackoverflow.com/questions/18338585/how-to-get-the-instance-name-from-the-instance-in-aws
# This requires AWS CLI to be installed in the instance
INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
INSTANCE_NAME=`aws ec2 describe-tags --filters Name=resource-id,Values=$INSTANCE_ID --query Tags[].Value --output text`
export PS1="[\u@$INSTANCE_NAME \W]\\$ "
@pavelanni
pavelanni / dynamodb_types.py
Created January 31, 2018 18:47
DynamoDB field types
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table("users")
response = table.get_item()
response = table.get_item(Key={
'user_id': 'REDACTED',
'user_name': 'John'
})
for k in response['Item'].keys():