Declaring simple metric in python script (for local use only)

#1

I was testing out some python code (that I had worked on in a notebook) and provisioned it to remixed ItemFacilityInventoryParams type as a function in the .py file. It provisions just fine, but gets upset when I actually try to use it in the console. The python code has a SimpleMetric and TSDecl object which is only used locally in the function. I understand this is probably not best practice (i.e. should declare simple metric within the python script, should probably be declared with the other metrics)–but in general, is it not possible to do this (to declare a simple metric in a python function and use it within that function)?

I’m mainly interested in doing this because it helps with initial prototyping (I don’t have to change it in 10 places for the initial test).


Details:
Error in console:
Error executing command ~py-pipeline2/bin/python/tmp/ItemFacilityInventoryParams_mycoolthing_test#############.py Traceback (most recent call last): File “tmp/ItemFacilityInventoryParams_mycoolthing_test#############.py” line #### in <module> _c3_result=mycoolthing_test(sku_ids = c3_inputs.get(‘sku_ids’),starttime=_c3_inputs.get(‘starttime’),endtime=c3_inputs.get(‘endtime’)) File “tmp/ItemFacilityInventoryParams_mycoolthing_test#############.py” line ####, in mycoolthing_test value=”latestQuantity.value” TypeError: ‘C3Type’ object is not callable

Declaration:
In ItemFacilityInventoryParams.c3typ:
@py(env=‘pipeline2’)
mycoolthing_test: function(sku_ids: ![string], starttime: !string, endtime: !string): [double]

In ItemFacilityInventoryParams.py:
def mycoolthing_test(sku_ids,starttime,endtime):
import pandas as pd
import numpy as np

    #tried with and without these
#from helper_functions import from_dataset, get_c3, from_dataset
#c3 = get_c3(url="https://##-##-tenant-tag.c3iot.ai", tenant="tenant", tag="tag")


tsDeclObject3 = c3.TSDecl(
     data= "itemPurchaseOrderHistories",
     treatment= "SUM",
     overlapHandling= "MAX",
     start= "promisedReceivedDate.value",
     value= "latestQuantity.value"
)

purchase_orders_perID = c3.SimpleMetric(
    id = "purchase_orders_perID_ItemFacility",
    name = "purchase_orders_perID",
    srcType = "ItemFacilityInventoryParams",
    tsDecl = tsDeclObject3
)

    #put dummy output here, for testing
means=[1,2]
return means

Call made to console:
var res2 = ItemFacilityInventoryParams.mycoolthing_test(["",""],“2018-01-01”,“2019-01-01”)

0 Likes

#2

Hello,

This is due to a difference in the way the c3 object is instantiated depending on the “connector” used in your runtime environment, c3ShowFunk(ActionRuntime,"connector"). Currently, the default is “C3FastConnection” which uses pyc3. The connector being used in Jupyter is either “remote” or “remote-types” and is the recommended way for interacting with c3 . The py-pipeline2 runtime does not specify a connector and therefore uses the C3FastConnection. You will need to define a new runtime in your package.json and specify the “connector” as “remote” or “remote-types”.

0 Likes