Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making code more efficient and effective

Reply
Thread Tools

Making code more efficient and effective

 
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      06-26-2008
I've written up a little piece of code that isn't that foolproof to
scan through a file (java presently) to find functions and then look
for them throughout the document and output the name of the function,
followed by how many times it appears and the lines it appears on.

What I was looking for was ways to improve, simplfy and beautify the
code. More to aid my learning of Python than produce a perfect way to
scan for this (netbeans was annoying me...which is why I did this)

Anyway, the source code:

from __future__ import with_statement

functions = dict()
func_words = ("private", "public", "protected")
ignore_class = " class "

def get_func_name(line):
for word in func_words:
if word in line: break
else: return None
# set it to ignore the func_word and the space after
line = line[len(word) + 1:]
index = line.find("(")
if index != -1:
func_name = ""
for letter in reversed(line[:index]):
if letter == " ": break
func_name += letter
return ''.join(reversed(func_name))
else: return None

with open(r"C:\example.java", "r") as test_file:
for number, line in enumerate(test_file):
line = line.strip()
if line.startswith(func_words) and line.find(ignore_class ) ==
-1:
func_name = get_func_name(line);
if func_name is not None:
functions.setdefault(func_name, []).append(number)

test_file.seek(0)
for number, line in enumerate(test_file):
for key in functions.iterkeys():
if line.find(key) != -1:
functions[key].append(number)

print "\n".join("Function: %s, found on %d line(s); these being
%s"
% (k, len(v), v) for k, v in
functions.iteritems())
 
Reply With Quote
 
 
 
 
Cédric Lucantis
Guest
Posts: n/a
 
      06-26-2008
Le Thursday 26 June 2008 14:11:35 http://www.velocityreviews.com/forums/(E-Mail Removed), vous avez écrit*:
> I've written up a little piece of code that isn't that foolproof to
> scan through a file (java presently) to find functions and then look
> for them throughout the document and output the name of the function,
> followed by how many times it appears and the lines it appears on.
>
> What I was looking for was ways to improve, simplfy and beautify the
> code. More to aid my learning of Python than produce a perfect way to
> scan for this (netbeans was annoying me...which is why I did this)
>
> Anyway, the source code:


I would use some regexp instead (assuming that the function prototype is on a
single line and that the return type of the function is a single identifier,
I don't remember if it's always the case in Java)

PAT = re.compile('^[ ]*(public|protected|private)[ ]+([a-zA-Z0-9_]+)
[ ]+([a-zA-Z0-9_]+)[ ]+\((.*)\).*$')

for line in source_file :
if PAT.match(line) :
func_vis = PAT.sub(r'\1', line)
func_type = PAT.sub(r'\2', line)
func_name = PAT.sub(r'\3', line)
func_args = PAT.sub(r'\4', line)
print "'%s' -> '%s' '%s' '%s' '%s'" % (line, func_vis, func_type,
func_name, func_args)

It might be hard to read but will avoid a lot of obscure parsing code. I can't
tell if it makes the code more efficient but you don't care about that unless
you're parsing several million lines of code.

--
Cédric Lucantis
 
Reply With Quote
 
 
 
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      06-26-2008
Cédric Lucantis:
> PAT = re.compile('^[ ]*(public|protected|private)[ ]+([a-zA-Z0-9_]+)
> [ ]+([a-zA-Z0-9_]+)[ ]+\((.*)\).*$')
> ...
> It might be hard to read but will avoid a lot of obscure parsing code.


You can use the VERBOSE mode, to add comments and split that RE into
some lines.

I think the RE module of Python 3.0 can be modified in some way to
encourage people to write more readable REs, but I don't know how.
Maybe the VERBOSE can be on by default...

Bye,
bearophile
 
Reply With Quote
 
cokofreedom@gmail.com
Guest
Posts: n/a
 
      06-26-2008
On Jun 26, 5:42 pm, (E-Mail Removed) wrote:
> Cédric Lucantis:
>
> > PAT = re.compile('^[ ]*(public|protected|private)[ ]+([a-zA-Z0-9_]+)
> > [ ]+([a-zA-Z0-9_]+)[ ]+\((.*)\).*$')
> > ...
> > It might be hard to read but will avoid a lot of obscure parsing code.

>
> You can use the VERBOSE mode, to add comments and split that RE into
> some lines.
>
> I think the RE module of Python 3.0 can be modified in some way to
> encourage people to write more readable REs, but I don't know how.
> Maybe the VERBOSE can be on by default...
>
> Bye,
> bearophile


Thank you to everyone that replied in the thread and privately, been
looking into the different techniques. Presently found the RE to be
readable once I put it into VERBOSE format. I had looked it at earlier
but struck a dead wall, till Cédric gave such clear code!

I've been doing Java code the last few weeks and it is hard to jump
between them sometimes, Java has to be so organised and checked over
and over, I forget Python doesn't need such defensive programming.

I currently have it finding functions, constructors and classes, and
looking for them within the code, which is exactly what I was hoping
for! It also reads a lot clearer, which was what I was aiming for. So
thanks to everyone that helped!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with making my code more efficient Larry.Martell@gmail.com Python 22 12-24-2012 05:57 PM
Writing more efficient code gonzlobo Python 13 01-03-2007 06:27 AM
More effective C++ vikram_p_nayak@yahoo.com C++ 15 06-17-2006 11:31 AM
When is std::list more effective than the other containers? Josh Mcfarlane C++ 44 12-16-2005 04:40 PM
more effective c++ item 31 cfchou@gmail.com C++ 4 09-27-2005 09:15 AM



Advertisments