handler
withpd.inputs["data_store"]
.def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Store a value under a key data_store["key"]= "Hello World" # Retrieve the value and print it to the step's Logs print(data_store["key"])
from datetimeimport datetimedef handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Store a timestamp data_store["last_ran_at"]= datetime.now().isoformat()
set
method. The TTL value is specified in seconds:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Store a temporary value that will expire after 1 hour (3600 seconds) data_store.set("temporaryToken","abc123",ttl=3600) # Store a value that will expire after 1 day data_store.set("dailyMetric",42,ttl=86400)
set_ttl
method:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Update an existing record to expire after 30 minutes data_store.set_ttl("temporaryToken",ttl=1800) # Remove expiration from a record data_store.set_ttl("temporaryToken",ttl=None)
keys
method:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Retrieve all keys in the data store keys= pd.inputs["data_store"].keys() # Print a comma separated string of all keys print(*keys,sep=",")
datastore.keys()
method does not return a list, but instead it returns aKeys
iterable object. You cannot export adata_store
ordata_store.keys()
from a Python code step at this time.Instead, build a dictionary or list when using thedata_store.keys()
method.key
exists in a data store, useif
andin
as a conditional:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Search for a key in a conditional if "last_ran_at" in data_store: print(f"Last ran at{data_store['last_ran_at']}")
def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Retrieve the timestamp value by the key name last_ran_at= data_store["last_ran_at"] # Print the timestamp print(f"Last ran at{last_ran_at}")
data_store.get()
method to retrieve a specific key’s contents:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Retrieve the timestamp value by the key name last_ran_at= data_store.get("last_ran_at") # Print the timestamp print(f"Last ran at{last_ran_at}")
data_store["key"]
anddata_store.get("key")
?data_store["key"]
will throw aTypeError
if the key doesn’t exist in the data store.data_store.get("key")
will instead returnNone
if the key doesn’t exist in the data store.data_store.get("key", "default_value")
will return"default_value"
if the key doesn’t exist on the data store.def handler(pd:"pipedream"): data_store= pd.inputs["data_store"] records= {} for k,vin data_store.items(): records[k]= v return records
key
a new value or''
to remove the value but retain the key.def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Assign a new value to the key data_store["myKey"]= "newValue" # Remove the value but retain the key data_store["myKey"]= ""
name
attribute on the stored dictionary stored under the keypokemon
:def handler(pd:"pipedream"): # The current dictionary looks like this: # pokemon: { # "name": "Charmander" # "type": "fire" # } # You'll see "Charmander" in the logs print(pd.inputs['data_store']['pokemon']['name']) # attempting to overwrite the pokemon's name will not apply pd.inputs['data_store']['pokemon']['name']= 'Bulbasaur' # Exports "Charmander" return pd.inputs['data_store']['pokemon']['name']
def handler(pd:"pipedream"): # retrieve the record item by it's key first pokemon= pd.inputs['data_store']['pokemon'] # now update the record's attribute pokemon['name']= 'Bulbasaur' # and out right replace the record with the new modified dictionary pd.inputs['data_store']['pokemon']= pokemon # Now we'll see "Bulbasaur" exported return pd.inputs['data_store']['pokemon']['name']
del
operation for a specifickey
:def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Delete the last_ran_at timestamp key del data_store["last_ran_at"]
clear
method.def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # Delete the entire contents of the datas store data_store.clear()
data_store.clear()
is anirreversible change,even when testing code in the workflow builder.def handler(pd:"pipedream"): # Access the data store under the pd.inputs data_store= pd.inputs["data_store"] # if the counter doesn't exist yet, start it at one if data_store.get("counter")== None: data_store["counter"]= 1 # Otherwise, increment it by one else: count= data_store["counter"] data_store["counter"]= count+ 1
def handler(pd:"pipedream"): # Access the data store data_store= pd.inputs["data_store"] # Reference the incoming email from the HTTP request new_email= pd.steps["trigger"]["event"]["body"]["new_customer_email"] # Retrieve the emails stored in our data store emails= data_store.get('emails', []) # If this email has been seen before, exit early if new_emailin emails: print(f"Already seen{new_email}, exiting") return False # This email is new, append it to our list else: print(f"Adding new email to data store{new_email}") emails.append(new_email) data_store["emails"]= emails return new_email
def handler(pd:"pipedream"): # Access the data store data_store= pd.inputs["data_store"] user_id= pd.steps["trigger"]["event"]["user_id"] rate_key= f"ratelimit:{user_id}" # Try to get current rate limit counter requests_num= data_store.get("rate_key") if not requests_num: # First request from this user in the time window data_store.set(rate_key,1,ttl=3600)# Expire after 1 hour return {"allowed":True,"remaining":4 } if requests_num>= 5: # Rate limit exceeded return {"allowed":False,"error":"Rate limit exceeded","retry_after":"1 hour" } # Increment the counter data_store["rate_key"]= requests_num+ 1 return {"allowed":True,"remaining":4 - requests_num }
Was this page helpful?