Exemple #1
0
def write_file(filename, text, encoding = 'utf-8', end_of_line = '\n'):
    if string_utils.is_blank(filename):
        raise IOError('filename is None')
    
    try:
        text_file = codecs.open(filename, 'w', encoding)
        
        if isinstance(text, list):
            for line in text:
                text_file.write(line + end_of_line)
        elif isinstance(text, basestring):
            text_file.write(text)
            
        text_file.close()
    except IOError:
        pass
Exemple #2
0
def write_file(filename, text, encoding='utf-8', end_of_line='\n'):
    if string_utils.is_blank(filename):
        raise IOError('filename is None')

    try:
        text_file = codecs.open(filename, 'w', encoding)

        if isinstance(text, list):
            for line in text:
                text_file.write(line + end_of_line)
        elif isinstance(text, basestring):
            text_file.write(text)

        text_file.close()
    except IOError:
        pass
Exemple #3
0
def read_file(filename, tail= 'all', encoding = 'utf-8', strip = False):
    if string_utils.is_blank(filename):
        raise IOError('filename is None')
    
    text = []
    try:   
        text_file = codecs.open(filename, 'r', encoding)
        text = [line.strip() if strip else line for line in text_file.readlines()]
        if tail and tail != 'all':
            if string_utils.is_numeric(tail):
                _tail = int(tail)
                _size = len(text)
                if _tail < _size:
                    text = text[_size - _tail : _size]
        text_file.close()
    except IOError:
        text = None

    return text
Exemple #4
0
def awk(target, pattern, separator, action):
    '''
    awk : pattern scanning and text processing language
    
    @param target: string list or text file name
    @param pattern: regex pattern
    @param separator: line separator
    @param action: column index list or function: str[]=action(str[])
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male']
              print awk.awk(list, '', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    
    '''

    text = grep.grep(target, pattern)
    if not text:
        return None

    if str_utils.is_blank(separator):
        separator = ' '

    result = []

    for line in text:
        if line != None and separator in line:
            split_text = str_utils.split(line, separator)
            if not action:
                result.append(split_text)
            elif type(action) == types.ListType:
                temp_line = []
                for column in action:
                    if str_utils.is_numeric(column):
                        temp_line.append(split_text[column])
                result.append(temp_line)
            elif type(action) == types.FunctionType:
                temp_line = action(split_text)
                if temp_line != None and type(temp_line) == types.ListType:
                    result.append(temp_line)

    return result
Exemple #5
0
def awk(target, pattern, separator, action):
    '''
    awk : pattern scanning and text processing language
    
    @param target: string list or text file name
    @param pattern: regex pattern
    @param separator: line separator
    @param action: column index list or function: str[]=action(str[])
    
    @summary: list= ['1:huiyugeng:male', '2:zhuzhu:male']
              print awk.awk(list, '', ':', [1])
              output: ['huiyugeng', 'zhuzhu']
    
    '''
    
    text = grep.grep(target, pattern)
    if not text:
        return None
    
    if str_utils.is_blank(separator):
        separator = ' '
    
    result = []
        
    for line in text:
        if line != None and separator in line:
            split_text = str_utils.split(line, separator)
            if not action:
                result.append(split_text)
            elif type(action) == types.ListType:
                temp_line = []
                for column in action:
                    if str_utils.is_numeric(column):
                        temp_line.append(split_text[column])
                result.append(temp_line)
            elif type(action) == types.FunctionType:
                temp_line = action(split_text)
                if temp_line != None and type(temp_line) == types.ListType:
                    result.append(temp_line)
                
    return result
Exemple #6
0
def read_file(filename, tail='all', encoding='utf-8', strip=False):
    if string_utils.is_blank(filename):
        raise IOError('filename is None')

    text = []
    try:
        text_file = codecs.open(filename, 'r', encoding)
        text = [
            line.strip() if strip else line for line in text_file.readlines()
        ]
        if tail and tail != 'all':
            if string_utils.is_numeric(tail):
                _tail = int(tail)
                _size = len(text)
                if _tail < _size:
                    text = text[_size - _tail:_size]
        text_file.close()
    except IOError:
        text = None

    return text
Exemple #7
0
 def test_is_blank(self):
     self.assertEqual(string_utils.is_blank(None), True)
     self.assertEqual(string_utils.is_blank('word'), False)
     self.assertEqual(string_utils.is_blank(True), False)
     self.assertEqual(string_utils.is_blank(21), False)