Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python Source Codes , Modules , Anything For It.


  • Please log in to reply
5 replies to this topic

#1 AMAMH

AMAMH

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 28 July 2007 - 06:17 AM

Please I want to make a topic that have all Python modules , codes , tools and books or anything about Python
so anyone who want to publish anything about Python publish it here in this topic.

BC AdBot (Login to Remove)

 


#2 AMAMH

AMAMH
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 28 July 2007 - 06:22 AM

OK let's begin with this source code :
It's my first Python program
It is for converting between : Binary , Octal , Decimal , and Hexadecimal.

BaseConvert.py

#!/usr/bin/env python# Filename : BaseConvert.py# by : A.M (Ahmed Mahmoud) --> AMAMH3@Gamil.com#Remember: all functions' output is a string , u can pass any type to a function as# an argument it doesn't matter 'cause they can handle it ex: any function here can# handle a string even if it needs an integer####################################################################################OK ,just one more thing:                                                         ##there are some main ideas here that are stolen from a website , these ideas are: ##Dec to Bin main func , Bin to Hex main func , Table idea ,                       ##The main idea of the prog and it is about converting to Bin first and then to    ##the wanted base                                                                  ################################################################################################################# I hope u like the proggie ###########################import mathdef RemoveLeftZeroes(aValue):    Value = str(aValue)    VList = []    for i in range(0,len(Value)):        VList.append(Value[i])    for i in range(0,len(VList)):        if VList[i] == '0':            VList[i] = ''        else:            break    Value = ''.join(VList)    if Value == '':        Value = '0'    return Valuedef BinAdjust(aBinValue,aBase):    Adjustor = int(math.log(int(aBase) , 2))    Binary = RemoveLeftZeroes(aBinValue)    if len(str(Binary)) % Adjustor == 0:        return str(Binary)    else:        return '0'*(Adjustor - (len(str(Binary)) % Adjustor)) + str(Binary)def ToBinTable(aValue):    '''To be used only for Octal and Hexadecimal convert'''    Value = RemoveLeftZeroes(aValue)    if Value == '0':        return '0'    elif Value == '1':        return '1'    elif Value == '2':        return '10'    elif Value == '3':        return '11'    elif Value == '4':        return '100'    elif Value == '5':        return '101'    elif Value == '6':        return '110'    elif Value == '7':        return '111'    elif Value == '8':        return '1000'    elif Value == '9':        return '1001'    elif Value.capitalize() == 'A':        return '1010'    elif Value.capitalize() == 'B':        return '1011'    elif Value.capitalize() == 'C':        return '1100'    elif Value.capitalize() == 'D':        return '1101'    elif Value.capitalize() == 'E':        return '1110'    elif Value.capitalize() == 'F':        return '1111'def DecToBin(aDecValue):    STOP = 0    Binary = str()    Decimal = int(aDecValue)    (DIV , MOD) = (Decimal // 2 , Decimal % 2)    Binary = str(MOD)    if DIV == 0:        STOP = 1    while STOP != 1:        (DIV , MOD)= (DIV // 2 , DIV % 2)        if DIV == 0:            STOP = 1        Binary = str(MOD) + Binary    return BinAdjust(Binary , 16)    #I just used BinAdjust with base 8 to make the output looks nice not for another reasondef FromBinTable(aBinValue):    Binary = RemoveLeftZeroes(aBinValue)    if Binary == '0':        return '0'    elif Binary == '1':        return '1'    elif Binary == '10':        return '2'    elif Binary == '11':        return '3'    elif Binary == '100':        return '4'    elif Binary == '101':        return '5'    elif Binary == '110':        return '6'    elif Binary == '111':        return '7'    elif Binary == '1000':        return '8'    elif Binary == '1001':        return '9'    elif Binary == '1010':        return 'A'    elif Binary == '1011':        return 'B'    elif Binary == '1100':        return 'C'    elif Binary == '1101':        return 'D'    elif Binary == '1110':        return 'E'    elif Binary == '1111':        return 'F'def OctToBin(aOctValue):    Octal = RemoveLeftZeroes(aOctValue)    Binary = str()    for i in range(0,len(Octal)):        Binary = Binary + BinAdjust(ToBinTable(Octal[i]),8)    return BinAdjust(Binary , 16)def HexToBin(aHexValue):    Hex = RemoveLeftZeroes(aHexValue)    Binary = str()    for i in range(0,len(Hex)):        Binary = Binary + BinAdjust(ToBinTable(Hex[i]),16)    return BinAdjust(Binary , 16)def BinToOct(aBinValue):    Binary = BinAdjust(aBinValue,8)    Oct = str()    for i in range(0,len(Binary),3):        Oct = Oct + FromBinTable(Binary[i:i+3])    return RemoveLeftZeroes(Oct)def BinToDec(aBinValue):    if RemoveLeftZeroes(aBinValue) == '':        return 0    Binary = BinAdjust(aBinValue,4)    Dec = 0    for i in range(0,len(Binary),2):            Dec = ( int(FromBinTable(Binary[i:i+2]))*math.pow(4,(((len(Binary[i:])-2)/2))) )+ Dec    return str(int(Dec))def BinToHex(aBinValue):    Binary = BinAdjust(aBinValue,16)    Hex = str()    for i in range(0,len(Binary),4):        Hex = Hex + FromBinTable(Binary[i:i+4])    return RemoveLeftZeroes(Hex)def MainDialog():    Binary = raw_input('Enter a binary value -->')    print 'Decimal' , BinToDec(Binary)    print 'Octal' , BinToOct(Binary)    print 'Hexadecimal' , BinToHex(Binary)    Octal = raw_input('Enter an octal value -->')    print 'Binary' , OctToBin(Octal)    print 'Decimal' , BinToDec(OctToBin(Octal))    print 'Hexadecimal' , BinToHex(OctToBin(Octal))    Decimal = raw_input('Enter a decimal value -->')    print 'Binary' , DecToBin(Decimal)    print 'Octal' , BinToOct(DecToBin(Decimal))    print 'Hexadecimal' , BinToHex(DecToBin(Decimal))    Hex = raw_input('Enter a hexadecimal value -->')    print 'Binary' , HexToBin(Hex)    print 'Octal' , BinToOct(HexToBin(Hex))    print 'Decimal' , BinToDec(HexToBin(Hex))if __name__ == '__main__':    MainDialog()raw_input('Hit a Enter to exit') #don't use input , use raw_input

Edited by AMAMH, 28 July 2007 - 06:44 AM.


#3 AMAMH

AMAMH
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 28 July 2007 - 06:28 AM

Some python books :

If u r a newbie I recommend that you complete this first:
http://www.byteofpython.com

then download the book:

Learning Python second edition:

http://www.bestsharing.com/files/EPYScQ310....Python.7z.html

### :thumbsup: ###


Edited by AMAMH, 28 July 2007 - 04:20 PM.


#4 AMAMH

AMAMH
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 28 July 2007 - 06:53 AM

OK here is the next one :
This is a backup maker (in the format *.tar.bz2)

actually this prog I designed it to make backups of my Dungeon Siege 2 savedgame

so u have to change :
Source='D:\Documents and Settings\BlackDemon\My Documents\My Games\Dungeon Siege 2\Save\\'
to:
Source=The Full dir of the file or folder u want to backup

and change :
Target_Dir='E:\A.M\My Games\Dungeon Siege 2\%s\\'
to:
Target_Dir=The full dir of the folder where u will keep the backups

feature:
1 --> You can add ur comment.
2 --> The backup will be saved with the name of the current time and in a folder with a name of the current day.


The Code :
[codebox]#!/usr/bin/env python
#! Filename : DungeonSiege2BackupMaker_bz2.py
# by A.M (Ahmed Mahmoud) --> AMAMH3@Gmail.com

import tarfile,os,shutil,time
Comment=raw_input('Enter a comment -->')
MyTarFile=tarfile.open('backup.tar.bz2','w:bz2')
Source='D:\Documents and Settings\BlackDemon\My Documents\My Games\Dungeon Siege 2\Save\\'
MyTarFile.add(Source)
MyTarFile.close()
Today=time.strftime('%d%y%m')
Target_Dir='E:\A.M\My Games\Dungeon Siege 2\%s\\' % Today
if not os.path.exists(Target_Dir):
os.mkdir(Target_Dir)
Now=time.strftime('%H%M')
if Comment=='':
Target=Target_Dir+Now+'.tar.bz2'
else:
Target=Target_Dir+Now+'_'+Comment.replace(' ','_')+'.tar.bz2'
shutil.move('backup.tar.bz2',Target)
raw_input('Hit a Enter to exit')[/codebox]

Edited by AMAMH, 28 July 2007 - 04:20 PM.


#5 AMAMH

AMAMH
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 28 July 2007 - 06:58 AM

This is the same program but this makes a ***Zip*** backup

Remember: u have to modify it to the desired files to backup and the desired folder where the backup will be saved

The Code:

#!/usr/bin/env python# Filename : DungeonSiege2BackupMaker_Zip.py# by A.M (Ahmed Mahmoud) --> AMAMH3@Gmail.comimport zipfile,os,shutil,timeComment=raw_input('Enter a comment -->')FileNames1=os.listdir('D:\Documents and Settings\TD#\My Documents\My Games\Dungeon Siege 2\Save\\')i=0for FileName in FileNames1:    FileNames1[i]='D:\Documents and Settings\TD#\My Documents\My Games\Dungeon Siege 2\Save\\'+FileName    i=i+1i=0FileNames2=os.listdir('D:\Documents and Settings\TD#\My Documents\My Games\Dungeon Siege 2\Save\SinglePlayer\AM\\')for FileName in FileNames2:    FileNames2[i]='D:\Documents and Settings\TD#\My Documents\My Games\Dungeon Siege 2\Save\SinglePlayer\AM\\'+FileName    i=i+1FileNames=FileNames1+FileNames2MyZipFile=zipfile.ZipFile('backup.zip','w')for FileName in FileNames:    (Name,ext)=os.path.splitext(FileName)    if ext != '':        MyZipFile.write(FileName,FileName.replace('D:\Documents and Settings\TD#\My Documents\My Games\Dungeon Siege 2\Save\\',''),zipfile.ZIP_DEFLATED)MyZipFile.close()Today=time.strftime('%d%y%m')Target_Dir='E:\A.M\My Games\Dungeon Siege 2\%s\\' % Todayif not os.path.exists(Target_Dir):    os.mkdir(Target_Dir)Now=time.strftime('%H%M')if Comment=='':    Target=Target_Dir+Now+'.zip'else:    Target=Target_Dir+Now+'_'+Comment.replace(' ','_')+'.zip'shutil.move('backup.zip',Target)raw_input('Hit a key t exit')

Edited by AMAMH, 28 July 2007 - 04:22 PM.


#6 AMAMH

AMAMH
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:08:12 PM

Posted 31 July 2007 - 12:05 PM

BaseConvert.py after some improvements
#!/usr/bin/env python
# Filename : BaseConvert.py
# by : A.M (Ahmed Mahmoud) --> AMAMH3@Gamil.com

#Remember: all functions' output is a string , u can pass any type to a function as
# an argument it doesn't matter 'cause they can handle it ex: any function here can
# handle a string even if it needs an integer
###################################################################################
#OK ,just one more thing:														 #
#there are some main ideas here that are stolen from a website , these ideas are: #
#Dec to Bin main func , Bin to Hex main func , Table idea ,					   #
#The main idea of the prog and it is about converting to Bin first and then to	#
#the wanted base																  #
###################################################################################
############################# I hope u like the proggie ###########################
import math
def RemoveLeftZeroes(aValue):
	Value = str(aValue)
	VList = []
	for i in range(0,len(Value)):
		VList.append(Value[i])
	for i in range(0,len(VList)):
		if VList[i] == '0':
			VList[i] = ''
		else:
			break
	Value = ''.join(VList)
	if Value == '':
		Value = '0'
	return Value
def AppAdjust(aValue,aBase):	 #Appearance adjustor
	''' adjsuts the appearance of the given value , this is mainly used for binary values'''
	Adjustor = int(math.log(int(aBase) , 2))
	Value = RemoveLeftZeroes(aValue)
	if len(Value) % Adjustor == 0:
		return Value
	else:
		return '0'*(Adjustor - (len(Value) % Adjustor)) + Value
def ToBinTable(aValue):
	'''To be used only for octal and hexadecimal convert'''
	Value = RemoveLeftZeroes(aValue)
	if Value == '0':
		return '0'
	elif Value == '1':
		return '1'
	elif Value == '2':
		return '10'
	elif Value == '3':
		return '11'
	elif Value == '4':
		return '100'
	elif Value == '5':
		return '101'
	elif Value == '6':
		return '110'
	elif Value == '7':
		return '111'
	elif Value == '8':
		return '1000'
	elif Value == '9':
		return '1001'
	elif Value.capitalize() == 'A':
		return '1010'
	elif Value.capitalize() == 'B':
		return '1011'
	elif Value.capitalize() == 'C':
		return '1100'
	elif Value.capitalize() == 'D':
		return '1101'
	elif Value.capitalize() == 'E':
		return '1110'
	elif Value.capitalize() == 'F':
		return '1111'
def DecToBin(aDecValue):
	STOP = 0
	Binary = str()
	Decimal = int(aDecValue)
	(DIV , MOD) = (Decimal // 2 , Decimal % 2)
	Binary = str(MOD)
	if DIV == 0:
		STOP = 1
	while STOP != 1:
		(DIV , MOD)= (DIV // 2 , DIV % 2)
		if DIV == 0:
			STOP = 1
		Binary = str(MOD) + Binary
	return AppAdjust(Binary , 16)	#I just used BinAdjust with base 8 to make
									 #the output looks nice not for another reason
def FromBinTable(aBinValue):
	Binary = RemoveLeftZeroes(aBinValue)
	if Binary == '0':
		return '0'
	elif Binary == '1':
		return '1'
	elif Binary == '10':
		return '2'
	elif Binary == '11':
		return '3'
	elif Binary == '100':
		return '4'
	elif Binary == '101':
		return '5'
	elif Binary == '110':
		return '6'
	elif Binary == '111':
		return '7'
	elif Binary == '1000':
		return '8'
	elif Binary == '1001':
		return '9'
	elif Binary == '1010':
		return 'A'
	elif Binary == '1011':
		return 'B'
	elif Binary == '1100':
		return 'C'
	elif Binary == '1101':
		return 'D'
	elif Binary == '1110':
		return 'E'
	elif Binary == '1111':
		return 'F'
def OctToBin(aOctValue):
	Octal = RemoveLeftZeroes(aOctValue)
	Binary = str()
	for i in range(0,len(Octal)):
		Binary = Binary + AppAdjust(ToBinTable(Octal[i]),8)
	return Binary
def HexToBin(aHexValue):
	Hex = RemoveLeftZeroes(aHexValue)
	Binary = str()
	for i in range(0,len(Hex)):
		Binary = Binary + AppAdjust(ToBinTable(Hex[i]),16)
	return Binary
def BinToOct(aBinValue):
	Binary = AppAdjust(aBinValue,8)
	Oct = str()
	for i in range(0,len(Binary),3):
		Oct = Oct + FromBinTable(Binary[i:i+3])
	return RemoveLeftZeroes(Oct)
def BinToDec(aBinValue):
	Binary = AppAdjust(aBinValue,4)
	Dec = 0.0
	for i in range(0,len(Binary),2):
			Dec = ( int(FromBinTable(Binary[i:i+2]))*math.pow(4,(((len(Binary[i:])-2)/2))) )+ Dec
	return str(int(Dec))
def BinToHex(aBinValue):
	Binary = AppAdjust(aBinValue,16)
	Hex = str()
	for i in range(0,len(Binary),4):
		Hex = Hex + FromBinTable(Binary[i:i+4])
	return RemoveLeftZeroes(Hex)
def MainDialog():
	Binary = raw_input('Enter a binary value -->')
	print 'Decimal' , BinToDec(Binary)
	print 'Octal' , BinToOct(Binary)
	print 'Hexadecimal' , BinToHex(Binary)
	Octal = raw_input('Enter an octal value -->')
	print 'Binary' , OctToBin(Octal)
	print 'Decimal' , BinToDec(OctToBin(Octal))
	print 'Hexadecimal' , BinToHex(OctToBin(Octal))
	Decimal = raw_input('Enter a decimal value -->')
	print 'Binary' , DecToBin(Decimal)
	print 'Octal' , BinToOct(DecToBin(Decimal))
	print 'Hexadecimal' , BinToHex(DecToBin(Decimal))
	Hex = raw_input('Enter a hexadecimal value -->')
	print 'Binary' , HexToBin(Hex)
	print 'Octal' , BinToOct(HexToBin(Hex))
	print 'Decimal' , BinToDec(HexToBin(Hex))
if __name__ == '__main__':
	MainDialog()
raw_input('Hit Enter to exit') #don't use input , use raw_input





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users