Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 3rd, 2005, 11:19 AM   #1
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Cool Code Libary

Ok, i said i had made a code libary, and i have.. so here it is..
The program will make all the directories when loaded..
All you need to do, is add your codes!! so whenever a nice snippet comes up on here, just copy it in!!
The program allows, search, remove, add, and also a DocToPy.. converts all txt's, doc's and 'dats, that have been added to the directory, C:\Code Libary < then given 'keyword' folder..
Enjoi.
import os
def DocToPy():
	for dir in os.listdir("C:\Code Libary"):
		for file in os.listdir(os.path.join("C:\Code Libary", dir)):
			if file.endswith(".dat") or file.endswith(".txt") or file.endswith(".doc"):
				if file.endswith(".txt"):
					oldfile = file.strip("txt")
					newfile = oldfile + 'py'
				elif file.endswith(".doc"):
					oldfile = file.strip("doc")
					newfile = oldfile + 'py'
				elif file.endswith(".dat"):
					oldfile = file.stip("dat")
					newfile = oldfile + 'py'
				print newfile
				os.rename(os.path.join("C:\Code Libary", dir, file), os.path.join("C:\Code Libary", dir, newfile))
	Main()
		
def Remove():
	num = 1
	code = []
	for dir in os.listdir("C:\Code Libary"):
		for file in os.listdir(os.path.join("C:\Code Libary", dir)):
			print num, file, 'from keyword' ,dir
			num += 1
			code.append(os.path.join("C:\Code Libary", dir, file))
	if len(code) >= 1:
		a = input("Enter Number Of Code To Delete: ")
		a -= 1
		print code[a]
		b = raw_input("Are you sure you wish to delete (Y/N): ")
		if b.lower() == "y":
			os.remove(code[a])
		else:
			print "Not Removed"
			Main()
	if len(code) <= 0:
		print "No Codes To Remove"
		Main()
	Main()
def Search(keyword):
	progs = []
	num = 1
	print
	for dir in os.listdir("C:\Code Libary"):
		if dir == keyword:
			path = os.path.join("C:\Code Libary", dir)
			for file in os.listdir(path):
				print num, file
				doc = os.path.join(path, file)
				progs.append(doc)
				num += 1
	read_which = input("\nEnter File Number To Read: ")
	read_which -= 1
	text = open(progs[read_which],'r').read()
	print '\nNow Printing Python File\n\n\n' ,text
	Main()
def Add():
	print "These Are A Choice Of Key Word Folders"
	for dir in os.listdir("C:\Code Libary"):
		print dir
	a = raw_input("Use One Of These Keyword's or create your own? (current/new) (C/N): ")
	if a.lower() == "c":
		key = raw_input("Enter Keyword: ")
		if key in os.listdir("C:\Code Libary"):
			title = raw_input("Enter Code Title: ")
			title = title.lower()
			title = title + '.py'
			if title not in os.path.join("C:\Code Libary", key):
				content = raw_input("Paste Code Content\n: ")
				code = open(os.path.join("C:\Code Libary", key, title),'w')
				code.write(content)
				code.close()
			else:
				print "Code Already Exists"
				Add()
		if key not in os.listdir("C:\Code Libary"):
			print "Keyword not listed"
			print "hint create your own"
			Add()
	if a.lower() == "n":
		newdir = raw_input("Enter new keyword: ")
		newdir = newdir.lower()
		os.mkdir("C:\Code Libary\%s")% newdir
		Add()
	Main()
	
								
def Main():
	s = input("""
1 = Search For Code
2 = Add Code
3 = Remove Code
4 = Turn All Files In To Python Files
>> """)
	if s == 1:
		keyword = raw_input(""""
Enter A Key Word To Search
example 'web' 'gaming' generators'
>> """)
		Search(keyword)
	if s == 2:
		Add()
	if s == 3:
		Remove()
	if s == 4:
		DocToPy()
 

#Used For When First Run
#Makes 'keyword' folders for searching
#Will NOT replace delete folders
if "C:\Code Libary" not in os.listdir("C:\\"):
	try:
		os.mkdir("C:\Code Libary")
		os.mkdir("C:\Code Libary\\web")
		os.mkdir("C:\Code Libary\\applications")
		os.mkdir("C:\Code Libary\\gaming")
		os.mkdir("C:\Code Libary\\snippets")
		os.mkdir("C:\Code Libary\\networking")
	except:
		IOError
	Main()
__________________
while me is alive:
	make(life,simple)

Last edited by SaturN; Jul 3rd, 2005 at 11:32 AM. Reason: slight error in code :D solved though enjoy
SaturN is offline   Reply With Quote
Old Jul 3rd, 2005, 5:04 PM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

A keyword would be something that appears in the code itself, and can appear in a number of code snippets. Something like zip, eval, endswith, random.sample, round, split, yield and so on. What you have done in your valiant effort is to categorize to folders, that would make searching for a keyword like 'sys.stdin.read' still quite difficult. Of course you can search for keyword and category. Well, it's a start!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jul 3rd, 2005, 6:04 PM   #3
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
ok, gimme 15 mins, and i'll make it return all codes for which the given word appears..
the user presses a number like before, and there you go, all code will be stored in one folder! etc etc.. BUT, i am not allowing print, raw_input, include, input, etc etc to be searched!! that's just a joke!! lol
__________________
while me is alive:
	make(life,simple)
SaturN is offline   Reply With Quote
Old Jul 3rd, 2005, 6:31 PM   #4
SaturN
Programmer
 
Join Date: Apr 2005
Location: Uk
Posts: 68
Rep Power: 4 SaturN is on a distinguished road
Talking Code Libary

ok here we go!!!
should all work, if you find any bugs, lemme know
import os
def DocToPy():
	for dir in os.listdir("C:\Code Libary"):
		for file in os.listdir(os.path.join("C:\Code Libary", dir)):
			if file.endswith(".dat") or file.endswith(".txt") or file.endswith(".doc"):
				if file.endswith(".txt"):
					oldfile = file.strip("txt")
					newfile = oldfile + 'py'
				elif file.endswith(".doc"):
					oldfile = file.strip("doc")
					newfile = oldfile + 'py'
				elif file.endswith(".dat"):
					oldfile = file.stip("dat")
					newfile = oldfile + 'py'
				print newfile
				os.rename(os.path.join("C:\Code Libary", dir, file), os.path.join("C:\Code Libary", dir, newfile))
	Main()
		
def Remove():
	num = 1
	code = []
	for dir in os.listdir("C:\Code Libary"):
		for file in os.listdir(os.path.join("C:\Code Libary", dir)):
			print num, file, 'from keyword' ,dir
			num += 1
			code.append(os.path.join("C:\Code Libary", dir, file))
	if len(code) >= 1:
		a = input("Enter Number Of Code To Delete: ")
		a -= 1
		print code[a]
		b = raw_input("Are you sure you wish to delete (Y/N): ")
		if b.lower() == "y":
			os.remove(code[a])
		else:
			print "Not Removed"
			Main()
	if len(code) <= 0:
		print "No Codes To Remove"
		Main()
	Main()
def Search(keyword):
	progs = []
	num = 1
	print
	for dir in os.listdir("C:\Code Libary"):
		if dir == keyword:
			path = os.path.join("C:\Code Libary", dir)
			for file in os.listdir(path):
				print num, file
				doc = os.path.join(path, file)
				progs.append(doc)
				num += 1
	read_which = input("\nEnter File Number To Read: ")
	read_which -= 1
	text = open(progs[read_which],'r').read()
	print '\nNow Printing Python File\n\n\n' ,text
	Main()
def Add():
	print "These Are A Choice Of Key Word Folders"
	for dir in os.listdir("C:\Code Libary"):
		print dir
	a = raw_input("Use One Of These Keyword's or create your own? (current/new) (C/N): ")
	if a.lower() == "c":
		key = raw_input("Enter Keyword: ")
		if key in os.listdir("C:\Code Libary"):
			title = raw_input("Enter Code Title: ")
			title = title.lower()
			title = title + '.py'
			if title not in os.path.join("C:\Code Libary", key):
				content = raw_input("""Paste Code Content\n:
""")
				code = open(os.path.join("C:\Code Libary", key, title),'w')
				code.writelines(content)
				code.close()
			else:
				print "Code Already Exists"
				Add()
		if key not in os.listdir("C:\Code Libary"):
			print "Keyword not listed"
			print "hint create your own"
			Add()
	if a.lower() == "n":
		newdir = raw_input("Enter new keyword: ")
		newdir = newdir.lower()
		os.mkdir("C:\Code Libary\%s")% newdir
		Add()
	Main()
	
								
def Main():
	s = input("""
1 = Search For Code
2 = Add Code
3 = Remove Code
4 = Turn All Files In To Python Files
>> """)
	if s == 1:
		keyword = raw_input(""""
Enter A Key Word To Search
example 'web' 'gaming' generators'
>> """)
		Search(keyword)
	if s == 2:
		Add()
	if s == 3:
		Remove()
	if s == 4:
		DocToPy()
 

#Used For When First Run
#Makes 'keyword' folders for searching
#Will NOT replace delete folders
if "C:\Code Libary" not in os.listdir("C:\\"):
	try:
		os.mkdir("C:\Code Libary")
	except:
		IOError
	Main()

Thanks
__________________
while me is alive:
	make(life,simple)

Last edited by SaturN; Jul 3rd, 2005 at 6:34 PM. Reason: too many folders to be created
SaturN is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC