Tuesday, 15 March 2022

Adding multiple variables in an object - need further assistance

First thing first - upon suggestions, please do your best to be understandable for newbies as if it is too complex, it may not be much useful as I need to furtherly continue after the current ask. Thank you in advance for that :)

I'm trying to define an object with multiple variables that I may use. So far I was able to create the basic class for myself (with just ID of the object), but I am now struggling to add the rest of the variables needed for the object.

The data that I have to store with the multiple instance of the object is as follows:

  • id of the user - this is the value thru which I need to be searching thru the objects as I will have multiple entries of the below example data for different time intervals that I need to count. It does not need to be changed within the objects variables.
  • Name - The name of the person for whom I will be counting the hours spent. It is static (does not need to be changed within the objects variables).
  • Started timestamp and Ended timestamp - The time within which the person has executed things. As I will have multiple instances of data coming towards the object, I need to check for overlapping of shifts and if so, such hours to be avoided, but if extra hours beside the overlapped - to be added. E.g. if overlapping is not a perfect match, then the additional time spent to be added to the "total spent hours". The data received for both timestamps are in format that I convert into datatime with "datetime.strptime(start, '%Y-%m-%dT%H:%M:%S+02:00')
  • Schedule ID - it is the ID of the entry for the started and ended timestamps. It may be saved as an array as it will not be used except for reporting purposes - e.g. the person has processed things during it's first shift (start_timestamp thru end_timestamp).
  • Array of contacts that I need to separate to two different values - one for e-mail, other for phone number (including country code). The array returns as [email, country_code, phone_number]

Quote of example data that I have:

PersonID: ID1234
Name: Anton Todorov
Started at: 2022-12-26T00:00:00+02:00
Ended at: 2022-12-26T02:00:00+02:00
Schedule ID: SCHEDID1
Contacts: ['a.todorov@e-mail.email', 359, '000000000']
---===Separator===---
PersonID: ID5678
Name: Morgan Freeman
Started at: 2022-12-26T02:00:00+02:00
Ended at: 2022-12-26T14:00:00+02:00
Schedule ID: SCHEDID2
Contacts: ['slogan@draftkings.com', 1, '0000000000']
---===Separator===---
PersonID: ID1234
Name: Anton Todorov
Started at: 2022-12-26T14:00:00+02:00
Ended at: 2022-12-27T02:00:00+02:00
Schedule ID: SCHEDID3
Contacts: ['a.todorov@e-mail.email', 359, '000000000']

So with that on, I have to calculate the total hours that each person has spend from within these sections of data that I have.

The object that I have so far is as follows:

class DataItem(object):
    def __init__(self, person_id):
        self._person_id = person_id

        self._updatable_id = ""

    @property
    def person_id(self):
        return self._person_id

    @property
    def updatable_id(self):
        return self._updatable_id

    @updatable_id.setter
    def updatable_id(self, value):
        self._updatable_id = value

    @updatable_id.deleter
    def updatable_id(self):
        del self._updatable_id


class Persons(object):
    def __init__(self):
        self._ids = []

    def find_person_by_id(self, person_id):
        # search by id
        existing = [i for i in self._ids if i.person_id == person_id]

        if not existing:
            # create and append
            existing_person = DataItem(id)
            self._ids.append(existing_person)

        else:
            # assign to existing
            existing_person = existing[0]

        # return the object to be acted upon
        return existing_person

So.. Would someone be able to assist me with furtherly developing the object so that I may be storing the data properly inside of each of its instances, please?

I would gladly appreciate all detailed suggestions (especially as soon as I am also able to understand them).

Thank you all in advance!



from Adding multiple variables in an object - need further assistance

No comments:

Post a Comment