Re: [Tutor] failing to learn python

2006-04-11 Thread Alan Gauld
>> What kind of real life problems are you interested in? You might like
>
> I am a parttime sys admin so I want system admin problem which usually I
> do through shell scripts like parsing logs, generating reports, greping
> with regexes etc.

My tutor is specifdically targetted at computer power users invcluding
sys admins. It has examples of parsing files and creating reports (albeit
very simple ones since its for beginners) and using regex. It also has
side bars on topics like using WSH on Windows and other OS specific
topics.

There are also specific topics on using the OS and IPC comms etc being
written.

> The only thing I don't want is silly problems like generate fibonnacci
> series, add numbers frm 0-n etc. non required silly stuff.

None of the above are silly stuff, they are all needed in various real
world computing and scientific problems. However I do agree that
they are not what most beginners will be doing and I try to avoid
any math type exercises. (I do use factorials as an example of
recursion alongside another more practical example)

I don't think my tutor was in your list so you might like to give it a go...

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Alan Gauld
> My problem, and this is after reading PEP 318 and other items found when I
> "Googled" for decorators, is that I can't figure out the practical use for

There is no practical use for decorators IMHO
They are syntactic sugar added to the language to make some things
that were already possible a little tidier looking (things like class/static
 methods etc)

I don;t think there is anything that you need decorators to achieve that
you can'tdo without them. In that respect they are like python's support
for lambdas - nice to have but not essential to the language.

Others may feel differently! :-)

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tuple

2006-04-11 Thread Kaushal Shriyan
Hi All

I am referring to http://www.ibiblio.org/obp/thinkCSpy/chap09.htm

>>> tuple = ('a', 'b', 'c', 'd', 'e')
>>> tuple[0]
'a'


And the slice operator selects a range of elements.

>>> tuple[1:3]
('b', 'c')


But if we try to modify one of the elements of the tuple, we get a error:

>>> tuple[0] = 'A'
TypeError: object doesn't support item assignment


Of course, even if we can't modify the elements of a tuple, we can
replace it with a different tuple:

>>> tuple = ('A',) + tuple[1:]
>>> tuple
('A', 'b', 'c', 'd', 'e')

How does tuple = ('A',) + tuple[1:]  this work 

Please explain me with an example

Thanks

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple

2006-04-11 Thread Noufal Ibrahim

On Tue, April 11, 2006 2:49 pm, Kaushal Shriyan wrote:
> Hi All
>
> I am referring to http://www.ibiblio.org/obp/thinkCSpy/chap09.htm
>
 tuple = ('a', 'b', 'c', 'd', 'e')
 tuple[0]
> 'a'
>
>
> And the slice operator selects a range of elements.
>
 tuple[1:3]
> ('b', 'c')
>
>
> But if we try to modify one of the elements of the tuple, we get a error:
>
 tuple[0] = 'A'
> TypeError: object doesn't support item assignment
>
>
> Of course, even if we can't modify the elements of a tuple, we can
> replace it with a different tuple:
>
 tuple = ('A',) + tuple[1:]
 tuple
> ('A', 'b', 'c', 'd', 'e')
>
> How does tuple = ('A',) + tuple[1:]  this work 

One question mark is enough. ;)

('A',) creates a tuple with a single element. The comma at the end is to
differentiate between a tuple and just grouping brackets.
tuple[1:] returns all elements of the tuple except the first.
So what do you have?
A tuple ('A') and another tuple ('b', 'c', 'd', 'e').

Now, the + operator concatenates these two into a new tuple. What do you get?
('A','b','c','d','e').

This is returned by the expression on the right hand side. And it's
assigned to the variable "tuple". When you print it, you get the value.

I think you're getting confused between changing a tuple itself and
creating a new one with pieces of others.

On a side note, it's not a good idea to call a variable "tuple" since
there is a python builtin by the same name.
-- 
-NI

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple

2006-04-11 Thread Kaushal Shriyan
On 4/11/06, Noufal Ibrahim <[EMAIL PROTECTED]> wrote:
>
> On Tue, April 11, 2006 2:49 pm, Kaushal Shriyan wrote:
> > Hi All
> >
> > I am referring to http://www.ibiblio.org/obp/thinkCSpy/chap09.htm
> >
>  tuple = ('a', 'b', 'c', 'd', 'e')
>  tuple[0]
> > 'a'
> >
> >
> > And the slice operator selects a range of elements.
> >
>  tuple[1:3]
> > ('b', 'c')
> >
> >
> > But if we try to modify one of the elements of the tuple, we get a error:
> >
>  tuple[0] = 'A'
> > TypeError: object doesn't support item assignment
> >
> >
> > Of course, even if we can't modify the elements of a tuple, we can
> > replace it with a different tuple:
> >
>  tuple = ('A',) + tuple[1:]
>  tuple
> > ('A', 'b', 'c', 'd', 'e')
> >
> > How does tuple = ('A',) + tuple[1:]  this work 
>
> One question mark is enough. ;)
>
> ('A',) creates a tuple with a single element. The comma at the end is to
> differentiate between a tuple and just grouping brackets.
> tuple[1:] returns all elements of the tuple except the first.
> So what do you have?
> A tuple ('A') and another tuple ('b', 'c', 'd', 'e').
>
> Now, the + operator concatenates these two into a new tuple. What do you get?
> ('A','b','c','d','e').
>
> This is returned by the expression on the right hand side. And it's
> assigned to the variable "tuple". When you print it, you get the value.
>
> I think you're getting confused between changing a tuple itself and
> creating a new one with pieces of others.
>
> On a side note, it's not a good idea to call a variable "tuple" since
> there is a python builtin by the same name.
> --
> -NI
>
>

Thanks Noufal for the explanation

Appreciate it

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] failing to learn python

2006-04-11 Thread Ed Singleton
On 10/04/06, Payal Rathod <[EMAIL PROTECTED]> wrote:
> Hi,
> I am trying to learn Python seriously for almost 2 months but have not
> gotten far at all. Infact, it seems I have not understood even the basic
> concepts itself. I know some shell, sed and awk programming.
> I have tried reading Learning Python - Mark Lutz
> Think C Spy
> A byte of Python
> Non-Programmers Tutorial For Python
> etc.
> But I have not got anything from them. I am feeling that they are
> superficial and do not touch real life problems. Also, not enough
> examples are provided which can help newbies like me.
>
> Can anyone help me, it is pretty pretty frustating thing for last couple
> of months?
>
> With warm regards,
> -Payal

You might find reading real code to be more useful than a tutorial:

Fredrik Lundh's Guide to the Standard Library contains thousands of examples:
http://effbot.org/zone/librarybook-index.htm

PLEAC contains loads of examples to show how to do things:
http://pleac.sourceforge.net/pleac_python/index.html

It all depends what kind of learner you are:
Learn by being taught (read the tutorials)
Learn by doing (think of a project, start trying to do it, then ask
here when you get stuck)
Learn by example (read lots of examples of other people's code to see
how they do it)

I'm very much the kind of person who likes to learn to swim by jumping
as far into the water as he can, then trying to get back to the side. 
It's amazing how well you can do something when you don't have any
choice. ;-)

Ed
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Kent Johnson
Alan Gauld wrote:
>> My problem, and this is after reading PEP 318 and other items found when I
>> "Googled" for decorators, is that I can't figure out the practical use for
> 
> There is no practical use for decorators IMHO
> They are syntactic sugar added to the language to make some things
> that were already possible a little tidier looking (things like class/static
>  methods etc)

It's true that decorators are syntactic sugar and don't add any new 
functional capabilities to the language.

@foo
def bar():
   pass

is equivalent to

def bar():
   pass
bar = foo(bar)

However this doesn't mean that there is no practical use for decorators. 
After all, list comprehension is mostly syntactic sugar too! Decorators 
add expressiveness and clarity to Python rather than functionality. If 
you read the PEP you see this is the primary motivation.

Decorators are used widely so apparently many people agree that they add 
to the expressiveness of the language.

Here is another example - decorators are used in the implementation of a 
proposal for dynamic function overloading:
http://www.artima.com/weblogs/viewpost.jsp?thread=155514

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Emailid

2006-04-11 Thread Kaushal Shriyan
Hi All

I am a ardent fan of python can I have email address for me
I mean For example for me it would be

[EMAIL PROTECTED]

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tuple (Section 9.3)

2006-04-11 Thread Kaushal Shriyan
Hi All

I am reading this http://www.ibiblio.org/obp/thinkCSpy/chap09.htm and
did not understood the Section 9.3 at all, Please explain me with an
example so the idea become clear and understood



9.3 Tuples as return values

Functions can return tuples as return values. For example, we could
write a function that swaps two parameters:

def swap(x, y):
  return y, x


Then we can assign the return value to a tuple with two variables:

a, b = swap(a, b)


In this case, there is no great advantage in making swap a function.
In fact, there is a danger in trying to encapsulate swap, which is the
following tempting mistake:

def swap(x, y):  # incorrect version
  x, y = y, x


If we call this function like this:

swap(a, b)


then a and x are aliases for the same value. Changing xinside swap
makes x refer to a different value, but it has no effect on a in
__main__. Similarly, changing y has no effect on b.

This function runs without producing an error message, but it doesn't
do what we intended. This is an example of a semantic error.

As an exercise, draw a state diagram for this function so that you can
see why it doesn't work.



Thanks in Advance

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] input with default value option

2006-04-11 Thread Alan Gauld
>> With raw_input(), it allows to input value. Can it be used to input value 
>> with default value option?
>>
> response = raw_input("Enter some data:")
> if not response: response = "default value"

This is one of the few places where I do use the short-circuit evaluation 
trick:

val = raw_input('> ') or myDefault

I reads well (IMHO) and works pretty much safely.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple

2006-04-11 Thread Alan Gauld
>>> tuple = ('A',) + tuple[1:]
>>> tuple
>('A', 'b', 'c', 'd', 'e')
>
> How does tuple = ('A',) + tuple[1:]  this work 
>
> Please explain me with an example

OK, we will use the example you gave us!

tuple is a bad name since Python has an internal type 
called tuple usd for converting a list etc to a tuple so 
I'll use t1:

>>> t1 = ('a','b','c','d','e')
>>> t2 = ('A',) + t1[1:]

We construct a new tuple by creating a single element 
tuple ('A',) and adding to it the contents of the original 
tuple starting with the second element (t1[1:]). We can 
add tuples together quite easily:

>>> t3 = (1,2,3) + (4,5,6)
>>> t3
(1,2,3,4,5,6)

The result is a new tuple. The only difference in your 
example is that instead of using a new name (t2) you 
replaced the original tuple with the new one in t1. This 
is just the same as when you do

x = x +1

Now which specific bit of that did you not understand
and we can expand on it if necessary?

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] comp.lang.python newsgroup

2006-04-11 Thread Kaushal Shriyan
Hi All

I went to a http://www.python.org/community/lists.html and found the
below newsgroup

How do i use this news group and access the information I need

comp.lang.python newsgroup

Thanks in Advance

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python newsgroup

2006-04-11 Thread Kent Johnson
Kaushal Shriyan wrote:
> Hi All
> 
> I went to a http://www.python.org/community/lists.html and found the
> below newsgroup
> 
> How do i use this news group and access the information I need
> 
> comp.lang.python newsgroup

You can use Google groups
http://groups.google.com/groups?group=comp.lang.python

You can subscribe to the email version:
http://www.python.org/mailman/listinfo/python-list

Both of these and a few more options are listed in the web page you 
referenced!

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Emailid

2006-04-11 Thread Kent Johnson
Kaushal Shriyan wrote:
> Hi All
> 
> I am a ardent fan of python can I have email address for me
> I mean For example for me it would be
> 
> [EMAIL PROTECTED]

I don't know how you get a python.org mailing address but I'm sure it's 
not by asking here!

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Emailid

2006-04-11 Thread Ed Singleton
On 11/04/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Kaushal Shriyan wrote:
> > Hi All
> >
> > I am a ardent fan of python can I have email address for me
> > I mean For example for me it would be
> >
> > [EMAIL PROTECTED]
>
> I don't know how you get a python.org mailing address but I'm sure it's
> not by asking here!

Actually, I can probably help.  If you give your bank details to my
friend in Nigeria, he can get you one.

Ed
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Scan Codes, Character Sets: Any Modules?

2006-04-11 Thread doug shawhan

The difficulty I have may or may not be something that may be easily
handled with pyserial and some other mystery module.

 I am attempting to screen scrape SuperDOS, an
extremely closed system that uses wyse 60 terminals to communicate with
a dos machine. I have not been able to communicate properly with
superdos until trying the handy  miniterm.py example from the pyserial package in
conjunction with Markus Gutschke's wy60 emulator which translates input through an xterm into wyse 60 commands. These programs
together allow me to interact with superdos pretty well ... with
the exception of the arrow keys: for those I must fall back to the old
cntl-letter combos to get the cursor to behave  (actually cntl-letter x 2.. for some reason it likes an extra
prod). This is fine, as now I have a way to
debug my eventual script. My big problem is,  I am completely unable to get SuperDos
to respond to my carriage returns from within the script!

I can watch the script work via miniterm.py. I have sent the return and
newline characters in various combinations starting with "\n,\r",
"\x0a\x0d", but they respond weirdly, putting the cursor *above* the
existing command line, changing the cursor to an outline and generally
letting me know that I am on the wrong track. 

Has some clever human alread created a handy module to handle wyse60
and other terminal emulation from within a python program? I have
looked over the curses module, but it seems to be aimed at drawing
proper screens for the user, not translation. PySerial's ability to
suck up the output via readlines() is awesome, and I can see how I
*think* it should all be done, but the weirdness has got me stymied! I
am going to look at Mr. Gutscheke's source to see how he does it, but I
am barely conversant in python and fear that exposure to that much C
code may cause dizziness!

Thanks!


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] unpack/regexp

2006-04-11 Thread Paul Kraus
Ok sorry for the perl refernce but I can't figure out how to do this.
I have a fixed width text file i need to parse.

so lets say I want an array to containt the pieces i need.
if the fields I want are lengths from left to right.
10 10 13
12345678901234567890123456789012
I want to turn this into an array that has these elements.
1234567890
1234567890
123456789012 <--notice white space

In Perl its a simple
my @array = unpack ( "A10 A10 A13" , $line )
this extracts it and removes the whitespace after doing so.

or if i wanted i could do
my @array = ( $1, $2, $3 ) if ( $line =~ m/^(.{10})(.{10})(.{13}) )


-- 
Paul Kraus
=-=-=-=-=-=-=-=-=-=-=
PEL Supply Company
Network Administrator
216.267.5775 Voice
216.267.6176 Fax
www.pelsupply.com
=-=-=-=-=-=-=-=-=-=-=
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Database Connectivity

2006-04-11 Thread Kaushal Shriyan
Hi ALL

How do i connect my python program to MySQL DB or Oracle DB or can you
please specify the URL which gives a detailed explanation on this.

Thanks in Advance

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database Connectivity

2006-04-11 Thread Ed Singleton
On 11/04/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote:
> Hi ALL
>
> How do i connect my python program to MySQL DB or Oracle DB or can you
> please specify the URL which gives a detailed explanation on this.

SQLObject is your best bet:
http://www.sqlobject.org/

If you're using MySQL, you will need MySQLdb as well:
http://sourceforge.net/projects/mysql-python

Any questions, just ask.

Ed
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unpack/regexp

2006-04-11 Thread Kent Johnson
Paul Kraus wrote:
> Ok sorry for the perl refernce but I can't figure out how to do this.
> I have a fixed width text file i need to parse.
> 
> so lets say I want an array to containt the pieces i need.
> if the fields I want are lengths from left to right.
> 10 10 13
> 12345678901234567890123456789012
> I want to turn this into an array that has these elements.
> 1234567890
> 1234567890
> 123456789012 <--notice white space
> 
> In Perl its a simple
> my @array = unpack ( "A10 A10 A13" , $line )
> this extracts it and removes the whitespace after doing so.

struct.unpack() is a direct analog:

In [10]: line = "12345678901234567890123456789012 "

In [16]: struct.unpack('10s10s13s', line)
Out[16]: ('1234567890', '1234567890', '123456789012 ')

You can also use string slicing:

In [14]: line[:10], line[10:20], line[20:]
Out[14]: ('1234567890', '1234567890', '123456789012 ')

> 
> or if i wanted i could do
> my @array = ( $1, $2, $3 ) if ( $line =~ m/^(.{10})(.{10})(.{13}) )

Python regex is a bit more verbose than Perl but you can do the same thing:

In [2]: import re

In [11]: m=re.match("(.{10})(.{10})(.{13})", line)

In [13]: m.group(1, 2, 3)
Out[13]: ('1234567890', '1234567890', '123456789012 ')

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database Connectivity

2006-04-11 Thread Kent Johnson
Kaushal Shriyan wrote:
> Hi ALL
> 
> How do i connect my python program to MySQL DB or Oracle DB or can you
> please specify the URL which gives a detailed explanation on this.

Basic connectivity is through modules that implement the DB-API 
standard. Read the spec and find implementations here:
http://www.python.org/doc/topics/database/

There are several popular packages that layer on top of this basic 
functionality such as SQLObject and SQLAlchemy.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unpack/regexp

2006-04-11 Thread Paul Kraus
> Python regex is a bit more verbose than Perl but you can do the same thing:
>
> In [2]: import re
>
> In [11]: m=re.match("(.{10})(.{10})(.{13})", line)
>
> In [13]: m.group(1, 2, 3)
> Out[13]: ('1234567890', '1234567890', '123456789012 ')

That work great. Regex tend to be "expensive" is there a "better" way to do 
it?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] unpack/regexp

2006-04-11 Thread doug shawhan
I always slice the string  in this sort of situation:

s = "12345678901234567890123456789012 "

t = s[:10],s[10:20],s[20:-1]
    
print t

('1234567890', '1234567890', '123456789012')

One could always bracket it to make a list or whatever. 

Hope this helps!

On 4/11/06, Paul Kraus <[EMAIL PROTECTED]> wrote:
Ok sorry for the perl refernce but I can't figure out how to do this.I have a fixed width text file i need to parse.so lets say I want an array to containt the pieces i need.if the fields I want are lengths from left to right.
10 10 1312345678901234567890123456789012I want to turn this into an array that has these elements.12345678901234567890123456789012 <--notice white spaceIn Perl its a simplemy @array = unpack ( "A10 A10 A13" , $line )
this extracts it and removes the whitespace after doing so.or if i wanted i could domy @array = ( $1, $2, $3 ) if ( $line =~ m/^(.{10})(.{10})(.{13}) )--Paul Kraus=-=-=-=-=-=-=-=-=-=-=
PEL Supply CompanyNetwork Administrator216.267.5775 Voice216.267.6176 Faxwww.pelsupply.com=-=-=-=-=-=-=-=-=-=-=___
Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] failing to learn python

2006-04-11 Thread Payal Rathod
On Mon, Apr 10, 2006 at 04:20:37PM -0700, Danny Yoo wrote:
> http://gnosis.cx/TPiP/

I will read that and Alan's tutorial too (isn't that MS-Windows specific 
???)

The reason I am disgrunted with Python is because lack of good 
documentation. Shell programming has great text and so do sed and awk 
but Python texts are rather boring and just hooting that Python is cool 
without proving it at all. All the examples I wanted were done better in 
shell/sed/awk.  But anyways thanks for the support I will dive into it 
again with fresh vigour. I will get back with any stupid queries I have.

With warm regards,
-Payal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] for loops

2006-04-11 Thread josip
  I have problem with this question.  Can someone show me the code and than explain it?     >>Write a Python program to print out the following  shape. You are expected to use two for loops (these must be nested) to solve this problem.     output:  * * * * *   *  *  *  *  * * * * *     Thanks
		Love cheap thrills? Enjoy PC-to-Phone  calls to 30+ countries for just 2¢/min with Yahoo! Messenger with Voice.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple (Section 9.3)

2006-04-11 Thread Hugo González Monteverde
Hi Kaushal,

I might have to do a little guessing and see what is not clear from the 
explanation.

The whole point of returning a tuple as opposed to, say, returning a 
list, is the fact that  tuples are NON mutable. That is, *apparently* 
you would not be returning a reference, but the values themselves.

There is no better example I can think of, than the one you already 
read. If you want a function tu return multiple parameters so that you 
can do.

ret1, ret2 = func()

then have func return a tuple. That's the whole point of the chapter you 
read.

Maybe there is too much information in the discussion that does not look 
useful to you if you have not run into a problem where you need it.

I hope this is not some kind of homework :)

Hugo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Webbrowser

2006-04-11 Thread Øyvind
Hello.

I have recently discovered the Webbrowser module, and have started using
it instead of just using IE thru win32com. But, have a few questions:

1) Is it possible to give it an adress, and if the browser gets a redirect
to a different adress, I can override it? For example, I tell the browser
to open a site. The browser tries, but gets redirected to a spam-site. I
would therefore like to send the user somewhere else. Is it possible?

2) It doesn't seem like open(url[, new=0][, autoraise=1]) new and
autoraise work with Firefox. I have tried all possible comboes, but the
site is opened in a new tab in Firefox regardless. Any suggestions?

Thanks in advance,
Øyvind


-- 
This email has been scanned for viruses & spam by Decna as - www.decna.no
Denne e-posten er sjekket for virus & spam av Decna as - www.decna.no

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scan Codes, Character Sets: Any Modules?

2006-04-11 Thread Alan Gauld
Hi Doug,

>  I am attempting to screen scrape SuperDOS, uses wyse 60 terminals
> I must fall back to the old cntl-letter combos to get the cursor to behave

Sounds like you need to find a tech spec for the Wyse 60. That should
give you all of the Ctrl character combos to control the screen.
If you have access to a Unix box you might find the info you need
in the termcap or terminfo databases. But Wyse should have the
spec sheet on their web site somewhere...

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Alan Gauld
>> There is no practical use for decorators IMHO
> It's true that decorators are syntactic sugar and don't add any new 
> functional capabilities to the language.

Which is all I intended to imply.

> However this doesn't mean that there is no practical use for decorators. 

Nor did I mean that, after all I am a big advocate of lambdas and 
they too are sugar. They have no practical use but are nice for 
conveying certain concepts in code. Similarly with decorators
(although I do hate the syntax!)

> After all, list comprehension is mostly syntactic sugar too! 

Absolutely and also add no practical functionality they are just 
shorthand, albeit with a small performance advantage in many 
cases - which is not true of decorators or lambdas!

> proposal for dynamic function overloading:
> http://www.artima.com/weblogs/viewpost.jsp?thread=155514

I really don't like this proposal nor many of the others that currently 
exist to seemingly turn Python into Java and C++! If people want to 
code in the Java porridge let them use Java not try to turn Python 
into the same sludge. (And yes, I do realise that it's Guido who's 
doing the work in this case! :-)

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scan Codes, Character Sets: Any Modules?

2006-04-11 Thread doug shawhan
Oho! http://www.wyse.com/service/support/kbase/Keydetl1.asp?Q=7&R=6 has the hex codes! 

Thanks! Perhaps this will fix what ails me.On 4/11/06, doug shawhan <[EMAIL PROTECTED]> wrote:
Yeah, termca was were I looked first. 

The OpenBSD 3.8 termcap shows:
:cr=^M:ct=\E0:dc=\EW:dl=\ER:do=^J:ds=\EF\r:ei=\Er:fs=^M:\ for the
carriage return,  but then I can't find the hex code for ^M! :-)
At that point I thought I'd ask if there was some sort of termcapesque
module available.

I will scope the wyse website again, perhaps I can locate it this time! 

ThanksOn 4/11/06, Alan Gauld <
[EMAIL PROTECTED]> wrote:
Hi Doug,>  I am attempting to screen scrape SuperDOS, uses wyse 60 terminals> I must fall back to the old cntl-letter combos to get the cursor to behaveSounds like you need to find a tech spec for the Wyse 60. That should
give you all of the Ctrl character combos to control the screen.If you have access to a Unix box you might find the info you needin the termcap or terminfo databases. But Wyse should have thespec sheet on their web site somewhere...
Alan G.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for loops

2006-04-11 Thread David Rock
* josip <[EMAIL PROTECTED]> [2006-04-11 09:13]:
>   I have problem with this question.
>   Can someone show me the code and than explain it?
>
>   >>Write a Python program to print out the following  shape. You are 
> expected to use two for loops (these must be nested) to solve this problem.
>
>   output:
>   * * * * * 
>   *  *
>   *  *
>   * * * * *

That looks a lot like homework.  If you have a specific question about a
_part_ of code _you_ have written, we'll be glad to help out explaining
those specifics.  

We generally try to stay away from doing the work for you :-)

-- 
David Rock
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Michael
On Tuesday 11 April 2006 17:43, Alan Gauld wrote:
> >> There is no practical use for decorators IMHO
> >
> > It's true that decorators are syntactic sugar and don't add any new
> > functional capabilities to the language.
>
> Which is all I intended to imply.

You have a different meaning of "practical use" from common usage then. 
Something that makes it easier to:
   * Understand that a function has been modified
   * Make it clear that a function is a staticmethod/classmethod/etc *right at
  the beginning of a function* without having to read the entire body.
   * Reduce typing
   * Allow hinting of types for integration with tools like PyObjC
   * Make it clear when reading the definition of a method/function is
  memoised, traced etc
   * Simplifying augmentation of generators with arbitrary extra
  communications attributes.

... are all practical benefits. That means they have practical use in my
book :-)

Syntactic sugar *IS* a practical benefit. After all, every language above 
assember is syntactic sugar, and by your definition of no practical use.


Michael.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comp.lang.python newsgroup

2006-04-11 Thread Danny Yoo

[taking [EMAIL PROTECTED] out of CC]


Hi Kaushal,


Please do not crosspost between [EMAIL PROTECTED] and [EMAIL PROTECTED] 
They are different mailing lists.


> I went to a http://www.python.org/community/lists.html and found the 
> below newsgroup
>
> How do i use this news group and access the information I need

Did you really read the page?  I don't mean to be facetious, but the 
content is all right there from your link.

What part about that page is confusing to you?  I'm baffled, so if you can 
explain what difficulty you had in reading it, that may help us better 
understand.


Also, I've noticed you're asking a lot of rapid-fire questions.  You may 
want to take a look at:

 http://www.catb.org/~esr/faqs/smart-questions.html

so that you get a better idea of asking questions that have a good chance 
of getting answered well.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] failing to learn python

2006-04-11 Thread Python
On Tue, 2006-04-11 at 12:06 -0400, Payal Rathod wrote:
> The reason I am disgrunted with Python is because lack of good 
> documentation.

http://www.python.org/doc/
The Python Docs - possibly you missed this because of the plethora of
links.  The Library Reference used to have the tag line:
(keep this under your pillow)

The Module Index is the other key starting point.


http://rgruet.free.fr/PQR24/PQR2.4.html
A very handy quick reference.


I think Python has incredibly good documentation.  Fred Drake and the
other folks who write, edit and maintain the documentation do a
wonderful job of keeping it fresh and current as the language changes.
There are very few rough spots within the thousands of pages of text.

It's been a while since I looked at the Tutorial.  Is that where you
felt let down?

I felt compelled to respond to your post.  I think the quality and
quantity of Python documentation is hard to beat.

-- 
Lloyd Kvam
Venix Corp

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Kent Johnson
Alan Gauld wrote:
>> proposal for dynamic function overloading:
>> http://www.artima.com/weblogs/viewpost.jsp?thread=155514
> 
> I really don't like this proposal nor many of the others that currently 
> exist to seemingly turn Python into Java and C++! If people want to 
> code in the Java porridge let them use Java not try to turn Python 
> into the same sludge.

FWIW there is enough perceived need for this (and adapters or protocols) 
that it has been invented already two or three times, in PEAK (by Philip 
J Eby, author of a popular "Python is not Java" rant*), Zope and twisted 
IIRC. The PEAK version has been adopted by TurboGears. I think twisted 
has abandoned its own effort and adopted Zope's.

So I don't think it is necessarily people who want to turn Python into 
Java, but meeting a need that comes up in large projects.

Kent

* http://dirtsimple.org/2004/12/python-is-not-java.html

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] failing to learn python

2006-04-11 Thread Alan Gauld

> On Mon, Apr 10, 2006 at 04:20:37PM -0700, Danny Yoo wrote:
>> http://gnosis.cx/TPiP/
> 
> I will read that and Alan's tutorial too (isn't that MS-Windows specific 
> ???)

Nope, it usually mentions *nix workarounds etc
Most of it is OS neutral. The OS and IPC topic are mainly 
Unix centric.

> without proving it at all. All the examples I wanted were done better in 
> shell/sed/awk.  

I use Python a lot but I still use sed and awk for what they are good at.
Using the Right Tool for the Job is still a sound maxim.

Python is a general programmjing language great for bigger jobs. 
If you just want to run a sequience of unix commands shell is better. 
If you want simple but bulk text manipulation awk or sed are best.

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scan Codes, Character Sets: Any Modules?

2006-04-11 Thread doug shawhan
No luck. I think I am missing something utterly basic, and completely
unrelated to python. :-) I'll quit filling everyone's mailbox with
cheese and move on to a terminal newsgroup for my future pesterances.
Thanks for the help folks!On 4/11/06, doug shawhan <[EMAIL PROTECTED]> wrote:
Oho! http://www.wyse.com/service/support/kbase/Keydetl1.asp?Q=7&R=6
 has the hex codes! 

Thanks! Perhaps this will fix what ails me.On 4/11/06, doug shawhan <
[EMAIL PROTECTED]> wrote:
Yeah, termca was were I looked first. 

The OpenBSD 3.8 termcap shows:
:cr=^M:ct=\E0:dc=\EW:dl=\ER:do=^J:ds=\EF\r:ei=\Er:fs=^M:\ for the
carriage return,  but then I can't find the hex code for ^M! :-)
At that point I thought I'd ask if there was some sort of termcapesque
module available.

I will scope the wyse website again, perhaps I can locate it this time! 

ThanksOn 4/11/06, Alan Gauld <

[EMAIL PROTECTED]> wrote:
Hi Doug,>  I am attempting to screen scrape SuperDOS, uses wyse 60 terminals> I must fall back to the old cntl-letter combos to get the cursor to behaveSounds like you need to find a tech spec for the Wyse 60. That should
give you all of the Ctrl character combos to control the screen.If you have access to a Unix box you might find the info you needin the termcap or terminfo databases. But Wyse should have thespec sheet on their web site somewhere...
Alan G.




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Database Connectivity

2006-04-11 Thread Mike Hansen
  
> Kaushal Shriyan wrote:
> > Hi ALL
> > 
> > How do i connect my python program to MySQL DB or Oracle DB 
> or can you 
> > please specify the URL which gives a detailed explanation on this.
> 
> Basic connectivity is through modules that implement the 
> DB-API standard. Read the spec and find implementations here:
> http://www.python.org/doc/topics/database/
> 
> There are several popular packages that layer on top of this 
> basic functionality such as SQLObject and SQLAlchemy.
> 
> Kent

The DB-API spec would go a long way if it had a couple of examples. Poking
around the database topics, I came across an article in Linux Journal that
seems to help http://www.linuxjournal.com/article/2605

Also, the MySQLdb module has some examples
http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307#som
e-examples

The module for Postgre called psycopg has a nice PDF about the DB-API
http://initd.org/pub/software/psycopg/dbapi20programming.pdf

Mike


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Alan Gauld
> Syntactic sugar *IS* a practical benefit. After all, every language above
> assember is syntactic sugar, and by your definition of no practical use.

Ah, but by that standard even assembler is syntactic sugar, now
where are those hex codes and my keypunch? :-)

By practical use I mean adding function to the language.
I agree decorators add a modicum of readability, but
they also remove a large slice of Python's readability
by introducing more line noise characters to the language,
and that's a really bad feature from my perspective.

As to your list of benefits:

   * Understand that a function has been modified

I'm not sure I see how decorators do that? Can you give
an example to explain what you mean?

   * Make it clear that a function is a staticmethod/classmethod/etc *right 
at
  the beginning of a function* without having to read the entire body.

Not been an issue, I found the previous explicit statement to
be just as useful.

   * Reduce typing

Never a good reason for introducing features if the feature
introduces line noise in my book! I'd rather type a few more
characters than contend with [EMAIL PROTECTED] type crud.

   * Allow hinting of types for integration with tools like PyObjC

Too esoteric for mainstream, if I want to program in ObjC (and I do!)
I'll use ObjC. Its never worth corrupting one language just to integrate
with another (The only exception I've ever been happy with in that
regard is  the asm/endasm directives in C/Pascal/Cobol/Fortran etc)

   * Make it clear when reading the definition of a method/function is
  memoised, traced etc

I use a debugger for that kind of thing.

   * Simplifying augmentation of generators with arbitrary extra
  communications attributes.

Never ventured into this area yet so can't comment.

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Alan Gauld
>> I really don't like this proposal nor many of the others that currently 
>> exist to seemingly turn Python into Java and C++! If people want to
>
> FWIW there is enough perceived need for this (and adapters or protocols) 
> that it has been invented already two or three times, in PEAK (by Philip 
> ...
> So I don't think it is necessarily people who want to turn Python into 
> Java, but meeting a need that comes up in large projects.

Just because there is a perceived need doesn't make it good.
C++ started out as a neat elegant extension to C that enabled it
to do things that vanilla C couldn't. Then 'perceived need' added
features to support just about every OOP concept and flavour
going, plus adding a few new ones. The end result is a nightmare
language with so many holes gaping that it is almost impossible
to use effectively.

The result of that was the brain-dead Java v1 language which is
now being frantically patched and is rapidly turning into another
rotting pile of syntax. Unfortunately there is a real danger of Python
going the same way.What was an easy to learn and use, ideal language
for medium sized to fairly large scripting projects is trying to turn into
an all encompassing general purpose language which will be
increasingly difficult to use without falling down some new syntax hole.
I would hate for that to happen to Python simply because people are
trying to stretch it beyond its natural envelope.

Sadly Lisp appears to be the only language so far that has succeeded
in extending without losing its inherent simplicity of structure and syntax.
Unfortunately Lisp's simplicity is only apparent to mathemeticians! :-)

Alan G 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] for loops

2006-04-11 Thread Alan Gauld
>>   >>Write a Python program to print out the following  shape.
>>   >>You are expected to use two for loops (these must be nested) to solve 
>> this problem.
>>
>>   output:
>>   * * * * *
>>   *  *
>>   *  *
>>   * * * * *
>
> That looks a lot like homework.

I agree and very poor homework too.
I certainly wouldn't expect to use two for loops, I'd only use one.
(And that assumes the more general problem of drawing an
arbitrary sized square, for the specific case I'd use a single print
statement and triple quoted string!)

Any homework which implies that only one design is acceptable
is a bad assignment in my book!

> We generally try to stay away from doing the work for you :-)

But the advice stays good. Tell us how far you got and what's
sticking you and we will try to help...

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Bob Gailer
Alan Gauld wrote:
>> Syntactic sugar *IS* a practical benefit. After all, every language above
>> assember is syntactic sugar, and by your definition of no practical use.
>> 
>
> Ah, but by that standard even assembler is syntactic sugar, now
> where are those hex codes and my keypunch? :-)
Only those of us who keyed the bin loader into a PDP-8 by toggling 
switches, each representing one bit, can claim to be the most free of 
syntactic sugar.

"Real programmers eschew languages. They do it on the bare metal."

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tuple (Section 9.3)

2006-04-11 Thread Hugo González Monteverde
Hi Kaushal,

I might have to do a little guessing and see what is not clear from the
explanation.

The whole point of returning a tuple as opposed to, say, returning a
list, is the fact that  tuples are NON mutable. That is, *apparently*
you would not be returning a reference, but the values themselves.

There is no better example I can think of, than the one you already
read. If you want a function tu return multiple parameters so that you
can do.

ret1, ret2 = func()

then have func return a tuple. That's the whole point of the chapter you
read.

Maybe there is too much information in the discussion that does not look
useful to you if you have not run into a problem where you need it.

I hope this is not some kind of homework :)

Hugo
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Andrew D. Fant
Alan Gauld wrote:
>>Syntactic sugar *IS* a practical benefit. After all, every language above
>>assember is syntactic sugar, and by your definition of no practical use.
> 
> 
> Ah, but by that standard even assembler is syntactic sugar, now
> where are those hex codes and my keypunch? :-)
> 
> By practical use I mean adding function to the language.
> I agree decorators add a modicum of readability, but
> they also remove a large slice of Python's readability
> by introducing more line noise characters to the language,
> and that's a really bad feature from my perspective.
> 
*chomp* (sorry for introducing perl constructs here 8)
> Alan G

Alan,
  May I just say AMEN?  One of the reasons I started using python is that it was
the first programming language I had seen since Fortran that didn't make my eyes
cross from line noise characters stuck in among the text.  Has anyone ever
studied the effect of decorators on code readability?  Personally, I think that
if we want to add a bunch of unpronounceable characters to our code, we should
go back to using APL.  (for those who miss it, A+ is a fairly decent modern
update of the concept)  Now if you REALLY want to get funky.  try crossing lisp
with APL for a mathematically pure language that nobody can ever read.

Andy

-- 
Andrew Fant| And when the night is cloudy| This space to let
Molecular Geek | There is still a light  |--
[EMAIL PROTECTED] | That shines on me   | Disclaimer:  I don't
Boston, MA | Shine until tomorrow, Let it be | even speak for myself

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Bob Gailer
Andrew D. Fant wrote:
> [snip]  try crossing lisp
> with APL for a mathematically pure language that nobody can ever read.
>   
One problem is that APL allowed unbalanced parentheses (as in )SAVE), 
whereas LISP does not. And of course there is the right-to-left vs 
left-to-right issue...

I do miss the incredible succinctness of APL. One "line noise" character 
was worth (say) 10 lines of (say) Fortran.

I'd like to see some APL-ness added to Python, as I struggle to write a 
comprehension equivalent to one APL character.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Extending a list within a list comprehension

2006-04-11 Thread Victor Bouffier
Hi all,

I have solved my problem, but would like to know if what I accomplished
can be done with different data using list comprehensions.

the list I want to sort has the following format:

elements = [
(codigo, [ cant, importe, porc]),
(codigo, [ cant, importe, porc]),
...
]

Actual data is:
In [129]: elements[0:5]
Out[129]:
[('2712', [22.0, 3618.80999, 0.0032389476163069883]),
 ('2713', [19.0, 6551.81004, 0.0058640739309320719]),
 ('2710', [21.0, 2553.57999, 0.0022855336019435113]),
 ('2716', [19.0, 8215.27004, 0.0073529224203034461]),
 ('4305', [4.0, 348.37, 0.00031180199598565978])]


And I want to sort descending on 'importe', which is x[1][1] for x in
elements.

What I did was the following, following the Schwarzian Transform:

temporal = []
temporal = [ [x[1][1], (x[0], description[x[0]],
x[1][0], x[1][1], x[1][2] ) ] for x in elements ]
temporal.sort()
temporal.reverse()  # sort descending
elements = [ x[1] for x in temporal ]

If the second element in each array passed as x is of variable length
(that is, it has a different element count than three, in this case),
the program needs to extend the list instead. Without list
comprehensions, and the added capability to utilize and sized list as a
second element, my code ended up looking like the following:

temporal = []
for x in elements:
lst = [x[0], description[x[0]]]
lst.extend(x[1])
temporal.append([x[1][1], lst])
temporal.sort()
temporal.reverse()  # sort descending
elements = [ x[1] for x in temporal ]

Is there a way to use list comprehensions to append or extend the array
as needed by the second code listing?

Thanks.
Victor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extending a list within a list comprehension

2006-04-11 Thread John Fouhy
On 12/04/06, Victor Bouffier <[EMAIL PROTECTED]> wrote:
> elements = [
> (codigo, [ cant, importe, porc]),
> (codigo, [ cant, importe, porc]),
> ...
> ]
>
> And I want to sort descending on 'importe', which is x[1][1] for x in
> elements.

In python 2.4, you could achieve this by saying:

elements.sort(key=lambda x: x[1][1])

In earlier versions of python, you can do:

elements.sort(lambda x, y: cmp(x[1][1], y[1][1]))

(but this is less efficient than key= in 2.4)

There's also the decorate-sort-undecorate idiom:

dec = [(x[1][1], x) for x in elements]
dec.sort()
elements = [x[1] for x in dec]

--
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Alan Gauld
>> Ah, but by that standard even assembler is syntactic sugar, now
>> where are those hex codes and my keypunch? :-)
> Only those of us who keyed the bin loader into a PDP-8 by toggling 
> switches, each representing one bit, can claim to be the most free of 
> syntactic sugar.

Never used a PDP 8 (but I did use a PDP 11), but I have keyed binary 
boot sequences into a Data General machine in the late '70s using the 
front panel DIP switches

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extending a list within a list comprehension

2006-04-11 Thread Alan Gauld
Hi Victor,

I've gotta say that I much prefer the second version here.

> temporal = []
> temporal = [ [x[1][1], (x[0], description[x[0]],
>x[1][0], x[1][1], x[1][2] ) ] for x in elements ]
> temporal.sort()
> temporal.reverse()  # sort descending
> elements = [ x[1] for x in temporal ]
> 
> 
> temporal = []
> for x in elements:
>lst = [x[0], description[x[0]]]
>lst.extend(x[1])
>temporal.append([x[1][1], lst])
> temporal.sort()
> temporal.reverse()  # sort descending
> elements = [ x[1] for x in temporal ]
> 

That looks a lot easier to rtead (and debug) and will I suspect be 
easier to maintain. Copmprehensions are great but unfortunately 
can rapidly become incomprehensible!

> Is there a way to use list comprehensions to append or extend the array
> as needed by the second code listing?

There may be but I wouldn't try.
I might however look at using a simple list or dictionary of objects based 
on a class... It might be easier.

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [Fwd: Re: Extending a list within a list comprehension]

2006-04-11 Thread Victor Bouffier
I sent this to John directly.
Posting to the list.

 Forwarded Message 
From: Victor Bouffier <[EMAIL PROTECTED]>
To: John Fouhy <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Extending a list within a list comprehension
Date: Tue, 11 Apr 2006 18:03:41 -0500

On Wed, 2006-04-12 at 10:29 +1200, John Fouhy wrote:
> On 12/04/06, Victor Bouffier <[EMAIL PROTECTED]> wrote:
> > elements = [
> > (codigo, [ cant, importe, porc]),
> > (codigo, [ cant, importe, porc]),
> > ...
> > ]
> >
> > And I want to sort descending on 'importe', which is x[1][1] for x in
> > elements.
> 
> In python 2.4, you could achieve this by saying:
> 
> elements.sort(key=lambda x: x[1][1])
> 
> In earlier versions of python, you can do:
> 
> elements.sort(lambda x, y: cmp(x[1][1], y[1][1]))
> 
> (but this is less efficient than key= in 2.4)
> 
> There's also the decorate-sort-undecorate idiom:
> 
> dec = [(x[1][1], x) for x in elements]
> dec.sort()
> elements = [x[1] for x in dec]

Hi John,

Thanks for your help. This is very nice and I will definitely use it
when sorting a fixed list.
However, I forgot to point out I am including an extra element: item
description from a dict, where its key=codigo (x[0]). This is why I
write the whole list as a single element.

Still I could follow your suggestion and do:

dec = [(x[1][1], x, description[x[0]]) for x in elements]
dec.sort()
elements = [x[1], x[2] for x in dec]

It is still better, but not quite there. Any feedback will be gladly
taken.

Victor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extending a list within a list comprehension

2006-04-11 Thread Victor Bouffier
On Tue, 2006-04-11 at 23:42 +0100, Alan Gauld wrote:
> Hi Victor,
> 
> I've gotta say that I much prefer the second version here.
> 
> > temporal = []
> > temporal = [ [x[1][1], (x[0], description[x[0]],
> >x[1][0], x[1][1], x[1][2] ) ] for x in elements ]
> > temporal.sort()
> > temporal.reverse()  # sort descending
> > elements = [ x[1] for x in temporal ]
> > 
> > 
> > temporal = []
> > for x in elements:
> >lst = [x[0], description[x[0]]]
> >lst.extend(x[1])
> >temporal.append([x[1][1], lst])
> > temporal.sort()
> > temporal.reverse()  # sort descending
> > elements = [ x[1] for x in temporal ]
> > 
> 
> That looks a lot easier to rtead (and debug) and will I suspect be 
> easier to maintain. Copmprehensions are great but unfortunately 
> can rapidly become incomprehensible!
> 
> > Is there a way to use list comprehensions to append or extend the array
> > as needed by the second code listing?
> 
> There may be but I wouldn't try.
> I might however look at using a simple list or dictionary of objects based 
> on a class... It might be easier.
> 
> Alan G
> 
> 

Hi Alan,

I believe you are right. The most pythonic way is not always the most
"perlish" way ;-) (no offense intended to Perl-comers, but Perl does
tend to promote nesting of expressions).

It is much easier to read and maintain, which is what I will need to do
eventually.
Thanks for the feedback.

Victor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Decorators

2006-04-11 Thread Victor Bouffier
On Tue, 2006-04-11 at 21:08 +0100, Alan Gauld wrote:
> What was an easy to learn and use, ideal language
> for medium sized to fairly large scripting projects is trying to turn
> into
> an all encompassing general purpose language which will be
> increasingly difficult to use without falling down some new syntax
> hole.

Alan,

I could not agree more. Python's has both simplicity in its syntax and
very powerful constructs like generators and object orientation
included. I would hate to eventually need a Python reference book by my
side for even the simplest of projects because I could not remember $#&
meant a returned value from a function when the value was a particular
type (I used to work with Perl and you can get into that pretty
quickly).



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question About Function Arguments and Returned Results

2006-04-11 Thread Richard Querin
I'm a relative newbie writing a function that carries out a bunch of calculations. The function requires about 4 or 5 numeric arguments as input but the data returned from the function call is about a dozen numbers in addition to a list of strings and a fairly long list of numbers (200+).  My question is whether to create an class object beforehand and pass this into and out of the function, or to just keep things simple and pass in the 4 arguments and return a simple list object containing the numbers and lists of strings and numbers.
Is there any benefit to using an class object for this? I don't want to limit what I can do with the returned data later. I want to be able to display the results of this function in different ways later on. I'm going to have a lot of functions of this type and I know I will have to keep the format of the results list object well documented in order to keep track of what is returned when I look at the code 6 months from now. Is a simple list the best way? or a class object?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] encoding

2006-04-11 Thread kakada
Hi all,

Does anybody know how to decode to Windows Latin ("ANSI")
I know how to decode to utf-8:
stringinput = stringinput.decode('utf-8')
but while I use
stringinput = stringinput.decode('ANSI')
LookupError: unknown encoding: ANSI

so what is the correct way to do it?

Thanks and !Happy Khmer New Year!

kakada

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extending a list within a list comprehension

2006-04-11 Thread Kent Johnson
Victor Bouffier wrote:

> If the second element in each array passed as x is of variable length
> (that is, it has a different element count than three, in this case),
> the program needs to extend the list instead. Without list
> comprehensions, and the added capability to utilize and sized list as a
> second element, my code ended up looking like the following:
> 
> temporal = []
> for x in elements:
> lst = [x[0], description[x[0]]]
> lst.extend(x[1])
> temporal.append([x[1][1], lst])
> temporal.sort()
> temporal.reverse()  # sort descending
> elements = [ x[1] for x in temporal ]
> 
> Is there a way to use list comprehensions to append or extend the array
> as needed by the second code listing?

I think you are looking for
temporal = [ [x[0], description[x[0]]] + x[1] for x in elements ]

but I would make two steps, one for the sort using just
   [ (x[0], x) for x in elements ]

then when you pick the data back out you can format it how you like.

Kent

PS to John: the original solution is using DSU, aka Schwarzian Transform

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question About Function Arguments and Returned Results

2006-04-11 Thread Richard Querin
On 4/11/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
There is no need to pass the class object in to the function, you cancreate it in the function and return it. A class might be nice becauseit gives names to the various values. A dict can also be used for this.
Do what feels right :-)To be more specific, I'm going to have probably 6 functions that do similar things. They all take slightly different arguments, they all do slightly different calculations, but the results of all the functions are the same format. So I guess it makes sense to use a class. Now, when you say 'create it in the function', you mean create the class instance inside the function and return that instance? The class itself is defined somewhere else in the module containing the functions so that all the functions have access to it. (Total newb to python and classes so sorry if that's a stupid question).
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question About Function Arguments and Returned Results

2006-04-11 Thread Kent Johnson
Richard Querin wrote:
> 
> 
> On 4/11/06, *Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> 
> There is no need to pass the class object in to the function, you can
> create it in the function and return it. A class might be nice because
> it gives names to the various values. A dict can also be used for this.
> Do what feels right :-)
> 
> 
> To be more specific, I'm going to have probably 6 functions that do 
> similar things. They all take slightly different arguments, they all do 
> slightly different calculations, but the results of all the functions 
> are the same format. So I guess it makes sense to use a class. Now, when 

Yes, that's a good argument for a class.

> you say 'create it in the function', you mean create the class instance 
> inside the function and return that instance? The class itself is 
> defined somewhere else in the module containing the functions so that 
> all the functions have access to it. (Total newb to python and classes 
> so sorry if that's a stupid question).

Right, create the class instance in the function.

class ReturnedValue(object):
   def __init__(self, value):
 self.value = value

def myFunction(x, y, z, w):
   return ReturnedValue(x+y+z+w)

Season to taste ;)
Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] dictionary datatype

2006-04-11 Thread kakada
Hello all,

For example, I have a dictionary:
dict1 = { 0x2018:u'k', 0x2019:u'd'}

I assign:
n = 0x2018
print dict1[n]

Then:
KeyError: '0x2018'

But I can call directly:
print dict1[0x2018]

So, what is wrong with this? How can I solve it?

Thx

kakada
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary datatype

2006-04-11 Thread Jason Massey
Works for me:>>> dict1 = { 0x2018:u'k', 0x2019:u'd'}>>> n = 0x2018>>> print dict1[n]k>>> On 4/11/06, 
kakada <[EMAIL PROTECTED]> wrote:
Hello all,For example, I have a dictionary:dict1 = { 0x2018:u'k', 0x2019:u'd'}I assign:n = 0x2018print dict1[n]Then:KeyError: '0x2018'But I can call directly:print dict1[0x2018]
So, what is wrong with this? How can I solve it?Thxkakada___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extending a list within a list comprehension

2006-04-11 Thread Victor Bouffier
On Tue, 2006-04-11 at 22:17 -0400, Kent Johnson wrote:
> Victor Bouffier wrote:
> 
> > If the second element in each array passed as x is of variable length
> > (that is, it has a different element count than three, in this case),
> > the program needs to extend the list instead. Without list
> > comprehensions, and the added capability to utilize and sized list as a
> > second element, my code ended up looking like the following:
> > 
> > temporal = []
> > for x in elements:
> > lst = [x[0], description[x[0]]]
> > lst.extend(x[1])
> > temporal.append([x[1][1], lst])
> > temporal.sort()
> > temporal.reverse()  # sort descending
> > elements = [ x[1] for x in temporal ]
> > 
> > Is there a way to use list comprehensions to append or extend the array
> > as needed by the second code listing?
> 
> I think you are looking for
> temporal = [ [x[0], description[x[0]]] + x[1] for x in elements ]
> 

Hi Kent,
I try this one and get the following error:

TypeError: list objects are unhashable

I figured it is because of the x[0] element being used as a dict key.
Can you explain further where this error comes from? Are you getting it
too?

> but I would make two steps, one for the sort using just
>[ (x[0], x) for x in elements ]
> 
You are right. This is cleaner and not a big deal to do in two steps.
However for the issue at hand, it still keeps x as a two element list:

[codigo, [ cant, importe, porc]],

which I would like to have extended:

[codigo, cant, importe, porc]

It is not a big deal. That is why I finally went with the longer version
Alan suggested. Before the sort I get a two element list, the second
element being the list I finally want as output.

> then when you pick the data back out you can format it how you like.
> 

Right. After the sort I just get that second element in another list
comprehension

lines = [ x[1] for x in temp ]

Thanks for your help.
Victor

> 
> Kent
> 
> PS to John: the original solution is using DSU, aka Schwarzian Transform
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dictionary datatype

2006-04-11 Thread Victor Bouffier
On Tue, 2006-04-11 at 22:37 -0500, Jason Massey wrote:
> Works for me:
> 
> >>> dict1 = { 0x2018:u'k', 0x2019:u'd'}
> >>> n = 0x2018
> >>> print dict1[n]
> k
> >>> 
> 
> On 4/11/06, kakada <[EMAIL PROTECTED]> wrote:
> Hello all,
> 
> For example, I have a dictionary:
> dict1 = { 0x2018:u'k', 0x2019:u'd'}
> 
> I assign:
> n = 0x2018
> print dict1[n]
> 
> Then:
> KeyError: '0x2018'
> 
> But I can call directly:
> print dict1[0x2018] 
> 
> So, what is wrong with this? How can I solve it?
> 
> Thx
> 
> kakada
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

Look into the value of n after assignment:

In [285]: n = 0x2018

In [286]: n
Out[286]: 8216

n is taken as an integer containing the decimal number 8216, which in
hex is represented as 0x2018.

Try again to see if you did not mistakenly typed dict1['0x2018'] or else
defined n as such.

If you try dict1[n], dict1[0x2018], or dict1[8216] you get a correct
result, since the integer variable 'n' contains that value. Trying
dict1['0x2016'] gives you the error because the key does not exist.

define dict1 as:
dict1 = { 0x2018:u'k', 0x2019:u'd'}

and then display it whole:

In [299]: print dict1
{8216: u'k', 8217: u'd'}

Can you see your '0x2018' key anywhere?

HTH

Victor


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Tuples

2006-04-11 Thread Kaushal Shriyan
Hi All

I am referring to http://www.ibiblio.org/obp/thinkCSpy/chap09.htm
I did not understood the below Section at all :(

9.3 Tuples as return values

Functions can return tuples as return values. For example, we could
write a function that swaps two parameters:

def swap(x, y):
  return y, x


Then we can assign the return value to a tuple with two variables:

a, b = swap(a, b)


In this case, there is no great advantage in making swap a function.
In fact, there is a danger in trying to encapsulate swap, which is the
following tempting mistake:

def swap(x, y):  # incorrect version
  x, y = y, x


If we call this function like this:

swap(a, b)


then a and x are aliases for the same value. Changing xinside swap
makes x refer to a different value, but it has no effect on a in
__main__. Similarly, changing y has no effect on b.

This function runs without producing an error message, but it doesn't
do what we intended. This is an example of a semantic error.

As an exercise, draw a state diagram for this function so that you can
see why it doesn't work.

Thanks in Advance

Regards

Kaushal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor