PyJWT - Generating Secret Key

import secrets
jwt_secret_key = secrets.token_hex(32)

423c984f59fff65efcbb055cdcac8f32162db4d8561b3007a6cb80703f86d702



Finding Dead Code

pip install vulture

vulture --ignore-decorators @app.route <path>

Hash Functions

import hashlib

str2hash = "test"
result = hashlib.sha256(str2hash.encode()).hexdigest()
print("sha256", len(result), result)

str2hash = "test"
result = hashlib.md5(str2hash.encode()).hexdigest()
print("md5", len(result), result)

sha256 64 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
md5 32 098f6bcd4621d373cade4e832627b4f6

Assessment - Python - Mukesh

Recursive Function:

 

Solution:

 

CSV to JSON

import pandas as pd

df = pd.read_csv("data.csv", low_memory=False)
df.to_json("data.json", orient="records")
print("Done")

How to set up virtual environment

Mac

  1. pip install virtualenv
  2. virtualenv -p python3.11 venv
  3. source venv/bin/activate

Windows

  1. pip install virtualenv
  2. virtualenv venv
  3. .\venv\Scripts\activate
PS: venv is <virtual env name>

Flask Rest API

Web Scraping

Commands:
$ pip install scrapy
$ pip install autopep8
$ pip install ipython
$ scrapy shell
$ scrapy startproject worldometers
$ cd worldometers
$ scrapy genspider countries www.worldometers.info/world-population/population-by-country
# Modify spider
$ scrapy crawl countries

$ scrapy shell
> url = "https://www.worldometers.info/world-population/population-by-country"
> r = scrapy.Request(url)
> fetch(r)
> view(response)
> title = response.xpath('//h1/text()').get()
> countries = response.xpath('//td/a/text()').getall()
> title = response.css('h1::text').get()
> countries = response.css('td a::text').getall()

How to fix broken pip?

python -m pip install --upgrade pip
pip install -r requirements.txt