Week 05 2020

  • Python : Python one liner to extract and transform Spring HttpStatus enums Raw
curl -s https://raw.githubusercontent.com/spring-projects/spring-framework/master/spring-web/src/main/java/org/springframework/http/HttpStatus.java | \
grep '\".*),' | \
while read line; \
  do echo $line | python -c 'import re,sys; s, c, m = re.search("([\w\_]+)\((\d+),\s+\"([\w\s]+)", sys.stdin.readline()).groups(); print("{}: (\"HttpStatus.{}\", \"{}\"),".format(c, s, m))'; \
done
  • Python: Find words in a sentence
source = 'The quick brown fox jumps over the lazy dog'

import re
print(re.findall(r'\b(\w+)\b', source))
  • jq: Length of array
$ echo '["123", "567", "abc"]' | jq '. | length'
  • Shell: Pass contents of clipboard as file descriptor. It'll work with apps expecting file argument.
$ command <(pbpaste)
  • Python/Jinja: Using Jinja template in a simple Python script
$ cat template.j2
{{ description }}
# pip install Jinja2

from jinja2 import Environment, FileSystemLoader

TEMPLATE_DIR = os.path.dirname(os.path.abspath(__file__))

jinja_env = Environment(loader=FileSystemLoader(TEMPLATE_DIR), trim_blocks=True)

book_item = dict(
	description="Hello World"
)

rendered = jinja_env.get_template("template.j2").render(book_item)
print(rendered)
  • Convert: Resize image
$ convert -quality 75% -resize 40x40 twitter.png twitter-icn.png
  • Python: A Quick script to collect data from website
from pathlib import Path
import re
import subprocess
import time

def run_command(command):
  return subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
  
link_rgx = re.compile("Link\s=\s\"(.*)\"")

def process_file(file):
  text = file.read_text()
  link = link_rgx.findall(text)[0]
  cmd = "curl -s " + link + ' | pup \'div.stacked div.left a.actionLinkLite text{}\' | head -n 1'
  category = run_command(cmd)
  category = category.strip()
  print("Running {}".format(cmd))
  print("Category Found for {} is {}".format(link, category))
  cmd = 'sed -i.bak \'s/categories = \[""\]/categories = \["' + category + '"\]/g\' ' + file.as_posix()
  print("Running {}".format(cmd))
  run_command(cmd)
  time.sleep(5)

def main():
  p = Path.cwd().joinpath("books")
  for f in p.iterdir():
    process_file(f)

if __name__=="__main__":
  main()