Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modernise demos and make them Py2/3 compatible
- Loading branch information
Showing
15 changed files
with
82 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
import sys | ||
|
||
f1 = open(sys.argv[1]) | ||
f2 = open(sys.argv[2]) | ||
try: | ||
if f1.read() != f2.read(): | ||
print ("Files differ") | ||
print("Files differ") | ||
sys.exit(1) | ||
else: | ||
print ("Files identical") | ||
print("Files identical") | ||
finally: | ||
f1.close() | ||
f2.close() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
# cython: language_level=3 | ||
|
||
print __name__ | ||
print(__name__) | ||
|
||
if __name__ == "__main__": | ||
print "Hi, I'm embedded." | ||
print("Hi, I'm embedded.") | ||
else: | ||
print "I'm being imported." | ||
print("I'm being imported.") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
# cython: language_level=3 | ||
|
||
import lcmath | ||
|
||
|
||
def nCr(n, r): | ||
"""Return the number of ways to choose r elements of a set of n.""" | ||
return lcmath.exp( lcmath.lfactorial(n) - lcmath.lfactorial(r) | ||
- lcmath.lfactorial(n-r) ) | ||
return lcmath.exp( | ||
lcmath.lfactorial(n) - | ||
lcmath.lfactorial(r) - | ||
lcmath.lfactorial(n-r) | ||
) | ||
|
||
if __name__ == "__main__": | ||
import sys | ||
if len(sys.argv) != 3: | ||
sys.stderr.write("USAGE: %s n r\nPrints n-choose-r.\n" % sys.argv[0]) | ||
sys.exit(2) | ||
n, r = map(float, sys.argv[1:]) | ||
print nCr(n, r) | ||
print(nCr(n, r)) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# cython: language_level=3 | ||
|
||
def f(x): | ||
return x**2-x | ||
|
||
|
||
def integrate_f(a, b, N): | ||
s = 0.0 | ||
dx = (b-a)/N | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
import timeit | ||
|
||
import integrate0, integrate1, integrate2 | ||
|
||
number = 10 | ||
py_time = None | ||
for m in ('integrate0', 'integrate1', 'integrate2'): | ||
print m | ||
print(m) | ||
t = min(timeit.repeat("integrate_f(0.0, 10.0, 10000000)", "from %s import integrate_f" % m, number=number)) | ||
if py_time is None: | ||
py_time = t | ||
print " ", t / number, "s" | ||
print " ", py_time / t | ||
print(" ", t / number, "s") | ||
print(" ", py_time / t) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
import sys | ||
from primes import primes | ||
|
||
if len(sys.argv) >= 2: | ||
n = int(sys.argv[1]) | ||
else: | ||
n = 1000 | ||
print primes(n) | ||
|
||
print(primes(n)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from __future__ import absolute_import, print_function | ||
|
||
from spam import Spam | ||
|
||
s = Spam() | ||
print "Created:", s | ||
print("Created:", s) | ||
s.set_amount(42) | ||
print "Amount =", s.get_amount() | ||
print("Amount =", s.get_amount()) | ||
s.describe() | ||
s = None |
This file contains 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