Question: # 13.2 Creating, then accessing, a package that uses `__init__.py` to predefine a variable. # supporting constants CREATE_AS_NEW_FILE = 'x' # file access mode for

# 13.2 Creating, then accessing, a package that uses `__init__.py` to predefine a variable.

# supporting constants CREATE_AS_NEW_FILE = 'x' # file access mode for open()

# library resources import os, sys, importlib, shutil

# supporting functions make_printable = lambda exception: '' if str(exception) is None else str(exception)

# precondition check: ensure that we're creating a totally new, unreferenced module in a totally new directory

package_name = 'my_package' corrective_action = f'please ensure that {package_name} names a non-existent file system object' assert not os.path.exists( package_name ), corrective_action

package_init_name = '__init__' package_init_path= f'{package_name}/{package_init_name}.py' package_module_name = 'my_module' package_module_path= f'{package_name}/{package_module_name}.py' package_var = 'a' module_fn = 'sample_function'

importlib.invalidate_caches() # clear stale module data, possibly from previous exercise

# preconditions established: create a package in the local directory, then try to import and use it

try: # create the package directory # os.mkdir( package_name ) try: # create and configure the __init__ module # package_init_fd = open( package_init_path, CREATE_AS_NEW_FILE ) package_init_fd.write( package_var + ' = 3' + ' ' ) package_init_fd.close() # define the package's lone function # function_header = f'def {module_fn}( ):' return_message = f'{module_fn} in {package_module_path}: {package_var} is ' function_body = "return f'" + return_message + "'" + ' + ' + 'str(' + f'{package_var}' + ')' try: # create the function's lone module # package_module_fd = open( package_module_path, CREATE_AS_NEW_FILE ) package_module_fd.write( function_header + ' ' ) package_module_fd.write( ' ' + 'from . import ' + package_var + ' ' ) package_module_fd.write( ' ' ) package_module_fd.write( function_body ) package_module_fd.write( ' ' ) package_module_fd.close() try: # (re)import the module and execute the function # exec( f'import {package_name}' ) exec( f'importlib.reload( {package_name} )' ) exec( f'import {package_name}.{package_module_name}' ) exec( f'importlib.reload( {package_name}.{package_module_name} )' ) try: result_from_sample_function = eval( f'{package_name}.{package_module_name}.{module_fn}( )' ) print( result_from_sample_function ) except Exception as exception: print( f"can't access {package_name}.{package_module_name}.{module_fn}: {make_printable(exception)}" ) except Exception as exception: print( f"can't import {package_name}: {make_printable(exception)}" ) except Exception as exception: print( f"can't create file ({package_module_path}): {make_printable(exception)}" ) except Exception as exception: print( f"can't create file ({package_init_path}): {make_printable(exception)}" ) finally: try: shutil.rmtree( package_name ) except Exception as exception: print( f"can't remove directory ({package_name}): {make_printable(exception)}" ) except Exception as exception: print( f"can't create directory ({package_name}): {make_printable(exception)}" )

(language python)

# 13.2 Creating, then accessing, a package that uses `__init__.py` to predefine

In the following code cell, modify the previous example so that sample_function takes one parameter, an int, computes the sum of this value and a; and returns the result. Illustrate the code's operation by invoking it at least twice with nonzero arguments and printing the return values. : In the following code cell, modify the previous example so that sample_function takes one parameter, an int, computes the sum of this value and a; and returns the result. Illustrate the code's operation by invoking it at least twice with nonzero arguments and printing the return values

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!