New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature Request: help message, type checker and input restrictions #7
Comments
We will look into this.
Hmmmm, I'm not sure if this kind of restriction is "pythonic", though I don't find including jsonc support could be harmful. Thank you for reporting these! |
Sorry, I cannot get your point, could you explain more?
I think it is not "Pythonic" at all. But writing too many type checks or input restriction codes is really annoying. I expect a more convenient way to do this. During my development, I need to manually check if some configs are missing. especially for those configurations that need user inputs. After that, I considered adding type hint/checker/choices to my configuration file. Now, I am not sure if it is necessary since some types are conventional (e.g. batch_size must be integer). Do you have any suggestions or ideas? |
The reason why we make
Could you elaborate more on why you need these checks? |
OK. I know what you mean. I have tested those configurations added by I would add type checks for programs that need many resources. In deep learning experiments, we usually define the dataset, dataloader, model sequentially and run |
Thank you for suggestion and clarification.
In the next few versions, we will include the pre-defined keys in the
This is very convincing and I believe this behaviour is mandatory to We will include the support of |
Hello, I found another more pythonic way for type hints. The config can be a Python file rather than yaml/json, as your usage example does. Type hints could be added through from chanfig import Config
from rich import print
class TestConfig(Config):
def __init__(self):
super().__init__()
self.name: str = "CHANfiG"
self.seed: int = 1013
self.data.batch_size: int = 64
self.model.encoder.num_layers: int = 6
self.model.decoder.num_layers: int = 6
self.activation: str = "GELU"
self.optim.lr: float = 1e-3
if __name__ == '__main__':
config = TestConfig()
config = config.parse()
config.freeze()
print(config) Or we can define a wrapper on the value, e.g.: from chanfig import Value
# ...
self.data.batch_size = Value(6, type=int, validator=lambda e: 4 < e < 128, required=False)
self.activation = Value('GELU', type=str, choices=['GELU', 'RELU', 'TanH'], required=True)
# ... I think the second way won't hurt default user behaviors and does fewer modifications to our code. |
This is an excellent idea, I'm working on it.
Using Although it won't hurt to add this feature, I'll work on it. |
Thank you for your hard working! While the first one can only do type checking and weak in the data validator part. For the second implementation, maybe we can save two copy of configs? One for current, one for the new Values, the Value only build a data validator entry when calling |
e4d179f add support for strong data validation for The first one is much harder than it appears and requires deep hack into the underling implementation. I'll continue work on it.
Considering two Config, a Maybe I could was too cautious, I use |
Awesome! So quick update, I would try to use |
maybe the |
This would be straightforward for experienced engineers and researcher. Although we could do modify the get/set method of Wrapper. If the value to set is a Validator, we set the ValidatorConfig, otherwise, we validate the value and then set the ValueConfig. |
No worries. I promised duanwu festival and I must honor it. |
To be honest and a little offense, I think those python users may not be our target users.
Maybe you misunderstand the logic or I haven't gotten your point? There is no situation that "the value to set is a Validator". For the user side, they would use: # ...
self.exp.seed = 42
self.data.batch_size = Wrapper(6, type=int, validator=lambda e: 4 < e < 128, required=False)
self.activation = Wrapper('GELU', type=str, choices=['GELU', 'RELU', 'TanH'], required=True)
# ... and in chanfig, it would create two flatten dict (I am not familiar with the implementation): data = {"exp.seed": 42, "data.batch_size": 6, "activation": "GELU"}
validators = {
"exp.seed": Validator(), # default
"data.batch_size": Validator(type=int, validator=lambda e: 4 < e < 128, required=False),
"activation": Validator(type=str, choices=['GELU', 'RELU', 'TanH'], required=True),
} and when we call def __setattr__(self, key, value):
if self.validators[key].check(value):
self.data[key] = value
else:
raise ValueError(...) Users won't access the validator directly, the Wrapper would define the validator. |
Please just ignore me... After re-check your commit e4d179f, I found it actually does what I want. Maybe the brain is ruined at night... The current implementation meets my demands. There is no reason to discuss how we implement the Wrapper (you have finished). Sorry and hope you have a good duanwu festival. |
Actually, this package (and also DanLing) is targeted for those who has minimum knowledge in programming. Therefore, every APIs are defined following intuitive. It should just works.
Actually our thoughts are the same. |
Sorry, my bad. Please feel free to keep your opinion.
Yes, I have found our thoughts are the same. Currently, I'm trying to use |
We just released v0.0.83 this~ |
We just pushed a fix, please let me know if the help message can be improved. |
Glad to hear that. I would try the newest version later. I apologize for my delayed response as I was sick. |
No worries, thank you so much for your kind and quick feedback Wish you a speedy recovery |
I think the Python Cookbook 3 section 9.20 (page 394) is helpful for us to do something with the type hinting. |
I have implemented corresponding code for annotation validation. |
I have fixed the compatibilities issue with previous python version. Note that due to the limitation of Python, we can only check annotations defined in class. |
When using this library, I typically write a
yaml
orjson
file as a configuration and load it usingchanfig.Config
. However, I have encountered two issues that make the process a bit uncomfortable:argparse
provides, could be shown.--type
and--choices
inargparse
). It would be more convenient if we can use a comment (useJSONC
forJSON
) to restrict input type and other conditions. For example, a configuration file in YAML format could look like this:Comments quoted with
**
would be parsed into input checker byCHANfiG
. When there is no comment,CHANfiG
parser would roll back to the current version. Although it is a bit ugly, I haven't found any other better solutions so far.I believe addressing these points would greatly enhance the development experience with
CHANfiG
. I would appreciate any comments or suggestions you may have on these matters.The text was updated successfully, but these errors were encountered: