4.8 KiB
Getting started with CPL
Welcome to CPL!
This tutorial introduces you to the essentials of the CPL package by walking through building an console based app.
Table of Contents
Prerequisites
To get most out of this tutorial you should already have a basic understanding of the following.
Also you need to have the following installed.
Create the project
To create the sample project to the following:
-
Open a terminal
-
Run
cpl new console sample-project
-
We don't want to use Application base or Dependency injection:
Do you want to use application base? (y/n) n Do you want to use service providing? (y/n) n
The ouput should look like this:
Creating sample-project/LICENSE done Creating sample-project/README.md done Creating sample-project/src/tests/__init__.py done Creating sample-project/appsettings.json done Creating sample-project/src/sample-project/__init__.py done Creating sample-project/src/sample-projectmain.py done
-
Open the project with an IDE like VS Code or PyCharm
-
Set the 'src' directory as source folder
-
Run the application
You should see an output like:
Hello World
-
In PyCharm you have to enable
Emulate terminal in output console
!
Create a menu
-
Open the 'sample-project/main.py'
-
Change 'Hello World' to 'Password generator:' in line 5
-
Add
Console.select
as follows:from cpl.console import Console def main(): Console.write_line('Password generator:') options = [ 'Only lower case', 'Only upper case', 'Lower and upper case' 'Exit' ] option = Console.select('>', 'Select option: ', options) if __name__ == '__main__': main()
-
Get Id of the selected option:
index = options.index(option)
-
Create the generate function as follows:
def generate_password(letters: str, length: int) -> str: return ''.join(random.choice(letters) for i in range(length))
Add imports:
import random import string
-
Get length of the password:
length = int(Console.read('Length: '))
-
Validate the selected option:
index = options.index(option) letters = '' if index == 0: letters = string.ascii_lowercase elif index == 1: letters = string.ascii_uppercase elif index == 2: letters = string.ascii_letters elif index == len(options) - 1: exit()
-
Print the generated password:
Console.write_line('Password: ', generate_password(letters, length))
-
The full 'sample-project/main.py':
import random import string from cpl.console import Console def generate_password(letters: str, length: int) -> str: return ''.join(random.choice(letters) for i in range(length)) def main(): Console.write_line('Password generator:') options = [ 'Only lower case', 'Only upper case', 'Lower and upper case', 'Exit' ] option = Console.select('>', 'Select option: ', options) length = int(Console.read('Length: ')) index = options.index(option) letters = '' if index == 0: letters = string.ascii_lowercase elif index == 1: letters = string.ascii_uppercase elif index == 2: letters = string.ascii_letters elif index == len(options) - 1: exit() Console.write_line('Password: ', generate_password(letters, length)) if __name__ == '__main__': main()
What's next
In this section, you've created an application that uses user input to generate a password.
To continue exploring CPL and developing applications: