Beispiel #1
0
def get_qty_from_OFF(df_P, OFF_csv):
    """
    STEP 4
    Get product quantity from OFF database
    Args:
        df_P (DataFrame): Product DataFrame
        OFF_CSV (str): Path to OFF DB in csv
    Returns:
        df_P (DataFrame): Product DataFrame with updated quantity
    """
    import off_mgt as off
    
    ddf_OFF = load_OFF(OFF_csv,col=["code", "product_name","quantity"])
    df_OFF = ddf_OFF.compute()
    
    filter_ = (df_P.Qty_std==False)
    df_PF = df_P[filter_]
    df_PF = merge_tables(df_PF,df_OFF,on_col=['P_EAN','code'],col_name='P_EAN',how='inner',del_col=False)
    df_PF.drop_duplicates('P_Id',inplace=True)
    
    df_PF['Q_'] = df_PF.quantity.apply(lambda name: off.convert_quantity(name))

    df_PF['Qty_val']    = [q.get('val')    for q in df_PF.Q_]
    df_PF['Qty_unit']   = [q.get('unit')   for q in df_PF.Q_]
    df_PF['Qty_std']    = [q.get('std')    for q in df_PF.Q_]
    df_PF['Qty_approx'] = [q.get('approx') for q in df_PF.Q_]
    df_PF['Qty_method'] = 4

    df_PF.drop(['product_name','quantity','Q_'],axis=1,inplace=True)
    df_PF.set_index(['P_Id'],inplace=True,drop=False)
    
    df_P.update(df_PF)
    print('STEP 4 - %d (not std) %d (std)' % (len(df_P[df_P.Qty_std==False]),len(df_P[df_P.Qty_std==True])))
    return df_P
def add_product_qty(df_P):
    """
    STEP 1
    Get product quantity from Phenix product name
    Args:
        df_P (DataFrame): Product DataFrame
    Returns:
        df_P (DataFrame): Product DataFrame with updated quantity
    """
    import off_mgt as off
    print('-- Adding Product quantity column --')
    print('WARNING - This function is time consuming.')
    df_P1 = df_P.copy()

    # First try to extract quantity from product name
    df_P1['Qty'] = df_P1.P_Nom.apply(lambda name: off.convert_quantity(name))
    df_P1['Qty_val'] = [q.get('val') for q in df_P1.Qty]
    df_P1['Qty_unit'] = [q.get('unit') for q in df_P1.Qty]
    df_P1['Qty_std'] = [q.get('std') for q in df_P1.Qty]
    df_P1['Qty_approx'] = [q.get('approx') for q in df_P1.Qty]
    df_P1['Qty_method'] = 1
    print('STEP 1 - %d (not std) %d (std)' % (len(
        df_P1[df_P1.Qty_std == False]), len(df_P1[df_P1.Qty_std == True])))

    return df_P1
    def test_convert(self):

        for s in self.test_data.keys():
            s_converted = convert_quantity(s)
            s_compare = {
                'val': s_converted['val'],
                'unit': s_converted['unit']
            }
            try:
                self.assertEqual(s_compare, self.test_data[s])
            except AssertionError:
                print('Not correctly handled :', s)
                print('converted dict is:', s_compare)
                print('Correct dict is:', self.test_data[s])