#π Messy code and can't find the problem
37 messages Β· Page 1 of 1 (latest)
@winged sable
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
//py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from datetime import datetime
import re
folder = "/Users/lamanazer/Desktop/UT"
out_dir = "/Users/lamanazer/Desktop/UT/out"
def get_basename(path):
return path.replace("\", "/").split("/")[-1]
def get_noextension(path):
return get_basename(path).rsplit(".", 1)[0]
def extract_measurement_time(base_name):
m = re.search(r"(\d{8})T(\d{6})", base_name)
if not m:
return None
date_str, time_str = m.groups()
try:
dt = datetime.strptime(date_str + time_str, "%d%m%Y%H%M%S")
return dt
except ValueError:
return None
def process_csv_file(file_path):
print(f"Processing file: {file_path}")
//
guys it keeps saying my code is too long what
I will screenshot
I am unable to screenshot whoever can help I can send priv
!code
It says its too big
do you see the link to the pastebin in that blurb?
Yes, was I supposed to download and send?
what is your actual question then?
It keeps erroring and I dont know why I am also pretty sure some parts are wrong in the code
Its not a long code but I can'y figur eit out
What error is being thrown?
at line 20 but only like a short error repeating that line (if you understand what i mean)
sometimes python throws an error in the line after the one that is causing an error
line 19 isn't indented properly. it has an extra space
smells a bit like AI.
anyhow, I'd suggest looking into pathlib module instead of having to parse and split the filepaths yourself.
btw, the problem seem to be stemming from line 113; wrongly indented lines.
yes, fix the extra space (in line 19).
I copy pasted sections from a professors code but I agree it looks not that efficient of a code
What do you suggest I improve in addirtion?
well it comes down to the logic itself. you could try to implement some things to streamline your process; but those will mostly be sub-par.
we dont know the final goal of your project, so people may only suggest sub-optimizations and tools based on the code that you've shown.
which leads to a whole different situation. where you think X is the solution; and working towards it. improving, optimizing, and learning about it. while a simpler faster Y solution exists.
!xy
The XY problem can be summarised as asking about your attempted solution, rather than your actual problem.
Often programmers will get distracted with a potential solution they've come up with, and will try asking for help getting it to work. However, it's possible this solution either wouldn't work as they expect, or there's a much better solution instead.
For more information and examples, see http://xyproblem.info/.
anyhow, regarding those "sub-par" optimizations;
A) look into defaultdict from the collections module.
B) look into slicing, or grouping parts of an iterator. possibly itertools.batched() could be an good alternative.
C) you could create a class to encapsulate the csv parsing.
D) look into glob library, or pathlib.Path.rglob()/Path.iterdir() to grab all csv files under a directory.
though, imo, this logic is a bit flawed. why do you need to parse everything recursively under a directory?
E) your plot generation and saving to file is the same througout the code. try making a function to handle that instead.
@winged sable
I don't know my csv files were weird so I saw it easuer to eneter the folder that i had all my csv's and iterating through each one. I will look into glob though
all these are just suggestions. not all have to be implemented. these ofc depend on the scale of your project. if its just a one-off thing, keep it as is.
if it works, dont fix it.
make a copy, and edit that.
Alright alright thanks I will check it out. Thanks
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.