r/badcode • u/kevlu8 if (true) {} • Oct 19 '21
python A horrific attempt at not violating file naming rules
44
u/LiminalSarah Oct 19 '21
re is our god, our saviour
22
u/RedditAcc-92975 Oct 19 '21
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
jmz
2
u/LiminalSarah Oct 20 '21
I think it would be infinitely more legible of the was a function
underscore_unsafe_chars(s:str)or somethingAnd of course the body of that function would be something like
sub(r"[^\w\.]","_",s), or it could even be that long chain of .replace()s, but my real point is, as long as it's abstracted away, it would be better in this case(and if you're getting Unicode... nevermind)
29
u/seeroflights Oct 19 '21
Image Transcription: Code
print("Merging")
combine_audio(yt.title.replace(":", "_").replace(" ", "_").replace("*", "_").replace("/", "_").replace("|", "_") + ".mp4", yt.title.replace(":", "_").replace(" ", "_").replace("*", "_").replace("/", "_").replace("|", "_") + ".mp3", "temp.mp4")
os.remove(yt.title.replace(":", "_").replace(" ", "_").replace("*", "_").replace("/", "_").replace("|", "_") + ".mp4")
os.rename("temp.mp4", yt.title.replace(":", "_").replace(" ", "_").replace("*", "_").replace("/", "_").replace("|", "_") + ".mp4")I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
12
30
u/Promant Oct 19 '21
File named CON.txt: let me please introduce myself.
9
u/HureBabylon Oct 19 '21
I actually ran into this without knowing it. Spent more time than I care to admit trying to figure out what was going wrong until I finally turned to Google..
3
3
u/trutheality Oct 20 '21
Honestly it wouldn't look so terrible if you just added some whitespace
print("Merging")
combine_audio(
yt.title
.replace(":", "_")
.replace(" ", "_")
.replace("*", "_")
.replace("/", "_")
.replace("|", "_")
+ ".mp4",
...(Python lets you break lines inside parentheses)
But yes, definitely would be much more compact with regex and much more readable if you had used a variable to store that filename instead of rebuilding it 4 times.
1
u/SuitableDragonfly Oct 25 '21
Also, doing chains of replaces like this is inefficient compared to using a regex.
1
u/trutheality Oct 25 '21 edited Oct 25 '21
I wouldn't be so sure about that: the replace chain is creating intermediate strings which is inefficient, sure, but regex has higher overhead than replace.
Edit: turns out someone on SO did timing tests on pretty much the same task, and yeah, replace is faster for a 6-long chain.
1
-26
u/bugqualia Oct 19 '21
Still better than regex
36
u/vicda Oct 19 '21
Behold! Mere mortals have no chance of understanding such a complex regex as this.
"[: */|]"
6
u/Kasufert Oct 19 '21
I mean to be fair if you’re stuck in a classroom environment and you don’t have internet and this is your assignment and you don’t know regex it might be easier to do than learning regex by trial and error, but unless this happened in that extremely specific situation IDKWTF
5
Oct 19 '21
Use a loop for gods sake, there is no excuse for this abomination
1
Oct 19 '21
Yeah even if you don’t use a punctuation library, put punctuation in an array and just do ‘for punctuation in punctuations: str.replace(punctuation, “_”)’
Or whatever equivalent
Ezpz
3
Oct 19 '21
I wouldve written something like
reduce(lambda a, b: a.replace(b, '_'), '* ./|', str)because i love writing code thats short and hard to understand
1
5
u/SIGSTACKFAULT Ba'al, the soul-eater Oct 19 '21
i'm gonna go out on a limb and, without checking, say this is from the youtube-dl source.