Re: Decorators (was: Re: I love assert)

2014-11-14 Thread Richard Riehle
Mayank,

Thanks.  I have only been using Python for about four years, so there are
features I have only recently discovered.   Decorators are one of them.  So
far, I encounter other Python users who are also unfamiliar with them.
When I discovered them, I instantly saw how they could be valuable.

Richard Riehle, PhD
Core Faculty, ITU

On Fri, Nov 14, 2014 at 6:36 PM, Mayank Tripathi  wrote:

> Decorators were there in Python 2.4, released in 2005. Not exactly new.
>
> On Sat Nov 15 2014 at 7:51:11 AM Richard Riehle  wrote:
>
>> On Friday, November 14, 2014 2:18:48 PM UTC-8, Marko Rauhamaa wrote:
>> > Richard Riehle :
>> >
>> > > I find that not a lot of Python user really appreciate the power of
>> > > decorators.
>> >
>> > Well, I don't.
>> >
>> > All it means is that I've never seen a use of decorators that has
>> > enhanced the code. Once I "see the light," I'll have no problem changing
>> > my view.
>> >
>> >
>> > Marko
>>
>> Decorators are new in Python, so there are not a lot of people using
>> them.  From my experience with other languages, especially Ada and Eiffel,
>> I enjoy the benefit of assertions (as pre-conditions and post-conditions
>> and invariants) at the specification level (not embedded in the code), so
>> decorators are closer to my other experience.   They bring me closer to the
>> Design by Contract model of Ada and Eiffel.   That is why I was so pleased
>> to see them added to Python.
>>
>> It is true, however, that they are not immediately intutive in Python,
>> but once understood, they are handy IMHO for improving code reliability.
>>  Perhaps I was spoiled by having this capability in some other languages.
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Decorators

2014-11-14 Thread Marko Rauhamaa
Tim Chase :

> And decorators are used pretty regularly in just about every code-base
> that I've touched (I've been programming in Python since early 2004,
> so I've maintained pre-2.4 code without decorators and then brought it
> forward to 2.4 where decorators were usable).

Funny. My experience with Python is about as old, and I have yet to
encounter them in code. I have seen (maybe even used) @staticmethod once
or twice over a decade and then as a magic keyword rather than a
"decorator pattern."


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


Re: Efficient Threading

2014-11-14 Thread Cameron Simpson

On 14Nov2014 18:42, Empty Account  wrote:

I am thinking about writing a load test tool in Python, so I am interested
in how I can create the most concurrent threads/processes with the fewest
OS resources. I would imagine that I/O would need to be non-blocking.

There are a number of options including standard library threading, gevent,
stackless python, cython parallelism etc. Because I am new to Python, I am
unsure on which libraries to choose. I am not really bothered on the tool
chain, just as long as it is Python related (so I'd use PyPy for example).


The locust.io load test tool uses gevent and seems to like it. I do not know 
enough to make a recommendation myself, but I believe they use it for the same 
reasons you have.


Cheers,
Cameron Simpson 

Whatever is not nailed down is mine.  What I can pry loose is not nailed
down. - Collis P. Huntingdon
--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about installing python and modules on Red Hat Linux 6

2014-11-14 Thread Cameron Simpson

On 14Nov2014 19:01, pythonista  wrote:

I am developing a python application as a contractor.
I would like to know if someone can provide me with some insight into the 
problems that then infrastructure team has been having.
The scope of the project was to install python 2.7.8 and 4 modules/site 
packages on a fresh linux build.
The first team failed after almost 3 weeks of work.
Then  they put their star Linux administrator on the task and it took almost a 
week of considerable effort.
I was able to perform the same task on my windows desktop in less than a 
morning.
I cannot get a straight answer as to what the problems are.
They just seemed to be building and rebuilding all of the components from 
scratch.
Can anyone provide me with insight as to the scope what the problem could have 
been?


It depends. If they were trying to interoperate with the vendor supplied 
Python, they could be fighting with the RPM system.


When I do this these days I do two things:

 - fetch and build Python 2.7.8 from source, to install in 
/usr/local/python-2.7.8 (i.e.  use that as the --prefix option for configure).

At this point you could just proceed and use that python, installing inside its 
site-packages as usual. Or you could go a little further and make a virtualenv 
for your project:


 - fetch virtualenv and (blasphemy!) install it in the vendor space using: "python 
setup.py install"

 - create a virtualenv for your project: "/usr/local/python-2.7.8/bin/python2.7 
virtualenv.py /usr/local/venv-2.7.8-your-project-name"

At that point you will have a "bin" directory inside 
/usr/local/venv-2.7.8-your-project-name with "python" and so forth.  Executing 
that "python" runs the python-2.7.8 you built earlier but with all the system 
paths set to install and use the lib tree inside the virtualenv directory.


The nice thing about that is that it is easy to make multiple virtualenvs, 
using distinct collections of additional packages, nicely isolated and easy to 
invoke (just run the "python" from within the appropriate virtualenv tree).


Cheers,
Cameron Simpson 

I just kept it wide-open thinking it would correct itself.
Then I ran out of talent.   - C. Fittipaldi
--
https://mail.python.org/mailman/listinfo/python-list


Re: Question about installing python and modules on Red Hat Linux 6

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 2:01 PM, pythonista
 wrote:
> The scope of the project was to install python 2.7.8 and 4 modules/site 
> packages on a fresh linux build.
>
> The first team failed after almost 3 weeks of work.
>
> Then  they put their star Linux administrator on the task and it took almost 
> a week of considerable effort.
>
> I was able to perform the same task on my windows desktop in less than a 
> morning.
>
> I cannot get a straight answer as to what the problems are.

Well, you're asking the wrong people. The crystal ball manufacturer
around here is horribly shoddy, shipping us faulty optics and devices
with frequent outages. But if you ask the people who did the work,
they should be able to tell you what was happening.

My suspicion here is that the admins were trying to use multiple
different installation methods, and they ran into issues. This
currently can't happen on Windows, as there simply isn't a
system-provided package manager (though that may be changing - soon
Windows people will have all the same advantages AND disadvantages
that everyone else has!), hence you perhaps found it easier. On my
Debian systems, I can use "apt-get install python python-bs4
python-django" and grab a consistent set of packages from the
OS-provided repositories; or alternatively, I can use pip to get
things from the Python-provided repository (PyPI). Trying to use both
can cause issues.

Want an easy solution? Get a recent Python (preferably 3.x, but
otherwise the latest builds of 2.7 should do I think), get pip, get
virtualenv, and then do everything inside a venv with "pip install"
(maybe using requirements.txt to save you the hassle of keying
everything in). You'll find that that "just works", pretty much all
the time. It's perhaps more work than you truly need (I personally
don't usually bother with a venv), but that way you can be sure you're
not trampling all over anything else.

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


Re: Decorators (was: Re: I love assert)

2014-11-14 Thread Tim Chase
On 2014-11-14 18:19, Richard Riehle wrote:
> Decorators are new in Python, so there are not a lot of people
> using them. 

Um...they were introduced in 2.4 which was released in late 2004.  So
they've only been around for about (almost exactly) a decade.  Not
sure that qualifies as "new in Python".

And decorators are used pretty regularly in just about every
code-base that I've touched (I've been programming in Python since
early 2004, so I've maintained pre-2.4 code without decorators and
then brought it forward to 2.4 where decorators were usable).

-tkc



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


Re: I love assert

2014-11-14 Thread Ethan Furman
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 11/14/2014 06:58 PM, Steven D'Aprano wrote:
> Ethan Furman wrote:
>> 
>> My point being:  a safety net that is so easily disabled does not count 
>> (IMHO) as a backup.
> 
> Assertions are not a backup or a safety net. [...]

Would you be happier if I phrased that as: Defensive programming techniques 
that can be unknowingly disabled by the
end-user aren't very helpful?


>>> If your process is so poor that you release code without running it with 
>>> asserts enabled, then assert will not
>>> save you from bugs.
>> 
>> This is one my very few complaints about Python:  running it normally is the 
>> same as running with DEBUG turned
>> on, so the unusual case is remembering to run with asserts turned /off/.
> 
> That's curious. Your complaint is that asserts are too easy to disable, 
> nevertheless it takes a deliberate and
> conscious act to disable them.

And more likely they were disabled because somebody was trying to get more 
performance from the app, not because that
someone thought, "I know, I'll turn asserts off and see how badly I can corrupt 
all my data."

> By default, Python runs assertions. And yet that is your complaint, so I 
> suppose that you would rather asserts
> *didn't* run by default and you needed a deliberate act to run in debug mode.

That is correct.

> That doesn't seem consistent to me.

Because debug mode runs by default, many folks (myself included) simply run 
everything in debug mode.  I have never
even considered, until this very moment, to run my unittests with -O.  And yet 
the very purpose of -O is to
(hopefully) get more performance.  So you end up with folks using asserts 
inappropriately, yet their tests pass.  They
distribute their code.  Somebody has a slower machine, or gobs of data, so they 
decide to use -O.  Asserts are
disabled, something somewhere isn't checked, an error passes silently, the 
wrong thing happens... I'll you use your
imagination for what happens next.

Having debug mode be the default makes it too easy to screw up.

And yes, I am aware of at least one very large, complex, and modular system 
that uses Python, Javascript, CSS, HTML,
and some templating language I don't remember the name of.  It allows for 
third-party module integration.  It checks
third-party code with asserts.  It checks user-input with asserts.

I strongly suspect that if debug mode was not the default, this particular 
abuse wouldn't happen nearly as frequently
as it does.

- --
~Ethan~
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBAgAGBQJUZtIBAAoJENZ7D1rrH75NmJAP/jmW9GhlR2TeThlUoiIaslVU
qKgiUeNplab9rbbIfQigmZaSTh/n+CeSK9pCu5dIOz7m6oBGoqtWJGFIl+E7pAsz
+MWLCDCIzCi4AGslDZ3ZZSW+e1zG2EK2BgNtLuh3EV4fBJq9GSNDfzlsUbXO7qnQ
yO0h41QuFOMuPiXDGkZ979u5OOBoH1JRUjmXhfUOSua0YcJ1dyRxNJKyhsMyUYPn
relMcTyT04rXj/sl3LUFgaO898mV7ieKgB5tBybAg5EKpArzpnAeIgMjcTP8B6YH
f+2n0jyL56oCCA0CO4BWP31unD8v89hBBeVJo0weprS9owq8OAdfyjXdfYMM77sM
oeQjCM8rs7uej17l4joGfvkrAnsa1BQLyhWzk0aeH4xpfMkgLHVnbwRt6v2PvPWK
IxNraSQQm3UjL1o1trLVznxT2+TOnNUucAl0XSLWSnIo9H1NJmYBZdHpmXvfo7R5
qIo1MyrQYLful+iaNI1I+5A3KLrKOduHm6Uqf4mDKXX6hMMbOdT1lHHpuD/fJYWW
jNqOiMpZl3NtxuEJjdLc5mvXvkoTZ9Z188WfFvFNnsL27aGxFfuulAWKc6EpbWgW
+8VoXVVhgs1mcB4UHM5dFrRSG7CgZi+btfHEGympWv74zun9FHHu2hNg+XPDibtS
D5P2/nCwQYFuCgapy+Ko
=QH9H
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fileno() not supported in Python 3.1

2014-11-14 Thread Cameron Simpson

On 14Nov2014 09:51, Ian Kelly  wrote:

On Fri, Nov 14, 2014 at 12:36 AM, Cameron Simpson  wrote:

On 13Nov2014 15:48, satishmlm...@gmail.com  wrote:

import sys
for stream in (sys.stdin, sys.stdout, sys.stderr):
  print(stream.fileno())
io.UnsupportedOperation: fileno

Is there a workaround?


The first workaround that suggests itself it to use a more modern Python.
I've got 3.4.2 here, and it goes: [... works just fine ...]


Why do you think the Python version has anything to do with it?
Python 2.7.6 (default, Mar 22 2014, 22:59:56) [... works just fine ...]
Clearly, in 2.7.6 it also "just works".


Yeah, true.

I think the consensus is now that the OP is using some kind of IDE where the IO 
streams are not connected to terminals or other real OS-level files, but to 
stream abstractions delivering the the IDE display widgetry. Thus no .fileno() 
facility, because there is no OS-level file.


But we have yet to obtain that information from the OP.

Cheers,
Cameron Simpson 

That's about as good as when one of my students left me a note signed
'anon.'--written on personalized stationery.
   - Ayse Sercan 
--
https://mail.python.org/mailman/listinfo/python-list


Question about installing python and modules on Red Hat Linux 6

2014-11-14 Thread pythonista
I am developing a python application as a contractor.

I would like to know if someone can provide me with some insight into the 
problems that then infrastructure team has been having.

The scope of the project was to install python 2.7.8 and 4 modules/site 
packages on a fresh linux build.

The first team failed after almost 3 weeks of work.

Then  they put their star Linux administrator on the task and it took almost a 
week of considerable effort.

I was able to perform the same task on my windows desktop in less than a 
morning.

I cannot get a straight answer as to what the problems are.

They just seemed to be building and rebuilding all of the components from 
scratch.

Can anyone provide me with insight as to the scope what the problem could have 
been?

Thanks

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


Re: I love assert

2014-11-14 Thread Steven D'Aprano
Ethan Furman wrote:

> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
> 
> On 11/14/2014 03:33 AM, Steven D'Aprano wrote:
>> 
>> I agree with Marko in this case. Marko's example of defensive programming
>> is very similar to the one I gave in my essay here:
>> 
>> http://import-that.dreamwidth.org/676.html
>> 
>> You're correct of course that under the circumstances you describe the
>> code will fail. But that is no different from the scenario:
>> 
>> "a new status is added, the code is not modified to handle it, no tests
>> are written to check the code is correct (or if the tests are written,
>> the test suite is not run), and then the code is released at which point
>> it fails"
> 
> I am reminded of a Star Treck episode (Deep Space 9?) in which a Klingon
> is mocking the Federation tech for having three backups, instead of only
> two, to which the Federation tech replies, "When the second one fails, I
> wouldn't want to be without my tertiary backup!"
> 
> My point being:  a safety net that is so easily disabled does not count
> (IMHO) as a backup.

Assertions are not a backup or a safety net. They are development tools,
like functional requirements, unit tests, code review and the use of
version control. Like any tool, you need to use it correctly. Writing
assertions but never actually running them is not correct use of
assertions, just like writing tests but never actually running them is not
correct use of tests.

One wouldn't say that code review by a senior developer before code is
checked into the main branch is pointless because "if the developer doesn't
perform the review, buggy code might be checked in". Well duh.

>> If your process is so poor that you release code without running it with
>> asserts enabled, then assert will not save you from bugs.
> 
> This is one my very few complaints about Python:  running it normally is
> the same as running with DEBUG turned on, so the unusual case is
> remembering to run with asserts turned /off/.

That's curious. Your complaint is that asserts are too easy to disable,
nevertheless it takes a deliberate and conscious act to disable them. By
default, Python runs assertions. And yet that is your complaint, so I
suppose that you would rather asserts *didn't* run by default and you
needed a deliberate act to run in debug mode. That doesn't seem consistent
to me.


>> Assertions are just a tool, not a panacea.
> 
> Right -- and using it is like using a rock when what you need is a
> ball-peen hammer.  ;)

Compared to Eiffel, Python doesn't have a lot of flexibility to assertions.
You either run them all, or you run none of them. But the assertions
themselves can be as narrow or as broad as you like. It's just code, you
can do anything you like in it.


-- 
Steven

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


Re: Decorators (was: Re: I love assert)

2014-11-14 Thread Mayank Tripathi
Decorators were there in Python 2.4, released in 2005. Not exactly new.

On Sat Nov 15 2014 at 7:51:11 AM Richard Riehle  wrote:

> On Friday, November 14, 2014 2:18:48 PM UTC-8, Marko Rauhamaa wrote:
> > Richard Riehle :
> >
> > > I find that not a lot of Python user really appreciate the power of
> > > decorators.
> >
> > Well, I don't.
> >
> > All it means is that I've never seen a use of decorators that has
> > enhanced the code. Once I "see the light," I'll have no problem changing
> > my view.
> >
> >
> > Marko
>
> Decorators are new in Python, so there are not a lot of people using
> them.  From my experience with other languages, especially Ada and Eiffel,
> I enjoy the benefit of assertions (as pre-conditions and post-conditions
> and invariants) at the specification level (not embedded in the code), so
> decorators are closer to my other experience.   They bring me closer to the
> Design by Contract model of Ada and Eiffel.   That is why I was so pleased
> to see them added to Python.
>
> It is true, however, that they are not immediately intutive in Python, but
> once understood, they are handy IMHO for improving code reliability.
>  Perhaps I was spoiled by having this capability in some other languages.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Decorators (was: Re: I love assert)

2014-11-14 Thread Richard Riehle
On Friday, November 14, 2014 2:18:48 PM UTC-8, Marko Rauhamaa wrote:
> Richard Riehle :
> 
> > I find that not a lot of Python user really appreciate the power of
> > decorators.
> 
> Well, I don't.
> 
> All it means is that I've never seen a use of decorators that has
> enhanced the code. Once I "see the light," I'll have no problem changing
> my view.
> 
> 
> Marko

Decorators are new in Python, so there are not a lot of people using them.  
From my experience with other languages, especially Ada and Eiffel, I enjoy the 
benefit of assertions (as pre-conditions and post-conditions and invariants) at 
the specification level (not embedded in the code), so decorators are closer to 
my other experience.   They bring me closer to the Design by Contract model of 
Ada and Eiffel.   That is why I was so pleased to see them added to Python.  

It is true, however, that they are not immediately intutive in Python, but once 
understood, they are handy IMHO for improving code reliability.   Perhaps I was 
spoiled by having this capability in some other languages.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 1:14 PM, Tim Chase
 wrote:
> On 2014-11-15 12:48, Chris Angelico wrote:
>> conn = establish_database_connection()
>> try:
>> do_stuff()
>> finally:
>> conn.rollback()
>
> this sounds suspiciously like you'd never actually commit.  Do you
> mean something like
>
>   conn = establisth_database_connection()
>   try:
> do_stuff(conn)
>   except:
> conn.rollback()
> raise

No; I prefer to have the commit be as close as possible to the code
that needs it done. So in this toy example, the commit would be inside
- maybe deep inside - do_stuff(). After committing, rolling back
wouldn't hurt.

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


Re: I love assert

2014-11-14 Thread Tim Chase
On 2014-11-15 12:48, Chris Angelico wrote:
> conn = establish_database_connection()
> try:
> do_stuff()
> finally:
> conn.rollback()

this sounds suspiciously like you'd never actually commit.  Do you
mean something like

  conn = establisth_database_connection()
  try:
do_stuff(conn)
  except:
conn.rollback()
raise

instead?

I don't generally like bare excepts, but in this case, the exception
just gets re-raised, so I feel Less Bad™ about it (there's still the
case where the rollback call raises some alternate exception,
obfuscating the underlying root issue).

-tkc



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


Re: I love assert

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 12:30 PM, Marko Rauhamaa  wrote:
> Chris Angelico :
>
>> On Sat, Nov 15, 2014 at 11:12 AM, Marko Rauhamaa  wrote:
>>> Most importantly, assertion failures are not supposed to be recovered
>>> from (within the program). Assertion failures can result in the loss
>>> of life and limb. They can result in database corruption. They can
>>> result in monetary losses. They can result in smoke coming out of the
>>> monitor.
>>
>> Or, in theory, AssertionError just prevented any of the above from
>> happening.
>
> I'd advice against catching AssertionError and trying to recover from it
> within the program. You could catch it, log it and reraise it, but since
> the failure modes could be completely unexpected (really, by
> definition), I would move fault-tolerance outside the failing process
> and try to restore a coherent reality from there.

I agree - never catch it. But you should be able to prevent database corruption:

conn = establish_database_connection()
try:
do_stuff()
finally:
conn.rollback()

The raising of AssertionError anywhere inside do_stuff() will prevent
craziness from getting to the database, because it aborts the
execution. (You know, the way a royal pardon does.)

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


Re: I love assert

2014-11-14 Thread Marko Rauhamaa
Chris Angelico :

> On Sat, Nov 15, 2014 at 11:12 AM, Marko Rauhamaa  wrote:
>> Most importantly, assertion failures are not supposed to be recovered
>> from (within the program). Assertion failures can result in the loss
>> of life and limb. They can result in database corruption. They can
>> result in monetary losses. They can result in smoke coming out of the
>> monitor.
>
> Or, in theory, AssertionError just prevented any of the above from
> happening.

I'd advice against catching AssertionError and trying to recover from it
within the program. You could catch it, log it and reraise it, but since
the failure modes could be completely unexpected (really, by
definition), I would move fault-tolerance outside the failing process
and try to restore a coherent reality from there.

Assertion errors are in the same class with CPython bugs, external
signals (say, SIGKILL), security breaches, hardware failures and the
like.

For expected failure modes, other exceptions are a suitable facility.


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


Re: I love assert

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 11:12 AM, Marko Rauhamaa  wrote:
> Most importantly, assertion failures are not supposed to be recovered
> from (within the program). Assertion failures can result in the loss of
> life and limb. They can result in database corruption. They can result
> in monetary losses. They can result in smoke coming out of the monitor.

Or, in theory, AssertionError just prevented any of the above from happening.

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



Re: I love assert

2014-11-14 Thread Marko Rauhamaa
Steven D'Aprano :

> Marko Rauhamaa wrote:
>
>> Asserts are not about notification, checking or optimization. They are
>> about communicating what's going on in the programmer's mind. They are
>> comments.
>
> Assertions can be used for *all of these things*.
>
> Assertions can be used for:
>
> - checking internal program logic;
> - testing pre- and post-conditions;
> - testing program invariants;
> - comments which are verified at runtime;

Most importantly, assertion failures are not supposed to be recovered
from (within the program). Assertion failures can result in the loss of
life and limb. They can result in database corruption. They can result
in monetary losses. They can result in smoke coming out of the monitor.

That's because the programmer is in no way prepared for the assertion to
fail and cannot rely on the assertions to really be checked.

IOW, assertions are not a mechanism for fault-tolerant programming.


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


Re: Array of Functions

2014-11-14 Thread Terry Reedy

On 11/14/2014 5:17 PM, Richard Riehle wrote:

In C, C++, Ada, and functional languages, I can create an array of
functions, albeit with the nastiness of pointers in the C family.
For example, an array of functions where each function is an active
button, or an array of functions that behave like formulae in a
spreadsheet.


Googling 'python spreadsheet', the first hit is
https://manns.github.io/pyspread/
"Pyspread expects Python expressions in its grid cells,"
and expressions can reference other cells.

There may be other relevant programs.


I am finding this a bit challenging in Python.


Since python functions are objects, they easy fit into any array of 
objects. They can also be the values in dicts.  Since strings can be 
passed to built-in runtime compile, eval, and exec functions, (not part 
of standard C), functions can also be represented by a return 
expression, as in Pyspread, or even quoted def statements.


A program that executes user-entered code is much easier in Python than 
in C or any other language where one would have to write or find lexer, 
parser, compiler, and execution functions.



I think my difficulty is related to the REPL nature of Python.


I do not understand this. The interpreter runs a REPL in interactive 
mode but can also run in batch mode.



--
Terry Jan Reedy

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


Re: I love assert

2014-11-14 Thread Steven D'Aprano
Marko Rauhamaa wrote:

> Asserts are not about notification, checking or optimization. They are
> about communicating what's going on in the programmer's mind. They are
> comments.

Assertions can be used for *all of these things*.

Assertions can be used for:

- checking internal program logic;
- testing pre- and post-conditions;
- testing program invariants;
- comments which are verified at runtime;



> So why use asserts instead of comments?

Not all comments can be replaced with an assertion, but some can:

# once we reach here, mylist has at least two items

is better turned into an assert:

assert len(mylist) >= 2


To someone who is fluent in Python, the assertion is as easy to read and
understandable as the English comment. And it has the added benefit that
the interpreter will verify that the comment is correct at runtime.
Verifying comments is important, since programmers are notorious for
ignoring comments and failing to update them:

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

Why would you choose a comment rather than an assertion? If the code is
performance critical, and the extra cost of the assertion is significant,
and you cannot reasonably run the code using -O to disable it, then a
comment might be better.


> Asserts *could* help in fixing bugs:
> 
>   1. An assertion failure immediately, unambiguously, declares even to
>  customer service representatives that this is a terrible bug and
>  should be brought to R&D's attention instead of hazing the poor
>  customer with standard questions ("have you updated?", "have you
>  rebooted?").

If only that were the case :-(


>   2. An assertion failure probably gives the developer an very good clue
>  as to what has gone wrong. There is a good chance of quickly
>  finding an accurate analysis and fix to the bug.

In general, it is desirable to have any eventual exception occur as soon as
possible to the ultimate cause of the exception. Assertions can aid in
that. Languages such as Eiffel provide a rich and powerful set of different
assertions, including:

require
ensure
check
debug
invariant

The Eiffel manual is explicit about the correct use of assertions:

It should be clear from the preceding discussion that contracts 
are not a mechanism to test for special conditions, for example
erroneous user input. For that purpose, the usual control 
structures ( if deposit_sum > 0 then ...) are available, 
complemented in applicable cases by the exception handling 
mechanism reviewed next. An assertion is instead a correctness
condition governing the relationship between two software modules
(not a software module and a human, or a software module and an 
external device). If sum is negative on entry to deposit, 
violating the precondition, the culprit is some other software 
element, whose author was not careful enough to observe the terms 
of the deal. Bluntly:

Rule -- Assertion Violation: A run-time assertion violation is 
the manifestation of a bug.

[...]

That violations indicate bugs explains why it is legitimate to 
enable or disable assertion monitoring through mere compilation 
options: for a correct system -- one without bugs -- assertions 
will always hold, so the compilation option makes no difference 
to the semantics of the system.


https://docs.eiffel.com/book/method/et-design-contract-tm-assertions-and-exceptions



-- 
Steven

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


Re: Array of Functions

2014-11-14 Thread MRAB

On 2014-11-14 22:17, Richard Riehle wrote:

In C, C++, Ada, and functional languages, I can create an array of
functions, albeit with the nastiness of pointers in the C family.
For example, an array of functions where each function is an active
button, or an array of functions that behave like formulae in a
spreadsheet.  I am finding this a bit challenging in Python.

Example:

 r1c1   r1c2  r1c3
 r2c1   r2c2  r2c3
 r3c1   r3c2  r3c3

where r1 is row 1 and c1 is column 1.  Suppose I want an array where
the colum three is a set of functions that operates on the other two
columns, depending on the values I set for those rows and columns?
As noted, I can do this pretty easily in most languages (well, except
for Java which does not support any kind of functional programming
capability), even if I have to use pointers.

I think my difficulty is related to the REPL nature of Python.
However, I am sure some clever Pythonista has found a way to do
this.

Thanks in advance for any suggestions,


In C, you're not creating an array of functions, but an array of
_pointers_ to functions.

In Python, we don't have pointers, but we do have references.

For example, you can define a function:

>>> def my_func():
... print('my_func called')
...

The name of the function is just a reference to that function:

>>> print(my_func)


You call it using (...):

>>> my_func()
my_func called

You can store the reference in a list or bind another name to it:

>>> func_by_another_name = my_func

And call it by that other name:

>>> func_by_another_name()
my_func called

Does that help?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Array of Functions

2014-11-14 Thread Ian Kelly
On Fri, Nov 14, 2014 at 3:17 PM, Richard Riehle  wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, 
> albeit with the nastiness of pointers in the C family.   For example, an 
> array of functions where each function is an active button, or an array of 
> functions that behave like formulae in a spreadsheet.  I am finding this a 
> bit challenging in Python.
>
> Example:
>
> r1c1   r1c2  r1c3
> r2c1   r2c2  r2c3
> r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the 
> colum three is a set of functions that operates on the other two columns, 
> depending on the values I set for those rows and columns?As noted, I can 
> do this pretty easily in most languages (well, except for Java which does not 
> support any kind of functional programming capability), even if I have to use 
> pointers.
>
> I think my difficulty is related to the REPL nature of Python.  However, I am 
> sure some clever Pythonista has found a way to do this.

>>> list_of_functions = [lambda x=x: x + 4 for x in range(3)]
>>> list_of_functions[0]()
4
>>> list_of_functions[1]()
5
>>> list_of_functions[2]()
6

If you want more help, you'll have to be more specific about the
problem you're encountering. Since you didn't go into any detail, I'm
just going to assume that it's the common pitfall of variable scoping
in functions defined in a loop and point you to this FAQ answer:

https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Decorators

2014-11-14 Thread Marko Rauhamaa
Mark Lawrence :

> Perhaps this helps
> http://blog.dscpl.com.au/2014/01/how-you-implemented-your-python.html ?

Thanks, but sorry, it didn't. I couldn't even relate to the supposed
WSGI craze.

I'm yet to face the situation where a colleague would point out, "See
how elegantly you could have done that using a decorator."


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


Re: Array of Functions

2014-11-14 Thread Sam Raker
I second the call for a more concrete implementation, but if you want the 
results of the functions in c3 to be responsive to the values of c1 and c2 
(i.e., if you change r1c1, r1c3 returns a different value), it might be worth 
encapsulating the whole thing in an object and making the c3 functions 
properties, which is Python for 'preprocessed attributes.' 

On Friday, November 14, 2014 5:17:38 PM UTC-5, Richard Riehle wrote:
> In C, C++, Ada, and functional languages, I can create an array of functions, 
> albeit with the nastiness of pointers in the C family.   For example, an 
> array of functions where each function is an active button, or an array of 
> functions that behave like formulae in a spreadsheet.  I am finding this a 
> bit challenging in Python.   
> 
> Example:
> 
> r1c1   r1c2  r1c3
> r2c1   r2c2  r2c3
> r3c1   r3c2  r3c3
> 
> where r1 is row 1 and c1 is column 1.  Suppose I want an array where the 
> colum three is a set of functions that operates on the other two columns, 
> depending on the values I set for those rows and columns?As noted, I can 
> do this pretty easily in most languages (well, except for Java which does not 
> support any kind of functional programming capability), even if I have to use 
> pointers.
> 
> I think my difficulty is related to the REPL nature of Python.  However, I am 
> sure some clever Pythonista has found a way to do this.   
> 
> Thanks in advance for any suggestions,
> 
> Richard Riehle, PhD, International Technological University, San Jose, CA

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


Re: Decorators

2014-11-14 Thread Mark Lawrence

On 14/11/2014 22:18, Marko Rauhamaa wrote:

Richard Riehle :


I find that not a lot of Python user really appreciate the power of
decorators.


Well, I don't.

All it means is that I've never seen a use of decorators that has
enhanced the code. Once I "see the light," I'll have no problem changing
my view.



Perhaps this helps 
http://blog.dscpl.com.au/2014/01/how-you-implemented-your-python.html ?


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

Mark Lawrence

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


Re: Array of Functions

2014-11-14 Thread Marko Rauhamaa
Richard Riehle :

> Example:
>
> r1c1   r1c2  r1c3
> r2c1   r2c2  r2c3
> r3c1   r3c2  r3c3
>
> where r1 is row 1 and c1 is column 1. Suppose I want an array where the
> colum three is a set of functions that operates on the other two
> columns, depending on the values I set for those rows and columns? As
> noted, I can do this pretty easily in most languages (well, except for
> Java which does not support any kind of functional programming
> capability), even if I have to use pointers.

Show some code in, say, Scheme (which is among the most functional
programming languages in existence), and we'll see how we could
translate that into Python.


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


Decorators (was: Re: I love assert)

2014-11-14 Thread Marko Rauhamaa
Richard Riehle :

> I find that not a lot of Python user really appreciate the power of
> decorators.

Well, I don't.

All it means is that I've never seen a use of decorators that has
enhanced the code. Once I "see the light," I'll have no problem changing
my view.


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


Array of Functions

2014-11-14 Thread Richard Riehle
In C, C++, Ada, and functional languages, I can create an array of functions, 
albeit with the nastiness of pointers in the C family.   For example, an array 
of functions where each function is an active button, or an array of functions 
that behave like formulae in a spreadsheet.  I am finding this a bit 
challenging in Python.   

Example:

r1c1   r1c2  r1c3
r2c1   r2c2  r2c3
r3c1   r3c2  r3c3

where r1 is row 1 and c1 is column 1.  Suppose I want an array where the colum 
three is a set of functions that operates on the other two columns, depending 
on the values I set for those rows and columns?As noted, I can do this 
pretty easily in most languages (well, except for Java which does not support 
any kind of functional programming capability), even if I have to use pointers.

I think my difficulty is related to the REPL nature of Python.  However, I am 
sure some clever Pythonista has found a way to do this.   

Thanks in advance for any suggestions,

Richard Riehle, PhD, International Technological University, San Jose, CA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-14 Thread Richard Riehle

On Tuesday, November 11, 2014 11:41:06 AM UTC-8, Peter Cacioppi wrote:
> I get the impression that most Pythonistas aren't as habituated with assert 
> statements as I am. Is that just a misimpression on my part? If not, is there 
> a good reason to assert less with Python than other languages?
> 
> As far as I can tell, Python supports assert perfectly well. When run with 
> the optimization flagging, the asserts are truly removed.
> 
> I think one needs to take care with some basic assert coding - it's not a 
> substitute for unit tests, it doesn't absolve you of normal exception 
> responsibilities, and, most of all, it should be used for passive inspection 
> and not action. But given these guidelines, I still find it very useful as 
> "active comments".
--
Reply & Comment:

I am new to this group, but really like Python.   One of the things I find 
helpful is to combine the assert with the decorator capability to produce code 
that is close to, although not identical to, the Design by Contract features 
built-in to Eiffel and Ada (2012).  I find that not a lot of Python user really 
appreciate the power of decorators.   

Richard Riehle, PhD, International Technological University, San Jose, CA.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two locations for module struct ?

2014-11-14 Thread Terry Reedy

On 11/14/2014 10:11 AM, ast wrote:

Hello

In module wave there is a sub module struct.


struct is not a documented part of the wave module.


You can call function pack() with:

import wave
val = wave.struct.pack(...)


wave imports several other stdlib modules. All are accessible the same 
way.  None are documented or included in wave.__all__.  Some modules, 
typically more recently written or edited, do things like


import struct as _struct

to avoid such leakage of import.  But this is a nuisance to write and 
read.  Best to stick with the documented api.


--
Terry Jan Reedy

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


Re: I love assert

2014-11-14 Thread Marko Rauhamaa
Ethan Furman :

> Python the language is just the opposite: debug mode is on /by
> default/, and to turn it off you have to specify -O:

C's the same way.

When I did Java, we enabled assertions in production code.


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


Re: I love assert

2014-11-14 Thread Ethan Furman
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 11/14/2014 11:12 AM, Marko Rauhamaa wrote:
> Ethan Furman wrote:
> 
>> My point being: a safety net that is so easily disabled does not count 
>> (IMHO) as a backup.
> 
> Correct. You never lean on assertions. They are primarily formal comments.

On this point we can agree.  :)


>> This is one my very few complaints about Python: running it normally is the 
>> same as running with DEBUG turned
>> on, so the unusual case is remembering to run with asserts turned /off/.
> 
> I don't remember ever having optimized them out in any programming language. 
> Has that really improved the 
> performance of your code?

In the handful of languages I have worked with so far, it's the other way 
'round:  you un-optimize debug mode in.
CPython itself is a good example: when configuring, you must add --with-pydebug 
in order to get all the extra checks
added (which is great when adding new features), but that option isn't used 
when making production CPythons; yet
Python the language is just the opposite: debug mode is on /by default/, and to 
turn it off you have to specify -O:

$ ./python -c "print(__debug__)"
True

$ ./python -O -c "print(__debug__)"
False

- --
~Ethan~
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBAgAGBQJUZmjlAAoJENZ7D1rrH75N3wMP/j9AowuuEvW9khTFX512QUHn
VGwEqkIu4FDFRA8T+qzwqhL7st5GGsnXaPvaWCLmPGbtt1qrVxJ1M6LLkuSWI8Fj
/hGe5rcEPP88/tMA5zy/pmg6jDgl96hojCnDmmS6meIQijVRpxMUGQ/4+N1oKI5d
JYKOlhFStk0N5O6gyZ3Yc6UDY+I9kLVPZsIgH0bXx5IkvwQUi1aNboADehDgSbM5
XDCuZvfNsauKyJZL52NkKu6qLSY/CHDx4kaWdP/i75gZPeVJirjEUkcLYSK6iE19
DpkyeR5wetH1yppH27yyfZPCKHnaYD8O/rykRrQLaIYefKb7XhxGWXa4C17w+AQP
3+hjONiPBsXe2FZZNTskKufI88oXbFwTyI3GseeWTIbS+V9TIwGTjZA+V9jc6EvL
T6T9PRGwOzfPolcLZ43x3ea8uCzLhxe0BILEfdmv6XPoPl9sJ5wHhbxg0oblo8GL
GNkfOFw0ncxplyobU0RXhvsKCUg6EpAukWgIcX5IhHfnesoS1BpSMtZubAbY4GVI
AHzL34KpYhOIDo3Ws26dCtHz5ocJSnDlHv2lxv+iEEZV+4lShFEreqfCLK0hcJTP
QMFoI401Dy3bLd3wJX9mYQA+nMGhNtRK+4uYwGSbAinOGDKVuqrmNM7wCaF9lr/I
aC+wno9PzNxkDNy2nUpY
=gJhZ
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help with Python Multiprocessing

2014-11-14 Thread sohcahtoa82
On Thursday, November 13, 2014 3:22:49 PM UTC-8, Anurag wrote:
> On Thursday, November 13, 2014 2:18:50 PM UTC-5, sohca...@gmail.com wrote:
> > On Thursday, November 13, 2014 10:07:56 AM UTC-8, Anurag wrote:
> > > I am having trouble understanding the Multiprocessing module.
> > > I need to run three different files 'Worker1' , 'Worker2', 'Worker3' all 
> > > at once. Currently I am doing this :
> > > 
> > > from multiprocessing import Process
> > > 
> > > import Worker1.py
> > > import Worker2.py
> > > import Worker3.py
> > > 
> > > 
> > > 
> > > p1 = Process(target=Worker1.py) 
> > > p1.start()
> > > p2 = Process(target=Worker2.py)
> > > p2.start()
> > > p3 = Process(target=Worker3.py)
> > > p3.start()
> > > 
> > > But this will only start the 'Worker1'. How do I execute all the three 
> > > files at once?
> > > 
> > > Thanks
> > 
> > Do your WorkerX.py files have a main() function or anything like that?  If 
> > not, they should.  Then, you'd set the targets to WorkerX.main.
> 
> My Worker files have three different functions

What I mean is that your code should probably look more like this:

# Contents of main.py
from multiprocessing import Process
import Worker1
import Worker2
import Worker3

p1 = Process(target=Worker1.main)
p1.start()
p2 = Process(target=Worker2.main)
p2.start()
p3 = Process(target=Worker3.main)
p3.start

# Contents of Worker1.py
def main():
# Do worker1 stuff...


# Contents of Worker2.py
def main():
# Do worker2 stuff...


# Contents of Worker3.py
def main():
# Do worker3 stuff...

Alternatively, you could have a single worker.py, import that, but have 3 
main() functions, main1(), main2(), main3(), and set the targets for each 
process to those functions.

Maybe its because I'm less experienced as most people on this group, but 
setting a Process's target to a module and not a specific function in that 
module seems slightly strange and ambiguous to me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Efficient Threading

2014-11-14 Thread Empty Account
Hi,

I am thinking about writing a load test tool in Python, so I am interested
in how I can create the most concurrent threads/processes with the fewest
OS resources. I would imagine that I/O would need to be non-blocking.

There are a number of options including standard library threading, gevent,
stackless python, cython parallelism etc. Because I am new to Python, I am
unsure on which libraries to choose. I am not really bothered on the tool
chain, just as long as it is Python related (so I'd use PyPy for example).

Many Thanks

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


Re: I love assert

2014-11-14 Thread Marko Rauhamaa
Ethan Furman :

> My point being: a safety net that is so easily disabled does not count
> (IMHO) as a backup.

Correct. You never lean on assertions. They are primarily formal
comments.

However, assertion failures do help in troubleshooting occasionally.
Most importantly, they immediately, unquestionably point the finger at
the developer.

> This is one my very few complaints about Python: running it normally
> is the same as running with DEBUG turned on, so the unusual case is
> remembering to run with asserts turned /off/.

I don't remember ever having optimized them out in any programming
language. Has that really improved the performance of your code?


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


Re: I love assert

2014-11-14 Thread Ethan Furman
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 11/14/2014 03:33 AM, Steven D'Aprano wrote:
> 
> I agree with Marko in this case. Marko's example of defensive programming is 
> very similar to the one I gave in my
> essay here:
> 
> http://import-that.dreamwidth.org/676.html
> 
> You're correct of course that under the circumstances you describe the code 
> will fail. But that is no different
> from the scenario:
> 
> "a new status is added, the code is not modified to handle it, no tests are 
> written to check the code is correct
> (or if the tests are written, the test suite is not run), and then the code 
> is released at which point it fails"

I am reminded of a Star Treck episode (Deep Space 9?) in which a Klingon is 
mocking the Federation tech for having
three backups, instead of only two, to which the Federation tech replies, "When 
the second one fails, I wouldn't want
to be without my tertiary backup!"

My point being:  a safety net that is so easily disabled does not count (IMHO) 
as a backup.


> If your process is so poor that you release code without running it with 
> asserts enabled, then assert will not save
> you from bugs.

This is one my very few complaints about Python:  running it normally is the 
same as running with DEBUG turned on, so
the unusual case is remembering to run with asserts turned /off/.


> Assertions are just a tool, not a panacea.

Right -- and using it is like using a rock when what you need is a ball-peen 
hammer.  ;)

- --
~Ethan~
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)

iQIcBAEBAgAGBQJUZkdzAAoJENZ7D1rrH75Nl9YQALx9UIAxYzlzH04EoMlILErb
0wz5xNhTM2JmF3csJPi3pQSpEr1XpHhWjFZYsFoPB24I8bN7tGALb91+ME7ElHgw
WP6Z+AVPuHCRxTG/oteK72+f5S+SN6zV1ECwrUuWxqIt5CHzYFtQjRKvEinNDtaT
xKtEfYZdrZFeqo7ssV2gHprDd1QFIAygLMbIlwke6nT/TdMGmw/wG9LQtpfSnrQc
4BeFoeYM3OL+hxHUNfYJI9cGL7skykU3EHnuFqNbhA6QhGawvBqUhVDGoFFDkJ/e
AX6AGk5Fu9cvjEFTzRQJzW4S5s6nD4dYM7Z93GfD9G9KBmfzQNM1pBA3yqxf0S8Q
CWTWBs8pPkYe/G1Y/WOn7lzYQdMneIGUNnHR8fnABKPPf5IeTXabkWgtVnq1g5Ty
DVcg/47G8gBMOTz5DD08iigu4f2bsNGowFoAkCDsbNmzjmxODGSUoj1ph/4xg6HP
HbuEQZXwNZXmlP9ZcLoxjCKdBdRJo3JZ4hAf6kVj4VT4y438GL2dI0/KIUSDWgYE
B0mCsQ7WYm80AvfpYR7fJoZsd/5jXe7RCLHanNLIZff3LupdFZri5E246KloUBj+
3Jw3HgwME6wyDshiHtsS5tuDcANvow+PICA+wn2KmU9Mn2JNwsG3D98ZFJfXp0JR
eG/Pg7cgQhrjhFqe6cgO
=EoEX
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two locations for module struct ?

2014-11-14 Thread ast


"Peter Otten" <__pete...@web.de> a écrit dans le message de 
news:mailman.15823.1415983912.18130.python-l...@python.org...




Do you see the pattern? You should ;)



Understood
thx 


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


Re: I love assert

2014-11-14 Thread Ian Kelly
On Fri, Nov 14, 2014 at 4:37 AM, Steven D'Aprano
 wrote:
> Ethan Furman wrote:
>
>>> There's no way to make the CONFUSED status be handled without actually
>>> changing the code. The difference is that this version will not
>>> incorrectly treat CONFUSED as WARNING; it just won't do anything at
>>> all if the code is optimized.
>>
>> So, a different wrong thing, but still a wrong thing.  ;)
>
> And potentially a *worse* wrong thing.

Potentially it's worse, but more likely doing the wrong thing will be
worse than doing nothing.

>"I find it amusing when novice programmers believe their main
> job is preventing programs from crashing. ... More experienced
> programmers realize that correct code is great, code that
> crashes could use improvement, but incorrect code that doesn’t
> crash is a horrible nightmare."
> -- Chris Smith
>
> Assertions can help by this, by causing wrong code to fail as soon as
> possible (provided, of course, that you don't defeat the assertions by
> running the code with assertions disabled).

I fail to see the relevance of this quote. The code I posted that
you're responding to does use an assertion. The case where it does "a
different wrong thing" is precisely the case where assertions are
disabled.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fileno() not supported in Python 3.1

2014-11-14 Thread Ian Kelly
On Fri, Nov 14, 2014 at 12:36 AM, Cameron Simpson  wrote:
> On 13Nov2014 15:48, satishmlm...@gmail.com  wrote:
>>
>> import sys
>> for stream in (sys.stdin, sys.stdout, sys.stderr):
>>   print(stream.fileno())
>>
>>
>> io.UnsupportedOperation: fileno
>>
>> Is there a workaround?
>
>
> The first workaround that suggests itself it to use a more modern Python.
> I've got 3.4.2 here, and it goes:
>
>Python 3.4.2 (default, Nov  5 2014, 21:19:51)
>[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
>Type "help", "copyright", "credits" or "license" for more information.
>>>> import sys
>>>> for stream in (sys.stdin, sys.stdout, sys.stderr):
>...   print(stream.fileno())
>...
>0
>1
>2
>>>>
>
> In short, in 3.4.2 it just works.

Why do you think the Python version has anything to do with it?

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> for stream in (sys.stdin, sys.stdout, sys.stderr):
... print(stream.fileno())
...
0
1
2

Clearly, in 2.7.6 it also "just works".
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python on android: where to start

2014-11-14 Thread Phil Thompson

On 14/11/2014 2:18 pm, maurog wrote:
I looked at the newsgroup, but I didn't find recent infos on this 
topic.

On the other side I went lost by looking for this topic with google. So
I'm asking you my question, if I want to develop or run some code with
python on android, what are the resources to start with?


PyQt4 or PyQt5 with pyqtdeploy...

http://pyqt.sourceforge.net/Docs/pyqtdeploy/

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


Re: Two locations for module struct ?

2014-11-14 Thread Peter Otten
ast wrote:

> In module wave there is a sub module struct.
> You can call function pack() with:
> 
> import wave
> val = wave.struct.pack(...)
> 
> but the same function can be called with:
> 
> import struct
> val = struct.pack(...)
> 
> Is it exactly the same module in both location ?

You can answer that yourself:

>>> import struct, wave
>>> struct is wave.struct
True

> Why putting struct in two places ?

Assumming your code is part of a module called mymodule.py

> import wave
> val = wave.struct.pack(...)

> import struct
> val = struct.pack(...)

you can now also do

import mymodule
val = mymodule.struct.pack(...)

or even

val = mymodule.wave.struct.pack(...)

Do you see the pattern? You should ;)

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


Re: Two locations for module struct ?

2014-11-14 Thread Chris Angelico
On Sat, Nov 15, 2014 at 2:11 AM, ast  wrote:
> In module wave there is a sub module struct.
> You can call function pack() with:
>
> import wave
> val = wave.struct.pack(...)
>
> but the same function can be called with:
>
> import struct
> val = struct.pack(...)
>
> Is it exactly the same module in both location ?
> Why putting struct in two places ?

I don't think that's a submodule of wave. It's just the standard
struct module, which presumably wave imported. Use it as struct, not
as wave.struct.

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


Two locations for module struct ?

2014-11-14 Thread ast

Hello

In module wave there is a sub module struct.
You can call function pack() with:

import wave
val = wave.struct.pack(...)

but the same function can be called with:

import struct
val = struct.pack(...)

Is it exactly the same module in both location ?
Why putting struct in two places ?






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


Re:python on android: where to start

2014-11-14 Thread Dave Angel
maurog  Wrote in message:
> I looked at the newsgroup, but I didn't find recent infos on this topic. 
> On the other side I went lost by looking for this topic with google. So 
> I'm asking you my question, if I want to develop or run some code with 
> python on android, what are the resources to start with?
> 
> Thanks
> 
> mauro
> 

My Google shows two useful sites on the first page.

Qpython.com
Kivy.org

-- 
DaveA

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


Re: python on android: where to start

2014-11-14 Thread Mayank Tripathi
You can try Kivy. http://kivy.org

On Fri Nov 14 2014 at 7:51:08 PM maurog  wrote:

> I looked at the newsgroup, but I didn't find recent infos on this topic.
> On the other side I went lost by looking for this topic with google. So
> I'm asking you my question, if I want to develop or run some code with
> python on android, what are the resources to start with?
>
> Thanks
>
> mauro
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


python on android: where to start

2014-11-14 Thread maurog
I looked at the newsgroup, but I didn't find recent infos on this topic. 
On the other side I went lost by looking for this topic with google. So 
I'm asking you my question, if I want to develop or run some code with 
python on android, what are the resources to start with?

Thanks

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


Authentication method with txjson-rpc.

2014-11-14 Thread ali . hallaji1
Hi,
I want to write authentication by txjson,
Please help me in this way.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Twisted and txJSON-RPC

2014-11-14 Thread ali . hallaji1
Hi,
I want to write authentication with txjson-rpc.
please guide me in this method!
best Regards,
Ali Hallaji
-- 
https://mail.python.org/mailman/listinfo/python-list


Islam prohibited women to be Unveiled....why?

2014-11-14 Thread bv4bv4bv4
Islam prohibited women to be Unveiledwhy?

Are there any harmful effects on women if they used to display parts of their 
body? Let us read.

According to the latest figures, the incidence of melanoma, a potentially fatal 
skin cancer, is increasing dramatically. It is currently the most common type 
of cancer in young women between the ages of 25 and 29.
Researchers believe that sun exposure plays a significant role in the 
development of melanoma. According to Dr. Diane Berson, an American 
dermatologist says "intense sun exposure prior to age 20 may be more of a 
significant risk factor for skin cancer than sun exposure past the age of 20. 
Three or more blistering sunburns early in life, or three or more years of 
working out of doors, (e.g. camp counselors or lifeguards), without protection, 
can increase the risk of skin cancer by more than three times." 
Another study held  from 1973 to 2004 found that the rate of new melanoma cases 
in younger women had jumped 50 percent since 1980, but did not increase for 
younger men in that period.
"It's worrying," said Mark Purdue, a research fellow at the National Cancer 
Institute, who led the analysis published in the Journal of Investigative 
Dermatology. "What we are seeing in young adults right now could foretell a 
much larger number of melanoma cases in older women." he said.
"One possible explanation is increases among young women of recreational sun 
exposure or tanning bed use," Purdue said. "Both of these things have been 
identified as risk factors."
Statistics say that 62,000 melanoma cases are diagnosed each year in the United 
States, and more than 8,400 people die from the disease, according to the 
American Cancer Society. Previous studies have shown that the rate of new 
diagnoses has been increasing among adults overall.
Woman's dress in Islam
According to religion of Islam woman should only display her face and palms of 
hands in front of foreigner men (indoor and outdoor) and more than that is 
prohibited.
Allah Almighty tells prophet Mohamed peace be upon him to order women to do the 
following: (And tell the believing women to lower their gaze (from looking at 
forbidden things), and protect their private parts (from illegal sexual acts) 
and not to show off their adornment except only that which is apparent (like 
both eyes for necessity to see the way, or outer palms of hands or one eye or 
dress like veil, gloves, head-cover, apron, etc.), and to draw their veils all 
over Juyûbihinna (i.e. their bodies, faces, necks and bosoms) and not to reveal 
their adornment except to their husbands, or their fathers, or their husband's 
fathers, or their sons, or their husband's sons, or their brothers or their 
brother's sons, or their sister's sons, or their (Muslim) women (i.e. their 
sisters in Islâm), or the (female) slaves whom their right hands possess, or 
old male servants who lack vigour, or small children who have no sense of 
feminine sex. And let them not stamp their feet so as to reveal what they hide 
of their adornment. And all of you beg Allâh to forgive you all, O believers, 
that you may be successful.){ Sûrat An-Nûr - The Light -verse31}.
Also prophet Mohamed peace be upon him says in the right Hadith about some 
indications for the imminence of day of resurrection that "there will be women 
who are dressed and naked at the same time, who are reeling while walking and 
their heads are like the top of the camel, those will not enter the heaven or 
even smell its smell" [narrated by Abu- horraira]
Muslim women who are veiled believe that their body is valuable so it should 
not be as a commodity that is exposed to everyone to evaluate.
Conclusion
The great religion of Islam doesn't approve any practice that may cause any 
harm to both men and women , therefore Allah Almighty and His prophet Mohamed 
peace be upon him order women to be veiled because woman's veil  definitely 
respects woman than exposing her parts in front of everyone.
Also, Allah Almighty didn't order women to be veiled and ordered men to do what 
they want but He Almighty orders men also to respect women by not to look in 
their parts because that is purer for them so that Allah Almighty tells the 
prophet to inform men to:(Tell the believing men to lower their gaze (from 
looking at forbidden things), and protect their private parts (from illegal 
sexual acts, etc.). That is purer for them. Verily, Allah is All-Aware of what 
they do.){ Sûrat An-Nûr - The Light -verse30}. 
 
--
By: Abduldaem Al-Kaheel
www.kaheel7.com/eng
 
http://www.kaheel7.com/eng/index.php/legislative-miracles/646-islam-prohibited-women-to-be-unveiledwhy
References:
 
*   
http://www.webmd.com/skin-beauty/guide/sun-exposure-skin-cancer
*   
http://www.boston.com/news/nation/washington/articles/2008/07/11/skin_cancer_on_rise_in_young_women/
*   http://www.medicalnewstoday.com/article

Re: fileno() not supported in Python 3.1

2014-11-14 Thread Joel Goldstick
On Fri, Nov 14, 2014 at 8:59 AM, Nobody  wrote:
> On Thu, 13 Nov 2014 15:48:32 -0800, satishmlmlml wrote:
>
>> import sys
>> for stream in (sys.stdin, sys.stdout, sys.stderr):
>>print(stream.fileno())
>>
>>
>> io.UnsupportedOperation: fileno
>>
>> Is there a workaround?
>
> Try:
> sys.stdin.buffer.fileno()
>
> or maybe
>
> sys.stdin.buffer.raw.fileno()
>
> In Python 3.x, sys.stdin isn't actually a "file", it's a TextIOWrapper
> around a BufferedReader around a file (io.FileIO).
>
> TextIOWrapper is responsible for converting a stream of bytes to a stream
> of (Unicode) characters. BufferedReader is responsible for buffering (like
> C stdio).
>
A little off topic, but why not upgrade to 3.4 if you are learning
python.  I run 2.7, but I've read that much has improved in the 3.x
series since 3.1
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: fileno() not supported in Python 3.1

2014-11-14 Thread Nobody
On Thu, 13 Nov 2014 15:48:32 -0800, satishmlmlml wrote:

> import sys 
> for stream in (sys.stdin, sys.stdout, sys.stderr): 
>print(stream.fileno()) 
> 
> 
> io.UnsupportedOperation: fileno 
> 
> Is there a workaround?

Try:
sys.stdin.buffer.fileno()

or maybe

sys.stdin.buffer.raw.fileno()

In Python 3.x, sys.stdin isn't actually a "file", it's a TextIOWrapper
around a BufferedReader around a file (io.FileIO).

TextIOWrapper is responsible for converting a stream of bytes to a stream
of (Unicode) characters. BufferedReader is responsible for buffering (like
C stdio).


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


Re: Python 3.x (beazley): __context__ vs __cause__ attributes in exception handling

2014-11-14 Thread Veek M
It's been answered here:

http://stackoverflow.com/questions/26924045/python-3-x-beazley-context-vs-
cause-attributes-in-exception-handling?noredirect=1#comment42403467_26924045
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: character maps to

2014-11-14 Thread Joel Goldstick
On Fri, Nov 14, 2014 at 6:57 AM,   wrote:
> For 'mimetypes' in the code given below, python is giving the following 
> error. Kindly help.
>
 import os
 matches = []
 for (dirname, dirshere, fileshere) in os.walk(r'C:\Python34'):
> for filename in fileshere:
> if filename.endswith('.py'):
> pathname = os.path.join(dirname, filename)
> if 'mimetypes' in open(pathname).read():
> matches.append(pathname)
>
>
> Traceback (most recent call last):
>   File "", line 5, in 
> if 'mimetypes' in open(pathname).read():
>   File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
> return codecs.charmap_decode(input,self.errors,decoding_table)[0]
> UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: 
> character maps to 


You should understand encodings.  There is lots of literature on the
web about unicode and python.  In your case, the fact that you are
reading a file called cp1252.py and your code is raising a
UnicodeDecodeError should be a clue
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: character maps to

2014-11-14 Thread satishmlmlml
For 'mimetypes' in the code given below, python is giving the following error. 
Kindly help.

>>> import os
>>> matches = []
>>> for (dirname, dirshere, fileshere) in os.walk(r'C:\Python34'):
for filename in fileshere:
if filename.endswith('.py'):
pathname = os.path.join(dirname, filename)
if 'mimetypes' in open(pathname).read():
matches.append(pathname)


Traceback (most recent call last):
  File "", line 5, in 
if 'mimetypes' in open(pathname).read():
  File "C:\Python34\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: 
character maps to 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-14 Thread Steven D'Aprano
Ethan Furman wrote:

>> There's no way to make the CONFUSED status be handled without actually
>> changing the code. The difference is that this version will not
>> incorrectly treat CONFUSED as WARNING; it just won't do anything at
>> all if the code is optimized.
> 
> So, a different wrong thing, but still a wrong thing.  ;)


And potentially a *worse* wrong thing.


   "I find it amusing when novice programmers believe their main 
job is preventing programs from crashing. ... More experienced
programmers realize that correct code is great, code that 
crashes could use improvement, but incorrect code that doesn’t 
crash is a horrible nightmare."
-- Chris Smith


Assertions can help by this, by causing wrong code to fail as soon as
possible (provided, of course, that you don't defeat the assertions by
running the code with assertions disabled).



-- 
Steven

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


Re: io.UnsupportedOperation: fileno

2014-11-14 Thread Thomas Rachel

Am 14.11.2014 00:42 schrieb satishmlm...@gmail.com:

fileno() in not supported. Is it only in 3.1? What is the workaround?


You have been asked many times about the details of your environment.

Especially, you have been told that it is important to know if you 
directly use the Python CLI or some GUI like IDLE.


I just tested the latter thing and found out that it indeed makes a 
difference, as it introduces a replacement for the std* stuff you are 
missing:


>>> sys.stdin

>>> sys.stdout

>>> sys.stderr


Calling .fileno() on such a Pseudo*File just raises an 
UnsupportedOperation exception.


This is in contrast to using the CLI directly.
--
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-14 Thread Steven D'Aprano
Ethan Furman wrote:

> On 11/12/2014 01:41 PM, Marko Rauhamaa wrote:
>>
>> Or I might indicate the exhaustion of possibilities:
>>
>>   if status == OK:
>>   ...
>>   elif status == ERROR:
>>   ...
>>   else:
>>   assert status == WARNING
>>   ...
> 
> And here's a nice example of what one should NOT do.  Imagine that a new
> status, CONFUSED is added, the above code is not modified to handle it,
> and for whatever reason the program is being run optimized -- the assert
> is not there, and CONFUSED is treated the same as WARNING.

I agree with Marko in this case. Marko's example of defensive programming is
very similar to the one I gave in my essay here:

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

You're correct of course that under the circumstances you describe the code
will fail. But that is no different from the scenario:

   "a new status is added, the code is not modified to handle it, 
no tests are written to check the code is correct (or if the 
tests are written, the test suite is not run), and then the 
code is released at which point it fails"

We know that even the best programmer writes buggy code, which is why we
invent processes like defensive programming, design by contract, test
driven development, unit testing and so on. Even so, buggy code gets
written and released. When that happens, it is a failure of process. If
your process is so poor that you release code without running it with
asserts enabled, then assert will not save you from bugs. No process is
immune to sufficiently malicious or incompetent use.

Assertions are just a tool, not a panacea.



-- 
Steven

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


Re: I love assert

2014-11-14 Thread Steven D'Aprano
Peter Cacioppi wrote:

> I get the impression that most Pythonistas aren't as habituated with
> assert statements as I am. Is that just a misimpression on my part? If
> not, is there a good reason to assert less with Python than other
> languages?

I love assert, and use it frequently. But there are good reasons and bad
reasons to use it. I've written an essay about the good reasons to use
assert, e.g. defensive programming, program invariants, checked comments,
design by contract, and when not to use them, e.g. testing arguments to
public interfaces.

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


> As far as I can tell, Python supports assert perfectly well. When run with
> the optimization flagging, the asserts are truly removed.
> 
> I think one needs to take care with some basic assert coding - it's not a
> substitute for unit tests, it doesn't absolve you of normal exception
> responsibilities, and, most of all, it should be used for passive
> inspection and not action. But given these guidelines, I still find it
> very useful as "active comments".

Agreed completely!



-- 
Steven

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


Re: I don't read docs and don't know how to use Google. What does the print function do?

2014-11-14 Thread Chris Angelico
On Fri, Nov 14, 2014 at 9:58 PM, Steven D'Aprano
 wrote:
> http://blog.codinghorror.com/why-cant-programmers-program/
>
> Not everyone agrees that this is a thing:
>
> http://www.skorks.com/2010/10/99-out-of-100-programmers-cant-program-i-call-bullshit/
>
> I'm inclined to accept that maybe 99 out of 100 *applications* for
> programming jobs are from people who can't program, but I don't think that
> 99 out of 100 *programmers* can't program. It's probably more like 65,
> maybe 70 out of 100, tops.

I was talking only about *applications* at that point. From the point
of view of an applicant, the massive noise of incompetent people
trying to get the job means it's really hard to get heard, especially
since a lot of the incompetents can make their resumes look great. [1]
So getting a job you apply for based on your own skill is... not easy.
At least, I'm fairly sure it's not easy. All I know is that it's never
happened to me.

[1] They probably learned from all the other department heads.
http://dilbert.com/strips/comic/2011-02-20/

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


Re: What does zip mean?

2014-11-14 Thread Steven D'Aprano
Grant Edwards wrote:

> What the zipper on a coat does is convert two separate sequences into
> a single sequence where the members alternate between the two input
> sequences.  IOW if we want to do something analogous to a zipper
> fastener it should do this:
> 
> zip([a,b,c,d,e,f],[1,2,3,4,5,6])  => [a,1,b,2,c,3,d,4,e,5,f,6]


While that is correct, the name "zip" for 

zip([a,b,c],[1,2,3])  => [(a,1), (b,2), (c,3)]


is long-established. I generally call the alternate behaviour "interleaving"
or "muxing", derived from multiplexer. While muxing and demuxing is
extremely important in circuit design and telecommunications, I've never
needed it in Python programming.




-- 
Steven

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


Re: I don't read docs and don't know how to use Google. What does the print function do?

2014-11-14 Thread Steven D'Aprano
Chris Angelico wrote:

> There are blog posts out there about how large proportions of
> applicants can't even write simple code on command... and I've taken
> the questions and shown them to my siblings (who protest that they're
> definitely not programmers), proving that a basic smattering of
> mathematical nous puts you above people who are trying to earn money
> from coding.
> 
> It's like a carpenter, looking for a skilled assistant, and getting
> people who don't know which end of a saw to hold.

http://blog.codinghorror.com/why-cant-programmers-program/

Not everyone agrees that this is a thing:

http://www.skorks.com/2010/10/99-out-of-100-programmers-cant-program-i-call-bullshit/

I'm inclined to accept that maybe 99 out of 100 *applications* for
programming jobs are from people who can't program, but I don't think that
99 out of 100 *programmers* can't program. It's probably more like 65,
maybe 70 out of 100, tops.

Ha ha only serious.

More here:

http://www.protocolostomy.com/2010/03/15/programmers-that-cant-program/




-- 
Steven

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


Re: netaddr value back to IP

2014-11-14 Thread Michael Ströder
Noah wrote:
> I am trying to get a value back to IP using the netaddr python module.
> How do I get the value 'ip' back to IP format?  how is it done?
> 
>  snip 
> 
> print IPNetwork(v4_peer_ip).value
> ip = IPNetwork(v4_peer_ip).value + 1
> print ip
> 
> --- snip ---

>>> ip=netaddr.IPAddress('192.168.71.20')
>>> ip+=1
>>> str(ip)
'192.168.71.21'

Ciao, Michael.

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


Re: A Freudian slip of *EPIC PROPORTIONS*!

2014-11-14 Thread KO
On 2014-11-13, Rick Johnson  wrote:

> On the other hand, if the author is not GvR, then he is most
> likely someone of great importance within the community.


As much as I love Python, I hate the amount of appeal to authority
that is present in the Python community.
Ok, GvR created Python, great.
He is extremely smart and knowledgable, no doubt.
But that doesn't make him right on everything.
That doesn't mean his opinion on something makes it True or False.

Gooosh.


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


Re: Bad file descriptor

2014-11-14 Thread Cameron Simpson

On 13Nov2014 15:40, satishmlm...@gmail.com  wrote:

import os
os.write(1, b'Hello descriptor world\n')
OSError: Bad file descriptor

How to give a file descriptor number to this function? How to get a file 
descriptor number?


Wow, this must be at least the 4th post of the same question.
It now occurs to me that a newer Python won't help you.

Are you running this at a command prompt?
Or are you running this in some IDE?

The reason I ask this question is that at a command prompt, the above would 
probably work.


In short, please provide more context. What OS? What version of Python? What 
environment (command prompt, IDE (which one?), a standalone script, etc)?


CHeers,
Cameron Simpson 

Louis Pasteur's theory of germs is ridiculous fiction.
  --Pierre Pachet, Professor of Physiology at Toulouse, 1872
--
https://mail.python.org/mailman/listinfo/python-list