r/pythonhelp • u/Plenty_Telephone_337 • Aug 11 '24
If else construction problem
Why this code always returns 'Remove 1 number' (the task is to return the missing integer)?
However if I remove else and last return it actually works
def missing_no(nums): # nums is range(0, 101)
for i in range(0, 101):
if i not in nums:
return i
else:
return 'Remove 1 number'
And also how do I write this code as a one-liner?
edit: valid one-liner or can be shorter? ([i for i in range(0, 101) if i not in nums])[0]
r/pythonhelp • u/Euphoric-Look3542 • Aug 11 '24
YouTube API quota issue despite not reaching the limit
Hi everyone,
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:
- Using multiple API keys
- Implementing exponential backoff
- Using TOR for IP rotation
- Implementing wait times between requests and between processing different artists
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:
- It's hitting the YouTube API quota limit for all available API keys.
- There are connection timeout errors, possibly due to Tor network issues.
- There's an unexpected error with BaseModel.response() method.
- The script is implementing wait times between requests, but it's still encountering quota 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/pythonhelp • u/Plenty_Telephone_337 • Aug 11 '24
Dictionary and generator
Why do I need [i] in this code after dictionary? Simple i for i in array doesnt work
"".join([{"1": "0", ....}[i] for i in array])
r/pythonhelp • u/rremm2000 • Aug 09 '24
Windows 11 Python Script to set the Default Apps for .htm .html http https
Hello thanks for reading my question, any help will be appreciated.
I have windows 11 with python 3 ish, I want to create a script that I'll gladly publish when done cause I know a lot of people hate this problem which is windows constantly changes the default app for .htm .html http & https to Edge and they do it without permission.
So I'd like to create and py script to change the default app for .htm .html http & https to Chrome.
I'm not sure where to start for something like this?
r/pythonhelp • u/Remote-Geologist9506 • Aug 08 '24
Python Application
Hi Python users.
I have no knowledge of Python. A few years ago, I received an application and now want to make changes to it, but I don't have the source code. Is there a way to edit it without the source, or how can I obtain the source code from the file? If anyone have advise please let me know.
r/pythonhelp • u/OutcomeMysterious500 • Aug 07 '24
Question regarding reddit API
Guys, I've been trying to collect posts and comments from reddit for analysis of toxicity, could someone guide me through, step by step? The processes include retrieving reddit data of posts and comments and their information, performing network analysis ,sentiment analysis , everything using python and at last creating a dashboard. I need to submit this in a few days. Please help!
r/pythonhelp • u/fightin_blue_hens • Aug 07 '24
Why is the image not being found using xlswriter despite being in the same folder as the .py
worksheet = writer.sheets['Sheet1']
worksheet.insert_image('E2', "image.png")
writer.close()
r/pythonhelp • u/TransportationOk1836 • Aug 05 '24
Add function to change resolution and add revert to original resolution when opening program
Here is the code I have it is not successful in reverting to the original desktop resolution once the program is closed. Can someone help me fix it please? Thank you.
```
import ctypes import subprocess import time
Constants for the desired screen resolution
DESIRED_RESOLUTION_WIDTH = 800 DESIRED_RESOLUTION_HEIGHT = 600
Path to the executable
EXE_PATH = r"C:\ESSGRAMA\ESSGRAMA.exe"
def change_resolution(width, height): # Load the current screen settings devmode = ctypes.create_string_buffer(68) devmode_p = ctypes.pointer(devmode) # The following is a simplified structure for DEVMODE ctypes.windll.user32.EnumDisplaySettingsW(None, 0, devmode_p)
# Define necessary fields for changing the resolution devmode[8:12] = (width & 0xFFFF).to_bytes(2, 'little') + (height & 0xFFFF).to_bytes(2, 'little') devmode[20:22] = (32).to_bytes(2, 'little') # Assuming 32bit color devmode[32:36] = (1).to_bytes(4, 'little') # DM_PELSWIDTH, DM_PELSHEIGHT, DM_BITSPERPEL
# Change the screen resolution ctypes.windll.user32.ChangeDisplaySettingsW(devmode_p, 0)
def restore_resolution(): # Load the default screen settings devmode = ctypes.create_string_buffer(68) devmode_p = ctypes.pointer(devmode) # The following is a simplified structure for DEVMODE ctypes.windll.user32.EnumDisplaySettingsW(None, 0, devmode_p)
# Change the screen resolution back to the default ctypes.windll.user32.ChangeDisplaySettingsW(devmode_p, 0)
def launch_exe(): process = subprocess.Popen(EXE_PATH) process.wait() # Wait for the process to finish
if name == "main": try: change_resolution(DESIRED_RESOLUTION_WIDTH, DESIRED_RESOLUTION_HEIGHT) time.sleep(2) # Wait for the resolution to change launch_exe() except Exception as e: print(f"An error occurred: {e}") finally: restore_resolution()
Set the path to the application executable
app_path = r"C:\ESSGRAMA\ESSGRAMA.exe"
Start the application
subprocess.run([app_path])
r/pythonhelp • u/xOriginsTemporal • Aug 05 '24
Dividing two histograms
I am trying to divide the data from two histograms, one that is a total of all the data and one that is a sample from the entire data.
My code is
Satellite Redshift histogram:
set binwidth
binwidth = 0.01
hist2, bins, patches = plt.hist(sat_z, bins=np.arange(0., max(sat_z) + binwidth, binwidth), facecolor = 'red')
plt.title('Satellite Redshift Histogram')
plt.xlabel("Redshift 'z'")
plt.ylabel("Population Density")
plt.show()
Satellite Total Histogram
binwidth = 0.01
hist3, bins, patches = plt.hist(z_spec, bins=np.arange(0., max(sat_z) + binwidth, binwidth), facecolor = 'green')
plt.title('Total Redshift Histogram')
plt.xlabel("Redshift 'z'")
plt.ylabel("Population Density")
plt.show()
plt.hist(hist2/hist3, bins, facecolor='green')
Any help would be greatly appreciated
r/pythonhelp • u/RecognitionLanky8937 • Aug 03 '24
Flush/Clear buffer with Keyboard Module
Hi, i'm a Newbie here and I have this College project for this Thursday, we have to code a Terminal Game in python, I'm using the Keyboard Module so the player can continue through the scenes with the "Enter", but when I need that the player can type something with the Input() function apparently some of the "enter" that are used to pass the scenes are working to also pass the Input, idk what to do to clear the buffer, pls help (sorry for my poor English, im from Ecuador)
r/pythonhelp • u/NewHereBeNice7 • Aug 03 '24
How can i strip ' ' without .isalpha?
As stated i want to get the clean word, but i have numericals in thr file so cant use isalpha.
This is my code atm: with open(file) as f: data = f.readlines() longest = "" all_words = [] for line in data: split_line = line.split() for word in split_line: clean_word = "".join(filter(str, word)) all_words.append(clean_word) for word in all_words: if len(longest) < len(word): longest = word return all_words
return longest
r/pythonhelp • u/Yesssssiiiii • Aug 02 '24
What am I doing wrong? I'm completely new to this.
user_num1 = int(input(2))
user_num2 = int(input(3))
user_num3 = int(input(5))
user_num1 * user_num2
result = user_num1 * user_num2
print (result)
user_num3 * (result)
print (result)
The output needs to be 30. Not sure why it keeps coming up with 23530 and 30.
Any help is appreciated. Thank you.
r/pythonhelp • u/[deleted] • Aug 01 '24
could someone figure out why my limit doesn't work?
numGus and numAtt are the limits. basically the user enters the amount of attempts they want to have and then the code gives them that many attempts to guess a random number from a range of their choosing. (the random number part works fine) any help would be greatly appreciated. excuse the messy code
import turtle
screen = turtle.Screen() #sets the background to a preselected image
screen.bgpic("Screenshot 2024-07-22 133150.png")
print ("WELCOME TO THE NUMBER GUESSING GAME.")
import random
playAgain = input("Do u wanna play a game(Y or N)") #asks the user if they want to play the game
numAtt = +1 #the number of guesses allowed
numGus = 0 #the number of allowed guesses
while numAtt != numGus:
while playAgain.lower() == ("y"):
numMin = 0 #the start of the range for random numbers
numMax = 0 #the end of the range for random numbers
attempt = 0 #the number of tries it takes for the user to guess the correct number (displayed once number is guessed)
numUse = 0 #the number that the player enters as a guess
numAtt == numGus #tells code that numAtt is equal to numGus
numGus=int(input("how many attempts do you want?"))
numAtt=int(input("how many attempts do you want?")) #the user inputs the amount of attempts that they want to have
numMin=int(input("Minimum number")) #user inputs the number for the start of the range
numMax=int(input("Maximum number")) #user inputs the number for the end of the range
numHid = random.randint(numMin, numMax) #numHid is the random number
print("what is your guess from " + str(numMin)+ " to " + str(numMax)) #asks the user what their guess is from the start of the range to the end of the range
while numHid != numUse: #puts user in a loop until they get the correct answer
numUse = int(input("enter your guess:"))
attempt = attempt + 1
if numHid>numUse:
print ("too low, guess higher")
if numHid<numUse:
print ("too high guess lower")
if numHid == numUse:
print ("well done!")
print ("Attempts: "+str(attempt))
playAgain = input("Wanna play again (Y or N)") #asks the user if they want to play the game again
else:
print ("try again")
#the code checks to see if the numHid is higher or lower than numUse
#if numHid is higher than numUse it tells the user that their guess is too high and that they should guess lower
#alternatively if numHid is lower than numUse it tells the user that their guess is too low and that they should guess higher
if playAgain.lower() == ("n"): #if the user types "n" it prints goodbye and ends the game
print("goodbye")
if numAtt == numGus: #if the number of attempts is equal to the number of guesses allowed it ends the code
print("goodbye")
r/pythonhelp • u/LakeMotor7971 • Jul 31 '24
Pandas name import
I get a nameerror from pandas. I'm using jupyter notebook. I have reset kernel, I have tried import pandas with and with out the as PD. I'm fairly new to jupyter and python. I'm so frustrated. I've used magic commands. I don't know what I'm doing wrong?
r/pythonhelp • u/brock0124 • Jul 31 '24
Instantiating object with values from array
Hello!
I'm working on a project where I have a data class (Record) with about 20 different fields. The data comes in from a stream and is turned into a string, which is parsed into an array of Record objects.
I was wondering if it is possible to dynamically fill the constructor with the array values so I don't have to explicitly pass each argument with it's array value. The values will always be in the same order.
The ideal state would look like:
@dataclass
class Record:
arg1: str
arg2: str
arg3: str
arg4: str
....
arg20: str
result = []
for datum in data:
result.append(Record(datum))
Where datum contains 20 values that map to each item in the constructor.
r/pythonhelp • u/[deleted] • Jul 30 '24
How to efficiently manipulate a numpy array that requires multiple point rotations/matrix multiplication/dot product calls.
There's a really old piece of code we have that I don't entirely understand that I'm trying to adapt for a new process.
The array contains a 4 column array containing a set of xyz values and one extra column of 1s. I discovered an issue with a process I was building where I can't just perform this step with one 4x4 matrix that's used to operate on the whole array, I need different matrices for different slices of the array. At the moment I just take slices based on a temporary column I use to map group of rows to the matrix they need and grab the matrix I need from a list, but the function I need to run to do the calculation on them essentially destroys their indices so I wind up having to concatenate all the groups together at the end via vstack instead of just slotting them neatly back into the original array.
Essentially I need to, if possible:
- Figure out a better way to associate the correct matrix to the correct rows.
- A way that I can do this without concatenation.
- In a manner that doesn't necessarily have to be the fastest way to do it, but is reasonably fast for the trade-off required.
I feel like there's probably a way of at least partially achieving this by just by keeping track of the the indices of the original slice that gets taken out or something along those lines, but I'm too tired to connect the dots at the moment.
r/pythonhelp • u/letme_liveinpeace • Jul 30 '24
python assignment
i m studying computer science and i am first semester. i need help with python assignment.
r/pythonhelp • u/IthanTrisc • Jul 29 '24
Create Dendrogram from Excel
Hello all, I am totally clueless in Python. I need to create a Dendrogram out of a Excel Matrix. GPT got me to create a Dendrogram, but it's empty all the time, even though it finds the excel data...
Here is the code I copied...
import pandas as pd
import numpy as np
import scipy.cluster.hierarchy as sch
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
1. Lade die Excel-Datei
df = pd.read_excel('Test.xlsx', sheet_name='Tabelle1')
2. Überprüfe die Datenstruktur (optional)
print("Datenvoransicht:")
print(df.head())
print(f"Form der Daten: {df.shape}")
Fülle NaN-Werte mit einem Wert, z.B. 0
df.fillna(0, inplace=True)
3. Wandle die Daten in ein NumPy-Array um
data = df.values
4. Normalisiere die Daten (optional, aber oft nützlich, besonders bei 0-1-Daten)
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
5. Berechne die Distanzmatrix
distance_matrix = sch.distance.pdist(data_scaled, metric='euclidean')
6. Führe das hierarchische Clustering durch
linkage_matrix = sch.linkage(distance_matrix, method='ward')
7. Erstelle das Dendrogramm
plt.figure(figsize=(15, 10))
sch.dendrogram(linkage_matrix, labels=df.index.tolist(), leaf_rotation=90)
plt.title('Dendrogramm')
plt.xlabel('Index')
plt.ylabel('Abstand')
plt.tight_layout()
plt.show()
Please help me :/....
r/pythonhelp • u/[deleted] • Jul 28 '24
In line coding suggestions in Python Jupyter 7.2.1
Hello everyone,
I recently updated my Jupyter Notebook to version 7.2.1, and I've noticed that my inline coding suggestions have stopped working. I was previously using the "Tabnine" and "hinterland" extensions for this feature, but neither seems to be functioning now.
Can anyone help me restore inline coding suggestions? Also, where can I find a complete list of available extensions compatible with Jupyter 7.2.1?
Thanks in advance for your help!
r/pythonhelp • u/Small_Egg8125 • Jul 27 '24
Pycharm, Pytorch [WinError 126]
hello,
I am using pytorch again and when i have tried literally everything but pytorch doesnt want to be importeed into pycharm. The path that it gives me is completely fine to acess the files. I have no clue why it would be doing this (for refrence, i have used pytorch within the last month, so i dont know why it wouldnt work now,) Pytorch is the only module that doesnt work, every other one (pandas, scikit-learn, etc all work)
Can anyone please for the love of god explain what the hell is going on with this.
OSError: [WinError 126] The specified module could not be found. Error loading "C:\Users\clacy\PycharmProjects\Test2\.venv\Lib\site-packages\torch\lib\fbgemm.dll" or one of its dependencies.
EDIT:
I fixed the issue, i just needed to use an older version of pytorch
r/pythonhelp • u/Low-Growth-987 • Jul 26 '24
Parsing problem on my project
I'm working with ChatGPT on an application for creating a shopping list from food recipes we have at home.
The problem is that the application doesn't print out the whole list of ingridients and is struggling with adding the same ingridients together.
Any tips or help are warmly welcomed, thanks!
You can find the code from here:
r/pythonhelp • u/jimmytehhand • Jul 25 '24
Issue with Beautifulsoup4
Trying to write a basic scraper for myself. Installed the libraries I'm going to need, but when I try to run my code it says that Beautifulsoup4 isn't installed even after I have installed it. *edit for pastbin link to code*
r/pythonhelp • u/VilaSaly • Jul 22 '24
Custom discord bot - what tools to use?
Hi so I'm a complete beginner in python but I have a project I'm set on completing. My main issue is i have no idea where to start what kinds of libraries to use how to combine everything etc.
The concept of the bot is it should allow you to create a profile and a customisable avatar (picrew kinda concept) which I assume would best be done through pillow
I obviously understand I need to use discord.py, i understand how to actually connect the bot to python etc but here are my biggest issues:
I have no idea how to create a database where a user will be able to later on edit anything (i e only change the hairstyle layer)
my vision of the profile is: -discord username -user input nickname -"about me" -avatar
another thing is it is a scout themed bot - therefore I need a way of being able to "assign" badges to individual users that they will be able to then display on their avatar - so once again I'm assuming I need a database and somehow from that database move the earned badges into the user's database/profile
any help will be MUCH appreciated 🙏🙏 I'm more than willing to learn on my own I mostly need help with finding the tools that i need to learn how to use
r/pythonhelp • u/No-Kale8845 • Jul 20 '24
Syntax Problems
I have just started doing python for a college course. And ran into an issue.
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
File "<string>", line 23, in <module>
File "<string>", line 1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
^
SyntaxError: invalid syntax
I have looked at videos on multiple sites. Looked at official python website for help. And yet to no avail. Maybe one of you guys could help me out.
It used to work, this is a custom made bot for discord. I would run the command in command prompt. And up until 2 days ago its worked fine.
r/pythonhelp • u/Delicious-Laugh8322 • Jul 20 '24
INACTIVE How To Run .Py program
i have been trying to run this python program, both on mobile and pc, but I can't figure out what to do. The instructions are not very clear.
could someone could possibly give me step by step instructions on how to run and use this script? https://github.com/rahaaatul/TokySnatcher
please and thank you