nepali 1.0.0__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
nepali/locations/utils.py CHANGED
@@ -1,8 +1,7 @@
1
1
  import re
2
2
  from functools import partial
3
3
 
4
- from ._locations import provinces, districts, municipalities
5
-
4
+ from ._locations import districts, municipalities, provinces
6
5
 
7
6
  __all__ = [
8
7
  "get_province",
nepali/number/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
- from ._number import NepaliNumber
2
1
  from ._nepalinumber import nepalinumber
2
+ from ._number import NepaliNumber
3
3
  from .utils import (
4
4
  add_comma,
5
5
  add_comma_english,
@@ -3,6 +3,7 @@ Contains the class for the nepalinumber feature
3
3
  """
4
4
 
5
5
  from typing import Any, Tuple, Type, Union
6
+
6
7
  from .utils import NP_NUMBERS, NP_NUMBERS_SET, english_to_nepali
7
8
 
8
9
 
@@ -19,10 +20,23 @@ class nepalinumber:
19
20
  """
20
21
  self.__value = self.__parse(value)
21
22
 
22
- def _raise_parse_exception(self, obj, ex_class: Type[Exception] = ValueError):
23
- raise ex_class(
23
+ def get_parse_exception(
24
+ self, obj: object, ex_class: Type[Exception] = ValueError
25
+ ) -> Exception:
26
+ """
27
+ Returns the exception object to be raised when the parse is failed.
28
+ The methods also sets a proper message to the exception class.
29
+
30
+ :param obj: Object that is failed during the parse
31
+ :type obj: object
32
+ :param ex_class: Exception class type to be returned, defaults to ValueError
33
+ :type ex_class: Type[Exception], optional
34
+ :return: Exception object to be raised
35
+ :rtype: Exception
36
+ """
37
+ return ex_class(
24
38
  f"could not convert {obj.__class__.__name__} to {self.__class__.__name__}: '{obj}'"
25
- ) from None
39
+ )
26
40
 
27
41
  def __parse(self, value: Any) -> Union[int, float]:
28
42
  """
@@ -61,10 +75,10 @@ class nepalinumber:
61
75
 
62
76
  :raises ValueError: If the value is invalid
63
77
  """
64
- result = 0
78
+ result: float = 0
65
79
  sign = 1
66
80
  decimal_found = False
67
- decimal_place = 1
81
+ decimal_place: float = 1
68
82
  i = 0
69
83
 
70
84
  # for negative sign
@@ -77,7 +91,7 @@ class nepalinumber:
77
91
  if value[i] == ".":
78
92
  if decimal_found:
79
93
  # decimal was already found
80
- self._raise_parse_exception(value)
94
+ raise self.get_parse_exception(value) from None
81
95
  decimal_found = True
82
96
  i += 1
83
97
  continue
@@ -86,7 +100,7 @@ class nepalinumber:
86
100
  if digit < 0 or digit > 9:
87
101
  # checking nepali character
88
102
  if value[i] not in NP_NUMBERS_SET:
89
- self._raise_parse_exception(value)
103
+ raise self.get_parse_exception(value) from None
90
104
  digit = NP_NUMBERS.index(value[i])
91
105
 
92
106
  if decimal_found:
@@ -113,7 +127,7 @@ class nepalinumber:
113
127
  return self.__parse_str(str(obj))
114
128
  except (ValueError, TypeError):
115
129
  # object conversion must raise TypeError if fails
116
- self._raise_parse_exception(obj, ex_class=TypeError)
130
+ raise self.get_parse_exception(obj, ex_class=TypeError) from None
117
131
 
118
132
  def __convert_or_return(self, obj) -> Union["nepalinumber", object]:
119
133
  """
@@ -184,7 +198,7 @@ class nepalinumber:
184
198
  Checks if nepalinumber is equal to another object
185
199
 
186
200
  :param other: The other number/object which is to be checked for
187
- equality againt nepalinumber
201
+ equality against nepalinumber
188
202
  :return: True if equal else False
189
203
  """
190
204
  if isinstance(other, nepalinumber):
@@ -197,7 +211,7 @@ class nepalinumber:
197
211
  Checks if nepalinumber is not equal to another object
198
212
 
199
213
  :param other: The other number/object which is to be checked for
200
- equality againt nepalinumber
214
+ equality against nepalinumber
201
215
  :return: True if not equal else False
202
216
  """
203
217
  if isinstance(other, nepalinumber):
@@ -349,7 +363,7 @@ class nepalinumber:
349
363
  the nepalinumber object
350
364
 
351
365
  :param other: The other number/object that is to get
352
- dividied by the value in the nepalinumber object
366
+ divided by the value in the nepalinumber object
353
367
  :raises TypeError: Raised when nepalinumber object is
354
368
  used to divide unsupported data types
355
369
  :return: Returns the quotient number as a nepalinumber
@@ -388,7 +402,7 @@ class nepalinumber:
388
402
  before the nepalinumber object
389
403
 
390
404
  :param other: The other number/object that is to get
391
- dividied by the value in the nepalinumber object
405
+ divided by the value in the nepalinumber object
392
406
  :raises TypeError: Raised when nepalinumber object is
393
407
  used to divide unsupported data types
394
408
  :return: Returns the quotient number as a nepalinumber
nepali/number/_number.py CHANGED
@@ -1,11 +1,6 @@
1
1
  import warnings
2
2
 
3
- from .utils import (
4
- add_comma,
5
- add_comma_english,
6
- english_to_nepali,
7
- nepali_to_english,
8
- )
3
+ from .utils import add_comma, add_comma_english, english_to_nepali, nepali_to_english
9
4
 
10
5
 
11
6
  # Backward compatibility support for legacy NepaliNumber
nepali/number/utils.py CHANGED
@@ -1,6 +1,5 @@
1
1
  from typing import Any
2
2
 
3
-
4
3
  NP_NUMBERS = ["०", "१", "२", "३", "४", "५", "६", "७", "८", "९"]
5
4
  NP_NUMBERS_SET = set(NP_NUMBERS)
6
5
 
@@ -39,7 +38,7 @@ def add_comma_english(number: Any) -> str:
39
38
  Adds comma in english style
40
39
  Eg. 123456789 => 123,456,789
41
40
  """
42
- return "{:,}".format(int(number))
41
+ return f"{int(number):,}"
43
42
 
44
43
 
45
44
  def add_comma(number: Any, convert=False) -> str:
nepali/phone_number.py CHANGED
@@ -2,10 +2,9 @@ import re
2
2
  from enum import Enum
3
3
  from typing import Union
4
4
 
5
-
6
- _mobile_number_re = re.compile(r"^(?:\+977|977)?(?:-)?(?:98|97|96)[0-9]{8}$")
5
+ _mobile_number_re = re.compile(r"^(?:\+977|977)?(?:-)?(?:98|97|96)\d{8}$")
7
6
  _landline_number_re = re.compile(
8
- r"^(?:\+977|977)?(?:-)?(?:0)?(?:[01][1-9]|2[13-9]|[3-9][0-9])[0-9]{6,7}$"
7
+ r"^(?:\+977|977)?(?:-)?(?:0)?(?:[01][1-9]|2[13-9]|[3-9]\d)\d{6,7}$"
9
8
  )
10
9
 
11
10
 
@@ -1,38 +1,201 @@
1
+ """
2
+ This module contains django templatetags for nepali date and time.
3
+ """
4
+ import datetime
5
+ import warnings
6
+ from typing import Optional, Union
7
+
1
8
  from django import template
2
9
  from django.utils import timezone
3
10
 
11
+ from nepali.datetime import nepalidate as _nepalidate
12
+ from nepali.datetime import nepalidatetime as _nepalidatetime
4
13
  from nepali.datetime import nepalihumanize as humanize
14
+ from nepali.exceptions import InvalidNepaliDateTimeObjectException
5
15
  from nepali.utils import to_nepalidatetime
6
16
 
17
+ _DEFAULT_DATE_FORMAT = "%B %d, %Y, %A"
18
+ DEPRECIATION_WARNING_MESSAGE = (
19
+ "The templatetag 'nepalidatetime' has been depreciated "
20
+ "and will be removed in the future release. "
21
+ "Please use `django-nepali` package."
22
+ )
23
+ _datetime = Union[datetime.date, datetime.datetime, _nepalidate, _nepalidatetime]
7
24
  register = template.Library()
8
25
 
9
26
 
10
27
  @register.filter(name="nepalidate")
11
- def nepalidate(datetime_obj, format="%B %d, %Y, %A"):
12
- nepali_datetime_obj = to_nepalidatetime(datetime_obj)
13
- if nepali_datetime_obj is None:
14
- return None
15
- return nepali_datetime_obj.strftime(format)
28
+ def nepalidate(
29
+ datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT
30
+ ) -> Union[str, None]:
31
+ """
32
+ Renders the datetime object into nepali datetime format in 'en-US' locale (English).
33
+
34
+ Usage:
35
+ ```
36
+ {{ datetime_obj|nepalidate }}
37
+ {{ datetime_obj|nepalidate:"%Y-%m-%d" }}
38
+ ```
39
+
40
+ :param datetime_obj: Datetime object
41
+ :param format: Output format, defaults to "%B %d, %Y, %A"
42
+ :returns: Nepali datetime format in 'en-US' locale
43
+ """
44
+ warnings.warn(
45
+ message=DEPRECIATION_WARNING_MESSAGE,
46
+ category=DeprecationWarning,
47
+ )
48
+ return nepalidate_en(datetime_obj, format=format)
16
49
 
17
50
 
18
51
  @register.filter(name="nepalidate_en")
19
- def nepalidate_en(datetime_obj, format="%B %d, %Y, %A"):
20
- nepali_datetime_obj = to_nepalidatetime(datetime_obj)
21
- if nepali_datetime_obj is None:
52
+ def nepalidate_en(
53
+ datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT
54
+ ) -> Union[str, None]:
55
+ """
56
+ Renders the datetime object into nepali datetime format in 'en-US' locale (English).
57
+
58
+ Usage:
59
+ ```
60
+ {{ datetime_obj|nepalidate_en }}
61
+ {{ datetime_obj|nepalidate_en:"%Y-%m-%d" }}
62
+ ```
63
+
64
+ :param datetime_obj: Datetime object
65
+ :param format: Output format, defaults to "%B %d, %Y, %A"
66
+ :returns: Nepali datetime format in 'en-US' locale
67
+ """
68
+ warnings.warn(
69
+ message=DEPRECIATION_WARNING_MESSAGE,
70
+ category=DeprecationWarning,
71
+ )
72
+ try:
73
+ nepali_datetime_obj = to_nepalidatetime(datetime_obj)
74
+ return nepali_datetime_obj.strftime_en(format)
75
+ except InvalidNepaliDateTimeObjectException:
76
+ return None
77
+
78
+
79
+ @register.filter(name="nepalidate_ne")
80
+ def nepalidate_ne(
81
+ datetime_obj: _datetime, format: str = _DEFAULT_DATE_FORMAT
82
+ ) -> Union[str, None]:
83
+ """
84
+ Renders the datetime object into nepali datetime format in 'ne' locale (Nepali).
85
+
86
+ Usage:
87
+ ```
88
+ {{ datetime_obj|nepalidate_ne }}
89
+ {{ datetime_obj|nepalidate_ne:"%Y-%m-%d" }}
90
+ ```
91
+
92
+ :param datetime_obj: Datetime object
93
+ :param format: Output format, defaults to "%B %d, %Y, %A"
94
+ :returns: Nepali datetime format in 'ne' locale
95
+ """
96
+ warnings.warn(
97
+ message=DEPRECIATION_WARNING_MESSAGE,
98
+ category=DeprecationWarning,
99
+ )
100
+ try:
101
+ nepali_datetime_obj = to_nepalidatetime(datetime_obj)
102
+ return nepali_datetime_obj.strftime_ne(format)
103
+ except InvalidNepaliDateTimeObjectException:
22
104
  return None
23
- return nepali_datetime_obj.strftime_en(format)
24
105
 
25
106
 
26
107
  @register.filter(name="nepalihumanize")
27
- def nepalihumanize(datetime_obj, threshold=None, format=None):
28
- """templatetag to humanize nepalidatetime"""
29
- nepali_datetime_obj = to_nepalidatetime(datetime_obj)
30
- if nepali_datetime_obj is None:
108
+ def nepalihumanize(
109
+ datetime_obj: _datetime,
110
+ threshold: Optional[int] = None,
111
+ format: Optional[str] = None,
112
+ ) -> Union[str, None]:
113
+ """
114
+ Renders the datetime object to a human readable form for 'ne' locale (Nepali).
115
+
116
+ Usage:
117
+ ```
118
+ {{ datetime_obj|nepalihumanize }}
119
+ ```
120
+
121
+ :param datetime_obj: Datetime object
122
+ :param threshold: Threshold in seconds that determines when to render
123
+ the datetime object in the standard datetime format, optional
124
+ :param format: Output format if threshold exceeded, optional
125
+ :returns: Datetime object in human readable form
126
+ """
127
+ warnings.warn(
128
+ message=DEPRECIATION_WARNING_MESSAGE,
129
+ category=DeprecationWarning,
130
+ )
131
+ try:
132
+ nepali_datetime_obj = to_nepalidatetime(datetime_obj)
133
+ return humanize(nepali_datetime_obj, threshold=threshold, format=format)
134
+ except InvalidNepaliDateTimeObjectException:
31
135
  return None
32
- return humanize(nepali_datetime_obj, threshold=threshold, format=format)
33
136
 
34
137
 
35
138
  @register.simple_tag
36
- def nepalinow(format="%B %d, %Y, %A"):
37
- """templatetag to display current datetime in nepali format"""
139
+ def nepalinow(format: str = _DEFAULT_DATE_FORMAT) -> str:
140
+ """
141
+ Renders the current nepali datetime in 'en-US' locale (English).
142
+
143
+ Usage:
144
+ ```
145
+ {% nepalinow %}
146
+ {% nepalinow '%Y-%m-%d' %}
147
+ ```
148
+
149
+ :param datetime_obj: Datetime object
150
+ :param format: Output format, defaults to "%B %d, %Y, %A"
151
+ :returns: Current nepali datetime
152
+ """
153
+ warnings.warn(
154
+ message=DEPRECIATION_WARNING_MESSAGE,
155
+ category=DeprecationWarning,
156
+ )
157
+ return nepalinow_en(format)
158
+
159
+
160
+ @register.simple_tag
161
+ def nepalinow_en(format: str = _DEFAULT_DATE_FORMAT) -> str:
162
+ """
163
+ Renders the current nepali datetime in 'en-US' locale (English).
164
+
165
+ Usage:
166
+ ```
167
+ {% nepalinow_en %}
168
+ {% nepalinow_en '%Y-%m-%d' %}
169
+ ```
170
+
171
+ :param datetime_obj: Datetime object
172
+ :param format: Output format, defaults to "%B %d, %Y, %A"
173
+ :returns: Current nepali datetime
174
+ """
175
+ warnings.warn(
176
+ message=DEPRECIATION_WARNING_MESSAGE,
177
+ category=DeprecationWarning,
178
+ )
38
179
  return to_nepalidatetime(timezone.now()).strftime(format)
180
+
181
+
182
+ @register.simple_tag
183
+ def nepalinow_ne(format: str = _DEFAULT_DATE_FORMAT) -> str:
184
+ """
185
+ Renders the current nepali datetime in 'ne' locale (Nepali).
186
+
187
+ Usage:
188
+ ```
189
+ {% nepalinow_ne %}
190
+ {% nepalinow_ne '%Y-%m-%d' %}
191
+ ```
192
+
193
+ :param datetime_obj: Datetime object
194
+ :param format: Output format, defaults to "%B %d, %Y, %A"
195
+ :returns: Current nepali datetime
196
+ """
197
+ warnings.warn(
198
+ message=DEPRECIATION_WARNING_MESSAGE,
199
+ category=DeprecationWarning,
200
+ )
201
+ return to_nepalidatetime(timezone.now()).strftime_ne(format)
@@ -1,15 +1,103 @@
1
+ """
2
+ This module contains django templatetags for nepali number.
3
+ """
4
+ import warnings
5
+ from typing import Any
6
+
1
7
  from django import template
2
8
 
3
- from nepali.number import nepali_to_english, convert_and_add_comma
9
+ from nepali.number import (
10
+ add_comma,
11
+ add_comma_english,
12
+ convert_and_add_comma,
13
+ english_to_nepali,
14
+ )
4
15
 
5
16
  register = template.Library()
6
17
 
18
+ DEPRECIATION_WARNING_MESSAGE = (
19
+ "The templatetag 'nepalinumber' has been depreciated "
20
+ "and will be removed in the future release. "
21
+ "Please use `django-nepali` package."
22
+ )
23
+
7
24
 
8
25
  @register.filter(name="nepalinumber")
9
- def nepalinumber(value):
10
- return nepali_to_english(value)
26
+ def nepalinumber(value: Any) -> str:
27
+ """
28
+ Converts the number into nepali number and renders it.
29
+
30
+ Usage:
31
+ ```
32
+ {{ number|nepalinumber }}
33
+ ```
34
+
35
+ :param value: Number to be converted
36
+ :returns: Nepali output of given number
37
+ """
38
+ warnings.warn(
39
+ message=DEPRECIATION_WARNING_MESSAGE,
40
+ category=DeprecationWarning,
41
+ )
42
+ return english_to_nepali(value)
11
43
 
12
44
 
13
45
  @register.filter(name="nepalinumber_with_comma")
14
- def nepalinumber_with_comma(value):
46
+ def nepalinumber_with_comma(value: Any) -> str:
47
+ """
48
+ Converts the number into nepali number and renders it.
49
+
50
+ Usage:
51
+ ```
52
+ {{ number|nepalinumber_with_comma }}
53
+ ```
54
+ Basically same as `{{ number|nepalinumber|nepali_comma }}`
55
+
56
+ :param value: Number to be converted and commas added
57
+ :returns: Nepali output of given number with commas
58
+ """
59
+ warnings.warn(
60
+ message=DEPRECIATION_WARNING_MESSAGE,
61
+ category=DeprecationWarning,
62
+ )
15
63
  return convert_and_add_comma(value)
64
+
65
+
66
+ @register.filter(name="nepali_comma")
67
+ def nepali_comma(value: Any) -> str:
68
+ """
69
+ Renders the given value with commas added in Nepali style without converting the number.
70
+
71
+ Usage:
72
+ ```
73
+ {{ number|nepali_comma }}
74
+ ```
75
+
76
+ :param value: Number to be added with commas
77
+ :returns: Output of given number with commas
78
+ """
79
+ warnings.warn(
80
+ message=DEPRECIATION_WARNING_MESSAGE,
81
+ category=DeprecationWarning,
82
+ )
83
+ return add_comma(value)
84
+
85
+
86
+ @register.filter(name="english_comma")
87
+ def english_comma(value: Any) -> str:
88
+ """
89
+ Renders the given value with commas added in English style without converting the number.
90
+
91
+ Usage:
92
+ ```
93
+ {{ number|english_comma }}
94
+ ```
95
+
96
+ :param value: Number to be added with commas
97
+ :returns: Output of given number with commas
98
+ """
99
+ warnings.warn(
100
+ message=DEPRECIATION_WARNING_MESSAGE,
101
+ category=DeprecationWarning,
102
+ )
103
+ return add_comma_english(value)
@@ -1,8 +1,8 @@
1
1
  import datetime
2
2
  import unittest
3
3
 
4
- from nepali.datetime import nepalidate, nepalitime, nepalidatetime
5
- from nepali.datetime.utils import to_nepalidatetime, to_nepalidate
4
+ from nepali.datetime import nepalidate, nepalidatetime, nepalitime
5
+ from nepali.datetime.utils import to_nepalidate, to_nepalidatetime
6
6
  from nepali.exceptions import InvalidNepaliDateTimeObjectException
7
7
  from nepali.timezone import NepaliTimeZone
8
8
 
@@ -250,9 +250,7 @@ class TestNepaliDateTime(unittest.TestCase):
250
250
  self.assertEqual(
251
251
  nepalidatetime_obj1,
252
252
  nepalidatetime_obj2,
253
- msg="{} and {} are not equal".format(
254
- nepalidatetime_obj1, nepalidatetime_obj2
255
- ),
253
+ msg=f"{nepalidatetime_obj1} and {nepalidatetime_obj2} are not equal",
256
254
  )
257
255
 
258
256
 
@@ -2,11 +2,10 @@ import datetime
2
2
  import unittest
3
3
  from unittest.mock import patch
4
4
 
5
- from nepali.datetime import nepalihumanize, HumanizeDateTime, nepalidate, nepalidatetime
5
+ from nepali.datetime import HumanizeDateTime, nepalidate, nepalidatetime, nepalihumanize
6
6
  from nepali.exceptions import InvalidNepaliDateTimeObjectException
7
7
  from nepali.timezone import NepaliTimeZone
8
8
 
9
-
10
9
  REF_TIME = datetime.datetime(2015, 1, 1, 10, 15, tzinfo=NepaliTimeZone())
11
10
 
12
11
 
@@ -2,17 +2,17 @@ import unittest
2
2
 
3
3
  from nepali import locations
4
4
  from nepali.locations.models import (
5
+ District,
5
6
  Location,
7
+ Municipality,
6
8
  MunicipalityType,
7
9
  Province,
8
- District,
9
- Municipality,
10
10
  )
11
11
  from nepali.locations.utils import (
12
12
  _filter_location,
13
- get_province,
14
13
  get_district,
15
14
  get_municipality,
15
+ get_province,
16
16
  )
17
17
 
18
18