pyfect-dev 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyfect/__init__.py +729 -0
- pyfect/_all.py +242 -0
- pyfect/_background.py +28 -0
- pyfect/_binary_codecs.py +124 -0
- pyfect/_cached.py +165 -0
- pyfect/_catch.py +128 -0
- pyfect/_catch_reason.py +574 -0
- pyfect/_concurrency.py +291 -0
- pyfect/_context_effects.py +163 -0
- pyfect/_context_managers.py +76 -0
- pyfect/_core.py +368 -0
- pyfect/_digits.py +71 -0
- pyfect/_effect_args.py +46 -0
- pyfect/_effect_base.py +71 -0
- pyfect/_effect_concurrency.py +133 -0
- pyfect/_effect_eager.py +103 -0
- pyfect/_effect_errors.py +344 -0
- pyfect/_effect_folding.py +110 -0
- pyfect/_effect_hash.py +140 -0
- pyfect/_effect_logging.py +57 -0
- pyfect/_effect_metrics.py +102 -0
- pyfect/_effect_resources.py +179 -0
- pyfect/_effect_services.py +25 -0
- pyfect/_effect_timing.py +199 -0
- pyfect/_effect_tracing.py +99 -0
- pyfect/_equal.py +195 -0
- pyfect/_extras.py +43 -0
- pyfect/_fiber.py +969 -0
- pyfect/_fiber_all.py +134 -0
- pyfect/_fiber_collections.py +106 -0
- pyfect/_fn.py +706 -0
- pyfect/_hamt.py +174 -0
- pyfect/_interrupt_reason.py +33 -0
- pyfect/_isaac.py +149 -0
- pyfect/_iterables.py +231 -0
- pyfect/_js.py +263 -0
- pyfect/_js_date.py +87 -0
- pyfect/_json_text.py +286 -0
- pyfect/_json_value.py +9 -0
- pyfect/_layer_core.py +403 -0
- pyfect/_layer_for_life.py +33 -0
- pyfect/_layer_tracing.py +70 -0
- pyfect/_locked_ref.py +147 -0
- pyfect/_loop_thread.py +152 -0
- pyfect/_metric_ref.py +29 -0
- pyfect/_one_or_many.py +14 -0
- pyfect/_os_wait.py +118 -0
- pyfect/_path_posix.py +129 -0
- pyfect/_primitives.py +434 -0
- pyfect/_public.py +36 -0
- pyfect/_reasons.py +64 -0
- pyfect/_released.py +67 -0
- pyfect/_root.py +87 -0
- pyfect/_round_robin.py +73 -0
- pyfect/_some.py +17 -0
- pyfect/_stack.py +52 -0
- pyfect/_stack_frame.py +114 -0
- pyfect/_string_hash.py +35 -0
- pyfect/_subscriber_queue.py +47 -0
- pyfect/_tags.py +14 -0
- pyfect/_tracer_ref.py +21 -0
- pyfect/_tx_ops.py +77 -0
- pyfect/_unsubscripted.py +23 -0
- pyfect/_url.py +54 -0
- pyfect/_waiters.py +127 -0
- pyfect/_wake_tracking.py +98 -0
- pyfect/_wire_codec.py +123 -0
- pyfect/ai/__init__.py +73 -0
- pyfect/ai/_approvals.py +249 -0
- pyfect/ai/_decision_answers.py +185 -0
- pyfect/ai/_generated_client.py +298 -0
- pyfect/ai/_generated_support.py +84 -0
- pyfect/ai/_mcp/__init__.py +1 -0
- pyfect/ai/_mcp/batch.py +68 -0
- pyfect/ai/_mcp/client_transport.py +412 -0
- pyfect/ai/_mcp/client_wire.py +284 -0
- pyfect/ai/_mcp/core.py +490 -0
- pyfect/ai/_mcp/dispatch.py +1048 -0
- pyfect/ai/_mcp/http.py +462 -0
- pyfect/ai/_mcp/http_admission.py +269 -0
- pyfect/ai/_mcp/http_status.py +27 -0
- pyfect/ai/_mcp/input_wire.py +226 -0
- pyfect/ai/_mcp/log_levels.py +55 -0
- pyfect/ai/_mcp/param_headers.py +63 -0
- pyfect/ai/_mcp/projection.py +248 -0
- pyfect/ai/_mcp/register.py +683 -0
- pyfect/ai/_mcp/reverse_policy.py +155 -0
- pyfect/ai/_mcp/routing_header.py +24 -0
- pyfect/ai/_mcp/sessions.py +114 -0
- pyfect/ai/_mcp/stdio.py +110 -0
- pyfect/ai/_mcp/subscriptions.py +134 -0
- pyfect/ai/_mcp/tool_schema.py +35 -0
- pyfect/ai/_noul_answers.py +64 -0
- pyfect/ai/_openai_all_of.py +137 -0
- pyfect/ai/_openai_errors.py +233 -0
- pyfect/ai/_provider.py +439 -0
- pyfect/ai/_provider_tools.py +129 -0
- pyfect/ai/_stream_resolution.py +231 -0
- pyfect/ai/_structured_output_checks.py +138 -0
- pyfect/ai/_structured_output_codec.py +229 -0
- pyfect/ai/_structured_output_json.py +109 -0
- pyfect/ai/_tool_call_ids.py +80 -0
- pyfect/ai/ai_error.py +785 -0
- pyfect/ai/anthropic/__init__.py +28 -0
- pyfect/ai/anthropic/_errors.py +164 -0
- pyfect/ai/anthropic/_prompt.py +375 -0
- pyfect/ai/anthropic/_response.py +443 -0
- pyfect/ai/anthropic/_schema.py +206 -0
- pyfect/ai/anthropic/_stream.py +377 -0
- pyfect/ai/anthropic/_tools.py +248 -0
- pyfect/ai/anthropic/client.py +289 -0
- pyfect/ai/anthropic/config.py +32 -0
- pyfect/ai/anthropic/error.py +27 -0
- pyfect/ai/anthropic/language_model.py +366 -0
- pyfect/ai/anthropic/telemetry.py +51 -0
- pyfect/ai/anthropic/tool.py +570 -0
- pyfect/ai/anthropic_structured_output.py +138 -0
- pyfect/ai/chat.py +548 -0
- pyfect/ai/cloudflare/__init__.py +15 -0
- pyfect/ai/cloudflare/_errors.py +108 -0
- pyfect/ai/cloudflare/client.py +174 -0
- pyfect/ai/cloudflare/decision_model.py +117 -0
- pyfect/ai/cloudflare/schema.py +144 -0
- pyfect/ai/decision.py +290 -0
- pyfect/ai/decision_model.py +283 -0
- pyfect/ai/embedding_model.py +177 -0
- pyfect/ai/id_generator.py +79 -0
- pyfect/ai/language_model.py +1077 -0
- pyfect/ai/mcp_client.py +682 -0
- pyfect/ai/mcp_client_error.py +223 -0
- pyfect/ai/mcp_protocol.py +205 -0
- pyfect/ai/mcp_schema.py +1300 -0
- pyfect/ai/mcp_server.py +404 -0
- pyfect/ai/model.py +56 -0
- pyfect/ai/openai/__init__.py +49 -0
- pyfect/ai/openai/_errors.py +89 -0
- pyfect/ai/openai/_prompt.py +527 -0
- pyfect/ai/openai/_requests.py +34 -0
- pyfect/ai/openai/_response.py +569 -0
- pyfect/ai/openai/_schema.py +769 -0
- pyfect/ai/openai/_stream.py +771 -0
- pyfect/ai/openai/_tools.py +128 -0
- pyfect/ai/openai/_web_socket.py +295 -0
- pyfect/ai/openai/client.py +296 -0
- pyfect/ai/openai/client_generated.py +123 -0
- pyfect/ai/openai/config.py +34 -0
- pyfect/ai/openai/decision_model.py +177 -0
- pyfect/ai/openai/embedding_model.py +116 -0
- pyfect/ai/openai/error.py +8 -0
- pyfect/ai/openai/generated/__init__.py +7213 -0
- pyfect/ai/openai/generated/_recursive.py +18 -0
- pyfect/ai/openai/generated/_schemas_0.py +1015 -0
- pyfect/ai/openai/generated/_schemas_1.py +1016 -0
- pyfect/ai/openai/generated/_schemas_2.py +1017 -0
- pyfect/ai/openai/generated/_schemas_3.py +615 -0
- pyfect/ai/openai/generated/client.py +5298 -0
- pyfect/ai/openai/generated/types.py +8123 -0
- pyfect/ai/openai/language_model.py +304 -0
- pyfect/ai/openai/telemetry.py +19 -0
- pyfect/ai/openai/tool.py +391 -0
- pyfect/ai/openai_compat/__init__.py +25 -0
- pyfect/ai/openai_compat/_prompt.py +191 -0
- pyfect/ai/openai_compat/_request.py +153 -0
- pyfect/ai/openai_compat/_response.py +407 -0
- pyfect/ai/openai_compat/_schema.py +97 -0
- pyfect/ai/openai_compat/client.py +234 -0
- pyfect/ai/openai_compat/config.py +32 -0
- pyfect/ai/openai_compat/embedding_model.py +139 -0
- pyfect/ai/openai_compat/error.py +7 -0
- pyfect/ai/openai_compat/language_model.py +245 -0
- pyfect/ai/openai_compat/telemetry.py +51 -0
- pyfect/ai/openai_structured_output.py +118 -0
- pyfect/ai/openrouter/__init__.py +36 -0
- pyfect/ai/openrouter/_errors.py +186 -0
- pyfect/ai/openrouter/_prompt.py +272 -0
- pyfect/ai/openrouter/_response.py +226 -0
- pyfect/ai/openrouter/_stream.py +320 -0
- pyfect/ai/openrouter/client.py +283 -0
- pyfect/ai/openrouter/config.py +36 -0
- pyfect/ai/openrouter/decision_model.py +161 -0
- pyfect/ai/openrouter/error.py +23 -0
- pyfect/ai/openrouter/generated/__init__.py +3029 -0
- pyfect/ai/openrouter/generated/_recursive.py +15 -0
- pyfect/ai/openrouter/generated/_schemas_0.py +1015 -0
- pyfect/ai/openrouter/generated/_schemas_1.py +522 -0
- pyfect/ai/openrouter/generated/client.py +1445 -0
- pyfect/ai/openrouter/generated/types.py +4769 -0
- pyfect/ai/openrouter/language_model.py +345 -0
- pyfect/ai/openrouter/schema.py +165 -0
- pyfect/ai/prompt.py +664 -0
- pyfect/ai/response.py +768 -0
- pyfect/ai/response_id_tracker.py +82 -0
- pyfect/ai/telemetry.py +126 -0
- pyfect/ai/tokenizer.py +61 -0
- pyfect/ai/tool.py +536 -0
- pyfect/ai/toolkit.py +490 -0
- pyfect/ai/typesafe/__init__.py +17 -0
- pyfect/ai/typesafe/_errors.py +120 -0
- pyfect/ai/typesafe/client.py +146 -0
- pyfect/ai/typesafe/config.py +34 -0
- pyfect/ai/typesafe/decision_model.py +90 -0
- pyfect/ai/typesafe/schema.py +140 -0
- pyfect/batching/__init__.py +58 -0
- pyfect/batching/_api.py +71 -0
- pyfect/batching/_engine.py +229 -0
- pyfect/batching/_from_effect.py +76 -0
- pyfect/batching/_persisted.py +98 -0
- pyfect/batching/_request.py +133 -0
- pyfect/batching/_resolver.py +477 -0
- pyfect/batching/_with_cache.py +90 -0
- pyfect/big_decimal.py +500 -0
- pyfect/brand.py +114 -0
- pyfect/byte_size.py +354 -0
- pyfect/cache/__init__.py +29 -0
- pyfect/cache/_cache.py +426 -0
- pyfect/cache/_expiry.py +69 -0
- pyfect/cache/_scoped_cache.py +546 -0
- pyfect/case.py +174 -0
- pyfect/cause.py +462 -0
- pyfect/channel/__init__.py +155 -0
- pyfect/channel/_base.py +92 -0
- pyfect/channel/_catch.py +158 -0
- pyfect/channel/_catch_reason.py +572 -0
- pyfect/channel/_channel.py +158 -0
- pyfect/channel/_combinators.py +220 -0
- pyfect/channel/_concurrency_methods.py +217 -0
- pyfect/channel/_concurrent_map.py +118 -0
- pyfect/channel/_constructors.py +349 -0
- pyfect/channel/_context.py +232 -0
- pyfect/channel/_error_methods.py +336 -0
- pyfect/channel/_errors.py +180 -0
- pyfect/channel/_filters.py +225 -0
- pyfect/channel/_lifecycle.py +71 -0
- pyfect/channel/_lifecycle_methods.py +206 -0
- pyfect/channel/_loop.py +100 -0
- pyfect/channel/_merge.py +246 -0
- pyfect/channel/_repeat.py +154 -0
- pyfect/channel/_run.py +149 -0
- pyfect/channel/_run_methods.py +341 -0
- pyfect/channel/_sinks.py +88 -0
- pyfect/channel/_sources.py +238 -0
- pyfect/channel/_text.py +176 -0
- pyfect/channel/_transform_methods.py +415 -0
- pyfect/channel/_upstream.py +67 -0
- pyfect/channel_schema.py +131 -0
- pyfect/cli/__init__.py +79 -0
- pyfect/cli/_ansi.py +82 -0
- pyfect/cli/_command.py +469 -0
- pyfect/cli/_config.py +164 -0
- pyfect/cli/_environment.py +10 -0
- pyfect/cli/_help.py +195 -0
- pyfect/cli/_lexer.py +81 -0
- pyfect/cli/_parser.py +465 -0
- pyfect/cli/_settings.py +85 -0
- pyfect/cli/_suggest.py +33 -0
- pyfect/cli/_width.py +134 -0
- pyfect/cli/_wizard.py +317 -0
- pyfect/cli/argument.py +212 -0
- pyfect/cli/cli_config.py +40 -0
- pyfect/cli/cli_error.py +242 -0
- pyfect/cli/cli_output.py +305 -0
- pyfect/cli/command.py +440 -0
- pyfect/cli/completions/__init__.py +50 -0
- pyfect/cli/completions/_completer.ps1 +190 -0
- pyfect/cli/completions/_descriptor.py +182 -0
- pyfect/cli/completions/bash.py +300 -0
- pyfect/cli/completions/fish.py +164 -0
- pyfect/cli/completions/powershell.py +156 -0
- pyfect/cli/completions/zsh.py +189 -0
- pyfect/cli/flag.py +246 -0
- pyfect/cli/global_flag.py +99 -0
- pyfect/cli/help_doc.py +63 -0
- pyfect/cli/param.py +789 -0
- pyfect/cli/primitive.py +308 -0
- pyfect/cli/prompt/__init__.py +75 -0
- pyfect/cli/prompt/_auto_complete.py +205 -0
- pyfect/cli/prompt/_choices.py +104 -0
- pyfect/cli/prompt/_confirm.py +195 -0
- pyfect/cli/prompt/_core.py +512 -0
- pyfect/cli/prompt/_date.py +173 -0
- pyfect/cli/prompt/_date_parts.py +387 -0
- pyfect/cli/prompt/_file.py +372 -0
- pyfect/cli/prompt/_multi_select.py +204 -0
- pyfect/cli/prompt/_number.py +257 -0
- pyfect/cli/prompt/_select.py +117 -0
- pyfect/cli/prompt/_text.py +239 -0
- pyfect/cli/terminal_capabilities.py +118 -0
- pyfect/clock.py +150 -0
- pyfect/cluster/__init__.py +155 -0
- pyfect/cluster/_cluster_abandon.py +36 -0
- pyfect/cluster/_cluster_layer.py +122 -0
- pyfect/cluster/_entity_activation.py +16 -0
- pyfect/cluster/_entity_manager/__init__.py +22 -0
- pyfect/cluster/_entity_manager/_activation.py +540 -0
- pyfect/cluster/_entity_manager/_core.py +186 -0
- pyfect/cluster/_entity_manager/_decode.py +60 -0
- pyfect/cluster/_entity_manager/_manager.py +423 -0
- pyfect/cluster/_entity_manager/_state.py +113 -0
- pyfect/cluster/_entity_reaper.py +94 -0
- pyfect/cluster/_hash.py +13 -0
- pyfect/cluster/_interruptors.py +88 -0
- pyfect/cluster/_resource_map.py +179 -0
- pyfect/cluster/_resource_ref.py +266 -0
- pyfect/cluster/_shard_lock.py +14 -0
- pyfect/cluster/cluster_cron.py +230 -0
- pyfect/cluster/cluster_error.py +81 -0
- pyfect/cluster/cluster_http.py +210 -0
- pyfect/cluster/cluster_metrics.py +54 -0
- pyfect/cluster/cluster_schema.py +137 -0
- pyfect/cluster/cluster_socket.py +197 -0
- pyfect/cluster/cluster_workflow_engine/__init__.py +24 -0
- pyfect/cluster/cluster_workflow_engine/_engine.py +1099 -0
- pyfect/cluster/cluster_workflow_engine/_entities.py +298 -0
- pyfect/cluster/deliver_at.py +31 -0
- pyfect/cluster/entity/__init__.py +39 -0
- pyfect/cluster/entity/_entity.py +332 -0
- pyfect/cluster/entity/_handlers.py +168 -0
- pyfect/cluster/entity/_keep_alive.py +74 -0
- pyfect/cluster/entity/_queue.py +103 -0
- pyfect/cluster/entity/_request.py +34 -0
- pyfect/cluster/entity/_services.py +23 -0
- pyfect/cluster/entity/_test_client.py +237 -0
- pyfect/cluster/entity_address.py +25 -0
- pyfect/cluster/entity_id.py +20 -0
- pyfect/cluster/entity_proxy.py +161 -0
- pyfect/cluster/entity_proxy_server.py +103 -0
- pyfect/cluster/entity_resource.py +122 -0
- pyfect/cluster/entity_type.py +20 -0
- pyfect/cluster/envelope.py +307 -0
- pyfect/cluster/http_runner.py +254 -0
- pyfect/cluster/k8s_http_client.py +281 -0
- pyfect/cluster/k8s_types.py +1394 -0
- pyfect/cluster/machine_id.py +21 -0
- pyfect/cluster/message.py +219 -0
- pyfect/cluster/message_storage/__init__.py +67 -0
- pyfect/cluster/message_storage/_encoded.py +393 -0
- pyfect/cluster/message_storage/_memory.py +386 -0
- pyfect/cluster/message_storage/_noop.py +109 -0
- pyfect/cluster/message_storage/_service.py +388 -0
- pyfect/cluster/reply.py +236 -0
- pyfect/cluster/runner.py +32 -0
- pyfect/cluster/runner_address.py +23 -0
- pyfect/cluster/runner_health.py +93 -0
- pyfect/cluster/runner_server.py +286 -0
- pyfect/cluster/runner_storage.py +212 -0
- pyfect/cluster/runners/__init__.py +55 -0
- pyfect/cluster/runners/_local.py +460 -0
- pyfect/cluster/runners/_noop.py +57 -0
- pyfect/cluster/runners/_rpc.py +290 -0
- pyfect/cluster/runners/_service.py +113 -0
- pyfect/cluster/shard_id.py +39 -0
- pyfect/cluster/sharding/__init__.py +12 -0
- pyfect/cluster/sharding/_acquisition.py +490 -0
- pyfect/cluster/sharding/_clients.py +271 -0
- pyfect/cluster/sharding/_make.py +388 -0
- pyfect/cluster/sharding/_routing.py +296 -0
- pyfect/cluster/sharding/_runners_sync.py +198 -0
- pyfect/cluster/sharding/_service.py +91 -0
- pyfect/cluster/sharding/_singletons.py +183 -0
- pyfect/cluster/sharding/_state.py +296 -0
- pyfect/cluster/sharding/_storage_loop.py +421 -0
- pyfect/cluster/sharding_config.py +230 -0
- pyfect/cluster/sharding_registration_event.py +28 -0
- pyfect/cluster/single_runner.py +49 -0
- pyfect/cluster/singleton.py +36 -0
- pyfect/cluster/singleton_address.py +20 -0
- pyfect/cluster/snowflake.py +133 -0
- pyfect/cluster/socket_runner.py +67 -0
- pyfect/cluster/sql_message_storage.py +1001 -0
- pyfect/cluster/sql_runner_storage.py +841 -0
- pyfect/cluster/test_runner.py +23 -0
- pyfect/config/__init__.py +93 -0
- pyfect/config/_all.py +96 -0
- pyfect/config/_collections.py +291 -0
- pyfect/config/_config.py +261 -0
- pyfect/config/_constructors.py +106 -0
- pyfect/config/_error.py +27 -0
- pyfect/config/_leaf.py +92 -0
- pyfect/config/_scalars.py +193 -0
- pyfect/config/_schema.py +340 -0
- pyfect/config_provider/__init__.py +60 -0
- pyfect/config_provider/_dir.py +84 -0
- pyfect/config_provider/_dotenv.py +127 -0
- pyfect/config_provider/_env.py +79 -0
- pyfect/config_provider/_layers.py +80 -0
- pyfect/config_provider/_node.py +90 -0
- pyfect/config_provider/_provider.py +118 -0
- pyfect/config_provider/_unknown.py +62 -0
- pyfect/console/__init__.py +63 -0
- pyfect/console/_effects.py +134 -0
- pyfect/console/_live.py +156 -0
- pyfect/console/_service.py +49 -0
- pyfect/console/_table.py +90 -0
- pyfect/context.py +414 -0
- pyfect/cron/__init__.py +30 -0
- pyfect/cron/_model.py +183 -0
- pyfect/cron/_search.py +268 -0
- pyfect/cron/_text.py +202 -0
- pyfect/cron/_wall.py +42 -0
- pyfect/crypto.py +257 -0
- pyfect/date_time/__init__.py +77 -0
- pyfect/date_time/_calendar.py +209 -0
- pyfect/date_time/_current.py +118 -0
- pyfect/date_time/_epoch.py +22 -0
- pyfect/date_time/_iso.py +80 -0
- pyfect/date_time/_zones.py +135 -0
- pyfect/date_time/intl.py +144 -0
- pyfect/deferred.py +145 -0
- pyfect/devtools/__init__.py +24 -0
- pyfect/devtools/client.py +323 -0
- pyfect/devtools/layers.py +32 -0
- pyfect/devtools/schema.py +301 -0
- pyfect/devtools/server.py +72 -0
- pyfect/duration.py +295 -0
- pyfect/effect.py +565 -0
- pyfect/effectable.py +37 -0
- pyfect/encoding/__init__.py +62 -0
- pyfect/encoding/_archive.py +197 -0
- pyfect/encoding/_json_array_scanner.py +477 -0
- pyfect/encoding/base64.py +23 -0
- pyfect/encoding/base64_url.py +19 -0
- pyfect/encoding/encoding_error.py +17 -0
- pyfect/encoding/hex.py +28 -0
- pyfect/encoding/ini.py +188 -0
- pyfect/encoding/json_array.py +246 -0
- pyfect/encoding/ndjson.py +461 -0
- pyfect/encoding/schema_binary/__init__.py +257 -0
- pyfect/encoding/schema_binary/_channels.py +173 -0
- pyfect/encoding/schema_binary/_compile.py +584 -0
- pyfect/encoding/schema_binary/_decode.py +848 -0
- pyfect/encoding/schema_binary/_dictionary.py +90 -0
- pyfect/encoding/schema_binary/_encode.py +838 -0
- pyfect/encoding/schema_binary/_fingerprint.py +168 -0
- pyfect/encoding/schema_binary/_layout.py +350 -0
- pyfect/encoding/schema_binary/_literals.py +47 -0
- pyfect/encoding/schema_binary/_parser.py +163 -0
- pyfect/encoding/schema_binary/_reader.py +148 -0
- pyfect/encoding/schema_binary/_target.py +310 -0
- pyfect/encoding/schema_binary/_values.py +133 -0
- pyfect/encoding/schema_binary/_wire.py +253 -0
- pyfect/encoding/sse.py +476 -0
- pyfect/encoding/tar/__init__.py +396 -0
- pyfect/encoding/tar/_decoder.py +220 -0
- pyfect/encoding/tar/_encoder.py +83 -0
- pyfect/encoding/tar/_header.py +494 -0
- pyfect/encoding/tar/_model.py +82 -0
- pyfect/encoding/toml.py +375 -0
- pyfect/encoding/xml/__init__.py +222 -0
- pyfect/encoding/xml/_chars.py +119 -0
- pyfect/encoding/xml/_collector.py +106 -0
- pyfect/encoding/xml/_inputs.py +126 -0
- pyfect/encoding/xml/_model.py +114 -0
- pyfect/encoding/xml/_parser.py +694 -0
- pyfect/encoding/xml/_scanner.py +180 -0
- pyfect/encoding/xml/_writer.py +238 -0
- pyfect/encoding/yaml.py +739 -0
- pyfect/encoding/zip/__init__.py +392 -0
- pyfect/encoding/zip/_decoder.py +378 -0
- pyfect/encoding/zip/_format.py +122 -0
- pyfect/encoding/zip/_inflate.py +65 -0
- pyfect/encoding/zip/_model.py +86 -0
- pyfect/encoding/zip/_writer.py +311 -0
- pyfect/error_reporter.py +240 -0
- pyfect/errors.py +321 -0
- pyfect/eventlog/__init__.py +77 -0
- pyfect/eventlog/_conflicts.py +94 -0
- pyfect/eventlog/_crypto.py +64 -0
- pyfect/eventlog/_identity.py +26 -0
- pyfect/eventlog/_identity_derivation.py +79 -0
- pyfect/eventlog/_transferable_bytes.py +55 -0
- pyfect/eventlog/_uuid.py +33 -0
- pyfect/eventlog/event.py +115 -0
- pyfect/eventlog/event_group.py +55 -0
- pyfect/eventlog/event_journal.py +630 -0
- pyfect/eventlog/event_log.py +1039 -0
- pyfect/eventlog/event_log_encryption.py +219 -0
- pyfect/eventlog/event_log_message.py +357 -0
- pyfect/eventlog/event_log_remote.py +465 -0
- pyfect/eventlog/event_log_server.py +442 -0
- pyfect/eventlog/event_log_server_encrypted.py +322 -0
- pyfect/eventlog/event_log_server_unencrypted.py +814 -0
- pyfect/eventlog/event_log_session_auth.py +250 -0
- pyfect/eventlog/event_row.py +423 -0
- pyfect/eventlog/sql_event_journal.py +692 -0
- pyfect/eventlog/sql_event_log_server_encrypted.py +440 -0
- pyfect/eventlog/sql_event_log_server_unencrypted.py +568 -0
- pyfect/execution_plan/__init__.py +42 -0
- pyfect/execution_plan/_events.py +128 -0
- pyfect/execution_plan/_model.py +108 -0
- pyfect/execution_plan/_run.py +176 -0
- pyfect/exit.py +205 -0
- pyfect/fiber_handle.py +305 -0
- pyfect/fiber_map.py +350 -0
- pyfect/fiber_set.py +232 -0
- pyfect/file_system/__init__.py +69 -0
- pyfect/file_system/_copy.py +116 -0
- pyfect/file_system/_errors.py +32 -0
- pyfect/file_system/_file.py +177 -0
- pyfect/file_system/_live.py +351 -0
- pyfect/file_system/_model.py +72 -0
- pyfect/file_system/_noop.py +189 -0
- pyfect/file_system/_service.py +336 -0
- pyfect/file_system/_stat.py +69 -0
- pyfect/file_system/_watch.py +107 -0
- pyfect/filters.py +245 -0
- pyfect/graph/__init__.py +101 -0
- pyfect/graph/_all_pairs.py +150 -0
- pyfect/graph/_combine.py +259 -0
- pyfect/graph/_compact.py +143 -0
- pyfect/graph/_components.py +200 -0
- pyfect/graph/_cycles.py +144 -0
- pyfect/graph/_enumerate.py +168 -0
- pyfect/graph/_errors.py +51 -0
- pyfect/graph/_flow.py +164 -0
- pyfect/graph/_forest.py +109 -0
- pyfect/graph/_graph.py +269 -0
- pyfect/graph/_lowlink.py +98 -0
- pyfect/graph/_matching.py +101 -0
- pyfect/graph/_mutate.py +211 -0
- pyfect/graph/_query.py +138 -0
- pyfect/graph/_records.py +119 -0
- pyfect/graph/_render.py +109 -0
- pyfect/graph/_shortest.py +274 -0
- pyfect/graph/_snapshot.py +50 -0
- pyfect/graph/_store.py +136 -0
- pyfect/graph/_traverse.py +289 -0
- pyfect/graph/_view.py +384 -0
- pyfect/graph/_walker.py +70 -0
- pyfect/graphql/__init__.py +87 -0
- pyfect/graphql/_codec.py +117 -0
- pyfect/graphql/_http.py +309 -0
- pyfect/graphql/_schemas.py +39 -0
- pyfect/graphql/_slices.py +110 -0
- pyfect/graphql/_template.py +111 -0
- pyfect/graphql/_types.py +115 -0
- pyfect/graphql/_ws.py +395 -0
- pyfect/graphql/_ws_messages.py +120 -0
- pyfect/graphql/client.py +428 -0
- pyfect/graphql/client_error.py +285 -0
- pyfect/graphql/group.py +142 -0
- pyfect/graphql/middleware.py +98 -0
- pyfect/graphql/operation.py +409 -0
- pyfect/graphql/protocol.py +119 -0
- pyfect/graphql/resolver.py +574 -0
- pyfect/graphql/template.py +61 -0
- pyfect/hash_map.py +317 -0
- pyfect/hash_ring.py +206 -0
- pyfect/hash_set.py +163 -0
- pyfect/http/__init__.py +125 -0
- pyfect/http/_asgi_listener.py +272 -0
- pyfect/http/_authority.py +63 -0
- pyfect/http/_brotli.py +27 -0
- pyfect/http/_compression.py +52 -0
- pyfect/http/_field.py +18 -0
- pyfect/http/_foreign_path.py +27 -0
- pyfect/http/_form_data.py +34 -0
- pyfect/http/_httpx_options.py +120 -0
- pyfect/http/_in_memory.py +76 -0
- pyfect/http/_request_state.py +59 -0
- pyfect/http/_vary.py +14 -0
- pyfect/http/abort.py +65 -0
- pyfect/http/asgi/__init__.py +59 -0
- pyfect/http/asgi/_app.py +155 -0
- pyfect/http/asgi/_http.py +201 -0
- pyfect/http/asgi/_mount.py +263 -0
- pyfect/http/asgi/_scope.py +86 -0
- pyfect/http/asgi/_serve.py +72 -0
- pyfect/http/asgi/_types.py +13 -0
- pyfect/http/asgi/_web_socket.py +325 -0
- pyfect/http/body.py +437 -0
- pyfect/http/client/__init__.py +111 -0
- pyfect/http/client/_circuit_breaker.py +113 -0
- pyfect/http/client/_cookie_jar.py +120 -0
- pyfect/http/client/_make.py +252 -0
- pyfect/http/client/_rate_limit.py +423 -0
- pyfect/http/client/_redirects.py +93 -0
- pyfect/http/client/_retry.py +115 -0
- pyfect/http/client/_service.py +497 -0
- pyfect/http/cookies.py +444 -0
- pyfect/http/error.py +188 -0
- pyfect/http/etag.py +96 -0
- pyfect/http/file_like.py +49 -0
- pyfect/http/find_my_way/__init__.py +10 -0
- pyfect/http/find_my_way/_nodes.py +186 -0
- pyfect/http/find_my_way/_paths.py +43 -0
- pyfect/http/find_my_way/_query_string.py +67 -0
- pyfect/http/find_my_way/_router.py +401 -0
- pyfect/http/headers.py +343 -0
- pyfect/http/http_effect.py +198 -0
- pyfect/http/httpx_client.py +291 -0
- pyfect/http/hypercorn_server.py +122 -0
- pyfect/http/incoming_message.py +140 -0
- pyfect/http/live_server/__init__.py +191 -0
- pyfect/http/live_server/_body.py +164 -0
- pyfect/http/live_server/_exchange.py +321 -0
- pyfect/http/live_server/_head.py +194 -0
- pyfect/http/live_server/_inbound.py +156 -0
- pyfect/http/live_server/_listener.py +211 -0
- pyfect/http/live_server/_serve.py +225 -0
- pyfect/http/live_server/_timers.py +20 -0
- pyfect/http/live_server/_upgrade.py +108 -0
- pyfect/http/live_server/_websocket.py +286 -0
- pyfect/http/live_server/_ws_frames.py +242 -0
- pyfect/http/method.py +30 -0
- pyfect/http/middleware/__init__.py +61 -0
- pyfect/http/middleware/_compression.py +156 -0
- pyfect/http/middleware/_cors.py +129 -0
- pyfect/http/middleware/_decompression.py +274 -0
- pyfect/http/middleware/_forwarded.py +113 -0
- pyfect/http/middleware/_logger.py +73 -0
- pyfect/http/middleware/_search_params.py +34 -0
- pyfect/http/middleware/_tracer.py +153 -0
- pyfect/http/middleware/_types.py +16 -0
- pyfect/http/mime/__init__.py +69 -0
- pyfect/http/mime/_types.py +361 -0
- pyfect/http/multipart.py +613 -0
- pyfect/http/multipart_parser/__init__.py +348 -0
- pyfect/http/multipart_parser/_content_type.py +109 -0
- pyfect/http/multipart_parser/_headers.py +206 -0
- pyfect/http/multipart_parser/_search.py +85 -0
- pyfect/http/origin.py +146 -0
- pyfect/http/platform.py +593 -0
- pyfect/http/prometheus.py +34 -0
- pyfect/http/request.py +384 -0
- pyfect/http/response.py +474 -0
- pyfect/http/router/__init__.py +82 -0
- pyfect/http/router/_api.py +462 -0
- pyfect/http/router/_in_memory.py +76 -0
- pyfect/http/router/_markers.py +47 -0
- pyfect/http/router/_middleware.py +181 -0
- pyfect/http/router/_middleware_stack.py +51 -0
- pyfect/http/router/_provided.py +31 -0
- pyfect/http/router/_route.py +114 -0
- pyfect/http/router/_router.py +308 -0
- pyfect/http/schema.py +276 -0
- pyfect/http/server.py +199 -0
- pyfect/http/server_error.py +261 -0
- pyfect/http/server_request.py +729 -0
- pyfect/http/server_respondable.py +77 -0
- pyfect/http/server_response.py +569 -0
- pyfect/http/static_server/__init__.py +257 -0
- pyfect/http/static_server/_conditional.py +72 -0
- pyfect/http/static_server/_mime.py +55 -0
- pyfect/http/static_server/_range.py +53 -0
- pyfect/http/status.py +51 -0
- pyfect/http/template.py +161 -0
- pyfect/http/trace_context.py +91 -0
- pyfect/http/url.py +442 -0
- pyfect/http/url_params.py +304 -0
- pyfect/http/uvicorn_server.py +119 -0
- pyfect/http/web.py +171 -0
- pyfect/http/web_handler.py +463 -0
- pyfect/http/wire.py +209 -0
- pyfect/http_api/__init__.py +83 -0
- pyfect/http_api/_assets/scalar.js +8 -0
- pyfect/http_api/_assets/swagger-ui.css +3 -0
- pyfect/http_api/_assets/swagger-ui.js +4 -0
- pyfect/http_api/_client_method.py +220 -0
- pyfect/http_api/_client_payload.py +144 -0
- pyfect/http_api/_client_response.py +377 -0
- pyfect/http_api/_handlers.py +282 -0
- pyfect/http_api/_html.py +38 -0
- pyfect/http_api/_media_type.py +6 -0
- pyfect/http_api/_openapi_responses.py +164 -0
- pyfect/http_api/_openapi_schemas.py +163 -0
- pyfect/http_api/_path.py +102 -0
- pyfect/http_api/_payload.py +260 -0
- pyfect/http_api/_response.py +363 -0
- pyfect/http_api/_security.py +174 -0
- pyfect/http_api/api.py +265 -0
- pyfect/http_api/builder.py +447 -0
- pyfect/http_api/client.py +474 -0
- pyfect/http_api/endpoint.py +969 -0
- pyfect/http_api/error.py +280 -0
- pyfect/http_api/group.py +215 -0
- pyfect/http_api/middleware.py +232 -0
- pyfect/http_api/openapi.py +396 -0
- pyfect/http_api/scalar.py +143 -0
- pyfect/http_api/schema.py +713 -0
- pyfect/http_api/security.py +91 -0
- pyfect/http_api/swagger.py +69 -0
- pyfect/http_api/test.py +105 -0
- pyfect/json_patch.py +255 -0
- pyfect/json_pointer.py +100 -0
- pyfect/json_schema/__init__.py +66 -0
- pyfect/json_schema/_api.py +96 -0
- pyfect/json_schema/_converter.py +258 -0
- pyfect/json_schema/_draft04.py +198 -0
- pyfect/json_schema/_draft07.py +203 -0
- pyfect/json_schema/_keywords.py +202 -0
- pyfect/json_schema/_model.py +201 -0
- pyfect/json_schema/_openapi.py +208 -0
- pyfect/latch.py +127 -0
- pyfect/layer.py +479 -0
- pyfect/layer_map.py +279 -0
- pyfect/layer_ref.py +208 -0
- pyfect/logger/__init__.py +91 -0
- pyfect/logger/_batched.py +49 -0
- pyfect/logger/_console.py +116 -0
- pyfect/logger/_file.py +31 -0
- pyfect/logger/_formats.py +104 -0
- pyfect/logger/_layer.py +50 -0
- pyfect/logger/_level.py +53 -0
- pyfect/logger/_log.py +142 -0
- pyfect/logger/_model.py +163 -0
- pyfect/logger/_refs.py +78 -0
- pyfect/logger/_stdlib.py +54 -0
- pyfect/logger/_text.py +263 -0
- pyfect/logger/_tracer.py +35 -0
- pyfect/managed_runtime.py +393 -0
- pyfect/metric/__init__.py +119 -0
- pyfect/metric/_boundaries.py +27 -0
- pyfect/metric/_kinds.py +417 -0
- pyfect/metric/_metric.py +302 -0
- pyfect/metric/_prometheus.py +150 -0
- pyfect/metric/_refs.py +78 -0
- pyfect/metric/_runtime.py +75 -0
- pyfect/metric/_snapshot.py +97 -0
- pyfect/metric/_state.py +74 -0
- pyfect/msgspec.py +98 -0
- pyfect/mutable_hash_map.py +140 -0
- pyfect/mutable_hash_set.py +95 -0
- pyfect/net/__init__.py +18 -0
- pyfect/net/_errors.py +29 -0
- pyfect/net/address_resolver.py +179 -0
- pyfect/net/dns/__init__.py +92 -0
- pyfect/net/dns/_error.py +43 -0
- pyfect/net/dns/_records.py +335 -0
- pyfect/net/dns/_service.py +287 -0
- pyfect/net/dns/_system.py +89 -0
- pyfect/net/host.py +311 -0
- pyfect/net/ip_interface.py +202 -0
- pyfect/net/ip_network.py +250 -0
- pyfect/net/net_address/__init__.py +245 -0
- pyfect/net/net_address/_inet.py +412 -0
- pyfect/net/net_address/_ip.py +348 -0
- pyfect/net/net_address/_mac.py +119 -0
- pyfect/optic/__init__.py +50 -0
- pyfect/optic/_builtins.py +90 -0
- pyfect/optic/_data.py +97 -0
- pyfect/optic/_optic.py +422 -0
- pyfect/optic/_steps.py +151 -0
- pyfect/option.py +362 -0
- pyfect/otel/__init__.py +22 -0
- pyfect/otel/_attributes.py +67 -0
- pyfect/otel/_producer.py +322 -0
- pyfect/otel/_shutdown.py +26 -0
- pyfect/otel/_span.py +292 -0
- pyfect/otel/_wrapper.py +92 -0
- pyfect/otel/logger.py +154 -0
- pyfect/otel/metrics.py +126 -0
- pyfect/otel/resource.py +111 -0
- pyfect/otel/sdk.py +195 -0
- pyfect/otel/tracer.py +157 -0
- pyfect/partitioned_semaphore.py +211 -0
- pyfect/path.py +304 -0
- pyfect/persistence/__init__.py +77 -0
- pyfect/persistence/_columns.py +32 -0
- pyfect/persistence/_error.py +14 -0
- pyfect/persistence/_seconds.py +68 -0
- pyfect/persistence/_sql_keys.py +47 -0
- pyfect/persistence/circuit_breaker/__init__.py +120 -0
- pyfect/persistence/circuit_breaker/_breaker.py +317 -0
- pyfect/persistence/circuit_breaker/_errors.py +67 -0
- pyfect/persistence/circuit_breaker/_memory.py +243 -0
- pyfect/persistence/circuit_breaker/_redis.py +509 -0
- pyfect/persistence/circuit_breaker/_store.py +60 -0
- pyfect/persistence/circuit_breaker/_types.py +191 -0
- pyfect/persistence/key_value_store/__init__.py +51 -0
- pyfect/persistence/key_value_store/_layers.py +216 -0
- pyfect/persistence/key_value_store/_schema_store.py +91 -0
- pyfect/persistence/key_value_store/_sql.py +197 -0
- pyfect/persistence/key_value_store/_store.py +212 -0
- pyfect/persistence/persistable.py +164 -0
- pyfect/persistence/persisted_cache.py +159 -0
- pyfect/persistence/persisted_queue/__init__.py +75 -0
- pyfect/persistence/persisted_queue/_error.py +42 -0
- pyfect/persistence/persisted_queue/_mailbox.py +101 -0
- pyfect/persistence/persisted_queue/_memory.py +182 -0
- pyfect/persistence/persisted_queue/_queue.py +272 -0
- pyfect/persistence/persisted_queue/_redis.py +455 -0
- pyfect/persistence/persisted_queue/_redis_scripts.py +395 -0
- pyfect/persistence/persisted_queue/_sql.py +334 -0
- pyfect/persistence/persisted_queue/_sql_migrations.py +319 -0
- pyfect/persistence/persisted_queue/_sql_statements.py +441 -0
- pyfect/persistence/persisted_queue/_store.py +147 -0
- pyfect/persistence/persistence/__init__.py +84 -0
- pyfect/persistence/persistence/_kvs.py +135 -0
- pyfect/persistence/persistence/_memory.py +62 -0
- pyfect/persistence/persistence/_redis.py +162 -0
- pyfect/persistence/persistence/_service.py +276 -0
- pyfect/persistence/persistence/_sql.py +499 -0
- pyfect/persistence/rate_limiter/__init__.py +82 -0
- pyfect/persistence/rate_limiter/_errors.py +59 -0
- pyfect/persistence/rate_limiter/_limiter.py +325 -0
- pyfect/persistence/rate_limiter/_memory.py +217 -0
- pyfect/persistence/rate_limiter/_redis.py +485 -0
- pyfect/persistence/rate_limiter/_store.py +89 -0
- pyfect/persistence/redis/__init__.py +52 -0
- pyfect/persistence/redis/_error.py +12 -0
- pyfect/persistence/redis/_make.py +234 -0
- pyfect/persistence/redis/_script.py +78 -0
- pyfect/persistence/redis/_service.py +104 -0
- pyfect/persistence/redis_py.py +303 -0
- pyfect/persistence/user_directories.py +195 -0
- pyfect/platform.py +26 -0
- pyfect/platform_error.py +256 -0
- pyfect/pool/__init__.py +36 -0
- pyfect/pool/_pool.py +707 -0
- pyfect/pool/_state.py +161 -0
- pyfect/pool/_ttl.py +105 -0
- pyfect/pool/_waiters.py +116 -0
- pyfect/primary_key.py +35 -0
- pyfect/process/__init__.py +82 -0
- pyfect/process/_child.py +213 -0
- pyfect/process/_command.py +358 -0
- pyfect/process/_handle.py +77 -0
- pyfect/process/_live.py +455 -0
- pyfect/process/_os.py +78 -0
- pyfect/process/_pipes.py +291 -0
- pyfect/process/_spawner.py +75 -0
- pyfect/pubsub/__init__.py +33 -0
- pyfect/pubsub/_atomic.py +181 -0
- pyfect/pubsub/_hub.py +214 -0
- pyfect/pubsub/_pubsub.py +214 -0
- pyfect/pubsub/_replay.py +107 -0
- pyfect/pubsub/_subscription.py +226 -0
- pyfect/pull.py +132 -0
- pyfect/py.typed +0 -0
- pyfect/pydantic.py +188 -0
- pyfect/queue/__init__.py +33 -0
- pyfect/queue/_queue.py +525 -0
- pyfect/queue/_state.py +111 -0
- pyfect/queue/_views.py +55 -0
- pyfect/random.py +146 -0
- pyfect/rc_map.py +363 -0
- pyfect/rc_ref.py +271 -0
- pyfect/reactivity/__init__.py +53 -0
- pyfect/reactivity/_async_result_schema.py +227 -0
- pyfect/reactivity/_hydration.py +18 -0
- pyfect/reactivity/_live.py +21 -0
- pyfect/reactivity/_reactivity.py +256 -0
- pyfect/reactivity/_result_effect.py +17 -0
- pyfect/reactivity/_time.py +11 -0
- pyfect/reactivity/async_result.py +679 -0
- pyfect/reactivity/atom/__init__.py +143 -0
- pyfect/reactivity/atom/_combinators.py +223 -0
- pyfect/reactivity/atom/_constructors.py +80 -0
- pyfect/reactivity/atom/_conversions.py +79 -0
- pyfect/reactivity/atom/_effect.py +252 -0
- pyfect/reactivity/atom/_fn.py +341 -0
- pyfect/reactivity/atom/_kvs.py +101 -0
- pyfect/reactivity/atom/_optimistic.py +162 -0
- pyfect/reactivity/atom/_runtime.py +323 -0
- pyfect/reactivity/atom/_serializable.py +65 -0
- pyfect/reactivity/atom/_subscription_ref.py +102 -0
- pyfect/reactivity/atom/_swr.py +69 -0
- pyfect/reactivity/atom/_types.py +435 -0
- pyfect/reactivity/atom_http_api.py +667 -0
- pyfect/reactivity/atom_ref.py +260 -0
- pyfect/reactivity/atom_registry/__init__.py +219 -0
- pyfect/reactivity/atom_registry/_batch.py +96 -0
- pyfect/reactivity/atom_registry/_dispatcher.py +60 -0
- pyfect/reactivity/atom_registry/_lifetime.py +202 -0
- pyfect/reactivity/atom_registry/_node.py +423 -0
- pyfect/reactivity/atom_registry/_registry.py +380 -0
- pyfect/reactivity/atom_rpc.py +283 -0
- pyfect/reactivity/hydration.py +200 -0
- pyfect/redacted.py +83 -0
- pyfect/ref.py +129 -0
- pyfect/resource.py +98 -0
- pyfect/result.py +306 -0
- pyfect/rpc/__init__.py +64 -0
- pyfect/rpc/_encoded_schema.py +86 -0
- pyfect/rpc/_json_rpc.py +246 -0
- pyfect/rpc/_methods.py +177 -0
- pyfect/rpc/_rpc.py +498 -0
- pyfect/rpc/client/__init__.py +100 -0
- pyfect/rpc/client/_core.py +449 -0
- pyfect/rpc/client/_http.py +257 -0
- pyfect/rpc/client/_protocol.py +171 -0
- pyfect/rpc/client/_serialized.py +321 -0
- pyfect/rpc/client/_transports.py +328 -0
- pyfect/rpc/client/_worker.py +283 -0
- pyfect/rpc/client_error.py +35 -0
- pyfect/rpc/group.py +296 -0
- pyfect/rpc/message.py +299 -0
- pyfect/rpc/middleware.py +182 -0
- pyfect/rpc/schema.py +89 -0
- pyfect/rpc/serialization.py +366 -0
- pyfect/rpc/server/__init__.py +181 -0
- pyfect/rpc/server/_core.py +549 -0
- pyfect/rpc/server/_protocol.py +123 -0
- pyfect/rpc/server/_serialized.py +363 -0
- pyfect/rpc/server/_stdio.py +101 -0
- pyfect/rpc/server/_transports.py +427 -0
- pyfect/rpc/server/_worker.py +108 -0
- pyfect/rpc/test.py +74 -0
- pyfect/rpc/worker.py +92 -0
- pyfect/run.py +393 -0
- pyfect/schedule/__init__.py +91 -0
- pyfect/schedule/_combinators.py +168 -0
- pyfect/schedule/_constructors.py +223 -0
- pyfect/schedule/_cron.py +49 -0
- pyfect/schedule/_drive.py +191 -0
- pyfect/schedule/_model.py +405 -0
- pyfect/scheduler.py +575 -0
- pyfect/schema/__init__.py +866 -0
- pyfect/schema/_adapters.py +39 -0
- pyfect/schema/_arbitrary_constraint.py +242 -0
- pyfect/schema/_arbitrary_graph.py +325 -0
- pyfect/schema/_arbitrary_links.py +179 -0
- pyfect/schema/_arbitrary_number.py +112 -0
- pyfect/schema/_arrays.py +287 -0
- pyfect/schema/_assignable.py +118 -0
- pyfect/schema/_boolean_literals.py +31 -0
- pyfect/schema/_checks_number.py +216 -0
- pyfect/schema/_checks_order.py +140 -0
- pyfect/schema/_checks_size.py +277 -0
- pyfect/schema/_checks_string.py +241 -0
- pyfect/schema/_class.py +569 -0
- pyfect/schema/_codec_derivation.py +227 -0
- pyfect/schema/_codec_links.py +221 -0
- pyfect/schema/_codecs.py +316 -0
- pyfect/schema/_collections.py +172 -0
- pyfect/schema/_constructor_default.py +89 -0
- pyfect/schema/_data.py +440 -0
- pyfect/schema/_data_codecs.py +145 -0
- pyfect/schema/_decimal_regexp.py +243 -0
- pyfect/schema/_declare.py +68 -0
- pyfect/schema/_derive.py +292 -0
- pyfect/schema/_duration_codecs.py +27 -0
- pyfect/schema/_ecma_pattern.py +416 -0
- pyfect/schema/_error.py +26 -0
- pyfect/schema/_error_instance.py +222 -0
- pyfect/schema/_graph.py +271 -0
- pyfect/schema/_instances.py +240 -0
- pyfect/schema/_jsonschema/__init__.py +90 -0
- pyfect/schema/_jsonschema/_annotations.py +114 -0
- pyfect/schema/_jsonschema/_checks.py +253 -0
- pyfect/schema/_jsonschema/_compile.py +346 -0
- pyfect/schema/_jsonschema/_exact.py +19 -0
- pyfect/schema/_jsonschema/_merge.py +211 -0
- pyfect/schema/_jsonschema/_objects.py +231 -0
- pyfect/schema/_jsonschema/_references.py +221 -0
- pyfect/schema/_jsonschema/_template.py +69 -0
- pyfect/schema/_literals.py +147 -0
- pyfect/schema/_middleware.py +209 -0
- pyfect/schema/_net.py +522 -0
- pyfect/schema/_options.py +49 -0
- pyfect/schema/_parse.py +351 -0
- pyfect/schema/_parsed_text.py +37 -0
- pyfect/schema/_primitives.py +48 -0
- pyfect/schema/_record.py +41 -0
- pyfect/schema/_refine.py +81 -0
- pyfect/schema/_schema.py +158 -0
- pyfect/schema/_semver.py +144 -0
- pyfect/schema/_strategy/__init__.py +258 -0
- pyfect/schema/_strategy/_big_decimal.py +203 -0
- pyfect/schema/_strategy/_composites.py +119 -0
- pyfect/schema/_strategy/_leaves.py +94 -0
- pyfect/schema/_strategy/_net.py +133 -0
- pyfect/schema/_struct.py +87 -0
- pyfect/schema/_struct_keys.py +128 -0
- pyfect/schema/_tagged.py +254 -0
- pyfect/schema/_template_literal.py +172 -0
- pyfect/schema/_to_codec_array_from_single.py +115 -0
- pyfect/schema/_to_codec_go_json.py +174 -0
- pyfect/schema/_to_codec_json.py +73 -0
- pyfect/schema/_to_codec_string_tree.py +115 -0
- pyfect/schema/_to_differ_json_patch.py +44 -0
- pyfect/schema/_to_encoder_xml.py +119 -0
- pyfect/schema/_to_equivalence.py +186 -0
- pyfect/schema/_to_formatter.py +180 -0
- pyfect/schema/_to_iso.py +73 -0
- pyfect/schema/_to_strategy.py +31 -0
- pyfect/schema/_transform.py +210 -0
- pyfect/schema/_tree.py +27 -0
- pyfect/schema/_type_walk.py +53 -0
- pyfect/schema/_undefined.py +58 -0
- pyfect/schema/_value_codecs.py +84 -0
- pyfect/schema/_values.py +123 -0
- pyfect/schema/_void.py +86 -0
- pyfect/schema/ast/__init__.py +227 -0
- pyfect/schema/ast/_annotations.py +43 -0
- pyfect/schema/ast/_arrays.py +210 -0
- pyfect/schema/ast/_base.py +295 -0
- pyfect/schema/ast/_candidates.py +309 -0
- pyfect/schema/ast/_check.py +181 -0
- pyfect/schema/ast/_class.py +159 -0
- pyfect/schema/ast/_constructors.py +65 -0
- pyfect/schema/ast/_declaration.py +97 -0
- pyfect/schema/ast/_encoding.py +111 -0
- pyfect/schema/ast/_json.py +129 -0
- pyfect/schema/ast/_keywords.py +125 -0
- pyfect/schema/ast/_literal.py +157 -0
- pyfect/schema/ast/_objects.py +356 -0
- pyfect/schema/ast/_record.py +36 -0
- pyfect/schema/ast/_result.py +116 -0
- pyfect/schema/ast/_steps.py +209 -0
- pyfect/schema/ast/_suspend.py +65 -0
- pyfect/schema/ast/_template_literal.py +256 -0
- pyfect/schema/ast/_union.py +172 -0
- pyfect/schema/getter/__init__.py +136 -0
- pyfect/schema/getter/_binary.py +104 -0
- pyfect/schema/getter/_coerce.py +196 -0
- pyfect/schema/getter/_getter.py +243 -0
- pyfect/schema/getter/_make.py +151 -0
- pyfect/schema/getter/_text.py +141 -0
- pyfect/schema/getter/_tree_record.py +170 -0
- pyfect/schema/issue.py +420 -0
- pyfect/schema/model/__init__.py +120 -0
- pyfect/schema/model/_class.py +245 -0
- pyfect/schema/model/_dates.py +103 -0
- pyfect/schema/model/_fields.py +77 -0
- pyfect/schema/model/_toolkit.py +114 -0
- pyfect/schema/model/_uuid.py +69 -0
- pyfect/schema/representation/__init__.py +424 -0
- pyfect/schema/representation/_builtin_revivers.py +331 -0
- pyfect/schema/representation/_code.py +106 -0
- pyfect/schema/representation/_code_builtins.py +281 -0
- pyfect/schema/representation/_code_document.py +563 -0
- pyfect/schema/representation/_code_sort.py +111 -0
- pyfect/schema/representation/_from.py +278 -0
- pyfect/schema/representation/_import/__init__.py +78 -0
- pyfect/schema/representation/_import/_finalize.py +121 -0
- pyfect/schema/representation/_import/_intersect.py +467 -0
- pyfect/schema/representation/_import/_js_order.py +66 -0
- pyfect/schema/representation/_import/_keywords.py +224 -0
- pyfect/schema/representation/_import/_nodes.py +199 -0
- pyfect/schema/representation/_import/_translate.py +455 -0
- pyfect/schema/representation/_json.py +520 -0
- pyfect/schema/representation/_json_schema.py +178 -0
- pyfect/schema/representation/_model.py +284 -0
- pyfect/schema/representation/_revivers.py +89 -0
- pyfect/schema/representation/_to.py +249 -0
- pyfect/schema/transformation/__init__.py +99 -0
- pyfect/schema/transformation/_builtins.py +314 -0
- pyfect/schema/transformation/_durations.py +150 -0
- pyfect/schema/transformation/_transformation.py +177 -0
- pyfect/schema/variant_schema/__init__.py +69 -0
- pyfect/schema/variant_schema/_class.py +188 -0
- pyfect/schema/variant_schema/_defaults.py +30 -0
- pyfect/schema/variant_schema/_field.py +32 -0
- pyfect/schema/variant_schema/_struct.py +130 -0
- pyfect/schema/variant_schema/_toolkit.py +160 -0
- pyfect/scope.py +374 -0
- pyfect/scoped_ref.py +124 -0
- pyfect/semaphore.py +276 -0
- pyfect/semver.py +403 -0
- pyfect/semver_range.py +420 -0
- pyfect/sink/__init__.py +131 -0
- pyfect/sink/_combinators.py +151 -0
- pyfect/sink/_constructors.py +187 -0
- pyfect/sink/_context.py +87 -0
- pyfect/sink/_folds.py +336 -0
- pyfect/sink/_for_each.py +87 -0
- pyfect/sink/_sink.py +265 -0
- pyfect/sink/_steps.py +34 -0
- pyfect/sink/_taking.py +183 -0
- pyfect/socket/__init__.py +146 -0
- pyfect/socket/_accepted.py +65 -0
- pyfect/socket/_connection.py +384 -0
- pyfect/socket/_errors.py +135 -0
- pyfect/socket/_loop.py +66 -0
- pyfect/socket/_net.py +281 -0
- pyfect/socket/_socket.py +245 -0
- pyfect/socket/_tls.py +88 -0
- pyfect/socket/_tls_session.py +117 -0
- pyfect/socket/_web_socket.py +555 -0
- pyfect/socket/datagram/__init__.py +110 -0
- pyfect/socket/datagram/_core.py +460 -0
- pyfect/socket/datagram/_errors.py +102 -0
- pyfect/socket/datagram/_live.py +675 -0
- pyfect/socket/datagram/_model.py +217 -0
- pyfect/socket/server.py +258 -0
- pyfect/socket/ws.py +484 -0
- pyfect/sql/__init__.py +60 -0
- pyfect/sql/_loop.py +134 -0
- pyfect/sql/clickhouse/__init__.py +74 -0
- pyfect/sql/clickhouse/_connection.py +237 -0
- pyfect/sql/clickhouse/_rows.py +87 -0
- pyfect/sql/clickhouse/client.py +322 -0
- pyfect/sql/clickhouse/config.py +241 -0
- pyfect/sql/clickhouse/driver.py +434 -0
- pyfect/sql/clickhouse/error.py +106 -0
- pyfect/sql/clickhouse/migrator.py +47 -0
- pyfect/sql/clickhouse/params.py +168 -0
- pyfect/sql/client/__init__.py +35 -0
- pyfect/sql/client/_client.py +309 -0
- pyfect/sql/client/_sqlite_acquirers.py +130 -0
- pyfect/sql/client/_transaction.py +410 -0
- pyfect/sql/connection.py +87 -0
- pyfect/sql/d1/__init__.py +48 -0
- pyfect/sql/d1/api.py +270 -0
- pyfect/sql/d1/client.py +344 -0
- pyfect/sql/d1/config.py +55 -0
- pyfect/sql/error.py +311 -0
- pyfect/sql/libsql/__init__.py +58 -0
- pyfect/sql/libsql/_connection.py +110 -0
- pyfect/sql/libsql/_hrana.py +258 -0
- pyfect/sql/libsql/_local.py +251 -0
- pyfect/sql/libsql/_targets.py +28 -0
- pyfect/sql/libsql/_values.py +46 -0
- pyfect/sql/libsql/client.py +225 -0
- pyfect/sql/libsql/config.py +170 -0
- pyfect/sql/libsql/migrator.py +43 -0
- pyfect/sql/libsql/sdk.py +123 -0
- pyfect/sql/migrator.py +434 -0
- pyfect/sql/model.py +324 -0
- pyfect/sql/mssql/__init__.py +79 -0
- pyfect/sql/mssql/_connection.py +169 -0
- pyfect/sql/mssql/_driver.py +103 -0
- pyfect/sql/mssql/_names.py +9 -0
- pyfect/sql/mssql/_pool.py +125 -0
- pyfect/sql/mssql/_tds.py +447 -0
- pyfect/sql/mssql/_tds_params.py +155 -0
- pyfect/sql/mssql/_worker.py +155 -0
- pyfect/sql/mssql/client.py +344 -0
- pyfect/sql/mssql/config.py +118 -0
- pyfect/sql/mssql/error.py +111 -0
- pyfect/sql/mssql/migrator.py +43 -0
- pyfect/sql/mssql/parameter.py +44 -0
- pyfect/sql/mssql/procedure.py +119 -0
- pyfect/sql/mssql/types.py +137 -0
- pyfect/sql/mysql/__init__.py +60 -0
- pyfect/sql/mysql/_asyncmy.py +168 -0
- pyfect/sql/mysql/_bind.py +76 -0
- pyfect/sql/mysql/_connection.py +156 -0
- pyfect/sql/mysql/_pool.py +63 -0
- pyfect/sql/mysql/_session.py +262 -0
- pyfect/sql/mysql/client.py +244 -0
- pyfect/sql/mysql/config.py +108 -0
- pyfect/sql/mysql/error.py +104 -0
- pyfect/sql/mysql/migrator.py +44 -0
- pyfect/sql/pg/__init__.py +82 -0
- pyfect/sql/pg/_resolve.py +284 -0
- pyfect/sql/pg/_session.py +380 -0
- pyfect/sql/pg/client.py +398 -0
- pyfect/sql/pg/config.py +142 -0
- pyfect/sql/pg/connection.py +585 -0
- pyfect/sql/pg/error.py +131 -0
- pyfect/sql/pg/migrator.py +139 -0
- pyfect/sql/pg/pool.py +265 -0
- pyfect/sql/resolver.py +294 -0
- pyfect/sql/result.py +24 -0
- pyfect/sql/schema.py +116 -0
- pyfect/sql/sqlite/__init__.py +26 -0
- pyfect/sql/sqlite/_connection.py +230 -0
- pyfect/sql/sqlite/_worker.py +71 -0
- pyfect/sql/sqlite/client.py +248 -0
- pyfect/sql/sqlite/migrator.py +43 -0
- pyfect/sql/statement/__init__.py +144 -0
- pyfect/sql/statement/_compiler.py +384 -0
- pyfect/sql/statement/_segments.py +487 -0
- pyfect/sql/statement/_statement.py +526 -0
- pyfect/sql/statement/_transforms.py +93 -0
- pyfect/sql/stream.py +88 -0
- pyfect/stdio.py +206 -0
- pyfect/stream/__init__.py +151 -0
- pyfect/stream/_async_iterable.py +129 -0
- pyfect/stream/_base.py +69 -0
- pyfect/stream/_bridge_methods.py +139 -0
- pyfect/stream/_catch.py +135 -0
- pyfect/stream/_catch_reason.py +558 -0
- pyfect/stream/_channel_methods.py +61 -0
- pyfect/stream/_chunk_methods.py +169 -0
- pyfect/stream/_chunking.py +310 -0
- pyfect/stream/_concat.py +65 -0
- pyfect/stream/_constructors.py +341 -0
- pyfect/stream/_context.py +147 -0
- pyfect/stream/_error_methods.py +254 -0
- pyfect/stream/_execution_plan.py +199 -0
- pyfect/stream/_fanout.py +154 -0
- pyfect/stream/_fanout_methods.py +183 -0
- pyfect/stream/_filter_methods.py +203 -0
- pyfect/stream/_grouping.py +168 -0
- pyfect/stream/_many.py +65 -0
- pyfect/stream/_map_methods.py +233 -0
- pyfect/stream/_mapping.py +126 -0
- pyfect/stream/_run.py +114 -0
- pyfect/stream/_run_methods.py +114 -0
- pyfect/stream/_service_methods.py +36 -0
- pyfect/stream/_sink_methods.py +151 -0
- pyfect/stream/_sinking.py +277 -0
- pyfect/stream/_sources.py +124 -0
- pyfect/stream/_stream.py +162 -0
- pyfect/stream/_taking.py +330 -0
- pyfect/stream/_text.py +43 -0
- pyfect/stream/_timing.py +294 -0
- pyfect/stream/_timing_methods.py +252 -0
- pyfect/stream/_zip_methods.py +279 -0
- pyfect/stream/_zipping.py +213 -0
- pyfect/subscription_ref.py +66 -0
- pyfect/synchronized_ref.py +38 -0
- pyfect/take.py +36 -0
- pyfect/terminal/__init__.py +41 -0
- pyfect/terminal/_keys.py +113 -0
- pyfect/terminal/_live.py +223 -0
- pyfect/terminal/_service.py +73 -0
- pyfect/testing/__init__.py +122 -0
- pyfect/testing/_assertions.py +92 -0
- pyfect/testing/_clock.py +309 -0
- pyfect/testing/_console.py +164 -0
- pyfect/testing/_crypto.py +44 -0
- pyfect/testing/_decorators.py +211 -0
- pyfect/testing/_flaky.py +34 -0
- pyfect/testing/_layers.py +153 -0
- pyfect/testing/_prop.py +255 -0
- pyfect/testing/_runners.py +84 -0
- pyfect/testing/_runtime.py +174 -0
- pyfect/testing/_schema.py +238 -0
- pyfect/testing/plugin.py +59 -0
- pyfect/tracer/__init__.py +107 -0
- pyfect/tracer/_model.py +176 -0
- pyfect/tracer/_native.py +172 -0
- pyfect/tracer/_refs.py +87 -0
- pyfect/tracer/_spans.py +269 -0
- pyfect/transaction.py +340 -0
- pyfect/trie.py +317 -0
- pyfect/tx_chunk.py +127 -0
- pyfect/tx_deferred.py +75 -0
- pyfect/tx_hash_map.py +265 -0
- pyfect/tx_hash_set.py +151 -0
- pyfect/tx_priority_queue.py +224 -0
- pyfect/tx_pubsub.py +194 -0
- pyfect/tx_queue/__init__.py +27 -0
- pyfect/tx_queue/_queue.py +359 -0
- pyfect/tx_queue/_views.py +60 -0
- pyfect/tx_reentrant_lock.py +201 -0
- pyfect/tx_ref.py +79 -0
- pyfect/tx_semaphore.py +136 -0
- pyfect/tx_subscription_ref.py +118 -0
- pyfect/workers/__init__.py +62 -0
- pyfect/workers/_frames.py +82 -0
- pyfect/workers/_mailbox.py +51 -0
- pyfect/workers/_port.py +106 -0
- pyfect/workers/_process.py +155 -0
- pyfect/workers/_thread.py +100 -0
- pyfect/workers/python_worker.py +133 -0
- pyfect/workers/python_worker_runner.py +112 -0
- pyfect/workers/transferable.py +103 -0
- pyfect/workers/worker.py +273 -0
- pyfect/workers/worker_error.py +86 -0
- pyfect/workers/worker_runner.py +85 -0
- pyfect/workflow/__init__.py +78 -0
- pyfect/workflow/_crypto.py +23 -0
- pyfect/workflow/_encoded.py +381 -0
- pyfect/workflow/_memory.py +424 -0
- pyfect/workflow/_result.py +43 -0
- pyfect/workflow/_services.py +241 -0
- pyfect/workflow/_suspension.py +153 -0
- pyfect/workflow/activity.py +427 -0
- pyfect/workflow/durable_clock.py +72 -0
- pyfect/workflow/durable_deferred.py +403 -0
- pyfect/workflow/durable_queue.py +259 -0
- pyfect/workflow/workflow.py +509 -0
- pyfect/workflow/workflow_engine.py +42 -0
- pyfect/workflow/workflow_proxy.py +161 -0
- pyfect/workflow/workflow_proxy_server.py +146 -0
- pyfect_dev-0.1.0.dist-info/METADATA +296 -0
- pyfect_dev-0.1.0.dist-info/RECORD +1265 -0
- pyfect_dev-0.1.0.dist-info/WHEEL +4 -0
- pyfect_dev-0.1.0.dist-info/entry_points.txt +3 -0
- pyfect_dev-0.1.0.dist-info/licenses/LICENSE +21 -0
pyfect/big_decimal.py
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
"""Arbitrary-precision decimals (Effect's ``BigDecimal``): an ``int`` value
|
|
2
|
+
and a ``scale``, the number being ``value * 10 ** -scale``::
|
|
3
|
+
|
|
4
|
+
d = big_decimal.from_string_unsafe("123.4500")
|
|
5
|
+
d.format() # "123.45"
|
|
6
|
+
d.divide_unsafe(BigDecimal(3, 0)) # 100 significant digits, rounded half up
|
|
7
|
+
d.round(scale=1, mode="half-even")
|
|
8
|
+
|
|
9
|
+
``decimal.Decimal`` rounds every result to its context's precision (28
|
|
10
|
+
digits by default) and keeps signed zeros and exponents apart; a
|
|
11
|
+
``BigDecimal`` is exact for ``+``, ``-`` and ``*``, divides to 100
|
|
12
|
+
significant digits as Effect does, and ``==`` / ``hash`` / ``<`` compare
|
|
13
|
+
values (``2`` equals ``2.00``). The scale is any integer from
|
|
14
|
+
``-(2**53 - 1)`` to ``2**53 - 1``.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import math
|
|
20
|
+
from collections.abc import Iterable
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from typing import Literal
|
|
23
|
+
|
|
24
|
+
from pyfect._digits import digits_value
|
|
25
|
+
from pyfect._js import int_text, js_number
|
|
26
|
+
|
|
27
|
+
_MAX_SAFE = 2**53 - 1
|
|
28
|
+
_DEFAULT_PRECISION = 100
|
|
29
|
+
_MAX_COMPARISON_SCALE_ALIGNMENT = 100
|
|
30
|
+
|
|
31
|
+
type RoundingMode = Literal[
|
|
32
|
+
"ceil",
|
|
33
|
+
"floor",
|
|
34
|
+
"to-zero",
|
|
35
|
+
"from-zero",
|
|
36
|
+
"half-ceil",
|
|
37
|
+
"half-floor",
|
|
38
|
+
"half-to-zero",
|
|
39
|
+
"half-from-zero",
|
|
40
|
+
"half-even",
|
|
41
|
+
"half-odd",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _check_scale(scale: int) -> int:
|
|
46
|
+
if type(scale) is not int or not -_MAX_SAFE <= scale <= _MAX_SAFE:
|
|
47
|
+
raise ValueError(f"Scale must be a safe integer, got {js_number(scale)}")
|
|
48
|
+
return scale
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _tdiv(a: int, b: int) -> int:
|
|
52
|
+
"""JavaScript's ``bigint`` division: truncated toward zero."""
|
|
53
|
+
q = abs(a) // abs(b)
|
|
54
|
+
return q if (a < 0) == (b < 0) else -q
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _trem(a: int, b: int) -> int:
|
|
58
|
+
"""JavaScript's ``bigint`` ``%``: the sign of the dividend."""
|
|
59
|
+
return a - b * _tdiv(a, b)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _cmp(a: int, b: int) -> Literal[-1, 0, 1]:
|
|
63
|
+
return -1 if a < b else 1 if a > b else 0
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@dataclass(frozen=True, slots=True, eq=False)
|
|
67
|
+
class BigDecimal:
|
|
68
|
+
"""``value * 10 ** -scale``; the constructor is Effect's ``make``."""
|
|
69
|
+
|
|
70
|
+
value: int
|
|
71
|
+
scale: int
|
|
72
|
+
_normalized: BigDecimal | None = field(default=None, init=False, repr=False)
|
|
73
|
+
|
|
74
|
+
def __post_init__(self) -> None:
|
|
75
|
+
_check_scale(self.scale)
|
|
76
|
+
|
|
77
|
+
# --- representation ---------------------------------------------------------------
|
|
78
|
+
|
|
79
|
+
def normalize(self) -> BigDecimal:
|
|
80
|
+
"""The same number without trailing zeros in ``value`` (``0`` has
|
|
81
|
+
scale 0); remembered once computed."""
|
|
82
|
+
if self._normalized is not None:
|
|
83
|
+
return self._normalized
|
|
84
|
+
if self.value == 0:
|
|
85
|
+
out = _ZERO
|
|
86
|
+
else:
|
|
87
|
+
digits = int_text(self.value)
|
|
88
|
+
stripped = digits.rstrip("0")
|
|
89
|
+
trailing = len(digits) - len(stripped)
|
|
90
|
+
out = self if trailing == 0 else _normalized(int(stripped), self.scale - trailing)
|
|
91
|
+
object.__setattr__(self, "_normalized", out)
|
|
92
|
+
return out
|
|
93
|
+
|
|
94
|
+
def rescale(self, scale: int) -> BigDecimal:
|
|
95
|
+
"""The number at ``scale``, digits beyond it truncated toward zero
|
|
96
|
+
(Effect's ``scale``)."""
|
|
97
|
+
if scale > self.scale:
|
|
98
|
+
return BigDecimal(self.value * 10 ** (scale - self.scale), scale)
|
|
99
|
+
if scale < self.scale:
|
|
100
|
+
return BigDecimal(_tdiv(self.value, 10 ** (self.scale - scale)), scale)
|
|
101
|
+
return self
|
|
102
|
+
|
|
103
|
+
def format(self) -> str:
|
|
104
|
+
"""Plain notation, or exponential when the normalized scale is 16 or
|
|
105
|
+
more either way."""
|
|
106
|
+
n = self.normalize()
|
|
107
|
+
if abs(n.scale) >= 16:
|
|
108
|
+
return n.to_exponential()
|
|
109
|
+
negative = n.value < 0
|
|
110
|
+
absolute = int_text(-n.value if negative else n.value)
|
|
111
|
+
if n.scale > 0:
|
|
112
|
+
digits = absolute.rjust(n.scale + 1, "0")
|
|
113
|
+
point = len(digits) - n.scale
|
|
114
|
+
complete = f"{digits[:point]}.{digits[point:]}"
|
|
115
|
+
else:
|
|
116
|
+
complete = absolute + "0" * -n.scale
|
|
117
|
+
return f"-{complete}" if negative else complete
|
|
118
|
+
|
|
119
|
+
def to_exponential(self) -> str:
|
|
120
|
+
"""``d[.ddd]e±n``, as JavaScript's ``toExponential()``."""
|
|
121
|
+
if self.value == 0:
|
|
122
|
+
return "0e+0"
|
|
123
|
+
n = self.normalize()
|
|
124
|
+
digits = int_text(n.value)
|
|
125
|
+
point = 2 if n.value < 0 else 1
|
|
126
|
+
head, tail = digits[:point], digits[point:]
|
|
127
|
+
exp = len(tail) - n.scale
|
|
128
|
+
return f"{head}{'.' + tail if tail else ''}e{'+' if exp >= 0 else ''}{exp}"
|
|
129
|
+
|
|
130
|
+
def to_number_unsafe(self) -> float:
|
|
131
|
+
"""The nearest float (``Number(format(n))``)."""
|
|
132
|
+
return float(self.format())
|
|
133
|
+
|
|
134
|
+
def __str__(self) -> str:
|
|
135
|
+
return self.format()
|
|
136
|
+
|
|
137
|
+
def __repr__(self) -> str:
|
|
138
|
+
return f"BigDecimal({self.format()})"
|
|
139
|
+
|
|
140
|
+
# --- predicates -------------------------------------------------------------------
|
|
141
|
+
|
|
142
|
+
def sign(self) -> Literal[-1, 0, 1]:
|
|
143
|
+
return _cmp(self.value, 0)
|
|
144
|
+
|
|
145
|
+
def is_integer(self) -> bool:
|
|
146
|
+
return self.normalize().scale <= 0
|
|
147
|
+
|
|
148
|
+
def is_zero(self) -> bool:
|
|
149
|
+
return self.value == 0
|
|
150
|
+
|
|
151
|
+
def is_negative(self) -> bool:
|
|
152
|
+
return self.value < 0
|
|
153
|
+
|
|
154
|
+
def is_positive(self) -> bool:
|
|
155
|
+
return self.value > 0
|
|
156
|
+
|
|
157
|
+
# --- arithmetic -------------------------------------------------------------------
|
|
158
|
+
|
|
159
|
+
def sum(self, that: BigDecimal) -> BigDecimal:
|
|
160
|
+
if that.value == 0:
|
|
161
|
+
return self
|
|
162
|
+
if self.value == 0:
|
|
163
|
+
return that
|
|
164
|
+
if self.scale > that.scale:
|
|
165
|
+
return BigDecimal(that.rescale(self.scale).value + self.value, self.scale)
|
|
166
|
+
if self.scale < that.scale:
|
|
167
|
+
return BigDecimal(self.rescale(that.scale).value + that.value, that.scale)
|
|
168
|
+
return BigDecimal(self.value + that.value, self.scale)
|
|
169
|
+
|
|
170
|
+
def subtract(self, that: BigDecimal) -> BigDecimal:
|
|
171
|
+
if that.value == 0:
|
|
172
|
+
return self
|
|
173
|
+
if self.value == 0:
|
|
174
|
+
return BigDecimal(-that.value, that.scale)
|
|
175
|
+
if self.scale > that.scale:
|
|
176
|
+
return BigDecimal(self.value - that.rescale(self.scale).value, self.scale)
|
|
177
|
+
if self.scale < that.scale:
|
|
178
|
+
return BigDecimal(self.rescale(that.scale).value - that.value, that.scale)
|
|
179
|
+
return BigDecimal(self.value - that.value, self.scale)
|
|
180
|
+
|
|
181
|
+
def multiply(self, that: BigDecimal) -> BigDecimal:
|
|
182
|
+
if self.value == 0 or that.value == 0:
|
|
183
|
+
return _ZERO
|
|
184
|
+
return BigDecimal(self.value * that.value, self.scale + that.scale)
|
|
185
|
+
|
|
186
|
+
def divide(self, that: BigDecimal) -> BigDecimal | None:
|
|
187
|
+
"""The quotient to 100 significant digits, the last rounded half up;
|
|
188
|
+
``None`` for a zero divisor."""
|
|
189
|
+
return None if that.value == 0 else self.divide_unsafe(that)
|
|
190
|
+
|
|
191
|
+
def divide_unsafe(self, that: BigDecimal) -> BigDecimal:
|
|
192
|
+
"""``divide``; raises ``ZeroDivisionError`` for a zero divisor."""
|
|
193
|
+
if that.value == 0:
|
|
194
|
+
raise ZeroDivisionError("Division by zero")
|
|
195
|
+
if self.value == 0:
|
|
196
|
+
return _ZERO
|
|
197
|
+
scale = self.scale - that.scale
|
|
198
|
+
if self.value == that.value:
|
|
199
|
+
return BigDecimal(1, scale)
|
|
200
|
+
return _divide(self.value, that.value, scale, _DEFAULT_PRECISION)
|
|
201
|
+
|
|
202
|
+
def remainder(self, divisor: BigDecimal) -> BigDecimal | None:
|
|
203
|
+
"""The remainder with the dividend's sign; ``None`` for a zero divisor."""
|
|
204
|
+
return None if divisor.value == 0 else self.remainder_unsafe(divisor)
|
|
205
|
+
|
|
206
|
+
def remainder_unsafe(self, divisor: BigDecimal) -> BigDecimal:
|
|
207
|
+
if divisor.value == 0:
|
|
208
|
+
raise ZeroDivisionError("Division by zero")
|
|
209
|
+
scale = max(self.scale, divisor.scale)
|
|
210
|
+
return BigDecimal(_trem(self.rescale(scale).value, divisor.rescale(scale).value), scale)
|
|
211
|
+
|
|
212
|
+
def abs(self) -> BigDecimal:
|
|
213
|
+
return BigDecimal(-self.value, self.scale) if self.value < 0 else self
|
|
214
|
+
|
|
215
|
+
def negate(self) -> BigDecimal:
|
|
216
|
+
return BigDecimal(-self.value, self.scale)
|
|
217
|
+
|
|
218
|
+
def __add__(self, that: BigDecimal) -> BigDecimal:
|
|
219
|
+
return self.sum(that)
|
|
220
|
+
|
|
221
|
+
def __sub__(self, that: BigDecimal) -> BigDecimal:
|
|
222
|
+
return self.subtract(that)
|
|
223
|
+
|
|
224
|
+
def __mul__(self, that: BigDecimal) -> BigDecimal:
|
|
225
|
+
return self.multiply(that)
|
|
226
|
+
|
|
227
|
+
def __truediv__(self, that: BigDecimal) -> BigDecimal:
|
|
228
|
+
return self.divide_unsafe(that)
|
|
229
|
+
|
|
230
|
+
def __mod__(self, that: BigDecimal) -> BigDecimal:
|
|
231
|
+
return self.remainder_unsafe(that)
|
|
232
|
+
|
|
233
|
+
def __neg__(self) -> BigDecimal:
|
|
234
|
+
return self.negate()
|
|
235
|
+
|
|
236
|
+
def __abs__(self) -> BigDecimal:
|
|
237
|
+
return self.abs()
|
|
238
|
+
|
|
239
|
+
# --- rounding ---------------------------------------------------------------------
|
|
240
|
+
|
|
241
|
+
def truncate(self, scale: int = 0) -> BigDecimal:
|
|
242
|
+
"""Digits beyond ``scale`` dropped (toward zero)."""
|
|
243
|
+
if self.scale <= scale:
|
|
244
|
+
return self
|
|
245
|
+
return BigDecimal(_tdiv(self.value, 10 ** (self.scale - scale)), scale)
|
|
246
|
+
|
|
247
|
+
def ceil(self, scale: int = 0) -> BigDecimal:
|
|
248
|
+
truncated = self.truncate(scale)
|
|
249
|
+
if self.value > 0 and truncated < self:
|
|
250
|
+
return truncated.sum(BigDecimal(1, scale))
|
|
251
|
+
return truncated
|
|
252
|
+
|
|
253
|
+
def floor(self, scale: int = 0) -> BigDecimal:
|
|
254
|
+
truncated = self.truncate(scale)
|
|
255
|
+
if self.value < 0 and truncated > self:
|
|
256
|
+
return truncated.sum(BigDecimal(-1, scale))
|
|
257
|
+
return truncated
|
|
258
|
+
|
|
259
|
+
def digit_at(self, scale: int) -> int:
|
|
260
|
+
"""The digit at ``scale`` (0 the units, 1 the tenths, -1 the tens),
|
|
261
|
+
negative for a negative number."""
|
|
262
|
+
if self.scale < scale:
|
|
263
|
+
return 0
|
|
264
|
+
return _trem(_tdiv(self.value, 10 ** (self.scale - scale)), 10)
|
|
265
|
+
|
|
266
|
+
def round(self, *, scale: int = 0, mode: RoundingMode = "half-from-zero") -> BigDecimal: # noqa: PLR0911
|
|
267
|
+
"""Rounded to ``scale`` digits by ``mode``."""
|
|
268
|
+
up, down = BigDecimal(5, scale + 1), BigDecimal(-5, scale + 1)
|
|
269
|
+
match mode:
|
|
270
|
+
case "ceil":
|
|
271
|
+
return self.ceil(scale)
|
|
272
|
+
case "floor":
|
|
273
|
+
return self.floor(scale)
|
|
274
|
+
case "to-zero":
|
|
275
|
+
return self.truncate(scale)
|
|
276
|
+
case "from-zero":
|
|
277
|
+
return self.ceil(scale) if self.value > 0 else self.floor(scale)
|
|
278
|
+
case "half-ceil":
|
|
279
|
+
return self.sum(up).floor(scale)
|
|
280
|
+
case "half-floor":
|
|
281
|
+
return self.sum(down).ceil(scale)
|
|
282
|
+
case "half-to-zero":
|
|
283
|
+
return self.sum(up).floor(scale) if self.value < 0 else self.sum(down).ceil(scale)
|
|
284
|
+
case "half-from-zero":
|
|
285
|
+
return self.sum(down).ceil(scale) if self.value < 0 else self.sum(up).floor(scale)
|
|
286
|
+
case "half-even" | "half-odd":
|
|
287
|
+
half_ceil, half_floor = self.sum(up).floor(scale), self.sum(down).ceil(scale)
|
|
288
|
+
if half_ceil == half_floor:
|
|
289
|
+
return half_ceil
|
|
290
|
+
even = _trem(half_ceil.digit_at(scale), 2) == 0
|
|
291
|
+
return half_ceil if even == (mode == "half-even") else half_floor
|
|
292
|
+
|
|
293
|
+
# --- comparison -------------------------------------------------------------------
|
|
294
|
+
|
|
295
|
+
def compare(self, that: BigDecimal) -> Literal[-1, 0, 1]:
|
|
296
|
+
"""Effect's ``Order``: ``-1``, ``0`` or ``1``."""
|
|
297
|
+
if self.scale == that.scale:
|
|
298
|
+
return _cmp(self.value, that.value)
|
|
299
|
+
mine, theirs = self.sign(), that.sign()
|
|
300
|
+
if mine != theirs:
|
|
301
|
+
return -1 if mine < theirs else 1
|
|
302
|
+
if mine == 0:
|
|
303
|
+
return 0
|
|
304
|
+
difference = self.scale - that.scale
|
|
305
|
+
if abs(difference) > _MAX_COMPARISON_SCALE_ALIGNMENT:
|
|
306
|
+
return _magnitude(that, self) if mine == -1 else _magnitude(self, that)
|
|
307
|
+
power = 10 ** abs(difference)
|
|
308
|
+
if difference > 0:
|
|
309
|
+
return _cmp(self.value, that.value * power)
|
|
310
|
+
return _cmp(self.value * power, that.value)
|
|
311
|
+
|
|
312
|
+
def between(self, *, minimum: BigDecimal, maximum: BigDecimal) -> bool:
|
|
313
|
+
"""``minimum <= self <= maximum``."""
|
|
314
|
+
return minimum <= self <= maximum
|
|
315
|
+
|
|
316
|
+
def clamp(self, *, minimum: BigDecimal, maximum: BigDecimal) -> BigDecimal:
|
|
317
|
+
"""``self`` held within ``minimum`` and ``maximum``."""
|
|
318
|
+
return min(max(self, minimum), maximum)
|
|
319
|
+
|
|
320
|
+
def __eq__(self, other: object) -> bool:
|
|
321
|
+
if not isinstance(other, BigDecimal):
|
|
322
|
+
return NotImplemented
|
|
323
|
+
return self.compare(other) == 0
|
|
324
|
+
|
|
325
|
+
def __hash__(self) -> int:
|
|
326
|
+
n = self.normalize()
|
|
327
|
+
return hash((n.value, n.scale))
|
|
328
|
+
|
|
329
|
+
def __lt__(self, other: BigDecimal) -> bool:
|
|
330
|
+
return self.compare(other) < 0
|
|
331
|
+
|
|
332
|
+
def __le__(self, other: BigDecimal) -> bool:
|
|
333
|
+
return self.compare(other) <= 0
|
|
334
|
+
|
|
335
|
+
def __gt__(self, other: BigDecimal) -> bool:
|
|
336
|
+
return self.compare(other) > 0
|
|
337
|
+
|
|
338
|
+
def __ge__(self, other: BigDecimal) -> bool:
|
|
339
|
+
return self.compare(other) >= 0
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def _normalized(value: int, scale: int) -> BigDecimal:
|
|
343
|
+
out = BigDecimal(value, scale)
|
|
344
|
+
object.__setattr__(out, "_normalized", out)
|
|
345
|
+
return out
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
_ZERO = _normalized(0, 0)
|
|
349
|
+
_ONE = _normalized(1, 0)
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def _magnitude(a: BigDecimal, b: BigDecimal) -> Literal[-1, 0, 1]:
|
|
353
|
+
"""Compare ``|a|`` and ``|b|`` without aligning their scales."""
|
|
354
|
+
a_digits, b_digits = int_text(abs(a.value)), int_text(abs(b.value))
|
|
355
|
+
exponent = len(a_digits) - len(b_digits) - a.scale + b.scale
|
|
356
|
+
if exponent != 0:
|
|
357
|
+
return -1 if exponent < 0 else 1
|
|
358
|
+
width = max(len(a_digits), len(b_digits))
|
|
359
|
+
left, right = a_digits.ljust(width, "0"), b_digits.ljust(width, "0")
|
|
360
|
+
return -1 if left < right else 1 if left > right else 0
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def round_terminal(n: int) -> int:
|
|
364
|
+
"""``1`` when the leading digit of ``n`` is 5 or more, else ``0``."""
|
|
365
|
+
return 0 if int(int_text(abs(n))[0]) < 5 else 1
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _divide(num: int, den: int, scale: int, precision: int) -> BigDecimal:
|
|
369
|
+
negate = (num < 0) != (den < 0)
|
|
370
|
+
num, den = abs(num), abs(den)
|
|
371
|
+
while num < den:
|
|
372
|
+
num *= 10
|
|
373
|
+
scale += 1
|
|
374
|
+
quotient, remainder = divmod(num, den)
|
|
375
|
+
if remainder == 0:
|
|
376
|
+
return BigDecimal(-quotient if negate else quotient, scale)
|
|
377
|
+
count = len(int_text(quotient))
|
|
378
|
+
remainder *= 10
|
|
379
|
+
while remainder != 0 and count < precision:
|
|
380
|
+
q, r = divmod(remainder, den)
|
|
381
|
+
quotient = quotient * 10 + q
|
|
382
|
+
remainder = r * 10
|
|
383
|
+
count += 1
|
|
384
|
+
scale += 1
|
|
385
|
+
if remainder != 0:
|
|
386
|
+
quotient += round_terminal(remainder // den)
|
|
387
|
+
return BigDecimal(-quotient if negate else quotient, scale)
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
# --- constructors -------------------------------------------------------------------
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def make(value: int, scale: int) -> BigDecimal:
|
|
394
|
+
"""``value * 10 ** -scale``; raises ``ValueError`` for a scale past
|
|
395
|
+
``2**53 - 1`` either way."""
|
|
396
|
+
return BigDecimal(value, scale)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def make_normalized_unsafe(value: int, scale: int) -> BigDecimal:
|
|
400
|
+
"""A ``BigDecimal`` already normalized; raises ``ValueError`` when
|
|
401
|
+
``value`` ends in a zero."""
|
|
402
|
+
if value != 0 and value % 10 == 0:
|
|
403
|
+
raise ValueError("Value must be normalized")
|
|
404
|
+
_check_scale(scale)
|
|
405
|
+
return _normalized(value, scale)
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
def from_big_int(n: int) -> BigDecimal:
|
|
409
|
+
return BigDecimal(n, 0)
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def from_number(n: float) -> BigDecimal | None:
|
|
413
|
+
"""The decimal JavaScript prints for ``n`` (``0.1`` is ``0.1``, not the
|
|
414
|
+
binary fraction); ``None`` for NaN and the infinities."""
|
|
415
|
+
if not math.isfinite(n):
|
|
416
|
+
return None
|
|
417
|
+
text = js_number(n)
|
|
418
|
+
if "e" in text:
|
|
419
|
+
return from_string(text)
|
|
420
|
+
lead, _, trail = text.partition(".")
|
|
421
|
+
return BigDecimal(int(lead + trail), len(trail))
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def from_number_unsafe(n: float) -> BigDecimal:
|
|
425
|
+
"""``from_number``; raises ``ValueError`` for a non-finite ``n``."""
|
|
426
|
+
out = from_number(n)
|
|
427
|
+
if out is None:
|
|
428
|
+
raise ValueError(f"Number must be finite, got {js_number(n)}")
|
|
429
|
+
return out
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def _finite_int(text: str) -> int | None:
|
|
433
|
+
"""``[+-]digits`` as an int (JavaScript's ``BigInt``), else ``None``."""
|
|
434
|
+
sign = text[:1]
|
|
435
|
+
if sign not in ("+", "-"):
|
|
436
|
+
return digits_value(text)
|
|
437
|
+
magnitude = digits_value(text[1:])
|
|
438
|
+
return None if magnitude is None else -magnitude if sign == "-" else magnitude
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def from_string(text: str) -> BigDecimal | None:
|
|
442
|
+
"""``[+-]digits[.digits][e[+-]digits]`` (``""`` is zero); ``None`` for
|
|
443
|
+
anything else or a scale past ``2**53 - 1``."""
|
|
444
|
+
if text == "":
|
|
445
|
+
return _ZERO
|
|
446
|
+
base, exp = text, 0
|
|
447
|
+
separator = next((i for i, c in enumerate(text) if c in "eE"), -1)
|
|
448
|
+
if separator != -1:
|
|
449
|
+
base, parsed = text[:separator], _finite_int(text[separator + 1 :])
|
|
450
|
+
if base == "" or parsed is None or not -_MAX_SAFE <= parsed <= _MAX_SAFE:
|
|
451
|
+
return None
|
|
452
|
+
exp = parsed
|
|
453
|
+
lead, dot, fraction = base.partition(".")
|
|
454
|
+
value = _finite_int(lead + fraction if dot else base)
|
|
455
|
+
if value is None:
|
|
456
|
+
return None
|
|
457
|
+
scale = len(fraction) - exp
|
|
458
|
+
if not -_MAX_SAFE <= scale <= _MAX_SAFE:
|
|
459
|
+
return None
|
|
460
|
+
return BigDecimal(value, scale)
|
|
461
|
+
|
|
462
|
+
|
|
463
|
+
def from_string_unsafe(text: str) -> BigDecimal:
|
|
464
|
+
"""``from_string``; raises ``ValueError`` for an invalid string."""
|
|
465
|
+
out = from_string(text)
|
|
466
|
+
if out is None:
|
|
467
|
+
raise ValueError(f"Invalid numerical string: {text}")
|
|
468
|
+
return out
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def sum_all(values: Iterable[BigDecimal]) -> BigDecimal:
|
|
472
|
+
out = _ZERO
|
|
473
|
+
for n in values:
|
|
474
|
+
out = out.sum(n)
|
|
475
|
+
return out
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def multiply_all(values: Iterable[BigDecimal]) -> BigDecimal:
|
|
479
|
+
out = _ONE
|
|
480
|
+
for n in values:
|
|
481
|
+
if n.value == 0:
|
|
482
|
+
return _ZERO
|
|
483
|
+
out = out.multiply(n)
|
|
484
|
+
return out
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
__all__ = [
|
|
488
|
+
"BigDecimal",
|
|
489
|
+
"RoundingMode",
|
|
490
|
+
"from_big_int",
|
|
491
|
+
"from_number",
|
|
492
|
+
"from_number_unsafe",
|
|
493
|
+
"from_string",
|
|
494
|
+
"from_string_unsafe",
|
|
495
|
+
"make",
|
|
496
|
+
"make_normalized_unsafe",
|
|
497
|
+
"multiply_all",
|
|
498
|
+
"round_terminal",
|
|
499
|
+
"sum_all",
|
|
500
|
+
]
|
pyfect/brand.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Branded types with checked constructors (Effect's ``Brand``).
|
|
2
|
+
|
|
3
|
+
A brand is a ``typing.NewType``: nominal under pyright, the plain value at
|
|
4
|
+
run time. ``Brand[Base, NewType]`` makes constructors for it: ``make`` and
|
|
5
|
+
``check`` ones that validate, ``nominal`` one that does not, and ``all``
|
|
6
|
+
one with the checks of several::
|
|
7
|
+
|
|
8
|
+
Int = NewType("Int", float)
|
|
9
|
+
PositiveInt = NewType("PositiveInt", float)
|
|
10
|
+
|
|
11
|
+
to_int = Brand[float, Int].make(lambda n: n.is_integer() or f"Expected {n} to be an integer")
|
|
12
|
+
positive = Brand[float, Int].check(S.is_greater_than(0))
|
|
13
|
+
to_positive_int = Brand[float, PositiveInt].all(to_int, positive)
|
|
14
|
+
|
|
15
|
+
to_positive_int(1.0) # PositiveInt(1.0)
|
|
16
|
+
to_positive_int(-1.5) # raises BrandError
|
|
17
|
+
to_positive_int.result(-1.5) # Err(BrandError(...))
|
|
18
|
+
|
|
19
|
+
A predicate passes with ``True`` or ``None``; ``False`` fails with
|
|
20
|
+
``"Expected <filter>"`` and a string fails with that message. ``check``
|
|
21
|
+
takes schema checks (``S.is_int()``, ``S.make_filter(...)``). Every check
|
|
22
|
+
runs and the messages are joined by newlines, except that an aborted check
|
|
23
|
+
(``.abort()``) that fails stops the rest. ``S.from_brand(brand)`` runs a
|
|
24
|
+
brand's checks in a schema.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
from collections.abc import Callable
|
|
30
|
+
from dataclasses import dataclass
|
|
31
|
+
from typing import Any, Self, TypeGuard, cast
|
|
32
|
+
|
|
33
|
+
from pyfect.errors import TaggedError
|
|
34
|
+
from pyfect.option import Option, Some
|
|
35
|
+
from pyfect.result import Err, Ok, Result
|
|
36
|
+
from pyfect.schema.ast import Check, FilterOutput, collect_issues, make_filter, unknown
|
|
37
|
+
from pyfect.schema.issue import Composite, Issue, format_issue
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(eq=False)
|
|
41
|
+
class BrandError(TaggedError, ValueError):
|
|
42
|
+
"""A value failed a brand's checks. ``issue`` says why; ``message``
|
|
43
|
+
(also ``str(error)``) is the issue in Effect's default format, one line
|
|
44
|
+
per failed check."""
|
|
45
|
+
|
|
46
|
+
issue: Issue
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def message(self) -> str:
|
|
50
|
+
return format_issue(self.issue)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class Brand[A, B]:
|
|
55
|
+
"""A checked constructor that brands an ``A`` as ``B`` (Effect's
|
|
56
|
+
``Brand.Constructor``). Name both types when making one:
|
|
57
|
+
``Brand[float, Int].make(...)``. Calling it raises ``BrandError`` on
|
|
58
|
+
failure; ``option``, ``result`` and ``is_`` report it instead.
|
|
59
|
+
``checks`` are schema checks, so ``S.from_brand`` can run them."""
|
|
60
|
+
|
|
61
|
+
checks: tuple[Check[A], ...] = ()
|
|
62
|
+
|
|
63
|
+
@classmethod
|
|
64
|
+
def nominal(cls) -> Self:
|
|
65
|
+
"""A constructor that checks nothing."""
|
|
66
|
+
return cls()
|
|
67
|
+
|
|
68
|
+
@classmethod
|
|
69
|
+
def make(cls, predicate: Callable[[A], FilterOutput]) -> Self:
|
|
70
|
+
"""A constructor with one check, ``predicate``: ``True`` / ``None``
|
|
71
|
+
pass, ``False`` fails with ``"Expected <filter>"`` and a string
|
|
72
|
+
fails with that message."""
|
|
73
|
+
return cls((make_filter(predicate),))
|
|
74
|
+
|
|
75
|
+
@classmethod
|
|
76
|
+
def check(cls, first: Check[A], /, *rest: Check[A]) -> Self:
|
|
77
|
+
"""A constructor running the schema checks in order (every one,
|
|
78
|
+
unless one marked ``abort()`` fails)."""
|
|
79
|
+
return cls((first, *rest))
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def all(cls, first: Brand[A, object], /, *rest: Brand[A, object]) -> Self:
|
|
83
|
+
"""A constructor running the checks of every brand given, in order."""
|
|
84
|
+
return cls(tuple(c for b in (first, *rest) for c in b.checks))
|
|
85
|
+
|
|
86
|
+
def __call__(self, value: A) -> B:
|
|
87
|
+
match self.result(value):
|
|
88
|
+
case Ok(branded):
|
|
89
|
+
return branded
|
|
90
|
+
case Err(error):
|
|
91
|
+
raise error
|
|
92
|
+
|
|
93
|
+
def result(self, value: A) -> Result[B, BrandError]:
|
|
94
|
+
checks = cast("tuple[Check[Any], ...]", self.checks)
|
|
95
|
+
issues = collect_issues(checks, value, unknown, {"errors": "all"})
|
|
96
|
+
if issues is not None:
|
|
97
|
+
return Err(BrandError(Composite(unknown, issues)))
|
|
98
|
+
# A brand is a ``NewType``: the branded value is the value itself.
|
|
99
|
+
return Ok(cast("B", value))
|
|
100
|
+
|
|
101
|
+
def option(self, value: A) -> Option[B]:
|
|
102
|
+
"""``Some(branded)``, or ``None`` when ``value`` fails."""
|
|
103
|
+
match self.result(value):
|
|
104
|
+
case Ok(branded):
|
|
105
|
+
return Some(branded)
|
|
106
|
+
case Err():
|
|
107
|
+
return None
|
|
108
|
+
|
|
109
|
+
def is_(self, value: A) -> TypeGuard[B]:
|
|
110
|
+
"""``value`` passes the checks (and so is already a ``B`` at run time)."""
|
|
111
|
+
return isinstance(self.result(value), Ok)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
__all__ = ["Brand", "BrandError"]
|