Understanding and Implementing Schemas in Python

Understanding and Implementing Schemas in Python Introduction In the world of programming, particularly in the context of data management and validation, schemas play a vital role. A schema is essentially a blueprint or a predefined structure that defines the expected format, data types, and constraints for a given data entity. In this blog, we will delve into the concept of schemas in Python, exploring what they are, why they are important, and how you can implement them in your projects. What is a Schema? A schema serves as a contract between different components of a system, ensuring that data is consistent, valid, and well-structured. It defines the rules for how data should be organized, what fields it should contain, and what types of values those fields can hold. In essence, a schema acts as a set of rules that data must adhere to in order to be considered valid. Why Are Schemas Important? Data Validation: Schemas provide a way to validate incoming data. When data is received o...

Create a Simple Calculator App in React Js | Interview Question

Create a Simple Calculator App in React Js | Interview Question

In this post, you will learn about how to create simple calculator App in React js using hooks.

Following is the UI functionality of the application.

calculator

Sample Code:

A) App.js

import React, { useState } from 'react';
import './style.css';

const Header = () => {
  return <div id="header">Simple Calculator</div>;
};

export default function App() {
  const buttons = [
    { id: 1name: '7' },
    { id: 2name: '8' },
    { id: 3name: '9' },
    { id: 4name: 'Del' },
    { id: 5name: '4' },
    { id: 6name: '5' },
    { id: 7name: '6' },
    { id: 8name: '+' },
    { id: 9name: '1' },
    { id: 10name: '2' },
    { id: 11name: '3' },
    { id: 12name: '-' },
    { id: 13name: '.' },
    { id: 14name: '0' },
    { id: 15name: '/' },
    { id: 16name: '*' },
  ];
  const [textsetText] = useState('');
  const [showsetShow] = useState('');

  const onClickDel = (name=> {
    let numStr = text.substring(text.length - 10);
    setText(numStr);
  };

  const onClickGeneral = (name=> {
    let numStr = text + name;
    setText(numStr);
  };

  const submit = () => {
    let evalStatement;
    try {
      evalStatement = eval(text);
      setShow(evalStatement);
    } catch (err) {
      setShow('Error');
    }
  };

  const reset = () => {
    setShow('');
    setText('');
  };

  return (
    <div id="mainDiv">
      <Header />
      <input id="input" type="text" value={text} />
      <input id="output" type="text" value={show} />
      <div>
        {buttons &&
          buttons.length &&
          buttons.map((item=> {
            return (
              <div
                id="circle"
                onClick={
                  item.name == 'Del'
                    ? () => {
                        onClickDel(item.name);
                      }
                    : () => {
                        onClickGeneral(item.name);
                      }
                }
                key={item.id}
              >
                {item.name}
              </div>
            );
          })}
      </div>

      <button id="reset" onClick={reset}>
        {' '}
        Reset{' '}
      </button>
      <button id="submit" onClick={submit}>
        {' '}
        Submit{' '}
      </button>
    </div>
  );
}

CSS:

B) styles.css

#mainDiv {
  border1px solid black;
  width320px;
  height330px;
  background-colororange;
  border-radius30px;
}

#reset,
#submit {
  border1px solid black;
  floatleft;
  width80px;
  margin-left25px;
  margin-right25px;
  margin-top10px;
  font-weightbold;
}

#input,
#output {
  width260px;
  height30px;
  margin-top10px;
  margin-left25px;
  margin-right25px;
  margin-bottom2px;
  border-radius5px;
  font-weightbold;
}

#circle {
  width30px;
  height30px;
  background-colorrgb(162162162);
  border-radius10px;
  floatleft;
  text-aligncenter;
  margin-top10px;
  margin-left25px;
  margin-right25px;
  margin-bottom2px;
  line-height30px;
  font-weightbold;
}

#header {
  margin-left25px;
  margin-right25px;
  margin-top10px;
  text-aligncenter;
}


Happy Learning!! Happy Coding!!

Comments

Popular posts from this blog

useNavigate and useLocation hooks react-router-dom-v6

Localization in React Js

How to implement error boundaries in React Js

Pass data from child component to its parent component in React Js

Create a Shopping Item App using React Js and Xstate

How to fetch data using Axios Http Get Request in React Js?

How to fetch data from an API using fetch() method in React Js

Create a ToDo App in React Js | Interview Question

Routing in React using React-Router Version 6

Auto Increment, Decrement, Reset and Pause counter in React Js | Interview Question