#πŸ”’ Messy code and can't find the problem

37 messages Β· Page 1 of 1 (latest)

winged sable
#

Helli everyone, first time writing code like this and I am genuinly confused and feel like I am messing around too much, also keep coming up with errors.

warm doveBOT
#

@winged sable

Python help channel opened

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.

winged sable
#

//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

sharp inlet
#

!code

warm doveBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

winged sable
#

It says its too big

sharp inlet
#

do you see the link to the pastebin in that blurb?

winged sable
#

Yes, was I supposed to download and send?

sharp inlet
#

download? huh?

#

paste your code to the pastebin and then copy paste the link here

winged sable
#

Ah okay 😭😭

sharp inlet
#

what is your actual question then?

winged sable
#

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

obtuse badger
#

What error is being thrown?

winged sable
obtuse badger
#

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

vale parcel
# winged sable https://paste.pythondiscord.com/54FA

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.

vale parcel
winged sable
#

What do you suggest I improve in addirtion?

vale parcel
# winged sable 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

warm doveBOT
#
xy-problem

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/.

vale parcel
#

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

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

vale parcel
#

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.

winged sable
#

Alright alright thanks I will check it out. Thanks

warm doveBOT
#
Python help channel closed for inactivity

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.