Skip to content

Instantly share code, notes, and snippets.

@sfc-gh-pneedleman
Last active February 12, 2024 12:14
Show Gist options
  • Save sfc-gh-pneedleman/cd7ffccf20b85f9ff7bebaf2d49d5ccc to your computer and use it in GitHub Desktop.
Save sfc-gh-pneedleman/cd7ffccf20b85f9ff7bebaf2d49d5ccc to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION FAKER("LOCALES" VARCHAR(16777216), "PROVIDER" VARCHAR(16777216), "PARAMETERS" VARCHAR(16777216))
RETURNS VARIANT
LANGUAGE PYTHON
RUNTIME_VERSION = '3.8'
PACKAGES = ('faker')
HANDLER = 'compute'
AS '
import json, datetime, decimal
from faker import Faker
locale_list = [''en_US'', ''en_GB'',''de_DE'', ''de_AT'', ''de_CH'',''pl_PL'', ''fr_FR'', ''es_ES'', ''it_IT'', ''nl_NL'', ''dk_DK'', ''ru_RU'']
faker = Faker(locale_list)
def compute(locales, provider, parameters):
try:
fake = faker[locales]
except:
raise Exception (''Country not implemented.'')
if len(parameters) > 2:
data = fake.format(provider,parameters)
else:
data = fake.format(provider)
data = json.loads(json.dumps(data, default=default_json_transform).replace(''\\\\n'','', ''))
return data
# format incompatible data types
def default_json_transform(obj):
if isinstance(obj, decimal.Decimal):
return str(obj)
if isinstance(obj, (datetime.date, datetime.datetime)):
return obj.isoformat()
raise TypeError
';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment