Making code more efficient and effective

Discussion in 'Python' started by cokofreedom@gmail.com, Jun 26, 2008.

  1. Guest

    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 26, 2008
    #1
    1. Advertising

  2. Le Thursday 26 June 2008 14:11:35 , 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
    Cédric Lucantis, Jun 26, 2008
    #2
    1. Advertising

  3. Guest

    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 26, 2008
    #3
  4. Guest

    On Jun 26, 5:42 pm, 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 26, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    408
  2. Josh Mcfarlane
    Replies:
    44
    Views:
    1,101
    Alf P. Steinbach
    Dec 16, 2005
  3. More effective C++

    , Jun 15, 2006, in forum: C++
    Replies:
    15
    Views:
    681
    Gernot Robert Bauer
    Jun 17, 2006
  4. barcaroller

    More Effective C++

    barcaroller, Apr 13, 2007, in forum: C++
    Replies:
    4
    Views:
    434
    Default User
    Apr 13, 2007
  5. Replies:
    22
    Views:
    313
Loading...

Share This Page