Skip to content

Commit

Permalink
Add Python 3 support
Browse files Browse the repository at this point in the history
1. Fix typo
2. Fix shadows name
3. Add Python 3 support
  • Loading branch information
neoblackcap committed Jul 18, 2016
1 parent cb929ef commit 4161a23
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions phone/phone.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#coding:utf-8
# coding:utf-8

import os
import struct
import sys

__author__ = 'lovedboy'


class Phone(object):

def __init__(self, dat_file=None):

if dat_file is None:
Expand All @@ -20,19 +20,21 @@ def __init__(self, dat_file=None):
self.phone_fmt = "<iiB"
self.head_fmt_length = struct.calcsize(self.head_fmt)
self.phone_fmt_length = struct.calcsize(self.phone_fmt)
self.version, self.first_phone_record_offset = struct.unpack(self.head_fmt, self.buf[:self.head_fmt_length])
self.phone_record_count = (len(self.buf) - self.first_phone_record_offset) / self.phone_fmt_length
self.version, self.first_phone_record_offset = struct.unpack(
self.head_fmt, self.buf[:self.head_fmt_length])
self.phone_record_count = (len(
self.buf) - self.first_phone_record_offset) / self.phone_fmt_length

def get_phone_dat_msg(self):
print "版本号:{}".format(self.version)
print "总记录条数:{}".format(self.phone_record_count)
print("版本号:{}".format(self.version))
print("总记录条数:{}".format(self.phone_record_count))

@staticmethod
def get_phone_no_type(no):
if no == 4:
return "电信虚拟运营商"
return "电信虚拟运营商"
if no == 5:
return "联通虚拟运营商"
return "联通虚拟运营商"
if no == 6:
return "移动虚拟运营商"
if no == 3:
Expand All @@ -43,33 +45,35 @@ def get_phone_no_type(no):
return "移动"

@staticmethod
def _format_phone_content(phone, record_conent, phone_type):
def _format_phone_content(phone_num, record_content, phone_type):

province, city, zip_code, area_code = record_conent.split('|')
province, city, zip_code, area_code = record_content.split('|')
return {
"phone": phone,
"phone": phone_num,
"province": province,
"city": city,
"zip_code": zip_code,
"area_code": area_code,
"phone_type": Phone.get_phone_no_type(phone_type)
}

def _lookup_phone(self, phone):
def _lookup_phone(self, phone_num):

phone = str(phone)
assert 7 <= len(phone) <= 11
int_phone = int(str(phone)[0:7])
phone_num = str(phone_num)
assert 7 <= len(phone_num) <= 11
int_phone = int(str(phone_num)[0:7])

left = 0
right = self.phone_record_count
while left <= right:
middle = (left + right) / 2
current_offset = self.first_phone_record_offset + middle * self.phone_fmt_length
current_offset = int(self.first_phone_record_offset + middle * self.phone_fmt_length)
if current_offset >= len(self.buf):
return

buffer = self.buf[current_offset: current_offset + self.phone_fmt_length]
cur_phone, record_offset, phone_type = struct.unpack(self.phone_fmt,
self.buf[current_offset: current_offset + self.phone_fmt_length])
buffer)

if cur_phone > int_phone:
right = middle - 1
Expand All @@ -79,29 +83,35 @@ def _lookup_phone(self, phone):
s = record_offset
e = record_offset + self.buf[record_offset:].find('\0')
record_content = self.buf[s: e]
return Phone._format_phone_content(phone, record_content, phone_type)
return Phone._format_phone_content(phone_num, record_content,
phone_type)

def find(self, phone):
return self._lookup_phone(phone)
def find(self, phone_num):
return self._lookup_phone(phone_num)

@staticmethod
def human_phone_info(phone_info):

if not phone_info:
return ''

return "{}|{}|{}|{}|{}|{}".format(phone_info['phone'],phone_info['province'], phone_info['city'],
phone_info['zip_code'], phone_info['area_code'],phone_info['phone_type'])
return "{}|{}|{}|{}|{}|{}".format(phone_info['phone'],
phone_info['province'],
phone_info['city'],
phone_info['zip_code'],
phone_info['area_code'],
phone_info['phone_type'])

def test(self):
self.get_phone_dat_msg()

for i in xrange(1529900, 1529999):
print self.human_phone_info(self.find(i))
if sys.version_info.major == 2:
for i in xrange(1529900, 1529999):
print(self.human_phone_info(self.find(i)))
else:
for i in range(1529900, 1529999):
print(self.human_phone_info(self.find(i)))

if __name__ == "__main__":

if __name__ == "__main__":
phone = Phone()
phone.test()


0 comments on commit 4161a23

Please sign in to comment.