Posts

How to Use Pytest Library to Test Python Funtions/Modules/Classes/APIS

how to initialize flask while using pytest- this is only for python+flask, other can leave this step from flask import Flask app = Flask(__name__) with app.app_context() :     def funtio():          pass          .... if __name__ == "__main__" : app.run() How to Install PYtest pip intall pytest how to run pytest: to run pytest we have different ways, one is using terminal, other one is using pycharm env setup now i am saying using terminal, open terminal >pytest (its run all test_*.py files) >pytest -v (display verbose_name) >pytest -v -s ( -s display print statement on termianal) >pytest <filename> -v -s (this is excute only specified file) >pytest -k <search_term> -v -s (this will search that <search_term> on funtion/method names, if it match then move to run other wise dis-select) >pytest <file_name> -k <search_term> -v -s >pytest -m <marker> -v -s (...

How To Convert Html File/URL/String Into Image Using Python

   I worked on converting HTML formate to PNG Image using python, its take 1 week to complete, First, I tried with  Imgkit  Package then I tried with  Html2image  Package, Later I tried with  Pypperteer  Package, When I used Imgkit package, I faced on one issue what it means, in HTML file we have any embedded code or more loading javascript code, its take more than 5 sec so when I used imgkit it is not taking those things on the image. when I used Html2image Package, I faced the same issue Finally, I solved that loading issue using Pypperteer Package ( https://github.com/pyppeteer/pyppeteer ), In Pypperteer Package we have one option, by using that option we can stop converting into the image until load the complete HTML file, And here we have one more great option to pass HTML string to get Document Elements like OffsetHight, etc. import asyncio from pyppeteer import launch def converting_html_image(input_url) -&gt; str:     file_na...

How to Insert/Copy content from One Html File to Other HTML File

  How to Insert/Copy content from One Html File to Other HTML File os.path.join(os.getcwd()) -> this will ge curent working directory path (sample HTML file content) content = '''<!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1">     <title>test</title>     <style>*{ margin: 0; padding: 0; width: 360px!important; max-width: 360px !important; }     </style> </head> <body> <div> <p>hello , This is paragraph content for testing purpose only</p> <script> </script>     <blockquote class="twitter-tweet" lang="en"> <a href="https://twitter.com/faniskoengage/status/1303453019204452352"></a> </blockquote> <script src="https://platform.twitter.com/widgets.js" charset="utf-8"><...

How to Crop or Resize the Image using python Script

Input - you need to pass input as a file path and save the out on the same path Ouput - Return True import os from PIP import Image  def compress_Me(file):      filepath = os.path.join(os.getcwd(), file)      oldsize = os.stat(filepath).st_size -    picture = Image.open(filepath) -    dim = picture.size -    top = 0 -    left = 0 -    right = 360 -    bottom = dim[1] -    cp = picture.crop((left, top, right, bottom)) -    cp.save(file, "PNG") -    newsize = os.stat(os.path.join(os.getcwd(), file)).st_size -    percent = (oldsize - newsize) / float(oldsize) * 100 -    print("File compressed from {0} to {1} or {2}%".format(oldsize, newsize, percent)) -    return True

Solution - Typeerror '<' not supported between instances of 'datetime.datetime' and 'str' while Sorted()

sorted(self.total_played_game_details, key=lambda i: i['start_datetime'], reverse=True) Type error '<' not supported between instances of 'datetime.datetime' and 'str'  Solution from datetime import datetime sorted(self.total_played_game_details, key=lambda i: datetime.strptime(str(i['start_datetime']), "%Y-%m-%d %H:%M:%S"))

HTTP Status Codes

  Informational 100 - Continue A status code of 100 indicates that (usually the first) part of a request has been received without any problems and that the rest of the request should now be sent. 101 - Switching Protocols HTTP 1.1 is just one type of protocol for transferring data on the web, and a status code of 101 indicates that the server is changing to the protocol it defines in the "Upgrade" header it returns to the client. For example, when requesting a page, a browser might receive a status code of 101, followed by an "Upgrade" header showing that the server is changing to a different version of HTTP. Successful 200 - OK The 200 status code is by far the most common returned. It means, simply, that the request was received and understood and is being processed. 201 - Created A 201 status code indicates that a request was successful and as a result, a resource has been created (for example a new page). 202 - Accepted The status code 202 indicates that the se...

MongoDB Basic & Statements

  Difference Between Terms and Syntax SQL Terms/Syntax MongoDB Terms/Syntax Database Database Table Collection Row Document Column Field Index Index Table $lookup or embedded docs Primary key Primary key Transactions Transactions Contrast Between SQL and MongoDB Statements CREATE and ALTER SQL Schema Statements MongoDB Schema Statements CREATE TABLE employee ( id MEDIUMINT NOT NULL AUTO_INCREMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) db.employee.insertOne { { id: "john25", name: john, status: "A" } ) However, you can also explicitly create a collection: db.createCollection (“employee”) ALTER TABLE employee ADD join_date DATETIME db.employee.updateMany( { }, {$set: {last_name: Adam}} ) ALTER TABLE employee DROP COLUMN join_date db.employee.updateMany( { }, {$unset: {“Age”: “”} } ) INSERT SQL INSERT Statements MongoDB insertOne() Statements INSERT INTO employee(user_id, age, status) VALUES ("test001", 45, "A") db.empl...