this is my first attempt to implement OOP. IS this class ok?(in terms of structure)
AND another Question:
there are some folders in message application in Symbian phones
and sometimes there are up to 1000 messages in each folders.
I do itterate over all messages for gathering their info about sender/reciever/etc
but its very slow to use this clas for every folder.
how can I make a class that prevents me from itteratin over and over?
some info:
a messaging folder accessed from python returns a list of IDs from all of messages
for every ID there is a dict that contains info about that message.

#-------------------------------------------------------------------------------
# Name:        DelSMS
# Purpose:     Application for deleting SMS messages in Nokia Symbian Phones
# Author:      M.S.
# Created:     22/09/2012
# Copyright:   (c) Mohsen Sarhady 2012
# Python Version:     PyS60 v1.45(Python 2.2.2)
#-------------------------------------------------------------------------------
# -*- coding: utf-8 -*-

import inbox

class DelSMS:
    """Class for deleting SMS messages with diferent possibilities:
       -Dlete Messages in different folders
       -Delete Messages for a contact in different folders
       -Delete messages that contain a search keyword"""

    def __init__(self, folder):
        self.folder=folder
        self.m=folder.sms_messages()

    def contacts_by_folder(self):
        """Returns the list of contacts in the given folder."""
        contacts = []
        if len(self.m) != 0:
            for idx in self.m:
                address = self.folder.address(idx)
                if address not in contacts:
                    contacts.append(address)
        contacts.sort()
        return contacts

    def del_by_folder(self):
        """Deletes messages in the given folder."""
        if len(self.m) != 0:
            for idx in self.m:
                self.folder.delete(idx)

    def del_by_contact(self, contact):
        """Deletes Messages by Contact in the given folder."""
        if len(self.m) != 0:
            for idx in self.m:
                address = self.folder.address(idx)
                if address == contact:
                    folder.delete(idx)

    def del_by_keyword(self, keyword):
        """Deletes messages that contain a given search term"""
        #It is a TO DO
        pass


if __name__ == '__main__':
    #Test:
    delsms = DelSMS(inbox.Inbox(inbox.ESent))
    contacts = delsms.contacts()
    print contacts

Dani AI

Generated

Good first attempt. Two practical improvements will make the class much more usable on S60/PyS60 (N95, S60v3): fix a couple of small bugs in the posted methods, and avoid re-scanning the same 1,000+ messages by building a one-time index (or a lazily-built cache) of message IDs keyed by sender/recipient. As noted, subclassing inbox.Inbox is possible, but wrapping the existing folder object (composition) is generally safer for adding utilities without changing or depending on internal behavior of the phone API.

A simple, robust approach is to scan the folder once to build an address -> [msg_id,...] map and then operate against that map. Example pattern (uses the same folder API methods shown in the thread: sms_messages(), address(id), delete(id)):

ids = folder.sms_messages()
index = {}
for mid in ids:
    addr = folder.address(mid)
    index.setdefault(addr, []).append(mid)

# delete all messages for a contact (use a snapshot)
for mid in list(index.get(target_contact, [])):
    try:
        folder.delete(mid)
        index[target_contact].remove(mid)
    except Exception:
        # handle/log failed deletion, keep index consistent
        pass

A few additional, concrete tips based on the posted code: the del_by_contact method calls folder.delete(idx) — it should use self.folder.delete(idx). In the test block the method name used is incorrect (contacts() vs the implemented contacts_by_folder()); fix the names. Normalize addresses before comparing (strip non-digits for numbers with re.sub(r'\D','', addr), or compare trailing digits for international formats) to avoid mismatches. Finally, provide refresh()/invalidate() methods so the cache can be rebuilt when new messages arrive or after bulk deletes; update the cache only after confirmed deletions to avoid stale state. These changes keep per-operation cost near O(1) after an initial O(n) scan and work well on constrained phones.

Recommended Answers

All 2 Replies

I would think you would be better off to add functionality to inbox.Inbox by inheriting, I do not see what object the DelSMS corresponds with.

actually inbox.Inbox in my phone(s60v3 n95) lacks some functionallity that I need.say for example, if I wanted to delete only recived messages from only one contact among 60 contacts and 1300+ SMSs, there is no option to do that.
(in newer symbian^3 it is almost possible, but not in s60v3).

I am just using the Python API for accessing Messaging on my phone. this class is just for collecting the functions I want use for each messaging folder(i.e. Inbox, Sent, Draft, Outbox).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.