Posts

Showing posts from February, 2023

Python Trimming Scraped Data

Trimming scraped data means removing unwanted or unnecessary parts of the data that you have collected from a web page. For example, you may want to trim whitespace, HTML tags, punctuation marks, or irrelevant text from your scraped data. One way to trim scraped data using Python is to use Pandas module, which provides various methods for data cleaning and manipulation. Pandas can read HTML tables from a web page and convert them into DataFrame objects, which are tabular structures that can be easily filtered, sorted, and modified. Another way to trim scraped data using Python is to use BeautifulSoup module, which allows you to parse HTML documents and extract elements based on their tags, attributes, or content. BeautifulSoup can also help you remove HTML tags, convert text into different formats, and handle encoding issues. When we scrape some text, heading there is a lot of unwanted text (\t, \n, \t, etc.) also get scraped. Trimming is a way to getting rid of that unwanted data....

Python Type Checking

Type checking is the process of verifying that a variable or an expression has a certain type. Python is a dynamically typed language, which means that the type checking is done at run-time , not at compile-time. Python also supports duck typing , which means that an object’s behavior determines its type, rather than its class or inheritance. For example, if an object can be iterated over, it is considered iterable, regardless of its actual type. Python also allows you to use type hints or annotations to indicate the expected types of variables, arguments and return values. Type hints are optional and do not affect the execution of your code. However, they can help you catch errors and improve readability. You can use tools like mypy to perform static type checking on your Python code using type hints. Mypy can detect inconsistencies and violations of type contracts before running your code. Checking the type of a variable is a task that you will perform again and again to gain know...

Python Code To Connect SQL Server

One way to connect Python to SQL Server is using pyodbc module. Here is a template that you can use: import pyodbc conn = pyodbc.connect('Driver={SQL Server};'                       'Server=server_name;'                       'Database=database_name;'                       'Trusted_Connection=yes;') cursor = conn.cursor() cursor.execute('SELECT * FROM table_name') for i in cursor:     print(i) You need to replace server_name, database_name and table_name with your own values. You also need to install pyodbc module using pip install pyodbc command. If you have any questions about this code, you can drop a line in comment .

Python Creating List Elements Smartly

A List in Python is similar to an array. It is mutable, can store heterogeneous elements, and is easy to use. Now to add elements to an array you need to run a loop and then add elements one by one. Also If there are any conditionals involved then the size of the code increases. Python provides a more efficient way of doing it combining all the steps in a one-liner called List comprehension. # the old way list = [] for i in range(10):     list.append(i) print(list) # Output -> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 # using list comprehension list = [i for i in range(10)] print(list) # Output -> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 # conditionals list = [i**4 for i in range(1, 15) if i%3==0] print(list) # Output -> 81, 1296, 6561, 20736 list = [i**4 if i%5==0 else i/5 for i in range(1, 10)] print(list) # Output -> 0.2, 0.4, 0.6, 0.8, 625, 1.2, 1.4, 1.6, 1.8 If you have any questions about this code, you can drop a line in comment.

Python Passing Multiple Arguments Without Declaration

In Python, with the help of *args you can pass any number of arguments to a function without specifying the number. def add_numbers(*numbers):        sum = 0        for number in numbers:            sum += number        return sum print(add_numbers(5,6,233,56,3,5,2,5)) ## 315 By specifying **kwargs you can pass any number of keyword arguments to a function. If you have any questions about this code, you can drop a line in comment.

Python Generating a File Sharing Server

In Python we can create files sharing server easily to share files from one computer to another computer or cell phone by setting a free online FTP server. python -m http.server 8000 Select any PORT number from 0 to 65353 upon execution of above paython command you will server is running on 127.0.0.1:8000 Now open chrome or any browser on your mobile phone and simply type YOUR_COMPUTER_IP_ADDRESS:PORT_NUMBER To get the IP address you can do ipconfig on the terminal of your computer. Below you, will see the IPv4 address. For example, if your IP address is 192.168.39.145 and Port Number is 8000 then the file-sharing server will be running at 192.168.39.145:8000 If you have any questions about this code, you can drop a line in comment.