2

Learning Python.
Wrote a script to download images from the official MotoGP website.
https://github.com/d02d33pak/...
Was curious to know what "professional" scripts look like.
Anyone willing to take a look and go I've some advise?

Comments
  • 0
    "professional" scripts look like someone just shat in a pan and made a burger out of it

    Looks a lot better than the python I'm used to seeing, though I must admit, I don't see a lot of python
  • 1
    @Tayo so the less python the better the script looks then? :)
  • 1
    @netikras yes exactly! Great minds think alike
  • 1
    Round 2: make it async with curio! (don't use asyncio) Download all the images in parallel instead of one by one to reduce the waiting times.

    Some good reading material:
    https://curio.readthedocs.io/en/...
    https://vorpus.org/blog/...

    Also, I'd probably use 'requests' istead of urllib: much easier to work with, handles stuff for you in the background, less bloat.
  • 0
    @Tayo so you mean it looks fine? maybe better?
  • 1
    @endor sure, will give both a try
  • 2
    I skimmed through it, you're doing well!

    In main.py replace the long if-elif with a dict and catch its KeyErrors to improve readability:

    pathByCategory = {'gp': 'events', 'best_of': 'best+of'} # Add more
    try:
    full_url = main_url + pathByCategory[category]
    content = urlopen(full_url)
    soup = ...
    download_category(...)
    except KeyError:
    print('wrong category')
    exit(1)

    For short functions I prefer the whole happy path in the try block. It allows to read it in one go and error cases one by one. It's unwieldy for many exceptions and if the same can be thrown from multiple places. In that case better split it.
    You can define dict&main_url outside the function so they aren't redefined on each call.
    Use non-zero exit codes on errors. Your shell can then indicate your script has failed, and it helps with integrating with others.

    downloader.py line 26 is missing the closing parens.

    Hope that helps, keep having fun!
  • 0
    @dontbeevil

    I like almost all sports[coz of the competitive feel], but recently fallen more in love with motogp coz of growing love for bikes
  • 0
    @VaderNT
    hey, I have made the above mentioned changes... thanks
  • 0
    hey @endor.. tried implement curio by just reading the first 10 lines of the doc... and ofcourse couldn't get it to work..
    have no prior experience in async/await... will have to dig deeper..
    and also I couldnt find much reading material on curio on Google or SO
  • 0
    @d02d33pak LOL dude, it's pretty much impossible to learn anything just by reading the first 10 lines. Try going through the whole tutorial section (in order) and *then* start thinking how to apply that to speed things up by running tasks in parallel.

    Also, read that blog post I liked as well (second link, after the docs one), it gives some nice detailed examples of why you wanna do things in a certain way.
    Take your time to properly digest those new concepts, you can't just smash your head into the docs and learn everything at once.
  • 0
    @endor brrooo 😵
    haven't done async/await ever before.. and not easy to understand and implement..
    also curio seems less popular than asyncIO coz i cudnt find many tuts or utube videos on it.. so.. thats how it stands right now... slow and sequencial
  • 0
    can anyone take another look at it now..
    made a few changes..
    github.com/d02d33pak/motoscript
Add Comment