Stefan Scherfke avatar Stefan Scherfke committed 0a07d4e

Moved examples to examples directory

Comments (0)

Files changed (3)

 include *.txt
 recursive-include docs *
+recursive-include examples *.py

collectors/examples.py

-# encoding: utf-8
-
-from collectors import Collector, get
-
-def simpy_example():
-    """Shows the general usage of *Collectors*."""
-    import random
-
-    import matplotlib.pyplot as plt
-    from numpy import array, float64
-    from SimPy.Simulation import Simulation, Process, hold
-
-    from collectors import Collector, get, manual
-
-
-    class MyProc(Process):
-        """A simple example process that illustrates the usage of the new
-        monitoring approach."""
-        def __init__(self, sim):
-            Process.__init__(self, sim=sim)
-
-            self.a = 0
-            self.b = 0
-            self.c = 0
-
-            # Create a monitor that will collect:
-            # time:     A list with timestampes from sim.now()
-            # a:        Values of proc.a
-            # b:        Values of proc.b
-            # diff:     A value passed manually
-            # square:   Square of self.c
-            self.monitor = Collector(
-                ('time', sim.now),
-                get(self, 'a', 'b'),
-                ('diff', manual),
-                ('square', lambda: self.c ** 2),
-            )
-
-        def run(self):
-            while True:
-                self.a += random.random()
-                self.b += random.randint(1, 2)
-                self.c += random.randint(2, 4)
-
-                self.monitor(diff=self.c-self.b)
-
-                yield hold, self, 2
-
-
-    # Run the simulation
-    sim = Simulation()
-    proc = MyProc(sim)
-    sim.activate(proc, proc.run())
-    sim.simulate(until=10)
-
-    # Just a test and a simple example how to acces the monitored values:
-    assert proc.monitor[1] == proc.monitor.a
-
-    print proc.monitor
-    print zip(*proc.monitor)
-
-    # NumPy helps you with the statistics and other calculations.
-    # Note: specification of dtype gives you a massive speed-up!
-    a = array(proc.monitor.a, dtype=float64)
-    print 'a:', a
-    # NumPy: average, std. deviation, variance
-    print 'a stats:', a.mean(), a.std(), a.var()
-
-    # This one creates a multi-dimensional array (see the output)
-    np_mon = array(proc.monitor, dtype=float64)
-    print 't, a, b, diff, square:\n', np_mon
-    # Get the average of all monitored proc.b
-    print 'b stats:', np_mon[2].mean()
-    # Get the std. deviation of all monitored proc.c
-    print 'c stats:', np_mon[3].std()
-
-    # Matplotlib plots your data:
-    # Either directly from a monitor ...
-    plt.plot(proc.monitor.time, proc.monitor.a, 'b')
-    # ... or the NumPy array
-    plt.plot(np_mon[0], np_mon[2], 'r')
-    plt.show()
-
-def storage_release_example():
-    print 'Storage release example (this is really more of a test)'
-
-    from collectors.core import DefaultStorage
-
-    class YellingStorage(DefaultStorage):
-        def __del__(self):
-            print 'Oh noez, I wuz deleted'
-
-    class ObserveMe(object):
-        pass
-
-    obj = ObserveMe()
-    c = Collector(get(obj, 'value_a', 'value_b'), backend=YellingStorage())
-
-    import gc
-    gc.collect()
-
-    print 'Collector initialized'
-
-    for a, b in zip(range(10), reversed(range(10))):
-        obj.value_a, obj.value_b = a, b
-        c()
-
-def pytables_example():
-    import tables
-
-    from collectors import storage
-
-    class ObserveMe(object):
-        a = 0
-        b = 0
-
-    obj = ObserveMe()
-    h5file = tables.openFile('example.h5', mode='w')
-    collector = Collector(get(obj, 'a', 'b'),
-            backend=storage.PyTables(h5file, 'groupname', ('int', 'int'))
-    )
-
-    for values in zip(range(10), reversed(range(10))):
-        obj.a, obj.b = values
-        collector()
-
-    print collector.a.read(), collector.b.read()
-    h5file.close()
-
-def excel_example():
-    print '- Excel example'
-
-    from xlwt import Workbook
-
-    from collectors import storage
-
-    w = Workbook()
-    s = w.add_sheet('my collected data')
-
-    class ObserveMe(object):
-        pass
-
-    obj = ObserveMe()
-    c = Collector(get(obj, 'value_a', 'value_b'), backend=storage.Excel(w, s))
-
-    for a, b in zip(range(10), reversed(range(10))):
-        obj.value_a, obj.value_b = a, b
-        c()
-
-    print 'Writing result to example.xls'
-    w.save('example.xls')
-
-if __name__ == '__main__':
-    simpy_example()
-    storage_release_example()
-    pytables_example()
-    excel_example()

examples/examples.py

+# encoding: utf-8
+
+from collectors import Collector, get
+
+def simpy_example():
+    """Shows the general usage of *Collectors*."""
+    import random
+
+    import matplotlib.pyplot as plt
+    from numpy import array, float64
+    from SimPy.Simulation import Simulation, Process, hold
+
+    from collectors import Collector, get, manual
+
+
+    class MyProc(Process):
+        """A simple example process that illustrates the usage of the new
+        monitoring approach."""
+        def __init__(self, sim):
+            Process.__init__(self, sim=sim)
+
+            self.a = 0
+            self.b = 0
+            self.c = 0
+
+            # Create a monitor that will collect:
+            # time:     A list with timestampes from sim.now()
+            # a:        Values of proc.a
+            # b:        Values of proc.b
+            # diff:     A value passed manually
+            # square:   Square of self.c
+            self.monitor = Collector(
+                ('time', sim.now),
+                get(self, 'a', 'b'),
+                ('diff', manual),
+                ('square', lambda: self.c ** 2),
+            )
+
+        def run(self):
+            while True:
+                self.a += random.random()
+                self.b += random.randint(1, 2)
+                self.c += random.randint(2, 4)
+
+                self.monitor(diff=self.c-self.b)
+
+                yield hold, self, 2
+
+
+    # Run the simulation
+    sim = Simulation()
+    proc = MyProc(sim)
+    sim.activate(proc, proc.run())
+    sim.simulate(until=10)
+
+    # Just a test and a simple example how to acces the monitored values:
+    assert proc.monitor[1] == proc.monitor.a
+
+    print proc.monitor
+    print zip(*proc.monitor)
+
+    # NumPy helps you with the statistics and other calculations.
+    # Note: specification of dtype gives you a massive speed-up!
+    a = array(proc.monitor.a, dtype=float64)
+    print 'a:', a
+    # NumPy: average, std. deviation, variance
+    print 'a stats:', a.mean(), a.std(), a.var()
+
+    # This one creates a multi-dimensional array (see the output)
+    np_mon = array(proc.monitor, dtype=float64)
+    print 't, a, b, diff, square:\n', np_mon
+    # Get the average of all monitored proc.b
+    print 'b stats:', np_mon[2].mean()
+    # Get the std. deviation of all monitored proc.c
+    print 'c stats:', np_mon[3].std()
+
+    # Matplotlib plots your data:
+    # Either directly from a monitor ...
+    plt.plot(proc.monitor.time, proc.monitor.a, 'b')
+    # ... or the NumPy array
+    plt.plot(np_mon[0], np_mon[2], 'r')
+    plt.show()
+
+def storage_release_example():
+    print 'Storage release example (this is really more of a test)'
+
+    from collectors.core import DefaultStorage
+
+    class YellingStorage(DefaultStorage):
+        def __del__(self):
+            print 'Oh noez, I wuz deleted'
+
+    class ObserveMe(object):
+        pass
+
+    obj = ObserveMe()
+    c = Collector(get(obj, 'value_a', 'value_b'), backend=YellingStorage())
+
+    import gc
+    gc.collect()
+
+    print 'Collector initialized'
+
+    for a, b in zip(range(10), reversed(range(10))):
+        obj.value_a, obj.value_b = a, b
+        c()
+
+def pytables_example():
+    import tables
+
+    from collectors import storage
+
+    class ObserveMe(object):
+        a = 0
+        b = 0
+
+    obj = ObserveMe()
+    h5file = tables.openFile('example.h5', mode='w')
+    collector = Collector(get(obj, 'a', 'b'),
+            backend=storage.PyTables(h5file, 'groupname', ('int', 'int'))
+    )
+
+    for values in zip(range(10), reversed(range(10))):
+        obj.a, obj.b = values
+        collector()
+
+    print collector.a.read(), collector.b.read()
+    h5file.close()
+
+def excel_example():
+    print '- Excel example'
+
+    from xlwt import Workbook
+
+    from collectors import storage
+
+    w = Workbook()
+    s = w.add_sheet('my collected data')
+
+    class ObserveMe(object):
+        pass
+
+    obj = ObserveMe()
+    c = Collector(get(obj, 'value_a', 'value_b'), backend=storage.Excel(w, s))
+
+    for a, b in zip(range(10), reversed(range(10))):
+        obj.value_a, obj.value_b = a, b
+        c()
+
+    print 'Writing result to example.xls'
+    w.save('example.xls')
+
+if __name__ == '__main__':
+    simpy_example()
+    storage_release_example()
+    pytables_example()
+    excel_example()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.