Skip to content

Commit b009dcd

Browse files
committed
ruff: Fix SIM108 Use ternary operator instead of if-else-block.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 2aa36f9 commit b009dcd

File tree

9 files changed

+31
-78
lines changed

9 files changed

+31
-78
lines changed

tools/deploy

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,7 @@ def prepare(options: argparse.Namespace) -> None:
181181
def log(options: argparse.Namespace) -> None:
182182
check_common_options(options)
183183
headers = {"key": options.token}
184-
if options.lines:
185-
lines = options.lines
186-
else:
187-
lines = None
184+
lines = options.lines
188185
payload = {"name": options.botname, "lines": lines}
189186
url = urllib.parse.urljoin(options.server, "bots/logs/" + options.botname)
190187
response = requests.get(url, json=payload, headers=headers)
@@ -209,10 +206,7 @@ def delete(options: argparse.Namespace) -> None:
209206
def list_bots(options: argparse.Namespace) -> None:
210207
check_common_options(options)
211208
headers = {"key": options.token}
212-
if options.format:
213-
pretty_print = True
214-
else:
215-
pretty_print = False
209+
pretty_print = options.format
216210
url = urllib.parse.urljoin(options.server, "bots/list")
217211
response = requests.get(url, headers=headers)
218212
result = handle_common_response(

tools/provision

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ the Python version this command is executed with."""
7777
else:
7878
print("Virtualenv already exists.")
7979

80-
if os.path.isdir(os.path.join(venv_dir, "Scripts")):
81-
# POSIX compatibility layer and Linux environment emulation for Windows
82-
# venv uses /Scripts instead of /bin on Windows cmd and Power Shell.
83-
# Read https://docs.python.org/3/library/venv.html
84-
venv_exec_dir = "Scripts"
85-
else:
86-
venv_exec_dir = "bin"
80+
# POSIX compatibility layer and Linux environment emulation for Windows
81+
# venv uses /Scripts instead of /bin on Windows cmd and Power Shell.
82+
# Read https://docs.python.org/3/library/venv.html
83+
venv_exec_dir = "Scripts" if os.path.isdir(os.path.join(venv_dir, "Scripts")) else "bin"
8784

8885
# On OS X, ensure we use the virtualenv version of the python binary for
8986
# future subprocesses instead of the version that this script was launched with. See

zulip/integrations/jabber/jabber_mirror_backend.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,7 @@ def config_error(msg: str) -> None:
452452
zulipToJabber.set_jabber_client(xmpp)
453453

454454
xmpp.process(block=False)
455-
if options.mode == "public":
456-
event_types = ["stream"]
457-
else:
458-
event_types = ["message", "subscription"]
455+
event_types = ["stream"] if options.mode == "public" else ["message", "subscription"]
459456

460457
try:
461458
logging.info("Connecting to Zulip.")

zulip/integrations/perforce/zulip_perforce_config.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@
3131
# * subject "change_root"
3232
def commit_notice_destination(path: str, changelist: int) -> Optional[Dict[str, str]]:
3333
dirs = path.split("/")
34-
if len(dirs) >= 4 and dirs[3] not in ("*", "..."):
35-
directory = dirs[3]
36-
else:
37-
# No subdirectory, so just use "depot"
38-
directory = dirs[2]
34+
# If no subdirectory, just use "depot"
35+
directory = dirs[3] if len(dirs) >= 4 and dirs[3] not in ("*", "...") else dirs[2]
3936

4037
if directory not in ["evil-master-plan", "my-super-secret-repository"]:
4138
return dict(stream=f"{directory}-commits", subject=path)

zulip/integrations/twitter/twitter-bot

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,9 @@ elif opts.twitter_name:
181181
else:
182182
statuses = api.GetUserTimeline(screen_name=opts.twitter_name, since_id=since_id)
183183

184-
if opts.excluded_terms:
185-
excluded_terms = opts.excluded_terms.split(",")
186-
else:
187-
excluded_terms = []
184+
excluded_terms = opts.excluded_terms.split(",") if opts.excluded_terms else []
188185

189-
if opts.excluded_users:
190-
excluded_users = opts.excluded_users.split(",")
191-
else:
192-
excluded_users = []
186+
excluded_users = opts.excluded_users.split(",") if opts.excluded_users else []
193187

194188
for status in statuses[::-1][: opts.limit_tweets]:
195189
# Check if the tweet is from an excluded user

zulip/zulip/__init__.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636

3737
logger = logging.getLogger(__name__)
3838

39-
# In newer versions, the 'json' attribute is a function, not a property
40-
requests_json_is_function = callable(requests.Response.json)
41-
4239
API_VERSTRING = "v1/"
4340

4441
# An optional parameter to `move_topic` and `update_message` actions
@@ -571,14 +568,11 @@ def do_api_query(
571568
if files is None:
572569
files = []
573570

574-
if longpolling:
575-
# When long-polling, set timeout to 90 sec as a balance
576-
# between a low traffic rate and a still reasonable latency
577-
# time in case of a connection failure.
578-
request_timeout = 90.0
579-
else:
580-
# Otherwise, 15s should be plenty of time.
581-
request_timeout = 15.0 if not timeout else timeout
571+
# When long-polling, set timeout to 90 sec as a balance
572+
# between a low traffic rate and a still reasonable latency
573+
# time in case of a connection failure.
574+
# Otherwise, 15s should be plenty of time.
575+
request_timeout = 90.0 if longpolling else timeout or 15.0
582576

583577
request = {}
584578
req_files = []
@@ -630,10 +624,7 @@ def end_error_retry(succeeded: bool) -> None:
630624

631625
while True:
632626
try:
633-
if method == "GET":
634-
kwarg = "params"
635-
else:
636-
kwarg = "data"
627+
kwarg = "params" if method == "GET" else "data"
637628

638629
kwargs = {kwarg: query_state["request"]}
639630

@@ -689,22 +680,17 @@ def end_error_retry(succeeded: bool) -> None:
689680
raise
690681

691682
try:
692-
if requests_json_is_function:
693-
json_result = res.json()
694-
else:
695-
json_result = res.json
683+
json_result = res.json()
696684
except Exception:
697-
json_result = None
698-
699-
if json_result is not None:
700-
end_error_retry(True)
701-
return json_result
702-
end_error_retry(False)
703-
return {
704-
"msg": "Unexpected error from the server",
705-
"result": "http-error",
706-
"status_code": res.status_code,
707-
}
685+
end_error_retry(False)
686+
return {
687+
"msg": "Unexpected error from the server",
688+
"result": "http-error",
689+
"status_code": res.status_code,
690+
}
691+
692+
end_error_retry(True)
693+
return json_result
708694

709695
def call_endpoint(
710696
self,

zulip_bots/zulip_bots/bots/dropbox_share/dropbox_share.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ def syntax_help(cmd_name: str) -> str:
113113
commands = get_commands()
114114
f, arg_names = commands[cmd_name]
115115
arg_syntax = " ".join("<" + a + ">" for a in arg_names)
116-
if arg_syntax:
117-
cmd = cmd_name + " " + arg_syntax
118-
else:
119-
cmd = cmd_name
116+
cmd = cmd_name + " " + arg_syntax if arg_syntax else cmd_name
120117
return f"syntax: {cmd}"
121118

122119

@@ -207,10 +204,7 @@ def dbx_read(client: Any, fn: str) -> str:
207204

208205

209206
def dbx_search(client: Any, query: str, folder: str, max_results: str) -> str:
210-
if folder is None:
211-
folder = ""
212-
else:
213-
folder = "/" + folder
207+
folder = "" if folder is None else "/" + folder
214208
if max_results is None:
215209
max_results = "20"
216210
try:

zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,7 @@ def display_game(topic_name, merels_storage):
303303

304304
response = ""
305305

306-
if data.take_mode == 1:
307-
take = "Yes"
308-
else:
309-
take = "No"
306+
take = "Yes" if data.take_mode == 1 else "No"
310307

311308
response += interface.graph_grid(data.grid()) + "\n"
312309
response += f"""Phase {data.get_phase()}. Take mode: {take}.

zulip_bots/zulip_bots/bots/virtual_fs/virtual_fs.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ def syntax_help(cmd_name: str) -> str:
166166
commands = get_commands()
167167
f, arg_names = commands[cmd_name]
168168
arg_syntax = " ".join("<" + a + ">" for a in arg_names)
169-
if arg_syntax:
170-
cmd = cmd_name + " " + arg_syntax
171-
else:
172-
cmd = cmd_name
169+
cmd = cmd_name + " " + arg_syntax if arg_syntax else cmd_name
173170
return f"syntax: {cmd}"
174171

175172

0 commit comments

Comments
 (0)