Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a faster way to write different classes in python?

I am making the periodic table in python, but I was wondering if there was a different way to make multiple classes without having to write each individual property for each element. Is there a faster and easier way to do that/ make it so then I don’t have to type as much.

This is my code

def gs(x):
    normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
    super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
    res1 = x.maketrans(''.join(normal),''.join(super_s))
    return x.translate(res1)
class elements():
    def __init__(self, name,symbol,an, atomicweight, state, mp, bp, oxstates, eleconfig, atomicradius, electronegativity, valance):
        self.name = name
        self.symbol = symbol
        self.an = an #atomic number
        self.atomicweight = atomicweight
        self.state = state #metal, metalloid, gas or unknown at 0C and Classification
        self.mp = mp #melting point
        self.bp = bp #boiling point
        self.oxstates = oxstates #oxidation States (charges)
        self.eleconfig = eleconfig #electron Configuration
        self.atomicradius = atomicradius
        self.electronegativity = electronegativity
        self.valance = valance
        
    def properties(self):
        print(self.name, self.symbol, self.an, self.atomicweight, self.state, self.mp, self.bp, self.oxstates, self.eleconfig, self.atomicradius, self.electronegativity, self.valance)

hydrogenele =  elements('Element: Hydrogen', '\n Symbol: H', '\n Atomic number: 1', '\n Atomic Weight: 1.008u', '\n State at 0ᵒC: Gas (Reactive nonmetal)',
                        '\n Melting point: -259.1ᵒC', '\n Boiling point: -252.76ᵒC', '\n Oxidation States (Charges): ±1',
                        '\n Electron Configuration: 1s{}'.format(gs('1')), '\n Atomic Radius: 37pm', '\n Electronegativity: 2.18', '\n Valance electrons: 1')

heliumele = elements('Element: Helium', '\n Symbol: He', '\n Atomic Number: 2', '\n Atomic Weight: 4.002602u', '\n State at 0ᵒC: Gas (Noble gas)',
                    '\n Melting Point: -268.93', "\n Boiling Point: N/A", '\n Oxidation States (Charges): 0', 
                    '\n Electron Configuration: 1s{}'.format(gs('2')), '\n Atomic Radius: 31pm', "\n Electronegativity: N/A", '\n Valance Electrons: 2')

while True:
    select = input("Select the element by name: ")
    ele_name = f"{select}ele"
    if ele_name in locals():
        print(f"{locals()[ele_name].properties()}")
    elif 'h' in select:
        hc = input("Please select the number that corresponds to your problem: \n1. Don't know how to spell the element \n2. Info is incorrect \n3. Cancel \n")
        if '1' in hc:
            print("Filler")
        elif '2' in hc:
            print("Please go to this google forms to report problem \n(Link)")
        elif '3' in hc:
            continue
    else:
        print("Element not found \nType if you need help type 'h'")

This is my code, as you can see, there is a lot of properties that I decided to put in each element, and they are the one I think are the most important. The def gs(x) allows me to get super scripts. The ele after the element, allows me to search the name. Please help me.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Consider using Data Classes:

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""
    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading