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

2012-05-07 Thread Rogelio
If I want to write this command to a file, would this be the right format?

*
import subprocess

(all my variables defined okay)

perl_script=subprocess.call(['perl',perl_prog,ipfile,cmd,user,timeout,])

log=open('/tmp/pythonOutput.txt',w)
log.write(subprocess.call(perl_script))
*

The program runs (and outputs stuff on the screen okay), but when I
"cat /tmp/pythonOutput.txt", nothing is there.

(While I'm not waiting for the entire program to run across all the IP
addresses, I would think that something would be go out into that log
file.)
___
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-07 Thread Peter Otten
Rogelio wrote:

> If I want to write this command to a file, would this be the right format?
> 
> *
> import subprocess
> 
> (all my variables defined okay)
> 
> perl_script=subprocess.call(['perl',perl_prog,ipfile,cmd,user,timeout,])
> 
> log=open('/tmp/pythonOutput.txt',w)
> log.write(subprocess.call(perl_script))
> *
> 
> The program runs (and outputs stuff on the screen okay), but when I
> "cat /tmp/pythonOutput.txt", nothing is there.

The documentation is fairly clear:

"""
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. Wait for command to complete, then return 
the returncode attribute.
"""

> (While I'm not waiting for the entire program to run across all the IP
> addresses, I would think that something would be go out into that log
> file.)

No, you'll get an integer return code and ultimately a TypeError when you 
pass that to the file's write() method.

Read the docs on Popen.communicate()

http://docs.python.org/library/subprocess.html#subprocess.Popen.communicate


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


[Tutor] Displaying data in columns

2012-05-07 Thread Thomas C. Hicks
I have some data that comes out of a database as a list of tuples of
integers and strings, one tuple for each row in the ResultProxy from
the select operation. The data looks something like this:

[(56, 12, 8, u'2012-02', 10, 12, u'Guangxi Province', u'Guangxi',
u'10', 8, u'TOT'), (57, 21, 1, u'2012-03', 36, 21,
u'Sichuan EQ Region', u'Sichuan', u'2', 1, u'Basic Medical -
Rural')]

I would like to display some of the items in the tuples in columnar
format with each column lining up on the left side (below should be
three neat columns with location, training type, number trained and
date):

Guangxi ProvinceTOT 10  2012-02
Sichuan EQ Region   Basic Medical - Rural   36  2012-03

Surely there is a python module that helps with that, isn't there?  I
know I could write a function that reads the items, figures string
lengths, adds padding white space and prints out the columns but am
hoping there is a more elegant solution.  I am not against reading
documentation, just can't find the right module to read about.

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


Re: [Tutor] Displaying data in columns

2012-05-07 Thread bob gailer

On 5/7/2012 9:43 AM, Thomas C. Hicks wrote:

I have some data that comes out of a database as a list of tuples of
integers and strings, one tuple for each row in the ResultProxy from
the select operation. The data looks something like this:
Where the data comes from is not relevant. Please don't clutter your 
questions with irrelevant facts.

[(56, 12, 8, u'2012-02', 10, 12, u'Guangxi Province', u'Guangxi',
u'10', 8, u'TOT'), (57, 21, 1, u'2012-03', 36, 21,
u'Sichuan EQ Region', u'Sichuan', u'2', 1, u'Basic Medical -
Rural')]

I would like to display some of the items in the tuples in columnar
format with each column lining up on the left side (below should be
three neat columns with location, training type, number trained and
date):

Guangxi ProvinceTOT 10  2012-02
Sichuan EQ Region   Basic Medical - Rural   36  2012-03

Surely there is a python module that helps with that, isn't there?  I
know I could write a function that reads the items, figures string
lengths, adds padding white space and prints out the columns but am
hoping there is a more elegant solution.  I am not against reading
documentation, just can't find the right module to read about.

the basic tool you'd use is string formatting. I prefer the original %.

"%24s%24s%8s%7s" % 

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] looking for some advice

2012-05-07 Thread Chris Hare
Hello Everyone:

Here is what I am trying to do:

I have a window which has a row of buttons on it.   Below the buttons is a 
label frame.  Depending upon which button they push, I want to change the 
widgets in the label frame.  I can add widgets now with no problem.   

Basically, I am trying to imitate a notebook like in Tkinter.ttk, without 
having to re-write a major chunk of the app to add such a widget.  I know Tix 
has a notebook widget, but that would require people (read end users) to 
download and compile the Tix components.  

My questions are:

1.  how do I remove all of the widgets from the label frame to add the new 
ones?  
2.  Am I better off just creating different label frames for each group of 
widgets and then just using grid_forget and grid_remember to hide or show them 
as I need them?
3.  How else would you approach this problem?

Yes - I guess I am looking for some design advice.  This is creeping a little 
since my customer (my wife who is a horse breeder) would like to have some 
things done a little differently in her app.

Thanks for your suggestions!

Chris

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


Re: [Tutor] Displaying data in columns

2012-05-07 Thread Emile van Sebille

On 5/7/2012 6:43 AM Thomas C. Hicks said...


I would like to display some of the items in the tuples in columnar
format with each column lining up on the left side



I am not against reading
documentation, just can't find the right module to read about.


You'll want to read up on the string interpolation related docs. Start 
with http://docs.python.org/release/2.5.2/lib/typesseq-strings.html or 
the equivalent for your python version.


Emile

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


[Tutor] Curious dictionary printing

2012-05-07 Thread Cranky Frankie
In 3.2.2 in IDLE I have this dictionary entry:

Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "212-222-",\
  "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}

when I print it:

print(Namath)

I get:

{'phone': '212-222-', 'first_name': 'Joe', 'last_name': 'Namath',
'email': 'joe.nam...@gmail.com', 'stadium': 'Shea Stadium'}

Why is it out of order?

-- 
Frank L. "Cranky Frankie" Palmeri
Risible Riding Raconteur & Writer
“The problem with quotes on the Internet is that
it is often difficult to verify their authenticity.”
- Abraham Lincoln
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Curious dictionary printing

2012-05-07 Thread Walter Prins
Hi,

On 7 May 2012 18:16, Cranky Frankie  wrote:

> In 3.2.2 in IDLE I have this dictionary entry:
>
> Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "
> 212-222-",\
>  "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}
>
> Why is it out of order?


Python dictionaries (unlike real physical language dictionaries), are not
ordered, and should be thought of as "an unordered set of *key:
value*pairs"[1]

Walter


[1] http://docs.python.org/tutorial/datastructures.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] looking for some advice

2012-05-07 Thread Peter Otten
Chris Hare wrote:

> Hello Everyone:
> 
> Here is what I am trying to do:
> 
> I have a window which has a row of buttons on it.   Below the buttons is a
> label frame.  Depending upon which button they push, I want to change the
> widgets in the label frame.  I can add widgets now with no problem.
> 
> Basically, I am trying to imitate a notebook like in Tkinter.ttk, without
> having to re-write a major chunk of the app to add such a widget.  I know
> Tix has a notebook widget, but that would require people (read end users)
> to download and compile the Tix components.
> 
> My questions are:
> 
> 1.  how do I remove all of the widgets from the label frame to add the new
> ones?
> 2.  Am I better off just creating different label frames for each group of
> widgets and then just using grid_forget and grid_remember to hide or show
> them as I need them?
> 3.  How else would you approach this problem?
> 
> Yes - I guess I am looking for some design advice.  This is creeping a
> little since my customer (my wife who is a horse breeder) would like to
> have some things done a little differently in her app.
> 
> Thanks for your suggestions!

There is a tabbedpages.TabbedPageSet widget in idlelib. At first glance I 
don't see any dependencies, so maybe you can use that?

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


[Tutor] How can I have type "function" in my script?

2012-05-07 Thread xancorreu

Hi,

I have this script:

from types import *

class Tag:

def __init__(self, nom, tipus, valor):
self.nom = nom
self.tipus = tipus
self.valor = valor

def __str__(self):
return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus)  
+ ", Valor: " + str(self.valor)



def main():
a = Tag("descripció", str, "primera tasca")
b = Tag("unmes", str, lambda x: x+1)
print(a)
print(b)

if __name__ == '__main__':
main()


All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1) 
for b = Tag("unmes", function, lambda x: x+1) I receive an error that 
function is not globally defined variable. How can I say that function 
is the types.function? (the type of lambda x: x+1)


I use python3


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


Re: [Tutor] looking for some advice

2012-05-07 Thread Chris Hare
Thanks Peter - I will give it a look

On May 7, 2012, at 1:02 PM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> Hello Everyone:
>> 
>> Here is what I am trying to do:
>> 
>> I have a window which has a row of buttons on it.   Below the buttons is a
>> label frame.  Depending upon which button they push, I want to change the
>> widgets in the label frame.  I can add widgets now with no problem.
>> 
>> Basically, I am trying to imitate a notebook like in Tkinter.ttk, without
>> having to re-write a major chunk of the app to add such a widget.  I know
>> Tix has a notebook widget, but that would require people (read end users)
>> to download and compile the Tix components.
>> 
>> My questions are:
>> 
>> 1.  how do I remove all of the widgets from the label frame to add the new
>> ones?
>> 2.  Am I better off just creating different label frames for each group of
>> widgets and then just using grid_forget and grid_remember to hide or show
>> them as I need them?
>> 3.  How else would you approach this problem?
>> 
>> Yes - I guess I am looking for some design advice.  This is creeping a
>> little since my customer (my wife who is a horse breeder) would like to
>> have some things done a little differently in her app.
>> 
>> Thanks for your suggestions!
> 
> There is a tabbedpages.TabbedPageSet widget in idlelib. At first glance I 
> don't see any dependencies, so maybe you can use that?
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Glen Zangirolami
Xan, it's "not defined" because you haven't defined a function called
"function" or any variable called "function".

Nom: descripci, Tipus: , Valor: primera tasca
Nom: unmes, Tipus: , Valor:  at 0x10df736e0>

str comes back as  because str is a built-in method and python
and will always be in the namespace.

See http://docs.python.org/library/functions.html#str

On Mon, May 7, 2012 at 1:24 PM, xancorreu  wrote:

> Hi,
>
> I have this script:
>
> from types import *
>
> class Tag:
>
>def __init__(self, nom, tipus, valor):
>self.nom = nom
>self.tipus = tipus
>self.valor = valor
>
>def __str__(self):
>return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus)  +
> ", Valor: " + str(self.valor)
>
>
> def main():
>a = Tag("descripció", str, "primera tasca")
>b = Tag("unmes", str, lambda x: x+1)
>print(a)
>print(b)
>
> if __name__ == '__main__':
>main()
>
>
> All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1) for
> b = Tag("unmes", function, lambda x: x+1) I receive an error that function
> is not globally defined variable. How can I say that function is the
> types.function? (the type of lambda x: x+1)
>
> I use python3
>
>
> Thanks in advance,
> Xan.
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Dave Angel
On 05/07/2012 02:24 PM, xancorreu wrote:
> Hi,
>
> I have this script:
>
> from types import *
>
Bad idea.  Once you do that, you can silently overwrite globals in your
own module with stuff that the current version of types happens to have
in it.  Besides, it then becomes very hard to read your program and
figure out which names you really did want to import.

If you're just getting one or two names, such as in your case, better
just do
import types

> class Tag:
>
> def __init__(self, nom, tipus, valor):
> self.nom = nom
> self.tipus = tipus
> self.valor = valor
>
> def __str__(self):
> return "Nom: " + str(self.nom) + ", Tipus: " +
> str(self.tipus)  + ", Valor: " + str(self.valor)
>
>
> def main():
> a = Tag("descripció", str, "primera tasca")
> b = Tag("unmes", str, lambda x: x+1)
> print(a)
> print(b)
>
> if __name__ == '__main__':
> main()
>
>
> All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1)
> for b = Tag("unmes", function, lambda x: x+1) I receive an error that
> function is not globally defined variable. How can I say that function
> is the types.function? (the type of lambda x: x+1)
>
Where's the stack trace and the exact error message?

types.function is undefined.  The types module does not expose a name
called 'function,' at least not in python 3.2

The type of a lambda is , so it's not clear what you
really want.

Why don't you show the program as you actually run it (perhaps with both
versions of the b= assignment), and the output and stack trace you got. 
Then explain just what you'd hoped to get, as output.


> I use python3
>
>
> Thanks in advance,
> Xan.

-- 

DaveA

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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Joel Goldstick
On Mon, May 7, 2012 at 3:07 PM, Dave Angel  wrote:
> On 05/07/2012 02:24 PM, xancorreu wrote:
>> Hi,
>>
>> I have this script:
>>
>> from types import *
>>
> Bad idea.  Once you do that, you can silently overwrite globals in your
> own module with stuff that the current version of types happens to have
> in it.  Besides, it then becomes very hard to read your program and
> figure out which names you really did want to import.
>
> If you're just getting one or two names, such as in your case, better
> just do
>    import types
>
>> class Tag:

can a class be defined this way in python 3.x?  I thought it needs a
parent class as a parameter?
>>
>>     def __init__(self, nom, tipus, valor):
>>         self.nom = nom
>>         self.tipus = tipus
>>         self.valor = valor
>>
>>     def __str__(self):
>>         return "Nom: " + str(self.nom) + ", Tipus: " +
>> str(self.tipus)  + ", Valor: " + str(self.valor)
>>
>>
>> def main():
>>     a = Tag("descripció", str, "primera tasca")
>>     b = Tag("unmes", str, lambda x: x+1)
>>     print(a)
>>     print(b)
>>
>> if __name__ == '__main__':
>>         main()
>>
>>
>> All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1)
>> for b = Tag("unmes", function, lambda x: x+1) I receive an error that
>> function is not globally defined variable. How can I say that function
>> is the types.function? (the type of lambda x: x+1)
>>
> Where's the stack trace and the exact error message?
>
> types.function is undefined.  The types module does not expose a name
> called 'function,' at least not in python 3.2
>
> The type of a lambda is , so it's not clear what you
> really want.
>
> Why don't you show the program as you actually run it (perhaps with both
> versions of the b= assignment), and the output and stack trace you got.
> Then explain just what you'd hoped to get, as output.
>
>
>> I use python3
>>
>>
>> Thanks in advance,
>> Xan.
>
> --
>
> DaveA
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Dave Angel
On 05/07/2012 03:11 PM, Joel Goldstick wrote:
> On Mon, May 7, 2012 at 3:07 PM, Dave Angel  wrote:
>> On 05/07/2012 02:24 PM, xancorreu wrote:
>>> Hi,
>>>
>>> I have this script:
>>>
>>> from types import *
>>>
>> Bad idea.  Once you do that, you can silently overwrite globals in your
>> own module with stuff that the current version of types happens to have
>> in it.  Besides, it then becomes very hard to read your program and
>> figure out which names you really did want to import.
>>
>> If you're just getting one or two names, such as in your case, better
>> just do
>>import types
>>
>>> class Tag:
> 
> can a class be defined this way in python 3.x?  I thought it needs a
> parent class as a parameter?
>>>

That's legal in 3.x   The difference between 2.x and 3.x is that if you
omit the base class in 2.x, it generates an old-style class, while in
3.x it always uses new-style classes.



-- 

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


Re: [Tutor] Displaying data in columns

2012-05-07 Thread Alan Gauld

On 07/05/12 14:43, Thomas C. Hicks wrote:


I would like to display some of the items in the tuples in columnar
format with each column lining up on the left side (below should be
three neat columns with location, training type, number trained and
date):


You need to read up on string formatting. That allows you to specify the 
length of fields, whether they are left or right justified, any leading 
pad characters etc. This is not in a module but is a standard feature of 
Python.



For anything more exotic you should look to a formatting language
like HTML.


Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] looking for some advice

2012-05-07 Thread Alan Gauld

On 07/05/12 15:19, Chris Hare wrote:


Basically, I am trying to imitate a notebook like in Tkinter.ttk,
I know Tix has a notebook widget, but that would require people

> to download and compile the Tix components.

You must be using a very old version of Python.
Tix has been in the standard library since version 2.3 I think...

The documentation for the Python version is not great but
there is a lot on the Tcl version wjhich is fairly easy
to translate.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread xancorreu

Al 07/05/12 21:07, En/na Dave Angel ha escrit:

On 05/07/2012 02:24 PM, xancorreu wrote:

Hi,

I have this script:

from types import *


Bad idea.  Once you do that, you can silently overwrite globals in your
own module with stuff that the current version of types happens to have
in it.  Besides, it then becomes very hard to read your program and
figure out which names you really did want to import.

If you're just getting one or two names, such as in your case, better
just do
 import types


class Tag:

 def __init__(self, nom, tipus, valor):
 self.nom = nom
 self.tipus = tipus
 self.valor = valor

 def __str__(self):
 return "Nom: " + str(self.nom) + ", Tipus: " +
str(self.tipus)  + ", Valor: " + str(self.valor)


def main():
 a = Tag("descripció", str, "primera tasca")
 b = Tag("unmes", str, lambda x: x+1)
 print(a)
 print(b)

if __name__ == '__main__':
 main()


All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1)
for b = Tag("unmes", function, lambda x: x+1) I receive an error that
function is not globally defined variable. How can I say that function
is the types.function? (the type of lambda x: x+1)


Where's the stack trace and the exact error message?

types.function is undefined.  The types module does not expose a name
called 'function,' at least not in python 3.2

The type of a lambda is, so it's not clear what you
really want.

Why don't you show the program as you actually run it (perhaps with both
versions of the b= assignment), and the output and stack trace you got.
Then explain just what you'd hoped to get, as output.



This is the code:

class Tag:

def __init__(self, nom, tipus, valor):
self.nom = nom
self.tipus = tipus
self.valor = valor

def __str__(self):
return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus)  
+ ", Valor: " + str(self.valor)


class Task:
_nombre = 0

def __init__(self):
self.tags = []
Task._nombre = Task._nombre + 1
self.num = Task._nombre


def __str__(self):
return "Número: " + str(self.num) + ", Tags: " + str(self.tags)

def main():
a = Tag("descripció", str, "primera tasca")
b = Tag("unmes", str, lambda x: x+1)
c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
# en comptes de str ha de ser lambda
print(a)
print(b)
print(b.valor(2))
t = Task()
print("Tasca 1:", t)
t2 = Task()
print("Tasca 2:", t2)

if __name__ == '__main__':
main()


and it fails here:

$ python3 tasques.py
  File "tasques.py", line 26
c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
   ^
SyntaxError: invalid syntax


Really, I want to specify "manually" the type of lambda, but it does not 
work. How to do that?



Thanks,


I use python3


Thanks in advance,
Xan.


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


Re: [Tutor] Curious dictionary printing

2012-05-07 Thread Mark Lawrence

On 07/05/2012 18:16, Cranky Frankie wrote:

In 3.2.2 in IDLE I have this dictionary entry:

Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "212-222-",\
   "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}

when I print it:

print(Namath)

I get:

{'phone': '212-222-', 'first_name': 'Joe', 'last_name': 'Namath',
'email': 'joe.nam...@gmail.com', 'stadium': 'Shea Stadium'}

Why is it out of order?



Cos plain old dicts have no order, but this can be done with 
http://docs.python.org/library/collections.html#ordereddict-objects


--
Cheers.

Mark Lawrence.

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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Dave Angel
On 05/07/2012 04:37 PM, xancorreu wrote:
> Al 07/05/12 21:07, En/na Dave Angel ha escrit:
>> On 05/07/2012 02:24 PM, xancorreu wrote:
>>> Hi,
>>>
>>> I have this script:
>>>
>>> from types import *
>>>
>> Bad idea.  Once you do that, you can silently overwrite globals in your
>> own module with stuff that the current version of types happens to have
>> in it.  Besides, it then becomes very hard to read your program and
>> figure out which names you really did want to import.
>>
>> If you're just getting one or two names, such as in your case, better
>> just do
>>  import types
>>
>>> class Tag:
>>>
>>>  def __init__(self, nom, tipus, valor):
>>>  self.nom = nom
>>>  self.tipus = tipus
>>>  self.valor = valor
>>>
>>>  def __str__(self):
>>>  return "Nom: " + str(self.nom) + ", Tipus: " +
>>> str(self.tipus)  + ", Valor: " + str(self.valor)
>>>
>>>
>>> def main():
>>>  a = Tag("descripció", str, "primera tasca")
>>>  b = Tag("unmes", str, lambda x: x+1)
>>>  print(a)
>>>  print(b)
>>>
>>> if __name__ == '__main__':
>>>  main()
>>>
>>>
>>> All is ok, but when I substitute b = Tag("unmes", str, lambda x: x+1)
>>> for b = Tag("unmes", function, lambda x: x+1) I receive an error that
>>> function is not globally defined variable. How can I say that function
>>> is the types.function? (the type of lambda x: x+1)
>>>
>> Where's the stack trace and the exact error message?
>>
>> types.function is undefined.  The types module does not expose a name
>> called 'function,' at least not in python 3.2
>>
>> The type of a lambda is, so it's not clear what you
>> really want.
>>
>> Why don't you show the program as you actually run it (perhaps with both
>> versions of the b= assignment), and the output and stack trace you got.
>> Then explain just what you'd hoped to get, as output.
>>
> 
> This is the code:
> 
> class Tag:
> 
> def __init__(self, nom, tipus, valor):
> self.nom = nom
> self.tipus = tipus
> self.valor = valor
> 
> def __str__(self):
> return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus) 
> + ", Valor: " + str(self.valor)
> 
> class Task:
> _nombre = 0
> 
> def __init__(self):
> self.tags = []
> Task._nombre = Task._nombre + 1
> self.num = Task._nombre
> 
> 
> def __str__(self):
> return "Número: " + str(self.num) + ", Tags: " + str(self.tags)
> 
> def main():
> a = Tag("descripció", str, "primera tasca")
> b = Tag("unmes", str, lambda x: x+1)
> c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
> # en comptes de str ha de ser lambda
> print(a)
> print(b)
> print(b.valor(2))
> t = Task()
> print("Tasca 1:", t)
> t2 = Task()
> print("Tasca 2:", t2)
> 
> if __name__ == '__main__':
> main()
> 
> 
> and it fails here:
> 
> $ python3 tasques.py
>   File "tasques.py", line 26
> c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
>^
> SyntaxError: invalid syntax
> 
> 
> Really, I want to specify "manually" the type of lambda, but it does not
> work. How to do that?
> 
> 
> Thanks,
> 
>>> I use python3
>>>
>>>
>>> Thanks in advance,
>>> Xan.
> 
> 

def main():
a = Tag("descripció", str, "primera tasca")
print (a)
b = Tag("unmes", str, lambda x: x+1)
print(b)
c = Tag("unmes", "function", lambda x: x+1)
print(c)

Does that help?

-- 

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


Re: [Tutor] How can I have type "function" in my script?

2012-05-07 Thread Alan Gauld

On 07/05/12 21:37, xancorreu wrote:


This is the code:


OK, But it's not clear from that why you want the type.
You are not doing anything meaningful with the type.


class Tag:

def __init__(self, nom, tipus, valor):
self.nom = nom
self.tipus = tipus
self.valor = valor

def __str__(self):
return "Nom: " + str(self.nom) + ", Tipus: " + str(self.tipus) + ",
Valor: " + str(self.valor)



You store it then print the string version of it.
Why not just pass a string name?


def main():
a = Tag("descripció", str, "primera tasca")
b = Tag("unmes", str, lambda x: x+1)
c = Tag("twice", type(lambda: x: x), lambda x: 2*x)



and it fails here:

$ python3 tasques.py
File "tasques.py", line 26
c = Tag("twice", type(lambda: x: x), lambda x: 2*x)
^
SyntaxError: invalid syntax



As it says there is a syntax error. Look at your two lambda expressions, 
the second one has an extra :


If you really want the type of a function just use one of the
built in functions... or a very simple lambda:

>>> type(pow)

>>> type(lambda : 0)

>>>

But in most cases you don't need the type and the callable() function is 
more useful.


>>> callable(pow)
True
>>> callable(lambda : 0)
True
>>> callable(7)
False
>>>


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Console Application - Key Events

2012-05-07 Thread BRAGA, Bruno
Hi tutors,

I would like to know if there is any "easy" way to handle events (such as
mouse movements, keyboard keys pressed, etc) in console based python
applications?

More specifically, I am working on a screensaver for terminals (
http://termsaver.info), so I would like to simulate the same behaviour of a
standard screensaver for the X windows, by:

   - running on background
   - starting some functionality (display a text, etc) if there is no event
   (mouse or keyboard) for more than N minutes
   - stopping the above if there is any movement detected

Any thoughts on this would be highly appreciated.

Thanks!

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Curious dictionary printing

2012-05-07 Thread bob gailer

On 5/7/2012 1:16 PM, Cranky Frankie wrote:

In 3.2.2 in IDLE I have this dictionary entry:

Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "212-222-",\
   "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}

when I print it:

print(Namath)

I get:

{'phone': '212-222-', 'first_name': 'Joe', 'last_name': 'Namath',
'email': 'joe.nam...@gmail.com', 'stadium': 'Shea Stadium'}

Why is it out of order?


May I recommend a different approach to such questions.

When you get an unexpected (undesired)  result try saying - "Oh I see - 
that's how Python does it!"


I want something different. How can I get it?

Then try reading the documentation.

Asking why does it not do what I want is not IMHO the best way to win 
friends here.


Taking this steps further
 - what does it mean to be "in order". To some it is the order in which 
items are added rather than some collating sequence.

 - Do you want order by key or by value?
 - how would you order {1 : 'cat', "a": 3, (2,3): True}?

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


[Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)

2012-05-07 Thread Rogelio
I am wrapping a Python wrapper script to "wc -l" (count lines) of a
list of IP addresses

***

import subprocess
IPcount = subprocess.call(['wc -l file.txt | awk \'{print $1}\''], shell=True)
print "You have",IPcount,"IP addresses that are alive."

***

I get the following output

***
46
You have 0 IP addresses that are alive.
***

Why does IPcount not equal 46?  Is this what the stout is for?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)

2012-05-07 Thread Rogelio
On Mon, May 7, 2012 at 8:09 PM, Rogelio  wrote:

> Why does IPcount not equal 46?  Is this what the stout is for?

FWIW, I think this seems to fix it and make IPcount an integer.

import os
IPcount = os.popen("wc -l file.txt | awk '{print $1}'").read()
print "You have",IPcount,"IP addresses that are alive."
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)

2012-05-07 Thread Kushal Kumaran
On Tue, May 8, 2012 at 8:39 AM, Rogelio  wrote:
> I am wrapping a Python wrapper script to "wc -l" (count lines) of a
> list of IP addresses
>
> ***
>
> import subprocess
> IPcount = subprocess.call(['wc -l file.txt | awk \'{print $1}\''], shell=True)
> print "You have",IPcount,"IP addresses that are alive."
>
> ***
>
> I get the following output
>
> ***
> 46
> You have 0 IP addresses that are alive.
> ***
>
> Why does IPcount not equal 46?  Is this what the stout is for?

You can do this:
http://docs.python.org/py3k/library/subprocess.html#replacing-bin-sh-shell-backquote

However, for this simple action of counting lines in a file, I
recommend you do it directly in python:

def count_lines(filename):
with open(f, 'r') as in_stream:
return len(in_stream.readlines())

The count_lines function takes a filename and returns the number of
lines in that file.  This way you avoid the problems with shell
metacharacters when using shell=True, mentioned in the subprocess
documentation: 
http://docs.python.org/py3k/library/subprocess.html#frequently-used-arguments

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


Re: [Tutor] IPcount = 0 when used with subprocess.call (use something to convert to integer?)

2012-05-07 Thread Steven D'Aprano
On Mon, May 07, 2012 at 08:09:55PM -0700, Rogelio wrote:

> import subprocess
> IPcount = subprocess.call(['wc -l file.txt | awk \'{print $1}\''], shell=True)
> print "You have",IPcount,"IP addresses that are alive."
> 
> I get the following output
>
> 46
> You have 0 IP addresses that are alive.
> 
> Why does IPcount not equal 46?  Is this what the stout is for?

Yes. The output of the call gets written to stdout. The return result of 
the call is 0 if the call succeeded and some other integer if it failed.


-- 
Steven

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


[Tutor] summary stats grouped by month year

2012-05-07 Thread questions anon
I would like to calculate summary statistics of rainfall based on year and
month.
I have the data in a text file (although could put in any format if it
helps) extending over approx 40 years:
YEAR MONTHMeanRain
1972 Jan12.7083199
1972 Feb14.17007142
1972 Mar14.5659302
1972 Apr1.508517302
1972 May2.780009889
1972 Jun1.609619287
1972 Jul0.138150181
1972 Aug0.214346148
1972 Sep1.322102228

I would like to be able to calculate the total rain annually:

YEAR   Annualrainfall
1972400
1973300
1974350

2011 400

and also the monthly mean rainfall for all years:

YEAR  MonthlyMeanRain
Jan  13
Feb  15
Mar   8
.
Dec   13


Is this something I can easily do?
I have started by simply importing the text file but data is not
represented as time so that is probably my first problem and then I am not
sure how to group them by month/year.

textfile=r"textfile.txt"
f=np.genfromtxt(textfile,skip_header=1)

Any feedback will be greatly appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] summary stats grouped by month year

2012-05-07 Thread Andre' Walker-Loud
Hello anonymous questioner,

first comment - you may want to look into hdf5 data structures

http://www.hdfgroup.org/HDF5/

and the python tools to play with them

pytables - http://www.pytables.org/moin
h5py - http://code.google.com/p/h5py/

I have personally used pytables more - but not for any good reason.  If you 
happen to have the Enthought python distribution - these come with the package, 
as well as an installation of hdf5

hdf5 is a very nice file format for storing large amounts of data (binary) with 
descriptive meta-data.  Also, numpy plays very nice with hdf5.  Given all your 
questions here, I suspect you would benefit from learning about these and 
learning to play with them.

Now to your specific question.

> I would like to calculate summary statistics of rainfall based on year and 
> month.
> I have the data in a text file (although could put in any format if it helps) 
> extending over approx 40 years:
> YEAR MONTHMeanRain
> 1972 Jan12.7083199
> 1972 Feb14.17007142
> 1972 Mar14.5659302
> 1972 Apr1.508517302
> 1972 May2.780009889
> 1972 Jun1.609619287
> 1972 Jul0.138150181
> 1972 Aug0.214346148
> 1972 Sep1.322102228
> 
> I would like to be able to calculate the total rain annually:
> 
> YEAR   Annualrainfall
> 1972400
> 1973300
> 1974350
> 
> 2011 400
> 
> and also the monthly mean rainfall for all years:
> 
> YEAR  MonthlyMeanRain
> Jan  13
> Feb  15
> Mar   8
> .
> Dec   13
> 
> 
> Is this something I can easily do?

Yes - this should be very easy.  Imagine importing all this data into a numpy 
array

===
import numpy as np

data = open(your_data).readlines()
years = []
for line in data:
if line.split()[0] not in years:
years.append(line.split()[0])
months = ['Jan','Feb',,'Dec']

rain_fall = np.zeros([len(n_year),len(months)])
for y,year in enumerate(years):
for m,month in enumerate(months):
rain_fall[y,m] = float(data[ y * 12 + m].split()[2])

# to get average per year - average over months - axis=1
print np.mean(rain_fall,axis=1)

# to get average per month - average over years - axis=0
print np.mean(rain_fall,axis=0)

===

now you should imagine doing this by setting up dictionaries, so that you can 
request an average for year 1972 or for month March.  That is why I used the 
enumerate function before to walk the indices - so that you can imagine 
building the dictionary simultaneously.

years = {'1972':0, '1973':1, }
months = {'Jan':0,'Feb':1,...'Dec':11}

then you can access and store the data to the array using these dictionaries.

print rain_fall[int('%(1984)s' % years), int('%(March)s' % months)]


Andre





> I have started by simply importing the text file but data is not represented 
> as time so that is probably my first problem and then I am not sure how to 
> group them by month/year. 
> 
> textfile=r"textfile.txt"
> f=np.genfromtxt(textfile,skip_header=1)
> 
> Any feedback will be greatly appreciated.
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Curious dictionary printing

2012-05-07 Thread BRAGA, Bruno
Put it simple, dictionaries do not sort. You can use the dict.keys() to get
a list of the dictionary keys, then sort them... there are lots of talks on
this, just google a bit, and you will find fancy ways to do key or value
sorting.

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com


On Tue, May 8, 2012 at 12:31 PM, bob gailer  wrote:

> On 5/7/2012 1:16 PM, Cranky Frankie wrote:
>
>> In 3.2.2 in IDLE I have this dictionary entry:
>>
>> Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "
>> 212-222-",\
>>   "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}
>>
>> when I print it:
>>
>> print(Namath)
>>
>> I get:
>>
>> {'phone': '212-222-', 'first_name': 'Joe', 'last_name': 'Namath',
>> 'email': 'joe.nam...@gmail.com', 'stadium': 'Shea Stadium'}
>>
>> Why is it out of order?
>>
>
> May I recommend a different approach to such questions.
>
> When you get an unexpected (undesired)  result try saying - "Oh I see -
> that's how Python does it!"
>
> I want something different. How can I get it?
>
> Then try reading the documentation.
>
> Asking why does it not do what I want is not IMHO the best way to win
> friends here.
>
> Taking this steps further
>  - what does it mean to be "in order". To some it is the order in which
> items are added rather than some collating sequence.
>  - Do you want order by key or by value?
>  - how would you order {1 : 'cat', "a": 3, (2,3): True}?
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor