Re: Reversing a string

2007-06-27 Thread ptn
>
> or one letter per line:
>
> >>> print "\n".join("spam"[::-1])
>
> m
> a
> p
> s
>

One liners rock ;)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reversing a string

2007-06-27 Thread ptn

> mylist = []
>
> That's bad. If you need to use a list in the rev function, you
> should bind a new list to a local variable inside rev.
>

He's right. If you want to use a list to temporarily store the
reversed version of your string, it should exist only in the local
namespace of your function.

There's still stuff you can do with your function to make it work,
such as:

>>> def rev(x):
mylist = []
for char in x:
 mylist.append(char)
mylist.reverse()
for letter in mylist:
 print letter

However, compare the incredible difference in clarity and elegance
between that and:

> >>> print "\n".join("spam"[::-1])

So, big lessons: (1) Global variables suck if you try to manipulate
them and (2) in Python, if your code isn't as clear as you would like,
there's probably a better way to do it.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Probably simple syntax error

2007-07-02 Thread ptn

>
> Problem 6: big_randomized_int can only have values in 0, 1, ..., 98,
> 99. So small_randomized_int will have the value 0, always.
>
> Perhaps you meant:
> small_randomised_float = big_randomized_int / 100.0
>
> > small_randomized_int = Round(small_randomized_int, 2)
> > # Round that value to 2 decimal places
>

PASCAL   -->PYTHON
5 div 2  -->   5/2
5 mod 2-->   5 % 2
5/2-->   5/2.(Notice the little dot at the end)

-- 
http://mail.python.org/mailman/listinfo/python-list


Files, directories and imports - relative to the current directory only

2008-03-25 Thread ptn
Hello, group.

I can only read files and import modules that are in the same
directory
as the one my script is.  Here is a test script (path.py):

import os
import uno  # some module I wrote

print list(os.walk('~/hacking/python'))
f = open('~/read/foo.txt')
print f.read()

And here is the output:

Traceback (most recent call last):
  File "path.py", line 2, in 
import uno
ImportError: No module named uno

If I comment that import, the output becomes this:

[]
Traceback (most recent call last):
  File "path.py", line 4, in 
f = open('~/read/foo.txt')
IOError: [Errno 2] No such file or directory: '~/read/foo.txt'

(Notice the empty list at the beginning, that would be the output of
os.walk().)

I have added this line to my .bashrc:
export PYTHONPATH=$PYTHONPATH:~/hacking/python
I thought that by doing so all the scripts found in that directory and
all it's children would be available for import, but that doesn't seem
to be the case.  As for the other problems, I have no idea.

So, what's wrong here? Maybe there's something I haven't set up?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Flattening lists

2009-02-09 Thread ptn
On Feb 5, 2:07 pm, [email protected] wrote:
> Quoth J Kenneth King :
>
>
>
> > mk  writes:
>
> > > Hello everybody,
>
> > > Any better solution than this?
>
> > > def flatten(x):
> > >     res = []
> > >     for el in x:
> > >         if isinstance(el,list):
> > >             res.extend(flatten(el))
> > >         else:
> > >             res.append(el)
> > >     return res
>
> > > a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]]
> > > print flatten(a)
>
> > > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
> > > Regards,
> > > mk
>
> >http://mail.python.org/pipermail/python-list/2005-July/330367.html
>
> That's worth reading.  I'm not sure why I'm finding this fun, but who
> cares.  I tried a couple of other functions after reading that article,
> and it looks like a generator that scans the nested lists is actually
> the winner, and also is in many ways the most elegant implementation.
> Of course, as noted in the emails following above article, the test data
> is really inadequate for proper optimization testing ;)
>
> -
> from __future__ import print_function
> from timeit import Timer
> from itertools import chain
>
> # This is the one from the article quoted above.
> def flatten6(seq):
>     i = 0
>     while (i != len(seq)):
>         while hasattr(seq[i], '__iter__'):
>             seq[i:i+1] = seq[i]
>         i = i + 1
>     return seq
>
> #This is my favorite from a readability standpoint out of
> #all the things I tried.  It also performs the best.
> def flatten8(seq):
>     for x in seq:
>         if not hasattr(x, '__iter__'): yield x
>         else:
>             for y in flatten8(x):
>                 yield y
>
> l = [[1, 2, 3], 5, [7, 8], 3, [9, [10, 11, 12], 4, [9, [10, 5, 7]], 1, 
> [5, 4], 3], 4, 3], 3, 1, 45], 9], 10]]
>
> if __name__=="__main__":
>     print(l)
>     print('flatten6', flatten6(l))
>     print('flatten8', list(flatten8(l)))
>     print('flatten6', Timer("flatten6(l)", "from temp3 import flatten6, 
> l").timeit())
>     print('flatten8', Timer("list(flatten8(l))", "from temp3 import flatten8, 
> l").timeit())
>
> -
>
> >src/python/Python-3.0/python temp3.py
>
> [[1, 2, 3], 5, [7, 8], 3, [9, [10, 11, 12], 4, [9, [10, 5, 7]], 1, [5, 
> 4], 3], 4, 3], 3, 1, 45], 9], 10]]
> flatten6 [1, 2, 3, 5, 7, 8, 3, 9, 10, 11, 12, 4, 9, 10, 5, 7, 1, 5, 4, 3, 4, 
> 3, 3, 1, 45, 9, 10]
> flatten8 [1, 2, 3, 5, 7, 8, 3, 9, 10, 11, 12, 4, 9, 10, 5, 7, 1, 5, 4, 3, 4, 
> 3, 3, 1, 45, 9, 10]
> flatten6 32.8386368752
> flatten8 30.7509689331
>
> >python temp3.py
>
> [[1, 2, 3], 5, [7, 8], 3, [9, [10, 11, 12], 4, [9, [10, 5, 7]], 1, [5, 
> 4], 3], 4, 3], 3, 1, 45], 9], 10]]
> flatten6 [1, 2, 3, 5, 7, 8, 3, 9, 10, 11, 12, 4, 9, 10, 5, 7, 1, 5, 4, 3, 4, 
> 3, 3, 1, 45, 9, 10]
> flatten8 [1, 2, 3, 5, 7, 8, 3, 9, 10, 11, 12, 4, 9, 10, 5, 7, 1, 5, 4, 3, 4, 
> 3, 3, 1, 45, 9, 10]
> flatten6 34.730714798
> flatten8 32.3252940178
>
> --RDM

I think the generator is clearer with a try statement, like in Magnus
Lie Hetland's solution from Beginning Python:

def flatten(nested):
try:
# Don't iterate over string-like objs.
try: nested + ''
except TypeError: pass
else: raise TypeError
for sub in nested:
for elem in flatten(sub):
yield elem
except TypeError:
# The for doesn't work for single elements.
yield nested

You can't iterate over string-like objs because the all strings are
built of infinite empty lists at the beginning, leading to infinite
recursion.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Guido's new method definition idea

2008-12-09 Thread ptn
On Dec 6, 10:15 am, "Russ P." <[EMAIL PROTECTED]> wrote:
> On Dec 6, 4:32 am, Andreas Waldenburger <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Sat, 6 Dec 2008 04:02:54 -0800 (PST) [EMAIL PROTECTED] wrote:
>
> > > class C:
> > >     def $method(arg):
> > >         $value = arg
>
> > > (Note there's no point after $, it's not currently possible).
> > > Ruby uses @ and @@ for similar purposes.
> > > I agree that the code looks worse, but also shorter to read and write,
> > > so in lines of code that use many instance attributes, that short $
> > > syntax helps keep the line shorter. So I may grow to accept this
> > > sugar...
>
> > But that is not the way Python is meant to work. There are several
> > tennets in the Zen of Python that don't chime well with this approach.
> > "self" is a speaking identifier, "$" isn't.
>
> Is "@" a "speaking identifier? How about "#" and "!="? Last I heard,
> they were all part of Python.

Those are operators and the comment starter, not identifiers.

I think that the more used an operator/variable is, the least mnemonic
it' name has to be.  Given that you'll be useing it all the time, you
don't need it's name reminding you what it's supposed to be used for.
So the "it's not a speaking-identifier" argument is not a good one, in
my opinion.

However, $ being ugly is a very strong argument.  Python is supposed
to be beautiful.  And sure you can get used to it, just as you can get
used to Pearl, assembly language or Brainfuck.  By beautiful we mean
beautiful at first sight.
--
http://mail.python.org/mailman/listinfo/python-list


Location HTTP Header

2008-12-17 Thread ptn
Hi all.

I tried this stupid script on my server:

#! /usr/bin/env python

print 'Location: http://www.google.com\n'

and it didn't work, I get a blank page.  I first tried the Location
header in another script, and when execution got to that point, it
would
just sort of ignore it, because the script would keep running to the
end
of the code (or stop at some unhandled exception).

I ran chmod 755 script.cgi and the cgi script is at the correct
directory, but still nothing.  I think it might be an error from the
server (the old PythonWeb.org webserver), maybe it doesn't support
Location?  Is that even possible?  Or maybe there's some setup I
forgot
to do?

Any ideas?

Thanks a lot.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Location HTTP Header

2008-12-18 Thread ptn
On Dec 17, 6:47 pm, "Gabriel Genellina" 
wrote:
> En Wed, 17 Dec 2008 20:52:42 -0200, ptn  escribió:
>
> > I tried this stupid script on my server:
>
> >    #! /usr/bin/env python
>
> >    print 'Location:http://www.google.com\n'
>
> > and it didn't work, I get a blank page.  I first tried the Location
> > header in another script, and when execution got to that point, it
> > would
> > just sort of ignore it, because the script would keep running to the
> > end
> > of the code (or stop at some unhandled exception).
>
> I assume this is a cgi script. For the Location field to be relevant, the  
> Status should be a 3xx (like 307 Temporary Redirect, or 302 Found)
> In your case, your server probably has already sent a 200 OK response, so  
> Location is ignored.
> Try adding a Status line -before Location above- like:
> print 'Status: 302 Found"
>
> > Any ideas?
>
> I'd use a different protocol other than CGI...
>
> --
> Gabriel Genellina

M no, that didn't do it.  The script now is:

#! /usr/bin/env python

print 'Status: 302 Found'
print 'Location: http://www.google.com\n'

but still nothing happens, I get the same blank page.
Since the sscript is so simple, maybe it is a matter of server
configuration?  Is that possible?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Joining of list

2008-07-18 Thread ptn
On Jul 18, 5:40 am, SUBHABRATA <[EMAIL PROTECTED]> wrote:
> Hi Peter,Peter Otten wrote:
> > SUBHABRATA wrote:
>
> > > Thanx Peter,
> > > I would change my variables next time I would post.
>
> > No, you should use meaningful variable names when you write your code no
> > matter whether you plan to post it or not.
>
> Good You are teaching me something good in life. Thanx.
>
> > > And obviously,
> > > thanx for your solution. I am reviewing it, I was also trying out some
> > > solutions.
>
> > You misunderstood. I did not modify your code other than changing the
> > variable names. My hope was that with this modification any errors sprang
> > to you eye...
>
> I was seeing that.
> I am almost near the solution. You can also try some hands if you
> feel.
> Best Regards,
> Subhabrata.
>
>
>
> > Peter
>
>

A couple more things on variable naming and coding style:

- You used "a{digit}" to name variables of different types (a4 was an
int, a2 was a list and the rest were strings). Remember C, where i, j,
k are indices, p, q, r are pointers, s, t are strings and x, y, z are
integers. For unimportant variables, you can skip long descriptive
names, so long you don't use a confusing one.

- You violated your own naming conventions. Why did you choose to use
s to name that last string? Use descriptive names and stick to your
own style.

- You use whitespace weirdly (like in a4>-1 or a4=a3.find).

Try reading PEP8 (http://www.python.org/dev/peps/pep-0008/), the Style
Guide for Python Code.

As for your code, you need to find where it is that missing_word and
first_char are being updated, and assign to s before that happens.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Joining of list

2008-07-19 Thread ptn
On Jul 18, 6:43 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On Jul 18, 11:42 pm, ptn <[EMAIL PROTECTED]> wrote:
> [snip]
>
> >  Remember C, where i, j,
> > k are indices, p, q, r are pointers, s, t are strings and x, y, z are
> > integers.
>
> Only by convention (even-K&R-v1 C required explicit declarations
> almost everywhere), and x etc being used for integers is news to
> me ... perhaps you were thinking of m and n.
>
> The only language I remember that had implicit typing was FORTRAN (GOD
> is real, but JESUS is an integer).

Yes, I meant by convention.

x is the first name that comes to mind when declaring an unimportant
int.  Perhaps it's just me.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Written in C?

2008-07-21 Thread ptn
On Jul 20, 5:50 pm, [EMAIL PROTECTED] wrote:
> I'm just learning about Python now and it sounds interesting. But I
> just read (on the Wiki page) that mainstream Python was written in C.
> That's what I was searching for: Python was written in what other
> language?
>
> See, my concern was something like: OK, if Python is so hot, then,
> hopefully someone is writing it in assembly language for each MPU chip
> out there. Otherwise, if, say, they've written it in C#, then it looks
> like the REAL, generally useful language to learn is C# and Python is
> akin to Visual Basic or something: a specialty languagewhereas
> REAL WORLD programmers who want to be generally useful go and learn
> C#.
>
> So I was suspecting the Python compiler or interpreter is written in a
> REAL language like C#. So, Wiki says it's written in C! It's almost as
> if it were an intentional trick...write your own, new language in an
> OLD, real world language that is passe. Compile it into executable
> modules of course, so it is a real, working compiler, alright. But the
> SOURCE is some old, high level language which no one wants to use
> anymore! So now you've got a hot new language package and no one can
> say "well, it is written in, the SOURCE code is written in, a REAL
> language." No, it's not! The source is some outdated language and
> compiler and no one is going to prefer learning THAT to learning your
> hot new language!
>
> I'm not dissing Python, here. Just noting that, if it is written in C,
> that throws a curve at me in trying to balance the value of learning
> Python vs. some other major language.


Sounds like you program only because someone's paying you. Any
programmer who says that C is outdated and not real *is* outdated and
not real.

Not used anymore? M I wonder, have you heard of something called
"Linux"? The open source Unix-like system? Or perhaps you are familiar
with "Apache"? Does "GNOME" ring any bells to you? "Vim"? "Git"? You
got some serious research to do, STFW.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Converting List of String to Integer

2008-07-22 Thread ptn
> n = []
> for k in a:
>     n.append([int(v) for v in k])
> print n
>
> Does anyone know what I am doing wrong?
>
> Thanks in advance.
>
> Samir

Use extend instead of append:

* Append -> add the one item to the end of the list
* Extend -> add the list of items to the end of the list
--
http://mail.python.org/mailman/listinfo/python-list


Function editing with Vim throws IndentError

2008-07-22 Thread ptn
Hi everybody,

I have a weird problem.  Say I have a .py file with some functions in
it, like this:

# (...)
def foo():
print("bar")

When I open it and add a line to one of the functions,

# (...)
def foo():
troz = "bar"
print(troz)

I get the following traceback from the interpreter:

Traceback (most recent call last):
  File "SOMEWHERE/example.py", line ??
troz = "bar"
  ^
IndentationError: unindent does not match any outer indentation
level

And so I'm forced to rewrite the function entirely just to add the new
line.

I thought that my problem was the w option from formatoptions, so I
changed my .vimrc file to this:

augroup filetype
  autocmd BufNewFile,BufRead *.txt set filetype=human
augroup END
autocmd FileType human setlocal formatoptions+=ta2w
autocmd FileType lisp,scheme,python,c,java,vim setlocal
formatoptions-=ta2w

But the problem didn't go away.  I don't think this has anything to
do
with the tabs and spaces, because I have them set up like this:

set tabstop=4 shiftwidth=4 expandtab

which is the standard way to handle them.

The scripts I load are: qbuf, TagList, indent/python.vim and a reduced
version of the standard python.vim

Could someone provide some pointers?

Thanks,

Pablo Torres N.
--
http://mail.python.org/mailman/listinfo/python-list


Re: problems displaying results in ibm_db

2009-07-01 Thread ptn
On Jul 1, 3:20 pm, digz  wrote:
> result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST
> 1 ROWS ONLY')

You have the same string split into two lines, which means that what
you actually have is this:

result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH
FIRST
1 ROWS ONLY')

You need to escape that little '\n' with a backslash:

result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST\
1 ROWS ONLY')

Better yet, use implicit string concatenation:

result = ibm_db.exec_immediate( conn, 'SELECT * FROM TEST FETCH FIRST'
'1 ROWS ONLY')

Note that now you have two strings: 'SELECT * FROM TEST FETCH FIRST'
and  '1 ROWS ONLY'


Pablo Torres N.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem installing python 2.6.2 from tarball

2009-07-01 Thread ptn
Sounds like you untarred directly into your home dir.  You're OK if
you delete those, they are the sources, although you should do so only
after uninstalling, just in case.

To unistall, follow this thread from the list from a while ago:
http://mail.python.org/pipermail/python-list/2004-December/296269.html

I have followed it and it worked.
-- 
http://mail.python.org/mailman/listinfo/python-list