Skip to content

Commit

Permalink
chore: remove __main__ code in context
Browse files Browse the repository at this point in the history
  • Loading branch information
z3z1ma committed Nov 11, 2024
1 parent cf01441 commit 1e09968
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions src/cdf/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,44 +742,3 @@ def decorator(func: t.Callable[P, T]) -> t.Callable[P, T]:
return decorator(name_or_func)

return decorator


if __name__ == "__main__":
ctx = Context(
loader=SimpleConfigurationLoader(
# Example configuration sources, dictionary, file path, callable
# with converters and environment variable expansion
{"name": "${USER}", "age": 30},
{"model": "SVM", "num_iter": "@int 35"},
lambda: {"processor": "add_one", "seq": "@tuple (1,2,3)"},
lambda: {"model_A": "@float @resolve age"},
"pyproject.toml",
{"dependency_paths": ["path/ok"]},
),
namespace="foo",
)
# Access configuration by index or attr (configs are merged)
print(ctx.config["name"], ctx.config.age)
# Num iter is converted to int via converter
print(ctx.config.num_iter, type(ctx.config.num_iter))
# A file path is used to load configuration based on extension
print(ctx.config["project"])
# A @resolve converter is used to self-reference a configuration value
print(ctx.config.model_A)
# Env vars are made available (case-sensitive)
print(ctx.config.USER)

active_context.set(ctx)

@register_dep("bar")
def foo(context: Context, configuration: dict) -> bool:
assert context is ctx, "Context is not the same"
assert configuration is ctx.config, "Config is not the same"
return True

# Inject dependencies
print(ctx["bar"])

ctx_other = Context(loader=SimpleConfigurationLoader({"name": "Alice"}))
ctx_merged = ctx.combine(ctx_other)
print(ctx_merged.config.name)

0 comments on commit 1e09968

Please sign in to comment.