romaklimenko / cluedin

CluedIn Python SDK
MIT License
4 stars 0 forks source link

Flatten entity properties #25

Closed romaklimenko closed 9 months ago

romaklimenko commented 9 months ago

We already have the entries method:

Instead of writing this every time:


def flatten_properties(d):
    for k, v in d['properties'].items():
        if k == 'attribute-type':
            continue

        if k.startswith('property-'):
            k = k[9:] # len('property-') == 9

        k = k.replace('.', '_')

        d[k] = v

    del d['properties']

    return d

df_titles = pd.DataFrame(
    map(
        flatten_properties,
        cluedin.gql.entries(ctx, query, { 'query': 'entityType:/IMDb/Title', 'pageSize': 10_000 })))

Add cluedin.utils.flatten_properties method and use it by default for cluedin.gql.entries:

df_titles = pd.DataFrame(cluedin.gql.entries(ctx, query, { 'query': 'entityType:/IMDb/Title', 'pageSize': 10_000 }))

Which is equivalent to:

df_titles = pd.DataFrame(
  cluedin.gql.entries(
    context=ctx,
    query=query,
    variables={ 'query': 'entityType:/IMDb/Title', 'pageSize': 10_000 },
    flat=True))