Re: How to get which attribute causes the AttributeError except inspecting strings?
On 03/07/2016 09:46 AM, ZhangXiang wrote: In python3, when I write code like this: try: fields = [getattr(Product, field) for field in fields.split(',')] except AttributeError as e: raise HTTPError(...) I want to raise a new type of error giving a string telling the user which attribute is not valid. But I don't see any method I can use to get the attribute name except inspecting e.args[0]. Could anyone give me a hint? Maybe I miss something. By the way, I don't quite want to change my code to a for-loop so I can access the field variable in exception handling. Hi, It is strange to morph an AttributeError in an HTTPError, anyway, you could write try: fields = [getattr(Product, field) for field in fields.split(',')] except AttributeError as e: raise HTTPError(str(e)) Or if you really need to write some custom message, you could parse the attribute error message for the attribute name. # I've not tested this code !! import re try: fields = [getattr(Product, field) for field in fields.split(',')] except AttributeError as e: # e.message should look like "class Foo has no attribute 'a'" attr = re.search(r"'(\w+)'", e.message).group(1) raise HTTPError('Custom message for the attribute %s' % attr) Tbh I don't like it but it could do the job until someone raises you an attribute error with a different string pattern. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On 03/07/2016 11:51 PM, Fillmore wrote: learning Python from Perl here. Want to do things as Pythonicly as possible. I am reading a TSV, but need to skip the first 5 lines. The following works, but wonder if there's a more pythonc way to do things. Thanks ctr = 0 with open(prfile,mode="rt",encoding='utf-8') as pfile: for line in pfile: ctr += 1 if ctr < 5: continue allVals = line.strip().split("\t") print(allVals) what about a generator expression ? The (not so)new hype: with open(prfile,mode="rt",encoding='utf-8') as pfile: for values in (l.strip().split("\t") for (i, l) in enumerate(pfile) if i >=5): print values slightly dense, could be better with a lambda function tovalues = lambda l: l.strip().split("\t") with open(prfile,mode="rt",encoding='utf-8') as pfile: for values in (tovalues(l) for (i, l) in enumerate(pfile) if i >=5): print values This should even work quite efficiently on big files, because I don't thing no more than one line is in memory at a given time. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On 03/08/2016 05:49 PM, justin walters wrote: Correct me if I'm wrong, but don't python generators usually use the yield statement so they can be used in list comprehensions? Hello, Please don't top post. Generator expressions are different from generator functions. They are both generators but use a different syntax. generator function: def func(iterable): for i in iterable: yield i the generator expression version would be (i for i in iterable) Both have their use cases but everytime you can actually use an generator expression, it's probably the correct thing to do. Cheers, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: context managers inline?
On 03/10/2016 07:59 PM, Neal Becker wrote: sohcahto...@gmail.com wrote: On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed? with open('my file', 'rb') as f: x = load(f) But not in a 1-line, composable manner? You you really want a 1 liner with open('my file', 'rb') as f: print 'foo'; x = load(f) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Encapsulation in Python
On 03/10/2016 02:41 PM, Ben Mezger wrote: Hi all, I've been studying Object Oriented Theory using Java. Theoretically, all attributes should be private, meaning no one except the methods itself can access the attribute; public class Foo { private int bar; ... Normally in Java, we would write getters and setters to set/get the attribute bar. However, in Python, we normally create a class like so; class Foo(object): bar = 0 ... And we usually don't write any getters/setters (though they exist in Python, I have not seen much projects making use of it). We can easily encapsulate (data hiding) Foo's class using the '_' (underscore) when creating a new attribute, however, this would require all attributes to have a underscore. According to this answer [1], it's acceptable to to expose your attribute directly (Foo.bar = 0), so I wonder where the encapsulation happens in Python? If I can access the attribute whenever I want (with the except of using a underscore), what's the best way to encapsulate a class in Python? Why aren't most of the projects not using getters/setters and instead they access the variable directly? Regards, Ben Mezger Strictly speaking there is not such things as public/private attributes in python. All attributes are public. '_' is just a (good) convention to tell the class users "don't mess with this attribute, don't read it nor write it". And the python way is to stick to this, trust your users to not use your 'private' attributes they've been warned. Short story : in Python we don't hide, we flag. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Enum questions.
On 04/13/2016 12:12 PM, Antoon Pardon wrote: I have been looking at the enum documentation and it seems enums are missing two features I rather find important. 1) Given an Enum value, someway to get the next/previous one 2) Given two Enum values, iterate over the values between them. Did I miss those in the documentation or are they really missing? From the doc : "While Enum and IntEnum are expected to cover the majority of use-cases, they cannot cover them all. Here are recipes for some different types of enumerations that can be used directly, or as examples for creating one’s own." I would disagree with you when you state that the features you mentioned are important. They could be useful, in certain cases but most of your code would better be agnostic to the enum value. Now it is possible that you specifically work with a system where those features would be really useful. As mentioned by the documentation, subclassing Enum is possible: tested with python 2.7 from enum import IntEnum class UltimateEnum(IntEnum): @classmethod def range(cls, *args): enumToVal = lambda e: e.value if isinstance(e, cls) else e return (i for i in cls if i.value in range(*map(enumToVal, args))) @property def next(self): it = iter(self.__class__) try: for e in it: if e is self: return next(it) except StopIteration: return None return None class Foo(UltimateEnum): A = 1 B = 4 C = 9 D = 28 print "first to C:" for f in Foo.range(Foo.C): print f print "B to D :" for f in Foo.range(Foo.B, Foo.D): print f print "A to D+1 with step 2 : " for f in Foo.range(Foo.A, Foo.D.value+1, 2): print f print "next property" print Foo.A.next print Foo.C.next print Foo.D.next In [1]: run test.py first to C: Foo.A Foo.B B to D : Foo.B Foo.C A to D+1 with step 2 : Foo.A Foo.C next property Foo.B Foo.D None Hope it helps, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Client support automation and self service
On 05/03/2016 10:02 AM, musoke wilson wrote: Hi Guys Currently working with a team to automate business operations and client support for a small enterprise. Key requirements: Clients to register, log queries and initiate service request through The Web and/or Mobile APP Clear tracking by the CRM team (SR alert through email/mobile APP) Real time support (IM/email/mobile phone APP) Analysis & Visualization of enrollment/Usage stats We are considering using possibility of using the Python-twisted framework Kindly advise if python & twisted framework are appropriate tools and any other possible open source solutions to avoid re-inventing the wheel regards Wilson If no one in your team is able to answer that question I'd rather seek for professional support than asking this mailing list. You need something better than "yes twisted can do what you want". (it surely can) However, regarding open source alternative, you could implement a client support using bugzilla, probably with a litle bit of tuning. https://www.bugzilla.org/ Regarding "business operations" which I'm not sure what it means exactly, look at http://www.tryton.org/ jm -- https://mail.python.org/mailman/listinfo/python-list
Re: What should a decorator do if an attribute already exists?
On 05/10/2016 05:45 PM, Steven D'Aprano wrote: I have a decorator that adds an attribute to the decorated function: [snip] I think 5 is clearly wrong, 4 is too difficult, and 3 seems pointless. So I think either 1 or 2 is the right thing to do. Thoughts? It depends if the attribute "instrument" is part of the public interface. If not, just find a different name unlikely to clash with an existing one *and* raise an exception if it does ever happen anyway. If the attribute is part of the public interface but you re using the code only internally then 1/ raise an exception, otherwise I don't know :o) It seems to me that over-writing silently (or not) may lead to bugs difficult to spot. jmp -- https://mail.python.org/mailman/listinfo/python-list
Re: OT: limit number of connections from browser to my server?
On 05/16/2016 06:06 PM, Grant Edwards wrote: This is not Python specific, though I'm turning to Python to do some experimentation and to try to prototype a solution. Is there any way to limit the number of connections a browser uses to download a web page? Browser writers seems to assume that all https servers are massively parallel server farms with hardware crypto support. So, when a browser wants to load a page that has the main html file, a css file, a javascript library or two, and a few icons and background bitmaps, they browser opens up a half-dozen SSL connections in parallel. That's fine when the server is Facebook's server farm. But when it's a small embedded device running at 40MHz with a single-threaded web server and software crypto, it turns a 2-second page load time into a 15-second page load time. When we first added https support years ago, this wasn't a problem. A browser would open _an_ SSL connection (handshake time around 2 seconds), and then send mutliple HTTP requests over that connection to grab a half-dozen files. Each HTTP request would take a few tens of milliseconds, and life was good. Now that 2-second page load takes up to 10-15 seconds because of all the SSL connection setup overhead involved in handling a half-dozen "parallel" connections. I was _hoping_ there was an HTTP header or HTML meta tag that could be used to smack the browser with a clue bat, but there doesn't seem to be. [Please tell me I'm wrong...] Some browsers used to have a global "max parallel connections" setting that the user could control, but a) that seems to be gone from recent versions of browsers I've looked at, and b) we can't ask customers to change that setting just for the benefit of our devices. So now I'm going to set up a simple Python HTTP server to try some other approaches: 1) Only allow the listening socket to accept 1 connection at a time. 2) Accept the TCP connection, but don't allow the SSL handshaking to start on the "extra" connections. 3) ??? 4) Profits! Any ideas? Have you considered upgrading the device with a recent CPU ? Or is it completely out of the picture ? Depending on what you are selling, it may be actually cheaper than spending time trying to make it work. You could also "externalize" the web service, a 35$ raspberry pi would do it. Of course I do realize that everything I said may not make any sense, we'd need to know a little bit more about the "device". If 35$ double the price, that may not be a good idea. That being said, your first idea seems also a good lead, have your server refuse more than one connection. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: OT: limit number of connections from browser to my server?
On 05/16/2016 09:31 PM, Grant Edwards wrote: On 2016-05-16, jmp wrote: Have you considered upgrading the device with a recent CPU ? Or is it completely out of the picture ? Not an option. We have to continue to support devices that are in the field. The newer models that are coming out now run at 133MHz instead of 44MHz, and page load times for https still aren't much better. That being said, your first idea seems also a good lead, have your server refuse more than one connection. Just got to figure out how to try it out without wasting a lot of time reverse-engineering the web server. Hence a Python prototype. :) I don't have time to read the whole thread but if I got it right, the main CPU consuming part is the crypto. Why not drop the https part an support only http ? Is is a device that needs to be accessed in untrusted networks ? Sorry for asking the obvious :o) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Summary grid
On 06/22/2016 04:46 PM, Jignesh Sutar wrote: Say I have list of data as given in the example code below, I want to find all the unique categories (alphabetic letters) and unique IDs (numbers) and then produce a summary grid as manually entered in the "results". How could I code this? Many thanks in advance, Jignesh data= ["A.1", "A.2", "A.3", "B.1", "C.2", "C.3", "D.4", "E.5", "E.6"] cols=[] rows=[] for item in data: i=item.split(".") if i[0] not in cols: cols.append(i[0]) if i[1] not in rows: rows.append(i[1]) print cols print rows results= [["Row/Col", "A", "B", "C", "D", "E"], [1, 1, 1, 0, 0, 0], [2, 1, 0, 1, 0, 0], [3, 1, 0, 1, 0, 0], [4, 0, 0, 0, 1, 0], [5, 0, 0, 0, 0, 1], [6, 0, 0, 0, 0, 1]] Easily done using only builtins and list comprehension: data= ["A.1", "A.2", "A.3", "B.1", "C.2", "C.3", "D.4", "E.5", "E.6"] # python magic with the "zip" function # "set" will take care of removing duplicates categories, ids = map(set, zip(*[d.split('.') for d in data])) results = [] for id_ in sorted(map(int,ids)): results.append([data.count("%s.%d" % (cat, id_)) for cat in sorted(categories)]) print results If you don't really understand the zip function: http://nedbatchelder.com/text/iter.html The whole presentation is worth reading. Cheers, Jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Namespaces are one honking great idea
On 07/01/2016 04:13 PM, Steven D'Aprano wrote: But classes are not like the others: they must be instantiated before they can be used, and they are more than just a mere namespace grouping related entities. Classes support inheritance. Classes should be used for "is-a" relationships, not "has-a" relationships. Although classes (and instances) are namespaces, they provide fundamentally different kind of behaviour than modules and packages. A namespace would not hurt but I really don't get why you don't consider classes a valid and rather helpful namespace. 1/ classes do not have to be instantiated. 2/ the fact that classes are more than a namespace is not an argument. Almost any object in python is able to do more than what you are actually using. 3/ Classes are used as much as 'is-a' than 'has-a', class instances *have* a state usually described by attributes 4/ "Although classes (and instances) are namespaces, ". You seem to contradict yourself. It was probably a rhetorical construct but it's rather confusing. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Namespaces are one honking great idea
On 07/04/2016 01:37 PM, Chris Angelico wrote: On Mon, Jul 4, 2016 at 9:23 PM, jmp wrote: On 07/01/2016 04:13 PM, Steven D'Aprano wrote: But classes are not like the others: they must be instantiated before they can be used, and they are more than just a mere namespace grouping related entities. Classes support inheritance. Classes should be used for "is-a" relationships, not "has-a" relationships. Although classes (and instances) are namespaces, they provide fundamentally different kind of behaviour than modules and packages. A namespace would not hurt but I really don't get why you don't consider classes a valid and rather helpful namespace. 1/ classes do not have to be instantiated. 2/ the fact that classes are more than a namespace is not an argument. Almost any object in python is able to do more than what you are actually using. 3/ Classes are used as much as 'is-a' than 'has-a', class instances *have* a state usually described by attributes 4/ "Although classes (and instances) are namespaces, ". You seem to contradict yourself. It was probably a rhetorical construct but it's rather confusing. Functions within the namespace can't call other functions within the same namespace using unqualified names. This was a stated requirement. ChrisA Ho, I missed that one. But if it's the only missing requirement, wouldn't be like stating that python instances are not instances because methods cannot call other methods without "self."ed qualified name ? We like explicit qualified stuff in python right ? ("explicit is better than implicit") jm -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
On 09/05/2016 05:27 PM, Smith wrote: Hello to all, I wanted to know because even though the files are present on the directory I write input gives me "file not found". You can help me? Thank you a = input("Digita la directory dove vuoi trovare i file py: ") for file in os.listdir(a): if file.endswith(".py"): print(file) else: break print("File not found") Hi, try a = input("Digita la directory dove vuoi trovare i file py: ") for file in os.listdir(a): if file.endswith(".py"): print(file) you don't want to break whenever a file does not fit the requirement, you want to to continue the iteration. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
On 09/05/2016 05:41 PM, Smith wrote: Il 05/09/2016 17:34, Rustom Mody ha scritto: So what do you get when you replace the if-else with a simple: print(file) a = input("search for files with the extension .py into directory: ") for file in os.listdir(a): if file.endswith(".py"): print(file) search for files with the extension .py into directory: /home/ filepy.py tempo.py filescript.py ticker.py toolgen.py words.py m.py scrapingweb.py partitesnai.py pythonprova.py scraper.py snmp.py printreturn.py multiping.py scraping.py funzionipython.py Is that what you're expecting ? A slightly different version: import glob, os a = input("search for files with the extension .py into directory: ") print glob.glob(os.path.join(a, '*.py')) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggestions to improve a code
On 09/06/2016 04:55 PM, GP wrote: I have a list: shelves2 =[{'Part No': '1', 'Length': 610.0, 'Width': 50.0}, {'Part No': '2', 'Length': 2319.0, 'Width': 465.0 }, {'Part No': '3', 'Length': 5.0,'Width': 465.0}] The length of shelf is calculated as follows: 1. Calculate the maximum length of all items in the shelf. 2. Calculate width of shelf = 610 (in this case). 3. Length of shelf : maxlength of all items + length ( if it less than width of shelf) or width of other items such that length of shelf is less than maximum allowed length. In the above case the length is 2319+50+5 = 2374. I have the following code which generates it correctly but would like to know if there is a better way to write it. I would like to know some smarter way to write the code ( especially if the "for", "if" statements can be avoided). list_1=[] list_2=[] WidthOfShelf = max([x['Width'] for x in shelves2]) MaxLengthOfItem = max([x['Length'] for x in shelves2]) f_LMax=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] ['Length'] >= m][0] MaxLengthOfItem_Index =f_LMax(shelves2,MaxLengthOfItem) current= MaxLengthOfItem list_1=[shelves2[MaxLengthOfItem_Index]] list_2 = [MaxLengthOfItem] del shelves2[MaxLengthOfItem_Index] for i in range(0,len(shelves2)): if shelves2[i]['Length'] <= Width: if (current + shelves2[i]['Length']) or (current + shelves2 [i]['Width'])<= 2438.5 : q_2= min(shelves2[i]['Length'], shelves2[i]['Width']) q=shelves2[i] list_1.append(q) list_2.append(q_2) current += (shelves2[i]['Length'] or shelves2[i] ['Width']) length = sum(list_2) print("LENGTH:",length) Hi, Your code does not make any sense. Before making it smarter, you need to make it understandable if you want us to help you. It's ok to write quick and dirty scripts, until you send them for others to review :) Here are my advices: 1/ use meaningful names * list_1, list_2, q_2, q, current give no clue about what information they're supposed to hold * you can use comments instead as sometimes a short name won't do it 2/ provide code that reproduces your problem, executing the above code gives a NameError. ---> 30 if shelves2[i]['Length'] <= Width: NameError: name 'Width' is not defined 3/ name different things with different names. at the end of your code you're printing "length", which is the sum of either length or width fields. It's difficult to guess what you're trying to achieve. Same confusion about the field 'Length' of a shelf, and the length of a shelf that is computed elsewhere. My guess would be that a shelf has an innate length, and another one depending on the shelves it's inserted into. If so, find 2 different names. 4/ You are iterating like a C/C++ addict. Short story: resist the urge to iterate over indexes. for i in range(0,len(shelves2)): print(shelves2[i]['Length']) is best written for shelf in shelves2: print(shelf['Length']) long story : http://nedbatchelder.com/text/iter.html 5/ use list comprehension to create filtered list: for shelf in [s for s in shelves2 if s['Length'] <= s['Width']]: ... Hope it helps somehow. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: What you can do about legalese nonsense on email
On 09/08/2016 10:52 PM, Ben Finney wrote: Joaquin Alzola writes: That's not true; you can do more than we. Added by the MTA of the company not by my client. Right. So, here are things you can do (that we cannot) about this: * Complain, with reasoned explanation, to the management responsible for deciding whether that nonsense continues to be added to every message. If they haven't heard the complaint before, they will no longer have that excuse. If they have heard the complaint before, yours will add to the weight of evidence that this is not wanted. * Switch to a different mail service, one which does not add that nonsense to your email. Yes, this is inconvenient for you. But it's impossible for us to do on your behalf, so you are in a position of more power than us. * Complain again to the management responsible for the ongoing decision to place nonsense on your email, that you have been forced to use a different mail system in order not to appear contemptuous of the communities in which you participate. If you are participating for reasons of benefit to the organisation, point out that their ongoing decision is making that more difficult for their employees. These are all powers you have; please don't feel that you can do nothing. I've been there, I feel the pain of both sides. Complaining won't do much, companies are not democracies, people are right because their title says so, not because they actually are. I personally had to switch to a usenet client instead of a mail client (I think so). I'm using thunderbird for that purpose configured with news.gmane.org The good news is that you don't need yet another fake email address. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: [Python-ideas] Inconsistencies
On 09/12/2016 03:11 AM, Chris Angelico wrote: On Mon, Sep 12, 2016 at 6:30 AM, Sven R. Kunze wrote: I could not agree more with what you said above, so I hope this will put the discussion in better perspective, especially when people here trying to be overly absolute in their views (which was the quote about). Strange that you think I'm a more moderate position. I have some _extremely_ strong views about absolutes (they come from the Creator of the Universe), and it's not just abstract concepts like numbers that have absolutes. There are hard-and-fast facts about the world, and even about human nature, that stem from the way we were made. However, lengthy discussion of such things is WAY off topic here. ChrisA Let me help you phrase on-topic nonsense: I strongly believe that God is within each "self" parameter of each method ever written since 1989 aka 0 BGvR :o) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to split value where is comma ?
On 09/11/2016 02:12 PM, Chris Angelico wrote: On Thu, Sep 8, 2016 at 7:27 PM, Joaquin Alzola wrote: I have worked places where they put stuff like this at the bottom of emails sent to the person sitting next to them :) -raising entropy-ly yrs- Robin Becker Cannot do anything about it. It is not on my MTA client and it is added by the company server :( [snip] Tell your bosses how stupid it makes the company look. ChrisA That could get someone into troubles. I would not follow that advice before considering the probable outcomes. I'd rather go for the change of mail server. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: sphinx (or other means to document python)
On 09/25/2016 03:20 AM, chitt...@uah.edu wrote: On Sunday, September 11, 2016 at 3:56:36 PM UTC-5, chit...@uah.edu wrote: (about being frustrated with sphinx) I _remain_ frustrated - even as I finally figured out how to use it (thanks to a complete example from a friend) sphinx is very picky about spaces, lines - I had a line with some math formula spaces and tabs (after r''' - and sphinx kept ignoring that line when it works, the documentation (my preference is LaTeX) is great - the procedure for embedding the documentation as doctrings can be difficult, at times noweb is considerably simpler - but does not allow for the extraction of docstrings/comments - and does provide for a fairly painless way to combine comments, documentation along with code Hi, Keep in mind sphinx has a greater scope than writing docs from/for python. You could use sphinx to document anything. That why it's not that straightforward to build doc from the source code. Yet it's possible with some scripts available on the internet. But it looks like you've managed to find out. Ultimately, sphinx is designed to write good documentation, and is slightly overkill if you want to build docs only from the source code. But keep in mind that this kind of doc tend to be poor. If you take a look at the python documentation, the paramount of good documentation :) you'll notice it's not generated from the code docstrings. For api reference documentation, the source code is sometimes the best option. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 05:25 PM, Cai Gengyang wrote: I just wanted to note that sometimes the code works, sometimes it doesn't. (even though both are exactly the same code) ... Weird , dum dum dum for row in range(10): for column in range(10): print("*",end="") SyntaxError: inconsistent use of tabs and spaces in indentation for row in range(10): for column in range(10): print("*",end="") Here, from itertools import product for row,column in product(range(10), range(10)): print("*", end="") Problem solved :) On a more serious note, they're not the same code, in the same way than print("foo") print("bar") is not the same code than print("foo")print("bar") You're fooled by your editor which displays 2 different characters the same way. Tab or space does matter in python, get over it and move on with your python life. Actually python is a great debugger for misconfigured text editors. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Nested for loops and print statements
On 09/26/2016 06:54 PM, Cai Gengyang wrote: What is a tab and what is a space in python and what's the difference ? Which piece of code is indented with tabs and which one is indented with spaces ? Please do not top-post in this list. Put your text after the message you quote. Tabs and spaces are 2 different characters, tab ASCII code is 9, space ASCII code is 32. You *need* to understand that they are 2 differents characters. The problem come with their representation. 1/ a space is represented by 1 whitespace 2/ a tab is represented by x whitespaces. The number x depends on your text editor. When you want to indent a python block, you put whitespaces on its left to indent it. You can indent using either tabs or spaces. Python does not tell you which one to use, but it tells you to choose *one* method, and stick with it. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a way to change the closure of a python function?
On 09/27/2016 04:01 PM, Peng Yu wrote: Hi, In many other functional language, one can change the closure of a function. Is it possible in python? http://ynniv.com/blog/2007/08/closures-in-python.html If I understood correctly your link: (untested) def func(x): return x+func.y func.y = 10 func(5) => 15 func.y = 100 func(5) => 105 implements a closure of a function. jm Note: function are objects, and can have attributes, however I rarely see usage of these, there could be good reasons for that. -- https://mail.python.org/mailman/listinfo/python-list
Re: User Interface Suggestions? (newbie)
On 10/05/2016 11:33 PM, Chris Angelico wrote: On Thu, Oct 6, 2016 at 8:19 AM, Beverly Howard wrote: Thanks for the responses... appreciated. print("value value data data data", end="\r") << That makes sense, but it also seems to suggest that there is no other way to position the cursor prior to printing. For example, if that line is halfway down the screen, is there any way to position the cursor two lines above it? fwiw, I am open to creating functions if there are positioning options, both to meet the need, but, more importantly, to learn. What I showed you was the very simplest way of doing things. If you want to move the cursor around, I would recommend the 'curses' library: https://docs.python.org/3/library/curses.html https://docs.python.org/3/howto/curses.html There are other ways, too; with more info on what you're trying to accomplish, we could better advise. It might be that a GUI will serve you well, particularly if you have several pieces of information that you want to update. ChrisA Since someone mentioned curses, I'll add that I've used npyscreen (built on top of ncurses), successfully, creating small guis with very few code lines. If you like oldschool gui, it's a must. http://npyscreen.readthedocs.io/introduction.html jm -- https://mail.python.org/mailman/listinfo/python-list
Re: default argument value is mutable
On 10/07/2016 01:38 PM, Daiyue Weng wrote: Hi, I declare two parameters for a function with default values [], def one_function(arg, arg1=[], arg2=[]): PyCharm warns me: Default argument value is mutable, what does it mean? and how to fix it? cheers You'll run into this bug def foo(a=[]): a.append('item') print a foo() foo() ['item'] ['item', 'item'] #what ?? 2 'item' ?? default argument are evaluated when defining the function, meaning the list you bound to the default argument is shared by all the function calls. Easily fixed: def foo(a=None): a = [] if a is None else a print a foo() foo() ['item'] ['item'] So the rule of thumb for default argument value is "No mutable" Cheers, Jm note : python 2.7 code -- https://mail.python.org/mailman/listinfo/python-list
Re: default argument value is mutable
On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message de news:mailman.209.1475841371.30834.python-l...@python.org... On 10/07/2016 01:38 PM, Daiyue Weng wrote: So the rule of thumb for default argument value is "No mutable" Cheers, It can be used to store some variables from one call of a function to an other one. def test( _store={'x':0}): x = _store['x'] . do some stuff _store['x'] = x For personal dirty scripts, possibly, for all other situations, never. Especially since there's nothing in the code above that cannot be solved using standard idioms . That is if you care about anyone reading your code ;) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: default argument value is mutable
On 10/07/2016 03:45 PM, ast wrote: "jmp" a écrit dans le message de news:mailman.210.1475844513.30834.python-l...@python.org... On 10/07/2016 02:07 PM, ast wrote: "jmp" a écrit dans le message de news:mailman.209.1475841371.30834.python-l...@python.org... On 10/07/2016 01:38 PM, Daiyue Weng wrote: So the rule of thumb for default argument value is "No mutable" Cheers, It can be used to store some variables from one call of a function to an other one. def test( _store={'x':0}): x = _store['x'] . do some stuff _store['x'] = x For personal dirty scripts, possibly, for all other situations, never. not so dirty in my opinion You made a point, it's not that dirty, it's an "advanced technique" that is often actually an error when you don't know what you're doing. See the OP's code. I'm in an environment where people use python as a tool more than an effective powerful language to write complex applications. That's probably why I'm bias on this issue and prefer the cautious approach. Especially since there's nothing in the code above that cannot be solved using standard idioms . Yes, putting _store dictionnary outside _store={'x':0} def test( ): x = _store['x'] . do some stuff _store['x'] = x or using a global variable, but you pollute in both case the global scope unnecessary. What about def test(): if not hasattr(test, '_store'): test._store={'x':0} test._store['x'] += 1 Neither you pollute the global namespace, nor you pollute the function signature with implementation specifics. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to use two threads (GUI and backend)
On 10/26/2016 12:22 PM, pozz wrote: Il 26/10/2016 09:13, pozz ha scritto: > [...] When the user press Start button (the pressed handler is in the GUI class): self.comm_active = True threading.Thread(target=self.comm_thread).start() The backend thread is: def comm_thread(self): while self.comm_active: self.device.get_status() GLib.idle_add(self.polling_received) time.sleep(1) self.m.close() > [...] Now I have some concerns even in using self.comm_active. It is a boolean variable accessed by the GUI thread (inside Start/Stop buttons handler) and backend thread (in the "while self.comm_active" instruction). Is it safe to access this variable from two different threads? Should I implement a safer and more complex mechanism? If yes, what mechanism? from http://nedbatchelder.com/blog/201204/two_problems.html Some people, when confronted with a problem, think, "I know, I'll use threads," and then two they hav erpoblesms. I suggest you write a GUI that make synchronous calls to a remote application, if possible. If the remote app is in python, you have access to remote protocols already written for you, Pyro is one of them, you can skip the low level communication part. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to use two threads (GUI and backend)
On 10/26/2016 02:45 PM, pozz wrote: Il 26/10/2016 13:16, jmp ha scritto: [...] I suggest you write a GUI that make synchronouscalls to a remote application, if possible. If the remote app is in python, you have access to remote protocols already written for you, Pyro is one of them, you can skip the low level communication part. I'm not sure Pyro (or similar alternatives) helps in my case. The real problem is that retrieving status from remote device is a slow operation. If the GUI thread blocks waiting for the answer, the GUI blocks and the user complains. From Pyro documentation: --- Normal method calls always block until the response is returned. This can be any normal return value, None, or an error in the form of a raised exception. The client code execution is suspended until the method call has finished and produced its result. --- So, even with Pyro, I need to have another thread that manages Pyro communication (instead of serial communication)... additional problems. Also from the Pyro doc: You can execute a remote method call and tell Pyro: “hey, I don’t need the results right now. Go ahead and compute them, I’ll come back later once I need them”. The call will be processed in the background and you can collect the results at a later time. [...] It is possible to define one or more callables (the “call chain”) that should be invoked automatically by Pyro as soon as the result value becomes available. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to use two threads (GUI and backend)
On 10/27/2016 12:22 PM, pozz wrote: Anyway I don't like this approach, because the main (and single) thread should check in_waiting every X milliseconds. If X is too high, I could wait for the answer even if it is already ready in the input buffer. If X is too low, the application consumes a lot of clocks to check in_waiting. I would prefer to have a callback automatically called when the read operation is complete. And I think the only method is using another (blocking) thread. The blocking function read returns *immediately* when all the bytes are received. And I think during blocking time, the thread isn't consuming CPU clocks. Threads do consume CPU clocks. An operation within a thread will not consume less CPU clocks, however, the scheduler will interrupt the thread and give other threads/operations a chance to process as well. Threads implement paralellism, not performances. From what I understand of your context, you don't want you GUI to "freeze" when waiting for the remote application. That's a valid concern. You can use threads to fix that(or you can use already written working python libraries that would mask this low level programing, it's up to you). What you should not do is focus on gaining "CPU clocks". You just don't care. It's probably not an issue. If it is, drop python and implement your app in C. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: How to use two threads (GUI and backend)
On 10/27/2016 01:43 PM, Chris Angelico wrote: Blocked threads don't consume CPU time. Why would they? ChrisA Agreed. My point being that a blocked thread achieve nothing, except parallelism, i.e. other threads can be processed. To be more specific, if you compute factorial(51354) in a thread, it will still require approx. the same amount of CPU clocks than in a main thread (probably slightly more due to the scheduler overhead). jm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to use two threads (GUI and backend)
On 10/27/2016 02:55 PM, Chris Angelico wrote: On Thu, Oct 27, 2016 at 11:33 PM, jmp wrote: On 10/27/2016 01:43 PM, Chris Angelico wrote: Blocked threads don't consume CPU time. Why would they? ChrisA Agreed. My point being that a blocked thread achieve nothing, except parallelism, i.e. other threads can be processed. To be more specific, if you compute factorial(51354) in a thread, it will still require approx. the same amount of CPU clocks than in a main thread (probably slightly more due to the scheduler overhead). jm Of course. But the OP wants to do blocking calls, which don't cost you like that. So it's fine. ChrisA Sure but the OP is very focus on performance(that's a mistake imo). "Because I don't want to drop python, I want to learn the best technique to use to have the best performance. " I just wanted to point that using thread implements parallelism, not performance. And that's probably what its gui needs. And that's probably why using a higher level API would have been acceptable. JM Best performance is achieved by sacrificing a lot in python. A better technique than polling threads would be sleeping thread where the thread is put to hold until a hardware interrupt wakes up the thread. -- https://mail.python.org/mailman/listinfo/python-list
Re: Enum with only a single member
On 01/10/2017 05:43 AM, Steven D'Aprano wrote: Is it silly to create an enumeration with only a single member? That is, a singleton enum? Don't think so, for the same reason that lists with one element make sense. def ham(arg): if isinstance(arg, MarxBros) or arg is Unique.FOO: ... Good, bad or indifferent? Though I'd write def ham(arg): if arg in MarxBros or arg in Unique: ... JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Temporary variables in list comprehensions
On 01/09/2017 04:53 AM, Steven D'Aprano wrote: Suppose you have an expensive calculation that gets used two or more times in a loop. The obvious way to avoid calculating it twice in an ordinary loop is with a temporary variable: result = [] for x in data: tmp = expensive_calculation(x) result.append((tmp, tmp+1)) But what if you are using a list comprehension? Alas, list comps don't let you have temporary variables, so you have to write this: [(expensive_calculation(x), expensive_calculation(x) + 1) for x in data] Or do you? ... no, you don't! [(tmp, tmp + 1) for x in data for tmp in [expensive_calculation(x)]] I can't decide whether that's an awesome trick or a horrible hack... In any situation, the double list comprehension (also used to flatten lists) is very difficult to read. What about for x in (f(d) for d in data): result.append(x, x+1) There's a double for loop in the same line but the generator parenthesis help a lot. No lame tmp variable involved. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Please don't make unfounded legalistic demands
On 08/26/2015 11:20 PM, Terry Reedy wrote: On 8/26/2015 12:36 PM, Jean-Michel Pichavant wrote: [snip] Are you allowed to use a newsreader or a mail+newsreader (Outlook Express, Thunderbird, )? If so post through newsgroup gmane.comp.python.general at news.gmane.org (as I am). I screwed already one time let's see if this post go through jm -- https://mail.python.org/mailman/listinfo/python-list
Re: OFF-TOPIC Ben's sig monster quote [was Re: Parametrized Unit Tests]
On 08/28/2015 11:24 AM, Marko Rauhamaa wrote: Would you rather be an powerful, armed war hero admired and feared by your nation or a foresaken unemployed drunkard who rots in jail? Marko Time to quote the most famous general in the galaxy: “Ohhh. Great warrior.Wars not make one great.” ;) JM -- https://mail.python.org/mailman/listinfo/python-list
Re: How to compare lists
On 09/01/2015 07:08 AM, Jahn wrote: 1. How can I save 256 lists, each list has 32 values( hexadecimal numbers) 2. How to compare the saved lists with another 256 lists ( that are read online and have the same structure as the list one)? ( the first list must be saved in the previous step) E.g Thanks Dumb down your problem to something simpler. saving 2 lists of 2 numbers. 1/ for saving/loading the list, use pickle if *you* will do the saving *and* the loading (don't load from an untrusted file) 2/ To compare 2 lists, simply use the == operator In [4]: [[1,2], [1,2]] == [[1,2], [1,3]] Out[4]: False In [5]: [[1,2], [1,2]] == [[1,2], [1,2]] Out[5]: True JM -- https://mail.python.org/mailman/listinfo/python-list
Re: issue while doing pexpect ssh
On 09/08/2015 03:57 PM, Chris Angelico wrote: On Tue, Sep 8, 2015 at 9:37 PM, wrote: Some where i am missing simple logic :) = child = pexpect.spawn('ssh hari@hostname') child.logfile = sys.stdout child.expect('hari\'s Password: ') = getting error as follows: child.expect('hari\'s Password: ') TypeError: must be str, not bytes === Laura's already answered your actual question. But I would recommend using public key login [snip] ChrisA My 2 cents, beside the public key, use the python module paramiko, unless you really want to work at the low level yourself. http://docs.paramiko.org/en/1.15/api/client.html JM -- https://mail.python.org/mailman/listinfo/python-list
Re: From logging to files to a better solution: syslog, Sentry, Logstash, ....
On 09/11/2015 09:22 AM, Thomas Güttler wrote: I want INFO to be logged and stored on the remote host. Therefore I must not filter INFO messages. I don't want to pull INFO messages over the VPN. Ergo, the filtering at Python level does not help in my use case. Or I am missing something. Probably, Your logger should have * a remote host handler * and a VPN handler. You can set filters and log levels separately for each handler. More info here https://docs.python.org/2/library/logging.html#handler-objects https://docs.python.org/2/howto/logging-cookbook.html#logging-to-multiple-destinations Something like (python 2.7) import logging logCfg = { 'remote':( logging.StreamHandler(), logging.Formatter('Remote - %(levelname)s - %(message)s'), logging.INFO, ), 'vpn':( logging.StreamHandler(), logging.Formatter('VPN - %(levelname)s - %(message)s'), logging.ERROR, ), } log = logging.getLogger() log.setLevel(logging.DEBUG) for handler, formatter, level in logCfg.itervalues(): handler.setFormatter(formatter) handler.setLevel(level) log.addHandler(handler) log.info('This is an info') log.error('This is error') and the result: Remote - INFO - This is an info VPN - ERROR - This is error Remote - ERROR - This is error JM -- https://mail.python.org/mailman/listinfo/python-list
Re: From logging to files to a better solution: syslog, Sentry, Logstash, ....
On 09/15/2015 11:35 AM, Thomas Güttler wrote: Yes, I could do it this way. But somehow I am not happy with this solution. I think the filtering should be outside of python. [snip] Can you understand my concerns? Thomas Güttler No, not really. I showed you how it can be done in python using standard logging technics. Others have also provided suggestions and you've also dismissed those with a "I don't feel like it". Remember this is a python mailing list hence if you're looking for help about a non python log infrastructure, you probably find better answers in those said log infrastructures mailing lists. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: True == 1 weirdness
On 09/16/2015 02:16 PM, Blake T. Garretson wrote: 1 in {1:1} == 1 #test2 The second test yield False, because True does not equal 1, apparently. Fair enough. No, it yields False because {1:1} == 1 is false. Test 2 looks actually like (1 in {1:1}) and ({1:1} == 1). Which in your example does not make any sense but think of this one: x = 5 3 < x < 10 JM -- https://mail.python.org/mailman/listinfo/python-list
Re: True == 1 weirdness
On 09/16/2015 02:53 PM, Jussi Piitulainen wrote: But now I expect to see a long thread about whether chained comparisons are a natural thing to have in the language. Nice forecast by the way. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Idiosyncratic python
On 09/24/2015 08:02 AM, Steven D'Aprano wrote: I was looking at an in-house code base today, and the author seems to have a rather idiosyncratic approach to Python. For example: for k, v in mydict.items(): del(k) ... instead of the more obvious for v in mydict.values(): ... What are your favorite not-wrong-just-weird Python moments? A lot of our in base weird python comes from heavily C-wired people: The classic for i in range(len(alist)): print alist[i] with its twin brother i=0 while i < len(alist): print alist[i] i += 1 And the even more annoying result = Result() getResult(result) JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Idiosyncratic python
On 09/24/2015 02:50 PM, paul.hermeneu...@gmail.com wrote: > A lot of our in base weird python comes from heavily C-wired people: > > The classic > for i in range(len(alist)): > print alist[i] > > with its twin brother > > i=0 > while i < len(alist): > print alist[i] > i += 1 > > And the even more annoying > > result = Result() > getResult(result) > > JM Please follow up with good ways to write these. I hear that creating one really good way is a Python maxim. for item in alist: print item and result = getResult() For the later, the original weird form come from a C habit to allocate returned structures within the caller and provide a pointer to it so the function can fill the data in, otherwise the structure is lost as the stack is popped out and the structure content is garbage. None of this make any sense in python. JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Python, convert an integer into an index?
On 09/24/2015 03:45 PM, paul.hermeneu...@gmail.com wrote: >> I'm suprised. Why not just: >> >> list(str(results)) >> >> In other words, is there something else the list constructor should do >> with a string other than convert it to a list? >> > The OP wanted the result to be a list of ints, not a list of strings. [int(x) for x in list(str(results))] Side note : strings are already iterable, the list function is superfluous. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Idiosyncratic python
On 09/24/2015 04:26 PM, Ian Kelly wrote: On Thu, Sep 24, 2015 at 8:07 AM, jmp wrote: result = getResult() For the later, the original weird form come from a C habit to allocate returned structures within the caller and provide a pointer to it so the function can fill the data in, otherwise the structure is lost as the stack is popped out and the structure content is garbage. None of this make any sense in python. Only if the structure is allocated on the stack and returned by pointer. If it's returned by value, then the content remains intact, but at the expense of copying it. The other option of course would be to allocate it on the heap and return the pointer, but this needlessly incurs malloc overhead and creates an opportunity for a memory leak if the variable is naturally stack-scoped. Python effectively takes this option, as everything is allocated on the heap. Leaving it up to the caller to provide a pointer also gives the caller the option of allocating on the stack or the heap as best fits the context. I'm not an expert but I think this "return by value thing" is only for C++. In vintage C, you can only return something that fits within a register. Anyway, there's a lot of legit C code in which functions are plagued by 'out parameters' and somehow it has transpired in some python code for no reason :o) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Question re class variable
On 09/29/2015 11:27 AM, ple...@gmail.com wrote: I have a perplexing problem with Python 3 class variables. Your problem is that when assigning values to your class attribute, you are actually creating a instance attribute. class Foo: bar = "I'm a class attribute" def __init__(self): self.bar = "I'm an instance attribute" def foo(self): print self.bar print Foo.bar # this is how you set a class attribute from an instance Foo.bar = "I am still a class attribute" print Foo.bar Foo.foo() I'm an instance attribute I'm a class attribute I am still a class attribute What can be confusing is that assuming you never use the same name for a class an instance attribute (that would be bad code), you can access your class attribute from the instance: class Foo: bar = "I'm a class attribute" def foo(self): # python will look into the class scope if not found in the instance print self.bar # this is not an assignment so we're fine Foo.foo() I'm an class attribute As side note and unrelated topic, your are using name mangling (attribute starting with __), are you sure you need it ? You need a strong motive to use this feature otherwise you're making things difficult for yourself without any benefit. Finally here's how I'd code your id, to give some idea on alternative ways: class GameObject: @property def id(self): return id(self) #use the builtin id function print GameObject().id Cheers, JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Question re class variable
On 09/29/2015 01:02 PM, jmp wrote: class GameObject: @property def id(self): return id(self) #use the builtin id function print GameObject().id Cheers, JM I should add that until you don't serialize your object you're fine. If you need to serialize it, you may want to look at https://docs.python.org/3/library/uuid.html import uuid class GameObject: def __init__(self): self._id = None @property def id(self): if self._id is None: # make a UUID based on the host ID and current time self._id = uuid.uuid1() return self._id -- https://mail.python.org/mailman/listinfo/python-list
Re: Create a .lua fle from Python
On 09/28/2015 11:41 PM, Ariel Argañaraz wrote: Hi, This is my first post, I would like to know if a library that can help me with this. I want to parse a XML fle with Python and save the data into a Lua table called for example "newTable", then I want to create a "table.lua" fle with the "newTable" write on it. And I want to create a cities_temp.lua file cities_temps ={ ["Buenos Aires"] = 30, ["Seatle"] = 25, } Can anyone give some help? Thanks. -- Ariel Argañaraz Use an xml parser to fetch the data from the xml file and use a template engine to generate the lua code. for instance, using bs4 (beautifulsoup) for xml and jinja2 for the template engine: import bs4 import jinja2 xml = """ BuenosAires 30 Seatle 25 """ lua_template = """ cities_temps ={ {%- for city, temp in cities.iteritems() %} ["{{city}}"] = {{temp}}, {%- endfor %} }""" xmlp = bs4.BeautifulSoup(xml, 'xml') # from xml to python dictionary data = {city.find('name').string:city.find('temperature').string for city in xmlp.findAll('city')} # from python dictionary to lua print jinja2.Template(lua_template).render(cities=data) will yield (python 2.7): cities_temps ={ ["BuenosAires"] = 30, ["Seatle"] = 25, } Cheers, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Create a .lua fle from Python
On 09/29/2015 07:28 PM, Peter Otten wrote: jmp wrote: import bs4 import jinja2 xml = """ BuenosAires 30 Seatle 25 """ lua_template = """ cities_temps ={ {%- for city, temp in cities.iteritems() %} ["{{city}}"] = {{temp}}, {%- endfor %} }""" xmlp = bs4.BeautifulSoup(xml, 'xml') # from xml to python dictionary data = {city.find('name').string:city.find('temperature').string for city in xmlp.findAll('city')} # from python dictionary to lua print jinja2.Template(lua_template).render(cities=data) will yield (python 2.7): cities_temps ={ ["BuenosAires"] = 30, ["Seatle"] = 25, } Is Ariel's xml file user-supplied? If so, how does your suggestion prevent the resulting lua script from executing arbitrary code? It does not. Like it doesn't fulfill the millions of possible requirements the OP could have written but did not. What if the OP want a thread safe, super fast, multi core solution distributed on multiple remote hosts ? jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Create a .lua fle from Python
On 10/01/2015 09:12 PM, Steven D'Aprano wrote: On Wed, 30 Sep 2015 07:21 pm, jmp wrote: Is Ariel's xml file user-supplied? If so, how does your suggestion prevent the resulting lua script from executing arbitrary code? It does not. Like it doesn't fulfill the millions of possible requirements the OP could have written but did not. What if the OP want a thread safe, super fast, multi core solution distributed on multiple remote hosts ? Then he should have said so. We are not *required* to guess every last requirement that somebody might have but didn't mention. But we do have a professional[1] duty of care to warn an *obvious beginner* that he may be introducing a serious security vulnerability into his code. I agree with you and to some extend to Peter's answer, my solution is not safe but note that I didn't mean it to be nor did I claimed it was safe. What I disagree with, is the suggestion that I should provide a safe version of my solution, just in case the OP forgot to mention that he was going public with his application while a simple "beware this solution is not safe" would have sufficed. Safety is like speed optimization, you care about it only when it can be a problem. And the vast majority (there's a recent trolling thread about the equivalent percentage of vast majority if you want to have fun) of python code may run on trusted networks. Meaning it's probable you are wrong when assuming security of a python snippet is a concern. JM Note : becoming public on the internet is not even enough for security to be a concern. Consider the OP's request, someone around the world would need to be willing to hack into the OP's server, guess/find out that the xml is able to execute lua and then attack the server for a reason yet to be known. If the OP's name is google, yeah someone will want to do that. If you're a complete anonymous... -- https://mail.python.org/mailman/listinfo/python-list
Re: teacher need help!
On 10/19/2015 03:42 PM, Storey, Geneva wrote: Our technician will be here this morning to take a look. I'll let you know what we find. Thanks again, Geneva Storey import turtle print turtle.__file__ may help you find the offending file. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: filter a list of strings
On 12/03/2015 10:27 AM, c.bu...@posteo.jp wrote: I often saw constructions like this x for x in y if ... But I don't understand that combination of the Python keywords (for, in, if) I allready know. It is to complex to imagine what there really happen. I understand this for x in y: if ... But what is about the 'x' in front of all that? I'd advise you insist on understanding this construct as it is a very common (and useful) construct in python. It's a list comprehension, you can google it to get some clues about it. consider this example [2*i for i in [0,1,2,3,4] if i%2] == [2,6] you can split it in 3 parts: 1/ for i in [0,1,2,3,4] 2/ if i/2 3/ 2*i 1/ I'm assuming you understand this one 2/ this is the filter part 3/ this is the mapping part, it applies a function to each element To go back to your question "what is about the 'x' in front of all that". The x is the mapping part, but the function applied is the function identity which simply keeps the element as is. # map each element, no filter [2*i for i in [0,1,2,3,4]] == [0, 2, 4, 6, 8] # no mapping, keeping only odd elements [i for i in [0,1,2,3,4] if i%2] == [1,3] JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Try: rather than if :
On 12/14/2015 11:38 PM, Vincent Davis wrote: In the code below try is used to check if handle has the attribute name. It seems an if statement could be used. Is there reason one way would be better than another? def write_header(self): handle = self.handle try: handle.write("# Report_file: %s\n" % handle.name) except AttributeError: pass handle.write("\n") The specific use case I noticed this was https://github.com/biopython/biopython/blob/master/Bio/AlignIO/EmbossIO.py#L38 Vincent Davis Nothing wrong with the try block. However other forms may be shorter/more readable. Since there is 2 attribute lookups in the try block, (write and name) it's not clear which one you want to catch (except for the line following the except clause but it could not be the case). Here are 2 alternative forms 1/ handle.write(handle.name if hasattr(handle, 'name') else '') 2/ handle.write(getattr(handle, 'name', '')) Here's the best solution imo: 3/ assume handle has always a name attribute and don't write code handling attribute existence. Instead handle the attribute values: class Handle: def __init__(self): self.name= None # and the problem is gone if handle.name : handle.write(handle.name) By the way, in the use case you've linked, it is written 'handle = self.handle' at every beginning of each method. Don't follow that rule. Jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Newbie XML problem
On 12/22/2015 05:29 AM, KP wrote: From my first foray into XML with Python: I would like to retrieve this list from the XML upon searching for the 'config' with id attribute = 'B' config = {id: 1, canvas: (3840, 1024), comment: "a comment", {id: 4, gate: 3, (0,0, 1280, 1024)}, {id: 5, gate: 2, (1280, 0, 2560, 1024)}, {id: 6, gate: 1, (2560, 0, 3840, 1024)}} I have started to use this code - but this is beginning to feel very non-elegant; not the cool Python code I usually see... import xml.etree.ElementTree as ET tree = ET.parse('master.xml') master = tree.getroot() for config in master: if config.attrib['id'] == 'B': ... It much depends on 1/ the xml parser you use. 2/ the xml structure 1/ I'm happily using beautifulSoup. Using it is really simple and yield simple code. 2/ Whenever the code gets complicated is because the xml is not properly structured. For instance in you example, 'id' is an attribute of 'config' nodes, that's fine, but for 'panel' nodes it's a child node. There's no point using a node when only one 'id' can be specified. Filtering by attributes is much easier than by child nodes. Anyway here's an example of using beautifulSoup: python 2.7 (fix the print statement if you're using python3) import bs4 xmlp = bs4.BeautifulSoup(open('test.xml', 'r'), 'xml') # print all canvas for cfg in xmlp.findAll('config'): print cfg.canvas.text # find config B panel 6 coordinates xmlp.find('config', id='B').find(lambda node: node.name=='panel' and node.id.text=='6').coordinates.text # if panel id were attributes: xmlp.find('config', id='B').find('panel', id='6').coordinates.text If you can change the layout of the xml file it's better that you do, put every values as attribute whenever you can: comments can span on multiple lines, you probably need a node Properly structured xml will yield proper python code. cheers, JM -- https://mail.python.org/mailman/listinfo/python-list
Writing a stream of bytes
Hi pyple ! I'd like to write a stream of bytes into a file. I'd like to use the struct (instead of bytearray) module because I will have to write more than bytes. let's say I want a file with 4 bytes in that order: 01 02 03 04 None of these work: import struct with open('toto', 'wb') as f: f.write(struct.pack('4B', *[1,2,3,4])) with open('toto', 'wb') as f: f.write(struct.pack('<4B', *[1,2,3,4])) with open('toto', 'wb') as f: f.write(struct.pack('>4B', *[1,2,3,4])) I always end up with the following bytes on file: !hexdump toto 000 0201 0403 Note: the '<' and '>' tells struct to pack for a litle/big endian architecture. The only solution I came up with is with open('toto', 'wb') as f: f.write(struct.pack('>4B', *[2,1,4,3])) But I'd rather not manipulate the stream, as it seems to me that this would be the job of struct. Or maybe I completely overlooked the actual issue ? Cheers, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Writing a stream of bytes
On 01/15/2016 05:08 PM, Tim Chase wrote: [sorry, toddler on my lap clicked before I could type] import struct with open('toto', 'wb') as f: f.write(struct.pack('<4B', *[1,2,3,4])) This one does what you want. The problem resides in your check: I always end up with the following bytes on file: !hexdump toto 000 0201 0403 Hexdump is grouping them. Try the canonical format: !hexdump -C toto 01 02 03 04 || 0004 -tkc Great ! thank you, (and Bartc). jm -- https://mail.python.org/mailman/listinfo/python-list
Re: class attribute
On 01/28/2016 02:15 PM, ast wrote: hello Here is a class from django framework from django.db import models class Article(models.Model): titre = models.CharField(max_length=100) auteur = models.CharField(max_length=42) contenu = models.TextField(null=True) date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date de parution") def __str__(self): return self.titre From a Python point of view, what are titre, auteur, contenu and date ? Are they class attributes, so common to all instance of Article ? It seems so to me. But if i do in a django shell (run with py manage.py shell) Article.titre it doesnt work, AttributeError: type object 'Article' has no attribute 'titre' why ? if I test on a small class class MyClass: i=0 MyClass.i 0 works When we create an object of class Article article = Article(titre="Bonjour", auteur="Maxime") article.contenu = "Les crêpes bretonnes sont trop bonnes !" we use the same names titre, auteur, contenu, which should be instance attribute this time. This is confusing to me thx My guess is that models.Model has a metclass. Without going too much int details, the metaclass may change the class structure when it's created. django is very specific and very database oriented. " article = Article(titre="Bonjour", auteur="Maxime") article.contenu = "Les crêpes bretonnes sont trop bonnes !" " this is probably the wrong way to assign a value to 'contenu'. You should have a look at django help files, from what I remember it's very well documented with a lot of examples. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Changing logging level only for my code?
On 02/08/2016 12:29 PM, egarr...@gmail.com wrote: I am using the "logging" module for my own package, but changing the level from "INFO" to "DEBUG" enables debugging statements from third-party libraries as well. How can I avoid them? Here is the code I am using: import logging logger = logging.getLogger(__name__) log_file = logging.FileHandler("activity.log", mode='w') log_file.setFormatter(logging.Formatter(fmt='%(levelname)s: %(message)s')) logger.addHandler(log_file) logging.basicConfig(level=logging.INFO) # or DEBUG Thank you. Hi, basicConfig will configure the *root* logger, the parent of all loggers. You need to set it to DEBUG, otherwise no DEBUG logs will be displayed. However you can set levels to loggers independently, and you simply need to configure the third party logger to whatever level you want. logging.basicConfig(level=logging.DEBUG) logging.getLogger("yourthirdpartyloggername").setLevel(logging.INFO) JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Suggested datatype for getting latest information from log files
On 02/11/2016 07:07 PM, ltomassm...@gmail.com wrote: I thought a dictionary would be a good idea because of the key restrictions ensuring no duplicates, so the data would always update - However because they are unordered and I need to do some more processing on the data afterwards I'm having trouble. If it's your only concern about using dictionaries, then you may have a look at https://docs.python.org/2/library/collections.html#collections.OrderedDict JM -- https://mail.python.org/mailman/listinfo/python-list
Re: Swiss Ephemeris
On 04/10/2017 07:29 AM, Deborah Swanson wrote: Fully recognizing that most of what you wrote was tongue-in-cheek, I just want to say that regardless of the wonders of modern medicine, it's a pity they learn so little about successful medicines other than their own. In other academic scientific disciplines such as physics and chemistry it's not uncommon to see history of science courses in the curriculum. But not in medicine. I learned what I know about ancient Greek science from a university physics professor, though I doubt he would ever have guessed that one of his students would someday breathe new life into that ancient science by attempting to ressurrect it. The great ancients were no less endowed with intelligence than we are, they simply directed it to different ends. 1/ success of medicine astrology is yet to be demonstrated 2/ history of science is about history, not actual science 3/ Ancients were probably as intelligent as we are, they just lacked a proper education and were filled with many false information I can understand why people would study ancient medicines, I don't when they decide to consider it actual applicable science. It could be harmful in some cases. But since it's done in Python, I guess we can let it slide :o) jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Bigotry and hate speech on the python mailing list
On 04/18/2017 02:48 AM, Ethan Furman wrote: On 04/17/2017 03:23 PM, Ben Finney wrote: So I will continue to treat all those topics equally: peripheral comments on beliefs are just part of respectful human discourse, so long as I'm respectful of the people who may hold such beliefs. So you're okay with respectfully making people of faith feel unwelcome? -- ~Ethan~ I don't like signatures in general because I think it's an unsolicited statement of one's opinion, but whenever I meet one I don't agree with, I don't feel suddenly unwelcome. However I fully support Ben's line of defense, people are worthy of respect, ideas not necessarily. Whenever I'm saying anything stupid about python, I expect this list to react immediately, showing me how wrong I am. And usually this list delivers :) and I never felt disrespected... cheers, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: repeat until keypressed
On 05/29/2017 03:14 PM, Poul Riis wrote: In good old pascal there was this one-liner command: repeat until keypressed Apparently there is no built-in analogue for that in python. I have explored several different possibilities (pyglet, keyboard, curses, ginput (from matplotlib) and others) but not managed to find anything that works the way I want. In the following example I just want to replace 'waitforbuttonpress' with something like 'continueuntilbuttonpress' if such a command exists. It could be a mouseclick or a keystroke from the terminal, for instance 'shift', 'space' or some character. Poul Riis ### The following two lines should be replaced by ### something like "Go on until some key is pressed - then break" if plt.waitforbuttonpress(): break Hello, What about try: for i in range(20): plot1.plot([20*(sin(i/10)+1)],[cos(i/10)],'bo') except KeyboardInterrupt: pass # you could also print a message You'd be using CTRL+C to interrupt the loop. jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Proposed new syntax
On 08/10/2017 04:28 PM, Steve D'Aprano wrote: Every few years, the following syntax comes up for discussion, with some people saying it isn't obvious what it would do, and others disagreeing and saying that it is obvious. So I thought I'd do an informal survey. What would you expect this syntax to return? [x + 1 for x in (0, 1, 2, 999, 3, 4) while x < 5] For comparison, what would you expect this to return? (Without actually trying it, thank you.) [x + 1 for x in (0, 1, 2, 999, 3, 4) if x < 5] How about these? [x + y for x in (0, 1, 2, 999, 3, 4) while x < 5 for y in (100, 200)] [x + y for x in (0, 1, 2, 999, 3, 4) if x < 5 for y in (100, 200)] Thanks for your comments! [1,2,3] [1,2,3,4,5] SyntaxError("Have you tried Perl ?") SyntaxError("Have you tried Perl ?") I really would not want to deal with 3 and 4. jm -- https://mail.python.org/mailman/listinfo/python-list