Ejemplo n.º 1
0
def main():
    parser = argparse.ArgumentParser(
        description=
        "Graph the simulation results, using IPC to access the simulation's in-memory database."
    )
    parser.add_argument('--use_db',
                        type=str,
                        default='false',
                        help='set to "true to use database instead of IPC')
    args = parser.parse_args()

    use_db = args.use_db.lower() == "true"

    if use_db:
        conn = Db(DB_PATH)
        run = Run(db=conn)
    else:
        conn = Ipc()
        run = Run(ipc=conn)

    run_name = "run{}".format(run.run_id)
    run_dir = "{}/{}".format(IMG_DIR, run_name)
    if os.path.exists(run_dir):
        shutil.rmtree(run_dir)
    os.makedirs(run_dir)

    if use_db:
        graph_gen = GraphGen(run, db=conn, run_dir=run_dir)
    else:
        graph_gen = GraphGen(run, ipc=conn, run_dir=run_dir)

    graph_gen.plot_cumulative_best_fitness()
    graph_gen.plot_avg_fitness()

    conn.close()
Ejemplo n.º 2
0
    def __init__(self):
        super().__init__()

        self.db = Db()
        self.session = self.db.get_session()

        products = self.session.query(Product).all()
        self.products = {
            ProductService.build_url(product): product
            for product in products
        }

        if not self.products:
            return

        self.start_urls = self.products.keys()

        brands = self.session.query(Brand).filter(
            Brand.id.in_([product.brand_id for product in products])).all()
        self.brands = {brand.title: brand for brand in brands}

        category_ids = self.session.query(func.unnest(Product.category_ids)) \
            .filter(Product.id.in_([product.id for product in products])).distinct()
        categories = self.session.query(Category) \
            .filter(Category.id.in_(category_ids)).all()

        self.categories = {
            catalog_category.title: catalog_category
            for catalog_category in categories
        }
Ejemplo n.º 3
0
    def __init__(self, url=''):
        super().__init__()

        self.db = Db()
        self.session = self.db.get_session()

        if url != '':
            self.start_urls.append(url)
Ejemplo n.º 4
0
def HomeService(args):

    response = {}
    house_object = {}
    houses_array = []

    # database query
    sql = '''SELECT house_id, title, description, rent, images FROM house ORDER BY house_id DESC'''
    rows = Db().select(sql)
    if rows:
        for row in rows:
            house_object['house_id'] = row['house_id']
            house_object['title'] = row['title']
            house_object['description'] = row['description']
            house_object['rent'] = row['rent']
            house_object['images'] = json.loads(row['images'])

            # storing house data in array of objects
            houses_array.append(house_object)
            house_object = {}

        response['status'] = "Success"
        response['data'] = houses_array
        return response
    else:
        response['status'] = "Failure"
        response['error'] = "Could not find data of houses"
        return response
Ejemplo n.º 5
0
def RegisterService(args):

    request = dict(args)
    response = {}

    # parameter checks
    if not 'email' in request:
        return Failure("email")

    if not 'password' in request:
        return Failure("passord")

    if not 'name' in request:
        return Failure("name")

    if not 'mobile' in request:
        return Failure("mobile")

    # database query
    sql = '''INSERT INTO user (`name`,`email`,`mobile`,`password`) VALUES (%s, %s, %s, %s);'''
    val = (request['name'], request['email'], request['mobile'],
           request['password'])

    user_id = Db().insert(sql, val)
    if isinstance(user_id, int):
        response['status'] = "Success"
        return response
    else:
        response['status'] = "Failure"
        response['error'] = user_id
        return response
Ejemplo n.º 6
0
def HouseService(args):

    request = dict(args)
    response = {}

    # parameter checks
    if not 'house_id' in request:
        return Failure("house_id")

    # database query
    sql = '''SELECT * from house INNER JOIN user on house.user_id = user.user_id WHERE house_id = %s''' % (
        request['house_id'])
    rows = Db().select(sql)
    if rows:
        for row in rows:
            response['house_id'] = row['house_id']
            response['title'] = row['title']
            response['description'] = row['description']
            response['rent'] = row['rent']
            response['address'] = row['address']
            response['images'] = json.loads(row['images'])
            response['name'] = row['name']
            response['email'] = row['email']
            response['mobile'] = row['mobile']

            response['status'] = "Success"
            return response

    else:
        return Failure("correct house_id")
Ejemplo n.º 7
0
def LoginService(args):

    request = dict(args)
    response = {}

    # parameter checks
    if not 'email' in request:
        return Failure("email")

    if not 'password' in request:
        return Failure("password")

    # database query
    sql = '''SELECT * FROM user WHERE email="%s" AND password="******"''' % (
        request['email'], request['password'])

    rows = Db().select(sql)
    if rows:
        for row in rows:
            response['user_id'] = row['user_id']
            response['name'] = row['name']
            response['email'] = row['email']
            response['mobile'] = row['mobile']
            response['password'] = row['password']

        response['status'] = "Success"
        return response
    else:
        return Failure("correct email and password")
Ejemplo n.º 8
0
class ProductsSpider(scrapy.Spider):
    name = "products"

    def __init__(self):
        super().__init__()

        self.db = Db()
        self.session = self.db.get_session()

        products = self.session.query(Product).all()
        self.products = {
            ProductService.build_url(product): product
            for product in products
        }

        if not self.products:
            return

        self.start_urls = self.products.keys()

        brands = self.session.query(Brand).filter(
            Brand.id.in_([product.brand_id for product in products])).all()
        self.brands = {brand.title: brand for brand in brands}

        category_ids = self.session.query(func.unnest(Product.category_ids)) \
            .filter(Product.id.in_([product.id for product in products])).distinct()
        categories = self.session.query(Category) \
            .filter(Category.id.in_(category_ids)).all()

        self.categories = {
            catalog_category.title: catalog_category
            for catalog_category in categories
        }

    def parse(self, response, **kwargs):
        loader = ItemLoader(item=ProductItem(), response=response)

        loader.add_value('url', response.url)
        loader.add_xpath('code', '//span[@class="j-article"]/text()')
        loader.add_xpath(
            'picker',
            '//div[contains(@class, "colorpicker")]/ul/li/@data-cod1s')
        loader.add_xpath('brand', '//span[@class="brand"]/text()')
        loader.add_xpath('name', '//span[@class="name"]/text()')
        loader.add_xpath(
            'images',
            '//div[contains(@class, "pv-carousel")]//a[contains(@class, "j-carousel-image")]/@href'
        )
        loader.add_xpath('price', '//span[@class="final-cost"]/text()')
        loader.add_xpath(
            'description',
            '//div[contains(@class, "description-text")]/p/text()')
        loader.add_xpath('categories', '//ul[@class="bread-crumbs"]/li/a')
        loader.add_xpath(
            'sizes',
            '//div[contains(@class, "size-list") and not(contains(@class, "hide"))]/label[not(contains(@class, "disabled"))]/@data-size-name'
        )
        loader.add_xpath('params', '//div[@class="params"]//div[@class="pp"]')

        return loader.load_item()
Ejemplo n.º 9
0
class ProductsSpider(Spider):
    name = "url"

    def __init__(self, url=''):
        super().__init__()

        self.db = Db()
        self.session = self.db.get_session()

        if url != '':
            self.start_urls.append(url)

    def parse(self, response, **kwargs):
        for href in response.css(
                'a.j-open-full-product-card::attr(href)').extract():
            self.process_product_page(response.urljoin(href))

        active_page = int(
            response.css('span.pagination-item.active::text').get())
        next_pages = response.css('a.pagination-item::attr(href)').extract()

        for next_page in next_pages:
            result = re.findall(r'page=(\d+)', next_page)

            if result and int(result[0]) > active_page:
                yield Request(response.urljoin(next_page), callback=self.parse)

    def process_product_page(self, href):
        product_data = ProductService.parse_product_url(href)

        if product_data:
            product = self.session.query(Product).filter_by(
                domain=product_data['domain'],
                code=product_data['code']).first()

            if not product:
                self.session.add(
                    Product(domain=product_data['domain'],
                            code=product_data['code'],
                            status=Product.STATUS_NEW))
                self.session.commit()
Ejemplo n.º 10
0
def AddService(args):

    request = dict(args)
    response = {}

    # parameter checks
    if not 'user_id' in request:
        return Failure("user_id")

    if not 'title' in request:
        return Failure("title")

    if not 'description' in request:
        return Failure("description")

    if not 'rent' in request:
        return Failure("rent")

    if not 'address' in request:
        return Failure("address")

    if not 'images' in request:
        return Failure("images")

    # database query
    sql = '''INSERT INTO house (`user_id`,`title`,`description`,`rent`,`address`,`images`) VALUES (%s, %s, %s, %s, %s, %s);'''
    val = (request['user_id'], request['title'], request['description'],
           request['rent'], request['address'], json.dumps(request['images']))

    house_id = Db().insert(sql, val)
    if isinstance(house_id, int):
        response['status'] = "Success"
        return response
    else:
        response['status'] = "Failure"
        response['error'] = house_id
        return response
Ejemplo n.º 11
0
    def __init__(self, db_path):
        Gtk.Window.__init__(self, title='Viewer')
        self.set_default_size(ViewWindow.WIDTH, ViewWindow.HEIGHT)
        self.maximize()

        self.db = Db(db_path)
        self.run = Run(self.db)
        self.tree_gen = TreeGen(self.db)
        self.graph_gen = GraphGen(self.run, db=self.db)

        #we'll simulate an MRU (most recently used) cache using an OrderedDict
        #it's not the most efficient, but it's good enough for this application
        self.tree_cache = OrderedDict()
        self.grn_cache = OrderedDict()

        vbox = Gtk.VBox()
        paned = Gtk.Paned()
        left_scroll = Gtk.ScrolledWindow()
        self.tree_img = Gtk.Image()
        left_scroll.add_with_viewport(self.tree_img)
        paned.add1(left_scroll)

        right_scroll = Gtk.ScrolledWindow()
        self.grn_img = Gtk.Image()
        right_scroll.add_with_viewport(self.grn_img)
        paned.add2(right_scroll)
        paned.set_position(ViewWindow.WIDTH // 2)

        vbox.pack_start(paned, True, True, 0)
        vbox.pack_end(self._build_bottom_panel(), False, False, 0)

        self.add(vbox)

        self.show_all()

        self.update_views()