Re: PEP 450 Adding a statistics module to Python
On Friday, August 9, 2013 9:10:18 PM UTC-4, Steven D'Aprano wrote: > I am seeking comments on PEP 450, Adding a statistics module to Python's > standard library: I think it's a very good idea. Good PEP points, too. I hope it happens. -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On 8/14/2013 1:05 PM, random...@fastmail.us wrote: On Wed, Aug 14, 2013, at 10:32, wxjmfa...@gmail.com wrote: I'm always and still be suprised by the number of hard coded '\n' one can find in Python code when the portable (here win) os.linesep '\r\n' exists. Because high-level code isn't supposed to use the os module directly. This is a bit extreme, but definitely true for os.linesep and *much* of os other than os.path and maybe os.environ. Text-mode streams automatically convert newlines you write to them. By default, to \n when reading files;, \n to os.linesep when writing. Windows is the only major OS for which os.linesep is not \n. The full details, from the builtin 'open' entry: " newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows: When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string. " -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Verifying Variable value
On 8/14/2013 12:36 PM, Prasad, Ramit wrote: chandan kumar wrote: Is there a way to validate variable values while debugging any python code. In addition to pdb, I would imagine most Python IDEs would support debugging in this manner. Idle also has a debugger. It uses the same bdb base debug module as pdb. One turns it on (or off) in the Shell window. Then run code from an editor window. For your example (which lacked the ':') --- def method(): a = 20 b =30 c = a + b return method() --- I added the return so the debugger would pause there after calculating c. The method has to be called in order to step into the body during execution. One can also set breakpoints in the editor, though I have never tried that. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: many constructors in a class?
On Wed, Aug 14, 2013 at 10:16 AM, climb65 wrote: > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in a > class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. > > No, Python does not allow method overloading: >>> class Test: ... def __init__(self): ...print "first init" ... def __init__(self, arg): ...print "init with arg" ... >>> a = Test() Traceback (most recent call last): File "", line 1, in TypeError: __init__() takes exactly 2 arguments (1 given) No error on actually writing the class, but only the last __init__ is kept. You could, however, emulate that behavior with optional arguments, or something more sophisticated as the need may be. This stackoverflow question covers a few alternatives: http://stackoverflow.com/questions/6434482/python-function-overloading -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Wed, Aug 14, 2013 at 6:29 PM, Tim Chase wrote: > On 2013-08-14 18:14, Chris Angelico wrote: >> On Wed, Aug 14, 2013 at 6:05 PM, wrote: >> > On Wed, Aug 14, 2013, at 10:32, wxjmfa...@gmail.com wrote: >> >> I'm always and still be suprised by the number of hard coded >> >> '\n' one can find in Python code when the portable (here >> >> win) >> >> >> >> >>> os.linesep >> >> '\r\n' >> >> >> >> exists. >> > >> > Because high-level code isn't supposed to use the os module >> > directly. Text-mode streams automatically convert newlines you >> > write to them. >> >> I'm always, and will still be, surprised by the number of hard coded >> decimal integers one can find in Python code, when the portable way >> to do it is to use ctypes and figure out whether your literals >> should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet >> people continue to just put decimal literals in their code! It >> can't be portable. > > No, no, no...you want > > from sys.platform.integers import 0, 1, 2, 3, 14, 42 > > to be portable against endian'ness and bit-width. Oh! I didn't know about sys.platform.integers. All this time I've been doing it manually, usually copying and pasting a block of integer definitions from the re module. (I used to copy them from adamant.princess.ida but some of them were buggy. 2+2 made 5, or 3, or 7, or 25, depending on need.) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Wed, Aug 14, 2013 at 12:05 PM, wrote: > Because high-level code isn't supposed to use the os module directly. That seems a bit extreme. One would hope that Guido and the rest of the crew created the os module so people would use it instead of resorting to other lower level hacks. A quick find/grep of my own code suggests that I import os more than sys. I use it mostly for os.path.* and os.environ. I'm not sure there's a higher level way to access them without putting more layers between your code and those objects, which code would obviously have to call them anyway. Did I just misread your comment? Skip -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On 2013-08-14 18:14, Chris Angelico wrote: > On Wed, Aug 14, 2013 at 6:05 PM, wrote: > > On Wed, Aug 14, 2013, at 10:32, wxjmfa...@gmail.com wrote: > >> I'm always and still be suprised by the number of hard coded > >> '\n' one can find in Python code when the portable (here > >> win) > >> > >> >>> os.linesep > >> '\r\n' > >> > >> exists. > > > > Because high-level code isn't supposed to use the os module > > directly. Text-mode streams automatically convert newlines you > > write to them. > > I'm always, and will still be, surprised by the number of hard coded > decimal integers one can find in Python code, when the portable way > to do it is to use ctypes and figure out whether your literals > should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet > people continue to just put decimal literals in their code! It > can't be portable. No, no, no...you want from sys.platform.integers import 0, 1, 2, 3, 14, 42 to be portable against endian'ness and bit-width. Granted, one might confuse them with regular numeric literals, so it would be best to clarify them by namespace: import sys answer_to_life = sys.platform.integers.42 print(sum(range(sys.platform.integers.0, sys.platform.integers.14))) That way you ensure platform independence, and *much* clearer! ;-) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Wed, Aug 14, 2013 at 6:05 PM, wrote: > On Wed, Aug 14, 2013, at 10:32, wxjmfa...@gmail.com wrote: >> I'm always and still be suprised by the number of hard coded >> '\n' one can find in Python code when the portable (here >> win) >> >> >>> os.linesep >> '\r\n' >> >> exists. > > Because high-level code isn't supposed to use the os module directly. > Text-mode streams automatically convert newlines you write to them. I'm always, and will still be, surprised by the number of hard coded decimal integers one can find in Python code, when the portable way to do it is to use ctypes and figure out whether your literals should be big-endian or little-endian, 32-bit or 64-bit, etc. Yet people continue to just put decimal literals in their code! It can't be portable. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Wed, Aug 14, 2013, at 10:32, wxjmfa...@gmail.com wrote: > I'm always and still be suprised by the number of hard coded > '\n' one can find in Python code when the portable (here > win) > > >>> os.linesep > '\r\n' > > exists. Because high-level code isn't supposed to use the os module directly. Text-mode streams automatically convert newlines you write to them. -- http://mail.python.org/mailman/listinfo/python-list
RE: Verifying Variable value
chandan kumar wrote: > Hi , > > Is there a way to validate variable values while debugging any python > code.Run below example in > debugging mode and i would like to know the value of c (I know print is an > option) with any other > option other than printing. > In C# or some other tools we can verify each statement and values. Is there > way to check each > statement in python code like in c#. > > Ex: > def method() > a = 20 > b =30 > c = a + b > > > Best Regards, > Chanadn In addition to pdb, I would imagine most Python IDEs would support debugging in this manner. pydev: http://pydev.org/manual_adv_debugger.html Wing: http://wingware.com/wingide/debugger Pycharm: http://www.jetbrains.com/pycharm/webhelp/running-and-debugging.html This actually has a nice table of some IDEs for Python. See which ones have a 'Y' under the PD column. http://stackoverflow.com/questions/81584/what-ide-to-use-for-python ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting a value that follows string.find()
In <40816fed-38d4-4baa-92cc-c80cd8feb...@googlegroups.com> englishkevin...@gmail.com writes: > I know the title doesn't make much sense, but I didnt know how to explain my > problem. > Anywho, I've opened a page's source in URLLIB > starturlsource = starturlopen.read() > string.find(starturlsource, 'http://mail.python.org/mailman/listinfo/python-list
ANN: Pyrolite 1.12 - native pickle and Pyro client library for java and .net
Hi, I'd like to announce Pyrolite 1.12, a tiny (~60k) pickle and Pyro client library for Java and .NET. Question: "what is a java/.net library doing in this newsgroup?" Answer 1: This library is meant to connect a Java or .NET program to Python in a very simple way, using the Pyro protocol. Pyro is my remote object library or Python. Answer 2: Pyrolite contains a native implementation of Python's pickle protocol. This can be useful by itself to read/write pickles from Java or .NET programs. Recently, I've implemented memo-support in the pickler, which made it feature complete. You can now pickle recursive object graphs, something which produced a stack overflow error earlier. Notice that the unpickler has been able to deal with these for a long time already. Pyrolite project page: https://github.com/irmen/Pyrolite Compiled binaries are here: http://irmen.home.xs4all.nl/pyrolite/ Enjoy, Irmen de Jong -- http://mail.python.org/mailman/listinfo/python-list
Re: many constructors in a class?
On 14/08/13 15:16, climb65 wrote: Hello, here is a small basic question : Is it possible to have more than one constructor (__init__ function) in a class? For instance, to create an object with 2 different ways? If my memory is good, I think that with C++ it is possible. Thanks for your answer. http://stackoverflow.com/questions/5738470/whats-an-example-use-case-for-a-python-classmethod Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: many constructors in a class?
2013/8/14 climb65 > Hello, > > here is a small basic question : > > Is it possible to have more than one constructor (__init__ function) in a > class? For instance, to create an object with 2 different ways? If my > memory is good, I think that with C++ it is possible. > > Thanks for your answer. > -- > http://mail.python.org/mailman/listinfo/python-list > Hello, You have to use default values in __init__ function, like : def __init__( self, name = None ): self.name_ = name and afterwards in your code, test variable : if self.name_: do something... Regards, Phil. -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
Le mercredi 14 août 2013 13:55:23 UTC+2, Joshua Landau a écrit : > On 14 August 2013 12:45, Peter Otten <__pete...@web.de> wrote: > > > Joshua Landau wrote: > > >> On 14 August 2013 09:30, Alister wrote: > > >>> I would agree with the last statement. > > >>> Please write list definitions as lists rather than taking a short-cut to > > >>> save a few key presses > > >> > > >> That's true with this example, but is: > > >> > > >> lines = [ > > >> "Developments in high-speed rail, and high-speed", > > > ... > > >> "same problems the latter was designed to solve." > > >> ] > > >> > > >> really more readable than: > > >> > > >> lines = """\ > > >> Developments in high-speed rail, and high-speed > > > ... > > >> same problems the latter was designed to solve. > > >> """[1:-1].split("\n") > > >> > > >> ? > > > > > > It's definitely more correct -- unless you meant to strip the "D" from the > > > first line ;) > > > > > > I would use > > > > > > lines = """\ > > > Developments in high-speed rail, and high-speed > > > ... > > > same problems the latter was designed to solve. > > > """.splitlines() > > > > Thanks, I didn't actually know about .splitlines()! a = ['==\r**', '==\n**', '==\r\n**', '==\u0085**', '==\u000b**', '==\u000c**', '==\u2028**', '==\u2029**'] for e in a: print(e.splitlines()) ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] ['==', '**'] Do not confuse these NLF's (new line functions) in the Unicode terminology, with the end of line *symbols* (pilcrow, \u2424, ...) I'm always and still be suprised by the number of hard coded '\n' one can find in Python code when the portable (here win) >>> os.linesep '\r\n' exists. jmf -- http://mail.python.org/mailman/listinfo/python-list
Re: Digging into multiprocessing
Awesome, thanks for the detailed response Chris. On Tue, Aug 13, 2013 at 8:03 AM, Chris Angelico wrote: > On Tue, Aug 13, 2013 at 12:17 AM, Demian Brecht > wrote: >> Hi all, >> >> Some work that I'm doing atm is in some serious need of >> parallelization. As such, I've been digging into the multiprocessing >> module more than I've had to before and I had a few questions come up >> as a result: >> >> (Running 2.7.5+ on OSX) >> >> 1. From what I've read, a new Python interpreter instance is kicked >> off for every worker. My immediate assumption was that the file that >> the code was in would be reloaded for every instance. After some >> digging, this is obviously not the case (print __name__ at the top of >> the file only yield a single output line). So, I'm assuming that >> there's some optimization that passes of the bytecode within the >> interpreter? How, exactly does this work? (I couldn't really find much >> in the docs about it, or am I just not looking in the right place?) > > I don't know about OSX specifically, but I believe it forks, same as > on Linux. That means all your initialization code is done once. Be > aware that this is NOT the case on Windows. > > http://en.wikipedia.org/wiki/Fork_(operating_system) > > Effectively, code execution proceeds down a single thread until the > point of forking, and then the fork call returns twice. Can be messy > to explain but it makes great sense once you grok it! > >> 2. For cases using methods such as map_async/wait, once the bytecode >> has been passed into the child process, `target` is called `n` times >> until the current queue is empty. Is this correct? > > That would be about right, yes. The intention is that it's equivalent > to map(), only it splits the work across multiple processes; so the > expectation is that it will call target for each yielded item in the > iterable. > >> 3. Because __main__ is only run when the root process imports, if >> using global, READ-ONLY objects, such as, say, a database connection, >> then it might be better from a performance standpoint to initialize >> that at main, relying on the interpreter references to be passed >> around correctly. I've read some blogs and such that suggest that you >> should create a new database connection within your child process >> targets (or code called into by the targets). This seems to be less >> than optimal to me if my assumption is correct. > > This depends hugely on the objects you're working with. If your > database connection uses a TCP socket, for instance, all forked > processes will share the same socket, which will most likely result in > interleaved writes and messed-up reads. But with a log file, that > might be okay (especially if you have some kind of atomicity guarantee > that ensures that individual log entries don't interleave). The > problem isn't really the Python objects (which will have been happily > cloned by the fork() procedure), but the OS-level resources used. > > With a good database like PostgreSQL, and reasonable numbers of > workers (say, 10-50, rather than 1000-5000), you should be able to > simply establish separate connections for each subprocess without > worrying about performance. If you really need billions of worker > processes, it might be best to use one of the multiprocessing module's > queueing/semaphoring facilities and either have one process that does > all databasing, or let them all use it but serially. But if you can > manage with separate connections, that would be the easiest, safest, > and simplest to debug. > >> 4. Related to 3, read-only objects that are initialized prior to being >> passed into a sub-process are safe to reuse as long as they are >> treated as being immutable. Any other objects should use one of the >> shared memory features. >> >> Is this more or less correct, or am I just off my rocker? > > When you fork, each process will get its own clone of the objects in > the parent. For read-only objects (module-level constants and such), > this is fine, as you say. The issue is if you want another process to > "see" the change you made. That's when you need some form of shared > data. > > So, yes, more or less correct; at least, what you've said is mostly > right for Unix - there may be some additional caveats for OSX > specifically that I'm not aware of. But I expect they'll be minor; > it's mainly Windows, which doesn't *have* fork(2), where there are > major differences. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list -- Demian Brecht http://demianbrecht.github.com -- http://mail.python.org/mailman/listinfo/python-list
many constructors in a class?
Hello, here is a small basic question : Is it possible to have more than one constructor (__init__ function) in a class? For instance, to create an object with 2 different ways? If my memory is good, I think that with C++ it is possible. Thanks for your answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading log and saving data to DB
On Wed, Aug 14, 2013, at 09:18 AM, Guy Tamir wrote: > Hi all, > > I have a Ubuntu server running NGINX that logs data for me. > I want to write a python script that reads my customized logs and after > a little rearrangement save the new data into my DB (postgresql). > > The process should run about every 5 minutes and i'm expecting large > chunks of data on several 5 minute windows.. > > My plan for achieving this is to install python on the server, write a > script and add it to cron. > > My question is what the simplest way to do this? > should i use any python frameworks? Rarely do I put "framework" and "simplest way" in the same set. I would do 1 of 2 things: * Write a simple script that reads lines from stdin, and writes to the db. Make sure it gets run in init before nginx does and tail -F -n 0 to that script. Don't worry about the 5-minute cron. * Similar to above but if you want to use cron also store in the db the offset of the last byte read in the file, then when the cron job kicks off again seek to that position + 1 and begin reading, at EOF write the offset again. This is irrespective of any log rotating that is going on behind the scenes, of course. -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Wed, 14 Aug 2013 11:31:01 +0100, Joshua Landau wrote: > On 14 August 2013 09:30, Alister wrote: >> On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: >> >>> On 08/13/2013 09:51 PM, eschneide...@comcast.net wrote: How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes? Thank you. import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] print(random.choice(pie)) Eric >>> >>> I think you are referring to this: >>> pie = 'keylime peach apple cherry pecan'.split() >>> >>> While it's easier to type, and does save a few characters, I think the >>> original list is clearer to a reader of your program. >>> >>> Gary Herron >> >> I would agree with the last statement. >> Please write list definitions as lists rather than taking a short-cut >> to save a few key presses > > That's true with this example, but is: > > lines = [ > "Developments in high-speed rail, and high-speed", "transport more > generally, have historically been", "impeded by the difficulties in > managing friction", "and air resistance, both of which become", > "substantial when vehicles approach high speeds.", "The vactrain > concept eliminates these obstacles", "by employing magnetically > levitating trains in", "tubes kept at a complete vacuum, allowing > for", "heoretical speeds of thousands of miles per", "hour. The high > cost of constructing such a system,", > "however, and the difficulty of maintaining a", "vacuum over large > distances, has prevented this", "type of system from ever being > built. The", "Hyperloop can be viewed as a modified vactrain,", > "employing more cost-effective solutions to the", "same problems the > latter was designed to solve." > ] > > really more readable than: > > lines = """\ > Developments in high-speed rail, and high-speed transport more > generally, have historically been impeded by the difficulties in > managing friction and air resistance, both of which become substantial > when vehicles approach high speeds. > The vactrain concept eliminates these obstacles by employing > magnetically levitating trains in tubes kept at a complete vacuum, > allowing for heoretical speeds of thousands of miles per hour. The high > cost of constructing such a system, > however, and the difficulty of maintaining a vacuum over large > distances, has prevented this type of system from ever being built. The > Hyperloop can be viewed as a modified vactrain, > employing more cost-effective solutions to the same problems the latter > was designed to solve. > """[1:-1].split("\n") > > ? Yes, because I can see at the start that a list is being created & can skip over the data top the next line of code. I could easily miss the .split() at the end of the string deffinition. -- "It ain't over until it's over." -- Casey Stengel -- http://mail.python.org/mailman/listinfo/python-list
Reading log and saving data to DB
Hi all, I have a Ubuntu server running NGINX that logs data for me. I want to write a python script that reads my customized logs and after a little rearrangement save the new data into my DB (postgresql). The process should run about every 5 minutes and i'm expecting large chunks of data on several 5 minute windows.. My plan for achieving this is to install python on the server, write a script and add it to cron. My question is what the simplest way to do this? should i use any python frameworks? For my python app i'm using Django, but on this server i just need to read a file, do some manipulation and save to DB. if you think any of my plan seem troubling in any way i'd love to hear.. Regards, Guy -- http://mail.python.org/mailman/listinfo/python-list
Re: Verifying Variable value
You can even use logging module in python to validate the variable values. You can import the module and use any of the following levels in your program import logging logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG For more you can refer to, http://docs.python.org/2/library/logging.html http://stackoverflow.com/questions/1623039/python-debugging-tips On Wed, Aug 14, 2013 at 3:12 AM, chandan kumar wrote: > Hi , > > Is there a way to validate variable values while debugging any python > code.Run below example in debugging mode and i would like to know the > value of c (I know print is an option) with any other option other than > printing. > In C# or some other tools we can verify each statement and values. Is > there way to check each statement in python code like in c#. > > Ex: > def method() > a = 20 > b =30 > c = a + b > > > Best Regards, > Chanadn > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I not seeing the Error?
In article , Chris Angelico wrote: > On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: > > On 14 August 2013 02:20, Gregory Ewing wrote: > >> Ned Batchelder wrote: > >>> > >>> Everyone: this program seems to be a direct and misguided transliteration > >>> from a bash script. > >> > >> Not a particularly well-written bash script, either -- > >> it's full of superfluous uses of 'cat'. > > > > What's wrong with cat? Sure it's superfluous but what makes it *bad*? > > Personally I often prefer the pipe "cat x | y" form to "x < y"... or > > "< y x". > > What's the use of it, in that situation? Why not simply use > redirection? (Though you have the letters backward; "cat y | x" would > be the equivalent of your others. Typo, I assume.) You're forking a > process that achieves nothing, if your cat has just one argument. This is wa off-topic for a Python discussion, but... There's two reasons UUOC is a silly issue. First, it may save human effort. I like to build up long complicated commands and pipelines one bit at a time, and look at the intermediate results. Let's say I'm starting with a sed command (abstracted from my current shell history) $ sed -e 's/.*; iOS/iOS/' -e 's/;.*//' -e 's/\..*//' x When I want to add the next "-e whatever" to the command, I need to get it in front of the "x". If I had written it as: $ cat x | sed -e 's/.*; iOS/iOS/' -e 's/;.*//' -e 's/\..*//' I just have to stick it at the end, which is easier; I just type control-p and add what I want. Or, "!!" and keep typing. A small amount of human convenience (especially when it's mine) is worth a lot of wasted CPU time. Second, in some cases, the extra "cat" process may actually speed up overall command execution because it provides additional I/O buffering. The cat process will read ahead from the disk file and block only when its output pipe buffers are full. When the sed command is ready to process more input, it only has to read from the pipe, not wait for a (very slow, by comparison) disk read. Yeah, I know, modern kernels do lots of read-ahead buffing on their own. This gives you more. Sure, it costs something to fork/exec another process. So what? The computer exists to do my bidding, not the other way around. -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I not seeing the Error?
On 14 August 2013 13:07, Chris Angelico wrote: > On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: >> >> What's wrong with cat? Sure it's superfluous but what makes it *bad*? >> Personally I often prefer the pipe "cat x | y" form to "x < y"... or >> "< y x". > > What's the use of it, in that situation? Why not simply use > redirection? (Though you have the letters backward; "cat y | x" would > be the equivalent of your others. Typo, I assume.) You're forking a > process that achieves nothing, if your cat has just one argument. > > Of course, there ARE many good uses for cat. If you give it multiple > arguments, or if you have arguments that modify the output on the way > through (eg "cat -n"), then it's not the same as redirection. And some > programs behave differently if stdout is a tty, so the quickest way to > get the porcelain version of something is to append "|cat" to the > command. Or maybe you need to retrieve something that only root can > read, so you use "sudo cat /x/y/z|somescript". But if you could spell > it "x < y", then why not do so? Because "cat y | x" often reads nicer. It's the whole "input -> function -> function -> ... -> output" thing. I especially hate "y < input > output" which reads awfully not matter where you chuck the spaces. "cat input | y > output" however, is acceptable. Honestly I do think Python would do well to get a pipe operator, because in some circumstances it's just cleaner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I not seeing the Error?
On Wed, Aug 14, 2013 at 7:59 AM, Joshua Landau wrote: > On 14 August 2013 02:20, Gregory Ewing wrote: >> Ned Batchelder wrote: >>> >>> Everyone: this program seems to be a direct and misguided transliteration >>> from a bash script. >> >> Not a particularly well-written bash script, either -- >> it's full of superfluous uses of 'cat'. > > What's wrong with cat? Sure it's superfluous but what makes it *bad*? > Personally I often prefer the pipe "cat x | y" form to "x < y"... or > "< y x". What's the use of it, in that situation? Why not simply use redirection? (Though you have the letters backward; "cat y | x" would be the equivalent of your others. Typo, I assume.) You're forking a process that achieves nothing, if your cat has just one argument. Of course, there ARE many good uses for cat. If you give it multiple arguments, or if you have arguments that modify the output on the way through (eg "cat -n"), then it's not the same as redirection. And some programs behave differently if stdout is a tty, so the quickest way to get the porcelain version of something is to append "|cat" to the command. Or maybe you need to retrieve something that only root can read, so you use "sudo cat /x/y/z|somescript". But if you could spell it "x < y", then why not do so? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Verifying Variable value
chandan kumar wrote: > Hi , > > Is there a way to validate variable values while debugging any python > code.Run below example in debugging mode and i would like to know the value > of c (I know print is an option) with any other option other than printing. > In C# or some other tools we can verify each statement and values. Is there > way to check each statement in python code like in c#. > > Ex: > def method() > a = 20 > b =30 > c = a + b I haven't used the language called C# since before it was renamed. But if they were at all consistent to its forbears, you must be talking about assert(). assert() works pretty much the same in Python, though you don't need the parens. assert c==50 will throw an AssertionError if c is unequal to 50 > > Hi > ,Is there a way to validate variable values > while debugging any python code.Run below example in debugging mode and > i would like to know the value of c (I know print is an option) with any > other option other than printing.http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On 14 August 2013 12:45, Peter Otten <__pete...@web.de> wrote: > Joshua Landau wrote: >> On 14 August 2013 09:30, Alister wrote: >>> I would agree with the last statement. >>> Please write list definitions as lists rather than taking a short-cut to >>> save a few key presses >> >> That's true with this example, but is: >> >> lines = [ >> "Developments in high-speed rail, and high-speed", > ... >> "same problems the latter was designed to solve." >> ] >> >> really more readable than: >> >> lines = """\ >> Developments in high-speed rail, and high-speed > ... >> same problems the latter was designed to solve. >> """[1:-1].split("\n") >> >> ? > > It's definitely more correct -- unless you meant to strip the "D" from the > first line ;) > > I would use > > lines = """\ > Developments in high-speed rail, and high-speed > ... > same problems the latter was designed to solve. > """.splitlines() Thanks, I didn't actually know about .splitlines()! -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
Joshua Landau wrote: > On 14 August 2013 09:30, Alister wrote: >> On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: >> >>> On 08/13/2013 09:51 PM, eschneide...@comcast.net wrote: How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes? Thank you. import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] print(random.choice(pie)) Eric >>> >>> I think you are referring to this: >>> pie = 'keylime peach apple cherry pecan'.split() >>> >>> While it's easier to type, and does save a few characters, I think the >>> original list is clearer to a reader of your program. >>> >>> Gary Herron >> >> I would agree with the last statement. >> Please write list definitions as lists rather than taking a short-cut to >> save a few key presses > > That's true with this example, but is: > > lines = [ > "Developments in high-speed rail, and high-speed", ... > "same problems the latter was designed to solve." > ] > > really more readable than: > > lines = """\ > Developments in high-speed rail, and high-speed ... > same problems the latter was designed to solve. > """[1:-1].split("\n") > > ? It's definitely more correct -- unless you meant to strip the "D" from the first line ;) I would use lines = """\ Developments in high-speed rail, and high-speed ... same problems the latter was designed to solve. """.splitlines() -- http://mail.python.org/mailman/listinfo/python-list
Re: Verifying Variable value
Hi Chandan, Python has built-in module called pdb which can be used for debugging. Importing it in the right place will be like setting break point in code and will change the execution to debugging mode. We can use different debugging commands ((n)ext, (c)ontinue, (s)tep etc) to evaluate through the code. Below is the link to module, http://docs.python.org/2/library/pdb.html We can run this code in the following way "python -m pdb filename.py" to run it in pdb debugging mode. Else import pdb and set the trace at right place like below. For example = def method() import pdb;pdb.set_trace() <<<-- import and set_trace() a = 20 b =30 c = a + b method() While running you will get pdb prompt to debug the code line by line. The execution will be in user's hands. Like below, > c:\users\krishnan\desktop\test.py(3)method() -> a = 20 (Pdb) n > c:\users\krishnan\desktop\test.py(4)method() -> b =30 (Pdb) n > c:\users\krishnan\desktop\test.py(5)method() -> c = a + b (Pdb) n --Return-- > c:\users\krishnan\desktop\test.py(5)method()->None -> c = a + b (Pdb) c You can explore the module and find it useful for debugging. I hope this helps Regards, Krishnan On Wed, Aug 14, 2013 at 3:12 AM, chandan kumar wrote: Hi , Is there a way to validate variable values while debugging any python code.Run below example in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing. In C# or some other tools we can verify each statement and values. Is there way to check each statement in python code like in c#. Ex: def method() a = 20 b =30 c = a + b Best Regards, Chanadn -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On 14 August 2013 09:30, Alister wrote: > On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: > >> On 08/13/2013 09:51 PM, eschneide...@comcast.net wrote: >>> How can I use the '.split()' method (am I right in calling it a >>> method?) without instead of writing each comma between words in the pie >>> list in the following code? Also, is there a way to use .split instead >>> of typing the apostrophes? Thank you. >>> >>> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >>> print(random.choice(pie)) >>> >>> Eric >> >> I think you are referring to this: >> pie = 'keylime peach apple cherry pecan'.split() >> >> While it's easier to type, and does save a few characters, I think the >> original list is clearer to a reader of your program. >> >> Gary Herron > > I would agree with the last statement. > Please write list definitions as lists rather than taking a short-cut to > save a few key presses That's true with this example, but is: lines = [ "Developments in high-speed rail, and high-speed", "transport more generally, have historically been", "impeded by the difficulties in managing friction", "and air resistance, both of which become", "substantial when vehicles approach high speeds.", "The vactrain concept eliminates these obstacles", "by employing magnetically levitating trains in", "tubes kept at a complete vacuum, allowing for", "heoretical speeds of thousands of miles per", "hour. The high cost of constructing such a system,", "however, and the difficulty of maintaining a", "vacuum over large distances, has prevented this", "type of system from ever being built. The", "Hyperloop can be viewed as a modified vactrain,", "employing more cost-effective solutions to the", "same problems the latter was designed to solve." ] really more readable than: lines = """\ Developments in high-speed rail, and high-speed transport more generally, have historically been impeded by the difficulties in managing friction and air resistance, both of which become substantial when vehicles approach high speeds. The vactrain concept eliminates these obstacles by employing magnetically levitating trains in tubes kept at a complete vacuum, allowing for heoretical speeds of thousands of miles per hour. The high cost of constructing such a system, however, and the difficulty of maintaining a vacuum over large distances, has prevented this type of system from ever being built. The Hyperloop can be viewed as a modified vactrain, employing more cost-effective solutions to the same problems the latter was designed to solve. """[1:-1].split("\n") ? Additionally,namedtuple has already set the precedence for this kind of thing. Finally, a simple extension or a decent editor should make it trivial to convert between the forms, so you can write the shorter way and convert on-the-fly. -- http://mail.python.org/mailman/listinfo/python-list
Verifying Variable value
Hi , Is there a way to validate variable values while debugging any python code.Run below example in debugging mode and i would like to know the value of c (I know print is an option) with any other option other than printing. In C# or some other tools we can verify each statement and values. Is there way to check each statement in python code like in c#. Ex: def method() a = 20 b =30 c = a + b Best Regards, Chanadn-- http://mail.python.org/mailman/listinfo/python-list
Re: .split() Qeustion
On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote: > On 08/13/2013 09:51 PM, eschneide...@comcast.net wrote: >> How can I use the '.split()' method (am I right in calling it a >> method?) without instead of writing each comma between words in the pie >> list in the following code? Also, is there a way to use .split instead >> of typing the apostrophes? Thank you. >> >> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan'] >> print(random.choice(pie)) >> >> Eric > > I think you are referring to this: > pie = 'keylime peach apple cherry pecan'.split() > > While it's easier to type, and does save a few characters, I think the > original list is clearer to a reader of your program. > > Gary Herron I would agree with the last statement. Please write list definitions as lists rather than taking a short-cut to save a few key presses -- Accuracy, n.: The vice of being right -- http://mail.python.org/mailman/listinfo/python-list
Re: Pair of filenos read/write each other?
Nobody nowhere.com> writes: > > On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote: > > > Is there anything like os.pipe() where you can read/write both ends? > > There's socket.socketpair(), but it's only available on Unix. > > Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones > created by os.pipe()) aren't bidirectional. I'm not sure I understand the problem: you can just create two pair of pipes using os.pipe(). If that's too low-level, you can wrap the fds using BufferedRWPair: http://docs.python.org/3.3/library/io.html#io.BufferedRWPair (actual incantation would be: r1, w1 = os.pipe() r2, w2 = os.pipe() end1 = io.BufferedRWPair(io.FileIO(r1, 'r'), io.FileIO(w2, 'w')) end2 = io.BufferedRWPair(io.FileIO(r2, 'r'), io.FileIO(w1, 'w')) end1.write(b"foo") end1.flush() end2.read(3) # -> return b"foo" ) An alternative is to use multiprocessing.Pipe(): http://docs.python.org/3.3/library/multiprocessing.html#multiprocessing.Pipe In any case, Python doesn't lack facilities for doing what you want. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Pair of filenos read/write each other?
On Tue, 13 Aug 2013 16:10:41 -0700, Jack Bates wrote: > Is there anything like os.pipe() where you can read/write both ends? There's socket.socketpair(), but it's only available on Unix. Windows doesn't have AF_UNIX sockets, and anonymous pipes (like the ones created by os.pipe()) aren't bidirectional. Named pipes are bidirectional, but you would need to choose a name, create one, connect, and accept (like with sockets); there's no convenience function like socketpair(). Also, you need to consider the security implications, as other processes can (try to) connect to a named pipe. -- http://mail.python.org/mailman/listinfo/python-list
Re: python and displaying on 10 bit monitors
On Tue, 13 Aug 2013 05:25:34 -0700, rlkling wrote: > Or, asking another way, are there any python libraries that display images > to 10 bit monitors as 10 bit images, and not scaled to 8 bit? This should be possible using PyOpenGL and GLUT, with: glutInitDisplayString("red=10 green=10 blue=10") That's based upon a quick look at: http://www.amd.com/us/Documents/10-Bit.pdf http://www.nvidia.com/docs/IO/40049/TB-04701-001_v02_new.pdf I don't have a 30-bpp monitor to test it with. You may be able to do the same thing using e.g. QGLWidget or wxGLCanvas, but I don't think that you can get 30-bpp with GDI, so you're limited to solutions involving OpenGL (or DirectX, if you can figure out the necessary pieces). -- http://mail.python.org/mailman/listinfo/python-list
Re: Am I not seeing the Error?
On 14 August 2013 02:20, Gregory Ewing wrote: > Ned Batchelder wrote: >> >> Everyone: this program seems to be a direct and misguided transliteration >> from a bash script. > > Not a particularly well-written bash script, either -- > it's full of superfluous uses of 'cat'. What's wrong with cat? Sure it's superfluous but what makes it *bad*? Personally I often prefer the pipe "cat x | y" form to "x < y"... or "< y x". There seems to be a militant "cat is evil" attitude where I feel it's just normally just people who want to show others they know more bash than they do. -- http://mail.python.org/mailman/listinfo/python-list