create python package. refactor the code style according to flake8

This commit is contained in:
Eliel Haouzi 2017-02-15 12:02:52 +02:00
parent 79373c49c9
commit 6240116f33
3 changed files with 85 additions and 74 deletions

26
setup.py Normal file
View File

@ -0,0 +1,26 @@
"""
See:
https://packaging.python.org/en/latest/distributing.html
"""
import os
from setuptools import setup
# To use a consistent encoding
from codecs import open
here = os.path.abspath(os.path.dirname(__file__))
# Get the long description from the README file
with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
long_description = f.read()
setup(
name='sms-counter',
version='1.0.0',
description='SMS Character Counter',
long_description=long_description,
author='Dayo Ayeni',
# author_email='',
packages=['sms_counter'],
install_requires=[""],
include_package_data=True)

5
sms_counter/__init__.py Normal file
View File

@ -0,0 +1,5 @@
#!/usr/bin/env python
from __future__ import absolute_import
"""SMSCounter Module."""
from .main import SMSCounter

View File

@ -1,14 +1,12 @@
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
''' '''
Created on Jul 10, 2016 Created on Jul 10, 2016
@author: Dayo @author: Dayo
''' '''
from math import ceil from math import ceil
class SMSCounter(): class SMSCounter():
GSM_7BIT = 'GSM_7BIT' GSM_7BIT = 'GSM_7BIT'
GSM_7BIT_EX = 'GSM_7BIT_EX' GSM_7BIT_EX = 'GSM_7BIT_EX'
UTF16 = 'UTF16' UTF16 = 'UTF16'
@ -17,137 +15,119 @@ 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): def get_gsm_7bit_map(self):
gsm_7bit_map = [
return( 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, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 92, 95, 97, 98, 99, 100,
92,95,97,98,99,100,101,102,103,104,105,106,107,108,109,110, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
111,112,113,114,115,116,117,118,119,120,121,122, 114, 115, 116, 117, 118, 119, 120, 121, 122, 161, 163, 164, 165,
161,163,164,165,191,196,197,198,199,201,209,214, 191, 196, 197, 198, 199, 201, 209, 214, 216, 220, 223, 224, 228,
216,220,223,224,228,229,230,232,233,236,241,242, 229, 230, 232, 233, 236, 241, 242, 246, 248, 249, 252, 915, 916,
246,248,249,252,915,916,920,923,926,928,931,934, 920, 923, 926, 928, 931, 934, 936, 937]
936,937] return gsm_7bit_map
)
def get_added_gsm_7bit_ex_map(self): def get_added_gsm_7bit_ex_map(self):
added_gsm_7bit_ex_map = [91, 92, 93, 94, 123, 124, 125, 126, 8364]
return [91,92,93,94,123,124,125,126,8364] return added_gsm_7bit_ex_map
def get_gsm_7bit_ex_map(self): def get_gsm_7bit_ex_map(self):
gsm_7bit_ex_map = (
return(self.get_added_gsm_7bit_ex_map()+self.get_gsm_7bit_map()) 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): 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): def unicode_pointcode_list_to_text(self, strlist):
text = '' text = ''
for stc in strlist: for stc in strlist:
text.join(chr(stc)) text.join(chr(stc))
return text return text
def detect_encoding(self, plaintext): def detect_encoding(self, plaintext):
rf = self.text_to_unicode_pointcode_list(plaintext) rf = self.text_to_unicode_pointcode_list(plaintext)
utf16chars = set(rf).difference(set(self.get_gsm_7bit_map())) utf16chars = set(rf).difference(set(self.get_gsm_7bit_map()))
if len(utf16chars): if len(utf16chars):
return self.UTF16 return self.UTF16
exchars = set(rf).intersection(set(self.get_added_gsm_7bit_ex_map())) exchars = set(rf).intersection(set(self.get_added_gsm_7bit_ex_map()))
if len(exchars): if len(exchars):
return self.GSM_7BIT_EX return self.GSM_7BIT_EX
return self.GSM_7BIT return self.GSM_7BIT
def count(self, plaintext): def count(self, plaintext):
textlist = self.text_to_unicode_pointcode_list(plaintext) textlist = self.text_to_unicode_pointcode_list(plaintext)
exchars = [] exchars = []
encoding = self.detect_encoding(plaintext) encoding = self.detect_encoding(plaintext)
length = len(textlist) length = len(textlist)
if encoding == self.GSM_7BIT_EX: if encoding == self.GSM_7BIT_EX:
lengthexchars = len(exchars) lengthexchars = len(exchars)
length+=lengthexchars length += lengthexchars
if encoding == self.GSM_7BIT: if encoding == self.GSM_7BIT:
permessage = self.GSM_7BIT_LEN permessage = self.GSM_7BIT_LEN
if length > self.GSM_7BIT_LEN: if length > self.GSM_7BIT_LEN:
permessage = self.GSM_7BIT_LEN_MULTIPART permessage = self.GSM_7BIT_LEN_MULTIPART
elif encoding == self.GSM_7BIT_EX: elif encoding == self.GSM_7BIT_EX:
permessage = self.GSM_7BIT_EX_LEN permessage = self.GSM_7BIT_EX_LEN
if length > self.GSM_7BIT_EX_LEN: if length > self.GSM_7BIT_EX_LEN:
permessage = self.GSM_7BIT_EX_LEN_MULTIPART permessage = self.GSM_7BIT_EX_LEN_MULTIPART
else: else:
permessage = self.UTF16_LEN permessage = self.UTF16_LEN
if length > self.UTF16_LEN: if length > self.UTF16_LEN:
permessage = self.UTF16_LEN_MULTIPART permessage = self.UTF16_LEN_MULTIPART
messages = ceil(length / permessage)
messages = ceil(length/permessage)
remaining = (permessage * messages) - length remaining = (permessage * messages) - length
returnset = { returnset = {
'encoding' : encoding, 'encoding': encoding,
'length' : length, 'length': length,
'per_message' : permessage, 'per_message': permessage,
'remaining' : remaining, 'remaining': remaining,
'messages' : messages 'messages': messages
} }
return returnset return returnset
def truncate(self, plaintext, limitsms): def truncate(self, plaintext, limitsms):
count = self.count(plaintext) count = self.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 = self.UTF16_LEN
if limitsms > 2: if limitsms > 2:
limit = self.UTF16_LEN_MULTIPART limit = self.UTF16_LEN_MULTIPART
if count.encoding != 'UTF16': if count.encoding != 'UTF16':
limit = self.GSM_7BIT_LEN limit = self.GSM_7BIT_LEN
if limitsms > 2: if limitsms > 2:
limit = self.GSM_7BIT_LEN_MULTIPART limit = self.GSM_7BIT_LEN_MULTIPART
while True: while True:
text = plaintext[0:limit*limitsms] text = plaintext[0:limit * limitsms]
count = self.count(plaintext) count = self.count(plaintext)
limit = limit - 1 limit = limit - 1
if count.messages < limitsms: if count.messages < limitsms:
break break
return text return text