A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[Python] General Class

1.8k views · started by Riddle ·
#1
[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.

#!/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).
#2
Updated, see changelog.
#3
Updated, see changelog.
#4
Wouldn't it have been far more efficient to use regular expressions in your GetInputValue method? Seems like a lot of unnecessary code and guess work on your part :p
#5
No one on LG seems to understand that regex is very useful and processor happy. As in it doesnt take up as much cpu/ram as something you made yourself. Thought it still wont take much up most likely.

No one liked that i used regex in one of my programs. They were like why dont you just use getbetween 50 times? >o
#6
GamerForce wrote:
No one on LG seems to understand that regex is very useful and processor happy. As in it doesnt take up as much cpu/ram as something you made yourself. Thought it still wont take much up most likely.

No one liked that i used regex in one of my programs. They were like why dont you just use getbetween 50 times? >o


Son, the day you release something on LG again is the day people will use Regex.
#7
Lol. I finished it. Just havnt released it. It was my account checker. It works. Im just not happy with it. It could be done more efficiently.
#8
Yes, and no.
That was just something I had lying around from ancient times, so I just copied pasted. It worked for w/e I was using it for. :)
#9
Just tested this in 2.7 and it works just fine so far :D Thought id let everyone know
#10
Only problem im having in 2.7 is the GetInputValue function. Always returns false..
#11
Anyone have a getbetweenall function?
#12
Should just use regular expressions.
#13
MattSmith wrote:
Only problem im having in 2.7 is the GetInputValue function. Always returns false..


Don't use that. I used it for a specific purpose. Use regular expressions.
#14
Riddle wrote:
Don't use that. I used it for a specific purpose. Use regular expressions.


When I try print self.General.Find_All("<test>;(.*)</test>, lines_in_file) nothing happens. /fail at python
#15
Chad wrote:
When I try print self.General.Find_All("<test>;(.*)</test>, lines_in_file) nothing happens. /fail at python

try:

self.General = General.General()
lines_in_file = "fdhnjskafhdskafs<test>THIS IS WIN?</test>j89syafe89Y&* 7*Y F78y&*FY f"
self.General.Find_All("<test>(.*)</test>", lines_in_file)



havnt tested it, but it should work.
#16
Print out whatever results from the function call. Post what you get.
#17
I fixed it. I wasn't calling the master thing so it wasn't executing any of the coding. It does return as ['value to grab']. Anyway to remove the brackets and single quotes or do I just have to use the replace method or do I just slap it into an array?

EDIT: Just slapped it into an array and it worked perfectly fine, thanks guys.
#18
It should return as a list.
print results[0][/0]
#19
Artificial wrote:
It should return as a list.
print results[0][/0]


Yea that's what I did before you posted and it worked.
#20
Oh XD Ive been removing the brackets and shit. Didnt know that alex. Thanks lmao
#21
If you check out the function, it returns a list. In python terms, a list is basically an array. Every instance that matches your pattern will be in an array. You guys should read about lists and how to use them DiveIntoPython has a good basis of lists and how to use them.
#22
Ive been using dictionarys for EVERY FUCKING THING, they are amazing. You can store functions and call them and crap from it. So god damn awesome!
#23
Lists in Pythons are like arrays on steroids. You can do everything you can with regular arrays in other programming languages, plus so much more!
#24
^ Deserves to be in a signature quote.

On topic; I've been having a problem making a towns collector in python grabbing all the trash in the gsi page. It's displayed as so
0:t0192995, 1:t010958, 2:039580

and so on. When I grab it using self.General.Find_All(": (.*),", self.Wrapper.GET("URL and stuff")) it returns it all in one array just like whats in the code tags, instead of actually splitting it. Any idea?
....................................................................... ^ Minus the space.
#25
Your pattern is too general. You probably want to try and make it a little more specific. For instance, just tested and it grabbed them all:

import re

search = "0:t0192995, 1:t010958, 2:039580"
matches = re.findall( "\d+\:([a-z]{0,1}\d+)", search)
for eachMatch in matches:
print "Match: " + eachMatch


Why does 0-1 have a t prior to the number, whereas 2 doesn't?
#26
Artificial wrote:
Your pattern is too general. You probably want to try and make it a little more specific. For instance, just tested and it grabbed them all:

import re

search = "0:t0192995, 1:t010958, 2:039580"
matches = re.findall( "[0-9]{0,}\:([t]{0,}[0-9]{0,})", search)
for eachMatch in matches:
print "Match: " + eachMatch[/t]


Why does 0-1 have a t prior to the number, whereas 2 doesn't?


I forgot to put it in. I was just typing random numbers as an example. t stands for trash.
#27
MattSmith wrote:
Ive been using dictionarys for EVERY FUCKING THING, they are amazing. You can store functions and call them and crap from it. So god damn awesome!


You can do the same with lists too. There's plenty of goodies that make difficult tasks on most programming languages, insanely easy to perform.
#28
Riddle wrote:
You can do the same with lists too. There's plenty of goodies that make difficult tasks on most programming languages, insanely easy to perform.
Only thing i dont like so far about python is, your forced to give up sources and the syntax isnt the greatest. Reminds me of vb6, a language for children.
#29
MattSmith wrote:
Only thing i dont like so far about python is, your forced to give up sources and the syntax isnt the greatest. Reminds me of vb6, a language for children.


It's disappointing Python doesn't have switches from what I've researched.
#30
MattSmith wrote:
Only thing i dont like so far about python is, your forced to give up sources and the syntax isnt the greatest. Reminds me of vb6, a language for children.


You can use py2exe if you really need to compile. You can also use the .pyc files that are compiled when you run your scripts, however, they can be de-compiled (if it's an old source, not sure about 2.4+). I don't like parts a bits of the syntax, but as a whole, it isn't too bad.

Chad wrote:
It's disappointing Python doesn't have switches from what I've researched.


See this: Why doesn't Python have a switch statement? - Stack Overflow
There are a few reasons provided why Python doesn't use switch statements. In my opinion, they're not needed at all. You can use work arounds if you really want to use switch statements:
Readable switch construction without lambdas or dictionaries Python recipes ActiveState Code
Exception-based Switch-Case Python recipes ActiveState Code
Using a Dictionary in place of a 'switch' statement Python recipes ActiveState Code

Then again, I suggest you just use if statements.
#31
Riddle wrote:
You can use py2exe if you really need to compile. You can also use the .pyc files that are compiled when you run your scripts, however, they can be de-compiled (if it's an old source, not sure about 2.4+). I don't like parts a bits of the syntax, but as a whole, it isn't too bad.



See this: Why doesn't Python have a switch statement? - Stack Overflow
There are a few reasons provided why Python doesn't use switch statements. In my opinion, they're not needed at all. You can use work arounds if you really want to use switch statements:
Readable switch construction without lambdas or dictionaries Python recipes ActiveState Code
Exception-based Switch-Case Python recipes ActiveState Code
Using a Dictionary in place of a 'switch' statement Python recipes ActiveState Code

Then again, I suggest you just use if statements.

Yea, there are many creative work arounds in python to do anything you want. Its very incredible what you can do with such a simple language. Im loving it<3