Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emrcontainers/base36.py
1567 views
1
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
15
class Base36(object):
16
def str_to_int(self, request):
17
"""Method to convert given string into decimal representation"""
18
result = 0
19
for char in request:
20
result = result * 256 + ord(char)
21
22
return result
23
24
def encode(self, request):
25
"""Method to return base36 encoded form of the input string"""
26
decimal_number = self.str_to_int(str(request))
27
alphabet, base36 = ['0123456789abcdefghijklmnopqrstuvwxyz', '']
28
29
while decimal_number:
30
decimal_number, i = divmod(decimal_number, 36)
31
base36 = alphabet[i] + base36
32
33
return base36 or alphabet[0]
34
35