Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable comparing Elements for equality #43

Merged
merged 1 commit into from
Dec 8, 2021

Conversation

lmmentel
Copy link
Owner

@lmmentel lmmentel commented Dec 7, 2021

fixes #42

@lmmentel lmmentel mentioned this pull request Dec 7, 2021
mendeleev/models.py Show resolved Hide resolved
tests/test_element.py Show resolved Hide resolved
@mattwthompson
Copy link

This handles my use case perfectly, thanks!

The things I can think of - basic behavior of == and !=, ability to still use as a key in a dict - work as I would intuitively expect. I'd be happy to consider other details if that would be of help, this is just what comes to mind.

There appear to be many attributes left over after to_drop is applied, so I would not be worried about a potential hash collision.

In [1]: from mendeleev import element, O

In [2]: assert element(8) == O

In [3]: assert element(8) == element(8)

In [4]: assert O == O

In [5]: def f():
   ...:     return element(8)
   ...:

In [6]: assert f() == element(8) == O

In [7]: element_dict = {O: 0}

In [8]: element_dict[O] = 1

In [9]: element_dict[O]
Out[9]: 1

In [10]:         to_drop = [
    ...:             "_ionization_energies",
    ...:             "_oxidation_states",
    ...:             "_sa_instance_state",
    ...:             "_series",
    ...:             "_series_id",
    ...:             "ec",
    ...:             "group",
    ...:             "ionic_radii",
    ...:             "isotopes",
    ...:             "screening_constants",
    ...:         ]

In [11]: [(k, v) for k, v in O.__dict__.items() if k not in to_drop]
Out[11]:
[('covalent_radius_cordero', 66.0),
 ('en_ghosh', 0.304575),
 ('mendeleev_number', 99),
 ('covalent_radius_pyykko', 63.0),
 ('en_pauling', 3.44),
 ('metallic_radius', None),
 ('covalent_radius_pyykko_double', 57.0),
 ('evaporation_heat', None),
 ('metallic_radius_c12', None),
 ('covalent_radius_pyykko_triple', 53.0),
 ('fusion_heat', None),
 ('molcas_gv_color', '#f32e42'),
 ('c6', 15.6),
 ('gas_basicity', 459.6),
 ('name', 'Oxygen'),
 ('c6_gb', 16.7),
 ('geochemical_class', 'major'),
 ('name_origin', 'Greek: oxys and genes, (acid former).'),
 ('cpk_color', '#f00000'),
 ('glawe_number', 97),
 ('period', 2),
 ('density', 0.0013080000000000001),
 ('goldschmidt_class', 'litophile'),
 ('pettifor_number', 101),
 ('group_id', 16),
 ('proton_affinity', 485.2),
 ('description',
  "A colourless, odourless gaseous element belonging to group 16 of the periodic table. It is the most abundant element present in the earth's crust. It also makes up 20.8% of the Earth's atmosphere. For industrial purposes, it is separated from liquid air by fractional distillation. It is used in high temperature welding, and in breathing. It commonly comes in the form of Oxygen, but is found as Ozone in the upper atmosphere. It was discovered by Priestley in 1774."),
 ('dipole_polarizability', 5.3),
 ('heat_of_formation', 249.229),
 ('sources',
  'Obtained primarily from liquid air by fractional distillation. Small amounts are made in the laboratory by electrolysis of water or heating potassium chlorate (KClO3) with manganese dioxide (MnO2) catalyst.'),
 ('dipole_polarizability_unc', 0.2),
 ('is_monoisotopic', None),
 ('specific_heat', None),
 ('discoverers', 'Joseph Priestly, Carl Wilhelm Scheele'),
 ('is_radioactive', False),
 ('symbol', 'O'),
 ('discovery_location', 'England/Sweden'),
 ('jmol_color', '#ff0d0d'),
 ('thermal_conductivity', 0.027),
 ('discovery_year', 1774),
 ('lattice_constant', 6.83),
 ('uses',
  'Used in steel making, welding, and supporting life. Naturally occuring ozone (O3) in the upper atmosphere shields the earth from ultraviolet radiation.'),
 ('electron_affinity', 1.4611135),
 ('lattice_structure', 'CUB'),
 ('vdw_radius', 152.0),
 ('en_allen', 21.36),
 ('melting_point', 54.8),
 ('vdw_radius_alvarez', 150.0),
 ('vdw_radius_bondi', 152.0),
 ('econf', '[He] 2s2 2p4'),
 ('vdw_radius_truhlar', None),
 ('vdw_radius_rt', 158.0),
 ('abundance_crust', 461000.0),
 ('vdw_radius_batsanov', 155.0),
 ('abundance_sea', 857000.0),
 ('vdw_radius_dreiding', 340.46),
 ('annotation', 'density(@ -183C), '),
 ('vdw_radius_uff', 350.0),
 ('atomic_number', 8),
 ('vdw_radius_mm3', 182.0),
 ('atomic_radius', 60.0),
 ('atomic_radius_rahm', 171.0),
 ('atomic_volume', 14.0),
 ('atomic_weight', 15.999),
 ('atomic_weight_uncertainty', None),
 ('block', 'p'),
 ('boiling_point', 90.19),
 ('cas', '7782-44-7'),
 ('covalent_radius_bragg', 65.0)]

In [12]: assert O != element(7)

In [13]: assert element(8) != element(7)

@lmmentel lmmentel force-pushed the feature/element-eq-operator branch from 32513be to 0579dc3 Compare December 8, 2021 10:35
@lmmentel
Copy link
Owner Author

lmmentel commented Dec 8, 2021

Thanks for the feedback @mattwthompson!

@lmmentel lmmentel merged commit bdabf87 into master Dec 8, 2021
@lmmentel lmmentel deleted the feature/element-eq-operator branch December 8, 2021 12:42
@lmmentel
Copy link
Owner Author

lmmentel commented Dec 8, 2021

Thanks for the comments @kalvdans! If you feel something should still be reworked please submit an issue.

@kalvdans
Copy link
Collaborator

kalvdans commented Dec 8, 2021

Yes, I feel that the hash is misused for equality, I've opened #47 with my suggestions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom equality operator
3 participants