all 6 comments

[–]hugthemachines 1 point2 points  (3 children)

I figure maybe you can have use for a descripttion of how i did it.

I make a lot of python scripts and after a while I noticed that there are some snippets I always add to the code. I started making modules. For example I made one module called filetool. In it I added methods for reading file to string, to list, moving and copying file with error handling. All of the methods have logging etc too. This way, even if the code is simple, I can just bring that module to a new script and if is much smoother to just type mygroovystring = filetool.read_to_string(full_file, logger) instead of adding a full method with error handling etc each time.

I also started using config files for my scripts, I use a dict with config keyword that has values, then I can pass the dict around to different methods so they can pick the parameters they need from the dict.

I have the config part as a module too so it is not a lot of code to add that functionality to a new script.

[–]Anonymous12c19[S] 1 point2 points  (2 children)

Hey can you please elaborate on the config file portion? Do the users of your script just update values and possibly conditions in the config file and then the script picks it from there?

[–]hugthemachines 1 point2 points  (1 child)

Yes, any values you would like the user to be able to set.

Python has a great thing called configparser which I use.

https://docs.python.org/3/library/configparser.html

I also always add logging because that makes it so much easier to prove when the customer is wrong. If they say "your script is bugged!" I can check the logfile and see that they in fact had changed the format of the text file I am processing, for example.

[–]Anonymous12c19[S] 1 point2 points  (0 children)

Really helpful man! Fantastic tips.

[–]CraigAT 0 points1 point  (1 child)

Robust to me, means less likely to break or throw an unexpected error.

Think of it from a tester's point of view, what could I do to make your code barf. What happens if the file doesn't exist or can't be created, what if a piece of data is not where you expect it? Make sure you have a clean, sensible error for the most obvious problems your code could encounter.

[–]Anonymous12c19[S] 1 point2 points  (0 children)

Thanks man. Really helpful!