Beispiel #1
0
def test_serialization_unserialization():

    trunk = Trunk.randomize('a', 30, 60, 20)

    s = trunk.serialize()

    unserialized = Trunk.unserialize(s)

    assert trunk == unserialized
Beispiel #2
0
    def unserialize(cls, obj):

        p = cls(obj["name"], obj["genre"], obj["max_trunk_length"], obj["pot_water_capacity"], obj["available_water"])

        p.base_trunk = Trunk.unserialize(obj["plant"])

        return p
Beispiel #3
0
def create_tree(tree, level, max_trunk_length):

    if level > 0:

        new_trunks = Trunk.next_children(tree.type, max_trunk_length)

        if level > 1:
            for t in new_trunks:
                create_tree(t, level - 1, max_trunk_length)

        tree.children = new_trunks
Beispiel #4
0
    def randomize(cls, name, start_level, max_trunk_length, pot_capacity, initial_water):

        p = cls(name, "random", max_trunk_length, pot_capacity, initial_water)

        p.max_trunk_length = max_trunk_length

        p.base_trunk = Trunk.randomize_default("a", max_trunk_length)

        create_tree(p.base_trunk, start_level, max_trunk_length)

        return p
Beispiel #5
0
 def __init__(self, data_list, x, y, bsize, sess):
     Trunk.__init__(self, data_list, x, y, bsize, sess)
Beispiel #6
0
    ################################################################################
    #Make trunk segments that will connect the nodes
    ################################################################################
    #a list of specific attachment points overrides the number of nodes argument
    if(config['attach_points']):
        config['nodes'] = len(config['attach_points'])
   
    #this object figures out how many cable segments are needed and what length each segment
    #should be.
    #trunk object figures out a list of attach_points after this object is instantiated
    trunk=Trunk( length=config['length']
            , nodes=config['nodes']
            , start_pad=config['start_pad']
            , end_pad=config['end_pad']
            , separation_min=config['separation_min']
            , start_attach=config['start_attach']
            , end_attach=config['end_attach']
            , random_attach=config['random_attach']
            , attach_error=config['attach_error']
            , attach_points=config['attach_points']
            , max_seg_length=(1/config['segments_per_meter'])
            )

    #feed back precise attach points into json data structure
    config['attach_points'] = trunk.attach_points

    #Prepare a data structure to hold cable models and trunk segment parameters
    #actual number of cables segments is between nnodes-1 and nnodes+1
    #add an extra cable segment if attach_point[0] != 0
    #add an extra cable segment if attach_point[-1] != config['length']
    #always declare space for segment[0] and segment[nodes+1] 
    #expect that segment[0] and segment[nodes+1] will be ignored later if they are not needed
Beispiel #7
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)

    def get(self, name, block=True, timeout=None):
        channel, payload = self.trunk.get(name, block=block, timeout=timeout)
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT id, message FROM pop_lock(%s)", (name,))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute("INSERT INTO trunk_queue (name, message) VALUES (%s, %s)", (name, message))
        self.trunk.notify(name)

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM trunk_queue WHERE name = %s", (name,))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM trunk_queue WHERE name = %s", (name,))
        return size

    def close(self):
        self.trunk.close()
Beispiel #8
0
 def __init__(self, dsn):
     self.trunk = Trunk(dsn)
Beispiel #9
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)

    def get(self, name, block=True, timeout=None):
        try:
            channel, payload = self.trunk.get(name,
                                              block=block,
                                              timeout=timeout)
        except Empty:
            pass
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT id, message FROM public.pop_lock(%s)",
                           (name, ))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute(
                "INSERT INTO public.trunk_queue (name, message) VALUES (%s, %s)",
                (name, message))
        self.trunk.notify(name)

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute(
                "SELECT COUNT(*) FROM public.trunk_queue WHERE name = %s",
                (name, ))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM public.trunk_queue WHERE name = %s",
                           (name, ))
        return size

    def close(self):
        self.trunk.close()
Beispiel #10
0
 def __init__(self, dsn):
     self.trunk = Trunk(dsn)
Beispiel #11
0
 def setUp(self):
     dsn = os.environ.get("DATABASE_URL")
     self.listener = Trunk(dsn or "postgres://localhost/trunk")
     self.notifier = Trunk(dsn or "postgres://localhost/trunk")
Beispiel #12
0
class TrunkTest(TestCase):
    def setUp(self):
        dsn = os.environ.get("DATABASE_URL")
        self.listener = Trunk(dsn or "postgres://localhost/trunk")
        self.notifier = Trunk(dsn or "postgres://localhost/trunk")

    def test_get(self):
        self.listener.listen("trunk_get")
        self.notifier. notify("trunk_get", "payload")
        channel, payload = self.listener.get("trunk_get")
        self.assertEqual(channel, "trunk_get")
        self.assertEqual(payload, "payload")

    def test_listen(self):
        self.listener.listen("trunk_listen")
        self.assertTrue("trunk_listen" in self.listener.channels())

    def test_unlisten(self):
        self.listener.listen("trunk_channels")
        self.assertTrue("trunk_channels" in self.listener.channels())
        self.listener.unlisten("trunk_channels")
        self.assertTrue("trunk_channels" not in self.listener.channels())

    def tearDown(self):
        self.listener.close()
        self.notifier.close()
Beispiel #13
0
class PGQueue(object):
    def __init__(self, dsn):
        self.trunk = Trunk(dsn)

    def create(self, name):
        self.trunk.listen(name)
        # This forces db trigger to resend notifications about pending messages.
        with self.trunk.cursor() as cursor:
            cursor.execute("UPDATE trunk_queue SET locked_at = NULL WHERE name = %s AND locked_at IS NULL", (name, ))

    def get(self, name, block=True, timeout=None):
        channel, payload = self.trunk.get(name, block=block, timeout=timeout)
        with self.trunk.cursor() as cursor:
            cursor.execute('UPDATE trunk_queue SET locked_at = (CURRENT_TIMESTAMP) '
                           'WHERE id = %s AND name = %s AND locked_at is NULL', (payload, name))
            row_count = cursor.rowcount
            if not row_count:
                raise Empty()
            cursor.execute("SELECT id, message FROM trunk_queue WHERE id = %s", (payload, ))
            row = cursor.fetchone()
            if row is None:
                raise Empty()
            return row

    def get_nowait(self, name):
        return self.get(name, block=False)

    def put(self, name, message):
        with self.trunk.cursor() as cursor:
            cursor.execute("INSERT INTO trunk_queue (name, message) VALUES (%s, %s)", (name, message))

    def empty(self, name):
        return 0 == self.qsize(name)

    def qsize(self, name):
        with self.trunk.cursor() as cursor:
            cursor.execute("SELECT COUNT(*) FROM trunk_queue WHERE name = %s", (name,))
            row = cursor.fetchone()
            return row[0]

    def purge(self, name):
        size = self.qsize(name)
        with self.trunk.cursor() as cursor:
            cursor.execute("DELETE FROM trunk_queue WHERE name = %s", (name,))
        return size

    def restore(self, message):
        message_id = message._raw['message_id']
        with self.trunk.cursor() as cursor:
            cursor.execute('UPDATE trunk_queue SET locked_at = NULL WHERE id = %s', (message_id, ))

    def close(self):
        self.trunk.close()
Beispiel #14
0
 def setUp(self):
     dsn = os.environ.get("DATABASE_URL")
     self.listener = Trunk(dsn or "postgres://localhost/trunk")
     self.notifier = Trunk(dsn or "postgres://localhost/trunk")
Beispiel #15
0
class TrunkTest(TestCase):
    def setUp(self):
        dsn = os.environ.get("DATABASE_URL")
        self.listener = Trunk(dsn or "postgres://localhost/trunk")
        self.notifier = Trunk(dsn or "postgres://localhost/trunk")

    def test_get(self):
        self.listener.listen("trunk_get")
        self.notifier.notify("trunk_get", "payload")
        channel, payload = self.listener.get("trunk_get")
        self.assertEqual(channel, "trunk_get")
        self.assertEqual(payload, "payload")

    def test_listen(self):
        self.listener.listen("trunk_listen")
        self.assertTrue("trunk_listen" in self.listener.channels())

    def test_unlisten(self):
        self.listener.listen("trunk_channels")
        self.assertTrue("trunk_channels" in self.listener.channels())
        self.listener.unlisten("trunk_channels")
        self.assertTrue("trunk_channels" not in self.listener.channels())

    def tearDown(self):
        self.listener.close()
        self.notifier.close()