Python - Generators - How to use them and the benefits you receive
** Generators - How to use them and the benefits you receive
==========================================
def square_numbers(nums):
result = []
for i in nums:
result.append(i*i)
return result
my_nums = square_numbers([1, 2, 3, 4, 5])
print(my_nums)
---------------------------------
[1, 4, 9, 16, 25]
---------------------------------
==========================================
def square_numbers(nums):
for i in nums:
yield (i*i)
my_nums = square_numbers([1, 2, 3, 4, 5])
print(my_nums)
---------------------------------
generator object square_numbers at 0x016DC9C8
---------------------------------
==========================================
def square_numbers(nums):
for i in nums:
yield (i*i)
my_nums = square_numbers([1, 2, 3, 4, 5])
print(next(my_nums))
---------------------------------
1
---------------------------------
==========================================
def square_numbers(nums):
for i in nums:
yield (i*i)
my_nums = square_numbers([1, 2, 3, 4, 5])
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
---------------------------------
1
4
9
16
25
---------------------------------
==========================================
def square_numbers(nums):
for i in nums:
yield (i*i)
my_nums = square_numbers([1, 2, 3, 4, 5])
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
print(next(my_nums))
---------------------------------
print(next(my_nums))
StopIteration
---------------------------------
==========================================
def square_numbers(nums):
for i in nums:
yield (i*i)
my_nums = square_numbers([1, 2, 3, 4, 5])
for num in my_nums:
print(num)
---------------------------------
1
4
9
16
25
---------------------------------
==========================================
my_nums = [x * x for x in [1, 2, 3, 4, 5]]
for num in my_nums:
print(num)
---------------------------------
1
4
9
16
25
---------------------------------
==========================================
my_nums = [x * x for x in [1, 2, 3, 4, 5]]
print(my_nums)
for num in my_nums:
print(num)
---------------------------------
[1, 4, 9, 16, 25]
1
4
9
16
25
---------------------------------
==========================================
my_nums = (x * x for x in [1, 2, 3, 4, 5])
print(my_nums)
for num in my_nums:
print(num)
---------------------------------
generator object genexpr at 0x0250C9C8
1
4
9
16
25
---------------------------------
==========================================
my_nums = (x * x for x in [1, 2, 3, 4, 5])
print(list(my_nums))
---------------------------------
[1, 4, 9, 16, 25]
---------------------------------
==========================================
C:\Users\purunet>pip install Pympler
Collecting Pympler
Downloading Pympler-0.8.tar.gz (175 kB)
|████████████████████████████████| 175 kB 252 kB/s
Building wheels for collected packages: Pympler
Building wheel for Pympler (setup.py) ... done
Created wheel for Pympler: filename=Pympler-0.8-py3-none-any.whl size=164719 s
ha256=a294448bf7bb1b6e4b657728323d0e926cf291e41e1dfd2107bf4f61edd874ba
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\2c\09\38\
7c8c5dc224099d3a7194d154a99093d429bb4bda18c241c4b5
Successfully built Pympler
Installing collected packages: Pympler
Successfully installed Pympler-0.8
==========================================
C:\Users\purunet>pip install psutil
Collecting psutil
Downloading psutil-5.7.0-cp38-cp38-win32.whl (231 kB)
|████████████████████████████████| 231 kB 234 kB/s
Installing collected packages: psutil
Successfully installed psutil-5.7.0
==========================================
C:\Users\purunet>pip install resource
Collecting resource
Downloading Resource-0.2.1-py2.py3-none-any.whl (25 kB)
Collecting python-easyconfig>=0.1.0
Downloading Python_EasyConfig-0.1.7-py2.py3-none-any.whl (5.4 kB)
Collecting JsonForm>=0.0.2
Downloading JsonForm-0.0.2.tar.gz (2.4 kB)
Collecting JsonSir>=0.0.2
Downloading JsonSir-0.0.2.tar.gz (2.2 kB)
Collecting PyYAML
Downloading PyYAML-5.3.1-cp38-cp38-win32.whl (199 kB)
|████████████████████████████████| 199 kB 251 kB/s
Requirement already satisfied: six in c:\users\purunet\appdata\roaming\python\py
thon38\site-packages (from python-easyconfig>=0.1.0->resource) (1.14.0)
Collecting jsonschema
Downloading jsonschema-3.2.0-py2.py3-none-any.whl (56 kB)
|████████████████████████████████| 56 kB 490 kB/s
Collecting pyrsistent>=0.14.0
Downloading pyrsistent-0.16.0.tar.gz (108 kB)
|████████████████████████████████| 108 kB 504 kB/s
Requirement already satisfied: setuptools in c:\users\purunet\appdata\local\prog
rams\python\python38-32\lib\site-packages (from jsonschema->JsonForm>=0.0.2->res
ource) (41.2.0)
Collecting attrs>=17.4.0
Downloading attrs-19.3.0-py2.py3-none-any.whl (39 kB)
Building wheels for collected packages: JsonForm, JsonSir, pyrsistent
Building wheel for JsonForm (setup.py) ... done
Created wheel for JsonForm: filename=JsonForm-0.0.2-py3-none-any.whl size=3331
sha256=1cc4d42590c96068142b60dde492165f473ab84f49c04c04dc94c963bf7b416d
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\27\f1\cd\
8320913c1525700582412597b77e0db1221dd351e2a74360b9
Building wheel for JsonSir (setup.py) ... done
Created wheel for JsonSir: filename=JsonSir-0.0.2-py3-none-any.whl size=4778 s
ha256=f25ad7884be1a9c4cdc231aaa3cb5aa58c886c3501d421f3a426c284b6f76f3b
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\db\8f\60\
243dd2c23b0c34ef061da9d9600029bc99f7444449644867da
Building wheel for pyrsistent (setup.py) ... done
Created wheel for pyrsistent: filename=pyrsistent-0.16.0-cp38-cp38-win32.whl s
ize=56579 sha256=7eb7cd8eaa27cf515608dc161a46e96d742fffe13a2bdd22446a51245f675a2
c
Stored in directory: c:\users\purunet\appdata\local\pip\cache\wheels\17\be\0f\
727fb20889ada6aaaaba861f5f0eb21663533915429ad43f28
Successfully built JsonForm JsonSir pyrsistent
Installing collected packages: PyYAML, python-easyconfig, pyrsistent, attrs, jso
nschema, JsonForm, JsonSir, resource
Successfully installed JsonForm-0.0.2 JsonSir-0.0.2 PyYAML-5.3.1 attrs-19.3.0 js
onschema-3.2.0 pyrsistent-0.16.0 python-easyconfig-0.1.7 resource-0.2.1
==========================================
=======참고 people.py=======================
import mem_profile
import random
import time
names = ['John', 'Corey', 'Adam', 'Steve', 'Rick', 'Thomas']
majors = ['Math', 'Engineering', 'CompSci', 'Arts', 'Business']
print('Memory (Before): {}Mb'.format(mem_profile.memory_usage_resource()))
def people_list(num_people):
result = []
for i in xrange(num_people):
person = {
'id': i,
'name': random.choice(names),
'major': random.choice(majors)
}
result.append(person)
return result
def people_generator(num_people):
for i in xrange(num_people):
person = {
'id': i,
'name': random.choice(names),
'major': random.choice(majors)
}
yield person
t1 = time.clock()
people = people_list(1000)
t2 = time.clock()
# t1 = time.clock()
# people = people_generator(1000000)
# t2 = time.clock()
print('Memory (After) : {}Mb'.format(mem_profile.memory_usage_resource()))
print('Took {} Seconds'.format(t2-t1))
=======참고 mem_profile.py=======================
from pympler import summary, muppy
import psutil
import resource
import os
import sys
def memory_usage_psutil():
# return the memory usage in MB
process = psutil.Process(os.getpid())
mem = process.get_memory_info()[0] / float(2 ** 20)
return mem
def memory_usage_resource():
rusage_denom = 1024.
if sys.platform == 'darwin':
# ... it seems that in OSX the output is different units ...
rusage_denom = rusage_denom * rusage_denom
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom
return mem
==========================================
OS가 windows 면 Error 남.
import resource
ModuleNotFoundError: No module named 'resource'
==========================================