Re: [Tutor] Sequences of letter

2010-04-12 Thread Stefan Behnel

Juan Jose Del Toro, 12.04.2010 07:12:

I wan to write a
program that could print out the suquence of letters from "aaa" all the way
to "zzz"  like this:
aaa
aab
aac
...
zzx
zzy
zzz

So far this is what I have:
letras =
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]


See the 'string' module. Also, note that you do not need a list here, you 
can iterate over a simple string value as well.




letra1 = 0
letra2 = 0
letra3 = 0
for i in letras:
 for j in letras:
 for k in letras:
 print letras[letra1]+letras[letra2]+letras[letra3]
 letra3=letra3+1
 letra2=letra2+1
letra1=letra1+1


Note that the above is highly redundant. Try printing the values of i, j 
and k during the loop, you will see why.


Stefan

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


Re: [Tutor] Sequences of letter

2010-04-12 Thread Alan Gauld

"Juan Jose Del Toro"  wrote

So far this is what I have:
letras =
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]


Because a string is a sequence of letters you could have saved
some typing by just doing:

letras = "abcdefghijklmnopqrstuvwxyz"



letra1 = 0
letra2 = 0
letra3 = 0


You don't really need these. Remember that a 'for' loop in Python is
really a 'foreach' loop. It will pick out the items in the sequence
itself, you don't need to use an index in most cases.


for i in letras:
for j in letras:
for k in letras:
print letras[letra1]+letras[letra2]+letras[letra3]


So this becomes:
  print i+j+k


letra3=letra3+1


However if you do insist on using indexes you will need to
reset the indexes to 0 at the end of each associated loop.


letra2=letra2+1

  letra3 = 0


letra1=letra1+1

  letra2, letra3=0,0


It goes all the way to aaz and then it gives me this error
print letras[letra1]+letras[letra2]+letras[letra3]
IndexError: list index out of range


Thats because you didn't reset the index to zero so letra3
was already at the end of letras and you tried to add one to
it again pushing it over the edge. This is the reason you
are best to use for loops without indexes, they handle all
that stuff for you.


Am I even in the right path?


The basic idea is right, you just forgot to reset the
inner index. When debugging this kind of problem
try adding a print line to priont out the variables.
That might have helped you see what was going wrong.

But inthis case it would be better to just forget the indexes
and use the loop variables, i,j,k directly.


I guess I should look over creating a function or something like that
because when I run it I can't even use my computer no memory left


Thats shouldn't be a problem, it does not use that much menory!
Unless you have a very small (and old) computer :-)

--
Alan Gauld
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] Sequences of letter

2010-04-12 Thread spir ☣
On Mon, 12 Apr 2010 00:12:59 -0500
Juan Jose Del Toro  wrote:

> Dear List;
> 
> I have embarked myself into learning Python, I have no programming
> background other than some Shell scripts and modifying some programs in
> Basic and PHP, but now I want to be able to program.
> 
> I have been reading Alan Gauld's Tutor which has been very useful and I've
> also been watching Bucky Roberts (thenewboston) videos on youtube (I get
> lost there quite often but have also been helpful).
> 
> So I started with an exercise to do sequences of letters, I wan to write a
> program that could print out the suquence of letters from "aaa" all the way
> to "zzz"  like this:
> aaa
> aab
> aac
> ...
> zzx
> zzy
> zzz
> 
> So far this is what I have:
> letras =
> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]

cinqLetras = "abcde"
# No need to enumerate manually to get a list of letters:
print list(cinqLetras)
# ==> ['a', 'b', 'c', 'd', 'e']

> letra1 = 0
> letra2 = 0
> letra3 = 0
> for i in letras:
> for j in letras:
> for k in letras:
> print letras[letra1]+letras[letra2]+letras[letra3]
> letra3=letra3+1
> letra2=letra2+1
> letra1=letra1+1

The items are *letters*, not indexes. Should be called l1,l2,l3 and "glued" 
directly.

> It goes all the way to aaz and then it gives me this error
> Traceback (most recent call last):
>  File "/home/administrador/programacion/python/letras2.py", line 8, in
> 
> print letras[letra1]+letras[letra2]+letras[letra3]
> IndexError: list index out of range
> Script terminated.
> 
> Am I even in the right path?
> I guess I should look over creating a function or something like that
> because when I run it I can't even use my computer no memory left


No need to use a list to traverse the string letter per letter:

def trioDeLetras(letras):
   for l1 in letras:
  for l2 in letras:
 for l3 in letras:
# (the comma avoid one line per letter trio)
print l1+l2+l3,

trioDeLetras(cinqLetras)
# ==>
'''\
aaa aab aac aad aae aba abb abc abd abe aca acb acc acd ace ada adb adc add ade 
aea aeb aec aed aee baa bab bac
bad bae bba bbb bbc bbd bbe bca bcb bcc bcd bce bda bdb bdc bdd bde bea beb bec 
bed bee caa cab cac cad cae cba
cbb cbc cbd cbe cca ccb ccc ccd cce cda cdb cdc cdd cde cea ceb cec ced cee daa 
dab dac dad dae dba dbb dbc dbd
dbe dca dcb dcc dcd dce dda ddb ddc ddd dde dea deb dec ded dee eaa eab eac ead 
eae eba ebb ebc ebd ebe eca ecb
ecc ecd ece eda edb edc edd ede eea eeb eec eed eee
''' 

Denis


vit esse estrany ☣

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


Re: [Tutor] Sequences of letter

2010-04-12 Thread Yashwin Kanchan
Hi Juan

Hope you have got the correct picture now...

I just wanted to show you another way of doing the above thing in just 4
lines.

for i in range(65,91):
for j in range(65,91):
for k in range(65,91):
print chr(i)+chr(j)+chr(k),


On 12 April 2010 06:12, Juan Jose Del Toro  wrote:

> Dear List;
>
> I have embarked myself into learning Python, I have no programming
> background other than some Shell scripts and modifying some programs in
> Basic and PHP, but now I want to be able to program.
>
> I have been reading Alan Gauld's Tutor which has been very useful and I've
> also been watching Bucky Roberts (thenewboston) videos on youtube (I get
> lost there quite often but have also been helpful).
>
> So I started with an exercise to do sequences of letters, I wan to write a
> program that could print out the suquence of letters from "aaa" all the way
> to "zzz"  like this:
> aaa
> aab
> aac
> ...
> zzx
> zzy
> zzz
>
> So far this is what I have:
> letras =
> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]
> letra1 = 0
> letra2 = 0
> letra3 = 0
> for i in letras:
> for j in letras:
> for k in letras:
> print letras[letra1]+letras[letra2]+letras[letra3]
> letra3=letra3+1
> letra2=letra2+1
> letra1=letra1+1
>
> It goes all the way to aaz and then it gives me this error
> Traceback (most recent call last):
>  File "/home/administrador/programacion/python/letras2.py", line 8, in
> 
> print letras[letra1]+letras[letra2]+letras[letra3]
> IndexError: list index out of range
> Script terminated.
>
> Am I even in the right path?
> I guess I should look over creating a function or something like that
> because when I run it I can't even use my computer no memory left
>
> --
> ¡Saludos! / Greetings!
> Juan José Del Toro M.
> jdeltoro1...@gmail.com
> Guadalajara, Jalisco MEXICO
>
>
> ___
> 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


[Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
I use this one-liner for moving photos nested a single folder deep
into the top-level folder:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh

I would like to expand this into an application that handles arbitrary
nesting and smart rename, so I figure that Python is the language that
I need. I have googled file handling in Python but I simply cannot get
something to replicate the current functionality of that lovely
one-liner. What fine manual should I be reading? I am not asking for
code, rather just a link to the right documentation.

Thanks.

-- 
Dotan Cohen

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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Serdar Tumgoren
 What fine manual should I be reading? I am not asking for

> code, rather just a link to the right documentation.
>

You'll definitely want to explore the os module, part of Python's built-in
standard library.

http://docs.python.org/library/os.html
http://docs.python.org/library/os.html#files-and-directories

There are a bunch of recipes on ActiveState and elsewhere online that can
demonstrate how the os methods are used to work with files and directories.
Some googling for the various method names (eg. os.mkdir ) should turn them
up.

Best of luck,

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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
On 12 April 2010 17:23, Serdar Tumgoren  wrote:
>  What fine manual should I be reading? I am not asking for
>>
>> code, rather just a link to the right documentation.
>
> You'll definitely want to explore the os module, part of Python's built-in
> standard library.
>
> http://docs.python.org/library/os.html
> http://docs.python.org/library/os.html#files-and-directories
>

Thanks, going through there now...

I found these relevant functions:
os.listdir
os.rename
os.removedirs (this looks great, as it is recursive and nondestructive)


> There are a bunch of recipes on ActiveState and elsewhere online that can
> demonstrate how the os methods are used to work with files and directories.
> Some googling for the various method names (eg. os.mkdir ) should turn them
> up.
>

ActiveState looks like a great resource. Thanks!

-- 
Dotan Cohen

http://bido.com
http://what-is-what.com

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Steven D'Aprano
On Tue, 13 Apr 2010 12:11:30 am Dotan Cohen wrote:
> I use this one-liner for moving photos nested a single folder deep
> into the top-level folder:
> find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh
>
> I would like to expand this into an application that handles
> arbitrary nesting and smart rename, so I figure that Python is the
> language that I need. I have googled file handling in Python but I
> simply cannot get something to replicate the current functionality of
> that lovely one-liner. 

"Lovely"??? What on earth does it do? It's worse than Perl code!!!
*half a wink*


> What fine manual should I be reading? I am not 
> asking for code, rather just a link to the right documentation.

See the shell utilities module:

import shutil




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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
> "Lovely"??? What on earth does it do? It's worse than Perl code!!!
> *half a wink*
>

Like a good wife, it does what I need even if it is not pretty on the
eyes. _That_ is lovely!
(I can get away with that, I'm married to a redhead.)


> See the shell utilities module:
>
> import shutil
>

It overwrites files. I am playing with os.rename instead as the two
directories will always be on the same file system.

One thing that I am stuck with is os.walk for getting all the
subfolders' files. Now that at least I have discovered the _names_ of
the functions that I need I can google my out of this! I will write
back when I have a working solution, for the sake of the archives.
Thanks!


-- 
Dotan Cohen

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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
I'm really stuck here. I need move all files in subdirectories of cwd
to cwd. So that, for instance, if we are in ~/photos then this file:
~/photos/a/b/file with space.jpg
...will move to this location:
~/photos/file with space.jpg

This is what I've come up with:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()

files = os.walk(currentDir)
for f in files:
os.rename(f, currentDir)


However, it fails like this:
$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 8, in 
os.rename(f, currentDir)
TypeError: coercing to Unicode: need string or buffer, tuple found

I can't google my way out of this one! The filenames on my test setup
are currently ascii, but this does need to be unicode-compatible as
the real filenames will have unicode non-ascii characters and even
spaces in the filenames!

What should I be reading now?

Thanks!


-- 
Dotan Cohen

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


Re: [Tutor] Sequences of letter

2010-04-12 Thread Dave Angel

Or more readably:

from string import lowercase as letters
for c1 in letters:
for c2 in letters:
for c3 in letters:
   print c1+c2+c3


Yashwin Kanchan wrote:

Hi Juan

Hope you have got the correct picture now...

I just wanted to show you another way of doing the above thing in just 4
lines.

for i in range(65,91):
for j in range(65,91):
for k in range(65,91):
print chr(i)+chr(j)+chr(k),


On 12 April 2010 06:12, Juan Jose Del Toro  wrote:

  

Dear List;

I have embarked myself into learning Python, I have no programming
background other than some Shell scripts and modifying some programs in
Basic and PHP, but now I want to be able to program.

I have been reading Alan Gauld's Tutor which has been very useful and I've
also been watching Bucky Roberts (thenewboston) videos on youtube (I get
lost there quite often but have also been helpful).

So I started with an exercise to do sequences of letters, I wan to write a
program that could print out the suquence of letters from "aaa" all the way
to "zzz"  like this:
aaa
aab
aac
...
zzx
zzy
zzz

So far this is what I have:
letras =
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z"]
letra1 = 0
letra2 = 0
letra3 = 0
for i in letras:
for j in letras:
for k in letras:
print letras[letra1]+letras[letra2]+letras[letra3]
letra3=letra3+1
letra2=letra2+1
letra1=letra1+1

It goes all the way to aaz and then it gives me this error
Traceback (most recent call last):
 File "/home/administrador/programacion/python/letras2.py", line 8, in

print letras[letra1]+letras[letra2]+letras[letra3]
IndexError: list index out of range
Script terminated.

Am I even in the right path?
I guess I should look over creating a function or something like that
because when I run it I can't even use my computer no memory left

--
¡Saludos! / Greetings!
Juan José Del Toro M.
jdeltoro1...@gmail.com
Guadalajara, Jalisco MEXICO


___
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] Move all files to top-level directory

2010-04-12 Thread Sander Sweers
On 12 April 2010 18:28, Dotan Cohen  wrote:
> However, it fails like this:
> $ ./moveUp.py
> Traceback (most recent call last):
>  File "./moveUp.py", line 8, in 
>    os.rename(f, currentDir)
> TypeError: coercing to Unicode: need string or buffer, tuple found

os.rename needs the oldname and the new name of the file. os.walk
returns a tuple with 3 values and it errors out.

Also os.getcwd returns the working dir so if you run it in the wrong
folder you will end up with a mess. In idle on my windows machine at
work this is what is gives me.

>>> os.getcwd()
'C:\\Python26'

So it is better to give the program the path you want it to look in
rather then relying on os.getcwd().

os.walk returns you a tuple with the following values:
(the root folder, the folders in the root, the files in the root folder).

You can use tuple unpacking to split each one in separate values for
your loop. Like:

for root, folder, files in os.walk('your path):
   #do stuff

It might be wise to only have this module print what it would do
instead of doing the actual move/rename so you can work out the bugs
first before it destroys your data.

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


Re: [Tutor] Sequences of letter

2010-04-12 Thread Steven D'Aprano
On Tue, 13 Apr 2010 02:46:39 am Dave Angel wrote:
> Or more readably:
>
> from string import lowercase as letters
> for c1 in letters:
>  for c2 in letters:
>  for c3 in letters:
> print c1+c2+c3


Here's another solution, for those using Python 2.6 or better:


>>> import itertools
>>> for x in itertools.product('abc', 'abc', 'abc'):
... print ''.join(x)
...
aaa
aab
aac
aba
abb
abc
[many more lines...]
ccc


If you don't like the repeated 'abc' in the call to product(), it can be 
written as itertools.product(*['ab']*3) instead.





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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
On 12 April 2010 20:12, Sander Sweers  wrote:
> On 12 April 2010 18:28, Dotan Cohen  wrote:
>> However, it fails like this:
>> $ ./moveUp.py
>> Traceback (most recent call last):
>>  File "./moveUp.py", line 8, in 
>>    os.rename(f, currentDir)
>> TypeError: coercing to Unicode: need string or buffer, tuple found
>
> os.rename needs the oldname and the new name of the file. os.walk
> returns a tuple with 3 values and it errors out.
>

I see, thanks. So I was sending it four values apparently. I did not
understand the error message.

> Also os.getcwd returns the working dir so if you run it in the wrong
> folder you will end up with a mess. In idle on my windows machine at
> work this is what is gives me.
>
 os.getcwd()
> 'C:\\Python26'
>
> So it is better to give the program the path you want it to look in
> rather then relying on os.getcwd().
>

I intend to use this in a Dolphin (KDE file manager) service menu
only. But I will try to be careful about that in the future. Running
the script in $HOME might be interesting!

Actually, I will add a check that cwd != $HOME || $HOME/.bin as those
are the only likely places it might run by accident. Or maybe I'll
wrap it in Qt and add a confirm button.


> os.walk returns you a tuple with the following values:
> (the root folder, the folders in the root, the files in the root folder).
>
> You can use tuple unpacking to split each one in separate values for
> your loop. Like:
>
> for root, folder, files in os.walk('your path):
>   #do stuff
>

I did see that while googling, but did not understand it. Nice!


> It might be wise to only have this module print what it would do
> instead of doing the actual move/rename so you can work out the bugs
> first before it destroys your data.
>

I am testing on fake data, naturally.

Thanks!


-- 
Dotan Cohen

http://bido.com
http://what-is-what.com

Please CC me if you want to be sure that I read your message. I do not
read all list mail.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dotan Cohen
All right, I have gotten quite a bit closer, but Python is now
complaining about the directory not being empty:

✈dcl:test$ cat moveUp.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()

filesList = os.walk(currentDir)
for root, folder, file in filesList:
for f in file:
toMove = root + "/" + f
#print toMove
os.rename(toMove, currentDir)

✈dcl:test$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 11, in 
os.rename(toMove, currentDir)
OSError: [Errno 39] Directory not empty


I am aware that the directory is not empty, nor should it be! How can
I override this?

Thanks!

-- 
Dotan Cohen

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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Wayne Werner
On Mon, Apr 12, 2010 at 1:21 PM, Dotan Cohen  wrote:

> All right, I have gotten quite a bit closer, but Python is now
> complaining about the directory not being empty:
>
> ✈dcl:test$ cat moveUp.py
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> import os
> currentDir = os.getcwd()
>
> filesList = os.walk(currentDir)
> for root, folder, file in filesList:
>for f in file:
>toMove = root + "/" + f
>#print toMove
>os.rename(toMove, currentDir)
>
> ✈dcl:test$ ./moveUp.py
> Traceback (most recent call last):
>   File "./moveUp.py", line 11, in 
>os.rename(toMove, currentDir)
> OSError: [Errno 39] Directory not empty
>
>
> I am aware that the directory is not empty, nor should it be! How can
> I override this?
>

from the docs:
os.rename(*src*, *dst*)¶
Rename
the file or directory *src* to *dst*. If *dst* is a directory,
OSErrorwill
be raised. On Unix, if
*dst* exists and is a file, it will be replaced silently if the user has
permission. The operation may fail on some Unix flavors if *src* and
*dst*are on different filesystems. If successful, the renaming will be
an atomic
operation (this is a POSIX requirement). On Windows, if *dst* already
exists, 
OSErrorwill
be raised even if it is a file; there may be no way to implement an
atomic rename when *dst* names an existing file. Availability: Unix,
Windows.It seems what you wan to do is os.rename(toMove, currentDir+f)

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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dave Angel

Dotan Cohen wrote:

On 12 April 2010 20:12, Sander Sweers  wrote:
  

On 12 April 2010 18:28, Dotan Cohen  wrote:


However, it fails like this:
$ ./moveUp.py
Traceback (most recent call last):
 File "./moveUp.py", line 8, in 
   os.rename(f, currentDir)
TypeError: coercing to Unicode: need string or buffer, tuple found
  

os.rename needs the oldname and the new name of the file. os.walk
returns a tuple with 3 values and it errors out.




I see, thanks. So I was sending it four values apparently. I did not
understand the error message.

  
No, you're sending it two values:  a tuple, and a string.  It wants two 
strings.  Thus the error. If you had sent it four values, you'd have 
gotten a different error.


Actually, I will add a check that cwd !=HOME || $HOME/.bin as those
are the only likely places it might run by accident. Or maybe I'll
wrap it in Qt and add a confirm button.


  

os.walk returns you a tuple with the following values:
(the root folder, the folders in the root, the files in the root folder).

You can use tuple unpacking to split each one in separate values for
your loop. Like:

for root, folder, files in os.walk('your path):
  #do stuff




I did see that while googling, but did not understand it. Nice!


  

Judging from your next message, you still don't understand it.

It might be wise to only have this module print what it would do
instead of doing the actual move/rename so you can work out the bugs
first before it destroys your data.




I am testing on fake data, naturally.

  
Is your entire file system fake?  Perhaps you're running in a VM, and 
don't mind trashing it.


While debugging, you're much better off using prints than really moving 
files around.  You might be amazed how much damage a couple of minor 
bugs could cause.


DaveA

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


Re: [Tutor] accessing Postgres db results by column name

2010-04-12 Thread Serdar Tumgoren
Hey folks,

Wanted to send along one more update about this topic. Steve Orr pointed out
in a comment on Ricardo's new recipe that there's yet another way to get
named attribute access to cursor results.

The secret sauce is namedtuple, "high performance" collection type.  This
appears to be the "canonical" approach these days:

http://code.activestate.com/recipes/500261/

The Python Docs have a nice little example of this approach, using both the
csv and sqlite3 modules:

http://docs.python.org/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields

And you can check out Steve's comment at the bottom of Ricardo's recipe:

http://code.activestate.com/recipes/577186-accessing-cursors-by-field-name/

Regards,

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


Re: [Tutor] accessing Postgres db results by column name

2010-04-12 Thread Serdar Tumgoren
> Wanted to send along one more update about this topic. Steve Orr pointed
> out in a comment on Ricardo's new recipe that there's yet another way to get
> named attribute access to cursor results.
>

I should add the disclaimer that namedtuple is only available in Python 2.6+
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Dave Angel



Dotan Cohen wrote:

All right, I have gotten quite a bit closer, but Python is now
complaining about the directory not being empty:

✈dcl:test$ cat moveUp.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir =s.getcwd()

filesList =s.walk(currentDir)
for root, folder, file in filesList:
  

Why is the print below commented out?

for f in file:
toMove =oot + "/" + f
#print toMove
os.rename(toMove, currentDir)

✈dcl:test$ ./moveUp.py
Traceback (most recent call last):
  File "./moveUp.py", line 11, in 
os.rename(toMove, currentDir)
OSError: [Errno 39] Directory not empty


I am aware that the directory is not empty, nor should it be! How can
I override this?

Thanks!

  
Have you looked at the value of "currentDir" ? Is it in a form that's 
acceptible to os.rename() ? And how about toMove? Perhaps it has two 
slashes in a row in it. When combining directory paths, it's generally 
safer to use


os.path.join()

Next, you make no check whether "root" is the same as "currentDir". So 
if there are any files already in the top-level directory, you're trying 
to rename them to themselves.


I would also point out that your variable names are very confusing. 
"file" is a list of files, so why isn't it plural? Likewise "folders."


DaveA


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


Re: [Tutor] Move all files to top-level directory

2010-04-12 Thread Sander Sweers
On 12 April 2010 22:13, Dave Angel  wrote:
> When combining directory paths, it's generally safer to use
>
> os.path.join()

As KDE/Dolphin runs on windows this is even more important as it will
sort out the directory separator (/ vs \) for you.

Some added reading on os.path can be found on Doug's excellent PyMOTW
[1]. Also check out the glob module [2].

Greets
Sander

[1] http://blog.doughellmann.com/2008/01/pymotw-ospath.html
[2] http://blog.doughellmann.com/2007/07/pymotw-glob.html
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sequences of letter

2010-04-12 Thread Alan Gauld


"Steven D'Aprano"  wrote


import itertools
for x in itertools.product('abc', 'abc', 'abc'):


If you don't like the repeated 'abc' in the call to product(), it can be
written as itertools.product(*['ab']*3) instead.


Nope, I think the repeated string is much clearer, and thus better,
than the cryptogram thanks very much! :-)

But I like the itertools solution.
I really, really, need to spend some time playing with itertools.


--
Alan Gauld
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] Move all files to top-level directory

2010-04-12 Thread Alan Gauld


"Dotan Cohen"  wrote


I use this one-liner for moving photos nested a single folder deep
into the top-level folder:
find * -name "*.jpg" | awk  -F/ '{print "mv "$0,$1"-"$2}' | sh


You could miss out the awk and use the exec option of find...

Or miss out the shell and use the system() function of awk.


I need. I have googled file handling in Python but I simply cannot get
something to replicate the current functionality of that lovely
one-liner. What fine manual should I be reading? I am not asking for
code, rather just a link to the right documentation.


You won't easily get a one liner to do the same in Python but you
can do the same things using the glob, subprocess, shutil, os and 
path modules. In particular look at the os.walk and the shutil.move 
functions.


In addition to the module documentation you will find examples and 
description of both in the Using the OS topic of my tutorial.


HTH,

--
Alan Gauld
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] Sequences of letter

2010-04-12 Thread Rich Lovely
On 13 April 2010 01:07, Alan Gauld  wrote:
>
> "Steven D'Aprano"  wrote
>
> import itertools
> for x in itertools.product('abc', 'abc', 'abc'):
>>
>> If you don't like the repeated 'abc' in the call to product(), it can be
>> written as itertools.product(*['ab']*3) instead.
>
> Nope, I think the repeated string is much clearer, and thus better,
> than the cryptogram thanks very much! :-)
>
> But I like the itertools solution.
> I really, really, need to spend some time playing with itertools.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

itertools.product() also has the repeat keyword argument:

for x in itertools.product('abc', 'abc', 'abc'):

is the same as

for x in itertools.product('abc', repeat=3):


-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor