Skip to main content

Posts

Showing posts from August, 2024

MySQL connection in JAVA

 Below is the code for MySQL connection in JAVA import java.sql.Connection; import java.sql.DriverManager; public class TestMySql { public static Connection con = null; public static void main(String[] args) { try{   String host = "";         String uName = "";         String uPass= "";        Class.forName("com.mysql.cj.jdbc.Driver");   con = DriverManager.getConnection(host, uName, uPass); System.out.println("Connection Successful..."); con.close();   } catch(Exception e) {  System.out.println(e);}   } }

File Comparison in JAVA

Below is the code for File Comparison in JAVA:  import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;   public class CompareTextFiles {     public static void main(String[] args) throws IOException     {         BufferedReader reader1 = new BufferedReader(new FileReader("C:\\FileCompare\\File1.txt"));                   BufferedReader reader2 = new BufferedReader(new FileReader("C:\\FileCompare\\File2.txt"));                   String line1 = reader1.readLine();                   String line2 = reader2.readLine();                   boolean areEqual = true;                   int lineNum = 1;                   while (line1 != null || li...

Multiple files creation logic in JAVA

  import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class Multiple_Files_Creation_logic { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub int totalCount = 1001; //userinput String filePath = "C:\\Users\\vmalge\\Desktop\\Test"; String fileName= "V_SHPT_OFR_MDCTEST4_"; String fileType = "csv"; File input = new File(filePath+fileName+"."+fileType);                   for(int counter = 1; counter < totalCount; counter++){     //int size = 250;     //tbd     File myFile = new File("C:\\Test\\result"+fileName+counter+"."+fileType);                                try { ...

Email monitoring in JAVA

 Email monitoring in JAVA. Here is the code: import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Authenticator; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; public class MonitoringMail { //public static void sendMail(String mailServer, String from,String username, String password,String port, String[] to, String subject, String messageBody, String attachmentPath, String attachmentName) throws MessagingException, AddressException public void sendMail(String mailServer, String from, String[] to, String subject, String mess...

Excel operations in JAVA

 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCreationHelper; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFHyperlink; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelReader { public  String path; public  FileInputStream fis = null; public  FileOutputStream fi...

Generate Logs in Python

 Below is the code which will show how Logging is done in Python: import logging import time class Logger: def __init__ ( self , logger , file_level=logging.info): self .logger = logging.getLogger(logger) self .logger.setLevel(logging.INFO) """ self.logger.setLevel(logging.DEBUG) self.logger.setLevel(logging.INFO) self.logger.setLevel(logging.WARNING) self.logger.setLevel(logging.ERROR) self.logger.setLevel(logging.CRITICAL) """ fmt = logging.Formatter( '%(asctime)s - %(filename)s:[%(lineno)s] - [%(levelname)s] - %(message)s' ) curr_time = time.strftime( "%Y-%m-%d" ) self .LogFileName = '. \\ Logs \\ log' + curr_time + '.log' # "a" to append the logs in same file, "w" to generate new logs and delete old one fh = logging.FileHandler( self .LogFileName , mode = "a" ) fh.set...

Excel file operations in Python

 Below is the code which will demonstrate the Excel file operations in Python import openpyxl def getRowCount (path , sheetName): workbook = openpyxl.load_workbook(path) sheet = workbook[sheetName] return sheet.max_row def getColCount (path , sheetName): workbook = openpyxl.load_workbook(path) sheet = workbook[sheetName] return sheet.max_column def getCellData (path , sheetName , rowNum , colNum): workbook = openpyxl.load_workbook(path) sheet = workbook[sheetName] return sheet.cell( row =rowNum , column =colNum).value def setCellData (path , sheetName , rowNum , colNum , data): workbook = openpyxl.load_workbook(path) sheet = workbook[sheetName] sheet.cell( row =rowNum , column =colNum).value = data workbook.save(path) path = "..//excel//testdata.xlsx" sheetName = "LoginTest" rows = getRowCount(path , sheetName) cols = getColCount(path , sheetName) print (rows , "---" , cols) print (getCellData(path , shee...

Read Excel file data in Python

 Below is the code to Read Excel file data in Python import openpyxl def get_data (sheetName): workbook = openpyxl.load_workbook( "..//excel//testdata.xlsx" ) sheet = workbook[sheetName] totalrows = sheet.max_row totalcols = sheet.max_column mainList = [] for i in range ( 2 , totalrows + 1 ): dataList = [] for j in range ( 1 , totalcols + 1 ): data = sheet.cell( row =i , column =j).value dataList.insert(j , data) mainList.insert(i , dataList) return mainList

Configuration reader Utility in Python

Below is the code for  Configration reader Utility in Python from configparser import ConfigParser def readConfig (section , key): config = ConfigParser() config.read( ". \\ conf.ini" ) return config.get(section , key) # print(readConfig("locators","Login_btn_URL_XPATH"))                     and the configuration reader file is like this: [base URL] api_base_url=https://google.com [Json Request Body Path] # All API Request Body present here API_Request_Body_PATH=C:\\Request_body\\API

SFTP connection in Python

  import paramiko # create ssh client ssh_client = paramiko.SSHClient() # remote server credentials host = "host name here" username = "username here" password = "password here" port = PORT_NUMBER ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh_client.connect( hostname =host , port =port , username =username , password =password) print ( 'connection established successfully' ) ftp = ssh_client.open_sftp() files = ftp.listdir() print ( "Listing all the files and Directory: " , files) # close the connection ftp.close() ssh_client.close()

Files I/O operations in Python

Below is the code for  Files I/O operations in Python import os import sys sys.path.extend( r'mention your local path here' ) print ( 'Get current working directory : ' , os.getcwd()) print ( 'File name : ' , os.path.basename(__file__)) print ( 'Directory Name: ' , os.path.dirname(__file__)) print ( 'Absolute path of file: ' , os.path.abspath(__file__)) print ( 'Absolute directory name: ' , os.path.dirname(os.path.abspath(__file__))) # Specify the directory you want to list directory_path = os.getcwd() # List all files and folders in the directory file_list = os.listdir(directory_path) # Get full paths full_paths = [os.path.join(directory_path , file) for file in file_list] print (full_paths)

Web Scraping in Python

 Here is the code to show how to do Web Scraping in Python import requests from lxml import html from bs4 import BeautifulSoup import csv import pandas as pd from openpyxl import Workbook from openpyxl.utils import get_column_letter from openpyxl.utils.dataframe import dataframe_to_rows from openpyxl.worksheet.table import Table , TableStyleInfo from datetime import datetime page_number = 1 Baseurl = 'https://www.vesselfinder.com' urls = 'https://www.vesselfinder.com/vessels' header = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/39.0.2171.95 Safari/537.36' , } # Get HTML Content r = requests.get(urls , headers =header) soup = BeautifulSoup(r.content , 'html.parser' ) page_links = [] table = soup.find( 'table' , class_ = 'results' ) for row in table.tbody.find_all( 'tr' ): # Find all data for each column columns = row...

Web Scraping/Crawling in python using BeautifulSoup

 Below is the code in python to so Web Scraping/Crawling using BeautifulSoup and request library. First you need to install libraries using following commands: 1. pip install BeautifulSoup 2. pip install request Here is the code: import csv from datetime import datetime import random import time import requests import requests from lxml import html from bs4 import BeautifulSoup import pandas as pd import numpy as np counter = 0 d = {} Vessel_record = [] Vessel_info = [] urls = [] url = 'https://www.vesselfinder.com/vessels/details/' file_Imo = "C:\\Users\\vmalge\\PycharmProjects\\vesselInfoAutomation\\InputTestData\\IMO1.xlsx" df = pd.read_excel(file_Imo, index_col=None, na_values=['NA'], usecols="A") # print(df) for i, row in df.iterrows():     for j, column in row.items():         urls.append(url + str(column)) user_agent_list = [     'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.457...

Calculate Percentage return for all stocks/share/equity for a certain period - NSE/Indian stock market - Python

 Below is the code in python to Calculate Percentage return for all stocks/share/equity for a certain period - NSE/Indian stock market here is the code import pandas as pd import glob from csv import writer import csv # The data assigned to the list list_data = ['Stock_Symbol', 'Percentage_Return', 'Period'] # Pre-requisite - The CSV file should be manually closed before running this code. # First, open the old CSV file in append mode, hence mentioned as 'a' # Then, for the CSV file, create a file object with open('Result/All_Stocks_Anaysis.csv', 'a',           newline='') as f_object:     # Pass the CSV  file object to the writer() function     writer_object = writer(f_object)     # Result - a writer object     # Pass the data in the list as an argument into the writerow() function     writer_object.writerow(list_data)     # Close the file object     f_object.close() for file i...