Skip to content

Instantly share code, notes, and snippets.

@ajitmp
ajitmp / Deploy_a_Django_Rest_Api_on_AWS_EC2_using_Docker_NGINX_Gunicorn_and_GitHub_Action.yml Django, a high-level Python web framework, is a popular choice for building RESTful APIs. Once your Django Rest API has been developed and tested, deploying it becomes a crucial step in making it accessible to users. In this blog post, we'll guide you through the process of deploying a Django Rest API on AWS EC2 using Docker for containerization…
name: Continuous Integration and Delivery
on: [push]
env:
#
DJANGO_TUTORIAL_API_IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')/store
DJANGO_TUTORIAL_NGINX_IMAGE: ghcr.io/$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]')/nginx
REGISTRY: ghcr.io
@ajitmp
ajitmp / pydantic_to_json.py
Created September 26, 2024 08:56
A experimental code to check use of pydantic for data validation and then outputing into a json file.
import json
from pydantic import BaseModel, ValidationError
from ajit_utils import list_of_dict_to_json
# Define the Pydantic data class (model)
class Person(BaseModel):
name: str
age: int
city: str = "Unknown" # Default value
@ajitmp
ajitmp / list_of_dict_to_json.py
Created September 26, 2024 08:29
This is a utiltiy method to write python list of dictonary data to a json file.
import json
def list_of_dict_to_json(list_data_dict:list, filename:str)-> None:
"""This is a utiltiy method to write python dictonary data
to a json file.
Args:
list_data_dict (list): Python list of dict having the data
filename (str): Filename for the json file
example_data = [
@ajitmp
ajitmp / data.json
Created August 8, 2024 11:32
A sample data for Book model having three fields
[
{
"id": 1,
"title": "Python Programming for Beginners",
"author": "John Smith",
"price": 29.99
},
{
"id": 2,
"title": "JavaScript: The Good Parts",
@ajitmp
ajitmp / books.json
Created August 8, 2024 11:28
Same data for a Book model
[
{
"title": "Example Book 1",
"author": "John Doe",
"isbn": "1234567890123",
"pages": 350,
"language": "English"
},
{
"title": "Example Book 2",
@ajitmp
ajitmp / sources.json
Last active July 27, 2024 18:37
A json file having information about website which are source for data scraping.
[
{
"spidername": "Quote Ajit Source",
"start_urls": ["https://quotes-scrape.netlify.app/"],
"quote_div_main": "div.quote-container",
"title_selector": "h2.title",
"author_selector": "p.author"
},
{
"spidername": "Brainy Quote",
@ajitmp
ajitmp / bluk_gen_spiders.py
Last active July 27, 2024 18:38
This is a spider generation script use jinja2 template and a json file to create spider/spiders for all the sources added to sources.json file.
import json
from jinja2 import Environment, FileSystemLoader
from urllib.parse import urlparse
import os
# Load JSON configuration
with open('sources.json') as f:
sources = json.load(f)
# Set up Jinja2 environment
@ajitmp
ajitmp / quotespider_template.jinja2
Created July 27, 2024 18:13
This gist is a template file to generate spider to scrape quote
import scrapy
from spiders_automation_demo.items import QuoteItem
from spiders_automation_demo.itemsloader import QuoteLoader
class {{ spiderclass}}(scrapy.Spider):
name = "{{spidername}}"
allowed_domains = ["{{allowed_domains}}"]
start_urls = {{start_urls}}
def parse(self, response):
@ajitmp
ajitmp / pipelines_settings.py
Created July 27, 2024 14:31
This is for showing configruation in settings.py of project for specific features. Like, it is for how to enable pipleline in settings.py
ITEM_PIPELINES = {
'spiders_automation_demo.pipelines.FilterQuotesPipelineLoveOrLife': 300,
}
@ajitmp
ajitmp / pipelines.py
Created July 27, 2024 14:30
This gist has code for pipeline for scrapy project and it will filter any title text that have 'love' or 'life' else quote will be dropped.
from scrapy.exceptions import DropItem
class FilterQuotesPipelineLoveOrLife:
def process_item(self, item, spider):
# Check if 'love' or 'life' is in the text
if 'love' in item['title'].lower() or 'life' in item['title'].lower():
return item
else:
raise DropItem(f"Quote without 'love' or 'life' in title text: {item['title']}")