Skip to main content
deleted 2 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

How can my generate Generating a unique key method be more Pythonic?

How can my generate Generating a unique key method be more Pythonic?

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join() works.

While this works, how can this function be more Pythonic?

How can my generate unique key method be more Pythonic?

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join() works.

While this works, how can this function be more Pythonic?

Generating a unique key

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join() works.

While this works, how can this function be more Pythonic?

Copy edited (e.g. ref. <https://en.wiktionary.org/wiki/Pythonic#Adjective>, <http://en.wikipedia.org/wiki/Python_%28programming_language%29>, ).
Source Link
Stephen Rauch
  • 4.3k
  • 12
  • 24
  • 36

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how joinjoin() works.

While this works, how can this function be more Pythonic?

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join works.

While this works, how can this function be more Pythonic?

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join() works.

While this works, how can this function be more Pythonic?

Copy edited (e.g. ref. <https://en.wiktionary.org/wiki/Pythonic#Adjective>, <http://en.wikipedia.org/wiki/Python_%28programming_language%29>, ).
Source Link

How can my generate unique key method be more pythonicPythonic?

I am coming from a Ruby background and I am learning pythonPython. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join works.

While this works, how can this function be more pythonic Pythonic?

How can my generate unique key method be more pythonic?

I am coming from a Ruby background and I am learning python. This is a method I've created to generate a URL safe unique key

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string but this I believe this is how join works.

While this works, how can this function be more pythonic ?

How can my generate unique key method be more Pythonic?

I am coming from a Ruby background and I am learning Python. This is a method I've created to generate a URL safe unique key:

import random;

def generate_unique_key():
    array = []
    for letter in range(97,123):
        array.append(chr(letter))
    for letter in range(65,91):
        array.append(chr(letter))
    for number in range(0,10):
        array.append(number)
    for char in ["-", ".", "_", "~"]:
        array.append(char)

    random_values = random.sample(array, 15)
    random_values = map(lambda x: str(x), random_values)
    return "".join(random_values)

print(generate_unique_key())

Coming from a Ruby background I was certainly puzzled first at not being able call join directly on my list, but having to call it from a string instance.

Also in Ruby I would have written something similar to random.sample(array, 15).join("") directly without having to convert them all to string, but this I believe this is how join works.

While this works, how can this function be more Pythonic?

Tweeted twitter.com/StackCodeReview/status/1022587618297618436
added 1 character in body; edited tags
Source Link
Loading
edited tags
Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158
Loading
Source Link
Loading