Skip to content

Commit

Permalink
New encoding utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Oct 25, 2019
1 parent 1a6e293 commit 64720f5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The HDX Python Utilities Library provides a range of helpful utilities:
1. [Easy logging setup](#configuring-logging)
1. [Path utilities](#path-utilities)
1. [Text processing](#text-processing)
1. [Encoding utilities](#encoding-utilities)
1. [Py3-like raise from for Py2](#raise-from)
1. [Check valid UUID](#valid-uuid)
1. [Easy building and packaging](#easy-building-and-packaging)
Expand Down Expand Up @@ -378,6 +379,15 @@ Examples:
result = get_matching_text([a, b, c], match_min_size=10)
assert result == ' brown fox over the It was so fast!'

### Encoding utilities

Examples:

## Base 64 encode and decode string
a = 'The quick brown fox jumped over the lazy dog. It was so fast!'
b = str_to_base64(a)
c = base64_to_str(b)

### Raise from

Examples:
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
basicauth==0.4.1
beautifulsoup4==4.8.1
colorlog==4.0.2
email_validator==1.0.4
email_validator==1.0.5
html5lib==1.0.1
psycopg2-binary==2.8.3
psycopg2-binary==2.8.4
pyaml==19.4.1
ratelimit==2.2.1
six==1.12.0
sshtunnel==0.1.5
tabulator==1.27.0
tabulator==1.28.0
typing==3.7.4.1
yamlloader==0.5.5
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'ratelimit',
'six>=1.12.0',
'sshtunnel',
'tabulator>=1.27.0',
'tabulator>=1.28.0',
'typing',
'yamlloader'
]
Expand Down
34 changes: 34 additions & 0 deletions src/hdx/utilities/encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
"""Encoding utilities"""

import base64


def str_to_base64(string):
# type: (str) -> str
"""
Base 64 encode string
Args:
string (str): String to encode
Returns:
str: Base 64 encoded string
"""
return base64.urlsafe_b64encode(string.encode('utf-8')).decode('utf-8')


def base64_to_str(bstring):
# type: (str) -> str
"""
Base 64 decode string
Args:
bstring (str): Base 64 encoded string to encode
Returns:
str: Decoded string
"""
return base64.urlsafe_b64decode(bstring.encode('utf-8')).decode('utf-8')
2 changes: 1 addition & 1 deletion src/hdx/utilities/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def multiple_replace(string, replacements):
# type: (str, Dict[str,str]) -> str
"""Simultaneously replace multiple strigns in a string
"""Simultaneously replace multiple strings in a string
Args:
string (str): Input string
Expand Down
2 changes: 1 addition & 1 deletion src/hdx/utilities/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8.5
1.8.6
11 changes: 11 additions & 0 deletions tests/hdx/utilities/test_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: UTF-8 -*-
"""Encoding Utility Tests"""
from hdx.utilities.encoding import str_to_base64, base64_to_str


class TestEncoding:
def test_base64(self):
string = 'hello'
result = str_to_base64(string)
assert result == 'aGVsbG8='
assert base64_to_str(result) == string

0 comments on commit 64720f5

Please sign in to comment.