Skip to content

Commit

Permalink
Make the leap from py2 to py3
Browse files Browse the repository at this point in the history
  • Loading branch information
obreitwi committed Sep 27, 2019
1 parent 2c93f6e commit d3a83a4
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Version 1.0.0

* Switch to Python 3
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ parameter-files that are still human-readable afterwards. See the

## Example
```python
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8
import os.path as osp
Expand Down Expand Up @@ -211,3 +211,8 @@ Options:

-l --last KEY Sort by KEY last (in order of specification).
```
# Requirements:
* Python 3 just [because](https://pythonclock.org/).
* [PyYAML](https://github.com/yaml/pyyaml)
10 changes: 5 additions & 5 deletions yccp/cli/sort_by_numbers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

from __future__ import print_function


from docopt import docopt
from pprint import pprint
Expand Down Expand Up @@ -76,12 +76,12 @@ def sort_filename(filenames, first=[], last=[],
if verbose:
print(fn_to_nums)

num_numbers = map(len, fn_to_nums.itervalues())
num_numbers = list(map(len, iter(fn_to_nums.values())))
assert min(num_numbers) == max(num_numbers), "Amount of numbers varies"

key_set = set()
for fnk in fn_to_nums.itervalues():
key_set.update(fnk.iterkeys())
for fnk in fn_to_nums.values():
key_set.update(iter(fnk.keys()))

assert len(key_set) == min(num_numbers),\
"Key values are not the same accross filenames"
Expand Down
2 changes: 1 addition & 1 deletion yccp/logcfg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

__all__ = ["log"]
Expand Down
12 changes: 6 additions & 6 deletions yccp/meta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8
#
"""
Expand Down Expand Up @@ -98,12 +98,12 @@ def __new__(mcs, name, bases, dct):
base.__name__))
del to_update[to_delete]

for k,v in to_update.iteritems():
for k,v in to_update.items():
if k in all_nonrec_updates:
updated_defaults.setdefault(k, {}).update(v)
else:
updated_defaults[k] = v
for k,v in dct.get(mcs.attr_defaults, {}).iteritems():
for k,v in dct.get(mcs.attr_defaults, {}).items():
if k in all_nonrec_updates:
updated_defaults.setdefault(k, {}).update(v)
else:
Expand Down Expand Up @@ -167,13 +167,13 @@ def _get_updated_parameters(cls, parameters):
cls.__name__) + pf(parameters))
# get a deep-copy of the default parameters to be updated
prms = copy.deepcopy(getattr(
cls, cls.__metaclass__.attr_defaults))
cls, cls.__class__.attr_defaults))

not_recursively_updated_attributes = getattr(
cls, cls.__metaclass__.attr_nonrec_update, set())
cls, cls.__class__.attr_nonrec_update, set())

# update the default parameters with whatever was proved
for k, v in copy.deepcopy(parameters).iteritems():
for k, v in copy.deepcopy(parameters).items():
if k in prms:
if isinstance(v, dict)\
and k not in not_recursively_updated_attributes:
Expand Down
20 changes: 10 additions & 10 deletions yccp/prelude.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

"""
Expand Down Expand Up @@ -29,8 +29,8 @@
# tags #
########
yccp_tags = {
"eval": [u"!eval", u"!ee"],
"get": [u"!get", u"!cc"],
"eval": ["!eval", "!ee"],
"get": ["!get", "!cc"],
}
# "__prelude__" used to be called "cache" → keep it for backwards compatability
default_prelude_attr = ["__prelude__", "cache"]
Expand All @@ -49,7 +49,7 @@ def __init__(self, expression):
self.expression = expression

def dump(self, dumper):
return dumper.represent_scalar(u"!eval", self.expression)
return dumper.represent_scalar("!eval", self.expression)


class RawPreludeEntry(object):
Expand All @@ -62,7 +62,7 @@ def __init__(self, expression):
self.expression = expression

def dump(self, dumper):
return dumper.represent_scalar(u"!get", self.expression)
return dumper.represent_scalar("!get", self.expression)


class ExpressionEvaluatorWithPrelude(object):
Expand Down Expand Up @@ -109,7 +109,7 @@ def disable(self):
def eval(self, value):
if isinstance(value, (RawExpression, RawPreludeEntry)):
value = value.expression
if not isinstance(value, basestring):
if not isinstance(value, str):
return value
eval_globals = {"np": np}
eval_locals = {
Expand Down Expand Up @@ -138,16 +138,16 @@ def __call__(self, loader, node):


# Constructors
for k, v in yccp_tags.items():
for k, v in list(yccp_tags.items()):
for tag in v:
yaml.add_constructor(tag, evaluate_expression, Loader=YccpLoader)

# Representations
yaml.add_representer(RawExpression,
lambda dumper, re: re.dump(dumper),
Dumper=YccpDumper)
yaml.add_representer(unicode, lambda dumper, value:
dumper.represent_scalar(u'tag:yaml.org,2002:str', value),
yaml.add_representer(str, lambda dumper, value:
dumper.represent_scalar('tag:yaml.org,2002:str', value),
Dumper=YccpDumper)


Expand Down Expand Up @@ -231,7 +231,7 @@ def load_data_with_prelude(obj, name_prelude=default_prelude_attr, **kwargs):
"of dictionaries".format(name_prelude_found))

for dct in prelude:
for k, v in dct.iteritems():
for k, v in dct.items():
evaluate_expression.prelude_add(
k, evaluate_expression.eval(v))

Expand Down
7 changes: 2 additions & 5 deletions yccp/sweeps/ranges.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

"""
Expand All @@ -7,9 +7,6 @@
"""

import collections as c
import itertools as it

from .. import utils as u

from . import transforms as trans

Expand Down Expand Up @@ -55,7 +52,7 @@ def __call__(self, paramset):
for tr in self.range_tuples:
p = paramset.copy()

for t, v in it.izip(self.transforms, tr):
for t, v in zip(self.transforms, tr):
t.set_value(v)
t.apply(p)

Expand Down
2 changes: 1 addition & 1 deletion yccp/sweeps/sweeps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

import errno
Expand Down
5 changes: 2 additions & 3 deletions yccp/sweeps/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
from .. import utils as _u


class Transform(object):
class Transform(object, metaclass=_m.InheritDefaults):
"""
Transform a single parameter set.
"""
__metaclass__ = _m.InheritDefaults

default_parameters = {}

Expand Down Expand Up @@ -220,7 +219,7 @@ class ApplyFunctionElaborate(AddValue):

def get_orig_value(self, paramset):
self.orig_values = {}
for local_key, yaml_key in self.prms['dict'].iteritems():
for local_key, yaml_key in self.prms['dict'].items():
self.orig_values[local_key] = _u.get_recursive(
paramset.data,
yaml_key)
Expand Down
6 changes: 3 additions & 3 deletions yccp/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

__all__ = [
Expand Down Expand Up @@ -128,7 +128,7 @@ def update_dict_recursively(d, u):
"""
Dictionary d with the contents from u in recursive manner.
"""
for k, v in copy.deepcopy(u).iteritems():
for k, v in copy.deepcopy(u).items():
if isinstance(v, c.Mapping):
if k in d and not isinstance(d.get(k), c.Mapping):
raise ValueError(
Expand All @@ -155,7 +155,7 @@ def chained(start_value):

while len(generators) > 0:
try:
value = generators[-1].next()
value = next(generators[-1])

if len(generators) < len(generator_functions):
generators.append(
Expand Down
4 changes: 2 additions & 2 deletions yccp/version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# encoding: utf-8

__version__ = (0, 5, 0)
__version__ = (1, 0, 0)

0 comments on commit d3a83a4

Please sign in to comment.