Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created March 1, 2010 07:31
Show Gist options
  • Save jschementi/318169 to your computer and use it in GitHub Desktop.
Save jschementi/318169 to your computer and use it in GitHub Desktop.
Demo snippets from PyCon 2010
>>> dir(document)
[..., 'CreateElement', ...]
>>> div = document.CreateElement("div")
>>> div.innerHTML = "Hello from Python!"
>>> document.Body.AppendChild(div)
>>> div.id = "message"
>>> div.SetStyleAttribute("font-size", "24px")
>>> def say_ouch(o, e):
... o.innerHTML = "Ouch!"
...
>>> document.message.events.onclick += say_ouch
<html>
<head>
<script src="http://gestalt.ironpython.net/dlr-latest.js"
type="text/javascript"></script>
</head>
<body>
<script type="text/python">
window.Alert("Hello from Python")
</script>
</body>
</html>
import clr
clr.AddReferenceToFile("bin/mandelbrotbase.dll")
import mandelbrotbase
gen = mandelbrotbase.MandelbrotGenerator()
bmap = gen.GenerateBitmap(-2.1, -1.3, 1, 1.3, 640, 480)
namespace mandelbrotbase {
public class MandelbrotGenerator {
public int[] GenerateBitmap(double xmin, double ymin, double xmax, double ymax, int width, int height) {
int[] bmap = new int[width * height];
double x, y, x1, y1, xx = 0.0;
int looper, s, z = 0;
double intigralX, intigralY = 0.0;
intigralX = (xmax - xmin) / width;
intigralY = (ymax - ymin) / height;
x = xmin;
for (s = 0; s < width; s++) {
y = ymin;
for (z = 0; z < height; z++) {
x1 = 0;
y1 = 0;
looper = 0;
while (looper < 100 && ((x1 * x1) + (y1 * y1)) < 4) {
looper++;
xx = (x1 * x1) - (y1 * y1) + x;
y1 = 2 * x1 * y1 + y;
x1 = xx;
}
double perc = looper / (100.0);
int val = ((int)(perc * (this.Palette.Length - 1)));
Color px = this.Palette[val];
bmap[z * this.Width + s] = px.A << 24 | px.R << 16 | px.G << 8 | px.B;
y += intigralY;
}
x += intigralX;
}
return bmap;
}
}
}
<script type="application/x-zip-compressed"
src="PythonStdLib.zip"></script>
<script type="text/python" src="repl.py"></script>
<script type="application/x-zip-compressed" src="PythonStdLib.zip">
</script>
<script type="text/python">
if document.QueryString.ContainsKey("test"):
import sys
sys.path.append("PythonStdLib")
import repl
repl.show()
import unittest
class TestHello(unittest.TestCase):
def testgreeting(self):
self.assertEqual(document.message.innerHTML, "Hello from Python!")
def testsayouch(self):
say_ouch(document.message, None)
self.assertEqual(document.message.innerHTML, "Ouch!")
suite = unittest.TestLoader().loadTestsFromTestCase(TestHello)
unittest.TextTestRunner().run(suite)
</script>
if _CaptureSource is not None and _CaptureSource.State != CaptureState.Started:
_CaptureSource.Stop() # stop whatever device may be capturing
# set the devices for the capture source
_CaptureSource.VideoCaptureDevice = xaml.VideoSources.SelectedItem;
_CaptureSource.AudioCaptureDevice = xaml.AudioSources.SelectedItem;
# create the brush
vidBrush = VideoBrush()
vidBrush.SetSource(_CaptureSource)
xaml.WebcamCapture.Fill = vidBrush # paint the brush on the rectangle
# request user permission and display the capture
if CaptureDeviceConfiguration.AllowedDeviceAccess or CaptureDeviceConfiguration.RequestDeviceAccess():
_CaptureSource.Start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment