Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coroutines
#1
today i am working on a project that probably should be a coroutine.  it takes in a list of strings.  it is expected that this list is very long.  it can be a tuple (it is not modified).  a page of a series of columns is constructed.  the order runs down each column until the bottom of the page, then another column is started.   as soon as a next column cannot fit on the page, then the page is done and a new page is started and the "next column" is the first column of the new page.  for now the whole list is passed to the function and the returned result is a list of pages.  each page is a list of strings that make up the formed lines of the page, ready to print.  also passed to the function is page geometry info assuming fixed sized characters: page width, page height, and gutter space between columns.  i am thinking that the usefulness of this can be expanded if it is a coroutine, or a generator that has data "sent" to it.  but i have not seen ... cannot find .. complete examples of this kind of thing (even if the example is something that we would never do as a coroutine).   my coroutine would be initialized with that geometry information then given one string at a time.  as soon as a page is complete, it would be yielded to the caller.  so i am looking for complete (shows both sides, fully ... how data is received and sent) examples (small, not filled with complexity of the example purpose).

a simple example i can imagine is a coroutine that adds all numbers it receives, and yields the running sum.  but an example needs to also show the use ... the other end ... the caller.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Have you gone through Beazley's presentation.  I found it very good, though admittedly I don't use coroutines and still find it a challenging subject.
http://www.dabeaz.com/coroutines/Coroutines.pdf
Reply
#3
(Feb-03-2017, 12:10 PM)Mekire Wrote: Beazley's presentation.

That at least answers my question about why to use coroutines instead of object instances (their lighter weight makes them faster).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
I would like to learn coroutines too but first time I tried I was scared  Big Grin Am I stupid  Huh
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Feb-03-2017, 12:10 PM)Mekire Wrote: Have you gone through Beazley's presentation.  I found it very good, though admittedly I don't use coroutines and still find it a challenging subject.
http://www.dabeaz.com/coroutines/Coroutines.pdf

yes. Smile

and it is now stored in our local share library (about a terabyte of goodies, such as lots of python docs).

i think it addresses most or all of the needed points.  but complete source for a simple generator and a simple app that uses it are what is lacking.  this example does not need to be an example of what is better done as a generator or coroutine.  i can figure that out.  i have made coroutines, before ... in both C and assembly.  in these languages i had to build the means to go back and forth, as well as the app.  now i need to fully grasp how to use all aspects of how this is done in python.

so tonight, more coding and refining my columnizer generator.  i may need to split it into 2 coroutines.  the first would be to take multiple lines of input and organize them (vertically) into a sequence of columns, each passed along as a list of strings.  the second would take multiple columns and organize them (horizontally) into a sequence of pages, each re-formed into a list of (now wider, with as many columns as will fit) strings that make a normal page.  this 2-coroutine organization will let me do things in between, better, such as adding headers over each column, or putting each column in a box by calling my boxit function for each column going between these coroutines.

(Feb-03-2017, 11:03 PM)wavic Wrote: I would like to learn coroutines too but first time I tried I was scared  Big Grin Am I stupid  Huh

doubtful.  anyone can read your past posts and see that you aren't.

it's just that coroutines are such a different concept (compared to what we have been doing), requiring us to understand things we didn't need to understand before, just to deal with the differences.  the big thing is context.   each context is like a thread or process, with the coroutine exchanges being the queues or pipelines.  diving in (writing some code) and getting wet (seeing the code) gets you experienced.

i need to do this, too.  i need to get a handle on python ways of things.

i think the end result for many will be:  why didn't i get to do things this way before?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
When you have the time, watch this video https://www.youtube.com/watch?v=Z_OAlIhXziw
Reply
#7
(Feb-04-2017, 04:15 AM)Larz60+ Wrote: When you have the time, watch this video https://www.youtube.com/watch?v=Z_OAlIhXziw

i do not have 3 hours time on any night, except when the net is down (so a pre-download-able mp4 could be useable).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Can you download it?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Quote:so a pre-download-able mp4 could be useable
Python has the best download module for Youtube,and a couple of hundred other sites.
youtube-dl
Usage:
youtube-dl url_from_youtube
Reply
#10
(Feb-04-2017, 11:03 AM)snippsat Wrote:
Quote:so a pre-download-able mp4 could be useable
Python has the best download module for Youtube,and a couple of hundred other sites.
youtube-dl
Usage:
youtube-dl url_from_youtube

This will download the best video and audio quality and will merge them. Most of the times the program packs the download to .mkv file.
But you can list all available formats with -F option.
victor@jerry:~$ youtube-dl -F https://youtu.be/Z_OAlIhXziw
[youtube] Z_OAlIhXziw: Downloading webpage
[youtube] Z_OAlIhXziw: Downloading video info webpage
[youtube] Z_OAlIhXziw: Extracting video information
[youtube] Z_OAlIhXziw: Downloading MPD manifest
[info] Available formats for Z_OAlIhXziw:
format code  extension  resolution note
249          webm       audio only DASH audio   67k , opus @ 50k (48000Hz), 70.54MiB
250          webm       audio only DASH audio   97k , opus @ 70k (48000Hz), 105.58MiB
171          webm       audio only DASH audio  116k , vorbis@128k (44100Hz), 138.91MiB
140          m4a        audio only DASH audio  138k , m4a_dash container, mp4a.40.2@128k (44100Hz), 173.02MiB
251          webm       audio only DASH audio  178k , opus @160k (48000Hz), 215.63MiB
278          webm       192x144    DASH video   91k , webm container, vp9, 15fps, video only, 59.29MiB
160          mp4        192x144    DASH video  150k , avc1.4d400c, 30fps, video only, 152.29MiB
242          webm       320x240    DASH video  206k , vp9, 30fps, video only, 66.51MiB
133          mp4        320x240    DASH video  306k , avc1.4d400d, 30fps, video only, 335.94MiB
243          webm       480x360    DASH video  381k , vp9, 30fps, video only, 140.15MiB
134          mp4        480x360    DASH video  446k , avc1.4d401e, 30fps, video only, 283.97MiB
244          webm       640x480    DASH video  724k , vp9, 30fps, video only, 283.95MiB
135          mp4        640x480    DASH video 1026k , avc1.4d401e, 30fps, video only, 735.69MiB
17           3gp        176x144    small , mp4v.20.3,  mp4a.40.2@ 24k
36           3gp        320x240    small , mp4v.20.3,  mp4a.40.2
18           mp4        480x360    medium , avc1.42001E,  mp4a.40.2@ 96k
43           webm       640x360    medium , vp8.0,  vorbis@128k (best)
Then: youtube-dl -f <format code> https://youtu.be/Z_OAlIhXziw
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in Linking two coroutines shankar 3 5,509 Jun-19-2020, 06:50 PM
Last Post: gogawaleankita

Forum Jump:

User Panel Messages

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