Making code more efficient and effective

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cokofreedom@gmail.com

    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(l ine):
    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(reverse d(func_name))
    else: return None

    with open(r"C:\examp le.java", "r") as test_file:
    for number, line in enumerate(test_ file):
    line = line.strip()
    if line.startswith (func_words) and line.find(ignor e_class ) ==
    -1:
    func_name = get_func_name(l ine);
    if func_name is not None:
    functions.setde fault(func_name , []).append(number )

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

    print "\n".join("Func tion: %s, found on %d line(s); these being
    %s"
    % (k, len(v), v) for k, v in
    functions.iteri tems())
  • =?iso-8859-1?q?C=E9dric_Lucantis?=

    #2
    Re: Making code more efficient and effective

    Le Thursday 26 June 2008 14:11:35 cokofreedom@gma il.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|protec ted|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

    Comment

    • bearophileHUGS@lycos.com

      #3
      Re: Making code more efficient and effective

      Cédric Lucantis:
      PAT = re.compile('^[ ]*(public|protec ted|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

      Comment

      • cokofreedom@gmail.com

        #4
        Re: Making code more efficient and effective

        On Jun 26, 5:42 pm, bearophileH...@ lycos.com wrote:
        Cédric Lucantis:
        >
        PAT = re.compile('^[ ]*(public|protec ted|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!

        Comment

        Working...