Re: Run on Startup

2009-03-18 Thread Gabriel Genellina
En Tue, 17 Mar 2009 00:16:53 -0200, MRAB   
escribió:

Ian Mallett wrote:
 I'd like to make a program that automatically runs on startup (right  
after login).  How should I do that?



Put it in the folder:

C:\Documents and Settings\\Start Menu\Programs\Startup


Please don't do that, if you care about your users at all, and any of them  
might use a non-English version of Windows. Programs that put anything in  
the above location (or install themselves into C:\Program Files, or things  
like that) are really annoying, and the author is plain stupid.


Any decent installer is able to register a program so it runs on startup  
(InnoSetup, by example). Anyway, if you want to it it yourself, see:

http://msdn.microsoft.com/en-us/library/bb776426(VS.85).aspx

--
Gabriel Genellina

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


Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-18 Thread bieffe62
On Mar 18, 6:20 am, CJ Kucera  wrote:
> Hello list!
>
> I'm having a strange issue, and I'm not entirely certain yet where
> the actual problem is (ie, Python, PyGTK, or gtk+), but I figure I'll
> start here.  Bear with me, this'll probably be a long explanation...
>
> I've been building an app which is meant to be run on both Linux and
> Windows.  It uses PyGTK for its GUI, and the main area of the app is
> a gtk.DrawingArea which I draw on using PyCairo.  I've been developing
> on Linux, and it works great on that platform, with no issues that
> I'm aware of.  When running on Windows, though, the app exhibits the
> following behavior:
>
>   1) When the .py of the main file which runs the application GUI first
>   gets compiled to a .pyc (ie: the first time it's run, or the first
>   time after .py modification), the application runs totally fine, with
>   no apparent problems.
>
>   2) Any attempt AFTER that, the application will start up, *start* to
>   do its data-loading, but then almost immediately crash with an
>   enigmatic "python.exe has generated errors and will be closed by
>   Windows."  When it does so, there is no output whatsoever to the
>   console that the application was launched from, and the crash doesn't
>   always happen in exactly the same place.
>
> The pattern remains the same, though - if the .pyc needs to be compiled,
> the application works fine, but if not: boom.
>
> I've been steadily stripping the program down to what I hoped would be a
> small, reproducible app that I could post here, and I do intend to do so
> still, but it's rather slow going.  For now, I was hoping to see if
> anyone's ever heard of behavior like this before, and might know what
> to do about it, or at least a possible avenue of attack.
>
> As I've been reducing the program down, I've encountered even stranger
> (IMO) behavior...  In one instance, changing a function name seemed to
> make the program work.  I took out the handler which draws my app's
> "About" box, and suddenly my problem went away.  Occasionally I would
> remove a function and the app would suddenly *always* fail with that
> Windows crash error, and I'd have to put the function back in.  Keep
> in mind, these are functions which *aren't being called anywhere.*
>
> Sometimes I could replace a function's entire contents with just "pass"
> and the app would suddenly behave properly, or not behave at all.
>
> It's almost as if whatever's doing the byte-compilation is getting
> screwed up somehow, and really small changes to parts of the file which
> aren't even being touched are having a huge impact on the application as
> a whole.  It's seriously vexing, and certainly the oddest problems I've
> seen in Python.
>
> Windows versions I can reproduce this on: XP and win2k
> Python versions I've reproduced this on:
>   Python 2.5.4 with:
>     PyGTK 2.12.1-2-win32-py2.5
>     PyGObject 2.14.1-1.win32-py2.5
>     PyCairo 1.4.12-1.win32-py2.5
>   Python 2.6.1 with:
>     PyGTK 2.12.1-3-win32-py2.6
>     PyGObject 2.14.2-2.win32-py2.6
>     PyCairo 1.4.12-2.win32-py2.6
> gtk+ 2.12.9-win32-2 (fromhttp://sf.net/projects/gladewin32, which is
> the version linked to from pygtk.org)
>
> The 2.6 Python stuff I've actually only tried on win2k so far, not XP,
> though given my history with this, I suspect that that wouldn't make a
> difference.
>
> Since gtk+ is the one bit of software that hasn't been swapped out for
> another version, I suppose that perhaps that's where the issue is, but
> it seems like Python should be able to at least throw an Exception or
> something instead of just having a Windows crash.  And having it work
> the FIRST time, when the .pyc's getting compiled, is rather suspicious.
>
> Anyway, I'll continue trying to pare this app down to one manageable
> script which I can post here, but until then I'd be happy to hear ideas
> from anyone else about this.
>
> Thanks!
>
> -CJ

It looks like some of the C extension you are using is causing a
segfault or similar in python
interpreter (or it could be a bug in the interpreter itself, but it is
a lot less likely).
I would suggest to fill the startup portion of your code with trace
statements to try to understand which module function is the
troublesome one, then go looking in the big tracking system of the
module, try the newest version and ask on the dedicated mailing list
if any.

Making a small script that cabn reproduce the bug is also a very good
idea, and will help speed-up the problem solution.

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


Re: How to do this in Python?

2009-03-18 Thread bieffe62
On Mar 18, 2:00 am, Jim Garrison  wrote:

>  I don't want "for line in f:" because binary
> files don't necessarily have lines and I'm bulk processing
> files potentially 100MB and larger.  Reading them one line
> at a time would be highly inefficient.
>
> Thanks- Hide quoted text -
>
> - Show quoted text -

For what I know, there are at least two levels of cache between your
application
and the actual file: python interpreter caches its reads, and the
operating system
does that too. So if you are worried about reading efficiently the
file, I think you can stop
worry. Instead, if you are processing files which might not have line
termination at all,
then reading in blocks is the right thing to do.

Ciao

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


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-18 Thread Steven D'Aprano
On Tue, 17 Mar 2009 22:41:26 -0700, John Machin wrote:

> On Mar 18, 4:19 pm, Matt Nordhoff  wrote:

>> The implicit string concatenation is actually done by the compiler; it
>> isn't an operator at all. Look:
>>
>> >>> import dis
>> >>> def f():
>>
>> ...     return "foo" "bar"
>> ...>>> dis.dis(f)
>>
>>   2           0 LOAD_CONST               1 ('foobar')
>>               3 RETURN_VALUE
>> --
> 
> I think you need better evidence than that obtained by proctologising
> about with dis.dis:
> 
> Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> (Intel)] onwin32
> Type "help", "copyright", "credits" or "license" for more information.
 import dis
 def f():
> ...return ('foo') + ('bar')
> ...
 dis.dis(f)
>   2   0 LOAD_CONST   3 ('foobar')
>   3 RETURN_VALUE


That's the keyhole optimizer in action. It replaces operations on two 
literals at compile-time whenever possible. By memory, that was 
introduced by Python 2.4, and implicit string concatenation was 
introduced way back in the mists of time. In Python 2.1 we have this:


>>> dis.dis(compile("'ab' 'cd'", '', 'single'))
  0 SET_LINENO   0

  3 SET_LINENO   1
  6 LOAD_CONST   0 ('abcd')
  9 PRINT_EXPR
 10 LOAD_CONST   1 (None)
 13 RETURN_VALUE
>>>
>>> dis.dis(compile("1+1", '', 'single'))
  0 SET_LINENO   0

  3 SET_LINENO   1
  6 LOAD_CONST   0 (1)
  9 LOAD_CONST   0 (1)
 12 BINARY_ADD
 13 PRINT_EXPR
 14 LOAD_CONST   1 (None)
 17 RETURN_VALUE



I suppose, technically, implicit string concatenation could happen inside 
the lexer, or the parser, or some other process, but I think most people 
are happy to simplify all of those as "the compiler". Whenever it 
happens, the important thing is that it is *not* at runtime. 



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


Re: Run on Startup

2009-03-18 Thread James Matthews
You may also add it to the registry or make it a windows service. Both will
startup.

On Wed, Mar 18, 2009 at 8:09 AM, Gabriel Genellina
wrote:

> En Tue, 17 Mar 2009 00:16:53 -0200, MRAB 
> escribió:
>
>> Ian Mallett wrote:
>>
>>>  I'd like to make a program that automatically runs on startup (right
>>> after login).  How should I do that?
>>>
>>>  Put it in the folder:
>>
>> C:\Documents and Settings\\Start Menu\Programs\Startup
>>
>
> Please don't do that, if you care about your users at all, and any of them
> might use a non-English version of Windows. Programs that put anything in
> the above location (or install themselves into C:\Program Files, or things
> like that) are really annoying, and the author is plain stupid.
>
> Any decent installer is able to register a program so it runs on startup
> (InnoSetup, by example). Anyway, if you want to it it yourself, see:
> http://msdn.microsoft.com/en-us/library/bb776426(VS.85).aspx
>
> --
> Gabriel Genellina
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
http://www.goldwatches.com/

http://www.jewelerslounge.com/
--
http://mail.python.org/mailman/listinfo/python-list


Python 2.6 + Pytz 2009a + Py2exe problem

2009-03-18 Thread Jeff Peck
I've recently upgraded to python 2.6 and I'm having trouble building an
executable using the new Pytz package. The relevant section of setup.py is
below.  This was working under python 2.5 using an ancient version of pytz
(2006p).

 

setup(

console = [dict(script = my_script.py')],

options = dict(

py2exe = dict(

packages = ['pytz', 'pyodbc', 'decimal', 'sqlalchemy'],

typelibs=[('{00020813---C000-0046}', 0, 1, 4)],

 )))

 



When I try to run the resulting executable, I get:

pytz.UnknownTimeZoneError: 'US/Central'

 

I noticed that the old version of pytz I was using compiled each timezone
into a .pyc, and these would be included in the resulting library.zip for my
programs. When I build against the new pytz, these files are no longer
getting compiled to .pyc. Instead, when I check the pytz directory in
library.zip, I see these files:

__init__.pyc

reference.pyc

tzfile.pyc

tzinfo.pyc

 

It appears that the zoneinfo directory is missing. I have tried pasting this
in manually with no luck. Any ideas?

 

Thanks,

Jeff Peck

 

 

 

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


SOAPpy help needed

2009-03-18 Thread somemilk
Hi everyone.

I have a PHP script which works and i need to write the same in Python
but SOAPpy generates a slightly different request and i'm not sure how
to fix it so the server likes it.

The request generated by php script looks like this (removed http's
from links):




*
*
*
6674262970
*



6674262970
false




When trying to make the same using SOAPPy i get this request:




*
*
*
6674262970
*



False
6674262970




A slightly different request but i guess it should work but i get an
error from the server: "Account ID specified in the header does not
match the one specified in the parameter."

But they do match!

The only thing i see is some difference in namespaces, but i have no
idea what to do right now. Please help.
--
http://mail.python.org/mailman/listinfo/python-list


2to3 does not fix FileType from types Module; no replacement ?

2009-03-18 Thread mkieverpy

Hello list,

just tried the new 3.1a1 release and its 2to3 script
on a simple program of mine.
The program uses a type check recipe from activestate.
The recipe uses 'types.FileType' which is no longer present
in 'types' in 3.1a1.
2to3 does not fix and gives no warning.

I found these comments in Misc/HISTORY
3.0a2:
- Removed all types from the 'types' module that are easily accessable
  through builtins.
3.0a1:
- Removed these Python builtins: apply(), callable(), coerce(),
  execfile(), file(), reduce(), reload().

and I could not find a replacement for 'FileType'.
Has this been overlooked?

Found this thread which does not mention FileType:
http://mail.python.org/pipermail/stdlib-sig/2008-April/thread.html#172

I guess I just did not see something simple.
Anyone with a hint?

Regards,
Matthias Kievernagel

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


Re: How to do this in Python?

2009-03-18 Thread Hrvoje Niksic
Luis Zarrabeitia  writes:

> One could use this: 
>
> with open(filename, "rb") as f:
> for buf in iter(lambda: f.read(1000),''):
> do_something(buff)

This is by far the most pythonic solution, it uses the standard 'for'
loop, and clearly marks the sentinel value.  lambda may look strange
at first, but this kind of thing is exactly what lambdas are for.
Judging by the other responses, it would seem that few people are
aware of the two-argument 'iter'.

> but I don't really like a lambda in there. I guess one could use
> functools.partial instead, but it still looks ugly to me. Oh, well,
> I guess I also want to see the canonical way of doing it.

I believe you've just written it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3D CAD -- need collaborators, or just brave souls :)

2009-03-18 Thread Josh Dukes
> VNC style remote control of other seats of the same software so parts
> can be discussed with ease over the phone etc.

It seems like project verse would be really cool to have for this. 
http://verse.blender.org/

-- 

Josh Dukes
MicroVu IT Department
--
http://mail.python.org/mailman/listinfo/python-list


Re: packaging

2009-03-18 Thread Craig Allen

> andrew

thanks andrew, good advice, I should probably use that throughout our
code.

btw, hope the world is treating you well, long time no see...

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


Re: array next pointer

2009-03-18 Thread Duncan Booth
Armin  wrote:

> Could you give an example of next() with a sentinel and describe its
> use case please?  I have a little trouble understanding what you guys
> mean! 

It means you don't have to worry about next() throwing StopIteration. 

e.g.
>>> def pairs(sequence, padding=None):
sequence = iter(sequence)
for a in sequence:
b = next(sequence, padding)
yield a, b


>>> list(pairs('abcd'))
[('a', 'b'), ('c', 'd')]
>>> list(pairs('abcde'))
[('a', 'b'), ('c', 'd'), ('e', None)]


-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can python quickly display results like bash?

2009-03-18 Thread robert song
thank you, it works with -m trace.

As to sh -x, it is used to expand the variables of command, and display it
before executing.

$ info bash
* The `-x' (`xtrace') option displays commands other than simple
 commands when performing an execution trace (*note The Set
 Builtin::).


2009/3/18 Chris Rebert :
> On Tue, Mar 17, 2009 at 11:13 AM, D'Arcy J.M. Cain  wrote:
>> On Tue, 17 Mar 2009 11:10:36 -0700
>> Chris Rebert  wrote:
>>> I've read the manpage for bash and can find no such -x option listed.
>>
>> It's an option from sh(1) that bash copies.  Check the man page for sh
>> (1) for a description.
>
> Ah, I should've thought to google for the sh manpage. Locally, man sh
> just gives me the bash manpage again which doesn't list -x :-(
>
> In answer to the OP's question, you can use the `trace` module
> (http://docs.python.org/library/trace.html):
>
> python -m trace -t somefile.py
>
> will display the lines of source as they are executed.
>
> Cheers,
> Chris
>
> --
> I have a blog:
> http://blog.rebertia.com
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python?

2009-03-18 Thread Tim Chase

  def chunk_file(fp, chunksize=1):
s = fp.read(chunksize)
while s:
  yield s
  s = fp.read(chunksize)


Ah.  That's the Pythonesque way I was looking for.


That's not pythonic unless you really do need to use
chumk_file() in a lot of places (IMO, more than 3 or 4).  If it
only going to be used once, then just do the usual thing:


Different strokes for different folks -- my reuse threshold tends 
towards "more than once".  So even a mere 2 copies of the same 
pattern would warrant refactoring out this idiom.


Thanks also to those in the thread that have modeled the new 
next() sentinel syntax -- nifty.


-tkc




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


Re: Python to Perl transalators

2009-03-18 Thread Andre Engels
On Wed, Mar 18, 2009 at 6:27 AM,   wrote:
>
> -Original Message-
> From: [email protected] [mailto:[email protected]] On Behalf Of Chris Rebert
> Sent: Wednesday, March 18, 2009 10:53 AM
> To: Raju, Abhinayaraj
> Cc: [email protected]
> Subject: Re: Python to Perl transalators
>
> 2009/3/17  :
>> Could anyone suggest whether there is any Python to Perl code convertors?
>> I found one on the net viz. Perthon. But it wasn’t really helping out.
>
> 
> Why on earth would you want to? That'd be like translating Shakespeare
> into a bad rap song!
> 
>
> Cheers,
> Chris
>
> --
> I have a blog:
> http://blog.rebertia.com
>
> :-)
>
> I am just a beginner learning both the languages. Wondered if I can have some 
> comparative understanding of both.

I think automatic translation would be a very bad way of getting
understanding of both. Translating from another language to Python
usually leads to very un-Pythonic code - syntactically correct and
working Python, but not 'real' Python. I assume that translating from
Python to Perl will lead to something similarly un-Perlish.



-- 
André Engels, [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


RE: Python to Perl transalators

2009-03-18 Thread Abhinayaraj . Raju

-Original Message-
From: Andre Engels [mailto:[email protected]] 
Sent: Wednesday, March 18, 2009 3:53 PM
To: Raju, Abhinayaraj
Cc: [email protected]
Subject: Re: Python to Perl transalators

On Wed, Mar 18, 2009 at 6:27 AM,   wrote:
>
> -Original Message-
> From: [email protected] [mailto:[email protected]] On Behalf Of Chris Rebert
> Sent: Wednesday, March 18, 2009 10:53 AM
> To: Raju, Abhinayaraj
> Cc: [email protected]
> Subject: Re: Python to Perl transalators
>
> 2009/3/17  :
>> Could anyone suggest whether there is any Python to Perl code convertors?
>> I found one on the net viz. Perthon. But it wasn’t really helping out.
>
> 
> Why on earth would you want to? That'd be like translating Shakespeare
> into a bad rap song!
> 
>
> Cheers,
> Chris
>
> --
> I have a blog:
> http://blog.rebertia.com
>
> :-)
>
> I am just a beginner learning both the languages. Wondered if I can have some 
> comparative understanding of both.

I think automatic translation would be a very bad way of getting
understanding of both. Translating from another language to Python
usually leads to very un-Pythonic code - syntactically correct and
working Python, but not 'real' Python. I assume that translating from
Python to Perl will lead to something similarly un-Perlish.

-- 
André Engels, [email protected]



Thanks Andre for your suggestions!

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


RE: Python to Perl transalators

2009-03-18 Thread Andreas Tawn
> > 2009/3/17  :

> >> Could anyone suggest whether there is any Python to Perl code convertors?

> >> I found one on the net viz. Perthon. But it wasn't really helping out.

> >

 > I am just a beginner learning both the languages. Wondered if I can have some

> comparative understanding of both.

> 

> I think automatic translation would be a very bad way of getting

> understanding of both. Translating from another language to Python

> usually leads to very un-Pythonic code - syntactically correct and

> working Python, but not 'real' Python. I assume that translating from

> Python to Perl will lead to something similarly un-Perlish.

> 

> --

> André Engels, [email protected]

> 

> Thanks Andre for your suggestions!

 

Maybe something like www.rosettacode.org would be helpful.

 

It has (hopefully) idiomatic implementations of common programming tasks in 
many languages (Python and Perl included).

 

Cheers,

 

Drea

 

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


REDIRECT

2009-03-18 Thread gaeasiankom
Hi !

I'm new to Python. I'm having some difficulties to redirect the pages
using python. Every time when I redirect, the URL changed accordingly.

(Example : from   http://localhost:   to  
http://localhost/hello.py?name='J')

What's the Python way of implementing this ? Can anyone help me out on
this ?



Thanks in advance,

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


objectoriented -?- functional

2009-03-18 Thread Walther Neuper

Hi,

loving Java (oo) as well as SML (fun) I use to practice both of them 
separately.
Now, with Python I would like to combine 'oo.extend()' with 'functional 
map':


Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def reverse_(list):
... """list.reverse() returns None; reverse_ returns the reversed 
list"""

... list.reverse()
... return list
...
>>> ll = [[11, 'a'], [33, 'b']]
>>> l = ll[:]  # make a copy !
>>> l = map(reverse_, l[:])  # make a copy ?
>>> ll.extend(l)
>>> print("ll=", ll)
('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

But I expected to get ...
('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
... how would that elegantly be achieved with Python ?

Cheers, Walther

--

Walther Neuper  Mailto: [email protected]
Institute for Software Technology  Tel: +43-(0)316/873-5728
University of Technology   Fax: +43-(0)316/873-5706
Graz, Austria Home: www.ist.tugraz.at/neuper


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


Re: How to do this in Python?

2009-03-18 Thread Ulrich Eckhardt
Grant Edwards wrote:
>   with open(filename,"rb") as f:
> while True:
>   buf = f.read(1)
>   if not buf: break
>   # do something

The pattern 

  with foo() as bar:
  # do something with bar

is equivalent to 

  bar = foo()
  if bar:
  # do something with bar

except for the calls to __enter__ and __exit__, right? What I was wondering
was whether a similar construct was considered for a while loop or even an
if clause, because then the above could be written like this:

  if open(filename, 'rb') as f:
  while f.read(1000) as buf:
  # do something with 'buf'

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: array next pointer

2009-03-18 Thread Hrvoje Niksic
Armin  writes:

>> Yep, that's what I meant, I forgot the parameter name.
>
> Could you give an example of next() with a sentinel and describe its
> use case please?  I have a little trouble understanding what you
> guys mean!

See the thread about reading the file in chunks.  Instead of:

while True:
chunk = f.read(1024)
if chunk == '':
break
# your processing here

with sentinel iter, you can write:

for chunk in iter(lambda: f.read(1024), ''):
# your processing here
--
http://mail.python.org/mailman/listinfo/python-list


Re: 2to3 does not fix FileType from types Module; no replacement?

2009-03-18 Thread Benjamin Peterson
  tlink.de> schrieb:
> and I could not find a replacement for 'FileType'.
> Has this been overlooked?
> 
> Found this thread which does not mention FileType:
> http://mail.python.org/pipermail/stdlib-sig/2008-April/thread.html#172
> 
> I guess I just did not see something simple.
> Anyone with a hint?

That's because this isn't simple. Python 3 doesn't have one file type. It has
several different kinds of streams in the io module. If you're checking for a
file type, you'll want to use isinstance(some_file, io.IOBase). If you're
inheriting form it, I suggest you look at the io module's classes. 




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


Re: objectoriented -?- functional

2009-03-18 Thread Francesco Guerrieri
On Wed, Mar 18, 2009 at 11:26 AM, Walther Neuper wrote:

> Hi,
>
> loving Java (oo) as well as SML (fun) I use to practice both of them
> separately.
> Now, with Python I would like to combine 'oo.extend()' with 'functional
> map':
>
> Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> def reverse_(list):
> ... """list.reverse() returns None; reverse_ returns the reversed
> list"""
> ... list.reverse()
> ... return list
> ...
> >>> ll = [[11, 'a'], [33, 'b']]
> >>> l = ll[:]  # make a copy !
> >>> l = map(reverse_, l[:])  # make a copy ?
> >>> ll.extend(l)
> >>> print("ll=", ll)
> ('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])
>
> But I expected to get ...
> ('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
> ... how would that elegantly be achieved with Python ?
>


Hi, I will try to give you some comments.

First of all, don't use "list" as a name, since it masks the real list.

When you do l = map(reverse_, l[:]), you are applying reverse_ to each item
of l. A more idiomatic approach would be:
l = [reverse_(sub_list) for sub_list in l] which is called a list
comprehension. There is no need to make a copy of l.

Note that the individual sub lists in l and ll are still the same object in
your example, thus you are reverting also the items of ll.

As a last thing, note that print in python 2.4 (which you are using) is not
a function and so you don't have to put parentheses around what you wish to
print.

Althugh this particular example is much better server by other Pythonic
techniques, for the sake of it, give a look at this small script which is a
rewrite of yours but should be doing what you expected:

def reverse_(a_list):
"""list.reverse() returns None; reverse_ returns the reversed list"""
a_list = a_list[:] # copy the list - try omitting this line and you will
get your previous behaviour
a_list.reverse() # reverse the copied list in place
return a_list # return the reversed list

list_of_lists = [[11, 'a'], [33, 'b']]

l = map(reverse_, list_of_lists)
list_of_lists.extend(l)

print "list_of_lists=", list_of_lists # second half is reverted, first hals
is not.

This will output:
list_of_lists= [[11, 'a'], [33, 'b'], ['a', 11], ['b', 33]]

Hope this helps.

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


split problem if the delimiter is inside the text limiter

2009-03-18 Thread rewonka
Hi,

I had a problem, i would like to process a file into a PSQL, but in
the file the delimiter char is ':'
and i have the same charater inside the text field also.
something like this:
text = 1:23:34:"sample: text":" something"
if I use text.split(':')
it will be ['1', '23', '34', '"sample', 'text"', 'something']
but it's not good for me, because the delimiter is in text limiter  "
"

is it a solution, that is fast to process ? so i don't need to split
by "hand made" function

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


Re: split problem if the delimiter is inside the text limiter

2009-03-18 Thread Tino Wildenhain

rewonka wrote:

Hi,

I had a problem, i would like to process a file into a PSQL, but in
the file the delimiter char is ':'
and i have the same charater inside the text field also.
something like this:
text = 1:23:34:"sample: text":" something"
if I use text.split(':')
it will be ['1', '23', '34', '"sample', 'text"', 'something']
but it's not good for me, because the delimiter is in text limiter  "
"

is it a solution, that is fast to process ? so i don't need to split
by "hand made" function


You should check out the csv module, you can define quote char and
delimiter and this should just work.

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: REDIRECT

2009-03-18 Thread Tino Wildenhain

[email protected] wrote:

Hi !

I'm new to Python. I'm having some difficulties to redirect the pages
using python. Every time when I redirect, the URL changed accordingly.

(Example : from   http://localhost:   to  
http://localhost/hello.py?name='J')

What's the Python way of implementing this ? Can anyone help me out on
this ?


What did you try so far? Hint: the redirect is done via Location: 
HTTP-Header. You can check all details in rfc2616, this is independend
from the language you would use. Everything else depends on the 
framework you use.


Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: split problem if the delimiter is inside the text limiter

2009-03-18 Thread Peter Otten
rewonka wrote:

> I had a problem, i would like to process a file into a PSQL, but in
> the file the delimiter char is ':'
> and i have the same charater inside the text field also.
> something like this:
> text = 1:23:34:"sample: text":" something"
> if I use text.split(':')
> it will be ['1', '23', '34', '"sample', 'text"', 'something']
> but it's not good for me, because the delimiter is in text limiter  "
> "
> 
> is it a solution, that is fast to process ? so i don't need to split
> by "hand made" function

Have a look at http://docs.python.org/library/csv.html.

with open(filename, "rb") as instream:
rows = csv.reader(instream, delimiter=":")
# decode, put into db

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


Creating 50K text files in python

2009-03-18 Thread [email protected]
Hello all,
  I've an application where I need to create 50K files spread
uniformly across 50 folders in python. The content can be the name of
file itself repeated 10 times.I wrote a code using normal for loops
but it is taking hours together for that. Can some one please share
the code for it using Multithreading. As am new to Python, I know
little about threading concepts.

This is my requiremnt:

C:\TestFolder
That folder contains 5 Folders.. Folder1, Folder2, Folder3.
Folder5
Each folder in turn contains 10 folders:
and Each of those folder contains 1000 text files.

Please let me know if you are not clear.

Thank you,
Venu Madhav.


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


Re: REDIRECT

2009-03-18 Thread Jeremiah Dodds
On Wed, Mar 18, 2009 at 10:54 AM,  wrote:

> Hi !
>
> I'm new to Python. I'm having some difficulties to redirect the pages
> using python. Every time when I redirect, the URL changed accordingly.
>
> (Example : from   http://localhost:   to
> http://localhost/hello.py?name='J ')
>
> What's the Python way of implementing this ? Can anyone help me out on
> this ?
>
>
Depends on what framework you're using.

In CherryPy it would be:

raise cherrypy.HTTPRedirect("/hello?name='J'")

What are you using to build your webpage?
--
http://mail.python.org/mailman/listinfo/python-list


Re: objectoriented -?- functional

2009-03-18 Thread Peter Otten
Walther Neuper wrote:

> loving Java (oo) 

Don't mind, weirder things have happened 

http://wiki.muenster.org/index.php/Schwan

> as well as SML (fun) I use to practice both of them 
> separately.
> Now, with Python I would like to combine 'oo.extend()' with 'functional
> map':
> 
> Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> def reverse_(list):
> ... """list.reverse() returns None; reverse_ returns the reversed
> list"""
> ... list.reverse()
> ... return list
> ...
>  >>> ll = [[11, 'a'], [33, 'b']]
>  >>> l = ll[:]  # make a copy !
>  >>> l = map(reverse_, l[:])  # make a copy ?
>  >>> ll.extend(l)
>  >>> print("ll=", ll)
> ('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])
> 
> But I expected to get ...
> ('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
> ... how would that elegantly be achieved with Python ?

Sorry, I cannot infer the pattern. How would you do that at all?

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


Re: [python-list] Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-18 Thread CJ Kucera
[email protected] wrote:
> It looks like some of the C extension you are using is causing a
> segfault or similar in python
> interpreter (or it could be a bug in the interpreter itself, but it is
> a lot less likely).

Okay...  I assume by "C extension" you'd include the PyGTK stuff, right?
(ie: pycairo, pygobject, and pygtk)  Those are the only extras I've got
installed, otherwise it's just a base Python install.

Would a bad extension really cause this kind of behavior though?
Specifically the working-the-first-time and crash-subsqeuent-times?  Do
C extensions contribute to the bytecode generated while compiling?

> I would suggest to fill the startup portion of your code with trace
> statements to try to understand which module function is the
> troublesome one, then go looking in the big tracking system of the
> module, try the newest version and ask on the dedicated mailing list
> if any.

Are you talking about just throwing in various print statements, to find
out where exactly it's dying?  Or I see that there is an actual "trace"
module in Python...  I did do the former awhile ago and didn't find
anything conclusive really.  It was when approaching it from that angle
that I stumbled across the case that if I simply renamed one of my
functions, everything started working again.  I'll do this a bit more
once I've gotten the program down to a more manageable level.

> Making a small script that cabn reproduce the bug is also a very good
> idea, and will help speed-up the problem solution.

Right, that's the goal.  Right now it's still pretty unwieldy, still.
I'll keep on it.

Thanks for the response!

-CJ

-- 
WOW: Flemmy|   "Happiness isn't good enough for me!  I
[email protected]   |  demand euphoria!"
24.24.2.3171   |  - Calvin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating 50K text files in python

2009-03-18 Thread Peter Otten
[email protected] wrote:

> Hello all,
>   I've an application where I need to create 50K files spread
> uniformly across 50 folders in python. The content can be the name of
> file itself repeated 10 times.I wrote a code using normal for loops
> but it is taking hours together for that. Can some one please share
> the code for it using Multithreading. As am new to Python, I know
> little about threading concepts.
> 
> This is my requiremnt:
> 
> C:\TestFolder
> That folder contains 5 Folders.. Folder1, Folder2, Folder3.
> Folder5
> Each folder in turn contains 10 folders:
> and Each of those folder contains 1000 text files.
> 
> Please let me know if you are not clear.
> 
> Thank you,
> Venu Madhav.

I've just tried it, creating the 50,000 text files took 17 seconds on my not
very fast machine. Python is certainly not the bottleneck here.

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


Re: Creating 50K text files in python

2009-03-18 Thread Tim Chase

  I've an application where I need to create 50K files spread
uniformly across 50 folders in python. The content can be the name of
file itself repeated 10 times.I wrote a code using normal for loops
but it is taking hours together for that. Can some one please share
the code for it using Multithreading. As am new to Python, I know
little about threading concepts.


I'm not sure multi-threading will assist much with what appears 
to be a filesystem & I/O bottleneck.  Far more important will be 
factors like


- what OS are you running?

- what filesystem are you writing to and does it journal?  (NTFS? 
 FAT?  ext[234]?  JFS?  XFS?  tempfs/tmpfs?  etc...)  Some 
filesystems deal much better with lots of small files while 
others perform better with fewer-but-larger files.


- how is that filesystem mounted?  Are syncs forced for all 
writes?  How large are your write caches?


- what sort of hardware is it hitting?  A 4500 RPM drive will 
have far worse performance than a 10k RPM drive; or you could try 
against a flash drive or RAID-0 which should have higher 
sustained write throughput.


- how sparse is the drive layout beforehand?  If you're cramped 
for space, the FS drivers have to scatter your files across 
disparate space; while lots of free-space may allow it to spew 
the files without great consideration.


Hope this gives you some ways to test your configuration...

-tkc





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


Re: Creating 50K text files in python

2009-03-18 Thread [email protected]
On Mar 18, 6:16 pm, "[email protected]"
 wrote:
> Hello all,
>           I've an application where I need to create 50K files spread
> uniformly across 50 folders in python. The content can be the name of
> file itself repeated 10 times.I wrote a code using normal for loops
> but it is taking hours together for that. Can some one please share
> the code for it using Multithreading. As am new to Python, I know
> little about threading concepts.
>
> This is my requiremnt:
>
> C:\TestFolder
> That folder contains 5 Folders.. Folder1, Folder2, Folder3.
> Folder5
> Each folder in turn contains 10 folders:
> and Each of those folder contains 1000 text files.
>
> Please let me know if you are not clear.
>
> Thank you,
> Venu Madhav.

-
I managed to get this code but the problem is main thread exits before
the completetion of child threads:

class MyThread ( threading.Thread ):

   def run ( self ):

  global theVar
  print 'This is thread ' + str ( theVar ) + ' speaking.'
  print 'Hello and good bye.'
  theVar = theVar + 1
  time.sleep (4)
  print 'This is thread ' + str ( theVar ) + ' speaking. again'

for x in xrange ( 20 ):
   MyThread().start()
---
I can't use a sleep in the main thread because I don't know when the
child threads end.

Thank you,
venu madhav.




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


Re: Mangle function name with decorator?

2009-03-18 Thread Adam
On Mar 17, 1:49 pm, Aaron Brady  wrote:

> You would need a unique attribute to look for on values in the
> dictionary, which means you'd need to detect what functions you are
> renaming; possibly by using a decorator to mark them.  (Untested:)
>
> class X( metaclass= M ):
>   @mark( 'A' )
>   def foo( ... ).
>
> class M( type ):
>   def __new__( cls, name, bases, namespace ):
>     for k, v in namespace.items():
>       if v.tag== 'mark-A':
>         namespace[ k ]= modification( v )
> or in your case:
>         del namespace[ k ]
>         namespace[ k_mod ]= v.original
>
> possibly also setting func_name as well.
>     return type( name, bases, namespace )

I tried this, but unfortunately, it still doesn't allow me to have two
methods that initially have the same name, because the namespace
dictionary that the metaclass gets only has one one entry, the last
one in.  So, if I try code like this:

def get_only(action):
setattr(action, "get_only", True)

return action

class ControllerMetaclass(type):
def __new__(cls, name, bases, namespace):
print str(namespace)
for k, v in namespace.items():
if hasattr(v, "get_only"):
new_name = "_get_" + k
print "new_name is " + new_name
del namespace[k]
namespace[new_name] = v
v.__name__ = new_name
return type(name, bases, namespace)

class Controller(object):
def __call__(self):
if (self.method != None):
get_mangled_name = "_" + self.method + "_" + self.action
if hasattr(self, get_mangled_name):
method = getattr(self, get_mangled_name)
return method()
else:
if hasattr(self, self.action):
return getattr(self, self.action)()
else:
raise NotImplementedError
else:
if hasattr(self, self.action):
return getattr(self, self.action)()
else:
raise NotImplementedError

class foo_controller(Controller):
__metaclass__= ControllerMetaclass
def __init__(self, action, method = None):
self.action = action
self.method = method


def foo(self):
print "in foo()"

@get_only
def foo(self):
print "in get_only foo()"

def bar(self):
print "in bar()"


a = foo_controller("foo", "get")
a()

b = foo_controller("foo")
print dir(b)
b()

c = foo_controller("bar","get")
c()

The first definition of foo (the one that is not decorated) is over-
written by the second one, and the metaclass never sees it.  So, I
think that I understand what you said about storing the functions in a
hashtable.  That makes sense, as I can keep a reference to the
function even if ti gets over-written in the class main namespace
dictionary.  However, I'm not entirely sure about how to do that with
decorators.  Wouldn't the decorator function have to be bound to a
context to keep a hastable between calls?  I can't us a bound method
as a decorator, can I?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating 50K text files in python

2009-03-18 Thread [email protected]
On Mar 18, 6:35 pm, Peter Otten <[email protected]> wrote:
> [email protected] wrote:
> > Hello all,
> >           I've an application where I need to create 50K files spread
> > uniformly across 50 folders in python. The content can be the name of
> > file itself repeated 10 times.I wrote a code using normal for loops
> > but it is taking hours together for that. Can some one please share
> > the code for it using Multithreading. As am new to Python, I know
> > little about threading concepts.
>
> > This is my requiremnt:
>
> > C:\TestFolder
> > That folder contains 5 Folders.. Folder1, Folder2, Folder3.
> > Folder5
> > Each folder in turn contains 10 folders:
> > and Each of those folder contains 1000 text files.
>
> > Please let me know if you are not clear.
>
> > Thank you,
> > Venu Madhav.
>
> I've just tried it, creating the 50,000 text files took 17 seconds on my not
> very fast machine. Python is certainly not the bottleneck here.
>
> Peter

Really... I just can't beleive it. I am running my scirpt on an
INTEL Core2duo CPU machine with 2GB of RAM. I've started it two hours
back, but still it is running. This is how my code looks like


def createFiles(path):
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Creating files in the
folder "+path+"\n")
global c
global d
os.chdir (path)
k = 1
for k in range (1,1001):
p = "%.2d"%(k)
FName = "TextFile"+c+"_"+d+"_"+p+".txt"
l =1
for l in range(1 , 11):
os.system ("\"echo "+FName+" >> "+FName+"\"")
l = l +1
k = k+1



MainPath = "C:\\Many_5_1KB"
try:
os.mkdir (MainPath)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the base directory
\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" base directory already
exists\n")
os.chdir (MainPath)
for i in range (1 , 6):
j = 1
c = "%.2d"%(i)
FolderName ="Folder"+c
try:
os.mkdir (FolderName)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the folder
"+FolderName+"\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Folder "+FolderName+"
already exists \n")
os.chdir (FolderName)
path = os.getcwd ()
#createFiles(path)
for j in range ( 1 , 11):
d = "%.2d"%(j)
FolderName = "Folder"+c+"_"+d
try:
os.mkdir (FolderName)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the
folder "+FolderName+"\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" the folder
"+FolderName+" exists \n")
os.chdir (FolderName)
path = os.getcwd ()
createFiles(path)
os.chdir ("..")
j = j + 1
os.chdir ("..")
i = i + 1

Can you please let me know where do I have to modify it, to make it
faster.

Thank you,
Venu Madhav
--
http://mail.python.org/mailman/listinfo/python-list


Re: Style formating of multiline query, advise

2009-03-18 Thread John Machin
On Mar 18, 11:25 pm, someone  wrote:
> Hi,
>
> what is good :) style for multiline queries to database?
> Is that one ok?
> query = """ SELECT * FROM (
>                    SELECT a.columna, a.columnb, a.iso
>                       FROM all a
>                       WHERE (a.name = LOWER(%s))  ) AS c
>                  JOIN other as b on c.gid = b.id
>                  WHERE class = 'A'
>                  ORDER BY population DESC
>                  LIMIT %s;"""

There's no tablet of stone, like PEP 8 :-) It seems to be a matter of
individual taste; clarity and consistency and not overdoing
parentheses count for me.

IMO you have too much leading whitespace, you have "as" in upper,
lower and no case, and I'd set out the inner select differently:

query = """
SELECT * FROM (
SELECT a.columna, a.columnb, a.iso
FROM all AS a
WHERE a.name = LOWER(%s)
) AS c
JOIN other AS b ON c.gid = b.id
WHERE class = 'A'
ORDER BY population DESC
LIMIT %s;
"""

Cheers,
John
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating 50K text files in python

2009-03-18 Thread Marco Mariani

[email protected] wrote:


for k in range (1,1001):

  ...

k = k+1


Man, you have a trouble with loops, all over.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python to Perl transalators

2009-03-18 Thread Boris Borcic

Armin wrote:



Why on earth would you want to? That'd be like translating Shakespeare
into a bad rap song!



lol, actually I would prefer a rap song over Shakespeare, so your analogy 
doesn't work there ;)


Why, some people do prefer Perl over Python, so what's wrong with the analogy ?

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


Re: Creating 50K text files in python

2009-03-18 Thread [email protected]
On Mar 18, 6:58 pm, Marco Mariani  wrote:
> [email protected] wrote:
> >     for k in range (1,1001):
>        ...
> >         k = k+1
>
> Man, you have a trouble with loops, all over.

But the situation demands it.:-(
I've to create 5 folders and 10 folders in each of them. Each folder
again should contain 1000 text files.


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


Re: 2to3 does not fix FileType from types Module; no replacement?

2009-03-18 Thread John Roth
On Mar 18, 5:48 am, Benjamin Peterson  wrote:
>   tlink.de> schrieb:
>
> > and I could not find a replacement for 'FileType'.
> > Has this been overlooked?
>
> > Found this thread which does not mention FileType:
> >http://mail.python.org/pipermail/stdlib-sig/2008-April/thread.html#172
>
> > I guess I just did not see something simple.
> > Anyone with a hint?
>
> That's because this isn't simple. Python 3 doesn't have one file type. It has
> several different kinds of streams in the io module. If you're checking for a
> file type, you'll want to use isinstance(some_file, io.IOBase). If you're
> inheriting form it, I suggest you look at the io module's classes.

Nontheless, a warning would be helpful, possibly with some doc.

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


Re: Creating 50K text files in python

2009-03-18 Thread Peter Otten
[email protected] wrote:

> On Mar 18, 6:35 pm, Peter Otten <[email protected]> wrote:
>> [email protected] wrote:
>> > Hello all,
>> > I've an application where I need to create 50K files spread
>> > uniformly across 50 folders in python. The content can be the name of
>> > file itself repeated 10 times.I wrote a code using normal for loops
>> > but it is taking hours together for that. Can some one please share
>> > the code for it using Multithreading. As am new to Python, I know
>> > little about threading concepts.
>>
>> > This is my requiremnt:
>>
>> > C:\TestFolder
>> > That folder contains 5 Folders.. Folder1, Folder2, Folder3.
>> > Folder5
>> > Each folder in turn contains 10 folders:
>> > and Each of those folder contains 1000 text files.
>>
>> > Please let me know if you are not clear.
>>
>> > Thank you,
>> > Venu Madhav.
>>
>> I've just tried it, creating the 50,000 text files took 17 seconds on my
>> not very fast machine. Python is certainly not the bottleneck here.
>>
>> Peter
> 
> Really... I just can't beleive it. I am running my scirpt on an
> INTEL Core2duo CPU machine with 2GB of RAM. I've started it two hours
> back, but still it is running. This is how my code looks like
> 
> 
> def createFiles(path):
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" Creating files in the
> folder "+path+"\n")
> global c
> global d
> os.chdir (path)
> k = 1
> for k in range (1,1001):
> p = "%.2d"%(k)
> FName = "TextFile"+c+"_"+d+"_"+p+".txt"
> l =1
> for l in range(1 , 11):
> os.system ("\"echo "+FName+" >> "+FName+"\"")
> l = l +1
> k = k+1
> 
> 
> 
> MainPath = "C:\\Many_5_1KB"
> try:
> os.mkdir (MainPath)
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the base directory
> \n")
> except:
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" base directory already
> exists\n")
> os.chdir (MainPath)
> for i in range (1 , 6):
> j = 1
> c = "%.2d"%(i)
> FolderName ="Folder"+c
> try:
> os.mkdir (FolderName)
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the folder
> "+FolderName+"\n")
> except:
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" Folder "+FolderName+"
> already exists \n")
> os.chdir (FolderName)
> path = os.getcwd ()
> #createFiles(path)
> for j in range ( 1 , 11):
> d = "%.2d"%(j)
> FolderName = "Folder"+c+"_"+d
> try:
> os.mkdir (FolderName)
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the
> folder "+FolderName+"\n")
> except:
> m.write(strftime("%Y-%m-%d %H:%M:%S") +" the folder
> "+FolderName+" exists \n")
> os.chdir (FolderName)
> path = os.getcwd ()
> createFiles(path)
> os.chdir ("..")
> j = j + 1
> os.chdir ("..")
> i = i + 1
> 
> Can you please let me know where do I have to modify it, to make it
> faster.

Multiple (!) os.system() calls to create a file are certainly a bad idea.
Personally I don't use os.chdir() because I try to avoid global state when
possible.

Here's my somewhat carelessly written code. Change the 

root = ...

line as appropriate before you run it (the root folder must not exist).


from __future__ import with_statement

import os

def makedir(*parts):
dir = os.path.join(*parts)
os.mkdir(dir)
return dir

def makefile(*parts):
fn = os.path.join(*parts)
with open(fn, "w") as out:
out.write((parts[-1]+"\n")*10)

if __name__ == "__main__":
root = "./tmp_tree"
makedir(root)
for i in range(1, 6):
outer = "Folder%d" % i
makedir(root, outer)
for k in range(1, 11):
inner = "Folder%02d" % k
dir = makedir(root, outer, inner)
for i in range(1, 1001):
makefile(dir, "file%04d.txt" % i)

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


Re: Creating 50K text files in python

2009-03-18 Thread Gabriel Genellina
En Wed, 18 Mar 2009 11:40:26 -0200, [email protected]  
 escribió:

On Mar 18, 6:16 pm, "[email protected]"
 wrote:



          I've an application where I need to create 50K files spread
uniformly across 50 folders in python. The content can be the name of
file itself repeated 10 times.I wrote a code using normal for loops
but it is taking hours together for that. Can some one please share
the code for it using Multithreading. As am new to Python, I know
little about threading concepts.


I managed to get this code but the problem is main thread exits before
the completetion of child threads:

class MyThread ( threading.Thread ):

   def run ( self ):

  global theVar
  print 'This is thread ' + str ( theVar ) + ' speaking.'
  print 'Hello and good bye.'
  theVar = theVar + 1
  time.sleep (4)
  print 'This is thread ' + str ( theVar ) + ' speaking. again'

for x in xrange ( 20 ):
   MyThread().start()


You have to .join() each thread. Replace the above two lines with:

threads = [MyThread() for x in range(20)]
for thread in threads:
  thread.start()
for thread in threads:
  thread.join()

But this is unrelated to your main problem. You don't have to use threads  
for this.


--
Gabriel Genellina

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


Re: Creating 50K text files in python

2009-03-18 Thread Marco Mariani

[email protected] wrote:


for k in range (1,1001):

   ...

k = k+1

Man, you have a trouble with loops, all over.


But the situation demands it.:-(


No. I mean, the for loops are wrong.

Compare with the following and see why


import os

base = '/tmp/foo'

for outer in xrange(1,6):
for inner in xrange(1,11):
d = os.path.join(base, str(outer), str(inner))
os.makedirs(d)
for fnum in xrange(1,1001):
file(os.path.join(d,str(fnum)), 'w').close()

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


Re: Creating 50K text files in python

2009-03-18 Thread John Machin
On Mar 19, 12:50 am, "[email protected]"
>         FName = "TextFile"+c+"_"+d+"_"+p+".txt"
>         l =1
>         for l in range(1 , 11):
>             os.system ("\"echo "+FName+" >> "+FName+"\"")
>             l = l +1

That is not the most clear code that I've ever seen. Makeover time!
os.system ("\"echo "+FName+" >> "+FName+"\"")
os.system('"echo ' + FName + ' >> ' + FName + '"')
os.system('"echo %s >> %s"') % (FName, FName)

Some questions:
(1) Isn't that one level of quoting too many?
(2) Does os.system create a whole new shell process to execute that
command?
(3) Can you think of a better/faster way to write 10 lines to a file
than doing os.system in a loop?
(4) Why are you incrementing the loop variable yourself?
(5) Why are you setting the loop variable to 1 before the loop?
(6, 7) as for (4, 5) but this time in relation to the enclosing "for
k..." loop.
(8) Did you actually test that this script was working on some *small*
number of files before you fired it off?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating 50K text files in python

2009-03-18 Thread Gabriel Genellina
En Wed, 18 Mar 2009 11:50:32 -0200, [email protected]  
 escribió:



On Mar 18, 6:35 pm, Peter Otten <[email protected]> wrote:

[email protected] wrote:
> Hello all,
>           I've an application where I need to create 50K files spread
> uniformly across 50 folders in python. The content can be the name of
> file itself repeated 10 times.I wrote a code using normal for loops
> but it is taking hours together for that. Can some one please share
> the code for it using Multithreading. As am new to Python, I know
> little about threading concepts.


I've just tried it, creating the 50,000 text files took 17 seconds on  
my not

very fast machine. Python is certainly not the bottleneck here.


Really... I just can't beleive it. I am running my scirpt on an
INTEL Core2duo CPU machine with 2GB of RAM. I've started it two hours
back, but still it is running. This is how my code looks like


You're creating 50 processes, 50 shells, just to call 'echo' (!)


def createFiles(path):
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Creating files in the
folder "+path+"\n")
global c
global d
os.chdir (path)
k = 1
for k in range (1,1001):
p = "%.2d"%(k)
FName = "TextFile"+c+"_"+d+"_"+p+".txt"
l =1
for l in range(1 , 11):
os.system ("\"echo "+FName+" >> "+FName+"\"")
l = l +1
k = k+1


with open(FName, "w") as f:
  for i in range(10):
f.write(FName)

--
Gabriel Genellina

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


Re: objectoriented -?- functional

2009-03-18 Thread Armin
On Wednesday 18 March 2009 10:19:41 Peter Otten wrote:
> Walther Neuper wrote:
> > loving Java (oo)
>
> Don't mind, weirder things have happened
>
> http://wiki.muenster.org/index.php/Schwan
>

LOL!!

> > as well as SML (fun) I use to practice both of them
> > separately.
> > Now, with Python I would like to combine 'oo.extend()' with 'functional
> > map':
> >
> > Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
> > [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >
> >  >>> def reverse_(list):
> >
> > ... """list.reverse() returns None; reverse_ returns the reversed
> > list"""
> > ... list.reverse()
> > ... return list
> > ...
> >
> >  >>> ll = [[11, 'a'], [33, 'b']]
> >  >>> l = ll[:]  # make a copy !
> >  >>> l = map(reverse_, l[:])  # make a copy ?
> >  >>> ll.extend(l)
> >  >>> print("ll=", ll)
> >
> > ('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])
> >
> > But I expected to get ...
> > ('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
> > ... how would that elegantly be achieved with Python ?
>
> Sorry, I cannot infer the pattern. How would you do that at all?
>
> Peter
> --
> http://mail.python.org/mailman/listinfo/python-list

If you want to reimplement list.extend() (as I understand?) you will have to 
subclass it and change the behaviour.

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


Re: Python to Perl transalators

2009-03-18 Thread Armin
On Wednesday 18 March 2009 11:01:00 Boris Borcic wrote:
> Armin wrote:
> >> 
> >> Why on earth would you want to? That'd be like translating Shakespeare
> >> into a bad rap song!
> >> 
> >
> > lol, actually I would prefer a rap song over Shakespeare, so your analogy
> > doesn't work there ;)
>
> Why, some people do prefer Perl over Python, so what's wrong with the
> analogy ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list

That would mostly apply to people who haven't coded in Python for one reason 
or another.

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


Re: Mangle function name with decorator?

2009-03-18 Thread J. Cliff Dyer
You might be interested in redefining __getattribute__(self, attr) on
your class.  This could operate in conjunction with the hash tables
(dictionaries) mentioned by andrew cooke.  i.e. (untested code):

class C(object):
def __init__(self):
self._get_table = {}
self._post_table = {}

def __getattribute__(self, x):
if self.method=='GET':
return object.__getattribute__(self, _get_table)[x]
elif self.method=='POST':
return object.__getattribute__(self, _post_table)[x]
else:
raise AttributeError
@GET
def foo(x):
return "Got", x
@POST
def foo(x)
return "Posted to", x

This is definitely not functional code, but might get you in the right
direction on __getattribute__.  __getattr__ might also work for you.  I
haven't worked too much with these corners of python.

Cheers,
Cliff

On Tue, 2009-03-17 at 13:54 -0400, andrew cooke wrote:
> ah, ok.  then yes, you can do that with decorators.  you'd need hash
> tables or something similar in a metaclass.  then the decorator would take
> the given function, stick it in the appropriate hash table, and return a
> function that does the dispatch (ie at run time does the lookup from the
> hash table) (you'd only want to set the function once once, so it would
> need to check the function wasn't already defined).  also, the decorator
> would have one name and get/post etc would be an argument.
> 
> this is all documented in the docs and peps, although it's very spread
> around.  you might look at the code for the property decorator - it's not
> doing what you want, but it's an interesting non-trivial example that's
> public.
> 
> http://docs.python.org/library/functions.html#property
> 
> andrew
> 
> 
> Adam wrote:
> > Thanks, Andrew.  I'm trying to accomplish something with a
> > metaprogramming flavor, where, for the convenience of the programmer
> > and the clarity of code, I'd like to have a decorator or some other
> > mechanism do twiddling behind the scenes to make a class do something
> > it wouldn't normally do.
> >
> > Here's a less-obfuscated exmaple:  I want to build a framework for
> > creating MVC web applications in Python (I know, I know, there's
> > already 2^1bazillion of them) where a controller class can have
> > methods that respond to the same action but for different HTTP verbs:
> >
> > class foo_controller(Controller):
> > @GET
> > def new(self):
> > # Display the form to create a new foo
> >
> > @POST
> > def new(self):
> > # Receive a form post with new foo data in it
> >
> > The Controller class will do all of the work behind the scenes to
> > makes sure that the correct method is called at run-time, but for the
> > sake of the programmer, I'd like to supply a clear, friendly syntax
> > like this.  Without a little metaprogramming magic, last-in-wins, and
> > only the second version of foo will end up in the class, so I'd like
> > to mangle the names to something that will result in a unique
> > attribute name and that the Controller class can work with behind the
> > scenes to do the right thing at run time.
> >
> > So, Python experts, am I completely barking up the wrong tree here?
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> >
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

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


Re: [python-list] Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-18 Thread MRAB

CJ Kucera wrote:

[email protected] wrote:

It looks like some of the C extension you are using is causing a
segfault or similar in python
interpreter (or it could be a bug in the interpreter itself, but it is
a lot less likely).


Okay...  I assume by "C extension" you'd include the PyGTK stuff, right?
(ie: pycairo, pygobject, and pygtk)  Those are the only extras I've got
installed, otherwise it's just a base Python install.

Would a bad extension really cause this kind of behavior though?
Specifically the working-the-first-time and crash-subsqeuent-times?  Do
C extensions contribute to the bytecode generated while compiling?


[snip]
One time I was doing some speed tests and I found that the code was
faster the first time it ran, when it had to compile to .pyc. If there
had been caching then it would've been faster after the first time, but
_slower_?

My conclusion was that it was down to memory allocation. If the
interpreter had claimed memory from the OS because it had to compile to
.pyc then more would be immediately available during my timing, hence
higher speed.

So perhaps your problem is that one of the C extensions segfaults unless
more memory is immediately available because the interpreter had to
compile to .pyc.
--
http://mail.python.org/mailman/listinfo/python-list


ANN: eGenix pyOpenSSL Distribution 0.8.1-0.9.8j now also for Mac OS X

2009-03-18 Thread eGenix Team: M.-A. Lemburg


ANNOUNCING

   eGenix.com pyOpenSSL Distribution

 Version 0.8.1-0.9.8j


 An easy to install and use repackaged distribution
   of the pyOpenSSL Python interface for OpenSSL -
  available on Windows, Mac OS X and Unix platforms


This announcement is also available on our web-site for online reading:
http://www.egenix.com/company/news/eGenix-pyOpenSSL-Distribution-0.8.1-0.9.8j-1-GA.html



INTRODUCTION

The eGenix.com pyOpenSSL Distribution includes everything you need to
get started with SSL in Python. It comes with an easy to use installer
that includes the most recent OpenSSL library versions in pre-compiled
form.

pyOpenSSL is an open-source Python add-on (http://pyopenssl.sf.net/)
that allows writing SSL aware networking applications as well as
certificate management tools.

OpenSSL is an open-source implementation of the SSL protocol
(http://www.openssl.org/).

For more information, please see the product page:

http://www.egenix.com/products/python/pyOpenSSL/



NEWS

This new release of the eGenix.com pyOpenSSL Distribution fixes a
serious problem in pyOpenSSL 0.8 related to threaded applications.

The problem causes invalid thread states in the Python interpreter which
then result in random core dumps and seg faults. The patch was provided
by Maxim Sobolev on SourceForge:

http://sourceforge.net/tracker/index.php?func=detail&aid=2543118&group_id=31249&atid=401760

Note that this patch has not yet been integrated into upstream
pyOpenSSL.

We have also fixed several compiler warnings found in the code. The
version of pyOpenSSL you find in the source release has those patches
applied.

Binaries are available for Linux x86 and x64 as well as Windows x86
and Mac OS X PPC/Intel. They include pyOpenSSL and the necessary
OpenSSL libraries.

For Plone users and friends of buildout scripts, we have added
pre-built binaries for Windows. They install just like the Linux
versions and allow easy integration of the archives into buildout
scripts.



DOWNLOADS

The download archives and instructions for installing the package can
be found at:

http://www.egenix.com/products/python/pyOpenSSL/



UPGRADING

Before installing this version of pyOpenSSL, please make sure that
you uninstall any previously installed pyOpenSSL version. Otherwise,
you could end up not using the included OpenSSL libs.

___

SUPPORT

Commercial support for these packages is available from eGenix.com.
Please see

http://www.egenix.com/services/support/

for details about our support offerings.

Enjoy,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Mar 18 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/


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


Re: Creating 50K text files in python

2009-03-18 Thread S Arrowsmith
[email protected]  wrote:
>FName = "TextFile"+c+"_"+d+"_"+p+".txt"
>l = 1
>for l in range(1 , 11):
>os.system ("\"echo "+FName+" >> "+FName+"\"")
>l = l +1

1. os.system spawns a new process, which on Windows (I'm guessing
you're on Windows given the full path names you've given) is a
particularly expensive operation.

2. You really do have a problem with loops. Not how many your
task has landed you, but the way you're writing them. You
don't need to pre-assign the loop variable, you don't need to
increment it, and it's generally clearer if you use a 0-based
range and add 1 when you use the variable if you need it to be
1-based.

So rewrite the above as:

fname = "TextFile%s_%s_%s.txt" % (c, d, p)
f = open(fname, 'a')
for l in range(10):
f.write(fname)
f.close()

Also:

3. All the logging with m.write is likely to be slowing things
down. It might be useful to see just how much of an impact that's
having.

4. Lose the globals and pass them as function arguments.
-- 
\S

   under construction

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


Re: Creating 50K text files in python

2009-03-18 Thread Laurent Rahuel

Hi,

I don't know why you are forking shells using os.system. You should 
either use system commands to do the job or plain python, but mixing 
both should never be an option.


Here is a plain portable python script (using the with statement. Your 
python should not be to old)



from __future__ import with_statement
import os

for i in xrange(1, 1001):
os.mkdir('Folder%s' %i)
for j in xrange(1, 51):
with open('Folder%s%sTextFile%s.txt' %(i, os.sep, j), "w") as f:
f.write('TextFile%s.txt' %j)

Here are the results on a ext3 file system

time python test.py

real0m5.299s
user0m0.939s
sys 0m3.539s

Regards,

Laurent


[email protected] a écrit :

On Mar 18, 6:35 pm, Peter Otten <[email protected]> wrote:

[email protected] wrote:

Hello all,
  I've an application where I need to create 50K files spread
uniformly across 50 folders in python. The content can be the name of
file itself repeated 10 times.I wrote a code using normal for loops
but it is taking hours together for that. Can some one please share
the code for it using Multithreading. As am new to Python, I know
little about threading concepts.
This is my requiremnt:
C:\TestFolder
That folder contains 5 Folders.. Folder1, Folder2, Folder3.
Folder5
Each folder in turn contains 10 folders:
and Each of those folder contains 1000 text files.
Please let me know if you are not clear.
Thank you,
Venu Madhav.

I've just tried it, creating the 50,000 text files took 17 seconds on my not
very fast machine. Python is certainly not the bottleneck here.

Peter


Really... I just can't beleive it. I am running my scirpt on an
INTEL Core2duo CPU machine with 2GB of RAM. I've started it two hours
back, but still it is running. This is how my code looks like


def createFiles(path):
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Creating files in the
folder "+path+"\n")
global c
global d
os.chdir (path)
k = 1
for k in range (1,1001):
p = "%.2d"%(k)
FName = "TextFile"+c+"_"+d+"_"+p+".txt"
l =1
for l in range(1 , 11):
os.system ("\"echo "+FName+" >> "+FName+"\"")
l = l +1
k = k+1



MainPath = "C:\\Many_5_1KB"
try:
os.mkdir (MainPath)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the base directory
\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" base directory already
exists\n")
os.chdir (MainPath)
for i in range (1 , 6):
j = 1
c = "%.2d"%(i)
FolderName ="Folder"+c
try:
os.mkdir (FolderName)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the folder
"+FolderName+"\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Folder "+FolderName+"
already exists \n")
os.chdir (FolderName)
path = os.getcwd ()
#createFiles(path)
for j in range ( 1 , 11):
d = "%.2d"%(j)
FolderName = "Folder"+c+"_"+d
try:
os.mkdir (FolderName)
m.write(strftime("%Y-%m-%d %H:%M:%S") +" Created the
folder "+FolderName+"\n")
except:
m.write(strftime("%Y-%m-%d %H:%M:%S") +" the folder
"+FolderName+" exists \n")
os.chdir (FolderName)
path = os.getcwd ()
createFiles(path)
os.chdir ("..")
j = j + 1
os.chdir ("..")
i = i + 1

Can you please let me know where do I have to modify it, to make it
faster.

Thank you,
Venu Madhav

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


Re: Style formating of multiline query, advise

2009-03-18 Thread Tino Wildenhain

John Machin wrote:

On Mar 18, 11:25 pm, someone  wrote:

Hi,

what is good :) style for multiline queries to database?
Is that one ok?
query = """ SELECT * FROM (
   SELECT a.columna, a.columnb, a.iso
  FROM all a
  WHERE (a.name = LOWER(%s))  ) AS c
 JOIN other as b on c.gid = b.id
 WHERE class = 'A'
 ORDER BY population DESC
 LIMIT %s;"""


There's no tablet of stone, like PEP 8 :-) It seems to be a matter of
individual taste; clarity and consistency and not overdoing
parentheses count for me.

IMO you have too much leading whitespace, you have "as" in upper,
lower and no case, and I'd set out the inner select differently:

query = """
SELECT * FROM (
SELECT a.columna, a.columnb, a.iso
FROM all AS a
WHERE a.name = LOWER(%s)
) AS c
JOIN other AS b ON c.gid = b.id
WHERE class = 'A'
ORDER BY population DESC
LIMIT %s;
"""


And I'd not recomment SELECT * for anything beside test queries in an
interactive session or if you are going to copy tables...

Regards
Tino


smime.p7s
Description: S/MIME Cryptographic Signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python?

2009-03-18 Thread Jim Garrison

Luis Zarrabeitia wrote:

On Tuesday 17 March 2009 06:04:36 pm Jim Garrison wrote:
with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000),''):
do_something(buff)


This is the most pythonic solution yet.

Thanks to all the responders who took time to ponder this seemingly
trivial question.  I learned a lot about the Python mind-set.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mangle function name with decorator?

2009-03-18 Thread R. David Murray
> class foo_controller(Controller):
> __metaclass__= ControllerMetaclass
> def __init__(self, action, method = None):
> self.action = action
> self.method = method
> 
> 
> def foo(self):
> print "in foo()"
> 
> @get_only
> def foo(self):
> print "in get_only foo()"
> 
> def bar(self):
> print "in bar()"

I don't have any wisdom on the metaclass/decorator stuff, but what
about slightly reformulating the interface?  Instead of having the
programmer type, eg:

@GET
def foo(self): pass

@POST
def foo(self): pass

have them type:

def GET_foo(self): pass
def POST_foo(self): pass

It's even one less character of typing (the  :)

--
R. David Murray   http://www.bitdance.com

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


Re: How to do this in Python?

2009-03-18 Thread Mel
Jim Garrison wrote:
> andrew cooke wrote:
>> Jim Garrison wrote:
>>> I'm an experienced C/Java/Perl developer learning Python.
>>> What's the canonical Python way of implementing this pseudocode?
[ ... ]
>> but not doing much binary i/o
>> myself, i suggest:
>> 
>> with open(...) as f:
>>   while (True):
>> buf = f.read(1)
>> if not buf: break
>> ...
[ ... ]

> For a one-off,,your first example would work fine.  See the
> other reply from Tim Chase for a much more Pythonesque
> pattern.  I don't want "for line in f:" because binary
> files don't necessarily have lines and I'm bulk processing
> files potentially 100MB and larger.  Reading them one line
> at a time would be highly inefficient.

It would be more work, but subclassing the file class, with a next method 
yielding the binary record you want would be fairly clean.

Mel.



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


Re: Mangle function name with decorator?

2009-03-18 Thread Adam
On Mar 18, 10:33 am, "J. Cliff Dyer"  wrote:
> You might be interested in redefining __getattribute__(self, attr) on
> your class.  This could operate in conjunction with the hash tables
> (dictionaries) mentioned by andrew cooke.  i.e. (untested code):
>
> class C(object):
>     def __init__(self):
>         self._get_table = {}
>         self._post_table = {}
>
>     def __getattribute__(self, x):
>         if self.method=='GET':
>             return object.__getattribute__(self, _get_table)[x]
>         elif self.method=='POST':
>             return object.__getattribute__(self, _post_table)[x]
>         else:
>             raise AttributeError
>     @GET
>     def foo(x):
>         return "Got", x
>     @POST
>     def foo(x)
>         return "Posted to", x
>
> This is definitely not functional code, but might get you in the right
> direction on __getattribute__.  __getattr__ might also work for you.  I
> haven't worked too much with these corners of python.
>
> Cheers,
> Cliff

Hey, Cliff.  Thanks for sharing this idea.  Unfortunately, providing a
way to actually call the method with the mangled name is relatively
easy, and there are options there.  The real issue, to me, seems to be
finding a way to prevent Python from eating all but the last version
of a function definition in a class.  While decorators are a elegant
and unintrusive approach, I don't believe that there is any way for a
decorator to collection information in a data structure and then
provide that data back to the class instance or the class's metaclass.

I'm beginning to think that I am trying to get the square peg of
Python to fit into the round hole of a .NET idiom.  I am trying to
borrow what I think is a really elegant and useful idiom from ASP.NET
MVC.  Specifically, in an ASP.NET MVC Controller class, I can have two
methods with the same name that are called for different HTTP Verbs by
applying an Attribute:

public ActionResult Payment() {
ViewData["Title"] = "Payment Information";
ViewData["submit_text"] = "Next >";

return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Payment(FormCollection form) {

return RedirectToAction("Legal");
}

Right?  The first Payment method is called when the Payment page is
rendered.  The second is called when the form that it contains is
submitted.  I find it to be readable, elegant and it does not intrude
into the actual logic of the method itself.  The alternatives that I
can readily identify are less-than-optimal.  For instance, if could
just have an if statement inside the body of the method that branches
on the HTTP verb:

def Payment(self):
if self.request.verb == 'GET':
# Do stuff for GET
elif self.request.verb == 'POST':
# So stuff for POST

Yes, it would work, but it is ugly and requires me to mix in the same
function the behaviors for two very separate things.

Or, I could do something like this:

def GET_Payment(self):
# Do stuff for GET

def POST_Payment(self):
# Do stuff for POST

This is trivially-easy to implement (in this case, a metaclass can
very easily manipulate the namespace), but it makes the source code
less appealing and just feels crufty and hacky.  It also makes it
difficult to deal elegantly with having one method respond to more
than verb like I could if I could write:

@GET
@POST
def foo(self):
# Do stuff for page foo, if it is GET or POST; PUT and DELETE not
allowed!


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


Re: Style formating of multiline query, advise

2009-03-18 Thread someone
On Mar 18, 2:51 pm, John Machin  wrote:
> On Mar 18, 11:25 pm, someone  wrote:
>
> > Hi,
>
> > what is good :) style for multiline queries to database?
> > Is that one ok?
> > query = """ SELECT * FROM (
> >                    SELECT a.columna, a.columnb, a.iso
> >                       FROM all a
> >                       WHERE (a.name = LOWER(%s))  ) AS c
> >                  JOIN other as b on c.gid = b.id
> >                  WHERE class = 'A'
> >                  ORDER BY population DESC
> >                  LIMIT %s;"""
>
> There's no tablet of stone, like PEP 8 :-) It seems to be a matter of
> individual taste; clarity and consistency and not overdoing
> parentheses count for me.
>
> IMO you have too much leading whitespace, you have "as" in upper,

thanks for reply.
So, no indentation? query is inside of function, of course.

> lower and no case, and I'd set out the inner select differently:
>
> query = """
>     SELECT * FROM (
>         SELECT a.columna, a.columnb, a.iso
>         FROM all AS a
>         WHERE a.name = LOWER(%s)
>         ) AS c
>     JOIN other AS b ON c.gid = b.id
>     WHERE class = 'A'
>     ORDER BY population DESC
>     LIMIT %s;
> """
>
> Cheers,
> John



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


Re: 2to3 does not fix FileType from types Module; no replacement?

2009-03-18 Thread R. David Murray
John Roth  wrote:
> On Mar 18, 5:48 am, Benjamin Peterson  wrote:
> >   tlink.de> schrieb:
> >
> > > and I could not find a replacement for 'FileType'.
> > > Has this been overlooked?
> >
> > > Found this thread which does not mention FileType:
> > >http://mail.python.org/pipermail/stdlib-sig/2008-April/thread.html#172
> >
> > > I guess I just did not see something simple.
> > > Anyone with a hint?
> >
> > That's because this isn't simple. Python 3 doesn't have one file type. It 
> > has
> > several different kinds of streams in the io module. If you're checking for 
> > a
> > file type, you'll want to use isinstance(some_file, io.IOBase). If you're
> > inheriting form it, I suggest you look at the io module's classes.
> 
> Nontheless, a warning would be helpful, possibly with some doc.

Submitting an issue to the tracker with this request would probably be a
good idea.  Even better if you include a suggested patch.

--
R. David Murray   http://www.bitdance.com

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


smtplib problem with newly rebuilt Debian/lenny system

2009-03-18 Thread cassiope
A hard drive failure forced me to rebuild my main system.  Just a few
things haven't been restored; one of them is a python script which is
used to email users of important events.

In attempting to diagnose the cause, I tried directly executing the
lines inside the python2.5 interpreter:

import  smtplib
s= smtplib.SMTP('localhost')

but the second line causes  a traceback:

File "", line 1, in 
File "/usr/lib/python2.5/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.5/smtplib.py", line 310, in connect
raise socket.error, msg
socket.error: (97, 'Address family not supported by protocol')

This is with exim4 and python2.5 on a newly installed lenny system.
No error messages appear in /var/log or /var/log/exim4 directories.

Helpful clues or pointers to relevant documentation would be
appreciated!

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


Re: Style formating of multiline query, advise

2009-03-18 Thread someone
On Mar 18, 4:04 pm, Tino Wildenhain  wrote:
> John Machin wrote:
> > On Mar 18, 11:25 pm, someone  wrote:
> >> Hi,
>
> >> what is good :) style for multiline queries to database?
> >> Is that one ok?
> >> query = """ SELECT * FROM (
> >>                    SELECT a.columna, a.columnb, a.iso
> >>                       FROM all a
> >>                       WHERE (a.name = LOWER(%s))  ) AS c
> >>                  JOIN other as b on c.gid = b.id
> >>                  WHERE class = 'A'
> >>                  ORDER BY population DESC
> >>                  LIMIT %s;"""
>
> > There's no tablet of stone, like PEP 8 :-) It seems to be a matter of
> > individual taste; clarity and consistency and not overdoing
> > parentheses count for me.
>
> > IMO you have too much leading whitespace, you have "as" in upper,
> > lower and no case, and I'd set out the inner select differently:
>
> > query = """
> >     SELECT * FROM (
> >         SELECT a.columna, a.columnb, a.iso
> >         FROM all AS a
> >         WHERE a.name = LOWER(%s)
> >         ) AS c
> >     JOIN other AS b ON c.gid = b.id
> >     WHERE class = 'A'
> >     ORDER BY population DESC
> >     LIMIT %s;
> > """

Agree, that looks nicer
>
> And I'd not recomment SELECT * for anything beside test queries in an
> interactive session or if you are going to copy tables...

yes, it I know.

Thank you for responce!

>
> Regards
> Tino
>
>  smime.p7s
> 4KViewDownload

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


Re: objectoriented -?- functional

2009-03-18 Thread Gerard Flanagan

Walther Neuper wrote:

 >>> def reverse_(list):
... """list.reverse() returns None; reverse_ returns the reversed 
list"""

... list.reverse()
... return list
...
 >>> ll = [[11, 'a'], [33, 'b']]
 >>> l = ll[:]  # make a copy !
 >>> l = map(reverse_, l[:])  # make a copy ?
 >>> ll.extend(l)
 >>> print("ll=", ll)
('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

But I expected to get ...
('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
... how would that elegantly be achieved with Python ?



Your example isn't clear, but you may be better off using list 
comprehensions and the builtin 'reversed' function (though I don't know 
if it is available in 2.4).


Some code if it helps:

a = [1, 2, 3]
b = [a, a, a]
print [list(reversed(X)) for X in b]
[[3, 2, 1], [3, 2, 1], [3, 2, 1]]

grid = [
[1, 2, 3],
[6, 5, 4],
[7, 8, 9],
]
print [list(reversed(X)) if i % 2 else X for i, X in enumerate(grid)]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

def revers_(seq):
seq = seq[:]
seq.reverse()
return seq

print a
[1, 2, 3]

print revers_(a)
[3, 2, 1]
print a
[1, 2, 3]


print b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
print map(revers_, b)
[[3, 2, 1], [3, 2, 1], [3, 2, 1]]
print b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]


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


Re: Python to Perl transalators

2009-03-18 Thread Nick Craig-Wood
Armin  wrote:
>  On Wednesday 18 March 2009 11:01:00 Boris Borcic wrote:
> > Armin wrote:
> > >> 
> > >> Why on earth would you want to? That'd be like translating Shakespeare
> > >> into a bad rap song!
> > >> 
> > >
> > > lol, actually I would prefer a rap song over Shakespeare, so your analogy
> > > doesn't work there ;)
> >
> > Why, some people do prefer Perl over Python, so what's wrong with the
> > analogy ?
> >
> 
>  That would mostly apply to people who haven't coded in Python for one reason 
>  or another.

Perhaps the OP is looking for something like this

  http://pleac.sourceforge.net/pleac_python/index.html

Which is a sort of Rosetta stone for perl and python ;-)

(The perl cookbook translated into python.)

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mangle function name with decorator?

2009-03-18 Thread Adam
On Mar 18, 11:11 am, "R. David Murray"  wrote:
> I don't have any wisdom on the metaclass/decorator stuff, but what
> about slightly reformulating the interface?  Instead of having the
> programmer type, eg:
>
>     @GET
>     def foo(self): pass
>
>     @POST
>     def foo(self): pass
>
> have them type:
>
>     def GET_foo(self): pass
>     def POST_foo(self): pass
>
> It's even one less character of typing (the  :)
>
> --
> R. David Murray          http://www.bitdance.com

David, would you believe that I just posted about this very idea,  It
doesn't seem to have shown up yet, though.  This idea works from the
perspective of being trivially easy to implement.  I can easily write
a metaclass that looks in the namespace for methods with names that
start with GET_or POST_, or I can override __getattribute__ to do the
look up that way.  However, there are a couple of weaknesses that I
see with this approach.

First, from a purely aesthetic point of view, prepending the desired
verb to the method name just looks a bit ugly.  Also, it makes it
difficult to deal elegantly with avoiding duplicating code when one
piece of logic should dealing with more than one verb.  So, if I want
to have one method that works for GET and POST, I can do this:

def GET_foo(self):
# Do stuff for GET

def POST_foo(self):
return self.GET_foo()

but then I feel like I am cluttering my controller code with unneeded
functions when writing
@GET
@POST
def foo(self):
   # Blah blah blah

would be so much neater.

Or, I could allow method signatures like:
def GET_POST_foo(self):
   # Blah, blah, blah

But now my code to parse and manipulate or do lookups on methods names
is much more complicated.  Also, it introduces difficult ambiguities
in the case that an action of the controller has the same name as an
HTTP Verb.  These ambiguities can be coded around, but doing so makes
the code more-and-more crufty and prone to breakage.  I don't want to
build too much of a Rube Goldberg machine here, right?  What I really
want to do is use Python's metaprogamming facilities to provide an
elegant solution to a problem.  Unfortunately, I really don't think
that it is going to work out in any way that is really satisfying.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mangle function name with decorator?

2009-03-18 Thread Michele Simionato
On Mar 18, 4:18 pm, Adam  wrote:
> Hey, Cliff.  Thanks for sharing this idea.  Unfortunately, providing a
> way to actually call the method with the mangled name is relatively
> easy, and there are options there.  The real issue, to me, seems to be
> finding a way to prevent Python from eating all but the last version
> of a function definition in a class.

As I said in my other post in this thread, this can only be done in
Python 3.X.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-18 Thread bieffe62
On Mar 18, 2:33 pm, CJ Kucera  wrote:
> [email protected] wrote:
> > It looks like some of the C extension you are using is causing a
> > segfault or similar in python
> > interpreter (or it could be a bug in the interpreter itself, but it is
> > a lot less likely).
>
> Okay...  I assume by "C extension" you'd include the PyGTK stuff, right?
> (ie: pycairo, pygobject, and pygtk)  Those are the only extras I've got
> installed, otherwise it's just a base Python install.
>
> Would a bad extension really cause this kind of behavior though?
> Specifically the working-the-first-time and crash-subsqeuent-times?  Do
> C extensions contribute to the bytecode generated while compiling?
>

If you have worked with C/C++, you know that memory-related bugs can
be very tricky.
More than once - working with C code - I had crashes that disappeared
if I just added
a 'printf', because the memory allocation scheme changed and the
memory corrupted was not anymore
relevant.

Ciao

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


Re: SOAPpy help needed

2009-03-18 Thread Piet van Oostrum
> somemilk  (D) wrote:

>D> Hi everyone.
>D> I have a PHP script which works and i need to write the same in Python
>D> but SOAPpy generates a slightly different request and i'm not sure how
>D> to fix it so the server likes it.

>D> The request generated by php script looks like this (removed http's
>D> from links):

>D> 
>D> D> xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/"
>D> xmlns:ns1="marketing.ews.yahooapis.com/V4"
>>> 
>D> 
>D> *
>D> *
>D> *
>D> 6674262970
>D> *
>D> 
>D> 
>D> 
>D> 6674262970
>D> false
>D> 
>D> 
>D> 

>D> When trying to make the same using SOAPPy i get this request:

>D> 
>D> D> SOAP-ENV:encodingStyle="schemas.xmlsoap.org/soap/encoding/"
>D> xmlns:xsi="w3.org/1999/XMLSchema-instance"
>D> xmlns:SOAP-ENV="schemas.xmlsoap.org/soap/envelope/"
>D> xmlns:xsd="w3.org/1999/XMLSchema"
>>> 
>D> 
>D> *
>D> *
>D> *
>D> 6674262970
>D> *
>D> 
>D> 
>D> 
>D> False
>D> 6674262970
>D> 
>D> 
>D> 

>D> A slightly different request but i guess it should work but i get an
>D> error from the server: "Account ID specified in the header does not
>D> match the one specified in the parameter."

>D> But they do match!

>D> The only thing i see is some difference in namespaces, but i have no
>D> idea what to do right now. Please help.

I see two differences. I don't think the namespaces are a problem. The
declarations are in different locations but that should not matter. The
real differences are:
1. The order of the parameters is different. This could well be the
   problem if the server is not paying attention to the parameter names
   but only to the order.
2. The types are included in Python. this is the RPC.encoded convention
   rather than RPC/literal. RPC/encoded is not WS-I compliant and I
   don't think SOAPpy supports it.

Actually I would guess number 1 is the culprit. Maybe you could use a
different SOAP implementation, e.g. suds
(https://fedorahosted.org/suds/). I haven't used it myself but had it
recommended by some of my students who used it for a project.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mangle function name with decorator?

2009-03-18 Thread andrew cooke
Adam wrote:
> Hey, Cliff.  Thanks for sharing this idea.  Unfortunately, providing a
> way to actually call the method with the mangled name is relatively
> easy, and there are options there.  The real issue, to me, seems to be
> finding a way to prevent Python from eating all but the last version
> of a function definition in a class.  While decorators are a elegant
> and unintrusive approach, I don't believe that there is any way for a
> decorator to collection information in a data structure and then
> provide that data back to the class instance or the class's metaclass.

way back i suggested you look at the property decorator.  that does a very
similar job, in that it combines three different methods into one.  it
does that by using a class as decorator and accumulating the methods in
the class.  as a side effect/bonus it has a curious syntax (you could make
this extensible by overriding getattr on the decorator).  so you would end
up with something like:

class Foo:

  @GET or @multi_dispatch('GET') or @multi.GET or something...
  def foo(...):
...

  @foo.POST
  def foo_post(...):
...

  @foo.DELETE
  def foo_delete(...):
...

another idea, which i have never tried, is to look at the metaclass and
intercept duplicate names there (perhaps by substituting the class
dictionary.

you are trying to do very "deep" things that most people do not do with
python.  that does not mean that there are no solutions, just that you
have to find them yourself (especially with the decline of this
newsgroup).

andrew


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


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread Jim Garrison

Jim Garrison wrote:

Luis Zarrabeitia wrote:

On Tuesday 17 March 2009 06:04:36 pm Jim Garrison wrote:
with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000),''):
do_something(buf)


This is the most pythonic solution yet.

Thanks to all the responders who took time to ponder this seemingly
trivial question.  I learned a lot about the Python mind-set.


I just tried the code as given above and it results in an infinite loop.

Since f.read() returns a byte string when in binary mode, the sentinel
has to be b''.  Is there a value that will compare equal to both '' and b''?

It's a shame the iter(o,sentinel) builtin does the
comparison itself, instead of being defined as iter(callable,callable)
where the second argument implements the termination test and returns a
boolean.  This would seem to add much more generality... is
it worthy of a PEP?

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


Re: array next pointer

2009-03-18 Thread R. David Murray
Duncan Booth  wrote:
> Armin  wrote:
> 
> > Could you give an example of next() with a sentinel and describe its
> > use case please?  I have a little trouble understanding what you guys
> > mean! 
> 
> It means you don't have to worry about next() throwing StopIteration. 
> 
> e.g.
> >>> def pairs(sequence, padding=None):
>   sequence = iter(sequence)
>   for a in sequence:
>   b = next(sequence, padding)
>   yield a, b
> 
>   
> >>> list(pairs('abcd'))
> [('a', 'b'), ('c', 'd')]
> >>> list(pairs('abcde'))
> [('a', 'b'), ('c', 'd'), ('e', None)]

That's using the next parameter for its primary purpose: supplying a default.
The sentinel sub-case might be something like this:

SENTINEL = object()
while somecondition:
#do something
val = next(someiterator, SENTINEL)
if val is SENTINEL: break
#do something with val

In most cases you'd refactor code like that into a generator or something,
but there are cases where a pattern like the above can be useful.  The
idea is that next will only return SENTINEL when the iterator is exhausted.
If you didn't use the SENTINEL, then StopIteration would be raised.  So you
could write the above as:

try: val = next(someiterator)
except StopIteration: break

but the version using SENTINEL avoids having the interpreter do the work
of generating a traceback, and is IMO slightly prettier.

I'm sure there are other use cases, too, but probably not a huge number
of them.  Certainly not as many as using the parameter as a default.

--
R. David Murray   http://www.bitdance.com

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


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread Andrii V. Mishkovskyi
On Wed, Mar 18, 2009 at 5:51 PM, Jim Garrison  wrote:
> Jim Garrison wrote:
>>
>> Luis Zarrabeitia wrote:
>>>
>>> On Tuesday 17 March 2009 06:04:36 pm Jim Garrison wrote:
>>> with open(filename, "rb") as f:
>>>    for buf in iter(lambda: f.read(1000),''):
>>>        do_something(buf)
>>
>> This is the most pythonic solution yet.
>>
>> Thanks to all the responders who took time to ponder this seemingly
>> trivial question.  I learned a lot about the Python mind-set.
>
> I just tried the code as given above and it results in an infinite loop.
>
> Since f.read() returns a byte string when in binary mode, the sentinel
> has to be b''.  Is there a value that will compare equal to both '' and b''?
>
> It's a shame the iter(o,sentinel) builtin does the
> comparison itself, instead of being defined as iter(callable,callable)
> where the second argument implements the termination test and returns a
> boolean.  This would seem to add much more generality... is
> it worthy of a PEP?

Just before you start writing a PEP, take a look at `takewhile'
function in `itertools' module. ;)

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



-- 
Wbr, Andrii V. Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
--
http://mail.python.org/mailman/listinfo/python-list


Re: objectoriented -?- functional

2009-03-18 Thread Piet van Oostrum
> Walther Neuper  (WN) wrote:

>WN> Hi,
>WN> loving Java (oo) as well as SML (fun) I use to practice both of them
>WN> separately.
>WN> Now, with Python I would like to combine 'oo.extend()' with 'functional
>WN> map':

>WN> Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
>WN> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
>WN> Type "help", "copyright", "credits" or "license" for more information.
> def reverse_(list):
>WN> ... """list.reverse() returns None; reverse_ returns the reversed
>WN> list"""
>WN> ... list.reverse()
>WN> ... return list
>WN> ...
> ll = [[11, 'a'], [33, 'b']]
> l = ll[:]  # make a copy !
> l = map(reverse_, l[:])  # make a copy ?
> ll.extend(l)
> print("ll=", ll)
>WN> ('ll=', [['a', 11], ['b', 33], ['a', 11], ['b', 33]])

>WN> But I expected to get ...
>WN> ('ll=', [[11, 22], [33, 44], [22, 11], [44, 33]])
>WN> ... how would that elegantly be achieved with Python ?

I guess you later changed 22 to 'a' and 44 to 'b'.

You have made a copy of the outer list with ll[:] (actually twice) but
you have not made copies of the inner lists and these are the important
ones. So the reverse will reverse the *original* inner lists which are
both in ll and in l.

If you still want to do destructive reverses on the inner list you
should make a deep copy:

>>> import copy
>>> l = copy.deepcopy(ll)
>>> l
[[11, 'a'], [33, 'b']]
>>> ll.extend(l)
>>> ll
[[11, 'a'], [33, 'b'], ['a', 11], ['b', 33]]

But IMHO it would be better to use non-destructive reverses:

>>> ll
[[11, 'a'], [33, 'b']]
>>> l = [list(reversed(x)) for x in ll]
>>> l
[['a', 11], ['b', 33]]
>>> ll.append(l)
>>> ll
[[11, 'a'], [33, 'b'], [['a', 11], ['b', 33]]]

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread Jim Garrison

Andrii V. Mishkovskyi wrote:

Just before you start writing a PEP, take a look at `takewhile'
function in `itertools' module. ;)


OK, after reading the itertools docs I'm not sure how to use it
in this context.  takewhile() requires a sequence, and turning
f.read(bufsize) into an iterable requires iter() (no?) which
wants to do its own termination testing.  The following kludge
would subvert iter()'s termination testing but this is starting
to look Perlishly byzantine.

with open(filename, "rb") as f:
for buf in itertools.takewhile( \
lambda b:b, \
iter(lambda: f.read(1000),None)):
do_something(buf)

As opposed to

with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000), lambda b:b)):
do_something(buf)

where iter(callable,callable) is defined to

1) call the first argument
2) pass the returned value to the second argument
3) yield the first result and continue if the return value
   from the second call is True, or terminate if False
--
http://mail.python.org/mailman/listinfo/python-list


Re: Roulette wheel

2009-03-18 Thread Aahz
In article ,
Peter Otten  <[email protected]> wrote:
>mattia wrote:
>>
>> cpop += [nchromosome1] + [nchromosome2]
>
>I'd write that as 
>
>cpop.append(nchromosome1)
>cpop.append(nchromosome2)
>
>thus avoiding the intermediate lists.

You could also write it as

cpop += [nchromosome1, nchromosome2]

which may or may not be faster, substituting one attribute lookup, one
list creation, and one method call for two attribute lookups and two
method calls.  I shan't bother running timeit to check, but I certainly
agree that either your version or mine should be substituted for the
original, depending on one's esthetics (meaning that I doubt there's
enough performance difference either way to make that the reason for
choosing one).
-- 
Aahz ([email protected])   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread S Arrowsmith
Jim Garrison   wrote:
>It's a shame the iter(o,sentinel) builtin does the
>comparison itself, instead of being defined as iter(callable,callable)
>where the second argument implements the termination test and returns a
>boolean.  This would seem to add much more generality... is
>it worthy of a PEP?

class sentinel:
def __eq__(self, other):
return termination_test()

for x in iter(callable, sentinel()):
...

Writing a sensible sentinel.__init__ is left as an exercise

-- 
\S

   under construction

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


Re: Style formating of multiline query, advise

2009-03-18 Thread Scott David Daniels

someone wrote:

what is good :) style for multiline queries to database?...
query = """ SELECT * FROM (
   SELECT a.columna, a.columnb, a.iso
  FROM all a
  WHERE (a.name = LOWER(%s))  ) AS c
 JOIN other as b on c.gid = b.id
 WHERE class = 'A'
 ORDER BY population DESC
 LIMIT %s;"""


Also, for SQL, (A) why are you using nested joins?, and
(B) always show what columns you want as output.  SELECT * is a hack
to make interactive use easier, not a durable way to write queries.

query = """SELECT a.columna, a.columnb, a.iso, b.id, ...
FROM all AS a, other as b
WHERE a.name = LOWER(%s)
 AND  a.gid = b.id
 AND  b.class = 'A'
ORDER BY population DESC
LIMIT %s;"""

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Style formating of multiline query, advise

2009-03-18 Thread Steve Holden
Scott David Daniels wrote:
> someone wrote:
>> what is good :) style for multiline queries to database?...
>> query = """ SELECT * FROM (
>>SELECT a.columna, a.columnb, a.iso
>>   FROM all a
>>   WHERE (a.name = LOWER(%s))  ) AS c
>>  JOIN other as b on c.gid = b.id
>>  WHERE class = 'A'
>>  ORDER BY population DESC
>>  LIMIT %s;"""
> 
> Also, for SQL, (A) why are you using nested joins?, and
> (B) always show what columns you want as output.  SELECT * is a hack
> to make interactive use easier, not a durable way to write queries.
> 
> query = """SELECT a.columna, a.columnb, a.iso, b.id, ...
> FROM all AS a, other as b
> WHERE a.name = LOWER(%s)
>  AND  a.gid = b.id
>  AND  b.class = 'A'
> ORDER BY population DESC
> LIMIT %s;"""

In more modern syntax this would be

SELECT a.columna, a.columnb, a.iso, b.id, ...
FROM all AS a
 JOIN other AS b
   ON a.gid = b.id
 WHERE  a.name = LOWER(%s)
   AND  b.class = 'A'
ORDER BY population DESC
LIMIT %s;"""

or something similar. The fact that you don't write JOIN in your SQL
doesn't mean the database isn't performing a join to perform the query
... I agree the nested query was somewhat over the top.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Want to know? Come to PyCon - soon! http://us.pycon.org/

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


Re: Style formating of multiline query, advise

2009-03-18 Thread someone
On Mar 18, 5:56 pm, Scott David Daniels  wrote:
> someone wrote:
> > what is good :) style for multiline queries to database?...
> > query = """ SELECT * FROM (
> >                    SELECT a.columna, a.columnb, a.iso
> >                       FROM all a
> >                       WHERE (a.name = LOWER(%s))  ) AS c
> >                  JOIN other as b on c.gid = b.id
> >                  WHERE class = 'A'
> >                  ORDER BY population DESC
> >                  LIMIT %s;"""
>
> Also, for SQL, (A) why are you using nested joins?, and

inner select produce smaller set which is then joined with other
table, kind a optimization

> (B) always show what columns you want as output.  SELECT * is a hack
> to make interactive use easier, not a durable way to write queries.

Yes, my original question was about formatting. It's not original
query (only a part).
 But thank you anyway!

>
> query = """SELECT a.columna, a.columnb, a.iso, b.id, ...
>      FROM all AS a, other as b
>      WHERE a.name = LOWER(%s)
>       AND  a.gid = b.id
>       AND  b.class = 'A'
>      ORDER BY population DESC
>      LIMIT %s;"""
>
> --Scott David Daniels
> [email protected]

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


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread Jim Garrison

S Arrowsmith wrote:

Jim Garrison   wrote:

It's a shame the iter(o,sentinel) builtin does the
comparison itself, instead of being defined as iter(callable,callable)
where the second argument implements the termination test and returns a
boolean.  This would seem to add much more generality... is
it worthy of a PEP?


class sentinel:
def __eq__(self, other):
return termination_test()

for x in iter(callable, sentinel()):
...

Writing a sensible sentinel.__init__ is left as an exercise



If I understand correctly, this pattern allows me to create
an object (instance of class sentinel) that implements whatever
equality semantics I need to effect loop termination.  In the
case in point, then, I end up with

class sentinel:
def __eq__(self,other):
return other=='' or other==b''

with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000), sentinel())):
do_something(buf)

i.e. sentinel is really "object that compares equal to both ''
and b''".  While I appreciate how this works, I think the
introduction of a whole new class is a bit of overkill for
what should be expressible in iter()
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread andrew cooke

have you looked in operators?  that might avoid the need for a class.

Jim Garrison wrote:
> S Arrowsmith wrote:
>> Jim Garrison   wrote:
>>> It's a shame the iter(o,sentinel) builtin does the
>>> comparison itself, instead of being defined as iter(callable,callable)
>>> where the second argument implements the termination test and returns a
>>> boolean.  This would seem to add much more generality... is
>>> it worthy of a PEP?
>>
>> class sentinel:
>> def __eq__(self, other):
>> return termination_test()
>>
>> for x in iter(callable, sentinel()):
>> ...
>>
>> Writing a sensible sentinel.__init__ is left as an exercise
>>
>
> If I understand correctly, this pattern allows me to create
> an object (instance of class sentinel) that implements whatever
> equality semantics I need to effect loop termination.  In the
> case in point, then, I end up with
>
>  class sentinel:
>  def __eq__(self,other):
>  return other=='' or other==b''
>
>  with open(filename, "rb") as f:
>  for buf in iter(lambda: f.read(1000), sentinel())):
>  do_something(buf)
>
> i.e. sentinel is really "object that compares equal to both ''
> and b''".  While I appreciate how this works, I think the
> introduction of a whole new class is a bit of overkill for
> what should be expressible in iter()
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread andrew cooke
sorry, ignore that.  hit send before thinking properly.

> have you looked in operators?  that might avoid the need for a class.


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


Re: Mangle function name with decorator?

2009-03-18 Thread R. David Murray
Adam  wrote:
> David, would you believe that I just posted about this very idea,  It
> doesn't seem to have shown up yet, though.  This idea works from the
> perspective of being trivially easy to implement.  I can easily write
> a metaclass that looks in the namespace for methods with names that
> start with GET_or POST_, or I can override __getattribute__ to do the
> look up that way.  However, there are a couple of weaknesses that I
> see with this approach.
> 
> First, from a purely aesthetic point of view, prepending the desired
> verb to the method name just looks a bit ugly.  Also, it makes it
> difficult to deal elegantly with avoiding duplicating code when one
> piece of logic should dealing with more than one verb.  So, if I want
> to have one method that works for GET and POST, I can do this:
> 
> def GET_foo(self):
> # Do stuff for GET
> 
> def POST_foo(self):
> return self.GET_foo()
> 
> but then I feel like I am cluttering my controller code with unneeded
> functions when writing
> @GET
> @POST
> def foo(self):
># Blah blah blah
> 
> would be so much neater.

def GET_foo(self):
#do stuff for GET/POST

POST_foo = GET_foo

> Or, I could allow method signatures like:
> def GET_POST_foo(self):
># Blah, blah, blah
> 
> But now my code to parse and manipulate or do lookups on methods names
> is much more complicated.  Also, it introduces difficult ambiguities
> in the case that an action of the controller has the same name as an
> HTTP Verb.  These ambiguities can be coded around, but doing so makes

Hmm.  I don't follow that.  GET_GET would seem to be unambiguous.

> the code more-and-more crufty and prone to breakage.  I don't want to
> build too much of a Rube Goldberg machine here, right?  What I really
> want to do is use Python's metaprogamming facilities to provide an
> elegant solution to a problem.  Unfortunately, I really don't think
> that it is going to work out in any way that is really satisfying.

Someone else suggested the property model, though that is probably not
as elegant as you want either.

Sohow about the below.  Extending it to handle multiple classes is
left as an exercise for the reader :)  As is extending it to handle
stacking the decorators.

--
R. David Murray   http://www.bitdance.com


registry = {}

def HTTP_decorator_factory(name):
def _(f):
registry['%s_%s' % (name, f.__name__)] = f
def _(self, *args, **kw):
return _dispatch(f.__name__, self, *args, **kw)
return _
return _

def _dispatch(name, self, *args, **kw):
return registry['%s_%s' % (self.mode, name)](self, *args, **kw)

GET = HTTP_decorator_factory('GET')
POST = HTTP_decorator_factory('POST')


class Controller(object):

def __init__(self, mode='GET'):
self.mode = mode

@POST
def foo(self):
print "in POST foo"

@GET
def foo(self):
print "in GET foo"


c1 = Controller(mode='GET')
c2 = Controller(mode='POST')

c1.foo()
c2.foo()

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


How complex is complex?

2009-03-18 Thread Kottiyath
When we say readability counts over complexity, how do we define what
level of complexity is ok?
For example:
Say I have dict a = {'a': 2, 'c': 4, 'b': 3}
I want to increment the values by 1 for all keys in the dictionary.
So, should we do:
>>> for key in a:
...   a[key] = a[key] + 1
or is it Ok to have code like:
dict(map(lambda key: (key, a[key] + 1), a))

How do we decide whether a level of complexity is Ok or not?
--
http://mail.python.org/mailman/listinfo/python-list


Openings for Python Programmer at CA

2009-03-18 Thread Aniket M
Hello ,
This is Aniket from Techclique, a New Jersey based software
development and IT consulting firm providing top quality technical and
software professionals on a permanent and contractual basis to
Government and commercial customer including fortune 500 companies and
most of states and federal governments. I am contacting you to see if
you are comfortable to the skills mentioned below, please forward
resume to this e-mail address [email protected]

Python Programmer (someone with industry knowledge/asset mgt)
Location: Newport Beach, CA (Need Local)
Duration: 6+ months
Note: Communication MUST be perfect!, Plus submit with 3 professional
references

Python programming skills (with web development and dynamically
generated charts/plots in particular) and working knowledge of Linux/
UNIX Shell Scripts and SQL.
Knowledge of Python integration with C/C++ - a definite plus.

Qualifications/Requirements
* 3+ years of Python programming on Linux/Unix platform; recent (2007)
required
* Programming skills building forms, lay-outs, charts, and graphing
required
* Designing, coding, and testing web based applications preferred.
* Strong organizational, oral and written communications skills
* High energy/self starter with the ability to work independently
within the firm*s demanding and highly focused environment


Thanks & Regards,
Aniket
Techclique Inc.
Jersey City, NJ
Email: [email protected]
Yahoo IM : [email protected]
Contact No : 732-357-3844
--
http://mail.python.org/mailman/listinfo/python-list


Disable automatic interning

2009-03-18 Thread George Sakkis
Is there a way to turn off (either globally or explicitly per
instance) the automatic interning optimization that happens for small
integers and strings (and perhaps other types) ? I tried several
workarounds but nothing worked:

>>> 'x' is 'x'
True
>>> 'x' is 'x'+''
True
>>> 'x' is ''+'x'
True
>>> 'x' is 'x'*1
True
>>> 'x' is str('x')
True
>>> 'x' is str('x')+str('')
True
>>> 'x' is str.__new__(str,'x')
True


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


Re: How complex is complex?

2009-03-18 Thread bearophileHUGS
Kottiyath:
> How do we decide whether a level of complexity is Ok or not?

I don't understand your question, but here are better ways to do what
you do:

>>> a = {'a': 2, 'c': 4, 'b': 3}
>>> for k, v in a.iteritems():
...   a[k] = v + 1
...
>>> a
{'a': 3, 'c': 5, 'b': 4}
>>> b = dict((k, v+1) for k, v in a.iteritems())
>>> b
{'a': 4, 'c': 6, 'b': 5}

The first modifies the dict in-place, and the second created a new
dict.

In Python 3 those lines become shorter:

for k, v in a.items():
{k: v+1 for k, v in a.items()}

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-18 Thread Casey Webster
On Mar 18, 1:30 pm, Kottiyath  wrote:
> When we say readability counts over complexity, how do we define what
> level of complexity is ok?
> For example:
> Say I have dict a = {'a': 2, 'c': 4, 'b': 3}
> I want to increment the values by 1 for all keys in the dictionary.
> So, should we do:>>> for key in a:
>
> ...   a[key] = a[key] + 1
> or is it Ok to have code like:
> dict(map(lambda key: (key, a[key] + 1), a))
>
> How do we decide whether a level of complexity is Ok or not?

This isn't just a question of readability; the two expressions are
entirely different. The second expression creates a whole new
dictionary, which might not have been obvious to you given the overall
complexity of the expression. The first expression is simple, clear,
and other than maybe changing "a[key] = a[key] + 1" to "a[key] += 1"
is pretty much hard to improve on.  If the number of lines matters to
you (it shouldn't, be opinions vary), then you could always write:

>>> for k in a: a[k] += 1

Which is shorter and far easier to read than the dict/map/lambda
expression.  And probably what you really intended!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How complex is complex?

2009-03-18 Thread George Sakkis
On Mar 18, 1:30 pm, Kottiyath  wrote:

> When we say readability counts over complexity, how do we define what
> level of complexity is ok?
> For example:
> Say I have dict a = {'a': 2, 'c': 4, 'b': 3}
> I want to increment the values by 1 for all keys in the dictionary.
> So, should we do:>>> for key in a:
>
> ...   a[key] = a[key] + 1
> or is it Ok to have code like:
> dict(map(lambda key: (key, a[key] + 1), a))
>
> How do we decide whether a level of complexity is Ok or not?

The second alternative is:
- unreadable (took me 10 seconds to parse vs 1 for the former).
- slower (makes a function call on every round).
- broken (creates a new dict instead of modifying the original in
place).

Really, there's not much of a dilemma here.

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


Re: How to do this in Python? - A "gotcha"

2009-03-18 Thread Luis Zarrabeitia

Quoting Jim Garrison :

> Jim Garrison wrote:
> > Luis Zarrabeitia wrote:
> >> On Tuesday 17 March 2009 06:04:36 pm Jim Garrison wrote:
> >> with open(filename, "rb") as f:
> >> for buf in iter(lambda: f.read(1000),''):
> >> do_something(buf)
> > 
> > This is the most pythonic solution yet.
> > 
> > Thanks to all the responders who took time to ponder this seemingly
> > trivial question.  I learned a lot about the Python mind-set.
> 
> I just tried the code as given above and it results in an infinite loop.
> 
> Since f.read() returns a byte string when in binary mode, the sentinel
> has to be b''.  Is there a value that will compare equal to both '' and b''?

Thank you for the correction.
It works in python2.5 (on my Debian, at least), but I can see why it doesn't in
python3.

> It's a shame the iter(o,sentinel) builtin does the
> comparison itself, instead of being defined as iter(callable,callable)
> where the second argument implements the termination test and returns a
> boolean.  

A shame indeed. The "takewhile" workaround is way too obfuscated, and to create
a new class only for this purpose is an overkill (and I'd also classify it as
obfuscated).

> This would seem to add much more generality... is
> it worthy of a PEP?

and, it wouldn't need to replace the current sentinel implementation... one
keyword argument, "predicate", would suffice.

+1.

Cheers,

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie


 Participe en Universidad 2010, del 8 al 12 de febrero de 2010
 La Habana, Cuba 
 http://www.universidad2010.cu
 
--
http://mail.python.org/mailman/listinfo/python-list


Disable automatic interning

2009-03-18 Thread R. David Murray
George Sakkis  wrote:
> Is there a way to turn off (either globally or explicitly per
> instance) the automatic interning optimization that happens for small
> integers and strings (and perhaps other types) ? I tried several
> workarounds but nothing worked:

No.  It's an implementation detail.

What use case do you have for wanting to disable it?

--
R. David Murray   http://www.bitdance.com

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


Re: Openings for Python Programmer at CA

2009-03-18 Thread Paul Rudin
Aniket M  writes:


> Note: Communication MUST be perfect!, Plus submit with 3 professional
> references

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


Re: How complex is complex?

2009-03-18 Thread Paul McGuire
You realize of course that these two alternatives are not equivalent.
The first does what your problem statement describes, for each key in
a given dict, increments the corresponding value.  The second creates
an entirely new dict with the modified values.  Even if you were to
write the second one as

a = dict(map(lambda key: (key, a[key] + 1), a))

This would not necessarily accomplish the same effect as the for loop.
If a is an argument to a function, then the for-loop actually updates
the given dict in place, so that the effects of the increment-by-one
for loop will be seen in the caller after this function ends.
However, constructing a new dict and assigning to 'a' only affects the
value of a in the local function - the caller's dict will be
unaffected.

For updating in place, as in your first example, I am hard-pressed to
come up with a simpler form (ah, thank you bearophile for looping over
iteritems instead of the keys).  But if constructing a new dict is an
acceptable approach, then the dict/map/lambda approach you have posted
is functional overkill.  To do the iteration over 'a' that map does
with the lambda, you may as well do with a list comprehension, in far
more readable form:

a = dict((k,v+1) for k,v in a.iteritems())

If you are using Py2.6 or 3.0, you can use the new dict comprehension
form:

a = {k:v+1 for k,v in a.iteritems()}

Would you *really* want to take the position that the map/lambda form
is easier to follow than this?

-- Paul

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


Re: Run on Startup

2009-03-18 Thread Mike Driscoll
On Mar 18, 1:09 am, "Gabriel Genellina" 
wrote:
> En Tue, 17 Mar 2009 00:16:53 -0200, MRAB   
> escribió:
>
> > Ian Mallett wrote:
> >>  I'd like to make a program that automatically runs on startup (right  
> >> after login).  How should I do that?
>
> > Put it in the folder:
>
> > C:\Documents and Settings\\Start Menu\Programs\Startup
>
> Please don't do that, if you care about your users at all, and any of them  
> might use a non-English version of Windows. Programs that put anything in  
> the above location (or install themselves into C:\Program Files, or things  
> like that) are really annoying, and the author is plain stupid.
>
> Any decent installer is able to register a program so it runs on startup  
> (InnoSetup, by example). Anyway, if you want to it it yourself, 
> see:http://msdn.microsoft.com/en-us/library/bb776426(VS.85).aspx
>
> --
> Gabriel Genellina

How is putting your program in "C:\Program Files" stupid? Almost every
program I've ever installed on Windows defaults to that location.
Putting a link in the Startup folder is kind of silly, I will grant
you that. Of course, putting the shortcut there makes it easier for
non-techie people to remove it. Otherwise you'll have to hack the
registry to stop the startup or run msconfig and stop auto-start
there.

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


python equivalent of java technologies

2009-03-18 Thread markolopa
Hello,

When comparing python and java with a colleague who is fan of java,
she challenged me to find in python the equivalent to the following
technologies. Could you please help telling if we have something
equivalent in python or not and how they compare to the java
solutions?

- persistance framework  (~hibernate)
- transational monitor (distributed transaction), XA compliance
(message system, DB) (http://en.wikipedia.org/wiki/X/Open_XA)
- web openess sur le web (web service)
- RIA and web deployable graphic library
- scheduling (quartz)
- secutity (PKI, single sign-on...)
- reporting (Jasper)

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


Re: Openings for Python Programmer at CA

2009-03-18 Thread Mike Driscoll
On Mar 18, 1:09 pm, Paul Rudin  wrote:
> Aniket M  writes:
> > Note: Communication MUST be perfect!, Plus submit with 3 professional
> > references
>
> Irony :)

I have yet to meet anyone with perfect communication. I think this is
an "unfillable" position.

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


Re: python equivalent of java technologies

2009-03-18 Thread Mike Driscoll
On Mar 18, 1:14 pm, markolopa  wrote:
> Hello,
>
> When comparing python and java with a colleague who is fan of java,
> she challenged me to find in python the equivalent to the following
> technologies. Could you please help telling if we have something
> equivalent in python or not and how they compare to the java
> solutions?
>
> - persistance framework  (~hibernate)
> - transational monitor (distributed transaction), XA compliance
> (message system, DB) (http://en.wikipedia.org/wiki/X/Open_XA)

See SqlAlchemy (or any DB_API module like adodb.py or pymssql),
SqlObject, Dabo for DB stuff. See PubSub for one type of messaging:
http://pypi.python.org/pypi/pubsub/1%20to%203

> - web openess sur le web (web service)

Python supports lots of web services. Here are a couple of links:

http://www.ibm.com/developerworks/webservices/library/ws-pyth1.html
http://pywebsvcs.sourceforge.net/

It also has lots of web frameworks, such as Django, TurboGears,
Pylons, Web2Py, Plone/Zope

> - RIA and web deployable graphic library
> - scheduling (quartz)

Maybe this:  
https://blueprints.launchpad.net/nssbackup/+spec/use-python-schedule

> - secutity (PKI, single sign-on...)

I assume this is supposed to be "security". TurboGears can probably do
this:

http://docs.turbogears.org/1.0/RoughDocs/OpenIDWithIdentity

If you do some Googling, it looks like Django does too...

> - reporting (Jasper)

reportlab?

>
> Thanks a lot!
> Marko


Alternatively, you could also use Jython, which can just use the Java
implementations...

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


Re: Disable automatic interning

2009-03-18 Thread George Sakkis
On Mar 18, 2:13 pm, "R. David Murray"  wrote:
> George Sakkis  wrote:
> > Is there a way to turn off (either globally or explicitly per
> > instance) the automatic interning optimization that happens for small
> > integers and strings (and perhaps other types) ? I tried several
> > workarounds but nothing worked:
>
> No.  It's an implementation detail.
>
> What use case do you have for wanting to disable it?

I'm working on some graph generation problem where the node identity
is significant (e.g. "if node1 is node2: # do something) but ideally I
wouldn't want to impose any constraint on what a node is (i.e. require
a base Node class). It's not a show stopper, but it would be
problematic if something broke when nodes happen to be (small)
integers or strings.

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


Re: Run on Startup

2009-03-18 Thread Tim Wintle
On Wed, 2009-03-18 at 11:13 -0700, Mike Driscoll wrote:
> On Mar 18, 1:09 am, "Gabriel Genellina" 
> > Any decent installer is able to register a program so it runs on startup  
> > (InnoSetup, by example). Anyway, if you want to it it yourself, 
> > see:http://msdn.microsoft.com/en-us/library/bb776426(VS.85).aspx
> >
> > --
> > Gabriel Genellina
> 
> How is putting your program in "C:\Program Files" stupid? Almost every
> program I've ever installed on Windows defaults to that location.
> Putting a link in the Startup folder is kind of silly, I will grant
> you that. Of course, putting the shortcut there makes it easier for
> non-techie people to remove it. Otherwise you'll have to hack the
> registry to stop the startup or run msconfig and stop auto-start
> there.

The reason is that on non-english language versions it's not called "C:
\Program Files" - there's a system setting that installers use to know
the correct place to put applications. You should ask windows where it
thinks you should put applications, and then put it there.

The "Startup" folder is (I believe) depricated, and I would have guessed
that it's not called "Startup" in other languages.

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

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


Re: split problem if the delimiter is inside the text limiter

2009-03-18 Thread rewonka
On márc. 18, 14:10, Peter Otten <[email protected]> wrote:
> rewonka wrote:
> > I had a problem, i would like to process a file into a PSQL, but in
> > the file the delimiter char is ':'
> > and i have the same charater inside the text field also.
> > something like this:
> > text = 1:23:34:"sample: text":" something"
> > if I use text.split(':')
> > it will be ['1', '23', '34', '"sample', 'text"', 'something']
> > but it's not good for me, because the delimiter is in text limiter  "
> > "
>
> > is it a solution, that is fast to process ? so i don't need to split
> > by "hand made" function
>
> Have a look athttp://docs.python.org/library/csv.html.
>
> with open(filename, "rb") as instream:
>     rows = csv.reader(instream, delimiter=":")
>     # decode, put into db
>
> Peter

Thanks foe the answers.

I tried that module it's work fine.
Now i stucked when i tried to pu into db.
Because i have some cell that is in somekind of unicoded text, and i'm
looking a solution how to put this into db (my db in utf-8 format).
Im using the pgdb modul, something like this:

# connection is an object from pgdb class

sql = ''' INSERT INTO table (column1,column2, ...) VALUES ( %s,
%s, ); '''
rows = csv.reader(open("sample.csv","rb"), delimiter=":",
quotechar='"')
for row in rows:
connection.cursor.execute(sql % (row[0],row[1],))
connection.corsur.commit()

but something binary in a cell, the pgdb says it is not in utf-8
format, or something like this.
I know it's a newbie solution :))
better solution?

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


  1   2   >