Wednesday, March 8, 2023

The State Pattern in Python...

 In the State Design Pattern, the same object behaves differently depending on the current state of the object.

For example, take the case of Chicken preparation.

When it is delivered, it remains in an uncleaned state. Hence the first task will be to clean it. 

At that time if you call a specific method of the Chicken State, say, cook, it will behave differently (say maybe flash out a message that the chicken can't be cooked until it is properly cleaned and marinated). But the same cook method will cook the chicken once it is in the Marinated State. This time the "cook" method will properly cook the chicken and will flash out a different message - maybe like the Chicken is getting cooked - so have patience.

In the example, we have a ChickenState interface and then Mamma as the chef. The transition from one state to another is done at the end of the appropriate method of the interface at each state.

Enjoy the State Pattern written in written in Python


from __future__ import annotations
from abc import ABC, abstractmethod
import time

class Mamma():
def __init__(self, state):
self._state = state


def getstate(self):
return self._state


def setstate(self, state):
self._state = state


def startpreparation(self):
self._state.wash()
self._state.marinate()
self._state.cook()
self._state.serve()




class State(ABC):
@abstractmethod
def wash(self):
pass


@abstractmethod
def marinate(self):
pass


@abstractmethod
def cook(self):
pass


@abstractmethod
def serve(self):
pass




class UncleanedState(State):
def __init__(self, mamma):
self._mamma = mamma


def wash(self):
print("chicken is in the uncleaned state and is getting cleaned:next state will be cleaned state")
time.sleep(1)
self._mamma.setstate(CleanedState(self._mamma))


def marinate(self):
print("chicken is in the uncleaned state...first clean it")
pass


def cook(self):
print("chicken is in the uncleaned state...first clean it")
pass


def serve(self):
print("chicken is in the uncleaned state...first clean it")
pass




class CleanedState(State):
def __init__(self, mamma):
self._mamma = mamma


def wash(self):
print("chicken is in the cleaned state...wash already done")
pass


def marinate(self):
print("chicken is in the cleaned state and is marinated. next state will be marinate state")
time.sleep(1)
self._mamma.setstate(MarinateState(self._mamma))


def cook(self):
print("chicken is in the cleaned state...first marinate it")
pass


def serve(self):
print("chicken is in the cleaned state...first marinate it")
pass




class MarinateState(State):
def __init__(self, mamma):
self._mamma = mamma


def wash(self):
print("chicken is in the marinated state...cleaning already done")
pass


def marinate(self):
print("chicken is in the marinated state...marination already done")
pass


def cook(self):
print("chicken is in the marinated state and being cooked. :next state will be cooked state")
time.sleep(1)
self._mamma.setstate(CookedState(self._mamma))


def serve(self):
print("chicken is in the marinated state and being cooked. only after cooking u can serve...")
pass




class CookedState(State):
def __init__(self, mamma):
self._mamma = mamma


def wash(self):
print("chicken has already being cooked")
pass


def marinate(self):
print("chicken has already being cooked")
pass


def cook(self):
print("chicken has already being cooked")
pass


def serve(self):
print("chicken is ready and is served : this is the last state")
time.sleep(1)


print("guest are saying thank you for the meal")


if __name__ == '__main__':
mamma = Mamma(None)
mamma.setstate(UncleanedState(mamma))
mamma.startpreparation()







Tuesday, March 7, 2023

The Command Pattern in Python...

When I taught my son #Ridit and many other students about Python, I had to cover the subject of the Design Patterns implemented in Python.

Here we go. - the Command Pattern being implemented in Python.

We have taken the same Restaurant problem which has Chef, Waiter, and Order classes participating in this design pattern.

The idea of Command Pattern is that the decoupling between the invoker of the command and the executor.

The command is kind of a full-fledged object having all knowledge about the executor of the command meaning the command will have a reference to the executor of the command.

The waiter is working as the invoker and the chef is working as the executor of the command.

Here we go.

The Source Code


from abc import ABC , abstractmethod
import time
class Item:
    def __init__(self,name,price):
        self.name = name
        self.price = price
class ItemOrder:
    def __init__(self,item,numberOfPlates):
        self.item = item
        self.numberOfPlates = numberOfPlates
class Order:
    def __init__(self):
    	self.itemorders = []
    def getItemOrder(self):
        return self.itemorders
#executor
class Chef(ABC):
    @abstractmethod
    def prepare(self):
        pass


class ChefTeaCoffe(Chef):
    def prepare(self,order):
        for itemorder in order.itemorders:
            if "Tea" in itemorder.item.name:
                print(str(itemorder.numberOfPlates) + " cups of " + itemorder.item.name + " is being prepared")
                time.sleep(5)
                

class ChefFood(Chef):
    def prepare(self,order):
        for itemorder in order.itemorders:
            if "Food" in itemorder.item.name:
                print(str(itemorder.numberOfPlates) + " plates of " + itemorder.item.name + " is being prepared")
                time.sleep(5)
                
#command
class Command(ABC):
    @abstractmethod
    def execute(self):
        pass
class TeaCoffeeCommand(Command):
    def __init__(self,chef,order):
        self.chef = chef
        self.order = order

    def execute(self):
        self.chef.prepare(self.order)

class FoodCommand(Command):
    def __init__(self,chef,order):
        self.chef = chef
        self.order = order

    def execute(self):
        self.chef.prepare(self.order)
#invoker
class Waiter:
    def setCommand(self,command):
        self.command = command

    def giveCommandToChef(self):
        self.command.execute()


if __name__ == '__main__':
    itemTea = Item("Tea",40)

    itemBread = Item("Food", 20)

    itemOrderTea = ItemOrder(itemTea, 3)

    itemOrderBread = ItemOrder(itemBread, 4)

    orderTea = Order()
    orderFood = Order()

    orderTea.itemorders.append(itemOrderTea)
    orderFood.itemorders.append(itemOrderBread)

    chefTea = ChefTeaCoffe()
    chefFood = ChefFood()

    commandTeaCoffee = TeaCoffeeCommand(chefTea, orderTea)
    commandFood = FoodCommand(chefFood, orderFood)

    waiter = Waiter()
    waiter.setCommand(commandTeaCoffee)
    waiter.giveCommandToChef()

    waiter.setCommand(commandFood)
    waiter.giveCommandToChef()

Here is my son, Ridit, explaining the command design pattern implemented in Python.
I hope you will like it.



Monday, February 6, 2023

Thursday, January 12, 2023

A little ray of Hope at the end of the tunnel - a little progress in the journey of Fluid Simulation...




There is no shortcut to success...


It depends on sheer hard work, burning the midnight oil in front of the computer, and of course, luck and destiny...

On this auspicious day of Swami Vivekananda's birthday, I am reiterating my faith in #karmyog - this is the best way to ward off depression, loneliness, and all sorts of modern-day psychological hazards - the best way to attain #wisdom

Once the purpose of a person's life is attuned to his/her skill and ability - there won't be any looking back for that person.

I remember, one of the reasons for my coming to the software world was the hollowness of my job at the Indian corporate headquarter of a Fortune 25 company (at that time).

I used to say it loud that most of us at the junior level till the high and middle management level are simply busy doing time pass activities all through the day - wasting a lot of company resources by making useless presentations and all sorts of time passing documents just to showcase that we are all playing important roles in the company - whereas the multi-million dollar deal will most probably be done at the golf club and in between the top boss of the telecom service provider and the top boss from the mother organization of the transnational company.

So why waste time there?

Rather it was good to choose a profession where I can enjoy the result of my karma instantly on the monitor of the computer.

In the age when there was no Google, it was a difficult decision for me - however, the risk was worth to be considered.

I remember after I joined the software bandwagon and started understanding the nitty-gritty of Object-Oriented Software, the major hurdle was to decipher what the ## (double hashes) does in C++ (it was part of the boilerplate code of the MFC/VC++ message_map macro) which was automatically produced by the wizard.

And as there was no #Google, I had to wait quite a long before it became clear to me. Later, when I was deciphering the service framework code of the Android, I found similar code and I thanked my inquisitive nature that I was able to showcase in the beginning.

Knowledge is all-powerful and it makes one a really interesting person.
 

Sunday, January 8, 2023

Starting over a new tech journey...

A new year - a new journey with greater determination and willingness...


There is no shortcut to success - whatsoever way one may try to define SUCCESS...





And the necessary Engineering Maths - the partial differential calculus and all such kinds of stuff please study from the book which I read many years back


Saturday, December 3, 2022

The Future of Computing Science - the Quantum Computing - and right there will be the Vedic World - just as the opposite side of the coin...

 

The Future of Computing Science is Quantum Computing...



And now the surprise

For the Humans of the UNIVERSE...

The other side of The Coin of Quantum Computing is the Vedic World...


The Future of Computing — Quantum Computing

Quantum computing represents a radical shift in the way we understand and process information. Traditional (classical) computing is based on bits, which can be 0 or 1. Quantum computing is based on qubits, which can be 0, 1, or both at the same time (superposition), and they can be entangled, meaning their states are deeply linked.

Key Ideas:

  • Superposition: A qubit can exist in multiple states simultaneously.

  • Entanglement: Qubits can influence each other instantly across distance.

  • Quantum gates: Information is processed in parallel across all possible states.

Quantum computing isn't just faster computing — it’s a fundamentally different paradigm. It challenges:

  • Our classical logic

  • Our deterministic view of cause and effect

  • Our understanding of information itself

The Vedic World — Consciousness and Non-Duality

The Vedic worldview, particularly as expressed in Advaita Vedanta, posits that:

  • The ultimate reality is non-dual (Advaita) — there is only one consciousness, Brahman.

  • The world of separateness and form is Maya — an illusion, or a limited appearance of a deeper, unified truth.

  • Time is cyclical, and consciousness is fundamental — not a byproduct of the brain, but the ground of being itself.

Where quantum computing deals with entangled possibilities and probabilistic realities, the Vedic tradition speaks of oneness, interconnectedness, and the illusory nature of duality.

🧭 Two Sides of the Same Coin?

Quantum computing and the Vedic world are not at odds — they are like Yin and Yang, or like the two sides of a coin. One explores reality through technology and math; the other through inner exploration and timeless wisdom.


Quantum Mechanics                                     Vedic Philosophy
Superposition                     Non-dual Brahman (all-in-one)
Entanglement                        Oneness of all beings
Observer affects the system                     Consciousness creates reality
Probabilistic                     Maya — reality is not fixed
Measurement collapses state                     Realization collapses illusion

🚀 A Synthesis: The Coming Age?

Imagine a future where:

  • Quantum computers are used to simulate consciousness, life, and the cosmos.

  • Scientists begin to validate ancient insights about the mind-matter relationship.

  • A new science emerges — Vedic Quantum Information — which does not just crunch numbers, but seeks understanding through experience.

The future journey of innovation...
  • A science of consciousness

  • A technology of the self

  • Spiritual computing, where intelligence, awareness, and compassion become built-in dimensions of machines and algorithms

✨ Final Thought

To say “Quantum Computing is the future — and Vedic World is the other side of the coin” is to recognize that:

The outer journey of computing and the inner journey of consciousness may finally converge.

When that convergence happens, we might not only build machines that think — we may also awaken beings that feel, know, and transcend.

And here's the #nemo of the society - trying to awaken the Humans of Bharat...

Read my original study of Upanishad




The result of my studies on this great scientific asset of #bharat to map the term infinity

Now-  as there were not really any authentic scientific studies of the dates when Upanishads were written - hence I would like to request the Humans of Bharat to take these things very seriously - if you really want to know #whoweare and reclaim our lost glory - in the new global order. There is no other way but to present the #original #bharat to the #universe 





The Humans of Bharat - you have spent a lot of time in hibernation
Now...
please wake up and reclaim who you are...

Educate the #UNIVERSE, especially the Western people who think that infinite is a recent term in Maths...



Read... Read...




And here we go.... My tryst with Para Vidya - the self realization....

Knowing thyself is knowing God...



Lyrics:

Apnake Ei Jana Amar Phurabe Na
Ei janari shonge shonge tomay chena
Koto jonom moronetey tomari oi charonotey
Apnake je debo, tobu barbe dena
Amaare je namtey hobe ghatey ghatey
Baare baare ei bhuboner praner haatey
Byabosha mor tomar shathe cholbe bere diney ratey
Apna niye korbo jotoi becha kena


English Translation:

My need to know myself is unending
With this each day my Lord I learn thee
Through endless cycles of life and death
At your feet, in surrender, I fall
Yet I remain forever in your thrall
I must cross each milestone in this sojourn
Time and again on this journey of life
The more I involve myself in worldly trades
The more I entwine in my dealing with you