This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''I recently had to deploy a test script I had written in python 2.7 to a server where only python 2.4 was allowed. In the process of making my code backwards compatible, I made a brief checklist of things to watch out for: | |
* any and all functions can still be used but you have to manually code them | |
* with-as blocks must be replaced with try-finally | |
* try-except-finally must be converted to try-except-else | |
* string formatting: "".format() must be replaced with "%s"%(args) approach. | |
* Replace all instances of "except Exception as err:" with "except Exception, e:" | |
* may need to manually convert some unicode strings into str, especially when loading json | |
* Some modules have different names, eg: "email.MIMEText" versus "email.mime.text.MIMEText" | |
* The datetime module does not have strptime() in 2.4. However, you can import it with a different name and then make a subclass with the desired behavior by calling strptime() from the time module instead:''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Example of using recursion to apply newton's method to an arbitrary function in order to find the zeros of the function. If no zeros are found after some number of iterations, NA is returned | |
newton.recursive<-function(func,deriv,counter=100,x=1,relchange=1){ | |
diff = func(x)/deriv(x) | |
relchange = abs(diff/x) | |
if(counter==0){ | |
print("Failed to converge") | |
return(NA) | |
} | |
else if(relchange <= 1e-10){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def listprimes2(m): | |
'''another attempt to list all primes below m''' | |
values = range(m+1) #note that in this list the key and the value are the same. | |
primes = values[:] | |
primes[1] = 0 #1 doesn't count as a prime | |
for i in values: | |
if primes[i] == 0: | |
pass | |
else: | |
for j in values[i+1:]: |
NewerOlder