A Time Out From Real Coding

It feels as if it was longer, but apparently I needed some time out from actual coding for diversion,  reflection and introspection. (More here) and (here)
[Jump to latest thoughts-- click here]

I found every excuse for not doing OOP project #2 which is the abbreviation expansion class and its "objects".

Was it nothing more than falling into a bunch of easy-dopamine traps? Did I get stuck in the identity-politics debates? Did I let 'notifications' on my smart phone distract me? Perhaps. Or maybe I needed to rewire my mental models in the background (the subconscious) of how OOP objects should be viewed?

So now I have a new view of what the definitions should be for what is a "class"? For what is an "object",  "attributes", "methods", the init method and so on; all to be found (HERE = an earlier OML2code post)

Given that, I've also re-designed the plan for my expander cookie-cutter (aka 'class') per the following:

class AbvXpandr:
    # This is my SECOND OOP attempt. Below is position of params when a Abbreviations Expander object/widget is instantiated
    # p1= in_txt = input text includes abbreviation initiators /triggers
    # p2= out_txt = to-be -generated expanded text (Abbreviations are Expanded]
    # p3= abvi = single character abbreviation initiator (e.g. the tilted quote under the Tilda key)
    #     the p3 is followed by a single char other than p3 or p4, the single is expanded via p5 Dictionary
   # p4= abvjj = start/end delimiter between which we have N>0 chars as abv code (e..g the Tilda "~")
    #     Special case 01: seq of p3p3 chars (no space) is a special, most often used abbreviation (eg white forefront text on black background escape strings)
    #     Special case 02: seq of p4p4 (no space) is a special, 2nd most often used abbreviation (eg yellow forefront on black background)
    #     Special case 03: seq of p3p4 (no space between) is a special (e.g. white on blue),
    #     Special case 04: seq of p4p3 (no space between) is a special (e.g. blue on yellow),
    #     Special case 05: seq of p3p3p3 (no spaces between) is a special, --perhaps? This allowing for longer permutations of p3/p4 with no spaces between for most often used abbreviations that are easy to type in 
    # p5= abvD = the abbreviations lookup Dictionary for p3 { "a": 'allowance ', "b": 'because ' ... "z": 'zero '}
    # p6= abvDD = the abbreviations lookup Dictionary for a p4 pair { "ab": 'about ', "qab": 'Query About ' ... "z": '??? '}
    # p7= abvDDS = the abbreviations lookup Dictionary for special p3/p4 cases { p3p3: white, p4p4: yellow,  ... "p3p4": '? '}
    # p8= module_name = where to extract abvD, abvDD and abvDDS dictionaries from
    # p9= default_max =50 #default max_count if loop max_count is excessive

  def __init__(self, in_txt, out_txt, abvi, abvjj, abvD, abvDD, module_name, default_max):
            self.xxx = xxx

TO BE CONTINUED
Next, the plan is to write bird's eye pseudo-code such as
(1) get input text
(2) scan input text for syntax violations and special case sequences, save indices for these
(3) scan input text for non-special expander codes (e.g. [p3][c1] or [p4][c2][c3][c4][p4]
            where c1, c2, ... cN are the abbreviation codes that are to be expanded), save indices
(4) apply found keys to appropriate dictionaries
(5) append to growing output text 

The following is an initial "draft" probably with many errors
It just forms the basic idea for the data structures to be generated for analyzing the input:

    def explain(self):
        print(fr''' --- ''') #triple quote marks

   def find_p34s (in_txt):     #discover where all the initiators (p3's/p4's) are in the input text
        p3_indices_list = [], p4_indices_list = []  #start with empty lists
        for i in range(len(in_txt)-1):              #step through i=0 to i= len()-1
            if in_txt[i] == p3:
                p3_indices_list.append(i)
            if in_txt[i] == p4:
                p4_indices_list.append(i)   #QUESTION: Is there a string method that finds them all ???
        return p3_indices_list, p4_indices_list

Confusions and Answers to those Confusions:

(1) gQAB ::: In a python class def, after assigning self.parameter in init, should I refer to the parameter alone or with the dot notation?

Gemini answer: Inside a Python class, you should use dot notation (self.parameter) to refer to the attribute throughout your methods. While using the bare parameter name works if you pass it into a function locally, self.parameter ensures you are accessing the specific instance's data.

My thoughts: Can I use a Decorator to convert the dot notation assignment into a non-dot notation? --- to be worked on later

Better yet, in the __init__ method, can I assign the P1 through Pn variable names as aliases to the parameters in their givn order? 

(2) gQAB ::: In a python, which string methods should I use to detect a char and determine its index?

Gemini answerTo detect a character and determine its index in a Python string, you should primarily use the find() or index() string methods. Both return the index of the first occurrence of a character, but they behave differently if the character is missing.

G4G answers: (2a) Using Regular Expressions (re.search())
    (2b) Using index() : 
The index() method returns the index of the first occurrence of a character. If not found, it raises a ValueError.
    (2c) 
Using a Loop : with range() and len()
    (2d) Using find() : 
find() method returns the index of the first match. If not found, it returns -1.
    (2e) Using enumerate() with next() : This approach uses list comprehension and lazy evaluation to get the first index where the character matches.
        def find_pos(s, k):
            try:
                return next(i for i, c in enumerate(s) if c == k)
            except StopIteration:
                return -1

        s = 'xybze'
        k = 'b'
        print(find_pos(s, k))

[ related G4G articles: enumerate(), next(), reverese find= findr(), re.search() ]


gQAB ::: Python string methods (here) ::: G4G listing (here) [ (count)_+  (find)_+  ++(in)
                                                        (index)_+ , ..... ] 
gQAB ::: Python list     methods (here)   ::: G4G listing (here) [ (count), (append), ...        

MORE TO EXPLORE

() = --
    --^^^--
    --^^^--
    --^^^--
    --^^^--

() = --
    --^^^--
    --^^^--
    --^^^--
    --^^^--

() = --
    --^^^--
    --^^^--
    --^^^--
    --^^^--

() = --
    --^^^--
    --^^^--
    --^^^--
    --^^^--

() = --
    --^^^--
    --^^^--
    --^^^--
    --^^^--

 

Comments

Popular posts from this blog

Links for Python Noobs

The Learn HOW to Learn Page

Welcome to Circular Import Hell