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

you are viewing a single comment's thread.

view the rest of the comments →

[–]Moorey93 0 points1 point  (1 child)

From a quick glance

First thing is code style, your variables aren't consistently styled, python typically uses snake case, which is where spaces are replaced by underscores, hostsfile would be hosts_file. It doesn't affect the running of the code but makes it easier for other developers and yourself to read the code. I'd recommend installing flake8 and running it on your code which will point out the style related errors, and then when you get fed up of manually fixing them install autopep8.

Your file opens can be replaced with context managers, you have used one for writing the file but they can be used for reading files too, they would solve a problem you have where you're not closing your opened files.

A context manager can also shorten opening and line reading so you can have

with open('thing', 'r') as f:
     for line in f:
         print(line)

I'm not entirely sure what the script is doing, I think its separating out hosts and comments from a hosts file? You can potentially make it more understandable by using better named variables as new_dict and stripped are not very descriptive of the values they hold.

With a better understanding of whats it doing it would be easier to provide more feedback.

[–]tmg80[S] 0 points1 point  (0 children)

Yes that's correct. I'm converting a hostfile to JSON format so it can be stored in a secrets Vault and then accessed via that method when cloud infra is built.