Re: [Tutor] cherrypy

2010-04-13 Thread Alan Gauld


"Rayon"  wrote 

I am running cherrypy server but I am having some trouble with sessions 


We need something a bit more specific.
What kind if "trouble"?

Does it execute but with the wrong result? (In what way wrong?)
Does it execute with errors? (please include error text)
Does it fail to execute?

Where is my code 


from cherrypy.lib import sessions

sess = sessions.Session()
x = sess.id 
return x; 


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


[Tutor] cherrypy

2010-04-13 Thread Rayon
I am running cherrypy server but I am having some trouble with sessions 

 

Where is my code 

 

from cherrypy.lib import sessions

sess = sessions.Session()

x = sess.id 

 

 

return x; 

 

 

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


Re: [Tutor] Making pretty web pages from python code examples?

2010-04-13 Thread Stefan Behnel

Modulok, 13.04.2010 16:52:

... generate pretty little web pages ...


Note that the 'pretty' bit usually doesn't reside in HTML but in CSS.

Stefan

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


Re: [Tutor] Making pretty web pages from python code examples?

2010-04-13 Thread Martin A. Brown
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hello,

 : Is there an easy way to get a docstring from a python file, 
 : without first importing it?
 : 
 : Basically, I'm trying to read python code examples from files on 
 : disk and generate pretty little web pages from them. I invision 
 : the docstring appearing as the main text on the page, with the 
 : rest of the code in HTML code or pre tags. That way I could just 
 : write up a bunch of python samples, without having to do anything 
 : more but upload them. Maybe something like this exists already? I 
 : don't know. I just figured it would be pretty easy to write, but 
 : getting the docstrings without trying to parse the files was a 
 : rut.

I think you may want sphinx [0] which is available in PyPI [1].

It understands reStrucTured text and adds a few Sphinx-specific 
directives/features for pulling the docstrings from your python 
modules.  Much easier than building your own.

Good luck,

- -Martin

 [0] http://sphinx.pocoo.org/
 http://sphinx.pocoo.org/contents.html
 http://pypi.python.org/pypi/Sphinx

- -- 
Martin A. Brown
http://linux-ip.net/
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/)

iD8DBQFLxIjeHEoZD1iZ+YcRAmNbAJ4kteAHzm6b7sSkLFn1w4Tbk5z3YQCgqtnE
1BsohLC9e8FIL/hpwvnKBWs=
=wKJ5
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Making pretty web pages from python code examples?

2010-04-13 Thread Modulok
Is there an easy way to get a docstring from a python file, without
first importing it?

Basically, I'm trying to read python code examples from files on disk
and generate pretty little web pages from them. I invision the
docstring appearing as the main text on the page, with the rest of the
code in HTML code or pre tags. That way I could just write up a bunch
of python samples, without having to do anything more but upload them.
Maybe something like this exists already? I don't know. I just figured
it would be pretty easy to write, but getting the docstrings without
trying to parse the files was a rut.

Thanks.
-Modulok-
___
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-13 Thread Dotan Cohen
> Actual the first item in the tuple (returned by os.walk) is singular (a
> string), so I might call it rootDir.  Only the other two needed to be
> changed to plural to indicate that they were lists.

I did discover this, too. I wanted to finish with the code at hand
before I started experimenting with things.


> Note that it's not just race conditions that can cause collisions.  You
> might have the same name in two distinct subdirectories, so they'll end up
> in the same place.  Which one wins depends on the OS you're running, I
> believe.
>

The code checks for that, and assigns a different filename if the name
is already taken. See the renameNumber variable.

Thank you very much for your help.


-- 
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-13 Thread Dave Angel

Dotan Cohen wrote:

Here is the revised version:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
i = 1
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
  
Actual the first item in the tuple (returned by os.walk) is singular (a 
string), so I might call it rootDir.  Only the other two needed to be 
changed to plural to indicate that they were lists.

for f in files:
if (rootDirs!=currentDir):
toMove  = os.path.join(rootDirs, f)
print "--- "+str(i)
print toMove
newFilename = os.path.join(currentDir,f)
renameNumber = 1
while(os.path.exists(newFilename)):
print "- "+newFilename
newFilename = os.path.join(currentDir,f)+"_"+str(renameNumber)
renameNumber = renameNumber+1
print newFilename
i=i+1
os.rename(toMove, newFilename)

Now, features to add:
1) Remove empty directories. I think that os.removedirs will work here.
2) Prevent race conditions by performing the filename check during
write. For that I need to find a function that fails to write when the
file exists.
3) Confirmation button to prevent accidental runs in $HOME for
instance. Maybe add some other sanity checks. If anybody is still
reading, I would love to know what sanity checks would be wise to
perform.

Again, thanks to all who have helped.


  
Note that it's not just race conditions that can cause collisions.  You 
might have the same name in two distinct subdirectories, so they'll end 
up in the same place.  Which one wins depends on the OS you're running, 
I believe.


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-13 Thread Dotan Cohen
Here is the revised version:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
i = 1
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
for f in files:
if (rootDirs!=currentDir):
toMove  = os.path.join(rootDirs, f)
print "--- "+str(i)
print toMove
newFilename = os.path.join(currentDir,f)
renameNumber = 1
while(os.path.exists(newFilename)):
print "- "+newFilename
newFilename = os.path.join(currentDir,f)+"_"+str(renameNumber)
renameNumber = renameNumber+1
print newFilename
i=i+1
os.rename(toMove, newFilename)

Now, features to add:
1) Remove empty directories. I think that os.removedirs will work here.
2) Prevent race conditions by performing the filename check during
write. For that I need to find a function that fails to write when the
file exists.
3) Confirmation button to prevent accidental runs in $HOME for
instance. Maybe add some other sanity checks. If anybody is still
reading, I would love to know what sanity checks would be wise to
perform.

Again, thanks to all who have helped.


-- 
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-13 Thread Dotan Cohen
> I would add a few different features to this 'find' to make it a bit
> more resistant to failure, although this sort of solution is always
> subject to the "somebody else is toying with my filesystem race
> condition".
>
>  find "$srcdir" -depth -type f -print0 \
>    | xargs --null --no-run-if-empty -- \
>      mv --target-directory "$dstdir" --
>
> The --target-directory option is only available in GNU mv (and cp),
> I believe.
>

I am trying to get into Python now, but I will go over that at a later
time to improve my shell scripting skills. Thanks. I do appreciate the
instruction.


> I'll second the recommendation for 'shutil', although you can have
> some fun playing with recursion by building your tree flattener
> using os.walk(), os.path.split() and os.path.join().
>

For certain values of "fun"! Actually, I did enjoy this exercise. I
learned a lot, and found some great resources. Python really is a fun
language.

-- 
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-13 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
>
> 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.
>

The truth is that is not really my code, I modified it from something
that I found. I really want to code my own, and to understand what I
am doing. I'd rather use a real language for it, not a scripting
language as I want the knowledge to apply as generally as possible.


> 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.
>

I am not looking for a one-liner. But thanks for clarifying.


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

I will read that. I did see your site once, I even bookmarked it. I
will go over it thoroughly! I actually have some comments about the
site, would you like them in private mail if you are interested?

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-13 Thread Dotan Cohen
>> 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.
>

Added, thanks!

> 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
>

Oh, that is a great site! Any others that I should know about? Being
new to the Python community, I am as of yet unfamiliar with the
standard resources.

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] Linux lib path

2010-04-13 Thread Joson
Thanks.

2010/4/13 Joson 

> thanks a lot. you've saved my life...
>
> 2010/4/10 Chris Fuller 
>
>
>> I'm using Debian.  Used to be etch, but I did a double dist-upgrade
>> recently.
>> So, whatever the current testing release is.  My shell is zsh, but bash
>> should
>> work the same.
>>
>> PYTHONPATH should have worked.  CLASSPATH is for Java.
>>
>>
>> Here's the documentation link you want:
>> http://docs.python.org/install/index.html#inst-search-path
>> http://docs.python.org/library/site.html
>>
>>
>> Files that end in ".pth" are read by Python and the contents are added to
>> sys.path.
>>
>>
>> 0 % python
>> Python 2.5.5 (r255:77872, Feb  1 2010, 19:53:42)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import sys
>> >>> '/home/cfuller/tmp' in sys.path
>> False
>>
>> 0 % export PYTHONPATH=/home/cfuller/tmp
>> 0 % python
>> Python 2.5.5 (r255:77872, Feb  1 2010, 19:53:42)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import sys
>> >>> '/home/cfuller/tmp' in sys.path
>> True
>>
>>
>> What I prefer to do, rather than mucking around with environment variables
>> (which isn't reliable, say if its called from a daemon, init script, or
>> maybe
>> a non-interactive shell, and probably other esoterica) is to use .pth
>> files.
>> These are just a listing of directories for Python to add to sys.path.  A
>> lot
>> of packages include some of their own, you should find some in
>> site-packges.
>> Used to be you had to put them there to get them loaded, but there is new
>> per-
>> user support in Python 2.6 and 3k:
>> http://www.python.org/dev/peps/pep-0370/.
>>
>> 0 % export PYTHONPATH=
>> 0 % python
>> Python 2.5.5 (r255:77872, Feb  1 2010, 19:53:42)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import sys
>> >>> '/home/cfuller/tmp' in sys.path
>>
>>
>> 0 % mkdir -p ~/.local/lib/python2.6/site-packages
>> 0 % echo /home/cfuller/tmp > ~/.local/lib/python2.6/site-packages/tmp.pth
>> 0 % python2.6
>> Python 2.6.5 (r265:79063, Mar 18 2010, 23:38:15)
>> [GCC 4.4.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import sys
>> >>> '/home/cfuller/tmp' in sys.path
>> True
>>
>>
>>
>> Cheers
>>
>>
>> On Friday 09 April 2010, Joson wrote:
>> > Hi all,
>> >
>> > How to append a path ("/var/myprog/src") to sys.path, but not in the
>> >  dynamic way like sys.path.apend(packpath), please?
>> > I use debian os. and I'd tried to set the classpath in /etc/profile
>> (export
>> > CLASSPATH="..."), and the pythonpath too (export PYTHONPATH="..."). I
>> found
>> > it didn't work.
>> >
>> > Best regards,
>> >
>> > Joson
>> >
>>
>> ___
>> 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-13 Thread Dotan Cohen
All right, I've got it! This script will move all files of
subdirectories into cwd.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
currentDir = os.getcwd()
filesList = os.walk(currentDir)
for rootDirs, folders, files in filesList:
for f in files:
toMove  = os.path.join(rootDirs, f)
newFilename = os.path.join(currentDir,f)
os.rename(toMove, newFilename)

Now, features to add:
1) Smart rename: don't clobber existing files
2) Remove empty directories
3) Check that it works with spaces in filenames and directories
4) Check that it works with non-ascii UTF-8 characters in filenames
and directories
5) Confirmation button to prevent accidental runs in $HOME for instance.

Thank you to everyone who helped!

-- 
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-13 Thread Martin A. Brown
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Hello,

 : 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 add a few different features to this 'find' to make it a bit 
more resistant to failure, although this sort of solution is always 
subject to the "somebody else is toying with my filesystem race 
condition".

  find "$srcdir" -depth -type f -print0 \
| xargs --null --no-run-if-empty -- \
  mv --target-directory "$dstdir" --

The --target-directory option is only available in GNU mv (and cp), 
I believe.

I'll second the recommendation for 'shutil', although you can have 
some fun playing with recursion by building your tree flattener 
using os.walk(), os.path.split() and os.path.join().

- -Martin

- -- 
Martin A. Brown
http://linux-ip.net/
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: pgf-0.72 (http://linux-ip.net/sw/pine-gpg-filter/)

iD8DBQFLxB5oHEoZD1iZ+YcRAvPUAKCmXcIhKVTUDx4IexWnAvnl64uQNACeOFf/
3tsR/sXGVe944dUMhxkPYkk=
=x0EG
-END PGP SIGNATURE-
___
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-13 Thread Dotan Cohen
> Why is the print below commented out?
>>
>>    for f in file:
>>        toMove =oot + "/" + f
>>        #print toMove
>>        os.rename(toMove, currentDir)
>>

I was testing, and it was not longer needed. It was returning what I
expected. Note that print can only be used to test that the value
matches what the dev expects, not to test that the value matches what
the function wants as we will soon see in the case of currentDir.


> Have you looked at the value of "currentDir" ? Is it in a form that's
> acceptible to os.rename() ?

No, but I though that it was. I had assumed that os.rename would
automatically add the filename as the unix command mv does. Yes, I
know what assumptions make out of me!


> And how about toMove? Perhaps it has two slashes
> in a row in it.

No, toMove was fine.


> When combining directory paths, it's generally safer to use
>
> os.path.join()
>

Thanks!


> 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 have no problem with that. It is unnecessary, but not harmful at
this stage. But thank you for mentioning it, I will get to that.


> 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."
>

Obfuscation! Just kidding, I did not realize that I would be
interacting with "file" as a list of files and not as an individual
file at the time I wrote that. I will revise the variable names, that
is good practice.

Thank you for your patience and advice. I am really enjoying Python
now that I am starting to get the hang of it. I do wish that the docs
had more usage examples but the help on this list and the volume of
information available through google mostly negates that. Thank you
very much.


-- 
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-13 Thread Dotan Cohen
>> 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.

I see. For some reason I was thinking that the tuple was just three
strings. I've slept now and it is clear to me that this is not the
case!


>>> 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.
>

I know that you are right. However, using prints would not have helped
me in my current case of not fully understanding the os.rename
function. I should probably develop as a different user, though, to
prevent "incidents". Thanks for the advice.

-- 
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-13 Thread Dotan Cohen
> from the docs:
> os.rename(src, dst)¶Rename the file or directory src to dst. If dst is a
> directory, OSError will be raised.

I did read that, thank you. That is why I asked how to override, as I
understood that Python was functioning exactly as intended.


> It seems what you wan to
> do is os.rename(toMove, currentDir+f)
>

Well, actually, that would be currentDir+"/"+f but you are correct! I
was thinking too much in terms of the unix mv command, which adds that
automatically. Thank you!


-- 
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