isiter builtin

2014-11-16 Thread Garrett Berg
I have been working with python for a few years now, and two of my favorite
features about it are iterators and duck typing. The fact that every
iterator under the sun can be accessed with a simple for loop is one of the
most amazing features of python.

However, there are times when I want to do type checking, and the builtin
function *isinstance* is of great use. However, this function fails to be
satisfactory in returning whether the object is a valid iterator. The call
hasattr(obj, '__iter__') also fails because str and bytes types both have
that, and are rarely what you mean when you are asking if something is an
iterator (how often have you iterated over a string?)

I propose a new builtin to address this problem. I have created code for it
here:

https://gist.github.com/cloudformdesign/de9b54d30547ddd28ec4

This will allow simple type checking on an input to determine whether it is
an iterator.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: isiter builtin

2014-11-16 Thread Chris Angelico
On Sun, Nov 16, 2014 at 6:57 PM, Garrett Berg  wrote:
> However, there are times when I want to do type checking, and the builtin
> function isinstance is of great use. However, this function fails to be
> satisfactory in returning whether the object is a valid iterator. The call
> hasattr(obj, '__iter__') also fails because str and bytes types both have
> that, and are rarely what you mean when you are asking if something is an
> iterator (how often have you iterated over a string?)

But the correct answer is that str and bytes are both iterable. So
what you're asking is not whether something's a valid iterator, but
whether it is a non-string-iterable, which is a definitely different
thing. And that's basically what your gist is doing, although there's
a slight error in its docstring: you talk about "iterators", but
you're actually looking for "iterables".

In Python, an iterable is something which you can iterate over:
calling iter() on it will return something (rather than raising
TypeError), which means you can use it in a 'for' loop. An iterator is
an iterable which, when you call iter() on it, returns *itself*.
Normally, you get an iterator by calling iter() on something iterable,
if that makes sense. For example:

>>> iter([1,2,3,4])

>>> iter(_)

>>> iter(range(10))

>>> iter(_)

>>> iter(globals())

>>> iter(_)


The list, range, and dict (as returned by globals()) are all iterable;
the ...iterator objects are iterators.

I suspect that your use-case is actually looking for iterables, not
iterators, so the fix is simply a docs change. At that point, your
function becomes 100% correct, and a perfectly idiomatic way to
express "iterable that isn't one of these types". It doesn't need to
be a builtin, though.

Incidentally, if you're doing a lot of isinstance tests like this, you
might want to look at the collections module, which has some 'base
classes' which can be used like that.

>>> isinstance([1,2,3],collections.Iterable)
True

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


Re: isiter builtin

2014-11-16 Thread Ben Finney
Garrett Berg  writes:

> However, there are times when I want to do type checking, and the
> builtin function *isinstance* is of great use.

I would advise that when you think you want type checking, you are
probably being overly restrictive.

> However, this function fails to be satisfactory in returning whether
> the object is a valid iterator. The call hasattr(obj, '__iter__') also
> fails because str and bytes types both have that

Yes, because both ‘str’ and ‘bytes’ objects are valid iterables.


Using ‘isinstance’ in particular is a code smell: it breaks duck typing
(checking not for the object's type, but for how the object behaves,
which is usually what matters) and is almost always the wrong choice.

Not *always* the wrong choice, which is why it's provided, and which is
why I call it a code smell: a strong but not infallible sign something
is wrong with the code as written.

When you reach for ‘isinstance’ (LBYL, which is un-Pythonic), instead
ask yourself: ignoring what type this object is, how do I want it to
behave, and should I just use it in the expectation that it will raise
an exception if it doesn't support that (EAFP, which is very Pythonic)?

-- 
 \ “What is needed is not the will to believe but the will to find |
  `\   out, which is the exact opposite.” —Bertrand Russell, _Free |
_o__)   Thought and Official Propaganda_, 1928 |
Ben Finney

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


Re: caught in the import web again

2014-11-16 Thread Charles T. Smith
On Sun, 16 Nov 2014 08:14:05 +0100, dieter wrote:

> "Charles T. Smith"  writes:
>> Now, I'm getting these errors:
>>
>>   ImportError: cannot import name ...
>>
>> and
>>
>>   AttributeError: 'module' object has no attribute ...
>>
>> (what is 'module'?)
>>
>> Is there a way to resolve this without having to restructure my code
>> every couple of days?
>>
>> I thought using imports of the form:
>>
>>   from module import symbol
>>
>> was the "right way" to avoid these hassles...
> 
> I have not noticed your previous posts regarding import problems --
> thus, I may repeats things already said.
> 
> Usually, Python imports are straight forward and do not lead to
> surprises. There are two exceptions: recursive imports and imports in
> separate threads. Let's look at the problem areas in turn.
> 
> Recursive imports. In this case you have module "A" which imports module
> "B" (maybe indirectly via a sequence of intervening imports) which
> imports module "A". In this, "import" means either "import ..." or "from
> ... import ..." (it does not matter). When module "B" tries to import
> module "A", then "A" is not yet complete (as during the execution of
> "A"'s initialization code, it started to import "B" (maybe indirectly)
> and the following initialization code has not yet been executed). As a
> consequence, you may get "AttributeError" (or "ImportError") when you
> try to access things from "A". To avoid problems like this, try to avoid
> recursive imports. One way to do this, are local imports -- i.e. imports
> inside a function. This way, the import happens when the functions is
> called which (hopefully) happens after module initialization.


Yes, we're talking about recursive imports here.  It's a complex, object-
oriented system with big classes and little classes that are strongly 
interrelated.  I can get the imports configured properly so everything 
works but if I make a little change to the code, then suddenly all my 
imports are broken and I must painstakingly go through the whole 
structure, reorganizing them.

Are others equally frustrated by this or is there a trick or principle 
that I'm missing.  At this point, I guess the way I'll have to proceed is 
to put every class in its own file, no matter how small.  Hopefully that 
takes care of the problem.

I compared perl's object-oriented philosophy with python's and didn't 
like how perl *requires* you to put everything into its own file.  Now it 
looks like python does, too, implicitly.


cts

www.creative-telcom-solutions.de
-- 
https://mail.python.org/mailman/listinfo/python-list


[ANN] Brython 3.0.0 relased

2014-11-16 Thread Pierre Quentel
Hi,

Version 3.0.0 of Brython has been released recently

Brython is an implementation of Python 3 running in the browser, with an 
interface to DOM elements and events. It allows writing web client applications 
with Python instead of Javascript.

Python programs are inserted in the HTML page inside tags 

Re: isiter builtin

2014-11-16 Thread Peter Otten
Garrett Berg wrote:

> I have been working with python for a few years now, and two of my
> favorite features about it are iterators and duck typing. The fact that
> every iterator under the sun can be accessed with a simple for loop is one
> of the most amazing features of python.
> 
> However, there are times when I want to do type checking, and the builtin
> function *isinstance* is of great use. However, this function fails to be
> satisfactory in returning whether the object is a valid iterator. The call
> hasattr(obj, '__iter__') also fails because str and bytes types both have
> that, and are rarely what you mean when you are asking if something is an
> iterator (how often have you iterated over a string?)
> 
> I propose a new builtin to address this problem. I have created code for
> it here:
> 
> https://gist.github.com/cloudformdesign/de9b54d30547ddd28ec4

I see no reason not to inline this short function:

> def isiter(obj, exclude=(str, bytes, bytearray)):
> '''Returns True if object is an iterator.
> Returns False for str, bytes and bytearray objects
> by default'''
> return (False if isinstance(obj, exclude)
> else True if hasattr(obj, '__iter__')
> else False)

Why not

  return hasattr(obj, "__iter__") and not isinstance(obj, exclude)

> This will allow simple type checking on an input to determine whether it
> is an iterator.

You mean "iterable".

I'd like to see a few of your use-cases. I think most the time you don't mix 
arbitrary iterables, so to walk a tree like

["alpha", "beta", [3, "delta", ["epsilon", "zeta"]]]

isinstance(obj, list) would be sufficient. Likewise for

["alpha", iter("abc"), ["beta", ["gamma"]]]

isinstance(obj, str) would work.

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


Re: Efficient Threading

2014-11-16 Thread Marko Rauhamaa
Dan Stromberg :

> On Fri, Nov 14, 2014 at 10:42 AM, 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.
>
> If you need a large amount of concurrency, you might look at Jython
> with threads.  Jython threads well.
>
> If you don't intend to do more than a few hundred concurrent things,
> you might just go with CPython and multiprocessing.

It is very rare to need a "large amount of concurrency." Most hardware
doesn't even support it. The number of CPU cores (plus hyperthreads)
poses a physical limit that cannot be exceeded. Also, the I/O throughput
will almost certainly be more limiting than the CPU.

What I'm getting at is that it is generally not a good idea to represent
a large number of simultaneous operations with an equal number of
threads or processes. While the simplicity of that idea is enticing, it
often leads to an expensive refactoring years down the road (been there,
done that).

Instead, address the true concurrency needs with a group/pool of
processes or threads, and represent your simultaneous contexts with
objects that you map onto the processes or threads. If your application
does not involve obnoxious, blocking library calls (eg, database
access), you might achieve top throughput with a single process (no
threads).

Java had to reinvent their whole stdlib I/O paradigm to address the
scalability problems of the naive zillion-thread approach (NIO). Python
is undergoing a similar transformation (asyncio), although it has always
provided low-level facilities for "doing the right thing."

To summarize, this is how I implement these kinds of applications:

 1. If all I/O is nonblocking (and linux's blocking file I/O doesn't get
in the way), I implement the application single-threaded. In Python,
I use select.epoll(EPOLLET) with callbacks. Python's new asyncio
framework is a portable, funky way to implement the same idea.

 2. If I must deal with blocking I/O calls, I set up a pool of
processes. The size of the pool is calculated from several factors:
the number of CPU cores, network latencies and the server
throughput.

 3. I generally much prefer processes over threads because
they provide for better fault-tolerance.


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


Re: caught in the import web again

2014-11-16 Thread Chris Angelico
On Sun, Nov 16, 2014 at 7:53 PM, Charles T. Smith
 wrote:
> Yes, we're talking about recursive imports here.  It's a complex, object-
> oriented system with big classes and little classes that are strongly
> interrelated.  I can get the imports configured properly so everything
> works but if I make a little change to the code, then suddenly all my
> imports are broken and I must painstakingly go through the whole
> structure, reorganizing them.

So reorganize *once*, such that there are no recursive imports.
Reorganize your code so things don't actually import one another.
Remember, you can pass an instance of some class to an instance of
another class that has no knowledge of the first class - like this:

# file1.py

class Foo:
def some_method(self, some_obj):
some_obj.some_other_method()

# file2.py

class Bar:
def some_other_method(self):
return 42

# main.py

import file1, file2

f=file1.Foo()
b=file2.Bar()
f.some_method(b)


This works just fine, and neither file1 nor file2 needs to import the
other. Recursive imports are almost never necessary.

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


Re: isiter builtin

2014-11-16 Thread Terry Reedy

On 11/16/2014 2:57 AM, Garrett Berg wrote:

(how often have you iterated over a string?)


Often enough, but perhaps more often have written functions for which a 
string is as valid an input as many other iterables.


def cross(iterable, reiterable):
for a in iterable:
for b in reiterable:
yield a, b

print(list(cross('ab', 'cde')))
# [('a', 'c'), ('a', 'd'), ('a', 'e'), ('b', 'c'), ('b', 'd'), ('b', 'e')]

For me, there is no point in excluding such input and output.

(And yes, the code above could be enhanced by at least checking that 
reiterable is *not* an iterator, or by replacing the second arg, renamed 
iterable2, with list(iterable2), but this is besides the point.)


--
Terry Jan Reedy

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


Re: [ANN] Brython 3.0.0 relased

2014-11-16 Thread Marko Rauhamaa
Pierre Quentel :

> Version 3.0.0 of Brython has been released recently

A timely notification! I have just introduced some server-side Python to
my son and was about to start talking about client-side JavaScript.
We'll give Brython a spin, instead.

I'll spread the message at the office as well.


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


Re: Where is inspect() located?

2014-11-16 Thread Albert Visser

On Sun, 16 Nov 2014 05:12:36 +0100, Igor Korot  wrote:


import lib

Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named lib




In the https://docs.python.org/2/library/inspect.html, it says it is
located in Lib/inspect.py.

What am I missing? Or its only for 3.x?

Thank you.


Windows may not be case-sensitive, but Python is iirc

--
Vriendelijke groeten / Kind regards,

Albert Visser

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


Re: Where is inspect() located?

2014-11-16 Thread Chris Angelico
On Sun, Nov 16, 2014 at 9:12 PM, Albert Visser  wrote:
> On Sun, 16 Nov 2014 05:12:36 +0100, Igor Korot  wrote:
>
> import lib
>>
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> ImportError: No module named lib
>
>
>>
>> In the https://docs.python.org/2/library/inspect.html, it says it is
>> located in Lib/inspect.py.
>>
>> What am I missing? Or its only for 3.x?
>>
>> Thank you.
>
>
> Windows may not be case-sensitive, but Python is iirc

And "import lib" would look for Lib/lib.py anyway.

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


Re: How about some syntactic sugar for " __name__ == '__main__' "?

2014-11-16 Thread Vito De Tullio
Steven D'Aprano wrote:

> Chris Kaynor wrote:
> 
>> I was thinking along the lines of replacing:
>> 
>> if __name__ == "__main__":
>> <<>>
>> 
>> with
>> 
>> @main
>> def myFunction()
>> <<<>
>> 
>> Both blocks of code will be called at the same time.
> 
> 
> You can't guarantee that, because you cannot tell ahead of time when the
> "if __name__" statement will be run. It is *usually* at the end of the
> file, but that's just the normal useful convention, it is not a hard
> requirement.


sure you can: simply the main decorator is just something like

def main(myMainFunction):
if myMainFunction.__module__ == '__main__':
myMainFunction()
return myMainFunction




-- 
By ZeD

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


Re: How about some syntactic sugar for " __name__ == '__main__' "?

2014-11-16 Thread Vito De Tullio
Ian Kelly wrote:

>> def main(func):
>> if func.__module__ == "__main__":
>> func()
>> return func # The return could be omitted to block the function from
>> being manually called after import.
>>
>> Just decorate the "main" function of the script with that, and it will be
>> automatically called when ran as a script, but not when imported as a
>> module.
> 
> This calls it at the wrong time, though. Typically the way this idiom
> is used is that you define everything you need (functions, classes,
> etc.) within the main script, and then you call the main function.
> This would call the main function at the time it's defined, when other
> things in the main script may not have been defined yet. One could
> place the main function last, but it would be preferable not to be
> forced.

for the "right time" you can choose to spin a thread and wait to the end of 
the load of the module

something like



from threading import Thread, current_thread

def run_func(func, module_thread):
module_thread.join()
func()

def main(func):
if func.__module__ == '__main__':
Thread(target=run_func, args=[func, current_thread()]).start()

return func


-- 
By ZeD

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


OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread jkn
Hi all
This is a little bit OT for this newsgroup, but I intend to use python 
for prototyping at least, and I know there are a lot of knowledgeable 
people using Python in a Network context here...

I have a use case of a single 'master' machine which will need to 
periodically 'push' data to a variety of 'slave' devices on a small local 
subnet, over Ethernet. We are talking perhaps a dozen devices in all with 
comms occurring perhaps once very few seconds, to much less often - once per 
half an hour, or less. There is probably an upper bound of 64KB or so of 
data that is likely to be sent on each occasion.

Previous similar systems have attempted to do this by maintaining multiple 
long-term TCP connections from the master to all the slave devices. The 
Master is the server and the slaves periodically check the master to see 
what has changed. Although this ... works ..., we have had trouble 
maintaining the connection, for reasons ... I am not yet fully aware of.

We are now considering an alternative approach where the master maintains a 
list of slave devices and acts as a client. Each device is a server from the 
point of view of data transfer. We would then use a short-lived TCP 
connection when data is available; the Master would connect to each slave 
which needed the data, send it, and close the connection.

I should also add that we desire our system to be 'robust' in the face of 
situations such as cable unplugging, device power cycles, etc.

Although this might get round some of our current problems, I can see that 
we might end up with new problems to deal with. I am wondering if this 
scenario rings bells with anyone, and seeking pointers to what has been done 
elsewhere. As I say, I am expecting to prototype it in Python so any 
specifics also welcome!

(suggestions as to a better forum to ask for previous experience also 
gratefully received)

Thanks al lot for any thoughts/suggestions

Jon N

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Chris Angelico
On Sun, Nov 16, 2014 at 11:02 PM, jkn  wrote:
> I have a use case of a single 'master' machine which will need to
> periodically 'push' data to a variety of 'slave' devices on a small local
> subnet, over Ethernet. We are talking perhaps a dozen devices in all with
> comms occurring perhaps once very few seconds, to much less often - once per
> half an hour, or less. There is probably an upper bound of 64KB or so of
> data that is likely to be sent on each occasion.

This is very similar to what a MUD server has to do, with the added
advantage that you're working with ethernet (most MUDs are accessed
through the dangerous waters of the internet, and there are all sorts
of extra problems).

> Previous similar systems have attempted to do this by maintaining multiple
> long-term TCP connections from the master to all the slave devices. The
> Master is the server and the slaves periodically check the master to see
> what has changed. Although this ... works ..., we have had trouble
> maintaining the connection, for reasons ... I am not yet fully aware of.

The most likely case on the internet is that idle connections are
getting dropped, maybe after five minutes. On a LAN, that shouldn't be
happening, but can you run a test and see if maintaining the
connection (by sending dummy packets every minute, perhaps) stops the
dropouts? That might be all you need.

> We are now considering an alternative approach where the master maintains a
> list of slave devices and acts as a client. Each device is a server from the
> point of view of data transfer. We would then use a short-lived TCP
> connection when data is available; the Master would connect to each slave
> which needed the data, send it, and close the connection.

That's also possible. You may find you have a lot of overhead,
percentage-wise, but based on your figures above (a dozen devices, no
more frequent than every few seconds), I very much doubt it'll be a
problem.

> I should also add that we desire our system to be 'robust' in the face of
> situations such as cable unplugging, device power cycles, etc.

Then you want asynchronous connections. If the device at the other end
is down, don't wait for it.

> Although this might get round some of our current problems, I can see that
> we might end up with new problems to deal with. I am wondering if this
> scenario rings bells with anyone, and seeking pointers to what has been done
> elsewhere. As I say, I am expecting to prototype it in Python so any
> specifics also welcome!

Not a problem. I usually do this kind of work in Pike, rather than
Python, so I can't offer ready-to-go Python code for you, but it's
pretty similar. Is there any reason you plan just to _prototype_ this
in Python? Seems to me you could do the whole job in it, no reason to
shift to another language - unless there's a huge aspect to this
project that's computationally intensive or something. The part you've
described is going to be dominated by the network, not the programming
language.

> (suggestions as to a better forum to ask for previous experience also
> gratefully received)

I don't know of any place better. Let's keep it here, at least for the
moment (unless you switch to using Pike, in which case I'll help you
off list...). Here's how I'd do up something like you're describing,
assuming the active-server model:

class Client(object):
def __init__(self, ip):
self.ip = ip
self.sock = None
self.unsent = ""
def send_message(self, message):
self.unsent += message
self.send_queued()
def send_queued(self):
if not self.sock:
# attempt asynchronous establishment of socket
else:
# attempt to send text to the socket, asynchronously

If you're using Python 3.4, you could make use of asyncio to do most
of the work. I've not actually put it to use yet, but it looks
excellent. But the above structure is most of what I'd be looking at.
Establish connections only when you need them, and only if you don't
have them already. (Note that this assumes the other end can separate
messages out, so it's happy to combine them. You may need to make sure
you have some kind of boundary - with text, end-of-line makes good
sense.) You would then create a bunch of Clients, one for each IP you
need to connect to, and send_message() to all appropriate ones. It
should be possible to separate your async comms code from your message
generation code, and keep both of them clean.

In short: What you're doing is certainly possible, and makes some
degree of sense. (Though maintaining connections would be both easier
and simpler, if you can resolve your issues.)

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


Re: [ANN] Brython 3.0.0 relased

2014-11-16 Thread Steven D'Aprano
Pierre Quentel wrote:

> Hi,
> 
> Version 3.0.0 of Brython has been released recently
> 
> Brython is an implementation of Python 3 running in the browser, with an
> interface to DOM elements and events. It allows writing web client
> applications with Python instead of Javascript.

Very nice!



-- 
Steven

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Marko Rauhamaa
jkn :

> Although this ... works ..., we have had trouble maintaining the
> connection, for reasons ... I am not yet fully aware of.

I can see your TCP connections are choppy. Your posting is breaking up.

Seriously, though, there shouldn't be any reason for TCP connections
dropping on their own. You need to have a chat with your IT people.

> We are now considering an alternative approach where the master
> maintains a list of slave devices and acts as a client. Each device is
> a server from the point of view of data transfer. We would then use a
> short-lived TCP connection when data is available; the Master would
> connect to each slave which needed the data, send it, and close the
> connection.
>
> I should also add that we desire our system to be 'robust' in the face
> of situations such as cable unplugging, device power cycles, etc.
>
> Although this might get round some of our current problems, I can see that 
> we might end up with new problems to deal with.

There are numerous ways to accomplish what you desire, and they can all
be made to work.

One simple idea is to use tunneling. IP-over-IP, GRE or OpenVPN
tunneling would not mind the underlying ethernet interfaces going down
occasionally. At worst, there's some IP fragmentation issues, but your
data rates are so low that you wouldn't feel a thing.

> As I say, I am expecting to prototype it in Python so any specifics
> also welcome!

If you set up tunneling, you don't have to change your original working
Python code, and you'd be safe against cable thieves and adventurous IT
administrators.

> (suggestions as to a better forum to ask for previous experience also 

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


Re: encode and decode builtins

2014-11-16 Thread Ned Batchelder

On 11/16/14 2:39 AM, Garrett Berg wrote:

I made the switch to python 3 about two months ago, and I have to say I
love everything about it, /especially/ the change to using only bytes
and str (no more unicode! or... everything is unicode!) As someone who
works with embedded devices, it is great to know what data I am working
with.


I am glad that you are excited about Python 3.  But I'm a little 
surprised to hear your characterization of the changes it brought.  Both 
Python 2 and Python 3 are the same in that they have two types for 
representing strings: one for byte strings, and one for Unicode strings.


The difference is that Python 2 called them str and unicode, with "" 
being a byte string; Python 3 calls them bytes and str, with "" being a 
unicode string.  Also, Python 2 happily converted between them 
implicitly, while Python 3 does not.




However, there are times that I do not care what data I am working with,
and I find myself writing something like:

if isinstance(data, bytes): data = data.decode()



This goes against a fundamental tenet of both Python 2 and 3: you should 
know what data you have, and deal with it properly.



This is tedious and breaks the pythonic method of not caring about what
your input is. If I expect that my input can always be decoded into
valid data, then why do I have to write this?

Instead, I would like to propose to add *encode* and *decode* as
builtins. I have written simple code to demonstrate my desire:

https://gist.github.com/cloudformdesign/d8065a32cdd76d1b3230


If you find these functions useful, by all means use them in your code. 
 BTW: looks to me like you have infinite recursion on lines 9 and 20, 
so that must be a simple oversight.




There may be a few edge cases I am missing, which would all the more
prove my point -- we need a function like this!


You are free to have a function like that.  Getting them added to the 
standard library is extremely unlikely.




Basically, if I expect my data to be a string I can just write:

data = decode(data)

​Which would accomplish two goals: explicitly stating what I expect of
my data, and doing so concisely and cleanly.






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

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Alain Ketterlin
jkn  writes:

> I have a use case of a single 'master' machine which will need to 
> periodically 'push' data to a variety of 'slave' devices on a small local 
> subnet, over Ethernet. We are talking perhaps a dozen devices in all with 
> comms occurring perhaps once very few seconds, to much less often - once per 
> half an hour, or less. There is probably an upper bound of 64KB or so of 
> data that is likely to be sent on each occasion.

OK, no big requirements, but 64K is still way too much to consider UDP.

> Previous similar systems have attempted to do this by maintaining multiple 
> long-term TCP connections from the master to all the slave devices. The 
> Master is the server and the slaves periodically check the master to see 
> what has changed. Although this ... works ..., we have had trouble 
> maintaining the connection, for reasons ... I am not yet fully aware
> of.

This doesn't make much sense to me. On a packet switching network
"maintaining the connexion" simply means keeping both ends alive. There
is nothing "in the middle" that can break the connection (unless you
use, e.g., ssh tunneling, but then it's a configuration problem, or NAT,
but I doubt it is your case on an LAN).

> We are now considering an alternative approach where the master maintains a 
> list of slave devices and acts as a client. Each device is a server from the 
> point of view of data transfer. We would then use a short-lived TCP 
> connection when data is available; the Master would connect to each slave 
> which needed the data, send it, and close the connection.

Yes but a TCP server is slightly more complex: it has to "accept()"
connexions, which means it blocks waiting for something to come in. Or
it has to periodically poll its passive socket, but then you have a
problem with timeouts. Your describing the slaves as "devices" makes me
think they are simple machines, maybe embedded micro-controllers. If it
is the case, you may not want to put the burden on slaves.

(BTW why not have the slaves periodically connect -- as clients -- to
the master? No need to keep the connection open, they can disconnect and
reconnect later. All you need is a way for the master to identify slaves
across several connections, but an IP address should be enough, no?)

> I should also add that we desire our system to be 'robust' in the face of 
> situations such as cable unplugging, device power cycles, etc.

Then avoid connected protocols like TCP. Maybe you should consider SCTP,
which is a message-oriented, reliable protocol. There is a pysctp module
on pypi. (Haven't tried it, I've just made a quick search on SCTP in
Python.)

Or roll your own protocol on top of UDP, but that's another story.

[...]

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 12:43 AM, Alain Ketterlin
 wrote:
> jkn  writes:
>
>> I have a use case of a single 'master' machine which will need to
>> periodically 'push' data to a variety of 'slave' devices on a small local
>> subnet, over Ethernet. We are talking perhaps a dozen devices in all with
>> comms occurring perhaps once very few seconds, to much less often - once per
>> half an hour, or less. There is probably an upper bound of 64KB or so of
>> data that is likely to be sent on each occasion.
>
> OK, no big requirements, but 64K is still way too much to consider UDP.

I wouldn't say "way too much"; the packet limit for UDP is actually
64KB (minus a few bytes of headers). But UDP for anything more than
your network's MTU is inefficient, plus you'd need to roll your own
acknowledgement system so you know when the client got the data, at
which point you're basically recreating TCP.

That said, though: UDP would be a good option, if and only if you can
comply with those two restrictions - sender doesn't care if the odd
packet doesn't get through, and no packet is allowed to exceed 64KB,
with typical packet sizes wanting to be more like 1KB. (DNS servers
usually switch you to TCP if you go above 512 bytes of response, but
that's because DNS responses are usually tiny.) It'd be pretty easy to
knock together a simple UDP system - you have the clients listen on
some particular port, and you could even make use of IP broadcast to
send to everyone all at once, since this is a single LAN.

>> Previous similar systems have attempted to do this by maintaining multiple
>> long-term TCP connections from the master to all the slave devices. The
>> Master is the server and the slaves periodically check the master to see
>> what has changed. Although this ... works ..., we have had trouble
>> maintaining the connection, for reasons ... I am not yet fully aware
>> of.
>
> This doesn't make much sense to me. On a packet switching network
> "maintaining the connexion" simply means keeping both ends alive. There
> is nothing "in the middle" that can break the connection (unless you
> use, e.g., ssh tunneling, but then it's a configuration problem, or NAT,
> but I doubt it is your case on an LAN).

NAT is the most common cause of breakage, but any router does have the
power to monitor and drop connections on any basis it likes. (I can't
imagine any reason a non-NAT router would want to prevent connections
from going idle, but it could be done.) It's also possible the
connections are being dropped at a lower level; for instance, a
wireless network might drop a client and force it to reconnect. I've
seen this happen with a number of cheap home wireless routers, and
generally Linux boxes hang onto any application-level connections just
fine (assuming DHCP doesn't cause a change of IP address at the same
time), but Windows XP (haven't checked more recent Windowses) goes and
closes any sockets that were using that connection... without sending
RST packets to the server, of course. So the client knows it's lost
link, but the server doesn't, until the next time it tries to send (at
which point it *might* get a courteous RST, or it might have to time
out).

So, I wouldn't say it's impossible for the connections to be dying...
but I would say for certain that connection death is diagnosable, and
on a LAN, often quite easily diagnosable.

> Yes but a TCP server is slightly more complex: it has to "accept()"
> connexions, which means it blocks waiting for something to come in. Or
> it has to periodically poll its passive socket, but then you have a
> problem with timeouts. Your describing the slaves as "devices" makes me
> think they are simple machines, maybe embedded micro-controllers. If it
> is the case, you may not want to put the burden on slaves.

You should be able to do an asynchronous accept. I don't know the
details of doing that in Python, but I expect asyncio can do this for
you. In Pike, I quite frequently run a single-threaded program that
just drops to a back-end loop, listening for new connections, new text
on the current connections, or any other events. At the C level, this
would be done with select() and friends. So you shouldn't have to
block, nor poll, though it would require a bit of extra work.

I wouldn't assume that "devices" are microcontrollers, though. I talk
about full-scale computers that way, if they're the "smaller" end of
the connection. For example, I have a video-playing server which
invokes VLC, and it runs a little HTTP daemon to allow end users to
choose what video gets played. The HTTP clients are usually on various
people's desktop computers, which generally have a lot more grunt than
the little video server does; but I call them "devices" in terms of
the Yosemite Project, because all they are is a source of signals like
"play such-and-such file", "pause", "skip forward", "stop".

> (BTW why not have the slaves periodically connect -- as clients -- to
> the master? No need to keep the connec

PySided (PyQted) question

2014-11-16 Thread doob

Help please.
http://veroboard.eu/?qa=26/need-to-convert-qgraphicsitem-to-svg-element-s-in-qt-python
--
https://mail.python.org/mailman/listinfo/python-list


PySide 1.2.2 setMaxWidth and AeroSnap

2014-11-16 Thread Juan Christian
PySide 1.2.2
Python 3.4.2

Code:

from PySide.QtGui import *

class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMinimumSize(600, 700)
self.setMaximumWidth(600)
self.setLayout(QVBoxLayout())

* Call to this module in another module *
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

When I set the "setMaxWidth" I can't use the Windows AeroSnap anymore, why?

I didn't set a "setMaxHeight", so this shouldn't lock me from doing the
snap. Without the "setMaxWidth" the AeroSnap works as usual, but I need to
limit my width to 600, because I need my tool to have this kind of
"short-width, long-height" look.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python on android: where to start

2014-11-16 Thread maurog
Il Fri, 14 Nov 2014 16:48:41 +, Phil Thompson ha scritto:

> 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

Thanks to all of you for yoour answers
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-11-16 Thread Grant Edwards
On 2014-11-16, Steven D'Aprano  wrote:
> Grant Edwards wrote:
>
>> On 2014-11-15, Steven D'Aprano 
>> wrote:
>>> 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.
>>>
>>> A "fresh linux build" of Red Hat Linux 6? RHL 6 was discontinued
>>> in 2000.  That's *at least* 14 years old. Why on earth are you
>>> using something so old instead of a recent version of RHEL, Centos
>>> or Fedora?
>> 
>> I'm sure the OP meant RHEL 6, and not RH 6 [yes, I realize you know
>> that two and are just making a point about how it pays to include
>> accurate info when asking for help.]
>
> Actually, no, the thought didn't even cross my mind. I just assumed
> that if somebody is going to cast aspersions on the professionalism
> of others, they'd get their facts right.

Nah, this is Usenet (from my POV).  Or Google Groups.  Or possibly a
mailing list.  In any case, casting aspersions is often done with
without regard to or even possesion of the facts.  :)

> Assuming it was RHEL 6, then installing Python 2.7 from source as a
> separate application from the system Python should be trivially
> easy, half an hour's work.

Yep.

> Download the source, untar, run ./configure, make, make altinstall
> and you should be done.

I think even "make install" should have worked -- since it will
install in /usr/local, right?

> There may be a few bumps in the road to get optional components
> supported, in which case a skilled Linux admin (which I am not)
> might need perhaps a couple of hours. Depending on just how bad the
> bumps were, an unskilled admin like me might take a day or so, not
> three weeks, before giving up.

All that should be involved is installing the "-devel" versions of a
few libraries -- exactly which ones depend on what features of Python
and the standard libarary you care about.  A few minutes with Google
would have ironed out any questions in that regard.

> The most obvious trap is to run `make install` instead of `make
> altinstall`, in which case congratulations, you've just broken the
> RHEL install, and why didn't you read the README first?

Doesn't 'make install' still install in /usr/local?  (I haven't
manually built built from sources for some time.)  If that's the case,
it shouldn't interfear with the installation in /usr.

But, you're right: anybody who spent more than a half hour on this is
either a flaming incompetent or a scam artist.

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Grant Edwards
On 2014-11-16, jkn  wrote:

> I have a use case of a single 'master' machine which will need to
> periodically 'push' data to a variety of 'slave' devices on a small
> local subnet, over Ethernet. We are talking perhaps a dozen devices
> in all with comms occurring perhaps once very few seconds, to much
> less often - once per half an hour, or less. There is probably an
> upper bound of 64KB or so of data that is likely to be sent on each
> occasion.
>
> Previous similar systems have attempted to do this by maintaining
> multiple long-term TCP connections from the master to all the slave
> devices. The Master is the server and the slaves periodically check
> the master to see what has changed.

Why "check the master"?  Why not just have the master send updates to
the slaves whenever something changes?

> Although this ... works ..., we have had trouble maintaining the
> connection, for reasons ... I am not yet fully aware of.

You need to find out what's wrong and fix it.

> We are now considering an alternative approach where the master
> maintains a list of slave devices and acts as a client. Each device
> is a server from the point of view of data transfer. We would then
> use a short-lived TCP connection when data is available; the Master
> would connect to each slave which needed the data, send it, and
> close the connection.

Why close the connection?  Just leave it open and re-use it the next
time an update needs to be sent.  If the connection has "gone away"
you can simply open a new one.

But, mostly you need to figure out why you can't maintain a TCP
connection between master and slave and fix that problem.

I think the best approach is for the "master" to act as the server.
Clients connect to the server and then wait for updates to be sent
from the server.  If a connection is dropped (e.g. server restarts),
the client should re-establish a new connection.

If there is very little TCP traffic, it can be difficult to detect an
unplugged cable.  The solution for that is either
  
 1) Enable TCP keepalive.  In a no-traffic situation, that will detect
an unplugged cable within an hour or two.

 2) If detecting an unplugged cable needs to happen faster, then add a
heartbeat message to your protocol that gets sent often enough
that you can detect an unplugged cable in within the required
amount of time.

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Grant Edwards
On 2014-11-16, Chris Angelico  wrote:
>
>> OK, no big requirements, but 64K is still way too much to consider UDP.
>
> I wouldn't say "way too much"; the packet limit for UDP is actually
> 64KB (minus a few bytes of headers). But UDP for anything more than
> your network's MTU is inefficient, plus you'd need to roll your own
> acknowledgement system so you know when the client got the data, at
> which point you're basically recreating TCP.
>
> That said, though: UDP would be a good option, if and only if you can
> comply with those two restrictions - sender doesn't care if the odd
> packet doesn't get through, and no packet is allowed to exceed 64KB,
> with typical packet sizes wanting to be more like 1KB. (DNS servers
> usually switch you to TCP if you go above 512 bytes of response, but
> that's because DNS responses are usually tiny.) It'd be pretty easy to
> knock together a simple UDP system - you have the clients listen on
> some particular port, and you could even make use of IP broadcast to
> send to everyone all at once, since this is a single LAN.

If, as the OP seemed to state, this is strictly within a single
network segement (no firewalls or routers involved), then UDP
multicast might be worth considering.  It's not reliable like TCP, but
it does make everything very simple: just decide how many different
logical data streams you are going to have, and allocate a multicast
address for each one.  Have the server multicast new data whenever it
becomes available, and the slaves all register for and and listen to
for whichever data streams they care about.  Note that the "register
for" (or join, or whatever it's called) isn't something the server
knows about, that's purely done by the slave app to inform the slave's
network stack and any intervening routers what multicast streams the
slave app wants to see.

In _theory_, if betwixt the server and client the routers and
firewalls are properly configured, multicast should still work.  But,
the smart money will be giving long odds that they aren't, and it
won't.

One possible advantage of UDP is that is packet oriented, so if your
data isn't message oriented rather than an undifferentiated byte
stream, UDP saves you the effort of having to packetize and
unpacketize the data as you have to via TCP.

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


Re: How about some syntactic sugar for " __name__ == '__main__' "?

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 3:39 AM, Vito De Tullio  wrote:
> for the "right time" you can choose to spin a thread and wait to the end of
> the load of the module

Yuck. "Just add threads" is /not/ the answer to everything.

This case looks fairly harmless on the surface, although I could
imagine it breaking things that might object to the main thread being
changed, e.g. things that use event loops or that rely in some way on
the threading.main_thread() function.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> UDP for anything more than your network's MTU is inefficient

Why do you say it's inefficient?  Sure, the UDP datagram will get 
fragmented and re-assembled at the other end, but it's not like TCP 
would do any better.  One way or another, your data is going to be 
transmitted in packet that fit into the MTU.

> plus you'd need to roll your own acknowledgement system so you know 
> when the client got the data, at which point you're basically 
> recreating TCP.

That I certainly agree with.  UDP should be used for fire-and-forget, 
where its not critical that every bit of data gets through.  A classic 
example is high-volume logging.  Once you start thinking about any kind 
of acknowledgements, you should just switch to TCP.  The alternative is 
that you will slowly end up reinventing TCP (and doing a bad job of it).

> NAT is the most common cause of breakage, but any router does have the
> power to monitor and drop connections on any basis it likes. (I can't
> imagine any reason a non-NAT router would want to prevent connections
> from going idle, but it could be done.)

It's common in corporate networks for routers to forcibly close idle 
connections (i.e. inject RST packets).  Often, the IT security guys 
think idle connections represent some kind of threat (and good luck 
trying to argue with them).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Grant Edwards
On 2014-11-16, Roy Smith  wrote:
> In article ,
>  Chris Angelico  wrote:
>
>> UDP for anything more than your network's MTU is inefficient
>
> Why do you say it's inefficient?  Sure, the UDP datagram will get 
> fragmented and re-assembled at the other end, but it's not like TCP 
> would do any better.  One way or another, your data is going to be 
> transmitted in packet that fit into the MTU.
>
>> plus you'd need to roll your own acknowledgement system so you know 
>> when the client got the data, at which point you're basically 
>> recreating TCP.
>
> That I certainly agree with.  UDP should be used for fire-and-forget, 
> where its not critical that every bit of data gets through.  A classic 
> example is high-volume logging.  Once you start thinking about any kind 
> of acknowledgements, you should just switch to TCP.  The alternative is 
> that you will slowly end up reinventing TCP (and doing a bad job of it).
>
>> NAT is the most common cause of breakage, but any router does have the
>> power to monitor and drop connections on any basis it likes. (I can't
>> imagine any reason a non-NAT router would want to prevent connections
>> from going idle, but it could be done.)
>
> It's common in corporate networks for routers to forcibly close idle 
> connections (i.e. inject RST packets).  Often, the IT security guys 
> think idle connections represent some kind of threat (and good luck 
> trying to argue with them).

According to the OP, the communication is "on a small local subnet,
over Ethernet."  To me that means no router, no firewall: just an
Ethernet switch (or two).

If that is the case, and the master and slaves can't maintain TCP
connectivity, then something is seriously broken in the master or
slaves and needs to get fixed.

What I don't understand is if TCP connections are going away (which
can happen if either end gets restarted, or cables get unplugged, or
switches lose power), why the app doesn't just open a new one and
resume operation?

-- 
Grant

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


Re: PySide 1.2.2 setMaxWidth and AeroSnap

2014-11-16 Thread Vincent Vande Vyvre

Le 16/11/2014 16:51, Juan Christian a écrit :

PySide 1.2.2
Python 3.4.2

Code:

from PySide.QtGui import *

class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMinimumSize(600, 700)
self.setMaximumWidth(600)
self.setLayout(QVBoxLayout())

* Call to this module in another module *
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()

When I set the "setMaxWidth" I can't use the Windows AeroSnap anymore, 
why?


I didn't set a "setMaxHeight", so this shouldn't lock me from doing 
the snap. Without the "setMaxWidth" the AeroSnap works as usual, but I 
need to limit my width to 600, because I need my tool to have this 
kind of "short-width, long-height" look.



No probleme with PyQt but I think this is a window manager question. 
Window, gnome, KDE, Mate, ...



If your widget is a QMainWindow or a QDialog add a size grip:

self.setSizeGripEnabled(True)

The QWidget don't have this method, so, in this case, you must 
reimplement his resizeEvent()


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


Re: PySide 1.2.2 setMaxWidth and AeroSnap

2014-11-16 Thread Juan Christian
On Sun Nov 16 2014 at 3:46:40 PM Vincent Vande Vyvre <
vincent.vande.vy...@telenet.be> wrote:
>
> No probleme with PyQt but I think this is a window manager question.
> Window, gnome, KDE, Mate, ...
>
>
> If your widget is a QMainWindow or a QDialog add a size grip:
>
>  self.setSizeGripEnabled(True)
>
> The QWidget don't have this method, so, in this case, you must
> reimplement his resizeEvent()
>

I changed to QMainWindow, but I don't have a function called
setSizeGripEnabled here.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PySide 1.2.2 setMaxWidth and AeroSnap

2014-11-16 Thread Vincent Vande Vyvre

Le 16/11/2014 19:09, Juan Christian a écrit :
On Sun Nov 16 2014 at 3:46:40 PM Vincent Vande Vyvre 
> wrote:


No probleme with PyQt but I think this is a window manager question.
Window, gnome, KDE, Mate, ...


If your widget is a QMainWindow or a QDialog add a size grip:

 self.setSizeGripEnabled(True)

The QWidget don't have this method, so, in this case, you must
reimplement his resizeEvent()


I changed to QMainWindow, but I don't have a function called 
setSizeGripEnabled here.




... missed in PySide ?

Reimplement the resizeEvent().

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


import math error

2014-11-16 Thread ryguy7272
When I type 'import math', it seems like my Python recognizes this library.  
Great.  When I try to run the following script, I get an error, which suggests 
(to me) the math library is not working correctly.

Script:
import math
def main():
print "This program finds the real solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = math.sqrt(b * b - 4 * a * c)
root1 = (-b + discRoot) / (2 * a)
root2 = (-b - discRoot) / (2 * a)
print
print "The solutions are:", root1, root2
main()

Error:
Traceback (most recent call last):
  File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in 
main()
  File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
a, b, c = input("Please enter the coefficients (a, b, c): ")
  File "", line 1
(1,1,2):
   ^
SyntaxError: unexpected EOF while parsing

The last line is line 11.  It seems like I need this, but that's what's causing 
the error.  Does anyone here have any idea what is wrong here?

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


Error when trying to open an image

2014-11-16 Thread Abdul Abdul
Hello,

I'm trying to open an image using the `Python Imaging Library` as follows:

from PIL import Image
img = Image.open('xyz.jpg')

But, got the following error:

File "C:/Users/abc/PycharmProjects/untitled/open_image.py", line 2, in

from PIL import Image
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 62, in 
from PIL import _imaging as core
ImportError: DLL load failed: %1 is not a valid Win32 application.

Why is that? How can I solve this issue?

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


How modules work in Python

2014-11-16 Thread Abdul Abdul
Hello,

I'm new to Python, and just have a small question, and thought you might
have an idea on it.

I came across the following example that uses the Python Imaging Library
(PIL):

from PIL import Image
img = Image.open('xyz.jpg')

I know that PIL is a module. And, I think that Image is also a module,
especially we are importing it.

I also understood the Image,open() part, as it appears we are using the
method open() from the Image module.

My question is, where did PIL go here? Can a module have another module
inside it?

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


Using map()

2014-11-16 Thread Pavel Volkov

I checked my modules with pylint and saw the following warning:

W: 25,29: Used builtin function 'map' (bad-builtin)

Why is the use of map() discouraged?
It' such a useful thing.
--
https://mail.python.org/mailman/listinfo/python-list


Interrupted system call

2014-11-16 Thread Peter Bell

Hi,

I'm new to Python, using Python3 on RaspberryPi/ArchLinux for a home 
automation project.


I started off with interfacing an LCD screen (serial interface) and have 
this working reasonably well.


Then I received a couple of interface boards.  I attached one of these, 
together with the serial interface for the LCD.


Even without loading (importing) any support libraries for the new 
interface board, the code which was running perfectly well before is now 
producing an error:


=
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/serial/serialposix.py", line 
461, in read

ready,_,_ = select.select([self.fd],[],[], self._timeout)
InterruptedError: [Errno 4] Interrupted system call

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./water.py", line 36, in 
picasso.putstr(strftime("%a, %d %b %Y %X"),1,1)
  File "/root/picasso.py", line 44, in putstr
port.read(1)
  File "/usr/lib/python3.4/site-packages/serial/serialposix.py", line 
480, in read

if e[0] != errno.EAGAIN:
TypeError: 'InterruptedError' object is not subscriptable
=


I'm not sure how to address the 'InterruptedError'.  Can it/should it be 
masked at the user code level with a 'try:' block?  Or is there another 
way to deal with it?


I presume that the subsequent error (TypeError) indicates some fault in 
the system level library.  I'm not sure where/how to report this.


Any advice would be appreciated.

--
---
Peter Bell
Tagum City, Philippines.

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


Re: How modules work in Python

2014-11-16 Thread Joel Goldstick
On Sun, Nov 16, 2014 at 2:36 PM, Abdul Abdul  wrote:
> Hello,
>
> I'm new to Python, and just have a small question, and thought you might
> have an idea on it.
>
> I came across the following example that uses the Python Imaging Library
> (PIL):
>
> from PIL import Image
> img = Image.open('xyz.jpg')
>
> I know that PIL is a module. And, I think that Image is also a module,
> especially we are importing it.
>
> I also understood the Image,open() part, as it appears we are using the
> method open() from the Image module.
>
> My question is, where did PIL go here? Can a module have another module
> inside it?
>
> Thanks.
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

You can learn what PIL has to offer by going into the python
interactive shell, and import PIL .  Then type help(PIL) to learn what
it contains.

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


Re: Using map()

2014-11-16 Thread Joel Goldstick
On Sun, Nov 16, 2014 at 8:01 AM, Pavel Volkov  wrote:
> I checked my modules with pylint and saw the following warning:
>
> W: 25,29: Used builtin function 'map' (bad-builtin)
>
> Why is the use of map() discouraged?
> It' such a useful thing.
> --
> https://mail.python.org/mailman/listinfo/python-list

I think people now favor list comprehensions.

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


import graphics library; causes error

2014-11-16 Thread ryguy7272
These libraries drive me totally nuts.  Sorry, just had to get it out there.
Anyway, I open the cmd window, and typed this: 'easy_install python graphics'.  
So, it starts up and runs/downloads the appropriate library from the web.  I 
get confirmation (in the cmd window) that it finishes, then I try to run this 
script.


# futval_graph2.py
from graphics import *
def main():
  # Introduction
  print "This program plots the growth of a 10-year investment."
  # Get principal and interest rate
  principal = input("Enter the initial principal: ")
  apr = input("Enter the annualized interest rate: ")
  # Create a graphics window with labels on left edge
  win = GraphWin("Investment Growth Chart", 640, 480)
  win.setBackground("white")
  win.setCoords(-1.75,-200, 11.5, 10400)
  Text(Point(-1, 0), '0.0K').draw(win)
  Text(Point(-1, 2500), '2.5K').draw(win)
  Text(Point(-1, 5000), '5.0K').draw(win)
  Text(Point(-1, 7500), '7.5k').draw(win)
  Text(Point(-1, 1), '10.0K').draw(win)
  # Draw bar for initial principal
  bar = Rectangle(Point(0, 0), Point(1, principal))
  bar.setFill("green")
  bar.setWidth(2)
  bar.draw(win)
  # Draw a bar for each subsequent year
  for year in range(1, 11):
principal = principal * (1 + apr)
bar = Rectangle(Point(year, 0), Point(year+1, principal))
bar.setFill("green")
bar.setWidth(2)
bar.draw(win)
  raw_input("Press  to quit.")


After I hit F5, I get this:
Traceback (most recent call last):
  File "C:\Users\Ryan\Desktop\Graphics_Test.py", line 2, in 
from graphics import *
ImportError: No module named graphics

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


RE: Using map()

2014-11-16 Thread Joseph L. Casale
> I checked my modules with pylint and saw the following warning:
>
> W: 25,29: Used builtin function 'map' (bad-builtin)
>
> Why is the use of map() discouraged?
> It' such a useful thing.

The warning manifests from the opinion that a comprehension is
more suitable. You can disable the warning or you can convert your
usage to a comprehension. If you are interested, there is plenty of
info floating around given the above to arm you sufficiently to make
your own mind up.

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


Re: import math error

2014-11-16 Thread Mayank Tripathi
Why the colon in '(1,1,2)'?

On Mon Nov 17 2014 at 1:41:10 AM ryguy7272  wrote:

> When I type 'import math', it seems like my Python recognizes this
> library.  Great.  When I try to run the following script, I get an error,
> which suggests (to me) the math library is not working correctly.
>
> Script:
> import math
> def main():
> print "This program finds the real solutions to a quadratic"
> print
> a, b, c = input("Please enter the coefficients (a, b, c): ")
> discRoot = math.sqrt(b * b - 4 * a * c)
> root1 = (-b + discRoot) / (2 * a)
> root2 = (-b - discRoot) / (2 * a)
> print
> print "The solutions are:", root1, root2
> main()
>
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in 
> main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
> a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "", line 1
> (1,1,2):
>^
> SyntaxError: unexpected EOF while parsing
>
> The last line is line 11.  It seems like I need this, but that's what's
> causing the error.  Does anyone here have any idea what is wrong here?
>
> Thanks.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error when trying to open an image

2014-11-16 Thread Gary Herron

On 11/16/2014 11:37 AM, Abdul Abdul wrote:

Hello,

I'm trying to open an image using the `Python Imaging Library` as follows:

from PIL import Image
img = Image.open('xyz.jpg')

But, got the following error:

File "C:/Users/abc/PycharmProjects/untitled/open_image.py", line 2, in 


from PIL import Image
  File "C:\Python34\lib\site-packages\PIL\Image.py", line 62, in 
from PIL import _imaging as core
ImportError: DLL load failed: %1 is not a valid Win32 application.

Why is that? How can I solve this issue?


... To be clearer, your problem is not with *opening* an image, but (if 
you look at the error message) its with importing PIL.


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


Re: [ANN] Brython 3.0.0 relased

2014-11-16 Thread Gary Herron

On 11/16/2014 12:54 AM, Pierre Quentel wrote:

Hi,

Version 3.0.0 of Brython has been released recently

Brython is an implementation of Python 3 running in the browser, with an 
interface to DOM elements and events. It allows writing web client applications 
with Python instead of Javascript.

Python programs are inserted in the HTML page inside tags 

How to fix those errors?

2014-11-16 Thread Abdul Abdul
Hello,

I'm walking through an example that goes as follows:

from PIL import Image
import os

for inputfile in filelist
outputfile = os.path.splitext(inputfile)[0]+".jpg"
if inputfile != outputfile:
try:
Image.open(inputfile).save(outputfile)
except IOError:
print "unable to convert ", inputfile

I'm writing that in "Pycharm", and getting the following errors:

* filelist ---> Unresolved reference 'filelist'

* IOError: Statement seems to have no effect

* The last 'filelist' ---> 'except' or 'finally' expected

How can I solve those errors?

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


Re: import math error

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 1:07 PM, ryguy7272  wrote:
> When I type 'import math', it seems like my Python recognizes this library.  
> Great.  When I try to run the following script, I get an error, which 
> suggests (to me) the math library is not working correctly.
>
> Script:
> import math
> def main():
> print "This program finds the real solutions to a quadratic"
> print
> a, b, c = input("Please enter the coefficients (a, b, c): ")
> discRoot = math.sqrt(b * b - 4 * a * c)
> root1 = (-b + discRoot) / (2 * a)
> root2 = (-b - discRoot) / (2 * a)
> print
> print "The solutions are:", root1, root2
> main()
>
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in 
> main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
> a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "", line 1
> (1,1,2):
>^
> SyntaxError: unexpected EOF while parsing
>
> The last line is line 11.  It seems like I need this, but that's what's 
> causing the error.  Does anyone here have any idea what is wrong here?

This has nothing to do with the math module. You're using input() in
Python 2, which is equivalent to eval(raw_input()). Because of the
eval, the string that is entered needs to be a valid Python
expression. You apparently entered "(1,1,2):" which is not a valid
Python expression because of the colon.

By the way, general use of the Python 2 input and eval functions are
strongly discouraged because they can lead to execution of arbitrary
code when given untrusted input. The recommended approach is to use
raw_input() instead (which is renamed to just input() in Python 3) and
then parse the input properly without resorting to eval. For example,
the above could be written as something like:

coeff_str = raw_input("Please enter the coefficients a, b, c:")
a, b, c = map(float, coeff_str.split(','))
-- 
https://mail.python.org/mailman/listinfo/python-list


What does this line of code mean?

2014-11-16 Thread Abdul Abdul
I just came across the following line of code:

outputfile = os.path.splitext(infile)[0] + ".jpg"

Can you kindly explain to me what those parts mean?

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread jkn
Hi All
Thanks for the various and interesting responses so far. A bit of 
fleshing out in a few areas:

The problems of maintaining the long-term TCP connection is something I'd 
like to leave to one side, for now at least. There are some non-technical 
project issues here which is why I am considering some alternatives. It may 
be that this gets revisited at a later date.

It is possible that the final deployment will, like the prototype, be 
written in Python; again, non-technical issues come into play here.

I use the term 'devices' a bit loosely; some of the 'slave' machines are 
fully-fledged and relatively powerful embedded machines running linux; some 
of them are simpler embedded machines running a scheduler-based system and a 
simple TCP/IP stack. The capability of the latter may be a consideration in 
the final design; they won't be running Python, for instance.

When a 'slave' (not necessarily acting in the client role, AFAICT) is 
unavailable, the behaviour should be that the data is lost, but that it 
should sync up to the next 'block' of data as soon as possible after 
(physical) reconnection, power up etc. An analogy might be with a master and 
multiple slave devices sharing a serial RS-485 bus,.

I have control over the format of the data to be send, so there can and 
should be some indication of the beginning and end of a data 'block'. In 
actual fact the data is very likely to be in JSON.

UDP has been mentioned but I am expecting the comms to be TCP-based, I 
think. The 65KB block size is indicative but I am reluctant to say that this 
is a hard limit.

Chris asks why the slave devices can't periodically connect to the master 
and request data. The reason is that it is unknown when data destined for a 
given slave will be available. The devices would need to connect perhaps 
once a second to get sufficient time resolution, and this seems over-
complicated to me at the moment.

(FWIW there will also have to be some (UDP?) transmission from the slaves to 
the master, for purposes of device discovery)

Thanks for the mention of SCTP; I was aware that there were other protocols 
to consider but haven't yet looked hard at them. One likely issue here is 
that our simpler devices won't be able to support such protocols 
straightforwardly.

I hope this gives sufficient background info to progress the discussion...

Cheers
Jon N
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does this line of code mean?

2014-11-16 Thread Tim Chase
On 2014-11-16 22:45, Abdul Abdul wrote:
> I just came across the following line of code:
> 
> outputfile = os.path.splitext(infile)[0] + ".jpg"
> 
> Can you kindly explain to me what those parts mean?

Have you tried them?

https://docs.python.org/2/library/os.path.html#os.path.splitext

This takes a path and splits it into everything-but-the-extension,
and the extension.  The "[0]" takes the first part (without the
extension), and the "+ '.jpg'" part tacks on that extension.

So it changes whatever the existing extension is to .jpg

-tkc


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


Re: How modules work in Python

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 12:36 PM, Abdul Abdul  wrote:
> My question is, where did PIL go here? Can a module have another module
> inside it?

Yes, a module that contains other modules is usually called a package.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does this line of code mean?

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 2:45 PM, Abdul Abdul  wrote:
> I just came across the following line of code:
>
> outputfile = os.path.splitext(infile)[0] + ".jpg"
>
> Can you kindly explain to me what those parts mean?

>>> import os.path
>>> help(os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
Split the extension from a pathname.

Extension is everything from the last dot to the end, ignoring
leading dots.  Returns "(root, ext)"; ext may be empty.

>>> os.path.splitext('test.txt')
('test', '.txt')
>>> os.path.splitext('test.txt')[0]
'test'
>>> os.path.splitext('test.txt')[0] + ".jpg"
'test.jpg'
-- 
https://mail.python.org/mailman/listinfo/python-list


import os

2014-11-16 Thread Abdul Abdul
I tried to refer to Python documentation for what "os" refers to, but, the
explanation was not clear.

When we write something like:

import os

What do we mean here? What is the purpose of such import?

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


Re: OTish: using short-term TCP connections to send to multiple slaves

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 4:21 AM, Roy Smith  wrote:
> In article ,
>  Chris Angelico  wrote:
>
>> UDP for anything more than your network's MTU is inefficient
>
> Why do you say it's inefficient?  Sure, the UDP datagram will get
> fragmented and re-assembled at the other end, but it's not like TCP
> would do any better.  One way or another, your data is going to be
> transmitted in packet that fit into the MTU.

Sorry, is less efficient. I was responding to the point about 64KB
being way too big for UDP, when really it's perfectly possible. It's
common to keep UDP packet sizes as low as possible to take advantage
of non-fragmentation efficiency (hence the DNS TCP switchover at 512
bytes), but I've sent some huge UDP packets around.

>> plus you'd need to roll your own acknowledgement system so you know
>> when the client got the data, at which point you're basically
>> recreating TCP.
>
> That I certainly agree with.  UDP should be used for fire-and-forget,
> where its not critical that every bit of data gets through.  A classic
> example is high-volume logging.  Once you start thinking about any kind
> of acknowledgements, you should just switch to TCP.  The alternative is
> that you will slowly end up reinventing TCP (and doing a bad job of it).

Another good use of UDP is where it's not 100% critical that *every*
update get to *every* client instantly, but a client can "catch up"
when it gets the next notification. (Since clients can be down, you'd
need some kind of catch-up mechanic anyway.) If the data involved can
fit inside a single UDP packet, this is trivially easy: just overwrite
with the new state. Otherwise, some kind of update counter works too
("hmm, I got update 5, this one's update 7, I guess I'd better catch
up"). If most clients collect most UDP updates, but occasionally they
establish a TCP connection to do the full catch-up, you can get some
handy conveniences.

>> NAT is the most common cause of breakage, but any router does have the
>> power to monitor and drop connections on any basis it likes. (I can't
>> imagine any reason a non-NAT router would want to prevent connections
>> from going idle, but it could be done.)
>
> It's common in corporate networks for routers to forcibly close idle
> connections (i.e. inject RST packets).  Often, the IT security guys
> think idle connections represent some kind of threat (and good luck
> trying to argue with them).

Yeah... but I can't imagine any reason for those connections to be a
threat. I've heard of it happening but I cannot fathom the reasoning
behind it. (You're also suggesting a much nicer approach than the
worst-case I was talking about: with RST injection, at least one end
will be immediately aware of the closure. If the router just starts
dropping packets, much harder.)

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


Re: What does this line of code mean?

2014-11-16 Thread Peter Otten
Abdul Abdul wrote:

> I just came across the following line of code:
> 
> outputfile = os.path.splitext(infile)[0] + ".jpg"
> 
> Can you kindly explain to me what those parts mean?

You can try it yourself in the interactive interpreter:

>>> import os.path
>>> help(os.path.splitext)
Help on function splitext in module ntpath:

splitext(p)
Split the extension from a pathname.

Extension is everything from the last dot to the end, ignoring
leading dots.  Returns "(root, ext)"; ext may be empty.

>>> infile = r"c:\some\folder\image.png"
>>> os.path.splitext(infile)
('c:\\some\\folder\\image', '.png')
>>> os.path.splitext(infile)[0]
'c:\\some\\folder\\image'
>>> os.path.splitext(infile)[0] + ".jpg"
'c:\\some\\folder\\image.jpg'

PS: I'm using Linux; If you look close enough at the above session you'll 
note that I've cheated a bit.

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


Re: How to fix those errors?

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 2:32 PM, Abdul Abdul  wrote:
> Hello,
>
> I'm walking through an example that goes as follows:
>
> from PIL import Image
> import os
>
> for inputfile in filelist
> outputfile = os.path.splitext(inputfile)[0]+".jpg"
> if inputfile != outputfile:
> try:
> Image.open(inputfile).save(outputfile)
> except IOError:
> print "unable to convert ", inputfile
>
> I'm writing that in "Pycharm", and getting the following errors:
>
> * filelist ---> Unresolved reference 'filelist'

Your for loop iterates over a variable called filelist, but you never define it.

> * IOError: Statement seems to have no effect
>
> * The last 'filelist' ---> 'except' or 'finally' expected

The indentation of your try and except statements don't appear to
match, which is probably confusing it. The except should be at the
same indentation level as the try. Also, the for should presumably be
at the same indentation level as the imports, as it's not contained
within a block.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to fix those errors?

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 8:32 AM, Abdul Abdul  wrote:
> from PIL import Image
> import os
>
> for inputfile in filelist
> outputfile = os.path.splitext(inputfile)[0]+".jpg"
> if inputfile != outputfile:
> try:
> Image.open(inputfile).save(outputfile)
> except IOError:
> print "unable to convert ", inputfile

The first issue I'm seeing here is that your indentation is messed up.
I've no idea where your example is coming from, but somewhere between
working code and what I'm seeing in front of me, some lines have
become indented further than they should.

You'll also need to figure out what the list of files should be.
That's why you're seeing errors about 'filelist'.

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


Re: import os

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 8:56 AM, Abdul Abdul  wrote:
> I tried to refer to Python documentation for what "os" refers to, but, the
> explanation was not clear.

If it's something you import, what you want is the module of that
name. Searching the web for 'python os module' should bring you
straight to one of these:

https://docs.python.org/2/library/os.html
https://docs.python.org/3/library/os.html

(Use the first link for Python 2.x, the second link for 3.x. The
module has the same purpose in both.)

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


Re: How to fix those errors?

2014-11-16 Thread Roy Smith
In article ,
 Chris Angelico  wrote:

> On Mon, Nov 17, 2014 at 8:32 AM, Abdul Abdul  wrote:
> > from PIL import Image
> > import os
> >
> > for inputfile in filelist
> > outputfile = os.path.splitext(inputfile)[0]+".jpg"
> > if inputfile != outputfile:
> > try:
> > Image.open(inputfile).save(outputfile)
> > except IOError:
> > print "unable to convert ", inputfile
> 
> The first issue I'm seeing here is that your indentation is messed up.
> I've no idea where your example is coming from, but somewhere between
> working code and what I'm seeing in front of me, some lines have
> become indented further than they should.
> 
> You'll also need to figure out what the list of files should be.
> That's why you're seeing errors about 'filelist'.
> 
> ChrisA

Not to mention the missing ":" at the end of the "for" statement.  
Please, people, when asking questions about code, copy-paste what you're 
running into your post.  Then we see exactly what you've got, and at 
least have a chance of figuring out what's wrong.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to fix those errors?

2014-11-16 Thread Thomas 'PointedEars' Lahn
Abdul Abdul wrote:

> from PIL import Image
> import os

You should only import the methods that you use.
 
> for inputfile in filelist
> outputfile = os.path.splitext(inputfile)[0]+".jpg"
> if inputfile != outputfile:
> try:
> Image.open(inputfile).save(outputfile)
> except IOError:
> print "unable to convert ", inputfile
  ^^
If possible, write new programs in Python 3.x (where paretheses are required 
for print()).
 
> I'm writing that in "Pycharm",

Try PyDev or LiClipse with PyDev instead.

> and getting the following errors:
> 
> * filelist ---> Unresolved reference 'filelist'

Define “filelist” which needs to refer to an iterable value.  Also, you have 
forgotten to end the “for…in” statement with a semicolon, and you need to 
unindent it one level if it is to make sense.
 
> * IOError: Statement seems to have no effect

You have indented the “except” clause one level too many.  It must start at 
the same column as the corresponding “try” clause.  In Python, indentation 
works like block braces in C-like programming languages.  Do not mix spaces 
and tabs for indentation.
 
> * The last 'filelist' ---> 'except' or 'finally' expected

Follows from the above.  Standalone “try” clauses are not allowed.
 
> How can I solve those errors?

See above.  Summary (untested):

from PIL import Image
from os.path import splitext

filelist = ["foo", "bar"]

for inputfile in filelist:
outputfile = splitext(inputfile)[0] + ".jpg"
if inputfile != outputfile:
try:
Image.open(inputfile).save(outputfile)
except IOError:
print("unable to convert", inputfile)


Please do not post multi-part messages.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What does this line of code mean?

2014-11-16 Thread Thomas 'PointedEars' Lahn
Abdul Abdul wrote:

> I just came across the following line of code:
> 
> outputfile = os.path.splitext(infile)[0] + ".jpg"
> 
> Can you kindly explain to me what those parts mean?

RTFM: 


An Python IDE like PyDev will also display the docstring of the method when 
you position the pointer cursor over the symbol.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Interrupted system call

2014-11-16 Thread Chris Angelico
On Sun, Nov 16, 2014 at 7:11 PM, Peter Bell  wrote:
>   File "/usr/lib/python3.4/site-packages/serial/serialposix.py", line 480,
> in read
> if e[0] != errno.EAGAIN:
> TypeError: 'InterruptedError' object is not subscriptable
> =
>
>
> I'm not sure how to address the 'InterruptedError'.  Can it/should it be
> masked at the user code level with a 'try:' block?  Or is there another way
> to deal with it?

At this stage, you're not getting it, because of the cascaded error,
so that's the thing to deal with.

> I presume that the subsequent error (TypeError) indicates some fault in the
> system level library.  I'm not sure where/how to report this.

Not system-level. According to the traceback, that's coming from line
480 of something in site-packages, which probably means it was
installed with pip or equivalent. Do you know where you got hold of
this module from? That's who to report this to.

It's trying to retrieve the errno from the exception. I suspect the
code was written for Python 2, and you're using it with Python 3. It's
probably possible to shorten/simplify the code somewhat.

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


Re: How to fix those errors?

2014-11-16 Thread Thomas 'PointedEars' Lahn
Thomas 'PointedEars' Lahn wrote:

> Abdul Abdul wrote:
>> from PIL import Image
>> import os
> […]
>> for inputfile in filelist
>> outputfile = os.path.splitext(inputfile)[0]+".jpg"
>> […]
> 
> Define “filelist” which needs to refer to an iterable value.  Also, you
> have forgotten to end the “for…in” statement with a semicolon,

_colon_ (“:”)

> and you need to unindent it one level if it is to make sense.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 3:28 AM, Grant Edwards  wrote:
> But, you're right: anybody who spent more than a half hour on this is
> either a flaming incompetent or a scam artist.

Half an hour of human time, maybe, but potentially spread across a few
hours of wall time. Building Python from source can take quite a
while, and if you then go back and get some more dev libraries and
start over, it might take a good while to get the job done. Let's be a
bit more generous and say a day. But beyond that, one must know one's
limits: it's better to give up after a day (or half a day, preferably)
and go ask for help, rather than struggle for weeks.

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


Re: How to fix those errors?

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 9:13 AM, Thomas 'PointedEars' Lahn
 wrote:
> Thomas 'PointedEars' Lahn wrote:
>
>> Abdul Abdul wrote:
>>> from PIL import Image
>>> import os
>> […]
>>> for inputfile in filelist
>>> outputfile = os.path.splitext(inputfile)[0]+".jpg"
>>> […]
>>
>> Define “filelist” which needs to refer to an iterable value.  Also, you
>> have forgotten to end the “for…in” statement with a semicolon,
>
> _colon_ (“:”)

You should be able to use two semicolons, that's equivalent to one colon right?

ChrisA
(No, it isn't, so don't take this advice. Thanks.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re:How modules work in Python

2014-11-16 Thread Dave Angel
Abdul Abdul  Wrote in message:
> Hello,
> 
> I'm new to Python, and just have a small question, and thought you might have 
> an idea on it.

You should start any new thread with a specification of the
 versions of software you're asking about.  Since you didn't, 
 I'll assume python version 2.7, PIL version 1.7, and Linux Ubuntu
  version 14.04. 

> 
> I came across the following example that uses the Python Imaging Library 
> (PIL):

> 
> from PIL import Image
> img = Image.open('xyz.jpg')
> 
> I know that PIL is a module. And, I think that Image is also a module, 
> especially we are importing it.

PIL is a package, whuch means it's a module containing other
 modules.,  and contains modules such as Image. But that syntax
 "from PIL import Image" doesn't tell you that. Any type of name
 defined in module PIL can be retrieved by the from
 syntax.

The way I can tell is either read the docs, or ask in the interpreter. 
from PIL import Image
print type (Image)



That from syntax is roughly equivalent to
import PIL
Image = PIL.Image

And the leading capital I would have made me guess it was a class.
 Thus I checked, using type ()

> 
> I also understood the Image,open() part, as it appears we are using the 
> method open() from the Image module.

Modules don't have methods. open is an ordinary function in the module.

> 
> My question is, where did PIL go here?

I don't understand the question.  By using the from syntax, you
 avoided having PIL in your namespace. If you wanted, you could
 have said. import PIL.  And used PIL.Image.open()

 Can a module have another module inside it?
> 
Yes

> 
> 


-- 
DaveA

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


Re:import math error

2014-11-16 Thread Dave Angel

You forgot to specify your Python version here.

ryguy7272  Wrote in message:
> When I type 'import math', it seems like my Python recognizes this library.  
> Great.  When I try to run the following script, I get an error, which 
> suggests (to me) the math library is not working correctly.

Nothing to do with the math library.  It's a syntax error. And it
 occurs before the first call into the math library.
 

> 
> Script:
> import math
> def main():
>   print "This program finds the real solutions to a quadratic"
>   print
>   a, b, c = input("Please enter the coefficients (a, b, c): ")

Ouch. input is unsafe, anot recommended. That's why python 3
 renamed the raw_input function to input,  eliminating the unsafe
 version.  Nevertheless,  I'll assume you understand the risks,
 and we'll go on with your problem. 

>   discRoot = math.sqrt(b * b - 4 * a * c)
>   root1 = (-b + discRoot) / (2 * a)
>   root2 = (-b - discRoot) / (2 * a)
>   print
>   print "The solutions are:", root1, root2
> main()
> 
> Error:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 11, in 
> main()
>   File "C:\Users\Ryan\Desktop\QuadraticEquation.py", line 5, in main
> a, b, c = input("Please enter the coefficients (a, b, c): ")
>   File "", line 1
> (1,1,2):
>^
> SyntaxError: unexpected EOF while parsing

The error is occurring while parsing the user's input,  which
 should not have a colon.  The smallest flaw with using input ()
 is that user error cause strange looking exceptions.

> 
> The last line is line 11.  It seems like I need this, but that's what's 
> causing the error.  Does anyone here have any idea what is wrong here?
> 
> Thanks.
> 


-- 
DaveA

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


Re: import graphics library; causes error

2014-11-16 Thread Terry Reedy

On 11/16/2014 3:39 PM, ryguy7272 wrote:

These libraries drive me totally nuts.  Sorry, just had to get it out there.
Anyway, I open the cmd window, and typed this:

> 'easy_install python graphics'.

In what directory?


So, it starts up and runs/downloads the appropriate library from the web.
> I get confirmation (in the cmd window) that it finishes, then I try 
to run this script.


# futval_graph2.py
from graphics import *
def main():

[snip 20 lines of 2.x code (it used raw_input)]


After I hit F5, I get this:
Traceback (most recent call last):
   File "C:\Users\Ryan\Desktop\Graphics_Test.py", line 2, in 
 from graphics import *
ImportError: No module named graphics


After opening Idle, I would first try
>>> import graphics

in the shell before entering any code.  When that failed, I would look 
in lib/site-packages to see the actual module or package name installed, 
and maybe look in the doc for the package about how to import it.



--
Terry Jan Reedy

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


Re: Using map()

2014-11-16 Thread Terry Reedy

On 11/16/2014 8:01 AM, Pavel Volkov wrote:

I checked my modules with pylint and saw the following warning:

W: 25,29: Used builtin function 'map' (bad-builtin)

Why is the use of map() discouraged?
It' such a useful thing.


I consider that to be a bug in pylint.  It misstates a careless 'bad' 
opinion as a fact.


If pylint sees 'map(lambda ...: ', it would be appropriate to suggest 
using a comprehension or generator expression instead.  This avoids the 
unneeded creation and repeated call of a new function.  If it sees 
'map(,', I think it should say nothing.


--
Terry Jan Reedy

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


Re: Error when trying to open an image

2014-11-16 Thread Terry Reedy

On 11/16/2014 2:37 PM, Abdul Abdul wrote:

Hello,

I'm trying to open an image using the `Python Imaging Library` as follows:

from PIL import Image
img = Image.open('xyz.jpg')

But, got the following error:

File "C:/Users/abc/PycharmProjects/untitled/open_image.py", line 2, in

 from PIL import Image
   File "C:\Python34\lib\site-packages\PIL\Image.py", line 62, in 


Answer is on line above.  Good for you for posting traceback.


 from PIL import _imaging as core
ImportError: DLL load failed: %1 is not a valid Win32 application.

Why is that? How can I solve this issue?


PIL does not work with 3.x.  Get pillow instead.

--
Terry Jan Reedy

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


Re: Using map()

2014-11-16 Thread Steven D'Aprano
Pavel Volkov wrote:

> I checked my modules with pylint and saw the following warning:
> 
> W: 25,29: Used builtin function 'map' (bad-builtin)
> 
> Why is the use of map() discouraged?
> It' such a useful thing.

That's a bug in pylint. It's not a bad builtin, it is perfectly fine.

Some people don't like map. Pay no attention to them.




-- 
Steven

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


Re: Using map()

2014-11-16 Thread Ned Batchelder

On 11/16/14 7:09 PM, Steven D'Aprano wrote:

Pavel Volkov wrote:


I checked my modules with pylint and saw the following warning:

W: 25,29: Used builtin function 'map' (bad-builtin)

Why is the use of map() discouraged?
It' such a useful thing.


That's a bug in pylint. It's not a bad builtin, it is perfectly fine.

Some people don't like map. Pay no attention to them.


Pylint isn't useful until you've tailored the messages.  Personally, I 
avoid map, but your usage may vary.  The pylint message that always 
irked me was:


W0142: Used * or ** magic

"magic"? They're features of the language!  Meanwhile, it doesn't mind 
if you write multiply-inherited metaclasses


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

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


Re: How modules work in Python

2014-11-16 Thread Steven D'Aprano
Abdul Abdul wrote:

> Hello,
> 
> I'm new to Python, and just have a small question, and thought you might
> have an idea on it.
> 
> I came across the following example that uses the Python Imaging Library
> (PIL):
> 
> from PIL import Image
> img = Image.open('xyz.jpg')
> 
> I know that PIL is a module. And, I think that Image is also a module,
> especially we are importing it.
> 
> I also understood the Image,open() part, as it appears we are using the
> method open() from the Image module.
> 
> My question is, where did PIL go here? Can a module have another module
> inside it?

Yes it can. 

If I have a module "spam.py", which imports "eggs.py", then I can do this:

from spam import eggs
eggs.some_function()


In your case, PIL hasn't gone anywhere, it still exists. Importing does
three things:

* it runs the code inside the file, creating a module object;
* it caches that module object in sys.modules;
* it optionally creates a name in your current namespace for that module.

So when you run `from PIL import Image`, something more or less like the
following events happen:

- Python locates the PIL file
- Python runs the PIL file and builds a module object
- which contains a line like "import Image"
  +- Python locates the Image file 
  +- Python runs the Image file and builds a module object
  +- caches Image in sys.module
  +- creates a name Image =  inside PIL
- Python finishes running the PIL module
- and caches PIL in sys.module
- it looks up PIL.Image in that module
- and creates a name Image =  inside your module


Think of `from PIL import Image` as being effectively something quite close
to this:

import PIL as _temporary_name_123456
Image = _temporary_name_123456.Image
del _temporary_name_123456

except that no temporary name is actually created. So you can see, the PIL
module has been imported (that's the only way Python can look up Image
inside PIL), but you don't have access to it in your name space. But you
can then do:

import PIL

which will be really quick because it is already cached and ready to go.




-- 
Steven

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


Re: How to fix those errors?

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

> You should be able to use two semicolons, that's equivalent to one colon
> right?
> 
> ChrisA
> (No, it isn't, so don't take this advice. Thanks.)


Oooh! Python-ideas territory! 

I think the parser should allow two consecutive semi-colons, or four commas,
as an alias for a colon. Then we could write code like:


def spam(arg);;
for x in seq
pass


for people who have a broken keyboard or have remapped colon to something
else. It would also make reading slices an exciting adventure:

seq[1][-1]


What do you think? Is it perhaps better suited to perl-ideas?


Feeling-whimsical-at-the-moment-ly y'rs,


-- 
Steven

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


Re: How modules work in Python

2014-11-16 Thread Abdul Abdul
Dave,

Thanks for your nice explanation. For your answer on one of my questions:

* Modules don't have methods. open is an ordinary function in the module.*

Isn't "method" and "function" used interchangeably? In other words, aren't
they the same thing? Or, Python has some naming conventions here?

Thanks.

On Sun, Nov 16, 2014 at 11:39 PM, Dave Angel  wrote:

> Abdul Abdul  Wrote in message:
> > Hello,
> >
> > I'm new to Python, and just have a small question, and thought you might
> have an idea on it.
>
> You should start any new thread with a specification of the
>  versions of software you're asking about.  Since you didn't,
>  I'll assume python version 2.7, PIL version 1.7, and Linux Ubuntu
>   version 14.04.
>
> >
> > I came across the following example that uses the Python Imaging Library
> (PIL):
>
> >
> > from PIL import Image
> > img = Image.open('xyz.jpg')
> >
> > I know that PIL is a module. And, I think that Image is also a module,
> especially we are importing it.
>
> PIL is a package, whuch means it's a module containing other
>  modules.,  and contains modules such as Image. But that syntax
>  "from PIL import Image" doesn't tell you that. Any type of name
>  defined in module PIL can be retrieved by the from
>  syntax.
>
> The way I can tell is either read the docs, or ask in the interpreter.
> from PIL import Image
> print type (Image)
>
> 
>
> That from syntax is roughly equivalent to
> import PIL
> Image = PIL.Image
>
> And the leading capital I would have made me guess it was a class.
>  Thus I checked, using type ()
>
> >
> > I also understood the Image,open() part, as it appears we are using the
> method open() from the Image module.
>
> Modules don't have methods. open is an ordinary function in the module.
>
> >
> > My question is, where did PIL go here?
>
> I don't understand the question.  By using the from syntax, you
>  avoided having PIL in your namespace. If you wanted, you could
>  have said. import PIL.  And used PIL.Image.open()
>
>  Can a module have another module inside it?
> >
> Yes
>
> >
> >
>
>
> --
> DaveA
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Error when trying to open an image

2014-11-16 Thread Abdul Abdul
Thanks for your kind reply. Yes, it seemed it worked with an older version
than 3.x

I got the following output:

Process finished with exit code 0

So, what is the purpose of open() here?

Thanks.

On Mon, Nov 17, 2014 at 12:31 AM, Terry Reedy  wrote:

> On 11/16/2014 2:37 PM, Abdul Abdul wrote:
>
>> Hello,
>>
>> I'm trying to open an image using the `Python Imaging Library` as follows:
>>
>> from PIL import Image
>> img = Image.open('xyz.jpg')
>>
>> But, got the following error:
>>
>> File "C:/Users/abc/PycharmProjects/untitled/open_image.py", line 2, in
>> 
>>  from PIL import Image
>>File "C:\Python34\lib\site-packages\PIL\Image.py", line 62, in
>> 
>>
>
> Answer is on line above.  Good for you for posting traceback.
>
>   from PIL import _imaging as core
>> ImportError: DLL load failed: %1 is not a valid Win32 application.
>>
>> Why is that? How can I solve this issue?
>>
>
> PIL does not work with 3.x.  Get pillow instead.
>
> --
> Terry Jan Reedy
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to fix those errors?

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 11:38 AM, Steven D'Aprano
 wrote:
> for people who have a broken keyboard or have remapped colon to something
> else. It would also make reading slices an exciting adventure:
>
> seq[1][-1]
>
>
> What do you think? Is it perhaps better suited to perl-ideas?

Actually, I think this would fit in very nicely with C's trigraphs -
an excellent idea spoiled by the use of the arbitrary number three.
Using even numbers, especially powers of two, will make the idea ever
so much better.

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


Re: Using map()

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 11:24 AM, Ned Batchelder  wrote:
> Pylint isn't useful until you've tailored the messages.  Personally, I avoid
> map, but your usage may vary.  The pylint message that always irked me was:
>
> W0142: Used * or ** magic

This is why I don't bother with linters at all. They're almost always
more hassle than they're worth, because no two programmers agree on
everything.

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


Re: import os

2014-11-16 Thread Abdul Abdul
Thanks for your reply. Yes, I came across this page, but didn't understand
what is meant by the operating system dependent functionality. What does
that mean? Is importing that module that serious?

Thanks.

On Sun, Nov 16, 2014 at 11:02 PM, Chris Angelico  wrote:

> On Mon, Nov 17, 2014 at 8:56 AM, Abdul Abdul  wrote:
> > I tried to refer to Python documentation for what "os" refers to, but,
> the
> > explanation was not clear.
>
> If it's something you import, what you want is the module of that
> name. Searching the web for 'python os module' should bring you
> straight to one of these:
>
> https://docs.python.org/2/library/os.html
> https://docs.python.org/3/library/os.html
>
> (Use the first link for Python 2.x, the second link for 3.x. The
> module has the same purpose in both.)
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: import os

2014-11-16 Thread Dan Stromberg
On Sun, Nov 16, 2014 at 5:10 PM, Abdul Abdul  wrote:
> Thanks for your reply. Yes, I came across this page, but didn't understand
> what is meant by the operating system dependent functionality. What does
> that mean? Is importing that module that serious?

The os module has mostly lower level OS interfaces;  there are
sometimes higher level equivalents elsewhere in the language.

It's not bad to use os, but for EG, file operations
os.open/os.read/os.write/os.close, it's better to use "with open as
file_", file_.read, file_.write.

Another example is os.fork() - you can use it, but you're probably
better off with the subprocess module or the multiprocessing module.

You don't really have to worry about breaking anything by using the os
module - at least, not particularly more than the higher level
interfaces for the same thing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Messages from code analysis tools (was: Using map())

2014-11-16 Thread Ben Finney
Ned Batchelder  writes:

> Pylint isn't useful until you've tailored the messages.

Definitely agreed.

> The pylint message that always irked me was:
>
> W0142: Used * or ** magic
>
> "magic"? They're features of the language!

It's a warning, because the use of that feature clobbers the static code
inspection you've asked for. PyLint is warning you “because the code
uses this feature, I can't say anything about undefined names”.

-- 
 \   “The future always arrives too fast, and in the wrong order.” |
  `\—Alvin Toffler |
_o__)  |
Ben Finney

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


Re: How modules work in Python

2014-11-16 Thread Ben Finney
Abdul Abdul  writes:

> Thanks for your nice explanation. For your answer on one of my
> questions:

(Please use “interleaved” posting style, for showing quoted material and
your responses 
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.)

> * Modules don't have methods. open is an ordinary function in the module.*
>
> Isn't "method" and "function" used interchangeably?

No (or more accurately: if someone does use those interchangeably,
they're not correctly describing Python).

A method is always a function, but a function is not always a method.

A method is a function bound to a specific object instance.

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

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


Re: Error when trying to open an image

2014-11-16 Thread Ben Finney
Abdul Abdul  writes:

> Thanks for your kind reply.

Abdul, please don't top-post. Instead, trim the quoted material just to
what you're responding to; then post your responses interleaved
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.

> I got the following output:
>
> Process finished with exit code 0
>
> So, what is the purpose of open() here?

I see no context for this question. Where is “here”?

Can you write a very minimal example of a complete program you're asking
about, and post it here so we can see what is confusing you?
http://sscce.org/>

-- 
 \   “I bet one legend that keeps recurring throughout history, in |
  `\  every culture, is the story of Popeye.” —Jack Handey |
_o__)  |
Ben Finney

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


Re: Using map()

2014-11-16 Thread Dan Stromberg
On Sun, Nov 16, 2014 at 4:09 PM, Steven D'Aprano
 wrote:
> Pavel Volkov wrote:
>
>> I checked my modules with pylint and saw the following warning:
>>
>> W: 25,29: Used builtin function 'map' (bad-builtin)
>>
>> Why is the use of map() discouraged?
>> It' such a useful thing.
>
> That's a bug in pylint. It's not a bad builtin, it is perfectly fine.
>
> Some people don't like map. Pay no attention to them.

I actually avoid map.

map works as intended; there's nothing particularly wrong with it. In
fact I believe it's pretty important in lisp, but it's a rather
glaring example of TMTOWTDI in Python.

BTW, I believe in Python 2.x map is like a list comprehension, but in
3.x map is like a generator expression.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: import os

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 12:10 PM, Abdul Abdul  wrote:
> Thanks for your reply. Yes, I came across this page, but didn't understand
> what is meant by the operating system dependent functionality. What does
> that mean? Is importing that module that serious?

It's not "serious" as in "using this module can break your code". What
it means is that stuff in the os module might not always be available;
on my Linux box, len(dir(os)) is 326, but on my Windows, it's only
145. (Similar Python versions.) A lot of the os module is available on
every platform you're ever likely to use, but need to be named
differently: for instance, os.curdir is the string representing the
current directory, and os.pathsep is the character that divides
entries in the PATH environment variable. You can assume that there
*will be* some kind of path separator, but you can't know whether
it'll be a semicolon (Windows), a colon (Unix), or something else, so
you call up the os module to find out. In fact, proper use of the os
module can make your code more, not less, cross-platform.

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


Re: Messages from code analysis tools (was: Using map())

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 12:21 PM, Ben Finney  wrote:
>> The pylint message that always irked me was:
>>
>> W0142: Used * or ** magic
>>
>> "magic"? They're features of the language!
>
> It's a warning, because the use of that feature clobbers the static code
> inspection you've asked for. PyLint is warning you “because the code
> uses this feature, I can't say anything about undefined names”.

Then that ought surely to be an informational? It's not saying there's
a problem in your code, it's pointing out that it can't check.

Either that, or warnings from PyLint are all low-grade and should be ignored.

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


Re: Messages from code analysis tools

2014-11-16 Thread Ned Batchelder

On 11/16/14 8:21 PM, Ben Finney wrote:

Ned Batchelder  writes:


Pylint isn't useful until you've tailored the messages.


Definitely agreed.


The pylint message that always irked me was:

 W0142: Used * or ** magic

"magic"? They're features of the language!


It's a warning, because the use of that feature clobbers the static code
inspection you've asked for. PyLint is warning you “because the code
uses this feature, I can't say anything about undefined names”.



That's the best explanation I've heard for why they warn about it.  I 
wish they would find a more sophisticated word than "magic".


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

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


from pylab import *

2014-11-16 Thread Abdul Abdul
I came across an example that uses "Matplotlib".

It used the following import to use that module:

from pylab import *

When I tried to run the example, I got the following error:

C:\Python27\python.exe
C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py
Traceback (most recent call last):
  File "C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py", line 4,
in 
from pylab import *
ImportError: No module named pylab

Process finished with exit code 1

Why is that? How can I solve this issue?

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


Re: I love assert

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

> -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?

No.

You appear to be labouring under the misapprehension that assertion-based
techniques (such as design by contract) are a technique for guarding
against expected errors in (say) data. If that were the case, then allowing
the end-user to disable such guards would be a bad idea. And that is one
reason (but not the only reason) why we should never use assert for "lazy
man's argument checking" of public library functions, or for validating
data.

# this is bad
positive_value = int(raw_input("Enter a positive integer: "))
assert positive_value > 0
blow_something_up_if_negative(positive_value)


But this is a misuse of assertions. If (generic) you is writing code like
that, PLEASE STOP, you're writing buggy code and giving assert a bad name.

Assertions should never be used to *enforce* correct behaviour. Assertions
are used for *verifying* correctness of the code, and they are a form of
runtime testing. The motto of Design By Contract might be "Trust, but
Verify".

Assertions are tests, not production code. They just happen to live inside
the production code, where they provide the additional benefits of checked
comments, validation of internal logic, and so on.

Since assertions are tests, the end-user should never, ever see an
AssertionError. If they do, it is a bug in your code. Since the end-user
will never see a failed assertion then it is harmless if they disable them.
At best it won't matter one whit, since your code is perfectly bug free[1].
At worst, they are no worse off than if you had neglected to write any
assertions at all.

But if they do run with assertions enabled, and one fails, then you, the
developer, get the bonus of "fail early, fail hard". Instead of the
undefined behaviour of buggy code, you get a nice clean assertion error
telling you what went wrong and where.



[1] Also on time and below budget.

-- 
Steven

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


Re: from pylab import *

2014-11-16 Thread MRAB

On 2014-11-17 02:04, Abdul Abdul wrote:

I came across an example that uses "Matplotlib".

It used the following import to use that module:

from pylab import *

When I tried to run the example, I got the following error:

C:\Python27\python.exe
C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py
Traceback (most recent call last):
   File "C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py",
line 4, in 
 from pylab import *
ImportError: No module named pylab

Process finished with exit code 1

Why is that? How can I solve this issue?


Have you downloaded and installed the relevant module/package? It's not
part of the standard library.
--
https://mail.python.org/mailman/listinfo/python-list


Re:from pylab import *

2014-11-16 Thread Dave Angel
Abdul Abdul  Wrote in message:
> I came across an example that uses "Matplotlib".
> 
> It used the following import to use that module:
> 
> from pylab import *
> 
> When I tried to run the example, I got the following error:
> 
> C:\Python27\python.exe 
> C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py
> Traceback (most recent call last):
>   File "C:/Users/abc/PycharmProjects/ComputerVision/plot_image.py", line 4, 
> in 
> from pylab import *
> ImportError: No module named pylab
> 
> Process finished with exit code 1
> 
> Why is that? How can I solve this issue?
> 

Did you perchance install matplotlib? And if so, did you install
 it to Python 2.7?  

Neither of those is part of the standard lib.
-- 
DaveA

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


Re: import os

2014-11-16 Thread Dave Angel
Abdul Abdul  Wrote in message:
> Thanks for your reply. Yes, I came across this page, but didn't understand 
> what is meant by the operating system dependent functionality. What does that 
> mean? Is importing that module that serious?
> 
> 
Please don't top-post here. Add your comments after some edited
 context, not before.

Importing the os module isn't important at all, unless you want to
 directly call some of its functionality.  It's being called all
 the time under the covers by other modules.

There are hundreds of modules to learn about. Did you have a
 particular problem you were trying to solve?

-- 
DaveA

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


Re: How modules work in Python

2014-11-16 Thread Dave Angel
(Please don't top-post. Use interleaved posting.  And remove parts
 you didn't respond to.)

(While I'm criticizing,  I should point out that your quoting
 seems doublespaced.  That makes me suspect buggy googlegroups. 
 If you're using that, you should either find a real newsreader,
 use the mailing list, or manually fix the double spacing. 
 Hundres of posts on here about it, just search out one of
 them)

Abdul Abdul  Wrote in message:
> Dave,
> 
> Thanks for your nice explanation. For your answer on one of my questions:
> 
> 
>> Modules don't have methods. open is an ordinary function in the module.
> 
> Isn't "method" and "function" used interchangeably? In other words, aren't 
> they the same thing? Or, Python has some naming conventions here?

Certainly not. A method is a very specic kind of function, you'll
 learn about it when you study classes. Some languages blur the
 distinction,  by not having any functions outside of classes. But
 that's not Python. 

> 
> 


-- 
DaveA

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


Re: How to fix those errors?

2014-11-16 Thread Roy Smith
In article <54694389$0$13001$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> Chris Angelico wrote:
> 
> > You should be able to use two semicolons, that's equivalent to one colon
> > right?
> > 
> > ChrisA
> > (No, it isn't, so don't take this advice. Thanks.)
> 
> 
> Oooh! Python-ideas territory! 
> 
> I think the parser should allow two consecutive semi-colons, or four commas,
> as an alias for a colon. Then we could write code like:
> 
> 
> def spam(arg);;
> for x in seq
> pass

Wouldn't it make more sense to use four periods?

def spam(arg)
for x in seq
pass

First, 2 colons is 4 dots, so it's conservation of punctuation.  Second, 
it reads pretty well.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: import graphics library; causes error

2014-11-16 Thread ryguy7272
On Sunday, November 16, 2014 3:39:45 PM UTC-5, ryguy7272 wrote:
> These libraries drive me totally nuts.  Sorry, just had to get it out there.
> Anyway, I open the cmd window, and typed this: 'easy_install python 
> graphics'.  So, it starts up and runs/downloads the appropriate library from 
> the web.  I get confirmation (in the cmd window) that it finishes, then I try 
> to run this script.
> 
> 
> # futval_graph2.py
> from graphics import *
> def main():
>   # Introduction
>   print "This program plots the growth of a 10-year investment."
>   # Get principal and interest rate
>   principal = input("Enter the initial principal: ")
>   apr = input("Enter the annualized interest rate: ")
>   # Create a graphics window with labels on left edge
>   win = GraphWin("Investment Growth Chart", 640, 480)
>   win.setBackground("white")
>   win.setCoords(-1.75,-200, 11.5, 10400)
>   Text(Point(-1, 0), '0.0K').draw(win)
>   Text(Point(-1, 2500), '2.5K').draw(win)
>   Text(Point(-1, 5000), '5.0K').draw(win)
>   Text(Point(-1, 7500), '7.5k').draw(win)
>   Text(Point(-1, 1), '10.0K').draw(win)
>   # Draw bar for initial principal
>   bar = Rectangle(Point(0, 0), Point(1, principal))
>   bar.setFill("green")
>   bar.setWidth(2)
>   bar.draw(win)
>   # Draw a bar for each subsequent year
>   for year in range(1, 11):
> principal = principal * (1 + apr)
> bar = Rectangle(Point(year, 0), Point(year+1, principal))
> bar.setFill("green")
> bar.setWidth(2)
> bar.draw(win)
>   raw_input("Press  to quit.")
> 
> 
> After I hit F5, I get this:
> Traceback (most recent call last):
>   File "C:\Users\Ryan\Desktop\Graphics_Test.py", line 2, in 
> from graphics import *
> ImportError: No module named graphics



In what directory?
Well, that's a damn good question.  I thought, by defailt, everything was 
downloaded to this folder:
'C:\Python27\Lib\site-packages'

In there, I have all kinds of things like:
'setuptools-6.1.dist-info', 'pip-1.5.6.dist-info', etc.
All kinds of other things too.  

It seems there is always a copy, so I cut/paste the folders named 'setuptools' 
& 'pip' (always taking off the versions and identifiers and the like...).  Then 
I cut/paste everything into this folder:
'C:\Python27\Lib'

Is that how it's done or not?  Honestly, for the life of me, I don't know why a 
human being would have do do any of this, including using the cmd window, to 
install anything in 2014.  I can code in 10 different languages, not including 
Python.  Python is by far the most backwards type of technology that I can 
think of.  Using it is completely counter-productive.  I can't take it serious. 
 I have plenty of tools in my toolbox.  I'll keep learning Python, and keep 
reading books, and keep using it...but strictly for fun.  I would never use 
this as a foundation for a mission critical business application.

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


Re: How to fix those errors?

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 1:55 PM, Roy Smith  wrote:
> Wouldn't it make more sense to use four periods?
>
> def spam(arg)
> for x in seq
> pass
>
> First, 2 colons is 4 dots, so it's conservation of punctuation.  Second,
> it reads pretty well.

You could then name it in Hebrew: Paamayim Nekudotayim. There is
excellent precedent for this - it's done by a language in whose
footsteps Python strives to follow.

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


Re: import graphics library; causes error

2014-11-16 Thread Chris Angelico
On Mon, Nov 17, 2014 at 2:03 PM, ryguy7272  wrote:
> Well, that's a damn good question.  I thought, by defailt, everything was 
> downloaded to this folder:
> 'C:\Python27\Lib\site-packages'
>
> In there, I have all kinds of things like:
> 'setuptools-6.1.dist-info', 'pip-1.5.6.dist-info', etc.
> All kinds of other things too.
>
> It seems there is always a copy, so I cut/paste the folders named 
> 'setuptools' & 'pip' (always taking off the versions and identifiers and the 
> like...).  Then I cut/paste everything into this folder:
> 'C:\Python27\Lib'
>
> Is that how it's done or not?

Most definitely not. That is not how you should go about installing
things. You're packing everything into the standard library, certain
to cause a mess. Just leave them in site-packages - they ought to be
importable from there. If they're not, you most likely have one very
simple configuration issue to fix, and then everything will just work.

> Python is by far the most backwards type of technology that I can think of.  
> Using it is completely counter-productive.  I can't take it serious.
>

You're blaming Python for your poor use of it. Python, as a
technology, is not the cause of your issues. Get to know the language
and its standard library first, then move on to learning about the
entire ecosystem of PyPI packages; base your opinion of Python
primarily on the language, not on what people do with it. Do you judge
C on the basis of the IOCCC? No, you judge it on the basis of manual
memory management and segfaults. (That said, I do like C... as a
language for writing high level languages in. I try to avoid writing
actual C code.)

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


Re: I love assert

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

On 11/16/2014 06:09 PM, Steven D'Aprano wrote:
> Ethan Furman wrote:
>> 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?
> 
> No.
> 
> You appear to be labouring under the misapprehension that assertion-based 
> techniques (such as design by contract)
> are a technique for guarding against expected errors in (say) data.

I believe we are in violent agreement here.  My hissy-fit is not on the correct 
use of asserts, but their incorrect use.

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

iQIcBAEBAgAGBQJUaWqRAAoJENZ7D1rrH75N5pAQAKTiuIMWSpVYX80XdyPh036f
7FTvIerwaCtxwhBG2ma5GKQSXLIPIp6n6LQopnIN6Y82Odx6CqtUmlOpbrxZ4L5w
HG+GaCgBKPCP43ghP8NV+IDNK+mpz3g8uqa3F7ofgygDiBUqXg07/aBR2Eifkyl2
yoB9OjwRek2/m+KkUxe+hnEAmNjeyHF1+0rpFkjf04c4O38/fXJ2Gv6aoluoc/on
IdmST56ukgcVU9JjTTLD+PhLA8v/uvTHyFFQvGng9cUTIMxJEvItIikp+20VS1CP
PY8xJabI28TA4IEtC3atMwk/bDme2H6ovv0O0yvYswF38Y0u6U+NpNgKYUZfXKwS
nFHIJMFSfFpWddmxUGCNmjvGlDRVH5py11QGEl430PhUr3Y5re4MUglye9lFWoy9
NwSVA4c6zoM4lrgdZ2qzzDX8LUaLr1c//sNvS+kVaI6syIf0C60++QJGBboauIMR
lprJ65FfmiHhfI9e+7sS5KX09iILkuPjnwGi79CJG/TehfFcvueqDjNzRan8JzVX
/Czo3zwBK8B4cV13DBeRP2oaJcreMvA5bysXXKJ7iqZkLST+K8hiFpoKCGZiGdcZ
OBafvCwMqP5GzuCCzBxwAOIPo4KHR+KDVMarLZ+NI2kebZPTgKmWmtPrIIztTxE0
DFNrXUK0o8z0WGKP2Ke6
=tM/a
-END PGP SIGNATURE-
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using map()

2014-11-16 Thread Ian Kelly
On Sun, Nov 16, 2014 at 4:22 PM, Terry Reedy  wrote:
> If pylint sees 'map(lambda ...: ', it would be appropriate to suggest using
> a comprehension or generator expression instead.  This avoids the unneeded
> creation and repeated call of a new function.

There's actually a separate warning for that: "map/filter on lambda
could be replaced by comprehension (deprecated-lambda)". I agree with
you, though.

On Sun, Nov 16, 2014 at 6:31 PM, Dan Stromberg  wrote:
> BTW, I believe in Python 2.x map is like a list comprehension, but in
> 3.x map is like a generator expression.

Yes, just like other functions such as range or dict.items that return
lists in Python 2 but something more light-weight in Python 3.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: caught in the import web again

2014-11-16 Thread dieter
"Charles T. Smith"  writes:
> ...
> Are others equally frustrated by this or is there a trick or principle 
> that I'm missing.  At this point, I guess the way I'll have to proceed is 
> to put every class in its own file, no matter how small.  Hopefully that 
> takes care of the problem.

Recursive imports are inherently difficult. It is best if
a careful design avoids it (note that a cycle free dependency
structure is much easier to handle than a cyclic one -- not only
for Python).

If you think such a cycle free module structure is impossible
in your case, then try to delay module related operations:

   *  consider local imports (imports inside a function).
  They delay the import until the function is called rather
  than execute it at module import time.

   *  prefer "import " over "from  import ..."
  This may delay the module access; perhaps (but not
  necessarily) sufficiently such that the accessed object
  is already defined.

Neither of these approaches is complete - you may still get
cyclic import dependencies and related problems. Usually, you
need a careful module dependency design to avoid cyclic dependencies
altogether.
  

> I compared perl's object-oriented philosophy with python's and didn't 
> like how perl *requires* you to put everything into its own file.  Now it 
> looks like python does, too, implicitly.

If you put everything in a single file, then you do not get
recursive imports (but you may still have other problems related to
cyclic dependencies). Thus, Python's difficulties with recursive
imports do not force you to "put everything in its own file".

If you think about it, you will recognize that cyclic dependencies are
inherently difficult (e.g. "chicken - egg" type problems)
and that the way Python handles cyclic
import dependencies is rather natural (and understandable).

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


  1   2   >