From 7eaf9eac491ff8fc8ae508d9fc7217a4e8b17d60 Mon Sep 17 00:00:00 2001 From: itsaryan72 Date: Thu, 14 Nov 2024 20:15:39 +0530 Subject: [PATCH] first project --- 1.Caluclator/file.py | 69 +++++++++++++++++++ 1.Caluclator/node_modules/.package-lock.json | 15 ++++ 1.Caluclator/node_modules/python/LICENSE.txt | 21 ++++++ 1.Caluclator/node_modules/python/README.md | 39 +++++++++++ .../node_modules/python/example/app.js | 19 +++++ .../node_modules/python/lib/python.js | 60 ++++++++++++++++ 1.Caluclator/node_modules/python/package.json | 13 ++++ .../node_modules/python/test/python.test.js | 22 ++++++ 1.Caluclator/package-lock.json | 20 ++++++ 1.Caluclator/package.json | 5 ++ 10 files changed, 283 insertions(+) create mode 100644 1.Caluclator/file.py create mode 100644 1.Caluclator/node_modules/.package-lock.json create mode 100644 1.Caluclator/node_modules/python/LICENSE.txt create mode 100644 1.Caluclator/node_modules/python/README.md create mode 100755 1.Caluclator/node_modules/python/example/app.js create mode 100644 1.Caluclator/node_modules/python/lib/python.js create mode 100644 1.Caluclator/node_modules/python/package.json create mode 100644 1.Caluclator/node_modules/python/test/python.test.js create mode 100644 1.Caluclator/package-lock.json create mode 100644 1.Caluclator/package.json diff --git a/1.Caluclator/file.py b/1.Caluclator/file.py new file mode 100644 index 0000000..d343683 --- /dev/null +++ b/1.Caluclator/file.py @@ -0,0 +1,69 @@ +# Make a calculator +# perform all operations +# use switch case and if else condition +# ask to perform calculation again if yes repeat else exit + + + + +def addition(a,b): + return a+b + +def subtraction(a,b): + return a-b + +def multiplication(a,b): + return a*b + +def division(a,b): + return a//b + + + +def calculator(): + while True: + try: + a = int(input("Enter your first number :")) + b = int(input("Enter your second number :")) + except ValueError: + print("Invalid input") + continue + + print("Operations:") + print("1.Addition") + print("2.Subtraction") + print("3.Multiplication") + print("4.Division") + print("5.Exit") + + ch = input("Enter your choice :") + + if ch == '1': + result = addition(a,b) + print(f"The addition of both numbers is:{result}") + + elif ch == '2': + result = subtraction(a,b) + print(f"The addition of both numbers is:{result}") + + elif ch == '3': + result = multiplication(a,b) + print(f"The addition of both numbers is:{result}") + + elif ch == '4': + result = division(a,b) + print(f"The addition of both numbers is:{result}") + + elif ch == '5': + exit() + + else: + print("Invalid input") + + repeat = input("Do you want to perform another calculation? (y/n): ") + if repeat!= 'y': + print("Exiting the calculator. Goodbye!") + break + + +calculator() diff --git a/1.Caluclator/node_modules/.package-lock.json b/1.Caluclator/node_modules/.package-lock.json new file mode 100644 index 0000000..2ebb288 --- /dev/null +++ b/1.Caluclator/node_modules/.package-lock.json @@ -0,0 +1,15 @@ +{ + "name": "caluclator", + "lockfileVersion": 3, + "requires": true, + "packages": { + "node_modules/python": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/python/-/python-0.0.4.tgz", + "integrity": "sha512-7avKA/6XxrwcGSDes8xGn7FHAUdAUQXKHtpjDulyv5/nm7TcPblmPRvXjjwx5knWHqeRiipqH/TZR2HhmJ4CGQ==", + "engines": { + "node": ">= 0.4.1" + } + } + } +} diff --git a/1.Caluclator/node_modules/python/LICENSE.txt b/1.Caluclator/node_modules/python/LICENSE.txt new file mode 100644 index 0000000..53655e4 --- /dev/null +++ b/1.Caluclator/node_modules/python/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2011 Darren DeRidder + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/1.Caluclator/node_modules/python/README.md b/1.Caluclator/node_modules/python/README.md new file mode 100644 index 0000000..3f57ba9 --- /dev/null +++ b/1.Caluclator/node_modules/python/README.md @@ -0,0 +1,39 @@ +node-python +=========== + +A super-simple wrapper for NodeJS to interact programatically with the Python shell. Enables the use of Python-based tools from Node. + +[![NPM Stats](https://nodei.co/npm/python.png?downloads=true&stars=true)](https://npmjs.org/package/python) + +![NPM Downloads](https://nodei.co/npm-dl/python.png?months=9) + +Example +------- +This example starts a python child process, reads stdin for python commands, pipes them through to the python shell and runs the callback method with the resulting output. State is preserved in the shell between calls. + +```javascript +// ------ +// app.js +// ------ +var python=require('python').shell; + +// a callback to handle the response +var mycallback = function(err, data) { + if (err) { + console.error(err); + } else { + console.log("Callback function got : " + data); + } +}; + +// to test, read and execute commands from stdin +process.stdin.resume(); +process.stdin.setEncoding('utf8'); +process.stdin.on('data', function(chunk) { + python(chunk, mycallback); +}); +``` + +License +------- +MIT diff --git a/1.Caluclator/node_modules/python/example/app.js b/1.Caluclator/node_modules/python/example/app.js new file mode 100755 index 0000000..fba8b1c --- /dev/null +++ b/1.Caluclator/node_modules/python/example/app.js @@ -0,0 +1,19 @@ +#!/usr/bin/env node +var python = require('../lib/python').shell; +var mycallback = function(err, data) { + if (err) { + console.error(err); + } else { + process.stdout.write(data + '\n>>> '); + } +}; +process.stdout.write('Using Python from NodeJS\n>>> '); +process.stdin.resume(); +process.stdin.setEncoding('utf8'); +process.stdin.on('data', function (chunk) { + python(chunk, mycallback); +}); + +process.stdin.on('end', function() { + python('quit()'); +}); diff --git a/1.Caluclator/node_modules/python/lib/python.js b/1.Caluclator/node_modules/python/lib/python.js new file mode 100644 index 0000000..c29c2a5 --- /dev/null +++ b/1.Caluclator/node_modules/python/lib/python.js @@ -0,0 +1,60 @@ +var util = require('util'); +var spawn = require('child_process').spawn; +var child = spawn('python',['-u','-i']); +var cmdQueue = new Array(); + + +child.stdout.on('data', handleStdout); +child.stderr.on('data', handleStderr); +child.on('exit', handleExit); + + +function handleStdout(data) { + var datastr = data.toString('utf8'); + var finished = false; + if (datastr.match(/Command Start\n/)) { + datastr = datastr.replace(/Command Start\n/,''); + } + if (datastr.match(/Command End\n/)) { + datastr = datastr.replace(/Command End\n/,''); + finished = true; + } + if (cmdQueue.length > 0) { + cmdQueue[0].data+=datastr; + } + if (finished) { + cmd = cmdQueue.shift(); + if (cmd && cmd.command) { + if (undefined != typeof cmd.callback) { + cmd.callback(null, cmd.data); + processQueue(); + } + } + } +}; + + +function handleStderr(data) { + processQueue(); +}; + +function processQueue() { + if (cmdQueue.length > 0 && cmdQueue[0].state === 'pending') { + cmdQueue[0].state = 'processing'; + child.stdin.write(cmdQueue[0].command, encoding='utf8'); + } +}; + + +function handleExit(code) { + console.log('child process exited with code ' + code); + process.exit(); +}; + + +this.shell = function (command, callback) { + command = 'print "Command Start"; ' + command + '\nprint "Command End"'; + if (command.charAt[command.length-1]!='\n') command += '\n'; + cmdQueue.push({'command':command, 'callback':callback, 'data': '', state: 'pending'}); + processQueue(); +}; diff --git a/1.Caluclator/node_modules/python/package.json b/1.Caluclator/node_modules/python/package.json new file mode 100644 index 0000000..3332f8e --- /dev/null +++ b/1.Caluclator/node_modules/python/package.json @@ -0,0 +1,13 @@ +{ + "author": "Darren DeRidder", + "name": "python", + "main": "./lib/python.js", + "description": "Interact with a long-running python child process", + "version": "0.0.4", + "homepage": "https://github.com/73rhodes/node-python", + "repository": { + "type": "git", + "url": "git://github.com/73rhodes/node-python.git" + }, + "engines": { "node": ">= 0.4.1" } +} diff --git a/1.Caluclator/node_modules/python/test/python.test.js b/1.Caluclator/node_modules/python/test/python.test.js new file mode 100644 index 0000000..1faeda2 --- /dev/null +++ b/1.Caluclator/node_modules/python/test/python.test.js @@ -0,0 +1,22 @@ +var assert = require('assert'); +var python = require('../lib/python').shell; + +var runTests = function() { + // Run a couple commands in series + python('print "Hello World!"', function(err, data) { + assert.equal('Hello World!\n', data); + console.log('test 1 ok!'); + python('print "Goodbye, Cruel World!"', function (err, data) { + assert.equal('Goodbye, Cruel World!\n', data); + console.log('test 2 ok!'); + python('quit()'); + }); + }); + // Run one in parallel with the first two + python('print "Asynch"', function (err, data) { + assert.equal('Asynch\n', data); + console.log('test 3 ok!'); + }); +}; + +runTests(); diff --git a/1.Caluclator/package-lock.json b/1.Caluclator/package-lock.json new file mode 100644 index 0000000..4affcc4 --- /dev/null +++ b/1.Caluclator/package-lock.json @@ -0,0 +1,20 @@ +{ + "name": "caluclator", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "python": "^0.0.4" + } + }, + "node_modules/python": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/python/-/python-0.0.4.tgz", + "integrity": "sha512-7avKA/6XxrwcGSDes8xGn7FHAUdAUQXKHtpjDulyv5/nm7TcPblmPRvXjjwx5knWHqeRiipqH/TZR2HhmJ4CGQ==", + "engines": { + "node": ">= 0.4.1" + } + } + } +} diff --git a/1.Caluclator/package.json b/1.Caluclator/package.json new file mode 100644 index 0000000..d126a00 --- /dev/null +++ b/1.Caluclator/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "python": "^0.0.4" + } +}