r/learnpython • • Mar 30 '21

How are you ‘planning’ your code?

There’s a thousand videos on Youtube on how to code Python - but they seem to be more like function references - which you can look up in the documentation?

How you guys and girls think through what you’re about to write?

Should I write 90 lines of code then reorganise it into functions? Or start writing functions and then finally ‘use’ the functions at the bottom of your code/script?

Is there a best practice way of doing this? I’m trying to make it easier for myself so everything works like ‘lego’ - otherwise I find I get really confused

Should I write down the steps as comments and then fill in the code after?

‘’’ getjson() Function will accept json string and return TSV def getjson():

‘’’ getinputs() function will read argv[1:] and return the first parameter, or exit with error message if none specified def getinputs():

‘’’ writetosql() function will accept TSV and write it to final.db def writetosql():

‘’’ Run the functions inputs=getinputs() tsv=getjson(inputs) writetosql(tsv)

349 Upvotes

107 comments sorted by

View all comments

69

u/IDontLikeBeingRight Mar 30 '21

Start by doing whatever workflow you want. But be aware of things that aren't going well, and what you could change, and why any particular element of any best practice might be there.

Don't expect gold from your first programs, and expect to have a lot more to learn the first time you share a project rather than just coding by yourself. Even start a project in a deliberately different way just because, to see where which things go wrong. (Uh, maybe don't collaborate for that one). Don't get attached to any particular project (also: have projects that you can avoid being attached to), learn from the mistakes & inefficiencies, and try to understand the best practices rather than simply implementing them.

4

u/brushygiraffe Mar 30 '21

There are sequences of bash commands that are running perfectly well for me:

grep -Ei (match lines - instead of “re” library) curl -s (instead of requests library) wc -l (count lines) q (query tsv with sql) jq (json to tsv)

I’m piping these into each other and I understand what the programs are all doing, and it’s easy to understand the flow

I’m trying to apply the same principles to my programming

  1. Use functions or ..

  2. make independent python scripts and make them executable with chmod +x getfile.py processjson.py etc

Not sure why I’m struggling so much organising the code so everything mentally connects easily like the command line tools

11

u/[deleted] Mar 30 '21

Based on your description it looks like you're not used to writing modular code or have less experience with python compared to bash. My suggestion is to keep doing what you are doing already. Step 1) since you seem to know bash already, write the program in bash first and run it on a small subset of your data to verify it works. 2) then try to replicate each module in python with generous commenting.

The choice to separate each function into independent python scripts depends on personal preference. In general I try to keep my scripts around 200 lines of code. If it's getting longer, I'll try to split it up into smaller files and run them as executables (much easier to troubleshoot!). Most times I can get rid of multiple lines of code by googling python libraries / functions that already do what I was trying to do using my dinky custom functions.

Last point is, perhaps try a different code editor like Jupyter? Jupyter is inherently more modular and is very useful for writing and debugging snippets of code compared to long scripts.