Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pipelined generator
#1
hi
in the below code, pipelined generator has been used to sum values in a" techcrunch.csv" file and the code is in site:
https://realpython.com/introduction-to-p...generators
file_name = "techcrunch.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == "a"
)
total_series_a = sum(funding)
print(f"Total series A fundraising: ${total_series_a}")
it is some ambiguous, however, at the end of the page has suggested changing this code so that in addition to sum, the average of the values should be calculated.
I thought about it, but I did not find anything.
do you have any suggestions?

second question:
(in IDLE), when I want to see the methods of a generator, I first create a generator and then I write dir(name of the generator).
it is time-consuming.
is it possible to without creating a generator, see methods related to generators? ( for string, without creating a string, I write dir(str))
also, I have this question about the result of the command"file=open(address)". namely, i want dir(f) without creating f.
I always do this time-consuming:
f= open(an address of a file)
dir(f)


thanks

Attached Files

.csv   techcrunch.csv (Size: 91.34 KB / Downloads: 63)
Reply
#2
(Oct-26-2023, 11:10 AM)akbarza Wrote: is it possible to without creating a generator, see methods related to generators?
You could add this in your module usercustomize.py
__builtins__.generator = type((x for x in ()))
Then you could use directly
dir(generator)
# or better
help(generator)
Quote:also, I have this question about the result of the command"file=open(address)". namely, i want dir(f) without creating f.
I suggest
import io
help(io.FileIO)
# or perhaps
help(io.TextIOWrapper)
akbarza likes this post
Reply
#3
I'd make funding a list.
funding = [
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == "a"
]
now you can get sum(funding) and len(funding).
Reply
#4
(Oct-28-2023, 04:13 AM)deanhystad Wrote: I'd make funding a list.
funding = [
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == "a"
]
now you can get sum(funding) and len(funding).

hi
the issue and problem is that we work with a large or huge file and we can not use a list. as you can see in the first code in this thread, all things were written in a generator not a list.
thanks for reply
Reply
#5
I can't read realpython.com because they want me to create an account, but you could do it like this
from collections import deque
from pathlib import Path

file_name = str(Path(__file__).parent/"techcrunch.csv")
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == "a"
)
cumsum = 0
sums = ((cumsum := cumsum + f) for f in funding)
size, total_series_a = deque(enumerate(sums, 1), maxlen=1)[-1]
print(f"Total series A fundraising: ${total_series_a}")
print(f"Data size: {size}")
print(f"Average: {total_series_a/size}")
akbarza likes this post
Reply
#6
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
Could be reduced to:
list_line = (s.rstrip().split(",") for s in open(file_name))
akbarza likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
(Oct-28-2023, 11:30 AM)Gribouillis Wrote: I can't read realpython.com because they want me to create an account, but you could do it like this
from collections import deque
from pathlib import Path

file_name = str(Path(__file__).parent/"techcrunch.csv")
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols, data)) for data in list_line)
funding = (
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == "a"
)
cumsum = 0
sums = ((cumsum := cumsum + f) for f in funding)
size, total_series_a = deque(enumerate(sums, 1), maxlen=1)[-1]
print(f"Total series A fundraising: ${total_series_a}")
print(f"Data size: {size}")
print(f"Average: {total_series_a/size}")

hi and thanks for the reply
I searched the net for generator in Chrome and then I chose the realpython and I went to it(also I have realpython user and pass but I went it without using them.).
I do not understand the line:
size, total_series_a = deque(enumerate(sums, 1), maxlen=1)[-1]
what is deque and its parameters? what is [-1] at the end of it?
Reply
#8
(Oct-26-2023, 12:19 PM)Gribouillis Wrote:
(Oct-26-2023, 11:10 AM)akbarza Wrote: is it possible to without creating a generator, see methods related to generators?
You could add this in your module usercustomize.py
__builtins__.generator = type((x for x in ()))
Then you could use directly
dir(generator)
# or better
help(generator)
Quote:also, I have this question about the result of the command"file=open(address)". namely, i want dir(f) without creating f.
I suggest
import io
help(io.FileIO)
# or perhaps
help(io.TextIOWrapper)

hi
what does __builtins__ ? can we use it as another object?
Reply
#9
https://docs.python.org/3/library/builti...e-builtins
Quote:As an implementation detail, most modules have the name __builtins__ made available as part of their globals. The value of __builtins__ is normally either this module or the value of this module’s __dict__ attribute. Since this is an implementation detail, it may not be used by alternate implementations of Python.
akbarza likes this post
Reply
#10
hi
is there any guide about deque?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  receive from a generator, send to a generator Skaperen 9 5,593 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020