add .gitignore. remove not used function. update README. add support for python 2.7
This commit is contained in:
parent
6240116f33
commit
61bd2502fc
61
.gitignore
vendored
Normal file
61
.gitignore
vendored
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# Byte-compiled / optimized / DLL files
|
||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
#IDEA
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# C extensions
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Distribution / packaging
|
||||||
|
.Python
|
||||||
|
env/
|
||||||
|
build/
|
||||||
|
develop-eggs/
|
||||||
|
dist/
|
||||||
|
downloads/
|
||||||
|
eggs/
|
||||||
|
lib/
|
||||||
|
lib64/
|
||||||
|
parts/
|
||||||
|
sdist/
|
||||||
|
var/
|
||||||
|
*.egg-info/
|
||||||
|
.installed.cfg
|
||||||
|
*.egg
|
||||||
|
|
||||||
|
# PyInstaller
|
||||||
|
# Usually these files are written by a python script from a template
|
||||||
|
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||||
|
*.manifest
|
||||||
|
*.spec
|
||||||
|
|
||||||
|
# Installer logs
|
||||||
|
pip-log.txt
|
||||||
|
pip-delete-this-directory.txt
|
||||||
|
|
||||||
|
# Unit test / coverage reports
|
||||||
|
htmlcov/
|
||||||
|
.tox/
|
||||||
|
.coverage
|
||||||
|
.cache
|
||||||
|
nosetests.xml
|
||||||
|
coverage.xml
|
||||||
|
|
||||||
|
# Translations
|
||||||
|
*.mo
|
||||||
|
*.pot
|
||||||
|
|
||||||
|
# Django stuff:
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Sphinx documentation
|
||||||
|
docs/_build/
|
||||||
|
|
||||||
|
# PyBuilder
|
||||||
|
target/
|
||||||
|
|
||||||
|
# Virtualenv
|
||||||
|
env/
|
44
README.md
44
README.md
@ -1,37 +1,33 @@
|
|||||||
# sms-counter-python
|
# sms-counter-python
|
||||||
SMS Character Counter
|
----
|
||||||
=============================
|
|
||||||
|
|
||||||
Character counter for SMS messages.
|
**sms-counter-python** is a lib that help to count characters of SMS messages.
|
||||||
|
|
||||||
##Usage
|
#### Get it now
|
||||||
|
```
|
||||||
|
$ pip install git+https://github.com/dedayoa/sms-counter-python.git#egg=sms_counter
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Support
|
||||||
|
* Python 2
|
||||||
|
* Python 3
|
||||||
|
|
||||||
|
#### Requirements
|
||||||
|
sms-counter-python has no external dependencies outside of the Python standard library
|
||||||
|
|
||||||
|
#### Usage
|
||||||
```python
|
```python
|
||||||
from sms_counter import SMSCounter
|
from sms_counter import SMSCounter
|
||||||
|
|
||||||
smsmsg = SMSCounter();
|
>>> counter = SMSCounter.count('ǂ some-string-to-be-counted ');
|
||||||
smsmsg.count('ǂ some-string-to-be-counted ');
|
>>> counter
|
||||||
|
>>> {'length': 29, 'messages': 1, 'remaining': 41, 'per_message': 70, 'encoding': 'UTF16'}
|
||||||
```
|
```
|
||||||
|
|
||||||
returns
|
#### Mentions
|
||||||
```
|
|
||||||
<class 'dict'>
|
|
||||||
{
|
|
||||||
'length': 28,
|
|
||||||
'per_message': 70,
|
|
||||||
'remaining': 42,
|
|
||||||
'messages': 1,
|
|
||||||
'encoding': 'UTF16'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
###Mentions
|
|
||||||
|
|
||||||
* Original idea : [danxexe/sms-counter](https://github.com/danxexe/sms-counter)
|
* Original idea : [danxexe/sms-counter](https://github.com/danxexe/sms-counter)
|
||||||
* Next Original : [wobblecode/sms-counter-php](https://github.com/wobblecode/sms-counter-php/)
|
* Next Original : [wobblecode/sms-counter-php](https://github.com/wobblecode/sms-counter-php/)
|
||||||
|
|
||||||
## License
|
#### License
|
||||||
|
MIT licensed. See the bundled [LICENSE](LICENSE) file for more details.
|
||||||
sms-counter-python is released under the [MIT License](LICENSE.txt).
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ Created on Jul 10, 2016
|
|||||||
from math import ceil
|
from math import ceil
|
||||||
|
|
||||||
|
|
||||||
class SMSCounter():
|
class SMSCounter(object):
|
||||||
GSM_7BIT = 'GSM_7BIT'
|
GSM_7BIT = 'GSM_7BIT'
|
||||||
GSM_7BIT_EX = 'GSM_7BIT_EX'
|
GSM_7BIT_EX = 'GSM_7BIT_EX'
|
||||||
UTF16 = 'UTF16'
|
UTF16 = 'UTF16'
|
||||||
@ -15,7 +15,8 @@ class SMSCounter():
|
|||||||
GSM_7BIT_LEN_MULTIPART = GSM_7BIT_EX_LEN_MULTIPART = 153
|
GSM_7BIT_LEN_MULTIPART = GSM_7BIT_EX_LEN_MULTIPART = 153
|
||||||
UTF16_LEN_MULTIPART = 67
|
UTF16_LEN_MULTIPART = 67
|
||||||
|
|
||||||
def get_gsm_7bit_map(self):
|
@classmethod
|
||||||
|
def _get_gsm_7bit_map(cls):
|
||||||
gsm_7bit_map = [
|
gsm_7bit_map = [
|
||||||
10, 13, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
|
10, 13, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
|
||||||
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
|
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
|
||||||
@ -28,69 +29,63 @@ class SMSCounter():
|
|||||||
920, 923, 926, 928, 931, 934, 936, 937]
|
920, 923, 926, 928, 931, 934, 936, 937]
|
||||||
return gsm_7bit_map
|
return gsm_7bit_map
|
||||||
|
|
||||||
def get_added_gsm_7bit_ex_map(self):
|
@classmethod
|
||||||
|
def _get_added_gsm_7bit_ex_map(cls):
|
||||||
added_gsm_7bit_ex_map = [91, 92, 93, 94, 123, 124, 125, 126, 8364]
|
added_gsm_7bit_ex_map = [91, 92, 93, 94, 123, 124, 125, 126, 8364]
|
||||||
return added_gsm_7bit_ex_map
|
return added_gsm_7bit_ex_map
|
||||||
|
|
||||||
def get_gsm_7bit_ex_map(self):
|
@classmethod
|
||||||
gsm_7bit_ex_map = (
|
def _text_to_unicode_pointcode_list(cls, plaintext):
|
||||||
self.get_added_gsm_7bit_ex_map() + self.get_gsm_7bit_map())
|
|
||||||
return gsm_7bit_ex_map
|
|
||||||
|
|
||||||
def text_to_unicode_pointcode_list(self, plaintext):
|
|
||||||
textlist = []
|
textlist = []
|
||||||
for stg in plaintext:
|
for stg in plaintext:
|
||||||
textlist.append(ord(stg))
|
textlist.append(ord(stg))
|
||||||
return textlist
|
return textlist
|
||||||
|
|
||||||
def unicode_pointcode_list_to_text(self, strlist):
|
@classmethod
|
||||||
text = ''
|
def _detect_encoding(cls, plaintext):
|
||||||
for stc in strlist:
|
rf = cls._text_to_unicode_pointcode_list(plaintext)
|
||||||
text.join(chr(stc))
|
|
||||||
return text
|
|
||||||
|
|
||||||
def detect_encoding(self, plaintext):
|
utf16chars = set(rf).difference(set(cls._get_gsm_7bit_map()))
|
||||||
rf = self.text_to_unicode_pointcode_list(plaintext)
|
|
||||||
|
|
||||||
utf16chars = set(rf).difference(set(self.get_gsm_7bit_map()))
|
|
||||||
|
|
||||||
if len(utf16chars):
|
if len(utf16chars):
|
||||||
return self.UTF16
|
return cls.UTF16
|
||||||
|
|
||||||
exchars = set(rf).intersection(set(self.get_added_gsm_7bit_ex_map()))
|
exchars = set(rf).intersection(set(cls._get_added_gsm_7bit_ex_map()))
|
||||||
|
|
||||||
if len(exchars):
|
if len(exchars):
|
||||||
return self.GSM_7BIT_EX
|
return cls.GSM_7BIT_EX
|
||||||
|
|
||||||
return self.GSM_7BIT
|
return cls.GSM_7BIT
|
||||||
|
|
||||||
def count(self, plaintext):
|
@classmethod
|
||||||
textlist = self.text_to_unicode_pointcode_list(plaintext)
|
def count(cls, plaintext):
|
||||||
|
textlist = cls._text_to_unicode_pointcode_list(plaintext)
|
||||||
|
|
||||||
exchars = []
|
exchars = []
|
||||||
encoding = self.detect_encoding(plaintext)
|
encoding = cls._detect_encoding(plaintext)
|
||||||
length = len(textlist)
|
length = len(textlist)
|
||||||
|
|
||||||
if encoding == self.GSM_7BIT_EX:
|
if encoding == cls.GSM_7BIT_EX:
|
||||||
lengthexchars = len(exchars)
|
lengthexchars = len(exchars)
|
||||||
length += lengthexchars
|
length += lengthexchars
|
||||||
|
|
||||||
if encoding == self.GSM_7BIT:
|
if encoding == cls.GSM_7BIT:
|
||||||
permessage = self.GSM_7BIT_LEN
|
permessage = cls.GSM_7BIT_LEN
|
||||||
if length > self.GSM_7BIT_LEN:
|
if length > cls.GSM_7BIT_LEN:
|
||||||
permessage = self.GSM_7BIT_LEN_MULTIPART
|
permessage = cls.GSM_7BIT_LEN_MULTIPART
|
||||||
|
elif encoding == cls.GSM_7BIT_EX:
|
||||||
elif encoding == self.GSM_7BIT_EX:
|
permessage = cls.GSM_7BIT_EX_LEN
|
||||||
permessage = self.GSM_7BIT_EX_LEN
|
if length > cls.GSM_7BIT_EX_LEN:
|
||||||
if length > self.GSM_7BIT_EX_LEN:
|
permessage = cls.GSM_7BIT_EX_LEN_MULTIPART
|
||||||
permessage = self.GSM_7BIT_EX_LEN_MULTIPART
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
permessage = self.UTF16_LEN
|
permessage = cls.UTF16_LEN
|
||||||
if length > self.UTF16_LEN:
|
if length > cls.UTF16_LEN:
|
||||||
permessage = self.UTF16_LEN_MULTIPART
|
permessage = cls.UTF16_LEN_MULTIPART
|
||||||
|
|
||||||
messages = ceil(length / permessage)
|
# Convert the dividend to fload so the division will be a float number
|
||||||
|
# and then convert the ceil result to int
|
||||||
|
# since python 2.7 return a float
|
||||||
|
messages = int(ceil(length / float(permessage)))
|
||||||
remaining = (permessage * messages) - length
|
remaining = (permessage * messages) - length
|
||||||
|
|
||||||
returnset = {
|
returnset = {
|
||||||
@ -103,27 +98,28 @@ class SMSCounter():
|
|||||||
|
|
||||||
return returnset
|
return returnset
|
||||||
|
|
||||||
def truncate(self, plaintext, limitsms):
|
@classmethod
|
||||||
count = self.count(plaintext)
|
def truncate(cls, plaintext, limitsms):
|
||||||
|
count = cls.count(plaintext)
|
||||||
|
|
||||||
if count.mesages <= limitsms:
|
if count.mesages <= limitsms:
|
||||||
return plaintext
|
return plaintext
|
||||||
|
|
||||||
if count.encoding == 'UTF16':
|
if count.encoding == 'UTF16':
|
||||||
limit = self.UTF16_LEN
|
limit = cls.UTF16_LEN
|
||||||
|
|
||||||
if limitsms > 2:
|
if limitsms > 2:
|
||||||
limit = self.UTF16_LEN_MULTIPART
|
limit = cls.UTF16_LEN_MULTIPART
|
||||||
|
|
||||||
if count.encoding != 'UTF16':
|
if count.encoding != 'UTF16':
|
||||||
limit = self.GSM_7BIT_LEN
|
limit = cls.GSM_7BIT_LEN
|
||||||
|
|
||||||
if limitsms > 2:
|
if limitsms > 2:
|
||||||
limit = self.GSM_7BIT_LEN_MULTIPART
|
limit = cls.GSM_7BIT_LEN_MULTIPART
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
text = plaintext[0:limit * limitsms]
|
text = plaintext[0:limit * limitsms]
|
||||||
count = self.count(plaintext)
|
count = cls.count(plaintext)
|
||||||
|
|
||||||
limit = limit - 1
|
limit = limit - 1
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user