Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103288 > unrolled thread

how to get the list form dictionary's values

Started bydavidbenny2000@gmail.com
First post2016-02-21 05:34 -0800
Last post2016-02-21 06:21 -0800
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  how to get the list form dictionary's values davidbenny2000@gmail.com - 2016-02-21 05:34 -0800
    Re: how to get the list form dictionary's values Chris Angelico <rosuav@gmail.com> - 2016-02-22 01:04 +1100
      Re: how to get the list form dictionary's values Ho Yeung Lee <davidbenny2000@gmail.com> - 2016-02-21 06:15 -0800
        Re: how to get the list form dictionary's values Chris Angelico <rosuav@gmail.com> - 2016-02-22 02:01 +1100
          Re: how to get the list form dictionary's values Marko Rauhamaa <marko@pacujo.net> - 2016-02-21 17:16 +0200
    Re: how to get the list form dictionary's values Peter Otten <__peter__@web.de> - 2016-02-21 15:06 +0100
      Re: how to get the list form dictionary's values Ho Yeung Lee <davidbenny2000@gmail.com> - 2016-02-21 06:21 -0800

#103288 — how to get the list form dictionary's values

Fromdavidbenny2000@gmail.com
Date2016-02-21 05:34 -0800
Subjecthow to get the list form dictionary's values
Message-ID<554c09f2-6a6b-4576-9c9d-50d6234c8583@googlegroups.com>
  File "mainpy.py", line 81
    for functionlistelement in functionlist0
                                           ^
SyntaxError: invalid syntax


import asyncio

def f000():
 try:
  print "000"
 except:
  print "000 exception"

def f001():
 try:
  print "001"
 except:
  print "001 exception"

def f002():
 try:
  print "002"
 except:
  print "002 exception"

def f003():
 try:
  print "003"
 except:
  print "003 exception"

def f004():
 try:
  print "004"
 except:
  print "004 exception"

machine = {}
mappedfunc = {}
functionlist000 = []
functionlist001 = []
functionlist002 = []
functionlist003 = []
functionlist004 = []

functionlist000.append('002')
functionlist001.append('000')
functionlist002.append('003')
functionlist002.append('004')

functionlist003.append('001')
functionlist004.append('001')

machine000 = {'000': functionlist000}
machine001 = {'001': functionlist001}
machine002 = {'002': functionlist002}
machine003 = {'003': functionlist003}
machine004 = {'004': functionlist004}

machine.update(machine000)
machine.update(machine001)
machine.update(machine002)
machine.update(machine003)
machine.update(machine004)

functionkey000 = {'000': f000 }
functionkey001 = {'001': f001 }
functionkey002 = {'002': f002 }
functionkey002 = {'003': f003 }
functionkey002 = {'004': f004 }

mappedfunc.update(functionkey000)
mappedfunc.update(functionkey001)
mappedfunc.update(functionkey002)
mappedfunc.update(functionkey003)
mappedfunc.update(functionkey004)

def workingthreadpool(currentnumber1, machine1):
 try:
  functionlist0 = machine1.get(currentnumber1)

  loop = asyncio.get_event_loop()
  tesks = []
  
  j = 1
  for functionlistelement in functionlist0
   tesks.append(asyncio.ensure_future(mappedfunc.get(functionlistelement)()))
   if j > 1:
    workingthreadpool(functionlistelement)
   j = j + 1

  loop.run_until_complete(asyncio.wait(tesks))
  loop.close()
 except:
  print "workingthreadpool exception"

currentnumber = "000"
workingthreadpool(currentnumber, machine)

[toc] | [next] | [standalone]


#103290

FromChris Angelico <rosuav@gmail.com>
Date2016-02-22 01:04 +1100
Message-ID<mailman.6.1456063449.20994.python-list@python.org>
In reply to#103288
On Mon, Feb 22, 2016 at 12:34 AM,  <davidbenny2000@gmail.com> wrote:
>   File "mainpy.py", line 81
>     for functionlistelement in functionlist0
>                                            ^
> SyntaxError: invalid syntax

Your actual syntax problem here is simply a missing colon. That's
easily fixed. But please, PLEASE, don't do this:

> def workingthreadpool(currentnumber1, machine1):
>  try:
>   functionlist0 = machine1.get(currentnumber1)
>
>   loop = asyncio.get_event_loop()
>   tesks = []
>
>   j = 1
>   for functionlistelement in functionlist0
>    tesks.append(asyncio.ensure_future(mappedfunc.get(functionlistelement)()))
>    if j > 1:
>     workingthreadpool(functionlistelement)
>    j = j + 1
>
>   loop.run_until_complete(asyncio.wait(tesks))
>   loop.close()
>  except:
>   print "workingthreadpool exception"

1) Indenting by a single space makes it hard to skim and see what's at
what indentation level. Indent by at least four spaces (I've seen two,
and it's still very narrow), or one tab.

2) Carrying a counter through a 'for' loop is much better done with
enumerate(). But the counter's sole purpose is to ignore the first
element, so you might want to find a completely different approach.

3) Why does this recurse anyway? Your function appends one future to
'tesks' (should that be 'tasks'?), then appends another... and then
calls itself recursively. What's that doing, exactly?

4) Bare except clauses are almost always a bad idea. In fact, if a
future version of Python raises a SyntaxWarning or even SyntaxError on
the bare except, I would see it as no loss. As of Python 2.7, it's
possible to raise an old-style class, which then won't be caught by
"except Exception:" or even "except BaseException:", but I'm pretty
sure there's nothing you can raise in Python 3 that isn't caught by
"except BaseException:". Proper exception handling generally means
either (a) catching a specific exception or list of exceptions, and
handling them; or (b) catching everything, logging them in some way,
and returning to some sort of main loop. Your code is simply reporting
the presence of an exception, nothing more, and then bailing out. Let
those exceptions bubble up to top level and be printed out!

5) Asynchronous code is way better supported in Python 3.5 than in
2.7. I strongly recommend upgrading your Python.

These considerations are less simple than the syntax issue you were
asking about, but they're also non-trivial problems. You might get
your code to _run_, but can you get it to do the right thing, even
after you tinker with it later on? Good code saves you time in the
long run.

ChrisA

[toc] | [prev] | [next] | [standalone]


#103292

FromHo Yeung Lee <davidbenny2000@gmail.com>
Date2016-02-21 06:15 -0800
Message-ID<813ad31d-b208-496f-91b0-5f39aef28965@googlegroups.com>
In reply to#103290
Hi Chris,

0 ---> 2 --> 3--> 1 ---> 0
        ---> 4 /

i am practicing task flow in this graph situation

when current state is 2 , there are 3 and 4 to run parallel and wait list of tasks finish before running to 1 , 

however i feel that my code has been wrong because 3 and 4 can not combine to run task 1 , 

which is the correct way to combine to run task 1?

or is this graph situation an example impossible to do in computer science?

Regards,

Martin Lee

Chris Angelico於 2016年2月21日星期日 UTC+8下午10時04分22秒寫道:
> On Mon, Feb 22, 2016 at 12:34 AM,  <davidbenny2000@gmail.com> wrote:
> >   File "mainpy.py", line 81
> >     for functionlistelement in functionlist0
> >                                            ^
> > SyntaxError: invalid syntax
> 
> Your actual syntax problem here is simply a missing colon. That's
> easily fixed. But please, PLEASE, don't do this:
> 
> > def workingthreadpool(currentnumber1, machine1):
> >  try:
> >   functionlist0 = machine1.get(currentnumber1)
> >
> >   loop = asyncio.get_event_loop()
> >   tesks = []
> >
> >   j = 1
> >   for functionlistelement in functionlist0
> >    tesks.append(asyncio.ensure_future(mappedfunc.get(functionlistelement)()))
> >    if j > 1:
> >     workingthreadpool(functionlistelement)
> >    j = j + 1
> >
> >   loop.run_until_complete(asyncio.wait(tesks))
> >   loop.close()
> >  except:
> >   print "workingthreadpool exception"
> 
> 1) Indenting by a single space makes it hard to skim and see what's at
> what indentation level. Indent by at least four spaces (I've seen two,
> and it's still very narrow), or one tab.
> 
> 2) Carrying a counter through a 'for' loop is much better done with
> enumerate(). But the counter's sole purpose is to ignore the first
> element, so you might want to find a completely different approach.
> 
> 3) Why does this recurse anyway? Your function appends one future to
> 'tesks' (should that be 'tasks'?), then appends another... and then
> calls itself recursively. What's that doing, exactly?
> 
> 4) Bare except clauses are almost always a bad idea. In fact, if a
> future version of Python raises a SyntaxWarning or even SyntaxError on
> the bare except, I would see it as no loss. As of Python 2.7, it's
> possible to raise an old-style class, which then won't be caught by
> "except Exception:" or even "except BaseException:", but I'm pretty
> sure there's nothing you can raise in Python 3 that isn't caught by
> "except BaseException:". Proper exception handling generally means
> either (a) catching a specific exception or list of exceptions, and
> handling them; or (b) catching everything, logging them in some way,
> and returning to some sort of main loop. Your code is simply reporting
> the presence of an exception, nothing more, and then bailing out. Let
> those exceptions bubble up to top level and be printed out!
> 
> 5) Asynchronous code is way better supported in Python 3.5 than in
> 2.7. I strongly recommend upgrading your Python.
> 
> These considerations are less simple than the syntax issue you were
> asking about, but they're also non-trivial problems. You might get
> your code to _run_, but can you get it to do the right thing, even
> after you tinker with it later on? Good code saves you time in the
> long run.
> 
> ChrisA

[toc] | [prev] | [next] | [standalone]


#103294

FromChris Angelico <rosuav@gmail.com>
Date2016-02-22 02:01 +1100
Message-ID<mailman.8.1456066906.20994.python-list@python.org>
In reply to#103292
On Mon, Feb 22, 2016 at 1:15 AM, Ho Yeung Lee <davidbenny2000@gmail.com> wrote:
> Hi Chris,
>
> 0 ---> 2 --> 3--> 1 ---> 0
>         ---> 4 /
>
> i am practicing task flow in this graph situation
>
> when current state is 2 , there are 3 and 4 to run parallel and wait list of tasks finish before running to 1 ,
>
> however i feel that my code has been wrong because 3 and 4 can not combine to run task 1 ,
>
> which is the correct way to combine to run task 1?

So after task 2 completes, tasks 3 and 4 both become available (and
can run in parallel), but then task 1 must wait until both have
finished before it runs? I'm not an asyncio expert, but this sounds
like a fairly straight-forward dependency requirement. If you were to
implement these as four separate threads, you would have threads 3 and
4 raise semaphores which thread 1 blocks on; I'm sure there'll be an
equivalent in asyncio.

One way to do it would be something like this:

import asyncio

loop = asyncio.get_event_loop()

async def starter():
    print("Starting!")
    global t1, t2
    t1 = asyncio.Task(dep1())
    t2 = asyncio.Task(dep2())
    print("Done!")
    await dependent()

async def dep1():
    print("Dependency 1")
    await asyncio.sleep(3)
    print("Dep 1 done.")

async def dep2():
    print("Dependency 2")
    await asyncio.sleep(2)
    print("Dep 2 done.")

async def dependent():
    await t1
    await t2
    print("Deps are done - dependent can run.")

loop.run_until_complete(starter())



The dependent task simply waits for each of its dependencies in turn.
It doesn't matter which one finishes first; it won't do its main work
until both are done. And if your starter and dependent are logically
connected, you can just have the 'await' lines in the middle of one
tidy function.

(Can an asyncio expert tell me how to tidy this code up, please? I'm
sure this isn't the cleanest.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#103296

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-02-21 17:16 +0200
Message-ID<87vb5inlfu.fsf@elektro.pacujo.net>
In reply to#103294
Chris Angelico <rosuav@gmail.com>:

> On Mon, Feb 22, 2016 at 1:15 AM, Ho Yeung Lee <davidbenny2000@gmail.com> wrote:
>> 0 ---> 2 --> 3--> 1 ---> 0
>>         ---> 4 /
>
> So after task 2 completes, tasks 3 and 4 both become available (and
> can run in parallel), but then task 1 must wait until both have
> finished before it runs? I'm not an asyncio expert, but this sounds
> like a fairly straight-forward dependency requirement. If you were to
> implement these as four separate threads, you would have threads 3 and
> 4 raise semaphores which thread 1 blocks on; I'm sure there'll be an
> equivalent in asyncio.
>
> [...]
>
> (Can an asyncio expert tell me how to tidy this code up, please? I'm
> sure this isn't the cleanest.)

Not an expert, but this does the trick:

========================================================================
#!/usr/bin/env python3

import asyncio

def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(t0())
    
async def t0():
    print("start t0")
    await t2()
    await asyncio.wait([ t3(), t4() ])
    await t1()
    print("finish t0")

async def t1():
    print("start t1")
    await asyncio.sleep(1)
    print("finish t1")

async def t2():
    print("start t2")
    await asyncio.sleep(1)
    print("finish t2")

async def t3():
    print("start t3")
    await asyncio.sleep(1)
    print("finish t3")

async def t4():
    print("start t4")
    await asyncio.sleep(1)
    print("finish t4")

if __name__ == '__main__':
    main()
========================================================================


Marko

[toc] | [prev] | [next] | [standalone]


#103291

FromPeter Otten <__peter__@web.de>
Date2016-02-21 15:06 +0100
Message-ID<mailman.7.1456063625.20994.python-list@python.org>
In reply to#103288
davidbenny2000@gmail.com wrote:

>   File "mainpy.py", line 81
>     for functionlistelement in functionlist0
>                                            ^
> SyntaxError: invalid syntax
> 
> 
> import asyncio

[snip]

> mappedfunc = {}
> functionlist000 = []
> functionlist001 = []
> functionlist002 = []
> functionlist003 = []
> functionlist004 = []

[snip many names with numeric suffix and repeated method calls]

I have no idea what you are trying to do; however, your code looks awfully 
redundant. This is errorprone:

functionkey000 = {'000': f000 }
functionkey001 = {'001': f001 }
functionkey002 = {'002': f002 }
functionkey002 = {'003': f003 }
functionkey002 = {'004': f004 }

>   for functionlistelement in functionlist0

To answer what seems to be your actual question: a colon is missing at the 
end of this line.

[toc] | [prev] | [next] | [standalone]


#103293

FromHo Yeung Lee <davidbenny2000@gmail.com>
Date2016-02-21 06:21 -0800
Message-ID<e7fec4ec-66ee-4095-bb74-58f206cbf834@googlegroups.com>
In reply to#103291
Peter Otten於 2016年2月21日星期日 UTC+8下午10時07分18秒寫道:
> davidbenny2000@gmail.com wrote:
> 
> >   File "mainpy.py", line 81
> >     for functionlistelement in functionlist0
> >                                            ^
> > SyntaxError: invalid syntax
> > 
> > 
> > import asyncio
> 
> [snip]
> 
> > mappedfunc = {}
> > functionlist000 = []
> > functionlist001 = []
> > functionlist002 = []
> > functionlist003 = []
> > functionlist004 = []
> 
> [snip many names with numeric suffix and repeated method calls]
> 
> I have no idea what you are trying to do; however, your code looks awfully 
> redundant. This is errorprone:
> 
> functionkey000 = {'000': f000 }
> functionkey001 = {'001': f001 }
> functionkey002 = {'002': f002 }
> functionkey002 = {'003': f003 }
> functionkey002 = {'004': f004 }
> 
> >   for functionlistelement in functionlist0
> 
> To answer what seems to be your actual question: a colon is missing at the 
> end of this line.


hi Peter,

i am running this task flow in this graph,

these code are not redundant, because this writing can clearly
show the work flow

https://drive.google.com/file/d/0B7fHc_dTzkY_OGMtTGI2UnR6ZEE/view?usp=sharing


Regards,

Martin Lee

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web