[Python] General Class
Last Tested in Python Version: 3.1.2
This is a class that I use in most of my scripts. It has handy functions. It has yet to be fully documented, I will hopefully take care of that when I have time.
Please feel free to post functions you'd like to add here, I'll gladly add them to the class.
Changelog:
This is a class that I use in most of my scripts. It has handy functions. It has yet to be fully documented, I will hopefully take care of that when I have time.
Please feel free to post functions you'd like to add here, I'll gladly add them to the class.
#!/usr/bin/env python
#####################
#Author: Riddle
#Website: www.logicalgamers.com
#E-mail: NONE
#Version: 0.8.2
#####################
#Collection of useful and general functions
import hashlib
import sys
import re
import os
import time
import random
class General:
def __init__(self):
pass
def GetBetween(self, content, start, end):
"""Returns '' on no match"""
r = content.split(start)
if len(r) > 1:
r = r[1].split(end)
return r[0]
return ''
def md5Digest(self, Str1):
a = hashlib.md5() #Create new MD5 object
a.update(Str1.encode('utf-8')) #Prepares password string to be digested
return a.hexdigest() #Returns the final outcome
def Log_Error(self, Data, File_Name = 'Error Log.txt', Mode = 'a'):
Target = open(os.getcwd()+self.sep()+File_Name, Mode)
Target.write(Data)
Target.close()
def Paused_Exit(self):
a = input("\nPress Enter To Exit...")
sys.exit(1)
def Str_To_File(self, str1, path, mode = "a"):
f = open(path, mode)
f.write(str1)
f.close()
def List_To_File(self, lst1, path, mode = "a", line_break = False): #Line Break adds a \n after each line
f = open(path,mode)
if line_break == False:
f.writelines(lst1)
else:
for line in lst1:
f.write(line+"\n")
f.close()
def File_To_List(self, path):
f = open(path, "r")
lines = f.readlines()
new_list = []
for line in lines:
new_list.append(line.strip())
f.close()
return new_list
def File_To_Str(self, path):
f = open(path, "r")
str1 = f.read()
f.close()
return str1
def De_Dupe_List(self, lst, case_sensitive = False):
if case_sensitive == False:
for item in lst:
for citem in lst:
if item != citem:
if item.lower() == citem.lower():
lst.remove(citem)
for item in lst:
lst.remove(item)
if not item in lst:
lst.append(item)
return lst
def Find_All(self, pattern, strToSearch):
#Find all matching strings with a Reg Ex.
pattern = re.compile(pattern)
return re.findall(pattern, strToSearch)
def Dir_Exists(self,path):
"Returns True if it exists"
return os.path.isdir(path)
def File_Exists(self,path):
"Returns True if the file exists"
return os.path.isfile(path)
def Cur_Dir(self):
"Returns Current Directory"
return os.getcwd()
def mkdir(self,path):
"Makes Directory on Path"
os.mkdir(path)
def sep(self):
return os.path.sep
def strip_ml_tags(self, in_text):
"""Description: Removes all HTML/XML-like tags from the input text.
Inputs: s --> string of text
Outputs: text string without the tags
>>> test_text = "Keep this Text <remove><me /> KEEP </remove> 123"
>>> strip_ml_tags(test_text)
'Keep this Text KEEP 123'
"""
# Routine by Micah D. Cochran
# Submitted on 26 Aug 2005
# This routine is allowed to be put under any license Open Source (GPL, BSD, LGPL, etc.) License
# or any Propriety License. Effectively this routine is in public domain. Please attribute where appropriate.
# convert in_text to a mutable object (e.g. list)
s_list = list(in_text)
i,j=0,0
while i < len(s_list):
# iterate until a left-angle bracket is found
if s_list[i] == '<':
while s_list[i] != '>':
# pop everything from the the left-angle bracket until the right-angle bracket
s_list.pop(i)
# pops the right-angle bracket, too
s_list.pop(i)
else:
i=i+1
# convert the list back into text
join_char=''
return join_char.join(s_list)
def GetInputValue(self, contentStr, nameStr, typeStr = "hidden"):
"""
GetInputValue(contentStr, nameStr, typeStr = "hidden")
Gets the value of an input (from HTML4 Forms).
-> contentStr: The HTML source to search for the input in.
-> typeStr: By default it looks for hidden inputs.
-> nameStr: Name of the input.
Returns:
-> String: Value when found
-> None: None when fails. Raises Exception.
-> Bool: False when any of the given arguments is not a string
"""
if contentStr.__class__ == str and typeStr.__class__ == str and nameStr.__class__ == str:
try:
return self.GetBetween(contentStr, "<input type="+typeStr+" name='"+nameStr+
"' value='", "'>")
except:
pass
try:
return self.GetBetween(contentStr, "<input type="+typeStr+" name=\""+nameStr+
"\" value=\"", "\">")
except:
pass
try:
return self.GetBetween(contentStr, "<input type='"+typeStr+"' name=\""+nameStr+
"\" value=\"", "\">")
except:
pass
try:
return self.GetBetween(contentStr, "<input type=\""+typeStr+"\" name=\""+nameStr+
"\" value=\"", "\">")
except:
pass
try:
return self.GetBetween(contentStr, "<input type='"+typeStr+"' name='"+nameStr+
"' value='", "'>")
except:
pass
try:
return self.GetBetween(contentStr, "<input type=\""+typeStr+"\" name='"+nameStr+
"' value='", "'>")
except:
#EXCEPTION HANDLING HERE!!
return None
return False
def Sleep(self, secs):
time.sleep(secs)
return
def RandFloat(self,low, high):
return random.uniform(low,high)
[/i][/i][/0][/1]Changelog:
- 6/30/2010: Added GetInputValue() function. Added version doc. in the classes.
- 7/4/2010: Version 0.8.2; Revamped GetInputValue(). Added Sleep(secs) and RandFloat(low,high).

Thought id let everyone know
.*)</test>, lines_in_file) nothing happens. /fail at python