472,325 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 software developers and data experts.

Making code more efficient and effective

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())
Jun 27 '08 #1
3 1435
Le Thursday 26 June 2008 14:11:35 co*********@gmail.com, 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 ona
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
Jun 27 '08 #2
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
Jun 27 '08 #3
On Jun 26, 5:42 pm, bearophileH...@lycos.com 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!
Jun 27 '08 #4

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

Similar topics

7
by: TTA | last post by:
I'm an intermediate C++ programmer. My skills are good but they need to be fine-tuned and my software methodologies in theory are good but I need...
11
by: Wilsoch | last post by:
Long story short: My Access developer is letting me down. He doesn't really know VB and he can't figure out how to do what I need. Situation:...
19
by: Materialised | last post by:
Hi everyone, What I am wanting to do, is to copy, a simple plain text file, to another file, but omitting duplicate items. The way I thought...
4
by: AH | last post by:
Hi, I need to print some simple barcode label for my stock control program (VB .NET) , it just needed to print the article number (alphanumeric) on...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the...
35
by: spekyuman | last post by:
Pointer and reference arguments provide C++ programmers with the ability to modify objects. What is more efficient, passing arguments via pointer...
36
by: The Frog | last post by:
Hi Everyone, I am trying to find a solution for handling zipped data without the need to ship / install any DLL files with the database. Does...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...

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.