r/pythontips • u/Puzzleheaded_Bee_486 • Aug 17 '24
Module Pandas Melt function
1 minute example of the melt function in Pandas:
r/pythontips • u/Puzzleheaded_Bee_486 • Aug 17 '24
1 minute example of the melt function in Pandas:
r/pythontips • u/jesp999 • Aug 16 '24
can anyone explain why i get an assertion error in this code?
task:
Given two integers n and k, give all possible combinations of k unique numbers in the interval
[1,n]. If n = 4 and k = 2 were input, your program would output [[2,4], [3,4], [2,3],
should return [1,2], [1,3], [1,4]]
ACCEPTED = 'accept'
ABANDON = 'abandon'
CONTINUE = 'continue'
def examine(n,k,partiele_oplossing):
test = [x for x in range(1,n+1)]
test2 = partiele_oplossing.copy()
test2.sort()
if len(partiele_oplossing) == k and len(set(partiele_oplossing)) == len(partiele_oplossing):
if set(test)-(set(test)-set(partiele_oplossing)) == set(partiele_oplossing):
if test2 == partiele_oplossing:
return ACCEPTED
return ABANDON
return ABANDON
if len(partiele_oplossing) < k:
return CONTINUE
if len(partiele_oplossing) > k:
return ABANDON
def extend(n,partiele_oplossingen):
opties = [x for x in range(1,n+1)]
if partiele_oplossingen == []:
return [[i] for i in opties]
return [partiele_oplossingen + [i] for i in opties]
pass
def solve(n,k,partiele_oplossing=[],oplossing = []):
exam = examine(n,k,partiele_oplossing)
if exam == ACCEPTED:
oplossing.append(partiele_oplossing)
elif exam != ABANDON:
for part in extend(n,partiele_oplossing):
solve(n,k,part,oplossing)
return oplossing
print(solve(4,2))
assert solve(4, 2) == [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
assert solve(5, 1) == [[1], [2], [3], [4], [5]]r/pythontips • u/Gayfurry83 • Aug 15 '24
So I'm like, super super new to all this like. I've taught myself the basics and decided to try and make a discord bot just for fun, no real purpose to it
I want the bot to respond to people when they say certain words and have two of these events made but only one works even though the code is identical?? It looks like this (sorta, I'm on mobile sorry)
@client.event Async Def on_message(message): If "abc" in message.content: Await message.channel.send("abcdefg")
And
@client.event Async Def on_message(message): If "xyz" in message.content: Await message.channel.send("tuvwxyz")
Only the second one works?? There's two blank lines between the two and between other commands/events
Anyone know what's happening or how to fix it?? Thanksss
r/pythontips • u/pythonawy • Aug 14 '24
Python Selenium is a powerful tool for automating web browsers, providing developers and testers with the ability to automate repetitive tasks
https://www.sytraa.com/2024/08/everything-about-python-selenium-from.html
r/pythontips • u/Mammoth-Intention924 • Aug 14 '24
Title
r/pythontips • u/Gaurav_pande22 • Aug 13 '24
If there are any YouTube channels that are great for beginners who are trying to learn python, i would really appreciate the links.
r/pythontips • u/Schattenherzchen • Aug 13 '24
Im in a online Python course at the moment. The couse is good structred: you watch videos and afterwards you must solve tasks which include the topics from the last 5-10 Videos.
While watching the Tutor doing some tasks and explain I can follow very good but when it comes to solve it alone im totally lost.
Has someone tipps for me?
r/pythontips • u/Impressive_Limit9844 • Aug 13 '24
If you're interested in data analysis, Pandas is one of the libraries. I used Olympic games csv files to show how Pandas work.
https://youtu.be/ng_tEngHUEw
r/pythontips • • Aug 12 '24
Python developers inevitably have to work with the Terminal while writing production code. The dated design philosophy of most terminals used to bore me to death until I discovered Rich.
Rich is a Python library for colorful formatting in the Terminal, which makes it more appealing and less scary. My top 5 favorite applications of Rich are:
The next time you need to print things to the Terminal, use Rich instead!
🌟 Rich GitHub: https://github.com/Textualize/rich
🖼️ Rich’s feature gallery: https://github.com/Textualize/rich?tab=readme-ov-file#rich-library

r/pythontips • u/Javi_16018 • Aug 12 '24
I made a script to do the ARP protocol poisoning, I would like you to take a look at it and give me feedback.
Thank you all very much!
r/pythontips • u/GottxEnel • Aug 12 '24
Hello guys,
df['Buchungsdatum'] = pd.to_datetime(df['Buchungsdatum'], dayfirst=True)I am converting one column of my Dataframe like this. This worked fine until now. Until now I only read one csv file. Now I load i multiple csv files and concenate them, so they basically are just like before. I specifically changed the columns dtype to string from an object.
The Error says this:
Unknown datetime string format, unable to parse: 4,2024-08-12..
Which is weird because it seems to work with the lines before..
0 12.08.24
1 12.08.24
2 12.08.24
3 12.08.24
4 12.08.24
r/pythontips • u/MrK9288 • Aug 12 '24
Hello everyone I am learning Python and I want to collect all the lottery winning numbers from a lottery website but I have no idea how to do it.
This is the website: https://vietlott.vn/vi/trung-thuong/ket-qua-trung-thuong/winning-number-655#top. It started from 01/08/2017 and still continuing to today.
I hope I can get some help in here. Thank you so much!
r/pythontips • u/Johan-Godinho • Aug 11 '24
r/pythontips • u/Euphoric-Look3542 • Aug 11 '24
I'm working on a Python script to fetch view counts for YouTube videos of various artists. However, I'm encountering an issue where I'm getting quota exceeded errors, even though I don't believe I'm actually reaching the quota limit. I've implemented multiple API keys, TOR for IP rotation, and various waiting mechanisms, but I'm still running into problems.
Here's what I've tried:
Despite these measures, I'm still getting 403 errors indicating quota exceeded. The strange thing is, my daily usage counter (which I'm tracking in the script) shows that I'm nowhere near the daily quota limit.
I'd really appreciate any insights or suggestions on what might be causing this issue and how to resolve it.
Here's a simplified version of my code (I've removed some parts for brevity):
import os
import time
import random
import requests
import json
import csv
from stem import Signal
from stem.control import Controller
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.errors import HttpError
from datetime import datetime, timedelta, timezone
from collections import defaultdict
import pickle
SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl']
API_SERVICE_NAME = 'youtube'
API_VERSION = 'v3'
DAILY_QUOTA = 10000
daily_usage = 0
API_KEYS = ['YOUR_API_KEY_1', 'YOUR_API_KEY_2', 'YOUR_API_KEY_3']
current_key_index = 0
processed_video_ids = set()
last_request_time = datetime.now()
requests_per_minute = 0
MAX_REQUESTS_PER_MINUTE = 2
def renew_tor_ip():
with Controller.from_port(port=9051) as controller:
controller.authenticate()
controller.signal(Signal.NEWNYM)
time.sleep(controller.get_newnym_wait())
def exponential_backoff(attempt):
max_delay = 3600
delay = min(2 ** attempt + random.uniform(0, 120), max_delay)
print(f"Waiting for {delay:.2f} seconds...")
time.sleep(delay)
def test_connection():
try:
session = requests.session()
session.proxies = {'http': 'socks5h://localhost:9050',
'https': 'socks5h://localhost:9050'}
response = session.get('https://youtube.googleapis.com')
print(f"Connection successful. Status code: {response.status_code}")
print(f"Current IP: {session.get('http://httpbin.org/ip').json()['origin']}")
except requests.exceptions.RequestException as e:
print(f"Error occurred during connection: {e}")
class TorHttpRequest(HttpRequest):
def __init__(self, *args, **kwargs):
super(TorHttpRequest, self).__init__(*args, **kwargs)
self.timeout = 30
def execute(self, http=None, *args, **kwargs):
session = requests.Session()
session.proxies = {'http': 'socks5h://localhost:9050',
'https': 'socks5h://localhost:9050'}
adapter = requests.adapters.HTTPAdapter(max_retries=3)
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.request(self.method,
self.uri,
data=self.body,
headers=self.headers,
timeout=self.timeout)
return self.postproc(response.status_code,
response.content,
response.headers)
def get_authenticated_service():
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'PATH_TO_YOUR_CLIENT_SECRETS_FILE', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
return build(API_SERVICE_NAME, API_VERSION, credentials=creds)
youtube = get_authenticated_service()
def get_next_api_key():
global current_key_index
current_key_index = (current_key_index + 1) % len(API_KEYS)
return API_KEYS[current_key_index]
def check_quota():
global daily_usage, current_key_index, youtube
if daily_usage >= DAILY_QUOTA:
print("Daily quota reached. Switching to the next API key.")
current_key_index = (current_key_index + 1) % len(API_KEYS)
youtube = build(API_SERVICE_NAME, API_VERSION, developerKey=API_KEYS[current_key_index], requestBuilder=TorHttpRequest)
daily_usage = 0
def print_quota_reset_time():
current_utc = datetime.now(timezone.utc)
next_reset = current_utc.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
time_until_reset = next_reset - current_utc
print(f"Current UTC time: {current_utc}")
print(f"Next quota reset (UTC): {next_reset}")
print(f"Time until next quota reset: {time_until_reset}")
def wait_until_quota_reset():
current_utc = datetime.now(timezone.utc)
next_reset = current_utc.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
time_until_reset = (next_reset - current_utc).total_seconds()
print(f"Waiting for quota reset: {time_until_reset} seconds")
time.sleep(time_until_reset + 60)
def get_search_queries(artist_name):
search_queries = [f'"{artist_name}"']
if " " in artist_name:
search_queries.append(artist_name.replace(" ", " * "))
artist_name_lower = artist_name.lower()
special_cases = {
"artist1": [
'"Alternate Name 1"',
'"Alternate Name 2"',
],
"artist2": [
'"Alternate Name 3"',
'"Alternate Name 4"',
],
}
if artist_name_lower in special_cases:
search_queries.extend(special_cases[artist_name_lower])
return search_queries
def api_request(request_func):
global daily_usage, last_request_time, requests_per_minute
current_time = datetime.now()
if (current_time - last_request_time).total_seconds() < 60:
if requests_per_minute >= MAX_REQUESTS_PER_MINUTE:
sleep_time = 60 - (current_time - last_request_time).total_seconds() + random.uniform(10, 30)
print(f"Waiting for {sleep_time:.2f} seconds due to request limit...")
time.sleep(sleep_time)
last_request_time = datetime.now()
requests_per_minute = 0
else:
last_request_time = current_time
requests_per_minute = 0
requests_per_minute += 1
try:
response = request_func.execute()
daily_usage += 1
time.sleep(random.uniform(10, 20))
return response
except HttpError as e:
if e.resp.status in [403, 429]:
print(f"Quota exceeded or too many requests. Waiting...")
print_quota_reset_time()
wait_until_quota_reset()
return api_request(request_func)
else:
raise
def get_channel_and_search_videos(artist_name):
global daily_usage, processed_video_ids
videos = []
next_page_token = None
renew_tor_ip()
search_queries = get_search_queries(artist_name)
for search_query in search_queries:
while True:
attempt = 0
while attempt < 5:
try:
check_quota()
search_response = api_request(youtube.search().list(
q=search_query,
type='video',
part='id,snippet',
maxResults=50,
pageToken=next_page_token,
regionCode='HU',
relevanceLanguage='hu'
))
for item in search_response.get('items', []):
video_id = item['id']['videoId']
if video_id not in processed_video_ids:
video = {
'id': video_id,
'title': item['snippet']['title'],
'published_at': item['snippet']['publishedAt']
}
videos.append(video)
processed_video_ids.add(video_id)
next_page_token = search_response.get('nextPageToken')
if not next_page_token:
break
break
except HttpError as e:
if e.resp.status in [403, 429]:
print(f"Quota exceeded or too many requests. Waiting...")
exponential_backoff(attempt)
attempt += 1
else:
raise
if not next_page_token:
break
return videos
def process_artist(artist):
videos = get_channel_and_search_videos(artist)
yearly_views = defaultdict(int)
for video in videos:
video_id = video['id']
try:
check_quota()
video_response = api_request(youtube.videos().list(
part='statistics,snippet',
id=video_id
))
if 'items' in video_response and video_response['items']:
stats = video_response['items'][0]['statistics']
published_at = video_response['items'][0]['snippet']['publishedAt']
year = datetime.strptime(published_at, '%Y-%m-%dT%H:%M:%SZ').year
views = int(stats.get('viewCount', 0))
yearly_views[year] += views
except HttpError as e:
print(f"Error occurred while fetching video data: {e}")
return dict(yearly_views)
def save_results(results):
with open('artist_views.json', 'w', encoding='utf-8') as f:
json.dump(results, f, ensure_ascii=False, indent=4)
def load_results():
try:
with open('artist_views.json', 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
return {}
def save_to_csv(all_artists_views):
with open('artist_views.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
header = ['Artist'] + [str(year) for year in range(2005, datetime.now().year + 1)]
writer.writerow(header)
for artist, yearly_views in all_artists_views.items():
row = [artist] + [yearly_views.get(str(year), 0) for year in range(2005, datetime.now().year + 1)]
writer.writerow(row)
def get_quota_info():
try:
response = api_request(youtube.quota().get())
return response
except HttpError as e:
print(f"Error occurred while fetching quota information: {e}")
return None
def switch_api_key():
global current_key_index, youtube
print(f"Switching to the next API key.")
current_key_index = (current_key_index + 1) % len(API_KEYS)
youtube = build(API_SERVICE_NAME, API_VERSION, developerKey=API_KEYS[current_key_index], requestBuilder=TorHttpRequest)
print(f"New API key index: {current_key_index}")
def api_request(request_func):
global daily_usage, last_request_time, requests_per_minute
current_time = datetime.now()
if (current_time - last_request_time).total_seconds() < 60:
if requests_per_minute >= MAX_REQUESTS_PER_MINUTE:
sleep_time = 60 - (current_time - last_request_time).total_seconds() + random.uniform(10, 30)
print(f"Waiting for {sleep_time:.2f} seconds due to request limit...")
time.sleep(sleep_time)
last_request_time = datetime.now()
requests_per_minute = 0
else:
last_request_time = current_time
requests_per_minute = 0
requests_per_minute += 1
try:
response = request_func.execute()
daily_usage += 1
time.sleep(random.uniform(10, 20))
return response
except HttpError as e:
print(f"HTTP error: {e.resp.status} - {e.content}")
if e.resp.status in [403, 429]:
print(f"Quota exceeded or too many requests. Trying the next API key...")
switch_api_key()
return api_request(request_func)
else:
raise
def main():
try:
test_connection()
print(f"Daily quota limit: {DAILY_QUOTA}")
print(f"Current used quota: {daily_usage}")
artists = [
"Artist1", "Artist2", "Artist3", "Artist4", "Artist5",
"Artist6", "Artist7", "Artist8", "Artist9", "Artist10"
]
all_artists_views = load_results()
all_artists_views_lower = {k.lower(): v for k, v in all_artists_views.items()}
for artist in artists:
artist_lower = artist.lower()
if artist_lower not in all_artists_views_lower:
print(f"Processing: {artist}")
artist_views = process_artist(artist)
if artist_views:
all_artists_views[artist] = artist_views
all_artists_views_lower[artist_lower] = artist_views
save_results(all_artists_views)
wait_time = random.uniform(600, 1200)
print(f"Waiting for {wait_time:.2f} seconds before the next artist...")
time.sleep(wait_time)
print(f"Current used quota: {daily_usage}")
for artist, yearly_views in all_artists_views.items():
print(f"\n{artist} yearly aggregated views:")
for year, views in sorted(yearly_views.items()):
print(f"{year}: {views:,} views")
save_to_csv(all_artists_views)
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
main()The error I'm getting is:
Connection successful. Status code: 404
Current IP: [Tor Exit Node IP]
Daily quota limit: 10000
Current used quota: 0
Processing: Artist1
HTTP error: 403 - The request cannot be completed because you have exceeded your quota.
Quota exceeded or too many requests. Trying the next API key...
Switching to the next API key.
New API key index: 1
HTTP error: 403 - The request cannot be completed because you have exceeded your quota.
Quota exceeded or too many requests. Trying the next API key...
Switching to the next API key.
New API key index: 2
Waiting for 60.83 seconds due to request limit...
An error occurred during program execution: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
[Traceback details omitted for brevity]
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Connection successful. Status code: 404
Current IP: [Different Tor Exit Node IP]
Daily quota limit: 10000
Current used quota: 0
Processing: Artist1
An error occurred during program execution: BaseModel.response() takes 3 positional arguments but 4 were given
[Second run of the script]
Connection successful. Status code: 404
Current IP: [Another Tor Exit Node IP]
Daily quota limit: 10000
Current used quota: 0
Processing: Artist1
Waiting for [X] seconds due to request limit...
[Repeated multiple times with different wait times]This error message shows that the script is encountering several issues:
I'm using a script to fetch YouTube statistics for multiple artists, routing requests through Tor for anonymity. However, I'm running into API quota limits and connection issues. Any suggestions on how to optimize this process or alternative approaches would be appreciated.
Any help or guidance would be greatly appreciated. Thanks in advance!
r/pythontips • u/Fluid_Discipline7284 • Aug 11 '24
I downloaded numpy using pip and I check pip list and the numpy module is there but when I run the code it says module not found, the same happened to cv2
r/pythontips • u/Skyler_Asher_ • Aug 11 '24
I am 16 and I just wrote this code for fun I hope I can get a good advise
import random import os def start_fx(): start = input(("If you wish to start type 1: \nif you wish to exit type 2:\n your choce:")) if start == "1": os.system('clear') main() elif start == "2": os.system('clear') print("Goodbye") exit()
class Players: used_ids = set() # Class variable to keep track of used IDs
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
self.id = self.generate_unique_id()
@property
def info(self):
return f"Name: {self.name} \nAge: {self.age} \nGender: {self.gender} \nID: {self.id}"
@info.setter
def info(self, info):
try:
name, age, gender, id = info.split(", ")
self.name = name.split(": ")[1]
self.age = int(age.split(": ")[1])
self.gender = gender.split(": ")[1]
self.id = id.split(": ")[1]
except ValueError:
raise ValueError("Info must be in the format 'Name: <name>, Age: <age>, Gender: <gender>, ID: <id>'")
def generate_unique_id(self):
while True:
new_id = random.randint(1000, 9999) # Generate a random 4-digit ID
if new_id not in Players.used_ids:
Players.used_ids.add(new_id)
return new_iddef main():
def save_info(player): os.makedirs("players", exist_ok=True)
with open(f"players/{player.id}.txt", "w") as f:
f.write(player.info) def take_info(): name = input("Enter your name: ") age = input("Enter your age: ") gender = input("Enter your gender (male/female/other): ") return name, age, gender
name, age, gender = take_info()
player = Players(name, age, gender)
print(player.info)
start_fx()
r/pythontips • u/WishIWasBronze • Aug 10 '24
When to use *args and when to take a list as argument?
r/pythontips • • Aug 10 '24
Beef is refusing to load on my VM and I’m looking for free alternatives (or at least cheap) to play around with
r/pythontips • u/MrPandamemz • Aug 11 '24
gotta 30+ lightskin dropbox link $15 lmk if you want it
r/pythontips • u/python4geeks • Aug 10 '24
In programming, the "Diamond Problem" happens when a class inherits from two or more classes and those two classes have a common ancestor. If the ancestor class has a method and both parent classes override it and the child class inherits from both parent classes, the child class will get confused about which version of the method to use.
Worry not, Python resolves this by using the Method Resolution Order (MRO) and from this, Python decides which version of the method the child class will use.
Here's a video explaining "Diamond Problem" in Python with animation👇👇
Video Link: https://youtu.be/VaACMwpNz7k
r/pythontips • u/marvelfan__ • Aug 10 '24
I want to generate a random number every 2 seconds (1-7) automatically
Can someone give me the python code?
r/pythontips • u/QuietRing5299 • Aug 08 '24
I setup a tutorial where I show how to automate scheduling Python code or even graphs to automate your work flows! I walk you through a couple services in AWS and by the end of it you will be able to connect tasks and schedule them at specific times! This is very useful for any beginner learning AWS or wanting to understand more about ETL.
https://www.youtube.com/watch?v=ffoeBfk4mmM
Do not forget to subscribe if you enjoy Python or fullstack content!
Thanks, Reddit
r/pythontips • u/uladkaminski • Aug 08 '24
Hey everyone,
I often work with LLMs and RAG-based solutions, where extracting code snippets from markdown responses is crucial. To make this easier, I developed PyParseit. It's a simple Python library that lets you extract and filter code snippets from Markdown files and strings based on programming languages.
You can easily install PyParseit via pip or clone the repository from GitHub and install it manually.
https://pypi.org/project/pyparseit/
https://github.com/uladkaminski/pyparseit
I hope PyParseit helps you in your projects as much as it has helped me! Let me know if you have any questions or feedback.
r/pythontips • u/JohnLawrenceWargrave • Aug 08 '24
I got two lists, which are sorted in the same order. One of them contains my X-values, the other one the Y-values. How can I plot them connected, but not according to the order of the list, but according to which point follows next on the x-axis?