avatarGraham Zemel

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4775

Abstract

/span>)], <span class="hljs-string">'email'</span>:[fake.email() <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>)], <span class="hljs-string">'text'</span>:[fake.text() <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">5</span>)]})

<span class="hljs-built_in">print</span>(fakeDataframe)</pre></div><p id="58aa"><i>Follow steps listed on above tools for dependency (/libraries — ‘pandas’ & ‘faker’) installation.</i></p><h2 id="ccf8">Nicely Formatted Strings</h2><div id="49a7"><pre>number = <span class="hljs-number">1234.56</span> percentage = <span class="hljs-number">0.33</span> big_number = <span class="hljs-number">149839020429482</span>

<span class="hljs-built_in">print</span>(<span class="hljs-string">f'No formatting: <span class="hljs-subst">{number}</span>'</span> <span class="hljs-comment"># self explanatory</span> <span class="hljs-string">f'\nVariable name: <span class="hljs-subst">{number = }</span>'</span> <span class="hljs-comment"># self explanatory</span> <span class="hljs-string">f'\nConverted to two decimal place: <span class="hljs-subst">{number:<span class="hljs-number">.2</span>f}</span>'</span> <span class="hljs-comment"># You can choose any number </span> <span class="hljs-string">f'\nChanging wide characters to 30: <span class="hljs-subst">{number:<span class="hljs-number">30</span>}</span>'</span> <span class="hljs-comment"># The field will be 30 characters wide</span> <span class="hljs-string">f'\nAlign response to center: <span class="hljs-subst">{number:^<span class="hljs-number">30</span>}</span>'</span> <span class="hljs-comment"># Left-aligned = less than 30 | right-aligned = greater than 30</span> <span class="hljs-string">f'\nFill white spaces: <span class="hljs-subst">{number:=^<span class="hljs-number">30</span>}</span>'</span> <span class="hljs-comment"># Fill white spaces with any character</span> <span class="hljs-string">f'\nPrinting percent: <span class="hljs-subst">{percentage_number:<span class="hljs-number">.2</span>%}</span>'</span> <span class="hljs-comment"># Multiply by 100 and add % symbol</span> <span class="hljs-string">f'\nTwo decimal places w/ comma: <span class="hljs-subst">{high_number:,<span class="hljs-number">.2</span>f}</span>'</span> <span class="hljs-string">f'\nTwo decimal places w/ scientific notation: <span class="hljs-subst">{high_number:<span class="hljs-number">.2</span>e}</span>'</span> )</pre></div><h2 id="89ba">TQDM Progress Bar (A bit more advanced)</h2><div id="35b7"><pre><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd <span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np <span class="hljs-keyword">from</span> tqdm <span class="hljs-keyword">import</span> tqdm

<span class="hljs-comment"># Generate a dataframe with random numbers of shape 1,000 x 1,000</span> df = pd.DataFrame(np.random.randint(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>, (<span class="hljs-number">100000</span>, <span class="hljs-number">1000</span>)))

<span class="hljs-comment"># Register pandas.progress_apply with tqdm</span> tqdm.pandas(desc=<span class="hljs-string">'Processing Dataframe'</span>)

<span class="hljs-comment"># Add 3 to each value then cube for entire dataframe</span> df.progress_apply(<span class="hljs-keyword">lambda</span> x: (x+<span class="hljs-number">3</span>)**<span class="hljs-number">3</span>)</pre></div><p id="58d4"><i>Result →</i></p><div id="89ec"><pre><span class="hljs-attr">Processing Dataframe:</span> <span class="hljs-number">100</span><span class="hljs-string">%|██████████|</span> <span class="hljs-number">1000</span><span class="hljs-string">/1000</span> [<span class="hljs-number">00</span><span class="hljs-string">:02<00:00</span>, <span class="hljs-number">336.</span><span class="hljs-string">21it/s</span>]</pre></div><h2 id="ffcf">Nato Alphabet Converter</h2><div id="5785"><pre><span class="hljs-keyword">import</span> sys

nato_alphabet = { <span class="hljs-string">'a'</span>: <span class="hljs-string">'alpha'</span>, <span class="hljs-string">'b'</span>: <span class="hljs-string">'bravo'</span>, <span class="hljs-string">'c'</span>: <span class="hljs-string">'charlie'</span>, <span class="hljs-string">'d'</span>: <span class="hljs-string">'delta'</span>, <span class="hljs-string">'e'</span>: <span class="hljs-string">'echo'</span>, <span class="hljs-string">'f'</span>: <span c

Options

lass="hljs-string">'foxtrot'</span>, <span class="hljs-string">'g'</span>: <span class="hljs-string">'golf'</span>, <span class="hljs-string">'h'</span>: <span class="hljs-string">'hotel'</span>, <span class="hljs-string">'i'</span>: <span class="hljs-string">'india'</span>, <span class="hljs-string">'j'</span>: <span class="hljs-string">'juliet'</span>, <span class="hljs-string">'k'</span>: <span class="hljs-string">'kilo'</span>, <span class="hljs-string">'l'</span>: <span class="hljs-string">'lima'</span>, <span class="hljs-string">'m'</span>: <span class="hljs-string">'mike'</span>, <span class="hljs-string">'n'</span>: <span class="hljs-string">'november'</span>, <span class="hljs-string">'o'</span>: <span class="hljs-string">'oscar'</span>, <span class="hljs-string">'p'</span>: <span class="hljs-string">'papa'</span>, <span class="hljs-string">'q'</span>: <span class="hljs-string">'quebec'</span>, <span class="hljs-string">'r'</span>: <span class="hljs-string">'romeo'</span>, <span class="hljs-string">'s'</span>: <span class="hljs-string">'sierra'</span>, <span class="hljs-string">'t'</span>: <span class="hljs-string">'tango'</span>, <span class="hljs-string">'u'</span>: <span class="hljs-string">'uniform'</span>, <span class="hljs-string">'v'</span>: <span class="hljs-string">'victor'</span>, <span class="hljs-string">'w'</span>: <span class="hljs-string">'whiskey'</span>, <span class="hljs-string">'x'</span>: <span class="hljs-string">'x-ray'</span>, <span class="hljs-string">'y'</span>: <span class="hljs-string">'yankee'</span>, <span class="hljs-string">'z'</span>: <span class="hljs-string">'zulu'</span> }

<span class="hljs-keyword">try</span>: sys.argv[<span class="hljs-number">1</span>] <span class="hljs-keyword">except</span>: <span class="hljs-built_in">print</span>(<span class="hljs-string">"Usage: natoalphabet.py <word>"</span>) exit(<span class="hljs-number">1</span>)

<span class="hljs-keyword">for</span> letter <span class="hljs-keyword">in</span> sys.argv[<span class="hljs-number">1</span>]: <span class="hljs-keyword">if</span> letter.lower() <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> nato_alphabet: <span class="hljs-built_in">print</span>(letter) <span class="hljs-keyword">else</span>: <span class="hljs-built_in">print</span>(nato_alphabet[letter])</pre></div><p id="a85d"><i>Run using →</i></p><div id="8816"><pre>python3 natoalphabet.py Graham</pre></div><h2 id="ac49">Image Manipulation</h2><div id="beb0"><pre><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageFilter

<span class="hljs-keyword">try</span>:
    original = Image.<span class="hljs-built_in">open</span>(<span class="hljs-string">"imagename.png"</span>)

    <span class="hljs-comment"># Blur the image</span>
    blurred = original.<span class="hljs-built_in">filter</span>(ImageFilter.BLUR)

    <span class="hljs-comment"># Display both images</span>
    original.show()
    blurred.show()

    blurred.save(<span class="hljs-string">"blurred.png"</span>)

<span class="hljs-keyword">except</span>:
    <span class="hljs-built_in">print</span> <span class="hljs-string">"Unable to load image"</span></pre></div><h2 id="b3e0">Website Status</h2><div id="f49c"><pre><span class="hljs-keyword">import</span> requests

r = requests.get(<span class="hljs-string">'https://blog.grahamzemel.com'</span>) <span class="hljs-built_in">print</span>(r.status_code)</pre></div><p id="ada0">Thanks for reading about neat task flows in Python! If you enjoyed this post or learned something new, feel free to give it a few claps and check out <a href="http://thegrayarea.tech"><b>The Gray Area</b></a> for similar posts.</p><p id="ab8c">Support me and help me create even more content by subscribing to a Medium membership using <a href="https://blog.grahamzemel.com/membership">my referral link</a></p><div id="470d" class="link-block"> <a href="https://blog.grahamzemel.com/membership"> <div> <div> <h2>Join Medium with my referral link - Graham Zemel</h2> <div><h3>Read all of Graham Zemel's posts, and any other post from thousands of Medium writers! You'll get full access to every…</h3></div> <div><p>blog.grahamzemel.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*msuafcisCN3CURyp)"></div> </div> </div> </a> </div><p id="73ae">Thanks!</p></article></body>

10 Simple Python Snippets for Web Development, Automation, and Data Science

TL;DR- A quick list of 10 modules and snippets that I use in a bunch of my Python programs, allowing me to automate complicated tasks and format my code just the way I like it.

Introduction

Python is one of my favorite coding tools, and it has tons of useful features as a ‘high-level’ programming language. High level languages usually automate a bunch of different functions natively, meaning you can essentially do the same thing in 5 lines of Python as 50 lines of Java.

For that reason, many developers use it to build even more productive functions and capabilities, here are a few that I’ve used to enhance my productivity and automate tasks

Note: All code has been tested and currently works with Python 3.11 as of 12/2/22, feel free to comment if anything stops working or to suggest more tips!

Password Generator

import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print(password)

Programmatic Google Search

from googlesearch import search
query = "Graham Zemel"
for url in search(query):
    print(url)

Requires ‘google’ Python library to be installed before running.

(pip3 install google on command line)

Automated Browser Bot

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

bot = webdriver.Chrome(“chromedriver.exe”)
bot.get('https://grahamzemel.com')

search = bot.find_element(By.XPATH, '/html/body/div[2]/div[6]/div/div/main/div/div[3]')
# useful method:
# search.send_keys("Hello World")
search.click()
time.sleep(5)
# bot.quit()

Requires ‘selenium’ Python library to be installed before running.

(pip3 install selenium on command line)

Create Custom Modules

# Find current path of Python script
import sys
print(sys.path)
# my_module example: '/Users/grahamzemel/my_module' (no need to include '.py')
import my_module
my_module.my_function()

Create Fake Data

import pandas as pd
from faker import Faker

# Create object
fake = Faker()

# Generate data
fake.name()
fake.text()
fake.address()
fake.email()
fake.date()
fake.country()
fake.phone_number()
fake.random_number(digits=5)

# Dataframe creation
fakeDataframe = pd.DataFrame({'date':[fake.date() for i in range(5)],
                         'name':[fake.name() for i in range(5)],
                         'email':[fake.email() for i in range(5)],
                         'text':[fake.text() for i in range(5)]})

print(fakeDataframe)

Follow steps listed on above tools for dependency (/libraries — ‘pandas’ & ‘faker’) installation.

Nicely Formatted Strings

number = 1234.56
percentage = 0.33
big_number = 149839020429482

print(f'No formatting: {number}' # self explanatory
      f'\nVariable name: {number = }' # self explanatory
      f'\nConverted to two decimal place: {number:.2f}' # You can choose any number 
      f'\nChanging wide characters to 30: {number:30}' # The field will be 30 characters wide
      f'\nAlign response to center: {number:^30}' # Left-aligned = less than 30 | right-aligned = greater than 30
      f'\nFill white spaces: {number:=^30}' # Fill white spaces with any character
      f'\nPrinting percent: {percentage_number:.2%}' # Multiply by 100 and add % symbol
      f'\nTwo decimal places w/ comma: {high_number:,.2f}' 
      f'\nTwo decimal places w/ scientific notation: {high_number:.2e}'
     )

TQDM Progress Bar (A bit more advanced)

import pandas as pd
import numpy as np
from tqdm import tqdm

# Generate a dataframe with random numbers of shape 1,000 x 1,000
df = pd.DataFrame(np.random.randint(0, 100, (100000, 1000)))

# Register `pandas.progress_apply` with `tqdm`
tqdm.pandas(desc='Processing Dataframe')

# Add 3 to each value then cube for entire dataframe
df.progress_apply(lambda x: (x+3)**3)

Result →

Processing Dataframe: 100%|██████████| 1000/1000 [00:02<00:00, 336.21it/s]

Nato Alphabet Converter

import sys

nato_alphabet = {
    'a': 'alpha',
    'b': 'bravo',
    'c': 'charlie',
    'd': 'delta',
    'e': 'echo',
    'f': 'foxtrot',
    'g': 'golf',
    'h': 'hotel',
    'i': 'india',
    'j': 'juliet',
    'k': 'kilo',
    'l': 'lima',
    'm': 'mike',
    'n': 'november',
    'o': 'oscar',
    'p': 'papa',
    'q': 'quebec',
    'r': 'romeo',
    's': 'sierra',
    't': 'tango',
    'u': 'uniform',
    'v': 'victor',
    'w': 'whiskey',
    'x': 'x-ray',
    'y': 'yankee',
    'z': 'zulu'
}

try:
    sys.argv[1]
except:
    print("Usage: natoalphabet.py <word>")
    exit(1)

for letter in sys.argv[1]:
    if letter.lower() not in nato_alphabet:
        print(letter)
    else:
        print(nato_alphabet[letter])

Run using →

python3 natoalphabet.py Graham

Image Manipulation

from PIL import Image, ImageFilter
    
    try:
        original = Image.open("imagename.png")
    
        # Blur the image
        blurred = original.filter(ImageFilter.BLUR)
    
        # Display both images
        original.show()
        blurred.show()
    
        blurred.save("blurred.png")
    
    except:
        print "Unable to load image"

Website Status

import requests
r = requests.get('https://blog.grahamzemel.com')
print(r.status_code)

Thanks for reading about neat task flows in Python! If you enjoyed this post or learned something new, feel free to give it a few claps and check out The Gray Area for similar posts.

Support me and help me create even more content by subscribing to a Medium membership using my referral link

Thanks!

Python
Programming
Automation
Web Development
Data Science
Recommended from ReadMedium