예제 #1
0
def create_models():
    """
    Create a Qt-model-view models for listings, products and tasks.
    Additionally returns the related ``DataStore``.
    
    Returns
    -------
    listings_model, product_model, task_model, price_model, data_store
    """
    from clair.qtgui import TaskModel, ProductModel, ListingsModel, PriceModel
    from clair.coredata import Product, SearchTask, DataStore, \
                               make_listing_frame, make_price_frame
    
    fr = make_listing_frame(3)
    #All listings need unique ids
    fr["id"] = ["eb-110685959294", "eb-111014122908", "eb-457"]
    
    fr["training_sample"] = [1.0, 0.0, nan]
    fr["search_tasks"] = ["s-nikon-d90", "s-nikon-d70", "s-nikon-d90"]
    
    fr["expected_products"][0] = ["nikon-d90", "nikon-sb-24"]
    fr["expected_products"][1] = ["nikon-d70"]
    fr["expected_products"][2] = ["nikon-d90", "nikon-sb-24"]
    fr["products"] = [["nikon-d90"], ["nikon-d70"], ["nikon-d90"]]
    fr["products_absent"][0] = ["nikon-sb-24"]
    
    fr["thumbnail"][0] = "www.some.site/dir/to/thumb.pg"
    fr["image"][0] = "www.some.site/dir/to/img.pg"
    fr["title"] = [u"Nikon D90 super duper!", u"Süper Camera", None]
    fr["description"][0] = "Buy my old Nikon D90 camera <b>now</b>!"
    fr["prod_spec"][0] = {"Marke":"Nikon", "Modell":"D90"}
    fr["active"][0] = False
    fr["sold"] = [1., 1., 0.]
    fr["currency"][0] = "EUR"
    fr["price"] = [400., 150, 300]
    fr["shipping"][0] = 12.
    fr["type"][0] = "auction"
    fr["time"] = [datetime(2013,1,10), datetime(2013,2,2), datetime(2013,2,3)]
    fr["location"][0] = u"Köln"
    fr["postcode"][0] = u"50667"
    fr["country"][0] = "DE"
    fr["condition"][0] = 0.7
    fr["server"][0] = "Ebay-Germany"
    fr["server_id"] = ["110685959294", "111014122908", "457"] #ID of listing on server
    fr["final_price"][0] = True
#    fr["data_directory"] = ""
    fr["url_webui"][0] = "www.some.site/dir/to/web-page.html"
#     fr["server_repr"][0] = nan
    #Put our IDs into index
    fr.set_index("id", drop=False, inplace=True, verify_integrity=True)
    
    tasks = [SearchTask("s-nikon-d90", datetime(2000, 1, 1), "ebay-de", 
                        "Nikon D90", "daily", 100, 150, 300, "EUR", 
                        ["nikon-d90", "nikon-18-105-f/3.5-5.6--1"]),
            SearchTask("s-nikon-d70", datetime(2000, 1, 1), "ebay-de", 
                        "Nikon D70", "daily", 100, 75, 150, "EUR", 
                        ["nikon-d70", "nikon-18-105-f/3.5-5.6--1"]),]
    
    products = [Product("nikon-d90", "Nikon D90", "Nikon D90 DSLR camera.", 
                        ["Nikon", "D 90"], ["photo.system.nikon.camera",
                                            "photo.camera.system.nikon"]),
                Product("nikon-d70", "Nikon D70", "Nikon D70 DSLR camera.", 
                        ["Nikon", "D 70"], ["photo.system.nikon.camera",
                                            "photo.camera.system.nikon"])]
    
    pri = make_price_frame(3)
    pri["id"] = ["pri-123", "pri-456", "pri-457"]
    pri["price"] = [310., 150., 300.]
    pri["currency"] = ["EUR", "EUR", "EUR"]
    pri["condition"] = [0.7, 0.7, 0.7]
    pri["time"] = [datetime(2013,1,10), datetime(2013,2,2), datetime(2013,2,3)]
    pri["product"] = ["nikon-d90", "nikon-d70", "nikon-d90"]
    pri["listing"] = ["eb-123", "eb-456", "eb-457"]
    pri["type"] = ["observed", "observed", "observed"]
    pri["avg_period"] = None
    pri["avg_num_listings"] = None
    
    conf_dir = relative("../../example-data")
    data_store = DataStore(conf_dir, None)
    data_store.merge_listings(fr)
    data_store.set_products(products)
    data_store.add_tasks(tasks)
    data_store.merge_prices(pri)
    
    #The models are tested here, creating them may fail. 
    #Don't break all test, because a single model is broken.
    try:
        listings_model = ListingsModel()
        listings_model.setDataStore(data_store)
    except: #IGNORE:W0702
        print "Error! ``listings_model`` could not be initialized!"
        listings_model = None
    try:
        task_model = TaskModel()
        task_model.setDataStore(data_store)
    except: #IGNORE:W0702
        print "Error! ``task_model`` could not be initialized!"
        task_model = None
    try:
        product_model = ProductModel()
        product_model.setDataStore(data_store)
    except: #IGNORE:W0702
        print "Error! ``product_model`` could not be initialized!"
        product_model = None
    try:
        price_model = PriceModel()
        price_model.setDataStore(data_store)
    except: #IGNORE:W0702
        print "Error! ``price_model`` could not be initialized!"
        price_model = None
    
    return listings_model, product_model, task_model, price_model, data_store