Skip to content

Instantly share code, notes, and snippets.

View willtownes's full-sized avatar

Will Townes willtownes

View GitHub Profile
@willtownes
willtownes / py27topy24.py
Created February 5, 2013 19:08
Checklist for making python 2.7 scripts run in python 2.4
'''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:'''
@willtownes
willtownes / newton_recursive.R
Created November 7, 2012 21:46
Example of recursion in R
#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){
@willtownes
willtownes / pythonprimes
Created December 6, 2011 01:43
Faster way to list primes in Python
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:]: