Python Packages and Data Access Questions and Answers 2022

Python Packages and Data Access


What is Python Module

A python module is a Python object with arbitrarily named attributes that enable to organize code in a logical manner. In simple words, a module is a file which consists of Python code. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.

Learn simple module, calc.py in Python

def add(x, y):
return (x+y)

def subtract(x, y):
return (x-y)

Import Module in Python – Import statement
We can import the functions, classes defined in a module to another module using the import statement in some other Python source file.

Syntax:

import module
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches for importing a module. For example, to import the module calc.py, we need to put the following command at the top of the script.

There are three types of modules:

A module written in Python.

A module written in “C” programming language, and which can be loaded at run-time (dynamically).

A built-in module present in the interpreter.

python package and modules

Locating Python Modules


Whenever a module is imported in Python the interpreter looks for several locations. First, it will check for the built-in module, if not found then it looks for a list of directories defined in the sys.path. Python interpreter searches for the module in the following manner –

First, it searches for the module in the current directory.
If the module isn’t found in the current directory, Python then searches each directory in the shell variable PYTHONPATH. The PYTHONPATH is an environment variable, consisting of a list of directories.
If that also fails python checks the installation-dependent list of directories configured at the time Python is installed.

Python Package

python packages

Suppose you have developed a very large application that includes many modules. As the number of modules grows, it becomes difficult to keep track of them all if they are dumped into one location. This is particularly so if they have similar names or functionality. You might wish for a means of grouping and organizing them.

Packages allow for a hierarchical structuring of the module namespace using dot notation. In the same way that modules help avoid collisions between global variable names, packages help avoid collisions between module names.

Here, there is a directory named pkg that contains two modules, mod1.py and mod2.py. The contents of the modules are:

mod1.py

def foo():
print(‘[mod1] foo()’)

class Foo:
pass
mod2.py

def bar():
print(‘[mod2] bar()’)

class Bar:
pass
Given this structure, if the pkg directory resides in a location where it can be found (in one of the directories contained in sys.path), you can refer to the two modules with dot notation (pkg.mod1, pkg.mod2) and import them with the syntax you are already familiar with:

import [, …]

import pkg.mod1, pkg.mod2
pkg.mod1.foo()
[mod1] foo()
x = pkg.mod2.Bar()
x

from import

from pkg.mod1 import foo
foo()
[mod1] foo()

from import as

from pkg.mod2 import Bar as Qux
x = Qux()
x

You can import modules with these statements as well:

from import [, …]
from import as

from pkg import mod1
mod1.foo()
[mod1] foo()

from pkg import mod2 as quux
quux.bar()
[mod2] bar()
You can technically import the package as well:

import pkg
pkg

But this is of little avail. Though this is, strictly speaking, a syntactically correct Python statement, it doesn’t do much of anything useful. In particular, it does not place any of the modules in pkg into the local namespace:

pkg.mod1
Traceback (most recent call last):
File “”, line 1, in
pkg.mod1
AttributeError: module ‘pkg’ has no attribute ‘mod1’
pkg.mod1.foo()
Traceback (most recent call last):
File “”, line 1, in
pkg.mod1.foo()
AttributeError: module ‘pkg’ has no attribute ‘mod1’
pkg.mod2.Bar()
Traceback (most recent call last):
File “”, line 1, in
pkg.mod2.Bar()
AttributeError: module ‘pkg’ has no attribute ‘mod2’
To actually import the modules or their contents, you need to use one of the forms shown above.

Package Initialization
If a file named init.py is present in a package directory, it is invoked when the package or a module in the package is imported. This can be used for execution of package initialization code, such as initialization of package-level data.

For example, consider the following init.py file:

init.py

print(f’Invoking init.py for {name}’)
A = [‘quux’, ‘corge’, ‘grault’]
Let’s add this file to the pkg directory from the above example:

Illustration of hierarchical file structure of Python packages
Now when the package is imported, the global list A is initialized:

import pkg
Invoking init.py for pkg
pkg.A
[‘quux’, ‘corge’, ‘grault’]

A package is a collection of modules. In other words, it is a directory of Python modules.

The packages discussed in this course are:

Built-in
Regular Expression
Third-party
Serialization and De-serialization

A Python interpreter has many built-in functions like print(), int(), list(), input(), set(), etc. It also contains many pre-defined functions as packages. The built-in modules are written in C, and is integrated with the interpreter. There are also standard libraries that contain Python scripts (.py extension).

Some of the built-in packages are:

  • os
  • sys
  • shutils
  • datetime
  • collections
  • itertools

Python Packages and Data Access Questions MCQs Final Assessment

1.How many components are present in SQLAlchemy?
Find the correct answer from given options:
a) 2
b) 5
c) 3
d) 1
e) 4

Correct answer : a)2

2.Which of the following identifier is used to identify whitespace around words in regular expressions?
Find the correct answer from given options:
a) \w
b) \b
c) \D
d) \s
e) \S

Correct answer : a)\w

3.Which of the following package contains permutations?
Find the correct answer from given options:
a) datetime
b) sys
c) collections
d) shutils
e) itertools

Answer: 5)itertools

4.Safe pickling is used because, by default, a class or function present in pickle data is imported.
Find the correct answer from given options:
a) FALSE
b) TRUE

Correct answer : b)TRUE

5.Which of the following analyses only the beginning of a string?
Find the correct answer from given options:
a) re.search
b) re.findall
c) re.compile
d) re.match
e) None of the options

Correct answer : d)re.match

6.If you copy a file from one folder to another with the same file name using shutil, the file will be overwritten.
Find the correct answer from given options:
a) TRUE
b) FALSE

Correct answer : a)TRUE

7.Which of the following specifies the number of connections in a connection pool?
Find the correct answer from given options:
a) pool_size
b) encoding
c) isolation_level
d) max_overflow
e) echo

Correct answer : a)pool_size

8.Which of the following is used to load a file?
a) Find the correct answer from given options:
b) json.dumps
c) json.loads
d) None of the options
e) json.load

Correct answer : d)json.load

9.Which of the following is used to get named entries into a tuple?
Find the correct answer from given options:
a) deque
b) counter
c) OrderedDict
d) namedtuple
e) chainmap

Correct answer : d)namedtuple

10.In SQLAlchemy Raw SQL, CRUD operations are performed by using?
Find the correct answer from given options:
a) Raw statements
b) None of the options
c) SQL syntax
d) Python statements

Correct answer : b)None of the options

11.Which of the following parameter is not present in datetime.time?
Find the correct answer from given options:
a) Second
b) Hour
c) Microsecond
d) Millisecond
e) Minute

Correct answer : d)Millisecond

12.Which of the following enables “.” that matches newline?
Find the correct answer from given options:
a) re.S
b) re.M
c) re.I
d) re.U
re.L

Correct answer : a)re.S

13.Which of the following is used to request data from a server?
Find the correct answer from given options:
a) Cookies
b) Session objects
c) Headers
d) GET
e) POST

Correct answer : d)GET

14.You can overwrite a file despite moving a file from one folder to another.
Find the correct answer from given options:
a) FALSE
b) TRUE

Correct answer : b)TRUE

15.Which of the following command is used to combine both date and time?
Find the correct answer from given options:
a) datetime.date.combine
b) combine
c) date
d) datetime.datetime.combine
e) time

Correct answer : d)datetime.datetime.combine

16.Which of the following command is used to change the location of a file from one folder to another?
Find the correct answer from given options:
a) shutil.copy
b) shutil.overwrite
c) None of the options
d) shutil.move

Correct answer : d)shutil.move

17.Which of the following enables to rename a directory in Python packages?
Find the correct answer from given options:
a) getcwd
b) mkdir
c) rename
d) rmdir
e) os.rename

Answer: 5)os.rename

18.Psycopg2 and pg80000 are the drivers for __.
Find the correct answer from given options:
a) MySQL
b) None of the options
c) MSSQL
d) PostgreSQL

Correct answer : d)PostgreSQL

19.Which of the following command is used to print error text?
Find the correct answer from given options:
a) None of the options
b) sys.stderr
c) stderr
d) sys.stdout
e) sys.argv

Correct answer : d)sys.stdout

20.Which of the following is used to pass arguments to a Python file by using command line?
Find the correct answer from given options:
a) os.mkdir
b) os.getcwd
c) sys.argv
d) sys.stderr
e) os.rename

Correct answer : c)sys.argv

21.Which of the following provides a pipe to access a file directly?
Find the correct answer from given options:
a) os.error
b) os.popen
c) os.close
d) os.name

Correct answer : b)os.popen

22.What are the major benefits of using SQLAlchemy?
Find the correct answer from given options:
a) Simple logic
b) Cleaner code
c) All the options
d) Secure code

Correct answer : c)All the options

23.Which of the following is considered an optimized list?
Find the correct answer from given options:
a) OrderedDict
b) namedtuple
c) counter
d) deque
e) chainmap

Correct answer : d)deque

24.Pickle cannot store class objects.
Find the correct answer from given options:
a) FALSE
b) TRUE

Correct answer : a)FALSE

25.os.mkdir enables to create a new file.
Find the correct answer from given options:
a) TRUE
b) FALSE

Correct answer : b)FALSE

26.In SQLAlchemy, performing operations using Raw SQL is easier in python packages
Find the correct answer from given options:
a) TRUE
b) FALSE

Correct answer : b)FALSE

About Author


After years of Technical Work, I feel like an expert when it comes to Develop wordpress website. Check out How to Create a Wordpress Website in 5 Mins, and Earn Money Online Follow me on Facebook for all the latest updates.