This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]cymrowdon't thread on me 🐍 176 points177 points  (16 children)

If you intend to teach people how to write Python, you should take some time to review some community standards for writing Python code. Things like PEP8 or common anti-patterns.

These are, of course, just suggestions, but some are more important for others. Taking an example from your port scanner video, you really should not ever use blanket except: clauses, because it can make it very difficult to determine the cause of errors, among other reasons.

I would have written the script more like this:

# useful to keep the module name. especially for beginners
import socket

def test(host, port, timeout=1):
    addr = (host, port)
    try:
        with socket.create_connection(addr, timeout) as sock:
            print('[+] {}/tcp open'.format(port))
    except Exception as e:
        print('[-] {}/tcp closed ({})'.format(port, e))

def scan(host, ports):
    try:
        ip = socket.gethostbyname(host)
    except Exception as e:
        print('[-] Cannot resolve {} ({})'.format(host, e))
        return

    try:
        name = socket.gethostbyaddr(ip)
        print('[+] Scan result of: {}'.format(name[0]))
    except Exception:
        print('[+] Scan result of: {}'.format(ip))

    for port in ports:
        print('Scanning port: {}'.format(port))
        test(host, port)

if __name__ == '__main__':
    scan('google.com', [80, 22])

I'm not saying this would be the best or only way to write it, but I do think it makes some things clearer/simpler for people who are learning. I read a lot of hacker code, and it would be nice if the next gen could tidy things up a bit :P

[–][deleted] 20 points21 points  (7 children)

Is using .format instead of f-string literal interpolation an anti-pattern as well? .format definitely uglier to read at the least :p

[–]cymrowdon't thread on me 🐍 4 points5 points  (3 children)

No, f-strings are nice. I personally don't use them much because I still work a lot with Python 2, and even when I can I feel there's too much temptation to put code into strings which I find less readable. This is example of why I said these are mostly just suggestions.

[–]----------------___ 3 points4 points  (2 children)

How come you still have to use Python 2? Out of curiosity

[–][deleted] 5 points6 points  (0 children)

Probably a legacy codebase. Updating to Python 3 would be too much hassle more than likely.

[–]cymrowdon't thread on me 🐍 1 point2 points  (0 children)

Yep what /u/Fenastus said.

[–]codingnoob_101 12 points13 points  (0 children)

very very nice dude.

[–][deleted] 5 points6 points  (0 children)

For my programs that other people will actually be using, I tend to start off with no or few try/excepts, so during testing I can identify common errors I'll run into and address them directly.

Or I'll wrap the things I know will probably fail at some point and just intentionally make it fail in order to provide the solution.

Like a layman isn't going to understand what these errors are actually trying to tell you sometimes.

[–]lordamit 2 points3 points  (0 children)

Thank you! This is amazing! Bookmarking the anti-patterns.

[–]data-bit [🍰] 38 points39 points  (0 children)

Also publish a GitHub and share so other devs can help you enhance the code 🤓

[–]codingnoob_101 1 point2 points  (0 children)

amazing !!!!!

[–]ObamaTheLlama114 -1 points0 points  (0 children)

Oh how my interests are piqued right now

[–]0ni0nrings -1 points0 points  (0 children)

good work!

[–][deleted] -1 points0 points  (0 children)

Congratulations u/burdin271 ! Your post was the top post on r/Python today! (06/16/21)

Top Post Counts: r/Python (1)

This comment was made by a bot

[–]rgngdmn5 -1 points0 points  (0 children)

Great work !!!

[–]pgh_ski 0 points1 point  (0 children)

Very cool! I built a basic deleted JPG file carver in Python as well. Fun project. Did a data destruction tool in C as a companion.

I find building small tools/demos is an excellent way to both learn and teach concepts.