[Tutor] Decorators
I need help with the following task for my new job: The code we're using is python, which I have never worked with before. Our AMS source code relies heavily on decorators.Things look something like this: @decomaker(argA, argB, ...) def func(arg1, arg2, ...): pass which is the same as func = decomaker(argA, argB, ...)(func) and @dec2 @dec1 def func(arg1, arg2, ...): pass func = dec2(dec1(func)) Or when implemented the second example looks like: def dec1(func): def new_func(arg1, arg2, ...): ... do something... ret = func(arg1, arg 2, ...) ... do more things... return ret return new_func My first task is that there is an issue with the name new_func. When the program crashes, that is what shows up in the logs-which doesn't help debug anything. I need to find out every decorator and make sure it has a descriptive name. I was thinking I could write a simple script which would parse through all of the source files and find decorators-maybe by looking for the @ symbol? Then I could manually check to make sure it has a good name. I was thinking I could copy the searching code from find_imports.py (up to the startswith() calls) and print the list of decorators found and which files that they're in. I have never worked with python before so I definitely need help with this task-any suggestions or examples that would be helpful? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Decorators revised
I need help with the following task for my new job: The code we're using is written in python, which I have never worked with before. Our AMS source code relies heavily on decorators.Things look something like this: @decomaker(argA, argB, ...) def func(arg1, arg2, ...): pass which is the same as func = decomaker(argA, argB, ...)(func) and @dec2 @dec1 def func(arg1, arg2, ...): pass func = dec2(dec1(func)) Or when implemented the second example looks like: def dec1(func): def new_func(arg1, arg2, ...): ... do something... ret = func(arg1, arg 2, ...) ... do more things... return ret return new_func My first task is that there is an issue with the name new_func. When the program crashes, that is what shows up in the logs-which doesn't help debug anything. I need to find out every decorator and make sure it has a descriptive name. I was thinking I could write a simple script which would parse through all of the source files and find decorators-maybe by looking for the @ symbol? Then I could manually check to make sure it has a good name. I was thinking I could copy the searching code from find_imports.py (up to the startswith() calls) and print the list of decorators found and which files that they're in. What I want to do is use a find() for the @dec maybe I also don't know how to parse out everything that's not the na...@dec(arg1, arg2) should be "dec". I have never worked with python before so I definitely need help with this task-any suggestions or examples that would be helpful? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Parsing through decorators program
So my new internship asked me to write a program that would parse through all of our source code on an svn server, find all the decorators and print them. I found all of the decorators by finding the '@' symbol. What I still have to do is write something that will make the computer print all of the names of the decorators that it finds. So far, this is what I have written: # Written by Mary Morris # July 2010 # import os import warnings import traceback ## # # def get_py_files(path): ret = [] if path[-1] != '/': path += '/' files = os.listdir(path) for file in files: if file == '.svn': continue if os.path.isdir(path + file + '/'): temp = get_py_files(path + file + '/') for ele in temp: ret.append(ele) else: ext = file.split('.')[-1] if ext != 'py': continue temp = path + file temp = temp.replace('/', '\\') ret.append(temp) return ret if __name__ == '__main__': imports = {} files = get_py_files(os.getcwd()) our_modules = [file.split('\\')[-1][:-3] for file in files] for file in files: # for file in files[:1]: f = open(file) lines = f.readlines() f.close() for line in lines: line = line[:-1] # # Conveniently, "import bleh" and "from bleh import bleh2" both have the second string (when split) of "bleh" # if line.startswith('import') or line.startswith('from'): line = line.replace('\t', ' ') temp = [s for s in line.split(' ') if len(s) != 0] # This is because FirstAssist uses "import os, sys" (and similar) all over the place... if temp[1].find('@') != -1: for ele in temp[1:]: ele = ele.replace('@', '') if not ele in imports.keys(): # imports[ele] = [file.split('\\')[-1]] imports[ele] = [file] else: # imports[ele] += [file.split('\\')[-1]] imports[ele] += [file] else: if not temp[1] in imports.keys(): # imports[temp[1]] = [file.split('\\')[-1]] imports[temp[1]] = [file] else: # imports[temp[1]] += [file.split('\\')[-1]] imports[temp[1]] += [file] external_imports = [im for im in imports.keys() if not im in our_modules] dependencies = [] Could you give me suggestions on how to make my program print the names of the decorators? Thanks, Mary ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] append, list and variables
Hi, I'm working on a program that parses through all of our source code at my office and i need to get my code to print a list of the decorators. I used a find(@) to locate all the decorators, but I need to assign them to a variable somehow to get it to print a list. How do I do this? How do I assign a variable to all the indexed strings after the @ symbol? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] append, list and variables
Thanks-that helps a lot. The only question I have about your pseudocode is what the 'initialize result container' does. I'm pretty new to python and scripting in general so I'm still trying to figure everything out. On Thu, Jul 15, 2010 at 12:58 PM, Emile van Sebille wrote: > On 7/15/2010 11:32 AM Mary Morris said... > > Hi, >> I'm working on a program that parses through all of our source code at my >> office and i need to get my code to print a list of the decorators. I >> used >> a find(@) to locate all the decorators, but I need to assign them to a >> variable somehow to get it to print a list. How do I do this? How do I >> assign a variable to all the indexed strings after the @ symbol? >> >> > So, decorator lines start with an '@'. Source files end in '.py'. > > Pseudo code could be: > > initialize result container > > with each sourcefilename in globbed list: > for eachline in opened(sourcefilename): >if line.startswith('@'): > append [sourcefilename, line] to result > > # print it > > for eachline in result: > print eachline > > > Hope this gets you going, > > Emile > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] decorators
I'm trying to compile a list of decorators from the source code at my office. I did this by doing a candidate_line.find("@") because all of our decorators start with the @ symbol. The problem I'm having is that the email addresses that are included in the comments are getting included in the list that is getting returned. I was thinking I could do a candidate_line.find(".com") to set the email addresses apart, but how do I tell the computer to not include the lines it finds with ".com" in them in the list? The part of my code that I'm hoping to include this in looks like this: #parse out the names of the decorators from those lines return_decorators= [] for ele in subset_lines: candidate_line, line_number = ele candidate_line = candidate_line.strip() i = candidate_line.find("(") j = candidate_line.find("#") #if () is in the last spot if i == -1: #if () is in the last spot and the decorator is in a comment if j == 0: #get rid of ( and # candidate_line = candidate_line[2:i] #if () is in the last spot and the decorator is not in a comment elif j != 0: candidate_line = candidate_line[1:i] #if there are not ()'s in the last spot elif i != -1: #if there are not ()'s, but the decorator is in a comment if j == 0: candidate_line = candidate_line[2:] #if there are not ()'s and the decorator isn't in a comment elif j != 0: candidate_line = candidate_line[1:] elif candidate_line.find(".com"): candidate_line != candidate_line return_decorators.append((line_number, candidate_line)) return return_decorators ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Decorator listing
I'm writing a program with python that compiles a list of decorators from my company's source code. I'm new to python and need help with the following: I'm trying to make the program find ".com" in any lines and not include them in the list of decorators that gets returned. THis is because I had the program look for the @ symbol to find the decorators and it returned email addresses in the list. What I was thinking was to do something like this: k = candidate_line.find(".com") if k != -1: candidate_line != candidate_line The problem is that I know this won't work because ".com" can't be in the last slot of the candidate line... So far the part of my code that needs to be changed looks like this... #parse out the names of the decorators from those lines return_decorators= [] for ele in subset_lines: candidate_line, line_number = ele candidate_line = candidate_line.strip() i = candidate_line.find("(") j = candidate_line.find("#") k = candidate_line.find(".com") #if () is in the last spot if i == -1: #if () is in the last spot and the decorator is in a comment if j == 0: #get rid of ( and # candidate_line = candidate_line[2:i] #if () is in the last spot and the decorator is not in a comment elif j != 0: candidate_line = candidate_line[1:i] #if there are not ()'s in the last spot elif i != -1: #if there are not ()'s, but the decorator is in a comment if j == 0: candidate_line = candidate_line[2:] #if there are not ()'s and the decorator isn't in a comment elif j != 0: candidate_line = candidate_line[1:] elif k in candidate_line: candidate_line != candidate_line ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor