Skip to content
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

Convert string to MIT #81

Open
Nic2020 opened this issue Aug 21, 2024 · 4 comments
Open

Convert string to MIT #81

Nic2020 opened this issue Aug 21, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@Nic2020
Copy link
Member

Nic2020 commented Aug 21, 2024

I would be great to have a function to convert a string to an MIT of the correct frequency.

For instance:

frequencymap = Dict{Char,Any}(
    'Q' => Quarterly,
    'M' => Monthly,
    'Y' => Yearly,
)
convert_string_to_mit(t::AbstractString) = length(t) > 5 ? MIT{frequencymap[t[5]]}(parse(Int, t[1:4]), parse(Int, t[6:end])) : MIT{Yearly}(parse(Int, t[1:4]))
convert_string_to_mit("2022")
convert_string_to_mit("2022M11")
convert_string_to_mit("2022Q3")

We could also use Julia's syntax: convert(MIT{Monthly},"2022M1")

@Nic2020 Nic2020 added the enhancement New feature or request label Aug 21, 2024
@bbejanov
Copy link
Member

I've been using, for example, eval(Meta.parse("2022M1")), and thought it's simple enough that it didn't need to be packaged in a new function. I felt the overhead of maintaining another function (doc string, unit tests) wasn't worth it. But we can add it, if you see value in doing so. Let me know.

@Nic2020
Copy link
Member Author

Nic2020 commented Aug 21, 2024

I don't feel strongly about this, but it might be more natural to think about convert(MIT{Monthly},"2022M1") compared to eval(Meta.parse("2022M1")). The performance is also a bit better when avoiding eval(). Below, myfunc uses eval().
2024-08-21_17-18-21

@bbejanov
Copy link
Member

How about we add a method to parse, e.g. Base.parse(::Type{<:MIT}, ::AbstractString). This way it's not a new function. I feel that if we're producing a value from a string that's parsing rather than a conversion. What do you think?

@Nic2020
Copy link
Member Author

Nic2020 commented Aug 21, 2024

Great idea!

@bbejanov bbejanov self-assigned this Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants