Skip to content

Conversation

Madhav2310
Copy link
Contributor

@Madhav2310 Madhav2310 commented Jun 21, 2022

In continuation to #397

@Madhav2310
Copy link
Contributor Author

@certik
Copy link
Contributor

certik commented Jun 21, 2022

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.

@Madhav2310
Copy link
Contributor Author

@czgdp1807 could you please help me fix this error?

test_capitalize()
test_upper()

check()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add final new line.

def test_capitalize():
i: str
i = capitalize("lpython")
assert i == "Lpython"
Copy link
Collaborator

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"

assert i == "Lpython"
i: str
i = capitalize("development")
assert i == "Development"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar for this.


def test_upper():
i: str
i = upper("lpython")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar for this.

Return a copy of the string with its first character capitalized and the rest lowercased.
"""
result: str
result = upper(s[0]) + s[1:]
Copy link
Collaborator

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.

Copy link
Collaborator

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.

char : str

for char in s:
if ord(char) >= 97 and ord(char) <=122 :
Copy link
Collaborator

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'.

@czgdp1807
Copy link
Collaborator

I think you have to register captialize, upper and the module string.py in https://github.com/lcompilers/lpython/blob/main/src/lpython/semantics/python_comptime_eval.h file. Just look how other functions are registered.

@namannimmo10
Copy link
Collaborator

You can also maybe include string constants.

@namannimmo10
Copy link
Collaborator

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.
@Madhav2310
Copy link
Contributor Author

Madhav2310 commented Jun 25, 2022

I think you have to register captialize, upper and the module string.py in https://github.com/lcompilers/lpython/blob/main/src/lpython/semantics/python_comptime_eval.h file. Just look how other functions are registered.

Should I just add these functions to python_builtin.py?
On looking more closely these string methods are all of str of python builtin functions: https://docs.python.org/3/library/stdtypes.html#string-methods

The string.py module has wholly different functions like format, parse, get_value etc. : https://docs.python.org/3/library/string.html

@Madhav2310
Copy link
Contributor Author

@czgdp1807 I've tried adding upper, lower, and capitalize in python_comptime_eval.h. Please review and point me towards the next step

Madhav2310 and others added 3 commits July 8, 2022 11:23
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")
Copy link
Collaborator

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)

@certik
Copy link
Contributor

certik commented Jul 11, 2022

If I understand correctly, this implements both runtime and compile time capitalization? If so, we need to also add tests for both use cases.

@Madhav2310
Copy link
Contributor Author

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

@Madhav2310
Copy link
Contributor Author

@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 czgdp1807 added the could close Issues/PRs which can be closed. label Oct 5, 2022
@certik
Copy link
Contributor

certik commented Oct 5, 2022

@czgdp1807 what is the status of this, was this implemented?

@certik
Copy link
Contributor

certik commented Oct 5, 2022

Obviously it is not finished, so I'll mark it as Draft for now.

@certik certik marked this pull request as draft October 5, 2022 11:38
@czgdp1807 czgdp1807 added please take over PRs which can be taken over by other contributors and removed could close Issues/PRs which can be closed. labels Oct 5, 2022
@czgdp1807
Copy link
Collaborator

Yes. I have added please take over label to it if someone wants to complete this.

@Smit-create
Copy link
Collaborator

Just the upper method is needed now. Will rebase this.

@Smit-create Smit-create self-assigned this Nov 9, 2022
@Harsh-Upadhayay
Copy link

Hi, if this issue is still remaining then I'd like to take it over and solve it.
After reading the above conversation I have a vague idea that some of the bulitins of string library were implemented and the changes didn't passed integration tests.
Could someone please summaries what needs to be done and what was done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
please take over PRs which can be taken over by other contributors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants