Scott wrote: > When creating a function is there any difference between putting > everything under the "def" or not? > > Here I created a function called CscoPortNum to convert the network > port number field in a Cisco syslog string from a an ascii name back > into its numeric form if required. Does it matter at all that I > created the translation dictionary first and then started the def? > > # def CscoPortNum(RulL) > # Accept a single ACL Rule as a List split into individual words and > # return Port number. Convert from Cisco syslog Port name if required > portfpth = "\\progra~1\\syslogd\\ACL_Logs\\Port-Translations.txt" > # Create dictionary of portnames to portnumbers > portD = {} > for prtnmS in open(portfpth): > prtnmS = prtnmS.rstrip() > spltprtL = prtnmS.split(" ") > portD[spltprtL[2]] = [spltprtL[1]] > def CscoPortNum(RulL): > if "eq" in RulL: # Is the Port listed? > if RulL[RulL.index("eq")+1][0].isdigit(): # Is it numeric? > # if re.search("\d", RulL[RulL.index("eq")+1][0]): # Is it > numeric? > portnum = RulL[RulL.index("eq")+1] # If numeric, use > as is. > else: > # If named, look up numeric translation > portnum = portD[RulL[RulL.index("eq")+1]] > portnum = str(portnum).strip("[]'") > else: portnum = "noeq" > return portnum
In this snippet no, you're not calling the function in the preceding code so there's no problem. You can intersperse functions with the rest of your code however you like, they just wont be visible to the preceding code, but it's better to stick them all at the top of your script. Even better when you have more than a handful is to bundle functions into separate py files and then import that file e.g. ---contents of foobar.py----- def foo(): print "foo" def bar: print "bar" ---contents of your main script----- import foobar print foo(),bar() Roger. -- http://mail.python.org/mailman/listinfo/python-list