Can't detect EOF from stdin on windows console
I want to read stdin in chunks of fixed size until EOF I want to be able (also) to supply data interactively in console window and then to hit Ctrl+Z when finished So what I do is: while True: s = sys.stdin.read(chunk_size) if not s: break # do something with s if stdin is standard console input (on windows xp), here what happens: (suppose, chunk_size = 3) input: 123^Zenter --- s gets 123 but reading doesn't end input: ^Zenter --- now s is empty and loop breaks, so you have to press Ctrl-Z Enter TWICE to end the loop worse still: input: 12^Zenter --- EOF is there, but there's only TWO chars instead of requested THREE, so stdin.read() doesn't even return yet input: ^Zenter --- s gets 12 but reading doesn't end input: ^Zenter --- only now loop breaks so you have to press Ctrl-Z Enter THRICE to end the loop I haven't discovered any EOF function in python which could tell me if eof was encountered. As you see, testing for empty string or for len(s) = chunk_size doesn't improve the situation, anyone can suggest a workaround? Also I think the latter case is a straightaway bug, doc says: read( [size]) Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). According to that, stdin.read(3), when supplied with 12^Z should return immediately with two-character string instead of waiting for third character after EOF. By the way, if I enter something between ^Z's that will be treated as vaild input and ^Z's will be completely ignored. -- http://mail.python.org/mailman/listinfo/python-list
cmp() on integers - is there guarantee of returning only +-1 or 0?
doc says that it must be 0, or 0, but it seems that it returns +1 or -1. Can it be reliably used to get the sign of x: cmp(x, 0) like pascal Sign() function does? I mean, I'm pretty sure that it can be used, but is it mentioned somewhere in language spec, or it may be implementation defined? If so, any other simple means of _reliably_ getting the sign? -- http://mail.python.org/mailman/listinfo/python-list
Why python doesn't use syntax like function(, , x) for default parameters?
I mean, it's very convenient when default parameters can be in any position, like def a_func(x = 2, y = 1, z): ... (that defaults must go last is really a C++ quirk which is needed for overload resolution, isn't it?) and when calling, just omit parameter when you want to use defaults: a_func(, , 3) There are often situations when a function has independent parameters, all having reasonable defaults, and I want to provide just several of them. In fact, I can do it using keyword parameters, but it's rather long and you have to remember/lookup names of parameters. Is there some contradiction in python syntax which disallows an easy implementation of this feature, or just nobody bothered with this? If former is the case, please show me why, because I badly need this feature in embedded python app (for compatibility with other language that uses such syntax) and might venture to implement it myself, so don't want to waste time if it's gonna break something. Or maybe it might be an idea for enhancement proposal? -- http://mail.python.org/mailman/listinfo/python-list
Re: Why python doesn't use syntax like function(, , x) for default parameters?
Some example (from real life). def ChooseItems(StartDate, EndDate, Filter): #function returns a set of some items in chronological order #from required interval possibly using filter ChooseItems() #get everything ChooseItems('01.01.2000', ,SomeFilter) #get everything after a date using filter ChooseItems(, '01.01.2000') #get everything before a date ChooseItems(, , SomeFilter) #get everything using filter Now compare this to something which (I hope) is rather pythonian Seq[:] #get everything Seq[2::3] #get everything after an index using filter (filter every third value) Seq[:3] #get everythin before an index Seq[::4] #get everything using a filter Do you see any significant difference? I understand that many do not need such a syntax, I don't understand why someone would be AGAINST it. I don't propose to CHANGE anything in python (right now this syntax is error anyway). What I propose is just ADD another way of calling a function with keyword parameters but using POSITIONS instead of NAMES. And sometimes position is easier to remember than name. Anyway, who wants names let them use names. Who wants positions let them use positions. But to have a CHOICE is always good. As far as the choice itself doesn't damage anything, and I don't think that my does. I think that if we compare ChooseItems('01.01.2000', ,SomeFilter) and ChooseItems(StartDate='01.01.2000', Filter=SomeFilter) the first one is more readable, 'cos you see what is meant right away. In second one you have to actually READ the keyword names to understand. It's not the common case, of course, but still, why not have a choice to use it? Some other examples which might benefit SetDate(year, month, day) SetDate(, month+1) # set next month, leaving year and day SetDate(, , 31) # set to end of month, not changing year #(wrong date adjusted automatically, of course) FormatFloat(Float, Length, Precision, FormatFlags) You might want precision, leaving length default, or just use FormatFlags In fact, I became so used to convenience of such syntax that it was a disappointment not to find it in python. Please, don't try to scare me with 25-parameter functions. This is not for them. But to remember positions of two to five parameters is actually easier (if their order has some logic) then what are their names: startDate ? beginDate? firstDate? openDate? Date1? The same approach can be used with tuples: (, , z) = func() # returning three element tuple() You think z = func()[2] is actually more clear? - By the way, I want THIRD value, not SECOND. And tuples don't have keyword names, do they? And what about (a, , b) = func() ...well, maybe I got carried away a little... Finally, if syntax func (None, None, 10) seems natural to you, I propose to make it even more natural: I don't want some None passed as argument, I don't want anything at all passed, so I just use empty space func ( , , 10) And the called func don't have to bother with checking None for EACH argument but will happily use defaults instead. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why python doesn't use syntax like function(, , x) for default parameters?
Some example (from real life). def ChooseItems(StartDate, EndDate, Filter): #function returns a set of some items in chronological order #from required interval possibly using filter ChooseItems() #get everything ChooseItems('01.01.2000', ,SomeFilter) #get everything after a date using filter ChooseItems(, '01.01.2000') #get everything before a date ChooseItems(, , SomeFilter) #get everything using filter Now compare this to something which (I hope) is rather pythonian Seq[:] #get everything Seq[2::3] #get everything after an index using filter (filter every third value) Seq[:3] #get everythin before an index Seq[::4] #get everything using a filter Do you see any significant difference? I understand that many do not need such a syntax, I don't understand why someone would be AGAINST it. I don't propose to CHANGE anything in python (right now this syntax is error anyway). What I propose is just ADD another way of calling a function with keyword parameters but using POSITIONS instead of NAMES. And sometimes position is easier to remember than name. Anyway, who wants names let them use names. Who wants positions let them use positions. But to have a CHOICE is always good. As far as the choice itself doesn't damage anything, and I don't think that my does. I think that if we compare ChooseItems('01.01.2000', ,SomeFilter) and ChooseItems(StartDate='01.01.2000', Filter=SomeFilter) the first one is more readable, 'cos you see what is meant right away. In second one you have to actually READ the keyword names to understand. It's not the common case, of course, but still, why not have a choice to use it? Some other examples which might benefit SetDate(year, month, day) SetDate(, month+1) # set next month, leaving year and day SetDate(, , 31) # set to end of month, not changing year #(wrong date adjusted automatically, of course) FormatFloat(Float, Length, Precision, FormatFlags) You might want precision, leaving length default, or just use FormatFlags In fact, I became so used to convenience of such syntax that it was a disappointment not to find it in python. Please, don't try to scare me with 25-parameter functions. This is not for them. But to remember positions of two to five parameters is actually easier (if their order has some logic) then what are their names: startDate ? beginDate? firstDate? openDate? Date1? The same approach can be used with tuples: (, , z) = func() # returning three element tuple() You think z = func()[2] is actually more clear? - By the way, I want THIRD value, not SECOND. And tuples don't have keyword names, do they? And what about (a, , b) = func() ...well, maybe I got carried away a little... Finally, if syntax func (None, None, 10) seems natural to you, I propose to make it even more natural: I don't want some None passed as argument, I don't want anything at all passed, so I just use empty space func ( , , 10) And the called func don't have to bother with checking None for EACH argument but will happily use defaults instead. -- http://mail.python.org/mailman/listinfo/python-list