Skip to content

Commit

Permalink
update(python-cli): basic python cli application
Browse files Browse the repository at this point in the history
issue #104
  • Loading branch information
sabertazimi committed Dec 20, 2018
1 parent 9c55c40 commit 6187e7d
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions language/python/pythonBasicNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<!-- TOC -->

- [Python Basic Notes](#python-basic-notes)
- [CLI Application](#cli-application)
- [Basic CLI](#basic-cli)
- [Progress Bar](#progress-bar)
- [Matplotlib Usage](#matplotlib-usage)
- [Plot Type](#plot-type)
- [Basic Usage](#basic-usage)
Expand All @@ -18,6 +21,98 @@

<!-- /TOC -->

## CLI Application

### Basic CLI

```python
import click

from caesar_encryption import encrypt

@click.command()
@click.option(
'--input_file',
type=click.File('r'),
help='File in which there is the text you want to encrypt/decrypt.'
'If not provided, a prompt will allow you to type the input text.',
)
@click.option(
'--output_file',
type=click.File('w'),
help='File in which the encrypted / decrypted text will be written.'
'If not provided, the output text will just be printed.',
)
@click.option(
'--decrypt/--encrypt',
'-d/-e',
help='Whether you want to encrypt the input text or decrypt it.'
)
@click.option(
'--key',
'-k',
default=1,
help='The numeric key to use for the caesar encryption / decryption.'
)
def caesar(input_file, output_file, decrypt, key):
if input_file:
text = input_file.read()
else:
text = click.prompt('Enter a text', hide_input=not decrypt)
if decrypt:
key = -key
cyphertext = encrypt(text, key)
if output_file:
output_file.write(cyphertext)
else:
click.echo(cyphertext)

if __name__ == '__main__':
caesar()
```

### Progress Bar

```python
import click
import enchant

from tqdm import tqdm

from caesar_encryption import encrypt

@click.command()
@click.option(
'--input_file',
type=click.File('r'),
required=True,
)
@click.option(
'--output_file',
type=click.File('w'),
required=True,
)
def caesar_breaker(input_file, output_file):
cyphertext = input_file.read()
english_dictionnary = enchant.Dict("en_US")
best_number_of_english_words = 0
for key in tqdm(range(26)):
plaintext = encrypt(cyphertext, -key)
number_of_english_words = 0
for word in plaintext.split(' '):
if word and english_dictionnary.check(word):
number_of_english_words += 1
if number_of_english_words > best_number_of_english_words:
best_number_of_english_words = number_of_english_words
best_plaintext = plaintext
best_key = key
click.echo(f'The most likely encryption key is {best_key}. It gives the following plaintext:\n\n{best_plaintext[:1000]}...')
output_file.write(best_plaintext)

if __name__ == '__main__':
caesar_breaker()
```

## Matplotlib Usage

```bash
Expand Down

0 comments on commit 6187e7d

Please sign in to comment.