Skip to content

Instantly share code, notes, and snippets.

@vinovator
vinovator / CombinePDF.py
Created June 26, 2015 08:17
This code is used to combine 2 separate pdf files and create a single pdf file with combined content. The user needs to input the names of 2 pdf source files and the name of the destination file. The 2 source files need to be present in the folder specified in the code.
#Python 3
#CombinePDF.py
#Gets inputs of 2 PDF file names from user and combines them into 1
import PyPDF2
import os
def getFileNameFromUser (file):
pdf_file_name = input("Enter {0} name: ".format(file))
if pdf_file_name in os.listdir():
@vinovator
vinovator / DueReminder.py
Created June 29, 2015 07:11
Scenario: You maintain a membership list with due paid information maintained in an excel file. This program will automatically read the due payment informamtion and store the due pending list in a notepad file. This program can be further extended to include send text mail and send sms.
#python 3
#Read the list of names and email ids from an excel file and populate them in a text file
import openpyxl
import os
import time
def fetchUnpaidMembers(max_row, max_col, sheet):
member_count = 0
mydict = {}
@vinovator
vinovator / FileManipulation.py
Last active August 29, 2015 14:25
Basic methods and best practices for file manipulation
# Python 3.4
import os
# Windows uses backward slash; Mac & Linux uses forward slash
# But python will always recognise forward slash
# Python recognises everything as a sequence of unicode characters (string)
# Computer manages directories as a sequence of bytes (byte stream)\
# To read or write unicode characters from byte stream, encoding is required
# Different OS use different methods for encoding
@vinovator
vinovator / shopper.py
Last active August 29, 2015 14:25
Open multiple shopping sites for one search parameter
# python 3
# shopper.py
'''
This is a very simple and effective program.
Open multiple shopping sites for one search parameter at one go
Search for a product term as a command prompt argument
Multiple words separated by space is fine
e.g. shooper.py iphone 6
'''
@vinovator
vinovator / RestfulPostClient.py
Last active October 2, 2015 14:04
A sample restful client for POST operation - assumes digest authentication
# RestfulPostClient.py
# Python 2.7.6
import requests
from requests.auth import HTTPDigestAuth
# import json # Json module is not required as we are directly passing json to requests
# Replace with the correct URL
url = "http://api_url"
@vinovator
vinovator / getHttpHeader.py
Last active October 2, 2015 14:24
Get the request header and response header from a http request-response sequence. Assumes that the url accepts digest authentication
# getHttpHeader.py
# Python 2.7.6
import requests
from requests.auth import HTTPDigestAuth
import getpass # To mask the password typed in
# Replace with the correct URL
url = "http://some_url"
@vinovator
vinovator / fileExplorer.py
Last active October 2, 2015 19:44
Loop through a folder path and extract all files and sub-folders. Get count of files by extension.
# fileExplorer.py
# python 2.7.6
import os
# defaultdict is used to have keys created if it doesn't exist or appended it if exists
from collections import defaultdict
folder_count = 0
file_count = 0
loop_count = 0
@vinovator
vinovator / portScanner.py
Created October 8, 2015 15:39
Simple Python socket program to scan TCP ports
# python 2.7.6.
# portScanner.py
import socket
from datetime import datetime
import sys
# Here we are scanning your own terminal
# Replace this with gethostbyname("host") to scan a remote host
@vinovator
vinovator / timeZoneExplorer.py
Last active October 9, 2015 20:39
Simple query to fetch all common time zones and their current time
# Python 2.7.6
# timeZoneExplorer.py
from pytz import timezone, common_timezones # import all_timezones for more exhaustive list
from datetime import datetime
import os
# Log file will be created in the same folder as the python script
my_path = "."
log_path = os.path.join(my_path + "/" + "loc_log.txt")
@vinovator
vinovator / jsonToCsv2.py
Last active November 3, 2015 13:59
Scans a JSON file and extracts the key value pairs to CSV
# jsonToCSV.py
# Python 2.7.6
'''
Place all the json payloads as separate text files in base folder
Program will extract each payload and generate single csv file
csv file will have key value pairs in separate columns
'''
import json