added floor and ceil to frappe.utils (#5536)

Added frappe wrappers to math.ceil and math.floor, since they are widely
used functions. Also added their docstrings for documentation and wote
test cases. And some minor typo fixes
This commit is contained in:
Ameya Shenoy 2018-05-08 15:13:50 +05:30 committed by Nabin Hait
parent 09cbf49ff1
commit 09e71d0d77
2 changed files with 61 additions and 3 deletions

View file

@ -5,6 +5,7 @@ from __future__ import unicode_literals
import unittest
from frappe.utils import evaluate_filters, money_in_words, scrub_urls, get_url
from frappe.utils import ceil, floor
class TestFilters(unittest.TestCase):
def test_simple_dict(self):
@ -84,3 +85,22 @@ class TestDataManipulation(unittest.TestCase):
self.assertTrue('<img src="{0}/assets/frappe/test.jpg">'.format(url) in html)
self.assertTrue('style="background-image: url(\'{0}/assets/frappe/bg.jpg\') !important"'.format(url) in html)
self.assertTrue('<a href="mailto:test@example.com">email</a>' in html)
class TestMathUtils(unittest.TestCase):
def test_floor(self):
from decimal import Decimal
self.assertEqual(floor(2), 2 )
self.assertEqual(floor(12.32904), 12)
self.assertEqual(floor(22.7330), 22)
self.assertEqual(floor('24.7'), 24)
self.assertEqual(floor('26.7'), 26)
self.assertEqual(floor(Decimal(29.45)), 29)
def test_ceil(self):
from decimal import Decimal
self.assertEqual(ceil(2), 2 )
self.assertEqual(ceil(12.32904), 13)
self.assertEqual(ceil(22.7330), 23)
self.assertEqual(ceil('24.7'), 25)
self.assertEqual(ceil('26.7'), 27)
self.assertEqual(ceil(Decimal(29.45)), 30)

View file

@ -24,7 +24,7 @@ DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT
# datetime functions
def getdate(string_date=None):
"""
Coverts string date (yyyy-mm-dd) to datetime.date object
Converts string date (yyyy-mm-dd) to datetime.date object
"""
if not string_date:
@ -208,7 +208,7 @@ def get_user_format():
def formatdate(string_date=None, format_string=None):
"""
Convers the given string date to :data:`user_format`
Converts the given string date to :data:`user_format`
User format specified in defaults
Examples:
@ -282,6 +282,44 @@ def cint(s):
except: num = 0
return num
def floor(s):
"""
A number representing the largest integer less than or equal to the specified number
Parameters
----------
s : int or str or Decimal object
The mathematical value to be floored
Returns
-------
int
number representing the largest integer less than or equal to the specified number
"""
try: num = cint(math.floor(flt(s)))
except: num = 0
return num
def ceil(s):
"""
The smallest integer greater than or equal to the given number
Parameters
----------
s : int or str or Decimal object
The mathematical value to be ceiled
Returns
-------
int
smallest integer greater than or equal to the given number
"""
try: num = cint(math.ceil(flt(s)))
except: num = 0
return num
def cstr(s, encoding='utf-8'):
return frappe.as_unicode(s, encoding)
@ -436,7 +474,7 @@ def get_number_format_info(format):
return number_format_info.get(format) or (".", ",", 2)
#
# convet currency to words
# convert currency to words
#
def money_in_words(number, main_currency = None, fraction_currency=None):
"""