Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Steven D'Aprano
On Fri, 18 Oct 2013 13:45:53 -0700, caldwellinva wrote:

> Hi!
> 
> I am looking for an example of a UNICODE to ASCII conversion example
> that will remove diacritics from characters (and leave the characters,
> i.e., Klüft to Kluft) as well as handle the conversion of other
> characters, like große to grosse.

Seems like a nasty thing to do, akin to stripping the vowels from English 
text just because Hebrew didn't write them. But if you insist, there's 
always this:

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

although it is nowhere near complete, and it's pretty ugly code too.

Perhaps a cleaner method might be to use a combination of Unicode 
normalisation forms and a custom translation table. Here's a basic 
version to get you started, written for Python 3:

import unicodedata

# Do this once. It may take a while.
table = {}
for n in range(128, 0x11000):
# Use unichar in Python2
expanded = unicodedata.normalize('NFKD', chr(n))
keep = [c for c in expanded if ord(c) < 128]
if keep:
table[n] = ''.join(keep)
else:
# None to delete, or use some other replacement string.
table[n] = None

# Add extra transformations.
# In Python2, every string needs to be a Unicode string u'xyz'.
table[ord('ß')] = 'ss'
table[ord('\N{LATIN CAPITAL LETTER SHARP S}')] = 'SS'
table[ord('Æ')] = 'AE'
table[ord('æ')] = 'ae'
table[ord('Œ')] = 'OE'
table[ord('œ')] = 'oe'
table[ord('fi')] = 'fi'
table[ord('fl')] = 'fl'
table[ord('ø')] = 'oe'
table[ord('Ð')] = 'D'
table[ord('Þ')] = 'TH'
# etc.

# Say you don't want control characters in your string, you might 
# escape them using caret ^C notation:
for i in range(32):
table[i] = '^%c' % (ord('@') + i)

table[127] = '^?'

# But it's probably best if you leave newlines, tabs etc. alone...
for c in '\n\r\t\f\v':
del table[ord(c)]

# Add any more transformations you like here. Perhaps you want to
# transliterate Russian and Greek characters to English?
table[whatever] = whatever

# In Python2, use unicode.maketrans instead.
table = str.maketrans(table)



That's a fair chunk of work, but it only needs be done once, at the start 
of your application. Then you call it like this:

cleaned = 'some Unicode string'.translate(table)

If you really want to be fancy, you can extract the name of each Unicode 
code point (if it has one!) and parse the name. Here's an example:

py> unicodedata.name('ħ')
'LATIN SMALL LETTER H WITH STROKE'
py> unicodedata.lookup('LATIN SMALL LETTER H')
'h'

but I'd only do that after the normalization step, if at all.

Too much work for your needs? Well, you can get about 80% of the way in 
only a few lines of code:

cleaned = unicodedata.normalize('NFKD', unistr)
for before, after in (
('ß', 'ss'), ('Æ', 'AE'), ('æ', 'ae'), ('Œ', 'OE'), ('œ', 'oe'),
# put any more transformations here...
):
cleaned = cleaned.replace(before, after)

cleaned = cleaned.encode('ascii', 'replace').decode('ascii')


Another method would be this:

http://effbot.org/zone/unicode-convert.htm


which is focused on European languages. But it might suit your purposes.


> There used to be a program called any2ascii.py
> (http://www.haypocalc.com/perso/prog/python/any2ascii.py) that worked
> well, but the link is now broken and I can't seem to locate it.
> 
> I have seen the page Unicode strings to ASCII ...nicely,
> http://www.peterbe.com/plog/unicode-to-ascii, but am looking for a
> working example.

He has a working example. How much hand-holding are you looking for?

Quoting from that page:

I'd much rather that a word like "Klüft" is converted to 
"Kluft" which will be more human readable and still correct.


The author is wrong. That's like saying that changing the English word 
"car" to "cer" is still correct -- it absolutely is not correct, and even 
if it were, what is he implying with the quip about "more human 
readable"? That Germans and other Europeans aren't human?

If an Italian said:

I'd much rather that a word like "jump" is converted to 
"iump" which will be more human readable and still correct.

we'd all agree that he was talking rubbish.

Make no mistake, this sort of simple-minded stripping of accents and 
diacritics is an extremely ham-fisted thing to do. To strip out letters 
without changing the meaning of the words is, at best, hard to do right 
and requiring good knowledge of the linguistic rules of the language 
you're translating. And at worst, it's outright impossible. For instance, 
in German I believe it is quite acceptable to translate 'ü' to 'ue', 
except in names: Herr Müller will probably be quite annoyed if you call 
him Herr Mueller, and Herr Mueller will probably be annoyed too, and both 
of them will be peeved to be confused with Herr Muller.



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


Re: Python was designed (was Re: Multi-threading in Python vs Java)

2013-10-19 Thread Steven D'Aprano
On Wed, 16 Oct 2013 23:49:02 -0700, Peter Cacioppi wrote:

> I don't know if I want to step into the flames here, 

Go on, be bold! You learn a lot by making bold claims and having them 
shot down. Or at least, I did. Now I know everything, so I can afford to 
be humble.

*wink*


> but my
> understanding has always been that in the absence of polymorphism the
> best you can do is "object based" programming instead of "object
> oriented" programming.

Well, that surely depends on the semantics of what you mean by "object 
based" versus "object oriented", and I don't think there is any one hard, 
universally agreed upon definition of those.


> Object based programming is a powerful step forward. The insight that by
> associating data structures and methods together you can significantly
> improve readability and robustness.

This implies that "object-based" simply means that you have *syntax* for 
associating methods with data, i.e. objects. I don't think I would agree 
with that definition. For instance, I often describe Python as "object-
based" in the sense that *all* values in Python are objects, even things 
which would be low-level primitives in some other languages, although you 
can still write procedural, imperative or functional-style code.


> Object oriented programming takes things further, most significantly by
> introducing the idea that the object reference you are referencing might
> be a run time dependent sub-class. 

And I *strongly* disagree with this. I wonder whether you have read this?

http://en.wikipedia.org/wiki/Object-oriented_programming#Fundamental_features_and_concepts

Quote:

Benjamin C. Pierce and some other researchers view any attempt
to distill OOP to a minimal set of features as futile. He 
nonetheless identifies fundamental features that support the
OOP programming style in most object-oriented languages: 
[list of five feature]

Similarly, in his 2003 book, Concepts in programming languages,
John C. Mitchell identifies four main features: [...] Michael
Lee Scott in Programming Language Pragmatics considers only
[three features]


It is notable that polymorphism is *not* one of the three features listed 
by Scott (although it is included by the other two). So I don't agree 
that subtype polymorphism is necessary for OOP.

I can easily conceive of object-oriented languages with inheritance but
no subtype polymorphism. For instance, prototype-based OOP languages have
inheritance, but since they don't really have types in the class-based 
OOP sense, they don't have subtypes, hence no subtype polymorphism.



> Even Python, which isn't strongly typed, 

That's not the case, although that's been discussed in other posts.

> manages polymorphism by allowing the self argument to a sub-class
> of the method class.

I must admit I don't really understand what this sentence is supposed to 
mean.


> There are many wonderful examples of object based programming in C. I
> believe VB (not VB.net, the older VBA language) is object based but not
> object oriented.
> 
> True object oriented programming 

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


> seems to require proper support from
> the language itself, because the run-time resolution of the "this/self"
> reference needs specific constructs in the language.

Again, I don't understand what you are trying to say here. Provided that 
the "this/self" reference has a type, what more does the language need to 
provide? The reference itself is enough to identify the instance (since 
it is the instance!) and the instance's type is enough to identify the 
type (since it is the type!).


> Bear in mind that my usual disclaimer when wading into the flames like
> this is to quote Randy Newman ... "I may be wrong  but I don't think
> so!!"

:-)


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


Re: Python was designed (was Re: Multi-threading in Python vs Java)

2013-10-19 Thread Chris Angelico
On Sat, Oct 19, 2013 at 8:57 PM, Steven D'Aprano
 wrote:
> On Wed, 16 Oct 2013 23:49:02 -0700, Peter Cacioppi wrote:
>
>> I don't know if I want to step into the flames here,
>
> Go on, be bold! You learn a lot by making bold claims and having them
> shot down.

Yes, it's a very effective technique. I just learned another meaning
of the word "trepan" via Savoynet that way. (It's a synonym for its
anagram "entrap", as well as being a surgical operation on the skull.
So now you know, too!)

>> Even Python, which isn't strongly typed,
>> manages polymorphism by allowing the self argument to a sub-class
>> of the method class.
>
> I must admit I don't really understand what this sentence is supposed to
> mean.

As I understand it, there's a little word missing: "... allowing the
self argument to BE a subclass...". That is, in this example:

class A:
def foo(self):
return "spam"
class B(A):
pass

x=B()
print(x.foo())

the method named foo and defined in class A might not get, as its
'self' argument, an instance of class A, but might instead get a
subclass thereof. Thus, polymorphism. Similarly, this C example cheats
a bit, but does work:

struct A
{
/* ... declare members here */
}
struct B
{
struct A base;
/* ... more members */
}

int foo(struct A *self)
{
/* ... */
}

int main()
{
struct B obj;
foo((struct A *)&obj);
}

It depends on the compiler not tinkering with the layout of the
structure at all, which I don't believe is guaranteed but is fairly
safe to assume. (The equivalent C++ code could use real inheritance,
and then it is guaranteed, plus the pointer can be cast implicitly.
But we already know C++ does object oriented code more cleanly.) As
far as foo() is concerned, it's been given a 'struct A', albeit one
with a few extra members after it.

>> True object oriented programming
>> seems to require proper support from
>> the language itself, because the run-time resolution of the "this/self"
>> reference needs specific constructs in the language.
>
> Again, I don't understand what you are trying to say here. Provided that
> the "this/self" reference has a type, what more does the language need to
> provide? The reference itself is enough to identify the instance (since
> it is the instance!) and the instance's type is enough to identify the
> type (since it is the type!).

See above C example - except that true support would include implicit
upcasting, and would thus disallow cross-casting (which the C example
above would have problems with - you could cast any pointer to any
type with the exact same syntax and no compiler warning or error).

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


Error Testing

2013-10-19 Thread Scott Novinger
Hello.

I've written a program for my kids to calculate arc length.  I want to include 
some error testing for value types entered that are something other than 
integer values.

My goal is to make sure that the value entered for the radius is an integer 
value.

How could I rewrite this code to make sure I accomplish my goal of getting an 
integer value entered?  I know the construct is not correct.  I'm just learning 
how to program.

# Create the variable for radius, "radius".
print('Please enter the circle radius and press ENTER:')
radius = input()

# Check to make sure the entered value is an integer.
if type(radius) != type(int):
print('You must enter an integer value.')
print('Please enter the circle radius and press ENTER:')
radius = input()
else:
print('The radius you entered is: ' + radius)

radius = int(radius)

Thanks for your help. I'm using Python v3.2 for windows.

Scott
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Roy Smith
In article ,
 [email protected] wrote:

> I am looking for an example of a UNICODE to ASCII conversion example that 
> will remove diacritics from characters (and leave the characters, i.e., Klüft 
> to Kluft) as well as handle the conversion of other characters, like große to 
> grosse.

https://pypi.python.org/pypi/Unidecode
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Mark Lawrence

On 19/10/2013 13:23, Scott Novinger wrote:

Hello.

I've written a program for my kids to calculate arc length.  I want to include 
some error testing for value types entered that are something other than 
integer values.

My goal is to make sure that the value entered for the radius is an integer 
value.

How could I rewrite this code to make sure I accomplish my goal of getting an 
integer value entered?  I know the construct is not correct.  I'm just learning 
how to program.

 # Create the variable for radius, "radius".
 print('Please enter the circle radius and press ENTER:')
 radius = input()

 # Check to make sure the entered value is an integer.
 if type(radius) != type(int):
 print('You must enter an integer value.')
 print('Please enter the circle radius and press ENTER:')
 radius = input()
 else:
 print('The radius you entered is: ' + radius)

 radius = int(radius)

Thanks for your help. I'm using Python v3.2 for windows.

Scott



Please see the example here 
http://docs.python.org/3/tutorial/errors.html#handling-exceptions.  If 
you want further data feel free to ask, we don't bite :)


--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: Error Testing

2013-10-19 Thread Ned Batchelder

On 10/19/13 8:23 AM, Scott Novinger wrote:

Hello.

I've written a program for my kids to calculate arc length.  I want to include 
some error testing for value types entered that are something other than 
integer values.

My goal is to make sure that the value entered for the radius is an integer 
value.

How could I rewrite this code to make sure I accomplish my goal of getting an 
integer value entered?  I know the construct is not correct.  I'm just learning 
how to program.

 # Create the variable for radius, "radius".
 print('Please enter the circle radius and press ENTER:')
 radius = input()

 # Check to make sure the entered value is an integer.
 if type(radius) != type(int):
 print('You must enter an integer value.')
 print('Please enter the circle radius and press ENTER:')
 radius = input()
 else:
 print('The radius you entered is: ' + radius)
 
 radius = int(radius)


Thanks for your help. I'm using Python v3.2 for windows.

Scott

Hi Scott, welcome!

This line doesn't do what you want:

if type(radius) != type(int):

for a few reasons:  First, radius is the result of input(), so it is 
always a string, never an int.  Second, "int" itself is already a type, 
so type(int) is actually "class" (or something like it), not "int".


What you want is to know whether the string inputted can be properly 
converted to an int.  In Python, the way to do that is to try converting 
it, and be prepared for it to fail:


try:
radius = int(radius)
except ValueError:
print('You must enter an integer value.')

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread caldwellinva
Zero/Stephen ... thank you for your replies ... they were both very helpful, 
both in addressing the immediate issue and for getting a better understanding 
of the context of the conversion. Greatly appreciate your taking the time for 
such good solutions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Roy Smith
On 10/19/13 8:23 AM, Scott Novinger wrote:
> > My goal is to make sure that the value entered for the radius is an integer 
> > value.

In article ,
 Ned Batchelder  wrote:

> First, radius is the result of input(), so it is 
> always a string, never an int.

input() returns ints or floats for values which can be converted to 
those.  I suspect you're thinking of raw_input()?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sat, Oct 19, 2013 at 11:23 PM, Scott Novinger  wrote:
> # Create the variable for radius, "radius".
> print('Please enter the circle radius and press ENTER:')
> radius = input()
>
> # Check to make sure the entered value is an integer.
> if type(radius) != type(int):
> print('You must enter an integer value.')
> print('Please enter the circle radius and press ENTER:')
> radius = input()
> else:
> print('The radius you entered is: ' + radius)
>
> radius = int(radius)
>
> Thanks for your help. I'm using Python v3.2 for windows.

In Python 3, the input() function always returns a string. Your type
check is never going to succeed. Also, you're not checking what you
think you're checking; you're actually looking to see if input()
returned a type, because the type of int is type:

>>> type(int)


You might want:

if type(radius) != int:

But that still won't work, because input() will always return a string:

>>> radius = input()
42
>>> type(radius)


You can try all these out in the interactive interpreter (you probably
have IDLE installed, which on Windows is rather nicer to work with
than the default interactive mode).

A better check would be to just try to turn the inputted value into an
integer, and fail if that doesn't work. Again, try this interactively:

>>> int("42")
42
>>> int("not an integer")
Traceback (most recent call last):
  File "", line 1, in 
int("not an integer")
ValueError: invalid literal for int() with base 10: 'not an integer'

With a couple of quick checks, you can easily see what happens if the
conversion fails, and then you can deal with that failure. So
effectively, just drop the whole if: block, go straight to the "radius
= int(radius)" line, and deal with the error there.

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


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sat, Oct 19, 2013 at 11:57 PM, Roy Smith  wrote:
> On 10/19/13 8:23 AM, Scott Novinger wrote:
>> > My goal is to make sure that the value entered for the radius is an integer
>> > value.
>
> In article ,
>  Ned Batchelder  wrote:
>
>> First, radius is the result of input(), so it is
>> always a string, never an int.
>
> input() returns ints or floats for values which can be converted to
> those.  I suspect you're thinking of raw_input()?

Negative. The OP stated Python 3.2, in which Ned's statement is
correct. :) In Python 2.x, yes, input() will return an int if it
evaluates as one, but in 2.x, I would STRONGLY advise against ever
using non-raw input(). (To be honest, I'd have to say that the
innocuous name 'input' concealing code evaluation is an embarrassment,
and one that I'm very much glad is now removed from the language.)

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


Re: Error Testing

2013-10-19 Thread Ned Batchelder

On 10/19/13 8:57 AM, Roy Smith wrote:

On 10/19/13 8:23 AM, Scott Novinger wrote:

My goal is to make sure that the value entered for the radius is an integer
value.

In article ,
  Ned Batchelder  wrote:


First, radius is the result of input(), so it is
always a string, never an int.

input() returns ints or floats for values which can be converted to
those.  I suspect you're thinking of raw_input()?

In Python 3, input() returns a string.

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Roy Smith
In article ,
 Ned Batchelder  wrote:

> On 10/19/13 8:57 AM, Roy Smith wrote:
> > On 10/19/13 8:23 AM, Scott Novinger wrote:
> >>> My goal is to make sure that the value entered for the radius is an 
> >>> integer
> >>> value.
> > In article ,
> >   Ned Batchelder  wrote:
> >
> >> First, radius is the result of input(), so it is
> >> always a string, never an int.
> > input() returns ints or floats for values which can be converted to
> > those.  I suspect you're thinking of raw_input()?
> In Python 3, input() returns a string.
> 
> --Ned.

Duh.  My bad.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Mark Lawrence

On 19/10/2013 13:57, Roy Smith wrote:

On 10/19/13 8:23 AM, Scott Novinger wrote:

My goal is to make sure that the value entered for the radius is an integer
value.


In article ,
  Ned Batchelder  wrote:


First, radius is the result of input(), so it is
always a string, never an int.


input() returns ints or floats for values which can be converted to
those.  I suspect you're thinking of raw_input()?



Really, not what it says here 
http://docs.python.org/3/library/functions.html#input and shown by the 
following?


In [5]: a=input()
12345

In [6]: a
Out[6]: '12345'

In [7]: type(a)
Out[7]: builtins.str

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence

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


Re: Error Testing

2013-10-19 Thread Scott Novinger
On Saturday, October 19, 2013 8:37:01 AM UTC-4, Mark Lawrence wrote:
> On 19/10/2013 13:23, Scott Novinger wrote:
> 
> > Hello.
> 
> >
> 
> > I've written a program for my kids to calculate arc length.  I want to 
> > include some error testing for value types entered that are something other 
> > than integer values.
> 
> >
> 
> > My goal is to make sure that the value entered for the radius is an integer 
> > value.
> 
> >
> 
> > How could I rewrite this code to make sure I accomplish my goal of getting 
> > an integer value entered?  I know the construct is not correct.  I'm just 
> > learning how to program.
> 
> >
> 
> >  # Create the variable for radius, "radius".
> 
> >  print('Please enter the circle radius and press ENTER:')
> 
> >  radius = input()
> 
> >
> 
> >  # Check to make sure the entered value is an integer.
> 
> >  if type(radius) != type(int):
> 
> >  print('You must enter an integer value.')
> 
> >  print('Please enter the circle radius and press ENTER:')
> 
> >  radius = input()
> 
> >  else:
> 
> >  print('The radius you entered is: ' + radius)
> 
> >
> 
> >  radius = int(radius)
> 
> >
> 
> > Thanks for your help. I'm using Python v3.2 for windows.
> 
> >
> 
> > Scott
> 
> >
> 
> 
> 
> Please see the example here 
> 
> http://docs.python.org/3/tutorial/errors.html#handling-exceptions.  If 
> 
> you want further data feel free to ask, we don't bite :)
> 
> 
> 
> -- 
> 
> Roses are red,
> 
> Violets are blue,
> 
> Most poems rhyme,
> 
> But this one doesn't.
> 
> 
> 
> Mark Lawrence

Mark/ All,

I read the link on Handling Exceptions.  The first bit of code worked for my 
purposes. I was able to reduce my number of lines of code significantly and my 
program works! Thank you all for your help solving this problem!

My plan is to create several different programs that perform specific Algebraic 
operations.  My boys are learning Algebra 2 and I thought it might be a fun way 
to help us all learn Algebra and programming together.  Python seems to be a 
good language for learning how to program.

Thanks again!

Scott
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 12:34 AM, Scott Novinger  wrote:
> I read the link on Handling Exceptions.  The first bit of code worked for my 
> purposes. I was able to reduce my number of lines of code significantly and 
> my program works! Thank you all for your help solving this problem!

As you get accustomed to exception handling, you'll find that it's a
really clean and easy way to... well, handle exceptional situations :)
Tip: In quick throw-away scripts that are run from the command line,
don't even bother catching most exceptions. Just let 'em happen if
they want to happen... your script will terminate with an error
message, and you can deal with it as a human. That saves you even more
code!

> My plan is to create several different programs that perform specific 
> Algebraic operations.  My boys are learning Algebra 2 and I thought it might 
> be a fun way to help us all learn Algebra and programming together.  Python 
> seems to be a good language for learning how to program.

It is an excellent language for that. It's also a great language for
applications. It may not be the fastest in the world, but all
computers wait at the same speed anyway...

Just one thing: When you post here, please either don't use Google
Groups, or follow the advice here:

https://wiki.python.org/moin/GoogleGroupsPython

Preferably, just avoid GG, as a number of people just filter those
posts out. It's not an indictment of you as a person, but the majority
of GG posts are, quite frankly, very annoying for the reasons set out
in the wiki article.

Hope to see you around more!

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


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Zero Piraeus
:

On Sat, Oct 19, 2013 at 09:19:12AM +, Steven D'Aprano wrote:
> Make no mistake, this sort of simple-minded stripping of accents and 
> diacritics is an extremely ham-fisted thing to do.

I used to live on a street called Calle Colón, so I'm aware of the
dangers of stripping diacritics:

https://es.wikipedia.org/wiki/Colón
https://es.wikipedia.org/wiki/Colon

... although in that particular case, there's a degree of poetic justice
in confusing Cristóbal Colón / Cristopher Columbus with the back end of
a digestive tract:

  http://theoatmeal.com/comics/columbus_day

Joking aside, there is a legitimate use for asciifying text in this way:
creating unambiguous identifiers.

For example, a miscreant may create the username 'míguel' in order to
pose as another user 'miguel', relying on other users inattentiveness.
Asciifying is one way of reducing the risk of that.

 -[]z.

-- 
Zero Piraeus: in ictu oculi
http://etiol.net/pubkey.asc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Roy Smith
In article ,
 Zero Piraeus  wrote:

> For example, a miscreant may create the username 'míguel' in order to
> pose as another user 'miguel', relying on other users inattentiveness.
> Asciifying is one way of reducing the risk of that.

Determining if two strings are "almost the same" is not easy.  If míguel 
and miguel are to be considered the same, then why not also consider 
michael to be the same?  Or, for that matter, mike, mikey, or mick?  
There's no easy answer, and what's the right answer for some 
applications will be the wrong answer for others.

A reasonable place to start exploring this topic is 
https://en.wikipedia.org/wiki/String_metric.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: matplotlib 1.3.1

2013-10-19 Thread Piet van Oostrum
Ned Deily  writes:

> In article ,
>  Piet van Oostrum  wrote:
>> I tried to install it from source, on Mac OS X 10.6.8, with Python
>> 3.3.2, and Tck/Tk 8.5 installed as Frameworks, but I get an error during
>> compilation. It seems it doesn't find the Tcl/TK framework.
>> 
>> 
>> building 'matplotlib.backends._tkagg' extension
>> gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot 
>> /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot 
>> /Developer/SDKs/MacOSX10.6.sdk -I/opt/local/include 
>> -DPY_ARRAY_UNIQUE_SYMBOL=MPL_matplotlib_backends__tkagg_ARRAY_API 
>> -DPYCXX_ISO_CPP_LIB=1 -DPYCXX_PYTHON_2TO3=1 
>> -I/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-package
>> s/numpy/core/include -I/usr/local/include -I/usr/include -I/usr/X11/include 
>> -I/opt/local/include -I. -I/Library/Frameworks/Tcl.framework/Headers 
>> -I/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders 
>> -I/Library/Frameworks/Tk.framework/Headers 
>> -I/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders 
>> -Iagg24/include 
>> -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c 
>> src/ag
>>  g_py_transforms.cpp -o 
>>  build/temp.macosx-10.6-intel-3.3/src/agg_py_transforms.o -framework Tcl 
>>  -framework Tk
>> In file included from 
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
>> numpy/core/include/numpy/ndarraytypes.h:1760,
>>  from 
>>  
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
>>  
>> .3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
>>  from 
>>  
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
>>  .3/site-packages/numpy/core/include/numpy/arrayobject.h:4,
>>  from src/agg_py_transforms.cpp:6:
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
>> numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning 
>> "Using deprecated NumPy API, disable it by " "#defining 
>> NPY_NO_DEPRECATED_API 
>> NPY_1_7_API_VERSION"
>> i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
>> linking not done
>> i686-apple-darwin10-gcc-4.2.1: Tcl: linker input file unused because linking 
>> not done
>> i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
>> linking not done
>> i686-apple-darwin10-gcc-4.2.1: Tk: linker input file unused because linking 
>> not done
>> In file included from 
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
>> numpy/core/include/numpy/ndarraytypes.h:1760,
>>  from 
>>  
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
>>  
>> .3/site-packages/numpy/core/include/numpy/ndarrayobject.h:17,
>>  from 
>>  
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3
>>  .3/site-packages/numpy/core/include/numpy/arrayobject.h:4,
>>  from src/agg_py_transforms.cpp:6:
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
>> numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning 
>> "Using deprecated NumPy API, disable it by " "#defining 
>> NPY_NO_DEPRECATED_API 
>> NPY_1_7_API_VERSION"
>> i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
>> linking not done
>> i686-apple-darwin10-gcc-4.2.1: Tcl: linker input file unused because linking 
>> not done
>> i686-apple-darwin10-gcc-4.2.1: -framework: linker input file unused because 
>> linking not done
>> i686-apple-darwin10-gcc-4.2.1: Tk: linker input file unused because linking 
>> not done
>> gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 -isysroot 
>> /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -isysroot 
>> /Developer/SDKs/MacOSX10.6.sdk -I/opt/local/include 
>> -DPY_ARRAY_UNIQUE_SYMBOL=MPL_matplotlib_backends__tkagg_ARRAY_API 
>> -DPYCXX_ISO_CPP_LIB=1 -DPYCXX_PYTHON_2TO3=1 
>> -I/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-package
>> s/numpy/core/include -I/usr/local/include -I/usr/include -I/usr/X11/include 
>> -I/opt/local/include -I. -I/Library/Frameworks/Tcl.framework/Headers 
>> -I/Library/Frameworks/Tcl.framework/Versions/Current/PrivateHeaders 
>> -I/Library/Frameworks/Tk.framework/Headers 
>> -I/Library/Frameworks/Tk.framework/Versions/Current/PrivateHeaders 
>> -Iagg24/include 
>> -I/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c 
>> src/_t
>>  kagg.cpp -o build/temp.macosx-10.6-intel-3.3/src/_tkagg.o -framework Tcl 
>>  -framework Tk
>> In file included from 
>> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
>> numpy/core/include/numpy/ndarraytypes.h:1760,
>>  from 
>>  
>> /Library/Frameworks/

Re: Weird problem with UDP and gevent

2013-10-19 Thread Grant Edwards
On 2013-10-18, James Harris  wrote:
> "Roy Smith"  wrote in message 
> news:[email protected]...
>> I'm running:
>>
>> Ubuntu Precise
>> Python 2.7.3
>> django 1.4.5
>> gunicorn 0.17.4
>> gevent 1.0dev (rc3)
>>
>> I haven't been able to pin this down exactly, but it looks like if I
>> do (inside of a custom logging.Handler subclass):
>>
>>   # Paraphrased from the actual code
>> remote_addr = ("localhost", 9700)
>>  self.socket = socket.socket(type=socket.SOCK_DGRAM)
>>payload = "..."
>> self.socket.connect(remote_addr)
>>self.socket.send(payload)
>>
>> I get intermittant hangs in the connect() call.  If I rewrite this as:
>>
>> remote_addr = ("localhost", 9700)
>>self.socket = socket.socket(type=socket.SOCK_DGRAM)
>>payload = "..."
>>self.socket.sendto(payload, remote_addr)
>>
>> everything works fine.  Has anybody seen anything like this?  I'm
>> guessing this is some kind of gevent bug.
>
> Those are two different things.

Yes.  They differ in the addresses _from_which_ packets can be
received. In the "connect" example, you will only be able to receive
packets from ("localhost",9700).  In the second example you can
receive packets from any address.  Since the snippets only send
packets not receive packets, they should both behave identically.

> You would normally use connect() on a SOCK_STREAM socket. It requires
> that the remote endpoint, in this case localhost:9700, has an open
> socket listening for connections. sendto() is the right thing to use
> with SOCK_DGRAM.

Either will work.  You can use connect() with a SOCK_DGRAM.  What it
does is 

 1) save the destination address and use it when you call send()

 2) limit the addresses from which packets will be received. 

>From the Linux connect(2) man page:

   If the socket sockfd is of type SOCK_DGRAM then addr is the address
   to which datagrams are sent by default, and the only address from
   which datagrams are received.

-- 
Grant
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread rusi
On Saturday, October 19, 2013 8:40:37 PM UTC+5:30, Roy Smith wrote:
>  Zero Piraeus  wrote:
> 
> > For example, a miscreant may create the username 'míguel' in order to
> > pose as another user 'miguel', relying on other users inattentiveness.
> > Asciifying is one way of reducing the risk of that.
> 
> 
> Determining if two strings are "almost the same" is not easy.  If míguel 
> and miguel are to be considered the same, then why not also consider 
> michael to be the same?  Or, for that matter, mike, mikey, or mick?  
> There's no easy answer, and what's the right answer for some 
> applications will be the wrong answer for others.

I did not know till quite recently that Jean and Ivan were just good ol John
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Steven D'Aprano
On Sat, 19 Oct 2013 11:14:30 -0300, Zero Piraeus wrote:

> :
> 
> On Sat, Oct 19, 2013 at 09:19:12AM +, Steven D'Aprano wrote:
>> Make no mistake, this sort of simple-minded stripping of accents and
>> diacritics is an extremely ham-fisted thing to do.
[...]
> Joking aside, there is a legitimate use for asciifying text in this way:
> creating unambiguous identifiers.
> 
> For example, a miscreant may create the username 'míguel' in order to
> pose as another user 'miguel', relying on other users inattentiveness.
> Asciifying is one way of reducing the risk of that.

I'm pretty sure that Oliver and 0liver may not agree. Neither will 
Megal33tHaxor and Mega133tHaxor.

It's true that there are *more* opportunities for this sort of 
shenanigans with Unicode, so I guess your comment about "reducing" the 
risk (rather than eliminating it) is strictly correct. But there are 
other (better?) ways to do so, e.g. you could generate an identicon for 
the user to act as a visual checksum:

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


Another reasonable use for accent-stripping is searches. If I'm searching 
for music by the Blue Öyster Cult, it would be good to see results for 
Blue Oyster Cult as well. And vice versa. (A good search engine should 
consider *adding* accents as well as removing them.)

On the other hand, if you name your band ▼□■□■□■, you deserve to wallow 
in obscurity :-)


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


Re: Error Testing

2013-10-19 Thread rusi
On Saturday, October 19, 2013 7:04:30 PM UTC+5:30, Scott Novinger wrote:
> 
> My plan is to create several different programs that perform specific 
> Algebraic 
> operations.  My boys are learning Algebra 2 and I thought it might be a fun 
> way 
> to help us all learn Algebra and programming together.  Python seems to be a 
> good language for learning how to program.

If you are a programmer and want to start doing some 'real' math stuff, your 
approach is fine.

Conversely if you are a mathematician (or at least someone whose math 
fundamentals are well established) then too to start by getting your hands 
dirty with coding up some paper-pen/chalk-blackboard math into a system is a 
good goal and python is as good a language for this as any.

The system sage http://www.sagemath.org/ does some serious math with python as 
glue.

However if your boys are new to both math and programming, you are doing them a 
disservice by mixing the two using python.

The problem is that python is an imperative language and uses the '=' sign for 
assignment.  In math of course '=' stands for equality.

Now these two usages of '=' are both center-stage and completely different:
- the math = is by definition symmetric -- we can replace x=y by y=x. Whereas 
in programming we can never replace x=1 by 1=x
- More significantly and dangerously the programming var=rhs has a timing 
element and in fact introduces a basic notion of a 'unit of computation', --the 
statement -- a notion completely absent from the math something=somethingElse

Now one word or signifier meaning different things -- a pun -- is not 
necessarily bad. To the extent that we humans have more entities to deal with 
than ready words its even inevitable.  Just yesterday there was a discussion 
about whether 'python' is a TV comic character or a snake, pike a fish or a 
poker.  I think these are relatively harmless.

However with '=' in math and programming, the two are too different to be 
equated(!!) and too close to be separated.
Because after the programming statement var = lhs
the math predicate var = lhs is typically true.

But then what happens with something like this?  x = x+1
For a programmer this is common daily fare.
For a mathematician its an impossibility.

So I suggest you try it (x=x+1) on your boys.
If they think its ok, youve damaged their mathematical acumen
If not, they've yet to begin programming.
If they can answer to the effect that in some contexts its natural and in some 
not then of course they are very mature and/or geniuses.

I should mention that John Backus, the creator of the first hi-level language 
and a Turing award winner, more or less said that the assignment is the single 
biggest problem why programming languages are in such a mess:
http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf

Having said that, I also need to say that most programmers dont agree with that.
The minority that do, would earlier be called 'declarative-devotees'; nowadays 
the fashionable term is functional programming.
My own rather fringe minority position is that Backus et al are right in 
denouncing the assignment. However so far the attempts at practically  
realizing this smack of throwing out the baby with the bathwater.

So for the time being, languages like python remain eminently practical.
Its just that they are not so good for building kids' theoretical foundations 
-- especially of a mathematical sort.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Roy Smith
On Saturday, October 19, 2013 12:16:02 PM UTC-4, Steven D'Aprano wrote:

> Another reasonable use for accent-stripping is searches. If I'm searching 
> for music by the Blue Öyster Cult, it would be good to see results for 
> Blue Oyster Cult as well.

Tell me about it (I work at Songza; music search is what we do).  Accents are 
easy (Beyoncé, for example).  What about NIN (where one of the N's is supposed 
to be backwards, but I can't figure out how to type that)?  And Ke$ha.  And 
"The artist previously known as a glyph which doesn't even exist in Unicode 6.3"

> On the other hand, if you name your band ▼□■□■□■, you deserve to wallow 
> in obscurity :-)

Indeed.

So, yesterday, I tracked down an uncaught exception stack in our logs to a user 
whose username included the unicode character 'SMILING FACE WITH SUNGLASSES' 
(U+1F60E).  It turns out, that's perfectly fine as a user name, except that in 
one obscure error code path, we try to str() it during some error processing.  
If you named your band something which included that character, would you 
expect it to match a search for the same name but with 'WHITE SMILING FACE' 
(U+263A) instead?


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


Re: Error Testing

2013-10-19 Thread David Robinow
On Sat, Oct 19, 2013 at 9:01 AM, Chris Angelico  wrote:

> You can try all these out in the interactive interpreter (you probably
> have IDLE installed, which on Windows is rather nicer to work with
> than the default interactive mode).
 IDLE is cross-platform.  Could you explain why you say "on Windows"?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Tim Chase
On 2013-10-19 14:08, David Robinow wrote:
> On Sat, Oct 19, 2013 at 9:01 AM, Chris Angelico wrote:
>> You can try all these out in the interactive interpreter (you
>> probably have IDLE installed, which on Windows is rather nicer to
>> work with than the default interactive mode).  
>
>  IDLE is cross-platform.  Could you explain why you say "on
> Windows"?

In my experience, the Win32 Python console lacks readline support
(like my stock Python on the Mac OS X machine I have here), and as
such lacks a lot of the niceties.  At least on Win32, there is
recall of previous commands (my Mac lacks even that), but there's no
easy "search for the previous command I typed that contains the
following keyword" (control-R followed by search term), no easy
"insert all matching filenames here" (alt+asterisk), etc.

Idle may not provide all that, but it hopefully provides at least
*some* basic features that the console python.exe lacks.

-tkc


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


Re: Error Testing

2013-10-19 Thread Terry Reedy

On 10/19/2013 2:08 PM, David Robinow wrote:

On Sat, Oct 19, 2013 at 9:01 AM, Chris Angelico  wrote:


You can try all these out in the interactive interpreter (you probably
have IDLE installed, which on Windows is rather nicer to work with
than the default interactive mode).

  IDLE is cross-platform.  Could you explain why you say "on Windows"?


The command line console on *nix is apparently less obnoxious that the 
one on Windows. So for people who use an editor other than the Idle 
editor, there is less reason to use just the Idle shell.


--
Terry Jan Reedy

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


What's wrong with Windows Command Prompt (was Re: Error Testing)

2013-10-19 Thread Terry Reedy

On 10/19/2013 2:31 PM, Tim Chase wrote:

On 2013-10-19 14:08, David Robinow wrote:

On Sat, Oct 19, 2013 at 9:01 AM, Chris Angelico wrote:

You can try all these out in the interactive interpreter (you
probably have IDLE installed, which on Windows is rather nicer to
work with than the default interactive mode).


  IDLE is cross-platform.  Could you explain why you say "on
Windows"?


In my experience, the Win32 Python console lacks readline support
(like my stock Python on the Mac OS X machine I have here), and as
such lacks a lot of the niceties.  At least on Win32, there is
recall of previous commands (my Mac lacks even that), but there's no
easy "search for the previous command I typed that contains the
following keyword" (control-R followed by search term), no easy
"insert all matching filenames here" (alt+asterisk), etc.

Idle may not provide all that, but it hopefully provides at least
*some* basic features that the console python.exe lacks.


Command Prompt almost 'religiously' imitates the DOS character 
interface. The mouse is mostly ignored; it is not tied to the cursor. 
Idle is a normal Windows gui app.


Command Prompt displays a window of k lines of a circular queue of n 
lines, which are initialized as blank. The default n=300 is not enough 
for a test suite run or many help() outputs. If you find the box on the 
3rd tab of properties, n can be increased up to , but if you do, the 
scroll bar becomes rather useless, as it scrolls through all n lines.


Idle has a normal expanding buffer, with no extra blank lines added.

CP does not have proper cut and paste. I'll leave out most of the 
obnoxious details, but selections are rectangular blocks. This is the 
only function for the mouse, and uses a separate mouse cursor. Idle has 
normal cut and paste that works like any other Windows app.


Idle can recall previous input two different ways, by cursor or key. One 
can use the mouse to select where to edit. CP requires use of arrow keys 
to move the cursor around. Idle recall input *statements*. CP recalls 
input *lines*. Previous multiline statements have to be recalled a line 
at a time. CP was not designed for true multiline commands (as opposed 
to long commands that wrap and display as multiple lines).


CP has no menu (other than a few entries when one right-clicks on the 
icon in the upper left). There is no way to directly save or print the 
buffer.


--
Terry Jan Reedy

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


skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Peter Cacioppi
Is the following considered poor Python form?

class Foo (object) :
_lazy = None
def foo(self, x) :
_lazy = _lazy or self.get_something(x)
def get_something(self, x) :
# doesn't really matter

I like this idiom for certain situations, just wondering if it will raise the 
hackles of other Pythonistas. 

I use this idiom sparingly, but sometimes it just fits the task at hand, I hear 
Guidos voice saying "use the Force" in my ear, etc.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Ned Batchelder

On 10/19/13 5:44 PM, Peter Cacioppi wrote:

Is the following considered poor Python form?

class Foo (object) :
 _lazy = None
 def foo(self, x) :
 _lazy = _lazy or self.get_something(x)
 def get_something(self, x) :
 # doesn't really matter

I like this idiom for certain situations, just wondering if it will raise the 
hackles of other Pythonistas.

I use this idiom sparingly, but sometimes it just fits the task at hand, I hear Guidos 
voice saying "use the Force" in my ear, etc.


You present this as a choice between __init__ or a class attribute, but 
those two things are very different.  Is your intent to have an instance 
attribute, or a class attribute?  Lazily populated instance attributes 
are fine, I would do it like this:


class Foo(object):
def __init__(self):
self._lazy = None

def foo(self, x):
if self._lazy is None:
self._lazy = self.get_something(x)
...

--Ned.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 3:49 AM, Roy Smith  wrote:
> So, yesterday, I tracked down an uncaught exception stack in our logs to a 
> user whose username included the unicode character 'SMILING FACE WITH 
> SUNGLASSES' (U+1F60E).  It turns out, that's perfectly fine as a user name, 
> except that in one obscure error code path, we try to str() it during some 
> error processing.

How is that a problem? Surely you have to deal with non-ASCII
characters all the time - how is that particular one a problem? I'm
looking at its UTF-8 and UTF-16 representations and not seeing
anything strange, unless it's the \x0e in UTF-16 - but, again, you
must surely have had to deal with
non-ASCII-encoded-whichever-way-you-do-it.

Or are you saying that that particular error code path did NOT handle
non-ASCII characters? If so, that's a strong argument for moving to
Python 3, to get full Unicode support in _all_ branches.

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


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Peter Cacioppi
On Saturday, October 19, 2013 2:44:55 PM UTC-7, Peter Cacioppi wrote:
> Is the following considered poor Python form?
> 
> 
> 
> class Foo (object) :
> 
> _lazy = None
> 
> def foo(self, x) :
> 
> _lazy = _lazy or self.get_something(x)
> 
> def get_something(self, x) :
> 
> # doesn't really matter
> 
> 
> 
> I like this idiom for certain situations, just wondering if it will raise the 
> hackles of other Pythonistas. 
> 
> 
> 
> I use this idiom sparingly, but sometimes it just fits the task at hand, I 
> hear Guidos voice saying "use the Force" in my ear, etc.

To be clear, I use this when I'm subclassing something and don't need to do 
anything with the _lazy other than set it to None in the __init__. I like this 
idiom because it removes the risk of miscalling the parent __init__, and it's 
also quick and easy to code.

Yes there is a class member I am creating, but each instance has a distinct 
member. (In other words, I'm an adult, I tested it and it works, but I couldn't 
find anything in the Google indicating what the collective reaction would be 
from Pythonistan)
-- 
https://mail.python.org/mailman/listinfo/python-list


Screenshots in Mac OS X

2013-10-19 Thread Pratik Mehta
Hi Guys,

Good Morning!
How are you all doing?

I am in need of your help.

I am stuck in a problem.

I want to write my own Screenshot Taking program in Python for Mac OS X.

Example : Whenever Command + Shift + 3 is pressed ==> whatever is there on the 
screen, should be grabbed / captured, and should be stored on my local with my 
own desired path, and name should automatically given as SnapshotX.. as in 
Snapshot1, Snapshot2, etc...

The same goes with Command + Shift + 4 ==> the only difference is that, it 
allows you to select a particular area to be captured / grabbed.

Command + Shift + 5 ==> This one is a little bit tricky which I am looking for. 
This shortcut doesn't exist. I want to create one of my own, where it would 
grab the entire webpage's screenshot which I am currently working on, and store 
the name as the webpage's link.


Thanks in advance for your kind help and time.


Thanks & Regards,
Pratts
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's wrong with Windows Command Prompt (was Re: Error Testing)

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 7:35 AM, Terry Reedy  wrote:
> Idle can recall previous input two different ways, by cursor or key. One can
> use the mouse to select where to edit. CP requires use of arrow keys to move
> the cursor around. Idle recall input *statements*. CP recalls input *lines*.
> Previous multiline statements have to be recalled a line at a time. CP was
> not designed for true multiline commands (as opposed to long commands that
> wrap and display as multiple lines).

This issue applies also to Linux builds with readline. It's Idle's
primary boast, I think, closely followed by hover help for function
signatures.

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


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Robert Kern

On 2013-10-19 22:55, Ned Batchelder wrote:

On 10/19/13 5:44 PM, Peter Cacioppi wrote:

Is the following considered poor Python form?

class Foo (object) :
 _lazy = None
 def foo(self, x) :
 _lazy = _lazy or self.get_something(x)
 def get_something(self, x) :
 # doesn't really matter

I like this idiom for certain situations, just wondering if it will raise the
hackles of other Pythonistas.

I use this idiom sparingly, but sometimes it just fits the task at hand, I
hear Guidos voice saying "use the Force" in my ear, etc.


You present this as a choice between __init__ or a class attribute, but those
two things are very different.  Is your intent to have an instance attribute, or
a class attribute?  Lazily populated instance attributes are fine, I would do it
like this:

class Foo(object):
 def __init__(self):
 self._lazy = None

 def foo(self, x):
 if self._lazy is None:
 self._lazy = self.get_something(x)
 ...


I think he left some important characters out.

class Foo (object) :
_lazy = None
def foo(self, x) :
self._lazy = self._lazy or self.get_something(x)
# Use self._lazy for something

def get_something(self, x) :
# doesn't really matter

The main difference being that he doesn't initialize the instance attribute and 
just relies on the fallback to class attribute lookup. In my experience, using a 
static[1] class attribute as a default for an instance attribute is accepted 
practice, and one that gets touted as a positive feature of Python's namespace 
model when compared against other languages. That said, I have seen it more 
often in the past.


[1] In the general "unchanging" sense rather than the C++ "static" keyword 
sense.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 6:50 AM, Terry Reedy  wrote:
> On 10/19/2013 2:08 PM, David Robinow wrote:
>>
>> On Sat, Oct 19, 2013 at 9:01 AM, Chris Angelico  wrote:
>>
>>> You can try all these out in the interactive interpreter (you probably
>>> have IDLE installed, which on Windows is rather nicer to work with
>>> than the default interactive mode).
>>
>>   IDLE is cross-platform.  Could you explain why you say "on Windows"?
>
>
> The command line console on *nix is apparently less obnoxious that the one
> on Windows. So for people who use an editor other than the Idle editor,
> there is less reason to use just the Idle shell.

Less obnoxious, more friendly, and way WAY more powerful. As I said in
the other thread, multi-line recall is its biggest lack, but for the
rest, it's quite usable. On Windows, not so much. (Mind you, I'm
accustomed to readline, so if you don't have it, you'll feel more
strongly about Idle.)

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


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 3:22 AM, rusi  wrote:
> The problem is that python is an imperative language and uses the '=' sign 
> for assignment.  In math of course '=' stands for equality.

Pascal tried to create a new operator, := to be read "becomes", to
deal with the whole equality-vs-assignment issue. Did it really help
anything? I don't think so. Just syntactic salt. Even the comparison
isn't really mathematical - in maths, "x = y" is a statement of truth,
whereas in programming, it's a question ("is x equal to y").

Teaching maths and programming at once is like teaching any other two
arts at once - there'll be differences to grok as well as similarities
to jump on. I would say that the expression evaluator in (almost) any
modern language is a fairly close parallel to standard mathematical
expressions; yes, abutting tokens is multiplication in algebra, but on
the flip side, we don't use (or need) subscript to make multi-letter
identifiers in code. Programming uses more words and less blackboard
notations (compare abs(x) to |x| for example), but it's expressing
things in fairly similar ways most of the time.

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


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Steven D'Aprano
On Sat, 19 Oct 2013 14:44:55 -0700, Peter Cacioppi wrote:

> Is the following considered poor Python form?
> 
> class Foo (object) :
> _lazy = None
> def foo(self, x) :
> _lazy = _lazy or self.get_something(x)
> def get_something(self, x) :
> # doesn't really matter
> 
> I like this idiom for certain situations, just wondering if it will
> raise the hackles of other Pythonistas.

I agree with Ned's comments -- the basic idea is fine, it's just caching 
some calculated result.

I often use this idiom in read-only properties, for something that only 
needs to be calculated once, but it's time-consuming to calculate and 
therefore I want to delay doing the calculation until the last possible 
minute. Something like this:

class Spam:
@property
def spam(self):
try:
result = self._spam
except AttributeError:
# Perform some time-consuming calculation.
result = self._spam = \
("spam "*5).capitalize() + "LOVELY SPAM!"
return result

but there are a million different variations on this theme.


> I use this idiom sparingly, but sometimes it just fits the task at hand,
> I hear Guidos voice saying "use the Force" in my ear, etc.

Good reasons for avoiding this pattern might include:

- the speed benefit is too trivial to justify the extra complexity;

- the risk that some condition will unexpectedly change, invalidating
  the cached value, but it's too hard to notice when the cache is 
  invalid;

- caching the result means you unnecessarily hold on to a chunk of
  memory which otherwise would be garbage collected (consider using
  weak references to solve this one);

but otherwise it is unobjectionable.


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


Re: Screenshots in Mac OS X

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 9:12 AM, Pratik Mehta
 wrote:
> Command + Shift + 5 ==> This one is a little bit tricky which I am looking 
> for. This shortcut doesn't exist. I want to create one of my own, where it 
> would grab the entire webpage's screenshot which I am currently working on, 
> and store the name as the webpage's link.

Sounds like a difficult thing to write from scratch. Can you find
something off-the-shelf that takes a snapshot of your current window,
and then maybe add some interaction with the web browser? Otherwise,
you're poking around with screenshotting directly, which is very much
a solved problem.

Also, please read this before you reply:

https://wiki.python.org/moin/GoogleGroupsPython

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


Re: Screenshots in Mac OS X

2013-10-19 Thread Ned Deily
In article <[email protected]>,
 Pratik Mehta  wrote:
> I want to write my own Screenshot Taking program in Python for Mac OS X.
> 
> Example : Whenever Command + Shift + 3 is pressed ==> whatever is there on 
> the screen, should be grabbed / captured, and should be stored on my local 
> with my own desired path, and name should automatically given as SnapshotX.. 
> as in Snapshot1, Snapshot2, etc...
> 
> The same goes with Command + Shift + 4 ==> the only difference is that, it 
> allows you to select a particular area to be captured / grabbed.
> 
> Command + Shift + 5 ==> This one is a little bit tricky which I am looking 
> for. This shortcut doesn't exist. I want to create one of my own, where it 
> would grab the entire webpage's screenshot which I am currently working on, 
> and store the name as the webpage's link.

I'm not sure how much what you want to do differs from what OS X already 
provides out of the box but perhaps this reference can give you some ideas:

http://guides.macrumors.com/Taking_Screenshots_in_Mac_OS_X

-- 
 Ned Deily,
 [email protected]

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


Re: Error Testing

2013-10-19 Thread Ned Deily
In article 
,
 Chris Angelico  wrote:
> Pascal tried to create a new operator, := to be read "becomes", to
> deal with the whole equality-vs-assignment issue.

Um, Pascal was just following the lead of ALGOL 60, roughly a decade earlier.

-- 
 Ned Deily,
 [email protected]

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


Error running "make install" on CentOS 6.4 (64bit)

2013-10-19 Thread jh113355
changing mode of /usr/local/lib/python2.7/lib-dynload/ to 755
running install_scripts
copying build/scripts-2.7/idle -> /usr/local/bin
copying build/scripts-2.7/2to3 -> /usr/local/bin
error: /usr/local/bin/2to3: No such file or directory
make: *** [sharedinstall] Error 1
[root@afdice-pc0v1 Python-2.7.5]# 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error Testing

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 9:46 AM, Ned Deily  wrote:
> In article
> ,
>  Chris Angelico  wrote:
>> Pascal tried to create a new operator, := to be read "becomes", to
>> deal with the whole equality-vs-assignment issue.
>
> Um, Pascal was just following the lead of ALGOL 60, roughly a decade earlier.

Sorry, the word "create" was poorly chosen. Would "deploy" be better?
I'm aware there's a family; what I'm trying to say is that, in Pascal
(I prefer to use a better-known language for discussion, where
possible), there's a new operator instead of =.

Anyway, my point is that it doesn't really help much. Imperative code
is temporal (maybe functional or declarative code could be closer to
maths, but we're talking Python here, which is imperative), where
mathematical truth is timeless. If a² + b² = c² now, then it still
will be true tomorrow. In programming, that's far from guaranteed
(though compilers will often optimize if they know there's a section
of code where the three won't change). You have to get your head
around that, whether you're doing assignment or comparison, and using
a different symbol to represent them is doing yourself as much of a
disservice as avoiding + for addition of floats to emphasize that they
don't always work like real numbers.

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


Screenshots in Mac OS X

2013-10-19 Thread Pratik Mehta
Hi Guys,

Good Morning!
How are you all doing?

I am in need of your help.

I am stuck in a problem.

I want to write my own Screenshot Taking program in Python for Mac OS X.

Example : Whenever Command + Shift + 3 is pressed ==> whatever is there on the 
screen, should be grabbed / captured, and should be stored on my local with my 
own desired path, and name should automatically given as SnapshotX.. as in 
Snapshot1, Snapshot2, etc...

The same goes with Command + Shift + 4 ==> the only difference is that, it 
allows you to select a particular area to be captured / grabbed.

Command + Shift + 5 ==> This one is a little bit tricky which I am looking for. 
This shortcut doesn't exist. I want to create one of my own, where it would 
grab the entire webpage's screenshot which I am currently working on, and store 
the name as the webpage's link.


Thanks in advance for your kind help and time.


Thanks & Regards,
Pratts
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Screenshots in Mac OS X

2013-10-19 Thread Pratik Mehta
Hey Ned,

Thanks for reverting.

I am looking for the similar functionality.

Thanks for sharing that link.

Could you pls help me out with the Python Code?


Thanks.  :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing the terminal title bar with Python

2013-10-19 Thread Tim Roberts
Steven D'Aprano  wrote:
>
>You might find this useful, or just for fun, but on POSIX systems (Linux, 
>Unix, Apple Mac, but not Windows) you can change the title of the 
>terminal window from Python. 

Just for completeness, this is also possible in Windows, assuming you have
installed py32win, which virtually every Windows Python user does:

import win32console
win32console.SetConsoleTitle(msg)
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: converting letters to numbers

2013-10-19 Thread Tim Roberts
Steven D'Aprano  wrote:
>On Sun, 13 Oct 2013 20:13:32 -0700, Tim Roberts wrote:
>
>> def add(c1, c2):
>>  % Decode
>>...
>Python uses # for comments, not %, as I'm sure you know. What language 
>were you thinking off when you wrote the above?


Psssht, I know better than that.

I've been reading through MATLAB code, which uses %, but I have CERTAINLY
not written enough MATLAB to excuse that.
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Peter Cacioppi
Yes, I see the light now. My idiom works, but Steven has shown me the droids I 
am looking for. 

Thanks!

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


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Peter Cacioppi
To be clear, my original post had a goof. 

So my original, de-goofed, idiom was


class Foo (object) :
_lazy = None
def foo(self, x) :
self._lazy = self._lazy or self.get_something(x)
def get_something(self, x) :
# doesn't really matter, so long as it returns truthy result

and the new, improved idiom is 

class Foo (object) :
def foo(self, x) :
self._lazy = getattr(self, "_lazy", None) or self._get_something(x)
def _get_something(self, x) :
# doesn't really matter, so long as it returns truthy result

I was laboring under some misconception that there was Python magic that 
allowed __init__ and only __init__ to add class attributes by setting their 
values. Good to know this piece of magic isn't part of Python, and thus lazy 
eval can be handled more cleanly than I originally thought. 

In other words, "Guido was here".

Thanks again

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


Re: skipping __init__ and using exploiting a class member instead

2013-10-19 Thread Ned Batchelder

On 10/19/13 8:42 PM, Peter Cacioppi wrote:

To be clear, my original post had a goof.

So my original, de-goofed, idiom was


class Foo (object) :
 _lazy = None
 def foo(self, x) :
 self._lazy = self._lazy or self.get_something(x)
 def get_something(self, x) :
 # doesn't really matter, so long as it returns truthy result

and the new, improved idiom is

class Foo (object) :
 def foo(self, x) :
 self._lazy = getattr(self, "_lazy", None) or self._get_something(x)
 def _get_something(self, x) :
 # doesn't really matter, so long as it returns truthy result


The use of getattr here seems unfortunate.  Your original at least 
didn't have that odd uncertainty about it.  I'm not sure why you want to 
avoid an __init__ method.  Why not simply have one, and use it to 
initialize your attributes, even if it is to None?


--Ned.

I was laboring under some misconception that there was Python magic that 
allowed __init__ and only __init__ to add class attributes by setting their 
values. Good to know this piece of magic isn't part of Python, and thus lazy 
eval can be handled more cleanly than I originally thought.

In other words, "Guido was here".

Thanks again



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


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Sun, Oct 20, 2013 at 3:49 AM, Roy Smith  wrote:
> > So, yesterday, I tracked down an uncaught exception stack in our logs to a 
> > user whose username included the unicode character 'SMILING FACE WITH 
> > SUNGLASSES' (U+1F60E).  It turns out, that's perfectly fine as a user name, 
> > except that in one obscure error code path, we try to str() it during some 
> > error processing.
> 
> How is that a problem? Surely you have to deal with non-ASCII
> characters all the time - how is that particular one a problem? I'm
> looking at its UTF-8 and UTF-16 representations and not seeing
> anything strange, unless it's the \x0e in UTF-16 - but, again, you
> must surely have had to deal with
> non-ASCII-encoded-whichever-way-you-do-it.
> 
> Or are you saying that that particular error code path did NOT handle
> non-ASCII characters?

Exactly.  The fundamental error was caught, and then we raised another 
UnicodeEncodeError generating the text of the error message to log!

> If so, that's a strong argument for moving to
> Python 3, to get full Unicode support in _all_ branches.

Well, yeah.  The problem is, my pip requirements file lists 76 modules 
(and installing all those results in 144 modules, including the cascaded 
dependencies).  Until most of those are P3 ready, we can't move.

Heck, I can't even really move off 2.6 because we use Amazon's EMR 
service, which is stuck on 2.6.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 12:52 PM, Roy Smith  wrote:
> In article ,
>  Chris Angelico  wrote:
>
>> Or are you saying that that particular error code path did NOT handle
>> non-ASCII characters?
>
> Exactly.  The fundamental error was caught, and then we raised another
> UnicodeEncodeError generating the text of the error message to log!

Ha... oh, that's awkward.

>> If so, that's a strong argument for moving to
>> Python 3, to get full Unicode support in _all_ branches.
>
> Well, yeah.  The problem is, my pip requirements file lists 76 modules
> (and installing all those results in 144 modules, including the cascaded
> dependencies).  Until most of those are P3 ready, we can't move.

It's still a strong argument, just that unavailability of key modules
may be a stronger one :)

> Heck, I can't even really move off 2.6 because we use Amazon's EMR
> service, which is stuck on 2.6.

Hrm. 2.6 is now in source-only security-only support, and that's about
to end (there's a 2.6.9 in the pipeline, and that's that). It's about
time Amazon moved to 2.7, at least...

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


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> > Heck, I can't even really move off 2.6 because we use Amazon's EMR
> > service, which is stuck on 2.6.
> 
> Hrm. 2.6 is now in source-only security-only support, and that's about
> to end (there's a 2.6.9 in the pipeline, and that's that). It's about
> time Amazon moved to 2.7, at least...

Tell that to Amazon.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Ben Finney
Roy Smith  writes:

> In article ,
>  Chris Angelico  wrote:
>
> > > Heck, I can't even really move off 2.6 because we use Amazon's EMR
> > > service, which is stuck on 2.6.
> > 
> > Hrm. 2.6 is now in source-only security-only support, and that's
> > about to end (there's a 2.6.9 in the pipeline, and that's that).
> > It's about time Amazon moved to 2.7, at least...
>
> Tell that to Amazon.

Tell that to customers of Amazon's EMR service, who are going to have
rather more leverage with Amazon than non-customers.

-- 
 \  “As soon as we abandon our own reason, and are content to rely |
  `\   upon authority, there is no end to our troubles.” —Bertrand |
_o__)Russell, _Unpopular Essays_, 1950 |
Ben Finney

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


Re: Looking for UNICODE to ASCII Conversioni Example Code

2013-10-19 Thread Chris Angelico
On Sun, Oct 20, 2013 at 1:26 PM, Ben Finney  wrote:
> Roy Smith  writes:
>
>> In article ,
>>  Chris Angelico  wrote:
>>
>> > > Heck, I can't even really move off 2.6 because we use Amazon's EMR
>> > > service, which is stuck on 2.6.
>> >
>> > Hrm. 2.6 is now in source-only security-only support, and that's
>> > about to end (there's a 2.6.9 in the pipeline, and that's that).
>> > It's about time Amazon moved to 2.7, at least...
>>
>> Tell that to Amazon.
>
> Tell that to customers of Amazon's EMR service, who are going to have
> rather more leverage with Amazon than non-customers.

Aye. My involvement with Amazon is pretty minimal - I evaluated EC2 a
while ago, and I think maybe the company paid Amazon something like
$20... maybe as much as $100, we had some of the higher-end instances
running for a while. They won't be listening to me. :)

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