[Tutor] Sorting the parts of a dictionary into a list

2012-05-06 Thread Jacob Bender
Dear tutors,

I'm trying to create a neural network program. Each neuron is in a
dictionary and each of its connections and their strengths are in a nested
dictionary. So {0:{1:4, 2:5}}, 1:{0:6}, 2:{1:2}} would mean that neuron 0
is connected to neuron 1 with a strength of 4. And it also means that
neuron 1 is connected to neuron 0 with a strength of 6.

The problem is that I'm working on a function that is supposed to add the
total strengths of each neuron. So, for example, neuron 0's connections
have a total strength of 9 (4+5). The other problem is getting all of the
total strengths and ordering the neurons into a list. So, from the example,
the list would be from [0,1,2] because zero has the greatest total strength
of 9, then 1 with a total strength of 6 and so on. I've been working on
this problem for at least 2 hours now and still haven't found anything
close to a solution.

Thank you and please help!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sorting the parts of a dictionary into a list

2012-05-06 Thread Peter Otten
Jacob Bender wrote:

> Dear tutors,
> 
> I'm trying to create a neural network program. Each neuron is in a
> dictionary and each of its connections and their strengths are in a nested
> dictionary. So {0:{1:4, 2:5}}, 1:{0:6}, 2:{1:2}} would mean that neuron 0
> is connected to neuron 1 with a strength of 4. And it also means that
> neuron 1 is connected to neuron 0 with a strength of 6.
> 
> The problem is that I'm working on a function that is supposed to add the
> total strengths of each neuron. So, for example, neuron 0's connections
> have a total strength of 9 (4+5). The other problem is getting all of the
> total strengths and ordering the neurons into a list. So, from the
> example, the list would be from [0,1,2] because zero has the greatest
> total strength of 9, then 1 with a total strength of 6 and so on. I've
> been working on this problem for at least 2 hours now and still haven't
> found anything close to a solution.

It is always a good idea to post the code you have -- if only to give as an 
idea of your abilities.
 
> Thank you and please help!

You need a function to calculate the total strength

def total_strength(neuron):
# calculate and return total strength

You can then sort the dictionary keys:

connections = {0:{1:4, 2:5}, 1:{0:6}, 2:{1:2}}
neurons_by_strength = sorted(connections, key=total_strength)

To give you an idea how the key function works:

>>> sorted([3, -2, 1], key=abs) # abs() calculates the absolute value
[1, -2, 3]

Writing the body of the total_strength() function is not hard: look up the 
neuron in the connections dict and sum up the values of the inner dict.

Come back here should these hints not be sufficient to get you going. 
Remember to provide some code next time ;)

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] error when using using subprocess.popen in Python wrapper script

2012-05-06 Thread Rogelio
I am new to Python and am trying to figure out how to execute Linux
commands via a Python wrapper.

This works

**
#this works okay
import subprocess

uname = "uname"
uname_arg = "-a"

subprocess.call([uname,uname_arg])
**

But this doesn't.

***
#this doesn't work
import subprocess

#perl prog works in real life
perl_prog = "perl perlprog.pl"
perl_prog_h ="-h"

#this is where it breaks
subprocess.call([perl_prog, perl_prog_h])



I get the following error when I run the program

python errorCheck.py

Linux mybox.domain.com 2.6.18-238.9.1.el5 #1 SMP Tue Apr 12 18:10:13
EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

Traceback (most recent call last):
  File "errorCheck.py", line 16, in ?
subprocess.call([perl_prog, perl_prog_h])
  File "/usr/lib64/python2.4/subprocess.py", line 419, in call
return Popen(*args, **kwargs).wait()
  File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
errread, errwrite)
  File "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory


any suggestions?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] error when using using subprocess.popen in Python wrapper script

2012-05-06 Thread Martin A. Brown

Hello,

 : perl_prog = "perl perlprog.pl"
 : perl_prog_h ="-h"
 : #this is where it breaks
 : subprocess.call([perl_prog, perl_prog_h])

This is asking your Linux to search your $PATH and execute a program 
called:

  'perl perlprog.pl'

Rather than to execute a program called 'perl' and pass as the first 
argument 'perlprog.pl'.

  perl_bin = 'perl'
  perl_prog = '/path/to/perlprog.pl'
  perl_prog_h = '-h'
  subprocess.call([perl_bin, perl_prog, perl_prog_h])

Or, if I were in your shoes, I would do something a bit more like 
this:

  cmd = [ '/usr/bin/perl', '/path/to/perlprog.pl', '-h' ]
  subprocess.call(cmd)

Probably the most interesting thing here for you to note in the long 
run is to learn what 'magically' happens in the shell when you run a 
command.  Since you are using Linux, you may find strace useful 
to see what Python is passing to your system for execution:

  strace -e process python /path/to/your/python/script.py

Enjoy,

-Martin

-- 
Martin A. Brown
http://linux-ip.net/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] error when using using subprocess.popen in Python wrapper script

2012-05-06 Thread Rogelio
On Sun, May 6, 2012 at 8:23 PM, Martin A. Brown  wrote:
> Or, if I were in your shoes, I would do something a bit more like
> this:
>
>  cmd = [ '/usr/bin/perl', '/path/to/perlprog.pl', '-h' ]
>  subprocess.call(cmd)


Thank you, Martin.  This was helpful.  Installed Strace and found out
that I mispelled something.  :b

I suspect that I'm having a problem with it reading a long string, but
I'm not sure why.  (Need to escape a char? Or perhaps something is
timing out?)

This works

cmd = ['perl','/path/to/my/script.pl, '-h']
subprocess.call(cmd)

But this does not

cmd = ['perl','/path/to/my/script.pl, '-x arg1 -y arg2 -z arg3']
subprocess.call(cmd)

strace gives me this output

***

MY COMMAND: strace -e process python myProg.py

OUTPUT

execve("/usr/bin/python", ["python", "myProg.py"], [/* 22 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x2b75cfb731e0) = 0
clone(child_stack=0,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x2b75cfb73270) = 15883
wait4(15883, 
+---+
|   (output from script file)

ERROR: You must specify either a command to be executed or an a
command file for execution
[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 15883
--- SIGCHLD (Child exited) @ 0 (0) ---
exit_group(0)   = ?
*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor