Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



 2000

Using Python Generator Comprehensions to Load Huge Combos

by clap - 12 February, 2021 - 10:25 PM
This post is by a banned member (clap) - Unhide
clap  
Godlike
519
Posts
134
Threads
5 Years of service
#1
(This post was last modified: 12 February, 2021 - 10:26 PM by clap.)
This is simply how to load in combos in a fast, efficient way. Let me elaborate what a generator is.

Think of it as a conveyor belt, what happens when you put your food on it? When the cashier needs something, they hold down the button (yielding) to allow one item to come down at a time.
Now, let's talk in Python terms. Yielding is a form of returning an object without destroying the state of the function. What this means is, we can yield from a file until there is nothing else to yield from.
How does this help up with loading combos? Well you see, I've seen a lot of people using List Comprehensions to load in combos. While not "terrible", this is a very inefficient and slow process that will not work
for larger files. Say for example, you want to strip a string, split it, and return the list. This is what most people would do
 
Code:
combos = open('combos.txt', 'r', encoding='utf-8', errors='ignore').readlines()
combos [_.strip().split(':') for _ in combos]

Scalability:
100k Lines = 0.3790s
1m Lines = 2.1860s

Note:
Numbers differ from computer to computer, they're not perfect however, gives a pretty close expectation

What do you know? It works! But, do you want something to just work, or work well? For larger projects (or files), you want both. This is where generators come into place. 
 
Code:
combos = open('combos.txt') # Read mode is not needed for either example, as it's defaulted to that.
combos = (combo.strip().split(':') for combo in combos)

Scalability:
100k Lines = 0.0s
1m Lines = 0.0s

Wow! That's a really big difference in numbers, why does this happen?

Let me explain:
When you use a list comprehension, it's like you're squishing a ball of paper into 1 ball, you keep squishing, mashing, and pounding until you get the desired ball. That's EXACTLY what a list comprehension does, it packs all the data into 1 list at 1 time.

What a generator comprehension does is squish the paper once very slightly every time you want something from the paper (generator / getting a combo). This takes no time! It just waits and waits, until it's needed for the paper to be mashed!
That's why, it's very fast and efficient.


That concludes my tutorial, leave a like if you learned anything,

Regards, iclapcheeks
Always confirm via PM before dealing with me.
This post is by a banned member (ANG) - Unhide

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)