Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 4, 3:17 pm, Chris Angelico  wrote:
> On Mon, Dec 5, 2011 at 1:51 AM, Dave Angel  wrote:
> > In C, the padding to the largest alignment occurs at the
> > end of a structure as well as between items.  Otherwise, an array of the
> > struct would not be safely aligned.  if you have an 8byte item followed by a
> > 4 byte item, the total size is 16.
>
> That's padding of the array, not of the structure.

That's a strange way to think of it, especially since the padding also
happens for a single struct object when there's no array present.  I
find it cleaner to think of C as having no padding in arrays, but
padding at the end of a struct.  See C99 6.7.2.1p15: 'There may be
unnamed padding at the end of a structure or union.'  There's no
mention in the standard of padding for arrays.

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


70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object

2011-12-05 Thread DPalao
El Martes Noviembre 29 2011, DPalao escribió:
> Hello,
> I'm trying to use multiprocessing to parallelize a code. There is a number
> of tasks (usually 12) that can be run independently. Each task produces a
> numpy array, and at the end, those arrays must be combined.
> I implemented this using Queues (multiprocessing.Queue): one for input and
> another for output.
> But the code blocks. And it must be related to the size of the item I put
> on the Queue: if I put a small array, the code works well; if the array is
> realistically large (in my case if can vary from 160kB to 1MB), the code
> blocks apparently forever.
> I have tried this:
> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar
> ge- objects/
> but it didn't work (especifically I put a None sentinel at the end for each
> worker).
> 
> Before I change the implementation,
> is there a way to bypass this problem with  multiprocessing.Queue?
> Should I post the code (or a sketchy version of it)?
> 
> TIA,
> 
> David

Just for reference. The other day I found the explanation by "ryles" on 
his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size 
limitations or bug...". It is very clarifying.
After having read that I arranged the program such that the main process did 
not need to know when the others have finished, so I changed the process join 
call with a queue get call, until a None (one per process) is returned.

Best,

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


Re: Install Python on Windows without Start Menu icons?

2011-12-05 Thread Wolfgang Strobl
"Pedro Henrique G. Souto" :

>On 02/12/2011 16:34, snorble wrote:
>> Is it possible to automate the Python installation on Windows using
>> the MSI file so it does not add a Start Menu folder? I would like to
>> push out Python to all of my office workstations, but I'd like for it
>> to be relatively silent from the user's point of view.
>
>If you just want to run python scripts in those machines (not developing 
>in it), you can use something like py2exe [http://www.py2exe.org/].

That doesn't answer the question.

Snorble might use  "ORCA", a MSI editor from Microsoft.

http://forums.frontmotion.com/viewtopic.php?f=10&t=837

discusses a similar question for Firefox.

I haven't tried it myself, but would give it a try.

-- 
Thank you for observing all safety precautions
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: struct calcsize discrepency?

2011-12-05 Thread Chris Angelico
On Mon, Dec 5, 2011 at 6:42 PM, Mark Dickinson  wrote:
> That's a strange way to think of it, especially since the padding also
> happens for a single struct object when there's no array present.  I
> find it cleaner to think of C as having no padding in arrays, but
> padding at the end of a struct.  See C99 6.7.2.1p15: 'There may be
> unnamed padding at the end of a structure or union.'  There's no
> mention in the standard of padding for arrays.

May be, yes, but since calcsize() is returning 12 when the elements
are put in the other order, it would seem to be not counting such
padding. The way I look at it, padding is always used to place the
beginning of something; in an array, it places the beginning of the
second element on a convenient boundary, rather than filling out the
first element to that boundary.

I tried a similar thing with a couple of C compilers, and both of them
gave the same sizeof() value for both orderings, which would imply
that they _do_ include such padding in the structure's end. My
statement that the padding was removed was based solely on
calcsize()'s different result.

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


Can't install pycrypto

2011-12-05 Thread Alec Taylor
Good afternoon,

Unfortunately my pycrpto install fails (tried with pip, easy_install
and pip -e git+)

Error: http://pastebin.com/wjjfTZvd

How do I get this working?

Thanks for all suggestions,

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


Re: struct calcsize discrepency?

2011-12-05 Thread Mark Dickinson
On Dec 5, 8:09 am, Chris Angelico  wrote:
> May be, yes, but since calcsize() is returning 12 when the elements
> are put in the other order, it would seem to be not counting such
> padding.

Indeed.  That's arguably a bug in the struct module, and one that
people have had to find workarounds for in the past.  See note 3 at:

http://docs.python.org/library/struct.html#byte-order-size-and-alignment

If it weren't for backwards compatibility issues, I'd say that this
should be fixed.

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


Re: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object

2011-12-05 Thread boB
On Mon, 5 Dec 2011 09:02:08 +0100, DPalao 
wrote:

>El Martes Noviembre 29 2011, DPalao escribió:
>> Hello,
>> I'm trying to use multiprocessing to parallelize a code. There is a number
>> of tasks (usually 12) that can be run independently. Each task produces a
>> numpy array, and at the end, those arrays must be combined.
>> I implemented this using Queues (multiprocessing.Queue): one for input and
>> another for output.
>> But the code blocks. And it must be related to the size of the item I put
>> on the Queue: if I put a small array, the code works well; if the array is
>> realistically large (in my case if can vary from 160kB to 1MB), the code
>> blocks apparently forever.
>> I have tried this:
>> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-lar
>> ge- objects/
>> but it didn't work (especifically I put a None sentinel at the end for each
>> worker).
>> 
>> Before I change the implementation,
>> is there a way to bypass this problem with  multiprocessing.Queue?
>> Should I post the code (or a sketchy version of it)?
>> 
>> TIA,
>> 
>> David
>
>Just for reference. The other day I found the explanation by "ryles" on 
>his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue size 
>limitations or bug...". It is very clarifying.
>After having read that I arranged the program such that the main process did 
>not need to know when the others have finished, so I changed the process join 
>call with a queue get call, until a None (one per process) is returned.
>
>Best,
>
>David


Why do people add character  like[* SPAM *]  to their  subject
lines ??   Is it supposed to do something  ??   I figured since
programmers hang out here, maybe one of you know this.

Thanks,
boB


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


Spam in subject lines (Re: 70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object)

2011-12-05 Thread Chris Angelico
On Mon, Dec 5, 2011 at 7:57 PM,   wrote:
> Why do people add character  like    [* SPAM *]  to their  subject
> lines ??   Is it supposed to do something  ??   I figured since
> programmers hang out here, maybe one of you know this.

People don't. It's something added by a spam filter that thought that
the email was likely to be junk (in this case, a 70% probability
thereof). If you're curious as to exactly _why_ the filter thought
that, check the email headers - it's all there, in exhaustive detail.

The purpose of the tag is to let recipients make some simple filter
rules (eg "if it has SPAM in the subject, put it in a separate
folder") and/or to be able to eyeball spam easily.

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


Re: Spam in subject lines (Re: multiprocessing.Queue blocks when sending large object)

2011-12-05 Thread Steven D'Aprano
On Mon, 05 Dec 2011 20:11:28 +1100, Chris Angelico wrote:

> On Mon, Dec 5, 2011 at 7:57 PM,   wrote:
>> Why do people add character  like    [* SPAM *]  to their  subject
>> lines ??   Is it supposed to do something  ??   I figured since
>> programmers hang out here, maybe one of you know this.
> 
> People don't. It's something added by a spam filter that thought that
> the email was likely to be junk (in this case, a 70% probability
> thereof). If you're curious as to exactly _why_ the filter thought that,
> check the email headers - it's all there, in exhaustive detail.

Hilariously, I have occasionally received spam where the spammer included 
SPAM in their subject line under the mistaken impression that this sign 
of honesty would make me more likely to read the body of the email.

Actually, in hindsight, not so mistaken really... 



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


Fwd: class print method...

2011-12-05 Thread Suresh Sharma
Pls help its really frustrating
-- Forwarded message --
From: Suresh Sharma
Date: Monday, December 5, 2011
Subject: class print method...
To: "d...@davea.name" 


Dave,
Thanx for the quick response, i am sorry that i did not explain correctly
look at the code below inspite of this i am just getting class object at
memory location.I am sort i typed all this code on my android in a hurry
so.indentation could.not.be.managed but this.similar code when i run all my
objects created by class deck are not shown but stored in varioia meory
locations. How can i display them.

Please help




Suit=[aces,clubs,diamonds,hearts]
Rank=[2,3,4,5,6,7,8,9,j,Q,K,A]
class Card:
Def __init__(self,rannk,suiit):
Self.suiit=suiit
Self.rannk=rannk

Def __str__(self):
Return suit[suiit],rank[rannk]

Class deck:
Def __init__(cards):
Self.cards=[ ]
For suit in range(4):
For rank in range(13):
Self.cards.append(Card(suit,rank))

Def __str__(self):
s = ""
For card in self.cards:
S = str(self.cards)
Return s.






On Monday, December 5, 2011, Dave Angel  wrote:
> On 12/05/2011 02:11 AM, Suresh Sharma wrote:
>>
>> Hello All,
>> I am new to python and i have stuck up on a particular issue with
classes,
>> i understand this might be a very dumb question but please help me out.
>>
>> I have created two classes and whenever i try to print the objects i get
>> this message but not the data,  __main__.cards instance at (memory
>> location) i even tried using __str__ but calling it also produces the
same
>> result. Can anyone please help me how to get my objects printed. I
googled
>> a lot but could not find anything relevant.
>>
>> thanks in advance
>>
>> regards
>> suresh
>>
> You were close, but you have it backward.  You don't call __str__() to
print an object, you implement __str__() in your object.
>
> If you write a class without also writing __str__(), then print won't
know what to do with it.
>
> --
>
> DaveA
>
>

--
Suresh Sharma
Regional Project Manager,
O2F,Mumbai
Maharashtra-400101.






-- 
Suresh Sharma
Regional Project Manager,
O2F,Mumbai
Maharashtra-400101.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: class print method...

2011-12-05 Thread Chris Angelico
On Mon, Dec 5, 2011 at 10:18 PM, Suresh Sharma  wrote:
>
> Suit=[aces,clubs,diamonds,hearts]
> Rank=[2,3,4,5,6,7,8,9,j,Q,K,A]
> class Card:
> Def __init__(self,rannk,suiit):
> Self.suiit=suiit
> Self.rannk=rannk
>
> Def __str__(self):
> Return suit[suiit],rank[rannk]
>
> Class deck:
> Def __init__(cards):
> Self.cards=[ ]
> For suit in range(4):
> For rank in range(13):
> Self.cards.append(Card(suit,rank))
>
> Def __str__(self):
> s = ""
> For card in self.cards:
> S = str(self.cards)
> Return s.

Python is case sensitive, and indentation is significant. Paste your
code exactly as you're trying to run it, and we'll be better able to
help you.

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


Re: order independent hash?

2011-12-05 Thread 88888 Dihedral
On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote:
> Two methods:
> 1) If you need your hash only once in an infrequent while, then save
> the elements in a list, appending as needed, and sort prior to
> hashing, as needed
> 
> 2) If you need your hash more often, you could keep your elements in a
> treap or red-black tree; these will maintain sortedness throughout the
> life of the datastructure.
> 
> 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound
> appealing, then you might try this one: Create some sort of mapping
> from your elements to the integers.  Then just use a sum.  This won't
> scatter things nearly as well as a cryptographic hash, but it's very
> fast, because you don't need to reevaluate some of your members as you
> go.
> 
> HTH
> 
A sorted list can behave like a hash table.  This is of O(log(n)) in accesses 
of n items in theory. 

I agree with you a hash key computation method too slow than a list of n items  
in accesses for a range of n items  should be reloadable.

But this is not supported in Python yet.  

For tedious trivial jobs of non-heavy computing , I'll opt for easy use. 
  

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


Re: 70% [* SPAM *] multiprocessing.Queue blocks when sending large object

2011-12-05 Thread Lie Ryan

On 11/30/2011 06:09 AM, DPalao wrote:

Hello,
I'm trying to use multiprocessing to parallelize a code. There is a number of
tasks (usually 12) that can be run independently. Each task produces a numpy
array, and at the end, those arrays must be combined.
I implemented this using Queues (multiprocessing.Queue): one for input and
another for output.
But the code blocks. And it must be related to the size of the item I put on
the Queue: if I put a small array, the code works well; if the array is
realistically large (in my case if can vary from 160kB to 1MB), the code
blocks apparently forever.
I have tried this:
http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-large-
objects/
but it didn't work (especifically I put a None sentinel at the end for each
worker).

Before I change the implementation,
is there a way to bypass this problem with  multiprocessing.Queue?
Should I post the code (or a sketchy version of it)?


Transferring data over multiprocessing.Queue involves copying the whole 
object across an inter-process pipe, so you need to have a reasonably 
large workload in the processes to justify the cost of the copying to 
benefit from running the workload in parallel.


You may try to avoid the cost of copying by using shared memory 
(http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes); 
you can use Queue for communicating when a new data comes in or when a 
task is done, but put the large data in shared memory. Be careful not to 
access the data from multiple processes concurrently.


In any case, have you tried a multithreaded solution? numpy is a C 
extension, and I believe it releases the GIL when working, so it 
wouldn't be in your way to achieve parallelism.


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


Re: Fwd: class print method...

2011-12-05 Thread Lie Ryan

On 12/05/2011 10:18 PM, Suresh Sharma wrote:


Pls help its really frustrating
-- Forwarded message --
From: Suresh Sharma
Date: Monday, December 5, 2011
Subject: class print method...
To: "d...@davea.name " mailto:d...@davea.name>>


Dave,
Thanx for the quick response, i am sorry that i did not explain
correctly look at the code below inspite of this i am just getting class
object at memory location.I am sort i typed all this code on my android
in a hurry so.indentation could.not.be.managed but this.similar code
when i run all my objects created by class deck are not shown but stored
in varioia meory locations. How can i display them.



I think you're in the right track, however I suspect you're running the 
code in the shell instead of as a script. The shell uses __repr__() to 
print objects instead of __str__(), so you either need to use 'print' or 
you need to call str(), note the following:


Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> suits = ['spades', 'clubs', 'diamonds', 'hearts']
>>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 
'Q', 'K']

>>> class Card:
... def __init__(self, rank, suit):
... self.suit = suit
... self.rank = rank
... def __str__(self):
... return suits[self.suit] + ' ' + ranks[self.rank]
...
>>> Card(2, 3) #1
<__main__.Card instance at 0x7f719c3a20e0>
>>> str(Card(2, 3)) #2 of your
'hearts 3'
>>> print Card(2, 3) #3
hearts 3

In #1, the output is the __repr__() of your Card class; you can modify 
this output by overriding the __repr__() on your Card class.


In #2, the output is the __repr__() of a string, the string is the 
return value from __str__() of your Card class. The repr of a string is 
the string enclosed in quotes, which is why there is an extra pair of 
quotes.


In #3, you're 'print'-ing a string, the string is the return value from 
__str__() of your Card class. There's no extra quotes, since 'print' 
prints the string itself, not the repr of the string.


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


Re: Install Python on Windows without Start Menu icons?

2011-12-05 Thread Lie Ryan

On 12/05/2011 07:01 PM, Wolfgang Strobl wrote:

"Pedro Henrique G. Souto":


On 02/12/2011 16:34, snorble wrote:

Is it possible to automate the Python installation on Windows using
the MSI file so it does not add a Start Menu folder? I would like to
push out Python to all of my office workstations, but I'd like for it
to be relatively silent from the user's point of view.


If you just want to run python scripts in those machines (not developing
in it), you can use something like py2exe [http://www.py2exe.org/].


That doesn't answer the question.


But it may solve his problem, which is the whole point.

Often in discussions, an OP may ask the wrong question that does not 
solve their problem or is a roundabout way to solve their problem (and 
it may not necessarily be their fault that they asked the wrong 
question). Which is why it is often best to describe the actual problem 
in addition to asking a specific question.


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


Re: Fwd: class print method...

2011-12-05 Thread Dave Angel

On 12/05/2011 07:41 AM, Lie Ryan wrote:

On 12/05/2011 10:18 PM, Suresh Sharma wrote:


Pls help its really frustrating
-- Forwarded message --
From: Suresh Sharma
Date: Monday, December 5, 2011
Subject: class print method...
To: "d...@davea.name " mailto:d...@davea.name>>


Dave,
Thanx for the quick response, i am sorry that i did not explain
correctly look at the code below inspite of this i am just getting class
object at memory location.I am sort i typed all this code on my android
in a hurry so.indentation could.not.be.managed but this.similar code
when i run all my objects created by class deck are not shown but stored
in varioia meory locations. How can i display them.



I think you're in the right track, however I suspect you're running 
the code in the shell instead of as a script. The shell uses 
__repr__() to print objects instead of __str__(), so you either need 
to use 'print' or you need to call str(), note the following:


Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> suits = ['spades', 'clubs', 'diamonds', 'hearts']
>>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 
'Q', 'K']

>>> class Card:
... def __init__(self, rank, suit):
... self.suit = suit
... self.rank = rank
... def __str__(self):
... return suits[self.suit] + ' ' + ranks[self.rank]
...
>>> Card(2, 3) #1
<__main__.Card instance at 0x7f719c3a20e0>
>>> str(Card(2, 3)) #2 of your
'hearts 3'
>>> print Card(2, 3) #3
hearts 3

In #1, the output is the __repr__() of your Card class; you can modify 
this output by overriding the __repr__() on your Card class.


In #2, the output is the __repr__() of a string, the string is the 
return value from __str__() of your Card class. The repr of a string 
is the string enclosed in quotes, which is why there is an extra pair 
of quotes.


In #3, you're 'print'-ing a string, the string is the return value 
from __str__() of your Card class. There's no extra quotes, since 
'print' prints the string itself, not the repr of the string.


Lie is most likely correct.   When I saw your message, i saw many bugs, 
but didn't know which were caused by your retyping, and which were 
actually there.  I also saw no mainline.  While i was deciding how to 
respond, Lie came up with his answer.


Notice that he fixed lots of your bugs, and explains the distinction of 
print, as you'd use in your program, and the output of the debugger (or 
some IDEs.  There are other places where defining a __str__() can be 
useful, such as if you print a list.  The __str__() method of list calls 
the __repr__() for each object inside it.  So it can be useful to define 
both, even if you decide to have them do the same thing.


Lie didn't include your Deck class, which has more problems.  If you 
really want print to work on the whole Deck, you're going to have to 
build a single string for the whole thing.  Currently you just return 
the name of the first card.   Instead of return inside the loop, you'll 
have to construct a string (perhaps by concatenating a new value each 
time through), and put the return after the loop.


Notice also that you top-posted, which is the wrong place for your 
comments.  Please put your comments after whatever part you're quoting 
from other messages, as Lie and I both demonstrate.



--

DaveA

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


Re: Can't install pycrypto

2011-12-05 Thread becky_lewis
My best guess from the error and a quick look at the setup.py file is
that during setup "chmod 0755 configure" is trying to run but failing.
I'm guessing that you're on windows and don't actually have chmod,
hence the error.

The project on github looks active so you could go and ask for a
windows friendly setup.py file and install using:
python setup.py install

or you could attempt to edit it yourself. I'd suggest asking for a
windows friendly file on github as they will either give you a reason
for it not being possible or will make the fix and everybody can
benefit :)

Hope that helps,

Becky Lewis


On Dec 5, 8:20 am, Alec Taylor  wrote:
> Good afternoon,
>
> Unfortunately my pycrpto install fails (tried with pip, easy_install
> and pip -e git+)
>
> Error:http://pastebin.com/wjjfTZvd
>
> How do I get this working?
>
> Thanks for all suggestions,
>
> Alec Taylor

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


How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread patrickwayodi
Hi,
How can I install Python on Debian GNU/Linux? I downloaded the file
"Python-2.7.2.tar.bz2" but I don't know how to install it.

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


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 1:53 AM, patrickwayodi  wrote:
> Hi,
> How can I install Python on Debian GNU/Linux? I downloaded the file
> "Python-2.7.2.tar.bz2" but I don't know how to install it.

You should actually already have Python installed. Try typing 'python'
at a terminal and see if it invokes the interactive interpreter.

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


Re: Install Python on Windows without Start Menu icons?

2011-12-05 Thread Brian Curtin
On Fri, Dec 2, 2011 at 12:34, snorble  wrote:

> Is it possible to automate the Python installation on Windows using
> the MSI file so it does not add a Start Menu folder? I would like to
> push out Python to all of my office workstations, but I'd like for it
> to be relatively silent from the user's point of view.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

http://www.python.org/getit/releases/2.4/msi/ outlines some of the MSI
command line features, but I don't see the option to not add the Start Menu
folder.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread patrickwayodi
> You should actually already have Python installed. Try typing 'python'
> at a terminal and see if it invokes the interactive interpreter.
>
> ChrisA


Yes, I have Python installed, but it's an old version. So I want to
upgrade to "Python-2.7.2.tar.bz2".

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


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi  wrote:
>> You should actually already have Python installed. Try typing 'python'
>> at a terminal and see if it invokes the interactive interpreter.
>>
>> ChrisA
>
>
> Yes, I have Python installed, but it's an old version. So I want to
> upgrade to "Python-2.7.2.tar.bz2".

Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at
least, you can on the Ubuntu system next to me. Not sure though; I
build my Python from source straight from Mercurial.

What you have there, I think, is a source code snapshot. You'd need to
extract it and then do the usual incantation:
$ ./configure
$ make
$ sudo make install

If you don't have the compiler/build environment set up, you'll have
to do that first. If you aren't too concerned about the exact version
you get, the above apt-get line should get you a stable Python in the
2.7 branch.

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


Backspace does not erase in stdout

2011-12-05 Thread Steven D'Aprano
I have a function which reads characters from stdin and writes stars to 
stdout, but backspacing does not erase the stars as I expected.

Tested in Python 2.6 on Linux. This will almost certainly not work on 
Windows.

import sys, tty, termios
def getpass():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
chars = []
try:
tty.setraw(sys.stdin.fileno())
while 1:
c = sys.stdin.read(1)
if c in '\n\r':  # Enter or Return key.
break
elif c == '\x7f':  # Backspace key.
if chars:
# Rubout previous character.
sys.stdout.write('\b')
sys.stdout.flush()
del chars[-1]
else:
# Obfuscate the letter typed.
sys.stdout.write('*')
chars.append(c)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
sys.stdout.write('\n')
return ''.join(chars)


When I call this function and then type, I get a serious of asterisks as 
expected. Each time I press the backspace key, the cursor moves one 
character to the left, but the asterisks remain visible.

Is there a way to erase the character other than backspacing, writing a 
space, then backspacing again? That feels inelegant.



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


Re: Backspace does not erase in stdout

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
 wrote:
>
> Is there a way to erase the character other than backspacing, writing a
> space, then backspacing again? That feels inelegant.

Emitting "\b \b" is one very common way to do a destructive backspace.
Inelegant? Perhaps, but a common inelegance.

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


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Steven D'Aprano
On Tue, 06 Dec 2011 02:51:21 +1100, Chris Angelico wrote:

> On Tue, Dec 6, 2011 at 2:40 AM, patrickwayodi 
> wrote:
>>> You should actually already have Python installed. Try typing 'python'
>>> at a terminal and see if it invokes the interactive interpreter.
>>>
>>> ChrisA
>>
>>
>> Yes, I have Python installed, but it's an old version. So I want to
>> upgrade to "Python-2.7.2.tar.bz2".
> 
> Ah gotcha. I believe you can 'sudo apt-get install python2.7' - at
> least, you can on the Ubuntu system next to me. Not sure though; I build
> my Python from source straight from Mercurial.
> 
> What you have there, I think, is a source code snapshot. You'd need to
> extract it and then do the usual incantation: $ ./configure
> $ make
> $ sudo make install

And you have now potentially broken your system python :(

Generally speaking, unless you are an expert, you should not use make 
install when installing Python from source, because it will replace the 
system Python with the newly installed one.

Instead use `sudo make altinstall`. This is exactly the same as install, 
except it won't replace the python symlink that points to the actual 
Python executable. That way system tools that call Python get the version 
they are expecting, together with any libraries installed for their use, 
while you can call the version you prefer manually. Or set up an alias in 
your bashrc file.


> If you don't have the compiler/build environment set up, you'll have to
> do that first.

The trickiest part for me is ensuring that tkinter works correctly. After 
installing Python from source about a dozen times now, I still don't know 
why sometimes it works and sometimes it doesn't.


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


Re: Backspace does not erase in stdout

2011-12-05 Thread Grant Edwards
On 2011-12-05, Chris Angelico  wrote:
> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
> wrote:
>>
>> Is there a way to erase the character other than backspacing, writing a
>> space, then backspacing again? That feels inelegant.
>
> Emitting "\b \b" is one very common way to do a destructive backspace.
> Inelegant? Perhaps, but a common inelegance.

That's pretty much the only way I've seen it done for the past 25
years.

What would be more elegant?

-- 
Grant Edwards   grant.b.edwardsYow! This PIZZA symbolizes
  at   my COMPLETE EMOTIONAL
  gmail.comRECOVERY!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano
 wrote:
> Generally speaking, unless you are an expert, you should not use make
> install when installing Python from source, because it will replace the
> system Python with the newly installed one.
>
> Instead use `sudo make altinstall`

Mea culpa, forgot that. Yes, use altinstall. Although it's probably
not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many
things.

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


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Christian Heimes
Am 05.12.2011 17:23, schrieb Steven D'Aprano:
> The trickiest part for me is ensuring that tkinter works correctly. After 
> installing Python from source about a dozen times now, I still don't know 
> why sometimes it works and sometimes it doesn't.

sudo apt-get build-dep python2.7

Done ;)

However there are additional pitfalls if you have multiarch support or
Kernel 3.x. Both are well documented in my blog
http://lipyrary.blogspot.com/

Christian

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


Re: How to install Python on Debian GNU/Linux (Python-2.7.2.tar.bz2)

2011-12-05 Thread Christian Heimes
Am 05.12.2011 17:28, schrieb Chris Angelico:
> On Tue, Dec 6, 2011 at 3:23 AM, Steven D'Aprano
>  wrote:
>> Generally speaking, unless you are an expert, you should not use make
>> install when installing Python from source, because it will replace the
>> system Python with the newly installed one.
>>
>> Instead use `sudo make altinstall`
> 
> Mea culpa, forgot that. Yes, use altinstall. Although it's probably
> not a problem to replace 2.6.6 with 2.7.2 - I doubt that'll break many
> things.

Except that all 3rd party extensions and packages are missing if you
install Python manually. Unless you *really* know what you are doing you
shouldn't install Python manually. Debian's backports should provide a
well integrated Python 2.7 package.

Christian

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


Re: Backspace does not erase in stdout

2011-12-05 Thread Steven D'Aprano
On Mon, 05 Dec 2011 16:23:55 +, Grant Edwards wrote:

> On 2011-12-05, Chris Angelico  wrote:
>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
>> wrote:
>>>
>>> Is there a way to erase the character other than backspacing, writing
>>> a space, then backspacing again? That feels inelegant.
>>
>> Emitting "\b \b" is one very common way to do a destructive backspace.
>> Inelegant? Perhaps, but a common inelegance.
> 
> That's pretty much the only way I've seen it done for the past 25 years.
> 
> What would be more elegant?

For backspace to actually backspace, and not just move the cursor.

Thanks for those who answered, I guess I'll just do the backspace, space, 
backspace dance.


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


Re: Backspace does not erase in stdout

2011-12-05 Thread Grant Edwards
On 2011-12-05, Steven D'Aprano  wrote:
> On Mon, 05 Dec 2011 16:23:55 +, Grant Edwards wrote:
>
>> On 2011-12-05, Chris Angelico  wrote:
>>> On Tue, Dec 6, 2011 at 3:09 AM, Steven D'Aprano
>>> wrote:

 Is there a way to erase the character other than backspacing, writing
 a space, then backspacing again? That feels inelegant.
>>>
>>> Emitting "\b \b" is one very common way to do a destructive backspace.
>>> Inelegant? Perhaps, but a common inelegance.
>> 
>> That's pretty much the only way I've seen it done for the past 25 years.
>> 
>> What would be more elegant?
>
> For backspace to actually backspace, and not just move the cursor.

Ah, but "just move the cursor" is what backspace has always meant. 
It's been that way for 100 years -- since the days of typewriters and
teletypes.  ;)

> Thanks for those who answered, I guess I'll just do the backspace,
> space, backspace dance.

After thinking a while, I do remember one program I ran across
recently that when you hit backspace would erase the entire line, then
rewrite the entire line stopping one character short of where it was
before.  Even at network speeds it was noticable when the link was
encrypted.

-- 
Grant Edwards   grant.b.edwardsYow! Inside, I'm already
  at   SOBBING!
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Consider the following django snippet.  Song(id) raises DoesNotExist if the id 
is unknown.

try:
songs = [Song(id) for id in song_ids]
except Song.DoesNotExist:
print "unknown song id (%d)" % id

Is id guaranteed to be in scope in the print statement?  I found one thread 
(http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) 
which says yes, but hints that it might not always be in the future.  Now that 
we're in the future, is that still true?  And for Python 3 also?

The current docs, 
http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are 
mute on this point.
-- 
http://mail.python.org/mailman/listinfo/python-list


70% [* SPAM *] Re: Re: multiprocessing.Queue blocks when sending large object

2011-12-05 Thread DPalao
El Lunes Diciembre 5 2011, b...@mail.python.org escribió:
> On Mon, 5 Dec 2011 09:02:08 +0100, DPalao 
> 
> wrote:
> >El Martes Noviembre 29 2011, DPalao escribió:
> >> Hello,
> >> I'm trying to use multiprocessing to parallelize a code. There is a
> >> number of tasks (usually 12) that can be run independently. Each task
> >> produces a numpy array, and at the end, those arrays must be combined.
> >> I implemented this using Queues (multiprocessing.Queue): one for input
> >> and another for output.
> >> But the code blocks. And it must be related to the size of the item I
> >> put on the Queue: if I put a small array, the code works well; if the
> >> array is realistically large (in my case if can vary from 160kB to
> >> 1MB), the code blocks apparently forever.
> >> I have tried this:
> >> http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-
> >> lar ge- objects/
> >> but it didn't work (especifically I put a None sentinel at the end for
> >> each worker).
> >> 
> >> Before I change the implementation,
> >> is there a way to bypass this problem with  multiprocessing.Queue?
> >> Should I post the code (or a sketchy version of it)?
> >> 
> >> TIA,
> >> 
> >> David
> >
> >Just for reference. The other day I found the explanation by "ryles" on
> >his/her mail of 27th aug 2009, with title "Re: Q: multiprocessing.Queue
> >size limitations or bug...". It is very clarifying.
> >After having read that I arranged the program such that the main process
> >did not need to know when the others have finished, so I changed the
> >process join call with a queue get call, until a None (one per process)
> >is returned.
> >
> >Best,
> >
> >David
> 
> Why do people add character  like[* SPAM *]  to their  subject
> lines ??   Is it supposed to do something  ??   I figured since
> programmers hang out here, maybe one of you know this.
> 
> Thanks,
> boB

Obviously it was not me who added the disgusting "70% [* SPAM *]" string to 
the subject. And I'd like to know the answer too.

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Jussi Piitulainen
Roy Smith writes:

> Consider the following django snippet.  Song(id) raises DoesNotExist
> if the id is unknown.
> 
> try:
> songs = [Song(id) for id in song_ids]
> except Song.DoesNotExist:
> print "unknown song id (%d)" % id
> 
> Is id guaranteed to be in scope in the print statement?  I found one
> thread
> (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html)
> which says yes, but hints that it might not always be in the future.
> Now that we're in the future, is that still true?  And for Python 3
> also?

Another id is in scope in this Python3 example (3.1.2, Ubuntu):

>>> try:
...songs = [1/0 for id in [1]]
... except Exception:
...print('Caught', id)
... 
Caught 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Chris Kaynor
On Mon, Dec 5, 2011 at 9:04 AM, Roy Smith  wrote:

> Consider the following django snippet.  Song(id) raises DoesNotExist if
> the id is unknown.
>
>try:
>songs = [Song(id) for id in song_ids]
>except Song.DoesNotExist:
>print "unknown song id (%d)" % id
>
> Is id guaranteed to be in scope in the print statement?  I found one
> thread (
> http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html)
> which says yes, but hints that it might not always be in the future.  Now
> that we're in the future, is that still true?  And for Python 3 also?
>

In Python2, id will always be in scope, unless the first iteration fails
(aka, an empty iterable, or a custom iterable which raises an exception
before yielding a result).
I believe in Python3, the scoping of list comprehensions was changed to
match that of generator expressions, which is that the loop variable(s)
fall out of scope outside of the expression.


>
> The current docs,
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions,
> are mute on this point.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Peter Otten
Roy Smith wrote:

> Consider the following django snippet.  Song(id) raises DoesNotExist if
> the id is unknown.
> 
> try:
> songs = [Song(id) for id in song_ids]
> except Song.DoesNotExist:
> print "unknown song id (%d)" % id
> 
> Is id guaranteed to be in scope in the print statement?  I found one
> thread
> (http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html)
> which says yes, but hints that it might not always be in the future.  Now
> that we're in the future, is that still true?  And for Python 3 also?
> 
> The current docs,
> http://docs.python.org/tutorial/datastructures.html#list-comprehensions,
> are mute on this point.

If you are using a generator expression id will already be out of scope in 
Python 2. In Python 3 list comprehensions have been changed to work the same 
way:

$ cat song.py
class DoesNotExist(Exception):
pass

class Song:
def __init__(self, id):
if id == 2:
raise DoesNotExist

ids = [1, 2]
try:
songs = [Song(i) for i in ids]
except DoesNotExist as e:
print("song #%d does not exist" % i)
$ python song.py
song #2 does not exist
$ python3 song.py
Traceback (most recent call last):
  File "song.py", line 11, in 
songs = [Song(i) for i in ids]
  File "song.py", line 11, in 
songs = [Song(i) for i in ids]
  File "song.py", line 7, in __init__
raise DoesNotExist
__main__.DoesNotExist

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "song.py", line 13, in 
print("song #%d does not exist" % i)
NameError: name 'i' is not defined
$


$ cat song_gen.py
class DoesNotExist(Exception):
pass

class Song:
def __init__(self, id):
if id == 2:
raise DoesNotExist

ids = [1, 2]
try:
songs = list(Song(i) for i in ids)
except DoesNotExist as e:
print("song #%d does not exist" % i)
$ python  song_gen.py
Traceback (most recent call last):
  File "song_gen.py", line 13, in 
print("song #%d does not exist" % i)
NameError: name 'i' is not defined

So you'd rather store the id in the exception.

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


Pythonbrew is not installing Python 2.7 successfully

2011-12-05 Thread Patrick Wayodi
Hi,
I'm trying to install Python 2.7 using Pythonbrew on a Debian
GNU/Linux system, but I've failed. This is the output I got on my
terminal:


root@debian:~# pythonbrew install /home/guest/python27
Copying /home/guest/python27 into /root/.pythonbrew/build/Python-python27

This could take a while. You can run the following command on another
shell to track the status:
  tail -f /root/.pythonbrew/log/build.log

Installing Python-python27 into /root/.pythonbrew/pythons/Python-python27
ERROR: Failed to install Python-python27. See
/root/.pythonbrew/log/build.log to see why.
  pythonbrew install --force python27
root@debian:~#


The log file build.log was empty. Please help me.

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


70% [* SPAM *] Re: multiprocessing.Queue blocks when sending large object

2011-12-05 Thread DPalao
Hi Lie,
Thank you for the reply.

El Lunes Diciembre 5 2011, Lie Ryan escribió:
> On 11/30/2011 06:09 AM, DPalao wrote:
> > Hello,
> > I'm trying to use multiprocessing to parallelize a code. There is a
> > number of tasks (usually 12) that can be run independently. Each task
> > produces a numpy array, and at the end, those arrays must be combined.
> > I implemented this using Queues (multiprocessing.Queue): one for input
> > and another for output.
> > But the code blocks. And it must be related to the size of the item I put
> > on the Queue: if I put a small array, the code works well; if the array
> > is realistically large (in my case if can vary from 160kB to 1MB), the
> > code blocks apparently forever.
> > I have tried this:
> > http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-l
> > arge- objects/
> > but it didn't work (especifically I put a None sentinel at the end for
> > each worker).
> > 
> > Before I change the implementation,
> > is there a way to bypass this problem with  multiprocessing.Queue?
> > Should I post the code (or a sketchy version of it)?
> 
> Transferring data over multiprocessing.Queue involves copying the whole
> object across an inter-process pipe, so you need to have a reasonably
> large workload in the processes to justify the cost of the copying to
> benefit from running the workload in parallel.
> 
> You may try to avoid the cost of copying by using shared memory
> (http://docs.python.org/library/multiprocessing.html#sharing-state-between-
> processes); you can use Queue for communicating when a new data comes in or
> when a task is done, but put the large data in shared memory. Be careful
> not to access the data from multiple processes concurrently.
> 

Yep, that was my first thought, but the arrays's elements are complex64 (or 
complex in general), and I don't know how to easily convert from 
multiprocessing.Array to/from numpy.array when the type is complex. Doing that 
would require some extra conversions forth and back which make the solution 
not very attractive to me.
I tried with a Manager too, but the array cannot be modified from within the 
worker processes.
 
In principle, the array I need to share is expected to be, at most, ~2MB in 
size, and typically should be only <200kB. So, in principle, there is no huge 
extra workload. But that could change, and I'd like to be prepared for it, so 
any idea about using an Array or a Manager or another shared memory thing 
would be great.

> In any case, have you tried a multithreaded solution? numpy is a C
> extension, and I believe it releases the GIL when working, so it
> wouldn't be in your way to achieve parallelism.

That possibility I didn't know. What does exactly break the GIL? The sharing 
of a numpy array? What if I need to also share some other "standard" python 
data (eg, a dictionary)?

Best regards,

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


Re: LinuxJournal Readers' Choice Awards 2011 Best {Programming, Scripting} Language

2011-12-05 Thread Ron
Hello Wesley,

Thanks for the interesting news from Linux Journal.

Now, enquring minds want to know, when will there be a Core Python 3?

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Jean-Michel Pichavant

Roy Smith wrote:

Consider the following django snippet.  Song(id) raises DoesNotExist if the id 
is unknown.

try:
songs = [Song(id) for id in song_ids]
except Song.DoesNotExist:
print "unknown song id (%d)" % id

Is id guaranteed to be in scope in the print statement?  I found one thread 
(http://mail.python.org/pipermail/python-bugs-list/2006-April/033235.html) 
which says yes, but hints that it might not always be in the future.  Now that 
we're in the future, is that still true?  And for Python 3 also?

The current docs, 
http://docs.python.org/tutorial/datastructures.html#list-comprehensions, are 
mute on this point.
  
For python 2, id will always be defined *as you meant it*. But you're 
doing something wrong : overiding the 'id' builtin function.
With python 3 you will probably print the 'id' builtin function 
representation, which is correct but not want you want to achieve.


The proper way to propagate information with exceptions is using the 
exception itself:


try:
   songs = [Song(_id) for _id in song_ids]
except Song.DoesNotExist, exc:
   print exc

class DoesNotExist(Exception):
   def __init__(self, songId):
   self.songId = songId
   def __str__(self):
   return "Unkown Song Id %s" % self.songId

class Song:
   def __init__(self, songId):
if whatever:
raise DoesNotExist(songId)
   self.id=songId

JM

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Hmmm, the use of id was just a simplification for the sake of posting.  The 
real code is a bit more complicated and used a different variable name, but 
that's a good point.

As far as storing the value in the exception, unfortunately, DoesNotExist is 
not my exception; it comes from deep within django.  I'm just passing it along.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: class print method...

2011-12-05 Thread Terry Reedy

On 12/5/2011 8:09 AM, Dave Angel wrote:


useful, such as if you print a list. The __str__() method of list calls
the __repr__() for each object inside it. So it can be useful to define
both, even if you decide to have them do the same thing.


If you want __str__ and __repr__ to do the same thing, you only need 
*one* def statement and an assignment.


def __str__(self): 
__repr__ = __str__

--
Terry Jan Reedy

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


Re: How to generate java .properties files in python

2011-12-05 Thread Serhiy Storchaka

03.12.11 23:34, Arnaud Delobelle написав(ла):

Is there a simple way to achieve this? I could do something like this:

def encode(u):
 """encode a unicode string in .properties format"""
 return u"".join(u"\\u%04x" % ord(c) if ord(c)>  0xFF else c for c
in u).encode("latin_1")


You must also encode backslash ('\\'), whitespaces and control 
characters (ord(c)<=32), '=' and ':' (key/value delimiters), '#' 
(comment) and '!'.


And don't forget characters with code >0x.

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Terry Reedy

On 12/5/2011 2:15 PM, Roy Smith wrote:

Hmmm, the use of id was just a simplification for the sake of
posting.  The real code is a bit more complicated and used a
different variable name, but that's a good point.

As far as storing the value in the exception, unfortunately,
DoesNotExist is not my exception; it comes from deep within django.
I'm just passing it along.


It is hard to sensibly answer a question when the questioner 
*significantly* changes the problem in the process of 'simplifying' it. 
Both of those are significant changes ;-)


Changing a name to a built-in name is a complexification, not a 
simplification, because it introduces new issues that were not in the 
original.


Changing the exception from one you do not control to one you apparently 
do also changes the appropriate answer. If you do not control the 
exception and you want guaranteed access to the loop variable with 
Python 3 (and the upgrade of django to work with Python 3 is more or 
less done), then use an explicit loop. If you want the loop to continue 
after an error, instead of stopping, you can put the try/except within 
the loop, instead of without. This possibility is one advantage of using 
an explicit loop.


songs = []
for song_id in song_ids:
  try:
songs.append(Song(song_id))
  except django.error:
print("unknown song id {}".format(song_id))

--
Terry Jan Reedy

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


Re: How to generate java .properties files in python

2011-12-05 Thread Arnaud Delobelle
On 5 December 2011 20:05, Serhiy Storchaka  wrote:
> 03.12.11 23:34, Arnaud Delobelle написав(ла):
>
>> Is there a simple way to achieve this? I could do something like this:
>>
>> def encode(u):
>>     """encode a unicode string in .properties format"""
>>     return u"".join(u"\\u%04x" % ord(c) if ord(c)>  0xFF else c for c
>> in u).encode("latin_1")
>
>
> You must also encode backslash ('\\'), whitespaces and control characters
> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'.

Fortunately there aren't any of these in the keys.

> And don't forget characters with code >0x.

I haven't thought of these.  I don't think that I have any, but I'll
check.  Do you know how they are encoded?

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


Re: Install Python on Windows without Start Menu icons?

2011-12-05 Thread snorble
On Dec 5, 2:01 am, Wolfgang Strobl  wrote:
> "Pedro Henrique G. Souto" :
>
> >On 02/12/2011 16:34, snorble wrote:
> >> Is it possible to automate the Python installation on Windows using
> >> the MSI file so it does not add a Start Menu folder? I would like to
> >> push out Python to all of my office workstations, but I'd like for it
> >> to be relatively silent from the user's point of view.
>
> >If you just want to run python scripts in those machines (not developing
> >in it), you can use something like py2exe [http://www.py2exe.org/].
>
> That doesn't answer the question.
>
> Snorble might use  "ORCA", a MSI editor from Microsoft.
>
> http://forums.frontmotion.com/viewtopic.php?f=10&t=837
>
> discusses a similar question for Firefox.
>
> I haven't tried it myself, but would give it a try.
>
> --
> Thank you for observing all safety precautions

Thank you! I got it working using Orca. Initially I had removed all of
the rows from the Shortcut table and saved the .msi file, but the
resulting .msi file was only about 600 KB (the original .msi file is
about 15 MB). I had to create a .mst transform file, and that allows
Python to install without any Start Menu entries.

For reference, here's what I did. I opened the python-2.7.2.msi file
in Orca, then click the Transform menu, then New Transform. Then go to
the Shortcut table and highlight all of the entries and press the
Delete key, and acknowledge that it will delete all of the rows. Then
click the Transform menu and click Generate Transform and save it
(python-2.7.2-no-icons.mst in my case).

Once you have the .mst file, you can install it from the command line:

msiexec /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst

Or to silently install:

msiexec /qn /i python-2.7.2.msi TRANSFORMS=python-2.7.2-no-icons.mst
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANNOUNCE] pypiserver 0.5.0 - minimal pypi server

2011-12-05 Thread Ralf Schmitt
Hi,

I've just uploaded pypiserver 0.5.0 to the python package index.

pypiserver is a minimal PyPI compatible server. It can be used to serve
a set of packages and eggs to easy_install or pip.

pypiserver is easy to install (i.e. just easy_install pypiserver). It
doesn't have any external dependencies.

http://pypi.python.org/pypi/pypiserver/ should contain enough
information to easily get you started running your own PyPI server in a
few minutes.

The code is available on github: https://github.com/schmir/pypiserver


Changes in version 0.5.0
-
- make setup.py install without calling 2to3 by changing source code
  to be compatible with both python 2 and python 3. We now ship a
  slightly patched version of bottle. The upcoming bottle 0.11
  also contains these changes.
- make the single-file pypi-server-standalone.py work with python 3

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


Re: How to generate java .properties files in python

2011-12-05 Thread Serhiy Storchaka
05.12.11 22:25, Arnaud Delobelle написав(ла):
> On 5 December 2011 20:05, Serhiy Storchaka  wrote:
>> You must also encode backslash ('\\'), whitespaces and control characters
>> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'.
> Fortunately there aren't any of these in the keys.

"a=b" is same as "a= b".

>> And don't forget characters with code>0x.
> I haven't thought of these.  I don't think that I have any, but I'll
> check.  Do you know how they are encoded?

Java uses UTF-16.

if i>=0x11:
raise some exception
if i>=0x1:
return '\\u%04X\\u%04X' % (0xD800+((i-0x1)>>10), 0xDC00+(i&0x3FF))

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


Re: Backspace does not erase in stdout

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 4:40 AM, Grant Edwards  wrote:
> After thinking a while, I do remember one program I ran across
> recently that when you hit backspace would erase the entire line, then
> rewrite the entire line stopping one character short of where it was
> before.  Even at network speeds it was noticable when the link was
> encrypted.

I've seen a few programs that do that. Benefit is that it works even
when (a) you're doing more than just "backspace one letter" (eg
deleting an entire word), and (b) when your logical line wraps over
multiple physical lines. It gets ugly fast, though.

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


loop through arrays and find maximum

2011-12-05 Thread questions anon
I would like to calculate the max and min across many netcdf files.
I know how to create one big array and then concatenate and find the
numpy.max but when I run this on 1000's of arrays I have a memory error.
What I would prefer is to loop through the arrays and produce the maximum
without having the make a big array.
Does anyone have any ideas as to how I could achieve this?
My idea goes something like:

netCDF_list=[]
maxarray=[]

for dir in glob.glob(MainFolder + '*/01/')+ glob.glob(MainFolder +
'*/02/')+ glob.glob(MainFolder + '*/12/'):
for ncfile in glob.glob(dir + '*.nc'):
netCDF_list.append(ncfile)
for filename in netCDF_list:
ncfile=netCDF4.Dataset(
filename)
TSFC=ncfile.variables['T_SFC'][:]
fillvalue=ncfile.variables['T_SFC']._FillValue
TSFC=MA.masked_values(TSFC, fillvalue)
for i in TSFC:
if i == N.max(TSFC, axis=0):
maxarray.append(i)
else:
pass

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Well, in my defense, I did ask a pretty narrow question, "Is id guaranteed to 
be in scope in the print statement?".  While I will admit that not knowing 
whether I could alter the exception, or whether id masked a builtin or not does 
complexify answering some questions, those are questions I didn't ask :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Steven D'Aprano
On Mon, 05 Dec 2011 19:57:15 +0100, Jean-Michel Pichavant wrote:

> The proper way to propagate information with exceptions is using the
> exception itself:
> 
> try:
> songs = [Song(_id) for _id in song_ids]
> except Song.DoesNotExist, exc:
> print exc


I'm not entirely sure that this is the proper way to propagate the 
exception. I see far to many people catching exceptions to print them, or 
worse, to print a generic, useless message like "an error occurred".

The problem here is that having caught the exception, songs now does not 
exist, and will surely cause another, unexpected, exception in a moment 
or two when the code attempts to use it. Since the error is (apparently) 
unrecoverable, the right way as far as I can see is:

songs = [Song(_id) for _id in song_ids]

allowing any exception to be fatal and the traceback to be printed as 
normal.

If the error is recoverable, you will likely need to do more to recover 
from it than merely print the exception and continue.



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


Re: How to generate java .properties files in python

2011-12-05 Thread Arnaud Delobelle
On 5 December 2011 21:46, Serhiy Storchaka  wrote:
> 05.12.11 22:25, Arnaud Delobelle написав(ла):
>> On 5 December 2011 20:05, Serhiy Storchaka  wrote:
>>> You must also encode backslash ('\\'), whitespaces and control characters
>>> (ord(c)<=32), '=' and ':' (key/value delimiters), '#' (comment) and '!'.
>> Fortunately there aren't any of these in the keys.
>
> "a=b" is same as "a= b".

You're right - all my values are trimmed for whitespace on both sides,
so it's OK for me.  Although if I wanted to make a general purpose
tool I would have to take this into account.
I might just escape all whitespace after all, for safety's sake.

>>> And don't forget characters with code>0x.
>> I haven't thought of these.  I don't think that I have any, but I'll
>> check.  Do you know how they are encoded?
>
> Java uses UTF-16.
>
> if i>=0x11:
>    raise some exception
> if i>=0x1:
>    return '\\u%04X\\u%04X' % (0xD800+((i-0x1)>>10), 0xDC00+(i&0x3FF))

Thanks.  I'm using .encode("latin1", "backslashreplace") now as
suggested by Peter Otten.  I can't see from the docs if this uses
UTF-16 escape sequences.

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Roy Smith
Sigh.  I attempted to reduce this to a minimal example to focus the discussion 
on the question of list comprehension variable scope.  Instead I seem to have 
gotten people off on other tangents.  I suppose I should post more of the real 
code...

song_ids = request.POST.getlist('song_id')
try:
songs = [Song.get(int(id)) for id in song_ids]
except Song.DoesNotExist:
return HttpResponseBadRequest("unknown song id (%d)" % id)

I may be in the minority here, but it doesn't bother me much that my use of 
'id' shadows a built-in.  Especially in small scopes like this, I use whatever 
variable names make the the code easiest to read and don't worry about 
shadowing builtins.  I don't have an exhaustive list of builtins in my head, so 
even if I worried about common ones like file or id, I'm sure I'd miss some 
others.  So I don't sweat it.

If shadowing builtins was really evil, they'd be reserved keywords and then you 
wouldn't be able to do it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: order independent hash?

2011-12-05 Thread Dan Stromberg
On 12/5/11, 8 Dihedral  wrote:
> On Monday, December 5, 2011 1:50:08 PM UTC+8, Dan Stromberg wrote:
>> Two methods:
>> 1) If you need your hash only once in an infrequent while, then save
>> the elements in a list, appending as needed, and sort prior to
>> hashing, as needed
>>
>> 2) If you need your hash more often, you could keep your elements in a
>> treap or red-black tree; these will maintain sortedness throughout the
>> life of the datastructure.
>>
>> 3) If A bunch of log(n) or n or nlog(n) operations doesn't sound
>> appealing, then you might try this one: Create some sort of mapping
>> from your elements to the integers.  Then just use a sum.  This won't
>> scatter things nearly as well as a cryptographic hash, but it's very
>> fast, because you don't need to reevaluate some of your members as you
>> go.
>>
>> HTH
>>
> A sorted list can behave like a hash table.  This is of O(log(n)) in
> accesses
> of n items in theory.
>
> I agree with you a hash key computation method too slow than a list of n
> items  in accesses for a range of n items  should be reloadable.
>
> But this is not supported in Python yet.
>
> For tedious trivial jobs of non-heavy computing , I'll opt for easy use.

A sorted list is O(log(n)) for lookups, but O(n) for insertions.  If
you have a process doing both, the table operations are O(n).

A hash table that isn't overfilled is O(1) for lookups, O(1) for
insertions.  But this is not ordered.

Here's a straightforward treap implementation for python, with pure
python and cython versions:
http://pypi.python.org/pypi/treap/0.995

There's also at least one red-black tree implementation available.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Terry Reedy

On 12/5/2011 5:36 PM, Roy Smith wrote:

Well, in my defense, I did ask a pretty narrow question, "Is id
guaranteed to be in scope in the print statement?".


Yes for 2.x, guaranteed no for 3.x.

If you had simply asked "Is the loop variable of a list comprehension 
guaranteed to be in scope after the list comprehension?", without a 
distracting example, that is the answer you would have received.


I intend(ed) to inform, not attack, hence no 'defense' needed.


 While I will
admit that not knowing whether I could alter the exception, or
whether id masked a builtin or not does complexify answering some
questions, those are questions I didn't ask :-)


Except that it bears on the question you did ask because it means that 
the code will run in 3.x but with different results, whereas a random 
name will fail in 3.x with a NameError.


--
Terry Jan Reedy

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 9:57 AM, Roy Smith  wrote:
> I may be in the minority here, but it doesn't bother me much that my use of 
> 'id' shadows a built-in.  Especially in small scopes like this, I use 
> whatever variable names make the the code easiest to read and don't worry 
> about shadowing builtins.  I don't have an exhaustive list of builtins in my 
> head, so even if I worried about common ones like file or id, I'm sure I'd 
> miss some others.  So I don't sweat it.
>
> If shadowing builtins was really evil, they'd be reserved keywords and then 
> you wouldn't be able to do it.

Agreed. The name 'id' is one that's shadowed benignly in a lot of
code. In the context of the original question, it's not an issue -
there'll be no confusion.

Python's scoping rules are "do what the programmer probably wants".
This works most of the time, but occasionally you get edge cases where
things are a bit weird, and C-style explicit scoping (with infinitely
nested block scope) begins to look better. But for probably 99% of
situations, Python's system "just works".

If you need more flexibility in the exception you throw, it may be
worth putting together a filter:

def HttpSongId(id):
   try:
   return Song.get(int(id))
   except Song.DoesNotExist:
   return HttpResponseBadRequest("unknown song id (%d)" % id)

   song_ids = request.POST.getlist('song_id')
   songs = [HttpSongId(id) for id in song_ids]

This encapsulates things in a somewhat weird way, but if there's any
other work to be done at the same time, you could probably come up
with a better name for the exception filter function.

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


Re: Questions about LISP and Python.

2011-12-05 Thread Xah Lee
On Dec 5, 4:31 am, Tim Bradshaw  wrote:
> On 2011-12-05 11:51:11 +, Xah Lee said:
>
> > python has more readible syntax, more modern computer language
> > concepts, and more robust libraries. These qualities in turn made it
> > popular.
>
> Yet you still post here: why?

i don't like python, and i prefer emacs lisp. The primary reason is
that python is not functional, especially with python 3. The python
community is full of fanatics with their drivels. In that respect,
it's not unlike Common Lisp community and Scheme lisp community.

see also:

〈Python Documentation Problems〉
http://xahlee.org/perl-python/python_doc_index.html

〈Computer Language Design: What's List Comprehension and Why is It
Harmful?〉
http://xahlee.org/comp/list_comprehension.html

〈Lambda in Python 3000〉
http://xahlee.org/perl-python/python_3000.html

〈What Languages to Hate〉
http://xahlee.org/UnixResource_dir/writ/language_to_hate.html

〈Xah on Programing Languages〉
http://xahlee.org/Periodic_dosage_dir/comp_lang.html

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


Re: Questions about LISP and Python.

2011-12-05 Thread Chris Angelico
On Tue, Dec 6, 2011 at 3:36 PM, Xah Lee  wrote:
> i don't like python, and i prefer emacs lisp. The primary reason is
> that python is not functional, especially with python 3. The python
> community is full of fanatics with their drivels. In that respect,
> it's not unlike Common Lisp community and Scheme lisp community.

So you hate Python. Fine. Why post here? Why not just abandon Python
as a dead loss and go code in Lithp?

Clearly something is keeping you here. Is it that there's something
about Python that you really like, or are you just trolling?

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


Single key press

2011-12-05 Thread Sergi Pasoev
Hi.

I wonder if it is realistic to get a single key press in Python
without ncurses or
any similar library. In single key press I mean something like j and k
in Gnu less
program, you press the key and and it is captured by the script without need to
press enter afterwards
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: class print method...

2011-12-05 Thread Suresh Sharma
Dave / Ryan
Thanks i have got it and it worked after using repr statement. Thanks
everyone for their valuable feedback.



On Mon, Dec 5, 2011 at 6:11 PM, Lie Ryan  wrote:

> On 12/05/2011 10:18 PM, Suresh Sharma wrote:
>
>>
>> Pls help its really frustrating
>> -- Forwarded message --
>> From: Suresh Sharma
>> Date: Monday, December 5, 2011
>> Subject: class print method...
>> To: "d...@davea.name " > >
>>
>>
>> Dave,
>> Thanx for the quick response, i am sorry that i did not explain
>> correctly look at the code below inspite of this i am just getting class
>> object at memory location.I am sort i typed all this code on my android
>> in a hurry so.indentation could.not.be.managed but this.similar code
>> when i run all my objects created by class deck are not shown but stored
>> in varioia meory locations. How can i display them.
>>
>>
> I think you're in the right track, however I suspect you're running the
> code in the shell instead of as a script. The shell uses __repr__() to
> print objects instead of __str__(), so you either need to use 'print' or
> you need to call str(), note the following:
>
> Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
> [GCC 4.6.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> suits = ['spades', 'clubs', 'diamonds', 'hearts']
> >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q',
> 'K']
> >>> class Card:
> ... def __init__(self, rank, suit):
> ... self.suit = suit
> ... self.rank = rank
> ... def __str__(self):
> ... return suits[self.suit] + ' ' + ranks[self.rank]
> ...
> >>> Card(2, 3) #1
> <__main__.Card instance at 0x7f719c3a20e0>
> >>> str(Card(2, 3)) #2 of your
> 'hearts 3'
> >>> print Card(2, 3) #3
> hearts 3
>
> In #1, the output is the __repr__() of your Card class; you can modify
> this output by overriding the __repr__() on your Card class.
>
> In #2, the output is the __repr__() of a string, the string is the return
> value from __str__() of your Card class. The repr of a string is the string
> enclosed in quotes, which is why there is an extra pair of quotes.
>
> In #3, you're 'print'-ing a string, the string is the return value from
> __str__() of your Card class. There's no extra quotes, since 'print' prints
> the string itself, not the repr of the string.
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>



-- 
Suresh Sharma
Regional Project Manager,
O2F,Mumbai
Maharashtra-400101.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Single key press

2011-12-05 Thread 88888 Dihedral
On Tuesday, December 6, 2011 1:49:55 PM UTC+8, Sergi Pasoev wrote:
> Hi.
> 
> I wonder if it is realistic to get a single key press in Python
> without ncurses or
> any similar library. In single key press I mean something like j and k
> in Gnu less
> program, you press the key and and it is captured by the script without need 
> to
> press enter afterwards

Sounds like the  fast key searching for a list in the DOS application in the  
old days. 

This is easy in GUI of just tens of items, but for thousands of items such 
as in a directory listing, some API is really slow.  


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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Rainer Grimm
Hello,

> try:
> songs = [Song(id) for id in song_ids]
> except Song.DoesNotExist:
> print "unknown song id (%d)" % id
that's is a bad programming style. So it will be forbidden with python 3. The 
reason is that list comprehension is a construct from the functional world. 
It's only syntactic sugar for the functions map and filter. So functions have 
to be pure functions. To say it in other words, they have to be side-effect 
free. But the python construct from above pollutes the namespace with name id.

Greetings from Rottenburg,
Rainer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about LISP and Python.

2011-12-05 Thread alex23
On Dec 6, 2:36 pm, Xah Lee  wrote:
> The python community is full of fanatics with their drivels.

You do know that you could just fuck right off and leave us to it,
yes?

In general, it's the person who is shrilly imposing their minority
opinion on a disinterested audience that deserves the title 'fanatic'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about LISP and Python.

2011-12-05 Thread Matt Joiner
This guy is an even better troll than that 8 guy. His spelling is
equally bad. His essays make some good points, but I don't see why he
doesn't shut his trap and move on.

ಠ_ಠ



On Tue, Dec 6, 2011 at 6:02 PM, alex23  wrote:
> On Dec 6, 2:36 pm, Xah Lee  wrote:
>> The python community is full of fanatics with their drivels.
>
> You do know that you could just fuck right off and leave us to it,
> yes?
>
> In general, it's the person who is shrilly imposing their minority
> opinion on a disinterested audience that deserves the title 'fanatic'.
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list