0

I'm current writing a short bit of code that will compare an etag for a web server page in a saved document to the etag on the server. If they are different, the code will indicate this. My code is below:-

import httplib

def currentTag():
    f = open('C:/Users/ME/Desktop/document.txt')
    e = f.readline()
    newTag(e)

def newTag(old_etag):
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    current_etag = r.getheader('etag').replace('"', '')
    compareTag(old_etag, current_etag)

def compareTag(old_etag, current_etag):
    if old_etag == current_etag:
        print "the same"
    else:
        print "different"

if __name__ == '__main__':
    currentTag()

Now, reviewing my code, there is actually no reason to pass 'etag' from the currentTag() method to the newTag() method given that the pre-existing etag is not processed in newTag(). Nonetheless, if I don't do this, how can I pass two different values to compareTag(). So for example, when defining compareTag(), how can I pass 'etag' from the currentTag() method and 'current_etag' from the newTag() method?

Wilduck
  • 13,822
  • 10
  • 58
  • 90
thefragileomen
  • 1,537
  • 8
  • 24
  • 40
  • As your code is currently structured, there's no reason for you to use three separate functions. And, since these functions aren't returning anything or being re-used, I'm not sure why you're not simply doing this procedurally. – Wilduck Jan 19 '12 at 23:03
  • Thanks @Wilduck. I was under the impression that it is more "Pythonistic" to separate your code into smaller functions than have a long string of code in one function. – thefragileomen Jan 19 '12 at 23:10
  • @thefragileomen: Separating code -- in the way you've separated it -- isn't really very sensible. These functions don't have any independent meaning and can't easily be reused. The goal is not "smaller". The goal is "meaningful". – S.Lott Jan 19 '12 at 23:21
  • I'll disagree with S.Lott and Wilduck, there is nothing wrong with splitting this task into the three functions. Your problem is how you hook them together as yurib pointed out. – Winston Ewert Jan 20 '12 at 00:22
  • @thefragileomen To expand on S.Lott's comment "meaningful" is a good measure for how to write functions. To describe meaningful function we often say they have "high cohesion." By doing what yurib suggested, returning values from your functions instead of chaining them together, you reduce their dependency on each other. This is "loose coupling." These are fundamental concepts of writing good code (in any language, not just python). See http://stackoverflow.com/questions/39946/coupling-and-cohesion for a good description. – Wilduck Jan 20 '12 at 17:36

4 Answers4

4

you shouldn't chain your function calls like that, have a main block of code that calls the functions serially, like so:

if __name__ == '__main__':
  currtag = currentTag()
  newtag = newTag()
  compareTag(currtag,newtag)

adjust your functions to return the relevant data

the basic idea of a function is that it returns data, you usually use functions to do some processing and return a value and not for control flow.

yurib
  • 8,043
  • 3
  • 30
  • 55
0

change your main to:

if __name__ == '__main__':
    compareTag(currentTag(), newTag())

and then have currentTag() return e and newTag() return current_etag

thagorn
  • 727
  • 7
  • 14
0
def checkTags():
    c = httplib.HTTPConnection('standards.ieee.org')
    c.request('HEAD', '/develop/regauth/oui/oui.txt')
    r = c.getresponse()
    with open('C:/Users/ME/Desktop/document.txt', 'r') as f:
        if f.readline() == r.getheader('etag').replace('"', ''): print "the same"
        else: print "different"
Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
0

you could make the variables (i.e. etag) global

Baboon
  • 307
  • 3
  • 6
  • 14