Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Monday, August 11, 2014 11:16:25 AM UTC+5:30, Chris Angelico wrote:
> On Mon, Aug 11, 2014 at 3:23 PM, Rustom Mody wrote:
> > A C programmer asked to swap variables x and y, typically writes something 
> > like
> > t = x; x = y; y = t;
> > Fine, since C cant do better.
> > But then he assumes that that much sequentialization is inherent to the 
> > problem...
> > Until he sees the python:
> > x,y = y,x
> > The same applies generally to all programmers brought up on imperative 
> > style.

> Uhh, that's still imperative. There is a chronological boundary here:
> prior to that statement's execution, x and y have certain values, and
> after it, they have the same values but the other way around. When
> you're manipulating algebraic statements, moving down the page doesn't
> correspond to any sort of time change, which is why "x = x + 1" has no
> solutions.

> Please explain to me how "x,y = y,x" is materially different from "x =
> x + 1" in that the latter is, in your words, an abomination, but you
> say the former doesn't have the same problem.

I was giving an analogy for a mindset that

- is sequencing hungup
- is not aware of being hungup

until a more abstract solution is presented.

The two -- x=x+1 and the swap -- are not related.
And yes the swap is imperative but less so than the C
because it elides the fact that an implementation of x,y = y,x
could be
- t=x;x=y;y=t   # the original
- t=y;y=x=x=t   # in the reverse order
- push x; push y; pop x; pop y  # use stack no temporary
- xchng x y # hardware instruction in x86

and perhaps many other plausible ones.

The terrible thing about an overspecific presentation is that it blinds one 
to alternatives

And as for your wanting a fully non-imperative version:

swap (x, y) = (y, x)

-- the only option in Haskell, natural/easy in python, painfully
laborious but still possible in C -- pass a struct, return a struct,
unpack on return.

You think that the '=' sign not standing for math equality is ok.

How come?

I aver that you (any competent programmer) have worked out/crystallized/created
a switch in your brain.  

In 'programming' state: x=x+1 is not a problem
In 'math' state : it is

And you have become adroit at flipping from one to the other -- you do it
all the time going from rhs to lhs!!

I too have that switch as do most practised programmers.

We are not talking about us, we are talking about noobs.

So my reverse question to you is how do you inculcate this switch in a noob?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the future of perfect Python?

2014-08-10 Thread Terry Reedy

On 8/11/2014 1:15 AM, 13813962782 wrote:


"Java is belonging to Oracle, C++ is defined by ISO; Python is designed
by great Guido van Rossum
;


Python is owned, as in copyright and trademark, by the Python Software 
Foundation, a US non-profit corporation, with a Board elected by the 
voting members.  Python is defined by the Reference and Library manuals. 
It was originally designed by Guido, who subsequently gave it to the 
PSF. At least 95% of current design work is done by other people under 
the framework Guido established, with his occasional input to make sure 
Python 'stays on track'.


--
Terry Jan Reedy

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


Minimal Python Build in Docker Container

2014-08-10 Thread sprin . dev
Hello,

I wanted to share what I've learned about making a reasonably minimal Docker 
image containing CPython built from source.

Motivation: Many popular distros are not able to provide packaged builds of the 
desired Python version. Operators of co-tenanted Python services have struggled 
since the dawn of time to manage the Matrix of Hell with Python apps - system 
Python version versus versions required by different applications versus shared 
lib versions, etc. We have made due with "almost good enough" tools like 
virtualenv, pyenv, and Software Collections. In spite of this, many operators 
just gave up and decided every Python service ought to live inside its own 
virtual machine. To me, it seems like we finally have a real solution:

Compile Python from source with the version and deps you need inside a Standard 
Container on top of the distro of your choosing.

Disclaimer: I have never been involved in the Python project or in the 
packaging of Python libs for any distro, so excuse me if this is naive.

If you, like me, have decided the key to sanity is in containerized Python 
services, you might try pulling the official Docker Python image:

python 2.7.7   a87a2288ce782 weeks ago  
   1.041 GB

Hmm, Python 2.7.8 has been out for over a month with "regression and security 
fixes over 2.7.7". Also, over 1 GB? The "debian:wheezy" image with Python 2.7.3 
installed from apt-get weighs in at 124 MB. And finally, this image is running 
on top of a pre-release version of the as-yet unreleased debian jessie.

So we have 3 very good reasons from staying away from the official Python 
Docker image.

Let's build our own. We chose Centos 7 as the standard base for both Docker 
hosts and guest containers. Here's what I did:

FROM centos:centos7

RUN yum install -y tar gcc make

RUN mkdir /usr/src/python
WORKDIR /usr/src/python
RUN curl -Sl "https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tar.xz"; \
| tar -xJ --strip-components=1
# You may want to verify the download with gpg: https://www.python.org/download

RUN ./configure \
&& make  -j$(nproc) \
&& make install \
&& make clean

# Clean up prior to flattening
RUN yum remove -y tar gcc make \
&& yum clean all \
&& rm -rf /usr/src/python

Beginning with the 244 MB Centos 7 base image, this yields a 369 MB image after 
flattening, with a compiled Python 2.7.8. While +125 MB to the base is not 
terrible, it seems like the image could still lose some weight.

Any ideas?

If you would like to check out the built image, it is at marina/python:2.7.8_r1 
on the public registry. Of course I recommend building your own with whatever 
makes sense for your "Python base" for production use!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 3:23 PM, Rustom Mody  wrote:
> A C programmer asked to swap variables x and y, typically writes something 
> like
>
> t = x; x = y; y = t;
>
> Fine, since C cant do better.
> But then he assumes that that much sequentialization is inherent to the 
> problem...
> Until he sees the python:
>
> x,y = y,x
>
> The same applies generally to all programmers brought up on imperative style.

Uhh, that's still imperative. There is a chronological boundary here:
prior to that statement's execution, x and y have certain values, and
after it, they have the same values but the other way around. When
you're manipulating algebraic statements, moving down the page doesn't
correspond to any sort of time change, which is why "x = x + 1" has no
solutions.

Please explain to me how "x,y = y,x" is materially different from "x =
x + 1" in that the latter is, in your words, an abomination, but you
say the former doesn't have the same problem.

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


Re: What's the future of perfect Python?

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 3:15 PM, 13813962782 <13813962...@139.com> wrote:
> Just like MANY people, I was one of guys who do very like Python. But
> there's one thing always puzzled me, could you kindly help give some words
> about it:
> "Java is belonging to Oracle, C++ is defined by ISO; Python is designed by
> great Guido van Rossum; then what is the future of Python( Guido van Rossum,
> GREAT man, but STILL just one person)? "

There are several things going on here.

Firstly, Python isn't developed solely by Guido, by any means. If you
poke around on python-dev and see who's talking, it's mainly not
Guido, but other people. And if you look at the commit history in
Mercurial, similar. There are lots of contributors. Guido's job is not
to *write* Python, but to *guide* it. If someone wants to add a new
operator to Python, s/he may well end up writing the code - but Guido
decides whether or not it's accepted. (Or, as the case may be,
delegates to someone else the authority and job of making that
decision.) Effectively, it's not so much that "nothing is added to
Python unless Guido does it" as it is "nothing can be added to Python
that Guido is specifically against".

Secondly, the state of a project that's commanded by a single person
is far better than one that's governed by a committee. Ever heard the
expression "designed by committee"? [1] It's not a compliment. PHP is
a language that seems to grow features without a huge amount of
guidance. It has not benefited from that status. Python, on the other
hand, has a transparent procedure for raising
potentially-controversial suggestions, which culminates in a BDFL
pronouncement. (Usually something like this: discussion on the
python-ideas mailing list, then someone writes a Python Enhancement
Proposal or PEP, which then gets bikeshedded to kingdom come and back,
and finally the PEP gets pronounced on - accepted or rejected.)

One person cannot, by definition, be in disagreement with himself.
(Conflicted, maybe, as Flynn Rider observed of his golden-haired
companion, but not in disagreement.) A committee can be; and then
what? Do you declare that no changes are made without 100% consensus?
Do you do whatever a majority of the committee wants? And how do you
decide who comprises this decision-making body? With organizations
(Oracle, ISO, Microsoft, and everyone else who owns a language),
there's either going to be ultimately one person in charge (in which
case it's not materially different from Python with the python-dev
community and Guido), or some means of resolving these kinds of
conflicts; and either way, chances are you won't know how it's dealt
with. I can surmise, for instance, that Microsoft will have someone
who has the ultimate say as to what goes into the next version of C#;
but I can't hazard a guess as to _who_ that might be. With the open
source world, there's a general principle that whoever starts a
project is its head, and that headship is transferred by explicit
action as the "final duty" [2], or can be claimed for an inactive
project by effectively forking it and becoming the head of a new
project (if the old one really is dead, its users will generally be
happy to follow you to the new project - especially if it has the same
name).

So there's a very good future. If something should happen to Guido,
the Python Steering Union will no doubt find someone else to take over
- which may result in a change of direction for the project, but I
suspect not a dramatic one. And in the meantime, we have a
well-defined hierarchy - the very best form of governance.

ChrisA

[1] https://en.wikipedia.org/wiki/Design_by_committee
[2] "When you lose interest in a program, your last duty to it is to
hand it off to a competent successor." -
https://en.wikipedia.org/wiki/The_Cathedral_and_the_Bazaar
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Monday, August 11, 2014 12:33:59 AM UTC+5:30, Chris Angelico wrote:
> On Mon, Aug 11, 2014 at 4:26 AM, Rustom Mody wrote:
> > Its when we have variables that are assigned in multiple places that
> > we start seeing mathematical abominations like
> > x = x+1

> That's an abomination to you because it breaks your mathematical
> model. It's fine to a computer, which has a sense of time.

It does?!?!
Completely missed the news flash

Last I knew they were as dumb as a rock -- maybe a GHz rock

A C programmer asked to swap variables x and y, typically writes something like

t = x; x = y; y = t;

Fine, since C cant do better.
But then he assumes that that much sequentialization is inherent to the 
problem...
Until he sees the python:

x,y = y,x

The same applies generally to all programmers brought up on imperative style.

Yeah there are problems that need to address time -- OSes, network
protocols, reactive systems like window managers etc

But the vast majority of problems that a programmer is likely to solve dont 
need time.

These are of the form: Given this situation, this is the desired outcome.

Nothing wrong with giving a sequential solution to a not inherently sequential 
problem.

What is wrong is then thinking that all *problems* are sequential rather than
seeing that some over-specific sequential *solutions* to non-sequential 
problems are ok.

A mindset exemplified by your hilarious statement: "computers have a sense of 
time"
-- 
https://mail.python.org/mailman/listinfo/python-list


What's the future of perfect Python?

2014-08-10 Thread 13813962782



Hi Sir/Madam,


 

Just like MANY people, I was one of guys who do very like Python. But there's 
one thing always puzzled me, could you kindly help give some words about it: 

"Java is belonging to Oracle, C++ is defined by ISO Python is designed by great 
Guido van Rossum then what is the future of Python( Guido van Rossum, GREAT 
man, but STILL just one person)? "

 

 

Thanks a lot,

Johnnny



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


Re: Template language for random string generation

2014-08-10 Thread Paul Wolf
On Sunday, 10 August 2014 17:31:01 UTC+1, Steven D'Aprano  wrote:
> Devin Jeanpierre wrote:
> 
> 
> 
> > On Fri, Aug 8, 2014 at 2:01 AM, Paul Wolf  wrote:
> 
> >> This is a proposal with a working implementation for a random string
> 
> >> generation template syntax for Python. `strgen` is a module for
> 
> >> generating random strings in Python using a regex-like template language.
> 
> >> Example:
> 
> >>
> 
> >> >>> from strgen import StringGenerator as SG
> 
> >> >>> SG("[\l\d]{8:15}&[\d]&[\p]").render()
> 
> >> u'F0vghTjKalf4^mGLk'
> 
> > 
> 
> > Why aren't you using regular expressions? I am all for conciseness,
> 
> > but using an existing format is so helpful...
> 
> 
> 
> You've just answered your own question:
> 
> 
> 
> > Unfortunately, the equivalent regexp probably looks like
> 
> > r'(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[a-zA-Z0-9]{8:15}'
> 
> 
> 
> Apart from being needlessly verbose, regex syntax is not appropriate because
> 
> it specifies too much, specifies too little, and specifies the wrong
> 
> things. It specifies too much: regexes like ^ and $ are meaningless in this
> 
> case. It specifies too little: there's no regex for the "shuffle operator".
> 
> And it specifies the wrong things: regexes like (?= ...) as used in your
> 
> example are for matching, not generating strings, and it isn't clear
> 
> what "match any character but don't consume any of the string" means when
> 
> generating strings.
> 
> 
> 
> Personally, I think even the OP's specified language is too complex. For
> 
> example, it supports literal text, but given the use-case (password
> 
> generators) do we really want to support templates like "password[\d]"? I
> 
> don't think so, and if somebody did, they can trivially say "password" +
> 
> SG('[\d]').render().
> 
> 
> 
> Larry Wall (the creator of Perl) has stated that one of the mistakes with
> 
> Perl's regular expression mini-language is that the Huffman coding is
> 
> wrong. Common things should be short, uncommon things can afford to be
> 
> longer. Since the most common thing for password generation is to specify
> 
> character classes, they should be short, e.g. d rather than [\d] (one
> 
> character versus four).
> 
> 
> 
> The template given could potentially be simplified to:
> 
> 
> 
> "(LD){8:15}&D&P"
> 
> 
> 
> where the round brackets () are purely used for grouping. Character codes
> 
> are specified by a single letter. (I use uppercase to avoid the problem
> 
> that l & 1 look very similar. YMMV.) The model here is custom format codes
> 
> from spreadsheets, which should be comfortable to anyone who is familiar
> 
> with Excel or OpenOffice. If you insist on having the facility to including
> 
> literal text in your templates, might I suggest:
> 
> 
> 
> "'password'd"  # Literal string "password", followed by a single digit.
> 
> 
> 
> but personally I believe that for the use-case given, that's a mistake.
> 
> 
> 
> Alternatively, date/time templates use two-character codes like %Y %m etc,
> 
> which is better than 
> 
> 
> 
> 
> 
> 
> 
> > (I've been working on this kind of thing with regexps, but it's still
> 
> > incomplete.)
> 
> > 
> 
> >> * Uses SystemRandom class (if available, or falls back to Random)
> 
> > 
> 
> > This sounds cryptographically weak. Isn't the normal thing to do to
> 
> > use a cryptographic hash function to generate a pseudorandom sequence?
> 
> 
> 
> I don't think that using a good, but not cryptographically-strong, random
> 
> number generator to generate passwords is a serious vulnerability. What's
> 
> your threat model? Attacks on passwords tend to be one of a very few:
> 
> 
> 
> - dictionary attacks (including tables of common passwords and 
> 
>   simple transformations of words, e.g. 'pas5w0d');
> 
> 
> 
> - brute force against short and weak passwords;
> 
> 
> 
> - attacking the hash function used to store passwords (not the password
> 
>   itself), e.g. rainbow tables;
> 
> 
> 
> - keyloggers or some other way of stealing the password (including
> 
>   phishing sites and the ever-popular "beat them with a lead pipe 
> 
>   until they give up the password");
> 
> 
> 
> - other social attacks, e.g. guessing that the person's password is their
> 
>   date of birth in reverse.
> 
> 
> 
> But unless the random number generator is *ridiculously* weak ("9, 9, 9, 9,
> 
> 9, 9, ...") I can't see any way to realistically attack the password
> 
> generator based on the weakness of the random number generator. Perhaps I'm
> 
> missing something?
> 
> 
> 
> 
> 
> > Someone should write a cryptographically secure pseudorandom number
> 
> > generator library for Python. :(
> 
> 
> 
> Here, let me google that for you :-)
> 
> 
> 
> https://duckduckgo.com/html/?q=python+crypto
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

I should clarify that the use case of password generation is only one of the 
use cases out of several that strgen is intended to support. It is also for: 

Test data generation: 

[\l]{1:20}&[._]{0:1

Re: Template language for random string generation

2014-08-10 Thread Paul Wolf
On Sunday, 10 August 2014 17:47:48 UTC+1, Ian  wrote:
> On Sun, Aug 10, 2014 at 10:34 AM, Paul Wolf  wrote:
> 
> > For instance, a template language that validates the output would have to 
> > do frequency analysis. But that is getting too far off the purpose of 
> > strgen, although such a mechanism would certainly have its place.
> 
> 
> 
> I don't think that would be necessary. The question being asked with
> 
> validation is "can this string be generated from this template", not
> 
> "is this string generated from this template with relatively high
> 
> probability".

Sorry, I meant frequency incidence within a produced string. And I understood 
Devin's point to be: For any given strgen expression that produces a set of 
strings, is there always a regex expression that captures the exact same set. 
And therefore is it not theoretically the case (leaving aside verbosity) that 
one of the syntaxes is superfluous (strgen). I think that is an entirely valid 
and interesting question. I'd have said before that it is not the case, but now 
I'm not so sure. I would still be sure that the strgen syntax is more fit for 
purpose for generating strings than regex on the basis of easy-of-use.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Monday, August 11, 2014 8:30:32 AM UTC+5:30, Steven D'Aprano wrote:

> You did the same thing in your own course, the only difference being you
> accepted a different set of primitive functions. But ultimately you have to
> introduce *some* amount of concreteness, at some level, otherwise we're
> just engaged in mental masturbation:

> Q: "Write a function to calculate the nth Catalan number."
> A: "Assume that a function Catalan(n) exists and calculates the nth Catalan
> number. Then the solution is Catalan."


You're concocting your own definitions and critiquing/refuting them.

Here is the Haskell definition [Ive modernized my '93 code].
It corresponds to the 4th interpretation from 
http://en.wikipedia.org/wiki/Catalan_number#Applications_in_combinatorics
-- applications of a bin op to a 'full' binary tree

All of 3 lines!

data Tree a = I (Tree a) (Tree a) | L a deriving Show
c [x]= [L x]
c ls = [I x y | i<-[1..length ls -1], let (l,r) = splitAt i ls, x <- c l, y <- 
c r]

Read 'L' as Leaf Node
 'I' as Internal Node
 'c' as the catalan enumerator

splitAt splits a list at a position:

*Main> splitAt 3 [3,4,5,6,7]
([3,4,5],[6,7])


And some example runs

*Main> c []
[]
*Main> c [1]
[L 1]
*Main> c [1,2]
[I (L 1) (L 2)]
*Main> c [1,2,3]
[I (L 1) (I (L 2) (L 3)),I (I (L 1) (L 2)) (L 3)]
*Main> c [1,2,3,4]
[I (L 1) (I (L 2) (I (L 3) (L 4))),I (L 1) (I (I (L 2) (L 3)) (L 4)),I (I (L 1) 
(L 2)) (I (L 3) (L 4)),I (I (L 1) (I (L 2) (L 3))) (L 4),I (I (I (L 1) (L 2)) 
(L 3)) (L 4)]
*Main> 

Opening up the last eg into separate lines:

[I (L 1) (I (L 2) (I (L 3) (L 4))),
 I (L 1) (I (I (L 2) (L 3)) (L 4)),
 I (I (L 1) (L 2)) (I (L 3) (L 4)),
 I (I (L 1) (I (L 2) (L 3))) (L 4),
 I (I (I (L 1) (L 2)) (L 3)) (L 4)]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Monday, August 11, 2014 3:31:08 AM UTC+5:30, Roy Smith wrote:

>  Mark Lawrence wrote:

> > On 10/08/2014 19:26, Rustom Mody wrote:
> > > Its when we have variables that are assigned in multiple places that
> > > we start seeing mathematical abominations like
> > > x = x+1
> > I'm not bothered about it being a mathematical or any other type of 
> > abomination.  It works, practically beats purity, so if it ain't broke, 
> > please don't fix it, for some definition of fix.

> I'm with Mark.  This isn't math, it's programming.  Sure, the 
> intersection of the two is non-null, but they are different things.  
> I'll often do things like:

> for line in input:
>line = line.strip()
># do more stuff

So would I

> Sure, I could invent some other variable name to avoid re-using the same 
> name, but does:

> for line in input:
>stripped_line = line.strip()
># do more stuff

> really make this any easier to read or understand?  I think not.

You are switching between software-engineer and teacher hat.

As a software engineer you (and I and possibly most reasonable people)
would do things like that.

As a teacher when you introduce problematic concepts you have to answer
questions -- possibly embarrassing -- about them.

Just as parents need to talk of 'birds-and-bees' to their children
sometime but also need good sense and judgement as to when, likewise
teachers should deliver maturity-building easier stuff before
maturity-needing harder ideas.

Assignment is definitely in the latter category because we have to
talk of time, environments and so on.  When one explicates (and not
handwaves) these pre-requisites -- as happens in denotational
semantics or in any language implementation -- then assignment is seen
to be as higher-order a concept as any other 2 level lambda expression.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Steven D'Aprano
Rustom Mody wrote:

> On Sunday, August 10, 2014 10:40:21 PM UTC+5:30, Roy Smith wrote:
>>  Rustom Mody  wrote:
> 
>> > > They haven't figured out yet that the
>> > > first step to solving a problem is to decide what algorithms you're
>> > > going to use, and only then can you start translating that into code.
>> > > They need to be led in small steps towards basic knowledge.
>> > [...]
>> > In my view this is starting from the wrong end.
>> > I do not need to know which kind of adder the hardware is implementing
>> > to use +, which sorting algorithm to use sort, etc.
> 
>> Well, no, but if the problem is, "Find the 5 largest numbers in a list",
>> you might start solving the problem by thinking, "OK, first I'm going to
>> sort the list into descending order, then I'm going to take the first
>> five items from that"(*)  Only then would you get down to "OK, how do I
>> sort a list of numbers in this language", and "OK, how do I select the
>> first five items from a list in this language".  That's what I mean by
>> first you come up with an algorithm, then you implement it in code.
> 
 l= [6,2,9,12,1,4]
 sorted(l,reverse=True)[:5]
> [12, 9, 6, 4, 2]
> 
> No need to know how sorted works nor [:5]
> 
> Now you (or Steven) can call it abstract.

It *is* an abstraction. You are abstracting away the details of the real
computer implementation into an abstraction of a pure mathematical
function, and that's fine. As programmers, that's what we do. But
abstractions leak, and someday someone is going to ask "Why does it take 45
minutes to find the five largest values of my list?", and if you persist in
insisting that sorted() is a pure mathematical function you will have no
clue on how to even begin solving this, except perhaps to deny that it
matters.

But because abstractions leak, and I know that the concrete implementation
is more fundamental than the abstraction, I can answer the question:
perhaps their system uses bubblesort instead of Timsort. Or perhaps their
list has a hundred thousand billion items. Or, if we're talking about
Python, perhaps the __lt__ method of the items is horribly inefficient. To
those who insist that the abstraction is all that matters, these concrete
factors are irrelevant, but in practice they can be critical.

The real question is, as educators, should we teach the abstraction or a
concrete implementation first? In actuality we do both. As a first year
computer science student learning Pascal, I was taught to add numbers using
+ without caring about the details of the hardware adder, although I was
taught to care about the abstractions "Integer" and "Real" leaking (i.e.
the possibility of overflow). On the other hand, I was taught to write my
own sort function, partly because Pascal doesn't have one, but mostly
because *learning how to program* was the whole point of the class and the
people teaching the course decided that sort algorithms was the right level
of complexity for their intended audience.

You did the same thing in your own course, the only difference being you
accepted a different set of primitive functions. But ultimately you have to
introduce *some* amount of concreteness, at some level, otherwise we're
just engaged in mental masturbation:

Q: "Write a function to calculate the nth Catalan number."
A: "Assume that a function Catalan(n) exists and calculates the nth Catalan
number. Then the solution is Catalan."


> And yet its
> 1. Actual running code in the interpreter
> 2. Its as close as one can get to a literal translation of your
>"Find the 5 largest numbers in a list"
> 
> Yeah the reverse=True is a fly in the ointment.

Why? It's just syntax. In the abstract, there is no difference between
sorted_descending(alist) and sorted(alist, reverse=True), they're just
different ways of spelling the same thing.



-- 
Steven

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


Re: Template language for random string generation

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 12:22 PM, Steven D'Aprano
 wrote:
> You mean the opposite to OpenSSL, which was handed down to Mankind from
> the Gods?

I thought Prometheus stole OpenSSL and gave it to mankind so a group
of Minotaurs would stop teasing him about Heartbleed.

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


Re: Template language for random string generation

2014-08-10 Thread Steven D'Aprano
Devin Jeanpierre wrote:

> On Sun, Aug 10, 2014 at 9:31 AM, Steven D'Aprano
>  wrote:

>> I don't think that using a good, but not cryptographically-strong, random
>> number generator to generate passwords is a serious vulnerability. What's
>> your threat model?
> 
> I've always wanted a password generator that worked on the fly based
> off of a master password. If the passwords are generated randomly but
> not cryptographically securely so, then given sufficiently many
> passwords, the master password might be deduced.

o_O

So, what you're saying is that you're concerned that if an attacker has all
your passwords, they might be able to generate new passwords?


[...]
>>> Someone should write a cryptographically secure pseudorandom number
>>> generator library for Python. :(
>>
>> Here, let me google that for you
> 
> I should clarify that OpenSSL has one (which is what I assume you're
> alluding to), 

No. If you follow the link I provided, I'm sure you will find what you are
after.


> but it doesn't let you choose the seed, so it's useless 
> for deterministic password generation. There are also lots of small
> libraries some person wrote at some time, but that sounds shady. ;)

You mean the opposite to OpenSSL, which was handed down to Mankind from the
Gods? The size of the library doesn't matter, what matters is how well it
implements what crypto standards.




-- 
Steven

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


Re: how to get the ordinal number in list

2014-08-10 Thread Steven D'Aprano
Rustom Mody wrote:

> Its when we have variables that are assigned in multiple places that
> we start seeing mathematical abominations like
> x = x+1

That's not a mathematical abomination. It's a perfectly reasonable
mathematical equation, one with no solutions since the line f(x) = x and
the line f(x) = x+1 are parallel.

But what does this have to do with programming? Programming *is not*
mathematics, and x = x+1 has a different meaning in programming than in
mathematics. Perhaps it would help if we wrote it using mathematical
notation? Using [x] for subscripts:

x[n+1] = x[n] + 1

we have a perfectly good mathematical recursive definition. All it needs is
an initial value x[0] and we're good to go.



-- 
Steven

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


Re: how to get the ordinal number in list

2014-08-10 Thread ismeal shanshi

Herion, Ketamine,Actavis promethazine codeine 16oz and 32oz available , 
Oxycontine Hydrocodone xanax and medicated

marijuana US- free shipping and other related products for sell at competitive 
prices.We do world wide shipping to any clear

address.Delivery is 100% safe due to our discreetness and experience.Try our 
quality and experience then have a story to tell

another day.

Email Address:stuffstorehouse2014 (AT) gmail.com
CONTACT NUMBER: (407)-4852249 * Pleas text me only 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why i can't copy mail with imaplib?

2014-08-10 Thread Ben Finney
luofeiyu  writes:

> >>> x.con.copy(b"1","[Gmail]/Important")
> ('NO', [b'[TRYCREATE] No folder [Gmail]/Important (Failure)'])

Your questions have mostly been unrelated to Python, and this is another
example. You should search elsewhere for assistance with IMAP and GMail.

-- 
 \“The restriction of knowledge to an elite group destroys the |
  `\   spirit of society and leads to its intellectual |
_o__)impoverishment.” —Albert Einstein |
Ben Finney

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


Re: why i can't copy mail with imaplib?

2014-08-10 Thread luofeiyu

>>> x.con.list()
('OK', [b'(\\HasNoChildren) "/" "INBOX"', b'(\\Noselect \\HasChildren) 
"/" "[Gma
il]"', b'(\\HasNoChildren \\Junk) "/" "[Gmail]/&V4NXPpCuTvY-"', 
b'(\\HasNoChildr
en \\Trash) "/" "[Gmail]/&XfJSIJZkkK5O9g-"', b'(\\HasNoChildren 
\\Flagged) "/" "
[Gmail]/&XfJSoGYfaAc-"', b'(\\HasNoChildren \\Sent) "/" 
"[Gmail]/&XfJT0ZCuTvY-"'
, b'(\\HasNoChildren \\All) "/" "[Gmail]/&YkBnCZCuTvY-"', 
b'(\\HasNoChildren \\D
rafts) "/" "[Gmail]/&g0l6Pw-"', b'(\\HasNoChildren \\Important) "/" 
"[Gmail]/&kc

2JgQ-"'])
>>> x.con.select("inbox")
('OK', [b'40'])
>>> x.con.copy(b"1","[Gmail]/&kc2JgQ-]")
('NO', [b'[TRYCREATE] No folder [Gmail]/\xe9\x87\x8d\xe8\xa6\x81] 
(Failure)'])

>>> x.con.copy(b"1","[Gmail]/Important")
('NO', [b'[TRYCREATE] No folder [Gmail]/Important (Failure)'])
On 8/10/2014 4:07 PM, John Gordon wrote:

In  luofeiyu 
 writes:


self.con.select("inbox")
self.con.copy(b"1","[Gmail]/&kc2JgQ-]")
why i can not copy the first email into my  important mailbox?

What happens when you run the above code?  Do you get an error?



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


Re: why i can't copy mail with imaplib?

2014-08-10 Thread John Gordon
In  luofeiyu 
 writes:

> self.con.select("inbox")
> self.con.copy(b"1","[Gmail]/&kc2JgQ-]")

> why i can not copy the first email into my  important mailbox?

What happens when you run the above code?  Do you get an error?

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re: how to get the ordinal number in list

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 8:01 AM, Roy Smith  wrote:
> I'm with Mark.  This isn't math, it's programming.  Sure, the
> intersection of the two is non-null, but they are different things.
> I'll often do things like:
>
> for line in input:
>line = line.strip()
># do more stuff

What does mathematics even have as a concept of looping? String
manipulation? Input and output?

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


Re: Idle crashes when using accentuated letters in Mac OS X

2014-08-10 Thread Ned Deily
In article ,
 Christian Gollwitzer  wrote:
> Am 10.08.14 15:03, schrieb Anamaria Martins Moreira
> > I am facing a problem with using accentuated characters in idle (2.7.6
> > or 2.7.8). When I type the accent, idle crashes. If I call python from a
> > terminal, there is no such problem.
> 
> Try updating your Tcl/Tk to the latest version, e.g. via ActiveTcl. I 
> remember such a bug in the OSX version, which was fixed a long time ago. 
> There is a number of bugs in the OSX Tk, a lot of them was recently 
> fixed, so stay tuned and maybe update when 8.6.2 becomes available.

You also need to be using a Python instance that will use the newer 
ActiveTcl.  The crash is due to a bug in older versions of OS X Cocoa 
Tk; unfortunately, the versions of Tk 8.5 shipped by Apple with OS X 
from OS X 10.6 through at least current 10.9.x still have this bug.  One 
way to avoid it is to use either one of the current python.org 
installers (Python 2.7.8 or 3.4.1) and the current ActiveTcl 8.5.15.0 
from ActiveState (using 8.6.x will not help).  There is more information 
here:

https://www.python.org/download/mac/tcltk/

-- 
 Ned Deily,
 n...@acm.org

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


Re: how to get the ordinal number in list

2014-08-10 Thread Roy Smith
In article ,
 Mark Lawrence  wrote:

> On 10/08/2014 19:26, Rustom Mody wrote:
> >
> > Its when we have variables that are assigned in multiple places that
> > we start seeing mathematical abominations like
> > x = x+1
> >
> 
> I'm not bothered about it being a mathematical or any other type of 
> abomination.  It works, practically beats purity, so if it ain't broke, 
> please don't fix it, for some definition of fix.

I'm with Mark.  This isn't math, it's programming.  Sure, the 
intersection of the two is non-null, but they are different things.  
I'll often do things like:

for line in input:
   line = line.strip()
   # do more stuff

Sure, I could invent some other variable name to avoid re-using the same 
name, but does:

for line in input:
   stripped_line = line.strip()
   # do more stuff

really make this any easier to read or understand?  I think not.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Idle crashes when using accentuated letters in Mac OS X

2014-08-10 Thread Christian Gollwitzer

Am 10.08.14 15:03, schrieb Anamaria Martins Moreira

I am facing a problem with using accentuated characters in idle (2.7.6
or 2.7.8). When I type the accent, idle crashes. If I call python from a
terminal, there is no such problem.


Try updating your Tcl/Tk to the latest version, e.g. via ActiveTcl. I 
remember such a bug in the OSX version, which was fixed a long time ago. 
There is a number of bugs in the OSX Tk, a lot of them was recently 
fixed, so stay tuned and maybe update when 8.6.2 becomes available.


Christian

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


Re: how to get the ordinal number in list

2014-08-10 Thread Mark Lawrence

On 10/08/2014 19:26, Rustom Mody wrote:


Its when we have variables that are assigned in multiple places that
we start seeing mathematical abominations like
x = x+1



I'm not bothered about it being a mathematical or any other type of 
abomination.  It works, practically beats purity, so if it ain't broke, 
please don't fix it, for some definition of fix.


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

Mark Lawrence

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


Re: Template language for random string generation

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 2:31 AM, Steven D'Aprano
 wrote:
> Personally, I think even the OP's specified language is too complex. For
> example, it supports literal text, but given the use-case (password
> generators) do we really want to support templates like "password[\d]"? I
> don't think so, and if somebody did, they can trivially say "password" +
> SG('[\d]').render().

What if you're using this to generate IDs for something (think Youtube
video references), and you want to have an alphabetic portion and a
numeric portion separated by a hyphen? I think there is a use-case for
interior literal text, because otherwise you'd have to either split
the result or do two calls to the generator.

> Here, let me google that for you :-)
>
> https://duckduckgo.com/html/?q=python+crypto

Hehe. :)

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


Re: how to get the ordinal number in list

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 5:14 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> In computing, assignment and reassignment aren't at all problematic,
>> and neither is printing to the console, so please stop telling people
>> off for using them.
>
> Printing to the console is somewhat problematic:
>
>>>> with open("/dev/console", "w") as console: console.write("hello\n")
>...
>Traceback (most recent call last):
>  File "", line 1, in 
>IOError: [Errno 13] Permission denied: '/dev/console'

Har har :) I meant printing to the single most obvious destination,
which is a combination of stdout and stderr (the latter for error
tracebacks, by default).

What he's saying is basically "print is EVIL, so do everything with
the REPL, and pretend that the P in REPL doesn't stand for print".
Apparently the idea that code should work from a .py file is foreign
to him.

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


Re: how to get the ordinal number in list

2014-08-10 Thread Marko Rauhamaa
Chris Angelico :

> In computing, assignment and reassignment aren't at all problematic,
> and neither is printing to the console, so please stop telling people
> off for using them.

Printing to the console is somewhat problematic:

   >>> with open("/dev/console", "w") as console: console.write("hello\n")
   ... 
   Traceback (most recent call last):
 File "", line 1, in 
   IOError: [Errno 13] Permission denied: '/dev/console'


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


Re: how to get the ordinal number in list

2014-08-10 Thread Chris Angelico
On Mon, Aug 11, 2014 at 4:26 AM, Rustom Mody  wrote:
>
> Its when we have variables that are assigned in multiple places that
> we start seeing mathematical abominations like
> x = x+1

That's an abomination to you because it breaks your mathematical
model. It's fine to a computer, which has a sense of time. And it even
can be strictly true. Consider:

>>> x = 2.0**53
>>> x = x + 1
>>> x == x + 1
True

x is clearly not infinity, yet it's equal to itself plus one. If
you're going to try to pretend that maths and computers are exactly
the same, you have a LOT of problems to deal with.

In computing, assignment and reassignment aren't at all problematic,
and neither is printing to the console, so please stop telling people
off for using them.

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


Re: Template language for random string generation

2014-08-10 Thread Devin Jeanpierre
On Sun, Aug 10, 2014 at 9:31 AM, Steven D'Aprano
 wrote:
>> (I've been working on this kind of thing with regexps, but it's still
>> incomplete.)
>>
>>> * Uses SystemRandom class (if available, or falls back to Random)
>>
>> This sounds cryptographically weak. Isn't the normal thing to do to
>> use a cryptographic hash function to generate a pseudorandom sequence?
>
> I don't think that using a good, but not cryptographically-strong, random
> number generator to generate passwords is a serious vulnerability. What's
> your threat model?

I've always wanted a password generator that worked on the fly based
off of a master password. If the passwords are generated randomly but
not cryptographically securely so, then given sufficiently many
passwords, the master password might be deduced. CSPRNGs guarantee
otherwise.

>> Someone should write a cryptographically secure pseudorandom number
>> generator library for Python. :(
>
> Here, let me google that for you

I should clarify that OpenSSL has one (which is what I assume you're
alluding to), but it doesn't let you choose the seed, so it's useless
for deterministic password generation. There are also lots of small
libraries some person wrote at some time, but that sounds shady. ;)

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


Re: Template language for random string generation

2014-08-10 Thread Devin Jeanpierre
On Sun, Aug 10, 2014 at 9:34 AM, Paul Wolf  wrote:
> * No one will want to write that expression

We've already established that one to be wrong. ;)

> * The regex expression doesn't work anyway

That's a cheap swipe. The regexp doesn't work because I used a colon
instead of a comma, because I accidentally copied you. :(

Speaking of which, is there a reason you've diverged from regex syntax
in x{8: 15} vs x{8,15}?


Don't mind my suggestion to use existing formats even when it's
inconvenient. It's a knee jerk reaction/question, not a serious
complaint.

> I should also clarify that when I say the strgen template language is the 
> converse of regular expressions, this is the case conceptually, not formally. 
> Matching text strings is fundamentally different from producing randomized 
> strings.

Mmmm, I wouldn't be so quick to dismiss any insights from regexps
here. It depends on your fundamentals. For example, automata-theoretic
approaches do apply, and can let you guarantee that equivalent
templates always generate the same outputs given the same inputs.
(Meaning that the only thing that matters is what the template
matches, not how it's spelled.)

> Whether using SystemRandom is cryptographically weak is not something I'm 
> taking up here. Someone already suggested allowing the class to accept a 
> different random source provider. That's an excellent idea. I wanted to make 
> sure strgen does whatever they would do anyway hand-coding using the Python 
> Standard Library except vastly more flexible, easier to edit and shorter. 
> strgen is two things: a proposed standard way of expressing a string 
> generation specification that relies heavily on randomness and a wrapper 
> around the standard library. I specifically did not want to try to write 
> better cryptographic routines.

The fallback is what worries me. Falling back from a secure thing to
an insecure thing doesn't sound good.

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


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Sunday, August 10, 2014 11:44:23 PM UTC+5:30, Roy Smith wrote:
>  Rustom Mody wrote:

> > >>> l= [6,2,9,12,1,4]
> > >>> sorted(l,reverse=True)[:5]
> > [12, 9, 6, 4, 2]
> > No need to know how sorted works nor [:5]
> > Now you (or Steven) can call it abstract.
> > And yet its 
> > 1. Actual running code in the interpreter
> > 2. Its as close as one can get to a literal translation of your
> >"Find the 5 largest numbers in a list"
> > [...]
> > All the above are clearer than loops+assignments and can be 
> > taught before them

> I disagree.  For a beginner, you want to be able to break things down 
> into individual steps and examine the result at each point.  If you do:

> > >>> l= [6,2,9,12,1,4]
> > >>> l2 = sorted(l,reverse=True)
> > >>> l2[:5]

> you have the advantage that you can stop after creating l2 and print it 
> out.  The student can see that it has indeed been sorted.  With the 
> chained operations, you have to build a mental image of an anonymous, 
> temporary list, and then perform the slicing operation on that.  Sure, 
> it's the way you or I would write it in production code, but for a 
> beginner, breaking it down into smaller pieces makes it easier to 
> understand.


Yeah Whats the disagreement??

You are writing straight-line code.
I am recommending straight-line code -- another way of saying loops are bad.

Or is it because you are using assignment?

I call that 'nominal' assignment.
Technically its called 'single-assignment'
www.cs.princeton.edu/~appel/papers/ssafun.ps

Its when we have variables that are assigned in multiple places that
we start seeing mathematical abominations like
x = x+1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Roy Smith
In article <154cc342-7f85-4d16-b636-a1a953913...@googlegroups.com>,
 Rustom Mody  wrote:

> >>> l= [6,2,9,12,1,4]
> >>> sorted(l,reverse=True)[:5]
> [12, 9, 6, 4, 2]
> 
> No need to know how sorted works nor [:5]
> 
> Now you (or Steven) can call it abstract.
> 
> And yet its 
> 1. Actual running code in the interpreter
> 2. Its as close as one can get to a literal translation of your
>"Find the 5 largest numbers in a list"
> [...]
> All the above are clearer than loops+assignments and can be 
> taught before them

I disagree.  For a beginner, you want to be able to break things down 
into individual steps and examine the result at each point.  If you do:

> >>> l= [6,2,9,12,1,4]
> >>> l2 = sorted(l,reverse=True)
> >>> l2[:5]

you have the advantage that you can stop after creating l2 and print it 
out.  The student can see that it has indeed been sorted.  With the 
chained operations, you have to build a mental image of an anonymous, 
temporary list, and then perform the slicing operation on that.  Sure, 
it's the way you or I would write it in production code, but for a 
beginner, breaking it down into smaller pieces makes it easier to 
understand.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
Pressed Send to early.

On Sunday, August 10, 2014 11:15:03 PM UTC+5:30, Rustom Mody wrote:
> >>> # Works the same (SEEMINGLY)

> ... # Now change the return to an yield
> ... 
> >>> def search(x,y):
> ...for id ,value in enumerate(x):
> ...if y==value : yield id
> ... 
> >>> search(["x1","x2","x3", "x2", "x5", "x2"], "x2")
> >>> # Hmm wazzat?!
> ... list(search(["x1","x2","x3", "x2", "x5", "x2"], "x2"))
> [1, 3, 5]

Now you can of course use that to print if you choose

>>> for x in search(["x1","x2","x3"], "x2"):
...   print x
1


But you can also put some other possibly more complex and useful action there
in place of the print if you choose.

With the print version that's not an option.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Saturday, August 9, 2014 7:53:22 AM UTC+5:30, luofeiyu wrote:
> >>> x=["x1","x3","x7","x5"]
>  >>> y="x3"

>   how can i get the ordinal number by some codes?

> for id ,value in enumerate(x):
>  if y==value : print(id)

> Is more simple way to do that?

I feel a bit discourteous going on a tangent and ignoring the OP...

To the OP:

The problem with your code is not that its not simple
but that it uses a print statement

To expunge the print statement you must pay a small price:
Wrap it in a function:

def search(x,y):
  for id ,value in enumerate(x):
  if y==value : print(id)

>>> search(["x1","x2","x3"], "x2")
1

>>> # Change print to return
>>> def search(x,y):
...for id ,value in enumerate(x):
...if y==value : return id
... 
>>> search(["x1","x2","x3"], "x2")
1
>>> # Works the same (SEEMINGLY)

... # Now change the return to an yield
... 
>>> def search(x,y):
...for id ,value in enumerate(x):
...if y==value : yield id
... 
>>> search(["x1","x2","x3", "x2", "x5", "x2"], "x2")

>>> # Hmm wazzat?!
... list(search(["x1","x2","x3", "x2", "x5", "x2"], "x2"))
[1, 3, 5]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Sunday, August 10, 2014 10:40:21 PM UTC+5:30, Roy Smith wrote:
>  Rustom Mody  wrote:

> > > They haven't figured out yet that the 
> > > first step to solving a problem is to decide what algorithms you're 
> > > going to use, and only then can you start translating that into code.  
> > > They need to be led in small steps towards basic knowledge.
> > [...]
> > In my view this is starting from the wrong end.
> > I do not need to know which kind of adder the hardware is implementing to
> > use +, which sorting algorithm to use sort, etc.

> Well, no, but if the problem is, "Find the 5 largest numbers in a list", 
> you might start solving the problem by thinking, "OK, first I'm going to 
> sort the list into descending order, then I'm going to take the first 
> five items from that"(*)  Only then would you get down to "OK, how do I 
> sort a list of numbers in this language", and "OK, how do I select the 
> first five items from a list in this language".  That's what I mean by 
> first you come up with an algorithm, then you implement it in code.

>>> l= [6,2,9,12,1,4]
>>> sorted(l,reverse=True)[:5]
[12, 9, 6, 4, 2]

No need to know how sorted works nor [:5]

Now you (or Steven) can call it abstract.

And yet its 
1. Actual running code in the interpreter
2. Its as close as one can get to a literal translation of your
   "Find the 5 largest numbers in a list"

Yeah the reverse=True is a fly in the ointment.

I can do

>>> sorted(l)[-5:]
[2, 4, 6, 9, 12]

But that's a bit arcane

In any case coming back to the point

All the above are clearer than loops+assignments and can be 
taught before them
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the ordinal number in list

2014-08-10 Thread Steven D'Aprano
Rustom Mody wrote:

> On Saturday, August 9, 2014 9:04:22 PM UTC+5:30, Roy Smith wrote:
[...]
>> They haven't figured out yet that the
>> first step to solving a problem is to decide what algorithms you're
>> going to use, and only then can you start translating that into code.
>> They need to be led in small steps towards basic knowledge.
> 
> makes perfect sense... under one assumption: viz.
> The 'first steps' to becoming a programmer are necessarily imperative
> steps. So much so that programming is usually *defined* as imperative
> programming. This usually goes thus:

"Necessarily"? Well, that's possibly a bit strong, but I think it is
reasonable to say that the first steps to becoming a programmer are
*preferably* imperative, because the real world is imperative. If you want
to make a coffee, you:

- get a cup
- put instant coffee in the cup
- put sugar in the cup
- boil water
- pour boiling water into the cup
- stir
- add milk

not:

- call an abstract make_coffee() function.


> - CS is defined as the science of algorithms (or algorithmics)

Yes, but algorithms can be either concrete ("put the kettle on the stove and
light the gas"), intermediate ("bring the water to the boil at 100°C"), or
fully abstract ("f(Temperature(water)) = lambda: 100°C", or something like
that). So CS can and does envelop both imperative and functional
programming. They just don't typically teach functional programming to
beginners.



> - Programming (in the layman sense: python, java, C etc) is just about
> converting these 'abstract algorithms' into executable code
> - And an algorithm?   An algorithm is an effective method expressed as a
> finite list[1] of well-defined instructions[2] for calculating a function.
> quoting http://en.wikipedia.org/wiki/Algorithm
> 
> In my view this is starting from the wrong end.
> I do not need to know which kind of adder the hardware is implementing to
> use +, which sorting algorithm to use sort, etc.

Nobody says that you do. You're attacking a strawman.


> IOW the 'calculating a function' is primary and the 'effective steps'
> is secondary and traditional programming pedagogy gets this order wrong.

"Calculating a function" is *incredibly* abstract, and quite hard to
consider. If you think it isn't, then you're probably focused on a subset
of problems that involve transformations which are trivially modelled by
mathematical functions.


> What do I need to know to use sort? Nothing? Not so. I should be able to
> distinguish a sorted list from an unsorted one.  This is primary.
> Getting from one to the other -- the how -- is secondary.

And how do you distinguish sorted list from an unsorted list, using
functional styles? You're not allowed to just propose a magic "is_sorted()"
function, any more than you're allowed to propose a magic "do whatever I
want" function. (The whole point of CS is to learn how to program, not to
just assume the program you want already exists.) Nor is it sufficient to
define it this way:

def is_sorted(alist):
return alist == sorted(alist)

since that assumes that sorted is correct. Consider this definition of
sorted:

def sorted(alist):
return alist

Now, the naturally intuitive way to determine whether something is sorted is
imperative: you start at one end of the list and loop over pairs of items.
If every item is >= to the previous item, the list is sorted. Now, of
course we both know that we can turn that into a functional approach using
reduce(), but if you can find one person in a hundred -- hell, even one
person in a thousand -- who will intuitively come up with that approach in
preference to the imperative style without being coached, I'll buy you a
coffee or soft drink of your choice.

Even Carl Gauss started off reasoning imperatively, and he was a natural
mathematics genius.

Learning programming is hard enough without trying to do it floating up in
the stratosphere breathing vacuum. Abstractions are *hard*, and the
functional approach is an abstraction.


[...]
> As a more demonstrative example of the above (abstract) talk, in the 90s
> I used to teach a first course in programming using a predecessor of
> haskell (gofer -- gofer stands for GOod For Equational Reasoning).

Aimed at what age and experience of student? There's a big difference
between aiming a course at mathematics post-grads, or at 12 year olds who
are still a bit fuzzy about the whole "what's a function?" thing.

What percentage of students passed?

> By the end of the course students could
> - handle data structures like generic n-ary trees, graphs etc
> - write toy interpreters, compilers, semantic functions
> - combinatorial enumerators corresponding to various types
> of combinatorial functions like ⁿCr ⁿPr Catalan numbers
> 
> All WITHOUT a single assignment statement
> [very easy to accomplish since the language has no assignment statement
> [:-) ]
> 
> and more important (and germane to this thread) NO PRINT statement.

Hmmm. With no print, how could they te

Re: how to get the ordinal number in list

2014-08-10 Thread Roy Smith
In article ,
 Rustom Mody  wrote:

> > They haven't figured out yet that the 
> > first step to solving a problem is to decide what algorithms you're 
> > going to use, and only then can you start translating that into code.  
> > They need to be led in small steps towards basic knowledge.
> [...]
> In my view this is starting from the wrong end.
> I do not need to know which kind of adder the hardware is implementing to
> use +, which sorting algorithm to use sort, etc.

Well, no, but if the problem is, "Find the 5 largest numbers in a list", 
you might start solving the problem by thinking, "OK, first I'm going to 
sort the list into descending order, then I'm going to take the first 
five items from that"(*)  Only then would you get down to "OK, how do I 
sort a list of numbers in this language", and "OK, how do I select the 
first five items from a list in this language".  That's what I mean by 
first you come up with an algorithm, then you implement it in code.

(*) Yes, I know, that's not the optimum way, but it's a reasonable first 
attempt.
-- 
https://mail.python.org/mailman/listinfo/python-list


why i can't copy mail with imaplib?

2014-08-10 Thread luofeiyu

Help on method copy in module imaplib:

copy(message_set, new_mailbox) method of imaplib.IMAP4_SSL instance
Copy 'message_set' messages onto end of 'new_mailbox'.

(typ, [data]) = .copy(message_set, new_mailbox)


self is an instance in my code."[Gmail]/&kc2JgQ-]" is the important mailbox.


self.con.select("inbox")
self.con.copy(b"1","[Gmail]/&kc2JgQ-]")

why i can not copy the first email into my  important mailbox?
--
https://mail.python.org/mailman/listinfo/python-list


Idle crashes when using accentuated letters in Mac OS X

2014-08-10 Thread Anamaria Martins Moreira
Hi!

I am facing a problem with using accentuated characters in idle (2.7.6 or
2.7.8). When I type the accent, idle crashes. If I call python from a
terminal, there is no such problem.

I looked around and saw that there are a number of issues with
utf-8-python-mac, but I could not find the solution. The one indicated on
the Python doc

"You need to create a file ~ /.MacOSX/environment.plist. See Apple’s
Technical Document QA1067 for details."

seems not to be recommended  by Apple anymore.

Does any mac users have an idea for me, please?

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


Re: Template language for random string generation

2014-08-10 Thread Ian Kelly
On Sun, Aug 10, 2014 at 10:34 AM, Paul Wolf  wrote:
> For instance, a template language that validates the output would have to do 
> frequency analysis. But that is getting too far off the purpose of strgen, 
> although such a mechanism would certainly have its place.

I don't think that would be necessary. The question being asked with
validation is "can this string be generated from this template", not
"is this string generated from this template with relatively high
probability".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Template language for random string generation

2014-08-10 Thread Ian Kelly
On Aug 10, 2014 6:45 AM, "Devin Jeanpierre"  wrote:
> > * Uses SystemRandom class (if available, or falls back to Random)
>
> This sounds cryptographically weak. Isn't the normal thing to do to
> use a cryptographic hash function to generate a pseudorandom sequence?

You mean in the fallback case, right?  I'm no crypto expert, but I've never
heard of SystemRandom being contra-recommended for crypto, and even the
Python docs recommend it.

The output of even a cryptographically strong hash isn't going to have any
more entropy than the input, so if the input is predictable then the output
will be also.  One approach I'm aware of, which is used by Django, is to
hash the RNG state along with the time and a local secret In order to
reseed the RNG unpredictably whenever randomness is required. That creates
a configuration burden in order to establish the secret, though.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Template language for random string generation

2014-08-10 Thread Paul Wolf
On Sunday, 10 August 2014 13:43:04 UTC+1, Devin Jeanpierre  wrote:
> On Fri, Aug 8, 2014 at 2:01 AM, Paul Wolf  wrote:
> 
> > This is a proposal with a working implementation for a random string 
> > generation template syntax for Python. `strgen` is a module for generating 
> > random strings in Python using a regex-like template language. Example:
> 
> >
> 
> > >>> from strgen import StringGenerator as SG
> 
> > >>> SG("[\l\d]{8:15}&[\d]&[\p]").render()
> 
> > u'F0vghTjKalf4^mGLk'
> 
> 
> 
> Why aren't you using regular expressions? I am all for conciseness,
> 
> but using an existing format is so helpful...
> 
> 
> 
> Unfortunately, the equivalent regexp probably looks like
> 
> r'(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[a-zA-Z0-9]{8:15}'
> 
> 
> 
> (I've been working on this kind of thing with regexps, but it's still
> 
> incomplete.)
> 
> 
> 
> > * Uses SystemRandom class (if available, or falls back to Random)
> 
> 
> 
> This sounds cryptographically weak. Isn't the normal thing to do to
> 
> use a cryptographic hash function to generate a pseudorandom sequence?
> 
> 
> 
> Someone should write a cryptographically secure pseudorandom number
> 
> generator library for Python. :(
> 
> 
> 
> (I think OpenSSL comes with one, but then you can't choose the seed.)
> 
> 
> 
> -- Devin

> Why aren't you using regular expressions?

I guess you answered your own question with your example: 

* No one will want to write that expression
* The regex expression doesn't work anyway
* The purpose of regex is just too different from the purpose of strgen

The purpose of strgen is to make life easier for developers and provide 
benefits that get pushed downstream (to users of the software that gets 
produced with it). Adopting a syntax similar to regex is only necessary or 
useful to the extent it achieves that. 

I should also clarify that when I say the strgen template language is the 
converse of regular expressions, this is the case conceptually, not formally. 
Matching text strings is fundamentally different from producing randomized 
strings. For instance, a template language that validates the output would have 
to do frequency analysis. But that is getting too far off the purpose of 
strgen, although such a mechanism would certainly have its place. 

> This sounds cryptographically weak.

Whether using SystemRandom is cryptographically weak is not something I'm 
taking up here. Someone already suggested allowing the class to accept a 
different random source provider. That's an excellent idea. I wanted to make 
sure strgen does whatever they would do anyway hand-coding using the Python 
Standard Library except vastly more flexible, easier to edit and shorter. 
strgen is two things: a proposed standard way of expressing a string generation 
specification that relies heavily on randomness and a wrapper around the 
standard library. I specifically did not want to try to write better 
cryptographic routines. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Template language for random string generation

2014-08-10 Thread Steven D'Aprano
Devin Jeanpierre wrote:

> On Fri, Aug 8, 2014 at 2:01 AM, Paul Wolf  wrote:
>> This is a proposal with a working implementation for a random string
>> generation template syntax for Python. `strgen` is a module for
>> generating random strings in Python using a regex-like template language.
>> Example:
>>
>> >>> from strgen import StringGenerator as SG
>> >>> SG("[\l\d]{8:15}&[\d]&[\p]").render()
>> u'F0vghTjKalf4^mGLk'
> 
> Why aren't you using regular expressions? I am all for conciseness,
> but using an existing format is so helpful...

You've just answered your own question:

> Unfortunately, the equivalent regexp probably looks like
> r'(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[a-zA-Z0-9]{8:15}'

Apart from being needlessly verbose, regex syntax is not appropriate because
it specifies too much, specifies too little, and specifies the wrong
things. It specifies too much: regexes like ^ and $ are meaningless in this
case. It specifies too little: there's no regex for the "shuffle operator".
And it specifies the wrong things: regexes like (?= ...) as used in your
example are for matching, not generating strings, and it isn't clear
what "match any character but don't consume any of the string" means when
generating strings.

Personally, I think even the OP's specified language is too complex. For
example, it supports literal text, but given the use-case (password
generators) do we really want to support templates like "password[\d]"? I
don't think so, and if somebody did, they can trivially say "password" +
SG('[\d]').render().

Larry Wall (the creator of Perl) has stated that one of the mistakes with
Perl's regular expression mini-language is that the Huffman coding is
wrong. Common things should be short, uncommon things can afford to be
longer. Since the most common thing for password generation is to specify
character classes, they should be short, e.g. d rather than [\d] (one
character versus four).

The template given could potentially be simplified to:

"(LD){8:15}&D&P"

where the round brackets () are purely used for grouping. Character codes
are specified by a single letter. (I use uppercase to avoid the problem
that l & 1 look very similar. YMMV.) The model here is custom format codes
from spreadsheets, which should be comfortable to anyone who is familiar
with Excel or OpenOffice. If you insist on having the facility to including
literal text in your templates, might I suggest:

"'password'd"  # Literal string "password", followed by a single digit.

but personally I believe that for the use-case given, that's a mistake.

Alternatively, date/time templates use two-character codes like %Y %m etc,
which is better than 



> (I've been working on this kind of thing with regexps, but it's still
> incomplete.)
> 
>> * Uses SystemRandom class (if available, or falls back to Random)
> 
> This sounds cryptographically weak. Isn't the normal thing to do to
> use a cryptographic hash function to generate a pseudorandom sequence?

I don't think that using a good, but not cryptographically-strong, random
number generator to generate passwords is a serious vulnerability. What's
your threat model? Attacks on passwords tend to be one of a very few:

- dictionary attacks (including tables of common passwords and 
  simple transformations of words, e.g. 'pas5w0d');

- brute force against short and weak passwords;

- attacking the hash function used to store passwords (not the password
  itself), e.g. rainbow tables;

- keyloggers or some other way of stealing the password (including
  phishing sites and the ever-popular "beat them with a lead pipe 
  until they give up the password");

- other social attacks, e.g. guessing that the person's password is their
  date of birth in reverse.

But unless the random number generator is *ridiculously* weak ("9, 9, 9, 9,
9, 9, ...") I can't see any way to realistically attack the password
generator based on the weakness of the random number generator. Perhaps I'm
missing something?


> Someone should write a cryptographically secure pseudorandom number
> generator library for Python. :(

Here, let me google that for you :-)

https://duckduckgo.com/html/?q=python+crypto



-- 
Steven

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


Re: how to get the ordinal number in list

2014-08-10 Thread Rustom Mody
On Saturday, August 9, 2014 9:04:22 PM UTC+5:30, Roy Smith wrote:
>  Rustom Mody  wrote:

> > [To the OP]
> > Yeah I am in the minority at least out here in considering
> > comprehensions simpler than loops. Take your pick

> When comprehensions first came out, I stubbornly refused to get my head 
> around them.  Now, I'm totally addicted.  To the extent that I consider 
> dict comprehensions to the THE killer feature of 2.7 :-)

Yeah I remember seeing a stunning example from Peter Otten a few months(?)
ago... first I heard of them. If someone can track that example down...
I'll be mighty pleased.

> But, putting on my instructor's hat, I think it's important to answer 
> questions at a level that can be understood by the student.  Painting 
> with a broad brush, there's three or four kinds of people asking 
> questions on this list:

> 1) People who are totally new to programming, and are learning Python as 
> their first language.  These are the people who are still struggling to 
> understand fundamental concepts.  They haven't figured out yet that the 
> first step to solving a problem is to decide what algorithms you're 
> going to use, and only then can you start translating that into code.  
> They need to be led in small steps towards basic knowledge.

> 2) People who are (at least somewhat) experienced programmers, and are 
> learning Python as a second language.  Their experiential background is 
> limited to one way of doing things (i.e. the Java way, or the PHP way, 
> or whatever language way they learned first).  They mostly should be 
> shown how translate the things they already know into familiar feeling 
> constructs.  You already know how to write a loop, this is how we do it 
> in Python.  You already know how build a data structure that maps keys 
> to values, this is how we do it in Python.  Only after they've become 
> comfortable with that, should they start exploring the really cool 
> features of Python.

> 3) People who already know many languages, and are learning Python as 
> their n-th.  These folks have seen multiple ways of doing things, and 
> can understand concepts at a higher level.  Oh, Python dicts are more 
> like C++ STL maps than PHP arrays.  Oh, variables have function scope 
> and don't have to be explicitly declared.  Oh, RAII is spelled "with" in 
> this language.  Oh, functions are first-class objects, but code blocks 
> are not.

> 4) People who are already proficient Python programmers and are looking 
> to explore deeper topics.

> I think suggesting comprehensions in an answer should be reserved for 
> people at levels 3 and 4.  Maybe level 2-1/2.  Certainly not level 1.

All this... (and in particular)

> They haven't figured out yet that the 
> first step to solving a problem is to decide what algorithms you're 
> going to use, and only then can you start translating that into code.  
> They need to be led in small steps towards basic knowledge.

makes perfect sense... under one assumption: viz.
The 'first steps' to becoming a programmer are necessarily imperative
steps. So much so that programming is usually *defined* as imperative
programming. This usually goes thus:

- CS is defined as the science of algorithms (or algorithmics)
- Programming (in the layman sense: python, java, C etc) is just about
converting these 'abstract algorithms' into executable code
- And an algorithm?   An algorithm is an effective method expressed as a finite 
list[1] of well-defined instructions[2] for calculating a function.
quoting http://en.wikipedia.org/wiki/Algorithm

In my view this is starting from the wrong end.
I do not need to know which kind of adder the hardware is implementing to
use +, which sorting algorithm to use sort, etc.

IOW the 'calculating a function' is primary and the 'effective steps'
is secondary and traditional programming pedagogy gets this order wrong.

What do I need to know to use sort? Nothing? Not so. I should be able to
distinguish a sorted list from an unsorted one.  This is primary.
Getting from one to the other -- the how -- is secondary.

[This also BTW implies that in the python world the 'sorted' function is
more fundamental than the sort (mutating) method. And their names are
pedagogically the wrong way round.  Of course this is for historical reasons.

But for now let us wear instructor not historian hats shall we?]

As a more demonstrative example of the above (abstract) talk, in the 90s
I used to teach a first course in programming using a predecessor of haskell
(gofer -- gofer stands for GOod For Equational Reasoning).

By the end of the course students could

- handle data structures like generic n-ary trees, graphs etc
- write toy interpreters, compilers, semantic functions
- combinatorial enumerators corresponding to various types
of combinatorial functions like ⁿCr ⁿPr Catalan numbers 

All WITHOUT a single assignment statement
[very easy to accomplish since the language has no assignment statement :-) ]

and more important (

Re: get the min date from a list

2014-08-10 Thread Roy Smith
In article ,
 Dave Angel  wrote:

> Your simplest answer is probably to write a function that converts
>  a string like you have into a datetime object, say call it
>  converter (). Then after testing it, you call
> 
> min (dates, key = converter)

Wow, after all these years, I didn't know min() took a key argument.  Of 
course, it makes sense, but I just never noticed that before.  Thanks!

And for the OP, for the converter function, I would suggest 
dateutil.parse.parser(), from the python-dateutil module 
(https://labix.org/python-dateutil).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Template language for random string generation

2014-08-10 Thread Devin Jeanpierre
On Fri, Aug 8, 2014 at 2:01 AM, Paul Wolf  wrote:
> This is a proposal with a working implementation for a random string 
> generation template syntax for Python. `strgen` is a module for generating 
> random strings in Python using a regex-like template language. Example:
>
> >>> from strgen import StringGenerator as SG
> >>> SG("[\l\d]{8:15}&[\d]&[\p]").render()
> u'F0vghTjKalf4^mGLk'

Why aren't you using regular expressions? I am all for conciseness,
but using an existing format is so helpful...

Unfortunately, the equivalent regexp probably looks like
r'(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[a-zA-Z0-9]{8:15}'

(I've been working on this kind of thing with regexps, but it's still
incomplete.)

> * Uses SystemRandom class (if available, or falls back to Random)

This sounds cryptographically weak. Isn't the normal thing to do to
use a cryptographic hash function to generate a pseudorandom sequence?

Someone should write a cryptographically secure pseudorandom number
generator library for Python. :(

(I think OpenSSL comes with one, but then you can't choose the seed.)

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


Re:get the min date from a list

2014-08-10 Thread Dave Angel
luofeiyu  Wrote in message:
>  >>> date
> ['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400', 
> 'Sat, 9 Au
> g 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug 
> . 
> 2014 03:4
> 4:56 +0200', 'Sun, 10 Aug 2014 01:55:24 + (UTC)', 'Sun, 10 Aug 2014 
> 02:01:06
>   + (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014 
> 22:51:29
>   -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014 
> 01:55:24 +000
> 0 (UTC)']
>  >>> min(date)
> 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)'
> 
> The result is wrong,the min date should be 'Tue, 5 Aug 2014 01:55:24 +000
> 0 (UTC)' ,how can i get it ?
> 

You neglected to specify your Python version,  but I'll assume at
 least 2.5.

The min function did exactly as it's documented to do, found the
 minimum string, by alphabetical ordering. Since 'F' is less than
 'T' it didn't need to look at the rest.  If you had been sorting
 a list of datetime objects, it would have found the least of
 those. 

Your simplest answer is probably to write a function that converts
 a string like you have into a datetime object, say call it
 converter (). Then after testing it, you call

min (dates, key = converter)

Note I did NOT use parens on converter.

I also used the name dates for the list,  since it's a collection
 of dates. But that assumes you rename it in your code that
 gathered them.

-- 
DaveA

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


Re: get the min date from a list

2014-08-10 Thread Mark Lawrence

On 10/08/2014 08:14, luofeiyu wrote:

 >>> date
['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400',
'Sat, 9 Au
g 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug
2014 02:51
:01 + (UTC)', 'Sat, 9 Aug 2014 13:03:24 +1000', 'Sat, 09 Aug 2014
13:06:28 +
1000', 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)', 'Fri, 8 Aug 2014 23:52:09
-0700 (
PDT)', 'Sat, 09 Aug 2014 09:15:50 +0200', 'Sat, 9 Aug 2014 01:49:54
-0600', 'Sat
, 9 Aug 2014 01:57:18 -0600', 'Sat, 9 Aug 2014 17:54:23 +0800 (CST)',
'Sat, 9 Au
g 2014 12:49:08 +0200', 'Sat, 9 Aug 2014 07:31:09 -0400', 'Sat, 9 Aug
2014 07:34
:16 -0400', 'Sat, 09 Aug 2014 11:39:16 +', 'Sat, 9 Aug 2014 07:40:41
-0400',
  'Sat, 9 Aug 2014 11:46:54 +', 'Sat, 09 Aug 2014 13:48:17 +0200',
'Sat, 09 A
ug 2014 21:53:11 +1000', 'Sat, 09 Aug 2014 14:13:13 +0200', 'Sat, 09 Aug
2014 08
:16:08 -0400', 'Sat, 09 Aug 2014 22:17:25 +1000', 'Sat, 09 Aug 2014
14:33:54 +02
00', 'Sat, 9 Aug 2014 14:46:01 +0200', 'Sat, 09 Aug 2014 10:34:58
-0300', 'Sat,
09 Aug 2014 11:34:22 -0400', 'Sat, 09 Aug 2014 12:16:56 -0400', 'Sat, 09
Aug 201
4 12:26:38 -0400', 'Sat, 09 Aug 2014 13:29:59 -0400', 'Sat, 09 Aug 2014
13:43:33
  -0400', 'Sat, 9 Aug 2014 11:30:35 -0300', 'Sat, 09 Aug 2014 20:14:20
+0200', 'S
un, 10 Aug 2014 08:18:34 +1000', 'Sat, 9 Aug 2014 18:23:08 -0400 (EDT)',
'Sat, 0
9 Aug 2014 18:30:17 -0400', 'Sat, 09 Aug 2014 18:31:38 -0400', 'Sun, 10
Aug 2014
  09:43:44 +1000', 'Sat, 9 Aug 2014 18:27:15 -0700 (PDT)', 'Sun, 10 Aug
2014 03:4
4:56 +0200', 'Sun, 10 Aug 2014 01:55:24 + (UTC)', 'Sun, 10 Aug 2014
02:01:06
  + (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014
22:51:29
  -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014
01:55:24 +000
0 (UTC)']
 >>> min(date)
'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)'

The result is wrong,the min date should be 'Tue, 5 Aug 2014 01:55:24 +000
0 (UTC)' ,how can i get it ?


The result is probably correct (I haven't checked) as you're comparing 
strings.  I'll leave you to read the docs to find out how to convert the 
strings to datetimes and compare them.  Start here 
https://docs.python.org/3/ as that way you'll get used to finding your 
way around them.


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

Mark Lawrence

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


get the min date from a list

2014-08-10 Thread luofeiyu

>>> date
['Sat, 09 Aug 2014 07:36:46 -0700', 'Fri, 8 Aug 2014 22:25:40 -0400', 
'Sat, 9 Au
g 2014 12:46:43 +1000', 'Sat, 9 Aug 2014 12:50:52 +1000', 'Sat, 9 Aug 
2014 02:51
:01 + (UTC)', 'Sat, 9 Aug 2014 13:03:24 +1000', 'Sat, 09 Aug 2014 
13:06:28 +
1000', 'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)', 'Fri, 8 Aug 2014 23:52:09 
-0700 (
PDT)', 'Sat, 09 Aug 2014 09:15:50 +0200', 'Sat, 9 Aug 2014 01:49:54 
-0600', 'Sat
, 9 Aug 2014 01:57:18 -0600', 'Sat, 9 Aug 2014 17:54:23 +0800 (CST)', 
'Sat, 9 Au
g 2014 12:49:08 +0200', 'Sat, 9 Aug 2014 07:31:09 -0400', 'Sat, 9 Aug 
2014 07:34
:16 -0400', 'Sat, 09 Aug 2014 11:39:16 +', 'Sat, 9 Aug 2014 07:40:41 
-0400',
 'Sat, 9 Aug 2014 11:46:54 +', 'Sat, 09 Aug 2014 13:48:17 +0200', 
'Sat, 09 A
ug 2014 21:53:11 +1000', 'Sat, 09 Aug 2014 14:13:13 +0200', 'Sat, 09 Aug 
2014 08
:16:08 -0400', 'Sat, 09 Aug 2014 22:17:25 +1000', 'Sat, 09 Aug 2014 
14:33:54 +02
00', 'Sat, 9 Aug 2014 14:46:01 +0200', 'Sat, 09 Aug 2014 10:34:58 
-0300', 'Sat,
09 Aug 2014 11:34:22 -0400', 'Sat, 09 Aug 2014 12:16:56 -0400', 'Sat, 09 
Aug 201
4 12:26:38 -0400', 'Sat, 09 Aug 2014 13:29:59 -0400', 'Sat, 09 Aug 2014 
13:43:33
 -0400', 'Sat, 9 Aug 2014 11:30:35 -0300', 'Sat, 09 Aug 2014 20:14:20 
+0200', 'S
un, 10 Aug 2014 08:18:34 +1000', 'Sat, 9 Aug 2014 18:23:08 -0400 (EDT)', 
'Sat, 0
9 Aug 2014 18:30:17 -0400', 'Sat, 09 Aug 2014 18:31:38 -0400', 'Sun, 10 
Aug 2014
 09:43:44 +1000', 'Sat, 9 Aug 2014 18:27:15 -0700 (PDT)', 'Sun, 10 Aug 
2014 03:4
4:56 +0200', 'Sun, 10 Aug 2014 01:55:24 + (UTC)', 'Sun, 10 Aug 2014 
02:01:06
 + (UTC)', 'Sat, 9 Aug 2014 19:41:08 -0700 (PDT)', 'Sat, 9 Aug 2014 
22:51:29
 -0400 (EDT)', 'Sun, 10 Aug 2014 07:34:44 +0200', 'Tue, 5 Aug 2014 
01:55:24 +000

0 (UTC)']
>>> min(date)
'Fri, 8 Aug 2014 20:48:44 -0700 (PDT)'

The result is wrong,the min date should be 'Tue, 5 Aug 2014 01:55:24 +000
0 (UTC)' ,how can i get it ?
--
https://mail.python.org/mailman/listinfo/python-list


Re: how to get the subject of email?

2014-08-10 Thread Mark Lawrence

On 10/08/2014 02:41, luofeiyu wrote:

I am in python3.4

typ, data = x.con.fetch(b'1', '(RFC822)')   #get  the first email
text = data[0][1]
message = email.message_from_string(text).get('subject')

Traceback (most recent call last):
   File "", line 1, in 
   File "D:\Python34\lib\email\__init__.py", line 40, in
message_from_string
 return Parser(*args, **kws).parsestr(s)
   File "D:\Python34\lib\email\parser.py", line 70, in parsestr
 return self.parse(StringIO(text), headersonly=headersonly)
TypeError: initial_value must be str or None, not bytes

message = email.message_from_string(str(text)).get('subject')
message  # nothing displayed


Could the answer to your numerous questions be in the docs, in this case 
https://docs.python.org/3/library/email.html ?


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

Mark Lawrence

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


Re: Fwd: How to draw a map using python

2014-08-10 Thread Mark Lawrence

On 10/08/2014 02:44, Yuanchao Xu wrote:

To kind whom it may concern:

I want to draw a map using python, not really a map with full
information, just a get together of a series of small shapes to reflect
land use.

The data is like below


|1  2  2  3  3  2
2  3  3  1  1  2
1  1  1  1  3  3
3  3  3  3  4  1|

Each number represents one land use type. and their positions in the
matrix are their coordinates.

I used VBA to do that before, the whole map consists many small square
shapes representing land use, but since the data was so large, it took a
long time to generate the map, also delete the map.

My question are :

1. I wonder in python, is there any more fast way to generate this kind
of map, as a whole, not a series of shapes, i think that would be faster??

2. I have tried using contourf, as below, but it says "out of bounds for
axis 1", but actually, I printed X,Y and cordi, they have the same
shape, why still out of bounds?

 1.


|y=  np.arange(0,  4  ,  1)
x=  np.arange(0,  6  ,  1)
X,Y=  np.meshgrid(x,y)

# cordi is the matrix containing all the data
# pyplot is imported before

plt.contourf(X,Y,  Cordi[X,Y],  8,  alpha=.75,  cmap='jet')|

3. Some kind person has suggested me to use imshow to plot. I checked
the explanation of imshow, it deals more about images not plots, and it
needs a 3D array to plot, in which for each pixel it needs 3 values to
show the color. I also tried, not so acceptable. The interfaces of each
color are so vague, and besides, when the data is large, it just failed
to present. So, if I use imshow, could I have some way to avoid those
two problems?


Thank you very much for answering!



See http://matplotlib.org/ specifically 
http://matplotlib.org/basemap/users/examples.html


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

Mark Lawrence

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


Re: how to write file into my android phone?

2014-08-10 Thread Marko Rauhamaa
Christian Gollwitzer :

> Am 10.08.14 11:39, schrieb Steven D'Aprano:
>> Android phones don't mount as storage devices?
>> Oh well, that's Android crossed off my list.
>
> Not any longer. They used to, but the support for mass storage was
> dropped in favour of MTP

I don't see anything inherently wrong with an open protocol like MTP.

> to allow concurrent access from both the computer and the phone.

I don't know MTP at all, but

   MTP allows no parallelism; unlike USB mass storage, MTP has been
   built to only allow a single operation at a time (for example, read,
   write or delete operation), while no other operation can be executed
   until the previous operation is complete.
   http://en.wikipedia.org/wiki/Media_Transfer_Protocol#Drawbacks>


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


Re: how to write file into my android phone?

2014-08-10 Thread Christian Gollwitzer

Am 10.08.14 11:39, schrieb Steven D'Aprano:

Android phones don't mount as storage devices?

Oh well, that's Android crossed off my list.



Not any longer. They used to, but the support for mass storage was 
dropped in favour of MTP to allow concurrent access from both the 
computer and the phone. It is still possible to activate the mass 
storage driver in current Android using custom firmware.


Christian


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


Re: how to write file into my android phone?

2014-08-10 Thread Steven D'Aprano
Chris “Kwpolska” Warrick wrote:

> On Sun, Aug 10, 2014 at 12:23 AM, Dave Angel  wrote:
>> 1) it's not necessarily j:   And not necessarily a single drive.
> 
> The OP claims it is a J: drive they want to write to.
> 
>> 2) the phone isn't necessarily visible on a pc as a drive at all.
>>  For example the Samsung gs4.
> 
> This is actually true for ALL android devices, starting with Android 3.0.

o_O

Android phones don't mount as storage devices?

Oh well, that's Android crossed off my list.



-- 
Steven

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


Re: How to draw a map using python

2014-08-10 Thread Ian Kelly
On Sat, Aug 9, 2014 at 7:44 PM, Yuanchao Xu  wrote:
> 1. I wonder in python, is there any more fast way to generate this kind of
> map, as a whole, not a series of shapes, i think that would be faster??

You mean like collecting all the shapes into a single sparse array and
passing the single array to contourf? Why would that be faster?

> 2. I have tried using contourf, as below, but it says "out of bounds for
> axis 1", but actually, I printed X,Y and cordi, they have the same shape,
> why still out of bounds?

I don't know. If you're going to ask about an error, it would be
helpful if you would include the full stack trace.

> 3. Some kind person has suggested me to use imshow to plot. I checked the
> explanation of imshow, it deals more about images not plots, and it needs a
> 3D array to plot, in which for each pixel it needs 3 values to show the
> color. I also tried, not so acceptable.

According to the documentation, it will also accept an MxN array of
greyscale or colormapped data.

> The interfaces of each color are so
> vague, and besides, when the data is large, it just failed to present. So,
> if I use imshow, could I have some way to avoid those two problems?

Failed how? As posed, this question is too vague to answer.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to write file into my android phone?

2014-08-10 Thread Chris “Kwpolska” Warrick
On Sun, Aug 10, 2014 at 12:23 AM, Dave Angel  wrote:
> 1) it's not necessarily j:   And not necessarily a single drive.

The OP claims it is a J: drive they want to write to.

> 2) the phone isn't necessarily visible on a pc as a drive at all.
>  For example the Samsung gs4.

This is actually true for ALL android devices, starting with Android 3.0.

It’s possible that the OP is running an ancient device, or a modern
one that appears as a USB drive for some reason (eg. custom ROM from
the manufacturer).

-- 
Chris “Kwpolska” Warrick 
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The "right" way to use config files

2014-08-10 Thread Fabien

On 10.08.2014 00:30, Terry Reedy wrote:

The advantage of TDD is that it forces one to make code testable as you do.


Thanks a lot, Terry, for your comprehensive example!
--
https://mail.python.org/mailman/listinfo/python-list