Manipulating image position using CSS

Introduction

AI generated CSS3
CSS3 Image generated by AI

Centering an image alongside text in a container might seem straightforward, but dealing with the differences between block and inline elements often complicates the task. While it's easy to position these elements separately, combining them can present unexpected challenges. I recently encountered this issue when trying to align an image to the left and center the heading within the same container. After experimenting with outdated techniques like float, I turned to modern CSS tools like Flexbox to find a cleaner, more efficient solution. This article explores my approach and the lessons learned along the way.

The challenge

The text I was using was my site heading with the HTML element <h1. which is a block-level element, which means that it doesn't play nice with other elements on the same line. It starts on a new line and needs to take up the entire line. Whereas the <img> element is an inline element which means it is happy to play with others on the same line. Of course, I could play with the CSS float property, but there is a problem. The moment I introduce float for an element, it takes that element out of the document flow. and it becomes hard to control the behavior of that element.

Options

As mentioned earlier, I tried using float: left and float: inline-start, but it doesn't always behave as I want. As a best practice, I try to use the latest techniques as much as possible and that's where the modern flex and CSS GridBox came in. Flexbox when assigned to the parent container, aligns all the content text to the left as shown below.

Text aligned to left with just flex
Text aligned to left with just flex

After a lot of trial and error, it came down to using specificity and going minimalist. I also wanted to have the option to style images that I might use on the site independently so I didn't apply any styling to the core img element. I created several classes to manipulate the images and applied those. During all this trial and error, another problem vexed me. I couldn't get the image to align to the middle of the parent container with all the techniques I knew. I researched and tried with align-self property. That finally worked. I didn't want to apply this to the core img element and I didn't want to create a class for this so I used the Child Combinator to target the specific img element which is a child of header element (header > img). That took care of the issue of image alignment.

The next issue was to align the header text in the center. I tried all the tricks I knew with text-align, align-self, align-items, justify-self, and justify-items. But because the parent header element was marked as flex, the subsequent styles didn't apply. Finally I tried a simple trick to center the content using margin: auto and that did the trick. Here's how the final output looks now.

This is the final result
This is the final result

Even when I change the height of the header container, the image and text are vertically in the middle of the element and stay where there on the x-axis.

Final code

HTML code:

<header class="flexi">
  <img class="round-img small" src="img/Mukul-2019.jpg" alt="Mukul Dharwadkar" caption="Picture of Mukul Dharwadkar" />
  <h1 class="center-align">
    Mukul Dharwadkar
  </h1>
</header>

CSS code:

header {
    width: 900px;
    margin: auto;
    height: 120px;
    background-color: antiquewhite;
  }

/* The CSS rule below is highly specific for an img element that is a child of the header element.
Typically there will be only one img element inside the header and therefore this is safe to keep */

header > img {
  align-self: center;
}

.flexi {
  display: flex;
}

.round-img {
  border-radius: 50%;
}

.small {
  width: 100px;
}

.flexi {
  display: flex;
}

.center-align {
  margin: auto;
}

The full code is on my Github repo. Feel free to use it.

Conclusion:

Achieving the perfect alignment of images and text in web design often requires experimenting with different CSS techniques. In this case, Flexbox proved to be the most efficient and modern solution for centering content within a container, while maintaining the flexibility to adjust styling independently. By using targeted selectors like the Child Combinator and leveraging Flexbox’s alignment properties, I was able to solve the issue cleanly and efficiently. This method not only streamlines the code but also ensures that future adjustments will be easier to manage. CSS can be tricky, but with the right approach, you can create polished, professional layouts.

Powered by AI

Introduction

AI
AI
Artificial Intelligence (AI) is the current buzz word in every industry, not only IT. No conversation between IT or business leader nowadays is complete with a reference to AI or ML. One would be not mistaken to think that AI is the magical wand that is going to solve all our problems. Or is it? This is my take on the use (or overuse / abuse) of the term AI in today's conversations and marketing. What does true AI mean? Where does the intelligence come in? How do you define intelligence? Does the word intelligence mean true intelligence which can somehow mimic human-like brain and decisions or does it mean intelligence like a lot of information as used in government agencies?

True AI, or artificial general intelligence (AGI), implies the ability to mimic human reasoning, adapt to new situations, and solve problems in a wide range of contexts. However, most of what is referred to as AI today is more about automation, data-driven decision-making, and machine learning, which excels in specific tasks but lacks the holistic understanding that real intelligence implies. Whereas Machine Learning (ML) focuses on algorithms that learn from data without being explicitly programmed

Powered by AI?

In more and more advertisements and marketing collateral are all peppered with AI, ML, Autonomous or some variant thereof. But when you think about it, what does powered by AI really means. So I decided to dive in. I examined three different services in some detail.

  • Workday's AI driven HR
  • Juniper's AI driven support
  • Duolingo's AI powered language learning

Workday's AI driven HR

When I looked at the services that Workday claims to provide by AI, the things that stood out for me were mostly around candidate matching, skills matching, and insights in several domains. Honestly, none of this is AI driven. All of this can be and has been done using analytics in spreadsheets on a smaller scale or BI tools for enterprise scale. As far as the matching is concerned, I believe it is more miss than hit. The beauty of humans is the unpredictability and un-repeatability. That's why there term human error is common place. It is very rare for a human to repeat the previous step / action exactly.

The other areas that the AI falls short is decision support system or sentiment analysis. Making decision based on varying data points is not easy. Humans can do it very easily but machines fail spectacularly in actually helping with decisions or sentiment analysis. The point I am trying to make is that everyone writes their resume and skills differently. Companies have tried to standardize it by providing drop down lists, but then they dilute it by allowing candidates to enter their skills as free text. This leads to a very low percentage of candidate matching using the Application Tracking System (ATS) which many companies use to screen resumes automatically.

Juniper's AI driven support

Being from IT operations background, this is was more interesting to me than the others. I read through Juniper's solution briefs, web pages as much as I could without giving my email or contact information and I came away a little disappointed. The whole pitch is based on analysis of past data and Juniper claims to use 9 years of reinforced learning to deliver that platform. I was looking for predictive detection and auto-heal / self heal of issues. All the solutions talked about were accurate troubleshooting help, quick deployments of solutions, and reduction of number of tickets by 90%. I admit 90% reduction in tickets is impressive, but do you really need AI for that?

Around 12 years ago, I built tools to automatically heal application instances and server instance when certain conditions were fulfilled. Those were simple shell scripts that I wrote based on a few months of observation and interaction. I also updated those scripts to handle different scenarios. This reduced tickets from 150 a month to 10 tickets that were real issues.
During the same time, I built tools to deploy applications to over 100 servers within 15 minutes instead of over 4 hours that it used to take before my automation.

It was not AI. It was just pure and simple common sense and a desire to avoid manual labor as much as possible.

Duolingo's AI powered language learning

I admit that this is a service that I have explored the least. I am learning a language using Duolingo. Duolingo claims that if I subscribe to their Super plan, I get AI driven insights into my mistakes and guidance on what mistakes I made. I tried their Super plan as a trial and during that it told me what mistake I made and what I should have used instead. To be fair, the way they are using the data that they gather from a learner, is to personalize the learning experience by focusing on the student's weak areas and trying to emphasize and reinforce those concepts. Which again is more of ML than true AI. It would be better to use the data to tailor the subsequent lessons to focus on the weaker concepts rather than just during the review of lesson. Generally speaking when learning a new language, grammar is the most difficult part. Use the data collected during lessons to focus on the students weak areas.

Conclusion:

AI Hype
AI and its Hype
With the explosion of Gen AI in the past couple of years, everyone is trying to get on the AI bandwagon. But in my humble opinion, the word intelligence used here is not the cognition or understanding that we term intelligence as, but rather it is a lot of information and based on that the computer will make some decisions. It indeed reduces a lot of workload, but I would strongly urge to stop using AI in every conversation or marketing collateral but rather focus on actually solving the problems and make human lives better. According to a 2023 Gartner report, only 6% of companies using ‘AI-powered’ solutions are deploying what experts would classify as advanced machine learning or artificial intelligence. The graphic in the report is very illuminating with GenAI being at the top of "Peak of Inflated Expectations".

Using AI in everything creates unrealistic expectations in people's minds. Rather than contributing to the AI hype, companies should be transparent about what their technologies actually do — whether it's data-driven automation or advanced machine learning. This will help set realistic expectations and foster trust in the potential of AI to truly improve human lives in meaningful ways. The customer should also be aware of the marketing techniques and really try to peel the layers of the solution being sold to understand what they are buying.

Beautify your terminal with Zsh and themes

Introduction

Oh MysZsh
Oh my zsh logo

I have been using Linux for a better part of two decades now but I won't say that I am an expert in Linux. Till recently I didn't even know that I could change shells and what capabilities other shells have. I was more of a functional and casual user than anything else who knew how to get a few things done but didn't know the internals and the intricacies of the operating system. One of the intricacies is to change the default Linux shell from bash to zsh. Zsh is indeed a powerful shell with a lot of extensions, customizations, and community support with themes to tailor the shell to ones own preferences. That was another thing I didn't know that there are themes for shell. I always used to think that bash shell is what you have and you have to use it without complaints.

Well, I was wrong and there are a lot of things you can change about your terminal and in this blog post, I am going to show you how to do it. I know there are a lot of tutorials around the web that show you exactly this and some are good than others. But this is more for me than anybody else. Really. When I started working for AWS, I got an opportunity to meet very smart people who have extended the capabilities of software tools to the limit of what they can do and then some. That is one of the reasons why I wanted to learn more and more about the basics so that I can have a very solid foundation.

Why Zsh?

By installing a different shell, you can actually decorate and customize your plain old command prompt to something fancy very easily and you have a lot of options. Of course, you can also customize and decorate your command prompt on bash shell, but for that you need to know a lot of configurations and parameters if not all. And you need to build the configuration file by hand as well.

As I mentioned earlier, there is a vibrant and strong community support and there are several themes available along with pre-made scripts that you can just run and choose options to configure and tailor. That's what I plan to show you in this post.

Preparation

All the Linux installations come with a bash shell by default. Depending on the distribution you use, there might be other shells also installed but we need to ensure that Zsh is installed. I am using Ubuntu to show the commands. The steps remain the same on a Red Hat clone as well. Red Hat clones use yum to install and maintain software whereas Debian and its clones use apt.

  • Ensure you have Zsh installed. Run the following command.
    sudo apt install zsh -y

  • Check if the Zsh installation is done properly and where it is installed by running the following command.
    which zsh
    It should show an output something like /usr/bin/zsh

  • Now that Zsh is installed, change your default shell to zsh by running the following command.
    chsh -s $(which zsh)

Once the command runs successfully, you will need to restart your session in order for the terminal to start in the new shell environment. Once the terminal starts, the Zsh configuration scripts starts automatically to enable the user to configure settings and preferences. We are not going to do any configurations at this time and will leave this for later once all the compoents are installed and ready. On the prompt, select 0 to Exit and create a placeholder .zshrc file so that the script doesn't run again.
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.

Install and setup

Now we are getting to the good part. First of all we need to install and download Oh-my-zsh. Oh-My-Zsh is a delightful, open source, community-driven framework for managing your ZSH configuration. Download and run it using the either of the commands below. Generally all Linux installations come with wget preinstalled and you will have to install curl. Curl is a powerful tool for testing and debugging and if you don't have it, don't worry about it and don't bother downloading it. If you don't have it and don't know about it, you probably won't need it.

sh -c "$(wget https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
==OR==
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Next we need to make the terminal our own by installing and configuring themes. There are a lot of themes available for Zsh but my favorite is PowerLevel10K. To use it first clone the Powerlevel10K repo using git. If you don't have git, install it by running sudo apt install git -y on your terminal.

git clone https://github.com/romkatv/powerlevel10k.git $ZSH_CUSTOM/themes/powerlevel10k

Once this is done, we will clone auto-suggestions and syntax highlighting repos which are plugins which extend the capabilities of Zsh a lot. Again there a lot of plugins available for Oh-my-zsh but for now we will concentrate on these two.
git clone https://github.com/zsh-users/zsh-autosuggestions.git $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

Now that all the major components are downloaded and ready, it's time to personalize, configure and make the terminal prompt our own. Open .zshrc (It's located in your home folder) file in your favorite editor and add the following lines
ZSH_THEME="powerlevel10k/powerlevel10k"
POWERLEVEL9K_MODE="nerdfont-complete"

Uncomment the options to activate them as needed. I suggest the following options.
CASE_SENSITIVE="true" # Around line 21
ENABLE_CORRECTION="true" # Around line 45

Finally add the plugins that we cloned earlier for autosuggestions and syntax-highlighting at around line 71
plugins=(git zsh-autosuggestions zsh-syntax-highlighting sudo)

Once done, save and exit. We are not done yet. We need to install a font that will allow us to use cool fonts and icons to spice up the prompt. The font that we need is Fira mono font. While the font itself is free from Google, we need the patched version. The entire fonts library is available on Github. The fonts library is a huge one and you don't really need all of those. I recommend downloading Firamono medium font from my [site](Internal link here) and install it. To install it, you can either double-click on the font file and open it in File manager and click install or create a .fonts directory in your home folder and copy it there. I recommend the first option to install it system wide.

Once installed configure Terminal app preferences --> Profile --> Text to use Fira mono font to harness the power of Oh-my-zsh

At this point you need to log out and log back in to ensure the changes made so far are persistent.

After logging back in launch Terminal. The P10K configure will run for the first time. Follow the prompts. My suggestions are:

  • For Prompt Style choose (3) Rainbow
  • For Character Set choose (1) Unicode
  • Choose whether you want to show current time on the command prompt or not. I suggest (3) 12 hour format
  • For prompt separators choose (1) Angled
  • For prompt heads choose (1) Sharp
  • For prompt tails choose (1) Flat
  • For prompt height choose (1) One line
  • For prompt spacing choose (1) Compact
  • For Icons you can choose your preference. I like a lot of fonts and I recommend (2) Many icons
  • For Prompt flow choose (1) Concise. Again this is a personal choice. I recommend Concise
    • Choose (y) to select Transient Prompt to provide a clean work area
  • Choose (2) to have Quiet Instant prompt mode
    Choose to save to .zshrc and apply the changes we just made.

And that's it. You are done! You now have a fantastic command prompt.

How to create a bootable USB drive on MacBook

Introduction

Bootable USB Drive for MacBook
Bootable USB Drive

I like to tinker around with technology. I think that much will be evident from my website and the type of posts that I write here. Sometime back, I was trying to play around with a Raspberry Pi. It was a RPi zero so it didn't have a lot of capabilities, but I figured out that I could run Raspbian Buster or Debian Buster on it and also ran this website on it till recently when I migrated it to AWS.

The problem

For quite some time now, I have been using a MacBook as my primary computer and while it is a great machine for personal productivity and development, I didn't really dive deep on system administration. I needed to figure out how to format a USB drive and a SD card on Mac and write a bootable image on it.

I did a lot of research and I came across a site from a Microsoft engineer who had written a very nice article on this and I used that article to very easily achieve my task. But I can't find that anymore so instead of relying on someone else, I thought I will document it myself and also add some additional details so that others can benefit. I could have very easily used my Windows computer but that wouldn't be integral with my tinkerer nature.

Recently, I had revived an old 2009 laptop which refused to run any of newer OSes so I did another research on possible OSes that could run on it and figured that I could run Lubuntu on it easily. So I went ahead and downloaded the latest version of Lubuntu - Focal Fossa and set to the task of creating a bootable USB drive.

The technical details

To achieve this, I would need only two tools from my MacBook

  1. diskutil
  2. dd

Let's take a look at the details now.

The first thing that needs to be done is to determine the device details of the USB drive. To do that, first insert the drive in your USB port and run the command below:

diskutil list

[image here]

This command will show an output of the disks mounted. Determine the device details by looking at the disk size and note down the device details which will be in the form /dev/diskN where N is a number. Once the device number is determined, run the following command under root (sudo) privileges

sudo diskutil eraseDisk FAT32 LABEL MBRFormat /dev/diskN

[image here]

Make sure to replace the LABEL with the name you want and N with the number noted above.

It will take a few minutes to complete the process and once it is complete, run the following command:

diskutil unmountDisk /dev/diskN

Again taking care to replace N with the appropriate number.

Once the disk is unmounted, we are now ready to write the bootable image to the USB drive. To do so, run the following command:

sudo dd bs=1m if=/Path/to/fileimage.iso of=/dev/diskN

[image here]
Depending on the size of the image, this can take several minutes. You can check the progress of the process by pressing Ctrl+T on the screen. Once finished, run the following command to eject the disk from the computer gracefully.

diskutil eject /dev/diskN

Don't forget to replace the N!

Even more deeper details

Now that you understand the commands, let's take a detailed look at the verbs and the switches we used in the commands.

  • diskutil: We used the following verbs with this command.

    • list: This option lists all the drives that are attached and mounted on the operating system
    • eraseDisk (note the capital D): This option will erase the disk that is provided as an option. It also takes the following arguments:
      • Filesystem Type: Choose from FAT32, NTFS, EXT4 etc.
      • Label: The name to be given to the disk
      • Format: The format type of the disk. Valid Values are: APM (Apple Partition map), GPT (GUID Partition Table) and MBR (Master Boot record). Using MBR will ensure that the drive will be bootable on non-Mac machines as well.
      • Device: The device number that we noted earlier.
        Note: This option needs sudo or root privileges to run.
    • unmountDisk: This will unmount the entire disk including all the volumes that may be present on the disk. It needs the device argument to work.
    • eject: This will eject the disk from the computer and make it safe for the removal media to be removed from the computer without the risk of data corruption.
  • MARKDOWN_HASH1aabac6d068eef6a7bad3fdf50a05cc8MARKDOWNHASH: **[dd](https://en.wikipedia.org/wiki/Dd(Unix))* stands for data duplicator* and is used to copy and transform data from one device to another. It is a low level Linux command line utility which will be a great addition in any system administrator's toolkit. We used the following verbs and switches in this exercise:

    • bs: Stands for block size. The default block size for dd utility is 512 bytes and there's not one right size for setting a block size. There is a good discussion here. This operand sets both the input block size and output block size to the desired value which I have used as 1 mb.
    • if: Denotes the input file where the dd should read from instead of standard input.
    • of: Denotes the destination where dd should write to instead of standard output.

Conclusion:

That's it for now. I hope this short tutorial has been helpful to you. Instead of using the GUI tools, I have found that using these command line utilities provide a lot of flexibility and power to the system administrator but can be confusing at times and have potential to destroy data if used incorrectly

Fix 404 errors in Laravel application authenication

Introduction

I am not a Laravel programmer by any stretch of imagination but I like to think that I am a pretty good engineer who can solve problems regardless of the situation. Recently I faced a situation where I was working with one of my clients on setting up a validated environment for their customers using a standard operating procedure (SOP). They had gotten the application developed from a third party vendor. Long story short, since their target market is highly regulated industry, they must have detailed and exhaustive documentation of all steps of setting up and tearing down of the computer systems.

It's not easy to write SOPs

There is a video where a father is asking his kids for some instructions to make a peanut butter and jelly sandwich. While for most of us making a peanut butter and jelly sandwich is highly intuitive and easy, for a person who doesn't know what is to be done, it can be stupefying. When we write instructions, we know a lot of things as we have already done the task successfully and assume that the reader is already aware of a lot of things that we know about and do not include those steps in our documentation.
Sometimes it is different. We miss some steps in our documentation and when someone else finds out and reports the gaps, we quickly make the change and do not update the documentation, hoping the problem will not recur. That exacerbates the actual problem. In case someone else has to reproduce the environment using those SOPs then they are basically stuck.
Therefore it is very important to have accurate and correct documentation updated. Admittedly, it is tedious and cumbersome, but in the long run it definitely is the right thing to do.

Laravel and Apache server

I had a Laravel application that was front ended by an Apache server and the backend was MySQL database. The developer had setup an instance and my job was to replicate the same using the SOP that they had built to ensure they are accurate and the process is predictable and repeatable. I followed the steps exactly as documented. I was able to get to the login screen. So far so good. But when I tried to log in using the test credentials, I was shown a 404 - Page not found error. Initially, I worked with the developer and they did something and fixed it. Interestingly, they failed to mention it to me what they did and neither was it documented in the SOP. So as it happened, I had to rebuild the server to try and get the SOP working. I again faced that issue and this time, instead of reaching out to the developer, I did my own research and Voila' I was able to solve the problem.
The research led me far and wide and in one of the forums I found the possible answer which suggested setting AllowOverride All on the parent directory of the application. In my case it was /var/www. The post suggested to set it at the main Apache configuration file. But I always try to localize these solutions and not make global changes if I can help it in order to keep the installation secure. So I added a Directory directive in the VHost configuration file which enables AllowOverride All for that VHost only while leaving the original configuration untouched.

Conclusion

They say that Laravel is a programming language for coding artists with elegant frameworks. But these issues are pretty simple and will be prevalent in all applications that require authentication. Why is this not better documented? More importantly why does the authenticate or login action in Laravel return a 404 error?
I wonder...

Responsible and Trustworthy AI

Introduction

Smart City Powered by AI
A futuristic view of Smart City powered by AI
Recently, AI technologies have seen explosive growth. With more genAI applications being built it is important to understand the impact and consequences of AI. I saw an interesting quote recently: "We wanted AI to do our mundane tasks so that we could focus on creativity and not that AI to do creative tasks so that we can do our mundane tasks”.

There is also a risk of using AI as GenAI is exciting and will open up many opportunities to add to the global economy. But it also opens up a lot of risks and dangers to corporations and individuals alike. Responsible GenAI is not only the government's responsibility, but we all are collectively responsible for generating more benefits with genAI. I built a genAI-powered application to provide educators with a platform to develop personalized lesson plans. That experience gave me a first-hand perspective of the power of genAI and the responsibilities that come to us as technology leaders to ensure we use AI safely and that it is trustworthy.

Problems with AI.

As AI’s power increases, it also opens up several risks and problems. This is in no way a comprehensive list but I am just scratching the surface.

  • Inappropriate use: A technology can be used beneficially or maliciously. We have seen a lot of issues come up with deep fakes and misinformation that are known. There are several which are unknown.
  • AI Race: We have seen the negative consequences of the arms race and if governments get into an AI race who’s to say where it will end? The impact of the arms race was visible and I am afraid that with AI technology, we won’t even see the impact as it can be highly targeted and obfuscated.
  • Rogue AIs: Humans have been trying to build an autonomously intelligent robot for several decades. While the objective is to benefit the human race and planet Earth, the risk comes from if and when the AI becomes truly autonomous and humans lose control over them.
  • Misalignment: For now, the AI is a robot that does what it is told. If the instructions provided are not accurate and aligned with the desired objective, the results could be unexpected to catastrophic.
  • Hallucinations: GenAI LLMs are compared to parrots. Sometimes they listen / read other people’s work and parrot it back without any validation even if it is incorrect. There was a very famous and hilarious incident where the chat bot declared that Australia didn’t exist.
  • Conformism: Even today, the AI LLMs conform to human intelligence. I asked the bot what 20+20 was and it correctly responded that it is 40 and confirmed it when I asked if it was sure about it. However, when I said that I think 20+20 was 42, it corrected itself and said that it made a mistake. If the bots can do this with absolute truths, then I am worried about the veracity of the results in subjective situations.

Conclusion:

AI and Machine Learning (ML) has made a lot of progress in the past few years, but it still has a long way to go. Especially the LLMs. There are other key and more impactful applications of AI, but the risks of using AI at this time can be extremely dangerous for humans. AI and ML can help in accelerating and eliminating mundane tasks, but it requires strong human oversight to ensure the results are not only accurate and actually helpful for people but also solving the intended problems and not creating a cobra effect.

Implementing nested loops with dynamic step in Python3

Introduction

The other day, my son asked me about the result of evaluation of program in Java that he had in his AP Computer Science A (APCSA) course. This course is as a part of his high school senior curriculum. The problem was quite simple of nested for loops in the control flow section. The code is as below.

    a = 0;
        for (i = 0; i < 10; i++) {
            for (k = 0; k <= 5; k++) {
                for (z = 1; z <= 16; z = z * 2) {
                    a++;
                }
            }
       }
    print a

The problem

As you can see, the problem is simple enough. I had done this kind of for loop in shell scripting and in C language when I was learning it a long time ago and somehow I thought that this was the same way you run a for loop in Python as well. Of course I know Python quite well, so I thought I would quickly port this code in Python and run it to verify the answer of the problem (My son had gotten it right by the way and through a very simple mechanism, I must add).

The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object. However, Python doesn't allow the developer to define both the iteration step and halting condition. Instead Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence.

The syntax for a for loop in Python is very simple and intuitive like so:

    for i in some_list:
        <do something>

Most common use case is to iterate some operation a certain number of times. And the easiest way to do it is to use the range function. In the range function, the structure contains the start point, end point and the step. The step takes an integer as an input and it does not accept an expression as a step. Similarly as Python doesn't allow the developer to define the iteration step in the for loop, it posed a problem for me as I the program above required to have the step in the powers of 2 for each loop.

The research

I tried various combinations and options to change the step size in range function, but as the step is a positional argument and doesn't accept either keyword argument or an expression, the choices were limited to only integers (Range also accepts floats but it for another day). I searched far and wide for a solution but to no avail. Finally I hit on the a possible solution on (where else) Stack Overflow.

The person who asked the question had a similar goal as me. The answer to the question was kind of cryptic, but I was able to figure it out as the answer pointed me in the right direction.

The solution

The solution in the end was embarrassingly simple. Here's the final code for this: There are certain commented statements here which I have left here that I used to debug and understand what was going on.

    #########################################
    # Implement nested for loops in Python  #
    # Programmer: Mukul Dharwadkar          #
    # Date: 24 September 2021               #
    #########################################

    a = 0
    for i in range(10):
        for j in range(6):
            c=1
            for k in range(1, 17, c):
                while c < 17:
                    # print(f'Index c is {c}')
                    c = c * 2
                    #print(f"Index i is {i}")
                    #print(f"Index j is {j}")
                    a += 1
                #print(f"The value of a when is i, j, k and c is {i}, {j}, {k}, {c} is {a}")
    print(f'The value of a is {a}') 

As you can see, I needed to initialize a new counter c that would then be used as the step size. In the innermost for loop, I needed to create a stop condition as the counter was completely independent of all other variables. The while loop above creates that stop condition. Inside the while loop, I am incrementing the counter by the powers of two.

The actual operation of interest is the value of a which is incremented by one everytime the loop is executed. So essentially, the easy way once you figure out how many times each loop is executing is to multiple all those values (5 times 6 times 10 in this case) and arrive at the final answer of 300.

    The value of a is 300

Updating a DynamoDB attribute with a hyphen or dash (-) in the name using CLI or SDK

Background

As a part of my personal growth plan and work commitments, I am working on the AWS Certified Developer - Associate certification using the Linux Academy platform. In one of the lab exercises that I was doing on DynamoDB, there were requirements for updating DynamoDB attribute using SDK and perform conditional updates and atomic counters on the tables. Being what I am, I did not use the examples they had provided, but created by own table to create a database of books I own and proceeded to create my own attribute names for the items.

The problem

As it happened, I created attributes like book-title, book-author, book-price, etc. which in itself is not a problem. However, the lab exercise had me perform the item updates using the BOTO3 Python SDK which got me excited to learn new things. I used the example files that the trainer had provided and modified it to suit my environment and ran the script.

UpdateExpression='SET book-price = :val',
ExpressionAttributeValues={
    ':val': {'N': '15.37'},  
    ':currval': {'N': '0'} 
},
ConditionExpression='book-price = :currval',
ReturnValues="ALL_NEW"

To my dismay, I started encountering errors.

Traceback (most recent call last):
  File "conditional_write.py", line 18, in 
    ReturnValues="ALL_NEW"
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "-", near: "book-price"

The Solution

I reviewed my code to ensure that I had not introduced any bugs myself. After ensuring that I had not introduced any bugs by adding new attributes to an item without any dashes and running the script successfully, I starting practicing my Google-Fu. There I found this awesome post on stackoverflow along with a link to official AWS documentation. The official documentation however only talks about a dot being a special character and it doesn't list a dash (-). After following the instructions from the stackoverflow post, my new code looked like this:

UpdateExpression='SET #bp = :val',
ExpressionAttributeValues={
    ':val': {'N': '15.37'},  # Make sure we keep this line the same
    ':currval': {'N': '0'}  # What was the current value?
},
ExpressionAttributeNames={
    "#bp": "book-price"
    },
ConditionExpression='#bp = :currval',
ReturnValues="ALL_NEW"

And once I implemented this code it all started working correctly. I have left a feedback for the AWS documentation team and hopefully they will update the documentation. I just want to make sure that all the cases are at listed and documented so that developers and wannabes like me are not stuck.

Elections in the new world

Context:

A long running episode has just turned an important page right now. Robert Mueller finally testified in front of the congress and as I expected provided almost nothing to the lawmakers outside his report. The focus of media and most of the public was around collusion and obstruction of justice. Indeed, that was the most newsworthy story but in my opinion not the main story or threat to the democracies of the world. It was only Rep. Adam Schiff brought out the question of integrity and security of elections. Director Mueller had already highlighted it in his monologue of a press conference in May 2019. How will the elections look like in the new world?

Subtext:

It is an important aspect that all the democratically elected governments of the

Elections
Elections in India

world should be really worried about. In fact there are questions being asked of the validity of the Brexit referendum vote and even some of the assembly Elections results in India. Now defunct Cambridge Analytica is being suspected as being involved and even instrumental in altering the outcome of both the results.

Just imagine if the Pakistani intelligence agency ISI decides to engage itself in Indian politics. It can ensure a party that is sympathetic towards Pakistan comes to power. Or even worse, it can ensure that an incompetent leader becomes the prime minister of India. That would be a disaster not only for India but to the stability of the region and I daresay, even the world. I can't think of India being ruled by Congress party led by an inept leader like Rahul Gandhi.

With the world becoming more and more digital and online, governments of the world should take infinitely more care about ensuring the data security and integrity to ensure fair and correct results. We all see in day to day life how easy it is to hack any computer system and bring it down. The private companies of the world realize it and spend a fortune on securing their IT infrastructure. The governments also should realize it. The bureaucrats must eliminate of reduce bureaucracy to a large extent and actually care about the security and integrity of the election process and the integrity of the results.

Conclusion:

It is very easy to ensure the security of elections in the new world if you think about it. First of all, Government must appoint competent people to key positions with reasonable autonomy to perform their function. As a result of strong and fair oversight, it will ensure that the right policies and procedures are implemented. Politicians must be kept at more than an arms length from the entire process. State of the art technology should be implemented. Most importantly, the people involved in the process at the grass roots level should be provided training and right incentives.

This is the just the starting point. But we don't have a lot of time to get it right. The bad actors are already off the blocks and the race is on!!!

A “Brave” new browser (?)

While watching the recently concluded 2019 cricket world cup, I saw some ads for Alluva, which calls itself a prediction platform. I am not sure how it works, but that's not the point of this article. I signed up to Alluva and it had me create an account on MetaMask, to receive the Alluva tokens. There on MetaMask site, it was strongly encouraging using a new browser called Brave.

Get Brave!

The browser in itself if based on Chromium project and they state that they have "taken almost all of Google from the Chrome."

I was intrigued. I am not the one to shy away from testing out new technologies. So I decided to take it for a spin. I downloaded it and took it for a spin. The first few sites all worked fine as the browser's core code base is Chrome itself. But the moment I tried to connect to my corporate sites, it started acting up. I faced two main issues while browsing:

  1. For any SSO enabled site, it started asking for username and password instead of taking the authentication from the kerberos ticket.
  2. For any SAML federation redirects, the redirects just failed and the site failed to work.

These issues were a deal breaker for me. For all the technology evangelism I just can't see myself using two browsers for my needs. I needed to have one browser. I was about to give up and go back to tried and tested Firefox. But I refused to give up. I asked myself, if Chrome works, then why not Brave? What is different in Brave that is causing the issue. I found the answer in one of the feature request on GitHub and a Brave Community post. Looks like when the browser code was compiled the developers disabled a couple of flags that are needed for SSO integration with kerberos and SAML redirects.

  • --auth-server-whitelist
  • --auth-negotiate-delegate-whitelist

When I tried to run the browser by running from command line and passing correct arguments for these parameters, everything worked fine. But again it is not very easy to always run it from the command line and all your settings are lost. So I was looking for an answer to make the process automatic and repeatable. I searched a lot of forums and help sites and I found the answer on superuser.com. This gives a step by step explanation of how to configure command line parameters for any application.

I tried both methods, and finally settled on the second method as the best method.

I created a small application using MacOS automator. It worked well. But I always had to launch the application from wherever I had saved it. Launching it from the Dock instead of from the actual location even after pinning it to the Dock defaulted to the original application launcher. The second method modifies the application bundle so it is a little risky but with enough due diligence and case, you can do it.

You can download the brave browser by clicking here.

Verified by MonsterInsights