<commit_before># -*- coding: utf-8 -*-
''' TWSE stock no. '''
# Copyright (c) 2012, 2013, 2014 <NAME>, http://toomore.net/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import csv
import os
import re

class ImportCSV(object):
    """ Import CSV """
    def __init__(self):
        self.last_update = ''

    def importcsv(self):
        ''' import data from csv '''
        csv_path = os.path.join(os.path.dirname(__file__), 'stock_no.csv')
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            for i in csv_data:
                try:
                    result[i[0]] = str(i[1]).decode('utf-8')
                except ValueError:
                    if i[0] == 'UPDATE':
                        self.last_update = str(i[1]).decode('utf-8')
                    else:
                        pass
        return result

    @staticmethod
    def __industry_code():
        ''' import industry_code '''
        csv_path = os.path.join(os.path.dirname(__file__), 'industry_code.csv')
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            for i in csv_data:
                result[i[0]] = i[1].decode('utf-8')
            return result

    @staticmethod
    def __loadindcomps():
        ''' import industry comps '''
        csv_path = os.path.join(os.path.dirname(__file__), 'stock_no.csv')
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            check_words = re.compile(r'^[\d]{2,}[\w]?')
            for i in csv_data:
                if check_words.match(i[2]):
                    try:
                        result[i[2]].append(i[0].decode('utf-8'))
                    except (ValueError, KeyError):
                        try:
                            result[i[2]] = [i[0].decode('utf-8')]
                        except KeyError:
                            pass
            return result


class TWSENo(ImportCSV):
    """ 上市股票代碼與搜尋 """
    def __init__(self):
        self.__allstockno = super(TWSENo, self).importcsv()

    def search(self, name):
        """ 搜尋股票名稱 by unicode

            :param str name: 欲搜尋的字串
            :rtype: dict
        """
        pattern = re.compile(name)
        result = {}
        for i in self.__allstockno:
            query = re.search(pattern, self.__allstockno[i])
            if query:
                query.group()
                result[i] = self.__allstockno[i]
        return result

    def searchbyno(self, no):
        """ 搜尋股票代碼

            :param str no: 欲搜尋的字串
            :rtype: dict
        """
        pattern = re.compile(str(no))
        result = {}
        for i in self.__allstockno:<commit_msg>Add ImportCSV.<commit_after># -*- coding: utf-8 -*-
''' TWSE stock no. '''
# Copyright (c) 2012, 2013, 2014 <NAME>iang, http://toomore.net/
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import csv
import os
import re


class ImportCSV(object):
    """ Import CSV """
    def __init__(self, stock_no_files, industry_code_files):
        self.industry_code_files = industry_code_files
        self.last_update = ''
        self.stock_no_files = stock_no_files

    def importcsv(self):
        ''' import data from csv '''
        csv_path = os.path.join(os.path.dirname(__file__), self.stock_no_files)
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            for i in csv_data:
                try:
                    result[i[0]] = str(i[1]).decode('utf-8')
                except ValueError:
                    if i[0] == 'UPDATE':
                        self.last_update = str(i[1]).decode('utf-8')
                    else:
                        pass
        return result

    @staticmethod
    def __industry_code():
        ''' import industry_code '''
        csv_path = os.path.join(os.path.dirname(__file__),
                self.industry_code_files)
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            for i in csv_data:
                result[i[0]] = i[1].decode('utf-8')
            return result

    @staticmethod
    def __loadindcomps():
        ''' import industry comps '''
        csv_path = os.path.join(os.path.dirname(__file__), self.stock_no_files)
        with open(csv_path) as csv_file:
            csv_data = csv.reader(csv_file)
            result = {}
            check_words = re.compile(r'^[\d]{2,}[\w]?')
            for i in csv_data:
                if check_words.match(i[2]):
                    try:
                        result[i[2]].append(i[0].decode('utf-8'))
                    except (ValueError, KeyError):
                        try:
                            result[i[2]] = [i[0].decode('utf-8')]
                        except KeyError:
                            pass
            return result


class TWSENo(ImportCSV):
    """ 上市股票代碼與搜尋 """
    def __init__(self):
        super(TWSENo, self).__init__('stock_no.csv', 'industry_code.csv')
        self.__allstockno = super(TWSENo, self).importcsv()

    def search(self, name):
        """ 搜尋股票名稱 by unicode

            :param str name: 欲搜尋的字串
            :rtype: dict
        """
        pattern = re.compile(name)
        result = {}
        for i in self.__allstockno:
            query = re.search(pattern, self.__allstockno[i])
            if query:
                query.group()
                result[i] = self.__allstockno[i]
        return result

    def searchbyno(self, no):
        """ 搜尋股票代碼

            :param str no: 欲搜尋的字串
            :rtype: dict
        """
        pattern = re.compile(str(no))
        result = {}
        for i in self.__allstockno:
{'fraction_non_alphanumeric': 0.10322745901639344, 'fraction_numerical': 0.007684426229508197, 'mean_word_length': 1.9669452887537995, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1298988', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2234, 'n_words': 681}