Skip to content

Instantly share code, notes, and snippets.

@nagarjunareddyg
nagarjunareddyg / gist:2012867fe081add43efdd99730bf29c7
Created January 11, 2023 11:20 — forked from RabeyaMuna/gist:39805ccf914940b32a0b27be052e5b66
The replace_ending function replaces the old string in a sentence with the new string, but only if the sentence ends with the old string. If there is more than one occurrence of the old string in the sentence, only the one at the end is replaced, not all of them. For example, replace_ending("abcabc", "abc", "xyz") should return abcxyz, not xyzxy…
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
if sentence.endswith(old):
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = sentence.split()
k =i[-1].replace(old,new)
new_sentence=sentence[0:-len(old)]+k
return new_sentence
@nagarjunareddyg
nagarjunareddyg / report_email.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/report_email.py
Generate a PDF report and send it through email - Send report through email
#report_email.py
#!/usr/bin/env python3
import datetime
import os
from run import catalog_data
from reports import generate_report
from emails import generate_email, send_email
def pdf_body(input_for, desc_dir):
@nagarjunareddyg
nagarjunareddyg / reports.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/reports.py
Generate a PDF report and send it through email
# reports.py
#!/usr/bin/env python3
from reportlab.platypus import Paragraph, Spacer, Image, SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet
def generate_report(file, title, add_info):
styles=getSampleStyleSheet()
report=SimpleDocTemplate(file)
report_title=Paragraph(title, styles['h1'])
#!/usr/bin/env python3
import socket
import shutil
import psutil
import emails
def check_localhost():
localhost=socket.gethostbyname('localhost')
return localhost=="127.0.0.1"
@nagarjunareddyg
nagarjunareddyg / run.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/run.py
Uploading the descriptions
#!/usr/bin/env python3
import os, requests, json
def catalog_data(url, description_dir):
fruit={}
for item in os.listdir(description_dir): # 문제
fruit.clear()
filename=os.path.join(description_dir,item)
with open(filename) as f:
@nagarjunareddyg
nagarjunareddyg / example_upload.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/example_upload.py
Uploading images to web server
#!/usr/bin/env python3
import requests, os
# The URL to upload the images
url="http://localhost/upload/"
# To get the username from environment variable
USER = os.getenv('USER')
# The directory which contains all the images.
image_directory='/home/{}/supplier-data/images/'.format(USER)
#Listing all the files in images directory
files=os.listdir(image_directory)
@nagarjunareddyg
nagarjunareddyg / changeImage.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/changeImage.py
Working with supplier images
#!/usr/bin/env python3
import os, sys
from PIL import Image
user = os.getenv('USER')
image_directory = '/home/{}/supplier-data/images/'.format(user)
for image_name in os.listdir(image_directory):
if not image_name.startswith('.') and 'tiff' in image_name:
image_path = image_directory + image_name
@nagarjunareddyg
nagarjunareddyg / answer.py
Created January 5, 2023 10:56 — forked from Kimjunkuk/answer.py
Automatically Generate a PDF and send it by Email
#!/usr/bin/env python3
import json
import locale
import sys
from reports import generate as report
from emails import generate as email_generate
from emails import send as email_send
@nagarjunareddyg
nagarjunareddyg / answer.py
Created January 5, 2023 10:54 — forked from Kimjunkuk/answer.py
3. Question 3 The following code can lead to an infinite loop. Fix the code so that it can finish successfully for all numbers. Note: Try running your function with the number 0 as the input, and see what you get!
def is_power_of_two(n):
# Check if the number can be divided by two without a remainder
while n % 2 == 0:
if n<=0:
return False
else:
n = n / 2
return True
break
# If after dividing by two the number is 1, it's a power of two
@nagarjunareddyg
nagarjunareddyg / lb-manager-client.sh
Created October 14, 2022 13:40 — forked from thomasdarimont/lb-manager-client.sh
Shell script to configure load- balancing with mod-proxy-balancer
#! /bin/sh
# Set up a default search path
PATH="/usr/bin:/bin"
CURL=`which curl`
if [ -z "$CURL" ]; then
echo "curl not found"
exit 1
fi