Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Rustom Mody
On Monday, March 21, 2016 at 12:11:19 PM UTC+5:30, Steven D'Aprano wrote:
> On Monday 21 March 2016 13:11, Ben Finney wrote:
> 
> > BartC writes:
> > 
> >> I don't have a clue about exceptions
> > 
> > Please, stop making assertions about Python code until you have learned
> > Python.
> 
> 
> I don't see how "I don't have a clue about exceptions" is an assertion about 
> Python code.
> 
> I think there's a lot of hostility aimed at Bart, undeserved hostility, 
> because he continues to demonstrate that Python is not as fast as it could 
> be. I don't see that Bart is trolling or being malicious, although I do 
> think he perhaps hasn't fully grasped the idea that *we know*, and we're 
> willing to live with reduced speed in favour of certain design choices.
> 
> (Ironically, for all that we as a community will go to the battlements to 
> fight to the death to keep these dynamic features, we'll also tar and 
> feather anyone who actually *uses* them. I've even seen people criticise 
> Raymond Hettinger, one of the most experienced and competent senior Python 
> guys, because his "namedtuple" uses exec. Go figure.)
> 
> I'll ask everyone to please give Bart the benefit of the doubt and assume 
> good faith. Rather than snark at him if he writes un-Pythonic code, teach 
> him how to write it better, don't just sneer at the poor quality of his code 
> or fob him off onto another list.
> 
> (Bart, I am also on the tutor mailing list that Ben suggested, and you would 
> be welcome them.)

Thanks Steven for a reasonable response.
Mark's comments are in joke category.
He has the highest #posts/#python-knowledge ratio out here
Chris and Ben's responses smell like sour grapes.
Notice how such responses seem to get sharper as the (imagined) criticism of
python increases

Bart(C): The best use of exceptions is to not bother about them:

Ignore:
 >>> open("NonExistentFile")
Traceback (most recent call last):
  File "", line 1, in 
IOError: [Errno 2] No such file or directory: 'NonExistentFile'

Handle:
>>> try:
...   open("NonExistentFile")
... except IOError as i:
...   print i
... 
[Errno 2] No such file or directory: 'NonExistentFile'
>>> 

So the payoff you get for the 4 extra lines of explicit handling is no traceback
Ok... But tracebacks are not that bad are they?

And JFTR: Pointing out unsatisfactory usage of one language feature
when you are trying to focus on another looks frankly silly to me.

The more reasonable response may be: If you are suggesting a new/changed 
feature you should be doing that on the ideas list

About switch/case:
Almost every modern functional language has a case that is way more powerful
(and as efficient) as the imasculated imperative case/switch.
See my post in the other thread on "case" for a small list of such languages
Why settle for a ½-assed choice?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Chris Angelico
On Mon, Mar 21, 2016 at 5:38 PM, Steven D'Aprano
 wrote:
> On Monday 21 March 2016 12:35, Chris Angelico wrote:
>
>> On Mon, Mar 21, 2016 at 12:28 PM, Mark Lawrence 
>> wrote:
>>> I got to line 22, saw the bare except, and promptly gave up.
>>
>> Oh, keep going, Mark. It gets better.
> [...]
>> So, if any exception happens during the reading of the file, it gets
>> squashed, and 0 is returned - which results in a generic message being
>> printed, and the program terminating, with return value 0. Awesome!
>
>
> While there is a certain level of entertainment to be gained from snarking
> at other people's bad code, and I'm as guilty as anyone else for doing so,
> please remember that Bart is engaged in a good-faith attempt to compare
> Python to other dynamic code. He's not claiming to be a Python expert or
> that he's writing idiomatic Python code.

Given that the exact same issue was pointed out the last time he
posted a version of this code, I have less sympathy and more snark. I
completely understand that not everyone understands what's idiomatic
Python code; but when something is pointed out as a deadly
anti-pattern (not just "that naming convention might confuse people"),
I would expect to see changes.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Marko Rauhamaa
Chris Angelico :

> Given that the exact same issue was pointed out the last time he
> posted a version of this code, I have less sympathy and more snark. I
> completely understand that not everyone understands what's idiomatic
> Python code; but when something is pointed out as a deadly
> anti-pattern (not just "that naming convention might confuse people"),
> I would expect to see changes.

How about simply not responding?


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Ben Finney
Steven D'Aprano  writes:

> On Monday 21 March 2016 13:11, Ben Finney wrote:
> > Please, stop making assertions about Python code until you have
> > learned Python.
>
> I don't see how "I don't have a clue about exceptions" is an assertion
> about Python code.

That's not the assertion. I'm asking Bart to acknowledge that, because
of the ignorance he admits to above, he should not be making elsewhere
the sky-is-falling assertions about Python's failings.

> I'll ask everyone to please give Bart the benefit of the doubt and
> assume good faith.

Bart is continuing to demonstrate *bad* faith by showing un-idiomatic
Python code in support of his claims about how bad Python is, while
simultaneously being confessedly ignorant about idiomatic Python.

Bart can show good faith by *learning* idiomatic Python, with the
humility of a beginner. And also by refraining from rhetoric about how
bad Python's performance is, until he gains experience to make those
claims.

-- 
 \ “Facts are stubborn things; and whatever may be our wishes, our |
  `\   inclinations, or the dictates of our passion, they cannot alter |
_o__)the state of facts and evidence.” —John Adams, 1770-12-04 |
Ben Finney

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


need help With understanding

2016-03-21 Thread mohamed mohamud



hey im new at Learning Python, and i have an issue which i would like som help 
With.

i have currently installed Python, and im Reading this book which tells me i 
have to have IDLE, but i cant find it on my computer. do i need to install it? 
and if so where do/can i find it.?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python boilerplate

2016-03-21 Thread Sam

On 03/19/2016 07:43 AM, Fernando Felix do Nascimento Junior wrote:

A simple boilerplate for those who don't know the structure of a project. 
https://goo.gl/lJRvS6

## Features

* Build and distribute with setuptools
* Check code style with flake8
* Make and run tests with pytest
* Run tests on every Python version with tox
* Code coverage with coverage.py

## Structure

Structure of the project in tree format.

├── CONTRIBUTING.md
├── LICENSE
├── Makefile
├── MANIFEST.in
├── module_name.py
├── README.md
├── requirements
│   ├── dev.txt
│   └── prod.txt
├── requirements.txt
├── setup.cfg
├── setup.py
├── tests.py
└── tox.ini

Fernando Felix




There is this too: https://pypi.python.org/pypi/python_boilerplate_template


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


Using SSL socket as stdin for subprocess.Popen

2016-03-21 Thread Matt Ruffalo
Hi all-

I'm writing a backup client for automating the synchronization of btrfs
snapshots between machines -- essentially piping the output of `btrfs
send` on my laptop/desktop to `btrfs receive` on a server. I've been
doing this manually for quite a while, and something automated would be
much more convenient.

I've started implementing this in terms of a daemon that will run on a
server, and client code that will be invoked every hour or so on client
machines. Every machine I use is configured to take btrfs snapshots of
all available subvolumes on every boot, so it's my expectation that most
of the time the client code runs it will see that it has nothing to do,
and exit with a message like "most recent snapshot is already on remote
server". I'll also implement some checks like "skip backups if running
on battery power" and "only back up over Ethernet instead of wifi", but
one thing at a time.

I've been using SSL for the communication between the client and server,
both in a static "control port" and a dynamic data port that is assigned
for each individual call to btrfs send | btrfs receive. In addition to
verifying the client certificates against a trusted CA, this also allows
things like selecting the backup destination based on the common name in
the client cert.

I've hit an issue that I'm not sure how to work through, though. I'm
attempting to use a SSL socket (and/or the result of its 'makefile'
method) directly as the `stdin` argument to subprocess.Popen, but it
seems that the *encrypted* data is used by the subprocess.

The WIP code is at https://github.com/mruffalo/btrfs-sync-daemon , and
the specific functionality that I'm describing is at
https://github.com/mruffalo/btrfs-sync-daemon/blob/master/server.py#L64
. The SSL socket behaves exactly as I expect in most cases, e.g. calling
its read() method, or calling ssl_sock.makefile('rb') and then read()ing
the result of the makefile() call. This gives me the decrypted data that
I send in the client code.

What seems to be consistently malfunctioning is the usage of the SSL
socket wrappers as `stdin` in a Popen call. While I'm getting all of
this working, I'm replacing the `btrfs receive` call with effectively
`cat > test` in the working directory of the code, and the 'test' file
seems to contain encrypted data instead of the b'hello' I'm sending over
the SSL socket. In addition to using `conn` as `stdin`, I also attempted
using conn.makefile('rb') with the same results: encrypted data in the
output file.

I could conceivably just use a non-encrypted socket for the data
connection since I don't intend this to be used outside of a home
network, but after implementing SSL communication for the control
connection I didn't see a reason *not* to use it for everything.

At the moment I'm running this under Python 3.5.1 on Kubuntu 16.04. I
did some searching in the Python documentation, on Google, and in the
Python bug tracker, but haven't seen anyone else report an issue like
this. Does this sound like a bug I should report?

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


Re: Using SSL socket as stdin for subprocess.Popen

2016-03-21 Thread Chris Angelico
On Sun, Mar 20, 2016 at 4:44 AM, Matt Ruffalo  wrote:
> Hi all-
>
> I'm writing a backup client for automating the synchronization of btrfs
> snapshots between machines -- essentially piping the output of `btrfs
> send` on my laptop/desktop to `btrfs receive` on a server. I've been
> doing this manually for quite a while, and something automated would be
> much more convenient.
> ...
> I've been using SSL for the communication between the client and server,
> both in a static "control port" and a dynamic data port that is assigned
> for each individual call to btrfs send | btrfs receive. In addition to
> verifying the client certificates against a trusted CA, this also allows
> things like selecting the backup destination based on the common name in
> the client cert.
> [snip details of subprocess.Popen issues]

Hmm. Any particular reason you're not using SSH here? That seems like
the most obvious way to pipe the output of a command on one computer
into a command on another. Instead of verifying client certificates,
you'd have a set of authorized public keys, and you could prevent
shell access by using your custom script as the shell of a dedicated
user - so it'd be something like "btrfs send|ssh backup@server".

Whenever I need to encrypt stuff, I try to let someone else do all the
work :) Whatever shell is used for that user, it will get "btrfs send"
piped into it, and all the authentication and encryption is completely
invisible.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Ned Batchelder
On Sunday, March 20, 2016 at 9:15:32 PM UTC-4, BartC wrote:
> 
> A tokeniser along those lines in Python, with most of the bits filled 
> in, is here:
> 
> http://pastebin.com/dtM8WnFZ
> 

Bart, we get it: you don't like the trade-offs that Python has made.
You want Python to be faster, but it can't be because of dynamic features
that you don't think are worthwhile.  That's fine, you don't have to
use or like Python.

You want Python to have features other languages have, like a switch
statement. It doesn't have it, sorry. Not all languages have the same
features. If they did, we'd only have one language.

Again: you don't have to like Python.  But going on and on here about 
your vision of how Python could change is not productive.  We aren't
going to agree with you that Python's dynamic nature is a bad trade-off,
and even if we did, Python is not going to change in the way that you
want.

> I started off trying to write it in a more efficient way that would suit 
> Python better, but quickly tired of that. I should be able to express 
> the code how I want.

You basically said, "I don't want to use Python the way it was designed
to be used." It's no wonder you are not pleased with the results.

You can choose any language you want.  You can even implement your own
language, as you have.

You don't like Python.  Can we leave it at that?

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 02:21, Terry Reedy wrote:

On 3/20/2016 9:15 PM, BartC wrote:

http://pastebin.com/dtM8WnFZ
This is a test of a character-at-a-time task in Python;


I disagree.  It tests of C code re-written in ludicrously crippled
Python.  No use of the re module,


You can't use the re module for this kind of test. It would be like a 
writing a C compiler in Python like this:


  system("gcc "+filename)

(or whatever the equivalent is in Python) and claiming the compilation 
speeds are due to Python's fast byte-code.



designed for tasks like this,


(I've tested someone's parser written in Python using regular 
expressions, I seem to remember it was still pretty slow.)



 > but exactly such tasks are what I often use dynamic languages for.

For instance, there are about 15 clauses like
---
elif c=="?":
lxsymbol=question_sym
return
---

I believe it would be much faster to combine these in one clause. First
define simple_symbols = {'?': question_sym, ...}. Then
elif c in simple_symbols:
lxsymbol = simple_symbols[c]
return



I tried that (for 11 clauses), and it actually got a bit slower if the 
one test was placed towards the end! But faster if placed nearer the 
beginning.


I also tweaked the way each identifier name is built up (using a slice 
after the limits of the name are established instead of building a 
character at a time).


The "\r" check was got rid of (in text mode, it won't occur); the eof 
check was last (as it will only occur once), and the chr(0) check was 
removed as chr(0) isn't used (that would have zero cost in a jumptable 
switch or using a function table, but it does cost here).


Overall, Python 3's throughput increased from 33Klps to 43Kpls (and 
Python 2 from 43Klps to 53Kpls).


HOWEVER: PyPy doesn't seem to like those Dict lookups: it's throughput 
reduced from 105Klps (after those other changes) to 29Klps when the Dict 
lookup was used. Odd.


(I haven't tried this on Ubuntu as that seems to have snappier versions 
of both Python 2 and Pypy, but that's a bit of a pain to test.)



In any case, the O(k), where k is the number of alternatives, linear
search should be replaced by an O(log k) binary search (nested if-else
statement) or O(1) hashed search (with a dictionary mapping chars to
functions.



I started off trying to write it in a more efficient way that would suit
Python better, but quickly tired of that. I should be able to express
the code how I want.


Of course you can.  But you cannot write in a crippled Python subset and
fairly claim that the result represents idiomatic Python code.


For Python I would have used a table of 0..255 functions, indexed by the 
ord() code of each character. So all 52 letter codes map to the same 
name-handling function. (No Dict is needed at this point.)


But that's a hell of a lot of infra-structure to set up, only to find 
out that Python's function call overheads mean it's not much faster (or 
maybe it's slower) than using an if-elif chain.


(I've no idea if it might be much faster or not. And yet, having said 
that, I can't resist trying it out! But it'll have to be a bit later.)


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 12:08, Ned Batchelder wrote:

On Sunday, March 20, 2016 at 9:15:32 PM UTC-4, BartC wrote:


A tokeniser along those lines in Python, with most of the bits filled
in, is here:

http://pastebin.com/dtM8WnFZ



Bart, we get it: you don't like the trade-offs that Python has made.

...

You don't like Python.  Can we leave it at that?


On the contrary, I do like it. It's just a shame it's made those 
trade-offs as a bit more speed would have made it more useful to me.


And apart from my personal opinions, if anyone else is engaged in 
implementing any of this stuff, they might be interested in what plain 
byte-code can achieve.


This tests highlights the benefits of an O(1) switch statement. And of 
being able to work with integers, as Python seems to discourage that. 
(It seems silly to have to compare strings because using ints could be 
slower!)


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Chris Angelico
On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:
> For Python I would have used a table of 0..255 functions, indexed by the
> ord() code of each character. So all 52 letter codes map to the same
> name-handling function. (No Dict is needed at this point.)
>

Once again, you forget that there are not 256 characters - there are
1114112. (Give or take.)

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Rustom Mody
On Monday, March 21, 2016 at 6:04:28 PM UTC+5:30, BartC wrote:
> On 21/03/2016 02:21, Terry Reedy wrote:
> > On 3/20/2016 9:15 PM, BartC wrote:
> >> http://pastebin.com/dtM8WnFZ
> >> This is a test of a character-at-a-time task in Python;
> >
> > I disagree.  It tests of C code re-written in ludicrously crippled
> > Python.  No use of the re module,
> 
> You can't use the re module for this kind of test. It would be like a 
> writing a C compiler in Python like this:
> 
>system("gcc "+filename)
> 
> (or whatever the equivalent is in Python) and claiming the compilation 
> speeds are due to Python's fast byte-code.

No fair!
Terry said re or dict. You answered the re and deleted the dict

I would add try dict and/or flat arrays 
[given that your charset is ASCII and array of 127 at worst even less if you 
chop off unprintables is as good as nothing]

[Ive see hugh speedup in C# going from dictionaries to arrays]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Mark Lawrence

On 21/03/2016 12:59, Chris Angelico wrote:

On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:

For Python I would have used a table of 0..255 functions, indexed by the
ord() code of each character. So all 52 letter codes map to the same
name-handling function. (No Dict is needed at this point.)



Once again, you forget that there are not 256 characters - there are
1114112. (Give or take.)

ChrisA



Head.  Wall.  Bang.

--
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: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Ned Batchelder
On Monday, March 21, 2016 at 8:44:01 AM UTC-4, BartC wrote:
> On 21/03/2016 12:08, Ned Batchelder wrote:
> > On Sunday, March 20, 2016 at 9:15:32 PM UTC-4, BartC wrote:
> >>
> >> A tokeniser along those lines in Python, with most of the bits filled
> >> in, is here:
> >>
> >> http://pastebin.com/dtM8WnFZ
> >>
> >
> > Bart, we get it: you don't like the trade-offs that Python has made.
> ...
> > You don't like Python.  Can we leave it at that?
> 
> On the contrary, I do like it. It's just a shame it's made those 
> trade-offs as a bit more speed would have made it more useful to me.
> 
> And apart from my personal opinions, if anyone else is engaged in 
> implementing any of this stuff, they might be interested in what plain 
> byte-code can achieve.
> 
> This tests highlights the benefits of an O(1) switch statement. And of 
> being able to work with integers, as Python seems to discourage that. 
> (It seems silly to have to compare strings because using ints could be 
> slower!)

You are simply reiterating (again!) the trade-offs you don't like. What
is the point?  Please stop.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread MRAB

On 2016-03-21 12:59, Chris Angelico wrote:

On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:

For Python I would have used a table of 0..255 functions, indexed by the
ord() code of each character. So all 52 letter codes map to the same
name-handling function. (No Dict is needed at this point.)



Once again, you forget that there are not 256 characters - there are
1114112. (Give or take.)


Strictly speaking, they're codepoints.

Only about 10% of codepoints are currently defined, and diacritics and 
the like classed aren't 'characters' as such.


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Mon, 21 Mar 2016 11:59 pm, Chris Angelico wrote:

> On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:
>> For Python I would have used a table of 0..255 functions, indexed by the
>> ord() code of each character. So all 52 letter codes map to the same
>> name-handling function. (No Dict is needed at this point.)
>>
> 
> Once again, you forget that there are not 256 characters - there are
> 1114112. (Give or take.)

Pardon me, do I understand you correctly? You're saying that the C parser is
Unicode-aware and allows you to use Unicode in C source code? Because
Bart's test is for a (simplified?) C tokeniser, and expecting his tokeniser
to support character sets that C does not would be, well, Not Cricket, my
good chap.



-- 
Steven

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Random832
On Mon, Mar 21, 2016, at 09:48, Steven D'Aprano wrote:
> Pardon me, do I understand you correctly? You're saying that the C parser
> is
> Unicode-aware and allows you to use Unicode in C source code?

Er, "the" C parser?

In the C standard, the source character set is implementation-defined,
and is specifically called out that it "may contain multibyte
characters, used to represent members of the extended character set".

But that's really not the point here, the point is that expecting an
implementation of a character-based switch statement in Python to be
able to rely on there only being 256 characters is unreasonable.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Scipy2016: call for proposals

2016-03-21 Thread Nelle Varoquaux
Dear all,

This is a quick reminder that the deadline for submitting talks and posters
proposal is next friday.

Thanks,
Nelle

On 22 February 2016 at 10:15, Nelle Varoquaux 
wrote:

>
> Dear all,
>
> SciPy 2016, the Fifteenth Annual Conference on Python in Science, takes
> place in Austin, TX on July, 11th to 17th. The conference features two days
> of tutorials by followed by three days of presentations, and concludes with
> two days of developer sprints on projects of interest to attendees. .
>
> The topics presented at SciPy are very diverse, with a focus on advanced
> software engineering and original uses of Python and its scientific
> libraries, either in theoretical or experimental research, from both
> academia and the industry. This year we are happy to announce two
> specialized tracks that run in parallel to the general conference (Data
> Science , High Performance Computing) and 8 mini-symposia (Earth and Space
> Science, Biology and Medicine, Engineering, Social Sciences, Special
> Purpose Databases, Case Studies in Industry, Education, Reproducibility)
>
> Submissions for talks and posters are welcome on our website (
> http://scipy2016.scipy.org). In your abstract, please provide details on
> what Python tools are being employed, and how. The talk and poster
> submission deadline is March 25th, 2016, while the tutorial submission
> deadline is March, 21st, 2016.
>
>
> Important dates:
>
> Mar 21: Tutorial Proposals Due
> Mar 25: Talk and Poster Proposals Due
> May 11: Plotting Contest Submissions Due
> Apr 22: Tutorials Announced
> Apr 22: Financial Aid Submissions Due
> May 4: Talk and Posters Announced
> May 11: Financial Aid Recipients Notified
> May 22: Early Bird Registration Deadline
> Jul 11-12: SciPy 2016 Tutorials
> Jul 13-15: SciPy 2016 General Conference
> Jul 16-17: SciPy 2016 Sprints
>
> We look forward to an exciting conference and hope to see you in Austin in
> July!
>
>
> The Scipy 2016
> http://scipy2016.scipy.org/
>
> Conference Chairs: Aric Hagberg, Prabhu Ramachandran
> Tutorial Chairs: Justin Vincent, Ben Root
> Program Chair: Serge Rey, Nelle Varoquaux
> Proceeding Chairs: Sebastian Benthall
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 01:04 am, Random832 wrote:

> On Mon, Mar 21, 2016, at 09:48, Steven D'Aprano wrote:
>> Pardon me, do I understand you correctly? You're saying that the C parser
>> is
>> Unicode-aware and allows you to use Unicode in C source code?
> 
> Er, "the" C parser?
> 
> In the C standard, the source character set is implementation-defined,
> and is specifically called out that it "may contain multibyte
> characters, used to represent members of the extended character set".
> 
> But that's really not the point here, the point is that expecting an
> implementation of a character-based switch statement in Python to be
> able to rely on there only being 256 characters is unreasonable.

Nobody has suggested that. Bart suggested that *his application* would use a
256 table. You trimmed the part of my post that quoted him:

"For Python I would have used a table of 0..255 functions"

Bart, like any of us, is perfectly entitled to only handle 8-bit ASCII
(Latin-1 perhaps?) if he chooses, and he wasn't talking about any
hypothetical future switch statement.



-- 
Steven

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Mon, 21 Mar 2016 11:34 pm, BartC wrote:

> On 21/03/2016 02:21, Terry Reedy wrote:
>> On 3/20/2016 9:15 PM, BartC wrote:
>>> http://pastebin.com/dtM8WnFZ
>>> This is a test of a character-at-a-time task in Python;
>>
>> I disagree.  It tests of C code re-written in ludicrously crippled
>> Python.  No use of the re module,
> 
> You can't use the re module for this kind of test. It would be like a
> writing a C compiler in Python like this:
> 
>system("gcc "+filename)
> 
> (or whatever the equivalent is in Python) and claiming the compilation
> speeds are due to Python's fast byte-code.

Of course you can and should use the re module, when necessary. It is as
much a part of Python's standard library as lists, strings, ints and dicts.
Would you refuse to use dicts because they're built into the core language
rather than written in pure Python? They're *part* of what "pure Python"
means.


>> designed for tasks like this,
> 
> (I've tested someone's parser written in Python using regular
> expressions, I seem to remember it was still pretty slow.)

This is fairly old now, but here are some comparisons of various Python
parsers:

http://www.dalkescientific.com/writings/diary/archive/2007/11/03/antlr_java.html

Parsing 2505 molecular formulae, the author gets the following times:

Custom hand-written parser using re: 0.18
PLY (pure Python) 2.33
ANTLR (Java) 8.73
PyParsing (pure Python) 9.87

I presume the times are in seconds.



>>  > but exactly such tasks are what I often use dynamic languages for.
>>
>> For instance, there are about 15 clauses like
>> ---
>> elif c=="?":
>> lxsymbol=question_sym
>> return
>> ---
>>
>> I believe it would be much faster to combine these in one clause. First
>> define simple_symbols = {'?': question_sym, ...}. Then
>> elif c in simple_symbols:
>> lxsymbol = simple_symbols[c]
>> return
> 
> 
> I tried that (for 11 clauses), and it actually got a bit slower if the
> one test was placed towards the end! But faster if placed nearer the
> beginning.

Without seeing exactly what you did, it is difficult to comment on why it
got slower, or whether the slowdown was significant or just "noise".

[...]
> Overall, Python 3's throughput increased from 33Klps to 43Kpls (and
> Python 2 from 43Klps to 53Kpls).
> 
> HOWEVER: PyPy doesn't seem to like those Dict lookups: it's throughput
> reduced from 105Klps (after those other changes) to 29Klps when the Dict
> lookup was used. Odd.


If you can replicate that with a smaller, more focused piece of code, I'm
sure that the PyPy people will be very interested to look at that.




-- 
Steven

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Rustom Mody
On Monday, March 21, 2016 at 7:19:03 PM UTC+5:30, Steven D'Aprano wrote:
> On Mon, 21 Mar 2016 11:59 pm, Chris Angelico wrote:
> 
> > On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:
> >> For Python I would have used a table of 0..255 functions, indexed by the
> >> ord() code of each character. So all 52 letter codes map to the same
> >> name-handling function. (No Dict is needed at this point.)
> >>
> > 
> > Once again, you forget that there are not 256 characters - there are
> > 1114112. (Give or take.)
> 
> Pardon me, do I understand you correctly? You're saying that the C parser is
> Unicode-aware and allows you to use Unicode in C source code? Because
> Bart's test is for a (simplified?) C tokeniser, and expecting his tokeniser
> to support character sets that C does not would be, well, Not Cricket, my
> good chap.

Sticking to C and integer switches, one would expect that
switch (n)
{
  case 1000:...
  case 1001:
  case 1002:
  :
  :
  case 2000:
  default:
}
would compile into faster/tighter code than
switch (n)
{
  case 1:...
  case 100:
  case 200:
  case 1000:
  case 1:
  default:
}

IOW if the compiler can detect an arithmetic progression or a reasonably dense
subset of one it can make a jump table.  If not it starts deteriorating into
if-else chains

Same applies to char even if char is full-unicode: if the switching is over a
small dense/contiguous subset, a jump table works well (at assembly level)
and so a switch at C level.

[And dicts/arrays of functions are ok approximations to that]
-- 
https://mail.python.org/mailman/listinfo/python-list


Static caching property

2016-03-21 Thread Joseph L. Casale
With non static properties, you can use a decorator that overwrites the
method on the instance with an attribute containing the methods return
effectively caching it.

What technique for a static property can be used to accomplish what the
descriptor protocol does?

I need to cache the results of a method on a class across all instances.

Thanks,
jlc

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 12:48 AM, Steven D'Aprano  wrote:
> On Mon, 21 Mar 2016 11:59 pm, Chris Angelico wrote:
>
>> On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:
>>> For Python I would have used a table of 0..255 functions, indexed by the
>>> ord() code of each character. So all 52 letter codes map to the same
>>> name-handling function. (No Dict is needed at this point.)
>>>
>>
>> Once again, you forget that there are not 256 characters - there are
>> 1114112. (Give or take.)
>
> Pardon me, do I understand you correctly? You're saying that the C parser is
> Unicode-aware and allows you to use Unicode in C source code? Because
> Bart's test is for a (simplified?) C tokeniser, and expecting his tokeniser
> to support character sets that C does not would be, well, Not Cricket, my
> good chap.

We nutted part of this out earlier in the thread; Python 3.x code is,
and any other modern language should be, defined to have Unicode
source. (And yes, MRAB, I'm aware that only a tiny fraction of
codepoints are defined; it's still a lot more than 256, and going to
make for a far larger lookup table.) While you could plausibly define
that your source code consists only of printable ASCII characters (eg
09,10,13,32-126), it is an extremely bad idea to declare that it has
256 possibilities - you're shackling your language to a parser
definition that includes both more and less than people will expect.

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


Re: Python boilerplate

2016-03-21 Thread Chris Warrick
On 21 March 2016 at 00:36, Fernando Felix do Nascimento Junior
 wrote:
> I made the boilerplate with intent that everyone can understand, download and 
> use quickly. So, I didn't put extra dependence like cookiecutter (that 
> depends jinja, that depends markupsafe) to **just** replace fields and then 
> run the project.

I used “replace manually” before, and it was painful.

> I also preferred to use .md instead .rst because it's more clean in my 
> opinion and used by default in platforms like GitHub and Stackoverflow. See 
> mkdocs to generate documentation with markdown.

The vast majority of the Python community uses Sphinx and reST. In
fact, that’s the only thing accepted on readthedocs.org, which is a
popular documentation platform.

> I didn't understand why packages are best than modules... both can be 
> reusable and not every project needs packages.

It might not need it today, but it will probably grow. At which point
you will notice that a module is not enough. You can also easily
separate code with packages.

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Static caching property

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 9:38 AM, Joseph L. Casale
 wrote:
> With non static properties, you can use a decorator that overwrites the
> method on the instance with an attribute containing the methods return
> effectively caching it.

Can you give an example of what you mean?

> What technique for a static property can be used to accomplish what the
> descriptor protocol does?
>
> I need to cache the results of a method on a class across all instances.

Why not do the same thing but using a class attribute instead of an
instance attribute?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Mon, 21 Mar 2016 06:47 pm, Ben Finney wrote:

> Steven D'Aprano  writes:
> 
>> On Monday 21 March 2016 13:11, Ben Finney wrote:
>> > Please, stop making assertions about Python code until you have
>> > learned Python.
>>
>> I don't see how "I don't have a clue about exceptions" is an assertion
>> about Python code.
> 
> That's not the assertion. I'm asking Bart to acknowledge that, because
> of the ignorance he admits to above, he should not be making elsewhere
> the sky-is-falling assertions about Python's failings.

I haven't seen these assertions that the sky is falling. What I have seen is
some benchmarks which purport to show that Python performs poorly compared
to Bart's custom language, which he describes as "dynamic", and Bart trying
to understand the design decisions which lead to this poor performance.

In fact, Bart even described one of those benchmarks as demonstrating that
Python was fast enough to be usable for typical tasks he performs (albeit
at the slow end of usable), so I believe that he is writing them in good
faith.


>> I'll ask everyone to please give Bart the benefit of the doubt and
>> assume good faith.
> 
> Bart is continuing to demonstrate *bad* faith by showing un-idiomatic
> Python code in support of his claims about how bad Python is, while
> simultaneously being confessedly ignorant about idiomatic Python.

Idiomatic like this example from the standard library?

https://hg.python.org/cpython/file/2.7/Lib/shlex.py


I do not believe that unfamiliarity with a language is prima facie evidence
of bad faith, and I do not believe that it is reasonable for us to demand
that programmers spend months or years learning all the idioms of a
language before judging whether or not it is fast enough for the types of
tasks they plan to use it for.

There are many users of Python, of many varying degrees of skill and
knowledge, and they don't always write what experts will call "idiomatic"
code. In the real world, people will write unidiomatic code. Don't you
think it would be good if Python was as fast as it could be even if your
code was less than perfect?

Besides, nobody has demonstrated that re-writing Bart's code would make it
significantly faster. Maybe it will double the speed. Maybe it will improve
times by 10%. Or maybe it won't make any real difference.


> Bart can show good faith by *learning* idiomatic Python, with the
> humility of a beginner. And also by refraining from rhetoric about how
> bad Python's performance is, until he gains experience to make those
> claims.

"Humility of a beginner"... what a strange phrase to use about somebody who
has been programming for decades. What exactly is the problem here? Is it
that Bart hasn't earned the right to say what we all know, that Python is
slow, because he's an outsider?

I mean, honestly, Bart has written some code to read text from a file and
parse it, found it is slow, and some people are rejecting the results
because he's unwisely suppressed tracebacks using bare excepts. How does
this invalidate his results? If you change the "except" to "except
IOError", will that magically speed up the script by a factor of five? I
don't think so.

We all know that Python can be quite slow, and the Python community has many
work-arounds for this fact, most of which come down to "write the slow bits
in C". Look how much of the standard library is either written in C or thin
wrappers around C: the builtins themselves, math, decimal, array, binascii,
bisect, pickle, datetime, itertools, heapq and more. There's even an entire
module designed to let you call C functions from within Python:

https://docs.python.org/3/library/ctypes.html

(Jython and IronPython don't have anything like this, but instead they have
interfaces for calling Java and .Net code.)

In some ways, Python could be considered a wrapper around C, and perhaps
even when that's not entirely accurate, it is good to remember that Python
started off as a glue language for C and Fortran libraries.

I think Bart is perfectly entitled to carry on investigating the speed of
Python using the styles of coding he is used to, and compare it to his own
language. If this group was half as welcoming and friendly as we like to
tell ourselves we are, we'd be helping him to write more idiomatic code,
which hopefully will be faster, but realistically may not be, not telling
him to bugger off until he's learnt more Python, not dismissing his
benchmarks as invalid because they're not idiomatic, and especially not
sneering at him for a few poor coding choices in a script which is
prominently flagged at the top of the file as "buggy" and not for
production use.



-- 
Steven

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


Re: Static caching property

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 03:15 am, Ian Kelly wrote:

> On Mon, Mar 21, 2016 at 9:38 AM, Joseph L. Casale
>  wrote:
>> With non static properties, you can use a decorator that overwrites the
>> method on the instance with an attribute containing the methods return
>> effectively caching it.
> 
> Can you give an example of what you mean?

I think Joseph is using "static" in the Java sense of being associated with
the class rather than an instance. (In Java, members of classes must be
known at compile-time.)


>> What technique for a static property can be used to accomplish what the
>> descriptor protocol does?
>>
>> I need to cache the results of a method on a class across all instances.
> 
> Why not do the same thing but using a class attribute instead of an
> instance attribute?

Properties don't work when called from a class:

py> class Test(object):
... @property
... def x(self):
... return 999
...
py> Test.x == 999
False
py> Test.x




But what you can do is have the property refer to a class attribute:


py> class Test(object):
... _private = 999
... @property
... def x(self):
... return type(self)._private
... @x.setter
... def x(self, value):
... type(self)._private = value
...
py> a = Test()
py> b = Test()
py> c = Test()
py> a.x
999
py> b.x = 50
py> c.x
50
py> a.x
50



-- 
Steven

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


Re: Static caching property

2016-03-21 Thread Joseph L. Casale
> I think Joseph is using "static" in the Java sense of being associated with
> the class rather than an instance. (In Java, members of classes must be
> known at compile-time.)

Yup, so a single value on the class itself, not instance specific.

> But what you can do is have the property refer to a class attribute:
> 
> 
> py> class Test(object):
> ... _private = 999
> ... @property
> ... def x(self):
> ... return type(self)._private
> ... @x.setter
> ... def x(self, value):
> ... type(self)._private = value
> ...
> py> a = Test()
> py> b = Test()
> py> c = Test()
> py> a.x
> 999
> py> b.x = 50
> py> c.x
> 50
> py> a.x
> 50

Right, but _private refers to an api call that is expensive and may not even be 
accessed,
so while I may new up three instances of Test across a, b and c, if none of 
those end up
accessing var x, it shouldn't get fetched. Without some deferred execution, if 
the value
of _private is a callable whose return value is what I am interested in, it 
gets invoked the
moment the class is compiled.

In the non static sense this is trivial to accomplish with the descriptor 
protocol, I am just not
clear for the static sense.

Thanks Ian and Steven,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Static caching property

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 3:49 AM, Joseph L. Casale
 wrote:
> Right, but _private refers to an api call that is expensive and may not even 
> be accessed,
> so while I may new up three instances of Test across a, b and c, if none of 
> those end up
> accessing var x, it shouldn't get fetched. Without some deferred execution, 
> if the value
> of _private is a callable whose return value is what I am interested in, it 
> gets invoked the
> moment the class is compiled.
>
> In the non static sense this is trivial to accomplish with the descriptor 
> protocol, I am just not
> clear for the static sense.

One solution is to use descriptor protocol on the class, which means
using a metaclass. I'm not sure it's the best option, but it is an
option.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 16:30, Steven D'Aprano wrote:

On Mon, 21 Mar 2016 06:47 pm, Ben Finney wrote:


Steven D'Aprano  writes:


On Monday 21 March 2016 13:11, Ben Finney wrote:

Please, stop making assertions about Python code until you have
learned Python.


I don't see how "I don't have a clue about exceptions" is an assertion
about Python code.


That's not the assertion. I'm asking Bart to acknowledge that, because
of the ignorance he admits to above, he should not be making elsewhere
the sky-is-falling assertions about Python's failings.


I haven't seen these assertions that the sky is falling. What I have seen is
some benchmarks which purport to show that Python performs poorly compared
to Bart's custom language, which he describes as "dynamic", and Bart trying
to understand the design decisions which lead to this poor performance.

In fact, Bart even described one of those benchmarks as demonstrating that
Python was fast enough to be usable for typical tasks he performs (albeit
at the slow end of usable), so I believe that he is writing them in good
faith.


Yes, on my machine even Python 3 can apparently tokenise C at around 40K 
lines per second. But this varies depending on the density of the 
source. On other inputs, it might achieve over 50Klps. And this was the 
slowest of the Pythons tested; others will be faster. My machine isn't 
the fastest either.


So, even though this test only does the lowest level of tokenising (the 
next steps are doing name lookups, and then actual parsing), I think it 
is quite practical to use Python in tasks like this.


(Note that my gcc C compiler can only compile at 2-5 Klps, peaking at 
12Klps with input in one large. Compared to that, the Python timing 
isn't too bad.)


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


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Adam"  wrote in message 
news:ncikss$tks$1...@news.albasani.net...
>
> Host OS:Ubuntu Desktop 14.04 LTS / Unity
>
> System crashed while using PyCharm / Python3.
> Booting takes forever and stuck at the purple screen with
> the Ubuntu logo and the five dots cycling.
> How to fix?
>

So, I tried Ubuntu repair methods suggested here...

How to Fix an Ubuntu System When It Won't Boot
http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
GRUB Boot Loader is accessible but using the recovery mode did not help.
It hangs with "Loading extension GLX" but I forget which command I used.

Then, I tried to...

Reinstall Ubuntu While Keeping Files and Programs

but, I am instead given the following options...
===
This computer currently has Ubuntu 14.04.4 LTS on it.  What would you like 
to do?

- Erase Ubuntu 14.04.4 LTS and reinstall
Warning: This will delete all your Ubuntu 14.04.4 LTS programs, 
documents, photos...
- Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
Documents, music, and other personal files will be kept.  You can choose 
which operating system...
- Erase disk and install Ubuntu
- Something else
===

Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is the 
safest ? 


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


Re: Static caching property

2016-03-21 Thread Joseph L. Casale
> One solution is to use descriptor protocol on the class, which means
> using a metaclass. I'm not sure it's the best option, but it is an
> option.

I will look at that, I wonder if however I am not over complicating it:

class Foo:
_bar = None
@property
def expensive(self):
if Foo._bar is None:
import something
Foo._bar = something.expensive()
return Foo._bar

Somewhat naive, but a test with if is pretty cheap...
Thanks Chris,
jlc
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 12:59, Chris Angelico wrote:

On Mon, Mar 21, 2016 at 11:34 PM, BartC  wrote:

For Python I would have used a table of 0..255 functions, indexed by the
ord() code of each character. So all 52 letter codes map to the same
name-handling function. (No Dict is needed at this point.)



Once again, you forget that there are not 256 characters - there are
1114112. (Give or take.)


The original code for this test expected the data to be a series of 
bytes, mostly ASCII. Any Unicode in the input would be expected to be in 
the form of UTF-8.


Since this was designed to tokenise C, I don't think C supports Unicode 
except in comments within the code, and within string literals. For 
those purposes, it is not necessary to do anything with UTF-8 escape 
sequences except ignore them or process them unchanged. (I'm ignoring 
'wide' string and char literals).


But it doesn't make any difference: you process a byte at a time, and 
trap codes C0 to FF which is the start of an escape sequence.


I understand that Python 3 doing text mode files can do this expansion 
automatically, and give you a string that might contain code points 
above 127. That's not a problem: you can still treat the first 128 
code-points exactly as I have, and have special treatment for the rest. 
But you /will/ need to know if data is a raw UTF-8 stream, or has been 
already processed into Unicode.


(I'm taking about 'top-level' character dispatch where you're looking 
for the start of a token.)


Note that my test data was 5,964,784 bytes on disk, of which 14 had 
values above 127: probably 3 or 4 Unicode characters, and most likely in 
comments.


Given that 99.9998% of input byte data is ASCII, and 99.% of 
characters (in this data), is it unreasonable to concentrate on that 
0..127 range?



--
Bartc

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


Re: Scipy2016: call for proposals

2016-03-21 Thread Nelle Varoquaux
I'm terribly sorry about this second email.
The deadline for submitting talks and posters for scipy 2016 is this friday
(friday 25th), and not next friday (april fools day).

Thanks,
Nelle

On 21 March 2016 at 15:32, Nelle Varoquaux 
wrote:

> Dear all,
>
> This is a quick reminder that the deadline for submitting talks and
> posters proposal is next friday.
>
> Thanks,
> Nelle
>
> On 22 February 2016 at 10:15, Nelle Varoquaux 
> wrote:
>
>>
>> Dear all,
>>
>> SciPy 2016, the Fifteenth Annual Conference on Python in Science, takes
>> place in Austin, TX on July, 11th to 17th. The conference features two days
>> of tutorials by followed by three days of presentations, and concludes with
>> two days of developer sprints on projects of interest to attendees. .
>>
>> The topics presented at SciPy are very diverse, with a focus on advanced
>> software engineering and original uses of Python and its scientific
>> libraries, either in theoretical or experimental research, from both
>> academia and the industry. This year we are happy to announce two
>> specialized tracks that run in parallel to the general conference (Data
>> Science , High Performance Computing) and 8 mini-symposia (Earth and Space
>> Science, Biology and Medicine, Engineering, Social Sciences, Special
>> Purpose Databases, Case Studies in Industry, Education, Reproducibility)
>>
>> Submissions for talks and posters are welcome on our website (
>> http://scipy2016.scipy.org). In your abstract, please provide details on
>> what Python tools are being employed, and how. The talk and poster
>> submission deadline is March 25th, 2016, while the tutorial submission
>> deadline is March, 21st, 2016.
>>
>>
>> Important dates:
>>
>> Mar 21: Tutorial Proposals Due
>> Mar 25: Talk and Poster Proposals Due
>> May 11: Plotting Contest Submissions Due
>> Apr 22: Tutorials Announced
>> Apr 22: Financial Aid Submissions Due
>> May 4: Talk and Posters Announced
>> May 11: Financial Aid Recipients Notified
>> May 22: Early Bird Registration Deadline
>> Jul 11-12: SciPy 2016 Tutorials
>> Jul 13-15: SciPy 2016 General Conference
>> Jul 16-17: SciPy 2016 Sprints
>>
>> We look forward to an exciting conference and hope to see you in Austin
>> in July!
>>
>>
>> The Scipy 2016
>> http://scipy2016.scipy.org/
>>
>> Conference Chairs: Aric Hagberg, Prabhu Ramachandran
>> Tutorial Chairs: Justin Vincent, Ben Root
>> Program Chair: Serge Rey, Nelle Varoquaux
>> Proceeding Chairs: Sebastian Benthall
>>
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 12:34, BartC wrote:

On 21/03/2016 02:21, Terry Reedy wrote:



Of course you can.  But you cannot write in a crippled Python subset and
fairly claim that the result represents idiomatic Python code.


For Python I would have used a table of 0..255 functions, indexed by the
ord() code of each character.



But that's a hell of a lot of infra-structure to set up, only to find
out that Python's function call overheads mean it's not much faster (or
maybe it's slower) than using an if-elif chain.


I've tried it now and it's a bit slower (perhaps 5%). But once done, I 
think it looks better structured, and does away with a few globals.


I won't post the code as it will only get picked on for some irrelevant 
detail! But the main readtoken() routine now looks like this:


def readtoken(psource):
global lxsptr, lxsymbol
lxsubcode = 0

while (1):
c=psource[lxsptr]
lxsptr+=1
d=ord(c)
if d<256:
lxsymbol = disptable[d](psource,c)
else:
lxsymbol = fn_error(psource,c)

if lxsymbol != skip_sym:
break

(This assumes input is ASCII or UTF-8. For Unicode, the 256 changes to 
128, and the call to fn_error() is replaced by something that deals with 
a Unicode token starter, which is most likely to be an error in the case 
of C source input.)


(While I here, something that came up yesterday: why hasn't Python fixed 
the bug it seems to have inherited from C, where:


  a << b + c

is evaluated as 'a << (b+c)'? That cost me half an hour to sort out! << 
and >> scale numbers just like * and /, so should have the same precedence.)


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


Re: crash while using PyCharm / Python3

2016-03-21 Thread Dirk T. Verbeek

Op 21-03-16 om 17:51 schreef Adam:

"Adam"  wrote in message
news:ncikss$tks$1...@news.albasani.net...


Host OS:Ubuntu Desktop 14.04 LTS / Unity

System crashed while using PyCharm / Python3.
Booting takes forever and stuck at the purple screen with
the Ubuntu logo and the five dots cycling.
How to fix?



So, I tried Ubuntu repair methods suggested here...

How to Fix an Ubuntu System When It Won't Boot
http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
GRUB Boot Loader is accessible but using the recovery mode did not help.
It hangs with "Loading extension GLX" but I forget which command I used.

Then, I tried to...

Reinstall Ubuntu While Keeping Files and Programs

but, I am instead given the following options...
===
This computer currently has Ubuntu 14.04.4 LTS on it.  What would you like
to do?

- Erase Ubuntu 14.04.4 LTS and reinstall
 Warning: This will delete all your Ubuntu 14.04.4 LTS programs,
documents, photos...
- Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
 Documents, music, and other personal files will be kept.  You can choose
which operating system...
- Erase disk and install Ubuntu
- Something else
===

Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is the
safest ?


It's easy to do a reinstall without wiping documents and application 
configurations/settings PROVIDING you have a separate / (root) and /home 
partition.


During partition you select Manual partitioning and assign the existing 
partitions being VERY careful to NOT format the existing /home.


But / where the OS and all applications reside will need to be 
formatted/deleted/overwritten.


Keeping the configuration on your /home might cause the old error to be 
included in the new install...


Although, you error seems to be the system, not the applications.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Static caching property

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 10:36 AM, Steven D'Aprano  wrote:
> On Tue, 22 Mar 2016 03:15 am, Ian Kelly wrote:
>> Why not do the same thing but using a class attribute instead of an
>> instance attribute?
>
> Properties don't work when called from a class:

Properties specifically do not, but descriptors in general do.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Static caching property

2016-03-21 Thread Ethan Furman

On 03/21/2016 10:03 AM, Joseph L. Casale wrote:


One solution is to use descriptor protocol on the class, which means
using a metaclass. I'm not sure it's the best option, but it is an
option.


I will look at that, I wonder if however I am not over complicating it:

class Foo:
 _bar = None
 @property
 def expensive(self):
 if Foo._bar is None:
 import something
 Foo._bar = something.expensive()
 return Foo._bar

Somewhat naive, but a test with if is pretty cheap...


A slightly cleaner approach (but only slightly):

  class Cache(object):
  _sentinal = object()
  def __init__(self, expensive_func):
  self.value = self._sentinal
  self.func = expensive_func
  def __get__(self, *args):
  if self.value is self._sentinal:
  self.value = self.func()
 return self.func()

The advantages:

- only one location in the class
- works correctly whether accessed via class or instance
- clue as to functionality in the name

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


Re: Static caching property

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 10:54 AM, Chris Angelico  wrote:
> On Tue, Mar 22, 2016 at 3:49 AM, Joseph L. Casale
>  wrote:
>> Right, but _private refers to an api call that is expensive and may not even 
>> be accessed,
>> so while I may new up three instances of Test across a, b and c, if none of 
>> those end up
>> accessing var x, it shouldn't get fetched. Without some deferred execution, 
>> if the value
>> of _private is a callable whose return value is what I am interested in, it 
>> gets invoked the
>> moment the class is compiled.
>>
>> In the non static sense this is trivial to accomplish with the descriptor 
>> protocol, I am just not
>> clear for the static sense.
>
> One solution is to use descriptor protocol on the class, which means
> using a metaclass. I'm not sure it's the best option, but it is an
> option.

You don't actually need a metaclass for this:

>>> class Desc:
... def __get__(self, obj, type=None):
... if not type._cached_value:
... type._cached_value = compute_value(type)
... return type._cached_value
...
>>> def compute_value(x): return 42
...
>>> class Test:
... foo = Desc()
... _cached_value = None
...
>>> Test._cached_value
>>> Test.foo
42
>>> Test._cached_value
42
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do you use python?

2016-03-21 Thread sohcahtoa82
On Saturday, October 31, 2009 at 12:11:02 AM UTC-7, sk wrote:
> What would be your answer if this question is asked to you in an
> interview?
> 
> a modified version might be:
> "Where would you use python over C/C++/Java?"
> 
> (because my resume says I know C/C++/Java)?

I use Python when speed of development is far more important than speed of 
execution.

I use C/C++ when speed of execution is priority and I don't care about being 
cross-platform.

Though for what it's worth, I've only been a software engineer using Python for 
two years, and I have yet to use C/C++ outside of small personal projects and 
school.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Adam"  wrote in message 
news:ncp8r1$ofd$1...@news.albasani.net...
>
> "Adam"  wrote in message 
> news:ncikss$tks$1...@news.albasani.net...
>>
>> Host OS:Ubuntu Desktop 14.04 LTS / Unity
>>
>> System crashed while using PyCharm / Python3.
>> Booting takes forever and stuck at the purple screen with
>> the Ubuntu logo and the five dots cycling.
>> How to fix?
>>
>
> So, I tried Ubuntu repair methods suggested here...
>
> How to Fix an Ubuntu System When It Won't Boot
> http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
> GRUB Boot Loader is accessible but using the recovery mode did not help.
> It hangs with "Loading extension GLX" but I forget which command I used.

I remember now, failsafeX and Ctrl+Alt+F1 from here...
http://askubuntu.com/questions/141606/how-to-fix-the-system-is-running-in-low-graphics-mode-error

But, still getting "The system is running in low-graphics mode" error.  How 
to fix?
The problem does not appear to be due to low disk space.
At least, it's no longer stuck at the purple screen with
the Ubuntu logo and the five dots cycling.
So, I will hold off reinstalling Ubuntu for now.


>
> Then, I tried to...
>
> Reinstall Ubuntu While Keeping Files and Programs
>
> but, I am instead given the following options...
> ===
> This computer currently has Ubuntu 14.04.4 LTS on it.  What would you like 
> to do?
>
> - Erase Ubuntu 14.04.4 LTS and reinstall
>Warning: This will delete all your Ubuntu 14.04.4 LTS programs, 
> documents, photos...
> - Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
>Documents, music, and other personal files will be kept.  You can 
> choose which operating system...
> - Erase disk and install Ubuntu
> - Something else
> ===
>
> Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is the 
> safest ?
> 


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


Re: empty clause of for loops

2016-03-21 Thread Rob Gaddi
Sven R. Kunze wrote:

> On 16.03.2016 16:02, Tim Chase wrote:
>> On 2016-03-16 15:29, Sven R. Kunze wrote:
>>> I would re-use the "for-else" for this. Everything I thought I
>>> could make use of the "-else" clause, I was disappointed I couldn't.
>> Hmm...this must be a mind-set thing.  I use the "else" clause with
>> for/while loops fairly regularly and would be miffed if their behavior
>> changed.
>>
>> Could I work around their absence?  Certainly.
>>
>> Does it annoy me when I have to work in other languages that lack
>> Python's {for/while}/else functionality?  You bet.
>
> I can imagine that. Could you describe the general use-case? From what I 
> know, "else" is executed when you don't "break" the loop. When is this 
> useful?
>

When you've got a sequence in which you only care about the first hit,
either because you only want one or because you know from everything
else going on in the program there can only be one.

for fn in self._containedfunctions:
  if fn.__doc__ is not None:
self.__doc__ = fn.__doc__
break
else:
  raise NoDocstringError("were you raised in a barn?")

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


WConio won't install in Python 3.4

2016-03-21 Thread David Lemper
Recently returned to Python after four years.
Installed version 3.4.4 and attempted to install WConio
in my Windows 8.1 system
Gonnerman's website has
WConio-1.5.1.win32-py3.3.exe
 &  WConio-1.5.1.win32-py3.5.exe , but no py3.4 ( ? )

Tried to install each of the above. Both failed giving
same error message :
"Python version 3.2 required, which was not found
 in the registry"

Searched the web. One site said that the installer was
looking in the registry for 
 HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.2

and possibly my Python installer had put this info in
HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2

Searched my registry and found nothing like either of
the above containing "PythonCore". This was many layers
deep under Installer  UserData  S-1-5-18  Components ...

Can WConio be installed for version 3.4 ?
If not should I go to 3.3 or 3.5 ?
Curious why Gonnermam's site has no version for 3.4
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Mark Lawrence

On 21/03/2016 17:31, BartC wrote:


(While I here, something that came up yesterday: why hasn't Python fixed
the bug it seems to have inherited from C, where:

   a << b + c

is evaluated as 'a << (b+c)'? That cost me half an hour to sort out! <<
and >> scale numbers just like * and /, so should have the same
precedence.)



Python is not C.  This is not a bug.  The rules are here 
https://docs.python.org/3/reference/expressions.html#operator-precedence


--
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: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Dirk T. Verbeek"  wrote in message 
news:56f03080$0$5924$e4fe5...@news.xs4all.nl...
> Op 21-03-16 om 17:51 schreef Adam:
>> "Adam"  wrote in message
>> news:ncikss$tks$1...@news.albasani.net...
>>>
>>> Host OS:Ubuntu Desktop 14.04 LTS / Unity
>>>
>>> System crashed while using PyCharm / Python3.
>>> Booting takes forever and stuck at the purple screen with
>>> the Ubuntu logo and the five dots cycling.
>>> How to fix?
>>>
>>
>> So, I tried Ubuntu repair methods suggested here...
>>
>> How to Fix an Ubuntu System When It Won't Boot
>> http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
>> GRUB Boot Loader is accessible but using the recovery mode did not help.
>> It hangs with "Loading extension GLX" but I forget which command I used.
>>
>> Then, I tried to...
>>
>> Reinstall Ubuntu While Keeping Files and Programs
>>
>> but, I am instead given the following options...
>> ===
>> This computer currently has Ubuntu 14.04.4 LTS on it.  What would you 
>> like
>> to do?
>>
>> - Erase Ubuntu 14.04.4 LTS and reinstall
>>  Warning: This will delete all your Ubuntu 14.04.4 LTS programs,
>> documents, photos...
>> - Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
>>  Documents, music, and other personal files will be kept.  You can 
>> choose
>> which operating system...
>> - Erase disk and install Ubuntu
>> - Something else
>> ===
>>
>> Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is the
>> safest ?
>>
>>
> It's easy to do a reinstall without wiping documents and application 
> configurations/settings PROVIDING you have a separate / (root) and /home 
> partition.

Yes, I have a separate /home partition.


>
> During partition you select Manual partitioning and assign the existing 
> partitions being VERY careful to NOT format the existing /home.
>
> But / where the OS and all applications reside will need to be 
> formatted/deleted/overwritten.
>
> Keeping the configuration on your /home might cause the old error to be 
> included in the new install...
>
> Although, you error seems to be the system, not the applications.

I think it's a system issue as well.  The problem happened almost right 
after
creating a python3 virtual environment using PyCharm.
There were two checkboxes (both "Inherit global site-packages" and
"Make available to all projects" were checked).
Can this be the problem ? 


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


Convert list to another form but providing same information

2016-03-21 Thread Maurice
Hello, hope everything is okay. I think someone might have dealt with a similar 
issue I'm having.

Basically I wanna do the following:

I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain key in 
the dictionary)

And I need to convert it to a list of 32 elements (meaning days of the month 
however first element ie index 0 or day zero has no meaning - keeping like that 
for simplicity's sake).
Therefore the resulting list should be:
[0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]

So the list index should tell how many occurrences of a certain day in the 
original list. 

My attempt:

weirdList = [[0]*32]*len(dict_days) #list's length should be how many keys in 
the dict.

counter = 0
k = 0
for key in dict_days.keys():
for i in range(1,32):
if i in dict_days[key]:
counter = dict_days[key].count(i)
weirdList[k][i] = counter
dict_days[key] = weirdList[k]
k+=1

However it does not work. weirdList seems to be always the same? 

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


Re: Convert list to another form but providing same information

2016-03-21 Thread Maurice
Just figured why:

If I type this on the kernel:

weirdList = [[0]*3]*5

weirdList
Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

weirdList[0][0] = 1

weirdList
Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]

All first elements of the sublists also changes. I dunno why...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Dirk T. Verbeek"  wrote in message 
news:56f03080$0$5924$e4fe5...@news.xs4all.nl...
> Op 21-03-16 om 17:51 schreef Adam:
>> "Adam"  wrote in message
>> news:ncikss$tks$1...@news.albasani.net...
>>>
>>> Host OS:Ubuntu Desktop 14.04 LTS / Unity
>>>
>>> System crashed while using PyCharm / Python3.
>>> Booting takes forever and stuck at the purple screen with
>>> the Ubuntu logo and the five dots cycling.
>>> How to fix?
>>>
>>
>> So, I tried Ubuntu repair methods suggested here...
>>
>> How to Fix an Ubuntu System When It Won't Boot
>> http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
>> GRUB Boot Loader is accessible but using the recovery mode did not help.
>> It hangs with "Loading extension GLX" but I forget which command I used.
>>
>> Then, I tried to...
>>
>> Reinstall Ubuntu While Keeping Files and Programs
>>
>> but, I am instead given the following options...
>> ===
>> This computer currently has Ubuntu 14.04.4 LTS on it.  What would you 
>> like
>> to do?
>>
>> - Erase Ubuntu 14.04.4 LTS and reinstall
>>  Warning: This will delete all your Ubuntu 14.04.4 LTS programs,
>> documents, photos...
>> - Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
>>  Documents, music, and other personal files will be kept.  You can 
>> choose
>> which operating system...
>> - Erase disk and install Ubuntu
>> - Something else
>> ===
>>
>> Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is the
>> safest ?
>>
>>
> It's easy to do a reinstall without wiping documents and application 
> configurations/settings PROVIDING you have a separate / (root) and /home 
> partition.
>
> During partition you select Manual partitioning and assign the existing 
> partitions being VERY careful to NOT format the existing /home.
>
> But / where the OS and all applications reside will need to be 
> formatted/deleted/overwritten.

I guess I can do something like...

$ dpkg --get-selections \*|awk '{print $1}'>~/package_list
$ cat ~/package_list|sudo xargs -l1 aptitude reinstall

to reinstall packages that were installed after installing Ubuntu.

There ought to be a way to just reinstall the graphics subsystem rather than
an all-or-none installation approach.


>
> Keeping the configuration on your /home might cause the old error to be 
> included in the new install...
>
> Although, you error seems to be the system, not the applications. 


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


Re: WConio won't install in Python 3.4

2016-03-21 Thread Mark Lawrence

On 21/03/2016 18:11, David Lemper wrote:

Recently returned to Python after four years.
Installed version 3.4.4 and attempted to install WConio
 in my Windows 8.1 system
Gonnerman's website has
 WConio-1.5.1.win32-py3.3.exe
  &  WConio-1.5.1.win32-py3.5.exe , but no py3.4 ( ? )

Tried to install each of the above. Both failed giving
same error message :
 "Python version 3.2 required, which was not found
  in the registry"

Searched the web. One site said that the installer was
looking in the registry for
  HKEY_CURRENT_USER\SOFTWARE\Python\PythonCore\3.2

and possibly my Python installer had put this info in
 HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.2

Searched my registry and found nothing like either of
the above containing "PythonCore". This was many layers
deep under Installer  UserData  S-1-5-18  Components ...

Can WConio be installed for version 3.4 ?
If not should I go to 3.3 or 3.5 ?
Curious why Gonnermam's site has no version for 3.4



3.5, no question, as it has all of the latest goodies as given in 
https://docs.python.org/3/whatsnew/3.5.html, plus 3.3 is all ready out 
of support.


--
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: multiprocessing, pool, queue length

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 4:25 AM, Michael Welle  wrote:
> Hello,
>
> I use a multiprocessing pool. My producer calls pool.map_async()
> to fill the pool's job queue. It can do that quite fast, while the
> consumer processes need much more time to empty the job queue. Since the
> producer can create a lot of jobs, I thought about asking the pool for
> the amount of jobs it has in its queue and then only produce more jobs
> if the current value is below a threshold. It seems like the pool
> doesn't want to tell me the level of the queue, does it? What is a
> better strategy to solve this problem? Implementing a pool around
> multiprocessing's Process and Queue?

A simple solution would be to have a shared multiprocessing.Value that
tracks how many items are in the pool. Whenever the producer produces
items it increments the Value, and whenever a consumer finishes a job
it decrements the Value.

An alternative solution that doesn't require adding a small amount of
work to every job would be to have the producer add a sentinel task
that does nothing at or near the end of the batch, and either wait on
the result or check it periodically. When it's done, then the pool is
low enough to add more jobs.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert list to another form but providing same information

2016-03-21 Thread Mark Lawrence

On 21/03/2016 18:26, Maurice wrote:

Hello, hope everything is okay. I think someone might have dealt with a similar 
issue I'm having.

Basically I wanna do the following:

I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain key in 
the dictionary)

And I need to convert it to a list of 32 elements (meaning days of the month 
however first element ie index 0 or day zero has no meaning - keeping like that 
for simplicity's sake).
Therefore the resulting list should be:
[0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]

So the list index should tell how many occurrences of a certain day in the 
original list.

My attempt:

weirdList = [[0]*32]*len(dict_days) #list's length should be how many keys in 
the dict.

counter = 0
k = 0
for key in dict_days.keys():
 for i in range(1,32):
 if i in dict_days[key]:
 counter = dict_days[key].count(i)
 weirdList[k][i] = counter
 dict_days[key] = weirdList[k]
 k+=1

However it does not work. weirdList seems to be always the same?

Thanks in advance.



>>> from collections import Counter
>>> counts = Counter([6,19,19,21,21,21])
>>> counts
Counter({21: 3, 19: 2, 6: 1})
>>> weird_list = [0]*32
>>> weird_list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0]

>>> for index, count in counts.items():
... weird_list[index] = count
...
>>> weird_list
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0]


--
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: Convert list to another form but providing same information

2016-03-21 Thread Peter Otten
Maurice wrote:

> Hello, hope everything is okay. I think someone might have dealt with a
> similar issue I'm having.
> 
> Basically I wanna do the following:
> 
> I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain
> key in the dictionary)
> 
> And I need to convert it to a list of 32 elements (meaning days of the
> month however first element ie index 0 or day zero has no meaning -
> keeping like that for simplicity's sake). Therefore the resulting list
> should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
> 
> So the list index should tell how many occurrences of a certain day in the
> original list.
> 
> My attempt:
> 
> weirdList = [[0]*32]*len(dict_days) #list's length should be how many keys
> in the dict.

Rewrite the above as

inner = [0] * 32
weirdList = [inner] * len(dict_days)

Now it should be clearer that weirdList is

[inner, inner, inner, ...]

i. e. len(dict_days) times the same list of 32 zeros. When you modify 
weirdList[0] you also modify weirdList[1] etc. because these are the 
actually the same list.

For the inner list this is not a problem because 0 is immutable, so you can 
only replace it. If the inner list contained a mutable object you'd see the 
same "weird" behaviour you see for the outer list.

The fix is to use multiple inner lists:

not_so_weirdList = [[0]*32 for _ in range(len(dict_days))]

> counter = 0
> k = 0
> for key in dict_days.keys():
> for i in range(1,32):
> if i in dict_days[key]:
> counter = dict_days[key].count(i)
> weirdList[k][i] = counter
> dict_days[key] = weirdList[k]
> k+=1
> 
> However it does not work. weirdList seems to be always the same?

Instead of the list you could use a counter and get basically the same 
interface as long as you ensure that the second index satisfies 0<=index<32:

import collections

for key, values in dict_days.items():
dict_days[key] = collections.Counter(values)


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


Re: Convert list to another form but providing same information

2016-03-21 Thread Mark Lawrence

On 21/03/2016 18:30, Maurice wrote:

Just figured why:

If I type this on the kernel:

weirdList = [[0]*3]*5

weirdList
Out[257]: [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]

weirdList[0][0] = 1

weirdList
Out[259]: [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]]

All first elements of the sublists also changes. I dunno why...



https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x

--
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: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 02:02, Mark Lawrence wrote:

On 21/03/2016 01:35, Chris Angelico wrote:

On Mon, Mar 21, 2016 at 12:28 PM, Mark Lawrence
 wrote:

I got to line 22, saw the bare except, and promptly gave up.


Oh, keep going, Mark. It gets better.

def readstrfile(file):
 try:
 data=open(file,"r").read()
 except:
 return 0
 return data

def start():
 psource=readstrfile(infile)
 if psource==0:
 print ("Can't open file",infile)
 exit(0)

So, if any exception happens during the reading of the file, it gets
squashed, and 0 is returned - which results in a generic message being
printed, and the program terminating, with return value 0. Awesome!

ChrisA



The essential question is "which is faster?".  Who cares about trivial
little details like the user being given false data, as (say) they can
open the file but can't read it.  Or inadvertantly writing an infinite
loop and not being able to CTRL-C out of it, having to revert to your OS
to kill the rogue that's killing your CPU.

25 years of trying to teach people how to write Pythonic code and this
is how far we've got.  Heck, I think I'll see my GP later today for some
more, more powerful, tranquilisers.



This code was adapted from a program that used:

   readstrfile(filename)

which either returned the contents of the file as a string, or 0.

That's all. My Python version was thrown together as I don't know if 
there's a similar function to do the same.


If you want to talk about Pythonic, I don't see why that file API 
doesn't count (the original is buried in a library).


Or does Pythonic mean bristling with exceptions and classes and what-not?


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Mark Lawrence

On 21/03/2016 19:43, BartC wrote:

On 21/03/2016 02:02, Mark Lawrence wrote:

On 21/03/2016 01:35, Chris Angelico wrote:

On Mon, Mar 21, 2016 at 12:28 PM, Mark Lawrence
 wrote:

I got to line 22, saw the bare except, and promptly gave up.


Oh, keep going, Mark. It gets better.

def readstrfile(file):
 try:
 data=open(file,"r").read()
 except:
 return 0
 return data

def start():
 psource=readstrfile(infile)
 if psource==0:
 print ("Can't open file",infile)
 exit(0)

So, if any exception happens during the reading of the file, it gets
squashed, and 0 is returned - which results in a generic message being
printed, and the program terminating, with return value 0. Awesome!

ChrisA



The essential question is "which is faster?".  Who cares about trivial
little details like the user being given false data, as (say) they can
open the file but can't read it.  Or inadvertantly writing an infinite
loop and not being able to CTRL-C out of it, having to revert to your OS
to kill the rogue that's killing your CPU.

25 years of trying to teach people how to write Pythonic code and this
is how far we've got.  Heck, I think I'll see my GP later today for some
more, more powerful, tranquilisers.



This code was adapted from a program that used:

readstrfile(filename)

which either returned the contents of the file as a string, or 0.


So you've used a dreadful piece of code, not recognising it as such. 
This again indicates that you know precisely nothing about Python, apart 
from the well known fact that relative to some languages, it is rather 
slow at run time.  In programmer time it more than makes up for that. 
Horses for courses?




That's all. My Python version was thrown together as I don't know if
there's a similar function to do the same.


"As I don't know", out of the horse's mouth.



If you want to talk about Pythonic, I don't see why that file API
doesn't count (the original is buried in a library).


Precisely, what does the file API have to do with a dreadfully written 
piece of code misusing exceptions?




Or does Pythonic mean bristling with exceptions and classes and what-not?



I've all ready pointed out that Python prefers EAFP to LBYL via 
exceptions.  Classes not necessarily, especially in the days of the 
namedtuple.


--
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: crash while using PyCharm / Python3

2016-03-21 Thread Jonathan N. Little

Adam wrote:

There ought to be a way to just reinstall the graphics subsystem rather than
an all-or-none installation approach.


Yes you can. Did it for a borked install of the nVidia driver. reference 
this:




--
Take care,

Jonathan
---
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
--
https://mail.python.org/mailman/listinfo/python-list


Re: Convert list to another form but providing same information

2016-03-21 Thread Ben Bacarisse
Maurice  writes:

> Hello, hope everything is okay. I think someone might have dealt with
> a similar issue I'm having.
>
> Basically I wanna do the following:
>
> I have a list such [6,19,19,21,21,21] (FYI this is the item of a
>certain key in the dictionary)
>
> And I need to convert it to a list of 32 elements (meaning days of the
> month however first element ie index 0 or day zero has no meaning -
> keeping like that for simplicity's sake).

> Therefore the resulting list should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]

How about

  reduce(lambda counts, day: counts[:day] + [counts[day]+1] + counts[day+1:],
 days, [0]*32)

?  (reduce is in functools).

Not efficient, but sometimes you just want to the job done.

More efficient would be:

  def inc_day(counts, day): counts[day] += 1; return counts
  reduce(inc_day, days, [0]*32)

For experts here: why can't I write a lambda that has a statement in it
(actually I wanted two: lambda l, i: l[i] += 1; return l)?


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


Re: Convert list to another form but providing same information

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse  wrote:
> For experts here: why can't I write a lambda that has a statement in it
> (actually I wanted two: lambda l, i: l[i] += 1; return l)?

https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-statements
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert list to another form but providing same information

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 2:12 PM, Ian Kelly  wrote:
> On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse  wrote:
>> For experts here: why can't I write a lambda that has a statement in it
>> (actually I wanted two: lambda l, i: l[i] += 1; return l)?
>
> https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-statements

tl;dr: If you want a function with a statement in it, use def, not lambda.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Ned Batchelder
On Monday, March 21, 2016 at 3:43:40 PM UTC-4, BartC wrote:
> 
> This code was adapted from a program that used:
> 
> readstrfile(filename)
> 
> which either returned the contents of the file as a string, or 0.
> 
> That's all. My Python version was thrown together as I don't know if 
> there's a similar function to do the same.
> 
> If you want to talk about Pythonic, I don't see why that file API 
> doesn't count (the original is buried in a library).
> 
> Or does Pythonic mean bristling with exceptions and classes and what-not?

Steven has argued eloquently that we should welcome you to the Python way
of doing things.  My question to you is: do you want to learn it? 

You already seem to have decided that exceptions and classes are useless. 
You aren't allowing yourself to use regexes, and probably other tools from
the standard library.

Your strategy seems to be to limit yourself to the subset of Python that
overlaps with C, and then complain when something from C is missing. Why?
Perhaps you would just be happier with C?

Help us understand: why are you using Python? Why aren't you interested in
learning what it has to offer?

I'll gladly explain why Python programmers prefer exceptions over returned
error codes, but I'll wait until I know whether you want to know.

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


Re: Why do you use python?

2016-03-21 Thread mbg1708
On Monday, 21 March 2016 04:13:45 UTC, Chris Angelico  wrote:
> On Mon, Mar 21, 2016 at 2:59 PM,   wrote:
> > instead, to be efficient, it is best to combine tools to solve problems 
> > that contain complexities where there is nothing available off the shelve 
> > that does the job. c# is free, free VS studio, i can run ironpython there, 
> > i can do python there, and talk to linux boxes with python, i can run c# on 
> > linux boxes using mono(did that back in 2004 and thereafter for a while).  
> > i can run python on my beaglebone black inside of snappy ubuntu, ect.
> >
> > so i ask those employers why not use what is available to solve problems 
> > instead of limiting yourself to just one???
> 
> Because you won't be there forever, and they'll have to find someone
> else to maintain your hellspawn hodge-podge of languages, tools, and
> libraries. (And yes, it will be described that way by the next person,
> no matter how careful you are.) It's in their interests to restrict
> its complexity at least a bit. I'm not sure what advantage you gain by
> incorporating C# into the mix, but the *dis*advantage is that, forever
> afterward, Visual Studio and Mono will be necessary to use and develop
> this project. Every new thing needed is another thing that can go
> wrong, another thing people need to learn, etc, etc.
> 
> So instead of treating programming like a plumber at a hardware store,
> treat it like an artist with a canvas. You wouldn't normally see a
> portrait done partly in watercolor and partly in oils - or if it is,
> it's for a VERY deliberate effect. You'd more often see one style used
> for one project, and maybe another one used for another.
> 
> ChrisA

Spot on.  It's actually worse than Chris says.  I've had trouble with old code 
that I WROTE MYSELF.  Please don't tell me that I didn't try hard enough the 
first time..the only time to find that out is the second time!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: monkey patching __code__

2016-03-21 Thread Matt Wheeler
On 20 March 2016 at 16:46, Sven R. Kunze  wrote:
> On 19.03.2016 00:58, Matt Wheeler wrote:
>>
>> I know you have a working solution now with updating the code &
>> defaults of the function, but what about just injecting your function
>> into the modules that had already imported it after the
>> monkeypatching?
>>
>> Seems perhaps cleaner, unless you'd end up having to do it to lots of
>> modules...
>
> Why do you consider it cleaner?

I think it would be more explicit and understandable for someone
reading your code.

I suppose it's quite subjective :)

-- 
Matt Wheeler
http://funkyh.at
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do you use python?

2016-03-21 Thread Larry Martell
On Mon, Mar 21, 2016 at 4:26 PM,   wrote:
> On Monday, 21 March 2016 04:13:45 UTC, Chris Angelico  wrote:
>> On Mon, Mar 21, 2016 at 2:59 PM,   wrote:
>> > instead, to be efficient, it is best to combine tools to solve problems 
>> > that contain complexities where there is nothing available off the shelve 
>> > that does the job. c# is free, free VS studio, i can run ironpython there, 
>> > i can do python there, and talk to linux boxes with python, i can run c# 
>> > on linux boxes using mono(did that back in 2004 and thereafter for a 
>> > while).  i can run python on my beaglebone black inside of snappy ubuntu, 
>> > ect.
>> >
>> > so i ask those employers why not use what is available to solve problems 
>> > instead of limiting yourself to just one???
>>
>> Because you won't be there forever, and they'll have to find someone
>> else to maintain your hellspawn hodge-podge of languages, tools, and
>> libraries. (And yes, it will be described that way by the next person,
>> no matter how careful you are.) It's in their interests to restrict
>> its complexity at least a bit. I'm not sure what advantage you gain by
>> incorporating C# into the mix, but the *dis*advantage is that, forever
>> afterward, Visual Studio and Mono will be necessary to use and develop
>> this project. Every new thing needed is another thing that can go
>> wrong, another thing people need to learn, etc, etc.
>>
>> So instead of treating programming like a plumber at a hardware store,
>> treat it like an artist with a canvas. You wouldn't normally see a
>> portrait done partly in watercolor and partly in oils - or if it is,
>> it's for a VERY deliberate effect. You'd more often see one style used
>> for one project, and maybe another one used for another.
>>
>> ChrisA
>
> Spot on.  It's actually worse than Chris says.  I've had trouble with old 
> code that I WROTE MYSELF.  Please don't tell me that I didn't try hard enough 
> the first time..the only time to find that out is the second time!!

https://xkcd.com/1421/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: creating multiple python Django projects in Windows environment

2016-03-21 Thread jogaserbia
On Saturday, March 19, 2016 at 3:16:53 AM UTC-4, Chris Angelico wrote:
> On Sat, Mar 19, 2016 at 6:04 AM, jogaserbia wrote:
> > Can someone please give me ideas on what I should read about (or pay 
> > someone to do) that would enable me to create a basis on which multiple 
> > Python (web and non-web) applications can be access by staff in a windows 
> > environment.
> >
> 
> You basically have two choices:
> 
> 1) Run the Python app on some central server, and have your staff
> access it using something they already have (eg a web server)
> 
> 2) Have them all install Python and run it locally.
> 
> Your call. :)
> 
> ChrisA

Hi Chris, 

To be clear I understand, do I create a Django project and create separate 
Django apps for each different application I want to build?

That way, I create a main website page (Django in the back), and have everyone 
click through to the application needed.  

Would I look into using vagrant and virtualbox to create an environment that 
would then be loaded onto my linux (home development) server and my windows 
(office) production server to ensure they are the same?  

Thanks for the help,

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


Re: multiprocessing, pool, queue length

2016-03-21 Thread Ian Kelly
On Mon, Mar 21, 2016 at 1:46 PM, Michael Welle  wrote:
> Wait on the result means to set a multiprocessing.Event if one of the
> consumers finds the sentinel task and wait for it on the producer? Hmm,
> that might be better than incrementing a counter. But still, it couples
> the consumers and the producer more than I like.

No, I mean calling AsyncResult.wait() on the result of the sentinel
task (or just calling Pool.apply instead of Pool.apply_async in the
first place).

> Another idea that I had is to use map() instead of map_async() and then
> put the producer in its own process. That should work if job creation is
> fast.

Essentially the same thing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: need help With understanding

2016-03-21 Thread Peter Pearson
On Mon, 21 Mar 2016 11:18:57 +, mohamed mohamud wrote:
> hey im new at Learning Python, and i have an issue which i would like
> som help With.
>
> i have currently installed Python, and im Reading this book which
> tells me i have to have IDLE, but i cant find it on my computer. do i
> need to install it? and if so where do/can i find it.?

Welcome.  I hope you will find this newsgroup pleasantly helpful.

You can use Python perfectly well without IDLE.  (I have, for years.)
If your book requires IDLE, you must either get IDLE or stop using
that book.   There are good Python tutorials online (Google will
find many), so you might not need any book.

Also, you'll be more likely to get good help from this group if you
mention what operating system and what version of Python you use.

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Jonathan N. Little"  wrote in message 
news:ncpjj0$7ug$1...@dont-email.me...
> Adam wrote:
>> There ought to be a way to just reinstall the graphics subsystem rather 
>> than
>> an all-or-none installation approach.
>
> Yes you can. Did it for a borked install of the nVidia driver. reference 
> this:
>
> 

Thanks, even after doing the following...
Problem: Need to purge -fglrx
Typically, the following manual commands will properly uninstall -fglrx:


  sudo apt-get remove --purge xorg-driver-fglrx fglrx*
  sudo apt-get install --reinstall libgl1-mesa-glx libgl1-mesa-dri 
xserver-xorg-core
  sudo dpkg-reconfigure xserver-xorgI still get that dreaded "The system is 
running in low-graphics mode" error.
And, recovery mode failsafeX and Ctrl+Alt+F1 hangs with the following...


Initializing built-in extension MIT-SCREEN-SAVER
Initializing built-in extension DOUBLE-BUFFER
Initializing built-in extension RECORD
Initializing built-in extension DPMS
Initializing built-in extension Present
Initializing built-in extension DRI3
Initializing built-in extension X-Resource
Initializing built-in extension XVideo
Initializing built-in extension XVideo-MotionCompensation
Initializing built-in extension SELinux
Initializing built-in extension XFree86-VidModeExtension
Initializing built-in extension XFree86-DGA
Initializing built-in extension XFree86-DRI
Initializing built-in extension DRI2
Loading extension GLX



>
> -- 
> Take care,
>
> Jonathan
> ---
> LITTLE WORKS STUDIO
> http://www.LittleWorksStudio.com 


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


Re: creating multiple python Django projects in Windows environment

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 8:02 AM, jogaserbia
 wrote:
> On Saturday, March 19, 2016 at 3:16:53 AM UTC-4, Chris Angelico wrote:
>> On Sat, Mar 19, 2016 at 6:04 AM, jogaserbia wrote:
>> > Can someone please give me ideas on what I should read about (or pay 
>> > someone to do) that would enable me to create a basis on which multiple 
>> > Python (web and non-web) applications can be access by staff in a windows 
>> > environment.
>> >
>>
>> You basically have two choices:
>>
>> 1) Run the Python app on some central server, and have your staff
>> access it using something they already have (eg a web server)
>>
>> 2) Have them all install Python and run it locally.
>>
>> Your call. :)
>>
>> ChrisA
>
> Hi Chris,
>
> To be clear I understand, do I create a Django project and create separate 
> Django apps for each different application I want to build?
>
> That way, I create a main website page (Django in the back), and have 
> everyone click through to the application needed.

Hmm, that depends how you want to structure things. If the different
applications are conceptually different, you can keep them completely
separate by making them separate Django projects; that would give you
the flexibility to put them onto independent URLs, post the source
code to one of them on GitHub and the source to the other on
SourceForge, etc, etc, etc. Alternatively, you can simply have a
single Django application, and then namespace everything using
directories, so people would access http://blah.blah/app1/whatever and
http://blah.blah/app2/whatever to choose what they're doing.

> Would I look into using vagrant and virtualbox to create an environment that 
> would then be loaded onto my linux (home development) server and my windows 
> (office) production server to ensure they are the same?
>

You shouldn't need to, but I would strongly recommend having a
"Staging" server which is as similar as possible to your production
server. It's all too easy to accidentally do something in a
Windows-specific or Linux-specific way if you don't test. (Note that
"Staging" could be actually on the same hardware as "Production", if
you don't have spare funds for a duplicate. Keep your modules separate
using venv, and access them on different URLs; just have some place
where you can quickly check your code under a production-like
environment prior to actually bringing everything down due to a
one-character bug.)

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Terry Reedy

On 3/21/2016 8:43 AM, BartC wrote:


This tests highlights the benefits of an O(1) switch statement.


So why are you not taking advantage of Python's O(1) dict lookup?


--
Terry Jan Reedy

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


Re: Static caching property

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 04:48 am, Ian Kelly wrote:

> You don't actually need a metaclass for this:
> 
 class Desc:
> ... def __get__(self, obj, type=None):
> ... if not type._cached_value:
> ... type._cached_value = compute_value(type)
> ... return type._cached_value


This won't quite work. What if the cached value happens to be falsey, yet
still expensive to compute? You should compare it to a known sentinel which
the expensive function will never return (possibly None). Or use a hasattr
test: if not hasattr(type, '_cached_value').

But my favourite is to combine them:


class Desc:
def __get__(self, obj, type):
sentinel = object()  # guaranteed to be unique
value = getattr(type, _cached_value, sentinel)
if value is sentinel:
value = type._cached_value = compute_value(type)
return value



Also, you don't need the default type=None. The descriptor protocol should
never call __get__ without supplying the type. The obj may be None, but I
don't believe there are any circumstances where type will be None.

Which is good, because if it is:

AttributeError: 'NoneType' object has no attribute '_cached_value'



-- 
Steven

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Terry Reedy

On 3/21/2016 8:34 AM, BartC wrote:

On 21/03/2016 02:21, Terry Reedy wrote:

On 3/20/2016 9:15 PM, BartC wrote:

http://pastebin.com/dtM8WnFZ
This is a test of a character-at-a-time task in Python;


I disagree.  It tests of C code re-written in ludicrously crippled
Python.  No use of the re module,


You can't use the re module for this kind of test. It would be like a
writing a C compiler in Python like this:

   system("gcc "+filename)


I disagree.  The re module is an integral part of python, as delivered, 
not an external program.  It is included (and the core written in C) 
*because* it is needed to do certain tasks with reasonable speed.


Would you say that one cannot use re syntax in a Perl program?  The 
decision to access the functions via syntax rather than import should 
not determine the answer.



(or whatever the equivalent is in Python) and claiming the compilation
speeds are due to Python's fast byte-code.


designed for tasks like this,


(I've tested someone's parser written in Python using regular
expressions, I seem to remember it was still pretty slow.)


I would be flabbergasted is recognizing floats with an re was slower 
than doing so with several lines of Python, as you did.



For instance, there are about 15 clauses like
---
elif c=="?":
lxsymbol=question_sym
return
---

I believe it would be much faster to combine these in one clause. First
define simple_symbols = {'?': question_sym, ...}. Then
elif c in simple_symbols:
lxsymbol = simple_symbols[c]
return


I tried that (for 11 clauses), and it actually got a bit slower if the
one test was placed towards the end! But faster if placed nearer the
beginning.


I would want to test the dict lookup directly against the equivalent if 
chain.  But the placement issue is one I did not directly address.  To 
optimize a series of conditional clauses, one must order according to 
frequency of truthfulness and time of execution.  Unless one is unlucky, 
dict lookup should be overall constant regardless of insertion order.


--
Terry Jan Reedy

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


Re: Convert list to another form but providing same information

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 05:26 am, Maurice wrote:

> I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain
> key in the dictionary)
> 
> And I need to convert it to a list of 32 elements (meaning days of the
> month however first element ie index 0 or day zero has no meaning -
> keeping like that for simplicity's sake). Therefore the resulting list
> should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
> 
> So the list index should tell how many occurrences of a certain day in the
> original list.

the_list = [6,19,19,21,21,21]
days = [0]*32
for item in the_list:
days[item] += 1



-- 
Steven

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


Re: Static caching property

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 11:05 AM, Steven D'Aprano  wrote:
> But my favourite is to combine them:
>
>
> class Desc:
> def __get__(self, obj, type):
> sentinel = object()  # guaranteed to be unique
> value = getattr(type, _cached_value, sentinel)
> if value is sentinel:
> value = type._cached_value = compute_value(type)
> return value
>
>
>

That seems like overkill. Inside getattr is the equivalent of:

try: return type._cached_value
except AttributeError: return sentinel

So skip getattr/hasattr and just use try/except:

class Desc:
def __get__(self, obj, type):
try:
return type._cached_value
except AttributeError:
value = type._cached_value = compute_value(type)
return value

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 23:50, Terry Reedy wrote:

On 3/21/2016 8:43 AM, BartC wrote:


This tests highlights the benefits of an O(1) switch statement.


So why are you not taking advantage of Python's O(1) dict lookup?

I've already reported using a list lookup which is also O(1), and it 
didn't really help. I doubt a dict lookup is going to be faster (but 
Python has surprised me before).


One problem is how to attach the handling code to the entry in the list 
or as the value in the dict. With the list, I stored a function reference.


I suspect that a switch implemented Python-style wouldn't be 
dramatically faster either. But the handling code can be expressed in-line.


--
bartc


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Terry Reedy

On 3/21/2016 1:12 PM, BartC wrote:


Given that 99.9998% of input byte data is ASCII, and 99.% of
characters (in this data), is it unreasonable to concentrate on that
0..127 range?


No.  And if you take the approach of mapping the whole ascii set, you 
should use a lookup sequence rather than a lookup dict.


--
Terry Jan Reedy

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


Re: creating multiple python Django projects in Windows environment

2016-03-21 Thread ivan77

> 
> Hmm, that depends how you want to structure things. If the different
> applications are conceptually different, you can keep them completely
> separate by making them separate Django projects; that would give you
> the flexibility to put them onto independent URLs, post the source
> code to one of them on GitHub and the source to the other on
> SourceForge, etc, etc, etc. Alternatively, you can simply have a
> single Django application, and then namespace everything using
> directories, so people would access http://blah.blah/app1/whatever and
> http://blah.blah/app2/whatever to choose what they're doing.
> 
> > Would I look into using vagrant and virtualbox to create an environment 
> > that would then be loaded onto my linux (home development) server and my 
> > windows (office) production server to ensure they are the same?
> >
> 
> You shouldn't need to, but I would strongly recommend having a
> "Staging" server which is as similar as possible to your production
> server. It's all too easy to accidentally do something in a
> Windows-specific or Linux-specific way if you don't test. (Note that
> "Staging" could be actually on the same hardware as "Production", if
> you don't have spare funds for a duplicate. Keep your modules separate
> using venv, and access them on different URLs; just have some place
> where you can quickly check your code under a production-like
> environment prior to actually bringing everything down due to a
> one-character bug.)
> 
> ChrisA

Thanks.  I like the idea of the separate URLs.  That way, I guess I could use 
another framework (e.g. flask), or whatever else if the need ever arose.  Also, 
I could use a very simple front page with links to all of the projects.  


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


Re: Convert list to another form but providing same information

2016-03-21 Thread Ben Bacarisse
Ian Kelly  writes:

> On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse  wrote:
>> For experts here: why can't I write a lambda that has a statement in it
>> (actually I wanted two: lambda l, i: l[i] += 1; return l)?
>
> https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-contain-statements

Thanks.  That makes it clear that it's just for syntactic and not
semantic reasons.

However, the explanation ("because Python’s syntactic framework can't
handle statements nested inside expressions") seemed, at first, to be
saying you can't because you can't!  But the term "syntactic framework"
hints that it's not really just an arbitrary choice -- that this is
something about the way Python is parsed that make this choice
inevitable.  Is it to do with the way that indentation has a syntactic
role?

To phrase my question in terms of syntax, why is the : in lambda
followed by a test (the top-level production for an expression in the
grammar) but the : in a function definition is followed by a suite?  I
expected them to be both a suite, but presumably something goes horribly
wrong if that were done.  Now I'm wondering what.

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


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Adam"  wrote in message 
news:ncprqb$tl9$1...@news.albasani.net...
>
> "Jonathan N. Little"  wrote in message 
> news:ncpjj0$7ug$1...@dont-email.me...
>> Adam wrote:
>>> There ought to be a way to just reinstall the graphics subsystem rather 
>>> than
>>> an all-or-none installation approach.
>>
>> Yes you can. Did it for a borked install of the nVidia driver. reference 
>> this:
>>
>> 
>
> Thanks, even after doing the following...
>
> Problem: Need to purge -fglrx
>
> Typically, the following manual commands will properly uninstall -fglrx:
>
>  $ sudo apt-get remove --purge xorg-driver-fglrx fglrx*
>  $ sudo apt-get install --reinstall libgl1-mesa-glx libgl1-mesa-dri 
> xserver-xorg-core
>  $ sudo dpkg-reconfigure xserver-xorg
>
> I still get that dreaded "The system is running in low-graphics mode" 
> error.
> And, recovery mode failsafeX and Ctrl+Alt+F1 hangs with the following...
>
>
> Initializing built-in extension MIT-SCREEN-SAVER
> Initializing built-in extension DOUBLE-BUFFER
> Initializing built-in extension RECORD
> Initializing built-in extension DPMS
> Initializing built-in extension Present
> Initializing built-in extension DRI3
> Initializing built-in extension X-Resource
> Initializing built-in extension XVideo
> Initializing built-in extension XVideo-MotionCompensation
> Initializing built-in extension SELinux
> Initializing built-in extension XFree86-VidModeExtension
> Initializing built-in extension XFree86-DGA
> Initializing built-in extension XFree86-DRI
> Initializing built-in extension DRI2
> Loading extension GLX
> 
>

After trying the following...

http://askubuntu.com/questions/577093/how-to-install-gnome-desktop

$ sudo apt-get update
$ sudo apt-get install gnome-shell


http://tipsonubuntu.com/2014/06/06/change-display-manager-ubuntu-14-04/

$ sudo dpkg-reconfigure lightdm

I am now able to login.  But,
not sure what happened to the "lightdm" display manager.
I am now using "gdm" display manager.


>
>>
>> -- 
>> Take care,
>>
>> Jonathan
>> ---
>> LITTLE WORKS STUDIO
>> http://www.LittleWorksStudio.com
>
> 


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


Re: crash while using PyCharm / Python3

2016-03-21 Thread Adam

"Adam"  wrote in message 
news:ncpev8$51d$1...@news.albasani.net...
>
> "Dirk T. Verbeek"  wrote in message 
> news:56f03080$0$5924$e4fe5...@news.xs4all.nl...
>> Op 21-03-16 om 17:51 schreef Adam:
>>> "Adam"  wrote in message
>>> news:ncikss$tks$1...@news.albasani.net...

 Host OS:Ubuntu Desktop 14.04 LTS / Unity

 System crashed while using PyCharm / Python3.
 Booting takes forever and stuck at the purple screen with
 the Ubuntu logo and the five dots cycling.
 How to fix?

>>>
>>> So, I tried Ubuntu repair methods suggested here...
>>>
>>> How to Fix an Ubuntu System When It Won't Boot
>>> http://www.howtogeek.com/196740/how-to-fix-an-ubuntu-system-when-it-wont-boot/
>>> GRUB Boot Loader is accessible but using the recovery mode did not help.
>>> It hangs with "Loading extension GLX" but I forget which command I used.
>>>
>>> Then, I tried to...
>>>
>>> Reinstall Ubuntu While Keeping Files and Programs
>>>
>>> but, I am instead given the following options...
>>> ===
>>> This computer currently has Ubuntu 14.04.4 LTS on it.  What would you 
>>> like
>>> to do?
>>>
>>> - Erase Ubuntu 14.04.4 LTS and reinstall
>>>  Warning: This will delete all your Ubuntu 14.04.4 LTS programs,
>>> documents, photos...
>>> - Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS
>>>  Documents, music, and other personal files will be kept.  You can 
>>> choose
>>> which operating system...
>>> - Erase disk and install Ubuntu
>>> - Something else
>>> ===
>>>
>>> Perhaps, "Install Ubuntu 14.04.4 LTS alongside Ubuntu 14.04.4 LTS" is 
>>> the
>>> safest ?
>>>
>>>
>> It's easy to do a reinstall without wiping documents and application 
>> configurations/settings PROVIDING you have a separate / (root) and /home 
>> partition.
>>
>> During partition you select Manual partitioning and assign the existing 
>> partitions being VERY careful to NOT format the existing /home.
>>
>> But / where the OS and all applications reside will need to be 
>> formatted/deleted/overwritten.
>
> I guess I can do something like...
>
> $ dpkg --get-selections \*|awk '{print $1}'>~/package_list
> $ cat ~/package_list|sudo xargs -l1 aptitude reinstall
>
> to reinstall packages that were installed after installing Ubuntu.
>
> There ought to be a way to just reinstall the graphics subsystem rather 
> than
> an all-or-none installation approach.
>

Sure glad I did not reinstall Ubuntu.  Whew!!


>
>>
>> Keeping the configuration on your /home might cause the old error to be 
>> included in the new install...
>>
>> Although, you error seems to be the system, not the applications.
>
> 


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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Mark Lawrence

On 22/03/2016 00:18, BartC wrote:

On 21/03/2016 23:50, Terry Reedy wrote:

On 3/21/2016 8:43 AM, BartC wrote:


This tests highlights the benefits of an O(1) switch statement.


So why are you not taking advantage of Python's O(1) dict lookup?


I've already reported using a list lookup which is also O(1), and it
didn't really help. I doubt a dict lookup is going to be faster (but
Python has surprised me before).


Please explain how you managed to make the list lookup O(1).



One problem is how to attach the handling code to the entry in the list
or as the value in the dict. With the list, I stored a function reference.


It has repeatedly been stated that a dict is used as a switch 
replacement, so there is no problem attaching a handler to a dict.




I suspect that a switch implemented Python-style wouldn't be
dramatically faster either. But the handling code can be expressed in-line.



There are umpteen recipes of all shapes and sizes that implement 
switch/case statements, even pattern matching, online.  Why don't you 
time some of them, as relying on gut instinct is almost always wrong in 
Python.


--
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: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 21/03/2016 23:20, Dennis Lee Bieber wrote:

On Mon, 21 Mar 2016 17:31:21 +, BartC  declaimed the
following:


I wasn't going to post it but here it is anyway:

http://pastebin.com/FLbWSdpT

(I've added some spaces for your benefit. This also builds a histogram 
of names so as to do something useful. Note that despite my concerns 
about speed, this module can process itself in around 100ms.)




def readtoken(psource):
global lxsptr, lxsymbol


Why is "lxsymbol" a global, and not something returned by the function
(I can understand your making lxsptr global as you intend to come back in
with it later).


Ideally there would be a descriptor or handle passed around which 
contains the current state of the tokeniser, and where you stick the 
current token values. But for a speed test, I was worried about 
attribute lookups.


In the first Python version, I used 'nonlocals' (belonging to an 
enclosing function), but they were just as slow as globals!



lxsubcode = 0


Unused in the rest of the sample


This is a global. Some lxsymbol values will set it, for the rest it's 
neater if it's zeroed.




while (1):


while True: #At least since Python 2.x... No () needed


c=psource[lxsptr]


Is the spacebar broken? How about some whitespace between language
elements... They don't take up that much memory


(It's not broken but it wouldn't be consistent.)


Given that you state you expect to only be working with 8-bit bytes...


if d<256:


this will always be true


Unfortunately Python 3 doesn't play along. There could be some Unicode 
characters in the string, with values above 255. (And if I used 
byte-sequences, I don't know what would work and what wouldn't.)




lxsymbol = disptable[d](psource,c)


Looks like you are indexing a 256-element table of functions, using the
numeric value of the character/byte as the index... Only to then pass your
entire source string along with the character from it to the function.


No, it passes only a reference to the entire string. The current 
position is in 'lxsptr'. Yes the mix of parameters and globals is messy. 
All globals might be better (in the original non-Python, 'globals' would 
be have module-scope, and not visible outside the tokeniser module 
unless explicitly exported. Semi-global...).



I have no idea what your disptable functions look like but...

while psource:
c, psource = psource[0], psource[1:]


I don't think this will work. Slicing creates a hard copy of the rest of 
the string. Performance is going to be n-squared.


(I tried a mock of this line, working with a duplicate of the data; the 
time to process a 600-line module doubled. I'm still waiting on the 6MB 
data data, and it's been seven minutes so far; it normally takes 7 seconds.


I was surprised at one time that slices don't create 'views', but I've 
since implemented view-slices and I can appreciate the problems.)


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


Re: Convert list to another form but providing same information

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 11:31 AM, Ben Bacarisse  wrote:
> However, the explanation ("because Python’s syntactic framework can't
> handle statements nested inside expressions") seemed, at first, to be
> saying you can't because you can't!  But the term "syntactic framework"
> hints that it's not really just an arbitrary choice -- that this is
> something about the way Python is parsed that make this choice
> inevitable.  Is it to do with the way that indentation has a syntactic
> role?
>
> To phrase my question in terms of syntax, why is the : in lambda
> followed by a test (the top-level production for an expression in the
> grammar) but the : in a function definition is followed by a suite?  I
> expected them to be both a suite, but presumably something goes horribly
> wrong if that were done.  Now I'm wondering what.

A "suite" is a series of statements - in common parlance, a block of
code. A "test" is a type of expression (there are a variety of these
kind of things, and they define operator precedence).

Part of the trickiness of permitting statements inside expressions is
that it would get confusing - not that it'd be truly ambiguous and
impossible to parse, but that small errors could result in peculiar
misinterpretations of subsequent code, which means the error messages
would be harder to understand. There are already a few places like
that:

functioncall( # ) # oops, forgot the close parenthesis
name = value
othername = othervalue

You'll get a SyntaxError on the last line, despite it being
functionally identical to the second. And it's just "invalid syntax",
the most completely unhelpful message. (This situation is considered
worthwhile, though. Named arguments and assignment both make way too
much sense to break one of them.) Now imagine if every "lambda" in the
code could result in the same kind of thing; currently, the keyword
"lambda" must be followed by a (possibly empty) argument list and then
an expression, which must either be entirely within one line, or have
parentheses around it. If, instead, "lambda x:" introduced a suite,
you'd have errors coming up much further down.

Having full statement syntax inside an expression isn't always even a
benefit. It's often a major cost to readability - just have a look at
a big pile of JavaScript callback code some time. (Although, to be
fair, function() {...} isn't the only readability problem there.) And
it has a debugging cost that gets significantly higher when you don't
have braces to delimit blocks.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread BartC

On 22/03/2016 00:42, Mark Lawrence wrote:

On 22/03/2016 00:18, BartC wrote:

On 21/03/2016 23:50, Terry Reedy wrote:

On 3/21/2016 8:43 AM, BartC wrote:


This tests highlights the benefits of an O(1) switch statement.


So why are you not taking advantage of Python's O(1) dict lookup?


I've already reported using a list lookup which is also O(1), and it
didn't really help. I doubt a dict lookup is going to be faster (but
Python has surprised me before).


Please explain how you managed to make the list lookup O(1).


It's not a 'lookup' in the sense of searching. It's just normal 
indexing. The 'key' will be an ASCII code 0 to 127.


--
Bartv

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Tue, 22 Mar 2016 06:43 am, BartC wrote:

> This code was adapted from a program that used:
> 
> readstrfile(filename)
> 
> which either returned the contents of the file as a string, or 0.

What an interesting function. And I don't mean that in a good way.

So if it returns 0, how do you know what the problem is? Mistyped file name?
Permission denied? File doesn't actually exist? Disk corruption and you
can't open the file? Some weird OS problem where you can't *close* the
file? (That can actually happen, although it's never happened to me.) How
do you debug any problems, given only "0" as a result?

What happens if you read (let's say) a 20GB Blue-Ray disk image?

Such a function is good only for the quickest and dirtiest of quick and
dirty scripts. But if you're going to be quick and dirty, in Python you
might as well be even quicker and dirtier:

open(filename).read()


and just don't bother to do any error handling at all. If there's a problem,
you'll get a traceback and the program will halt.


> That's all. My Python version was thrown together as I don't know if
> there's a similar function to do the same.
> 
> If you want to talk about Pythonic, I don't see why that file API
> doesn't count (the original is buried in a library).

What library? Who wrote it?

 
> Or does Pythonic mean bristling with exceptions and classes and what-not?

Not everything written in Python is Pythonic. In fact, there's really no
objective definition of "Pythonic". It's like art, "I'll know it when I see
it". It means elegant use of Python idioms, but people can and do argue
over what's elegant and idiomatic.

This is no different from any other language. How do you know the difference
between good, idiomatic C code, and poor C code written by people who don't
know the language?


You can start to get an idea of Pythonic by reading the Zen of Python:

import this


but beware that, like most koans, the Zen is deliberately contradictory, and
contains at least one subtle joke.

"Pythonic" code should not be "too clever", just clever enough. If it is
hard to understand, it's probably not Pythonic. If it slavishly follows
recipes and practices from other languages, it's probably not Pythonic.

Code which does a lot of clever bit-twiddling to optimise arithmetic (as
seen often in C code) is probably not Pythonic, because in Python the
performance difference between `x << 1` and `x*2` is probably
insignificant. So write what is most clear, not what you write in C code.

Code which has dozens of classes like Java is usually not Pythonic. See:

http://dirtsimple.org/2004/12/python-is-not-java.html

http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html


If you're writing code to solve problems that don't occur in Python, then
your code is not Pythonic.

Pythonic code probably uses a lot of iterables:

for value in something:
...


in preference to Pascal code written in Python:

for index in range(len(something)):
value = something[index]
...


or worse:

index = 0
while index < len(something):
value = something[index]
...
index += 1



(I don't know where that while-loop idiom comes from. C? Assembly? Penitent
monks living in hair shirts in the desert and flogging themselves with
chains every single night to mortify the accursed flesh? But I'm seeing it
a lot in code written by beginners. I presume somebody, or some book, is
teaching it to them. "Learn Python The Hard Way" perhaps?)


Pythonic code tends to reduce boilercode, not increase it. One line for
the "for value" loop; two lines for the Pascal version; four lines for the
hairshirt version using while.

But on the other hand, Pythonic code doesn't try too hard to minimise the
number of lines of code. If you find yourself asking "how do I write this
as one line", and the answer is some awful tricky code, then it probably
isn't Pythonic. If it takes two lines, it takes two lines, and not
everything needs to be a one-liner.

Perl one-liners are not Pythonic.


Really, the only way to learn what is and isn't Pythonic is to read and
write so much Python code that you learn what's elegant, idiomatic,
Pythonic code.

Unfortunately, real world code, which has to deal with errors and exceptions
and optimizations, is rarely Pythonic. But if you start with something like
the Python standard library, close one eye and squint, you can usually see
the Pythonic code peeking out from behind the error checking and
optimizations.


-- 
Steven

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


Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread Wildman via Python-list
I have a gui that has text widget and I want to be able to
copy to the clipboard the text that is highlighted or the
text widget's entire contents if no text is highlighted.
This line of code works for the highlighted text:

text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)

However, this code will generate an exception if no text
is highlighted.  So here is what I come up with and it
works:

def copy_clipboard(self):
try:
text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
except:
text2copy = self.text.get()
root.clipboard_clear()
root.clipboard_append(text2copy)

My concern is whether or not this approach is acceptable.
Is it ok to let the exception occur or would it be better
to avoid it?  If the later, I would appreciate suggestions
on how to do that, I mean how to determine if any text is
highlighted without generating an exception.  My research
was not very fruitful.

-- 
 GNU/Linux user #557453
May the Source be with you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Convert list to another form but providing same information

2016-03-21 Thread Paul Rubin
Maurice  writes:
> I have a list such [6,19,19,21,21,21]
> Therefore the resulting list should be:
> [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]

Rather than a sparse list you'd typically want a dictionary (untested):

  from collections import defaultdict
  the_list = [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0]
  ...
  days = defaultdict(int)
  for t in the_list:
 days[t] += 1

this results in days being the defaultdict { 6:1, 19:2, 21:3 }.

defaultdict is a special type of dictionary where if you try to access a
non-existent element, it gets created and initialized with the data
constructor you made it with.  In this case the data constructor is int,
and int() makes the number 0, so the defaultdict elements are
initialized to 0.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
 wrote:
> I have a gui that has text widget and I want to be able to
> copy to the clipboard the text that is highlighted or the
> text widget's entire contents if no text is highlighted.

Fortunately your code reveals that you're using "tk" here, but
otherwise, please state up-front which GUI library you're using; there
are quite a few.

> This line of code works for the highlighted text:
>
> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>
> However, this code will generate an exception if no text
> is highlighted.  So here is what I come up with and it
> works:
>
> def copy_clipboard(self):
> try:
> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
> except:
> text2copy = self.text.get()
> root.clipboard_clear()
> root.clipboard_append(text2copy)
>
> My concern is whether or not this approach is acceptable.
> Is it ok to let the exception occur or would it be better
> to avoid it?  If the later, I would appreciate suggestions
> on how to do that, I mean how to determine if any text is
> highlighted without generating an exception.  My research
> was not very fruitful.

You're trying to do one of two things:

1) Copy the selected text to the clipboard
2) If there isn't any, copy all the text.

So I would say yes, the basic layout of try/except to get the text is
perfect. However, DON'T use a bare "except:" clause. You'll get back a
specific exception; catch that instead. Other than that, sure, your
code looks fine.

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Mark Lawrence

On 22/03/2016 00:49, BartC wrote:


I was surprised at one time that slices don't create 'views', but I've
since implemented view-slices and I can appreciate the problems.)



Why, the docs are quite clear on how Python works?  Of course you can 
always use memoryviews 
https://docs.python.org/3/library/stdtypes.html#typememoryview


--
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: Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread MRAB

On 2016-03-22 01:47, Chris Angelico wrote:

On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
 wrote:

I have a gui that has text widget and I want to be able to
copy to the clipboard the text that is highlighted or the
text widget's entire contents if no text is highlighted.


Fortunately your code reveals that you're using "tk" here, but
otherwise, please state up-front which GUI library you're using; there
are quite a few.


This line of code works for the highlighted text:

text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)

However, this code will generate an exception if no text
is highlighted.  So here is what I come up with and it
works:

def copy_clipboard(self):
try:
text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
except:
text2copy = self.text.get()
root.clipboard_clear()
root.clipboard_append(text2copy)

My concern is whether or not this approach is acceptable.
Is it ok to let the exception occur or would it be better
to avoid it?  If the later, I would appreciate suggestions
on how to do that, I mean how to determine if any text is
highlighted without generating an exception.  My research
was not very fruitful.


You're trying to do one of two things:

1) Copy the selected text to the clipboard
2) If there isn't any, copy all the text.

So I would say yes, the basic layout of try/except to get the text is
perfect. However, DON'T use a bare "except:" clause. You'll get back a
specific exception; catch that instead. Other than that, sure, your
code looks fine.


It'll raise TclError.

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


Re: crash while using PyCharm / Python3

2016-03-21 Thread Jonathan N. Little

Adam wrote:

"Adam"  wrote in message
news:ncprqb$tl9$1...@news.albasani.net...


"Jonathan N. Little"  wrote in message
news:ncpjj0$7ug$1...@dont-email.me...

Adam wrote:

There ought to be a way to just reinstall the graphics subsystem rather
than
an all-or-none installation approach.


Yes you can. Did it for a borked install of the nVidia driver. reference
this:




Thanks, even after doing the following...

Problem: Need to purge -fglrx

Typically, the following manual commands will properly uninstall -fglrx:

  $ sudo apt-get remove --purge xorg-driver-fglrx fglrx*
  $ sudo apt-get install --reinstall libgl1-mesa-glx libgl1-mesa-dri
xserver-xorg-core
  $ sudo dpkg-reconfigure xserver-xorg

I still get that dreaded "The system is running in low-graphics mode"
error.
And, recovery mode failsafeX and Ctrl+Alt+F1 hangs with the following...


Initializing built-in extension MIT-SCREEN-SAVER
Initializing built-in extension DOUBLE-BUFFER
Initializing built-in extension RECORD
Initializing built-in extension DPMS
Initializing built-in extension Present
Initializing built-in extension DRI3
Initializing built-in extension X-Resource
Initializing built-in extension XVideo
Initializing built-in extension XVideo-MotionCompensation
Initializing built-in extension SELinux
Initializing built-in extension XFree86-VidModeExtension
Initializing built-in extension XFree86-DGA
Initializing built-in extension XFree86-DRI
Initializing built-in extension DRI2
Loading extension GLX




After trying the following...

http://askubuntu.com/questions/577093/how-to-install-gnome-desktop

$ sudo apt-get update
$ sudo apt-get install gnome-shell


http://tipsonubuntu.com/2014/06/06/change-display-manager-ubuntu-14-04/

$ sudo dpkg-reconfigure lightdm


Not sure about ATI, but when you purge the nVidia driver it takes Unity 
with it so I had to reinstall unity-desktop afterwards. If you want 
lightdm but are now using gdm, maybe it happened because unity-desktop 
was uninstalled and you just installed gnome-shell, your can reset:


sudo dpkg-reconfigure gdm

and select lightdm from the list.


--
Take care,

Jonathan
---
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
--
https://mail.python.org/mailman/listinfo/python-list


Re: crash while using PyCharm / Python3

2016-03-21 Thread Jonathan N. Little

Adam wrote:

Sure glad I did not reinstall Ubuntu.  Whew!!


Unless you really-really-really screw things up, you usually do not have 
to. Linux is not Windows ;-) Even if you had to, with /home on its own 
partition a reinstall would not have been an issue, you just have to 
choose the "Something Else" on partitioning and only format the "/" and 
set the "/home" partition mount point and NOT FORMAT it.


A tip: If you use gparted or e2label and give partitions labels. Makes 
moving partitions and data easier...


sda1 "12.04-root"
sda5 "old-home"

sdb1 "14.04-root"
sdb5 "new-home"

So from a live session when I mount both drives it is easy to keep 
things straight when I copy my profiles from old drive on sda to new 
drive with newer version of Ubuntu on sdb...


--
Take care,

Jonathan
---
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com
--
https://mail.python.org/mailman/listinfo/python-list


Re: Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread Wildman via Python-list
On Tue, 22 Mar 2016 02:01:53 +, MRAB wrote:

> On 2016-03-22 01:47, Chris Angelico wrote:
>> On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
>>  wrote:
>>> I have a gui that has text widget and I want to be able to
>>> copy to the clipboard the text that is highlighted or the
>>> text widget's entire contents if no text is highlighted.
>>
>> Fortunately your code reveals that you're using "tk" here, but
>> otherwise, please state up-front which GUI library you're using; there
>> are quite a few.
>>
>>> This line of code works for the highlighted text:
>>>
>>> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>>>
>>> However, this code will generate an exception if no text
>>> is highlighted.  So here is what I come up with and it
>>> works:
>>>
>>> def copy_clipboard(self):
>>> try:
>>> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>>> except:
>>> text2copy = self.text.get()
>>> root.clipboard_clear()
>>> root.clipboard_append(text2copy)
>>>
>>> My concern is whether or not this approach is acceptable.
>>> Is it ok to let the exception occur or would it be better
>>> to avoid it?  If the later, I would appreciate suggestions
>>> on how to do that, I mean how to determine if any text is
>>> highlighted without generating an exception.  My research
>>> was not very fruitful.
>>
>> You're trying to do one of two things:
>>
>> 1) Copy the selected text to the clipboard
>> 2) If there isn't any, copy all the text.
>>
>> So I would say yes, the basic layout of try/except to get the text is
>> perfect. However, DON'T use a bare "except:" clause. You'll get back a
>> specific exception; catch that instead. Other than that, sure, your
>> code looks fine.
>>
> It'll raise TclError.

Thanks, that saved me some research time.

-- 
 GNU/Linux user #557453
"It is a dangerous notion that we need a
government to protect us from ourselves."
  -Ron Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread Wildman via Python-list
On Tue, 22 Mar 2016 12:47:11 +1100, Chris Angelico wrote:

> On Tue, Mar 22, 2016 at 12:24 PM, Wildman via Python-list
>  wrote:
>> I have a gui that has text widget and I want to be able to
>> copy to the clipboard the text that is highlighted or the
>> text widget's entire contents if no text is highlighted.
> 
> Fortunately your code reveals that you're using "tk" here, but
> otherwise, please state up-front which GUI library you're using; there
> are quite a few.

Noted.

>> This line of code works for the highlighted text:
>>
>> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>>
>> However, this code will generate an exception if no text
>> is highlighted.  So here is what I come up with and it
>> works:
>>
>> def copy_clipboard(self):
>> try:
>> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
>> except:
>> text2copy = self.text.get()
>> root.clipboard_clear()
>> root.clipboard_append(text2copy)
>>
>> My concern is whether or not this approach is acceptable.
>> Is it ok to let the exception occur or would it be better
>> to avoid it?  If the later, I would appreciate suggestions
>> on how to do that, I mean how to determine if any text is
>> highlighted without generating an exception.  My research
>> was not very fruitful.
> 
> You're trying to do one of two things:
> 
> 1) Copy the selected text to the clipboard
> 2) If there isn't any, copy all the text.

Yes, that is exactly it.

> So I would say yes, the basic layout of try/except to get the text is
> perfect. However, DON'T use a bare "except:" clause. You'll get back a
> specific exception; catch that instead. Other than that, sure, your
> code looks fine.
> 
> ChrisA

Thanks.  I changed the code as you (and MRAB) suggested.

def copy_clipboard(self):
try:
text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
except tk.TclError:
text2copy = self.text.get()
root.clipboard_clear()
root.clipboard_append(text2copy)

Works perfectly.  Thanks again.

-- 
 GNU/Linux user #557453
"Our country's founders cherished liberty, not democracy."
  -Ron Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Copy To Clipboard Highlighted Text From Text Widget

2016-03-21 Thread Chris Angelico
On Tue, Mar 22, 2016 at 2:36 PM, Wildman via Python-list
 wrote:
> Thanks.  I changed the code as you (and MRAB) suggested.
>
> def copy_clipboard(self):
> try:
> text2copy = self.text.get(tk.SEL_FIRST, tk.SEL_LAST)
> except tk.TclError:
> text2copy = self.text.get()
> root.clipboard_clear()
> root.clipboard_append(text2copy)
>
> Works perfectly.  Thanks again.

Looks good!

For future reference, and for others reading, this was a great
question. You posted actual code, with a specific question, and it was
easy to figure out what's going on. Thank you.

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


GAPI -- Sharing a post to Social Networking Pages from my App

2016-03-21 Thread Karthik Reddy
Hi Experts,

I am trying to post on facebook and google plus  page  from my application. I 
am using facebook-sdk an d I am able to post using local machine but I am not 
able to post from dev server.

Can Anyone Please help me on this.


Thanks,
Karthik

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


Re: GAPI -- Sharing a post to Social Networking Pages from my App

2016-03-21 Thread Mark Lawrence

On 22/03/2016 04:14, Karthik Reddy wrote:

Hi Experts,

I am trying to post on facebook and google plus  page  from my application. I 
am using facebook-sdk an d I am able to post using local machine but I am not 
able to post from dev server.

Can Anyone Please help me on this.

Thanks,
Karthik



Please state your OS and Python version, the code that you've tried and 
exactly what went wrong, including the full traceback if there is one.


--
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: Convert list to another form but providing same information

2016-03-21 Thread Steven D'Aprano
On Tuesday 22 March 2016 11:31, Ben Bacarisse wrote:

> Ian Kelly  writes:
> 
>> On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse 
>> wrote:
>>> For experts here: why can't I write a lambda that has a statement in it
>>> (actually I wanted two: lambda l, i: l[i] += 1; return l)?
>>
>> https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-
contain-statements
> 
> Thanks.  That makes it clear that it's just for syntactic and not
> semantic reasons.
> 
> However, the explanation ("because Python’s syntactic framework can't
> handle statements nested inside expressions") seemed, at first, to be
> saying you can't because you can't!  But the term "syntactic framework"
> hints that it's not really just an arbitrary choice -- that this is
> something about the way Python is parsed that make this choice
> inevitable.  Is it to do with the way that indentation has a syntactic
> role?

Don't ask me for a link, because I'm lazy, but this has been discussed like 
a bazillion times in the past, here, and on the Python-Ideas and Python-Dev 
mailing lists. No offense intended, but if you want definitive statements 
from the core developers, you can google for them :-)

But in a nutshell, there are four reasons for the limitation on Python's 
lambda statement:

- The use of indentation makes it hard to insert arbitrary statements into 
an expression without confusing the parser.

- Guido has a strict rule that Python's syntax must be parsable using a 
relatively simply parser. A LL(1) parser, if I remember correctly. So none 
of the fancy tricks and hard-to-understand syntax rules that (say) C++ uses.

- Given those limitations, nobody has been able to come up with syntax that 
has broad support.

- And besides, quite a large number of people believe that limiting lambda 
to simple, single-expression cases is a feature, not a bug.


Nobody has quite ruled out multi-statement lambda, or code blocks, but after 
20+ years of people trying to find syntax which works but isn't hated, and 
failing, I think it's probably safe to say it isn't going to happen.



-- 
Steve

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


Re: GAPI -- Sharing a post to Social Networking Pages from my App

2016-03-21 Thread Steven D'Aprano
On Tuesday 22 March 2016 15:14, Karthik Reddy wrote:

> Hi Experts,
> 
> I am trying to post on facebook and google plus  page  from my
> application. I am using facebook-sdk an d I am able to post using local
> machine but I am not able to post from dev server.
> 
> Can Anyone Please help me on this.

Certainly. Follow the instructions here for more help:

http://www.sscce.org/

It's written for Java programmers, but it applies to everyone.


-- 
Steve

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


Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)

2016-03-21 Thread Steven D'Aprano
On Tuesday 22 March 2016 11:49, BartC wrote:

[...]
> Ideally there would be a descriptor or handle passed around which
> contains the current state of the tokeniser, and where you stick the
> current token values. But for a speed test, I was worried about
> attribute lookups.

Bart, in my experience, it is very difficult to predict where the 
bottlenecks are in Python without many, many years of experience, and next 
to impossible if you are not familiar with Python. Your intuitions about 
what's fast and what's slow, honed from other languages, are likely to be 
very wrong when in comes to Python.

I applaud you writing different versions of code to try different tactics, 
but you should start from "write the most natural Python code you can" 
*before* you trying guessing what's fast and what's slow.


[...]
>> Looks like you are indexing a 256-element table of functions, using the
>> numeric value of the character/byte as the index... Only to then pass
>> your entire source string along with the character from it to the
>> function.
> 
> No, it passes only a reference to the entire string.

In Python terms, that *is* "the entire string".

The point is, under the hood of the interpreter, *everything* passes along 
references. We rarely draw attention to this fact. Given:

function(1.5, [])

we say "pass the float 1.5 and an empty list to function", not "pass a 
reference to the float 1.5 and a reference to an empty list to a reference 
to the function", because that would get rather annoying quickly. But of 
course the experienced developer would be aware that this is going on behind 
the scenes.

But that's the critical point: it is *behind the scenes*, an implementation 
detail. In Python code, you don't pass "a reference to" 1.5, you pass 1.5. 
Likewise for all other objects. The abstraction is that you pass objects 
around, not references to objects.



-- 
Steve

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


  1   2   >