i'm a python newbie & wrote my first script, can someone critique it?
Marc Brooks
marcwbrooks at gmail.com
Sat Jun 11 14:18:05 EDT 2016
Look into docstrings. They will make your code much more readable to a
Python reader.
On Sat, Jun 11, 2016 at 2:16 PM mad scientist jr <mad.scientist.jr at gmail.com>
wrote:
> For those who don't want to have to wade through comments, here is a
> version without so many comments:
>
> # For Python 3.x
> # This script creates multiple numbered empty folders
> # in the desired location. To change the folder names
> # or location, edit function get_default_options.
>
> import datetime
> import os
> import errno
> import sys
>
>
> ###############################################################################
> # EDIT VALUES HERE TO CUSTOMIZE THE OUTPUT
> def get_default_options():
> dict = {
> "s_for_python_version": "3",
> "s_folder_path_template": "C:/temp/test/MP3 Disk {count:03}",
> "i_from_count": 3,
> "i_to_count": 7,
> }
> return dict
>
> ###############################################################################
>
> def get_exact_python_version():
> s_version = ".".join(map(str, sys.version_info[:3]))
> s_version = s_version.strip()
> return s_version
>
> def get_general_python_version():
> s_version = get_exact_python_version()
> return s_version[0]
>
> def exit_if_wrong_python_version(s_right_version):
> s_current_version = get_general_python_version()
> if (s_current_version != s_right_version):
> print(
> "Wrong Python version ({}), "
> "this script should be run using "
> "Python {}.x, Exiting..."
> "".format(s_current_version, s_right_version))
> sys.exit()
>
> def get_script_filename():
> return sys.argv[0]
>
> def get_timestamp():
> return datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d
> %H:%M:%S')
>
> def create_folder(s_path):
> try:
> os.makedirs(s_path, exist_ok=True)
> except (FileExistsError, IsADirectoryError) as e:
> print("FileExistsError IN makedirs")
> raise
> return False
> except OSError as exception:
> print("ERROR #" + str(exception.errno) + "IN makedirs")
> raise
> return False
> print("" + get_timestamp() + " " + "Created folder: " + s_path + "")
>
> def create_folders(
> s_folder_path_template:str="",
> i_from_count:int=1,
> i_to_count:int=0
> ):
> i_count=0
> for i_loop in range(i_from_count, i_to_count + 1):
> create_folder(s_folder_path_template.format(count=i_loop))
> i_count += 1
>
> return i_count
>
> def main():
> options_dict = get_default_options()
> exit_if_wrong_python_version(options_dict["s_for_python_version"])
>
>
> print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
> print("" + get_timestamp() + " " + get_script_filename() + " started.")
>
> i_total_created = create_folders(
> options_dict["s_folder_path_template"],
> options_dict["i_from_count"],
> options_dict["i_to_count"])
>
> print("" + get_timestamp() + " " + str(i_total_created) + " folders
> created.")
> print("" + get_timestamp() + " " + get_script_filename() + "
> finished.")
>
> if __name__ == '__main__':
> main()
> --
> https://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list