updated taxi simulator
This commit is contained in:
@@ -28,12 +28,14 @@ import requests
|
|||||||
|
|
||||||
SAVE_DIR = 'downloaded/'
|
SAVE_DIR = 'downloaded/'
|
||||||
|
|
||||||
#POTD_BASE_URL = 'http://en.wikipedia.org/wiki/Template:POTD/'
|
HTTP_PORT = 8002
|
||||||
POTD_BASE_URL = 'http://127.0.0.1:8001/Template-POTD/'
|
POTD_BASE_URL = 'http://en.wikipedia.org/wiki/Template:POTD/'
|
||||||
|
#POTD_BASE_URL = 'http://127.0.0.1:{}/Template-POTD/'.format(HTTP_PORT)
|
||||||
|
|
||||||
REMOTE_PICT_BASE_URL = 'http://upload.wikimedia.org/wikipedia/'
|
REMOTE_PICT_BASE_URL = 'http://upload.wikimedia.org/wikipedia/'
|
||||||
LOCAL_PICT_BASE_URL = 'http://127.0.0.1:8001/'
|
#LOCAL_PICT_BASE_URL = 'http://127.0.0.1:{}/'.format(HTTP_PORT)
|
||||||
PICT_BASE_URL = LOCAL_PICT_BASE_URL
|
LOCAL_PICT_BASE_URL = REMOTE_PICT_BASE_URL
|
||||||
|
PICT_BASE_URL = REMOTE_PICT_BASE_URL
|
||||||
|
|
||||||
POTD_IMAGE_RE = re.compile(r'src="(//upload\..*?)"')
|
POTD_IMAGE_RE = re.compile(r'src="(//upload\..*?)"')
|
||||||
PODT_EARLIEST_TEMPLATE = '2007-01-01'
|
PODT_EARLIEST_TEMPLATE = '2007-01-01'
|
||||||
|
|||||||
3
concurrency/wikipedia/delay.sh
Executable file
3
concurrency/wikipedia/delay.sh
Executable file
@@ -0,0 +1,3 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
vaurien --protocol http --proxy localhost:8002 --backend localhost:8001 \
|
||||||
|
--behavior 100:delay --behavior-delay-sleep .1
|
||||||
42
control/adder/adder.py
Normal file
42
control/adder/adder.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def ask():
|
||||||
|
prompt = '>'
|
||||||
|
while True:
|
||||||
|
response = input(prompt)
|
||||||
|
if not response:
|
||||||
|
return 0
|
||||||
|
yield response
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
yield from iter(sys.argv[1:])
|
||||||
|
|
||||||
|
|
||||||
|
def fetch(producer):
|
||||||
|
gen = producer()
|
||||||
|
next(gen)
|
||||||
|
yield from gen
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
if args:
|
||||||
|
producer = parse_args
|
||||||
|
else:
|
||||||
|
producer = ask
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
count = 0
|
||||||
|
gen = fetch(producer())
|
||||||
|
while True:
|
||||||
|
term = yield from gen
|
||||||
|
term = float(term)
|
||||||
|
total += term
|
||||||
|
count += 1
|
||||||
|
average = total / count
|
||||||
|
print('total: {} average: {}'.format(total, average))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv[1:])
|
||||||
37
control/adder/coroadder0.py
Normal file
37
control/adder/coroadder0.py
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
"""
|
||||||
|
Closing a generator raises ``GeneratorExit`` at the pending ``yield``
|
||||||
|
|
||||||
|
>>> adder = adder_coro()
|
||||||
|
>>> next(adder)
|
||||||
|
>>> adder.send(10)
|
||||||
|
>>> adder.send(20)
|
||||||
|
>>> adder.send(30)
|
||||||
|
>>> adder.close()
|
||||||
|
-> total: 60 terms: 3 average: 20.0
|
||||||
|
|
||||||
|
|
||||||
|
Other exceptions propagate to the caller:
|
||||||
|
|
||||||
|
>>> adder = adder_coro()
|
||||||
|
>>> next(adder)
|
||||||
|
>>> adder.send(10)
|
||||||
|
>>> adder.send('spam')
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
def adder_coro(initial=0):
|
||||||
|
total = initial
|
||||||
|
num_terms = 0
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
term = yield
|
||||||
|
total += term
|
||||||
|
num_terms += 1
|
||||||
|
except GeneratorExit:
|
||||||
|
average = total / num_terms
|
||||||
|
msg = '-> total: {} terms: {} average: {}'
|
||||||
|
print(msg.format(total, num_terms, average))
|
||||||
47
control/adder/yetanother.py
Normal file
47
control/adder/yetanother.py
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import sys
|
||||||
|
import collections
|
||||||
|
|
||||||
|
Result = collections.namedtuple('Result', 'total average')
|
||||||
|
|
||||||
|
def adder():
|
||||||
|
total = 0
|
||||||
|
count = 0
|
||||||
|
while True:
|
||||||
|
term = yield
|
||||||
|
try:
|
||||||
|
term = float(term)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
total += term
|
||||||
|
count += 1
|
||||||
|
return Result(total, total/count)
|
||||||
|
|
||||||
|
def process_args(coro, args):
|
||||||
|
for arg in args:
|
||||||
|
coro.send(arg)
|
||||||
|
try:
|
||||||
|
next(coro)
|
||||||
|
except StopIteration as exc:
|
||||||
|
return exc.value
|
||||||
|
|
||||||
|
|
||||||
|
def prompt(coro):
|
||||||
|
while True:
|
||||||
|
term = input('+> ')
|
||||||
|
try:
|
||||||
|
coro.send(term)
|
||||||
|
except StopIteration as exc:
|
||||||
|
return exc.value
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
coro = adder()
|
||||||
|
next(coro) # prime it
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
res = process_args(coro, sys.argv[1:])
|
||||||
|
else:
|
||||||
|
res = prompt(coro)
|
||||||
|
print(res)
|
||||||
|
|
||||||
|
main()
|
||||||
69
control/countdown_yf.py
Normal file
69
control/countdown_yf.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
from time import sleep
|
||||||
|
|
||||||
|
def countdown(n):
|
||||||
|
while n:
|
||||||
|
print('\tn ->', n)
|
||||||
|
yield n
|
||||||
|
n -= 1
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
def foo():
|
||||||
|
for i in range(6, 3, -1):
|
||||||
|
yield i
|
||||||
|
yield from countdown(3)
|
||||||
|
|
||||||
|
#for j in foo():
|
||||||
|
# print('j ->', j)
|
||||||
|
|
||||||
|
|
||||||
|
def squares(n):
|
||||||
|
yield from [i for i in range(n)]
|
||||||
|
yield from [i*i for i in range(n)]
|
||||||
|
|
||||||
|
def squares_stupid(n):
|
||||||
|
for i in range(n):
|
||||||
|
yield i
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
yield i*i
|
||||||
|
|
||||||
|
#for s in squares(10):
|
||||||
|
# print(s)
|
||||||
|
|
||||||
|
|
||||||
|
def tokenize():
|
||||||
|
while True:
|
||||||
|
source = input('> ')
|
||||||
|
try:
|
||||||
|
obj = eval(source)
|
||||||
|
except BaseException:
|
||||||
|
print('*crash*')
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
it = iter(obj)
|
||||||
|
except TypeError:
|
||||||
|
yield obj
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
yield from it
|
||||||
|
|
||||||
|
#g = tokenize()
|
||||||
|
|
||||||
|
#for res in g:
|
||||||
|
# print(res)
|
||||||
|
|
||||||
|
|
||||||
|
from concurrent.futures import Future
|
||||||
|
|
||||||
|
def f():
|
||||||
|
f = future()
|
||||||
|
|
||||||
|
def foo(fut):
|
||||||
|
print(fut, fut.result())
|
||||||
|
f = Future()
|
||||||
|
f.add_done_callback(foo)
|
||||||
|
f.set_result(42)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
61
control/simulation/taxi_sim.py
Normal file
61
control/simulation/taxi_sim.py
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import sys
|
||||||
|
import random
|
||||||
|
import collections
|
||||||
|
import queue
|
||||||
|
|
||||||
|
Event = collections.namedtuple('Event', 'time actor description')
|
||||||
|
|
||||||
|
FIND_CUSTOMER_INTERVAL = 4
|
||||||
|
TRIP_DURATION = 10
|
||||||
|
|
||||||
|
|
||||||
|
def compute_delay(interval):
|
||||||
|
return int(random.expovariate(1/interval))
|
||||||
|
|
||||||
|
|
||||||
|
def taxi_process(sim, ident):
|
||||||
|
trips = 3
|
||||||
|
for i in range(trips):
|
||||||
|
prowling_time = compute_delay(FIND_CUSTOMER_INTERVAL)
|
||||||
|
yield Event(sim.time + prowling_time, ident, 'customer picked up')
|
||||||
|
|
||||||
|
trip_time = compute_delay(TRIP_DURATION)
|
||||||
|
yield Event(sim.time + trip_time, ident, 'customer dropped off')
|
||||||
|
|
||||||
|
|
||||||
|
class Simulator:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.events = queue.PriorityQueue()
|
||||||
|
self.time = 0
|
||||||
|
|
||||||
|
def run(self, end_time):
|
||||||
|
taxis = [taxi_process(self, i) for i in range(3)]
|
||||||
|
while self.time < end_time:
|
||||||
|
for index, taxi in enumerate(taxis):
|
||||||
|
try:
|
||||||
|
future_event = next(taxi)
|
||||||
|
except StopIteration:
|
||||||
|
del taxis[index] # remove taxi not in service
|
||||||
|
else:
|
||||||
|
self.events.put(future_event)
|
||||||
|
if self.events.empty():
|
||||||
|
print('*** end of events ***')
|
||||||
|
break
|
||||||
|
event = self.events.get()
|
||||||
|
self.time = event.time
|
||||||
|
print('taxi:', event.actor, event)
|
||||||
|
else:
|
||||||
|
print('*** end of simulation time ***')
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
if args:
|
||||||
|
end_time = int(args[0])
|
||||||
|
else:
|
||||||
|
end_time = 10
|
||||||
|
sim = Simulator()
|
||||||
|
sim.run(end_time)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv[1:])
|
||||||
Reference in New Issue
Block a user