Skip to content

Commit

Permalink
Refactor: Use random.choices (#33631)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored Aug 24, 2023
1 parent 66d0222 commit ba0bab0
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion airflow/auth/managers/fab/cli_commands/user_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def users_create(args):
valid_roles = appbuilder.sm.get_all_roles()
raise SystemExit(f"{args.role} is not a valid role. Valid roles are: {valid_roles}")
if args.use_random_password:
password = "".join(random.choice(string.printable) for _ in range(16))
password = "".join(random.choices(string.printable, k=16))
elif args.password:
password = args.password
else:
Expand Down
5 changes: 2 additions & 3 deletions airflow/cli/commands/standalone_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,8 @@ def initialize_database(self):
self.print_output("standalone", "Creating admin user")
role = appbuilder.sm.find_role("Admin")
assert role is not None
password = "".join(
random.choice("abcdefghkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ23456789") for i in range(16)
)
# password does not contain visually similar characters: ijlIJL1oO0
password = "".join(random.choices("abcdefghkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ23456789", k=16))
with open(password_path, "w") as file:
file.write(password)
make_group_other_inaccessible(password_path)
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/google/cloud/hooks/cloud_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ def _generate_unique_path() -> str:
random.seed()
while True:
candidate = os.path.join(
gettempdir(), "".join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8))
gettempdir(), "".join(random.choices(string.ascii_lowercase + string.digits, k=8))
)
if not os.path.exists(candidate):
return candidate
Expand Down
4 changes: 2 additions & 2 deletions airflow/providers/google/cloud/hooks/mlengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def _poll_with_exponential_delay(
log.info("Operation is done: %s", response)
return response

time.sleep((2**i) + (random.randint(0, 1000) / 1000))
time.sleep((2**i) + random.random())
except HttpError as e:
if e.resp.status != 429:
log.info("Something went wrong. Not retrying: %s", format(e))
raise
else:
time.sleep((2**i) + (random.randint(0, 1000) / 1000))
time.sleep((2**i) + random.random())

raise ValueError(f"Connection could not be established after {max_n} retries.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

def get_random_id(size=DEFAULT_ELASTICSEARCH_ID_SIZE):
"""Returns random if for elasticsearch"""
return "".join(random.choice(CHARSET_FOR_ELASTICSEARCH_ID) for _ in range(size))
return "".join(random.choices(CHARSET_FOR_ELASTICSEARCH_ID, k=size))


def query_params(*es_query_params, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/ssh/hooks/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def generate_host_key(pkey: paramiko.PKey):
TEST_CMD_TIMEOUT_NOT_SET = "NOT SET"
TEST_CMD_TIMEOUT_EXTRA = 15

PASSPHRASE = "".join(random.choice(string.ascii_letters) for i in range(10))
PASSPHRASE = "".join(random.choices(string.ascii_letters, k=10))
TEST_ENCRYPTED_PRIVATE_KEY = generate_key_string(pkey=TEST_PKEY, passphrase=PASSPHRASE)

TEST_DISABLED_ALGORITHMS = {"pubkeys": ["rsa-sha2-256", "rsa-sha2-512"]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def _producer_function():


def _generate_uuid():
letters = string.ascii_lowercase
return "".join(random.choice(letters) for i in range(6))
return "".join(random.choices(string.ascii_lowercase, k=6))


with DAG(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils/azure_system_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def prepare_share(cls, share_name: str, azure_fileshare_conn_id: str, file_name:
cls.create_directory(
share_name=share_name, azure_fileshare_conn_id=azure_fileshare_conn_id, directory=directory
)
string_data = "".join(random.choice(string.ascii_letters) for _ in range(1024))
string_data = "".join(random.choices(string.ascii_letters, k=1024))
cls.upload_file_from_string(
string_data=string_data,
share_name=share_name,
Expand Down

0 comments on commit ba0bab0

Please sign in to comment.