while = 1

"""
Explanation: <div style="width:1000 px">

<div style="float:right; width:98 px; height:98px;">
<img src="https://raw.githubusercontent.com/Unidata/MetPy/master/metpy/plots/_static/unidata_150x150.png" alt="Unidata Logo" style="height: 98px;">
</div>

<h1>What to do when things go wrong</h1>
<h3>Unidata Python Workshop</h3>

<div style="clear:both"></div>
</div>

<hr style="height:2px;">

<div style="float:right; width:250 px"><img src="https://regmedia.co.uk/2015/06/15/silicon-valley-fire.jpg" alt="Server on fire" style="height: 200px;"></div>

This session is designed as both a workshop and group discussion.   We will examine basic failures in Python and how they manifest, followed by a discussion of what sort of errors and failure cases plague you, the audience.  We will try to identify things that we (Unidata) can do differently to make the software development/usage process easier for our community.  We will also pool our python knowledge/experiences to try to identify common issues and potential solutions.
This is useful because, eventually, problems will arise when programming with Python. Common issues include:

Syntax Errors
Runtime Errors
Unexpected Results

Identifying and correcting these issues can be difficult.  However, there are strategies for preventing and mitigating many of the problems a developer may encounter.
Syntax Errors
Syntax errors usually occur when python is translated from source code into byte code.  This is typically identified by a message similar to:
~~~
SyntaxError: invalid syntax
~~~
Common syntax errors include:

Using a reserved python keyword for a variable name.
End of explanation
"""


for i in range(1,10) print('Hello world')

"""
Explanation: Missing colon at the end of for, while, if, def statements.
End of explanation
"""


for i in range(1,10):
  x = 1 + i
    print(x)

"""
Explanation: Incorrect indentation. Don't mix spaces and tabs!
End of explanation
"""


print("Some python issues are easier to find than others ')

"""
Explanation: Mismatched quotation marks.
End of explanation
"""


print("Missing brackets can be difficult" , range(1,10)
print("to find. probably not these ones however.")

"""
Explanation: Unclosed brackets
End of explanation
"""


x = int(input("Please enter a number: "))
print(x)

while True:
    try:
        x = int(input("Please enter a number:"))
        break
    except ValueError:
        print("oops!")
            

"""
Explanation: Runtime errors
Once the program is syntactically correct, it can run and now nothing will go wrong!  Until it does.  There are multiple ways runtime errors may manifest:

Nothing happens.
The program hangs.
Infinite loop or recursion.

Debugging Runtime errors in Python with GDB
It's possible to debug python using gdb.  It requires the pythong debugging extensions be installed.
Running gdb interactively:
$ gdb python
   ....
   (gdb) run &lt;programname&gt;.py &lt;arguments&gt;
Running gdb Automatically:
$ gdb -ex r --args python &lt;programname&gt;.py &lt;arguments&gt;
The latter case runs the program until it exits, segfaults or execution is stopped.
You can get a stack trace or python stack trace via:
(gdb) bt
or
(gdb) py-bt   
Handling exceptions with try/except
Handling exceptions may done using try/except statements in python.
End of explanation
"""


class MyInputError(Exception):
    """Exception raised for errors in the input.

    Attributes:
        message -- explanation of the error
    """

    def __init__(self,*args,**kwargs):
        Exception.__init__(self,*args,**kwargs)
        self.dError = "Internal Error 10"

try:
    raise MyInputError("MyInputError")
    raise AssertionError("Sample Assertion Error")
except MyInputError as err: 
    print('InputError caught:',err)
    print('dError:',err.dError)
except AssertionError as err:
    print('Assertion error caught:',err)

?type

"""
Explanation: User-defined exceptions
Developers can create thir own exceptions by creating new exception classes derived from the Exception class.
End of explanation
"""


try:
    # raise MyInputError("New error, test finally")
    # 
except MyInputError as err:
    print('MyInputError:',err)
    print('dError:',err.dError)
finally:
    print('Graceful Cleanup: Goodbye, world!')
    

"""
Explanation: Defining Clean-up Actions
The try statement has an optional cluase which lets you define a clean-up action; this is an action executed whether an action has occured or not.
End of explanation
"""


