r/learnpython • • Nov 05 '20

How does one learn to make well-structured programs? Mine are dumpster-fires.

So I just (kind-of) finished reading Automate The Boring Stuff With Python, and for the past month I've felt like a big-brained super-hero automating myself out of a job (Data Analyst).

The problem is, I feel I missing something along the way. I don't understand the proper way to use functions, what a class is, why people have multiple files in a program, how/why people use GitHub...probably some other things I haven't discovered yet.

They all seem to be related to organization? Is there a "How To Structure Your Python Stuff With Python" book?

478 Upvotes

98 comments sorted by

370

u/pbw Nov 05 '20

There is an art and science to programming that must be learned that’s totally independent of any specific language. It takes time and practice and study.

You need to develop an engineering aesthetic so when you look at code you immediately see how it could be improved. Then you need the skills to push it into that shape without breaking tests or functionality in general.

There are books you can read but it’s somewhat like reading a book on how to play basketball. It can give you ideas but you have to practice it yourself, figure out your own ways of looking at things and doing things.

In writing (English) they say everyone has 100,000 words of poor writing in them. So the best you can do is speed through that to get the good stuff.

78

u/TheGrapez Nov 05 '20

This is oddly inspirational thank you!

39

u/postb Nov 05 '20

Adding to this excellent answer. I really started to improve on code structure and usability once I started writing code and programs within a team. Having to adhere to standards and norms to enable co working.

27

u/Solonotix Nov 05 '20

Some tips: consider cyclomatic complexity. This is a fancy way of saying "How hard is it to understand what can happen?" There are formulas that can calculate roughly the cognitive burden of understanding code, but the simple take away is this: each if counts as 1, and every nested level adds a multiplier of 2n where N is the number of nested levels.

So code that reads if(if()else if(if()) else... would be 11 (if my math is correct). Depending on what you're writing, set a limit on this value (probably 6 to start). If you go over this, you should probably break the logic into parts, and make those parts functions, or class methods, depending on the type of work you're doing. This won't simplify the action, as these steps are still being performed, but you can at least better understand what each step does in a vacuum.

Another good practice is to start with your intended result, in the form you want it, and then work backwards until you connect that logical goal to your logical start. Working with a friend who is learning some rudimentary programming in school, she frequently struggles with visualizing the final data structure she wants, so I figured I would call that out. Doing this frequently reduces the number of intermediate steps you need to do.

6

u/IDontLikeBeingRight Nov 05 '20

You'll also improve once you have a job, because you'll be collaborating. You'll share skills & approaches & techniques with your workmates. You'll start to see why some of the "superficial" guidance really does make it demonstrably easier to work in a team. You'll hear disaster stories of what happened when someone tried a quick hack they knew was bad form, and you'll start to collect your own such stories too.

All of this is part of "experience", the nurturing of judgement to not just be able to write code, but to know which code to write.

1

u/[deleted] Nov 06 '20

Make your code public and get people to look at it. Someone will inevitably tell you what you could have done better :)

14

u/[deleted] Nov 05 '20

This is brilliantly put.

I'm no programmer, but I have an ambitious project I want to create and it's inspired me to try to learn C# (primarily due to the ease of Windows GUI design...). I'm a consummate planner, but even with this project my code is monolithic and, in a word, gross. I figure a weekend of organization could actually save me weeks, if not months, of recoding, as good organization prevents one from stepping on their own dick on the regular.

6

u/Solonotix Nov 05 '20

When working in C#, it's usually a good practice to create a class for every complex value you work with, each class should be its own file, and frequently it should have an accompanying interface in a separate namespace (sometimes it will be it's own project). What this does is separate definition (Interface) from implementation (Class), so that you can readily import the code that lets you interact, without needing the code that dictates how it all works.

From there, avoid the urge to put everything in your Program class, or Main method, and make each distinct data type a Class, and each distinct action should be a method. Any method signature that takes more than about 20 lines to spell out probably could be broken up into more manageable chunks (Note: 20 lines is not a hard rule, and instead just a soft indicator that you might be getting long-winded).

5

u/[deleted] Nov 06 '20

Pretty much the way I've decided to restructure things...Put keyboard handlers in their own class, mouse handlers in their own class, etc., etc., just to keep things clean (and portable, for future projects!).

Now if only I had time to dedicate to the project, I'd be on my way :D

1

u/cyberst0rm Nov 06 '20

consistency and reasonable expectations.

1

u/Nixellion Nov 06 '20

I would say its the same with almost everything. There are aspects of any kind of occupation thay you just cant teach/learn (effectively), it comes with experience.

3

u/pbw Nov 06 '20 edited Nov 06 '20

For sure. One of my favorite examples are the charismatic “sales gurus” who hold seminars teaching their sales techniques. Hundreds of earnest people cram into a hotel conference room to hear them speak.

But again it’s like listening to Tom Brady explain how to be a great quarterback. It might not be useless, but it’s certainly not going to bring you up to his level. Maybe it will get you 1% of the way.

I think a legit benefit of this conferences or reading software books by engineers you admire is motivating you. And giving you ideas. But then you have to take those ideas and motivation and work out your own ways of doing things with practice.

58

u/Malassi Nov 05 '20 edited Nov 05 '20

There are 2 good book you could look into but they don't use python but it doesn't really matter since it's not about the language but rather how to write good code and write good architecture.

  1. Clean code

  2. Clean architecture

They are two really good book. The first is about writing better code and the second about how to write a better architecture.

About git. Git is a versioning software. In other word it helps to keep versions of you code, work with other without having conflicts and loads more. It's essential to know how to work with git.

If you want to make your architecture better without reading a entire book about it, check design patterns. Same for classes you can see how do they work here's a tutorial that may help (there's a load more on the internet).

Hope it helps. Have a good day.

Ps. If you want you could share a project and I could check it out and see what you could do to make it better.

4

u/TheGrapez Nov 05 '20

Thank you for this - exactly what I was hoping for!

I will check those both out. I thought I would hate reading a book about programming, but I much preferred reading vs. the video course for Automate The Boring Stuff.

This gives me a great starting point as I was not even sure what to google.

This will help me for sure. I hope you have a good day as well!

13

u/arjo_reich Nov 05 '20

I think Clean Code is great recommendation. Covers programming by "side effect" in a language agnostic manner.

4

u/Malassi Nov 05 '20

Indeed and if you haven't read Clean Architecture you would love it. It's also written by Uncle bob and it's really useful.

1

u/selling_crap_bike Nov 06 '20

If pages upon pages of dense Java code is 'language agnostic' then ok

1

u/toastedstapler import itertools Nov 06 '20

most widely used languages have OO aspects to them and most principles will apply regardless. things like the single responsibility principle are always good to have

11

u/twoy519 Nov 05 '20

I was actually speaking with one of my friends in the engineering org of my company about this recently. He sent me the following list of books:

  1. The Pragmatic Programmer

  2. Test Driven Development – by Example – Kent Beck

  3. Refactoring: Improving the Design of Existing Code - Martin Fowler

  4. Growing Object-Oriented Software, Guided by Tests – Freeman and Pryce

  5. Working Effectively with Legacy Code - Michael Feathers

  6. Agile Principles, Patterns, and Practices - Robert C. Martin

  7. Refactoring to Patterns - Joshua Kerievsky

  8. Domain-driven Design: Tackling Complexity in the Heart of Software - Eric Evans

1

u/Malassi Nov 05 '20

I haven't red all of them by yes they are good books. Some may be to advance for a beginner but they are still worth the read for sure.

1

u/nulltensor Nov 05 '20

Uncle Bob always gets an upvote.

0

u/[deleted] Nov 05 '20

[deleted]

5

u/gmorf33 Nov 05 '20

His clean code lectures on youtube are what really helped me get over this hump of how to structure larger code bases and things to be thinking about while you write code.

30

u/[deleted] Nov 05 '20

No one mentioned PEP8 yet ?

Have a look, it might help you :

https://www.python.org/dev/peps/pep-0008/

2

u/TheGrapez Nov 05 '20

that's awesome thanks :)

2

u/Gaminguitarist Nov 05 '20

Yea my prof references this whenever it’s comes to script writing. Pretty useful.

2

u/teeaton Nov 06 '20

This is what helped me. I set up my editor (Atom) to run flake8 in the background and it threw up lots of errors in the code. Nothing wrong with it functionally, I'd just not written it the 'proper' way.

What I like about doing it this way is I'm almost forced to fix it as I go, which then develops into good habits. Minor example - when I add a new function, my muscle memory now puts in two blank lines between them not one.

14

u/arjo_reich Nov 05 '20 edited Nov 05 '20

You might want to branch out to C#, Java or C++ (in order of personal preference, nothing more) for more resources however that still kinda open ended...

You'll want to look into Functional Perishing to learn how to write functions without side effects and Domain Driven Design for how to representative object models.

Both are "deep water" topics so do not feel "too good for" overly simplistic primers on each for a while.

Edit: I do not ascribe to "programming is an art-form" rhetoric. It's a science, and as a science, there is always room to improve the process or the product. Thinking your code is art makes code reviews a PITA.

Edit 2: Functional Programming, not Perishing, silly autocorrect...

5

u/TheGrapez Nov 05 '20

I've been thinking about Java as my next adventure. I once attended a conference where an NBA team analyst spoke about how he used data science + java script to build really stunning visuals and allotted that to his early success.

I also took a few classes in C++ in school so am familiar with object oriented programming. Never kept it up though and did not do very well now that I think about it.

thank you :)

26

u/arjo_reich Nov 05 '20

Javascript is to Java what a Mars bar is to Mars.

8

u/TheGrapez Nov 05 '20

totally thought they were the same.

...mars like the planet, right?

9

u/arjo_reich Nov 05 '20

The first is a candy bar the other is a planet, the only thing similar is the first word. Java & Javascript should be considered similarly.

6

u/TheGrapez Nov 05 '20

interesting. I'll add this to my list of things to google today to avoid doing work.

Thank you for your help today - I was not expecting such a response.

4

u/[deleted] Nov 05 '20

There's also TypeScript, which is JS with some QoL improvements.

1

u/[deleted] Nov 06 '20

Forget about the java in javascript, it's most commonly referred to as JS nowadays anyway.

2

u/Doppelbockk Nov 05 '20

I am interested in reading more about Functional Perishing but my google fu is failing me today, can you provide an elevator pitch for it?

3

u/arjo_reich Nov 05 '20

Grr.autocorrect("Programming");

1

u/toastedstapler import itertools Nov 06 '20

1

u/Doppelbockk Nov 07 '20

Heh thanks, I figured I was just ignorant of the term perishing. :)

0

u/darthminimall Nov 05 '20

Big yikes on that preference order. C++ > Java > C#.

2

u/arjo_reich Nov 05 '20

lol, I did it just for you.

5

u/PercyJackson235 Nov 05 '20

Well, functions are, nominally, for any large enough chunk of code that you are going to reuse, either in the current program or in other program, without having to rewrite it. A class is a blueprint for creating an object that is a grouping of data and functions, that are in this instance called methods. Multiple files in a program is for logical separation. People will put data, functions, classes, etc.. that go logical together in the same file. GitHub is used for version control, collaboration, and, as a byproduct, backups. It allows you to keep a full history of a project, make and test changes without making a real impact on the project, and facilitates multiple people working on a project at once.

5

u/TheGrapez Nov 05 '20

Thank you for this explanation!

I feel like my issue with functions also comes with the fact that I'm not yet at the point where I need to worry about the structure. Most of my "programs" are just small scripts that do a single thing.

I now feel compelled to take on a larger project, so I can practice logical separation.

1

u/gmorf33 Nov 05 '20

That could be it. Your script might be small enough that it itself could be considered a function in a larger piece of code.

Functions, as a general rule, should do one thing and have little to no side effects beyond that one thing the function is doing. They are also for parts of your code that you would want to call more than once.

A couple of example functions:

A csv row formatter. It takes in a row (as a string) from a CSV file, and it returns a row with some formatting changes (as a string).

An error collector: It takes a logfile path as an argument, searches the log file for "ERROR" entries, and returns a list of strings with all the errors.

A phone number formatter: It takes phone numbers (as a string) and returns them in a normalized format (as a string).

With all of these examples, you have specific tasks each function handles with no side effects on the rest of your code. They are reusable in any part of your code base because they take arguments, which allows you to call them any time you need that functionality, without rewriting all the code. Like these phone number format calls:

format_phone_num('555.555.5555') -> '1 (555)-555-5555'
format_phone_num('5556666666') -> '1 (555)-666-6666'

You can keep calling it and passing whatever argument you want, and never have to rewrite the code that's doing the actual formatting.

5

u/Malassi Nov 05 '20

functions are, nominally, for any large enough chunk of code

The idea behind what you said is good but I don't think it's the best way to separate your code by functions. What I mean is that if you start separating you function by large chunk is not the optimal way of separating you code in functions.

The better way, at my opinion (and uncle bob's) is to first make function small as possible. They also need to this rule. FUNCTIONS SHOULD DO ONE THING. I've seen a lot of programmer separating their code by function but doing huge logic in function and having a hard time debugging their functions. Well their error lies there. When writing function the best way to do it efficiently, is to make small function that do one thing (This doesn't mean they can't use other functions).

2

u/PercyJackson235 Nov 05 '20

I said "large enough". I have created three line functions because I use said functions in a multitude of places but I am also NOT about to make every three line for loop into a function, either. A large enough chunk of code is something that is up to personal discernment.

3

u/Malassi Nov 05 '20 edited Nov 05 '20

Hi

I just wanted to clarify because I felt like it could be misunderstood by a beginner programmer. That's why I'v said that the idea behind what you said is good it was just, at my opinion, not clear enough for a beginner programmer and felt like it needed a little bit more clarification about what is a large enough chunk of code.

2

u/PercyJackson235 Nov 05 '20

That's fine. You are right.

3

u/anh86 Nov 05 '20

This is simply a sign that you need to go further with additional Python learning resources. Automate the Boring Stuff is a great book but its purpose is to help professionals (who aren't software engineers) write scripts to make them more efficient and accurate in their work. It's not meant to be an exhaustive resource on Python for someone who wants to dig deeply into application design.

If you became more efficient at automating tasks in your day job, the book accomplished its stated purpose!

1

u/TheGrapez Nov 05 '20

oh it did for sure. I especially like the pyautogui for our old legacy CRM system...

15

u/Stabilo_0 Nov 05 '20

Hi. I'll try to share my view on the answer, but its not 100% best one.

Overal structure: Check out older languages for that: c and c++, pascal.

Python can help with that too, being scripting language your program is executed line by line. Try addressing the variable before declaring it and you get error.

Ive read and wrote some considerable amounts of code and while and cant point you to specific book that defines good code structuring, almost all codepieces i've seen follow the same pattern:

divide you code into blocks,

1st contains includes\imports

2nd constants

3rd variables

4th classes and functions

5th if necessary, calling and using all those from 1 to 4.

Proper way to use functions: you use functions when you have some action be done more than one time. Function is just that, a wrapper for one or more actions that needs to be done on its own (a procedure) or that should get, process and return something like a variable.

When you write something and find out you're repeating yourself, make a function out of it, call it and let it do the work for you.

What a class is: class is a blueprint for something that can be described with code (with varying levels of abstractions pretty much anything could be). Most of the time you write a class and use objects that python creates when you call the class, instances. Other approach is to look at class as a wrapper for functions and data. Function is there when you want some action to be made again and again. Class is there when you need to represent some object and change its state with functions. Look at some standard library entrances like Time or Random. Its a class. You dont have to make a Time object all the time, you call the functions in there, so classes can be used as dictionary of functions as well, or lets better call it a 'namespace'.

Why people have multiple files in a program: the larger your project gets, the harder it is to maintain. What if you need the same piece of code in two places at once? Would you copy the code or make a separate py file, put your code there and just import the file (block 1 from overall structure part of the answer). If you happen to find a mistake in your imported file, you will now have to fix it just once instead of all those files you used it it. Divide and conquer, sort of. Also imagine if you start working with a friend and both of you add code to the same program, how would you even do that if all your code is in one file?

Speaking of that...

What is github and how people use it: first, let me say i hate it/ Look at this http://www.evan.org/Fun/Git/index.htm years go by and its the same. Instead of making some steps towards being more user friendly devs at git decided they'd better change default branch name because code is about politics now. Github is a cloud storage for your projects. You can share these projects with other people and work on them in collaboration. You can split your code into branches for testing new features and then merge these branches into main branch. It uses git program with awful controls that you just have to endure but its still very useful and really the best thing there is if you are going to work with other people on anything remotely serious. Make a habit out of it, use it as an online backup storage for your project first, get the basics.

Is there a how to organize your stuff book: sort of, https://www.python.org/dev/peps/pep-0008/

7

u/BruceJi 📦 Nov 05 '20

For a start, it gets easier with practice.

With that said...

Learning how to use functions will help you the most with that. Classes next (classes use functions so knowing functions will help you with classes!)

Here's a tip with functions:

The, er, function of functions is twofold:

First, if you know you'll need to have the same process being performed multiple times in your code, put it in a function.

Second, if you can see a process sort of sits really neatly on its own, and that if it were tucked away and in the code all you saw was the call to get the results from that part of the code, that's another thing that could become a function.

5

u/FloydATC Nov 05 '20

There's nothing wrong with "dumpster fires" of code, we all write them sometimes; huge functions with 18 indentation levels and logic that came to you in a 3AM insomnia-induced hallucination, as long as 1) it does what you want, and 2) you understand you only just got started.

Now that the code finally WORKS (sort of), it's time to change it into code you can work WITH.

Watching a few YouTube videos of Uncle Bob talking about "clean code" will get you started down the right path, and it's much more fun than it sounds. He can be a little extreme, but you don't necessarily have to take things 100% literally to reap the benefits.

4

u/Packbacka Nov 05 '20

The author of Automate has an upcoming book that talks about some of this.

1

u/excelnotfionado Nov 07 '20

Any chance you know a rough date(or month) it might be released?

2

u/Packbacka Nov 07 '20

It's called Automate the Basic Stuff with Python and should be out this month. eBook maybe is already available, though I'm not sure.

1

u/excelnotfionado Nov 07 '20

Oh dope thanks!

1

u/OnlySeesLastSentence Nov 06 '20

Wait, so data analyst jobs don't require you to be an expert programmer to begin with? I've been ignoring them because they all talk about "modeling statistical inference models" and other weird sounding stuff that I feel is beyond me.

Do I have to be good at stats class to do well? Or is it one of those jobs that are actually easy but everyone acts like it's hard to make themselves look smart?

1

u/TheGrapez Nov 06 '20

Well I'm not an expert for sure. Also I think my job was a classic case of "we're not sure what to call this job, so we're just going to use this term".

My official title is "marketing intelligence analyst". I do technically use stats, but i was never good at it. I think it all depends on the business's needs. for example i work for a marketing department, so all of my "models" are mostly profiles of clusters/groups of customers, how old they are, and how much money they make.

i'm sure i'd be better at my job if i were better at stats.

I also learned 100% of what I actually use today on the job, so my advice to you is to just shoot for whatever job you think sounds great, and just accept that you will always feel like an imposter ;)

1

u/SwizzleTizzle Nov 06 '20

Depends on the organisation but yes, many places don't actually understand what a data analyst is/does and has possibly rebranded other existing employee titles.

Case in point, my employer. The "data analysts" there are nothing more than report generators/dashboard builders. If $application's data isn't already available somewhere in an SQL database they have access to, they are stumped. No understanding of any scripting (python or otherwise) and very often do dumb things like pretending the UTC offset on a datetime doesn't matter.

1

u/OnlySeesLastSentence Nov 06 '20

Oh wow. I should start applying. Can I expect >$50,000?

1

u/SwizzleTizzle Nov 06 '20

Assuming that's USD I'd hope so. I'm in Australia, the data analyst jobs here are usually paying 70 - 90K AUD.

6

u/funkless_eck Nov 05 '20

"Why and how do people use github"

This baffled me for a long time too - then I started using it myself to teach myself. Its really opaque because of the jargon but the basic concept is this:

(I will get some things wrong - im a beginner too - looking forward to feedback on what I mucked up)

  • You have a folder on your computer like c:/users/joe/documents/code/

  • You want everyone working with you to see it and be able to comment and make suggestions on it

  • but you don't want them changing it permanently and be able to undo their changes

  • Enter github. You make a repo / repository. This is a copy of /joe/documents/code. When you make changes to it (you make the background blue) github asks you to name this ("the great blue background change") and commit to this being the latest addition.

  • then you want to push this commit -ed changes to the online copy of your code folder (your repo)

  • but Katie thinks the background should be green - so she makes a branch of the code folder, commits the green changes and pushes them online

  • you all look at the green and blue versions and decide to go with green so you merge that back into the main branch of your repo. (Main used to be called "master" but they're working to drop the slave/master connotations)

  • to carry on using the green background you pull the merged branch of the repo to your local machine. But it stays on the remote online version

9

u/anh86 Nov 05 '20

Git is version control software. It enables an individual or team to make changes to code without altering a working master version. For instance, if I want to fix a bug or add a feature, I can create a new branch off of the master to work from. Once done, I can run tests or get approval on my code before merging it into the master branch. I could even abandon that branch altogether if needed. If I want to create a new version, I could create a new branch for those changes and preserve the previous version code as it was. Git can also help identify and resolve conflicts when multiple parties make changes to the same lines of code.

Github is an online repository that works with Git technology. It provides a convenient way to share code and back it up off-site. You can clone other users' code into your own Git repositories, you can easily push up your own changes or pull down teammates' changes when coding with a team.

1

u/funkless_eck Nov 05 '20

Yes. Git and github are separate. If I understand it correctly - git is more akin to a program or programming language and github is one way to use git. Just like how you can use python on the command line, or in IDLE or in Brackets or in VSCode...

3

u/anh86 Nov 05 '20

Git is simply a technology for tracking code and placing it under version control. You can work with Git in a number of ways (command line tool, GUI tools, etc.). You never have to move any code beyond your own local storage if you don't want to.

Often, however, people want to share or backup code through some remote repository. Github is one such remote repository but you could also host your own server, use Gitlab, etc. These are simply remote repositories in which code under Git version control can be stored or shared.

3

u/Crypt0Nihilist Nov 05 '20

Something I've seen people do is start with a main file and as it gets too big an unmanageable, they push classes and functions out into modules.

3

u/Dogeek Expert - 3.9.1 Nov 05 '20

The first step you can take in improving code quality is to install a linter, such as pylint, flake8, pycodestyle or pyflakes. A linter will tell you what your formatting mistakes are. That won't help you with organisation though.

The next step you can take is to learn how to use git. Git is a very powerful tool, but many people don't use it to its full capabilities. I come accross plenty of repos on github or gitlab with only one active branch. Make several branches ! It never hurts.

Finally, how to structure your project. It varies from project to project (some frameworks have specific folder structures). The basics of it though, are :

project
│   contributing.md
│   gitignore
│   readme.md
│   setup.py
│   requirements.txt
├───docs
├───src
└───tests

The src directory can also be named after your project. It's where your code lives. The docs directory is where your documentation is. Using a documentation generator like the ever popular sphinx module helps there. Then you have the tests folder. This last folder is for unit tests, which you can pretty easily write with the unittest module of the standard library in python.

2

u/33KGB Nov 05 '20

If you PM me one of your projects I can refactor it for you. Kinda give a before & after with comments / explanations.

2

u/[deleted] Nov 05 '20

I would buy that book.

2

u/JeamBim Nov 05 '20

Go find a large project(like Flask or something) and see how they structure their code. There's no single "right" way to do it, but there are certainly wrong ways to do it.

2

u/x462 Nov 05 '20

If the fire lights and burns the right stuff in the right dumpster you’re doing ok. The rest will come in time.

1

u/Bigtbedz Nov 05 '20

Idk if anyone mentioned it but I picked up "The Pragmatic Programmer" and its shined some light on alot of things for me

1

u/fergal-dude Nov 05 '20

I have learned A LOT by watching the videos in the TalkPython series. Just one of his courses and you’ll see and hear him think through structuring code. It’s pretty great, I use a lot of what I saw there to even help with small CLI programs I write.

Check them out, buy a cheap one.

1

u/m4rx Nov 05 '20

It's mostly something that comes with time and practice. Understanding Classes / Imports / Inheritance is huge, I'd suggest reading up on creating classes as a start.

I am an experienced programmer that has moved to Python for a new position and my projects* quickly become dumpster fires as well, but the more you write the cleaner your code becomes.

*I hacked together imports so I can import the app->db to other classes, I hate the __init__.py method and would prefer to keep everything in `app.py` / `routes.py` etc. but kept getting cyclical import errors stopping the code.

I understand I should look into using Flask Blueprints but couldn't find any examples online and didn't have time during a hackaton to learn it. I found a project using __init__.py like I wanted and just copied that :X

1

u/1andonlyHoneyBadger Nov 05 '20

Jetbrains academy might help. I've been using the site to learn python.

1

u/[deleted] Nov 05 '20

Can't be taught. It's learning by doing. There are "convention" and "standards" more or less, but these don't apply to everything.

1

u/Sigg3net Nov 05 '20

You're looking for design patterns.

Object oriented design patterns are meta structures tested by time. If you know patterns, you can understand languages you haven't learned yet. (I recently watched a two hour video about patterns and it was all in java, which I don't know.)

From what little I've gathered, the structural patterns are the mist useful for python. (And the Borg pattern was designed for python.)

Check out https://python-3-patterns-idioms-test.readthedocs.io/

1

u/honk-thesou Nov 05 '20

Mine are a bit too, but I think that what helps a lot is to review them in a regular basis and reformat them as I advance with it.

This way I keep everything ordered and make it little by little.

1

u/Pastoolio91 Nov 05 '20

Check out this video on clean architecture on python. Helped me a lot.

1

u/samw1979 Nov 05 '20

O'Reilly does some good lectures (at least, sometimes they're good) with Q&As included on such things. This one is coming up on Nov. 9. If you sign up for a trial now, it'll be free to attend.

https://learning.oreilly.com/live-training/courses/python-next-steps-objects-protocols-dunders/0636920467328/

1

u/snugglyboy SkillLevels.INTERMEDIATE Nov 05 '20

Lots of great comments already. I'll just add that my wife finished Automating the Boring Stuff, and I helped her through it, and there's a lot the book doesn't cover that a normal programming book or course would. I don't blame you for feeling unclear about functions, and the book doesn't cover classes at all, which is a huge topic that can't just be explained in a few sentences. Not that it's a bad book by any means, but I think the goal of that book is to get you up and running quickly, to see the different ways you can automate things, rather than really teach you Python.

So now that you've had a solid intro to Python, you may want to find a more technical book that delves deeper into the topics. I think when you have a solid understanding of python, a lot of the structure falls into place. When I'm learning a language, I always need to read a couple books before I really feel confident with a language. I'm doing that now with C++.

That said, my process for writing a program is usually to write a huge mess of code that is embarrassingly horrible, just to get it working first. Often, in writing code, you encounter logical problems and design challenges you hadn't thought about initially. So it's my process to encounter all of that in the messy phase, and then once it's all working, I know what I'm dealing with, and can go back and clean or re-write things much more elegantly using what I've learned along the way.

1

u/L880s Nov 05 '20

Are you me?

1

u/rommon010110 Nov 05 '20

I honestly have not found a better resource to make Python "click" than the DevNet Associate OCG, I just got done with the front end 2-3 chapters of almost pure python, and coming out of it I understand creating functions / methods / using help(module) to find its functions / all that stuff. I am just trying to finish "unit testing" which is a real pain.

I have used tons of resources, Automate the Boring Stuff / CBT Nuggets / MasterClass for Python 3 on Udemy / Network Automation with David Bombal - The DevNet OCG for some reason made it all just click labbing through those chapters.

Highly recommend!

1

u/nearsingularity Nov 05 '20

Study the architecture of popular open source systems.

1

u/HellVollhart Nov 06 '20

Practice is the key. Good things often take time. Just keep on practising and be patient bud.

1

u/coder155ml Nov 06 '20

Orthogonality

1

u/TrainingShift3 Nov 06 '20

practice, practice, practice

good formatting will come naturally with time my man

1

u/[deleted] Nov 06 '20

I offer no advice; just that you aren’t alone. We’re all working to perfecting our craft. Blaze on, duder.

1

u/thrallsius Nov 06 '20

I don't understand the proper way to use functions, what a class is, why people have multiple files in a program, how/why people use GitHub...probably some other things I haven't discovered yet

you are on the right path, since you already put these things in the order that you must learn them, now just approach them one by one :)

Is there a "How To Structure Your Python Stuff With Python" book?

you probably need this https://docs.python-guide.org/

1

u/PolygonAndPixel2 Nov 06 '20 edited Nov 06 '20

You may take a look how people organize their stuff on GitHub and take a look at programs, that need to be compiled.

Most of all look for books about design patterns. That's pretty simple and easy to learn. My university recommends the following books:

Gamma, E., Helm G., Johnson R. & Vlissides J. (2002). Design Patterns - Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN: 0-201-63361-2Freeman E. & Freeman E. (2004). Head First - Design Patterns. O'Reilly. ISBN: 0-596-00712-4

The first one is by the Gang of Four and should be pretty popular.

Edit:
I disagree with any "art" speech. That just sounds like something that cannot be structured and analyzed and is somewhat magic. It isn't. Make yourself clear what your program needs, how classes might be reused and choose design patterns that seem to fit. It gets easier with experience of course.

1

u/Red0Adrenaline Nov 06 '20

PRACTICE PRACTICE PRACTICE

0

u/vanmorrison2 Nov 05 '20

I am not good at coding, but from my pow to get the code under control patterns are essentials, functions should be self explanatory by their name and short enough to do what the name suggest to do, the documentation must be very accurate as you think to the organization of the code in advance. Once you made some code that works, you must immediately look at it and think about making patterns out of it so that the code is easy to mantain and reusable. I prefer an intuitive name of functions / methods and a brief clear description to the comments that can be distracting. I prefer to cut the function / methods in their minumum functionality, instead of long ones, even if I read somewhere that the long functions are less subjected to bugs (?). So comment, generalize code, give appropriate names to functions and document every important aspect.

0

u/Cayde-6699 Nov 05 '20

GitHub is the website where programmers go to ask question like u have here on Reddit or u can go to stack overflow it’s also a website where employers will look to see if u have an account to see that u know what your doing.Functions work just the same as a variable but bigger the same way u would put x=5 in function form would be def plus(number): x+=number print(x)

So you would call this function just like the variable but filling in the number you want to add to x let’s say it’s 10 u would do

plus(10)

This would print the number 15.

Classes are a little more complicated I suggest going to w3schools website for python this teaches a lot on everything for python but u should wait until u have a good understanding of functions