How to find the shortest path - python-3.x

In the function of find_shortest_func, i think if now position isn't "T" which is also known as the terminal or exit, then i will try to find for direction and see if it is "T", if not, check if it is space and i can go there. Besides, tell the next state function now output and dic to tell the place where i visited. But some errors occur and I don't know why.
I think the problem may occur where I tried to deepcopy the output list
import copy
def set_symbol(symbol_name):
def set_symbol_decorator(func):
def wrapper(self, symbol):
setattr(self, symbol_name, symbol)
return wrapper
return set_symbol_decorator
class Maze:
space_symbol = " "
obstacle_symbol = "X"
path_symbol = "•"
output = []
dis = 0
def __init__(self, input_string):
self.maze = []
if input_string.endswith("txt"):
with open(input_string) as f:
count = 0
for line in f.readlines():
self.maze.append([])
for j in line:
if j != '\n':
self.maze[count].append(j)
count += 1
else:
count = 0
for i in input_string.split("\n"):
self.maze.append([])
for j in i:
self.maze[count].append(j)
count += 1
def __str__(self):
output_string = ""
for i in range(20):
for j in range(20):
output_string += self.maze[i][j]
output_string += "\n"
return output_string
#set_symbol("space_symbol")
def set_space_symbol(self, change):
pass
#set_symbol("obstacle_symbol")
def set_obstacle_symbol(self, change):
pass
#set_symbol("path_symbol")
def set_path_symbol(self, change):
pass
def find_shortest_func(self, position: tuple, d: dict, out: list, dis: int):
dic = copy.deepcopy(d)
output = copy.deepcopy(out)
dic[(position[0], position[1])] = 1
output.append((position[0], (position[1])))
dis += 1
if self.maze[position[0]][position[1]] != "T":
if position[0]+1 < 20 and self.maze[position[0]+1][position[1]] == self.space_symbol and (position[0]+1, position[1]) not in dic:
self.find_shortest_func(
(position[0]+1, position[1]), dic, output, dis)
if position[1]+1 < 20 and self.maze[position[0]][position[1]+1] == self.space_symbol and (position[0], position[1]+1) not in dic:
self.find_shortest_func(
(position[0], position[1]+1), dic, output, dis)
if position[0]-1 >= 0 and self.maze[position[0]-1][position[1]] == self.space_symbol and (position[0]-1, position[1]) not in dic:
self.find_shortest_func(
(position[0]-1, position[1]), dic, output, dis)
if position[1]-1 >= 0 and self.maze[position[0]][position[1]-1] == self.space_symbol and (position[0], position[1]-1) not in dic:
self.find_shortest_func(
(position[0], position[1]-1), dic, output, dis)
if self.maze[position[0]][position[1]] == "T":
if dis < self.dis:
self.output = copy.deepcopy(output)
self.dis = dis
return
def find_shortest_path(self):
d = dict()
output = []
dis = -1
self.find_shortest_func((1, 0), d, output, dis)
return self.output, self.dis

Related

AttibuteError when trying to make configs

AttributeError: module 'collections' has no attribute 'Sequence'
i get this error everything i try to run my code but there isn't any information about how to use Mothur except for the the documentation.
`# python3
import sys
import queue
import itertools
from collections import deque
from mothur_py import Mothur
import collections.abc as collections
class KmerIdMgmt:
def __init__(self):
self.id = 0
self.ids_map = {}
self.kmers = {}
def insert(self, kmer):
if kmer not in self.ids_map:
self.ids_map[kmer] = self.id
self.kmers[self.id] = kmer
self.id += 1
return self.ids_map[kmer]
class DeBruijnGraph(object):
def __init__(self, k, reads):
self.k = k
self.threshold = self.k + 1
self.kmer_ids = KmerIdMgmt()
self.coverage = {}
self.graph = {}
self.outgoing_num = lambda k: len(self.graph[k][0])
self.incoming_num = lambda k: self.graph[k][1]
self.make_deBruijn_graph(self.break_reads_into_kmers(reads))
def break_reads_into_kmers(self, reads):
break_read = lambda read: [ read[j:j + self.k] for j in range(len(read) - self.k + 1) ]
return [ kmer for read in reads for kmer in break_read(read) ]
def make_deBruijn_graph(self, kmers):
def add_edge(graph, coverage, left, right):
graph.setdefault(left, [set(), 0])
graph.setdefault(right, [set(), 0])
coverage.setdefault((left, right), 0)
coverage[(left, right)] += 1
if right not in graph[left][0]:
graph[left][0].add(right)
graph[right][1] += 1
for kmer in kmers:
left = self.kmer_ids.insert(kmer[:-1])
right = self.kmer_ids.insert(kmer[1:])
if left != right:
add_edge(self.graph, self.coverage, left, right)
def remove_leaves(self):
removable = [ k for k, v in self.graph.items() if len(v[0]) == 0 ]
for k in removable:
del self.graph[k]
def print_graph(self):
for k, v in self.graph.items():
print(k, v)
class TipRemoval(DeBruijnGraph):
def __init__(self, k, reads):
DeBruijnGraph.__init__(self, k, reads)
def remove_tips(self):
for k, v in self.graph.items():
find_and_remove = None
if self.outgoing_num(k) == 1 and self.incoming_num(k) == 0:
find_and_remove = self.find_and_remove_incoming
elif self.outgoing_num(k) > 1:
find_and_remove = self.find_and_remove_outgoing
else: continue
condition = True
while condition:
condition = False
for edge in v[0]:
if find_and_remove(edge, 0):
v[0].remove(edge)
condition = True
break
def find_and_remove_outgoing(self, current, depth):
if self.outgoing_num(current) > 1 or self.incoming_num(current) > 1:
return False
if depth == self.threshold:
return False
if self.outgoing_num(current) == 0:
return True
if self.find_and_remove_outgoing(next(iter(self.graph[current][0])), depth + 1):
to = next(iter(self.graph[current][0]))
self.graph[current][0].pop()
self.graph[to][1] -= 1
return True
return False
def find_and_remove_incoming(self, current, depth):
if self.outgoing_num(current) == 0 or self.incoming_num(current) > 1:
return True
if depth == self.threshold:
return False
if self.find_and_remove_incoming(next(iter(self.graph[current][0])), depth + 1):
to = next(iter(self.graph[current][0]))
self.graph[current][0].pop()
self.graph[to][1] -= 1
return True
return False
class BubbleRemoval(TipRemoval):
def __init__(self, k, reads):
TipRemoval.__init__(self, k, reads)
self.paths = {}
def remove_bubbles(self):
for k, v in self.graph.items():
if self.outgoing_num(k) > 1:
self.dfs(path=[k], current=k, depth=0)
for pair, candidates_list in self.paths.items():
source, target = pair[0], pair[1]
best_path = max(candidates_list, key=lambda item: item[1])[0]
for path, _ in candidates_list:
if best_path == path or not self.bubble_possible(source, target):
continue
if self.paths_disjoint(best_path, path) and self.path_exists(path):
self.remove_path(path)
def bubble_possible(self, source, target):
return len(self.graph[source][0]) > 1 and self.graph[target][1] > 1
def path_exists(self, path):
for j in range(len(path) -1):
if path[j +1] not in self.graph[path[j]][0]:
return False
return True
def remove_path(self, path):
for j in range(len(path) -1):
self.graph[path[j]][0].remove(path[j +1])
self.graph[path[j +1]][1] -= 1
del self.coverage[(path[j], path[j +1])]
def paths_disjoint(self, a, b):
return len(set(a) & set(b)) == 2
def dfs(self, path, current, depth):
if current != path[0] and self.incoming_num(current) > 1:
weight = sum(self.coverage[(path[i], path[i+1])] for i in range(len(path)-1)) / len(path)
self.paths.setdefault((path[0], current), list()).append((path[:], weight))
if depth == self.threshold:
return
for next_ in self.graph[current][0]:
if next_ not in path:
path.append(next_)
self.dfs(path, next_, depth + 1)
path.remove(next_)
class PhiX174GenomeAssembler(BubbleRemoval):
def __init__(self, k, reads):
BubbleRemoval.__init__(self, k, reads)
def make_Euler_cycle(self):
verteces = deque()
path = []
# line 191
current = next(iter(self.graph))
verteces.append(current)
while verteces:
current = verteces[0]
if len(self.graph[current][0]) != 0:
t = next(iter(self.graph[current][0]))
verteces.append(t)
self.graph[current][0].remove(t)
continue
path.append(current)
verteces.popleft()
return path
def assemble(self):
self.remove_tips()
self.remove_leaves()
self.remove_bubbles()
cycle = self.make_Euler_cycle()
circular_genome = self.kmer_ids.kmers[cycle[0]]
for i in range(1, len(cycle) - (self.k - 1)):
circular_genome += self.kmer_ids.kmers[cycle[i]][-1]
return circular_genome
if __name__ == "__main__":
n_kmers = int(input())
for _ in range(n_kmers):
reads = list(input())
reads = str(reads)
with open('reads.fasta', 'w') as read:
read.write(reads)
k = 100
m = Mothur()
contig = m.make.contigs(ffasta = read)
for x in range(n_kmers):
print(">CONTIG", x)
print(contig)
`

I've tried running the code but it says list index is out of range

from typing import List
# You are given an integer n, denoting the no of people who needs to be seated, and a list of m integer seats, where 0 represents a vacant seat. Find whether all people can be seated, provided that no two people can sit together
When I run this code in geeks for geeks for submission I get a error that List index is out of range.
but seems to work fine when I run it as a script.
class Solution:
def is_possible_to_get_seats(self, n: int, m: int, seats: List[int]) -> bool:
vacant_seats = 0
if len(seats) == 2:
if seats[0] or seats[1] == 1:
print(seats)
return False
else:
print(seats)
return True
else:
for x in range(len(seats)):
if x == 0:
if seats[x] == 0 and seats[x+1] == 0:
seats[x] = 1
vacant_seats += 1
elif x == len(seats)-1:
if seats[x] == 0 and seats[x-1] == 0:
seats[x] = 1
vacant_seats += 1
else:
if seats[x] == 0:
if seats[x+1] == 0 and seats[x-1] == 0:
seats[x] = 1
vacant_seats += 1
if vacant_seats < n:
return False
else:
return True
# {
# Driver Code Starts
class IntArray:
def __init__(self) -> None:
pass
def Input(self, n):
arr = [int(i) for i in input().strip().split()] # array input
return arr
def Print(self, arr):
for i in arr:
print(i, end=" ")
print()
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
m = int(input())
seats = IntArray().Input(m)
obj = Solution()
res = obj.is_possible_to_get_seats(n, m, seats)
result_val = "Yes" if res else "No"
print(result_val)
# } Driver Code Ends

Switching between classes and passing results

I am trying to add different PlugLead's to the PlugBoard and extract the combination. In the first class I am extracting the one letter from the list should they match or return the input. e.g.
lead = PlugLead("AG")
assert(lead.encode("A") == "G")
class PlugLead:
def __init__(self, c):
self.c = c
def encode(self, l):
c0 = self.c[0]
c1 = self.c[1]
if len(l) == 1 and c0 == l:
return c1
elif len(l) == 1 and c1 == l:
return c0
else:
return l
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = []
self.c = []
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif item in self.leads:
print(f"leads already have this item: {item}")
else:
self.leads.append(item)
self.__head += 1
return self.leads
def encode(self)
lines = plugboard.leads
for word in lines:
word = word.split(",")
PlugLead.encode(word)
In the second class I am trying to add multiple combinations and then at the end pass the one letter to see what its match is in the Class PlugLead however am not able to switch between the two. In the class PlugLead I have a add function that allows the different combinations to be added up to 10 and then I would like to encode from this list the combination of the pairs. e.g.
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("A") == "A")
if you want to use PlugLead("{balabala}"),you need use __new__ to return a dict when you create an instance, not __init__.
you want a key-value pair in Plugboard, it should be a dict not list
fix some other typo and bugs.
code:
class PlugLead:
def __new__(self, c):
return {c[0]:c[1]}
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = {}
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif list(item.keys())[0] in self.leads.keys():
print(f"leads already have this item: {item}")
else:
self.leads.update(item)
self.__head += 1
return self.leads
def encode(self,key):
if key in self.leads:
return self.leads[key]
elif key in self.leads.values():
return list(self.leads.keys())[list(self.leads.values()).index(key)]
return key
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("U") == "K")
assert(plugboard.encode("A") == "A")
print(plugboard.encode("K"))
print(plugboard.encode("U"))
print(plugboard.encode("A"))
result:
leads already have this item: {'K': 'U'}
U
K
A

Add a node specific counter to a slingly linked list

Did some research, but could only find examples where there was a key - say '5' and they count the occurrences of '5' in the linked list. I want to count each occurrence of each string in a llist. Say I have a linked list with ' a, a, a, b, d, f'. I want the output to say a - 3 b - 1 d -1 f -1.
I have built the list but the only way I can think of doing it is initializing a count variable, however I can't figure out how to reset it as I print the entire list after everything is done so right now my output looks like: a - 3 b -3 d -3 f -3.
Here is the code:
class Linked_List:
def __init__(self):
self.head = None
self.count = 0
def print(self):
p = self.head
while p is not None:
print(p.data, ' -', self.count)
p = p.next
def insert(self, x):
""""""
p = self.head
q = None
done = False
while not done:
if self.head == x:
done = True
elif p == None:
head = Linked_List_node(x)
q.next = head
done = True
elif x == p.data:
# head = Linked_List_node(x)
# self.head = head
self.count += 1
done = True
elif x < p.data:
if self.head == p:
head = Linked_List_node(x)
head.next = p
self.head = head
done = True
else:
head = Linked_List_node(x)
head.next = p
q.next = head
done = True
q = p
if p is not None:
p = p.next
class Linked_List_node:
def __init__(self, value):
self.data = value
self.next = None
Revised Code:
def print(self):
p = self.head
head = Linked_List_node(p.data)
while p is not None:
print(p.data, '-', self.count(p.data))
p = p.next
def count(self, x):
# loop thru list for all x, if find x add 1 to count. Assign final count to that word.
with open('cleaned_test.txt', 'r') as f:
for line in f:
for word in line.split():
if word == x:
self.count += 1
Since you want your count function to be able to count the frequencies of each word, I would create a function similar to print called count in class Linked_List, which iterates through the list, and updates the frequency dictionary.
def count(self):
dct = {}
p = self.head
while p is not None:
if p.data in dct:
dct[p.data] += 1
else:
dct[p.data] = 1
p = p.next
return dct
The output will look like.
head = Linked_List_node('a')
ll = Linked_List()
ll.head = head
for item in ['a', 'a', 'b', 'd', 'f']:
ll.insert(item)
print(ll.count())
#{'a': 3, 'b': 1, 'd': 1, 'f': 1}

Runtime/Resource Warning error in Python

I was trying to run this code and encountered a run time error. I am not able to debug the code. I do believe that the error lies in functions huffman_encode and huffman_decode. The error showing is the resource warning error. Here is the code:
from linked_list import *
from huffman_bits_io import HuffmanBitsWriter as writer, HuffmanBitsReader as reader
import unittest
class Leaf:
'''class that implements Leaf'''
def __init__(self, parent, value, code, frequency):
self.parent = parent
self.frequency = frequency
self.value = value
self.code = code
def __eq__(self, other):
return type(other) == Leaf and self.parent == other.parent and self.frequency ==other.frequency and self.value==other.value and self.code==other.code
def __repr__(self):
return "[ {}, frequency = {} ]".format(self.code, self.frequency)
class Node:
'''class that implements Node'''
def __init__(self, parent, code, lchild, rchild, frequency):
self.parent = parent
self.code = code
self.frequency = frequency
self.lchild = lchild
self.rchild = rchild
def __eq__(self, other):
return type(other) == Node and self.parent==other.parent and self.code == other.code and self.frequency == other.frequency and self.lchild == other.lchild and self.rchild == other.rchild
def __repr__(self):
return "{}, freq = {}\n\left = {}\n\right = {}".format(self.code, self.frequency, self.lchild.___repr__(), self.rchild.__repr__())
def strip(string, seq):
'''this function cuts sequence from beginning of string if possible and returns result '''
if len(seq) > len(string):
return string
for i in range(len(seq)):
if seq[i] != string[i]:
return string
else:
return string[len(seq):]
def find(lst, item):
'''this function finds index of first occurrence of given element in the list and returns it or raise error if there is no such element'''
for i in range(lst.length):
if get(lst, i).value[0] == item:
return i
else:
raise ValueError
def string_traverse(node):
'''this function returns string representation of tree in pre-order traversal'''
lst = empty_list()
traverse(node, lst) #calls traverse
result_string = ''
for i in range(lst.length): #accumulate string from lst list
result_string += chr(get(lst, i).value)
return result_string
def traverse(node, code):
'''this function traverse the try and return list of leaf's value(helper for string_traverse)'''
if type(node) == Leaf:
code = add(code, node.value, code.length) #if node is Leaf than terminate recursion and return character
else:
traverse(node.lchild, code) #recursive call
traverse(node.rchild, code) #recursive call
def count_occurrences(file_name):
'''this function returns list that represent occurrence of every character of given string'''
with open(file_name) as file: #reads file
data = file.read()
lst = list()
for i in range(len(data)): #creates list of integer representation of string
lst.append(ord(data[i]))
data = lst
lst = empty_list()
for char in data: #this loop calculates occurrences of characters in the string
try:
index = find(lst, char)
lst = set(lst, index, (char, get(lst, index).value[1] + 1))
except ValueError:
lst = add(lst, (char, 1), 0)
lst = sort(lst, lambda x: x.value[1], False) #sorts occurrences
return lst
def comes_before(el1, el2):
'''this function returns True if el1 leaf should come before el2 leaf in Huffman tree meaning'''
if el1[1] < el2[1] or (el1[1] == el2[1] and type(el1[0]) is int and type(el2[0]) is int and el1[0] < el2[0]):
return True
else:
return False
def build_tree(occurrences):
'''this function returns Huffman tree based on given list of occurrences'''
if occurrences.length == 1: #if we have only one character returns Leaf with this character and code '0'
return Leaf(None, get(occurrences, 0).value[0], '0', get(occurrences, 0).value[1])
while occurrences.length != 1: #algorith described in the task
el1, occurrences = remove(occurrences, 0)
el2, occurrences = remove(occurrences, 0)
el1, el2 = el1.value, el2.value
if not comes_before(el1, el2): #finds order of elements in the tree
el1, el2 = el2, el1
new = Node(None, '', None, None, el1[1] + el2[1]) #creates new node
if type(el1[0]) is Node:
el1[0].code = '0' #sets up code for node
el1[0].parent = new
new.lchild = el1[0]
else:
new.lchild = Leaf(new, el1[0], '0', el1[1]) #if el1 is character not Node we will create leaf for that character
if type(el2[0]) is Node:
el2[0].code = '1' #sets up code for node
el2[0].parent = new
new.rchild = el2[0]
else:
new.rchild = Leaf(new, el2[0], '1', el2[1]) #if el2 is character not Node we will create leaf for that character
occurrences = insert_sorted(occurrences, (new, new.frequency), comes_before) #inserts new node
return get(occurrences, 0).value[0]
def collect_code(node, code = ''):
'''this function traverse Huffman tree and collect code for each leaf and returns them as nested list(helper for create_code)'''
if type(node) == Leaf:
lst = empty_list()
return add(lst, (node.value, code + node.code), 0) #if node is Leaf terminates recursion and returns code for the leaf
else:
lst = empty_list()
lst = add(lst, collect_code(node.lchild, code + node.code), 0) #recursive call
lst = add(lst, collect_code(node.rchild, code + node.code), 0) #recursive call
return lst
def create_code(tree):
'''this function unpack result of calling collect_code and return Huffman code as a list of tuples'''
code = collect_code(tree) #calls collect code
i = 0
while i < code.length: #this loop unpacks list
if type(get(code, i).value) is not tuple:
item, code = remove(code, i)
for j in range(item.value.length):
code = add(code, get(item.value, j).value, i)
continue
i += 1
return code
def huffman_encode(input_file, output_file):
'''task describe this function'''
occurrences = count_occurrences(input_file)
tree = build_tree(occurrences)
string = empty_list()
t = traverse(tree, string)
code = create_code(tree)
with open(input_file) as file:
string = file.read()
result_string = ''
for i in range(len(string)): #this loop encodes string using code produced by create_code function
for j in range(code.length):
temp = get(code, j).value
if string[i] == chr(temp[0]):
result_string += temp[1]
break
for i in range(occurrences.length):
temp = get(occurrences, i).value
occurrences = set(occurrences, i, (chr(temp[0]), temp[1]))
occurrences = sort(occurrences, lambda x: x.value[0], False)
file = writer(output_file)
file.write_int(code.length)
for i in range(occurrences.length):
temp = get(occurrences, i).value
file.write_byte(ord(temp[0]))
file.write_int(temp[1])
file.write_code(result_string)
file.close()
return string_traverse(tree)
def huffman_decode(input_file, output_file):
'''task describe this function'''
file = reader(input_file)
number_of_codes = file.read_int()
occurrences = empty_list()
for i in range(number_of_codes):
char = file.read_byte()
number = file.read_int()
occurrences = add(occurrences, (char, number), 0)
occurrences = sort(occurrences, lambda x: x.value[1], False)
tree = build_tree(occurrences)
code = sort(create_code(tree), lambda x: x.value[0], False)
occurrences = sort(occurrences, lambda x: x.value[0], False)
quantity_of_bits = 0
for i in range(code.length):
quantity_of_bits += get(occurrences, i).value[1]*len(get(code, i).value[1])
occurrences = sort(occurrences, lambda x: x.value[1], False)
bit_string = ''
for i in range(quantity_of_bits):
bit_string = bit_string + ('1' if file.read_bit() else '0')
result_string = ''
while bit_string: #this loop decodes string using code produced by create_code function
for j in range(code.length):
temp = get(code, j).value
stripped = strip(bit_string, temp[1])
if len(stripped) < len(bit_string):
result_string += chr(temp[0])
bit_string = stripped
break
with open(output_file, 'w') as file:
file.write(result_string)
file.close()
class Test(unittest.TestCase):
def test_strip1(self):
self.assertEqual(strip('123456', '123'), '456')
def test_strip2(self):
self.assertEqual(strip('123', '4567'), '123')
def test_strip3(self):
self.assertEqual(strip('123', '456'), '123')
def test_find(self):
lst = empty_list()
lst = add(lst, (1, 'b'), 0)
lst = add(lst, (2, 'a'), 1)
self.assertEqual(find(lst, 2), 1)
def test_find_raise(self):
lst = empty_list()
lst = add(lst, (1, 'b'), 0)
lst = add(lst, (2, 'a'), 1)
self.assertRaises(ValueError, find, lst, 5)
def test_occurrences(self):
lst = empty_list()
lst = add(lst, (97, 5), 0)
lst = add(lst, (98, 3), 0)
lst = add(lst , (99, 7), 2)
self.assertEqual(str(count_occurrences(r'test2.txt')), str(lst))
def test_create_code_and_tree_build(self):
occurrences = count_occurrences(r'test2.txt')
tree = build_tree(occurrences)
code = create_code(tree)
code = sort(code, lambda x: x.value[0], False)
self.assertEqual(str(code), "[(97, '11'), (98, '10'), (99, '0')]")
def test_huffman_encode_decode(self):
string = huffman_encode(r'test1.txt', r'test_out.txt')
huffman_decode(r'test_out.txt', r'test_decode.txt')
self.assertEqual(string, 'a')
with open(r'test1.txt') as file1:
with open(r'test_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
def test_huffman_encode_decode3(self):
string = huffman_encode(r'test2.txt', r'test2_out.txt')
huffman_decode(r'test2_out.txt', r'test2_decode.txt')
self.assertEqual(string, 'cba')
with open(r'test2.txt') as file1:
with open(r'test2_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
def test_huffman_encode_decode2(self):
string = huffman_encode(r'test3.txt', r'test3_out.txt')
huffman_decode(r'test3_out.txt', r'test3_decode.txt')
self.assertEqual(string, 'edcba')
with open(r'test3.txt') as file1:
with open(r'test3_decode.txt') as file2:
self.assertEqual(file1.read(), file2.read())
file2.close()
file1.close()
if __name__ == '__main__':
unittest.main()
And following is the error:
...
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test_out.txt'>
.
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test3_out.txt'>
.
Warning (from warnings module):
File "C:\Users\Vikas\Documents\fwdregardingprojectdevelopment\huffman.py", line 212
with open(output_file, 'w') as file:
ResourceWarning: unclosed file <_io.BufferedReader name='test2_out.txt'>
.....
----------------------------------------------------------------------
Ran 10 tests in 0.272s
OK
it seems somewhere in your code file 'out_file' is opened and not closed
find where it is opened and close it :
out_file.close()

Resources