Struggling with python-daemon and subprocess module to work together
Hi, I am writing a very basic server side application[0] which get data from a client and create a virtual machine using provided data and also tells client about what's going on during *virt-install* command execution. Previously this basic server is executed using *openvt* but I thought it would be nice to have a daemon process for it and used python-daemon. Issue I am facing is after some time server will stop sending data to client which suppose to happen in #120 and get error message "Cannot run interactive console without a controlling TTY" . I am not able to figure out issue is occurred due to module or I missed something during daemon initialization. [0] http://fpaste.org/143455/ -- Praveen Kumar http://fedoraproject.org/wiki/User:Kumarpraveen http://fedoraproject.org/ http://kumar-pravin.blogspot.com -- https://mail.python.org/mailman/listinfo/python-list
real-life example LC_CTYPE effects?
Hi, The locale category LC_CTYPE may affect character classification and case conversion. That's the theory. Can you give a practical example where this locale setting matters? Eg.: locale.setlocale(locale.LC_CTYPE, loc) m = re.match("\d+", s, re.I | re.L) So with two different values for loc, and s is identical, there will or won't be a match. Thanks! Albert-Jan -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB wrote: Do you have to know the number of items the list will have before making it? No, it is not necessary, lists are NOT the same as arrays in other languages. But it IS possible to create an initial list of a specific size: myList = [None] * 50 That creates a 50-element list with each element set to None. (BTW, the indexes are from 0-49, not 0-50.) I have found this occasionally useful, but I'll emphasize, it's only RARELY useful. The .append() method is far more versatile. As to your original problem: my question to you is what is your purpose? 1) To solve this particular problem, using Python. or 2) To explore the usage of lists, applying them to this problem. If your purpose is the first, then I agree with the advice you have already been given here. Dictionaries are a much better fit to this problem. If your purpose is the second, then go ahead and use this for your exploration. But realize that to more experienced Pythonistas this would be a very un-pythonic approach. Even better would be to try multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or tuples... And any other combinations you can come up with. This will give you even more experience, and allow you to evaluate the different approaches. And no, I will not give you a ready-made "canned" answer. For one thing, your description is too vague to effectively do that. Good luck. -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB wrote: Do you have to know the number of items the list will have before making it? No. Lists are NOT the same as arrays in other languages. But it IS possible to create an initial list of a specific size: myList = [None] * 50 This creates a 50-element list with each element set to None. (BTW, that makes the indexes 0-49, not 0-50.) I have occasionally found this useful, but I emphasize it is only RARELY useful. The .append() method is far more versatile. As to your original problem: my question to you is what is your purpose? 1) To solve this particular problem, using Python. or 2) To explore the usage of lists by applying them to this problem. If your purpose is the first, then I agree with the advice you have already been given here -- dictionaries are a much better fit to this problem. If your purpose is the second, then go ahead, have at it. But realize that to more experienced Pythonistas this approach the very un-pythonic for this problem. It would be even better to try multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or tuples... or whatever combination you can come up with. This will give you even more experience and allow you to evaluate these various techniques. And no, I won't give you a ready-made "canned" answer. For one thing your original description is much too vague. But good luck! -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: When to use assert
On Saturday, November 16, 2013 11:35:50 PM UTC-8, Steven D'Aprano wrote: > The question of when to use the assert statement comes up occasionally, > > usually in response to somebody misusing it, so I thought I'd write a > > post describing when and why to use assertions, and when not to. > > > > For those who aren't aware of it, Python's "assert" checks a condition, > > if it is true it does nothing, and if it is false it raises an > > AssertionError with an optional error message. For example: > > > > py> x = 23 > > py> assert x > 0, "x is not zero or negative" > > py> assert x%2 == 0, "x is not an even number" > > Traceback (most recent call last): > > File "", line 1, in > > AssertionError: x is not an even number > > > > > > Many people use asserts as a quick and easy way to raise an exception if > > an argument is given the wrong value. But this is wrong, dangerously > > wrong, for two reasons. The first is that AssertionError is usually the > > wrong error to give when testing function arguments. You wouldn't write > > code like this: > > > > if not isinstance(x, int): > > raise AssertionError("not an int") > > > > you'd raise TypeError instead. "assert" raises the wrong sort of > > exception. > > > > But, and more dangerously, there's a twist with assert: it can be > > compiled away and never executed, if you run Python with the -O or -OO > > optimization flags, and consequently there is no guarantee that assert > > statements will actually be run. When using assert properly, this is a > > feature, but when assert is used inappropriately, it leads to code that > > is completely broken when running with the -O flag. > > > > When should use assert? In no particular order, assertions should be used > > for: > > > > * defensive programming; > > * runtime checks on program logic; > > * checking contracts (e.g. pre-conditions and post-conditions); > > * program invariants; and > > * checked documentation. > > > > (It's also acceptable to use assert when testing code, as a sort of quick- > > and-dirty poor man's unit testing, so long as you accept that the tests > > simply won't do anything if you run with the -O flag. And I sometimes use > > "assert False" in code to mark code branches that haven't been written > > yet, and I want them to fail. Although "raise NotImplementedError" is > > probably better for that, if a little more verbose.) > > > > Opinions on assertions vary, because they can be a statement of > > confidence about the correctness of the code. If you're certain that the > > code is correct, then assertions are pointless, since they will never > > fail and you can safely remove them. If you're certain the checks can > > fail (e.g. when testing input data provided by the user), then you dare > > not use assert since it may be compiled away and then your checks will be > > skipped. > > > > It's the situations in between those two that are interesting, times when > > you're certain the code is correct but not *quite* absolutely certain. > > Perhaps you've missed some odd corner case (we're all only human). In > > this case an extra runtime check helps reassure you that any errors will > > be caught as early as possible rather than in distant parts of the code. > > > > (This is why assert can be divisive. Since we vary in our confidence > > about the correctness of code, one person's useful assert is another > > person's useless runtime test.) > > > > Another good use for asserts is checking program invariants. An invariant > > is some condition which you can rely on to be true unless a bug causes it > > to become false. If there's a bug, better to find out as early as > > possible, so we make a test for it, but we don't want to slow the code > > down with such tests. Hence assert, which can be turned on in development > > and off in production. > > > > An example of an invariant might be, if your function expects a database > > connection to be open when it starts, and promises that it will still be > > open when it returns, that's an invariant of the function: > > > > def some_function(arg): > > assert not DB.closed() > > ... # code goes here > > assert not DB.closed() > > return result > > > > > > Assertions also make good checked comments. Instead of writing a comment: > > > > # when we reach here, we know that n > 2 > > > > you can ensure it is checked at runtime by turning it into an assert: > > > > assert n > 2 > > > > Assertions are also a form of defensive programming. You're not > > protecting against errors in the code as it is now, but protecting > > against changes which introduce errors later. Ideally, unit tests will > > pick those up, but let's face it, even when tests exist at all, they're > > often incomplete. Build-bots can be down and nobody notices for weeks, or > > people forget to run
Re: Is there an easy way to control indents in Python
On Monday, 20 October 2014 18:56:05 UTC+1, Ian wrote: > Rather, I'm saying that where the blank line is should be the start of > a new function. There would still be a blank line, just no longer > inside the function. > > Now, maybe you think there should be more blank lines in the above, in > which case we'll just have to disagree on that point. Why did you separate the above 2 sequences of thoughts by a blank line? Is the inherent pause in the communication of your thoughts not also applicable to your code? Where we see the pause between thoughts appears to be in a different place. I see them both within the function and between the functions and I assume you see them between the functions only. BTW I'm more than happy to disagree. There is no right or wrong answer unless Guido wants to pronounce on the issue :-J -- https://mail.python.org/mailman/listinfo/python-list
Re: OS X Menubar in Tkinter
On Tuesday, October 21, 2014 12:59:08 AM UTC-5, Mark Lawrence wrote: > On 21/10/2014 02:34, Noble Bell wrote: > I'm pleased to see that you have an answer. In return would you please > > access this list via > > https://mail.python.org/mailman/listinfo/python-list or read and action > > this https://wiki.python.org/moin/GoogleGroupsPython to prevent us > > seeing double line spacing and single line paragraphs, thanks. I was not aware of the issue. Sorry. I will correct my posts in the future. Thanks for pointing it out to me. NB -- https://mail.python.org/mailman/listinfo/python-list
Re: real-life example LC_CTYPE effects?
On Mon, Oct 20, 2014, at 16:33, Albert-Jan Roskam wrote: > Hi, > > The locale category LC_CTYPE may affect character classification and case > conversion. > > That's the theory. Can you give a practical example where this locale > setting matters? Eg.: > locale.setlocale(locale.LC_CTYPE, loc) > m = re.match("\d+", s, re.I | re.L) > > So with two different values for loc, and s is identical, there will or > won't be a match. You're generally isolated from this by using unicode strings - there are only a few unicode characters that have different case mappings in different languages. LC_CTYPE was designed in an era of 8-bit character sets. For example, in a Russian locale with KOI8-R character set, C0-DF are all lowercase letters, and E0-FF are all the uppercase equivalent, whereas in an English or other western european locale with ISO-8859-1, C0-DF [except D7] are all uppercase letters, with the lowercase versions in E0-FF [except F7], and in a Hebrew ISO-8859-8 locale only E0-FA are letters and are not uppercase/lowercase. Try setting the locale to tr_TR and matching "i" against "I", for a demonstration of one of the few remaining effects this can have. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 00:11:38 -0700, Larry Hudson wrote: >On 10/20/2014 12:49 PM, Seymore4Head wrote: >> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB >> wrote: > > > >> Do you have to know the number of items the list will have before >> making it? >> > >No, it is not necessary, lists are NOT the same as arrays in other languages. >But it IS >possible to create an initial list of a specific size: > >myList = [None] * 50 > >That creates a 50-element list with each element set to None. (BTW, the >indexes are from 0-49, >not 0-50.) I have found this occasionally useful, but I'll emphasize, it's >only RARELY useful. > The .append() method is far more versatile. > >As to your original problem: my question to you is what is your purpose? > >1) To solve this particular problem, using Python. > or >2) To explore the usage of lists, applying them to this problem. > >If your purpose is the first, then I agree with the advice you have already >been given here. >Dictionaries are a much better fit to this problem. > >If your purpose is the second, then go ahead and use this for your >exploration. But realize >that to more experienced Pythonistas this would be a very un-pythonic >approach. Even better >would be to try multiple approaches -- lists, dictionaries, lists with >dictionaries, >dictionaries with lists or tuples... And any other combinations you can come >up with. This >will give you even more experience, and allow you to evaluate the different >approaches. > >And no, I will not give you a ready-made "canned" answer. For one thing, your >description is >too vague to effectively do that. Good luck. > > -=- Larry -=- The concept I was asking about was a master list with my example of 1,2,3 as a index for the second and third items. It was suggested to make my task easier. It turns out that it didn't. Thanks for all the suggestions, though. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 00:40:06 -0700, Larry Hudson wrote: >On 10/20/2014 12:49 PM, Seymore4Head wrote: >> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB >> wrote: >> > > >> Do you have to know the number of items the list will have before >> making it? >> > >No. Lists are NOT the same as arrays in other languages. But it IS possible >to create an >initial list of a specific size: > >myList = [None] * 50 > >This creates a 50-element list with each element set to None. (BTW, that >makes the indexes >0-49, not 0-50.) I have occasionally found this useful, but I emphasize it is >only RARELY >useful. The .append() method is far more versatile. > >As to your original problem: my question to you is what is your purpose? > >1) To solve this particular problem, using Python. > or >2) To explore the usage of lists by applying them to this problem. > >If your purpose is the first, then I agree with the advice you have already >been given here -- >dictionaries are a much better fit to this problem. > >If your purpose is the second, then go ahead, have at it. But realize that to >more experienced >Pythonistas this approach the very un-pythonic for this problem. It would be >even better to try >multiple approaches -- lists, dictionaries, lists with dictionaries, >dictionaries with lists or >tuples... or whatever combination you can come up with. This will give you >even more >experience and allow you to evaluate these various techniques. > >And no, I won't give you a ready-made "canned" answer. For one thing your >original description >is much too vague. But good luck! > > -=- Larry -=- Also, I had no need to impose a limit on the list, but I asked that question, just in case. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 21:31:05 -0400, Dennis Lee Bieber wrote: >On Mon, 20 Oct 2014 17:25:31 -0400, Seymore4Head > declaimed the following: > > >> >>The thing is I am not really sure what I want. I do know I need more >>practice to find out. Since I am taking a course now, I can't really >>ask a direct question and my first example wasn't so good. >> > Unfortunately, that puts your questions at the level of algorithm, not >language... Once you know the algorithm and data structures, THEN you can >figure out how to map them into Python (or Rexx, or any other language). Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Py2App - Could not import Tkinter error from resulting app
On Monday, October 20, 2014 11:07:51 PM UTC-5, Terry Reedy wrote: > tkinter imports _tkinter > > _tkinter connects with tclx.dll and tkx.dll (x is variable) > > So one possibility is no accessible tcl/tx. I have no idea how py2app > > is supposed to handle this on your undisclosed system. > -- > > Terry Jan Reedy Thanks for the reply. I am using a Mac. The problem turns out that for some reason when I installed the py2app utility it got installed under python 2.7 instead of python 3, which I am using, and that caused it to create the bundle under 2.x instead of 3.x. The result was it was trying to use python 2.x instead of 3.x syntax. I realized this problem shortly after I posted the question and tried to go back to google groups and delete my post before anyone had seen it. NB -- https://mail.python.org/mailman/listinfo/python-list
Re: Py2App - Could not import Tkinter error from resulting app
On Tue, Oct 21, 2014 at 8:20 AM, Noble Bell wrote: > I realized this problem shortly after I posted the question and tried to > go back to google groups and delete my post before anyone had seen it. In general, that won't work, as lots of people use email ( python-list@python.org) or Usenet (comp.lang.python) to read the group. In fact, Google Groups is very much a latecomer to the party. More importantly, just because you made a mistake doesn't mean you should delete your message. Other people might make the same mistake and learn from your post what the solution is. Skip -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there an easy way to control indents in Python
On Wednesday, October 15, 2014 12:13:51 PM UTC+10, ryguy7272 wrote: > I'm just learning Python. One of the best ways to learn any language is to type in the example code by hand. As you type in the code you will make mistakes, you will learn from your mistakes and that will help you learn the language and in the process make you a better programmer. So if you really want to learn the language just type in the code by hand and the problem goes away. -- https://mail.python.org/mailman/listinfo/python-list
string processing - some problems whenever I have to parse a more complex string
Hello together, currently I have to parse a string in an atomic way. Normally - in this case too - I have a counter variable to keep the current position inside the string. So far, I think this is the most flexible solution to do some lookaround's inside the string if necessary. Subroutines will be feed by the underlying data and the current position. A subroutine returns a tuple of the new position and the result. But I would like process subroutines with the same flexibillity (slicing/lookaround) but without returning the new position every again. Is there any implementation like C++ StringPiece class? Or something like the following behavior: >>>s = StringSlice('abcdef') >>>s StringSlice('abcdef') at xxx >>>s[0] 'a' >>>s.chop(1) # chop the first item >>>s[0] # 'b' is the new first item 'b' >>>s[:2] 'bc' >>>s.chop(-1) # chop the last item >>>s[-1] 'e' >>>s[1:] 'cde' >>>while s[0] != 'e': s.chop(1) >>>s[0] 'e' >>>s.startswith('e') True >>>s.isdigit() False Subroutines could chop the number of processed items internally if no error occours. Another possibillty will be to chop the current item manually. But I don't know how efficient this is in case of large strings. >>>while string: c = string[0] # process it ... string = string[1:] But this assumes that I have to return rest of the string too and not useful for my problem covered abrove. Has anyone some tips what's the best practice to process a complex string, handling prositions in/and subroutines. Thanks for all answers... Cheers Chris -- https://mail.python.org/mailman/listinfo/python-list
Pex import problems
Hi all, I'm trying to use Pex (http://pex.readthedocs.org/en/latest/index.html) to include requests in a little script to ping a server from a machine that doesn't come with pip (or much of anything, really) installed. I'm running into problems outputting a pex file that depends on a local script. I've got a command-line file, `client.py`, that can be called with a few command-line options. I can run `pex -r requests -o client.pex -- client.py`, but when I try `./client.pex -h` I get an error about "no such file or directory: '-h'". Ok, next attempt: `pex -r requests -o client.pex -e client` --> "ImportError: No module named client". I dug around in the code a bit, and from what I can tell, `-e FOO` boils down to `__import__(FOO)`, which I can do, both from the interpreter and from a test script. So what am I missing? The only other option I can think of would be: `pex -r requests -o client.pex` & then write a script that calls `client.py` from the pex environment (i.e., `./client.pex client.py -h` or whatever), and bundle the pex file, `client.py`, and the script together. But that seems like a misuse of the tool, at best. Alternatively/additionally: is there any mailing list/help source for pex? It seems like a great project, but I've not been able to find many resources out there, which is why I'm turning to you guys. (Why not just install pip/requests on the target machine? Because this is part of an effort to automate provisioning of a bunch of machines.) (Why not use pants? Because literally all we need is requests, and that seems like overkill.) (Also: anyone who's planning on chewing me out about formatting: I TRIED posting by email to comp.lang.pyt...@googlegroups.com, but it wouldn't let me. Sorry for the extra whitespace.) -- https://mail.python.org/mailman/listinfo/python-list
Re: Pex import problems
On Wed, Oct 22, 2014 at 2:34 AM, Sam Raker wrote: > (Also: anyone who's planning on chewing me out about formatting: I TRIED > posting by email to comp.lang.pyt...@googlegroups.com, but it wouldn't let > me. Sorry for the extra whitespace.) That's because that isn't the mailing list's name. Sign up here: https://mail.python.org/mailman/listinfo/python-list ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Pex import problems
On Tuesday, October 21, 2014 12:05:00 PM UTC-4, Chris Angelico wrote: > On Wed, Oct 22, 2014 at 2:34 AM, Sam Raker wrote: > > > (Also: anyone who's planning on chewing me out about formatting: I TRIED > > posting by email to comp.lang.pyt...@googlegroups.com, but it wouldn't let > > me. Sorry for the extra whitespace.) > That's because that isn't the mailing list's name. Sign up here: > https://mail.python.org/mailman/listinfo/python-list > ChrisA Thank you for your help. -- https://mail.python.org/mailman/listinfo/python-list
Re: Pex import problems
On Wed, Oct 22, 2014 at 3:36 AM, Sam Raker wrote: >> That's because that isn't the mailing list's name. Sign up here: > >> https://mail.python.org/mailman/listinfo/python-list > >> ChrisA > > Thank you for your help. No probs. Sorry I can't help with your main issue, as I'm not at all familiar with pex. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
PyPy3 2.4.0 released
= PyPy3 2.4 - Snow White = We're pleased to announce PyPy3 2.4, which contains significant performance enhancements and bug fixes. You can download the PyPy3 2.4.0 release here: http://pypy.org/download.html PyPy3 Highlights Issues reported with our previous release were fixed after reports from users on our new issue tracker at https://bitbucket.org/pypy/pypy/issues or on IRC at #pypy. Here is a summary of the user-facing PyPy3 specific changes: * Better Windows compatibility, e.g. the nt module functions _getfinalpathname & _getfileinformation are now supported (the former is required for the popular pathlib library for example) * Various fsencode PEP 383 related fixes to the posix module (readlink, uname, ttyname and ctermid) and improved locale handling * Switched default binary name os POSIX distributions to 'pypy3' (which symlinks to to 'pypy3.2') * Fixed a couple different crashes related to parsing Python 3 source code Further Highlights (shared w/ PyPy2) Benchmarks improved after internal enhancements in string and bytearray handling, and a major rewrite of the GIL handling. This means that external calls are now a lot faster, especially the CFFI ones. It also means better performance in a lot of corner cases with handling strings or bytearrays. The main bugfix is handling of many socket objects in your program which in the long run used to "leak" memory. We fixed a memory leak in IO in the sandbox_ code We welcomed more than 12 new contributors, and conducted two Google Summer of Code projects, as well as other student projects not directly related to Summer of Code. * Reduced internal copying of bytearray operations * Tweak the internal structure of StringBuilder to speed up large string handling, which becomes advantageous on large programs at the cost of slightly slower small *benchmark* type programs. * Boost performance of thread-local variables in both unjitted and jitted code, this mostly affects errno handling on linux, which makes external calls faster. * Move to a mixed polling and mutex GIL model that make mutlithreaded jitted code run *much* faster * Optimize errno handling in linux (x86 and x86-64 only) * Remove ctypes pythonapi and ctypes.PyDLL, which never worked on PyPy * Classes in the ast module are now distinct from structures used by the compiler, which simplifies and speeds up translation of our source code to the PyPy binary interpreter * Win32 now links statically to zlib, expat, bzip, and openssl-1.0.1i. No more missing DLLs * Many issues were resolved_ since the 2.3.1 release in June .. _`whats-new`: http://doc.pypy.org/en/latest/whatsnew-2.4.0.html .. _resolved: https://bitbucket.org/pypy/pypy/issues?status=resolved .. _sandbox: http://doc.pypy.org/en/latest/sandbox.html We have further improvements on the way: rpython file handling, numpy linalg compatibility, as well as improved GC and many smaller improvements. Please try it out and let us know what you think. We especially welcome success stories, we know you are using PyPy, please tell us about it! Cheers The PyPy Team -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there an easy way to control indents in Python
On Tue, Oct 21, 2014 at 2:45 AM, Simon Kennedy wrote: > On Monday, 20 October 2014 18:56:05 UTC+1, Ian wrote: >> Rather, I'm saying that where the blank line is should be the start of >> a new function. There would still be a blank line, just no longer >> inside the function. >> >> Now, maybe you think there should be more blank lines in the above, in >> which case we'll just have to disagree on that point. > > Why did you separate the above 2 sequences of thoughts by a blank line? Is > the inherent pause in the communication of your thoughts not also applicable > to your code? > > Where we see the pause between thoughts appears to be in a different place. I > see them both within the function and between the functions and I assume you > see them between the functions only. That makes sense. I see two distinct thoughts in the function that I posted: "build the graph" and "search the graph". The blank line separates them. By my view, a function should represent a single, complete thought. Thus, the function should be broken up at the blank line. -- https://mail.python.org/mailman/listinfo/python-list
Re: PyPy3 2.4.0 released
On Wed, Oct 22, 2014 at 4:03 AM, Philip Jenvey wrote: > PyPy3 2.4 - Snow White Interesting choice of name. I flipped through some of the release pages for previous versions and couldn't see a pattern to the names; is there a list somewhere of the code names and why they were selected? I'm sure there's a story behind this! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Problem with Android Build
Hello, If I have a problem with building Python on an Android device, would this be the list to post it to, or should I post it to python-help or python-dev? Thanks! Cyd -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with Android Build
On Wed, Oct 22, 2014 at 2:32 AM, Cyd Haselton wrote: > Hello, > If I have a problem with building Python on an Android device, would > this be the list to post it to, or should I post it to python-help or > python-dev? Hi! Start here. If we can't help, python-dev might be the next place to ask, but this is the best first option. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Pex import problems
Hi all, I'm trying to use Pex (http://pex.readthedocs.org/en/latest/index.html) to include requests in a little script to ping a server from a machine that doesn't come with pip (or much of anything, really) installed. I'm running into problems outputting a pex file that depends on a local script. I've got a command-line file, `client.py`, that can be called with a few command-line options. I can run `pex -r requests -o client.pex -- client.py`, but when I try `./client.pex -h` I get an error about "no such file or directory: '-h'". Ok, next attempt: `pex -r requests -o client.pex -e client` --> "ImportError: No module named client". I dug around in the code a bit, and from what I can tell, `-e FOO` boils down to `__import__(FOO)`, which I can do, both from the interpreter and from a test script. So what am I missing? The only other option I can think of would be: `pex -r requests -o client.pex` & then write a script that calls `client.py` from the pex environment (i.e., `./client.pex client.py -h` or whatever), and bundle the pex file, `client.py`, and the script together. But that seems like a misuse of the tool, at best. Alternatively/additionally: is there any mailing list/help source for pex? It seems like a great project, but I've not been able to find many resources out there, which is why I'm turning to you guys. (Why not just install pip/requests on the target machine? Because this is part of an effort to automate provisioning of a bunch of machines.) (Why not use pants? Because literally all we need is requests, and that seems like overkill.) -- https://mail.python.org/mailman/listinfo/python-list
Re: string processing - some problems whenever I have to parse a more complex string
On 10/21/14 10:32 AM, CWr wrote: Is there any implementation like C++ StringPiece class? Or something like the following behavior: >>>s = StringSlice('abcdef') >>>s StringSlice('abcdef') at xxx >>>s[0] 'a' >>>s.chop(1) # chop the first item >>>s[0] # 'b' is the new first item 'b' >>>s[:2] 'bc' >>>s.chop(-1) # chop the last item >>>s[-1] 'e' >>>s[1:] 'cde' >>>while s[0] != 'e': s.chop(1) >>>s[0] 'e' >>>s.startswith('e') True >>>s.isdigit() False You could certainly implement a StringSlice object that worked like this. Store the string, the start and end indexes. Override __getitem__ to adjust indexes and slice endpoints, implement chop() to change the start or end indexes. Implementing the string operations would get messy, likely you would materialize the sliced string to make them work. -- Ned Batchelder, http://nedbatchelder.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with Android Build
On Tue, Oct 21, 2014 at 12:36 PM, Chris Angelico wrote: > On Wed, Oct 22, 2014 at 2:32 AM, Cyd Haselton wrote: >> Hello, >> If I have a problem with building Python on an Android device, would >> this be the list to post it to, or should I post it to python-help or >> python-dev? > > Hi! > > Start here. If we can't help, python-dev might be the next place to > ask, but this is the best first option. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list Thanks! I'm building Python v2.7.8 on my Android device in a fakechroot environment with GCC 4.8.0 and make fails with the following error Could not import runpy module Running the Makefile command (minus the LD_LIBRARY_PATH prepend) produces the same error, but running the same command minus the -S returns a File "..Lib/os.py", line 117, in raise ImportError, 'no os specific module found' I've configured with ./configure --prefix=/usr/python --build=arm-linux-androideabi --host=arm-linux-androideabi --target=arm-linux-androideabi --enable-shared and ./configure --prefix=/usr/python with the same results. The two post-configure makefile edits I've added are: LDFLAGS=-Wl,--allow-shlib-undefined which allows make to continue past an 'undefined reference to sincos' and RUNSHARED= LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/Python/src which prevents the fakechroot environment from breaking I've also commented out the initpwd references in config.c...as Android doesn't do passwords per se. Any help would be appreciated! -- https://mail.python.org/mailman/listinfo/python-list
No Error; No Output...Nothing
Hey everyone, I'm trying to run this code. import os import pickle #import urllib2 from urllib.request import urlopen #import cookielib import http.cookiejar import re import time import numpy as np #import pylab as pl # symbol - USDEUR=X - problem that the server sometimes returns 0.0 def getSpotPrice(symbol): numberOfAttempts = 0 while numberOfAttempts < 10: url = 'http://download.finance.yahoo.com/d/quotes.csv?s='+symbol+'&f=l1&e=.cs' fxrate_pure = urllib2.urlopen(url).read() fxrate = fxrate_pure.strip() if fxrate != "0.00": return fxrate else: numberOfAttempts += 1 time.sleep(1) raise Exception("Unable to obtain market data from Yahoo! ... wrong ticker???") # symbol = the yahoo ticker; the expected tickers of the components contain alphanumerical characters or dot or hyphen; if the yahoo format changes, nothing is returned def getConstituentsOfAnIndexFromYahoo(symbol): url = 'http://finance.yahoo.com/q/cp?s=%s' % symbol p = re.compile('') components = [] pageIndex = 0 finished = False while not finished: if pageIndex == 0: actualUrl = url else: actualUrl = url + "&c=" + str(pageIndex) pageResults = p.findall(urllib2.urlopen(actualUrl).read()) if len(pageResults) == 0: finished = True else: components.extend(pageResults) pageIndex+=1 return components # prices = data[:,6] or prices = data[:, title.index("Adj Close")], pl.num2date(data[:,1]) back dates # syntax http://ichart.yahoo.com/table.csv?s={Yahoo.Symbol.[isin]}&a={Von.M-1}&b={Von.T}&c={Von.J}&d={Bis.M}&e={Bis.T}&f={Bis. J}&g=d&y=0&z=jdsu&ignore=.csv def getNumpyHistoricalTimeseries(symbol,fromDate, toDate): f = urllib2.urlopen('http://ichart.yahoo.com/table.csv?a='+ str(fromDate.month -1) +'&c='+ str(fromDate.year) +'&b=' + str(fromDate.day) + '&e='+ str(toDate.day) + '&d='+ str(toDate.month-1) +'&g=d&f=' + str(toDate.year) + '&s=' + symbol + '&ignore=.csv') header = f.readline().strip().split(",") #return np.loadtxt(f, dtype=np.float, delimiter=",", converters={0: pl.datestr2num}) I commented out the import pylab as pl because I couldn't get the matplotlib.pylab import working. So, anyway, I hit F5, and it seems to run, but it doesn't really do anything. Isn't this either supposed to be downloading data from the web, or throwing an error so I can troubleshoot, and try to figure out what's going on? It's hard to troubleshoot, when you don't get any error. Does this work for others? Thanks. -- https://mail.python.org/mailman/listinfo/python-list
Re: No Error; No Output...Nothing
On Wed, Oct 22, 2014 at 8:44 AM, ryguy7272 wrote: > So, anyway, I hit F5, and it seems to run, but it doesn't really do anything. It defines a few functions, but nothing ever calls them, so you won't see much out of it :) Try adding some code at the bottom that actually calls one of your functions, providing constants for whatever parameters it needs. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: No Error; No Output...Nothing
On Tue, Oct 21, 2014 at 2:44 PM, ryguy7272 wrote: > I commented out the import pylab as pl because I couldn't get the > matplotlib.pylab import working. So, anyway, I hit F5, and it seems to > run, but it doesn't really do anything. Isn't this either supposed to be > downloading data from the web, or throwing an error so I can troubleshoot, > and try to figure out what's going on? It's hard to troubleshoot, when you > don't get any error. Does this work for others? No where in the code are any of the functions being called, so unless your "F5" is bound to something that will call some functions, I would not expect it to do anything. Chris -- https://mail.python.org/mailman/listinfo/python-list
Re: string processing - some problems whenever I have to parse a more complex string
On 10/21/2014 10:32 AM, CWr wrote: Hello together, currently I have to parse a string in an atomic way. Normally - in this case too - I have a counter variable to keep the current position inside the string. So far, I think this is the most flexible solution to do some lookaround's inside the string if necessary. Subroutines will be feed by the underlying data and the current position. A subroutine returns a tuple of the new position and the result. But I would like process subroutines with the same flexibillity (slicing/lookaround) but without returning the new position every again. Is there any implementation like C++ StringPiece class? I am going to guess that this is a string view class that encapsulates a piece of an underlying class. Otherwise there is no point. A view class depends on a primary, independently accessible class for its data. There are two main categories. A subview gives the primary class interface to a part of the primary data. Numpy had array subviews an I presume you are talking about string subviews here. An altview class gives an alternative interface to the primary data. Dict views are examples. If the primary object is mutable, one reason to use a view instead of a copy is to keep the data for two objects synchronized. This does not apply to strings. Another reason is to save memory space. The downside is that the primary data cannot be erased until *both* objects are deleted. Moreover, if the primary data is small or the subview data is a small fraction of the primary data, the memory saving is small. So small subviews that persist after the primary object may end up costing more memory than they save. This is one reason Python does not have string subview. The numpy array view use case is large subarrays of large arrays that have to persist through a calculation anyway. Another reason Python lack sequence subviews is that the extra data needed for a contiguous slice are only the start and stop indexes. These can easily be manipulated directly without wrapping them in a class. And anyone who does want a method interface can easily create a class to their liking. To answer your question, I tried https://pypi.python.org/pypi?%3Aaction=search&term=string+view&submit=search and did not find anything. 'view' matches the generic use of 'view', as well as 'views', 'viewed', 'viewer', 'review', and 'preview'. The third answer here https://stackoverflow.com/questions/10085568/slices-to-immutable-strings-by-reference-and-not-copy has a StringView class that could be modifed to work on 3.x by removing the unneeded use of buffer. > Or something like the following behavior: s = StringSlice('abcdef') s = 'abcdef' a, b = 0, len(s) # s start, s end s StringSlice('abcdef') at xxx s[0] s[a] 'a' s.chop(1) # chop the first item s[0] # 'b' is the new first item a += 1 s[a] 'b' s[:2] s[a:a+2] 'bc' s.chop(-1) # chop the last item s[-1] b -= 1 s[b-1] 'e' s[1:] s[a+1:b] 'cde' while s[0] != 'e': s.chop(1) s[0] while s[a] != 'e': a += 1 s[a] 'e' s.startswith('e') s[a:b].startswith('e') True s.isdigit() s[a:b].isdigit() False Subroutines could chop the number of processed items internally if no error occours. Another possibillty will be to chop the current item manually. But I don't know how efficient this is in case of large strings. while string: c = string[0] # process it ... string = string[1:] This is extremely bad as it replaces the O(n) processing (below) with O(n*n) processing. In general, the right way to linearly process any iterable is for item in iterable: process(c) or sometimes for index, item in enumerate(iterable): process(index, item) or even, for sequences, (but not when the first option above suffices) for index in range(len(sequence)): process(index, sequence) -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: No Error; No Output...Nothing
On 10/21/2014 05:44 PM, ryguy7272 wrote: > Hey everyone, I'm trying to run this code. > > ... > > I commented out the import pylab as pl because I couldn't get the > matplotlib.pylab import working. So, anyway, I hit F5, and it seems to run, > but it doesn't really do anything. Isn't this either supposed to be > downloading data from the web, or throwing an error so I can troubleshoot, > and try to figure out what's going on? It's hard to troubleshoot, when you > don't get any error. Does this work for others? > > Thanks. No, it isn't supposed to be downloading data from the web. You have defined a few functions but you're not actually calling any of them. The script terminates successfully with no output, since there's nothing for it to do after executing your imports and function definitions. Additionally, your attempted use of 'urllib2' inside getConstituentsOfAnIndexFromYahoo will fail. Your import of that module is commented out, since (from your imports) you're presumably using Python 3 and a module with that name no longer exists. Since you have 'from urllib.request import urlopen', you can use the 'urlopen' module without any fully-qualified name. The relevant line in your getConstituentsOfAnIndexFromYahoo function should be 'pageResults = p.findall(urlopen(...'. MMR... -- https://mail.python.org/mailman/listinfo/python-list
Re: No Error; No Output...Nothing
On Tuesday, October 21, 2014 5:44:33 PM UTC-4, ryguy7272 wrote: > Hey everyone, I'm trying to run this code. > > > > import os > > import pickle > > #import urllib2 > > from urllib.request import urlopen > > #import cookielib > > import http.cookiejar > > import re > > import time > > import numpy as np > > #import pylab as pl > > > > # symbol - USDEUR=X - problem that the server sometimes returns 0.0 > > def getSpotPrice(symbol): > > numberOfAttempts = 0 > > while numberOfAttempts < 10: > > url = > 'http://download.finance.yahoo.com/d/quotes.csv?s='+symbol+'&f=l1&e=.cs' > > fxrate_pure = urllib2.urlopen(url).read() > > fxrate = fxrate_pure.strip() > > if fxrate != "0.00": > > return fxrate > > else: > > numberOfAttempts += 1 > > time.sleep(1) > > raise Exception("Unable to obtain market data from Yahoo! ... wrong > ticker???") > > > > # symbol = the yahoo ticker; the expected tickers of the components contain > alphanumerical characters or dot or hyphen; if the yahoo format changes, > nothing is returned > > def getConstituentsOfAnIndexFromYahoo(symbol): > > url = 'http://finance.yahoo.com/q/cp?s=%s' % symbol > > p = re.compile(' href=\"/q\?s=([A-Z0-9\.\-]*)\">') > > components = [] > > pageIndex = 0 > > finished = False > > while not finished: > > if pageIndex == 0: > > actualUrl = url > > else: > > actualUrl = url + "&c=" + str(pageIndex) > > pageResults = p.findall(urllib2.urlopen(actualUrl).read()) > > if len(pageResults) == 0: > > finished = True > > else: > > components.extend(pageResults) > > pageIndex+=1 > > return components > > > > # prices = data[:,6] or prices = data[:, title.index("Adj Close")], > pl.num2date(data[:,1]) back dates > > # syntax > http://ichart.yahoo.com/table.csv?s={Yahoo.Symbol.[isin]}&a={Von.M-1}&b={Von.T}&c={Von.J}&d={Bis.M}&e={Bis.T}&f={Bis. > J}&g=d&y=0&z=jdsu&ignore=.csv > > def getNumpyHistoricalTimeseries(symbol,fromDate, toDate): > > f = urllib2.urlopen('http://ichart.yahoo.com/table.csv?a='+ > str(fromDate.month -1) +'&c='+ str(fromDate.year) +'&b=' + str(fromDate.day) > + '&e='+ str(toDate.day) + '&d='+ str(toDate.month-1) +'&g=d&f=' + > str(toDate.year) + '&s=' + symbol + '&ignore=.csv') > > header = f.readline().strip().split(",") > > #return np.loadtxt(f, dtype=np.float, delimiter=",", converters={0: > pl.datestr2num}) > > > > I commented out the import pylab as pl because I couldn't get the > matplotlib.pylab import working. So, anyway, I hit F5, and it seems to run, > but it doesn't really do anything. Isn't this either supposed to be > downloading data from the web, or throwing an error so I can troubleshoot, > and try to figure out what's going on? It's hard to troubleshoot, when you > don't get any error. Does this work for others? > > > > Thanks. OK. Thanks everyone! -- https://mail.python.org/mailman/listinfo/python-list
Re: No Error; No Output...Nothing
On 21/10/2014 23:15, ryguy7272 wrote: OK. Thanks everyone! I'm pleased to see that you have answers. In return would you please access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- 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: Flush stdin
On Mon, Oct 20, 2014 at 9:41 PM, Marko Rauhamaa wrote: > Dan Stromberg : > >> Often with TCP protocols, line buffered is preferred to character >> buffered, > > Terminal devices support line buffering on write. Yes, though that's not the only place it's useful. > Line buffering on read is an illusion created by higher-level libraries. > The low-level read function reads in blocks of bytes. Actually, doesn't line buffering sometimes exist inside an OS kernel? stty/termios/termio/sgtty relate here, for *ix examples. Supporting code: http://stromberg.dnsalias.org/~strombrg/ttype/ It turns on character-at-a-time I/O in the tty driver via a variety of methods for portability. I wrote it in C before I took an interest in Python. Also, here's some supporting documentation: http://man7.org/linux/man-pages/man3/stdout.3.html - excerpt: Indeed, normally terminal input is line buffered in the kernel. But even if line buffering (or even character buffering) were never in the kernel, calling it an illusion is perhaps going a little far. It's useful sometimes, irrespective of where it comes from. "Illusion" has a bit of an undeserved pejorative connotation. >> Also, it's a straightforward way of framing your data, to avoid >> getting messed up by Nagle or fragmentation. > > Nagle affects the communication between the peer OS kernels and isn't > directly related to anything the application does. Actually, Nagle can cause two or more small packets to be merged, which is something an application must be able to deal with, because they could show up in the receiving application as one or more (but anyway: fewer) merged recv()'s. That's one reason why something like http://stromberg.dnsalias.org/~strombrg/bufsock.html can be helpful. > Also, Nagle doesn't > play any role with pipes. Yes, but pipes aren't the only thing involved in the OP's question. You "simplified" the problem down to pipes, but that doesn't really capture the complete essence of the matter. Nagle is one of the reasons. >>> >>> $ bash ./test.sh | strace python3 ./test.py >>> ... >>> read(0, "x", 4096) = 1 >>> read(0, "x", 4096) = 1 >>> read(0, "x", 4096) = 1 >>> read(0, "x", 4096) = 1 >>> read(0, "x", 4096) = 1 >>> fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 3), ...}) = 0 >>> mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, >>> 0) = 0x7f3143bab000 >>> write(1, "120\n", 4120 >>> )= 4 >>> ... >> >> >> This is tremendously inefficient. It demands a context switch for >> every character. > > Inefficiency isn't an issue when you generate one byte a second. Of course, but who's doing one byte per second? You and I in our tests, and perhaps some application developers with remarkably undemanding I/O. That doesn't really mean we should _recommend_ a series of os.read(0, 1)'s. > If data > were generated at a brisker pace, "read(0, ..., 4096)" could get more > bytes at a time. Notice that even if the Python code requests 5 bytes, > CPython requests up to 4096 bytes in a single read. Not if you use os.read(0, 1), for example, which was what you appeared to be recommending. os.read(0, 1) (when on a pipe) makes a call into kernel space via a context switch, once for each os.read(0, 1). I guess I should add that when you do an os.read(0, 1), and see it show up in strace, strace is showing kernel<->userspace interactions, not library stuff, and not stuff in an application that sits above libraries. ltrace shows some of the library stuff, but probably not all of it - I haven't studied ltrace as much as I have strace. Just wondering: Are we helping the OP? -- https://mail.python.org/mailman/listinfo/python-list
Re: Struggling with python-daemon and subprocess module to work together
On Mon, Oct 20, 2014 at 2:16 AM, Praveen Kumar wrote: > I am writing a very basic server side application[0] which get data > from a client and create a virtual machine using provided data and > also tells client about what's going on during *virt-install* command > execution. Previously this basic server is executed using *openvt* but > I thought it would be nice to have a daemon process for it and used > python-daemon. > > Issue I am facing is after some time server will stop sending data to > client which suppose to happen in #120 and get error message "Cannot > run interactive console without a controlling TTY" . I am not able to > figure out issue is occurred due to module or I missed something > during daemon initialization. I believe you either want a pty, to make your interactive application believe it's on a tty, or a redesign. Here's some Python pty code: http://stromberg.dnsalias.org/~strombrg/pypty/ I imagine pexpect might help too, though I tend to favor going directly to a pty. HTH -- https://mail.python.org/mailman/listinfo/python-list
Re: Struggling with python-daemon and subprocess module to work together
Praveen Kumar writes: > Previously this basic server is executed using *openvt* but I thought > it would be nice to have a daemon process for it and used > python-daemon. An important difference is that a daemon process has no controlling terminal, by definition. > Issue I am facing is after some time server will stop sending data to > client which suppose to happen in #120 and get error message "Cannot > run interactive console without a controlling TTY" . So, if the code you're running inside that daemon process needs access to a terminal, you have at least two options: * If the code really needs to talk on an interactive terminal, fake it with a pseudoterminal, and keep that pseudoterminal (its file handle) open. * If, as is probably the case, the code doesn't actually need to talk on an interactive terminal, cut that part of the code out entirely and the rest will run fine inside the daemon. -- \ “I may disagree with what you say, but I will defend to the | `\death your right to mis-attribute this quote to Voltaire.” | _o__) —Avram Grumer, rec.arts.sf.written, 2000-05-30 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: Flush stdin
On 21Oct2014 16:16, Dan Stromberg wrote: [...snip...] This is tremendously inefficient. It demands a context switch for every character. Inefficiency isn't an issue when you generate one byte a second. Of course, but who's doing one byte per second? You and I in our tests, and perhaps some application developers with remarkably undemanding I/O. That doesn't really mean we should _recommend_ a series of os.read(0, 1)'s. Indeed not. But there is one glaring exception: the shell's read builtin. Because it can be interspersed in a script between other input-consuming commands, it _must_ read no more than one line, and therefore is has to read in increments of 1 character. Of course, that says nothing about the upstream write() granularity. I now return y'all to your regularly sheduled nit picking. Cheers, Cameron Simpson If it ain't broken, keep playing with it. -- https://mail.python.org/mailman/listinfo/python-list
Re: When to use assert
Anton wrote: > I use ORM and often need to write a function that either takes an id of > the record or already loaded model object. So I end up writing a piece of > code like below: > > def do_something(instance): > if isinstance(instance_or_id, int): > instance = Model.get(instance) > assert isinstance(instance, Model) > # Code that assumes that instance is an object of type Model > > do_somthing is not a part of public library, though it is a public > function, which can and intended to be used by other programmers; and > assert should never happen if a client uses the function as planned. I think you mean the assert should never fail. That seems like a reasonable use for assert, with a proviso below. It's behaving like a checked comment or a post-condition test: asserting that Model.get returns a Model instance. But, the idea of *requiring* Model.get to return a Model instance may be inadvisable. It goes against duck-typing, and it prevents Model from making some kinds of implementation changes that might break your post-condition that get() always returns an instance. For example, it might return a proxy object instead, and then your assert will fail. > I > wonder if this use-case is controversial to this part: > >> Many people use asserts as a quick and easy way to raise an exception if >> an argument is given the wrong value. But this is wrong, dangerously >> wrong, for two reasons. The first is that AssertionError is usually the >> wrong error to give when testing function arguments. You wouldn't write >> code like this: >> >> if not isinstance(x, int): >> raise AssertionError("not an int") >> >> you'd raise TypeError instead. "assert" raises the wrong sort of >> exception. No, because the nature of the exception depends on the intent of the test and the audience who is expected to see it. In an ideal world, AssertionError should never be seen by the end user, or the developer calling your code (assuming that she obeys the documented requirements of your code). A failed assert should be considered an internal error, which the user never sees. Since the failure: "Model.get has stopped returning Model instances" is likely to be an internal problem ("oops, I broke the get() method, better fix that") rather than an expected error, using assert is okay. What would *not* be okay is something like this: def do_something(instance_or_id): if isinstance(instance_or_id, int): instance = Model.get(instance_or_id) assert isinstance(instance, Model) # Code that assumes that instance is an object of type Model since that fails with (for example) do_something(None): either the assert is not checked at all, and there will be some mysterious failure deep inside your code, or the caller will see AssertionError instead of TypeError, violating user expectations and good design that type errors should raise TypeError. A better way to write this might be to have Model.get() responsible for the error checking, and then just delegate to it: class Model: def get(self, obj): if isinstance(obj, Model): return obj elif isinstance(obj, int): model = Model("do something here") return model raise TypeError('expected an int ID or a Model instance') def do_something(instance_or_id): instance = Model.get(instance_or_id) assert isinstance(instance, Model) # Code that assumes that instance is an object of type Model That means that the logic for what is acceptable as a Model is all in one place, namely the Model.get method, and callers don't need to care about the pre-condition "argument is a Model or an integer ID", they only need to care about the post-condition "result of get() is a Model". -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: When to use assert
On Wed, Oct 22, 2014 at 12:44 PM, Steven D'Aprano wrote: > def do_something(instance_or_id): > instance = Model.get(instance_or_id) > assert isinstance(instance, Model) > # Code that assumes that instance is an object of type Model > > > That means that the logic for what is acceptable as a Model is all in one > place, namely the Model.get method, and callers don't need to care about > the pre-condition "argument is a Model or an integer ID", they only need to > care about the post-condition "result of get() is a Model". And at that point, the assertion is redundant, on par with: a = len(seq) assert isinstance(a, int) because you shouldn't have to assert what's part of a function's guarantee. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Flush stdin
On Sat, 18 Oct 2014 18:42:00 -0700, Dan Stromberg wrote: > On Sat, Oct 18, 2014 at 6:34 PM, Dan Stromberg wrote: >>> Once the "nc" process actually write()s the data to its standard >>> output (i.e. desriptor 1, not the "stdout" FILE*) >> I'm not sure why you're excluding stdout, but even if nc is using >> filedes 1 instead of FILE * stdout, isn't it kind of irrelevant? > > On further reflection, isn't it stdio that does the varied buffering, > and filedes 1 that's always unbuffered? IOW, the OP might wish nc was > using 1, but it probably can't be given what they're seeing. Yes. stdio does buffering. Writing to stdout stores data in a buffer; that data should eventually be written to descriptor 1, although perhaps not until immediately prior to termination. Which is probably the cause of the OP's problem. If it is, using a pseudo-tty would probably fix it. At startup, stdin and stdout are line-buffered if they are associated with a tty and fully-buffered otherwise (file, pipe, ...); stderr is unbuffered. At least, this is the case on Unix and Windows. The exact requirements of the C standard are: As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flush stdin
On Tue, Oct 21, 2014 at 7:49 PM, Nobody wrote: > On Sat, 18 Oct 2014 18:42:00 -0700, Dan Stromberg wrote: > >> On Sat, Oct 18, 2014 at 6:34 PM, Dan Stromberg wrote: Once the "nc" process actually write()s the data to its standard output (i.e. desriptor 1, not the "stdout" FILE*) >>> I'm not sure why you're excluding stdout, but even if nc is using >>> filedes 1 instead of FILE * stdout, isn't it kind of irrelevant? >> >> On further reflection, isn't it stdio that does the varied buffering, >> and filedes 1 that's always unbuffered? IOW, the OP might wish nc was >> using 1, but it probably can't be given what they're seeing. > > Yes. stdio does buffering. Writing to stdout stores data in a buffer; that > data should eventually be written to descriptor 1, although perhaps not > until immediately prior to termination. > > Which is probably the cause of the OP's problem. Huh. And here I thought I demonstrated elsewhere in this thread, that the buffering between nc and python didn't appear to be the problem. 'found it, here it is again: If I run the following in one tty: nc -l localhost 9000 | /tmp/z ...where /tmp/z has just: #!/usr/bin/python3 import sys for line in sys.stdin.buffer: print(line) And then run the following in another tty on the same computer: while read line; do echo $line; sleep 1; done < /etc/passwd | nc localhost 9000 ...then everything acts line buffered, or perhaps even character buffered (the two are pretty indistinguishable in this test). What I see is my /etc/passwd file popping out of the nc -l side, one line at a time, each line one second apart. I suppose this suggests that it's the client that's sending TCP data that is buffering. That, or we're using two different versions of netcat (there are at least two available). -- https://mail.python.org/mailman/listinfo/python-list
Matplotlib: getting a figure to show without plt.show()
I'm using Matplotlib to present a "control" window with clickable buttons, and to plot things in another window when you click buttons, in the style of the code below. I'm ashamed of the stinky way I use "first" to call plt.show the first time data are plotted but then to call fig.canvas.draw for subsequent data plots. Can someone tell me the right way? If I call plt.show every time, then after about 40 plots I get "RuntimeError: maximum recursion depth exceeded while calling a Python object", which makes sense because I'm getting one layer deeper in callbacks with every plot (plt.show doesn't return). But if I call fig.canvas.draw every time, the window for the data plot never appears on my screen. Thanks for any suggestions. ## import matplotlib.pyplot as plt from matplotlib.widgets import Button def callback(event): global n, first fig = plt.figure(2) fig.clear() plt.plot([0,1],[0,n]) n += 1 # (Pretending something changes from one plot to the next.) if first: first = False plt.show() else: fig.canvas.draw() global n, first n = 0 first = True fig = plt.figure(1) quit_button = Button(plt.axes([.1,.3,.4,.2]), "Quit") quit_button.on_clicked(lambda x: plt.close("all")) plot_button = Button(plt.axes([.1,.1,.4,.2]), "Plot") plot_button.on_clicked(callback) plt.show() ## -- To email me, substitute nowhere->runbox, invalid->com. -- https://mail.python.org/mailman/listinfo/python-list
Re: Flush stdin
Dan Stromberg : > On Mon, Oct 20, 2014 at 9:41 PM, Marko Rauhamaa wrote: >> Nagle affects the communication between the peer OS kernels and isn't >> directly related to anything the application does. > > Actually, Nagle can cause two or more small packets to be merged, > which is something an application must be able to deal with, because > they could show up in the receiving application as one or more (but > anyway: fewer) merged recv()'s. Packets have barely anything to do with TCP sockets since they provide an octet stream abstraction. > Of course, but who's doing one byte per second? You and I in our > tests, and perhaps some application developers with remarkably > undemanding I/O. That doesn't really mean we should _recommend_ a > series of os.read(0, 1)'s. No, here's my statement: if you need to process input as soon as it becomes available, you can't use sys.stdin. Instead, you need to use os.read(). You typically supply os.read() with a buffer of a kilobyte or more. Key is, os.read() returns right away if fewer bytes are available. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: Flush stdin
On Wed, Oct 22, 2014 at 4:38 PM, Marko Rauhamaa wrote: > Dan Stromberg : > >> On Mon, Oct 20, 2014 at 9:41 PM, Marko Rauhamaa wrote: >>> Nagle affects the communication between the peer OS kernels and isn't >>> directly related to anything the application does. >> >> Actually, Nagle can cause two or more small packets to be merged, >> which is something an application must be able to deal with, because >> they could show up in the receiving application as one or more (but >> anyway: fewer) merged recv()'s. > > Packets have barely anything to do with TCP sockets since they provide > an octet stream abstraction. TCP does abstract over the individual packets, but they are still important. >> Of course, but who's doing one byte per second? You and I in our >> tests, and perhaps some application developers with remarkably >> undemanding I/O. That doesn't really mean we should _recommend_ a >> series of os.read(0, 1)'s. > > No, here's my statement: if you need to process input as soon as it > becomes available, you can't use sys.stdin. Instead, you need to use > os.read(). > > You typically supply os.read() with a buffer of a kilobyte or more. Key > is, os.read() returns right away if fewer bytes are available. Then your statement is false. Maybe it's not *efficient* if you always use sys.stdin.read(1), but you certainly can do it. It's not that you *need to* use something else. ChrisA -- https://mail.python.org/mailman/listinfo/python-list