Skip to content

Instantly share code, notes, and snippets.

@rnegron
Last active June 16, 2017 22:44
Show Gist options
  • Save rnegron/f177e05684a03cfdc96a to your computer and use it in GitHub Desktop.
Save rnegron/f177e05684a03cfdc96a to your computer and use it in GitHub Desktop.
Practicing strings and generally de-rusting Python...
# !/usr/bin/env python
from sys import argv
import string
import random
def main():
if len(argv) < 2 or len(argv) > 3:
return 'Usage: $ python {script} url opt-div\n'.format(script=argv[0])
url = argv[1]
if len(argv) == 3:
div = int(argv[2])
else:
div = 5
url_head = url[:(-1) * url[::-1].find('/', 1)]
url_tail = url.replace(url_head, "")
temp_list = []
for i in xrange(len(url_tail) // div):
temp_list.append(random.choice(string.letters + string.digits))
short_str = ''.join(elem for elem in temp_list)
return '\n=== Shortened url ===\n' + url_head + short_str + '\n'
if __name__ == "__main__":
print main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment