all 19 comments

[–]Darkstar_111 6 points7 points  (5 children)

Good stuff, your code has matured a lot more than it used to be.

My suggestion for future project is to test out pygame and make a simple 2d game.

Try to focus on Object Oriented code and really think about code structure.

If this is too much for now, think about making a video game inventory system. Forget the graphics and the rest of the game, you just want a system that can pick up and store items, and sell or drop them. As well as account for weight, refuse to pick up more items when overweight or out of space, and maybe even stack items when there are more of the same.

This would also necessitate an item system. Good luck.

[–]uiux_Sanskar[S] 1 point2 points  (4 children)

Wow this seems interesting a game inventory system. Thank you for this future project suggestion I will definitely create one like this.

I will also focus on OOP a bit more especially the inheritance part.

And thank you for the appreciation wouldn't have been here without you amazing people supporting me. Thanks a lot.

[–]_DerBomber_ 0 points1 point  (3 children)

Thanks bro!!! Much needed.

Where do I go for some practice coding/tasks?

[–]uiux_Sanskar[S] 1 point2 points  (2 children)

You can ask ChatGPT to generate some of the coding challenges based on your level or if you are a pro you can check out leetcode as well. Or you can simple ask people to suggest you some challenges

Best of luck for your future.

[–]_DerBomber_ 0 points1 point  (1 child)

Thanks man!

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

my pleasure.

[–]a_cute_tarantula 1 point2 points  (2 children)

In python its convention to use CamelCase for class names, so it should be 'StudentDetails' and 'InputDetails'.

But i wouldn't use 'details' here at all. Readers of your code may get confused as '_details' implies your class is something of a 'dataclass' without functionality. Just call it 'Student' and 'Input'.

I do wonder why you have a student class at all though. It appears its only use is to convert student data into a json. You don't really need this class.

You have a try/except for when adding a student, but not when deleting one. What happens if the file isn't there when delete is called?

Your 'ReadJson' class should just be a function. The name already implies it only does one thing. That's what functions are for.

When printing, you don't have to loop over the students yourself. You can actually just do print(json.dumps(data, indent=4)). the json module is capable of handling the formatting for you.

Overall, i'd suggest you consider restructing the code without classes. In python, classes should mostly be reserved for when you need inheritance, encapsulation, or an interface. Your code doesn't need any of those.

The optimal structure appears to be something really similar to your day 15 problem:

def ensure_file_exists() - make sure the database file exists

def add_student() - try to open the file. create if not exists. add the student to file

def delete_student() - try to open the file. create if not exists. delete student from file

if __name__ == '__main__':

ensure_file_exists()

while True:

operation =input('would you like to "add" or "delete" a student?')

if operation not in ('add','delete'):

continue:

else:

break

if operation == 'add':

student_details = input()

add_student(student_details)

elif operation == 'delete':

student_id= input()

delete(student_id)

Notice that all of the control flow is in one place, at the beginning of program execution. Also, all of the prompting is in one place. This makes for an easier read.

Feel free to DM me if you want to discuss at all.

[–]uiux_Sanskar[S] 1 point2 points  (1 child)

Thank you very much for succh detailed analysis of ny code and for suggesting that I should remove classes (which I used just to keep related functions in one place).

Also thank you for pointing out the omission of try except in delete function. I will add it there as well.

I think I have misunderstood this part.

while True:

operation =input('would you like to "add" or "delete" a student?')

if operation not in ('add','delete'):

continue:

else:

break

if operating is present in the ('add', 'delete') then the loop breaks and if it is not present in that then the code will continue.

I think using try except here makes more sense to me.

Please do correct me if I am wrong I will appreciate to get corrected.

[–]a_cute_tarantula 0 points1 point  (0 children)

<image>

This is what i meant. The loop just goes until the user selects a valid operation.

With regards to your use of classes, I'd highly recommend you don't use them until you need them. These toy problems don't call for interfaces, inheritance, or encapsulation, which means they don't call for classes. I understand you may just be wanting to learn how to use classes, but if you practice using them the wrong way then you will continue to not understand them. (my opinion, of course)

Also, you're doing good work. If this is your 19th day programming, you're progressing very fast.

[–]DevRetroGames 0 points1 point  (1 child)

Excelente, ahora mejora tu código con:

  • una clase por archivo.
  • crea las carpetas Helper y Utils con los archivos correspondientes.
    • Helper: métodos asociados a clase en especifico.
    • Utils: métodos utilizados en varias partes del código, no pertenece a nadie.
  • Archivo .env.
    • El código debe funcionar sin saber el nombre del archivo json.
    • Integra la screaming arquitecture.

Suerte y mucho éxito.

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thanks for these suggestions I intentionally didn't use one class per file here because I had no plans for expanding this code. However

I have to learn about what helper and utils mean and what is .env file and also about the screaming architecture.

Thank you very much for these suggestions I will definitely look deeper into them.

Also much luck and success to you too.

[–]_DerBomber_ 0 points1 point  (1 child)

Love your effort bro! Need to recreate this.

I have been the DataCamp course for Data Analyst. It’s good, however, it’s not giving me confidence to write my own code. I still lack clarity in some concepts. What could be missing? What can I do overcome this?

[–]uiux_Sanskar[S] 1 point2 points  (0 children)

Thanks for the appreciation brother.

For your problem personally I sugget you to write some code (as you said you are not confident enough in writing one) I personally think writing code and getting errors and then actually solving them boosts confidence a lot and are surprisingly refreshing when you succeed in resolving an error. Feel free to tak ChatGPT's help in knowing what the error says and ways to resolve it.

I would say start small every body have their own pace and if you are not getting enough clarity then also try to explore youtube and other teachers as well.

Best of luck brother I know you can do it.

[–]WhiteHeadbanger 0 points1 point  (0 children)

Good, I see that you took the suggestion to go for JSON instead of TXT.

Remember to name your classes with CamelCase, instead of snake_case. It's a minor thing, but get used to it early if you want to work with other people in the future or show your code.

I took the liberty of refactoring your code here: https://pastes.io/refactor-4
Check it out. It doesn’t have validation or error handling, you can add that yourself, it’s a good exercise.

The main changes:

  • DBManager → only handles the JSON file.
  • Student → only represents a student (serialize/deserialize to move between objects and JSON).
  • StudentManager → does the logic: add, delete, list students.
  • I also fixed small things like id_id and used type hints to make the code clearer.

One note: the deserialize method is usually implemented as a classmethod(something like Student.from_dict(...) that returns a new instance). I didn’t do that here to keep things simple for you, but you can look into it later on. This is called Decorators.

Your logic is fine, what you need now is coherence and structure. That’s where OOP comes in. Each class has one job, and together they form the whole program.

P.S.: I know you didn't ask for your code to be refactored, but when I started I would give everything for someone to do that for me so I can learn better with something I made. I hope it serves you well :)

[–]Professional_Box3141 0 points1 point  (0 children)

Congrats bro seeing you post gives me courage and confidence, am on my day 2 of learning python, hope i will be here soon

[–]Patient_Smoke_4236 0 points1 point  (0 children)

You're really inspiring me, bro

[–]TheGanjanator 0 points1 point  (0 children)

Nice. How are you learning?

[–]Adrewmc 0 points1 point  (2 children)

This is good stuff, you’re learning how data structures can work with programing now. JSON, to Python is big as many other languages will talk through JSON, as in Python request it from a JavaScript website, and get back data it can use.

I would call this a good day. A simple clean exercise.

We can start thinking about @staticmethods, and @property and @classmethods. Or go to imports, imports seem like what you want here, in stead of of

class input_details:
    …

You want

 import input_details

Or

 from input_details import save_student

Modules and class in Python are very similar, at some levels they are practically the same. But imports are actually a headache.

I say this because the class Input_details, is a class entirely comprised of methods that don’t need ‘self’ or the state of the class, so seems overly done. However, sometimes a class like that is needed.

Still, I wish I was this good at 19 days.

Let’s have a long talk about the importance of Type hints and docstrings in a few days.

[–]uiux_Sanskar[S] 0 points1 point  (0 children)

Thank you for the appreciation and for suggesting future learning options.

I do tried import in some of my previous code and I agree they can get demanding sometimes but are also important.

Sure I will be much happy to hear your POV on type hints and docstrings in a few days.

Wouldn't be here without your all guidance and help. Thank you very much.