Skip to content

Commit 5186479

Browse files
committed
Simplify a bunch of len(x) > 0/len(x) == 0 style expressions
1 parent 3e99577 commit 5186479

25 files changed

Lines changed: 47 additions & 48 deletions

‎extensions-builtin/LDSR/sd_hijack_autoencoder.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ def init_from_ckpt(self, path, ignore_keys=None):
9191
del sd[k]
9292
missing, unexpected = self.load_state_dict(sd, strict=False)
9393
print(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys")
94-
if len(missing) > 0:
94+
if missing:
9595
print(f"Missing Keys: {missing}")
96+
if unexpected:
9697
print(f"Unexpected Keys: {unexpected}")
9798

9899
def on_train_batch_end(self, *args, **kwargs):

‎extensions-builtin/LDSR/sd_hijack_ddpm_v1.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ def init_from_ckpt(self, path, ignore_keys=None, only_model=False):
195195
missing, unexpected = self.load_state_dict(sd, strict=False) if not only_model else self.model.load_state_dict(
196196
sd, strict=False)
197197
print(f"Restored from {path} with {len(missing)} missing and {len(unexpected)} unexpected keys")
198-
if len(missing) > 0:
198+
if missing:
199199
print(f"Missing Keys: {missing}")
200-
if len(unexpected) > 0:
200+
if unexpected:
201201
print(f"Unexpected Keys: {unexpected}")
202202

203203
def q_mean_variance(self, x_start, t):

‎extensions-builtin/Lora/extra_networks_lora.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ def __init__(self):
99
def activate(self, p, params_list):
1010
additional = shared.opts.sd_lora
1111

12-
if additional != "None" and additional in lora.available_loras and len([x for x in params_list if x.items[0] == additional]) == 0:
12+
if additional != "None" and additional in lora.available_loras and not any(x for x in params_list if x.items[0] == additional):
1313
p.all_prompts = [x + f"<lora:{additional}:{shared.opts.extra_networks_default_multiplier}>" for x in p.all_prompts]
1414
params_list.append(extra_networks.ExtraNetworkParams(items=[additional, shared.opts.extra_networks_default_multiplier]))
1515

1616
names = []
1717
multipliers = []
1818
for params in params_list:
19-
assert len(params.items) > 0
19+
assert params.items
2020

2121
names.append(params.items[0])
2222
multipliers.append(float(params.items[1]) if len(params.items) > 1 else 1.0)

‎extensions-builtin/Lora/lora.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def load_lora(name, lora_on_disk):
219219
else:
220220
raise AssertionError(f"Bad Lora layer name: {key_diffusers} - must end in lora_up.weight, lora_down.weight or alpha")
221221

222-
if len(keys_failed_to_match) > 0:
222+
if keys_failed_to_match:
223223
print(f"Failed to match keys when loading Lora {lora_on_disk.filename}: {keys_failed_to_match}")
224224

225225
return lora
@@ -267,7 +267,7 @@ def load_loras(names, multipliers=None):
267267
lora.multiplier = multipliers[i] if multipliers else 1.0
268268
loaded_loras.append(lora)
269269

270-
if len(failed_to_load_loras) > 0:
270+
if failed_to_load_loras:
271271
sd_hijack.model_hijack.comments.append("Failed to find Loras: " + ", ".join(failed_to_load_loras))
272272

273273

‎extensions-builtin/extra-options-section/scripts/extra_options_section.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def ui(self, is_img2img):
2121
self.setting_names = []
2222

2323
with gr.Blocks() as interface:
24-
with gr.Accordion("Options", open=False) if shared.opts.extra_options_accordion and len(shared.opts.extra_options) > 0 else gr.Group(), gr.Row():
24+
with gr.Accordion("Options", open=False) if shared.opts.extra_options_accordion and shared.opts.extra_options else gr.Group(), gr.Row():
2525
for setting_name in shared.opts.extra_options:
2626
with FormColumn():
2727
comp = ui_settings.create_setting_component(setting_name)

‎modules/api/api.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def init_script_args(self, request, default_script_args, selectable_scripts, sel
280280
script_args[0] = selectable_idx + 1
281281

282282
# Now check for always on scripts
283-
if request.alwayson_scripts and (len(request.alwayson_scripts) > 0):
283+
if request.alwayson_scripts:
284284
for alwayson_script_name in request.alwayson_scripts.keys():
285285
alwayson_script = self.get_script(alwayson_script_name, script_runner)
286286
if alwayson_script is None:

‎modules/call_queue.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def wrap_gradio_gpu_call(func, extra_outputs=None):
2121
def f(*args, **kwargs):
2222

2323
# if the first argument is a string that says "task(...)", it is treated as a job id
24-
if len(args) > 0 and type(args[0]) == str and args[0][0:5] == "task(" and args[0][-1] == ")":
24+
if args and type(args[0]) == str and args[0].startswith("task(") and args[0].endswith(")"):
2525
id_task = args[0]
2626
progress.add_task_to_queue(id_task)
2727
else:

‎modules/extra_networks_hypernet.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def __init__(self):
99
def activate(self, p, params_list):
1010
additional = shared.opts.sd_hypernetwork
1111

12-
if additional != "None" and additional in shared.hypernetworks and len([x for x in params_list if x.items[0] == additional]) == 0:
12+
if additional != "None" and additional in shared.hypernetworks and not any(x for x in params_list if x.items[0] == additional):
1313
hypernet_prompt_text = f"<hypernet:{additional}:{shared.opts.extra_networks_default_multiplier}>"
1414
p.all_prompts = [f"{prompt}{hypernet_prompt_text}" for prompt in p.all_prompts]
1515
params_list.append(extra_networks.ExtraNetworkParams(items=[additional, shared.opts.extra_networks_default_multiplier]))
1616

1717
names = []
1818
multipliers = []
1919
for params in params_list:
20-
assert len(params.items) > 0
20+
assert params.items
2121

2222
names.append(params.items[0])
2323
multipliers.append(float(params.items[1]) if len(params.items) > 1 else 1.0)

‎modules/generation_parameters_copypaste.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def image_from_url_text(filedata):
5555
if filedata is None:
5656
return None
5757

58-
if type(filedata) == list and len(filedata) > 0 and type(filedata[0]) == dict and filedata[0].get("is_file", False):
58+
if type(filedata) == list and filedata and type(filedata[0]) == dict and filedata[0].get("is_file", False):
5959
filedata = filedata[0]
6060

6161
if type(filedata) == dict and filedata.get("is_file", False):
@@ -437,7 +437,7 @@ def paste_settings(params):
437437

438438
vals_pairs = [f"{k}: {v}" for k, v in vals.items()]
439439

440-
return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=len(vals_pairs) > 0)
440+
return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=bool(vals_pairs))
441441

442442
paste_fields = paste_fields + [(override_settings_component, paste_settings)]
443443

@@ -454,5 +454,3 @@ def paste_settings(params):
454454
outputs=[],
455455
show_progress=False,
456456
)
457-
458-

‎modules/images.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def prompt_no_style(self):
406406

407407
prompt_no_style = self.prompt
408408
for style in shared.prompt_styles.get_style_prompts(self.p.styles):
409-
if len(style) > 0:
409+
if style:
410410
for part in style.split("{prompt}"):
411411
prompt_no_style = prompt_no_style.replace(part, "").replace(", ,", ",").strip().strip(',')
412412

@@ -415,15 +415,15 @@ def prompt_no_style(self):
415415
return sanitize_filename_part(prompt_no_style, replace_spaces=False)
416416

417417
def prompt_words(self):
418-
words = [x for x in re_nonletters.split(self.prompt or "") if len(x) > 0]
418+
words = [x for x in re_nonletters.split(self.prompt or "") if x]
419419
if len(words) == 0:
420420
words = ["empty"]
421421
return sanitize_filename_part(" ".join(words[0:opts.directories_max_prompt_words]), replace_spaces=False)
422422

423423
def datetime(self, *args):
424424
time_datetime = datetime.datetime.now()
425425

426-
time_format = args[0] if len(args) > 0 and args[0] != "" else self.default_time_format
426+
time_format = args[0] if (args and args[0] != "") else self.default_time_format
427427
try:
428428
time_zone = pytz.timezone(args[1]) if len(args) > 1 else None
429429
except pytz.exceptions.UnknownTimeZoneError:

0 commit comments

Comments
 (0)