A function to check if a block already exists woul...
# prefect-community
j
A function to check if a block already exists would be helpful
👍 1
a
Hey @Jared Robbins, thanks for the suggestion! Could you provide an example of when such a function would be helpful?
j
Conditionally running a task based on last time it was run. There is no last time if it is the first time.
a
Gotcha, so in that case it sounds like you would use a
DateTime
block to hold the last run time and that value would be saved on each run. To handle the case where a block document doesn’t exist, you can run
DateTime.load("my-block-name")
in a try clause and catch the
ValueError
that is thrown if a block document with that name doesn’t exist. You then would be able to create the block in the except clause.
j
Hm, i’ll try that again tomorrow. For some reason it wasn’t catching the error.
Man… it was because I was creating a new block using String(‘’) in the except block. Forgot to specify value= On the bright side, the whole thing is working great now :)