Business issues regarding adapting Python

2009-09-27 Thread Nash
Hello everyone,

I'm a big time python fan and it has helped me write code fast and
push it out quickly. We have a medium sized telecom product written
90% in Python and 10% in Java. The problem is, in the place where we
work (Pakistan), we can't find Python developers. I asked HR to send
me figures on how many people do we have available who have worked
with C++, Java, PHP and Python with 2-3 years of experience. They did
a search on available candidates on Pakistan's biggest jobsite and
this is what they sent:

Language: Available Candidates in Pakistan (Available Candidates in
our city)
Java: 2020 (750)
C++: 1540 (650)
PHP: 630 (310)
Python: 25 (4)

Almost no-one shows up with Python experience when we put out a job
opening and now it is becoming a real hurdle. Despite our liking and
cost savings with the language, we are thinking about shifting to
Java.

1. Have any of you faced a similar issue? How did you resolve it?
2. Do you think it makes sense to hire good programmers and train them
on Python?
3. If we do train people in Python for say a month; are we just
creating a team of mediocre programmers? Someone who has worked with
Python for over an year is much different than someone who has worked
with Python for only a month.
4. Any suggestions or idea? Related posts, articles etc would
certainly help!

I know that going Java will probably mean a 3x increase in the number
of people that we have and require time for Python component
replacement with Java ones. But for Business Continuity sake,
management doesn't mind.


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


Re: unexplainable python

2009-09-27 Thread Matt Joiner
Yes the needless use of classes further supports that theory.

On Sun, Sep 27, 2009 at 4:31 PM, John Nagle  wrote:
> dads wrote:
>>
>> Sorry forgot to mention I'm using python 2.6
>
>   This looks like a homework assignment.
>
>                                John Nagle
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Business issues regarding adapting Python

2009-09-27 Thread Roger Binns
Nash wrote:
> 3. If we do train people in Python for say a month; are we just
> creating a team of mediocre programmers? Someone who has worked with
> Python for over an year is much different than someone who has worked
> with Python for only a month.

In my experience the best way to "train" new developers is to have them work
on porting, maintenance, bug fixing, testing etc of your product.  This way
they get exposed to your code, methodologies, quirks and values (eg
security, internationalization, test coverage etc).  It only needs to happen
for a few months and has them in a position where they will do little harm
to the main development.  You'll also get a good idea where they will be
best deployed.

If your existing code base is a good example for the new developers to
follow then this should work very well even for developers new to Python
(but competent in other languages).

> 4. Any suggestions or idea? Related posts, articles etc would
> certainly help!

Start a user group:

  http://wiki.python.org/moin/LocalUserGroups

There is a Pakistan Linux User's Group and so should be some affinity and
overlap with them.

> I know that going Java will probably mean a 3x increase in the number
> of people that we have and require time for Python component
> replacement with Java ones. But for Business Continuity sake,
> management doesn't mind.

Or you could offer to pay Python developers more, and make it known that is
happening.  You'll soon find some more supply :-)

Hopefully your next question will be about interviewing Python developers ...

Roger

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


Re: Business issues regarding adapting Python

2009-09-27 Thread Gregor Horvath
Hi,

Am Sun, 27 Sep 2009 00:13:47 -0700 (PDT)
schrieb Nash :

> I'm a big time python fan and it has helped me write code fast and
> push it out quickly. We have a medium sized telecom product written
> 90% in Python and 10% in Java. The problem is, in the place where we
> work (Pakistan), we can't find Python developers. I asked HR to send

[...]

> 4. Any suggestions or idea? Related posts, articles etc would
> certainly help!

http://www.paulgraham.com/pypar.html

--
Gregor
http://gregor-horvath.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Got undefined symbol: _PyUnicodeUCS2_AsDefaultEncodedString on OpenSuSE 11.1

2009-09-27 Thread Mark Dickinson
On Sep 25, 10:13 pm, Alejandro Valdez 
wrote:

> The compiler issued some warnings, two of them (related to Unicode)
> called my attention:
>
> In file included from Python/formatter_unicode.c:13:
> Python/../Objects/stringlib/formatter.h: In function
> ‘unknown_presentation_type’:
> Python/../Objects/stringlib/formatter.h:35: warning: format ‘%c’
> expects type ‘int’, but argument 3 has type ‘Py_UNICODE’

I only see one warning here, not two.  But it should definitely
be fixed.

>
> Should I ignore them?
>
> Other warning messages:
> [...]

I *think* all of these warnings are benign, though the source should
really be corrected if necessary to silence them (some of them, like
the _struct.c one, have already been fixed in svn).  I'll take a
closer
look at them, though.  Thanks for reporting these!

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


Re: Business issues regarding adapting Python

2009-09-27 Thread Krishnakant
On Sun, 2009-09-27 at 00:13 -0700, Nash wrote:
> Hello everyone,
> Salam Valicum.
I am Krishnakant from Mumbai India and in our country case was exactly
the same but now pritty different in just a matter of few years.
I have answered your queries inline with your questions so read on. 
> I'm a big time python fan and it has helped me write code fast and
> push it out quickly. We have a medium sized telecom product written
> 90% in Python and 10% in Java. The problem is, in the place where we
> work (Pakistan), we can't find Python developers. I asked HR to send
> me figures on how many people do we have available who have worked
> with C++, Java, PHP and Python with 2-3 years of experience. They did
> a search on available candidates on Pakistan's biggest jobsite and
> this is what they sent:
> 
> Almost no-one shows up with Python experience when we put out a job
> opening and now it is becoming a real hurdle. Despite our liking and
> cost savings with the language, we are thinking about shifting to
> Java.

Well, As I said this was a case in India just a few years back.
But now we pritty well find programmers in languages like python or ruby
or php.  The main reason for such growth in the number of python
programmers is the awareness people like myself create amongst the
masses of new computer programmres.
Moreover the industry itself is slowly realising the time and resulting
cost saving by making use of python.


> 1. Have any of you faced a similar issue? How did you resolve it?
I currently lead the development of an accounting software called
GNUKhata ( http://gnukhata.gnulinux.in ) which is totally developed in
python.
Let me tell you that none of the developers working on this project were
python programmers.
It took me about 15 days to train them till intermediate level.  The
trick here is to take good programmers who have good logical sence and
have the fundamental idea of programming in some other language.  For
example the coordinator of this project knew .net pritty well and today
she is a much better python programmer than me.
> 2. Do you think it makes sense to hire good programmers and train them
> on Python?
Exactly.  Look at my anser to your question number 1.
> 3. If we do train people in Python for say a month; are we just
> creating a team of mediocre programmers? Someone who has worked with
> Python for over an year is much different than someone who has worked
> with Python for only a month.
Firstly, that's true with all programming languages.  Whether you will
create good efficient programmers in python depends on how well you use
my trick of taking good programmers proficient in any other language,
and yes they should be open to learning new languages.
May I repeat, including GNUKhata, the accounting software I lead, all
the projects I worked on, we hardly had python programmres.  But we
trained them by making them do porting work and also do some bug fixing.
So the other hack is that you make the new python programmres port the
code from let's say some module in Java.  Now if you hire java
programmers and decide to train them in python for example, then this
trick works very well because they alredy know the language of the
source module and now portint it to python.

Now due to many such projects being done in huge companies in India, we
have a good repository of python programmers.
Remember that popularity of a programming language directly depends on
how much you involve the programmers.

> 4. Any suggestions or idea? Related posts, articles etc would
> certainly help!
> I think you can find that your self.

> I know that going Java will probably mean a 3x increase in the number
> of people that we have and require time for Python component
> replacement with Java ones. But for Business Continuity sake,
> management doesn't mind.
> 

Well, If you take my words as coming from an experienced person, I
highly recommend you to believe in what your inner feelings say about
this decision.
If you feel python will svae cost and time then just go ahead and train
programmers in python.  From your own experience you might have realised
that trainning people in python for a month will be not as bad as
trainning new programmers in java for a month.
I mean the learning curve is very very narrow in python.

Khuda Hafiz.
Krishnakant.


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


Unable to create scrollable editor window using curses

2009-09-27 Thread Andy Elvey

Hi all -

I've been trying to create a simple text editor using the curses module.
Doing the basic editor functionality is easy enough (thanks to the 
built-in textpad library), but I'm having real problems getting the text 
to scroll up when the bottom of the page is reached. 

When the user is on the bottom line of the window, the text should 
scroll when they press Enter (or when they hit the end of the bottom line).


This is my code as it is at present (apologies if the code gets mangled 
in the email. )
By the way, if anything in this seems "odd", my answer to the question 
"why did you do this?"  is - 
"because I didn't know any better ;)   " 



*** Start of code *
import sys, curses, curses.wrapper, curses.ascii, curses.textpad, \
  traceback, string, os
   
def draw(): 
 # Start at top-left of screen  
 x = y = 0  
 # Create a new window

 mywin = curses.newwin(23, 80, y, x)
 mywin.idlok(1)
 mywin.scrollok(1)
 mywin.setscrreg(1, 20)  
 mywin.box() 
 mywin.addstr(0, 25, "*** P.D. Editor ***")  
 mywin.refresh()


 # A subwindow. This stops us from over-writing the main window. 
 mywin2 = mywin.subwin(21, 77, y+1, x+1)   
 mywin2.refresh()


 # An editor in the subwindow.
 myeditor = curses.textpad.Textbox(mywin2)
 myeditor.edit()
 # Get the position of the cursor and see if we need to scroll the text.
 pos = mywin2.getyx()
 if pos[0] >= 20:   
mywin2.scroll(1)

 else:
pass
 
def main(stdscr): 
   while 1:

 draw()
  
  
#  Run the code from the command-line
if __name__ == '__main__': 
 try:
stdscr = curses.initscr()  
curses.noecho() ; curses.cbreak()

stdscr.keypad(1)
main(stdscr)  # Enter the main loop
# Set everything back to normal
stdscr.keypad(0)
curses.echo() ; curses.nocbreak()
curses.endwin()  # Terminate curses
 except:
# In the event of an error, restore the terminal
# to a sane state.
stdscr.keypad(0)
curses.echo() ; curses.nocbreak()
curses.endwin()
traceback.print_exc()  # Print the exception

  End of code ** 

I've tried all sorts of things, including having all kinds of 
contortions with pads, subpads and so on.  Nothing has worked, so any 
help with this (so that the editor can be made to scroll the text) would 
be very gratefully received.  Many thanks in advance -

- Andy

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


Detecting changes to a dict

2009-09-27 Thread Steven D'Aprano
I'm pretty sure the answer to this is No, but I thought I'd ask just in 
case... 

Is there a fast way to see that a dict has been modified? I don't care 
what the modifications are, I just want to know if it has been changed, 
where "changed" means a key has been added, or deleted, or a value has 
been set. (Modifications to mutable values aren't important.) In other 
words, any of these methods count as modifying the dict:

__setitem__
__delitem__
clear
pop
popitem
setdefault
update

Of course I can subclass dict to do this, but if there's an existing way, 
that would be better.


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


Re: removing a post

2009-09-27 Thread Sjoerd Mullender
You need to put this request to [email protected].  As mailing list 
administrator I have no access to the archives.


On 2009-09-26 05:32, Mike L wrote:

hello
could you remove this old post, off topic and spam

http://www.mail-archive.com/[email protected]/msg175722.html

thank you


We are your photos. Share us now with Windows Live Photos.




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


Re: Most "active" coroutine library project?

2009-09-27 Thread Piet van Oostrum
> Grant Edwards  (GE) wrote:

>GE> On 2009-09-25, Piet van Oostrum  wrote:
 [email protected] (e) wrote:
>>> 
>e> I specifically left out all "yield" statements in my version, since that's
>e> exactly the point here. :)  With "real" coroutines, they're not necessary -
>e> coroutine calls look just like any other call.  With Python's enhanced
>e> generators, they are.
>>> 
>>> The first time I encountered coroutines was in Simula-67. Coroutine
>>> switching was certainly explicit there. IIRC, the keyword was resume.

>GE> I'm not sure exactly what "coroutine calls" refers to, but the
>GE> "mis-feature" in Python co-routines that's being discussed is
>GE> the fact that you can only yeild/resume from the main coroutine
>GE> function.

Yes, I know, but the discussion had drifted to making the yield
invisible, if I understood correctly.

>GE> You can't call a function that yields control back to the other
>GE> coroutine(s).  By jumping through some hoops you can get the
>GE> same effect, but it's not very intuitive and it sort of "feels
>GE> wrong" that the main routine has to know ahead of time when
>GE> calling a function whether that function might need to yield or
>GE> not.

I know. I think this is an implementation restriction in Python, to make
stack management easier. Although if you would lift this restriction
some new syntax would have to be invented to distinguish a generator
from a normal function.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


unicode text file

2009-09-27 Thread Junaid
I want to do replacements in a utf-8 text file. example

f=open("test.txt","r") #this file is uft-8 encoded

raw = f.read()
txt = raw.decode("utf-8")

txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
but not working

f.write(txt)
f.close()
f.flush()


please, help me

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


Re: unexplainable python

2009-09-27 Thread dads
Thank you for the help, it's amazing what you can't spot. It seems the
harder you look the less likely you're to find the issue. Fresh eyes
make the world of difference.

To Matt and John:

No this certainly isn't homework, I'm 29 and in full time work. I
decided to learn to program about a year ago and picked up python, so
it's one of my hobbies. Starting from level 0 it's been challenging
and fun.

This exercise was just a bit of fun, I got the idea from a forum. I'm
using classes to help me solidify how they work. Unfortunately I don't
have the experience to know that this is a bad place to use them.

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


Re: unicode text file

2009-09-27 Thread Vlastimil Brom
2009/9/27 Junaid :
> I want to do replacements in a utf-8 text file. example
>
> f=open("test.txt","r") #this file is uft-8 encoded
>
> raw = f.read()
> txt = raw.decode("utf-8")
>
> txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> but not working
>
> f.write(txt)
> f.close()
> f.flush()
>
>
> please, help me
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Does
txt.replace('English', ur'ഇംഗ്ലീഷ്')
instead of
txt.replace{'English', ur'ഇംഗ്ലീഷ്')

fix the problem?

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


Re: Doubley imported module caused devastating bug

2009-09-27 Thread Francis Carr
> I would like to propose that it be made impossible in the Python
> source to import two instances of the same module.

A fully-automatic solution is more difficult than it might seem at
first:
  http://www.python.org/dev/peps/pep-0328/
But there is a simple code-discipline solution: never ever use
relative imports, even between code in the same package.

We got "bit" by double-imports (a mix of relative, absolute, and even
cross-imports A "import B" and B "import A") early on in one of our
projects.  The symptom was that the imported module would be
initialized *twice*, once for a relative import and once for an
absolute.  This is not a happy situation for pseudo-singletons like
the "logging" module --- esp. if one is hacking the internals! :-)  We
no longer use relative imports *EVER*, even within the same package.

Perhaps Perforce is doing something tricky with scoping or importing,
and you've just managed to stumble across this trickiness because of a
double-import.  Good luck, these things are a bugger to debug.
-- 
http://mail.python.org/mailman/listinfo/python-list


pep370 python 2.6?

2009-09-27 Thread Neal Becker
Is pep370 (per-user site-packages) available on 2.6?

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


user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Stef Mientki

hello,

I've a Python desktop application, running under Widows, that stores the 
data in a central SQLite database.


Depending on the user login on the system, some of the data from the 
database is allowed and other data is forbidden.


I can read the current logged in user.
The authorization for each user is stored encrypted in the database.
The program is delivered as pyc files,
but from what I read, these can easily be reversed engineered.

There is even an encrypted version of SQLite (not freeware),
but as long as test the authorization in Python,
it doesn't seem to be a good protection.

So at first thought, a better way might be the following process:
- encrypt the whole database
- run all queries through a DLL written in C or Delphi, where also the 
authorization is checked


But on second thought that isn't true.
The login on the computer is assumed to be enough (1-time login).
So the Python program passes the username to the external DLL.
And there's the weak point again,
the username of every person in our organization is known to every one.
So it shouldn't be too difficult to modify the program,
so it will always use the external DLL with the username with the 
highest authorization.


So the question remains:
Is it possible to create a Python Desktop application,
with user authorization, based on the login of the user ?

thanks,
Stef Mientki
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode text file

2009-09-27 Thread MRAB

Junaid wrote:

I want to do replacements in a utf-8 text file. example

f=open("test.txt","r") #this file is uft-8 encoded

raw = f.read()
txt = raw.decode("utf-8")

txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
but not working


txt = txt.replace{'English', ur'ഇംഗ്ലീഷ്')


f.write(txt)
f.close()
f.flush()


The file will be flushed when it's closed, and flushing it after closing 
is meaningless.



please, help me

thanks


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


Re: unicode text file

2009-09-27 Thread Mark Tolonen


"Junaid"  wrote in message 
news:0267bef9-9548-4c43-bcdf-b624350c8...@p23g2000vbl.googlegroups.com...

I want to do replacements in a utf-8 text file. example

f=open("test.txt","r") #this file is uft-8 encoded
raw = f.read()
txt = raw.decode("utf-8")


You can use the codecs module to open and decode the file in one step



txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
but not working


The replace method returns the altered string.  It does not modify it in 
place.  You also should use Unicode strings for both the arguments (although 
it doesn't matter in this case).  Using a raw Unicode string is also 
unnecessary in this case.


   txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')


f.write(txt)


You opened the file for writing.  You'll need to close the file and reopen 
it for writing.



f.close()
f.flush()


Flush isn't required.  close() will flush.

Also to have text like ഇംഗ്ലീഷ് in a file you'll need to declare the 
encoding of the file at the top and be sure to actually save the file in the 
encoding.


In summary:

   # coding: utf-8
   import codecs
   f = codecs.open('test.txt','r','utf-8')
   txt = f.read()
   txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
   f.close()
   f = codecs.open('test.txt','w','utf-8')
   f.write(txt)
   f.close()

-Mark


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


Re: Business issues regarding adapting Python

2009-09-27 Thread Nash
On Sep 27, 4:13 pm, "Martin P. Hellwig" 
wrote:
> Nash wrote:
>
> 
> I think normal market rules will apply to Pakistan too, if your desired
> trade has not the quantity you wish, the price per item should get
> higher. Net result should be that more quantity will be available due to
> increased interest.
>
> --
> MPHhttp://blog.dcuktec.com
> 'If consumed, best digested with added seasoning to own preference.'

If I rephrase the question: In an absense of steady Python Developers;
can there be a viable strategy involving training? Or will it be much
safer going with an already common developer pool.

Please note that my goal is not to promote python but to make a sound
business decision. Using Python is not an absolute requirement.

I appreciate all the feedback thus far, please keep it coming in,
thanks everyone!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Business issues regarding adapting Python

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 10:48 AM, Nash  wrote:
> On Sep 27, 4:13 pm, "Martin P. Hellwig" 
> wrote:
>> Nash wrote:
>>
>> 
>> I think normal market rules will apply to Pakistan too, if your desired
>> trade has not the quantity you wish, the price per item should get
>> higher. Net result should be that more quantity will be available due to
>> increased interest.
>>
>> --
>> MPHhttp://blog.dcuktec.com
>> 'If consumed, best digested with added seasoning to own preference.'
>
> If I rephrase the question: In an absense of steady Python Developers;
> can there be a viable strategy involving training? Or will it be much
> safer going with an already common developer pool.
>
> Please note that my goal is not to promote python but to make a sound
> business decision. Using Python is not an absolute requirement.
>
> I appreciate all the feedback thus far, please keep it coming in,
> thanks everyone!
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Hire good programmers, they can pick up python rapidly.

It's widely acknowledged that hiring good people is a (the?) crucial
factor in the success of programming endeavors.  "Good" programmers,
almost by definition, will be able to handle learning python without
problems.

Just my $0.02,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Business issues regarding adapting Python

2009-09-27 Thread Krishnakant
On Sun, 2009-09-27 at 10:57 -0400, Simon Forman wrote:
> On Sun, Sep 27, 2009 at 10:48 AM, Nash  wrote:
> > On Sep 27, 4:13 pm, "Martin P. Hellwig" 
> > wrote:
> >> Nash wrote:
> >>
> >> 
> >> I think normal market rules will apply to Pakistan too, if your desired
> >> trade has not the quantity you wish, the price per item should get
> >> higher. Net result should be that more quantity will be available due to
> >> increased interest.
> >>
> >> --
> >> MPHhttp://blog.dcuktec.com
> >> 'If consumed, best digested with added seasoning to own preference.'
> >
> > If I rephrase the question: In an absense of steady Python Developers;
> > can there be a viable strategy involving training? Or will it be much
> > safer going with an already common developer pool.
> >
> > Please note that my goal is not to promote python but to make a sound
> > business decision. Using Python is not an absolute requirement.
> >
> > I appreciate all the feedback thus far, please keep it coming in,
> > thanks everyone!
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
> 
> Hire good programmers, they can pick up python rapidly.
> Bingo!  That's the point even I mentioned to Nash in my last email.
When I take interviews of programmres aspiring for a job, I never ask
them which programming language they know and never take them on the
basis of how good they are in comparison to that language for my
projects (in python for example ).

> It's widely acknowledged that hiring good people is a (the?) crucial
> factor in the success of programming endeavors.  "Good" programmers,
> almost by definition, will be able to handle learning python without
> problems.

+ the easy and power of python.
Happy hacking.
Krishnakant.


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


Re: Business issues regarding adapting Python

2009-09-27 Thread Duncan Booth
Nash  wrote:

> If I rephrase the question: In an absense of steady Python Developers;
> can there be a viable strategy involving training? Or will it be much
> safer going with an already common developer pool.

My experience has been that:

a) Python developers come from the better end of the programmer pool. It
doesn't really matter that there are 100 times as many PHP programmer if
you wouldn't want to employ 99% of them. In an environment where the
Python programmers are rare you can be pretty sure that the ones there
are the ones with an interest in finding out the best way to do things,
not just the ones for whom cutting code is a 9 to 5 job. 

b) Python is pretty easy to learn. Any half way competent programmer
picks it up pretty quickly. 

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


Re: unexplainable python

2009-09-27 Thread D'Arcy J.M. Cain
On Sat, 26 Sep 2009 23:31:25 -0700
John Nagle  wrote:
> dads wrote:
> > Sorry forgot to mention I'm using python 2.6
> 
> This looks like a homework assignment.

So what?  He supplied code and showed what he tried so far didn't he?
I suppose he could have said that it was homework but maybe it was a
self assigned problem to learn Python.  I thought that he was doing
exactly what everyone suggested to do when they had problems with an
assignment except, maybe, telling us it was homework if it actually was.

And the answers were spot on too.  Most of the help was things like
"don't use classes" and "why call _4digit."  No one handed him a
complete answer on a silver platter.  They made him think about it.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting changes to a dict

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 5:36 AM, Steven D'Aprano
 wrote:
> I'm pretty sure the answer to this is No, but I thought I'd ask just in
> case...
>
> Is there a fast way to see that a dict has been modified? I don't care
> what the modifications are, I just want to know if it has been changed,
> where "changed" means a key has been added, or deleted, or a value has
> been set. (Modifications to mutable values aren't important.) In other
> words, any of these methods count as modifying the dict:
>
> __setitem__
> __delitem__
> clear
> pop
> popitem
> setdefault
> update
>
> Of course I can subclass dict to do this, but if there's an existing way,
> that would be better.
>
>
> --
> Steven


Depending on what you're doing you could use something like this:

(Note that it doesn't work on empty dicts, and you'd have to "reset
it" if your dict ever became empty after processing.)

def f(d):
while True:
i = iter(d).next
try:
while True:
try:
i()
except RuntimeError:
yield True
break
else:
yield False
except StopIteration:
if not d:
break # else we'd enter an infinite loop.


In [1]: d = {23: 18}

In [2]: check = f(d).next

In [3]: check()
Out[3]: False

In [4]: d['cats'] = 'lol'

In [5]: check()
Out[5]: True

In [6]: check()
Out[6]: False

In [7]: d.clear()

In [8]: check()
Out[8]: True

In [9]: check()
---
StopIteration Traceback (most recent call last)

/home/sforman/ in ()

StopIteration:



HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Steven D'Aprano
On Sun, 27 Sep 2009 16:11:52 +0200, Stef Mientki wrote:

> hello,
> 
> I've a Python desktop application, running under Widows, that stores the
> data in a central SQLite database.
> 
> Depending on the user login on the system, some of the data from the
> database is allowed and other data is forbidden.
> 
> I can read the current logged in user. The authorization for each user
> is stored encrypted in the database. The program is delivered as pyc
> files, but from what I read, these can easily be reversed engineered.

What does that have to do with anything? You're not storing the user's 
password in the source code are you?


> There is even an encrypted version of SQLite (not freeware), but as long
> as test the authorization in Python, it doesn't seem to be a good
> protection.

What exactly are you doing to authenticate the user?


> So at first thought, a better way might be the following process: -
> encrypt the whole database

What is your threat model? What are you trying to protect against?

If your threat model is that desktop users will sneak into the server 
room while the boss is away, boot the server in single-user mode, and 
then use a disk utility to inspect the raw bytes on disk to read the data 
in the database (or that the government will swoop in and seize your 
computer and do the same), then encrypting the entire database may be a 
good idea. 

(But if your threat model is the government, then what are you going to 
do when they arrest you and demand you hand over the encryption keys?)

But if your threat model is that Fred will guess Barney's password, then 
encrypting the entire database is a waste of time. All it does is make 
your job harder, and the application slower, and it accomplishes nothing 
to stop Fred guessing the password.

Rather than trying to invent your own scheme for authentication, I 
suggest you see how your database handles it. Chances are this is already 
a solved problem. If SQLite doesn't handle authentication, there are 
plenty of "real" databases that do.


> - run all queries through a DLL written in C or Delphi, where also the
> authorization is checked
> 
> But on second thought that isn't true. The login on the computer is
> assumed to be enough (1-time login). So the Python program passes the
> username to the external DLL. And there's the weak point again,
> the username of every person in our organization is known to every one.


If you can't trust the users not to swap usernames and passwords, you 
can't stop users from logging in as somebody else. Writing your 
application in C or Delphi can't prevent that. (I suppose you could use 
biometrics, but then what's to stop Fred calling Barney and getting him 
to put his thumb on the thumbprint reader?)

You need to think carefully about what threat you are defending against. 
Who is the enemy? Who can you trust? What *exactly* are you trying to 
stop them from doing?


> So it shouldn't be too difficult to modify the program, so it will
> always use the external DLL with the username with the highest
> authorization.
> 
> So the question remains:
> Is it possible to create a Python Desktop application, with user
> authorization, based on the login of the user ?

Are you trying to integrate that with the user's Windows login? That will 
probably be hard.



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


Re: What does the list_folders() method of mailbox.Maildir actually ?do (if anything)?

2009-09-27 Thread tinnews
Tim Roberts  wrote:
> [email protected] wrote:
> >
> >My maildir hierarchy is created by mutt which is a *very* standards
> >compliant MUA, surely standard python libraries should work with
> >standard maildirs not some wierd extension thereof.
> 
> The Maildir specification does not allow for subfolders.  That was added in
> Maildir++, and the subfolder names start with a dot.  It's not a "wierd
> extension thereof".

No sub-folders within maildir folders but that's not what I was expecting.

I was just expecting to be able to list my maildir mailboxes.

-- 
Chris Green

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


Re: Detecting changes to a dict

2009-09-27 Thread geremy condra
On Sun, Sep 27, 2009 at 5:36 AM, Steven D'Aprano <
[email protected]> wrote:

> I'm pretty sure the answer to this is No, but I thought I'd ask just in
> case...
>
> Is there a fast way to see that a dict has been modified? I don't care
> what the modifications are, I just want to know if it has been changed,
> where "changed" means a key has been added, or deleted, or a value has
> been set. (Modifications to mutable values aren't important.) In other
> words, any of these methods count as modifying the dict:
>
> __setitem__
> __delitem__
> clear
> pop
> popitem
> setdefault
> update
>
> Of course I can subclass dict to do this, but if there's an existing way,
> that would be better.
>

d1 = {"a": "b", "c": "d"}
d2 = d1.copy()
assert d1 == d2
d2["e"] = "f"
assert d1 == d2

Is that what you're looking for?

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


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread geremy condra
On Sun, Sep 27, 2009 at 11:42 AM, Steven D'Aprano <
[email protected]> wrote:

> On Sun, 27 Sep 2009 16:11:52 +0200, Stef Mientki wrote:
>
> > hello,
> >
> > I've a Python desktop application, running under Widows, that stores the
> > data in a central SQLite database.
> >
> > Depending on the user login on the system, some of the data from the
> > database is allowed and other data is forbidden.
> >
> > I can read the current logged in user. The authorization for each user
> > is stored encrypted in the database. The program is delivered as pyc
> > files, but from what I read, these can easily be reversed engineered.
>
> What does that have to do with anything? You're not storing the user's
> password in the source code are you?
>
>
> > There is even an encrypted version of SQLite (not freeware), but as long
> > as test the authorization in Python, it doesn't seem to be a good
> > protection.
>
> What exactly are you doing to authenticate the user?
>
>
> > So at first thought, a better way might be the following process: -
> > encrypt the whole database
>
> What is your threat model? What are you trying to protect against?
>
> If your threat model is that desktop users will sneak into the server
> room while the boss is away, boot the server in single-user mode, and
> then use a disk utility to inspect the raw bytes on disk to read the data
> in the database (or that the government will swoop in and seize your
> computer and do the same), then encrypting the entire database may be a
> good idea.
>
> (But if your threat model is the government, then what are you going to
> do when they arrest you and demand you hand over the encryption keys?)
>

http://en.wikipedia.org/wiki/Deniable_encryption

cool stuff.

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


Re: pep370 python 2.6?

2009-09-27 Thread Christian Heimes
Neal Becker wrote:
> Is pep370 (per-user site-packages) available on 2.6?

Yes

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


Re: Business issues regarding adapting Python

2009-09-27 Thread Martin P. Hellwig

Nash wrote:


If I rephrase the question: In an absense of steady Python Developers;
can there be a viable strategy involving training? Or will it be much
safer going with an already common developer pool.

Please note that my goal is not to promote python but to make a sound
business decision. Using Python is not an absolute requirement.

I appreciate all the feedback thus far, please keep it coming in,
thanks everyone!


I would like to say YES!, however there is no general answer. Much of it 
depends on your business strategy. If your average flow-out of 
programmers is less then two* years (whether this is due to management, 
location, reward system, trigger happy HR or anything else doesn't 
matter) than it is not cost effective to 'train' your staff. So the 
options left are hire 'expensive' staff which meet your requirements or 
adjust your requirements and hire 'less expensive' staff.


However if you intend to have staff for long term periods and do what 
you need to do to keep them happy (market conform or above salary, room 
for personal improvements, keeping politics away and no 
micro-management), than training staff up to your requirements may be 
cost effective and is generally beneficial not only for the sheer work 
that needs to be done.


Actually if you make sure you have self motivated people with reasonable 
intelligence they will do much of that training for them self, the only 
thing you need to give them is time and a clear understanding what the 
long-term expectation is.


The expectation bit is very important and should be detailed, well at 
least for you. How can you blame your staff for not being up to 
expectation if you are not sure for yourself what it should be.


So in short, if you run your business where people are considered more 
than resources, training is beneficial and provides long term stability.


*YMMV
--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Tim Golden

Steven D'Aprano wrote:

On Sun, 27 Sep 2009 16:11:52 +0200, Stef Mientki wrote:


I've a Python desktop application, running under Widows, that stores the
data in a central SQLite database.

Depending on the user login on the system, some of the data from the
database is allowed and other data is forbidden.



What is your threat model? What are you trying to protect against?



[... snip further stuff from Stephen, usefully requiring
clarification if ideas from the OP ...]

As a general rule within Windows, you let Windows do the
authentication for you and you base your authorisation on
the presence or otherwise of certain security groups in
user's token. You can get hold of the token via -- in
Python -- the win32security module, whose functionality
you can supplement if needed by using the ctypes model
to map in API functions which aren't already exposed.

The kind of thing you might do here -- altho' it obviously
depends on your particular situation -- would be to store
a security group's identifier (name or SID) in some sort
of roles-or-permissions table, requiring that to perform
function X, the user must be in group Y. A logged on user
either is or isn't in that group.

Obviously, as was pointed out, if your users are so
untrustworthy, they might steal or guess each other's
logon details, but at that point you've got problems far
more serious than merely the data your sqlite database
represents.

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


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Peng Yu
On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern  wrote:
> On 2009-09-26 09:32 AM, Peng Yu wrote:
>>
>> Hi,
>>
>> I am looking for a method in python that is similar to the function
>> str() in R, if you are familiar with R,
>>
>> If you have no idea of R, what I want is to print the class
>> information of an object and the values of its members. Overloading
>> '__expr__' and '__repr__' then using 'print' can sort of do what I
>> want. But not quite. For example, if I have a list of many elements, I
>> don't want to print all the elements. R's str() function can
>> automatically take care of this issue. It also has other advantages, I
>> am wondering if there is something similar available in python?
>
> I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
> into its logic to implement these kinds of tools.
>
>  http://dev.pocoo.org/hg/sandbox/file/tip/pretty

But I want an even simpler solution. I don't want the user to define
__pretty__. Is there a tool that can automatically print the content
of an object without defining such a member function like __pretty__.

Regards,
Peng
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Tim Golden

Tim Golden wrote:

Steven D'Aprano wrote:



[... snip further stuff from Stephen


whose name I can't even copy from three lines above :)
Sorry, Steven.

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


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Martin P. Hellwig

Stef Mientki wrote:


What you want is pretty hard as long as the data source is not centrally 
protected with a password. That is you have a database on a server you 
only access, there is a central db but access to it is restricted to the 
admin, everybody else has a unique login name and a 'personal' database 
which is a view on the central database.
Authentication must be done with any of the single sign on solutions 
available, probably the most likely one is Microsoft's kerberos 
implementation.


However it might be good enough to fetch the username from the 
environment variables and use that to build custom queries inside your 
application to get the data from the central resource.


As long as somebody is not using your standard database credentials to 
directly go to the data or fiddles with your program and changes the 
part where it fetches the username from the environment, you should have 
hidden the security flaw sufficiently to appear to be protected.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


ANN: PyQt v4.6 Released

2009-09-27 Thread Phil Thompson
PyQt v4.6 has been released and is available from
http://www.riverbankcomputing.com/software/pyqt/.

PyQt is a comprehensive set of bindings for the Qt application and UI
framework from Nokia.  It supports the same platforms as Qt (Windows,
Linux and MacOS/X).

PyQt supports Python v3 and Python v2.3 and later.

The highlights of this release include:

 - alternate, more Pythonic, APIs have been implemented for QDate,
   QDateTime, QString, QTextStream, QTime, QUrl and QVariant. Applications
   may select a particular API. By default Python v3 uses the new versions
   and Python v2 uses the old versions

 - Qt properties can be initialised, and signals connected using keyword
   arguments passed when creating an instance.  Properties and signals can
   also be set using the QObject.pyqtConfigure() method.

Windows installers are provided for the GPL version of PyQt which contains
everything needed for PyQt development (including Qt, Qt Designer and
QScintilla) except Python itself.

PyQt v4 is implemented as a set of 18 extension modules containing over
400 classes and over 6,000 functions and methods.

QtCore
The non-GUI infrastructure including event loops, threads, i18n,
Unicode, signals and slots, user and application settings, mapped
files and shared memory.

QtDesigner
A set of classes that allow the Qt Designer GUI design tool to be
extended with PyQt.

QtGui
A rich collection of GUI widgets.

QtHelp
A set of classes for creating and viewing searchable documentation and
being able to integrate online help with PyQt applications.  It
includes the C++ port of the Lucene text search engine.

QtNetwork
A set of classes to support TCP and UDP socket programming and higher
level protocols (eg. HTTP, SSL).

QtOpenGL
A set of classes that allows PyOpenGL to render onto Qt widgets.

QtScript
A set of classes that implements a JavaScript interpreter.  Python
objects may be exposed in the interpreter as JavaScript objects.

QtScriptTools
A debugger for the JavaScript interpreter.

QtSql
A set of classes that implement SQL data models and interfaces to
industry standard databases.  The Windows installers include support
for SQLite, MySQL, PostgreSQL and ODBC.

QtSvg
A set of classes to render SVG files onto Qt widgets.

QtTest
A set of classes to automate unit testing of PyQt applications and
GUIs.

QtWebKit
This implements a web browser engine based on the WebKit engine used by
Apple's Safari browser.  It allows the methods and properties of Python
objects to be published and appear as JavaScript objects to scripts
embedded in HTML pages.

QtXML
A set of classes that implement DOM and SAX parsers.

QtXMLPatterns
A set of classes that implement XQuery and XPath support for XML and
custom data models.

QtAssistant
A set of classes that enables the Qt Assistant online help browser to
be integrated with an application.

QAxContainer
A set of classes for Windows that allows the integration of ActiveX
controls and COM objects.

phonon
A cross-platform multimedia framework that enables the use of audio and
video content in PyQt applications.  DirectX is used as the Windows
backend, QuickTime as the MacOS/X backend, and GStreamer as the Linux
backend.

DBus
PyQt includes dbus.mainloop.qt that allows the Qt event loop to be used
with the standard DBus Python bindings.

PyQt includes the pyuic4 utility which generates Python code to implement
user interfaces created with Qt Designer in the same way that the uic
utility generates C++ code.  It is also able to load Designer XML files
dynamically.

PyQt is available under the GPL and a commercial license.  Unlike Qt, PyQt
is not available under the LGPL.  The commercial PyQt license allows GPL
applications to be relicensed at any time.
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: SIP v4.9 Released (Python Bindings Generator)

2009-09-27 Thread Phil Thompson
SIP v4.9 has been released and can be downloaded from
http://www.riverbankcomputing.com/software/sip/.

SIP is a tool for generating Python modules that wrap C or C++ libraries.
It is similar to SWIG.  It is used to generate PyQt and PyKDE.

The SIP license is similar to the Python License and is also licensed under
the GPL v2 and v3.

SIP runs on Windows, UNIX, Linux and MacOS/X.

SIP requires Python v2.3 or later and fully supports Python v3.

The main focus of this release is to allow alternate, incompatible
wrappings of classes and functions to be defined which can then be
selected by an application at runtime.  This allows application developers
to manage the migration from an old, deprecated API to a new one.

Other features of SIP include:

- extension modules are implemented as a single binary .pyd or .so file (no
  Python stubs)
- support for Python new-style classes
- the ability to specify the super-type and meta-type used to wrap
  instances
- generated modules are quick to import, even for large libraries
- thread support
- the ability to re-implement C++ abstract and virtual methods in Python
- the ability to define Python classes that derive from abstract C++
  classes
- the ability to spread a class hierarchy across multiple Python modules
- support for C++ namespaces
- support for C++ exceptions
- support for C++ operators
- an extensible build system written in Python that supports over 50
  platform/compiler combinations
- the generation of API files for IDEs that support autocompletion and call
  tips.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 12:14 PM, Peng Yu  wrote:
> On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern  wrote:
>> On 2009-09-26 09:32 AM, Peng Yu wrote:
>>>
>>> Hi,
>>>
>>> I am looking for a method in python that is similar to the function
>>> str() in R, if you are familiar with R,
>>>
>>> If you have no idea of R, what I want is to print the class
>>> information of an object and the values of its members. Overloading
>>> '__expr__' and '__repr__' then using 'print' can sort of do what I
>>> want. But not quite. For example, if I have a list of many elements, I
>>> don't want to print all the elements. R's str() function can
>>> automatically take care of this issue. It also has other advantages, I
>>> am wondering if there is something similar available in python?
>>
>> I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
>> into its logic to implement these kinds of tools.
>>
>>  http://dev.pocoo.org/hg/sandbox/file/tip/pretty
>
> But I want an even simpler solution. I don't want the user to define
> __pretty__. Is there a tool that can automatically print the content
> of an object without defining such a member function like __pretty__.
>
> Regards,
> Peng

Have you examined the pprint module?

http://docs.python.org/library/pprint.html

The repr module might also be of interest:

http://docs.python.org/library/repr.html

HTH,
~Simon
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unexplainable python

2009-09-27 Thread Terry Reedy

dads wrote:

Thank you for the help, it's amazing what you can't spot. It seems the
harder you look the less likely you're to find the issue. Fresh eyes
make the world of difference.

To Matt and John:

No this certainly isn't homework, I'm 29 and in full time work. I
decided to learn to program about a year ago and picked up python, so
it's one of my hobbies. Starting from level 0 it's been challenging
and fun.

This exercise was just a bit of fun, I got the idea from a forum.


So it is a self-assigned homework problem ;-).
Keep at it and good luck!
We of course mostly think you made a good choice.

tjr

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


Re: user authorization (with one time login) in a Python desktop application ?

2009-09-27 Thread Stef Mientki

Tim Golden wrote:

Steven D'Aprano wrote:

On Sun, 27 Sep 2009 16:11:52 +0200, Stef Mientki wrote:

I've a Python desktop application, running under Widows, that stores 
the

data in a central SQLite database.

Depending on the user login on the system, some of the data from the
database is allowed and other data is forbidden.



What is your threat model? What are you trying to protect against?



[... snip further stuff from Stephen, usefully requiring
clarification if ideas from the OP ...]


thanks guys,
as Steven said, I'll first identify the threats,
and I'll rethink the whole situation.
( I always want to have a solution for life, independent of the actual 
threats ;-)

As a general rule within Windows, you let Windows do the
authentication for you and you base your authorisation on
the presence or otherwise of certain security groups in
user's token. You can get hold of the token via -- in
Python -- the win32security module, whose functionality
you can supplement if needed by using the ctypes model
to map in API functions which aren't already exposed.


By making use of the one time login on windows,
I'm not sure, but I guess the user environment variable "USER"  should 
hold the vald user,

which has probably a one-to-one relation with the SID

The kind of thing you might do here -- altho' it obviously
depends on your particular situation -- would be to store
a security group's identifier (name or SID) in some sort
of roles-or-permissions table, requiring that to perform
function X, the user must be in group Y. A logged on user
either is or isn't in that group.
Yes that's exactly what I'm doing, the table is stored encrypted in a 
(separate) database


Obviously, as was pointed out, if your users are so
untrustworthy, they might steal or guess each other's
logon details, but at that point you've got problems far
more serious than merely the data your sqlite database
represents.

The point is that usernames are published in our telephone / email lists ;-)
So the only personalization is the users password,
which is verified ones during windows logon.

I'll also study your's win32security module closer.

thanks and cheers,
Stef


TJG


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


Multidimensional arrays/lists

2009-09-27 Thread Someone Something
I'm trying to write a little tic-tac-toe program I need a array/list such
that I can represent the tic tac toe board with an x axis and y axis and i
can access each square to find out whether there is an X or an O. I have
absolutely no idea how to do this in python and I really, really, don't want
to do this is C.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multidimensional arrays/lists

2009-09-27 Thread Gary Herron

Someone Something wrote:
I'm trying to write a little tic-tac-toe program I need a array/list 
such that I can represent the tic tac toe board with an x axis and y 
axis and i can access each square to find out whether there is an X or 
an O. I have absolutely no idea how to do this in python and I really, 
really, don't want to do this is C.


Here's a 3x3 array (actually a list of lists) filled with space to 
represent the empty board

board = [  [' ', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' '] ]

And here's two moves, X grabs the center, and O grabs the upper right
board[1][1] = 'X'
board[0][2] = 'O'

And so on.

Gary Herron


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


Re: Multidimensional arrays/lists

2009-09-27 Thread Rhodri James
On Sun, 27 Sep 2009 17:40:57 +0100, Someone Something  
 wrote:



I'm trying to write a little tic-tac-toe program I need a array/list such
that I can represent the tic tac toe board with an x axis and y axis and  
i

can access each square to find out whether there is an X or an O. I have
absolutely no idea how to do this in python and I really, really, don't  
want

to do this is C.


A list of lists.


board = [[None, None, None], [None, None, None], [None, None, None]]
board[0][0] = 'X'
print board[0][1]

None

Just be a little careful constructing your board.  It's awfully easy to
end up with rows being the same list object if you're careless, and then
unexpected things happen :-)

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Catch script hangs

2009-09-27 Thread Bakes
Due to an ftp server issue, my python script sometimes hangs whilst
downloading, unable to receive any more data. Is there any way that I
could have python check, maybe through a thread or something, whether
it has hanged (or just, if it's still active after 10 seconds, stop
it?). I have looked at threading but there does not seem to be a stop
method on threading, which is a problem. Could the lower level thread
module be a solution?

I was thinking something like:
thread:
spawn threaded timer, if it gets to 20, close the thread
attempt download of file
cancel threaded timer.
exit thread.

would that work at all?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catch script hangs

2009-09-27 Thread exarkun

On 10:40 pm, [email protected] wrote:

Due to an ftp server issue, my python script sometimes hangs whilst
downloading, unable to receive any more data. Is there any way that I
could have python check, maybe through a thread or something, whether
it has hanged (or just, if it's still active after 10 seconds, stop
it?). I have looked at threading but there does not seem to be a stop
method on threading, which is a problem. Could the lower level thread
module be a solution?


No.  There are a great many issues which arise when trying to forcibly 
terminate a thread.  Python doesn't expose this functionality because 
most platforms don't provide it in a safe or reliable way.


You could give Twisted's FTP client a try.  Since it isn't blocking, you 
don't need to use threads, so it's easy to have a timeout.


You could also explore solutions based on signal.alarm().  A single- 
threaded signal-based solution has some issues as well, but not nearly 
as many as a thread-based solution.


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


Re: itools 0.60.2 released

2009-09-27 Thread David Calman

you mean MobileMe? go back to comp.sys.mac.misc

--
From: ""J. David Ibáñez"" 
Sent: Wednesday, June 03, 2009 1:49 AM
Newsgroups: comp.lang.python.announce
To: 
Subject: itools 0.60.2 released



itools 0.60.2 (2009/06/03)
==

itools is a Python library, it groups a number of packages into a single
meta-package for easier development and deployment:

  itools.abnf itools.i18n itools.stl
  itools.core itools.ical itools.tmx
  itools.csv  itools.odf  itools.uri
  itools.datatypesitools.pdf  itools.vfs
  itools.gettext  itools.pkg  itools.web
  itools.git  itools.relaxng  itools.workflow
  itools.handlers itools.rest itools.xapian
  itools.html itools.rss  itools.xliff
  itools.http itools.srx  itools.xml

The mechanism to use a subprocess to run commands and save memory has
been generalized and is now provided by itools.core; the functions to
check are 'start_subprocess', 'read_subprocess', 'send_subprocess' and
'stop_subprocess'.

Some bugs have been fixed, including #650 and #670.


Resources
-

Download
http://download.hforge.org/itools/0.60/itools-0.60.2.tar.gz

Home
http://www.hforge.org/itools/

Mailing list
http://www.hforge.org/community/
http://archives.hforge.org/index.cgi?list=itools

Bug Tracker
http://bugs.hforge.org/


--
J. David Ibáñez
Itaapy  Tel +33 (0)1 42 23 67 45
9 rue Darwin, 75018 Paris  Fax +33 (0)1 53 28 27 88 


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


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Peng Yu
On Sun, Sep 27, 2009 at 1:20 PM, Simon Forman  wrote:
> On Sun, Sep 27, 2009 at 12:14 PM, Peng Yu  wrote:
>> On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern  wrote:
>>> On 2009-09-26 09:32 AM, Peng Yu wrote:

 Hi,

 I am looking for a method in python that is similar to the function
 str() in R, if you are familiar with R,

 If you have no idea of R, what I want is to print the class
 information of an object and the values of its members. Overloading
 '__expr__' and '__repr__' then using 'print' can sort of do what I
 want. But not quite. For example, if I have a list of many elements, I
 don't want to print all the elements. R's str() function can
 automatically take care of this issue. It also has other advantages, I
 am wondering if there is something similar available in python?
>>>
>>> I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
>>> into its logic to implement these kinds of tools.
>>>
>>>  http://dev.pocoo.org/hg/sandbox/file/tip/pretty
>>
>> But I want an even simpler solution. I don't want the user to define
>> __pretty__. Is there a tool that can automatically print the content
>> of an object without defining such a member function like __pretty__.
>>
>> Regards,
>> Peng
>
> Have you examined the pprint module?
>
> http://docs.python.org/library/pprint.html
>
> The repr module might also be of interest:
>
> http://docs.python.org/library/repr.html

Here is the example that I tried. It seems that I have to define
__expr__ to show A's member '_x'.  But this is tedious if A has many
members. I'm looking for a solution that I don't have to write
anything explicitly in a class in order to print it pretty.

$ cat main.py
import pprint

class A:
  def __init__(self):
self._x = 10

  def show(self):
print self._x

a = A()
a.show()

pp = pprint.PrettyPrinter()
pp.pprint(a)
$ python main.py
10
<__main__.A instance at 0x2aba2bd8>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Zero Piraeus
:

2009/9/27 Peng Yu :
>>> But I want an even simpler solution. I don't want the user to define
>>> __pretty__. Is there a tool that can automatically print the content
>>> of an object without defining such a member function like __pretty__.

Not tested (much):

from pprint import pprint

def examine(obj, limit=3):
truncated = dict()
for attr_name in dir(obj):
if attr_name.startswith(("__", "_%s__" % obj.__class__.__name__)):
continue # don't include "private" or special attributes
attr = getattr(obj, attr_name)
if callable(attr):
continue # don't include methods
if hasattr(attr, "__getitem__") and not isinstance(attr, str):
truncated[attr_name] = attr[:limit]
else:
truncated[attr_name] = attr
pprint(truncated)

It truncates sequences silently, which may or may not be what you want.

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to run python script in emacs

2009-09-27 Thread devilkin
On 9月27日, 下午12时43分, Nobody  wrote:
> On Sat, 26 Sep 2009 08:54:49 -0700, devilkin wrote:
> > I'm just starting learning python, and coding in emacs. I usually
> > split emacs window into two, coding in one, and run script in the
> > other, which is not very convenient. anyone can help me with it? is
> > there any tricks like emacs short cut?
>
> According to "C-h m":
>
> M-C-x   py-execute-def-or-class
> C-c !   py-shell
> C-c |   py-execute-region
> C-c return  py-execute-import-or-reload
> C-c C-c py-execute-buffer
> C-c C-s py-execute-string
>
> Also, "C-c ?" from within a python-mode buffer provides an introductory
> tutorial on python-mode.

Thanks, I tried C-c C-c, it gives no output, perhaps because I'm using
emacs on Windows. I'll test it on Ubuntu later.
and does the python-mode support auto-complete?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Catch script hangs

2009-09-27 Thread Sean DiZazzo
On Sep 27, 3:40 pm, Bakes  wrote:
> Due to an ftp server issue, my python script sometimes hangs whilst
> downloading, unable to receive any more data. Is there any way that I
> could have python check, maybe through a thread or something, whether
> it has hanged (or just, if it's still active after 10 seconds, stop
> it?). I have looked at threading but there does not seem to be a stop
> method on threading, which is a problem. Could the lower level thread
> module be a solution?
>
> I was thinking something like:
> thread:
> spawn threaded timer, if it gets to 20, close the thread
> attempt download of file
> cancel threaded timer.
> exit thread.
>
> would that work at all?

I messed around, and came up with this:


import ftplib, socket

class MyFTP(ftplib.FTP):
def storbinary(self, command, f, blocksize=8192, callback=None,
timeout=0):
"""
Override the storbinary method to make the socket.connection()
object available
outside the object, and to set the timeout of the socket
"""
self.voidcmd('TYPE I')
self.conn = self.transfercmd(command)
self.conn.settimeout(timeout)
while 1:
buf = f.read(blocksize)
if not buf:
break
self.conn.sendall(buf)
if callback: callback(buf)
self.conn.close()


ftp = MyFTP("ftp.host","user","password")


fname = "FireOnTheMountain.mov"
timeout = 1


try:
with open(fname, 'r') as fi:
#send the extra timeout arg
ftp.storbinary("STOR %s" % fname, fi, timeout=timeout)
except socket.timeout:
print "TIMED OUT!"
#if we shutdown the socket connection, it seems to properly stop
the transfer
ftp.conn.shutdown(socket.SHUT_RDWR)

ftp.quit()


I think that should at least let you set a timeout for sending each
packet that will end the connection gracefully if the timeout is
reached.  Not sure if it raises any other problems.

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


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread John Nagle

Zero Piraeus wrote:

:

2009/9/27 Peng Yu :

But I want an even simpler solution. I don't want the user to define
__pretty__. Is there a tool that can automatically print the content
of an object without defining such a member function like __pretty__.


Not tested (much):

from pprint import pprint

def examine(obj, limit=3):
truncated = dict()
for attr_name in dir(obj):
if attr_name.startswith(("__", "_%s__" % obj.__class__.__name__)):
continue # don't include "private" or special attributes
attr = getattr(obj, attr_name)
if callable(attr):
continue # don't include methods
if hasattr(attr, "__getitem__") and not isinstance(attr, str):
truncated[attr_name] = attr[:limit]
else:
truncated[attr_name] = attr
pprint(truncated)

It truncates sequences silently, which may or may not be what you want.

 -[]z.


   Ah, introspection.  Is that a defined feature of Python or an implementation
quirk of CPython?

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


Re: is this whiff/wsgi claim true?

2009-09-27 Thread Дамјан Георгиевски


> Hi folks.  I just modified the WHIFF concepts index page
> 
> http://aaron.oirt.rutgers.edu/myapp/docs/W1000.concepts
> 
> To include the following paragraph with a startling and arrogant
> claim in the final sentence :)

mod_wsgi (the apache module) can be configured to automatically run any 
.wsgi file dropped in a folder just like CGI ... see here 
http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScriptAlias

-- 
дамјан ( http://softver.org.mk/damjan/ )

Today we create the legacy of tomorrow.
-- 
http://mail.python.org/mailman/listinfo/python-list


Bash shell to Python shell?

2009-09-27 Thread edwithad
I am sure you have not read a question this basic in some time, but I 
am curious. Using Linux I open a terminal window and type: python.


Does Bash Shell go away and to become a Python Shell, or is it still a 
Bash Shell with Python running inside? Thanks in advance.


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


Python shared lib

2009-09-27 Thread namekuseijin
So, I was trying to get the yafaray raytracer to work with the 3D 
package Blender, but it asks for python2.6 and all I got is a 2.5. 
Actually, quite a lot of other related Blender packages are also 
migrating to 2.6, so a compilation was inevitable.


Then I go:
./configure --prefix=~/ && make && make install

and then I realize that, for whatever reason, the super popular and 
trendy python DOESN'T FRIGGIN BUILD SHARED LIBS BY DEFAULT!


why oh why it is so popular and doesn't even think of poor software 
packages that might use its shared libs?...   You gotta activate it with 
a friggin' --enable-shared, which isn't listed by --help as either yes 
or no as default (I assumed yes for obvious reasons)...


bored and annoyed after second recompilation... hope some dev picks this 
minor nitpick...

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


Re: Bash shell to Python shell?

2009-09-27 Thread Chris Rebert
On Sun, Sep 27, 2009 at 8:13 PM, edwithad  wrote:
> I am sure you have not read a question this basic in some time, but I am
> curious. Using Linux I open a terminal window and type: python.
>
> Does Bash Shell go away and to become a Python Shell, or is it still a Bash
> Shell with Python running inside? Thanks in advance.

The latter. Press Ctrl+D or enter exit() or quit() to exit Python and
return to bash.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bash shell to Python shell?

2009-09-27 Thread Ned Deily
In article <2009092721134550073-edwit...@hotmailcom>,
 edwithad  wrote:
> I am sure you have not read a question this basic in some time, but I 
> am curious. Using Linux I open a terminal window and type: python.
> 
> Does Bash Shell go away and to become a Python Shell, or is it still a 
> Bash Shell with Python running inside? Thanks in advance.

See for youself:

$ ps
  PID TTY  TIME CMD
17478 pts/000:00:00 bash
17571 pts/000:00:00 ps

$ python
Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45) 
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system('ps')
  PID TTY  TIME CMD
17478 pts/000:00:00 bash
17572 pts/000:00:00 python
17573 pts/000:00:00 sh
17574 pts/000:00:00 ps
0
>>>

So you can see that the top-level shell (process id 17478) starts new 
child processes to run other programs (17571 for the ps in the first 
example, 17572 for python in the second), just as the python os.system 
function starts a new shell (17573, a default /bin/sh) to process the 
'ps' argument string passed to it and that shell starts another process 
(17574) to run 'ps'.

For a more detailed explanation of what's going on, see, for example:

http://tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/running-progra
ms.html

-- 
 Ned Deily,
 [email protected]

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


UnboundLocalError - (code is short & simple)

2009-09-27 Thread pylearner
Python version = 2.6.1
IDLE
Computer = Win-XP, SP2 (current with all windows updates)

---

Greetings:

I have written code for two things:  1) simulate a coin toss, and 2)
assign the toss result to a winner.  Code for the simulated coin toss
is in a file named "coin_toss.py."  Code for the assignment of the
toss result is in a file named "toss_winner.py."  Each file has one
simple function:  1) coin_toss(), and 2) toss_winner(), respectively.
The code for each file is listed below.

Problem:

I am getting an error when I run "toss_winner.py."  The error message
is listed below.

Question #1:

Why am I getting this error?

Explanation:

As I understand it, the first statement of the toss_winner() function
body -- i.e. "coin_toss = coin_toss()" -- causes four things to
happen: 1) the coin_toss() function is called, 2) the coin_toss()
function is executed, 3) the coin_toss() function returns the value of
its local "coin_toss" variable, and 4) the returned value of the "coin
toss" variable that is local to the coin_toss() function is assigned
to the "coin toss" variable that is local to the toss_winner()
function.

Given this understanding, it seems I should NOT be getting a
"referenced before assignment" error, involving the "coin_toss" local
variable of "toss_winner()."

Note:

I am new to programming and Python.  I'm currently self-studying
"Python Programming: An Intro to Computer Science" by Zelle.

Thanks!

---

Traceback (most recent call last):
  File "", line 1, in 
toss_winner()
  File "C:/Python26/toss_winner.py", line 7, in toss_winner
coin_toss = coin_toss()
UnboundLocalError: local variable 'coin_toss' referenced before
assignment

---

# toss_winner.py

from coin_toss import coin_toss

def toss_winner():

coin_toss = coin_toss()

if coin_toss == "Heads":
toss_winner = "Player A"
print 'From "toss_winner" function >>',
print "Toss Winner = " + str(toss_winner)

else:
toss_winner = "Player B"
print 'From "toss_winner" function >>',
print "Toss Winner = " + str(toss_winner)

return toss_winner

---

# coin_toss.py

from random import random

def coin_toss():

random_number = random()

if random_number < .5:

coin_toss = "Heads"

print 'From "coin_toss" function >>',
print "Toss result = " + str(coin_toss)

else:

coin_toss = "Tails"

print 'From "coin_toss" function >>',
print "Toss result = " + str(coin_toss)

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


UnboundLocalError - code is short & simple

2009-09-27 Thread pylearner
System Specs:

Python version = 2.6.1
IDLE
Computer = Win-XP, SP2 (current with all windows updates)



Greetings:

I have written code for two things:  1) simulate a coin toss, and 2)
assign the toss result to a winner.  Code for the simulated coin toss
is in a file named "coin_toss.py."  Code for the assignment of the
toss result is in a file named "toss_winner.py."  Each file has one
simple function:  1) coin_toss(), and 2) toss_winner(), respectively.
(The code for each file is listed below.)


Problem:

I am getting an error when I run "toss_winner.py."


Error Message:

Traceback (most recent call last):
  File "", line 1, in 
toss_winner()
  File "C:/Python26/toss_winner.py", line 7, in toss_winner
coin_toss = coin_toss()
UnboundLocalError: local variable 'coin_toss' referenced before
assignment


Question #1:

After reviewing the code below, does anybody know why I am getting
this error?


Explanation:

As I understand, the first statement of the toss_winner() function
body -- i.e. "coin_toss = coin_toss()" -- causes four things to
happen: 1) the coin_toss() function is called, 2) the coin_toss()
function is executed, 3) the coin_toss() function returns the value of
its local "coin_toss" variable, and 4) the returned value of the "coin
toss" variable that is local to the coin_toss() function is assigned
to the "coin toss" variable that is local to the toss_winner()
function.

Given this understanding, it seems I should NOT be getting a
"referenced before assignment" error, involving the "coin_toss" local
variable of "toss_winner()."


Note:

I am new to programming and Python.  I'm currently self-studying
"Python Programming: An Intro to Computer Science" by Zelle.

Thanks!

---

# toss_winner.py

from coin_toss import coin_toss

def toss_winner():

coin_toss = coin_toss()

if coin_toss == "Heads":
toss_winner = "Player A"
print 'From "toss_winner" function >>',
print "Toss Winner = " + str(toss_winner)

else:
toss_winner = "Player B"
print 'From "toss_winner" function >>',
print "Toss Winner = " + str(toss_winner)

return toss_winner

---

# coin_toss.py

from random import random

def coin_toss():

random_number = random()

if random_number < .5:

coin_toss = "Heads"

print 'From "coin_toss" function >>',
print "Toss result = " + str(coin_toss)

else:

coin_toss = "Tails"

print 'From "coin_toss" function >>',
print "Toss result = " + str(coin_toss)

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


Re: Bash shell to Python shell?

2009-09-27 Thread Grant Edwards
On 2009-09-28, Chris Rebert  wrote:
> On Sun, Sep 27, 2009 at 8:13 PM, edwithad  wrote:
>> I am sure you have not read a question this basic in some time, but I am
>> curious. Using Linux I open a terminal window and type: python.
>>
>> Does Bash Shell go away and to become a Python Shell, or is it still a Bash
>> Shell with Python running inside? Thanks in advance.
>
> The latter. Press Ctrl+D or enter exit() or quit() to exit Python and
> return to bash.

Bash doesn't really "go away".  It's still there, it just hands
over the terminal to the python interpreter and then sits and
waits for the python interpreter to exit.  At that point the
Bash shell (the same one as before) resumes.

-- 
Grant


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


Re: UnboundLocalError - (code is short & simple)

2009-09-27 Thread Chris Kaynor
Lets look at what is happening on a few of the lines here:

First:
from coin_toss import coin_toss

imports the module coin_toss and sets the local variable coin_toss to the
value of coin_toss in the module coin_toss.


Second:
coin_toss = coin_toss()

calls the function bound to the name coin_toss and assigns the result to
coin_toss. Now this appears to be what you want (and run outside a function
it would work as you intend, the first time). However, within a function, a
different namespace exists, and Python sees that you are assigning to
coin_toss, and thus uses the local version of that variable everywhere else
within the function. As such, Python is attempting to call the
local variable coin_toss, which has not yet been assigned too.



While not really the "correct" solution, you may want to look into the
global keyword for more information about your problem.



Chris


On Sun, Sep 27, 2009 at 8:53 PM, pylearner  wrote:

> Python version = 2.6.1
> IDLE
> Computer = Win-XP, SP2 (current with all windows updates)
>
> ---
>
> Greetings:
>
> I have written code for two things:  1) simulate a coin toss, and 2)
> assign the toss result to a winner.  Code for the simulated coin toss
> is in a file named "coin_toss.py."  Code for the assignment of the
> toss result is in a file named "toss_winner.py."  Each file has one
> simple function:  1) coin_toss(), and 2) toss_winner(), respectively.
> The code for each file is listed below.
>
> Problem:
>
> I am getting an error when I run "toss_winner.py."  The error message
> is listed below.
>
> Question #1:
>
> Why am I getting this error?
>
> Explanation:
>
> As I understand it, the first statement of the toss_winner() function
> body -- i.e. "coin_toss = coin_toss()" -- causes four things to
> happen: 1) the coin_toss() function is called, 2) the coin_toss()
> function is executed, 3) the coin_toss() function returns the value of
> its local "coin_toss" variable, and 4) the returned value of the "coin
> toss" variable that is local to the coin_toss() function is assigned
> to the "coin toss" variable that is local to the toss_winner()
> function.
>
> Given this understanding, it seems I should NOT be getting a
> "referenced before assignment" error, involving the "coin_toss" local
> variable of "toss_winner()."
>
> Note:
>
> I am new to programming and Python.  I'm currently self-studying
> "Python Programming: An Intro to Computer Science" by Zelle.
>
> Thanks!
>
> ---
>
> Traceback (most recent call last):
>  File "", line 1, in 
>toss_winner()
>  File "C:/Python26/toss_winner.py", line 7, in toss_winner
>coin_toss = coin_toss()
> UnboundLocalError: local variable 'coin_toss' referenced before
> assignment
>
> ---
>
> # toss_winner.py
>
> from coin_toss import coin_toss
>
> def toss_winner():
>
>coin_toss = coin_toss()
>
>if coin_toss == "Heads":
>toss_winner = "Player A"
>print 'From "toss_winner" function >>',
>print "Toss Winner = " + str(toss_winner)
>
>else:
>toss_winner = "Player B"
>print 'From "toss_winner" function >>',
>print "Toss Winner = " + str(toss_winner)
>
>return toss_winner
>
> ---
>
> # coin_toss.py
>
> from random import random
>
> def coin_toss():
>
>random_number = random()
>
>if random_number < .5:
>
>coin_toss = "Heads"
>
>print 'From "coin_toss" function >>',
>print "Toss result = " + str(coin_toss)
>
>else:
>
>coin_toss = "Tails"
>
>print 'From "coin_toss" function >>',
>print "Toss result = " + str(coin_toss)
>
>return coin_toss
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError - (code is short & simple)

2009-09-27 Thread Chris Rebert
On Sun, Sep 27, 2009 at 8:53 PM, pylearner  wrote:

> ---
>
> Traceback (most recent call last):
>  File "", line 1, in 
>    toss_winner()
>  File "C:/Python26/toss_winner.py", line 7, in toss_winner
>    coin_toss = coin_toss()
> UnboundLocalError: local variable 'coin_toss' referenced before
> assignment
>
> ---
>
> # toss_winner.py
>
> from coin_toss import coin_toss
>
> def toss_winner():
>
>    coin_toss = coin_toss()

When Python sees this assignment to coin_toss as it compiles the
toss_winner() function, it marks coin_toss as a local variable and
will not consult global scope when looking it up at runtime (this is
an implementation optimization that leaks into the language level).
Hence, when the function is executed, Python does a fast lookup of
coin_toss in the local scope, finds it has not been assigned to, and
raises the error you're seeing, not falling back to and examining the
global variable scope.

To fix the problem, rename the variable so its name differs from that
of the coin_toss() function. That way, Python won't optimize the
lookup and will consult the global scope when it goes to resolve
coin_toss() in toss_winner().

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting changes to a dict

2009-09-27 Thread CTO
On Sep 27, 5:36 am, Steven D'Aprano  wrote:
> I'm pretty sure the answer to this is No, but I thought I'd ask just in
> case...
>
> Is there a fast way to see that a dict has been modified? I don't care
> what the modifications are, I just want to know if it has been changed,
> where "changed" means a key has been added, or deleted, or a value has
> been set. (Modifications to mutable values aren't important.) In other
> words, any of these methods count as modifying the dict:
>
> __setitem__
> __delitem__
> clear
> pop
> popitem
> setdefault
> update
>
> Of course I can subclass dict to do this, but if there's an existing way,
> that would be better.
>
> --
> Steven

d = {"a": "b", "c": "d"}
d2 = d.copy()
assert d == d2
d["e"] = "f"
assert d == d2

Is this what you're looking for?

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


Re: Multidimensional arrays/lists

2009-09-27 Thread Simon Forman
On Sun, Sep 27, 2009 at 12:40 PM, Someone Something
 wrote:
> I'm trying to write a little tic-tac-toe program I need a array/list such
> that I can represent the tic tac toe board with an x axis and y axis and i
> can access each square to find out whether there is an X or an O. I have
> absolutely no idea how to do this in python and I really, really, don't want
> to do this is C.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

You can use tuples as keys in a dict

R = range(3)

board = {}

# Initialize the board.
for x in R:
for y in R:
board[x, y] = 'O'


def board_to_string(b):
return '\n'.join(
   ' | '.join(b[x, y] for x in R)
   for y in R
   )


>>> print board_to_string(board)
O | O | O
O | O | O
O | O | O

>>> board[0, 1] = 'X'
>>> print board_to_string(board)
O | O | O
X | O | O
O | O | O
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting changes to a dict

2009-09-27 Thread Steven D'Aprano
On Sun, 27 Sep 2009 21:42:10 -0700, CTO wrote:

>> Is there a fast way to see that a dict has been modified?
...


> d = {"a": "b", "c": "d"}
> d2 = d.copy()
> assert d == d2
> d["e"] = "f"
> assert d == d2
> 
> Is this what you're looking for?

In general, no. I was hoping for an O(1) check. Yours test is only O(1) 
if the length of the dict changes[1], and probably O(N) otherwise. 
Perhaps I'm guilty of premature optimization, but the above simple check 
may be expensive in both space and time if the dict is very large and the 
modification doesn't change the length. If the dict is large enough, 
duplicating it may cause swapping, which is O(N**2) or worse.

In my case, the dict only has a few hundred items, so your suggestion is 
probably perfectly adequate for my specific need. But as a general 
solution, imagine checking a dict with tens of millions of key/values. If 
the length is different, that's a fast check. But if the length is the 
same, the equality test has to check every key/value pair in the dict 
(presumably bailing out early if it finds a difference).

For my specific need, I'll probably end up using your suggestion simply 
because I'm lazy and it's more convenient than writing a subclass.






[1] That is, I *assume* that dict equality checking uses that obvious 
optimization.


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


Re: Most "active" coroutine library project?

2009-09-27 Thread greg

Dave Angel wrote:

Actually even 64k looked pretty good, compared to the 1.5k of RAM and 2k 
of PROM for one of my projects, a navigation system for shipboard use.


Until you wanted to do hi-res colour graphics, at which
point the video memory took up an inconveniently large
part of the address space.

E.g. on the original BBC, you could either have a
decently large program, *or* decently hi-res graphics,
but not both at the same time. :-(

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


Re: Why the file mode of .pyc files has x?

2009-09-27 Thread greg

Peng Yu wrote:

-rw--- 1 pengy lilab  29 2009-09-26 10:10:45 main.py
-rwx-- 1 pengy lilab 106 2009-09-26 10:19:17 test.py
-rwx-- 1 pengy lilab 339 2009-09-26 10:20:39 test.pyc


Doesn't happen for me with 2.5 on Darwin.

What python/OS are you using?

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


Re: Why the file mode of .pyc files has x?

2009-09-27 Thread greg

Also, what kind of file system are the files on?
Unixes often report the x bit as always being on
when the file system doesn't have x bits (e.g.
for FAT-based file systems).

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


Re: Re: Twisted PB: returning result as soon as ready

2009-09-27 Thread jacopo . pecci

Jean-Paul, thanks a lot for your patient.
I have read most of a the “The Twisted Documentation” which I think is very  
good for Deferred and ok for PB but it is really lacking on the Reactor. In  
my case it looks like this is key to achieve what I have in mind. I've also  
just received “Twisted network programming essential” but I don't expect  
too much from this book. Would you be able to suggest me a reference to  
understand the Reactors? I need to be aware of when this is blocked and how  
to avoid it.


I have a very simple objective in mind. I want to distribute some  
processing to different severs and collect and process results from a  
client as soon as they are ready. To achieve true parallelism it looks more  
complex than expected.


Many thank,
Jacopo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Detecting changes to a dict

2009-09-27 Thread CTO
On Sep 28, 1:11 am, Steven D'Aprano
 wrote:
> On Sun, 27 Sep 2009 21:42:10 -0700, CTO wrote:
> >> Is there a fast way to see that a dict has been modified?
>
> ...
>
> > d = {"a": "b", "c": "d"}
> > d2 = d.copy()
> > assert d == d2
> > d["e"] = "f"
> > assert d == d2
>
> > Is this what you're looking for?
>
> In general, no. I was hoping for an O(1) check. Yours test is only O(1)
> if the length of the dict changes[1], and probably O(N) otherwise.
> Perhaps I'm guilty of premature optimization, but the above simple check
> may be expensive in both space and time if the dict is very large and the
> modification doesn't change the length. If the dict is large enough,
> duplicating it may cause swapping, which is O(N**2) or worse.
>
> In my case, the dict only has a few hundred items, so your suggestion is
> probably perfectly adequate for my specific need. But as a general
> solution, imagine checking a dict with tens of millions of key/values. If
> the length is different, that's a fast check. But if the length is the
> same, the equality test has to check every key/value pair in the dict
> (presumably bailing out early if it finds a difference).
>
> For my specific need, I'll probably end up using your suggestion simply
> because I'm lazy and it's more convenient than writing a subclass.
>
> [1] That is, I *assume* that dict equality checking uses that obvious
> optimization.
>
> --
> Steven

If you can enumerate the language of possible inputs you could
generate a unique binary representation. Against a language of size
l that would only take you O(l*n) to build the repr for a dict
and for certain repr sizes the comparison could be O(1), making
the entire operation O(l*n+l*m) vs O(n*m).

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


Re: Things to know about super (was: super() and multiple inheritance failure)

2009-09-27 Thread Dieter Maurer
Michele Simionato  writes on Fri, 25 Sep 2009 
22:58:32 -0700 (PDT):
> ...
>You know that in an ideal world I would just
> throw
> away multiple inheritance, it is just not worth the complication.

I am a fan of multiple inheritance: it lets the compliler/language runtime
do stupid tasks (implementing delegations), I would otherwise have to do
explicitely. True, there may be complications - but often, they can be
avoided
-- 
http://mail.python.org/mailman/listinfo/python-list