Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,10 @@
# Search for an odd factor of a using brute force:
for i in range(n):
if (n%2) == 0:
continue
if (n%i) == 0:
result = i
break
else:
result = None
print "No odd factors found"

View file

@ -0,0 +1,13 @@
# Let's call our custom error "StupidError"; it inherits from the Exception class
class StupidError(Exception): pass
# Try it out.
try:
raise StupidError("Segfault") # here, we manually 'raise' the error within the try block
except StupidError, details: # 'details' is the StupidError object we create in the try block.
print 'Something stupid occurred:', details # so we access the value we had stored for it...
# Output :
# Something stupid occurred: Segfault

View file

@ -0,0 +1,20 @@
i = 101
for i in range(4): # loop 4 times
print "I will always be seen."
if i % 2 == 0:
continue # continue goes back to the loop beginning for a new iteration.
print "I'll only be seen every other time."
else:
print "Loop done"
# Output:
# I will always be seen.
# I will always be seen.
# I'll only be seen every other time.
# I will always be seen.
# I will always be seen.
# I'll only be seen every other time.
# Loop done
if(__name__ == "__main__"):
main()

View file

@ -0,0 +1,9 @@
class Quitting(Exception): pass
max = 10
with open("some_file") as myfile:
exit_counter = 0
for line in myfile:
exit_counter += 1
if exit_counter > max:
raise Quitting
print line,

View file

@ -0,0 +1 @@
class MyException(Exception): pass

View file

@ -0,0 +1,3 @@
class MyVirtual(object):
def __init__(self):
raise NotImplementedError

View file

@ -0,0 +1,6 @@
try:
temp = 0/0
# 'except' catches any errors that may have been raised between the code of 'try' and 'except'
except: # Note: catch all handler ... NOT RECOMMENDED
print "An error occurred."
# Output : "An error occurred"

View file

@ -0,0 +1,6 @@
try:
temp = 0/0
# here, 'except' catches a specific type of error raised within the try block.
except ZeroDivisionError:
print "You've divided by zero!"
# Output : "You've divided by zero!"

View file

@ -0,0 +1,11 @@
try:
temp = 0/0
except:
print "An error occurred."
# here, 'finally' executes when the try - except block ends, regardless of whether an error was raised or not
# useful in areas such as closing opened file streams in the try block whether they were successfully opened or not
finally:
print "End of 'try' block..."
# Output :
# An error occurred
# End of 'try' block...

View file

@ -0,0 +1,8 @@
try:
try:
pass
except (MyException1, MyOtherException):
pass
except SomeOtherException:
finally:
do_some_cleanup() # run in any case, whether any exceptions were thrown or not

View file

@ -0,0 +1,9 @@
try:
temp = 1/1 # not a division by zero error
except ZeroDivisionError: # so... it is not caught
print "You've divided by zero."
# here, 'else' executes when no exceptions are caught...
else:
print "No apparent error occurred."
# Output :
# No apparent error occurred.

View file

@ -0,0 +1,20 @@
i = 0
while 1: # infinite loop
try:
temp2 = 0/i # will raise a ZeroDivisionError first.
temp = math.sqrt(i)
break # 'break' will break out of the while loop
except ValueError: #
print "Imaginary Number! Breaking out of loop"
break # 'break' out of while loop
except ZeroDivisionError:
print "You've divided by zero. Decrementing i and continuing..."
i-=1 # we decrement i.
# we 'continue', everything within the try - except block will be executed again,
# this time however, ZeroDivisionError would not be raised again.
continue # Note that removing it, replacing it with 'pass' would perform the equivalent
# see below for a better example
# Output :
# You've divided by zero. Decrementing i and continuing...
# Imaginary Number! Breaking out of loop