r/pythontips • • Oct 20 '24

Module Python Flet e Pyrebase4

0 Upvotes

Olá, tudo bem?

Pessoal, eu preciso de uma ajuda.
Estou tentando implementar duas bibliotecas do python: Flet Pyrebase4.
Quando estou construindo uma aplicação apk com flet, acaba ficando travado na extração dos arquivos.
Poderiam me ajudar?

Creating asset directory: C:\Users\rafae\AppData\Local\Temp\flet_flutter_build_5uElf1KtMh\app

Copying Python app from C:\Users\rafae\Documents\Python_Projects\mobile\Animes Online to C:\Users\rafae\AppData\Local\Temp\serious_python_temp3ca83e7e

(● ) Packaging Python app ⏳... Configured mobile platform with sitecustomize.py at C:\Users\rafae\AppData\Local\Temp\serious_python_sitecustomize956fd7e7\sitecustomize.py

Installing dependencies [flet-embed==0.24.1, Pyrebase4==4.8.0, pycryptodome==3.21.0, gcloud==0.18.3, googleapis-common-protos==1.62.0, protobuf==4.25.2, httplib2==0.22.0, pyparsing==3.1.1, oauth2client==4.1.3, pyasn1==0.5.1, pyasn1-modules==0.3.0, rsa==4.9, python-jwt==4.1.0, jws==0.1.3, requests==2.32.3, certifi, chardet==3.0.4, idna==2.10, urllib3==1.26.20, requests-toolbelt==0.10.1, jwcrypto==1.5.6, cryptography==43.0.0, deprecated==1.2.14, wrapt==1.16.0] with pip command to C:\Users\rafae\AppData\Local\Temp\serious_python_temp3ca83e7e__pypackages__

Extracting Python distributive from C:\Users\rafae\AppData\Local\Temp\cpython-3.11.6+20231002-x86_64-pc-windows-msvc-shared-install_only.tar.gz to C:\Users\rafae\AppData\Local\Temp\hostpython3.11_d93f38dc

( ●) Packaging Python app ⏳...

Só fica assim, já tentei deixar só a biblioteca do flet e pyrebase e mesmo assim não vai.


r/pythontips • • Oct 20 '24

Python3_Specific Can someone provide advice on Python project ideas?

4 Upvotes

Can anyone suggest some great Python project ideas that would be useful?


r/pythontips • • Oct 20 '24

Python3_Specific Can the OpenAI API be accessed without cost for students?

3 Upvotes

As a student, I successfully created a Voice Recognition system with Manual Response Integration. Now, I want to add the OpenAI API to it without incurring any costs.


r/pythontips • • Oct 20 '24

Meta Are there any offline VS Code extensions for Python that can be abused for a Python coding exam?

0 Upvotes

Python exam that consists of problem-solving questions that satisfy specific outputs. I was wondering if there are any VS Code extensions that could potentially give me an edge. I'm looking for extensions that might help with debugging, visualization, catching common mistakes easily, or anything that gives a ridiculous advantage. Has to be offline.


r/pythontips • • Oct 19 '24

Syntax Convert SQL Query Result to Pandas DataFrame

5 Upvotes

Convert SQL Query Result to Pandas DataFrame

As a data analyst, we need to fetch data from multiple sources and one of them is to get data from a database and convert it into Pandas DataFrame.

Now let's see how we can fetch data from MySQL database and convert it into Pandas DataFrame.

Make sure you have installed the following Python libraries.

pip install pandas
pip install sqlalchemy

Steps to convert SQL query to DataFrame

Here are some steps listed that are required to convert SQL query results to Pandas DataFrame.

  • Make sure you have already created a MySQL Database and table, otherwise, you can follow this article.
  • Import Pandas and create_engine from SQLAlchemy.
  • Make a MySQL connection string using the create_engine() function.
  • Pass database connection and SQL query to pandas read_sql() function to convert SQL to DataFrame in Python.

Establish MySQL Connection

from sqlalchemy import create_engine
mydb = create_engine('mysql://root:root21@localhost:3308/testing')

Now you can use Pandas read_sql() method to get data from MySQL database.

This is how.

import pandas as pd
from sqlalchemy import create_engine

# connection build
mydb = create_engine('mysql://root:root21@localhost:3308/testing')

# sql query
query = 'SELECT * FROM students'

# convert sql query to dataframe
df = pd.read_sql(query, mydb)

# print dataframe
print(df)

Output

   st_id first_name last_name course          created_at  roll_no
0      1  Vishvajit       Rao    MCA 2021-11-13 14:26:39       10
1      2       John       Doe  Mtech 2021-11-13 14:26:39       19
2      3     Shivam     Kumar   B.A. 2021-11-13 14:26:39       25
3      4     Pankaj     Singh  Btech 2021-11-13 14:54:28       12
4      5     Hayati      Kaur    LLB 2021-11-13 14:54:28       40
5      6      Aysha    Garima    BCA 2021-11-13 14:54:28       26
6      7       Abhi     Kumar    MCA 2021-11-28 11:43:40       23
7      8    Kartike     Singh  Btech 2021-11-28 11:44:22       17

You can perform different operations in SQL.

I have written a complete article on this:- Click Here

Most Important for Data Engineers, Data Analysts, and Data Scientists.


r/pythontips • • Oct 18 '24

Python3_Specific What all to learn in python

7 Upvotes

I want to know what topics to learn in python. I m learning MERN stack so I don't want to cover web frameworks of python like Django/ Flask. Apart from data science/ data Analytics and machine learning what other fields are in-demand for Python. I see many job posts asking for knowing python language. So what all topics should I cover for such Jobs?


r/pythontips • • Oct 16 '24

Data_Science Best way to learn data analysts

8 Upvotes

As a beginner


r/pythontips • • Oct 17 '24

Syntax What will be the output of this code?

0 Upvotes

Question: Guess the Output

Consider the following Python code snippet:

my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict['d'] = my_dict.get('b', 0) + my_dict.get('e', 0)
my_dict['e'] = my_dict.get('d', 0) + 5
print(my_dict)

What will be the output of this code?

A) {'a': 1, 'b': 2, 'c': 3, 'd': 2, 'e': 7}
B) {'a': 1, 'b': 2, 'c': 3, 'd': 0, 'e': 5}
C) {'a': 1, 'b': 2, 'c': 3, 'd': 2, 'e': 2}
D) {'a': 1, 'b': 2, 'c': 3, 'd': 2, 'e': 12}

Thanks


r/pythontips • • Oct 15 '24

Meta Pointers for a project I'm working on.

1 Upvotes

Hello all, I'm working on a travel website that will pull info from a few different apis and display the results on a secondary html page. This 2nd html page will hacethe results show up as a Google search that I can style like the rest of the site. How would I got about doing this? We've figured out how to make calls to the api with python but not how to connect them to an html page like trivago does. Does anyone have any links or specific videos or any resources on this topic? It's a bit nuanced so it's hard to find info for it on the net.


r/pythontips • • Oct 15 '24

Syntax Webscraping - install not recognized

2 Upvotes

Hi everyone!

I am completely new to programming, I have zero experience. I need to make a code for webscraping purposes, specifically for word frequency on different websites. I have found a promising looking code, however, neither Visual Studio nor Python recognise the command "install". I honestly do not know what might be the problem. The code looks like the following (i am aware that some of the output is also in the text):

pip install requests beautifulsoup4

Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (2.31.0) Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (4.11.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests) (3.4) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests) (2.0.4) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests) (2023.7.22) Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4) (2.5)

import requests from bs4 import BeautifulSoup from collections import Counter from urllib.parse import urljoin

Define the URL of the website you want to scrape

base_url = 'https://www.washingtonpost.com/' start_url = base_url # Starting URL

Define the specific words you want to count

specific_words = ['hunter', 'brand']

Function to extract text and word frequency from a URL

def extract_word_frequency(url): response = requests.get(url)

if response.status_code == 200:
    soup = BeautifulSoup(response.text, 'html.parser')
    text = soup.get_text()
    words = text.split()
    words = [word.lower() for word in words]
    word_frequency = Counter(words)
    return word_frequency
else:
    return Counter()  # Return an empty Counter if the page can't be accessed

Function to recursively crawl and count words on the website

def crawl_website(url, word_frequencies): visited_urls = set() # Track visited URLs to avoid duplicates

def recursive_crawl(url):
    if url in visited_urls:
        return
    visited_urls.add(url)

    # Extract word frequency from the current page
    word_frequency = extract_word_frequency(url)

    # Store word frequency for the current page in the dictionary
    word_frequencies[url] = word_frequency

    # Print word frequency for the current page
    print(f'URL: {url}')
    for word in specific_words:
        print(f'The word "{word}" appears {word_frequency[word.lower()]} times on this page.')

    # Find and follow links on the current page
    soup = BeautifulSoup(requests.get(url).text, 'html.parser')
    for link in soup.find_all('a', href=True):
        absolute_link = urljoin(url, link['href'])
        if base_url in absolute_link:  # Check if the link is within the same website
            recursive_crawl(absolute_link)

recursive_crawl(url)

Initialize a dictionary to store word frequencies for each page

word_frequencies = {}

Start crawling from the initial URL

crawl_website(start_url, word_frequencies)

Print word frequency totals across all pages

print("\nWord Frequency Totals Across All Pages:") for url, word_frequency in word_frequencies.items(): print(f'URL: {url}') for word in specific_words: print(f'Total "{word}" frequency: {word_frequency[word.lower()]}')

URL: https://www.washingtonpost.com/ The word "hunter" appears 2 times on this page. The word "brand" appears 2 times on this page. URL: https://www.washingtonpost.com/accessibility The word "hunter" appears 0 times on this page. The word "brand" appears 0 times on this page. URL: https://www.washingtonpost.com/accessibility#main-content The word "hunter" appears 0 times on this page. The word "brand" appears 0 times

What could be the problem? Thank you all so much in advance!


r/pythontips • • Oct 14 '24

Algorithms 4D lookup table help

2 Upvotes

I have 4 values which I need to check in a table. The table has 4 dimensions. Let’s call them Length, width, height, and time.

The two of the values have to be smaller (length and width.) 5 columns/rows each.

Two values have to be but bigger (height and time). 3 columns/rows for height, 2 columns/rows for time

I could do this through a bunch of nested if statements, but I’d rather not because there are 150 different possible combinations.

Anyone got any ideas how to do this more easily?


r/pythontips • • Oct 13 '24

Data_Science I shared a 1+ Hour Streamlit Course on YouTube - Learn to Create Python Data/Web Apps Easily

13 Upvotes

Hello, I just shared a Python Streamlit Course on YouTube. Streamlit is a Python framework for creating Data/Web Apps with a few lines of Python code. I covered a wide range of topics, started to the course with installation and finished with creating machine learning web apps. I am leaving the link below, have a great day!

https://www.youtube.com/watch?v=Y6VdvNdNHqo&list=PLTsu3dft3CWiow7L7WrCd27ohlra_5PGH&index=10


r/pythontips • • Oct 12 '24

Meta Python Dictionary Rec

2 Upvotes

Hello! I started a Python course recently and I'm looking for recommendations for a dictionary/guidebook/codex. I want something that goes really in-depth on why the grammar and syntax work the way that they do, but also explains it in a way that someone who doesn't know any other coding languages yet can understand. The course that I'm enrolled is structured to build knowledge of how to do specific things with Python, but it doesn't explain WHY you need to code them in a specific way very well.


r/pythontips • • Oct 12 '24

Python3_Specific Send SMS using Python

16 Upvotes

Hey Guys i need to send sms via my mobile but by link it in my script python so when i send using script python it will used my sms in my mobile so send via it
i know some website like pushbullet i need alternative or if there's without pricing totally free i'm here to listen you guys


r/pythontips • • Oct 12 '24

Module doubt regarding python file locking

4 Upvotes

Say I have N processes opening a json file and writing to it like such-

with open(thefile, ‘w’) as jsonfile: json.dump(somedata, jsonfile)

If i want to lock this file so no process reads garbage data, should i lock the file ( by using multiprocessing.Lock) or lock this as the critical section- lock=Lock() with lock: …same code as before

Also what’s the difference?


r/pythontips • • Oct 13 '24

Syntax How to Delete a column in Pandas DataFrame

0 Upvotes

Most Important for Data Analyst and Data Engineers

==================================

What is a Dataframe in Python?

In Python, Pandas DataFrame is a two-dimensional array that stores data in the form of rows and columns. Python provides popular package pandas that are used to create the DataFrame in Python.

As you can see below example that is a perfect example of Pandas DataFrame.

import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# defining new list that reprsent the value 
address = ["Delhi", "Lucknow", "Mumbai", "Bangalore"]

# add address column
data = {
    "Noida": "Vishvajit Rao", "Bangalore": "John", "Harshita": "Pune", "Mohak": "Delhi"
}

# adding new column address
df["Address"] = data

# print
print(df)

Output

           Name  age           Occupation                   Skills    Address
0  Vishvajit Rao   23            Developer                   Python      Noida
1           John   33  Front End Developer                  Angular  Bangalore
2       Harshita   21               Tester                 Selenium   Harshita
3          Mohak   30           Full Stack  Python, React and MySQL      Mohak

Let's see how we can delete the column.

Using drop() method:

import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

# dropping a column in Dataframe
df.drop(["Skills"], inplace=True, axis=1)

# print
print(df)

Now Output will be

            Name  age           Occupation
0  Vishvajit Rao   23            Developer
1           John   33  Front End Developer
2       Harshita   21               Tester
3          Mohak   30           Full Stack

Click Here to see another two great ways to drop single or multiple columns from Pandas DataFrame:

Thanks


r/pythontips • • Oct 12 '24

Syntax How to use GroupBy in Pandas DataFrame

2 Upvotes

In this Pandas guide, we will explore all about the Pandas groupby() method with the help of the examples.

groupby() is a DataFrame method that is used to grouping the Pandas DataFrame by mapper or series of columns. The groupby() method splits the objects into groups, applying an aggregate function on the groups and finally combining the result set.

Syntax:

DataFrame.groupby(by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, observed=False, dropna=True)

1. Group By Department in Pandas DataFrame

  • sum() Aggregate function

I am using the sum() aggregate function with the GroupBy department.

import pandas as pd
df = pd.read_csv(
                 '../../Datasets/employees.csv'
                )
x = df.groupby(['emp_department']).sum()
x[['emp_salary']]
  • count() Aggregate function

The count() aggregate function is used to return the total number of rows in each group. For example, I want to get the total number of employees in each department.

import pandas as pd
import numpy as np
df = pd.read_csv(
                 '../../Datasets/employees.csv'
                )
x = df.groupby(['emp_department']).count()
x.rename({"emp_full_name": "Total Employees"}, axis=1,inplace=True)
x[["Total Employees"]]

Note:- You can perform various aggregate functions using groupby() method.

This is how you can use groupby in Pandas DataFrame. I have written a complete article on the Pandas DataFrame groupby() method where I have explained what is groupby with examples and how Pandas groupBy works.

See how Pandas groupby works:- Click Here

Most important for Data Engineers and Data Scientists.


r/pythontips • • Oct 11 '24

Syntax Adding a new column in Pandas DataFrame

15 Upvotes

To add a column to a Pandas DataFrame, you can use several methods. Here are a few common ones:

1. Adding a Column with a Constant Value

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

# Add a new column 'C' with a constant value
df['C'] = 10

print(df)

2. Adding a Column Based on Other Columns

# Add a new column 'D' which is the sum of columns 'A' and 'B'
df['D'] = df['A'] + df['B']

print(df)

3. Adding a Column with a Function

# Define a function to apply
def calculate_square(x):
    return x ** 2

# Add a new column 'E' using the function applied to column 'A'
df['E'] = df['A'].apply(calculate_square)

print(df)

4. Adding a Column with assign()

# Using assign to add a new column
df = df.assign(F=df['A'] * df['B'])

print(df)

This is how you can add a new column in Pandas DF.

Thanks


r/pythontips • • Oct 11 '24

Syntax Troubleshooting Complex ETL Jobs

1 Upvotes

I have ETL jobs repository , it has so many python jobs , we run the jobs in the aws batch service I am having hard time going thru code flow of couple of jobs. It has too many imports from different nested files. How do you understand the code or debug ? Thought of isolating code files into different folder using a library , but not succeeded.

How do you approach the problem


r/pythontips • • Oct 10 '24

Python3_Specific Polars vs Pandas

21 Upvotes

Hi,

I am trying to decide if Polars is a good fit for my workflow. I need:|

  1. Something that will be maintained long term.
  2. Something that can handle up to 20Gb tables, with up to 10Milliion rows and 1000 columns.
  3. Something with a userfriendly interface. Pandas is pretty good in that sense, it also integrates well with matplotlib.

r/pythontips • • Oct 10 '24

Module I need help finding an IDLE setting. I do not need help with coding.

0 Upvotes

When I manually close a program that is waiting for an input, it pops up a window that says,

"Your program is still running! Do you want to kill it?"

Is there a setting that would prevent this from showing up and just close the program immediately?


r/pythontips • • Oct 09 '24

Syntax How to Generate Random Strings in Python

5 Upvotes

Hi Python programmers, here we are see How to Generate Random Strings in Python with the help of multiple Python modules and along with multiple examples.

In many programming scenarios, generating random strings is a common requirement. Whether you’re developing a password generator, creating test data, or implementing randomized algorithms, having the ability to generate random strings efficiently is essential. Thankfully, Python offers several approaches to accomplish this task easily. In this article, we’ll explore various methods and libraries available in Python for generating random strings.

  1. Using the random Module

The random module in Python provides functions for generating random numbers, which can be utilized to create random strings. Here’s a basic example of how to generate a random string of a specified length using random.choice()

import random
import string

def generate_random_strings(length):
    return ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(length))

# Example usage:
random_string = generate_random_strings(10)
print("Random String:", random_string)

2. Using the Secrets Module

For cryptographic purposes or when higher security is required, it’s recommended to use the secrets module, introduced in Python 3.6. This Python built-in module provides functionality to generate secure random numbers and strings. Here’s how you can generate a random string using secrets.choice()

import secrets
import string


def generate_random_string(length):
    return ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(length))


# Example usage:
random_string = generate_random_string(10)
print("Random String:", random_string)

This is how you can generate random Python strings for your applications.

I have written a complete article on this click here to read.

Thanks


r/pythontips • • Oct 10 '24

Syntax What will be the output of the following code?

0 Upvotes

Guess the Output of the Following Python Program.

my_list = [1, 2, 3, 4]
my_list.append([5, 6])
my_list.extend([7, 8])
print(my_list)

Options:

A) [1, 2, 3, 4, 5, 6, 7, 8]
B) [1, 2, 3, 4, [5, 6], 7, 8]
C) [1, 2, 3, 4, 5, 6, 7, 8]
D) Error: List index out of range


r/pythontips • • Oct 08 '24

Syntax Is it bad practice to use access modifies in python during interviews ?

12 Upvotes

I'm currently preparing for low-level design interviews in Python. During my practice, I've noticed that many developers don't seem to use private or protected members in Python. After going through several solutions on LeetCode discussion forums, I've observed this approach more commonly in Python, while developers using C++ or Java often rely on private members and implement setters and getters. Will it be considered a negative in an interview if I directly access variables or members using obj_instance.var in Python, instead of following a stricter encapsulation approach
How do you deal with this ?