Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Add some other Persian tools #7

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 34 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Digit/Character converter:

.. code:: python

>>> from persiantools import characters, digits
>>> from persiantools import characters, digits, utils

>>> digits.en_to_fa("0987654321")
'۰۹۸۷۶۵۴۳۲۱'
Expand All @@ -101,10 +101,41 @@ Digit/Character converter:

>>> digits.fa_to_en("۰۹۸۷۶۵۴۳۲۱")
'0987654321'


>>> digits.ar_to_en("٠٩٨٧٦٥٤٣٢١")
'0987654321'

>>> digits.fa_to_ar("۰۹۸۷۶۵۴۳۲۱")
'٠٩٨٧٦٥٤٣٢١'


>>> digits.en_to_ar("0987654321")
'٠٩٨٧٦٥٤٣٢١'


>>> digits.to_fa("0987٦٥٤3۲۱")
'۰۹۸۷۶۵۴۳۲۱'

>>> digits.to_en("0987٦٥٤3۲۱")
'0987654321'

>>> digits.to_ar("0987٦٥٤3۲۱")
'٠٩٨٧٦٥٤٣٢١'

>>> utils.is_valid_national_id('1234567890')
False

>>> utils.generate_random_national_id()
'3934540414'

>>> utils.clean_mobile_number('+989366926847')
'09366926847'

>>> utils.clean_mobile_number('9366926847', add_98=True, add_plus=True)
'+989366926847'

>>> utils.is_valid_mobile_number('989366926847')
True

>>> characters.ar_to_fa("راك")
'راک'

Expand Down
185 changes: 123 additions & 62 deletions persiantools/digits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
# -*- coding: utf-8 -*-
from persiantools import utils

EN_FA_MAP = {
'0': '۰',
'1': '۱',
'2': '۲',
'3': '۳',
'4': '۴',
'5': '۵',
'6': '۶',
'7': '۷',
'8': '۸',
'9': '۹'
}

AR_FA_MAP = {
'٠': '۰',
'١': '۱',
'٢': '۲',
'٣': '۳',
'٤': '۴',
'٥': '۵',
'٦': '۶',
'٧': '۷',
'٨': '۸',
'٩': '۹'
}

AR_EN_MAP = {
'٠': '0',
'١': '1',
'٢': '2',
'٣': '3',
'٤': '4',
'٥': '5',
'٦': '6',
'٧': '7',
'٨': '8',
'٩': '9'
}


def en_to_fa(string):
"""Convert EN digits to Persian
Expand All @@ -12,72 +51,37 @@ def en_to_fa(string):
:param string: A string, will be converted
:rtype: str
"""
dic = {
'0': '۰',
'1': '۱',
'2': '۲',
'3': '۳',
'4': '۴',
'5': '۵',
'6': '۶',
'7': '۷',
'8': '۸',
'9': '۹'
}

return utils.replace(string, dic)

return utils.replace(string, EN_FA_MAP)

def ar_to_fa(string):
"""Convert Arabic digits to Persian

def fa_to_en(string):
"""Convert Persian digits to EN

Usage::
>>> from persiantools import digits
>>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩")
>>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹")

:param string: A string, will be converted
:rtype: str
"""
dic = {
'٠': '۰',
'١': '۱',
'٢': '۲',
'٣': '۳',
'٤': '۴',
'٥': '۵',
'٦': '۶',
'٧': '۷',
'٨': '۸',
'٩': '۹'
}

return utils.replace(string, dic)
fa_to_en_map = dict((x, y) for (y, x) in EN_FA_MAP.items())

return utils.replace(string, fa_to_en_map)

def fa_to_en(string):
"""Convert Persian digits to EN

def ar_to_fa(string):
"""Convert Arabic digits to Persian

Usage::
>>> from persiantools import digits
>>> converted = digits.fa_to_en("۰۱۲۳۴۵۶۷۸۹")
>>> converted = digits.ar_to_fa("٠١٢٣٤٥٦٧٨٩")

:param string: A string, will be converted
:rtype: str
"""
dic = {
'۰': '0',
'۱': '1',
'۲': '2',
'۳': '3',
'۴': '4',
'۵': '5',
'۶': '6',
'۷': '7',
'۸': '8',
'۹': '9'
}

return utils.replace(string, dic)

return utils.replace(string, AR_FA_MAP)


def fa_to_ar(string):
Expand All @@ -90,17 +94,74 @@ def fa_to_ar(string):
:param string: A string, will be converted
:rtype: str
"""
dic = {
'۰': '٠',
'۱': '١',
'۲': '٢',
'۳': '٣',
'۴': '٤',
'۵': '٥',
'۶': '٦',
'۷': '٧',
'۸': '٨',
'۹': '٩'
}

return utils.replace(string, dic)
fa_to_ar_map = dict((x, y) for (y, x) in AR_FA_MAP.items())

return utils.replace(string, fa_to_ar_map)


def ar_to_en(string):
"""Convert Arabic digits to EN

Usage::
>>> from persiantools import digits
>>> converted = digits.ar_to_en("٠١٢٣٤٥٦٧٨٩")

:param string: A string, will be converted
:rtype: str
"""

return utils.replace(string, AR_EN_MAP)


def en_to_ar(string):
"""Convert EN digits to Arabic

Usage::
>>> from persiantools import digits
>>> converted = digits.en_to_ar("0123456789")

:param string: A string, will be converted
:rtype: str
"""
en_to_ar_map = dict((x, y) for (y, x) in AR_EN_MAP.items())

return utils.replace(string, en_to_ar_map)


def to_fa(string):
"""Convert digits to Persian

Usage::
>>> from persiantools import digits
>>> converted = digits.to_fa("0123456789٠١٢٣٤٥٦٧٨٩")

:param string: A string, will be converted
:rtype: str
"""
return en_to_fa(ar_to_en(string))


def to_en(string):
"""Convert digits to EN

Usage::
>>> from persiantools import digits
>>> converted = digits.to_en("۰۱۲۳۴۵۶۷۸۹٠١٢٣٤٥٦٧٨٩")

:param string: A string, will be converted
:rtype: str
"""
return fa_to_en(ar_to_fa(string))


def to_ar(string):
"""Convert digits to Arabic

Usage::
>>> from persiantools import digits
>>> converted = digits.to_en("۰۱۲۳۴۵۶۷۸۹0123456789")

:param string: A string, will be converted
:rtype: str
"""
return fa_to_ar(en_to_fa(string))
101 changes: 101 additions & 0 deletions persiantools/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import random
import re
import string


def replace(string, dictionary):
Expand Down Expand Up @@ -30,3 +32,102 @@ def check_int_field(value):
'an integer is required (got type %s)' % type(value).__name__)

raise TypeError('integer argument expected, got float')


def is_valid_national_id(number):
"""Check whether number is a valid national id or not

Usage::
>>> from persiantools import utils
>>> is_valid = utils.is_valid_national_id("3934540414")

:param number: A number, will be checked for validation
:rtype: str
"""

if number is None or number == '':
return False

pattern = re.compile('^\d{10}$')

if not pattern.match(number):
return False

n = 0
for i in range(9):
n += (10 - i) * int(number[i])

r = n % 11

if r > 1:
r = 11 - r

no = number[0:9] + str(r)

return no == number


def generate_random_national_id():
"""Generate random Iranian national ID

Usage::
>>> from persiantools import utils
>>> national_id = utils.generate_random_national_id()

"""

national_id = ''.join(random.choice(string.digits) for _ in range(9))
n = 0
for i in range(9):
n += (10 - i) * int(national_id[i])

r = n % 11
if r > 1:
r = 11 - r

national_id += str(r)

return national_id


def clean_mobile_number(number, add_98=False, add_plus=False):
"""Clean mobile number

Usage::
>>> from persiantools import utils
>>> cleaned_number = utils.clean_mobile_number('+989366926847')

:param number: A string, will be cleaned
:param add_98: Add country code prefix to the final number
:param add_plus: Add + prefix to the final number
:return: str
"""
number = re.sub('^\+?98', '0', number)
number = re.sub('^9', '09', number)

if add_98:
number = re.sub('^0', '98', number)
if add_plus:
number = re.sub('^9', '+9', number)

return number


def is_valid_mobile_number(number):
"""Check whether the number is valid mobile number or not

Usage::
>>> from persiantools import utils
>>> is_valid = utils.is_valid_mobile_number('093432212121212')

:param number: A mobile number, will be checked for validation
:return: boolean
"""
if not number:
return False

pattern = re.compile('^(\+?98|0?)9[0-3|9]\d{8}$')
if not pattern.match(number):
return False

return True
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
nose<=2.0.0
coverage<=5.0.0
pytz<=2018