Skip to content

Configuration parsing: "digits + underline" should be considered as a string #862

Open

Description

@Jarvis73

When parsing configurations (such as name=123_456), maybe the digits + underline should be considered as a string? Now the underline will be ignored and the remaining digits are treated as an integer.

Example (main.py):

from sacred import Experiment

ex = Experiment("Exp")

@ex.config
def config():
    name = "abc"

@ex.automain
def main(_config):
    print(_config['name'], type(_config['name']))
  • For python main.py, we get abc <class 'str'>.
  • For python main.py with name=123_456, we get 123456 <class 'int'>, which is quite wired.

A simple solution is to run the script with python main.py with name='"123_456"', but the command is a little messy and not intuitive.

Activity

  1. thequilo commented on Feb 23, 2022

    @thequilo
    Collaborator

    Hi @Jarvis73!

    Sacred treats the arguments of configuration values as python literals (and treats everything it can't handle as a string), and what you see is actually just the way that python interprets numbers. I actually like the idea to just treat everything as python literal because it seems intuitive (to me, at least). But, I agree that we can come up with weird things:

    • For python main.py with name=0xdeef, we get 57071 <class 'int'>
    • For python main.py with name=1j, we get a TypeError (because JSON apparently can't serialize complex numbers?)
    • For python main.py with name=1e-1, we get 0.1 <class 'float'>. Obvious, but I really wouldn't expect that when seeing name=1e-1
    • python main.py with name=['a'] we get [a] <class 'str'>, and
    • For python main.py with name=["'a'"], we get ['a'] <class 'sacred.config.custom_containers.ReadOnlyList'>

    This list could probably go on for a while, and after writing it down, I start to feel that it is actually not that intuitive. But, this list also shows that if we start to have exceptions to the "treat it as a literal" rule, we'll end up with many of them.

  2. Jarvis73 commented on Feb 24, 2022

    @Jarvis73
    Author

    Hi @thequilo!

    Thanks for the detailed explanation. Now I can understand the design purpose, and maybe it is more reasonable, in spite that we have to use '"xxx"' in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions