#🔒 python project
33 messages · Page 1 of 1 (latest)
@solid steeple
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
these pics are AI generated
Ok? Why do you need the AI? Also, please copy & paste the text next time rather than give pictures, as they are usually more accessible than pictures.
wait
Also, what is your specific problem?
Imagine you are writing a simple inventory tracker app for a tech YouTuber who builds PCs on their channel.
They have a stash of computer parts in their studio (for example: five AMD Ryzen processors).
When they build a PC for a video, they take a part from the stash.
When they take apart an old PC, they return the part to the stash.
Sometimes they buy new parts to add to the stash.
Sometimes they push a part too hard, catch it on fire, and break it completely—meaning it has to be thrown away forever.
Your job is to build the Python blueprints (Classes) that can track these parts and numbers while the program is running. You don’t need to worry about saving this data to a file or a database.
Blueprint 1: The General "Resource" (The Master Rulebook)
Every single item in the studio is a "Resource." Whether it’s a CPU or an SSD, all of them need to track these basic details:
Name: What it's called (e.g., "Intel Core i9").
Manufacturer: Who made it (e.g., "Nvidia", "AMD").
Total: The overall number of these parts they own in total.```
Category: A smart shortcut that looks at the item and automatically says its type in lowercase (like "cpu" or "sdd").
Things a Resource Can Do (The Methods)
You need to write actions that change the inventory counts safely:
Claim: Take n parts out of the stash to use in a PC build. (You can't take more than you actually have left on the shelf!).
Free up: Take apart a PC and put n parts back on the shelf.
Died: Permanent accidental destruction! Permanently remove n parts from both the total ownership and the active build list because they got fried.
Purchased: A delivery truck arrived! Add n brand-new parts to the total count.
The Safety Rules
Printing the item normally should just show its basic Name.
Printing the technical version (__repr__) should show a detailed summary of its current stats.
The lockdown: The basic details (Name, Manufacturer) and the inventory counts (Total, Allocated) should be protected so they can't be edited directly from the outside like a free-for-all. The only way to change the inventory counts is by using the official actions above (Claim, Free up, Died, Purchased).```
Blueprint 2: "Storage" (The Middleman)
Some parts are specific. Hard drives (HDDs) and Solid State Drives (SSDs) are both used to hold files. Instead of writing copy-pasted code for both, create a middleman blueprint called Storage.
It automatically gets everything from the main Resource rulebook.
It adds one new specific rule: It must track Capacity in GB (e.g., 500 GB).
Blueprint 3: The Specific Parts
Now, you create the actual final blueprints for the specific items the YouTuber uses. They will inherit the rules from the blueprints above and add their own unique tech specs:
CPU (Processor): Inherits from Resource. Adds:
Number of Cores (e.g., 8)
Socket Type (e.g., "AM4")
Power Usage in Watts (e.g., 95)
HDD (Mechanical Hard Drive): Inherits from Storage. Adds:
Physical Size (e.g., 2.5 inches or 3.5 inches)
Spin Speed / RPM (e.g., 7200)
SSD (Solid State Drive): Inherits from Storage. Adds:
Connection Type / Interface (e.g., "NVMe PCIe 4.0")
Final Step Goal
Every single one of these final classes needs a setup function (__init__) that lets you input all their stats at birth, checks to make sure numeric values make sense (no negative power watts or negative storage sizes), and prints a nice, clear summary when looked at in the console.```
? do you understand what is going on in those first images ?
which first images? the AI ones, or the prject assignment ones?
from typing import Tuple
class Resource:
def __init__(self, name: str, manufacturer: str):
self.name = name
self.manufacturer = manufacter
class StockPile:
def __init__(self):
self.resources: list[Tuple[Resource, int, int]] = []
# the first int in the tuple is the amount
# The second int is how many are allocated
# now you should define the class methods
also, gtg, sry
the project ones
yes? I have basic literacy skills
i mean ...you could....but i dont
so that is why i took help
is it ok?
what is name: str
and manufacturer: str
This is type hinting, it doesn't do anything when the program is acutally run, but it is very useful for development, as it tells other developers (including future you) what the type is supposed to be. But another reason to do it is for IDEs, as with language servers that do type checking, they can help make sure that your program won't come across any TypeErrors
The list in type hinting is basically just list[type], e.g. list[str], this signifies that it is a list of strs
The Tuple from the typing module, basically just works as Tuple[ItemOne, ItemTwo, ItemThree, AndSoOn]
oh wait, sry, this won't really be able to built upon, I forgot that python tuples are immutable, lemme fix it rq
from typing import Type
class ResourcePile:
def __init__(self, name: str, manufacturer: str, amount: int, allocated: int):
# putting __ in front of them tells python to mangle them so others can't use it, and tells devs that they shouldn't use it
# maybe this could be used for the lockdown
self.name = name
self.manufacturer = manufacter
self.amount = amount
self.allocated = allocated
# you need to fill out these functions
# don't forget to sanitize the inputs, by checking for negative numbers
def claim(self, n: int):
pass
def free(self, n: int):
pass
def died(self, n: int):
pass
def purchased(self, n: int):
pass
def __str__(self) -> str:
return self.name
# show a detailed summary of it's stats
def __repr__(self) -> str:
pass
class Storage(ResourcePile):
def __init__(self, name: str, manufacturer: str, amount: int, allocated: int, capacity: int)
self.capacity = capacity
# The super() function gets the base class.
super().__init__(name, manufacturer, amount, allocated, space)
class Cpu(ResourcePile):
category = "cpu"
def __init__(self, name: str, manufacturer: str, amount: int, allocated: int, cores: int)
self.cores = cores
super().__init__(name, manufacturer, amount, allocated, space)
class Ssd(Storage):
category = "ssd"
# you should maybe make more classes like these
# don't forget to add the class specific properites
# IMPORTANT!
# don't forget to make sure **ALL** of the inputted values to the mehtods are correct
tuple[int, str, str] (for example) will work just as well without any imports
-# didn't know that, thanks for informing me
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.