Skip to main content

Python packages

Package Module Structure in Python Programming


A python package is a collection of python modules. These modules are python scripts with *.py file extension. Typically, these scripts will contain python functions, classes, custom data types etc.


Looking at the figure above, the Game package consist of an _init_.py file together with 3 other sub-packages Sound, Image and Level respectively.
The _init_.py file must be included inside a directory for it to be considered a python package.In addition, the directory must be defined inside sys.path.

Example usage:

1. import Game.Level.Start
  • Suppose the start.py module consists of a function called mince()
  • Inside your python application, you would need to call it as follows
2. Game.Level.Start.mince(<input_params>)
  • Alternatively, you can reference it as follows
3. from Game. Level import Start     // recommended approach
4. Start.mince(<input_params>)
  • A less common method is calling the function as if it was defined inside your current script
5. from Game.Level.Start import mince       //not recommended 
6. mince(<input_params>)

Comments

Popular posts from this blog

Gunicorn vs NGINX Explained

Web applications typically disseminate information via three server layers: Web server   - First layer: Receives request from client(web browser) Application server - 2nd layer: Receives requests from web server if the client is requesting dynamic content Database - 3rd layer: Receives database queries from web framework's request handler methods In this example , nginx is the web server, gunicorn is the application server (interface between nginx and web framwork request handling function) and the database can be assumed to be a lightweight sqlite3 database. Example: Django architecture Alternatives to nginx : Cherokee Apache HTTP server Alternatives for gunicorn: Apache Mongoose

How to transfer a gitlab repository into github

Method 1: Use the linux command line 1. Assume you have a gitlab repository called matric2016.git 2. Create a new working directory: $ mkdir myproj && cd myproj $ git clone gitlab@gitlab.com/Banzyme2/matric2016.git $ cd matric2016.git Make sure you create a github repository with the same name as the gitlab repository ,i.e. matric2016  3. Clone your project into github as follows:  $ git remote add github https://github.com/Banzyme/matric2016.git  $  git push --mirror github Method 2: Using the github dashboard repository import 1. Click "+" next to your github profile. Select import repository 2. Fill out the import form  as follows