-3

I am not a python geek but have tried to solve this problem using information from several answers to similar questions but none seem to really work in my case. Here it is:

I am calling a function from a python script: Here is the function:

def getsom(X):
    #some codes
    try:
        st = get data from site 1 using X
    except:
        print "not available from site 1, getting from site 2"
        st = get data from site 2 using X

    #some codes that depend on st

I am calling this from a python script as such:

#some codes
for yr in range(min_yr,max_yr+1):
    day=1
    while day<max_day:
        st1 = getsom(X)
        #some code that depends on st1
        day+=1

This works fine when data is available on either site 1 or 2 for a particular day, but breaks down when it is unavailable on both sites for another day.

I want to be able to check for the next day if data is unavailable for a particular day for both sites. I have tried different configurations of try and except with no success and would appreciate any help on the most efficient way to do this.

Thanks!

***Edits Final version that worked:

in the function part:

def getsom(X):
    #some codes
    try:
        st = get data from site 1 using X
    except:
        print "not available from site 1, getting from site 2"
        st = get data from site 2 using X

        try:
           st = get data from site 2 using X
        except:
           print "data not available from sites 1 and 2"
           st=None
    if st is not None:
        #some codes that depend on st

In order to iterate to the next day on the script side, I had to handle the none case from the function with another try/except block:

#some codes
for yr in range(min_yr,max_yr+1):
    day=1
    while day<max_day:


   try:
       st=getsom(X)
   except:
       st=None

    if st is not None:
       #some codes that depend
QP1
  • 15
  • 4
  • 2
    So, wrap the code in the `except` block in `try/except`... – ForceBru Aug 22 '16 at 08:33
  • Wonder why I got down-voted 4 pts in less than a minute.@ForceBru... thanks for your suggestion, I tried it but didnt seem to get it working. could you kindly give an example – QP1 Aug 22 '16 at 08:50
  • 1
    the code of `getsom` is almost OK. You wrapped `st = get data from site 1` in a `try/except` construction, and so you should do with `st = get data from site 2`. The current answer's got it correct. – ForceBru Aug 22 '16 at 09:05

2 Answers2

2

As mentioned in the comments you seem like you want to catch the exception in first-level exception handler. You can do it like this:

def getsom(X):
    #some codes
    try:
        st = get data from site 1 using X
    except:
        print "not available from site 1, getting from site 2"
        try:
             st = get data from site 2 using X
        except:
             print "Not available from site 2 as well."
             # Here you can assign some arbitrary value to your variable (like None for example) or return from function.

    #some codes that depend on st

If data is not available on neither of the sites you can assign some arbitrary value to your variable st or simply return from the function.

Is this what you are looking for? Also, you shouldn't simply write except without specifying the type of exception you expect - look here for more info: Should I always specify an exception type in `except` statements?

Edit to answer the problem in comment:

If you have no data about certain day you can just return None and handle it like this:

#some codes
for yr in range(min_yr,max_yr+1):
    day=1
    while day<max_day:
        st1 = getsom(X)
        if st1 is not None:
            #some code that depends on st1
        day+=1
Community
  • 1
  • 1
grael
  • 657
  • 2
  • 11
  • 26
  • 1
    This should works but to use try...except... in another try is not recommended according to PEP8. – Page David Aug 22 '16 at 09:15
  • @grael ... thanks all for the quick response. With the inner try/except set to return, the function works well but the script does not go to the next day but exits with: "TypeError: 'NoneType' object is not iterable". With st set to None (or other arb. variable), the other dependent parts of the function below the try/except block break down, as no data is found. – QP1 Aug 22 '16 at 09:45
  • @grael... thanks a bunch, I modified your idea a bit and it worked! This is just part of a very long and multi-functional code. Check the edit to find the final version. – QP1 Aug 23 '16 at 19:50
  • @David Page... thanks David for the best-practice suggestion! – QP1 Aug 23 '16 at 19:52
1

Why don't you create a separate function for it?

def getdata(X):
    for site in [site1, site2]:  # possibly more
        try:
            return get_data_from_site_using_X()
        except:
            print "not available in %s" % site
    print "couldn't find data anywhere"

Then getsom becomes:

def getsom(X):
    #some codes
    st = getdata(X)
    #some codes that depend on st
grovina
  • 2,999
  • 19
  • 25