Skip to content

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

Merged
cthulahoops merged 3 commits into
RustPython:masterfrom
lausek:master
Feb 9, 2019
Merged

cthulahoops merged 3 commits into
RustPython:masterfrom
lausek:master

Conversation

@lausek

@lausek lausek commented Feb 5, 2019 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

lausek commented Feb 5, 2019

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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

mrveera commented Feb 6, 2019

Copy link
Copy Markdown
Contributor

/ignore
Related to #304

@lausek

lausek commented Feb 6, 2019

Copy link
Copy Markdown
Contributor Author

@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
33 of 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