-
Notifications
You must be signed in to change notification settings - Fork 170
capitalize and upper functions (String module) #669
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
base: main
Are you sure you want to change the base?
Conversation
See the discussion here: https://gitlab.com/lfortran/lfortran/-/merge_requests/1752 I think we should add StringChr, StringOrd or something like that, and just use those for chr(). That is a builtin function, unrelated to this PR. But I wanted to point that out. |
@czgdp1807 could you please help me fix this error? |
integration_tests/test_string.py
Outdated
test_capitalize() | ||
test_upper() | ||
|
||
check() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add final new line.
integration_tests/test_string.py
Outdated
def test_capitalize(): | ||
i: str | ||
i = capitalize("lpython") | ||
assert i == "Lpython" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i = "lPythoN"
assert capitalize(i) == "Lpython"
integration_tests/test_string.py
Outdated
assert i == "Lpython" | ||
i: str | ||
i = capitalize("development") | ||
assert i == "Development" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar for this.
integration_tests/test_string.py
Outdated
|
||
def test_upper(): | ||
i: str | ||
i = upper("lpython") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar for this.
src/runtime/string.py
Outdated
Return a copy of the string with its first character capitalized and the rest lowercased. | ||
""" | ||
result: str | ||
result = upper(s[0]) + s[1:] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rest is not getting lower cased here I think. The rest is just picked up as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be fill result
character wise.
src/runtime/string.py
Outdated
char : str | ||
|
||
for char in s: | ||
if ord(char) >= 97 and ord(char) <=122 : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python allows character comparison. So you can directly do, char > 'a' and char < 'z'
.
I think you have to register |
You can also maybe include string constants. |
These functions are not available in CPython: >>> from string import upper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'upper' from 'string' (/Users/namannimmo/miniconda3/envs/lp/lib/python3.10/string.py)
>>> That's why the integration tests won't pass. This would work as a method: >>> "abc".upper()
'ABC'
>>> "abc".capitalize()
'Abc'
>>> |
This reverts commit e07b0a0.
Should I just add these functions to python_builtin.py? The string.py module has wholly different functions like format, parse, get_value etc. : https://docs.python.org/3/library/string.html |
@czgdp1807 I've tried adding |
Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
Co-authored-by: Gagandeep Singh <gdp.1807@gmail.com>
|
||
def test_capitalize(): | ||
i: str | ||
i = capitalize("lpyThon") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s: str
s = "lpyThon"
capitalize(s)
If I understand correctly, this implements both runtime and compile time capitalization? If so, we need to also add tests for both use cases. |
Yes, as soon as we get this working, I'll add the tests too |
@czgdp1807 please check now, I think the python_comptime_eval.h code works now. I'm unsure why its shows an error in the complex1 test file, as I didn't make any changes whatsoever there. Thank you! |
@czgdp1807 what is the status of this, was this implemented? |
Obviously it is not finished, so I'll mark it as Draft for now. |
Yes. I have added |
Just the |
Hi, if this issue is still remaining then I'd like to take it over and solve it. |
In continuation to #397