473,791 Members | 3,071 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Script Discussion & Critique

Is there a forum where one could post a Python script and have it
critiqued by others?

Something like: Y would be more efficent if you did it this way, or
doing it that way could cause problems with X, etc.

Thanks!!!

Jul 18 '05 #1
12 1962
here will do

On Wed, 27 Aug 2003 18:33:21 -0400, hokiegal99 <ho********@hot mail.com> wrote:
Is there a forum where one could post a Python script and have it
critiqued by others?

Something like: Y would be more efficent if you did it this way, or
doing it that way could cause problems with X, etc.

Thanks!!!


Jul 18 '05 #2
derek / nul wrote:
here will do


OK, I have a few questions about the script at the end of this message:

Is this script written well? Is it a good design? How could it be made
better (i.e. write to a file exactly what the scipt did)? I hope to use
it to recursively find a replace strings in all types of files (binary
and text).

Thanks for any ideas, pointers or critique... most of the ideas behind
the script came from the list.
#Thanks to comp.lang.pytho n
import os, string
print " "
print "************** *************** *************** **********"
print " Three Easy Steps to a Recursive find and Replace "
print "************** *************** *************** **********"
print " "
x = raw_input("1. Enter the string that you'd like to find: ")
print " "
y = raw_input("2. What would you like to replace %s with: " %x)
print " "
setpath = raw_input("3. Enter the path where the prgroam should run: ")
print " "
for root, dirs, files in os.walk(setpath ):
fname = files
for fname in files:
inputFile = file(os.path.jo in(root,fname), 'r')
data = inputFile.read( )
inputFile.close ()
search = string.find(dat a, x)
if search >=1:
data = data.replace(x, y)
outputFile = file(os.path.jo in(root,fname), 'w')
outputFile.writ e(data)
outputFile.clos e()
print "Replacing" , x, "with", y, "in", fname
print " "
print "********** "
print " Done "
print "********** "
print " "

Jul 18 '05 #3
Something odd that I've discovered about this script is that it won't
replace strings at the far left side of a text file *unless* the string
has one or more spaces before it. For example:

THIS (won't be replace with THAT)
THIS (will be replaced with THAT)
THIS (will be replaced with THAT)
THIS (will be replaced with THAT)

#Thanks to comp.lang.pytho n
import os, string
print " "
print "************** *************** *************** **********"
print " Three Easy Steps to a Recursive find and Replace "
print "************** *************** *************** **********"
print " "
x = raw_input("1. Enter the string that you'd like to find: ")
print " "
y = raw_input("2. What would you like to replace %s with: " %x)
print " "
setpath = raw_input("3. Enter the path where the prgroam should run: ")
print " "
for root, dirs, files in os.walk(setpath ):
fname = files
for fname in files:
inputFile = file(os.path.jo in(root,fname), 'r')
data = inputFile.read( )
inputFile.close ()
search = string.find(dat a, x)
if search >=1:
data = data.replace(x, y)
outputFile = file(os.path.jo in(root,fname), 'w')
outputFile.writ e(data)
outputFile.clos e()
print "Replacing" , x, "with", y, "in", fname
print " "
print "********** "
print " Done "
print "********** "
print " "

Jul 18 '05 #4
Looks good, there is one small bug and a couple very minor things that
basically boil down to style and personal preference.

-----------------------------------------------------------

import os # no need for string

# this can just go in one print with triple quotes
print """
*************** *************** *************** *********
Three Easy Steps to a Recursive find and Replace
*************** *************** *************** *********
"""

x = raw_input("1. Enter the string that you'd like to find: ")

# if you just want a newline, you can just do a print by itself
print

y = raw_input("2. What would you like to replace %s with: " %x)
print

setpath = raw_input("3. Enter the path where the prgroam should run: ")
print

for root, dirs, files in os.walk(setpath ):
fname = files # what's this for? fname will just get reassigned on
# the next line.
for fname in files:
inputFile = file(os.path.jo in(root,fname), 'r')
data = inputFile.read( )
inputFile.close ()
search = data.find(x) # .find is a method of str, you don't need
# the string module for it.

# bug
#if search >=1: # this won't find it at the beginning of the file.

if search >= 0: # change to this (find returns -1 when it finds
# nothing)
data = data.replace(x, y)
outputFile = file(os.path.jo in(root,fname), 'w')
outputFile.writ e(data)
outputFile.clos e()
print "Replacing" , x, "with", y, "in", fname
# You could also do something like this to print out the centered
# messages:

print "*"*10
print "Done".center(1 0)
print "*"*10

-----------------------------------------------------------

HTH,
--
m a c k s t a n n mack @ incise.org http://incise.org
Dentist, n.:
A Prestidigitator who, putting metal in one's mouth, pulls
coins out of one's pockets.
-- Ambrose Bierce, "The Devil's Dictionary"

Jul 18 '05 #5

"hokiegal99 " <ho********@hot mail.com> wrote in message
news:3F******** ******@hotmail. com...
Is this script written well? Is it a good design? How could it be made better (i.e. write to a file exactly what the scipt did)? I hope to use it to recursively find a replace strings in all types of files (binary and text).
My main answer is your answer to the following: does it operate
correctly? does it produce output within the bounds of what you
consider acceptible? In particular, does it pass your test case(s)?
and are your test case(s) reasonably representative of the domain of
application?
Thanks for any ideas, pointers or critique... most of the ideas behind the script came from the list.
#Thanks to comp.lang.pytho n
import os, string
print " "
print "************** *************** *************** **********"
print " Three Easy Steps to a Recursive find and Replace "
print "************** *************** *************** **********"
I would lazily print one multiline string.
print " "
x = raw_input("1. Enter the string that you'd like to find: ")
I would delete the print and add '\n' to the front of the prompt
print " "
y = raw_input("2. What would you like to replace %s with: " %x)
print " "
setpath = raw_input("3. Enter the path where the prgroam should run: ") print " "
for root, dirs, files in os.walk(setpath ):
fname = files
for fname in files:
inputFile = file(os.path.jo in(root,fname), 'r')
data = inputFile.read( )
inputFile.close ()
search = string.find(dat a, x)
if search >=1:
data = data.replace(x, y)
outputFile = file(os.path.jo in(root,fname), 'w')
outputFile.writ e(data)
outputFile.clos e()
print "Replacing" , x, "with", y, "in", fname
print " "
print "********** "
print " Done "
print "********** "


Terry J. Reedy
Jul 18 '05 #6
On Wed, Aug 27, 2003 at 08:54:06PM -0400, hokiegal99 wrote:
Something odd that I've discovered about this script is that it won't
replace strings at the far left side of a text file *unless* the string
has one or more spaces before it. For example:

THIS (won't be replace with THAT)
THIS (will be replaced with THAT)
THIS (will be replaced with THAT)
THIS (will be replaced with THAT)


See my reply :)

--
m a c k s t a n n mack @ incise.org http://incise.org
The church is near but the road is icy; the bar is far away but I will
walk carefully.
-- Russian Proverb

Jul 18 '05 #7
Terry Reedy wrote:
My main answer is your answer to the following: does it operate
correctly? does it produce output within the bounds of what you
consider acceptible? In particular, does it pass your test case(s)?
and are your test case(s) reasonably representative of the domain of
application?


Well it works now, mackstan pointed out a flaw in my logic that caused
the script to miss stings if they occured at the very front of a file.
Thanks!!!


Jul 18 '05 #8
hokiegal99 wrote:
derek / nul wrote:
here will do

OK, I have a few questions about the script at the end of this message:

Is this script written well? Is it a good design? How could it be made
better (i.e. write to a file exactly what the scipt did)? I hope to use
it to recursively find a replace strings in all types of files (binary
and text).

Thanks for any ideas, pointers or critique... most of the ideas behind
the script came from the list.

Err... Is this my version of Python having troubles, or could it be
possible that nobody actually took time to *test* that script ?
There is a real problem on line 15: for root, dirs, files in os.walk(setpath ):


on Python 2.2.2, here is what I get :

[laotseu@localho st dev]$ python rfp.py
(snip)
Traceback (most recent call last):
File "rfp.py", line 15, in ?
for root, dirs, files in os.walk(setpath ):
AttributeError: 'module' object has no attribute 'walk'
[3]+ Done emacs testrfp
[laotseu@localho st dev]$
Of course, 'walk' is in os.path, not in os.

Python 2.2.2 (#2, Feb 5 2003, 10:40:08)
[GCC 3.2.1 (Mandrake Linux 9.1 3.2.1-5mdk)] on linux-i386
Type "help", "copyright" , "credits" or "license" for more information.
import os
os.walk Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'walk'

Now a another problem on the same line : for root, dirs, files in os.path.walk(se tpath):

.... print root, dirs, files
....
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: walk() takes exactly 3 arguments (1 given)

Of course, this is not the syntax nor the way to use os.path.walk

I checked the os module for a function with similar syntax and usage,
and could not find one. os.listdir would have been a candidate, but it
does not recurse, and do not return a (root, dirs, files) tuple but a
list of filenames.

So what ?

Bruno

Jul 18 '05 #9
Bruno Desthuilliers wrote:
Err... Is this my version of Python having troubles, or could it be
possible that nobody actually took time to *test* that script ?


Python 2.3 (#1, Jul 30 2003, 11:19:43)
[GCC 3.2] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
import os
dir(os.walk) ['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute __', '__hash__', '__init__', '__module__', '__name__',
'__new__', '__reduce__', '__reduce_ex__' , '__repr__', '__setattr__',
'__str__', 'func_closure', 'func_code', 'func_defaults' , 'func_dict',
'func_doc', 'func_globals', 'func_name']


It's time to upgrade :-)

Peter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1449
by: Anonymous | last post by:
Hi I dont know how to program Javascript, but I am a web designer. I a looking for an easy-to-use script (for a beginner) which i can use t stop users from accessing my website using what I call (i dont know i ive got the right term) "Back Doors": so that users cannot access m pages without going through a pwd protected "front entrance". e.g. my website is www.johnsmith.com and it has password protection o the index.html file, to get to...
37
2114
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those on comp.programming, this may be off topic, but I've posted there because the critique was part of a discussion in that group.
19
2555
by: TC | last post by:
Are there any good sites or forums for a web critique? I went to alt.html.critique and it's pretty dead.
188
7257
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
3
1451
by: sklett | last post by:
I need to add extensive validation and interaction client scripting to a web form. I've done some initial searches for "asp.net and client scripting" and I've found a couple articles that show methods like "register script block" and what not. None of these methods will do what I need, I need to add onChange, onClick, etc events to my controls. For a simple example: How would I pop an alert when a user clicks on a server textbox...
4
2752
by: johkar | last post by:
The below script sets and maintains the equal heights of 2 (or 3) divs which are floated next to each other on a page. I want to ensure that I have not overlooked anything and it will not error. In other words, it either executes successfully or not at all. This script is a compilation of code found on various posts along with my personalization thrown in to meet our specs. Note that this is just a "nice to have" script so if it...
3
5195
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
2
1183
by: arty | last post by:
hello i have no problem with this code: var z=new Array() z="<span style ='color:red;'>yeah</span>" document.getElementById("aa").innerHTML = z; it ouputs everything and also with the style. but if i do:
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5431
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.