[yt-svn] commit/yt: ngoldbaum: Merged in ngoldbaum/yt (pull request #2092)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Apr 6 11:15:53 PDT 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/47103efaf15a/
Changeset:   47103efaf15a
Branch:      yt
User:        ngoldbaum
Date:        2016-04-06 18:15:40+00:00
Summary:     Merged in ngoldbaum/yt (pull request #2092)

Ensure user-defined derived fields names that are strings work in python3. Closes #1053
Affected #:  2 files

diff -r 9488a6181a069af1fbc2339b144039342ea1a437 -r 47103efaf15acd4f13408b11f292e05e1cb4abcc yt/fields/field_info_container.py
--- a/yt/fields/field_info_container.py
+++ b/yt/fields/field_info_container.py
@@ -38,6 +38,13 @@
     add_volume_weighted_smoothed_field, \
     sph_whitelist_fields
 
+def tupleize(inp):
+    if isinstance(inp, tuple):
+        return inp
+    # prepending with a '?' ensures that the sort order is the same in py2 and
+    # py3, since names of field types shouldn't begin with punctuation
+    return ('?', inp, )
+
 class FieldInfoContainer(dict):
     """
     This is a generic field container.  It contains a list of potential derived
@@ -271,7 +278,7 @@
         self.ds.field_dependencies.update(deps)
         # Note we may have duplicated
         dfl = set(self.ds.derived_field_list).union(deps.keys())
-        self.ds.derived_field_list = list(sorted(dfl))
+        self.ds.derived_field_list = list(sorted(dfl, key=tupleize))
         return loaded, unavailable
 
     def add_output_field(self, name, **kwargs):
@@ -357,5 +364,5 @@
             deps[field] = fd
             mylog.debug("Succeeded with %s (needs %s)", field, fd.requested)
         dfl = set(self.ds.derived_field_list).union(deps.keys())
-        self.ds.derived_field_list = list(sorted(dfl))
+        self.ds.derived_field_list = list(sorted(dfl, key=tupleize))
         return deps, unavailable

diff -r 9488a6181a069af1fbc2339b144039342ea1a437 -r 47103efaf15acd4f13408b11f292e05e1cb4abcc yt/fields/tests/test_fields.py
--- a/yt/fields/tests/test_fields.py
+++ b/yt/fields/tests/test_fields.py
@@ -301,6 +301,19 @@
     u2 = array_like_field(ad, 1., ("all", "particle_mass")).units
     assert u1 == u2
 
+def test_add_field_string():
+    ds = fake_random_ds(16)
+    ad = ds.all_data()
+
+    def density_alias(field, data):
+        return data['density']
+
+    ds.add_field('density_alias', function=density_alias, units='g/cm**3')
+
+    ad['density_alias']
+    assert ds.derived_field_list[0] == 'density_alias'
+
+
 if __name__ == "__main__":
     setup()
     for t in test_all_fields():
@@ -308,3 +321,4 @@
     test_add_deposited_particle_field()
     test_add_field_unit_semantics()
     test_array_like_field()
+    test_add_field_string()

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-svn-spacepope.org/attachments/20160406/14bc89ff/attachment-0001.htm>


More information about the yt-svn mailing list