Package: py-basic-commands
Py Basic CommandsA package with some basic tools and commands for Python (+3.7)Author@RasseTheBoyInstallationInstall using pippipinstallpy_basic_toolspip3installpy_baisc_toolsTable of input variablesVariableUsageTypetextThe text to be printedstrnlWhether to print a newline or notboolflushWhether to flush the output print or notbooldo_printWhether to print the text or notbooluse_end_addonWhether or not to add a colon to the end of the textboolfile_pathThe path of the file to be readstrcreateIf True, the file/directory will be created if it does not existboolforceIf True, the file/directory will be created even if it existsboolremove_emptyIf True, empty lines will be removed from listbooldoAction for directory or file'dir'/'file'ret_typeVariable type to returntyperet_varVariable to returnstrExamplesLinks to functionsfprint()finput()enter_to_continue()choose_from_list()read_file()create_file_dir()remove_file_dir()get_dir_path_for_file()join_path()try_traceback()func_timer()fprint()Customizable input function>>>fprint('Hello World')HelloWorld>>>fprint('Hello World',nl=False)HelloWorld>>>fprint()>>>fprint('Hello World',do_print=False)The above fprint() examples are very basic. But you could implement and customise it to your liking.finput()Customizable input function>>>finput('Enter a number',ret_type=int)Enteranumber:55>>>finput('Enter a number',ret_type=int)Enteranumber:5.5Couldn't return input 5.5 as <class 'int'>Inputtype:<class'str'>Returningvalueasstring'5.5'>>>finput('Enter a number',ret_type=int)Enteranumber:fiveCouldn't return input five as <class 'int'>Inputtype:<class'str'>Returningvalueasstring'five'enter_to_continue()Wait until user presses enter to continueReturns:True: No input was given, only enter was pressedFalse: Something was written before pressing enterfrompy_basic_toolsimportenter_to_continue>>>enter_to_continue()Pressentertocontinue...>>>enter_to_continue('Waiting')Waiting(entertocontinue)>>>enter_to_continue('Waiting',nl=False)Waiting(entertocontinue)>>>enter_to_continue('Waiting',use_help_text=False)Waiting>>>enter_to_continue(use_help_text=False)choose_from_list()Choose one or more variables from user given listWhen choosing more than one, add a space between chosen indexesReturns:list of variables: If variables were chosen correctlyempty list: If anexceptionoccured>>>lst=['foo','bar']>>>choose_from_list(lst)---Choose1value---(0)foo(1)barInputindex:0'foo'>>>choose_from_list(lst,choose_total=2,start_num=1)---Choose2values---(1)foo(2)barInputindex:12['foo','bar']>>>choose_from_list(lst,start_num=3,choose_until_correct=True)---Choose1value---(3)foo(4)barInputindex:0GivenindexisoutofrangeListlength:2Inputindex:0Inputindex:3foo>>>choose_from_list(lst,choose_total=2,start_num=1,choose_until_correct=False)---Choose2values---(1)foo(2)barInputindex:01GivenindexisoutofrangeValuehastobebetween:1-2[]>>>choose_from_list(lst,header_text='Choose what you will',header_nl=True,input_text='Input what you will')Choosewhatyouwill(0)foo(1)barInputwhatyouwill:0['foo']read_file()Function tries to read the file, and if it fails, it will create the file if thecreateargument is True.If the file is read successfully, the function will return a list of lines from the file>>>read_file('path/to/file.txt')Filenotfound:path/to/file.txtNone>>>read_file('path/to/file.txt',do_print=False)None>>>read_file('path/to/file.txt',create=True)Filecreated:path/to/file.txt>>>read_file('path/to/file.txt')['hello','world','foo bar']>>>read_file('path/to/file.txt',remove_empty_lines=False)['hello','world','','foo bar']create_file_dir()Creates a file or directory to the given path>>>create_file_dir('file','path/to/file.txt')Filecreated:path/to/file.txt>>>create_file_dir('file','path/to/file.txt')Filealreadyexists:path/to/file.txt>>>create_file_dir('file','path/to/file.txt',do_print=False)>>>create_file_dir('file','path/to/file.txt',force=True)Filecreated:path/to/file.txt>>>create_file_dir('dir','path/to/new directory')Directorycreated:path/to/newdirectoryremove_file_dir()Removes a file or directory to the given path>>>remove_file_dir('file','path/to/file.txt')Fileremoved:path/to/file.txt>>>remove_file_dir('file','path/to/another_file.txt')Fileisnotempty,notremoving:file.txt>>>remove_file_dir('file','path/to/another_file.txt',force=True)Fileremoved:path/to/anotherfile.txt>>>remove_file_dir('file','path/to/another_file.txt')Filenotfound:path/to/anotherfile.txt>>>remove_file_dir('dir','path/to/directory')Directorypathnotfound:path/to/directoryget_dir_path_for_file()Gets the path to the direcotry where given file isReturns:dir_path: Paht to the directoryfilename: Name of the file>>>get_dir_path_for_file('path/to/file.txt')('path/to','file.txt')>>>get_dir_path_for_file('path/to/file.txt',ret_var='filename')'file.txt'>>>get_dir_path_for_file('path/to/file.txt',ret_var='dir')'path/to'join_path()Joins given paths togetherLikeos.path.join()>>>join_path('hello','world')'hello\\world'>>>join_path('hello','world',join_with='/')'hello/world'try_traceback()Code:@try_traceback()deffoo():raiseException('foo')@try_traceback(skip_traceback=True)defbar():raiseException('bar')foo()bar()Output:Traceback(mostrecentcalllast):File"test.py",line13,inwrapperreturnfunc(*args,**kwargs)File"test.py",line8,infooraiseException('foo')bar()doesn't raise an exception, because traceback was skipped.func_timer()The decorator can be used to time any function, and it will print the time it took to run the functionReturns:ret_val: Values to return from the functiontime_delta: The time it took for the function to runCode:fromtimeimportsleep@func_timer()deffoo(sec_to_sleep):sleep(sec_to_sleep)return'Foo is done'@func_timer(ret_time=True,do_print=False)defbar(sec_to_sleep):sleep(sec_to_sleep)return'bar is done'foo(3)bar(3)Output:FunctiontimerstartedFunctionfoo(3,){}Took3.0094secondstorunFooisdone('bar is done',3.0135743618011475)