Skip to content

Commit

Permalink
Replace strftime with f-strings where nicer (#33455)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Aug 23, 2023
1 parent 4037d79 commit 94f70d8
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 29 deletions.
2 changes: 1 addition & 1 deletion airflow/models/taskinstance.py
Expand Up @@ -2139,7 +2139,7 @@ def get_prev_ds() -> str | None:
execution_date = get_prev_execution_date()
if execution_date is None:
return None
return execution_date.strftime(r"%Y-%m-%d")
return execution_date.strftime("%Y-%m-%d")

def get_prev_ds_nodash() -> str | None:
prev_ds = get_prev_ds()
Expand Down
6 changes: 2 additions & 4 deletions airflow/providers/amazon/aws/hooks/sagemaker.py
Expand Up @@ -128,10 +128,8 @@ def secondary_training_status_message(
status_strs = []
for transition in transitions_to_print:
message = transition["StatusMessage"]
time_str = timezone.convert_to_utc(cast(datetime, job_description["LastModifiedTime"])).strftime(
"%Y-%m-%d %H:%M:%S"
)
status_strs.append(f"{time_str} {transition['Status']} - {message}")
time_utc = timezone.convert_to_utc(cast(datetime, job_description["LastModifiedTime"]))
status_strs.append(f"{time_utc:%Y-%m-%d %H:%M:%S} {transition['Status']} - {message}")

return "\n".join(status_strs)

Expand Down
6 changes: 1 addition & 5 deletions airflow/providers/elasticsearch/log/es_json_formatter.py
Expand Up @@ -31,11 +31,7 @@ class ElasticsearchJSONFormatter(JSONFormatter):
def formatTime(self, record, datefmt=None):
"""Return the creation time of the LogRecord in ISO 8601 date/time format in the local time zone."""
dt = pendulum.from_timestamp(record.created, tz=pendulum.local_timezone())
if datefmt:
s = dt.strftime(datefmt)
else:
s = dt.strftime(self.default_time_format)

s = dt.strftime(datefmt or self.default_time_format)
if self.default_msec_format:
s = self.default_msec_format % (s, record.msecs)
if self.default_tz_format:
Expand Down
3 changes: 1 addition & 2 deletions airflow/providers/google/cloud/operators/dataproc.py
Expand Up @@ -1578,8 +1578,7 @@ class DataprocSubmitPySparkJobOperator(DataprocJobBaseOperator):

@staticmethod
def _generate_temp_filename(filename):
date = time.strftime("%Y%m%d%H%M%S")
return f"{date}_{str(uuid.uuid4())[:8]}_{ntpath.basename(filename)}"
return f"{time:%Y%m%d%H%M%S}_{str(uuid.uuid4())[:8]}_{ntpath.basename(filename)}"

def _upload_file_temp(self, bucket, local_file):
"""Upload a local file to a Google Cloud Storage bucket."""
Expand Down
4 changes: 1 addition & 3 deletions airflow/providers/oracle/hooks/oracle.py
Expand Up @@ -302,9 +302,7 @@ def insert_rows(
elif numpy and isinstance(cell, numpy.datetime64):
lst.append("'" + str(cell) + "'")
elif isinstance(cell, datetime):
lst.append(
"to_date('" + cell.strftime("%Y-%m-%d %H:%M:%S") + "','YYYY-MM-DD HH24:MI:SS')"
)
lst.append(f"to_date('{cell:%Y-%m-%d %H:%M:%S}','YYYY-MM-DD HH24:MI:SS')")
else:
lst.append(str(cell))
values = tuple(lst)
Expand Down
2 changes: 1 addition & 1 deletion airflow/task/task_runner/cgroup_task_runner.py
Expand Up @@ -134,7 +134,7 @@ def start(self):
return

# Create a unique cgroup name
cgroup_name = f"airflow/{datetime.datetime.utcnow().strftime('%Y-%m-%d')}/{str(uuid.uuid4())}"
cgroup_name = f"airflow/{datetime.datetime.utcnow():%Y-%m-%d}/{uuid.uuid4()}"

self.mem_cgroup_name = f"memory/{cgroup_name}"
self.cpu_cgroup_name = f"cpu/{cgroup_name}"
Expand Down
6 changes: 1 addition & 5 deletions airflow/utils/log/timezone_aware.py
Expand Up @@ -40,11 +40,7 @@ def formatTime(self, record, datefmt=None):
date and time format in the local time zone.
"""
dt = pendulum.from_timestamp(record.created, tz=pendulum.local_timezone())
if datefmt:
s = dt.strftime(datefmt)
else:
s = dt.strftime(self.default_time_format)

s = dt.strftime(datefmt or self.default_time_format)
if self.default_msec_format:
s = self.default_msec_format % (s, record.msecs)
if self.default_tz_format:
Expand Down
Expand Up @@ -1203,7 +1203,7 @@ class ProviderPRInfo(NamedTuple):
get_console().print()
get_console().print(
"Issue title: [yellow]Status of testing Providers that were "
f"prepared on {datetime.now().strftime('%B %d, %Y')}[/]"
f"prepared on {datetime.now():%B %d, %Y}[/]"
)
get_console().print()
syntax = Syntax(issue_content, "markdown", theme="ansi_dark")
Expand Down
2 changes: 1 addition & 1 deletion tests/dags_corrupted/test_impersonation_custom.py
Expand Up @@ -40,7 +40,7 @@

def print_today():
date_time = FakeDatetime.utcnow()
print(f"Today is {date_time.strftime('%Y-%m-%d')}")
print(f"Today is {date_time:%Y-%m-%d}")


def check_hive_conf():
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/amazon/aws/utils/eks_test_utils.py
Expand Up @@ -235,7 +235,7 @@ def convert_keys(original: dict) -> dict:


def iso_date(input_datetime: datetime.datetime) -> str:
return input_datetime.strftime("%Y-%m-%dT%H:%M:%S") + "Z"
return f"{input_datetime:%Y-%m-%dT%H:%M:%S}Z"


def generate_dict(prefix, count) -> dict:
Expand Down
4 changes: 2 additions & 2 deletions tests/providers/apache/kylin/operators/test_kylin_cube.py
Expand Up @@ -37,8 +37,8 @@ class TestKylinCubeOperator:
"project": "learn_kylin",
"cube": "kylin_sales_cube",
"command": "build",
"start_time": datetime(2012, 1, 2, 0, 0).strftime("%s") + "000",
"end_time": datetime(2012, 1, 3, 0, 0).strftime("%s") + "000",
"start_time": str(int(datetime(2012, 1, 2, 0, 0).timestamp() * 1000)),
"end_time": str(int(datetime(2012, 1, 3, 0, 0).timestamp() * 1000)),
}
cube_command = [
"fullbuild",
Expand Down
4 changes: 1 addition & 3 deletions tests/providers/http/sensors/test_http.py
Expand Up @@ -244,9 +244,7 @@ def test_sensor(self):
endpoint="/search",
request_params={"client": "ubuntu", "q": "airflow", "date": "{{ds}}"},
headers={},
response_check=lambda response: (
"apache/airflow/" + DEFAULT_DATE.strftime("%Y-%m-%d") in response.text
),
response_check=lambda response: f"apache/airflow/{DEFAULT_DATE:%Y-%m-%d}" in response.text,
poke_interval=5,
timeout=15,
dag=self.dag,
Expand Down

0 comments on commit 94f70d8

Please sign in to comment.