Skip to content

Commit

Permalink
Merge branch 'dev' into 0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeDacre committed Feb 9, 2018
2 parents 9ef18fd + fc2de29 commit 3700657
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
.pytest_cache

# C extensions
*.so
Expand Down
37 changes: 19 additions & 18 deletions fyrd/batch_systems/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,24 +304,19 @@ def check_arguments(kwargs):
else:
raise OptionsError('Unrecognized argument {}'.format(arg))
if opt is not None and not isinstance(opt, ALLOWED_KWDS[arg]):
try:
newtype = ALLOWED_KWDS[arg]
if (newtype is list or newtype is tuple) \
and not isinstance(arg, (list, tuple)):
if newtype is list:
opt2 = [opt]
elif newtype is tuple:
opt2 = (opt,)
else:
raise Exception("Shouldn't be here")
else:
opt2 = newtype(opt)
except:
raise TypeError('arg must be {}, is {}'.format(
ALLOWED_KWDS[arg], type(opt)))
new_kwds[arg] = opt2
else:
new_kwds[arg] = opt
newtype = ALLOWED_KWDS[arg]
if (newtype is list or newtype is tuple) \
and not isinstance(arg, (list, tuple)):
opt = run.listify(opt)
elif newtype is int and isinstance(opt, str) and opt.isdigit():
opt = int(opt)
else:
raise TypeError(
'arg "{}" must be {}, is {} ({})'.format(
arg, ALLOWED_KWDS[arg], opt, type(opt)
)
)
new_kwds[arg] = opt

# Parse individual complex options
for arg, opt in new_kwds.items():
Expand Down Expand Up @@ -456,6 +451,12 @@ def option_to_string(option, value=None, qtype=None):
if '{}' in kwds[option][qtype]:
if value is None:
return ''
if 'type' in kwds[option]:
if not isinstance(value, kwds[option]['type']):
raise ValueError(
'Argument to {} must be {}, but is:\n{}'
.format(option, kwds[option]['type'], value)
)
return '{prefix} {optarg}'.format(
prefix=prefix, optarg=kwds[option][qtype].format(value))
else:
Expand Down
44 changes: 44 additions & 0 deletions fyrd/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,50 @@ def _section_to_dict(section):
out[key] = _typecast_items(val)
return out

def _typecast_items(x):
"""Try to convert a variable into the true type.
Avoids eval() as we will be used on config files.
For example: 'True' becomes True and '0.125' becomes 0.125
"""
c = re.compile(r', *')
r = re.compile(r': *')
if not isinstance(x, (six.string_types, six.text_type)):
return x
if x == 'True':
return True
if x == 'False':
return False
if x == 'None':
return None
if x.isdigit():
return int(x)
try:
return float(x)
except (ValueError, TypeError):
pass
try:
return complex(x)
except (ValueError, TypeError):
pass
try:
if x.startswith('[') and x.endswith(']'):
return [_typecast_items(i) for i in c.split(x.strip('[]'))]
if x.startswith('{') and x.endswith('}'):
if ':' in x:
return {
_typecast_items(k): _typecast_items(v) for k, v in [
r.split(i) for i in c.split(x.strip('{}'))
]
}
return {_typecast_items(i) for i in c.split(x.strip('{}'))}
except (ValueError, TypeError):
pass
if isinstance(x, (six.string_types)):
return x.strip('\'"')
return x


def _typecast_items(x):
"""Try to convert a variable into the true type.
Expand Down
2 changes: 0 additions & 2 deletions fyrd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ def listify(iterable):
return [iterable]
if not iterable:
return []
# if callable(iterable):
# iterable = iterable()
try:
iterable = list(iterable)
except TypeError:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ def test_parapply_summary(delete=True):
df_comp.T.A
df_comp.T.B
try:
assert str(new_df.T.A) == str(df_comp.T.A)
assert str(round(new_df.T.A, 2)) == str(round(df_comp.T.A, 2))
except AssertionError:
print('A:', type(new_df.T.A), ':', str(new_df.T.A),
type(df_comp.T.A), ':', str(df_comp.T.A))
print('Job df', new_df)
print('Comp df', new_df)
raise
try:
assert str(new_df.T.B) == str(df_comp.T.B)
assert str(round(new_df.T.B, 2)) == str(round(df_comp.T.B, 2))
except AssertionError:
print('B:', type(new_df.T.B), ':', str(new_df.T.B),
type(df_comp.T.B), ':', str(df_comp.T.B))
Expand Down Expand Up @@ -172,15 +172,15 @@ def test_parapply_summary_indirect(delete=True):
df_comp.T.A
df_comp.T.B
try:
assert str(new_df.T.A) == str(df_comp.T.A)
assert str(round(new_df.T.A, 2)) == str(round(df_comp.T.A, 2))
except AssertionError:
print('A:', type(new_df.T.A), ':', new_df.T.A,
type(df_comp.T.A), ':', df_comp.T.A,)
print('Job df:\n', new_df)
print('Comp df:\n', new_df)
raise
try:
assert str(new_df.T.B) == str(df_comp.T.B)
assert str(round(new_df.T.B, 2)) == str(round(df_comp.T.B, 2))
except AssertionError:
print('B:', type(new_df.T.B), ':', str(new_df.T.B),
type(df_comp.T.B), ':', str(df_comp.T.B))
Expand Down

0 comments on commit 3700657

Please sign in to comment.