I try to edit and find data from a text file with python 2.7 (data from stock market)
hi, I try to edit a text file with python 2.7: * AAPL * Date: September 07 2018 Time: 14:10:52 Price: ,068,407 Ask: None High: None Low: None Previous Close: ,068,407 Volume: $ 227.35 / $ 221.30 Market Cap: 20.23 but when I write it to a file I get: {'previous_close': ',068,407', 'volume': u'$\xa0227.35\xa0/\xa0$\xa0221.30', 'market_cap': '20.23', 'price': ',068,407', 'high': 'None', 'ask': 'None', 'low': 'None', 'time': '14:15:45', 'date': 'September 07 2018', 'ticker': 'AAPL'} why is that? and how do I get the price value only? so I will have only that in a variable? for example the variable: Price. this is my code.. import csv import itertools from nasdaq_stock import nasdaq_stock x=str(nasdaq_stock.stock('AAPL')) with open("log.txt", "w") as text_file: text_file.write(format(x)) with open('log.txt', 'r') as in_file: lines = in_file.read().splitlines() stripped = [line.replace(","," ").split() for line in lines] grouped = itertools.izip(*[stripped]*1) with open('log.csv', 'w') as out_file: writer = csv.writer(out_file) writer.writerow(('title', 'intro', 'tagline')) for group in grouped: writer.writerows(group) #locate cell import csv def read_cell(x, y): with open('log.csv', 'r') as f: reader = csv.reader(f) y_count = 0 for n in reader: if y_count == y: cell = n[x] return cell y_count += 1 #I try to find the value of Price.. print (read_cell(2,3)) -- https://mail.python.org/mailman/listinfo/python-list
I need help to put the output from terminal in a csv file
HI! I need help putting the output from terminal in a csv file. I work on linux and I have python 2.7.15. I am running a python program which also runs some shell scripts. I need to capture the output from the .sh scripts and put it nicely in a csv table. I am using "commands.getoutput('bash example.sh')" to capture the output of the scripts (I can use subprocess too if needed). The outputs looks something like this: In case of an error { "error" : { "body" : "The requested device is currently not online. "detail" : "Make sure to connect the device", "event" : 123456, "header" : "Device Unavailable (123456)" } } And in case of a good output it look similar but has "status :0" instead of event and ofc it does say that it passed the test. I need to export the results of the .sh tests in a csv file with my .py program. The boss expects a table with 3 columns. The first one with The title TEST which has the name of the tests (.sh scripts). The 2nd with PASS/FAIL (this is determined by the "status" : 0 output for some tests and with a number between 1 and 4000 for others (i used regex for this, but I struggle a bit here too) and the 3rd one which returns the Value ( 0 in case status is 0 or the number between 1 and 4000 for the other tests). I really need some ideas, I struggle for 3 days already using regex, commands, subprocess and watching tutorials. I just can't solve this yet, I really need some ideas. Much appreciated. -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
On Thu, 06 Sep 2018 13:48:54 +0300, Marko Rauhamaa wrote: > Chris Angelico : >> The request was to translate this into Python, not to slavishly imitate >> every possible semantic difference even if it won't actually affect >> behaviour. > > I trust Steven to be able to refactor the code into something more > likable. His only tripping point was the meaning of the "let" construct. Thanks for the vote of confidence :-) However I have a follow up question. Why the "let" construct in the first place? Is this just a matter of principle, "put everything in its own scope as a matter of precautionary code hygiene"? Because I can't see any advantage to the inner function: def isqrt(n): if n == 0: return 0 else: def f2398478957(r): if n < (2*r+1)**2: return 2*r else: return 2*r+1 return f2398478957(isqrt(n//4)) Sure, it ensures that r is in its own namespace. But why is that an advantage in a function so small? Perhaps its a SML functional- programming thing. Putting aside the observation that recursion may not be the best way to do this in Python, I don't think that the inner function is actually needed. We can just write: def isqrt(n): if n == 0: return 0 else: r = isqrt(n//4) if n < (2*r+1)**2: return 2*r else: return 2*r+1 By the way I got this from this paper: https://www.cs.uni-potsdam.de/ti/kreitz/PDF/03cucs-intsqrt.pdf -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
On Fri, 7 Sep 2018 at 14:06, Steven D'Aprano wrote: > > On Thu, 06 Sep 2018 13:48:54 +0300, Marko Rauhamaa wrote: > > > Chris Angelico : > >> The request was to translate this into Python, not to slavishly imitate > >> every possible semantic difference even if it won't actually affect > >> behaviour. > > > > I trust Steven to be able to refactor the code into something more > > likable. His only tripping point was the meaning of the "let" construct. > > Thanks for the vote of confidence :-) > > However I have a follow up question. Why the "let" construct in the first > place? Is this just a matter of principle, "put everything in its own > scope as a matter of precautionary code hygiene"? Because I can't see any > advantage to the inner function: My impression is that this is just functional programming "good style". As you say, it's not needed, it's just "keep things valid in the smallest range possible". Probably also related to the mathematical style of naming sub-expressions. Also, it's probably the case that in a (compiled) functional language like SML, the compiler can optimise this to avoid any actual inner function, leaving it as nothing more than a temporary name. Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: I need help to put the output from terminal in a csv file
Please study the following to get you started. It looks like JSON output that you are dealing, which is good. I added a ", to the "body"-line, because I assume that you botched that when giving an example. ```python #!/usr/bin/env python import json output = ''' { "error" : { "body" : "The requested device is currently not online.", "detail" : "Make sure to connect the device", "event" : 123456, "header" : "Device Unavailable (123456)" } } ''' # help(json.loads) parsed = json.loads(output) if type(parsed) == dict: if type(parsed['error']) == dict: print("yay!") ``` HTH -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
On 2018-09-07, Steven D'Aprano wrote: > On Thu, 06 Sep 2018 13:48:54 +0300, Marko Rauhamaa wrote: > >> Chris Angelico : >>> The request was to translate this into Python, not to slavishly imitate >>> every possible semantic difference even if it won't actually affect >>> behaviour. >> >> I trust Steven to be able to refactor the code into something more >> likable. His only tripping point was the meaning of the "let" construct. > > Thanks for the vote of confidence :-) > > However I have a follow up question. Why the "let" construct in > the first place? Is this just a matter of principle, "put > everything in its own scope as a matter of precautionary code > hygiene"? Because I can't see any advantage to the inner > function: let is a convenient way for functional languages to introduce new names for things. You'd consider it wherever you'd consider assigning something to a new name in Python. In this case, it was probably just to avoid writing out that square root calculation twice. Though you could use lambda calculus directly instead, that might be considered painful, and the first thing you'd then do is write a let macro. -- Neil Cerutti -- https://mail.python.org/mailman/listinfo/python-list
Re: I need help to put the output from terminal in a csv file
On 07/09/18 13:55, catanaang...@gmail.com wrote: HI! I need help putting the output from terminal in a csv file. I work on linux and I have python 2.7.15. I am running a python program which also runs some shell scripts. I need to capture the output from the .sh scripts and put it nicely in a csv table. I am using "commands.getoutput('bash example.sh')" to capture the output of the scripts (I can use subprocess too if needed). The outputs looks something like this: In case of an error { "error" : { "body" : "The requested device is currently not online. "detail" : "Make sure to connect the device", "event" : 123456, "header" : "Device Unavailable (123456)" } } And in case of a good output it look similar but has "status :0" instead of event and ofc it does say that it passed the test. I need to export the results of the .sh tests in a csv file with my .py program. The boss expects a table with 3 columns. The first one with The title TEST which has the name of the tests (.sh scripts). The 2nd with PASS/FAIL (this is determined by the "status" : 0 output for some tests and with a number between 1 and 4000 for others (i used regex for this, but I struggle a bit here too) and the 3rd one which returns the Value ( 0 in case status is 0 or the number between 1 and 4000 for the other tests). I really need some ideas, I struggle for 3 days already using regex, commands, subprocess and watching tutorials. I just can't solve this yet, I really need some ideas. From what you've said above, your basic problem is that you need to parse the output looking for lines containing '"status": ' where is some number. Let's assume that you have run one of the shell scripts and captured its output into a string. The first thing to do is to split the output into lines, which you can do with the "split()" string method. Then check each line in turn. If it contains a colon it may be what you want. "split()" the line at the colon and look at the first half. Once you have "strip()"ed off the spaces at the beginning and the end, is it "status"? If so, you've got your pass and the VALUE you want is in the second half. I hope that helps. -- Rhodri James *-* Kynesim Ltd -- https://mail.python.org/mailman/listinfo/python-list
Re: ModuleNotFoundError: No module named 'encodings'
Thanks Thomas, You are right, this seems the Python home configuration issue. One more question. Is there a way I can catch the error ( Fatal Python error: initfsencoding: ..) as exception in the c code ? try{ Py_Initialize(); }catch(xxx) { } Thanks On Thu, Sep 6, 2018 at 5:29 PM, Thomas Jollans wrote: > On 09/06/2018 09:46 PM, Jason Qian via Python-list wrote: > >> Hi >> >> Need some help. >> >> I have a C++ application that invokes Python. >> >> ... >> Py_SetPythonHome("python_path"); >> > > This isn't actually a line in your code, is it? For one thing, > Py_SetPythonHome expects a wchar_t*... > > Py_Initialize(); >> >> This works fine on Python 3.6.4 version, but got errors on Python 3.7.0 >> when calling Py_Initialize(), >> >> Fatal Python error: initfsencoding: unable to load the file system codec >> ModuleNotFoundError: No module named 'encodings' >> > > So, Python can't find a core module. This either means your Python 3.7 > build is broken, or it doesn't know where to look. Perhaps whatever it is > you're actually passing to Py_SetPythonHome needs to be changed to point to > the right place? (i.e. maybe you're giving it the location of the Python > 3.6 library rather than the Python 3.7 one) > > -- Thomas > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
RE: Why emumerated list is empty on 2nd round of print?
The question "If I do this "aList = enumerate(numList)", isn't it stored permanently in aList now? I see your point to use it directly, but just in case I do need to hang onto it from one loop to another, then how is that done?" Reflects that you are thinking in a C++ kind of way I think. When you declare a variable name and assign to it, in Python what you have done is assigned to the name a reference to the content. It is never true that the content is "in" the variable. The variable does not have storage for what you assign to it. That content is stored somewhere, but the variable just has a reference to it. So, aList = enumerate(numList) creates an iterator, and aList is a reference to that iterator. It is not "in" aLIst. Despite the name, aList does not contain a list. (See above). Now, on to the second part: the problem you showed - that you can only loop through aList:print(i,j) once - is BECAUSE you hung onto it from one loop to another. Once the iterator is exhausted, it's exhausted. Think of another more common iterator you've probably used: file = open("filename.txt") for line in file;print line On EOF the iterator throws an exception (that is expected), the for loop catches the exception and exits. Files can be "rewound" by file.seek(0). But other than that, once you have reached EOF, you have reached EOF. It doesn't automagically rewind. If you do another for line in file; print line it will not print anything. Exactly the same behavior as you are surprised about for the enumerate iterator. I don’t even know if there is a method to reset the enumerate iterator. If there isn't one, then you should not hold the iterator from one loop to the next, you have to make another one. --- Joe S. -Original Message- From: Viet Nguyen Sent: Thursday, September 6, 2018 2:50 PM To: python-list@python.org Subject: Re: Why emumerated list is empty on 2nd round of print? On Thursday, September 6, 2018 at 10:34:19 AM UTC-7, Chris Angelico wrote: > On Fri, Sep 7, 2018 at 3:26 AM, Viet Nguyen via Python-list > wrote: > numList > > [2, 7, 22, 30, 1, 8] > > > aList = enumerate(numList) > > > for i,j in aList:print(i,j) > > > > 0 2 > > 1 7 > > 2 22 > > 3 30 > > 4 1 > > 5 8 > > > for i,j in aList:print(i,j) > > > > > Because it's not an enumerated list, it's an enumerated iterator. > Generally, you'll just use that directly in the loop: > > for i, value in enumerate(numbers): > > There's generally no need to hang onto it from one loop to another. > > ChrisA Thanks ChrisA. If I do this "aList = enumerate(numList)", isn't it stored permanently in aList now? I see your point to use it directly, but just in case I do need to hang onto it from one loop to another, then how is that done? Anyway I think I'm ok and I got what I need for now. -- https://mail.python.org/mailman/listinfo/python-list
Re: I try to edit and find data from a text file with python 2.7 (data from stock market)
alon.naj...@gmail.com wrote: > hi, > > I try to edit a text file with python 2.7: > > * AAPL * > Date: September 07 2018 > Time: 14:10:52 > Price: ,068,407 > Ask: None > High: None > Low: None > Previous Close: ,068,407 > Volume: $ 227.35 / $ 221.30 > Market Cap: 20.23 It looks like the author of the nasdaq_stock module was interested in a quick and dirty tool for personal needs rather than a clean library for general use. The text quoted above is printed implicitly by the stock() function. To suppress it you have to modify that function or to redirect sys.stdout. Another problem with the code is that it may swallow arbitrary exceptions. Therefore my error message below has to be vague. > but when I write it to a file I get: > {'previous_close': ',068,407', 'volume': > {u'$\xa0227.35\xa0/\xa0$\xa0221.30', 'market_cap': '20.23', 'price': > {',068,407', 'high': 'None', 'ask': 'None', 'low': 'None', 'time': > {'14:15:45', 'date': 'September 07 2018', 'ticker': 'AAPL'} > > why is that? If all goes well the stock function returns the above dict. > and how do I get the price value only? so I will have only > that in a variable? for example the variable: Price. import sys from nasdaq_stock import nasdaq_stock stock_info = nasdaq_stock.stock('AAPL') print if stock_info: price = stock_info["price"] ticker = stock_info["ticker"] print "Ticker:", ticker, "Price:", price else: print >> sys.stderr, "Something went wrong" -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
On Fri, 7 Sep 2018 at 15:10, Paul Moore wrote: > > On Fri, 7 Sep 2018 at 14:06, Steven D'Aprano > wrote: > > > > On Thu, 06 Sep 2018 13:48:54 +0300, Marko Rauhamaa wrote: > > > > > Chris Angelico : > > >> The request was to translate this into Python, not to slavishly imitate > > >> every possible semantic difference even if it won't actually affect > > >> behaviour. > > > > > > I trust Steven to be able to refactor the code into something more > > > likable. His only tripping point was the meaning of the "let" construct. > > > > Thanks for the vote of confidence :-) > > > > However I have a follow up question. Why the "let" construct in the first > > place? Is this just a matter of principle, "put everything in its own > > scope as a matter of precautionary code hygiene"? Because I can't see any > > advantage to the inner function: > > My impression is that this is just functional programming "good > style". As you say, it's not needed, it's just "keep things valid in > the smallest range possible". Probably also related to the > mathematical style of naming sub-expressions. Also, it's probably the > case that in a (compiled) functional language like SML, the compiler > can optimise this to avoid any actual inner function, leaving it as > nothing more than a temporary name. It's also worth noting that functional languages don't typically have variables or assignments (more accurately, such things aren't fundamental to the programming model the way they are in imperative languages). So although technically let introduces a new scope, in practical terms it's basically just "how functional programmers do assignments". Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: ModuleNotFoundError: No module named 'encodings'
On 2018-09-07 17:13, Jason Qian via Python-list wrote: > Thanks Thomas, > > You are right, this seems the Python home configuration issue. > > One more question. > > Is there a way I can catch the error ( Fatal Python error: initfsencoding: > ..) as exception in the c code ? It's a fatal error, which means it aborts. As I'm sure you know, C doesn't have exceptions. You might be able to handle SIGABRT, I'm not sure. > > try{ > Py_Initialize(); > }catch(xxx) > { > > } > > > Thanks > > > > On Thu, Sep 6, 2018 at 5:29 PM, Thomas Jollans wrote: > >> On 09/06/2018 09:46 PM, Jason Qian via Python-list wrote: >> >>> Hi >>> >>> Need some help. >>> >>> I have a C++ application that invokes Python. >>> >>> ... >>> Py_SetPythonHome("python_path"); >>> >> >> This isn't actually a line in your code, is it? For one thing, >> Py_SetPythonHome expects a wchar_t*... >> >> Py_Initialize(); >>> >>> This works fine on Python 3.6.4 version, but got errors on Python 3.7.0 >>> when calling Py_Initialize(), >>> >>> Fatal Python error: initfsencoding: unable to load the file system codec >>> ModuleNotFoundError: No module named 'encodings' >>> >> >> So, Python can't find a core module. This either means your Python 3.7 >> build is broken, or it doesn't know where to look. Perhaps whatever it is >> you're actually passing to Py_SetPythonHome needs to be changed to point to >> the right place? (i.e. maybe you're giving it the location of the Python >> 3.6 library rather than the Python 3.7 one) >> >> -- Thomas >> -- >> https://mail.python.org/mailman/listinfo/python-list >> -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
Paul Moore : > On Fri, 7 Sep 2018 at 15:10, Paul Moore wrote: >> >> On Fri, 7 Sep 2018 at 14:06, Steven D'Aprano >> wrote: >> > However I have a follow up question. Why the "let" construct in the >> > first place? Is this just a matter of principle, "put everything in >> > its own scope as a matter of precautionary code hygiene"? Because I >> > can't see any advantage to the inner function: >> >> My impression is that this is just functional programming "good >> style". [...] > > It's also worth noting that functional languages don't typically have > variables or assignments (more accurately, such things aren't > fundamental to the programming model the way they are in imperative > languages). So although technically let introduces a new scope, in > practical terms it's basically just "how functional programmers do > assignments". To put it succinctly, SML does it because there's no other way to introduce local variables. IOW, in many functional programming languages, the only local variables are function arguments. And if you want to go really functional, you can't even alter bindings. So to implement a for loop in Python under these constraints, you would implement: def f(n): sum = 0 m = 1 for i in range(n): sum += m * i if sum % m == 0: m += 1 return sum like this: def f(n): def auxf1(sum, m, i): if i == n: return sum else: def auxf2(sum, m, i): if sum % m == 0: return auxf1(sum, m + 1, i) else: return auxf1(sum, m, i) return auxf2(sum + m * i, m, i + 1) return auxf1(0, 1, 0) cheating slightly with locally named functions. If cheating is not allowed, you will need a Y combinator construct... Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: don't quite understand mailing list
On 07/09/18 03:40, Steven D'Aprano wrote: On Thu, 06 Sep 2018 13:06:22 -0700, Ethan Furman wrote: On 09/06/2018 12:42 PM, Reto Brunner wrote: What do you think the link, which is attached to every email you receive from the list, is for? Listinfo sounds very promising, doesn't it? And if you actually go to it you'll find: "To unsubscribe from Python-list, get a password reminder, or change your subscription options enter your subscription email address" So how about you try that? Reto, your response is inappropriate. If you can't be kind and/or respectful, let someone else respond. No it wasn't inappropriate, and your attack on Reto is uncalled for. Reto's answer was kind and infinitely more respectful than your unnecessary criticism. As far as I can tell, this is Reto's first post here. After your hostile and unwelcoming response, I wouldn't be surprised if it was his last. His answer was both helpful and an *amazingly* restrained and kind response to a stupid question[1] asked by somebody claiming to be an professional software engineer. It was not condescending or mean- spirited, as you said in another post, nor was it snarky. But even had the OP been a total beginner to computing, it was still a helpful response containing the information needed to solve their immediate problem (how to unsubscribe from the list) with just the *tiniest* (and appropriate) hint of reproach to encourage them to learn how to solve their own problems for themselves so that in future, they will be a better contributor to whatever discussion forums they might find themselves on. Ethan, you are a great contributor on many of the Python mailing lists, but your tone-policing is inappropriate, and your CoC banning of Rick and Bart back in July was an excessive and uncalled for misuse of moderator power. To my shame, I didn't say anything at the time, but I won't be intimidated any longer by fear of the CoC and accusations of incivility. I'm speaking up now because your reply to Reto is unwelcoming, unhelpful and disrespectful, and coming from a moderator who has been known to ban people, that makes it even more hostile. [1] Yes, there are such things as stupid questions. If your doctor asked you "remind me again, which end of the needle goes into your arm?" what would you do? +1000 -- 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: I need help to put the output from terminal in a csv file
Thanks a lot, I've managed to make it work they way I wanted -- https://mail.python.org/mailman/listinfo/python-list
Re: Why emumerated list is empty on 2nd round of print?
On Fri, 7 Sep 2018 at 16:25, Schachner, Joseph wrote: >... > Now, on to the second part: the problem you showed - that you can only loop > through aList:print(i,j) once - is BECAUSE you hung onto it from one loop to > another. Once the iterator is exhausted, it's exhausted. > > Think of another more common iterator you've probably used: file = > open("filename.txt") for line in file;print line > On EOF the iterator throws an exception (that is expected), the for loop > catches the exception and exits. > Files can be "rewound" by file.seek(0). But other than that, once you have > reached EOF, you have reached EOF. It doesn't automagically rewind. If you > do another for line in file; print line it will not print anything. > Exactly the same behavior as you are surprised about for the enumerate > iterator. > > I don’t even know if there is a method to reset the enumerate iterator. If > there isn't one, then you should not hold the iterator from one loop to the > next, you have to make another one. There is no way to reset the enumerate iterator because it would not be possible in general. You can use enumerate with any iterable including an iterator that cannot itself be reset: for lineno, line in enumerate(fileobj, 1): print(lineno, line) In this case enumerate cannot reset the fileobj iterator there would be no way to implement enumerate such that it is always resettable. (Even if enumerate knew it was a file object that wouldn't necessarily make it seekable e.g. if it was a FIFO). You *could* however have enumerate return an object that was re-iterable whenever its underlying iterable is (but that's not how it is designed): class enumerate: def __init__(self, iterable, start=0): self.iterable = iterable self.start = start def __iter__(self): count = self.start for val in self.iterable: # Implicitly calls iter on iterable again yield count, val count += 1 -- Oscar -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
Marko Rauhamaa : > def f(n): > def auxf1(sum, m, i): > if i == n: > return sum > else: > def auxf2(sum, m, i): > if sum % m == 0: > return auxf1(sum, m + 1, i) > else: > return auxf1(sum, m, i) > return auxf2(sum + m * i, m, i + 1) > return auxf1(0, 1, 0) > > cheating slightly with locally named functions. > > If cheating is not allowed, you will need a Y combinator construct... ... and here's the same function without cheating: f = (lambda n: (lambda auxf1, auxf2: auxf1(auxf1, auxf2, 0, 1, 0))( lambda auxf1, auxf2, sum, m, i: sum if i == n else auxf2(auxf1, auxf2, sum + m * i, m, i + 1), lambda auxf1, auxf2, sum, m, i: auxf1(auxf1, auxf2, sum, m + 1, i) if sum % m == 0 else auxf1(auxf1, auxf2, sum, m, i))) ... or replacing the conditional with arrays: f = (lambda n: (lambda auxf1, auxf2: auxf1(auxf1, auxf2, 0, 1, 0))( lambda auxf1, auxf2, sum, m, i: [lambda m: auxf2(auxf1, auxf2, sum + m * i, m, i + 1), lambda m: sum][i == n](m), lambda auxf1, auxf2, sum, m, i: [lambda m: auxf1(auxf1, auxf2, sum, m, i), lambda m: auxf1(auxf1, auxf2, sum, m + 1, i)][sum % m == 0](m))) It is possible to get rid of the arrays and numbers, too. Combinatory logic would allow us to get rid of the local variables. At the pinnacle of functional programming is iota: https://en.wikipedia.org/wiki/Iota_and_Jot> The iota programming language has only one primitive value, the Swiss-army-knife function ι, which can express Life, Universe and Everything: ι = lambda f: (f(lambda x: lambda y: lambda z: (x(z))(y(z( lambda q: lambda i: q) Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On 2018-09-06 16:00, MRAB wrote: > On 2018-09-06 21:24, Michael F. Stemper wrote: >> On 2018-09-06 09:35, Rhodri James wrote: >>> Is it worth creating the superclass in Python? It sounds like it's a >>> bit marginal in your case. I'm not that seasoned in object-oriented >>> design either, but my yardstick would be how much common code does it >>> eliminate? >> >> About half a dozen lines. Here's the common part: >> >> def __init__( self, xmlmodel, V, id ): >> >> try: >> P_0s = xmlmodel.findall( 'RatedPower' )[0].text >> self.P_0 = float( P_0s ) >> except: >> Utility.DataErr( "Power not specified for %s load" % (id) ) >> if self.P_0<=0.0: >> Utility.DataErr( "Power for %s load must be postive, not %s" \ >> % (id,P_0s) ) > A word of advice: don't use a "bare" except, i.e. one that doesn't > specify what exception(s) it should catch. Given that I moved the first line ("P_0s = ...") out of the "try" clause, does this align with your advice? # If pre-validation has been run, guaranteed to have RatedPower P_0s = xmlmodel.findall( 'RatedPower' )[0].text try: self.P_0 = float( P_0s ) except ValueError: Utility.DataErr( "Power for %s load, '%s', not parsable" \ % (id,P_0s) ) if self.P_0<=0.0: Utility.DataErr( "Power for %s load must be postive, not %s" \ % (id,P_0s) ) > Your try...except above will catch _any_ exception. If you misspelled a > name in the 'try' part, it would raise NameError, which would also be > caught. In another case where I had a "bare exception", I was using it to see if something was defined and substitute a default value if it wasn't. Have I cleaned this up properly? try id = xmlmodel.attrib['name'] except KeyError: id = "constant power" (Both changes appear to meet my intent, I'm more wondering about how pythonic they are.) -- Michael F. Stemper Isaiah 10:1-2 -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On 2018-09-07 14:51, Michael F. Stemper wrote: > On 2018-09-06 16:00, MRAB wrote: >> On 2018-09-06 21:24, Michael F. Stemper wrote: >> A word of advice: don't use a "bare" except, i.e. one that doesn't >> specify what exception(s) it should catch. > In another case where I had a "bare exception", I was using it to see if > something was defined and substitute a default value if it wasn't. Have > I cleaned this up properly? > > try >id = xmlmodel.attrib['name'] > except KeyError: >id = "constant power" Never mind! After I continued testing, I realized that the above should have been written as: if 'name' in xmlmodel.attrib: id = xmlmodel.attrib['name'] else: id = "constant power" -- Michael F. Stemper Always use apostrophe's and "quotation marks" properly. -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On 2018-09-07 20:51, Michael F. Stemper wrote: On 2018-09-06 16:00, MRAB wrote: On 2018-09-06 21:24, Michael F. Stemper wrote: On 2018-09-06 09:35, Rhodri James wrote: Is it worth creating the superclass in Python? It sounds like it's a bit marginal in your case. I'm not that seasoned in object-oriented design either, but my yardstick would be how much common code does it eliminate? About half a dozen lines. Here's the common part: def __init__( self, xmlmodel, V, id ): try: P_0s = xmlmodel.findall( 'RatedPower' )[0].text self.P_0 = float( P_0s ) except: Utility.DataErr( "Power not specified for %s load" % (id) ) if self.P_0<=0.0: Utility.DataErr( "Power for %s load must be postive, not %s" \ % (id,P_0s) ) A word of advice: don't use a "bare" except, i.e. one that doesn't specify what exception(s) it should catch. Given that I moved the first line ("P_0s = ...") out of the "try" clause, does this align with your advice? # If pre-validation has been run, guaranteed to have RatedPower P_0s = xmlmodel.findall( 'RatedPower' )[0].text try: self.P_0 = float( P_0s ) except ValueError: Utility.DataErr( "Power for %s load, '%s', not parsable" \ % (id,P_0s) ) if self.P_0<=0.0: Utility.DataErr( "Power for %s load must be postive, not %s" \ % (id,P_0s) ) That's better. However, if P_0s can't be parse as a float, then self.P_0 will be unchanged (assuming that it already has a value, of course). Is it OK that if there's no rated power, or it's invalid, it'll report it and continue with whatever value, if any, that it already has? Your try...except above will catch _any_ exception. If you misspelled a name in the 'try' part, it would raise NameError, which would also be caught. In another case where I had a "bare exception", I was using it to see if something was defined and substitute a default value if it wasn't. Have I cleaned this up properly? try id = xmlmodel.attrib['name'] except KeyError: id = "constant power" (Both changes appear to meet my intent, I'm more wondering about how pythonic they are.) There's an alternative that's recommended when the key is often absent: id = xmlmodel.attrib.get('name', "constant power") Catching an exception has a cost, so the usual advice is to use .get when the key is often absent, or catch KeyError when the key is usually present but occasionally is absent and you can provide a default value. -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On 2018-09-07 21:08, Michael F. Stemper wrote: On 2018-09-07 14:51, Michael F. Stemper wrote: On 2018-09-06 16:00, MRAB wrote: On 2018-09-06 21:24, Michael F. Stemper wrote: A word of advice: don't use a "bare" except, i.e. one that doesn't specify what exception(s) it should catch. In another case where I had a "bare exception", I was using it to see if something was defined and substitute a default value if it wasn't. Have I cleaned this up properly? try id = xmlmodel.attrib['name'] except KeyError: id = "constant power" Never mind! After I continued testing, I realized that the above should have been written as: if 'name' in xmlmodel.attrib: id = xmlmodel.attrib['name'] else: id = "constant power" That's actually _less_ Pythonic. Have a look at the entries "EAFP" and "LBYL" here: https://docs.python.org/3/glossary.html -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On 2018-09-07 15:39, MRAB wrote: > On 2018-09-07 20:51, Michael F. Stemper wrote: >> On 2018-09-06 16:00, MRAB wrote: >>> A word of advice: don't use a "bare" except, i.e. one that doesn't >>> specify what exception(s) it should catch. >> >> Given that I moved the first line ("P_0s = ...") out of the "try" >> clause, does this align with your advice? >> >> # If pre-validation has been run, guaranteed to have RatedPower >> P_0s = xmlmodel.findall( 'RatedPower' )[0].text >> try: >> self.P_0 = float( P_0s ) >> except ValueError: >> Utility.DataErr( "Power for %s load, '%s', not parsable" \ >> % (id,P_0s) ) >> if self.P_0<=0.0: >> Utility.DataErr( "Power for %s load must be postive, not %s" \ >> % (id,P_0s) ) >> > That's better. > > However, if P_0s can't be parse as a float, then self.P_0 will be > unchanged (assuming that it already has a value, of course). This is the unique place where self.P_0 might have a value assigned. > Is it OK that if there's no rated power, or it's invalid, it'll report > it and continue with whatever value, if any, that it already has? No. If the data file doesn't have the right data, the simulation can't run. That's why DataErr reports the problem and invokes sys.exit(71). >> In another case where I had a "bare exception", I was using it to see if >> something was defined and substitute a default value if it wasn't. Have >> I cleaned this up properly? >> >> try >> id = xmlmodel.attrib['name'] >> except KeyError: >> id = "constant power" >> >> (Both changes appear to meet my intent, I'm more wondering about how >> pythonic they are.) >> > There's an alternative that's recommended when the key is often absent: > > id = xmlmodel.attrib.get('name', "constant power") Oh, I like that much better than what I showed above, or how I "fixed" it cross-thread. Thanks! -- Michael F. Stemper You can lead a horse to water, but you can't make him talk like Mr. Ed by rubbing peanut butter on his gums. -- https://mail.python.org/mailman/listinfo/python-list
Re: Object-oriented philosophy
On Fri, 07 Sep 2018 16:07:06 -0500, Michael F. Stemper wrote: >>> In another case where I had a "bare exception", I was using it to see >>> if something was defined and substitute a default value if it wasn't. >>> Have I cleaned this up properly? >>> >>> try >>> id = xmlmodel.attrib['name'] >>> except KeyError: >>> id = "constant power" >>> >>> (Both changes appear to meet my intent, I'm more wondering about how >>> pythonic they are.) Yes, catch the direct exception you are expecting. That's perfectly Pythonic. >> There's an alternative that's recommended when the key is often absent: >> >> id = xmlmodel.attrib.get('name', "constant power") > > Oh, I like that much better than what I showed above, or how I "fixed" > it cross-thread. Thanks! However, if the key is nearly always present, and your code is performance-critical, calling the "get" method has the slight disadvantage that it will be slightly slower than using the try...except form you show above. On the other hand, the get method has the big advantage that it's an expression that can be used in place, not a four-line compound statement. If I don't care about squeezing out every last bit of performance from the interpreter, I use whatever looks good to me on the day. That will often be the "get" method. But on the rare occasions I do care about performance, the basic rule of thumb I use is that if the key is likely to be missing more than about 10% of the time, I use the "LBYL" idiom (either an explicit test using "if key in dict" or just call the dict.get method). But don't stress about the choice. Chances are that any of the three options you tried (catch KeyError, check with "if" first, or using the get method) will be good enough. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: don't quite understand mailing list
On Fri, 07 Sep 2018 07:39:33 +0300, Marko Rauhamaa wrote: > I'm with Ethan on this one. > > There was nothing in the original posting that merited ridicule. Then its a good thing there was nothing in the response that was ridicule. (A mild rebuke for a mild social faux pas is not ridicule.) -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list
Re: Any SML coders able to translate this to Python?
On Fri, 07 Sep 2018 15:10:10 +0100, Paul Moore wrote: > On Fri, 7 Sep 2018 at 14:06, Steven D'Aprano > wrote: [...] >> However I have a follow up question. Why the "let" construct in the >> first place? Is this just a matter of principle, "put everything in its >> own scope as a matter of precautionary code hygiene"? Because I can't >> see any advantage to the inner function: > > My impression is that this is just functional programming "good style". > As you say, it's not needed, it's just "keep things valid in the > smallest range possible". Probably also related to the mathematical > style of naming sub-expressions. Also, it's probably the case that in a > (compiled) functional language like SML, the compiler can optimise this > to avoid any actual inner function, leaving it as nothing more than a > temporary name. I guessed it would be something like that. Thanks Paul, and especially Marko for going above and beyond the call of duty with his multiple translations into functional-style Python, and everyone else who answered. -- Steven D'Aprano "Ever since I learned about confirmation bias, I've been seeing it everywhere." -- Jon Ronson -- https://mail.python.org/mailman/listinfo/python-list