Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 17:04:55 +1100, Chris Angelico wrote:

> On Tue, Mar 4, 2014 at 4:35 PM, Steven D'Aprano 
> wrote:
>> On Tue, 04 Mar 2014 05:37:27 +1100, Chris Angelico wrote:
>>> x = 23 # Compiler goes: Okay, x takes ints. x += 5 # Compiler: No
>>> prob, int += int --> int x = str(x) # Compiler: NO WAY! str(int) -->
>>> str, not allowed!
>>>
>>> It's fine and correct to infer that x is an int, x is an int, x is a
>>> str. It's *not* okay to make the third line a SyntaxError because you
>>> just put a str into an int variable.
>>
>> It won't be a Syntax Error, it will be a compile-time Type Error. And,
>> yes, it is fine. That's the point of static typing! The tradeoff of
>> being able to detect a whole lot of errors *at compile time* is that
>> you give up the ability to re-use the same variable for different types
>> in a single scope. (You can have an x which is a string elsewhere, just
>> not in this scope where it is an int.)
> 
> Okay, a compile-type type error, same difference. What I'm saying is
> that the auto-detection can't know what else you plan to do. 

Obviously it can't see the code you haven't written yet, but it can see 
what you *do* do.


> If you
> explicitly say that this is an int, then yes, that should be disallowed;

It's that "explicitly" part that doesn't follow. Having to manage types 
is the most tedious, boring, annoying, *unproductive* part of languages 
like Java, C and Pascal. Almost always, you're telling the compiler stuff 
that it can work out for itself.

In the same way that managing jumps for GOTO has been automated with for 
loops, while, etc., and managing memory has been automated, there's no 
good reason not to allow the compiler to manage types. Dynamically typed 
languages like Python do so at runtime. Type inference simply allows 
statically typed languages to do the same only at compile time.


[...]
>> That Haskell has homogeneous lists is not a property of the type
>> system, but a design choice. I'm sure Haskell will also have a tuple or
>> record type that allows fields of different types.
> 
> If it's not the list type, pick some other. It's not uncommon to want to
> have a record that has different types (C does this with struct, C++ has
> a few more ways to do it); what I'm finding odd is that whatever goes
> into it first is specifying for everything else.

That's because in Haskell the design was made that lists *must* be used 
for homogeneous data. If you read Python documentation from back in the 
1.5 and early 2.x days, there was a *very* strong recommendation that 
lists be used for homogeneous data only and tuples for heterogeneous 
data. This recommendation goes all the way up to Guido.

# Yes
[1, 3, 4, 2, 5, 9]
(1, "hello", None, 3.5)

# No
[1, "hello", None, 3.5]


That is, lists are for collections of data of arbitrary length, tuples 
are for records or structs with dedicated fields.

That convention is a bit weaker these days than it used to be. Tuples now 
have list-like methods, and we have namedtuple for record/struct-like 
objects with named fields. But still, it is normal to use lists with 
homogeneous data, where there is an arbitrary number of "things" with 
different values, but all the same kind of thing.

In the case of Haskell, that's more than a mere convention, it's a rule, 
but that's not terribly different from (say) Pascal where you can have an 
array of integer but not an array of integer-or-real.

The thing is though, how often do you really have a situation where you 
have a bunch of arbitrary data, or unknown length, where you don't know 
what type of data it is? Sure, in the interactive interpreter it is 
useful to be able to write

[1, "spam", None, [], {}, 0.1, set()]

and I write unit tests with that sort of thing all the time:

for obj in list_of_arbitrary_objects:
self.assertRaises(TypeError, func, obj)

kind of thing. But that doesn't have to be a *list*. It just needs to 
have convenient syntax.


>> I have not used Haskell enough to tell you whether you can specify
>> subtypes. I know that, at least for numeric (integer) types, venerable
>> old Pascal allows you to define subtypes based on integer ranges, so
>> I'd be surprised if you couldn't do the same thing in Haskell.
>>
>> The flexibility of the type system -- its ability to create subtypes
>> and union types -- is independent of whether it is explicitly declared
>> or uses type inference.
> 
> I'm not sure how you could have type inference with subtypes. How does
> the compiler figure out what subtype of integers is acceptable, such
> that it can reject some?

You seem to be under the impression that type inference means "guess what 
the programmer wants", or even "read the programmer's mind". Take this 
example:

> x = 5
> x = 7
> x = 11
> x = 17
> x = 27
> 
> Should the last one be rejected because it's not prime? How can it know
> that I actually wanted that to be int(3..20)? 

It can't, of course, any more than I could, or anyone other than you

Decoding a process output

2014-03-04 Thread Francis Moreau
Hi,

In my understanding (I'm relatively new to python), I need to decode any
bytes data provided, in my case, by a shell command (such as findmnt)
started by the subprocess module. The goal of my application is to parse
the command outputs.

My application runs only on linux BTW and should run fine on both python
2.7 and py3k.

My question is when decoding the output bytes data of the external
command, which encoding should I use ?

Should I guess the encoding by inspecting LANG or any LC_* environment
variables ?

Should I force one of those environment variable to a specific value
before running my external command ?

Thanks for any tips.


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


Re: Functional programming

2014-03-04 Thread BartC

"Steven D'Aprano"  wrote in message
news:5314bb96$0$29985$c3e8da3$54964...@news.astraweb.com...


Think about the sort of type declarations you have to do in (say) Pascal,
and consider how stupid the compiler must be:

function add_one(x: integer):integer;
 begin
   add_one := x+1;
 end;

Given that x is an integer, and that you add 1 (also an integer) to it,
is it really necessary to tell the compiler that add_one returns an
integer? What else could the output type be?

This was state of the art back in 1970, but these days, if the compiler
cannot *at least* infer the type of the return result of a function given
the argument types, the static type system is too dumb to bother with.


To me that is perfectly reasonable. This kind of language, while it allows
expressions of mixed numeric types, treats each kind of number type as
different: integer and real, even signed and unsigned integer, and there
might be short, medium and long versions of each! And Pascal has an
unlimited number of scalar types too.

The compiler could make a guess at what the intended result might be, based
on a hundred add_one:= assignments, all with a slightly different type on 
the right-hand-side, and perhaps choose a type that encompasses all the 
possibilities (although it might erroneously decide the result is real, 
based on one of those hundred, and use floating point for all of them).


But then someone edits the code, and this time the guess will be different!
For a static language such as this, type-discipline is important. And even
if the compiler gets it right, a human reading the code would have trouble
determining the return type, except in trivial examples like this.

Putting in an explicit return type is the simplest way to go.

--
Bartc 


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


Python 2.7 documentation with readthedocs theme

2014-03-04 Thread Charles Gunawan
Hi,

I was just looking into some documentation on readthedocs and I saw that they 
have a cool new theme. I was wondering how the python documentation will look 
like with that theme. So after some tinkering I have build python 2.7 CHM 
documentation with the theme.

If you are wondering too you can look at the chm (http://d-h.st/e1W) or the 
screenshot (http://imgur.com/7kVzZUt).
-- 
https://mail.python.org/mailman/listinfo/python-list


Logging

2014-03-04 Thread Igor Korot
Hi, ALL,
Could someone please explain to me how the code in
http://docs.python.org/2/howto/logging#logging-from-multiple-modules
works?
In particular I'm interested in how the mylib.py knows about the myapp.log.

What I mean is: logging object is not passed to mylib.py, so
essentially it should create a new instance of the logging object.

What am I missing?

But this question comes from the following fact about my application.
I tried to create a logging object which will store the logging
information to the file in the main class. Then I pass this object to
another class constructor and use it in that second class.

Upon running everything is OK, but when the program successfully
finishes, the log file has 0 length.

AFAIU, I'm doing it properly and the example referenced is wrong, yet
the results are completely different.

Thank you for any expplanation.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: modification time in Python - Django: datetime != datetime :-(

2014-03-04 Thread Jaap van Wingerde
Op 2014-03-03T12:22:48 UTC schreef donarb  in het
bericht , ID: <04659633-e14e-4d5b-90f2-93af04f05...@googlegroups.com> het 
volgende. 

> You're using the months format '%m' when you should be using minutes
> '%M'.

Arrgh: stupid error (snik).

p 2014-03-04T08:11:46 UTC schreef Chris Angelico  in
het bericht , ID: 

het volgende. 

> Heh! I didn't even notice that. When I tested it, I didn't use
> strftime at all, just looked at gmtime's output.

Op 2014-03-04T08:06:21 UTC schreef Ben Finney  in 
het bericht , ID: 
<85txbfhu0i@benfinney.id.au> het volgende.

> You're using ‘gmtime’ to display the Python datetime value, but ‘ls’
> will display the time in the local timezone. Do you have a strange
> timezone set?

I only use UTC.

Thanks

The view.py is now as follows.

...
home_lastmod = 
strftime('%Y-%m-%dT%H:%M:%SZ',gmtime(os.path.getmtime(os.path.dirname(os.path.realpath(__file__
 ))+'/templates/art_index.html')))
...

-- 

Jaap van Wingerde
e-mail: 1234567...@vanwingerde.nl

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


Re: Functional programming

2014-03-04 Thread Antoon Pardon
Op 04-03-14 09:56, Steven D'Aprano schreef:

>
>
>> If you
>> explicitly say that this is an int, then yes, that should be disallowed;
> It's that "explicitly" part that doesn't follow. Having to manage types 
> is the most tedious, boring, annoying, *unproductive* part of languages 
> like Java, C and Pascal. Almost always, you're telling the compiler stuff 
> that it can work out for itself.

In the same way writing unit tests is the most tedious, boring, annoying,
*unproductive* part. Amost always you are giving the program results
it can work out for itself.

-- 

Antoon Pardon

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


Re: why indentation should be part of the syntax

2014-03-04 Thread Antoon Pardon

Op 02-03-14 11:41, Stefan Behnel schreef:
> Haven't seen any mention of it on this list yet, but since it's such an
> obvious flaw in quite a number of programming languages, here's a good
> article on the recent security bug in iOS, which was due to accidentally
> duplicated code not actually being as indented as it looked:
> 
> https://www.imperialviolet.org/2014/02/22/applebug.html
> 
> Stefan
> 

Well I can give an example where accidentally duplicated code can get
you in trouble with python and which would be easily caught in C.
If you accidentally duplicate the first half of a function. Chances
are python will just accept it, because it is unlikely the last line
that was duplicated was an if, for or while line. So your half
duplicated function is probably legal python.

However it is very unlikely that the braces will match so you will
very likely get a compilation error.

IMO the problem with C, is not that indentation is not part of the
language. The problem is that after an if, for or while, you get
the choice between putting either one simple statement or a block.
I doubt you would get this problem in a language like modula2 where
an if, for or while statement is always followed by a block, terminated
with "END". So with modula like syntax the code would have looked like

IF (err := SSLHashSHA1.update(&hashCtx, &signedParams)) != 0 THEN
goto fail;
goto fail;
END

which wouldn't have been a problem


Or it might have looked like this

IF (err := SSLHashSHA1.update(&hashCtx, &signedParams)) != 0 THEN
goto fail;
END
goto fail;

This would have produced the same problem but it would also have
stood out because the second goto is indented in a place where
it is obvious it doesn't match the program structure.

-- 
Antoon Pardon



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


Re: Working with the set of real numbers

2014-03-04 Thread Gregory Ewing

Chris Angelico wrote:

In constant space, that will produce the sum of two infinite sequences
of digits.


It's not constant space, because the nines counter
can grow infinitely large.

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


Re: Reference

2014-03-04 Thread Alister
On Tue, 04 Mar 2014 09:18:57 +1100, Ben Finney wrote:

> Marko Rauhamaa  writes:
> 
>> Mark Lawrence :
>>
>> > I'd just like to know why people are so obsessed with identities,
>> > I've never thought to use them in 10+ years of writing Python.  Do I
>> > use the KISS principle too often?
>>
>> Calmly choosing the right tool for the job is not an obsession.
> 
> Persistently banging your head in contradiction to the facts of Python's
> data model, as you have been doing, starts to look very much like
> obsession.

Definition of insanity

Doing the same thing over and over again & expecting different results



-- 
Support staff hung over, send aspirin and come back LATER.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with the set of real numbers (was: Finding size of Variable)

2014-03-04 Thread Ian Kelly
On Mon, Mar 3, 2014 at 11:35 PM, Chris Angelico  wrote:
> In constant space, that will produce the sum of two infinite sequences
> of digits. (And it's constant time, too, except when it gets a stream
> of nines. Adding three thirds together will produce an infinite loop
> as it waits to see if there'll be anything that triggers an infinite
> cascade of carries.) Now, if there's a way to do that for square
> rooting a number, then the CF notation has a distinct benefit over the
> decimal expansion used here. As far as I know, there's no simple way,
> in constant space and/or time, to progressively yield more digits of a
> number's square root, working in decimal.

The code for that looks like this:

def cf_sqrt(n):
"""Yield the terms of the square root of n as a continued fraction."""
   m = 0
d = 1
a = a0 = floor_sqrt(n)
while True:
yield a
next_m = d * a - m
next_d = (n - next_m * next_m) // d
if next_d == 0:
break
next_a = (a0 + next_m) // next_d
m, d, a = next_m, next_d, next_a


def floor_sqrt(n):
"""Return the integer part of the square root of n."""
n = int(n)
if n == 0: return 0
lower = 2 ** int(math.log(n, 2) // 2)
upper = lower * 2
while upper - lower > 1:
mid = (upper + lower) // 2
if n < mid * mid:
upper = mid
else:
lower = mid
return lower


The floor_sqrt function is merely doing a simple binary search and
could probably be optimized, but then it's only called once during
initialization anyway.  The meat of the loop, as you can see, is just
a constant amount of integer arithmetic.  If it were desired to halt
once the continued fraction starts to repeat, that would just be a
matter of checking whether the triple (m, d, a) has been seen already.

Going back to your example of adding generated digits though, I don't
know how to add two continued fractions together without evaluating
them.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with the set of real numbers (was: Finding size of Variable)

2014-03-04 Thread Ian Kelly
On Tue, Mar 4, 2014 at 4:19 AM, Ian Kelly  wrote:
> def cf_sqrt(n):
> """Yield the terms of the square root of n as a continued fraction."""
>m = 0
> d = 1
> a = a0 = floor_sqrt(n)
> while True:
> yield a
> next_m = d * a - m
> next_d = (n - next_m * next_m) // d
> if next_d == 0:
> break
> next_a = (a0 + next_m) // next_d
> m, d, a = next_m, next_d, next_a

Sorry, all that "next" business is totally unnecessary.  More simply:

def cf_sqrt(n):
"""Yield the terms of the square root of n as a continued fraction."""
m = 0
d = 1
a = a0 = floor_sqrt(n)
while True:
yield a
m = d * a - m
d = (n - m * m) // d
if d == 0:
break
a = (a0 + m) // d
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 11:56:07 +0100, Antoon Pardon wrote:

> Op 04-03-14 09:56, Steven D'Aprano schreef:
> 
> 
>>
>>> If you
>>> explicitly say that this is an int, then yes, that should be
>>> disallowed;
>> It's that "explicitly" part that doesn't follow. Having to manage types
>> is the most tedious, boring, annoying, *unproductive* part of languages
>> like Java, C and Pascal. Almost always, you're telling the compiler
>> stuff that it can work out for itself.
> 
> In the same way writing unit tests is the most tedious, boring,
> annoying, *unproductive* part. 

Actually, I like writing unit tests. How do you know what the function 
does until you test it? I'm not a TDD fanatic, but often I write tests 
before I write the code, and there's really nothing nicer than seeing a 
whole lot of failing unit tests suddenly start working.

Well, maybe a nice BLT sandwich, when the bacon is nice and lean and the 
lettuce crisp and the tomato flavourful and not too soggy. But other than 
that, writing unit tests and seeing them pass is great.

On the other hand, writing

int n, m
double x, y

and similar three hundred times throughout your program is not terribly 
exciting. Even when it compiles, it doesn't mean it works.

> Amost always you are giving the program 
> results it can work out for itself.

Not even close. I'd like to see the compiler that can work out for itself 
that this function is buggy:

def sine_rule(side_a, side_b, angle_a):
"""Return the angle opposite side_b."""
return math.sin(side_b/side_a)*angle_a


If you don't remember your Sine Rule from trigonometry, that's okay. 
Trust me, the function is badly wrong. It's rubbish really. But short of 
some really impressive AI coupled with a Mathematica-level of maths 
knowledge, how is the compiler supposed to know that?

Static type testing, while valuable, cannot tell you that the program 
does what you wanted it to do.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 11:10:34 +, Alister wrote:

> Definition of insanity
> 
> Doing the same thing over and over again & expecting different results

*rolls dice*


:-)



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


find and replace string in binary file

2014-03-04 Thread loial
How do I read a binary file, find/identify a character string and replace it 
with another character string and write out to another file?

Its the finding of the string in a binary file that I am not clear on.

Any help appreciated
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with the set of real numbers

2014-03-04 Thread Chris Angelico
On Tue, Mar 4, 2014 at 10:05 PM, Gregory Ewing
 wrote:
> Chris Angelico wrote:
>>
>> In constant space, that will produce the sum of two infinite sequences
>> of digits.
>
>
> It's not constant space, because the nines counter
> can grow infinitely large.

Okay, okay, technically yes. But the counter can go a long way up
before it takes up any additional space, so all that's really saying
is that this has a major flaw with anything that produces a long
stream of nines. It can't tell the difference between
.8 and .[11] where the 11
suddenly carries and it has to flip it all back.

Anyway, that was like ten minutes' knocking-together work, you can't
expect it to be perfect. I'm amazed it even worked. :)

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


Re: Origin of 'self'

2014-03-04 Thread MRAB

On 2014-03-04 02:09, Dennis Lee Bieber wrote:

On Sun, 2 Mar 2014 22:16:31 -0800 (PST), Westley Martínez
 declaimed the following:


I understand that in an object method the first argument in the
object itself, called self.  However, it doesn't have to be called
self, and can be called anything.  So my question is why is it
called self and not this like from C++ and Java.  It's kind of a
silly question, but one that I'm curious about nevertheless.



It didn't want to be egotistical (as I recall, M$ VB uses "me")


So does AppleScript.


In AppleScript a script can refer to the title of a window as "title of
window" or "window's title", and it can refer to the title of its own
window as "title of window of me" or "me's window's title". Consistent,
yes, but bad English.

That's why I prefer a programming language not to be too much like a
natural language. :-)


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


Re: Reference

2014-03-04 Thread Alexander Blinne
Am 03.03.2014 19:48, schrieb Terry Reedy:
> The 'is' operator has three uses, two intended and one not. In
> production code, 'is' tests that an object *is* a particular singular
> object, such as None or a sentinel instance of class object.

Just a bit of statistics on this one from a recent small project:

<13:51:20> alex@firefly$ grep ' is ' *.py | wc
 65 4153234
<13:51:35> alex@firefly$ grep ' is None' *.py | wc
 43 2431948
<13:51:40> alex@firefly$ grep ' is not None' *.py | wc
 21 1671241
<13:51:44> alex@firefly$ grep ' is False' *.py | wc
  1   5  45

No other uses if 'is' found in almost 3 KLOC...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Tue, Mar 4, 2014 at 10:47 PM, Steven D'Aprano
 wrote:
> Not even close. I'd like to see the compiler that can work out for itself
> that this function is buggy:
>
> def sine_rule(side_a, side_b, angle_a):
> """Return the angle opposite side_b."""
> return math.sin(side_b/side_a)*angle_a
>
>
> If you don't remember your Sine Rule from trigonometry, that's okay.
> Trust me, the function is badly wrong. It's rubbish really.

I'm not entirely sure what it's trying to do, but you're taking the
sine of a ratio. That... seems wrong, gut-feeling-wise. You take the
sine of an angle and get a ratio, or the arcsine of a ratio and get an
angle. Also, trig functions apply only to right triangles, so the
other angle is either going to be 90°-angle_a or 90°, depending on
whether you want the angle opposite the hypotenuse or not.

But it's years since I studied any of that.

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


Re: find and replace string in binary file

2014-03-04 Thread MRAB

On 2014-03-04 12:27, loial wrote:

How do I read a binary file, find/identify a character string and
replace it with another character string and write out to another
file?

Its the finding of the string in a binary file that I am not clear
on.

Any help appreciated


Read it in chunks and search each chunk (the chunks should be at least
as long as the search string).

You should note that the string you're looking for could be split
across 2 chunks, so when writing the code make sure that you include
some overlap between adjacent chunks (it's best if the overlap is at
least N-1 characters, where N is the length of the search string).
--
https://mail.python.org/mailman/listinfo/python-list


Re: find and replace string in binary file

2014-03-04 Thread Peter Otten
loial wrote:

> How do I read a binary file, find/identify a character string and replace
> it with another character string and write out to another file?
> 
> Its the finding of the string in a binary file that I am not clear on.

That's not possible. You have to convert either binary to string or string 
to binary before you can replace. Whatever you choose, you have to know the 
encoding of the file. Consider

#python3
ENCODING = "iso-8859-1"
with open(source, encoding=ENCODING) as infile:
data = infile.read()
with open(dest, "w", encoding=ENCODING) as outfile:
outfile.write(data.replace("nötig", "möglich"))

If the file is indeed iso-8859-1 this will replace occurrences of the bytes

b'n\xf6tig' with b'm\xf6glich'

But if you were guessing wrong and the file is utf-8 it may contain the 
bytes b'n\xc3\xb6tig' instead which are incorrectly interpreted by your 
script as 'nötig' and thus left as is.


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


Re: Idle thread (Polling) python GUI and saving program state

2014-03-04 Thread MRAB
On 2014-03-04 02:41, Rolando wrote:> On Monday, March 3, 2014 6:06:22 PM 
UTC-8, MRAB wrote:

>> On 2014-03-04 01:33, Rolando wrote:
>>  > I have a GUI with a bunch of cells which is my "View" in the MVC
>>  > design. The user enters some information in the view and I pass
>>  > this on to the model so that using the information entered by
>>  > the user it(model) can do some processing.
>>  >
>>  > I have coded up my model as a state machine. Wherein, once a
>>  > function is complete it switches to the next state and so on.
>>  >
>>  > I want to know how do I save the program state between restarts.
>>  > Basically, I'm trying to handle a scenario wherein if someone
>>  > closes the program. The next time someone starts it up, it
>>  > should start from where it left off (both the view and model)
>>  >
>> When the user closes the window (you don't say what you're using for
>> the GUI, but there should be a way of detecting when window closes),
>> save the necessary info to a file using, say, the 'json' module. You
>> can then reload the info from the file when the program is
>< restarted.
>>
>>  > Also, I want to setup a poll method where once in a while I can
>>  > poll each cell to know the state it is in. How do I do this? I
>>  > don't want my GUI to hang when I'm doing anything. It will be
>>  > great if someone could help. Thanks!
>>  >
>> Can the model run continuously? If so, you should run it in its own
>> (background) thread and the GUI in the main thread and communicate
>> with the model's thread via, say, a queue (for that use the 'queue'
>> module).
>
> I'm using wxPython for my GUI. I have tried running the model thread 
in the background. But how will I save the state if the whole model is 
running on a thread? When the program is restarted I want the program to 
continue from the same state where it left off for each cell. I was 
thinking of having an idle thread that calls a poll method which polls 
all the cells and checks what each one is doing. But, I don't know how 
to do that. I'm confused.

>
Please read this:

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

because Gmail is formatting your email badly, making it harder to read.


When the front-end wants to quit, it tells the back-end and then waits
for it to terminate before itself quitting.

When the back-end receives the message that it should quit, it saves
its state and terminates.

When the front-end starts up again, it starts the back-end.

When the back-end starts, it load the state and runs.

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


Re: Functional programming

2014-03-04 Thread BartC

"Steven D'Aprano"  wrote in message
news:53159540$0$2923$c3e8da3$76491...@news.astraweb.com...


It's that "explicitly" part that doesn't follow. Having to manage types
is the most tedious, boring, annoying, *unproductive* part of languages
like Java, C and Pascal. Almost always, you're telling the compiler stuff
that it can work out for itself.


Isn't creating classes in Python similar to creating types elsewhere?


In the same way that managing jumps for GOTO has been automated with for
loops, while, etc., and managing memory has been automated, there's no
good reason not to allow the compiler to manage types. Dynamically typed
languages like Python do so at runtime. Type inference simply allows
statically typed languages to do the same only at compile time.


Eliminating 'goto' is simple code-generation logic; type inference is more
of an art.

But declaring variables is not just about specifying a type; it registers
the name too so that misspelled names can be picked up very early rather
than at runtime (and that's if you're lucky).


# Yes
[1, 3, 4, 2, 5, 9]
(1, "hello", None, 3.5)

# No
[1, "hello", None, 3.5]


That is, lists are for collections of data of arbitrary length, tuples
are for records or structs with dedicated fields.

That convention is a bit weaker these days than it used to be. Tuples now
have list-like methods, and we have namedtuple for record/struct-like
objects with named fields.


(Aren't tuples immutable? They wouldn't work well for records then, because 
it would be impossible to change a field of a record.)


--
Bartc 


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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 12:30 AM, BartC  wrote:
> But declaring variables is not just about specifying a type; it registers
> the name too so that misspelled names can be picked up very early rather
> than at runtime (and that's if you're lucky).

The two are separate. I don't know of any language that lets you
declare a type without catching the names, but there's certainly the
other way around (ECMAScript just has "var x, y, z"). It'd be
theoretically possible to have a Python-style "variable inference"
system (if I can call it that - the rules of "if you assign to it and
don't declare it as global/nonlocal, it's local") coupled with an
optional type declaration system; if you don't declare, then it can
hold anything. I just don't know of any language that does it.

>> That convention is a bit weaker these days than it used to be. Tuples now
>> have list-like methods, and we have namedtuple for record/struct-like
>> objects with named fields.
>
> (Aren't tuples immutable? They wouldn't work well for records then, because
> it would be impossible to change a field of a record.)

They are, including namedtuples. But an object() can be used that way,
if you want.

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


Re: Functional programming

2014-03-04 Thread Mark Lawrence

On 04/03/2014 13:30, BartC wrote:


But declaring variables is not just about specifying a type; it registers
the name too so that misspelled names can be picked up very early rather
than at runtime (and that's if you're lucky).



I've said before that this, to me, is one of the major downsides of 
dynamic typing.  Once a statically typed language has been compiled the 
programmer can head down to the pub.  The programmer using dynamically 
typed languages has to hang around doing long, boring, tedious testing. 
 Unless they're using an IDE like Pydev and have Pylint turned on so it 
picks up errors as they type, in which case they can also head down to 
the pub.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Reference

2014-03-04 Thread Chris Angelico
On Tue, Mar 4, 2014 at 11:55 PM, Alexander Blinne  wrote:
> Am 03.03.2014 19:48, schrieb Terry Reedy:
>> The 'is' operator has three uses, two intended and one not. In
>> production code, 'is' tests that an object *is* a particular singular
>> object, such as None or a sentinel instance of class object.
>
> Just a bit of statistics on this one from a recent small project:
>
> <13:51:20> alex@firefly$ grep ' is ' *.py | wc
>  65 4153234
> <13:51:35> alex@firefly$ grep ' is None' *.py | wc
>  43 2431948
> <13:51:40> alex@firefly$ grep ' is not None' *.py | wc
>  21 1671241
> <13:51:44> alex@firefly$ grep ' is False' *.py | wc
>   1   5  45
>
> No other uses if 'is' found in almost 3 KLOC...

Lemme spin you up a different way of doing it, which actually looks
for the operators.

https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py

Run across the Python stdlib, that tells me there are 4040 uses of
is/is not, of which 16 compare against False, 18 against True (see?
Python has a bias for truth above falsehood!), and 3386 against None.
The other 620 are probably mostly sentinel objects, but I didn't look
at them.

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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 1:05 AM, Mark Lawrence  wrote:
> On 04/03/2014 13:30, BartC wrote:
>>
>>
>> But declaring variables is not just about specifying a type; it registers
>> the name too so that misspelled names can be picked up very early rather
>> than at runtime (and that's if you're lucky).
>>
>
> I've said before that this, to me, is one of the major downsides of dynamic
> typing.  Once a statically typed language has been compiled the programmer
> can head down to the pub.  The programmer using dynamically typed languages
> has to hang around doing long, boring, tedious testing.  Unless they're
> using an IDE like Pydev and have Pylint turned on so it picks up errors as
> they type, in which case they can also head down to the pub.

Type declarations are orthogonal to that. ECMAScript, as mentioned,
just has 'var'. If it didn't have the implicit variables rule
(anything not explicitly declared goes onto the primary object), it'd
give you exactly that functionality, without any type checking at all.

And there's not "static" and "dynamic". It's a spectrum. Each time you
move one direction, you gain a set of potential bugs that the language
can detect; each time you move the other direction, you save on
keyboarding. But at no time do you truly get away from the need to
test, because anything non-trivial can't be proven by the language
anyway.

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


Re: Origin of 'self'

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 12:47:09 +, MRAB wrote:

> In AppleScript a script can refer to the title of a window as "title of
> window" or "window's title", and it can refer to the title of its own
> window as "title of window of me" or "me's window's title". Consistent,
> yes, but bad English.
> 
> That's why I prefer a programming language not to be too much like a
> natural language. :-)

But the problem with that is not that it is too much like a natural 
language, but too little like a natural language.



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


OT Sine Rule [was Re: Functional programming]

2014-03-04 Thread Steven D'Aprano
On Wed, 05 Mar 2014 00:01:01 +1100, Chris Angelico wrote:

> On Tue, Mar 4, 2014 at 10:47 PM, Steven D'Aprano
>  wrote:
>> Not even close. I'd like to see the compiler that can work out for
>> itself that this function is buggy:
>>
>> def sine_rule(side_a, side_b, angle_a):
>> """Return the angle opposite side_b.""" return
>> math.sin(side_b/side_a)*angle_a
>>
>>
>> If you don't remember your Sine Rule from trigonometry, that's okay.
>> Trust me, the function is badly wrong. It's rubbish really.
> 
> I'm not entirely sure what it's trying to do, 

The Sine Rule applies to any triangle, right-angled or not. I'm not going 
to try to draw an ASCII-art triangle here, so you just have to imagine 
one. Label the three sides "a", "b" and "c". Now label the angle opposite 
each side as "A", "B" and "C", so angle A is opposite side a, and so 
forth. The Sine Rule, or Law of Sines, tells us that the ratio of the 
length of a side and the sine of the angle opposite that side is constant 
for any triangle. That is:

a/sin(A) == b/sin(B) == c/sin(C)


Given any two sides and one corresponding angle, let's say a, b and A, 
you can calculate the other angle B. The correct formula would be:

B = asin( sin(A)*b/a )

which is not what my bogus function does.


> but you're taking the sine
> of a ratio. That... seems wrong, gut-feeling-wise. 

Well of course it's wrong. Given the intended geometric meanings of the 
function parameters, the quantity calculated is meaningless. But 
mathematically, it's okay to calculate the sine of a ratio of two other 
quantities. It's just a number. These even a whole lot of double-angle 
formulae that allow you to calculate sin(2*x) given sin(x), or sin(x/2) 
given sin(x), etc.

But, giving that ratio physical or geometric meaning is another story, 
and in this case the semantics of the function is entirely bogus. That 
was my point -- the compiler cannot possibly tell what the function is 
intended to do, it can only check that it is self-consistent.


> You take the sine of
> an angle and get a ratio, or the arcsine of a ratio and get an angle.
> Also, trig functions apply only to right triangles, 

Not quite. Or to put it another way, not at all :-)

Trig functions are *defined* in terms of right-angled triangles, but 
(say) the sine of 30° is 0.5 regardless of whether that 30° angle is in a 
right-angled triangle, an acute triangle or an obtuse triangle. 30° is 
30° regardless of what the rest of the triangle is doing.



Ask-me-about-versine-and-haversine-ly y'rs,

-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OT Sine Rule [was Re: Functional programming]

2014-03-04 Thread Tim Chase
On 2014-03-04 14:25, Steven D'Aprano wrote:
> Ask-me-about-versine-and-haversine-ly y'rs,

More interested in a karosine, cuisine, and a limousine. ;-)

-tkc


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


Re: OT Sine Rule [was Re: Functional programming]

2014-03-04 Thread Mark Lawrence

On 04/03/2014 14:37, Tim Chase wrote:

On 2014-03-04 14:25, Steven D'Aprano wrote:

Ask-me-about-versine-and-haversine-ly y'rs,


More interested in a karosine, cuisine, and a limousine. ;-)

-tkc




What do you get if you differentiate versines, haversines, karosines, 
cuisines and limosines?


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 14:05:44 +, Mark Lawrence wrote:

> Once a statically typed language has been compiled the programmer can
> head down to the pub.

"It compiles? Quick! Ship it!"

Well, that certainly explains the quality of some programs...




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functional programming

2014-03-04 Thread Grant Edwards
On 2014-03-03, Ben Finney  wrote:
> Gregory Ewing  writes:
>
>> Just because the compiler *can* infer the return type doesn't
>> necessarily mean it *should*. When I was playing around with
>> functional languages, I ended up adopting the practice of always
>> declaring the types of my functions, because it helps the *human*
>> reader.
>
> Sure. In a duck-typed language like Python, it is still helpful to the
> human reader to document the *meaning* of each parameter, beyond what is
> indicated by the name. We have reStructuredText and docstrings for this
> purpose.
>
> def frobnicate(flang, splets, queeble=False):
> """ Righteously frobnicate the flang.
>
> :param flang: A file-like object, opened for reading.
> :param splets: A sequence of unprocessed Splet instances.
> :param queeble: If ``True``, re-vitrify the flang during
> frobnication.
> :return: A new list of processed Splet instances.
>
> The flang is frobnicated according to the Weebly-Ruckford
> algorithm.
>
> """
> for line in flang:

That's fine, if the comments are correct.  

I'm currently working with a library of third party code that was
internally documented like that (though in a different language, with
a slightly different comment formatting). Then they run it through
something (Doxygen?) to produce a giant .CHM file that's pretty much
useless to those of us running Linux.  It turns out it's just as well
I can't read a CHM file: the documentation in the comments is wrong
often enough that I've learned it's best to ignore it completely.
Sometimes the number of parameters and their names don't even match up
with the comments. Sometimes the "docstring" is from a completely
different function which was apparently cut/pasted and then reworked
to do something else.

After a couple decades of working in software development, I've
decided that comments like that are not correct often enough to be
useful. You've got to reverse-engineer the code if there's no such
comment.  If there _is_ a comment, you have to reverse-engineer the
code to see of the comment is accurate.

-- 
Grant Edwards   grant.b.edwardsYow! I'm young ... I'm
  at   HEALTHY ... I can HIKE
  gmail.comTHRU CAPT GROGAN'S LUMBAR
   REGIONS!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OT Sine Rule [was Re: Functional programming]

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 1:25 AM, Steven D'Aprano
 wrote:
>
> The Sine Rule, or Law of Sines, tells us that the ratio of the
> length of a side and the sine of the angle opposite that side is constant
> for any triangle. That is:
>
> a/sin(A) == b/sin(B) == c/sin(C)

Oh! Right. Now I remember. Yeah. Still, it looks wrong to... well,
what I said next:

> On Wed, 05 Mar 2014 00:01:01 +1100, Chris Angelico wrote:
>> but you're taking the sine
>> of a ratio. That... seems wrong, gut-feeling-wise.
>
> Well of course it's wrong. Given the intended geometric meanings of the
> function parameters, the quantity calculated is meaningless. But
> mathematically, it's okay to calculate the sine of a ratio of two other
> quantities. It's just a number. These even a whole lot of double-angle
> formulae that allow you to calculate sin(2*x) given sin(x), or sin(x/2)
> given sin(x), etc.

What I meant there was "ratio of side lengths". The definition of sine
is that, in a right triangle, the ratio of the lengths of the side
opposite an angle and the hypotenuse is the sine of that angle. So I
consider sine to be a function that takes an angle and returns a
side-length-ratio, and arcsine does the reverse. It's like asking for
the length of an integer and getting back a string - it just looks
wrong. The sine of double an angle makes sense - it's still an angle.
You don't multiply an angle by a side length, you don't take the sine
of a number of meters per second, and you don't calculate the number
of parsecs it takes you to get to Kessel. The units are just wrong.
Maybe there's some specific situation where that makes sense, but I'd
call it "formula smell". And note that your corrected form still
applies the functions the way I describe - the units are maintained.
(Technically the sine of an angle is a pure number, a straight ratio.
I'm not sure that "pure number" is a unit - it's kinda the absence of
any unit - but that's still something to be maintained.)

> But, giving that ratio physical or geometric meaning is another story,
> and in this case the semantics of the function is entirely bogus. That
> was my point -- the compiler cannot possibly tell what the function is
> intended to do, it can only check that it is self-consistent.

Right. It's a consequence of a type system that distinguishes floating
point from string, but not angle_in_degrees from length_in_meters.
It's theoretically possible to build a type system that's like that
(sometimes you can subclass to do that, and create explicit operations
only), but I've never really felt the need to. But that's the theory
behind some forms of Hungarian notation - identifying a "data type"
concept that's broader than the compiler knows.

>> You take the sine of
>> an angle and get a ratio, or the arcsine of a ratio and get an angle.
>> Also, trig functions apply only to right triangles,
>
> Not quite. Or to put it another way, not at all :-)
>
> Trig functions are *defined* in terms of right-angled triangles, but
> (say) the sine of 30° is 0.5 regardless of whether that 30° angle is in a
> right-angled triangle, an acute triangle or an obtuse triangle. 30° is
> 30° regardless of what the rest of the triangle is doing.

Yes indeed. Like I said, I'm a bit rusty on all that, and forgot about
the ways of using them in non-right triangles :) But that 0.5 doesn't
have intrinsic meaning if you don't have a right triangle around it;
it needs something else to give it useful meaning, like another
angle's sine.

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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 1:55 AM, Steven D'Aprano
 wrote:
> On Tue, 04 Mar 2014 14:05:44 +, Mark Lawrence wrote:
>
>> Once a statically typed language has been compiled the programmer can
>> head down to the pub.
>
> "It compiles? Quick! Ship it!"
>
> Well, that certainly explains the quality of some programs...

It explains the quality of other programs too. They were written after
the programmer came back from the pub.

*ducks for cover*

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


Re: Reference

2014-03-04 Thread Jerry Hill
On Mon, Mar 3, 2014 at 11:52 PM, Steven D'Aprano  wrote:
> If your intention is to treat None as a singleton sentinel, not as a
> value, then you ought to use "is" to signal that intention, rather than
> using == even if you know that there won't be any false positives.

In all of the years I've been on this list, I don't think I've seen
more than one or two cases of someone deliberately treating None as a
singleton sentinel.  In most cases, they're either checking the return
value from a function or using it as a default argument to a function
to force some default behavior when no parameter is passed.  I'm
pretty sure you're going to say that the latter use is exactly where
you should us 'is' instead of '=='.  Respectfully, I disagree.

For a beginning python user, identity checking is an attractive
nuisance. The only time most beginners should use it is when comparing
to None.  But, as soon as they are taught that there are two
comparison operators, I start to see 'is' cropping up in more and more
places where they ought to use '=='.  And the problem is that
sometimes it works for them, and sometimes it doesn't.  Sure, students
eventually need to understand the difference between identity and
equality.  My problem is that by enshrining in python custom that the
only correct way to compare to None is with 'is', we have to explain
that concept way early in the teaching process.  I can't count the
number of times that a thread has completely derailed into identity vs
equality, then into interning of strings and small integers, and
suddenly the thread is 40 messages long, and no one has actually
talked about the code that was originally posted beyond that issue.
In approximately zero cases, have I seen code where 'is' versus '=='
actually made any difference, except where the 'is' is wrong.  I've
also never seen the supposedly ever-present boogie man of an object
that mistakenly compares equal to None, much less seen that object
passed to functions with None-based sentinels.

I feel like 'is' is an operator that ought to be saved for an advanced course.

Out of curiosity, do you think we should be doing truth checking with
'is'?  True and False are singletons, and it seems to me that the
justification for idenity versus equality should be just as strong
there, but I don't think I've ever seen anyone even suggest that.

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


Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 13:30:04 +, BartC wrote:

> "Steven D'Aprano"  wrote in message
> news:53159540$0$2923$c3e8da3$76491...@news.astraweb.com...
> 
>> It's that "explicitly" part that doesn't follow. Having to manage types
>> is the most tedious, boring, annoying, *unproductive* part of languages
>> like Java, C and Pascal. Almost always, you're telling the compiler
>> stuff that it can work out for itself.
> 
> Isn't creating classes in Python similar to creating types elsewhere?

Depends on the type: I suppose you can draw an analogy between records or 
structs and classes with no methods.

But I'm not talking about creating types, I'm talking about type 
declarations.

int x=2;  # 2 is an int? Who would have guessed!


>> In the same way that managing jumps for GOTO has been automated with
>> for loops, while, etc., and managing memory has been automated, there's
>> no good reason not to allow the compiler to manage types. Dynamically
>> typed languages like Python do so at runtime. Type inference simply
>> allows statically typed languages to do the same only at compile time.
> 
> Eliminating 'goto' is simple code-generation logic; type inference is
> more of an art.

Type inference is nothing like an art. It's a mathematically provable 
correct algorithm from the lambda calculus.

http://en.wikipedia.org/wiki/Hindley%E2%80%93Milner

Unfortunately the wikipedia page above appears to be complete 
gobbledygook if you aren't an expert in the lambda calculus, which I 
certainly am not, so I'm not even going to try to explain how it works. 
But there is no *guessing* involved, no heuristics which only sometimes 
work. There are certain assumptions involved, e.g. that the type of 
something is, in the absence of a declaration otherwise, the *most* 
general thing it could be (e.g. "it's an integer" rather than "it's an 
integer between 3 and 27"). But they're reasonable, practical assumptions.


> But declaring variables is not just about specifying a type; it
> registers the name too so that misspelled names can be picked up very
> early rather than at runtime (and that's if you're lucky).

You don't need to have static typing to have declared variables. The two 
are independent. E.g. one might have a system like Python, except you 
have to declare your variables before using them:

global x
local a
a = x+1
...

Or a system like (ancient) BASIC, where the type of the variable is given 
by the name. E.g. X is a numeric variable, and X$ is a string variable. 
There's no need for a separate declaration, because the presence or 
absence of the $ sign tells the interpreter whether it is a number or 
string variable. Perl, PHP and many other languages also use sigils.

Having to declare variables, and having to declare their type, are 
independent.


-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OT Sine Rule [was Re: Functional programming]

2014-03-04 Thread Tim Chase
On 2014-03-04 14:42, Mark Lawrence wrote:
> What do you get if you differentiate versines, haversines,
> karosines, cuisines and limosines?

Well, with cuisines, you can usually differentiate by seasoning:
your Tex/Mex is spicier and tends to have chili & cumin, while your
Indian tends to lean more towards the garam masala.

With your limosines, you have stretch and non-stretch, and some are
these crazy contraptions made out of Hummers or buses rather than
luxury sedans.

And if I could spell "kerosine" instead of "karosine", I guess they
would differentiate from other hydrocarbons by the length of the
carbon chain. ;-)

-tkc


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


Re: how to get bytes from bytearray without copying

2014-03-04 Thread Juraj Ivančić

On 3.3.2014. 2:27, Ian Kelly wrote:


Python 3.3 has a C API function to create a memoryview for a char*,
that can be made read-only.

 http://docs.python.org/3/c-api/memoryview.html#PyMemoryView_FromMemory

I don't see a way to do what you want in pure Python, apart from
perhaps writing an elaborate proxy class that would just be a poor
man's memoryview.  Or you could bite the bullet and copy everything
once at the start to create a bytes object, and then never have to
worry about it again.


Just for reference, it is doable in pure Python, with ctypes help:

pydll = ctypes.cdll.LoadLibrary("python{}{}".format(
sys.version_info.major, sys.version_info.minor))

def ro_memoryview_from_bytearray(buffer):
assert isinstance(buffer, bytearray)
ptr = ctypes.c_char_p(pydll.PyByteArray_AsString(
ctypes.py_object(buffer)))
mv_id = pydll.PyMemoryView_FromMemory(ptr, len(buffer), 0)
return ctypes.cast(mv_id, py_object).value

Note that this is just the jist, in real code I added safeguards to 
prevent misuse of the (temporary) memoryview.



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


Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 14:59:51 +, Grant Edwards wrote:

> After a couple decades of working in software development, I've decided
> that comments like that are not correct often enough to be useful.
> You've got to reverse-engineer the code if there's no such comment.  If
> there _is_ a comment, you have to reverse-engineer the code to see of
> the comment is accurate.

http://import-that.dreamwidth.org/956.html




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 2:19 AM, Jerry Hill  wrote:
> Out of curiosity, do you think we should be doing truth checking with
> 'is'?  True and False are singletons, and it seems to me that the
> justification for idenity versus equality should be just as strong
> there, but I don't think I've ever seen anyone even suggest that.

Normal truth testing is done like this:

if cond:

This isn't truth testing, this is checking the identity of what's in cond:

if cond is True:

And that's specifically testing for something much tighter than
truthiness. As you can see from my stats above, that's actually fairly
rare. Usually you'd just accept that True, 1, "yes", [1,2,3], and
1.2345 are all equally true.

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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 2:18 AM, Steven D'Aprano
 wrote:
> You don't need to have static typing to have declared variables. The two
> are independent. E.g. one might have a system like Python, except you
> have to declare your variables before using them:
>
> global x
> local a
> a = x+1

Aside: If you declare your locals, you shouldn't need to declare your
globals. Though I could imagine a rule that global rebinding still
needs to be declared, but you certainly shouldn't need to declare
nonlocal if you have a local declaration. Absence of local =>
nonlocal.

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


Re: Logging

2014-03-04 Thread Peter Otten
Igor Korot wrote:

> Hi, ALL,
> Could someone please explain to me how the code in
> http://docs.python.org/2/howto/logging#logging-from-multiple-modules
> works?
> In particular I'm interested in how the mylib.py knows about the
> myapp.log.
> 
> What I mean is: logging object is not passed to mylib.py, so
> essentially it should create a new instance of the logging object.
> 
> What am I missing?

loggers are cached and organized in a tree. If you ask for a logger

abc = logging.getLogger("a.b.c")

you get the same logger every time. At the top of the tree there is the root 
logger

>>> import logging
>>> root = logging.getLogger()
>>> abc = logging.getLogger("a.b.c")
>>> ab = logging.getLogger("a.b")
>>> a = logging.getLogger("a")
>>> abc.parent is ab
True
>>> ab.parent is a
True
>>> a.parent is root
True
>>> logging.getLogger("a.b.c") is abc
True
>>> 


logging.info(...) 

is basically a shortcut for

root.info(...)

that also does a basicConfig() if necessary.

By default loggers delegate handling log messages to their parent. You only 
have to tell the root logger what to do with the incoming messages.

There are optimizations (placeholders for the intermediate loggers), but the 
above is the basic concept.

> But this question comes from the following fact about my application.
> I tried to create a logging object which will store the logging
> information to the file in the main class. Then I pass this object to
> another class constructor and use it in that second class.
> 
> Upon running everything is OK, but when the program successfully
> finishes, the log file has 0 length.
> 
> AFAIU, I'm doing it properly and the example referenced is wrong, yet
> the results are completely different.
> 
> Thank you for any expplanation.

Did you run the example consisting of myapp.py and myapp.lib? Did it work? 
If so, what did you differently in your real app?

Please provide a minimal example showing the unexpected behaviour.

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


Re: Reference

2014-03-04 Thread Steven D'Aprano
On Tue, 04 Mar 2014 10:19:22 -0500, Jerry Hill wrote:

> On Mon, Mar 3, 2014 at 11:52 PM, Steven D'Aprano 
> wrote:
>> If your intention is to treat None as a singleton sentinel, not as a
>> value, then you ought to use "is" to signal that intention, rather than
>> using == even if you know that there won't be any false positives.
> 
> In all of the years I've been on this list, I don't think I've seen more
> than one or two cases of someone deliberately treating None as a
> singleton sentinel.  In most cases, they're either checking the return
> value from a function 

Okay, that's not *precisely* a sentinel, but it's related. I don't know 
what to call that, but it's a sentinel applied to the return result 
rather than to an input parameter.


> or using it as a default argument to a function to
> force some default behavior when no parameter is passed.

I call that a sentinel. It doesn't match the Wikipedia definition of a 
sentinel, but I think that's wrong. What Wikipedia calls a sentinel, I 
would call a guard.

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



> I'm pretty
> sure you're going to say that the latter use is exactly where you should
> us 'is' instead of '=='.  

Yes.


> Respectfully, I disagree.
> 
> For a beginning python user, identity checking is an attractive
> nuisance. 

True. In Hypertalk, which was designed for non-coders, the "is" operator 
was a synonym for "==". Even after nearly 20 years of using Python, I 
still sometimes instinctively write "x is y" when I mean equality, 
especially the "if __name__ is __main__" idiom.


> The only time most beginners should use it is when comparing
> to None.

We're agreed on that.


> But, as soon as they are taught that there are two comparison
> operators, I start to see 'is' cropping up in more and more places where
> they ought to use '=='.  And the problem is that sometimes it works for
> them, and sometimes it doesn't.  Sure, students eventually need to
> understand the difference between identity and equality.  My problem is
> that by enshrining in python custom that the only correct way to compare
> to None is with 'is', we have to explain that concept way early in the
> teaching process.  I can't count the number of times that a thread has
> completely derailed into identity vs equality, then into interning of
> strings and small integers, and suddenly the thread is 40 messages long,
> and no one has actually talked about the code that was originally posted
> beyond that issue. 

Heh heh, welcome to the Internet.


> In approximately zero cases, have I seen code where
> 'is' versus '==' actually made any difference, except where the 'is' is
> wrong.  I've also never seen the supposedly ever-present boogie man of
> an object that mistakenly compares equal to None, much less seen that
> object passed to functions with None-based sentinels.
> 
> I feel like 'is' is an operator that ought to be saved for an advanced
> course.
> 
> Out of curiosity, do you think we should be doing truth checking with
> 'is'?  True and False are singletons, and it seems to me that the
> justification for idenity versus equality should be just as strong
> there, but I don't think I've ever seen anyone even suggest that.

Normally you shouldn't compare to True or False at all. Python duck-types 
truth-values, or to put it another way, you should normally only care 
about truthy and falsey values:

# Duck-typing is your friend
if flag: ...

# These are buggy unless you know flag is an actual bool
if flag == True: ...
if flag is True: ...

# Why convert to a canonical bool flag just to do a comparison?
if bool(flag): ...

# Not this
if bool(flag) is True:

# I never know when to stop
if bool(flag) is True is True is True is True is True is True: ...




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functional programming

2014-03-04 Thread Steven D'Aprano
On Wed, 05 Mar 2014 02:28:17 +1100, Chris Angelico wrote:

> On Wed, Mar 5, 2014 at 2:18 AM, Steven D'Aprano
>  wrote:
>> You don't need to have static typing to have declared variables. The
>> two are independent. E.g. one might have a system like Python, except
>> you have to declare your variables before using them:
>>
>> global x
>> local a
>> a = x+1
> 
> Aside: If you declare your locals, you shouldn't need to declare your
> globals. Though I could imagine a rule that global rebinding still needs
> to be declared, but you certainly shouldn't need to declare nonlocal if
> you have a local declaration. Absence of local => nonlocal.

You missed that the purpose of the declaration is to avoid accidental 
typos:

local process
procces = 1234


With declarations, the compiler can catch some typos at compile-time.




-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 2:42 AM, Steven D'Aprano
 wrote:
> # I never know when to stop
> if bool(flag) is True is True is True is True is True is True: ...

The banana problem.

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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 2:45 AM, Steven D'Aprano
 wrote:
>> Aside: If you declare your locals, you shouldn't need to declare your
>> globals. Though I could imagine a rule that global rebinding still needs
>> to be declared, but you certainly shouldn't need to declare nonlocal if
>> you have a local declaration. Absence of local => nonlocal.
>
> You missed that the purpose of the declaration is to avoid accidental
> typos:
>
> local process
> procces = 1234
>
>
> With declarations, the compiler can catch some typos at compile-time.

Yep, but if you're declaring all your locals (and globals get declared
at module scope - they're just local to a different and broader
scope), then "procces" will never have been declared anywhere. You
shouldn't need to re-declare everything you're referencing from an
outer scope.

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


Re: Decoding a process output

2014-03-04 Thread Peter Otten
Francis Moreau wrote:

> Hi,
> 
> In my understanding (I'm relatively new to python), I need to decode any
> bytes data provided, in my case, by a shell command (such as findmnt)
> started by the subprocess module. The goal of my application is to parse
> the command outputs.
> 
> My application runs only on linux BTW and should run fine on both python
> 2.7 and py3k.
> 
> My question is when decoding the output bytes data of the external
> command, which encoding should I use ?
> 
> Should I guess the encoding by inspecting LANG or any LC_* environment
> variables ?
> 
> Should I force one of those environment variable to a specific value
> before running my external command ?
> 
> Thanks for any tips.

You can use locale.getpreferredencoding(), which seems to evaluate LANG:

$ python3 -c 'import locale; print(locale.getpreferredencoding())'
UTF-8
$ LANG= python3 -c 'import locale; print(locale.getpreferredencoding())'
ANSI_X3.4-1968

I haven't seen a Linux system that doesn't use UTF-8 for a while, but you 
have to remember that filenames are still arbitrary byte sequences. You can 
cope with this in Python by not decoding the bytes or using surrogateescape

>>> os.mkdir(bytes([i for i in range(1, 256) if i != b"/"[0]]))
>>> os.listdir(b".")
[b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
 
!"#$%&\'()*+,-.0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff']
>>> os.listdir(".")
['\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f
 
!"#$%&\'()*+,-.0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\udc80\udc81\udc82\udc83\udc84\udc85\udc86\udc87\udc88\udc89\udc8a\udc8b\udc8c\udc8d\udc8e\udc8f\udc90\udc91\udc92\udc93\udc94\udc95\udc96\udc97\udc98\udc99\udc9a\udc9b\udc9c\udc9d\udc9e\udc9f\udca0\udca1\udca2\udca3\udca4\udca5\udca6\udca7\udca8\udca9\udcaa\udcab\udcac\udcad\udcae\udcaf\udcb0\udcb1\udcb2\udcb3\udcb4\udcb5\udcb6\udcb7\udcb8\udcb9\udcba\udcbb\udcbc\udcbd\udcbe\udcbf\udcc0\udcc1\udcc2\udcc3\udcc4\udcc5\udcc6\udcc7\udcc8\udcc9\udcca\udccb\udccc\udccd\udcce\udccf\udcd0\udcd1\udcd2\udcd3\udcd4\udcd5\udcd6\udcd7\udcd8\udcd9\udcda\udcdb\udcdc\udcdd\udcde\udcdf\udce0\udce1\udce2\udce3\udce4\udce5\udce6\udce7\udce8\udce9\udcea\udceb\udcec\udced\udcee\udcef\udcf0\udcf1\udcf2\udcf3\udcf4\udcf5\udcf6\udcf7\udcf8\udcf9\udcfa\udcfb\udcfc\udcfd\udcfe\udcff']

However, the typical shell tools have problems with names containing a 
newline or even space (and if not you may introduce such problems parsing 
the tool's output), so I'd like to see how findmnt responds to a mount point 
like the above directory.

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


Re: Reference

2014-03-04 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Wed, Mar 5, 2014 at 2:42 AM, Steven D'Aprano
>  wrote:
> > # I never know when to stop
> > if bool(flag) is True is True is True is True is True is True: ...
> 
> The banana problem.
> 
> ChrisA

You can refactor that as:

eval(" is ".join(itertools.chain(["if bool(flag)"], [str(t) for t in 
itertools.repeat(True)])))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get bytes from bytearray without copying

2014-03-04 Thread Stefan Behnel
Juraj Ivančić, 04.03.2014 16:23:
> Just for reference, it is doable in pure Python, with ctypes help

For some questionable meaning of "pure".

Stefan


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


Re: Reference

2014-03-04 Thread Rustom Mody
On Tuesday, March 4, 2014 8:49:22 PM UTC+5:30, Jerry Hill wrote:
> On Mon, Mar 3, 2014 at 11:52 PM, Steven D'Aprano wrote:
> > If your intention is to treat None as a singleton sentinel, not as a
> > value, then you ought to use "is" to signal that intention, rather than
> > using == even if you know that there won't be any false positives.

> In all of the years I've been on this list, I don't think I've seen
> more than one or two cases of someone deliberately treating None as a
> singleton sentinel.  In most cases, they're either checking the return
> value from a function or using it as a default argument to a function
> to force some default behavior when no parameter is passed.  I'm
> pretty sure you're going to say that the latter use is exactly where
> you should us 'is' instead of '=='.  Respectfully, I disagree.

> For a beginning python user, identity checking is an attractive
> nuisance. The only time most beginners should use it is when comparing
> to None.  But, as soon as they are taught that there are two
> comparison operators, I start to see 'is' cropping up in more and more
> places where they ought to use '=='.  And the problem is that
> sometimes it works for them, and sometimes it doesn't.  Sure, students
> eventually need to understand the difference between identity and
> equality.  My problem is that by enshrining in python custom that the
> only correct way to compare to None is with 'is', we have to explain
> that concept way early in the teaching process.  I can't count the
> number of times that a thread has completely derailed into identity vs
> equality, then into interning of strings and small integers, and
> suddenly the thread is 40 messages long, and no one has actually
> talked about the code that was originally posted beyond that issue.
> In approximately zero cases, have I seen code where 'is' versus '=='
> actually made any difference, except where the 'is' is wrong.  I've
> also never seen the supposedly ever-present boogie man of an object
> that mistakenly compares equal to None, much less seen that object
> passed to functions with None-based sentinels.

Beautifully put -- thanks Jerry!

> I feel like 'is' is an operator that ought to be saved for an advanced course.

+100

My choice: have an isNone function which should take care of 90%
of the cases of valid 'is' (if I got Chris' statistics right) and avoid
most of the metaphysical BS about identity
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How security holes happen

2014-03-04 Thread sffjunkie
On Monday, 3 March 2014 22:55:32 UTC, Chris Kaynor  wrote:
> You can go much simpler than that. Merely port Python to LISP, then write a 
> LISP interpreter in Python. Done.

http://blog.pault.ag/post/46982895940/heres-my-talk-from-pycon-2013-i-tried-to-queue
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: modification time in Python - Django: datetime != datetime :-(

2014-03-04 Thread donarb
Note that it's bad form to post the same question to different forums, you also 
posted this question to django-users. By posting to multiple forums, you run 
the risk of not having the question answered or followed up in one of the 
forums. This frustrates other users who may one day have a similar problem and 
find your orphaned question.

Remember, these forums are not just about you, they are also about those who 
follow later.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Functional programming

2014-03-04 Thread MRAB

On 2014-03-04 15:13, Chris Angelico wrote:

On Wed, Mar 5, 2014 at 1:55 AM, Steven D'Aprano
 wrote:

On Tue, 04 Mar 2014 14:05:44 +, Mark Lawrence wrote:


Once a statically typed language has been compiled the programmer can
head down to the pub.


"It compiles? Quick! Ship it!"

Well, that certainly explains the quality of some programs...


It explains the quality of other programs too. They were written after
the programmer came back from the pub.

*ducks for cover*


Or compiled before and tested after?

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


Re: How security holes happen

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 3:41 AM,   wrote:
> On Monday, 3 March 2014 22:55:32 UTC, Chris Kaynor  wrote:
>> You can go much simpler than that. Merely port Python to LISP, then write a 
>> LISP interpreter in Python. Done.
>
> http://blog.pault.ag/post/46982895940/heres-my-talk-from-pycon-2013-i-tried-to-queue

I don't have time to watch an hour-long video... what'd he do, exactly that?

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


Re: Origin of 'self'

2014-03-04 Thread MRAB

On 2014-03-04 14:27, Steven D'Aprano wrote:

On Tue, 04 Mar 2014 12:47:09 +, MRAB wrote:


In AppleScript a script can refer to the title of a window as
"title of window" or "window's title", and it can refer to the
title of its own window as "title of window of me" or "me's
window's title". Consistent, yes, but bad English.

That's why I prefer a programming language not to be too much like
a natural language. :-)


But the problem with that is not that it is too much like a natural
language, but too little like a natural language.


The more it's like a natural language, the more intelligent you expect
it to be, and the more you expect it to be able to work out ambiguities
for itself.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Reference

2014-03-04 Thread MRAB

On 2014-03-04 16:02, Chris Angelico wrote:

On Wed, Mar 5, 2014 at 2:42 AM, Steven D'Aprano
 wrote:

# I never know when to stop
if bool(flag) is True is True is True is True is True is True: ...


The banana problem.


Speaking of which:

The 'right' way to peel a banana
http://www.telegraph.co.uk/news/good-to-share/10664935/The-right-way-to-peel-a-banana.html

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


Re: How security holes happen

2014-03-04 Thread Skip Montanaro
On Tue, Mar 4, 2014 at 11:07 AM, Chris Angelico  wrote:
> I don't have time to watch an hour-long video... what'd he do, exactly that?

If you fast forward to 16:14, his talk is about five minutes long. He
wrote a Lisp compiler whose backend is Python.

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


Re: modification time in Python - Django: datetime != datetime :-(

2014-03-04 Thread Roy Smith
In article ,
 donarb  wrote:

> Note that it's bad form to post the same question to different forums, you 
> also posted this question to django-users. By posting to multiple forums, you 
> run the risk of not having the question answered or followed up in one of the 
> forums. This frustrates other users who may one day have a similar problem 
> and find your orphaned question.

Even worse, you run the risk of having it answered differently in 
different places, and then you need to figure out which is right :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with the set of real numbers

2014-03-04 Thread Marko Rauhamaa
Chris Angelico :

> As far as I know, there's no simple way, in constant space and/or
> time, to progressively yield more digits of a number's square root,
> working in decimal.

I don't know why the constant space/time requirement is crucial. Anyway,
producing more digits simple: http://nrich.maths.org/5955>.

I believe producing the nth digit is O(n) in time and space.

Still, there's more to arithmetics than that. For example, if you have
two generated decimal expansions, you don't have an effective algorithm
to generate the decimal expansion of their ratio. That's because there's
no effective algorithm to decide if a < b.


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


Re: Functional programming

2014-03-04 Thread Marko Rauhamaa
Antoon Pardon :

> In the same way writing unit tests is the most tedious, boring,
> annoying, *unproductive* part. Amost always you are giving the program
> results it can work out for itself.

Undoubtedly, explicit type declarations add a dimension of quality to
software. However, they also significantly reduce readability and tempt
you to dirty shortcuts (to avoid writing truckloads of boilerplate
code).

On the balance, I estimate the explicit style reduces code quality.

Example (found by a random Google search):

===JAVA BEGIN===

class WrappedSqlException extends RuntimeException {
static final long serialVersionUID = 2013080804480L;
public WrappedSqlException(SQLException cause) { super(cause); }
public SQLException getSqlException() { return (SQLException) getCause(); }
}

public ConnectionPool(int maxConnections, String url) throws SQLException {
try {
super(() -> {
try {
return DriverManager.getConnection(url);
} catch ( SQLException ex ) {
throw new WrappedSqlException(ex);
}
}, maxConnections);
} catch (WrappedSqlException wse) {
throw wse.getSqlException();
}
}

===JAVA END=

===PYTHON BEGIN=

def __init__(self, max_connections, url):
super().__init__(lambda: DriverManager.get_connection(url), max_connections)

===PYTHON END===

or, a bit less cryptically:

===PYTHON BEGIN=

def __init__(self, max_connections, url):
def get_connection():
return DriverManager.get_connection(url)

super().__init__(get_connection, max_connections)

===PYTHON END===


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


Re: Working with the set of real numbers

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> As far as I know, there's no simple way, in constant space and/or
>> time, to progressively yield more digits of a number's square root,
>> working in decimal.
>
> I don't know why the constant space/time requirement is crucial. Anyway,
> producing more digits simple: http://nrich.maths.org/5955>.
>
> I believe producing the nth digit is O(n) in time and space.

The reason for striving for constant space/time is because the obvious
method (cut-and-try) is already O(n) for the nth digit, which means
it's quadratic on the number of digits desired. That gets pretty
nasty. So what I was asking was: By representing values as continued
fractions rather than as decimal digits, are you able to perform a
straight-forward transformation that produces the square root, in
constant time (which means linear in the length)? And I guess the
answer's no. CF representation doesn't have the advantage I was
wondering about.

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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa  wrote:
> public ConnectionPool(int maxConnections, String url) throws SQLException {
> try {
> super(() -> {
> try {
> return DriverManager.getConnection(url);
> } catch ( SQLException ex ) {
> throw new WrappedSqlException(ex);
> }
> }, maxConnections);
> } catch (WrappedSqlException wse) {
> throw wse.getSqlException();
> }
> }
>
> ===JAVA END=
>
> ===PYTHON BEGIN=
>
> def __init__(self, max_connections, url):
> super().__init__(lambda: DriverManager.get_connection(url), 
> max_connections)

You're not doing the same thing, though. The Java rigmarole is to
ensure that an SQLException thrown in getConnection will propagate up,
despite (presumably) something inside the equivalent of
super().__init__ that swallows SQLExceptions. Of course it looks
tidier when you don't do the messy bit.

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


NEW RESEARCH TEARS PALAEOANTHROPOLOGY APART: THE THRINAXODON TIMES

2014-03-04 Thread Thrinazodji0fecnfv

===
 >BREAKING NEWS!
===
NEW YORK TIMES, THRINAXODON, OHIO
=
 >
THRINAXODON RECENTLY FOUND 3 HUMAN FOSSILS FROM DEVONIAN STRATA FROM
GREENLAND, THE EVOLUTIONISTS HAVE NO BONES ABOUT.
 >
ONE EVIL EVOLUTIONIST, BOB CASANOVA HAS ADMITTED THAT HUMAN EVOLUTION IS
IN FREE-FALL.
 >
RICHARD LEAKEY HAS DIED FROM A HEART ATTACK DUE TO THIS GROUND-BREAKING
FIND THAT CONCLUSIVELY SHOWS THAT HUMANS HAVE ORIGINS IN THE DEVONIAN.
 >
NOW, IF YOU PLEASE, I HAVE TO SUE THE SMITHSONIAN FOR YEARS OF CENSORSHIP.
 >
==
EVIDENCE THAT HUMANS LIVED IN THE DEVONIAN:

https://groups.google.com/group/sci.bio.paleontology/browse_thread/thread/6f501c469c7af24f#

https://groups.google.com/group/sci.bio.paleontology/browse_thread/thread/3aad75c16afb0b82# 


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


Re: How security holes happen

2014-03-04 Thread Ned Batchelder

On 3/4/14 12:16 PM, Skip Montanaro wrote:

On Tue, Mar 4, 2014 at 11:07 AM, Chris Angelico  wrote:

I don't have time to watch an hour-long video... what'd he do, exactly that?


If you fast forward to 16:14, his talk is about five minutes long. He
wrote a Lisp compiler whose backend is Python.

Skip



It's Hy: http://hylang.org

--
Ned Batchelder, http://nedbatchelder.com

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


Re: Functional programming

2014-03-04 Thread Marko Rauhamaa
Chris Angelico :

> On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa  wrote:
>> public ConnectionPool(int maxConnections, String url) throws SQLException {
>> try {
>> super(() -> {
>> try {
>> return DriverManager.getConnection(url);
>> } catch ( SQLException ex ) {
>> throw new WrappedSqlException(ex);
>> }
>> }, maxConnections);
>> } catch (WrappedSqlException wse) {
>> throw wse.getSqlException();
>> }
>> }
>>
>> ===JAVA END=
>
> You're not doing the same thing, though. The Java rigmarole is to
> ensure that an SQLException thrown in getConnection will propagate up,
> despite (presumably) something inside the equivalent of
> super().__init__ that swallows SQLExceptions. Of course it looks
> tidier when you don't do the messy bit.

See http://stackoverflow.com/questions/14039995/
java-8-mandatory-checked-exceptions-handling-in-lambd
a-expressions-for-standard>.

The "rigmarole" is trying to get around Java's mandatory exception
handling limitations, which Python doesn't have.

You are not allowed to pass a lambda to the super constructor that
throws an SQLException. To get around the limitation, a RuntimeException
wrapper is created to smuggle the SQLException through the compiler's
defenses. Any code is free to throw a RuntimeException.

I don't know, though, if this is that good of an example. I don't know
if the lambda gets called within the constructor or, as I would guess,
whenever a new connection is needed. The whole wrapping exercise would
be for nothing, then.

Here's a more prosaic example (probably contains errors):

===JAVA BEGIN===

private Map> filings =
new TreeMap>();

class FormFiling {
public FormFiling(TaxpayerIdNumber taxpayerId, Form form) {
this.taxpayerId = taxpayerId;
this.form = form;
}

public TaxpayerIdNumber getTaxpayerId() { return taxpayerId; }
public Form getForm() { return form; }

private TaxpayerIdNumber taxpayerId;
private Form form;
};

List getForms(FormId formId)
{
List forms = new LinkedList();
for (Map.Entry> payerEntry :
 filings.entrySet()) {
TaxpayerIdNumber taxpayerId = payerEntry.getKey();
Map filing = payerEntry.getValue();
if (filing.containsKey(formId))
forms.add(new FormFiling(taxpayerId, filing.get(formId)))
}
return forms;
}

===JAVA END=

===PYTHON BEGIN=

filings = {}

def getForms(formId):
forms = []
for taxpayerId, filing in filings.iteritems():
 if formId in filing:
 forms.append((taxpayerId, filing[formId]))
return forms

===PYTHON END===

or:

===PYTHON BEGIN=

filings = {}

def getForms(formId):
return ((taxpayerId, filing[formId])
for (taxpayerId, filing) in filings.iteritems()
if formId in filing)

===PYTHON END===


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


Proper conversion of timestamp

2014-03-04 Thread Igor Korot
Hi, ALL,
I'm getting this:

timestamp out of range for platform localtime()/gmtime() function

trying to convert the timestamp with milliseconds into the datetime object.

The first hit of Google gives me this:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

but the solution described is not good for me since it does not gives
me the milliseconds value.

How do I get the proper datetime value including milliseconds from the
timestamp?

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


Re: Working with the set of real numbers

2014-03-04 Thread Oscar Benjamin
On 4 March 2014 19:58, Chris Angelico  wrote:
> On Wed, Mar 5, 2014 at 6:49 AM, Marko Rauhamaa  wrote:
>> Chris Angelico :
>>
>>> As far as I know, there's no simple way, in constant space and/or
>>> time, to progressively yield more digits of a number's square root,
>>> working in decimal.
>>
>> I don't know why the constant space/time requirement is crucial. Anyway,
>> producing more digits simple: http://nrich.maths.org/5955>.
>>
>> I believe producing the nth digit is O(n) in time and space.
>
> The reason for striving for constant space/time is because the obvious
> method (cut-and-try) is already O(n) for the nth digit, which means
> it's quadratic on the number of digits desired. That gets pretty
> nasty.

I don't quite follow your reasoning here. By "cut-and-try" do you mean
bisection? If so it gives the first N decimal digits in N*log2(10)
iterations. However each iteration requires a multiply and when the
number of digits N becomes large the multiplication is worse than
linear. So the result is something like N**2 log(N)log(log(N)),

To me the obvious method is Newton iteration which takes O(sqrt(N))
iterations to obtain N digits of precision. This brings the above
complexity below quadratic:

#!/usr/bin/env python

from decimal import Decimal as D, localcontext

def sqrt(y, prec=1000):
'''Solve x**2 = y'''
assert y > 0
eps = D(10) ** -(prec + 5)
x = D(y)
with localcontext() as ctx:
ctx.prec = prec + 10
while x ** 2 - y > x * eps:
x = (x + y/x) / 2
return x

print(sqrt(2))

Some modification would be required to handle a situation where it
ends in a run of nines or zeros if you really care about the exact
digits rather than having a bounded error.


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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 7:50 AM, Marko Rauhamaa  wrote:
> The "rigmarole" is trying to get around Java's mandatory exception
> handling limitations, which Python doesn't have.
>
> You are not allowed to pass a lambda to the super constructor that
> throws an SQLException. To get around the limitation, a RuntimeException
> wrapper is created to smuggle the SQLException through the compiler's
> defenses. Any code is free to throw a RuntimeException.
>

Oh, it's THAT problem. Well, it's still not really a fair comparison
of declared types. It shows how Python's much easier to work with, but
what you're showing off is the simpler exception handling :)

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


Re: Working with the set of real numbers

2014-03-04 Thread Marko Rauhamaa
Oscar Benjamin :

> To me the obvious method is Newton iteration which takes O(sqrt(N))
> iterations to obtain N digits of precision. This brings the above
> complexity below quadratic:
>
> #!/usr/bin/env python
>
> from decimal import Decimal as D, localcontext
>
> def sqrt(y, prec=1000):
> '''Solve x**2 = y'''
> assert y > 0
> eps = D(10) ** -(prec + 5)
> x = D(y)
> with localcontext() as ctx:
> ctx.prec = prec + 10
> while x ** 2 - y > x * eps:
> x = (x + y/x) / 2
> return x
>
> print(sqrt(2))

At a quick glance, I believe x ** 2 is O(N²) and so the total complexity
should be O(N ** 2.5).


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


Re: Working with the set of real numbers

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin
 wrote:
> I don't quite follow your reasoning here. By "cut-and-try" do you mean
> bisection? If so it gives the first N decimal digits in N*log2(10)
> iterations. However each iteration requires a multiply and when the
> number of digits N becomes large the multiplication is worse than
> linear. So the result is something like N**2 log(N)log(log(N)),

By "cut and try" I'm talking about the really REALLY simple algorithm
for calculating square roots. It's basically brute force.

epsilon = 0.0001
def sqrt(n):
guess1, guess2 = 1, n
while abs(guess1-guess2) > epsilon:
guess1 = n/guess2
guess2 = (guess1 + guess2)/2
   return guess1

It's generally going to take roughly O(n*n) time to generate n digits,
give or take. That's the baseline against which anything else can be
compared. There are plenty of better ways to calculate them.

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


Re: Functional programming

2014-03-04 Thread Marko Rauhamaa
Chris Angelico :

> Oh, it's THAT problem. Well, it's still not really a fair comparison
> of declared types. It shows how Python's much easier to work with, but
> what you're showing off is the simpler exception handling :)

The other example I gave is really bread-and-butter Java. An ergonomic
disaster and takes some staring at to figure out what's going on.

Note that Java doesn't possess typedef's so you really are pretty much
forced to write those < , < >>'s a lot.


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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 8:21 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> Oh, it's THAT problem. Well, it's still not really a fair comparison
>> of declared types. It shows how Python's much easier to work with, but
>> what you're showing off is the simpler exception handling :)
>
> The other example I gave is really bread-and-butter Java. An ergonomic
> disaster and takes some staring at to figure out what's going on.
>
> Note that Java doesn't possess typedef's so you really are pretty much
> forced to write those < , < >>'s a lot.

C++ at least has typedefs, and in the newer standards, the 'auto'
keyword was repurposed.

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


Re: Proper conversion of timestamp

2014-03-04 Thread MRAB

On 2014-03-04 20:57, Igor Korot wrote:

Hi, ALL,
I'm getting this:

timestamp out of range for platform localtime()/gmtime() function

trying to convert the timestamp with milliseconds into the datetime object.

The first hit of Google gives me this:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

but the solution described is not good for me since it does not gives
me the milliseconds value.

How do I get the proper datetime value including milliseconds from the
timestamp?

Thank you.


Are you using Python 2? If yes, then try dividing by 1000.0.

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


Re: python decimal library dmath.py v0.3 released

2014-03-04 Thread Stefan Krah

[I found this via the python-ideas thread]

Wolfgang Maier  biologie.uni-freiburg.de> writes:
> math.factorial is accurate and faster than your pure-Python function,
especially for large numbers.

It is slower for huge numbers than decimal if you use this
Python function:

http://www.bytereef.org/mpdecimal/quickstart.html#factorial-in-pure-python


Be sure to set MAX_EMAX and MIN_EMIN, that's missing in the example.


If you want to *see* all digits of a very large number, then decimal is
probably even faster than gmpy. See:

http://www.bytereef.org/mpdecimal/benchmarks.html#arbitrary-precision-libraries



Stefan Krah

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


Re: Proper conversion of timestamp

2014-03-04 Thread Mark Lawrence

On 04/03/2014 20:57, Igor Korot wrote:

Hi, ALL,
I'm getting this:

timestamp out of range for platform localtime()/gmtime() function

trying to convert the timestamp with milliseconds into the datetime object.

The first hit of Google gives me this:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

but the solution described is not good for me since it does not gives
me the milliseconds value.

How do I get the proper datetime value including milliseconds from the
timestamp?

Thank you.



You have a long record of asking timestamp related questions so you 
should know where the docs are that provide the answer to this question. 
 I'll leave you to go off and read them.  If you don't understand them, 
please cut and paste your code here, state what you expected to happen, 
what actually happened, including any traceback if applicable, and then 
we'll be happy to point you the error of your ways.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Functional programming

2014-03-04 Thread Marko Rauhamaa
Chris Angelico :

> C++ at least has typedefs, and in the newer standards, the 'auto'
> keyword was repurposed.

Last I checked, C++ had no satisfactory way to express
callbacks/functors/listeners/lambdas. That's why Qt came up with a
metacompiler to supplement C++'s facilities.

No, STL and Boost can't remedy the situation.

The main reason was the unfortunate way method pointers were defined in
C++. C#'s delegates and Java's anonymous inner classes are something a
C++ developer can only dream of (unless something has already been
dreamt up in a recent standard).

Python, of course, has delegates:

   that_object.register_callback(self.handle_it)

Python doesn't have anonymous inner classes, but it has named inner
classes, and that's quite sufficient.


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


Re: Proper conversion of timestamp

2014-03-04 Thread Igor Korot
MRAB,


On Tue, Mar 4, 2014 at 1:38 PM, MRAB  wrote:

> On 2014-03-04 20:57, Igor Korot wrote:
>
>> Hi, ALL,
>> I'm getting this:
>>
>> timestamp out of range for platform localtime()/gmtime() function
>>
>> trying to convert the timestamp with milliseconds into the datetime
>> object.
>>
>> The first hit of Google gives me this:
>>
>> http://stackoverflow.com/questions/12458595/convert-
>> epoch-timestamp-in-python
>>
>> but the solution described is not good for me since it does not gives
>> me the milliseconds value.
>>
>> How do I get the proper datetime value including milliseconds from the
>> timestamp?
>>
>> Thank you.
>>
>>  Are you using Python 2? If yes, then try dividing by 1000.0.
>

Yes, I'm using python 2.7.
But dividing by 1000 will give the precision in seconds, i.e. "-MM-DD
HH:MM:SS".

What I want is to have this: "-MM-DD HH:MM:SS.XXX", where "XXX" is a
milliseconds.

Thank you.


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


Re: Proper conversion of timestamp

2014-03-04 Thread Mark Lawrence

On 04/03/2014 21:38, MRAB wrote:

On 2014-03-04 20:57, Igor Korot wrote:

Hi, ALL,
I'm getting this:

timestamp out of range for platform localtime()/gmtime() function

trying to convert the timestamp with milliseconds into the datetime
object.

The first hit of Google gives me this:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python


but the solution described is not good for me since it does not gives
me the milliseconds value.

How do I get the proper datetime value including milliseconds from the
timestamp?

Thank you.


Are you using Python 2? If yes, then try dividing by 1000.0.



You learn something new every day, I wasn't aware that you could 
multiply or divide timestamps.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: Functional programming

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 8:43 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> C++ at least has typedefs, and in the newer standards, the 'auto'
>> keyword was repurposed.
>
> Last I checked, C++ had no satisfactory way to express
> callbacks/functors/listeners/lambdas. That's why Qt came up with a
> metacompiler to supplement C++'s facilities.

I think one of the recent standards added some kind of closure for
callbacks. I tried to grok it and couldn't figure out what it was
doing, so I gave it up as a bad job. It got hairy. Really hairy.
Either that, or I was reading a poorly-written article, which is also
possible.

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


Re: Reference

2014-03-04 Thread Alexander Blinne
Am 04.03.2014 15:06, schrieb Chris Angelico:
> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py

I have always found it quite nice that the python parser is so easy to
use from within python itself.

> Run across the Python stdlib, that tells me there are 4040 uses of
> is/is not, of which 16 compare against False, 18 against True (see?
> Python has a bias for truth above falsehood!), and 3386 against None.

I think i will have to rephrase my "is False" condition to make it more
truthy :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proper conversion of timestamp

2014-03-04 Thread Igor Korot
Mark,


On Tue, Mar 4, 2014 at 1:45 PM, Mark Lawrence wrote:

> On 04/03/2014 21:38, MRAB wrote:
>
>> On 2014-03-04 20:57, Igor Korot wrote:
>>
>>> Hi, ALL,
>>> I'm getting this:
>>>
>>> timestamp out of range for platform localtime()/gmtime() function
>>>
>>> trying to convert the timestamp with milliseconds into the datetime
>>> object.
>>>
>>> The first hit of Google gives me this:
>>>
>>> http://stackoverflow.com/questions/12458595/convert-
>>> epoch-timestamp-in-python
>>>
>>>
>>> but the solution described is not good for me since it does not gives
>>> me the milliseconds value.
>>>
>>> How do I get the proper datetime value including milliseconds from the
>>> timestamp?
>>>
>>> Thank you.
>>>
>>>  Are you using Python 2? If yes, then try dividing by 1000.0.
>>
>>
> You learn something new every day, I wasn't aware that you could multiply
> or divide timestamps.


Of course you can. Its just the number.
And this is exactly what happens on the stackoverflow question I referenced
in the OP.
Problem is I want the milliseconds in the datetime object.

Thank you.


>
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proper conversion of timestamp

2014-03-04 Thread Igor Korot
Hi, Mark,


On Tue, Mar 4, 2014 at 1:44 PM, Mark Lawrence wrote:

> On 04/03/2014 20:57, Igor Korot wrote:
>
>> Hi, ALL,
>> I'm getting this:
>>
>> timestamp out of range for platform localtime()/gmtime() function
>>
>> trying to convert the timestamp with milliseconds into the datetime
>> object.
>>
>> The first hit of Google gives me this:
>>
>> http://stackoverflow.com/questions/12458595/convert-
>> epoch-timestamp-in-python
>>
>> but the solution described is not good for me since it does not gives
>> me the milliseconds value.
>>
>> How do I get the proper datetime value including milliseconds from the
>> timestamp?
>>
>> Thank you.
>>
>>
> You have a long record of asking timestamp related questions so you should
> know where the docs are that provide the answer to this question.  I'll
> leave you to go off and read them.  If you don't understand them, please
> cut and paste your code here, state what you expected to happen, what
> actually happened, including any traceback if applicable, and then we'll be
> happy to point you the error of your ways.
>

Working with the dates is not that easy and not just in Python.
There are too many different formatting involved with many different
representation.
And on top of it it is possible to use one system in completely different
environment.

But this particular question is easy.

What I have is a timestamp which reads: 1289410678L.

Trying to convert this into the datetime object in Python using:

import datetime
datetime.datetime.fromtimestamp( stamp )

produces the error: timestamp out of range for platform
localtime()/gmtime() function.

This is because this timestamp is not in seconds, but rather in
milliseconds.

Now the question I have is: how do I properly convert this timestamp into
the datetime object with the milliseconds?

Thank you.


>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Proper conversion of timestamp

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 8:46 AM, Igor Korot  wrote:
>> Are you using Python 2? If yes, then try dividing by 1000.0.
>
>
> Yes, I'm using python 2.7.
> But dividing by 1000 will give the precision in seconds, i.e. "-MM-DD
> HH:MM:SS".

Did you notice the bit at the end there? Try dividing by 1000.0, see
if that's any different.

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


Re: Reference

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 8:53 AM, Alexander Blinne  wrote:
> Am 04.03.2014 15:06, schrieb Chris Angelico:
>> https://github.com/Rosuav/ExceptExpr/blob/master/find_except_expr.py
>
> I have always found it quite nice that the python parser is so easy to
> use from within python itself.

Yes. Until I put together the original version of that, to search for
PEP 463 translation candidates, I'd never used the ast module other
than for literal_eval. It's marvelously powerful.

>> Run across the Python stdlib, that tells me there are 4040 uses of
>> is/is not, of which 16 compare against False, 18 against True (see?
>> Python has a bias for truth above falsehood!), and 3386 against None.
>
> I think i will have to rephrase my "is False" condition to make it more
> truthy :)

if x is False:

-->

if not x:

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


Re: Proper conversion of timestamp

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 8:55 AM, Igor Korot  wrote:
>
> This is because this timestamp is not in seconds, but rather in
> milliseconds.
>
> Now the question I have is: how do I properly convert this timestamp into
> the datetime object with the milliseconds?

Read elsewhere in the thread, two people have explained this already.

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


Re: Working with the set of real numbers

2014-03-04 Thread Oscar Benjamin
On 4 March 2014 21:18, Chris Angelico  wrote:
> On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin
>  wrote:
>> I don't quite follow your reasoning here. By "cut-and-try" do you mean
>> bisection? If so it gives the first N decimal digits in N*log2(10)
>> iterations. However each iteration requires a multiply and when the
>> number of digits N becomes large the multiplication is worse than
>> linear. So the result is something like N**2 log(N)log(log(N)),
>
> By "cut and try" I'm talking about the really REALLY simple algorithm
> for calculating square roots. It's basically brute force.
>
> epsilon = 0.0001
> def sqrt(n):
> guess1, guess2 = 1, n
> while abs(guess1-guess2) > epsilon:
> guess1 = n/guess2
> guess2 = (guess1 + guess2)/2
>return guess1

That's the exact same algorithm I showed! How on earth would you call
that brute force?

> It's generally going to take roughly O(n*n) time to generate n digits,
> give or take.

It does not take O(n*n) time. This is Newton iteration and for
well-behaved problems such as this it generates more than n digits
after n iterations. I modified my code to show the error (x**2 - y) at
each iteration:

$ python3.3 root.py
2
0.2
0.007
0.06
5E-12
3E-24
8E-49
8E-98
8E-196
9E-392
1E-783

The number of correct digits doubles at each iteration so after n
iterations you have 2**n digits (I misstated this as n**2 before).
This means that it takes log(N) iterations to get N digits. See here
for more:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method

See also the section below that:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Digit-by-digit_calculation

> That's the baseline against which anything else can be
> compared. There are plenty of better ways to calculate them.

Such as?


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


Re: Working with the set of real numbers

2014-03-04 Thread Oscar Benjamin
On 4 March 2014 21:05, Marko Rauhamaa  wrote:
> Oscar Benjamin :
>
>> To me the obvious method is Newton iteration which takes O(sqrt(N))
>> iterations to obtain N digits of precision. This brings the above
>> complexity below quadratic:
>>
>> #!/usr/bin/env python
>>
>> from decimal import Decimal as D, localcontext
>>
>> def sqrt(y, prec=1000):
>> '''Solve x**2 = y'''
>> assert y > 0
>> eps = D(10) ** -(prec + 5)
>> x = D(y)
>> with localcontext() as ctx:
>> ctx.prec = prec + 10
>> while x ** 2 - y > x * eps:
>> x = (x + y/x) / 2
>> return x
>>
>> print(sqrt(2))
>
> At a quick glance, I believe x ** 2 is O(N²) and so the total complexity
> should be O(N ** 2.5).

x**2 is just a multiplication which can be done in better than O(N**2):
http://en.wikipedia.org/wiki/Multiplication_algorithm#Fast_multiplication_algorithms_for_large_inputs


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


Re: Working with the set of real numbers

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 9:02 AM, Oscar Benjamin
 wrote:
> On 4 March 2014 21:18, Chris Angelico  wrote:
>> On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin
>>  wrote:
>>> I don't quite follow your reasoning here. By "cut-and-try" do you mean
>>> bisection? If so it gives the first N decimal digits in N*log2(10)
>>> iterations. However each iteration requires a multiply and when the
>>> number of digits N becomes large the multiplication is worse than
>>> linear. So the result is something like N**2 log(N)log(log(N)),
>>
>> By "cut and try" I'm talking about the really REALLY simple algorithm
>> for calculating square roots. It's basically brute force.
>>
>> epsilon = 0.0001
>> def sqrt(n):
>> guess1, guess2 = 1, n
>> while abs(guess1-guess2) > epsilon:
>> guess1 = n/guess2
>> guess2 = (guess1 + guess2)/2
>>return guess1
>
> That's the exact same algorithm I showed! How on earth would you call
> that brute force?

It uses a lot of division. There are various refinements that can be
done that replace some of that division with multiplication, but I'd
have to go do some research to figure it out.

This is the purest form of attempted-division algorithm. If you're
describing it on a blackboard, you would write it pretty much like
this. At each iteration, you have to divide by a number that's n
digits long, and then do some additional arithmetic.

>> It's generally going to take roughly O(n*n) time to generate n digits,
>> give or take.
>
> It does not take O(n*n) time. This is Newton iteration and for
> well-behaved problems such as this it generates more than n digits
> after n iterations. I modified my code to show the error (x**2 - y) at
> each iteration:
>
> $ python3.3 root.py
> 2
> 0.2
> 0.007
> 0.06
> 5E-12
> 3E-24
> 8E-49
> 8E-98
> 8E-196
> 9E-392
> 1E-783
>
> The number of correct digits doubles at each iteration so after n
> iterations you have 2**n digits (I misstated this as n**2 before).
> This means that it takes log(N) iterations to get N digits.

It seems I'm partly mistaken, though not entirely. Let's compare two
versions. In the first, you set the precision (I'm talking in terms of
REXX's "NUMERIC DIGITS" statement - anything beyond this many digits
will be rounded (and represented exponentially, if necessary); I'm not
sure if decimal.Decimal precision works this way) such that you get 10
digits. Each iteration requires division by a 10-digit number, which
is an operation that takes a certain amount of time; and it's going to
take some number of iterations to get to the final answer.

Second version, you set the precision so you get 20 digits. Now, it's
going to take you approximately one more iteration to get to the final
answer. (This bit I was mistaken on. I thought it would take something
like 25% more or 50% more iterations.) But each iteration will take
longer. The complexity of division depends on the algorithm - grade
school long division would be O(n) with a fixed-length dividend, I
think, but you could probably pull that down a bit.

So that's why I said it'd be very roughly O(n*n) - because the
division in each step is O(n), and I thought it'd take O(n) steps.
Turns out it's O(n*log(n)), which is a lot better.

>> That's the baseline against which anything else can be
>> compared. There are plenty of better ways to calculate them.
>
> Such as?

Improved versions of the above, and I was under the impression that
there were some completely different techniques that converged a lot
more quickly. But it may be that I was mistaken, as I'd been expecting
this to converge in O(n) steps. Reducing the amount of division would
speed things up significantly, although it probably won't change the
algorithmic complexity.

So, put it down to misremembering and thinking the simple algorithm
was worse than it actually is :)

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


Re: Proper conversion of timestamp

2014-03-04 Thread Ethan Furman

On 03/04/2014 01:55 PM, Igor Korot wrote:


Now the question I have is: how do I properly convert this timestamp
into the datetime object with the milliseconds?


And Mark's point is:  How do the docs say to do it?  What fails when you 
try it that way?


--
~Ethan~

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


Re: How security holes happen

2014-03-04 Thread Ethan Furman

On 03/04/2014 12:47 PM, Ned Batchelder wrote:

On 3/4/14 12:16 PM, Skip Montanaro wrote:

On Tue, Mar 4, 2014 at 11:07 AM, Chris Angelico  wrote:

I don't have time to watch an hour-long video... what'd he do,
exactly that?


If you fast forward to 16:14, his talk is about five minutes long. He
wrote a Lisp compiler whose backend is Python.

Skip



It's Hy: http://hylang.org


Okay, that looks totally cool.  Maybe I'll finally get a handle on LISP!  :)

--
~Ethan~

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


Re: find and replace string in binary file

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 12:18 AM, Peter Otten <__pete...@web.de> wrote:
> loial wrote:
>
>> How do I read a binary file, find/identify a character string and replace
>> it with another character string and write out to another file?
>>
>> Its the finding of the string in a binary file that I am not clear on.
>
> That's not possible. You have to convert either binary to string or string
> to binary before you can replace. Whatever you choose, you have to know the
> encoding of the file.

If it's actually a binary file (as in, an executable, or an image, or
something), then the *file* won't have an encoding, so you'll need to
know the encoding of the particular string you want and encode your
string to bytes.

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


Re: Proper conversion of timestamp

2014-03-04 Thread Mark Lawrence

On 04/03/2014 21:55, Igor Korot wrote:


But this particular question is easy.



If it's easy why can't you answer it via the docs rather than ask here?

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: How security holes happen

2014-03-04 Thread Marko Rauhamaa
Ethan Furman :

> Okay, that looks totally cool. Maybe I'll finally get a handle on
> LISP! :)

Lisp is conceptually simpler than Python, but awe-inspiring. One day, it
will overtake Python, I believe.

Once you have Lisp down pat, you'll be able to appreciate http://en.wikipedia.org/wiki/Combinatory_logic>.

The final Nirvana is reached with http://semarch.linguistics.fas.nyu.edu/barker/Iota/>.


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


Re: Working with the set of real numbers

2014-03-04 Thread Oscar Benjamin
On 4 March 2014 22:18, Chris Angelico  wrote:
> On Wed, Mar 5, 2014 at 9:02 AM, Oscar Benjamin
>  wrote:
>> On 4 March 2014 21:18, Chris Angelico  wrote:
>>> On Wed, Mar 5, 2014 at 7:55 AM, Oscar Benjamin
>>>  wrote:
>>>
>>> epsilon = 0.0001
>>> def sqrt(n):
>>> guess1, guess2 = 1, n
>>> while abs(guess1-guess2) > epsilon:
>>> guess1 = n/guess2
>>> guess2 = (guess1 + guess2)/2
>>>return guess1
>>
>> That's the exact same algorithm I showed! How on earth would you call
>> that brute force?
>
> It uses a lot of division. There are various refinements that can be
> done that replace some of that division with multiplication, but I'd
> have to go do some research to figure it out.

There's a description of such a method here:
http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots

I don't know whether that would work out faster (when using decimal -
for float it would probably be slower).

> Let's compare two
> versions. In the first, you set the precision (I'm talking in terms of
> REXX's "NUMERIC DIGITS" statement

I have no idea what that is.

>- anything beyond this many digits
> will be rounded (and represented exponentially, if necessary); I'm not
> sure if decimal.Decimal precision works this way) such that you get 10
> digits.

With the decimal module if you set the precision to 5 digits then it
basically represents the number in "standard form" with 5 digits .e.g:
1.2345 x 10**21.

> Each iteration requires division by a 10-digit number, which
> is an operation that takes a certain amount of time; and it's going to
> take some number of iterations to get to the final answer.
>
> Second version, you set the precision so you get 20 digits.

If we're talking about 10-20 digits then the decimal module is
overkill: just use float. The speed up from hardware arithmetic will
massively out-weigh any other performance considerations. My version
was intended to produce large numbers of digits which is when the
big-O comes in:

$ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 10)'
1 loops, best of 3: 22.4 usec per loop
$ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 100)'
1 loops, best of 3: 59.1 usec per loop
$ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 1000)'
1000 loops, best of 3: 1.15 msec per loop
$ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 1)'
10 loops, best of 3: 85.9 msec per loop
$ python3.3 -m timeit -s 'from root import sqrt' 'sqrt(2, 10)'
10 loops, best of 3: 1.59 sec per loop


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


Re: How security holes happen

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 9:48 AM, Marko Rauhamaa  wrote:
> Lisp is conceptually simpler than Python, but awe-inspiring. One day, it
> will overtake Python, I believe.
>
> The final Nirvana is reached with...

No no no. The final Nirvana is achieved when you no longer write text
at all, but simply edit an empty file. When you are done, the file is
still empty, and you have truly reached nirvana.

Either that, or you code in
http://en.wikipedia.org/wiki/Whitespace_(programming_language) ...

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


Re: How security holes happen

2014-03-04 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Wed, Mar 5, 2014 at 9:48 AM, Marko Rauhamaa  wrote:
> > Lisp is conceptually simpler than Python, but awe-inspiring. One day, it
> > will overtake Python, I believe.
> >
> > The final Nirvana is reached with...
> 
> No no no. The final Nirvana is achieved when you no longer write text
> at all, but simply edit an empty file. When you are done, the file is
> still empty, and you have truly reached nirvana.
> 
> Either that, or you code in
> http://en.wikipedia.org/wiki/Whitespace_(programming_language) ...
> 
> ChrisA

Man, imagine what you could do with a Unicode version of Whitespace?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Working with the set of real numbers

2014-03-04 Thread Chris Angelico
On Wed, Mar 5, 2014 at 9:54 AM, Oscar Benjamin
 wrote:
>> Let's compare two
>> versions. In the first, you set the precision (I'm talking in terms of
>> REXX's "NUMERIC DIGITS" statement
>
> I have no idea what that is.
>
>>- anything beyond this many digits
>> will be rounded (and represented exponentially, if necessary); I'm not
>> sure if decimal.Decimal precision works this way) such that you get 10
>> digits.
>
> With the decimal module if you set the precision to 5 digits then it
> basically represents the number in "standard form" with 5 digits .e.g:
> 1.2345 x 10**21.

That's how NUMERIC DIGITS works, so we're on the same page. I'm not
familiar enough with decimal.Decimal and how precision is configured,
but it seems to function the same way.

>> Each iteration requires division by a 10-digit number, which
>> is an operation that takes a certain amount of time; and it's going to
>> take some number of iterations to get to the final answer.
>>
>> Second version, you set the precision so you get 20 digits.
>
> If we're talking about 10-20 digits then the decimal module is
> overkill: just use float. The speed up from hardware arithmetic will
> massively out-weigh any other performance considerations.

Yeah, I'm just digging into the algorithm. The same concept applies
when going from 100 to 200 digits, or 1000 to 2000, and in each case,
the division will get way slower, but the number of iterations won't
go up as fast as I thought it would.

In theory, it should be possible to do the first few divisions at
lower precision, and scale up as you have need. In practice, would the
churning of precisions mean that you lose all the benefit?

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


Geezer learns python

2014-03-04 Thread notbob
I'm trying to learn python.  I'm doing it via Zed Shaw's Learn Python
the Hard Way.  Sure enough, 16 lessons in and I've run aground.  Is it
OK for a painfully stupid ol' fart to ask painfully stupid noob
questions, here?  I'm a long time usenet fan and prefer it to irc.

I've run Slackware for many yrs and know jes enough bash to get the
drift of a slack script, but am no programmer, not by any stretch of
the imagination.  I really wanna succeed on this one and have signed
up for an online python class from Rice U.  I'd like to finish Zed's
tutorial by the 24th of this month.  I'm retired, so have the time to
burn.  Thnx.  ;)

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


Re: Geezer learns python

2014-03-04 Thread Tobiah

On 03/04/2014 03:03 PM, notbob wrote:

I'm trying to learn python.  I'm doing it via Zed Shaw's Learn Python
the Hard Way.  Sure enough, 16 lessons in and I've run aground.  Is it
OK for a painfully stupid ol' fart to ask painfully stupid noob
questions, here?  I'm a long time usenet fan and prefer it to irc.

I've run Slackware for many yrs and know jes enough bash to get the
drift of a slack script, but am no programmer, not by any stretch of
the imagination.  I really wanna succeed on this one and have signed
up for an online python class from Rice U.  I'd like to finish Zed's
tutorial by the 24th of this month.  I'm retired, so have the time to
burn.  Thnx.  ;)

nb



All are welcome here.  Try to narrow down the focus of the
question, and where applicable, post source code and program
output.


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


  1   2   >