[svn r63208] moving example to top-level dir.

--HG--
branch : trunk
This commit is contained in:
hpk
2009-03-22 18:08:31 +01:00
parent 3305a0e3db
commit c81ffeb027
7 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
"""
example
reading results from possibly blocking code running in sub processes.
"""
import py
NUM_PROCESSES = 5
channels = []
for i in range(NUM_PROCESSES):
gw = py.execnet.PopenGateway() # or use SSH or socket gateways
channel = gw.remote_exec("""
import time
secs = channel.receive()
time.sleep(secs)
channel.send("waited %d secs" % secs)
""")
channels.append(channel)
print "*** instantiated subprocess", gw
mc = py.execnet.MultiChannel(channels)
queue = mc.make_receive_queue()
print "***", "verifying that timeout on receiving results from blocked subprocesses works"
try:
queue.get(timeout=1.0)
except Exception:
pass
print "*** sending subprocesses some data to have them unblock"
mc.send_each(1)
print "*** receiving results asynchronously"
for i in range(NUM_PROCESSES):
channel, result = queue.get(timeout=2.0)
print "result", channel.gateway, result

13
example/genhtml.py Normal file
View File

@@ -0,0 +1,13 @@
from py.xml import html
paras = "First Para", "Second para"
doc = html.html(
html.head(
html.meta(name="Content-Type", value="text/html; charset=latin1")),
html.body(
[html.p(p) for p in paras]))
print unicode(doc).encode('latin1')

23
example/genhtmlcss.py Normal file
View File

@@ -0,0 +1,23 @@
import py
html = py.xml.html
class my(html):
"a custom style"
class body(html.body):
style = html.Style(font_size = "120%")
class h2(html.h2):
style = html.Style(background = "grey")
class p(html.p):
style = html.Style(font_weight="bold")
doc = my.html(
my.head(),
my.body(
my.h2("hello world"),
my.p("bold as bold can")
)
)
print doc.unicode(indent=2)

17
example/genxml.py Normal file
View File

@@ -0,0 +1,17 @@
import py
class ns(py.xml.Namespace):
pass
doc = ns.books(
ns.book(
ns.author("May Day"),
ns.title("python for java programmers"),),
ns.book(
ns.author("why", class_="somecssclass"),
ns.title("Java for Python programmers"),),
publisher="N.N",
)
print doc.unicode(indent=2).encode('utf8')

View File

@@ -0,0 +1,120 @@
from py.test import raises
import py
def otherfunc(a,b):
assert a==b
def somefunc(x,y):
otherfunc(x,y)
def otherfunc_multi(a,b):
assert (a ==
b)
class TestFailing(object):
def test_simple(self):
def f():
return 42
def g():
return 43
assert f() == g()
def test_simple_multiline(self):
otherfunc_multi(
42,
6*9)
def test_not(self):
def f():
return 42
assert not f()
def test_complex_error(self):
def f():
return 44
def g():
return 43
somefunc(f(), g())
def test_z1_unpack_error(self):
l = []
a,b = l
def test_z2_type_error(self):
l = 3
a,b = l
def test_startswith(self):
s = "123"
g = "456"
assert s.startswith(g)
def test_startswith_nested(self):
def f():
return "123"
def g():
return "456"
assert f().startswith(g())
def test_global_func(self):
assert isinstance(globf(42), float)
def test_instance(self):
self.x = 6*7
assert self.x != 42
def test_compare(self):
assert globf(10) < 5
def test_try_finally(self):
x = 1
try:
assert x == 0
finally:
x = 0
def test_raises(self):
s = 'qwe'
raises(TypeError, "int(s)")
def test_raises_doesnt(self):
raises(IOError, "int('3')")
def test_raise(self):
raise ValueError("demo error")
def test_tupleerror(self):
a,b = [1]
def test_reinterpret_fails_with_print_for_the_fun_of_it(self):
l = [1,2,3]
print "l is", l
a,b = l.pop()
def test_some_error(self):
if namenotexi:
pass
def test_generator(self):
yield None
def func1(self):
assert 41 == 42
def test_generator2(self):
yield self.func1
# thanks to Matthew Scott for this test
def test_dynamic_compile_shows_nicely():
src = 'def foo():\n assert 1 == 0\n'
name = 'abc-123'
module = py.std.imp.new_module(name)
code = py.code.compile(src, name, 'exec')
exec code in module.__dict__
py.std.sys.modules[name] = module
module.foo()
def globf(x):
return x+1

View File

@@ -0,0 +1,14 @@
import py
failure_demo = py.magic.autopath().dirpath('failure_demo.py')
pytest_plugins = "pytest_pytester"
def test_failure_demo_fails_properly(testdir):
sorter = testdir.inline_run(failure_demo)
passed, skipped, failed = sorter.countoutcomes()
assert passed == 0
assert failed == 20, failed
colreports = sorter.getnamed("collectionreport")
failed = len([x.failed for x in colreports])
assert failed == 5

View File

@@ -0,0 +1,42 @@
def setup_module(module):
module.TestStateFullThing.classcount = 0
class TestStateFullThing:
def setup_class(cls):
cls.classcount += 1
def teardown_class(cls):
cls.classcount -= 1
def setup_method(self, method):
self.id = eval(method.func_name[5:])
def test_42(self):
assert self.classcount == 1
assert self.id == 42
def test_23(self):
assert self.classcount == 1
assert self.id == 23
def teardown_module(module):
assert module.TestStateFullThing.classcount == 0
""" For this example the control flow happens as follows::
import test_setup_flow_example
setup_module(test_setup_flow_example)
setup_class(TestStateFullThing)
instance = TestStateFullThing()
setup_method(instance, instance.test_42)
instance.test_42()
setup_method(instance, instance.test_23)
instance.test_23()
teardown_class(TestStateFullThing)
teardown_module(test_setup_flow_example)
Note that ``setup_class(TestStateFullThing)`` is called and not
``TestStateFullThing.setup_class()`` which would require you
to insert ``setup_class = classmethod(setup_class)`` to make
your setup function callable.
"""