#πŸ”’ How can i make this better pt2 lol

44 messages Β· Page 1 of 1 (latest)

fast summit
#
import os
import sys

UAFN={}
with open(sys.argv[1], "r") as file:
    for line in file:
        if ".js" in line:
            line=line.split(" ")
            file_path=line[6][1:]
            file_name=file_path.split("/")[1]
            url=line[10].strip('"')
            unique_suffix=url.split("http://")[1].replace("/","_")
            domain=url.split("/")[2]
            full_url=f"{url}{file_path}"
            unique_file_name=f"{unique_suffix}_{file_name}"
            unique_file_name.replace("-","_",1)
            print(domain)
            print(file_name)
            print(file_path)
            print(unique_file_name)
            print(url)
            print(full_url)
            print(line[6] ,line[10])

            UAFN[full_url]=unique_file_name


for d,n in UAFN.items():
    os.system(f"wget -O {n} {d}")
jaunty lanternBOT
#

@fast summit

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.

remote lichen
#

Also I'd switch the if statement to a guard clause, so ```py
if '.js' not in line:
continue
... # The rest of the code that is curently in the if statement

ocean stag
remote lichen
remote lichen
ocean stag
fast summit
#

Oh i see yes this some sort of exercise i have to do in python but basically i did it in bash and then were told to write in python. Its to locate all .js files in a http server log and then download them with a unique name but yeah i dont know much python so i just wanted tips on how to clean it up

#

looks like more comments and also the check for validating that its given an argument

ocean stag
#

I find a good opening thing is to include a sample of a source line as one of the comments so that it's clear what kind of text you're working against.

fast summit
remote lichen
ocean stag
remote lichen
ocean stag
fast summit
ocean stag
vague rapids
#

i reccomend pathlib instead of os

#

it works better

remote lichen
ocean stag
#

... not by hacking the log file, but by feeding things to whatever's doing the logging.

remote lichen
#

As they said, you can run it with subprocess.run or even better (imo) use the library requests or built in library urllib, Which both should be fairly easy to setup in this context.

raw ravine
vague rapids
#

for d, n in UAFN.items(): Path(n).write_text(Path(d).read_text())

raw ravine
#

That's not what wget does

vague rapids
#

🀷

raw ravine
#

unless Path has gained HTTP capabilities while I wasn't looking πŸ™‚

ocean stag
raw ravine
#

that would be cursed

remote lichen
#

I mean the ellipses class doesn't really have any uses other than a place holder afaik, why not make it callable to make http requests too?

#

I mean think about how much you could condense your code if they added this:

....get("https://thisisjustwrong.com")
#

gotta test if the tokenizer can handle a 4th dot lol

fast summit
remote lichen
#

!e

print(....__init__())
jaunty lanternBOT
remote lichen
#

i love this

remote lichen
#

I'll stick with Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo

fast summit
#

!close

jaunty lanternBOT
#
Python help channel closed with !close

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.