Class

Sat 17 May 2025

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


class Employee:
    def __init__(self, name, age, salary):
        self.name = name
        self.age = age
        self.salary = salary

    def __repr__(self):
        return repr((self.name, self.age, self.salary))
employees = [
    Employee('Peter', 21, 6),
    Employee('Kevin', 22 …

Category: basics

Read More

Class-2-Json

Sat 17 May 2025

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


import json
class City:
    def toJSON(self):
        return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
me = City()
me.name = "Onur"
me.id = 1

print(me.toJSON())  
{
    "id": 1,
    "name": "Onur …

Category: basics

Read More

Class-Method

Sat 17 May 2025

title: "Class Method" author: "Raja CSP Raman" date: 2019-04-20 description: "-" type: technical_note draft: false


class Person:  
    """
    revisit the Person class to validate argument
    """
    def __init__(self, name, job, pay):
        self.job  = job
        self.pay  = pay
                                          # arg 0 is the self instance here
                                          # giveRaise = rangetest(..)(giveRaise)
    def giveRaise(self, percent):
        self …

Category: basics

Read More

Classes-Sample-2024

Sat 17 May 2025
import os
import urllib.parse
from urllib.parse import urlparse

class Client:
    def __init__(self, base_url, http_client):
        self.base_url = base_url
        self.http_client = http_client


def client_from_environment():
    default_port = "11434"
    ollama_host = os.getenv("OLLAMA_HOST", "")

    # Parse scheme and hostport
    if "://" in ollama_host:
        scheme, hostport = ollama_host.split("://", 1)
    else:
        scheme, hostport = "http", ollama_host

    # Adjust default …

Category: basics

Read More

Collections-Counter

Sat 17 May 2025

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


from collections import Counter
cnt = Counter()

for word in ['Toronto', 'Montreal', 'Montreal', 'Waterloo', 'Toronto', 'Toronto']:
    cnt[word] += 1
cnt
Counter({'Montreal': 2, 'Toronto': 3, 'Waterloo': 1})
type(cnt)
collections.Counter


Score: 5

Category: basics

Read More

Compress Image

Sat 17 May 2025

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


from PIL import Image
# Open the image
im = Image.open("/Users/rajacsp/datasets/barcode_images/orange.jpg")

im.save("/Users/rajacsp/datasets/barcode_images/orange_3.jpg", format="JPEG", quality=70)


Score: 0

Category: basics

Read More

Compress Image Cv2

Sat 17 May 2025

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


from PIL import Image
import cv2
# Open the image
img = cv2.imread('/Users/rajacsp/datasets/barcode_images/orange.jpg')


# save image with lower compression—bigger file size but faster decoding
#cv2.imwrite('/Users/str-kwml0011/datasets/random_images …

Category: basics

Read More

Consume-Rest-Api

Sat 17 May 2025

import requests
import json
response = requests.get("https://randomuser.me/api/")
response_json = response.json()
print(response_json)
{'results': [{'gender': 'female', 'name': {'title': 'Ms', 'first': 'Mia', 'last': 'Walker'}, 'location': {'street': {'number': 8562, 'name': 'Port Hills Road'}, 'city': 'Hamilton', 'state': 'Waikato', 'country': 'New Zealand', 'postcode': 45938, 'coordinates': {'latitude': '54.8821', 'longitude': '15.6902 …

Category: basics

Read More

Copy List

Sat 17 May 2025

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


import copy
a = [1, 2, 3]
b = copy.copy(a)
b
[1, 2, 3]
b.append(4)
a
[1, 2, 3]
b
[1, 2, 3, 4]
# deep copy

c = copy.deepcopy(a)
c
[1, 2, 3 …

Category: basics

Read More

Counter-On-News

Sat 17 May 2025

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


import re
from collections import Counter
words = re.findall(r'\w+', open('../../../assets/news.txt').read().lower())
Counter(words).most_common(10)
[('a', 37),
 ('and', 36),
 ('to', 30),
 ('the', 27),
 ('that', 24),
 ('of', 18),
 ('employees', 17 …

Category: basics

Read More
Page 2 of 17

« Prev Next »