Re: How the heck does async/await work in Python 3.5
On Sat, Feb 20, 2016 at 6:48 PM, Ian Kelly wrote: > As another point that happens to be fresh in my mind, awaiting a > Future on which an exception gets set is supposed to propagate the > exception. I recently found that this breaks if the exception in > question happens to be StopIteration (granted not one that should > generally be allowed to propagate anyway, but that's a separate > discussion) for the simple reason that raising StopIteration in a > generator is equivalent to returning None. Solved by PEP 479. Use "from __future__ import generator_stop" to save yourself the pain. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Fri, 19 Feb 2016 10:53 pm, wrong.addres...@gmail.com wrote: >> See http://nedbatchelder.com/text/python-parsers.html for a list of >> parsers that can do all sorts for you. Or the stdlib re module > > I am an engineer, and do not understand most of the terminology used > there. Google, or the search engine of your choice, is your friend. https://duckduckgo.com/html/ https://startpage.com/eng/? Wikipedia even more so: Wikipedia has lots of good, useful articles on computing concepts. https://en.wikipedia.org/wiki/Category:Computing And use the search box visible at the top of every page. Or ask here. > And do I need something fancy to read a line of numbers? Should it > not be built into the language? In your own words: "I will have to read data given to me by people, which may not come in nice formats like CSV" I trust that you don't expect the language to come with pre-written functions to read numbers in every imaginable format. If you do, you will be disappointed -- no language could possible do this. To answer your question "Do I need something fancy...?", no, of course not, reading a line of numbers from a text file is easy. with open("numbers.txt", "r") as f: for line in f: numbers = [int(s) for s in split(line)] will read and convert integer-valued numbers separated by whitespace like: 123 654 9374 1 -45 3625 one line at a time. You can then collate them for later use, or process them as you go. If they're floating point numbers, change the call to int() to a call to float(). -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Guido on python3 for beginners
On Thu, 18 Feb 2016 07:40 pm, Terry Reedy wrote: > 8. 2.x has two subtlely different types of classes. The 2.x docs do not > document the type of builtin and stdlib classes. I discovered that > tkinter classes are still old-style in 2.7 when I backported a patch > from 3.x to 2.7 and it mysteriously did not work. Py 3 wins here. To > me, this alone makes 2.x a bad choice for most beginners. An excellent point! > 11. To test is something is text, isinstance s, c), where 'c' is one of > str, bytes, unicode, basestring, (bytes, unicode), (str, unicode). +1 > for 3.x. In Python 3, it should be much rarer to want to test whether something is unicode or bytes. You can't combine them easily, so you rarely want to handle them in identical ways. > 12. 2.7 has two different open' functions, open and io.open. In 3.x > these are the same opjects. I believe there are other 3.x backports > like this. There's also codecs.open(). -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
Paul Rubin : > Marko Rauhamaa writes: >> "Frank Millman" : >>> I would love to drive the database asynchronously, but of the three >>> databases I use, only psycopg2 seems to have asyncio support. >> Yes, asyncio is at its infancy. There needs to be a moratorium on >> blocking I/O. > > Unfortunately there appears to be no way to open a file in Linux > without at least potentially blocking (slow disk or whatever). You > need separate threads or processes to do the right thing. I have been wondering about the same thing. It would appear that disk I/O is considered nonblocking at a very deep level: * O_NONBLOCK doesn't have an effect * a process waiting for the disk to respond cannot receive a signal * a process waiting for the disk to respond stays in the "ready" state Note that * most disk I/O operates on a RAM cache that is flushed irregularly * memory mapping and swapping make disk I/O and RAM access two sides of the same coin * page faults can turn any assembly language instruction into a blocking disk I/O operation * ordinary disks don't provide for much parallelism; processes are usually serialized for disk I/O If the file system happens to be NFS, a networking issue may paralyze the whole system. ... On the networking side, there is also a dangerous blocking operation: socket.getaddrinfo() (and friends). As a consequence, socket.bind(), socket.connect() may block indefinitely. In fact, even asyncio's BaseEventLoop.create_server() and BaseEventLoop.create_sonnection() may block indefinitely without yielding. SEE ALSO getaddrinfo_a(3) Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
On Sat, Feb 20, 2016 at 12:57 AM, Chris Angelico wrote: > On Sat, Feb 20, 2016 at 6:48 PM, Ian Kelly wrote: >> As another point that happens to be fresh in my mind, awaiting a >> Future on which an exception gets set is supposed to propagate the >> exception. I recently found that this breaks if the exception in >> question happens to be StopIteration (granted not one that should >> generally be allowed to propagate anyway, but that's a separate >> discussion) for the simple reason that raising StopIteration in a >> generator is equivalent to returning None. > > Solved by PEP 479. Use "from __future__ import generator_stop" to save > yourself the pain. Nope. py> from __future__ import generator_stop py> import asyncio py> async def test_coro(): ... fut = asyncio.Future() ... fut.set_exception(StopIteration()) ... print('received %r' % await fut) ... py> list(test_coro().__await__()) received None [] I think because __future__ imports are per-file, and asyncio.Future.__iter__ is defined in a file outside my control that doesn't have the __future__ import. I suppose that when the generator_stop behavior becomes standard then it will work, but still that will just cause a RuntimeError to propagate instead of the desired StopIteration. It's not really that big a deal since there is a code smell to it, but it's surprising since intuitively StopIteration should have no special meaning to a PEP 492 coroutine (it's not an iterator, wink wink, nudge nudge), and the thing being awaited is a Future, which also doesn't intuitively look like an iterator. Note that if you just call Future.result(), then the exception propagates as expected; it's just awaiting it that doesn't work. -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
jenswaelk...@gmail.com wrote: > File "/usr/lib/python2.7/decimal.py", line 3744, in >_numbers.Number.register(Decimal) >AttributeError: 'module' object has no attribute 'Number' Your decimal module seems broken. Confirm that in the Python shell: import numbers print numbers.Number I'm guessing you'll get: AttributeError: 'module' object has no attribute 'Number' It appears to be the usual decimal module of the current linux system python 2.7, so a corrupt hard disk? -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
Marko Rauhamaa writes: > It would appear that disk I/O is considered nonblocking at a very deep > level: > * O_NONBLOCK doesn't have an effect > * a process waiting for the disk to respond cannot receive a signal > * a process waiting for the disk to respond stays in the "ready" state You can handle those issues with AIO. It's open(2) that seems to have no asynchronous analog as far as I can tell. Actually, looking at the AIO man pages, it appears that the Linux kernel currently doesn't support it and it's instead simulated by a userspace library using threads. I didn't realize that before. But AIO is at least specified by POSIX, and there was some kernel work (io_setup(2) etc.) that may or may not still be in progress. It also doesn't have an open(2) analog, sigh. > On the networking side, there is also a dangerous blocking operation: > socket.getaddrinfo() (and friends). As a consequence, socket.bind(), > socket.connect() may block indefinitely. In fact, even asyncio's > BaseEventLoop.create_server() and BaseEventLoop.create_sonnection() may > block indefinitely without yielding. getaddrinfo is a notorious pain but I think it's just a library issue; an async version should be possible in principle. How does Twisted handle it? Does it have a version? I've just felt depressed whenever I've looked at any Python async stuff. I've written many Python programs with threads and not gotten into the trouble that people keep warning about. But I haven't really understood the warnings, so maybe they know something I don't. I just write in a multiprocessing style, with every mutable object owned by exactly one thread and accessed only by RPC through queues, sort of a poor man's Erlang. There's a performance hit but there's a much bigger one from using Python in the first place, so I just live with it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Guido on python3 for beginners
On 02/19/2016 06:36 PM, Steven D'Aprano wrote: On Fri, 19 Feb 2016 02:39 pm, Rustom Mody wrote: [snip] But you can't do anything interesting with this language, so it is not satisfying. On the other hand, here's "Hello World" in another language, one which is Turing complete so it can do anything Python or C can do: (=<`#9]~6ZY32Vx/4Rs+0No-&Jk)"Fh}|Bcy?`=*z]Kw%oG4UUS0/@-ejc(:'8dc but the learning curve is steep enough that it will be frustrating rather than interesting. https://en.wikipedia.org/wiki/Malbolge Somewhat OT, but speaking of "Hello World", check out: http://www2.latech.edu/~acm/HelloWorld.shtml It's a collection of "Hello World" programs in umpteen languages. (Well, not really umpteen, but there are a lot of them.) ;-) -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
On 20/02/2016 07:42, jenswaelk...@gmail.com wrote: When I use either of the following commands I get an error for which I don't have a solution, could someone here help me further? These are the commands: import matplotlib.pyplot as plt Are you certain that this is what you typed? C:\Users\Mark\Desktop>py -2.7 Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import matplotlib.pyplot as plt >>> Please have another go. or from matplotlib.pyplot import pyplot as plt The above is incorrect, you are trying to import 'pyplot' from 'matplotlib.pyplot'. This is the error I get: Traceback (most recent call last): File "/home/waelkens/matplotlibdemo.py", line 2, in from matplotlib.pyplot import pyplot as plt File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 27, in import matplotlib.colorbar File "/usr/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 36, in import matplotlib.contour as contour File "/usr/lib/python2.7/dist-packages/matplotlib/contour.py", line 17, in import matplotlib.ticker as ticker File "/usr/lib/python2.7/dist-packages/matplotlib/ticker.py", line 152, in import decimal File "/usr/lib/python2.7/decimal.py", line 3744, in _numbers.Number.register(Decimal) AttributeError: 'module' object has no attribute 'Number' thanks in advance Jens With this type of problem I'm inclined to throw the last line of the traceback, here the 'AttributeError' one, into a search engine and see what you come up with. The hits you get are often enough to help you diagnose the problem. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
On Sat, Feb 20, 2016 at 7:14 PM, Ian Kelly wrote: > On Sat, Feb 20, 2016 at 12:57 AM, Chris Angelico wrote: >> On Sat, Feb 20, 2016 at 6:48 PM, Ian Kelly wrote: >>> As another point that happens to be fresh in my mind, awaiting a >>> Future on which an exception gets set is supposed to propagate the >>> exception. I recently found that this breaks if the exception in >>> question happens to be StopIteration (granted not one that should >>> generally be allowed to propagate anyway, but that's a separate >>> discussion) for the simple reason that raising StopIteration in a >>> generator is equivalent to returning None. >> >> Solved by PEP 479. Use "from __future__ import generator_stop" to save >> yourself the pain. > > Nope. > > py> from __future__ import generator_stop > py> import asyncio > py> async def test_coro(): > ... fut = asyncio.Future() > ... fut.set_exception(StopIteration()) > ... print('received %r' % await fut) > ... > py> list(test_coro().__await__()) > received None > [] > > I think because __future__ imports are per-file, and > asyncio.Future.__iter__ is defined in a file outside my control that > doesn't have the __future__ import. You need the future directive in the file that defines the function that raises, so I guess you'd need to apply that to an asyncio call. The tricky bit here is that it's a backward compatibility change, but since asyncio is flagged provisional, I suspect the future directive could be added (anyone who's depending on set_exception(StopIteration()) to terminate without an exception will have to change code). Actually, that mightn't be a bad thing. Maybe raise that as a tracker issue? I just tested, and slapping "from __future__ import generator_stop" at the top of Lib/asyncio/futures.py causes your example to raise an exception instead of returning None, and doesn't seem to break the test suite. > I suppose that when the generator_stop behavior becomes standard then > it will work, but still that will just cause a RuntimeError to > propagate instead of the desired StopIteration. That then becomes a pretty minor wart, on par with hash() never returning -1 (which I came across recently, but only by snooping the source) - it'll hack it to -2 instead, because -1 is used as an error signal. In the same way, StopIteration is special-cased as a return signal (because there needs to be _some_ mechanism for distinguishing between yield and raise and return; normally, the difference between "has a value to return" and "has no value to return" is indicated by raising in the latter case, but now we need even more distinguishments), it can't actually be raised per se. Since the exception chains, you can't get confused. > It's not really that big a deal since there is a code smell to it, but > it's surprising since intuitively StopIteration should have no special > meaning to a PEP 492 coroutine (it's not an iterator, wink wink, nudge > nudge), and the thing being awaited is a Future, which also doesn't > intuitively look like an iterator. Note that if you just call > Future.result(), then the exception propagates as expected; it's just > awaiting it that doesn't work. Definitely seems like it should be fixed, then; the current behaviour is that Future.result() raises RuntimeError if you raise StopIteration, so having await do the same would make sense. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
It occurs to me now that the trackback might misidentify the module in use, if say, you'd named a file "numbers.py" then got rid of it later leaving a "numbers.pyc" somewhere. If so, see where it is: import numbers print numbers.__file__ -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
On Sat, Feb 20, 2016 at 7:37 PM, Paul Rubin wrote: > getaddrinfo is a notorious pain but I think it's just a library issue; > an async version should be possible in principle. How does Twisted > handle it? Does it have a version? In a (non-Python) program of mine, I got annoyed by synchronous name lookups, so I hacked around it: instead of using the regular library functions, I just do a DNS lookup directly (which can then be event-based - send a UDP packet, get notified when a UDP packet arrives). Downside: Ignores /etc/nsswitch.conf and /etc/hosts, and goes straight to the name server. Upside: Is able to do its own caching, since the DNS library gives me the TTLs, but gethostbyname/getaddrinfo won't. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
Chris Angelico : > In a (non-Python) program of mine, I got annoyed by synchronous name > lookups, so I hacked around it: instead of using the regular library > functions, I just do a DNS lookup directly (which can then be > event-based - send a UDP packet, get notified when a UDP packet > arrives). Downside: Ignores /etc/nsswitch.conf and /etc/hosts, and > goes straight to the name server. Upside: Is able to do its own > caching, since the DNS library gives me the TTLs, but > gethostbyname/getaddrinfo won't. Ditto in a Python program of mine, although I don't bother with caching: the DNS server is perfectly capable of caching the entries for me. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
On Sat, Feb 20, 2016 at 7:59 PM, Marko Rauhamaa wrote: > Chris Angelico : > >> In a (non-Python) program of mine, I got annoyed by synchronous name >> lookups, so I hacked around it: instead of using the regular library >> functions, I just do a DNS lookup directly (which can then be >> event-based - send a UDP packet, get notified when a UDP packet >> arrives). Downside: Ignores /etc/nsswitch.conf and /etc/hosts, and >> goes straight to the name server. Upside: Is able to do its own >> caching, since the DNS library gives me the TTLs, but >> gethostbyname/getaddrinfo won't. > > Ditto in a Python program of mine, although I don't bother with caching: > the DNS server is perfectly capable of caching the entries for me. If you know you have a local DNS server, sure. Mine is written for a generic situation where that can't be depended on, so it caches itself. But it's no big deal. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
On Sat, Feb 20, 2016 at 1:49 AM, Chris Angelico wrote: > Definitely seems like it should be fixed, then; the current behaviour > is that Future.result() raises RuntimeError if you raise > StopIteration, so having await do the same would make sense. Future.result() itself simply raises the StopIteration. If you call it inside a coroutine (which seems odd -- why not await it?) then it's the coroutine that reraises the StopIteration as RuntimeError. The __future__ import doesn't even seem to be needed in this case. -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
Dave Farrance wrote: >It occurs to me now that the trackback might misidentify the module in >use, if say, you'd named a file "numbers.py" then got rid of it later >leaving a "numbers.pyc" somewhere. If so, see where it is: > >import numbers >print numbers.__file__ I seem to have "numbers" on the brain. Replace with "decimal", of course. -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
On Sat, Feb 20, 2016 at 1:49 AM, Chris Angelico wrote: > Actually, that mightn't be a bad thing. Maybe raise that as a tracker > issue? I just tested, and slapping "from __future__ import > generator_stop" at the top of Lib/asyncio/futures.py causes your > example to raise an exception instead of returning None, and doesn't > seem to break the test suite. I added your suggestion to the existing http://bugs.python.org/issue26221. -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
Paul Rubin : > I've just felt depressed whenever I've looked at any Python async > stuff. I've written many Python programs with threads and not gotten > into the trouble that people keep warning about. Programming-model-wise, asyncio is virtually identical with threads. In each, I dislike the implicit state concept. I want the state to stand out with big block letters. > I've just felt depressed whenever I've looked at any Python async > stuff. I've written many Python programs with threads and not gotten > into the trouble that people keep warning about. But I haven't really > understood the warnings, so maybe they know something I don't. I just > write in a multiprocessing style, with every mutable object owned by > exactly one thread and accessed only by RPC through queues, sort of a > poor man's Erlang. There's a performance hit but there's a much bigger > one from using Python in the first place, so I just live with it. Good for you if you have been able to choose your own programming model. Most people have to deal with a legacy mess. Also, maintainers who inherit your tidy code might not be careful to ship only nonmutable objects in the queues. Your way of using threads works, of course, with the caveat that it is not possible to get rid of a blocking thread from the outside. With asyncio, you can at least cancel tasks. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: extending PATH on Windows?
On Fri, Feb 19, 2016 at 6:57 PM, Dennis Lee Bieber wrote: > > Problem -- if the OP's PATH had contained the location of the add2path > script, the OP might not have needed to search for it... (presuming their > configuration also was set up to treat .py files as executable, entering > win_add2path would be all that was needed to run it on a command line) Ulli didn't need to run the file, but to read it as example code. > "where" only searches the locations defined in PATH I made it clear in several examples that where.exe is not limited to searching PATH. -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
> getaddrinfo is a notorious pain but I think it's just a library issue; an async version should be possible in principle. How does Twisted handle it? Does it have a version? I think we're a little outside the scope of OP's question at this point, but for the sake of answering this: There are a few cases that I know of where Twisted uses the standard lib socket DNS methods. One is when resolving names to IPv6 addresses [1] when creating a client connection to a remote source. The other is in the default DNS resolver that is installed in the reactor [2]. Creating client connections allows the call to 'getaddrinfo' to block without mitigation. The default DNS resolver, unfortunately, dispatches calls of 'gethostbyname' to a thread pool. Without seeing the commit history, I'd assume the use of 'socket' and threads by default is an artifact that predates the implementation of the DNS protocol in Twisted. Twisted has, in 'twisted.names' [3], a DNS protocol that uses UDP and leverages the reactor appropriately. Thankfully, Twisted has a reactor method called 'installResolver' [4] that allows you to hook in any DNS resolver implementation you want so you aren't stuck using the default, threaded implementation. As far as asyncio, it also defaults to an implementation that delegates to an executor (default: threadpool). Unlike Twisted, though, it appears to require a subclass of the event loop to override the 'getaddrinfo' method [5]. [1] https://github.com/twisted/twisted/blob/trunk/twisted/internet/tcp.py#L622 [2] https://github.com/twisted/twisted/blob/trunk/twisted/internet/base.py#L257 [3] https://github.com/twisted/twisted/tree/trunk/twisted/names [4] https://github.com/twisted/twisted/blob/trunk/twisted/internet/base.py#L509 [5] https://github.com/python/cpython/blob/master/Lib/asyncio/base_events.py#L572 On Sat, Feb 20, 2016, 03:31 Marko Rauhamaa wrote: > Paul Rubin : > > > I've just felt depressed whenever I've looked at any Python async > > stuff. I've written many Python programs with threads and not gotten > > into the trouble that people keep warning about. > > Programming-model-wise, asyncio is virtually identical with threads. In > each, I dislike the implicit state concept. I want the state to stand > out with big block letters. > > > I've just felt depressed whenever I've looked at any Python async > > stuff. I've written many Python programs with threads and not gotten > > into the trouble that people keep warning about. But I haven't really > > understood the warnings, so maybe they know something I don't. I just > > write in a multiprocessing style, with every mutable object owned by > > exactly one thread and accessed only by RPC through queues, sort of a > > poor man's Erlang. There's a performance hit but there's a much bigger > > one from using Python in the first place, so I just live with it. > > Good for you if you have been able to choose your own programming model. > Most people have to deal with a legacy mess. Also, maintainers who > inherit your tidy code might not be careful to ship only nonmutable > objects in the queues. > > Your way of using threads works, of course, with the caveat that it is > not possible to get rid of a blocking thread from the outside. With > asyncio, you can at least cancel tasks. > > > Marko > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
I can't install python on my pc
I can’t install python on my pc because it encounters an error every time I try to install it. Please help as soon as possible. Sent from Mail for Windows 10 -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't install python on my pc
On 20/02/2016 07:34, Giriprasadh Raghavan wrote: I can’t install python on my pc because it encounters an error every time I try to install it. Please help as soon as possible. Sent from Mail for Windows 10 Asked and answered repeatedly over the last few months, so please search the archives for the answer to your specific situation, whatever that may be. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Wednesday, February 17, 2016 at 2:49:44 PM UTC-5, wrong.a...@gmail.com wrote: > I am mostly getting positive feedback for Python. > ... I'm surprised no one has mentioned jupyter yet, so here goes ... A browser-based notebook, see http://www.jupyter.org I think this is an unparalleled way to learn Python, by experimentation at the start and for development later. As an example, I wanted to do some table processing, so a couple of lines to experiment with Pandas reading CSV files, then a couple of more lines to start processing the data ... What a wonderful way to experimentally develop software ... Can even develop simple GUIs *really* easily (but packaging for distribution is probably not easy, unless distribution is only within an organization and that organization can run a private notebook server). If you want to try it, this page http://jupyter.readthedocs.org/en/latest/install.html recommends installing Anaconda Python which installs a *lot* of stuff (numpy, scipy, sympy, pandas, etc. etc.) I highly recommend both (I use them for Civil Engineering (structures) software and teach a related university course) -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
Op zaterdag 20 februari 2016 09:50:05 UTC+1 schreef Dave Farrance: > It occurs to me now that the trackback might misidentify the module in > use, if say, you'd named a file "numbers.py" then got rid of it later > leaving a "numbers.pyc" somewhere. If so, see where it is: > > import numbers > print numbers.__file__ Dear Farrance, You have solved my problem, there was indeed a program which I had called numbers.py and a numbers.pyc which caused this behaviour. Thanks a lot for helping me. kind regards, Jens -- https://mail.python.org/mailman/listinfo/python-list
Re: [newbie] Problem with matplotlib
Op zaterdag 20 februari 2016 09:43:35 UTC+1 schreef Mark Lawrence: > On 20/02/2016 07:42, jenswaelk...@gmail.com wrote: > > When I use either of the following commands I get an error for which I > > don't have a solution, could someone here help me further? > > These are the commands: > > import matplotlib.pyplot as plt > > Are you certain that this is what you typed? > > C:\Users\Mark\Desktop>py -2.7 > Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit > (AMD64)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> import matplotlib.pyplot as plt > >>> > > Please have another go. > > > > > or > > > > from matplotlib.pyplot import pyplot as plt > > The above is incorrect, you are trying to import 'pyplot' from > 'matplotlib.pyplot'. You are right, this doesn't work, I got it from some source on the Internet tryin to solve the initial problem with the other statement...Anyway, as you see in my other reply, the problem has been solved. kind regards, Jens > > > > > This is the error I get: > > Traceback (most recent call last): > >File "/home/waelkens/matplotlibdemo.py", line 2, in > > from matplotlib.pyplot import pyplot as plt > >File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 27, > > in > > import matplotlib.colorbar > >File "/usr/lib/python2.7/dist-packages/matplotlib/colorbar.py", line 36, > > in > > import matplotlib.contour as contour > >File "/usr/lib/python2.7/dist-packages/matplotlib/contour.py", line 17, > > in > > import matplotlib.ticker as ticker > >File "/usr/lib/python2.7/dist-packages/matplotlib/ticker.py", line 152, > > in > > import decimal > >File "/usr/lib/python2.7/decimal.py", line 3744, in > > _numbers.Number.register(Decimal) > > AttributeError: 'module' object has no attribute 'Number' > > > > thanks in advance > > Jens > > > > With this type of problem I'm inclined to throw the last line of the > traceback, here the 'AttributeError' one, into a search engine and see > what you come up with. The hits you get are often enough to help you > diagnose the problem. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
Hello there, I realize that this discussion of supporting asynchronous name lookup requests in DNS is merely a detour in this thread on asyncio, but I couldn't resist mentioning an existing tool. >> getaddrinfo is a notorious pain but I think it's just a library >> issue; an async version should be possible in principle. How >> does Twisted handle it? Does it have a version? > >In a (non-Python) program of mine, I got annoyed by synchronous >name lookups, so I hacked around it: instead of using the regular >library functions, I just do a DNS lookup directly (which can then >be event-based - send a UDP packet, get notified when a UDP packet >arrives). Downside: Ignores /etc/nsswitch.conf and /etc/hosts, and >goes straight to the name server. Upside: Is able to do its own >caching, since the DNS library gives me the TTLs, but >gethostbyname/getaddrinfo won't. Another (non-Python) DNS name lookup library that does practically the same thing (along with the shortcomingsn you mentioned, Chris: no NSS nor /etc/hosts) is the adns library. Well, it is DNS, after all. http://www.gnu.org/software/adns/ https://pypi.python.org/pypi/adns-python/1.2.1 And, there are Python bindings. I have been quite happy using the adns tools (and tools built on the Python bindings) for mass lookups (millions of DNS names). It works very nicely. Just sharing knowledge of an existing tool, -Martin -- Martin A. Brown http://linux-ip.net/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Saturday, 20 February 2016 09:49:17 UTC+2, Larry Hudson wrote: > On 02/19/2016 10:14 AM, wrong.addres...@gmail.com wrote: > [snip] > > > > This is precisely reading one character at a time. If not exactly reading > > one character, it is effectively looking at each character to assemble the > > number. Not a good sign. I guess there might be libraries which will help > > read numbers better, but I would expect a good language to be able to > > handle this basic thing for engineers - to read numbers like Fortran and > > Basic do. > > > > Still, if I have misunderstood something, I will be glad to be corrected. I > > would generally know that the first three numbers will be floating point, > > the next will be something and then the next will be something else, etc. > > Does that help or does one still have to look at each character and > > determine how to place it in a number? > > > > Thanks for your patience. I don't want surprises later on, which is why I > > am asking very stupid questions. > > > It absolutely does NOT require reading a character at a time! You are > reading the data from a > text file, which means everything is a string. These strings (or > sub-strings) can represent > integers, floats, dates, phone numbers or whatever. Your example data > implies a free-form > style, where it is then necessary to determine the data type for each of > these (sub)strings > individually. Of course, if your data has a defined fixed format, this is > simplified -- but > they are still initially strings that have to be converted. String > processing in Python is very > powerful and versatile. > > BTW, it does no good to continue to think strictly in BASIC techniques. > Python is a different > language with a different approach to attacking problems. If you try to > write BASIC (or C or > Java or ...) programs in Python syntax you'll just get bad programs. Forget > trying to find > features in Python that are identical to the features of BASIC. Python > requires a different > mind-set to use it properly -- just like any other language. > > Here is a rather naive somewhat brute-force way to read your example data. > I'm using a Python > list here to simulate the file reading. (Actually, using read() instead of > readline() will also > give this list.) Python lists are essentially the same as arrays in other > languages, but much > more powerful and versatile. Two examples of the differences between arrays > and lists are: can > use mixed data types, you are not restricted to all the same data type, and > the size of lists > are dynamic -- they grow or shrink as necessary. > > Comments start with a # > Strings in Python can be delimited by single-quotes ('), double-quotes (") or > triple-quotes (""" > or '''). Triple-quoted strings can be multi-line text. The triple-quote > here is used as what > Python calls a docstring, which it saves internally for documenting your > program. > > > # Define a function to determine the data type a string represents > def chktyp(s): > """Check string s for int, float or string. Returns the data and a type > code""" > try: > return int(s), 'int' # Try to convert to int, return it if > successful > except ValueError:# No, it's not an int > pass # Drop into float test > try: > return float(s), 'float' # Try to convert to float, return it if > successful > except ValueError:# No, it's not a float > return s, 'str' # It must be a string > > # Here is your (simulated) data as a list of strings > data = [ > '2 12.657823 0.1823467E-04 114 0', > '3 4 5 9 11', > '"Lower"',# Could be left simply as "Lower" > '278.15'] > > # Process the data > for line in data: # For each line of the data > dat = line.split()# Make a list of the sub-strings in the > line > for stng in dat: # For each substring > dt, tp = chktyp(stng) # Get the data and the type > print('{} is a {}'.format(dt, tp))# Print this data > > > Running this example gives this result: > > 2 is a int > 12.657823 is a float > 1.823467e-05 is a float > 114 is a int > 0 is a int > 3 is a int > 4 is a int > 5 is a int > 9 is a int > 11 is a int > "Lower" is a str > 278.15 is a float > > I hope this gives you a slight hint about the Python way of doing things. > Yes, of course it's > very different from BASIC, but if you can pick up on the Pythonic way of > doing things I think > you might at least find it useful. > > -=- Larry -=- I realise that this file i/o will have to be thought of in a different way if I start to use Python. This may still be worthwhile inspite of the complexity and loss in readability of the code. To give an example of the kind of things I have to read (and I have
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Saturday, 20 February 2016 09:54:15 UTC+2, Steven D'Aprano wrote: > On Fri, 19 Feb 2016 10:53 pm, wrong.addres...@gmail.com wrote: > > >> See http://nedbatchelder.com/text/python-parsers.html for a list of > >> parsers that can do all sorts for you. Or the stdlib re module > > > > I am an engineer, and do not understand most of the terminology used > > there. > > Google, or the search engine of your choice, is your friend. > > https://duckduckgo.com/html/ > > https://startpage.com/eng/? > > > Wikipedia even more so: Wikipedia has lots of good, useful articles on > computing concepts. > > https://en.wikipedia.org/wiki/Category:Computing > > And use the search box visible at the top of every page. > > Or ask here. > > > > > And do I need something fancy to read a line of numbers? Should it > > not be built into the language? > > In your own words: > > "I will have to read data given to me by people, which may not come in nice > formats like CSV" > > I trust that you don't expect the language to come with pre-written > functions to read numbers in every imaginable format. If you do, you will > be disappointed -- no language could possible do this. > > To answer your question "Do I need something fancy...?", no, of course not, > reading a line of numbers from a text file is easy. > > with open("numbers.txt", "r") as f: > for line in f: > numbers = [int(s) for s in split(line)] > This looks good enough. This does not seem complicated (although I still understand almost nothing of what is written). I was simply not expressing myself in a way you people would understand. > > will read and convert integer-valued numbers separated by whitespace like: > > 123 654 9374 1 -45 3625 > > > one line at a time. You can then collate them for later use, or process them > as you go. If they're floating point numbers, change the call to int() to a > call to float(). > And I guess if the first three numbers are integers and the fourth is a float, then also there must be some equally straightforward way. Thanks for this explanation, which changes my view about reading numbers in Python. > > > > > > -- > Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Saturday, 20 February 2016 18:23:26 UTC+2, nholtz wrote: > On Wednesday, February 17, 2016 at 2:49:44 PM UTC-5, wrong.a...@gmail.com > wrote: > > I am mostly getting positive feedback for Python. > > ... > > I'm surprised no one has mentioned jupyter yet, so here goes ... > > A browser-based notebook, see http://www.jupyter.org > > I think this is an unparalleled way to learn Python, by experimentation at > the start and for development later. As an example, I wanted to do some table > processing, so a couple of lines to experiment with Pandas reading CSV files, > then a couple of more lines to start processing the data ... What a wonderful > way to experimentally develop software ... > > Can even develop simple GUIs *really* easily (but packaging for distribution > is probably not easy, unless distribution is only within an organization and > that organization can run a private notebook server). > > If you want to try it, this page > > http://jupyter.readthedocs.org/en/latest/install.html > > recommends installing Anaconda Python which installs a *lot* of stuff (numpy, > scipy, sympy, pandas, etc. etc.) > > I highly recommend both (I use them for Civil Engineering (structures) > software and teach a related university course) Thanks. This gives me some more confidence that it will be a good choice for my work as well. -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On Saturday, 20 February 2016 06:58:39 UTC+2, Denis Akhiyarov wrote: > On Wednesday, February 17, 2016 at 1:49:44 PM UTC-6, wrong.a...@gmail.com > wrote: > > I am mostly getting positive feedback for Python. > > > > It seems Python is used more for web based applications. Is it equally fine > > for creating stand-alone *.exe's? Can the same code be compiled to run on > > Linux or Android or web-based? > > > > Is it possible to create GUI elements with a good IDE? Can they be defined > > like in Visual Basic with given sizes, fonts, visible/invisible, etc.? > > > > Is it easy to do matrix operations in Python? Or do I need to write > > subroutines like in Visual Basic? > > > > Could someone kindly tell me advantages and disadvantages of Python? Or any > > better options? I have like 40-50 VB Forms and may be around 2 lines of > > code. It will be a task to learn a new language and translate/re-write that > > code. > > > > Thanks for your responses. > > I'm surprised that no one mentioned this tool called vb2py. It looks > outdated, but I actually used it successfully to convert vba code to python, > once all dependencies were installed correctly :) > > http://vb2py.sourceforge.net/ > > You can also understand how vb objects map to python objects. > > vb2py has also minimal support for GUI conversion. > > Someone has even forked it on github recently: > > https://github.com/reingart/vb2py This will definitely be useful to me in the early phase. -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
Am 20.02.16 um 19:45 schrieb wrong.addres...@gmail.com: On Saturday, 20 February 2016 09:54:15 UTC+2, Steven D'Aprano wrote: To answer your question "Do I need something fancy...?", no, of course not, reading a line of numbers from a text file is easy. with open("numbers.txt", "r") as f: for line in f: numbers = [int(s) for s in split(line)] This looks good enough. This does not seem complicated (although I still understand almost nothing of what is written). I was simply not expressing myself in a way you people would understand. will read and convert integer-valued numbers separated by whitespace like: 123 654 9374 1 -45 3625 one line at a time. You can then collate them for later use, or process them as you go. If they're floating point numbers, change the call to int() to a call to float(). And I guess if the first three numbers are integers and the fourth is a float, then also there must be some equally straightforward way. If you know in advance, what you expect, then it is easy. Most people who gave you answers assumed that you have a messy file and don't know if an int or float follows, and you want a program which decides by itself whether to read an int, float or string. Whereas, if the format is fixed, for 3 ints and 1 float, you could do: str='1 2 3 3.14159' # some example fmt=(int,int,int,float) # define format a,b,c,d=[type(x) for (type,x) in zip(fmt, str.split())] It's one line, but it might look involved and actually it uses a list comprehension, tuple unpacking and first class functions, so it's certainly not comprehensible from the first Python lesson. Another alternative would be sscanf. This is a 3rd party module which simulates C sscanf, optimized more or less for this kind of number parsing: https://hkn.eecs.berkeley.edu/~dyoo/python/scanf/ After installing that, you can do: from scanf import sscanf str='1 2 3 3.14159' a,b,c,d=sscanf(str, '%d %d %d %f') whereby '%d' means integer and '%f' is float. sscanf also handles fixed-width columns which you often get from Fortran programs. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't install python on my pc
On 20 Feb 2016 14:05, "Giriprasadh Raghavan" wrote: > > > I can’t install python on my pc because it encounters an error every time I try to install it. Please help as soon as possible. You need to give more information than that to get clear help. What error do you see? If there is an error message what does it say? What exactly do you do that results in the message? -- https://mail.python.org/mailman/listinfo/python-list
Re: asyncio - run coroutine in the background
On Sun, Feb 21, 2016 at 4:45 AM, Martin A. Brown wrote: > Another (non-Python) DNS name lookup library that does practically > the same thing (along with the shortcomingsn you mentioned, Chris: > no NSS nor /etc/hosts) is the adns library. Well, it is DNS, after > all. > > http://www.gnu.org/software/adns/ > https://pypi.python.org/pypi/adns-python/1.2.1 > > And, there are Python bindings. I have been quite happy using the > adns tools (and tools built on the Python bindings) for mass lookups > (millions of DNS names). It works very nicely. > > Just sharing knowledge of an existing tool, > Ultimately, anything that replaces a gethostbyname/getaddrinfo call with an explicit DNS lookup is going to have the exact same benefit and downside: lookups won't freeze the program, but you can't use /etc/hosts any more. (Slightly sloppy language but that's how an end user will see it.) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
wrong.addres...@gmail.com schreef op 2016-02-19 11:47: Thanks. The data I will often have to read from text files could read like 2 12.657823 0.1823467E-04 114 0 3 4 5 9 11 "Lower" 278.15 Is it straightforward to read this, or does one have to read one character at a time and then figure out what the numbers are? The question is: what is known about the format of the data? Is it fixed, or does the code need to be smart enough to be able to deal with varying formats? If we can assume that there are 4 lines, of which the first line contains floating point numbers, the second contains integers, the third contains one string and the fourth contains one floating point number, it's pretty easy. For example: def readinput(filename): with open(filename, 'rt') as f: lines = f.readlines() line0 = [float(part) for part in lines[0].split()] line1 = [int(part) for part in lines[1].split()] line2 = lines[2].strip()[1:-1] line3 = float(lines[3]) return line0, line1, line2, line3 line0, line1, line2, line3 = readinput('input.txt') print(line0) print(line1) print(line2) print(line3) Output: [2.0, 12.657823, 1.823467e-05, 114.0, 0.0] [3, 4, 5, 9, 11] Lower 278.15 This basically splits the two first line by whitespace, then converts each part (or the whole line in case of the last two lines) into the desired data type. -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On 2/19/2016 8:58 PM, Denis Akhiyarov wrote: On Wednesday, February 17, 2016 at 1:49:44 PM UTC-6, wrong.a...@gmail.com wrote: I am mostly getting positive feedback for Python. It seems Python is used more for web based applications. Is it equally fine for creating stand-alone *.exe's? Can the same code be compiled to run on Linux or Android or web-based? Is it possible to create GUI elements with a good IDE? Can they be defined like in Visual Basic with given sizes, fonts, visible/invisible, etc.? Is it easy to do matrix operations in Python? Or do I need to write subroutines like in Visual Basic? Could someone kindly tell me advantages and disadvantages of Python? Or any better options? I have like 40-50 VB Forms and may be around 2 lines of code. It will be a task to learn a new language and translate/re-write that code. Thanks for your responses. I'm surprised that no one mentioned this tool called vb2py. > It looks outdated, but I actually used it successfully to convert vba code to python,> once all dependencies were installed correctly :) http://vb2py.sourceforge.net/ You can also understand how vb objects map to python objects. vb2py has also minimal support for GUI conversion. Someone has even forked it on github recently: https://github.com/reingart/vb2py Thanks! I have a lot of VB6 and VB.NET code and I'm learning Python thinking about doing machine learning, this might come in handy for other programs! -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't install python on my pc
On Sat, 20 Feb 2016 06:34 pm, Giriprasadh Raghavan wrote: > > I can’t install python on my pc because it encounters an error every time > I try to install it. Please help as soon as possible. Sent from Mail for > Windows 10 Shall we guess what the error says? My guess is that you are out of disk space. You need to delete some files, or put in a bigger hard drive. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
On Sat, 20 Feb 2016 05:44 pm, Paul Rubin wrote: > But frankly the stuff I'm seeing in this thread makes me sad for Python. > It's an impossible dream but it would be awesome to have Erlang-like > communicating microtasks in Python. "But frankly the stuff I'm seeing in this thread makes me sad for *literally every programming language in existence except for Erlang and maybe one or two others*, which altogether about six people use in total..." :-) -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Considering migrating to Python from Visual Basic 6 for engineering applications
On 02/20/2016 10:38 AM, wrong.addres...@gmail.com wrote: [snip] How complicated could this get in Python? Reading the numbers is one thing, and then placing the values in text boxes of the GUI. If that is the only object of using these values, there is no conversions necessary. The data is read as text (strings), and you could simply write them directly into the text boxes. Of course, this is unlikely -- you surely want the actual numeric values for other purposes. As already pointed out, if you know in advance what the data types are, the necessary conversions are trivial. Just slightly more complex if you don't know the types in advance. OTOH, the conversion from int or float back to strings is also trivial. It's as simple as writing str(n), where n is an int OR a float -- it works automatically with either. (Data typing in Python is dynamic, you generally don't need to specify the types.) But if you want the strings to be in a specific format, this is also possible with a different syntax that lets you specify the output format. As an example, assume the variable val is 1.97834, and the formatting string is "${:.2f}".format(val) -- this will give you the string '$1.98'. This expression breaks down to: $ -> a literal dollar sign {} -> a placeholder, it is where the formatted data will be put : -> what follows is formatting code .2 -> round to, and print, two decimal places f -> the data is a float .format(val) -> the data to format BTW, this leaves the variable val unchanged -- it is NOT rounded, it still holds its original precision. It only affects how it is displayed. You CAN round it if you want, but that's an entirely different function. Naturally, learning all the formatting codes takes some effort, but it allows defining the appearance of the resulting strings in a very detailed and complete manner. [Aside: this is the "new" formatting method. Python also supports an "old" method, which is very much like the way strings are formatted in the C language.] Or can I write my own reading subroutines which can then be called like ReadVBstyle 8, ND, NIN, NT to make the code more readable? ABSOLUTELY!! Most Python programming consists of defining the functions and classes needed, which definitely makes Python more readable. (Classes imply Object Oriented Programming. Python _allows_ OOP but does not _require_ it -- and is irrelevant here. Ignore anything I say about classes and OOP!) For your example here, it wouldn't _match_ the BASIC syntax, but would be used in an equivalent way. In fact, if you find yourself writing functions (or classes) that could be general-purpose routines, it is a trivial matter to put them into a personal library which you can then use in future programs. You don't need to rewrite them, just use them. Now, in practice, it takes a little care to write them as library functions. That is, the original version may rely on some details of the original program, so the library version will need to be tweaked a bit to remove these 'local' dependencies. But this is generally easily handled through the parameters to the functions. Also they are probably written with a bit more care to handle error conditions (which Python calls exceptions, Python has very extensive exception handling). This capability (a personal library) is enormously convenient. While I am saying 'personal', I really mean library(s) available to everyone involved in a programming project. No need for anyone to re-invent the wheel! ;-)Python calls them modules rather than libraries, but it's the same thing. -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: How the heck does async/await work in Python 3.5
Steven D'Aprano writes: > "But frankly the stuff I'm seeing in this thread makes me sad for > *literally every programming language in existence except for Erlang > and maybe one or two others*, which altogether about six people use in > total..." Erlang microtasks are more a matter of the runtime environment than the language. It might be possible to have something like it in PyPy. Other languages typically do concurrency with something like: 1. threads - various hazards that can be avoided if you're careful 2. callback chaining like in node.js - gets messy if the program is complicated, but conceptually simple 3. explicit state machines with something like libevent in C -- same as #2, simple but tedious 4. lightweight threads/tasks like in GHC and Erlang -- very nice though requires a sophisticated runtime system 5. cooperative multasking (small RTOS's, Forth, etc): simple, but apparently bug-prone when the program gets complicated Python's async stuff seems to combine various of the above approaches and (while I'm not saying it's objectively bad) the experiences I've had with it so far have been confusing and unpleasant. I do want to put some effort into understanding asyncio, but so far threads have worked ok for me. OK Web Server (uses a separate C++ process for each page on the site) looked straightforward and fast, though makes some architectural impositions. http://seastar-project.org/ seems interesting but I don't understand it at the moment. -- https://mail.python.org/mailman/listinfo/python-list