Abstract-With-Ast

Sat 17 May 2025
!python --version
Python 3.12.4
import ast
# Python code as a string
code = """
def greet(name):
    return f"Hello, {name}!"

x = 10
y = x + 20
print(greet("Python"))
"""
# Parse the code into an AST
tree = ast.parse(code)
# Define a visitor class to analyze nodes
class CodeAnalyzer(ast.NodeVisitor):
    def visit_FunctionDef(self, node):
        print(f"Function found: {node.name}")
        self.generic_visit(node)

    def visit_Assign(self, node):
        targets = [t.id for t in node.targets if isinstance(t, ast.Name)]
        print(f"Assignment to: {targets}")
        self.generic_visit(node)

    def visit_Call(self, node):
        if isinstance(node.func, ast.Name):
            print(f"Function call: {node.func.id}")
        self.generic_visit(node)
# Create an analyzer instance and visit nodes
analyzer = CodeAnalyzer()
analyzer.visit(tree)
Function found: greet
Assignment to: ['x']
Assignment to: ['y']
Function call: print
Function call: greet


Score: 5

Category: basics


Add-Padding-Around-String

Sat 17 May 2025

Create Some Text

text = 'Chapter 2'

Add Padding Around Text

# Add Spaces Of Padding To The Left
format(text, '>20')
'           Chapter 2'
# Add Spaces Of Padding To The Right
format(text, '<20')
'Chapter 2           '
# Add Spaces Of Padding On Each Side
format(text, '^20')
'     Chapter 2      '
# Add * Of Padding On …

Category: basics

Read More

Age-Calculator

Sat 17 May 2025
from datetime import datetime
def get_age(d):
    d1 = datetime.now()
    months = (d1.year - d.year) * 12 + d1.month - d.month

    year = int(months / 12)
    return year
age = get_age(datetime(1991, 1, 1))
age
33


Score: 5

Category: basics

Read More

Args-Sample

Sat 17 May 2025

def print_everything(*args):
        for count, thing in enumerate(args):
            print( '{0}. {1}'.format(count, thing))
print_everything('apple', 'banana', 'cabbage', 'spinach')
0. apple
1. banana
2. cabbage
3. spinach


Score: 0

Category: basics

Read More

Article-Reader

Sat 17 May 2025

import requests
from bs4 import BeautifulSoup
# Collect and parse first page
page = requests.get('https://www.wired.com/story/intel-great-american-microchip-mobilization/')
soup = BeautifulSoup(page.text, 'html.parser')    
content = soup.select('article.article-body-component')
content
[]


Score: 5

Category: basics

Read More

Barcode Or Not

Sat 17 May 2025

title: "Barcode Or Not" author: "Rj" date: 2019-04-20 description: "-" type: technical_note draft: false


from PIL import Image
from colormap import rgb2hex
from os import listdir
from os.path import isfile, join, isdir
import os.path as path
NON_BARCODE_PAR = 500

def is_barcode_image(filename):

    im = Image.open(filename, 'r')
    #width, height = im …

Category: basics

Read More

Barcode-Validator

Sat 17 May 2025

# !pip install colormap
from PIL import Image
from colormap import rgb2hex
from os import listdir
from os.path import isfile, join, isdir
import os.path as path
NON_BARCODE_PAR = 500

def is_barcode_image(filename):

    im = Image.open(filename, 'r')
    #width, height = im.size
    pixel_values = list(im.getdata())

    #print(pixel_values)

    color_set = set()

    for …

Category: basics

Read More

Binary Search

Sat 17 May 2025

title: "Binary Search" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


def binary_search(lys, val):  
    first = 0
    last = len(lys)-1
    index = -1
    while (first <= last) and (index == -1):
        mid = (first+last)//2
        if lys[mid] == val:
            index = mid
        else:
            if val<lys[mid]:
                last = mid -1 …

Category: basics

Read More

Chainmap

Sat 17 May 2025

title: "Chainmap" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


city = {'name': 'Toronto', 'short_name': 'TO'}
number = {'a': 2, 'b': 3, 'c' : 4}
city
{'name': 'Toronto', 'short_name': 'TO'}
number
{'a': 2, 'b': 3, 'c': 4}
from collections import ChainMap
ab = ChainMap(city, number)
ab
ChainMap({'name': 'Toronto', 'short_name …

Category: basics

Read More
Page 1 of 17

Next »