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

planets data class - ease of access #2

Open
astroDimitrios opened this issue May 8, 2021 · 8 comments
Open

planets data class - ease of access #2

astroDimitrios opened this issue May 8, 2021 · 8 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@astroDimitrios
Copy link
Owner

planets.csv provides a useful dataset for learning/exploration.

I would like something class/dict based which is easier to access:

from astroedu.planets import earth
print(earth.r)
>>> 6371000
print(earth.units)
>>> r, Radius in m

Not sure about the units bit etc. Might just leave that for the docs.

I am thinking this could be coded straight into a .py file and then dumped into a .json.
The rest of the code would use the .py file and the .json could be backup/if people want it for other languages.

https://www.blog.pythonlibrary.org/2014/02/14/python-101-how-to-change-a-dict-into-a-class/
https://stackoverflow.com/questions/11821322/elegant-way-to-store-dictionary-permanently-with-python

@astroDimitrios astroDimitrios added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 8, 2021
@moiez0
Copy link

moiez0 commented Dec 4, 2022

still want this done?

@astroDimitrios
Copy link
Owner Author

@Muhammad-MM Yes! I haven't had a chance to look at the package in a while.

@moiez0
Copy link

moiez0 commented Dec 6, 2022

Hi, this is just a bit of a prototype but what do you think?

import csv
import json
planets = dict() # Create a dictionary to store the planets
properties = [] # Create a list to store the properties of the planets

with open('planets.csv', 'r') as f:
    reader = csv.reader(f)
    for column in reader:
        if column[0] == 'planet':
            properties = column # Store the properties of the planets
        else:
            planets[str(column[0])] = dict() # Create a dictionary for each planet
            indx = 0 # Create an index to iterate through the properties
            for property in properties:
                planets[str(column[0])][str(property)] = str(column[indx]) # Store the properties of each planet
                indx += 1 # Increment the index

with open('planets.json', 'w') as f:
    json.dump(planets, f) # Dump the planets dictionary to a JSON file per your request 

print(planets['Earth']['mass']) # Print the mass of Earth

@moiez0
Copy link

moiez0 commented Dec 6, 2022

@astroDimitrios

@astroDimitrios
Copy link
Owner Author

@Muhammad-MM I've thought about this more, I don't think an extra external file .json or otherwise is the way to go.

I would like to see a .py file with a planets module. This would contain a planets class and individual planet child instances which have their default values from the csv built in.

Then it would be possible to do:

from astroedu.planets import earth
print(earth.r)
>>> 6371000
print(earth.units)
>>> r, Radius in m

@moiez0
Copy link

moiez0 commented Dec 13, 2022

Ah, okay I see. So just to clarify about the values, do you want them to be built into the py file instead of reading off values from the csv? Or should I keep loading the properties from the csv through iteration?

@astroDimitrios

@astroDimitrios
Copy link
Owner Author

@Muhammad-MM Yes exactly the values for each planet would be stored in the attributes of the planets class.

@moiez0
Copy link

moiez0 commented Dec 16, 2022

Hi, please check out #3 :)
@astroDimitrios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants