Python.Templates.Functions.Wrappers #py #python #Python #wrapper #function #template #readycode #ready
PYTHON_SCRIPT_WRAPPERS
# ##########################################################################
# NAME: DrivesList.py
#
# # ------------------------------------------------------------------------
# AUTHOR: P.Doulgeridis
#
# # ------------------------------------------------------------------------
# PACKAGE: Utils
#
# # ------------------------------------------------------------------------
# LOCATION: C:\Users\p.doulgeridis\Desktop\CODES\4. PYTHON\SystemManip\DrivesManip\DrivesList.py
#
# # ------------------------------------------------------------------------
# INPUT: None
#
# # ------------------------------------------------------------------------
# OUTPUT: None
#
# # ------------------------------------------------------------------------
# USAGE: python DrivesList.py
#
#
# # ------------------------------------------------------------------------
# FUNCTION: Parses drives installed on the machine
# and their respective names, mappign them.
#
# # ------------------------------------------------------------------------
# NOTES:
# Drives that the user does not have
# permission to see, or drives that for
# some reason cannot be reached, are
# termed unreachable.
#
# # ------------------------------------------------------------------------
# REVISION HISTORY:
#
# # ------------------------------------------------------------------------
# TO DO: Split into dif utils:
# 1. DrivesListtoFile
# 2. Named / Unnamed
# 3. Reach/Unreach
# 4. Check if drive is plugged in
# 5. Check if drive is reachable
# 6. Check if drive is named
#
#
# ############################################################################
'''
Author:
Name:
Function:
Input:
Output:
Usage:
Notes:
'''
ie:
'''
Function: filexists
Description: Checks for existence of file
Input: filepath (or raw string of it)
Output: Boolean
Usage: if filexists(file_in):...
Notes: Depending on system may need to
conver to raw string with r'file_in.
'''
1. chk_args_num : Checks number of arguments
2. chk_args_type : Checks type of arguments
3. script_param : Prints script parameters
4. script_args : Parses supplied arguments to script
5. path_of_script : Returns the path to the script (Full)
6. Fullwrapper : Full wrapper for script
7. Script-Timer : Time the execution of a script
8. CodeBlock-Timer : Time the execution of a code block
9. PromptExample : Prompt Function with syntax control
10. Function-Timer : Time the execution of a function
11. Func Documentation Templ : Function Documentation Template
12. Script Documentation Template
13. Autolog Function : To replace print - Includes line.no and caller.
"""
Created on Tue Jan 31 12:45:00 2017
@author: P.Doulgeridis
"""
# necessary module to work with excel
#import openpyxl
# documentation: http://openpyxl.readthedocs.org/.
import sys
import os
import timeit
from timeit import Timer
def costly_func():
return map(lambda x: x^2, range(10))
def more_costly_func(y):
lista = []
for x in range(0, int(y)):
lista.append(x)
#print (lista)
return lista
t = Timer(lambda: more_costly_func(11150))
print(str(timeit.timeit(costly_func)))
print(t.timeit(number=200))
# print(str(timeit.timeit(more_costly_func(10))))
# print(str(timeit.timeit(more_costly_func(20))))
# print(str(timeit.timeit(more_costly_func(30))))
# print(str(timeit.timeit(more_costly_func(40))))
# print(str(timeit.timeit(more_costly_func(50))))
def timefunc(func, func_input, exec_times):
from timeit import Timer
t = Timer(lambda: func(func_input))
return t.timeit(number=exec_times)
print("##")
print(timefunc(more_costly_func, 211150, 300))
def script_params(string):
'''
Function: script_params(string)
Description: Prints all the basic script parameters
Input: sys.argv[0]
Output: STDOUT print
Called as: script_params(sys.argv[0])
Notes: Always returns true
'''
import time
import sys
print ('Script name : ' + filename)
print ('Script directory : ' + sys.path[0] )
print ('Script started at: ' + time.strftime("%c"))
#print (sys.path)
print ('Processing')
# Called as :
script_params(sys.argv[0])
## Version 2:
def script_path_param(string1, vocal = 'yes'):
'''
Name:
Function:
Input:
Output:
Usage:
Notes:
'''
#import sys
#import os
# Called as: script_path_param(sys.argv[0])
try:
script_name = os.path.basename(string1)
script_dir = os.path.dirname(os.path.realpath(string1))
script_full = string1
except:
print("Error loading script parameters. Possible problem with os module")
if vocal != 'yes':
return ( script_name, script_dir, script_full )
else:
print("Script name: " + str(script_name))
print("Script directory: " + str(script_dir))
print("Script full: " + str(script_full))
return True
############################################
# Syntax
from datetime import datetime
starttime = datetime.now()
#....... script
print ("Elapsed duration: " + str(datetime.now() - starttime))
#############################################
# Example:
from datetime import datetime
starttime = datetime.now()
print ("@" + "\n" + "Script started at " + script_time_param() + "\n")
print ("Script parameters: ")
print (str(script_path_param(sys.argv[0])[0]))
print (str(script_path_param(sys.argv[0])[1]))
print (str(script_path_param(sys.argv[0])[2]))
print ("\n")
choice = inputscreenmenu()
#print (choice)
if choice == '1':
source = input("Enter binary string for conversion: ")
target = bintoascii(source)
#print (source)
#print (target)
#print ("Specific alphanumeric digits are stored in List:Target")
print ("Equivalint alphanumeric: " + str((target)[0]))
print ("Equivalent alphanumeric in list form: " + str((target)[1]))
elif choice == '2':
source = input("Enter alphanumeric string for conversion: ")
target = to_binary(source)
target2 = string2bits(source)
#print ("Specific binary digits are stored in List:Target")
print ("Equivalint binary: " + str(target))
print ("Equivalent binary in list form: " + str(target2))
print ("\n"+ "@\n" + "Script ended at " + script_time_param())
print ("Elapsed duration: " + str(datetime.now() - starttime))
def inputSomething(prompt, errorMessage = 'Atleast one character must be used'):
while True:
# Assign Input
value = input(prompt)
# Remove special characters from begin / end of word
Response = value.strip()
# Normalize case (lower)
fixedRes = Response.lower()
# Check for presence of whitespace or special characters
# in beginning or end
status = len(fixedRes) - len(fixedRes.strip())
if fixedRes.islower() and status == 0:
print ('Valid Input')
return fixedRes
else:
print(errorMessage)
something = inputSomething('Enter str: ')
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 12 13:43:25 2017
@author: panagos
"""
import timeit
import time
import functools
def timef( funcname, *args, **kwargs ):
""" timeit a func with args, e.g.
for window in ( 3, 31, 63, 127, 255 ):
timef( "filter", window, 0 )
This doesn't work in ipython;
see Martelli, "ipython plays weird tricks with __main__" in Stackoverflow
"""
argstr = ", ".join([ "%r" % a for a in args]) if args else ""
kwargstr = ", ".join([ "%s=%r" % (k,v) for k,v in kwargs.items()]) \
if kwargs else ""
comma = ", " if (argstr and kwargstr) else ""
fargs = "%s(%s%s%s)" % (funcname, argstr, comma, kwargstr)
# print "test timef:", fargs
t = timeit.Timer( fargs, "from __main__ import %s" % funcname )
ntime = 3
print ("%.0f usec %s" % (t.timeit( ntime ) * 1e6 / ntime, fargs))
# Works, precise, shows parameters of internal function.
def timing_val(func):
def wrapper(*arg, **kw):
'''source: http://www.daniweb.com/code/snippet368.html'''
t1 = time.time()
res = func(*arg, **kw)
t2 = time.time()
print ('%r (%r, %r) %0.9f sec' % \
(func.__name__, arg, kw, t1-t2))
return (t2 - t1), res
return wrapper
# Works for functions with no return value
# Works for functions with return value
# Time in ms
def timeit2(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
startTime = time.time()
func(*args, **kwargs)
elapsedTime = time.time() - startTime
print('function [{}] finished in {} ms'.format(
func.__name__, int(elapsedTime * 1000)))
return func(*args, **kwargs)
return newfunc
# Works, and also properly assigns return value
# Less precise
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print ('%s function took %0.9f ms' % (f.__name__, (time2-time1)*1000.0))
return ret
return wrap
# Works, and also properly assigns return value
def timeit3(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
print ('%r (%r, %r) %0.9f sec' % \
(method.__name__, args, kw, te-ts))
return result
return timed
#@timeit3
#@timing_val
#@timeit2
#@timing
def looper(x):
lista = []
for j in range(x):
lista.append(j)
#@timeit3
#@timing_val
#@timeit2
#@timing
def looper2(x):
lista = []
for j in range(x):
lista.append(j)
return lista
#timef("looper", 10)
looper(10011)
looper2(30009)
#timef("looper2", 30009)
b = looper2(15000)
#print(str(b))
#print (looper(100)[0])
def chck_args_type(string):
'''
Function : chck_args_type(string)
Description: Checks the content of parameters
Input: sys.argv
Output: STDOUT print
Called as chck_args_type(sys.argv)
'''
for i in range(len(string)):
if i == 0:
print "Function name: %s" % sys.argv[0]
else:
print "%d. argument: %s" % (i,sys.argv[i])
print ("\n")
return 0
# Called as : chck_args_type(sys.argv)
# Parse arguments
def script_args():
'''
Function: script_args()
Description: Prints a report on script parameters
Input: None
Output: STDOUT, True
Usage: script_args()
'''
print 'Argument Sector: '
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv), "\n"
# Script args can be called as :
# sys.argv[0] = script name
# sys.argv[1] = ... and so on
return True
import os
import sys
def get_script_path():
return os.path.dirname(os.path.realpath(sys.argv[0]))
print (get_script_path())
def script_path_param(string1):
# PUT IT INTO MODULE
#import sys
#import os
script_name = os.path.basename(string1)
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
script_full = script_dir + script_name
return ( script_name, script_dir, script_full )
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 22 08:48:37 2017
@author: P.Doulgeridis
"""
#####################################################################################
#
# FILE QUERY V1
#
# Function : Queries a file in a specific substring for a value
#
# Usage : python FILEQUERY.PY <input> <output> <substart> <subend>
#
# Notes:
#
# When we have to match a specific string, we should c/p it from the original
# file into this script, on the give nline. Always check encoding of files to
# be processed.
#
# python FILEQUERY.py ENDXMV_20170309.TXT TESTFORPYOUT2.TXT 445 2 > OEOEOEO2.TXT
# #
######################################################################################
#######################################
# LIBRARIES
import os
import sys
import time
#######################################
# FUNCTIONS
def script_path_param(string1):
# PUT IT INTO MODULE
#import sys
#import os
script_name = os.path.basename(string1)
script_dir = os.path.dirname(os.path.realpath(string1))
script_full = string1
return ( script_name, script_dir, script_full )
def script_time_param():
# Name: time parameter
# Function: script_time_param
# Input: None
# Output: string with formatted time
# Usage: print (script_time_param) or a = script_time_param
return time.strftime("%c")
def usage(string1):
print ("\n" + "Name: " + str(os.path.basename(string1)))
print ("Call as: " + str(os.path.basename(string1)) + " <FILE_IN> <FILE_OT> <SUB_START> <SUB_LENGTH> \n")
return True
def file_abs_path(file):
'''
Function : file_abs_path
Description : Returns absolute path to file
'''
import os
return os.path.abspath(str(file))
def filetolist(file):
'''
Function: filetolist
Description: Reads a file, stores in list
Input: File
Output: List
Usage: print (filetolist("C:\\Users\\p.doulgeridis\\Desktop\\testpy.txt"))
Notes: Path needs double \\ or reverse /
'''
file_in = str(file)
lines = list(open(file_in, 'r'))
return lines
def file_size_mb(filePath):
'''
Function:
Description:
Input:
Output:
'''
return float(os.path.getsize(filePath)) / (1024 * 1024)
def count_file(file):
chars = words = lines = 0
with open(str(file), 'r') as in_file:
for line in in_file:
lines += 1
words += len(line.split())
chars += len(line)
return (lines, words, chars)
#######################################
# CONSTANTS
# Expected n. of Arguments
cor_args_nbr = 3
#script_usage = usage()
#######################################
# SCRIPT PATHS
# Manual way to get paths - requires modules (sys, os)
script_name = os.path.basename(sys.argv[0])
script_dir = os.path.dirname(sys.argv[0])
script_full = str(sys.argv[0])
#print (script_name)
#print (script_dir)
#print (script_full)
# With Function use
a = (script_path_param(sys.argv[0])[1])
b = (script_time_param())
c = usage(sys.argv[0])
#print (a)
#print (b)
#print (usage(sys.argv[0]))
#######################################
# GLOBAL VARIABLES
log_file = r"C:\Users\p.doulgeridis\Desktop\CODES\CENTRAL_LOG.txt"
file_in = sys.argv[1]
file_ot = sys.argv[2]
sub_start = sys.argv[3]
sub_lend = int(sys.argv[3]) + int(sys.argv[4])
#print(file_in)
#print(file_abs_path(file_in))
#print(file_ot)
#print(sub_start)
#print(sub_lend)
#######################################
# SCRIPT PARAMETERS
print ("Input File: " + file_in )
print ("Input File - Absolute path " + file_abs_path(file_in))
print ("Output File: " + file_ot )
print ("Start Digit: " + str(sub_start) )
print ("Length: " + str(sys.argv[4]) )
print ("Logfile: " + log_file + "\n" )
#######################################
# SCRIPT START
print ("Script started at " + script_time_param() + "\n" )
# DO WORK
print("Reading input file .....\n")
b = filetolist(file_in)
out = open(file_ot, 'w')
count = 0
for j in b:
count += 1
check = str(j[int(sub_start):int(sub_lend)])
# IMPORTANT - N01 : HERE WE ENTER THE STRING MATCH
# TAKE THE STRING WITH C/P FROM THE ORIGINAL FILE AND COPY PASTE IT HERE
# IF MORE STRING MATCHES ARE REQUIRED, CHANGE THE IF STATEMENT
if check == "Ι7":
out.write(j)
#print(check)
out.close
# DO WORK 2
count_in = count_file(file_in)
count_ot = count_file(file_ot)
# REPORTING
print ("@@@@")
print ("File input : " + file_in )
print ("Size: " + str(file_size_mb(file_in)) + " MB")
print ("Linecount: " + str(count) )
print ("Word count: " + str(count_in[1]))
print ("Character count: " + str(count_in[2]))
print ("@")
print ("File output: " + file_ot )
print ("Size: " + str(file_size_mb(file_ot)) + " MB")
print ("Line count: " + str(count_ot[0]))
print ("Word count: " + str(count_ot[1]))
print ("Character count: " + str(count_ot[2]))
# LOGGING
log = open(log_file, 'a')
log.write("Script: " + script_name + " run at: " + script_time_param() + "\n" )
log.close
##########################################
# SCRIPT END
print ("\n\nScript ended at " + script_time_param() + "\n" )
def chck_args_num(var):
'''
Function : chck_args_num(var)
Description: Checks n. of variables
Input: Integer
Output: Boolean
Called as : chk_args(len(sys.argv))
'''
import sys
args_correct = 3
args_in = var
args_in_fixed = args_in - 1
if args_in_fixed != args_correct:
print ('Wrong number of arguments : ' + str(args_in_fixed))
print ('Must be : ' + str(args_correct))
return False
else:
print ('Correct number of arguments provided: ' + str(args_in_fixed) + "\n" )
return True
# Called as :
# Check n. of arguments or terminate
if not chck_args_num(len(sys.argv)):
print ('Terminating Script....')
sys.exit()
def script_path_param_adv(string1, vocal = 'yes'):
'''
Name: script_path_param
Function: script_path_param(string1, vocal = 'yes')
Input: script_path_param(sys.argv[0])
Output: [ T | F ]
Usage: Precaches basic script parameters
Notes: None
'''
#import sys
#import os
#Called as: script_path_param(sys.argv[0])
try:
script_name = os.path.basename(string1)
script_dir = os.path.dirname(os.path.realpath(string1))
script_full = script_dir + script_name
except:
print("Error loading script parameters. Possible problem with os module")
if vocal != 'yes':
return ( script_name, script_dir, script_full )
else:
print("Script name: " + str(script_name))
print("Script directory: " + str(script_dir))
print("Script full: " + str(script_full))
print ('Script started at: ' + time.strftime("%c"))
return True
def script_path_param(string1):
# PUT IT INTO MODULE
#import sys
import os
script_name = os.path.basename(string1)
script_dir = os.path.dirname(os.path.realpath(string1))
script_full = string1
return ( script_name, script_dir, script_full )
def script_time_param():
# Name: time parameter
# Function: script_time_param
# Input: None
# Output: string with formatted time
# Usage: print (script_time_param) or a = script_time_param
return time.strftime("%c")
# file exists
def fileexists(filepath):
'''
Function: filexists
Description: Checks for existence of file
Input: filepath (or raw string of it)
Output: Boolean
Usage: if filexists(file_in):...
Notes: Depending on system may need to
conver to raw string with r'file_in.
'''
import os.path
if os.path.exists(filepath):
return True
else:
return False
def makedir(output_dir):
print("\n")
print("Creating output directory")
if not os.path.exists(output_dir):
print("Output directory does not exist. Attempting to create...")
try:
os.makedirs(output_dir)
except:
print("Could not create directory: " + output_dir)
sys.exit(6)
else:
print("Created: " + str(output_dir))
else:
print("Directory: " + output_dir + " already exists. Skipping creation.")
def pretty_print(input, indent_depth):
'''
Name: pretty_print
Description: pretty_prints a data structure
Input: <List/Tuple/Dict>, indent_depth
Output: stdout
Usage: pretty_print(dict_in, 4)
Notes: Works on any kind of data structure.
Requires: pprint module
'''
import pprint
try:
pprint.pprint(input)
#pprint.PrettyPrinter(indent=indent_depth)
except:
print("Pretty print failed")
def pplist(list_in):
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(list_in)
def pplist(list_in, ind_lvl):
import pprint
pp = pprint.PrettyPrinter(indent=int(ind_lvl))
pp.pprint(list_in)
def pretty_print(input, indent_depth, compact = False):
'''
Name: pretty_print
Description: pretty_prints a data structure
Input: <List/Tuple/Dict>, indent_depth
Output: stdout
Usage: pretty_print(dict_in, 4, T/F)
Notes: Works on any kind of data structure.
Requires: pprint module
'''
import pprint
try:
pp = pprint.PrettyPrinter(indent=indent_depth, compact=compact)
pp.pprint(input)
#pprint.PrettyPrinter(indent=indent_depth)
except:
print("Pretty print failed")
def autolog(message):
"""
Function: autolog()
Description: Autologs - Prints to stdout with caller and line.no
Input: Message <string>
Output: Message will be appended to the caller\line.no string
Usage: autolog("This is a test message from autolog")
Notes: Get the previous frame in the stack, otherwise it would be this function!!!
"""
import inspect, logging
func = inspect.currentframe().f_back.f_code
#print(func)
#print(inspect.getframeinfo(f_code))
#print(inspect.stack())
#print(inspect.trace())
# Dump the message + the name of this function to the log.
print("[[Script: %s]]:[Method: %s]: Line:%i -> : %s" % (
func.co_filename,
func.co_name,
func.co_firstlineno,
message
))
def autolog(message):
"""
Function: autolog()
Description: Autologs - Prints to stdout with caller and line.no
Input: Message <string>
Output: Message will be appended to the caller\line.no string
Usage: autolog("This is a test message from autolog")
Notes: Get the previous frame in the stack, otherwise it would be this function!!!
"""
import inspect, logging
func = inspect.currentframe().f_back.f_code
#print(func)
#print(inspect.getframeinfo(f_code))
#print(inspect.stack())
#print(inspect.trace())
# Dump the message + the name of this function to the log.
print("[[Script: %s]]:[Method: %s]: Line:%i -> : %s" % (
func.co_filename,
func.co_name,
func.co_firstlineno,
message
))
def autologfile(message, filename):
"""
Function: autolog()
Description: Autologs - Prints to stdout with caller and line.no
Input: Message <string>
Output: Message will be appended to the caller\line.no string
Usage: autolog("This is a test message from autolog")
Notes: Get the previous frame in the stack, otherwise it would be this function!!!
"""
import inspect, logging
func = inspect.currentframe().f_back.f_code
#print(func)
#print(inspect.getframeinfo(f_code))
#print(inspect.stack())
#print(inspect.trace())
# Dump the message + the name of this function to the log.
with open(filename, 'a') as f:
f.write("[[Script: %s]]:[Method: %s]: Line:%i -> : %s" % (
func.co_filename,
func.co_name,
func.co_firstlineno,
message
))
import sys
if sys.version_info[0] < 3:
raise Exception("Must be using Python 3")
import platform
print(platform.python_version())
import sys
if sys.hexversion >= 0x3000000:
print('Python 3.x hexversion %s is in use.' % hex(sys.hexversion))
def check_installation(rv):
current_version = sys.version_info
if current_version[0] == rv[0] and current_version[1] >= rv[1]:
pass
else:
sys.stderr.write( "[%s] - Error: Your Python interpreter must be %d.%d or greater (within major version %d)\n" % (sys.argv[0], rv[0], rv[1], rv[0]) )
sys.exit(-1)
return 0
from sys import version_info
if version_info[0] < 3:
from __future__ import print_function
import sys
import warnings
def checkVersion():
# Checking Python version:
expect_major = 2
expect_minor = 7
expect_rev = 14
if sys.version_info[:3] != (expect_major, expect_minor, expect_rev):
print("INFO: Script developed and tested with Python " + str(expect_major) + "." + str(expect_minor) + "." + str(expect_rev))
current_version = str(sys.version_info[0])+"."+str(sys.version_info[1])+"."+str(sys.version_info[2])
if sys.version_info[:2] != (expect_major, expect_minor):
warnings.warn("Current Python version was unexpected: Python " + current_version)
else:
print(" Current version is different: Python " + current_version)