Python Forum
a contact book - a class made to store data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a contact book - a class made to store data
#1
hello dear Community, 


a class for storing contact data - Here is the code of a simple custom class which stores information about a person:

import datetime # so we will use this for date objects

class Person: # this is it - this is the class 

    def __init__(self, name, surname, birthdate, address, telephone, email):
        self.name = name
        self.surname = surname
        self.birthdate = birthdate

        self.address = address
        self.telephone = telephone
        self.email = email

    def age(self):
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

person = Person(
    "Joe",
    "Doe",
    datetime.date(1952, 4, 22), # year, month, day
    "No. 1444 Short Street, Munich",
    "555 4564444444444444 0987",
    "[email protected]"
)

print(person.name)
print(person.email)
print(person.age())
i want to store the data in a database. 


background: We start the class definition with the class keyword, followed by the class name and a colon. i think it is not bad to list any parent classes in between round brackets before the colon, but this class doesn’t have any, so i can leave them out. Inside the class body, we ve got  two functions – these are our object’s methods.

1. The first method: is called __init__, which is a special method. When we call the class object, a new instance of the class is created, and the __init__ method on this new object is immediately executed with all the parameters that we passed to the class object. The purpose of this method is thus to set up a new object using data that we have provided.

The second method is a custom method which calculates the age of our person using the birthdate and the current date.

i want to store the data in a db or in a file
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply
#2
If you don't have any specific requirements, then sqlalchemy is a pretty good library: https://docs.sqlalchemy.org/en/13/orm/tutorial.html
Reply
#3
good day dear nilamo,

many thanks for the quick rely with your tipp. I will look at this library.
....and besides this i will have a closer look at peewee. 



have a great day

have a great day Smile
Wordpress - super toolkits a. http://wpgear.org/ :: und b. https://github.com/miziomon/awesome-wordpress :: Awesome WordPress: A curated list of amazingly awesome WordPress resources and awesome python things https://github.com/vinta/awesome-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Store variable data and display sum after 60 seconds the_dude 11 3,368 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  Regex text file to store data in list TheSithSiggi 1 1,501 Dec-03-2020, 04:46 PM
Last Post: bowlofred
  How do I store the data in another txt file blacklight 1 1,905 Jun-26-2020, 11:09 AM
Last Post: Larz60+
  Assigning data read from CSV to class faruk61 2 2,080 Apr-15-2020, 05:52 PM
Last Post: buran
  Read csv file, parse data, and store in a dictionary markellefultz20 4 4,487 Nov-26-2019, 03:33 PM
Last Post: DeaD_EyE
  Reading blob data from database by python and store it in .zip format Adityasi 2 6,445 Nov-18-2019, 05:22 PM
Last Post: ibreeden
  Receive Serial Data and store in different Variables in Python jenkins43 5 5,488 Dec-28-2018, 01:33 PM
Last Post: snippsat
  user input to select and print data from another class python TyTheChosenOne 6 4,057 Aug-30-2018, 05:53 PM
Last Post: TyTheChosenOne
  how to parse multipart/form-data for xls or jpeg stream into python code and store v71017 0 3,275 Mar-20-2018, 01:09 PM
Last Post: v71017
  Trouble with a context manager class made with dunders Regulus 1 2,644 Jan-28-2018, 03:04 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020