Exemplo n.º 1
0
class RedditReader:
    def __init__(self):
        self.looper = Looper(wait=0.01)
        self.client = HttpsSocketAsync(self.looper, 'www.reddit.com')

    def request_top_post(self, subreddit, n=4):
        url = '/r/{0}/top.json?sort=top&t=day&limit={1}'.format(subreddit, n)
        self.client.request("GET", url, ExtensibleCallback(self.on_response))

    def on_response(self, response):
        if response.status == 200:
            json_body = json.loads(response.read())
            self.on_json(json_body)
        else:
            self.on_http_error(None, response)

    def on_json(self, response):
        for post in response['data']['children']:
            data = post["data"]
            print u"/r/{subreddit:<12} {ups:>4}↕{downs:<3} {title} ({url})".format(
                **data)

    def run(self):
        self.looper.enqueue_now(self.request_top_post, "python")
        self.looper.enqueue_now(self.request_top_post, "programming")
        self.looper.enqueue_now(self.request_top_post, "java")
        self.looper.enqueue_now(self.request_top_post, "android")
        self.looper.enqueue_now(self.request_top_post, "vive")
        self.looper.loop()

    def on_http_error(self, url, response):
        print "Error {0} at {1}".format(response.status, url)
        for header in response.getheaders():
            print header
        self.looper.stop()
Exemplo n.º 2
0
  def map( fibers_file, brain_file, lh_smoothwm_file, rh_smoothwm_file, output_fibers_file, scalar_name='smoothwm' ):
    '''
    '''

    actions = [_actions.FySurfaceMapAction( scalar_name, brain_file, lh_smoothwm_file, rh_smoothwm_file )]

    # start the mapping using the looper
    Looper.loop( fibers_file, output_fibers_file, actions )
Exemplo n.º 3
0
    def test_get(self):
        looper = Looper()

        client = AsyncHttpSocket(looper, "www.google.com")
        client.get("/", self.on_response)

        looper.loop()

        self.assertTrue(self.has_response)
Exemplo n.º 4
0
 def super_map( input_file, brain_file, lh_smoothwm_file, rh_smoothwm_file, neighbors, connectivity_matrix_file, left_crv_file, right_crv_file ):
   '''
   '''
   actions = [_actions.FySuperSurfaceMapAction( 'super', brain_file, lh_smoothwm_file, rh_smoothwm_file, connectivity_matrix_file, neighbors )]
   
   # start the mapping using the looper
   Looper.loop( input_file, os.devnull, actions, True )
   
   # clean-up
   actions[0].close_file(lh_smoothwm_file, rh_smoothwm_file,left_crv_file, right_crv_file )
Exemplo n.º 5
0
 def map(fibers_file, volume_files, output_fibers_file):
   '''
   '''
   actions = []
 
   for v in volume_files:
     volume_name = os.path.splitext(os.path.basename(v))[0]
     # if extension was .nii.gz, also get rid of .nii
     volume_name = volume_name.replace('.nii','')
     
     actions.append(_actions.FyMapAction(volume_name, v))
   
   # start the mapping using the looper
   Looper.loop(fibers_file, output_fibers_file, actions)
Exemplo n.º 6
0
  def threshold( fibers_file, scalar_name, valid_values, output_fibers_file ):
    '''
    '''

    # load fibers file
    streamlines, header = nibabel.trackvis.read( fibers_file )

    # check if the scalar_name exists
    scalar_names = header['scalar_name'].tolist()
    try:
      scalar_index = scalar_names.index( scalar_name )
    except:
      raise Exception( 'Scalar name was not found.' )


    actions = [_actions.FyThresholdAction( scalar_index, valid_values )]

    # start the thresholding using the looper
    Looper.loop( fibers_file, output_fibers_file, actions )
Exemplo n.º 7
0
from fizz_buzz_runner import FizzBuzzRunner
from printer import Printer
from inputer import Inputer
from looper import Looper

if __name__ == '__main__':
    inputer = Inputer()
    printer = Printer()
    runner = FizzBuzzRunner(inputer, printer)
    looper = Looper(runner, inputer)
    looper.loop()
Exemplo n.º 8
0
class LooperTest(unittest.TestCase):
    def setUp(self):
        self.looper = Looper(wait=0)

    def dummy(self):
        pass

    def dummy2(self):
        pass

    def stopper(self):
        self.looper.stop()

    def test_empty_loop_exits(self):
        self.looper.loop()

    def test_stop_stops(self):
        self.looper.enqueue_now(self.stopper)
        self.looper.enqueue_now(self.dummy)
        self.looper.enqueue_now(self.dummy)

        self.looper.loop()

        self.assertEquals(2, self.looper.size())

    def test_enqueue_now(self):
        # race condition - will fail sometimes
        time = Time.now()
        self.looper.enqueue_now(self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(time, self.looper._queue[0].time)

    def test_enqueue_next(self):
        self.looper.enqueue_now(self.dummy)
        self.looper.enqueue_next(self.dummy2)
        self.assertEquals(2, self.looper.size())
        self.assertEquals(0, self.looper._queue[0].time)
        self.assertEquals(self.dummy2, self.looper._queue[0].func)

    def test_enqueue_with_millis(self):
        # race condition - will fail sometimes
        time = Time.millis_from_now(1000)
        self.looper.enqueue(1000, self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(time, self.looper._queue[0].time)

    def test_enqueue_with_datetime(self):
        d = datetime(2016, 1, 1, 12, 12, 12)
        self.looper.enqueue(d, self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(Time.on_date(d), self.looper._queue[0].time)

    def test_enqueue_with_date(self):
        d = date(2016, 1, 1)
        self.looper.enqueue(d, self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(Time.on_date(d), self.looper._queue[0].time)

    def test_enqueue_with_timedelta(self):
        # race condition - will fail sometimes
        d = timedelta(milliseconds=340)
        self.looper.enqueue(d, self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(Time.after_timedelta(d), self.looper._queue[0].time)

    def test_enqueue_at(self):
        # race condition - will fail sometimes
        time = Time.millis_from_now(1000)
        self.looper.enqueue_at(time, self.dummy)
        self.assertEquals(1, self.looper.size())
        self.assertEquals(time, self.looper._queue[0].time)

    def test_size(self):
        self.assertEquals(0, self.looper.size())

        self.looper.enqueue_now(self.stopper)
        self.assertEquals(1, self.looper.size())

        self.looper.enqueue_now(self.stopper)
        self.assertEquals(2, self.looper.size())

        self.looper.loop()

        self.assertEquals(1, self.looper.size())
Exemplo n.º 9
0
SYSTEM_MASS = 1.5
SYSTEM_POSITION = vector(0, 0, 0)
SYSTEM_INITIAL_VELOCITY = vector(5, 0, 0)
SYSTEM_INITIAL_POSITION = vector(0, 0, 0)
DELTA_T = 0.003

# Setup Display window for visualization
scene = display(width=640, height=480, background=color.white, range=2.5, title="VPython Test")
# Create object for visualization
ball = sphere(color=color.blue, radius=0.10)
# Create a track behind object to visualize trajectory
trail = curve(color=color.green, radius=0.02)
# Create small sphere to mark the origin in Display window
origin = sphere(pos=SYSTEM_POSITION, color=color.yellow, radius=0.04)

ball.m = SYSTEM_MASS

# noinspection PyPropertyAccess
ball.pos = SYSTEM_INITIAL_POSITION
ball.vel = SYSTEM_INITIAL_VELOCITY

looper = Looper(delta_t=DELTA_T)
looper.open_file()
looper.loop(ball, vector(0, 0, 0), .867)
looper.loop(ball, vector(-18.1,0,0), .074)
looper.loop(ball, vector(0,0,0), .634)
looper.close_file()