Fundamental Function Question (beginner)

S

Scott

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
 
M

MRAB

Scott said:
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

There's nothing wrong with building dicts or other lookup tables outside
a function in order to avoid re-creating them every time the function is
called.
 
R

r0g

Scott said:
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.
 
R

r0g

r0g said:
Scott said:
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.


Whoops, that should have been...
 
P

Phlip

MRAB said:
Scott wrote:
There's nothing wrong with building dicts or other lookup tables outside
a function in order to avoid re-creating them every time the function is
called.

However, please consider writing complete, pronounceable names for variables.
This looks like Fortran!
 
S

Scott

However, please consider writing complete, pronounceable names for variables.
This looks like Fortran!

Thanks for the advice.
Scott
Chrmn, Scty fr th lmntn f vwls
 
S

Steven D'Aprano

There's nothing wrong with building dicts or other lookup tables outside
a function in order to avoid re-creating them every time the function is
called.

Actually there is, but the benefit (avoiding the re-creation of the
table) may be worth the cost (loss of encapsulation due to the use of a
global variable).

Also, it is slightly slower to access a global than to access a local.
This truly is a micro-optimization, and won't matter one bit for most
functions, but in some rare cases it may.

But these are just quibbles. In general, I would agree with you.
 
S

Scott

There's nothing wrong with building dicts or other lookup tables outside
a function in order to avoid re-creating them every time the function is
called.

Brilliant! I didn't think of that. I guess I accidentally did it right
this time as I query that dictionary quite a few times from the
function.

Thanks MRAB and All!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top