Error when trying to open csv file in Microptyhon Raspberry Pi Pico version #13548
Unanswered
clarsbyte
asked this question in
RP2040 / Pico
Replies: 1 comment
-
Try to use a generator to save memory. I guess the optimization is better and in this case the problem is shifted to the function The import time
import micropython
import gc
DEBUG = True
def timeit(function):
def inner(*args, **kwargs):
start = time.ticks_ms()
result = function(*args, **kwargs)
print(f"{function.__name__} took {time.ticks_diff(time.ticks_ms(), start):.1} ms\n")
return result
return inner
@timeit
def colorname(color, *, file="colors.csv"):
"""
Source of colors.csv: https://github.com/codebrainz/color-names/blob/master/output/colors.csv
"""
@micropython.native
def reader():
delimiter = ","
with open(file) as fd:
for line in fd:
yield line.split(delimiter)
@micropython.native
def parser():
"""
Assigning the first field to name and the last 3 fields to R, G, B
Calculating the threshohld.
Yield the result, if threshold is lesser or equal than 50 as the calculated absolute difference
"""
# https://peps.python.org/pep-3132/
for name, *_, R, G, B in reader():
# https://peps.python.org/pep-0572/
if (threshold := sum(abs(C - c) for C, c in zip(map(int, (R,G,B)), color))) <= 50:
yield threshold, name
try:
if DEBUG:
gc.collect()
before = gc.mem_free()
result = min(parser())
if DEBUG:
after = gc.mem_free()
print("Mem usage:", before - after)
return result[1]
except ValueError:
return "Unknown"
def min_function_test():
def gen(start, end):
while True:
if start >= end:
break
yield start
start += 1
@timeit
def min_func():
print("min with generator")
min(gen(0, items))
@timeit
def max_func():
print("max with generator")
min(gen(0, items))
@timeit
def min_max_1():
print("Min-Max with generator")
min_val = 0
max_val = 0
for value in gen(0, items):
min_val, max_val = min(value, min_val), max(value, max_val)
print(f"Min: {min_val} | Max: {max_val}")
print()
@timeit
def min_max_2():
print("Min-Max with range")
min_val = 0
max_val = 0
for value in range(0, items):
min_val, max_val = min(value, min_val), max(value, max_val)
print(f"Min: {min_val} | Max: {max_val}")
print()
items = gc.mem_free()
print("From 0 to", items, end="\n\n")
for func in (min_func, max_func, min_max_1, min_max_2):
func()
def bad():
gc.collect()
names = []
colors = []
with open("colors.csv") as fd:
for line in fd:
row = line.split(",")
names.append(row[0])
# it fails here
colors.append((int(row[-3]), int(row[-2]), int(row[-1])))
print("Running min_function_test")
try:
min_function_test()
except MemoryError:
print("My assumption was wrong\n")
else:
print("min_function_test has enough memory\n")
colors = [
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(0, 255, 255),
(255, 0, 255),
(210, 90, 38),
]
print("Running optimized function")
for color in colors:
print(colorname(color), color)
print()
print("Running bad function")
bad() Output:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So this is my code; I have both my python file and csv file in my raspberry pi pico already as you can see in the image1. However, when I run it, it returns an error.
The error:
What can I do to fix this calamity?
Beta Was this translation helpful? Give feedback.
All reactions