Skip to content

int type: rounding, index, trunc, int (#304) #326

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

Merged
merged 3 commits into from
Feb 9, 2019

Conversation

lausek
Copy link
Contributor

@lausek lausek commented Feb 5, 2019

Implements __ceil__, __floor__, __round__, __trunc__, __index__, __int__ for int

TODO: calling round with parameter should check if the value is convertible to int and fail otherwise:

x = 10
# RustPython
x.__round__("") # => 10
# CPython
x.__round__("") # => 'str' object cannot be interpreted as an integer

@lausek lausek changed the title implemented rounding funcs for int (#304) int type: rounding, index, trunc (#304) Feb 5, 2019
@cthulahoops
Copy link
Collaborator

Would it be better to test these via idiomatic python usage, rather than directly testing the magic methods?

Ie.:

round(10) == 10
math.ceil(10) == 10

(Would require implementing round and math.ceil too, but that seems like a good thing to do anyway.)

@lausek
Copy link
Contributor Author

lausek commented Feb 5, 2019

@cthulahoops That's definitely a good advice. Are you aware of a routine that could call the objects round method? I haven't found it yet, but I would implement it on PyObjectPayload.

@cthulahoops
Copy link
Collaborator

I think you just need to implement builtin_round in builtins.rs. All it does is check the arguments, and then call __round__ via vm.call_method.

@mrveera
Copy link
Contributor

mrveera commented Feb 6, 2019

/ignore
Related to #304

@lausek
Copy link
Contributor Author

lausek commented Feb 6, 2019

@cthulahoops Thanks, that was exactly the functionality I was looking for. The next step would be to implement __round__ and __int__ for float.

@lausek lausek changed the title int type: rounding, index, trunc (#304) int type: rounding, index, trunc, int (#304) Feb 6, 2019
@cthulahoops cthulahoops merged commit 8ad95c4 into RustPython:master Feb 9, 2019
@lausek lausek mentioned this pull request Feb 10, 2019
35 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants