pymetacode.coding module

The actual code generators of the pymetacode package.

Currently, there are four types of code generators:

Each of these generators uses templates in the templates subdirectory of the pymetacode package.

class pymetacode.coding.PackageCreator

Bases: object

Generate the basic package structure.

The basic package structure follows Python best practices and has been used in a number of packages by the author of this package. In short, the modules reside in a subdirectory with the same name as the package, and parallel to that are directories for tests and documentation ( “tests”, “docs”).

Furthermore, a “setup.py” file is created to have the package installable using pip, and a license (“LICENSE”) and readme (“README.rst”) file are present.

The package version is stored in the file “VERSION”, a “.gitignore” file exists as well in the package root, and depending on the configuration, a git repository is initialised within the package root directory. In this case, a pre-commit hook is installed as well incrementing the version number for each commit, using the file “incrementVersion.sh” from the “bin” directory.

name

Name of the package to be created.

Will usually be read from the configuration.

Type

str

subdirectories

Directories and subdirectories created within the package root.

Type

list

configuration

Configuration as usually read from the configuration file.

Type

pymetacode.configuration.Configuration

documentation

Settings for creating the documentation.

Type

dict

Examples

The following examples demonstrate how to use the CLI from the terminal, rather than how to use this class programmatically.

The first step when creating a new package is to write a config file that can be filled with sensible content afterwards:

pymeta write config to mypackage_config.yaml

This would write the default configuration to “mypackage_config.yaml”. Change all values in this file according to your needs. Afterwards, you can create the basic structure of your new project:

pymeta create package from mypackage_config.yaml

Now, you have a complete package that is installable and deployable.

create(name='')

Generate the basic package structure.

Parameters

name (str) –

Name of the package to create.

If provided, this will set the name attribute of the class.

class pymetacode.coding.ModuleCreator

Bases: object

Add a module to an existing package.

Actually, adding a module consists of creating three files: the module itself, an accompanying test module, and the API documentation file. The latter gets added to the API toctree directive as well.

name

Name of the module to be created.

Type

str

configuration

Configuration as usually read from the configuration file.

Type

pymetacode.configuration.Configuration

Raises

ValueError – Raised if no name is provided

Examples

The following examples demonstrate how to use the CLI from the terminal, rather than how to use this class programmatically.

Prerequisite for using the CLI is to have the package configuration stored within the package root directory in the file “.project_configuration.yaml”. This will be the case if the package has been created by the CLI as well. Furthermore, all following commands need to be issued from within the root directory of your package.

pymeta add module mymodule

This will add a module “mymodule” to your package, together with a “test_mymodule” module in the “tests” subdirectory. And even better, the API documentation will be updated as well for you.

create(name='')

Add a module to an existing package.

Parameters

name (str) –

Name of the module to add.

If provided, this will set the name attribute of the class.

class pymetacode.coding.ClassCreator

Bases: object

Add a class to an existing module, including a test class.

When a class is added to a module, it will always be added to the bottom of the module file, and at the same time a test class with a very basic setup and a first test will be added to the corresponding test module.

name

Name of the class to be added to the module

Type

str

module

Name of the module the class should be added to

Type

str

configuration

Configuration as usually read from the configuration file.

Type

pymetacode.configuration.Configuration

Raises

ValueError – Raised if no class or module name is provided

Examples

The following examples demonstrate how to use the CLI from the terminal, rather than how to use this class programmatically.

Prerequisite for using the CLI is to have the package configuration stored within the package root directory in the file “.project_configuration.yaml”. This will be the case if the package has been created by the CLI as well. Furthermore, all following commands need to be issued from within the root directory of your package.

pymeta add class MyClass to mymodule

This will add the class “MyClass” to the module “mymodule”, together with a test class in the “test_mymodule” module. The class will come with a basic docstring, and the test class with a minimalistic setup and first test (for implementation of the class) that gets you started with writing further tests.

create(name='', module='')

Create actual code stub for the class.

Parameters
  • name (str) – Name of the function to be created

  • module (str) – Name of the module the function should be added to

Raises

ValueError – Raised if function or module name is missing

class pymetacode.coding.FunctionCreator

Bases: object

Add a function to an existing module, including a test class.

When a function is added to a module, it will always be added to the bottom of the module file, and at the same time a test class with a first test will be added to the corresponding test module.

name

Name of the function to be added to the module

Type

str

module

Name of the module the function should be added to

Type

str

configuration

Configuration as usually read from the configuration file.

Type

pymetacode.configuration.Configuration

Raises

ValueError – Raised if no function or module name is provided

Examples

The following examples demonstrate how to use the CLI from the terminal, rather than how to use this class programmatically.

Prerequisite for using the CLI is to have the package configuration stored within the package root directory in the file “.project_configuration.yaml”. This will be the case if the package has been created by the CLI as well. Furthermore, all following commands need to be issued from within the root directory of your package.

pymeta add function my_function to mymodule

This will add the function “my_function” to the module “mymodule”, together with a test class in the “test_mymodule” module. The function will come with a basic docstring, and the test class with a minimalistic first test that gets you started with writing further tests.

create(name='', module='')

Create actual code stub for the function.

Parameters
  • name (str) – Name of the function to be created

  • module (str) – Name of the module the function should be added to

Raises

ValueError – Raised if function or module name is missing