Created
December 9, 2014 08:10
-
-
Save overdese/f4c4f82d2a93c9546441 to your computer and use it in GitHub Desktop.
This script looking directory with *.txt files and wrap this files by needed functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding:utf-8 -*- | |
import codecs | |
import os | |
from random import choice, shuffle | |
from copy import deepcopy | |
from itertools import cycle | |
class FileOperation(object): | |
""" | |
Making operations with file | |
attr: | |
d_file - description of file | |
lst - list of lines from file | |
methods: | |
read - read data from files | |
get_list - read file and return list | |
reverse_list - return revers_list of file | |
random_item - get random item from lst | |
random_shuffle - get shuffle list lst | |
""" | |
def __init__(self, path_to_file): | |
self.d_file = codecs.open(path_to_file, 'r', 'utf-8') | |
self.lst = self.d_file.read().splitlines() | |
def __del__(self): | |
self.d_file.close() | |
def read(self): | |
return self.d_file.read() | |
def get_list(self): | |
return self.lst | |
def reverse_list(self): | |
tmp_lst = self.lst | |
tmp_lst.reverse() | |
return tmp_lst | |
def cycle_list(self, max_elements): | |
n = 0 | |
for e in cycle(self.lst): | |
if n < max_elements: | |
yield e | |
else: | |
break | |
n += 1 | |
def random_item(self): | |
return choice(self.lst) | |
def random_shuffle(self): | |
tmp_lst = deepcopy(self.lst) | |
shuffle(tmp_lst) | |
return tmp_lst | |
def text_split(self): | |
pass | |
class FileWrapper(object): | |
""" | |
Class for wrapping files in directory of class FileOperation. | |
Can get override class FileOperation through parameter c_file_operation. | |
attr: | |
dynamic-added attrs - has prefix file_ (ex. if filename 'keys.txt' - 'file_keys') | |
methods: | |
find_files - search files into directory and return dictionary with file name and path | |
add_attrs - adding attributes from dictionary | |
get_dict - return dictionary of users attributes | |
""" | |
def __init__(self, path_to_dir, c_file_operation=FileOperation): | |
if os.path.exists(path_to_dir): | |
self.add_attrs(self.find_files(path_to_dir), c_file_operation) | |
def find_files(self, path_to_dir): | |
files_dict = {} | |
for item in os.listdir(path_to_dir): | |
if os.path.isfile(os.path.join(path_to_dir, item)): | |
if os.path.splitext(item)[1] == '.txt': | |
files_dict['file_%s' % os.path.splitext(item)[0]] = os.path.join(path_to_dir, item) | |
return files_dict | |
def add_attrs(self, files_dict, c_file_operation): | |
for name, path in files_dict.items(): | |
self.__setattr__(name, c_file_operation(path)) | |
def get_dict(self): | |
return self.__dict__ | |
if __name__ == '__main__': | |
# create files | |
for e in xrange(3): | |
os.system(r'echo "1\n2\n3" > /tmp/tmpf_%d.txt' % e) | |
fw = FileWrapper('/tmp/') | |
print fw.get_dict() | |
print fw.file_tmpf_0.get_list() | |
print fw.file_tmpf_1.random_shuffle() | |
print fw.file_tmpf_2.random_item() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment