Skip to content

Commit 022c515

Browse files
style: use pythonic style
Including: - raising ValueError instead of Exception - using `isinstance` instead of `type(...) is ...` - multi-line continuation style
1 parent 927dd12 commit 022c515

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

juju/constraints.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
"spaces",
5353
"virt_type",
5454
"zones",
55-
"allocate_public_ip"]
55+
"allocate_public_ip",
56+
]
5657

5758
LIST_KEYS = {'tags', 'spaces', 'zones'}
5859

@@ -69,18 +70,21 @@ def parse(constraints):
6970
if not constraints:
7071
return None
7172

72-
if type(constraints) is dict:
73+
if isinstance(constraints, dict):
7374
# Fowards compatibilty: already parsed
7475
return constraints
7576

7677
normalized_constraints = {}
7778
for s in constraints.split(" "):
7879
if "=" not in s:
79-
raise Exception("malformed constraint %s" % s)
80+
raise ValueError("malformed constraint %s" % s)
8081

8182
k, v = s.split("=")
82-
normalized_constraints[normalize_key(k)] = normalize_list_value(v) if\
83-
k in LIST_KEYS else normalize_value(v)
83+
normalized_constraints[normalize_key(k)] = (
84+
normalize_list_value(v)
85+
if k in LIST_KEYS
86+
else normalize_value(v)
87+
)
8488

8589
return normalized_constraints
8690

@@ -95,7 +99,7 @@ def normalize_key(orig_key):
9599
key = SNAKE2.sub(r'\1_\2', key).lower()
96100

97101
if key not in SUPPORTED_KEYS:
98-
raise Exception("unknown constraint in %s" % orig_key)
102+
raise ValueError("unknown constraint in %s" % orig_key)
99103
return key
100104

101105

@@ -132,7 +136,7 @@ def parse_storage_constraint(constraint):
132136
pool = m.group('pool')
133137
if pool:
134138
if 'pool' in storage:
135-
raise Exception("pool already specified")
139+
raise ValueError("pool already specified")
136140
storage['pool'] = pool
137141
count = m.group('count')
138142
if count:
@@ -152,7 +156,7 @@ def parse_storage_constraint(constraint):
152156
def parse_device_constraint(constraint):
153157
m = DEVICE.match(constraint)
154158
if m is None:
155-
raise Exception("device constraint does not match")
159+
raise ValueError("device constraint does not match")
156160
device = {}
157161
count = m.group('count')
158162
if count:

0 commit comments

Comments
 (0)