Class: @property -> .__dict__
Good morning, I wonder if someone could please help me out with the @property function as illustrated in the following example. class te(): def __init__(self): self.a = 23 @property def b(self): return 2 * self.a t = te() In [4]: t.a Out[4]: 23 In [5]: t.b Out[5]: 46 #works asexpected so far, now let's have a look into t.__dict__ In [6]: t.__dict__ Out[6]: {'a': 23} -> b does not show up. Could anyone please explain me why this does not work / how to get b into .__dict__ / hint me to an explanation? Thanks a lot in advance! Cheers, Ulrich -- http://mail.python.org/mailman/listinfo/python-list
Re: Class: @property -> .__dict__
On Dec 16, 10:03 am, Steven D'Aprano wrote: > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > Good morning, > > > I wonder if someone could please help me out with the @property function > > as illustrated in the following example. > > > class te(): > > def __init__(self): > > self.a = 23 > > @property > > def b(self): > > return 2 * self.a > [...] > > Could anyone please explain me why this does not work / how to get b > > into .__dict__ / hint me to an explanation? > > b is a property object. Like any other assignment inside the body of the > class, it is shared across all instances, so you won't find it in the > instance's personal dict. You will find it in the shared class dict. > > t.__dict__['b'] # not found > te.__dict__['b'] # will return the property object > > (By the way: it is the usual convention to start the name of a class with > initial capital, so Te would be a better name.) > > To get something into the instance dict, you need to assign it onto the > instance: > > t.x = 42 # puts 'x':42 into t.__dict__ > > -- > Steven Hi Steven, Thanks a lot for your quick and helpful answer! This would imply that I have to search in the dict of the class and the dict of the instance. - works nicely. I wonder if there is somewhere a "merge of the two" already available. In the meantime, I came across dir() In [7]: dir(t) Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] This seems to return 'a' and 'b', but now crashes @property def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in self.__dict__ if isinstance(getattr(self, attr), numpy.ndarray)] -- http://mail.python.org/mailman/listinfo/python-list
Re: Class: @property -> .__dict__
On Dec 16, 10:11 am, Ulrich wrote: > On Dec 16, 10:03 am, Steven D'Aprano > > > > > > > > > +comp.lang.pyt...@pearwood.info> wrote: > > On Fri, 16 Dec 2011 00:52:11 -0800, Ulrich wrote: > > > Good morning, > > > > I wonder if someone could please help me out with the @property function > > > as illustrated in the following example. > > > > class te(): > > > def __init__(self): > > > self.a = 23 > > > @property > > > def b(self): > > > return 2 * self.a > > [...] > > > Could anyone please explain me why this does not work / how to get b > > > into .__dict__ / hint me to an explanation? > > > b is a property object. Like any other assignment inside the body of the > > class, it is shared across all instances, so you won't find it in the > > instance's personal dict. You will find it in the shared class dict. > > > t.__dict__['b'] # not found > > te.__dict__['b'] # will return the property object > > > (By the way: it is the usual convention to start the name of a class with > > initial capital, so Te would be a better name.) > > > To get something into the instance dict, you need to assign it onto the > > instance: > > > t.x = 42 # puts 'x':42 into t.__dict__ > > > -- > > Steven > > Hi Steven, > > Thanks a lot for your quick and helpful answer! > > This would imply that I have to search in the dict of the class and > the dict of the instance. - works nicely. > > I wonder if there is somewhere a "merge of the two" already available. > > In the meantime, I came across dir() > In [7]: dir(t) > Out[7]: ['__doc__', '__init__', '__module__', 'a', 'b'] > > This seems to return 'a' and 'b', but now crashes > > @property > def attributelist(self): > # find all attributes to the class that are of type numpy > arrays: > return [attr for attr in self.__dict__ if > isinstance(getattr(self, attr), numpy.ndarray)] hi again, I must have hit the send accidently before finishing. This attributelist should return me all attributes of type numpy.ndarry. if I replace it to def attributelist(self): # find all attributes to the class that are of type numpy arrays: return [attr for attr in dir(self) if isinstance(getattr(self, attr), numpy.ndarray)] it crashes going into some kind of endless loop. Do you happen to have any idea? thanks again! cheers, ulrich -- http://mail.python.org/mailman/listinfo/python-list
Re: access interactive namespace from module (shared namespace?)
Thanks for the reply, Of course the suggested solution is working and good, but a bit complicated. The module/function where i need to access the variable value from the interactive shell is burried quite deep and I would nedd to hand the locals() quite often from one module to another. Furthermore it makes the call function slightly more complicated, as the locals()-argunment has to be given every time. I was hoping for something a bit different: If I wanted to access a value b from another module "utest2.py", I would simply need to type in utest.py: import utest2; print 2*utest2.b Isn't there a name for the interactive namespace (like here the utest2), which I can use to access the variable without handing the whole dictionary? Cheers, Ulrich -- http://mail.python.org/mailman/listinfo/python-list
Re: access interactive namespace from module (shared namespace?)
Thanks a lot to all! Apart from obtaining the solution I was searching for, I learned a lot by studying your answers! Cheers, Ulrich -- http://mail.python.org/mailman/listinfo/python-list
Re: Fractal
Am 16.05.2013 02:00, schrieb alex23: My favourite is this one: http://preshing.com/20110926/high-resolution-mandelbrot-in-obfuscated-python Not only is this blog entry an interesting piece of art, there's other interesting things to read there, too. Thanks! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassing from unittest
Am 22.05.2013 17:32, schrieb Charles Smith: I'd like to subclass from unittest.TestCase. I observed something interesting and wonder if anyone can explain what's going on... some subclasses create null tests. I can perhaps guess what's going on, though Terry is right: Your question isn't very helpful and informative. I can create this subclass and the test works: class StdTestCase (unittest.TestCase): blahblah and I can create this subsubclass and the test works: class aaaTestCase (StdTestCase): moreblahblah but if I create this subsubclass (or any where the first letter is capital): class AaaTestCase (StdTestCase): differentblahblah the test completes immediately without any work being done. Well, per PEP 8, classes use CamelCaps, so your naming might break automatic test discovery. Then, there might be another thing that could cause this, and that is that if you have an intermediate class derived from unittest.TestCase, that class on its own will be considered as test case! If this is not what you want but you still want common functionality in a baseclass, create a mixin and then derive from both the mixin and unittest.TestCase for the actual test cases. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: I want to know how to implement concurrent threads in Python
Am 26.05.2013 21:10, schrieb Daniel Gagliardi: I want to know how to implement concurrent threads in Python Have you tried searching the web or maybe looked on docs.python.org? Seriously, show at least some effort before asking here. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a set works, sorting a dictionary fails ?
Am 10.06.2013 10:29, schrieb Νικόλαος Κούρας: for key in sorted( months.values() ): ^^^ ^^ KeyError 1 ??!! All i did was to tell python to sort the dictionary values, which are just integers. ...and which you then proceed to use as key, which is obviously wrong. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a set works, sorting a dictionary fails ?
Am 10.06.2013 10:04, schrieb Νικόλαος Κούρας: months = { 'Ιανουάριος':1, 'Φεβρουάριος':2, 'Μάρτιος':3, 'Απρίλιος':4, 'Μάϊος':5, 'Ιούνιος':6, \ 'Ιούλιος':7, 'Αύγουστος':8, 'Σεπτέμβριος':9, 'Οκτώβριος':10, 'Νοέμβριος':11, 'Δεκέμβριος':12 } for key in sorted( months.keys() ): I'm having trouble ordering a dictionary though. I can't find a problem here. I tried simple dictionaries containing numbers as keys using Python 3.3, and sorting the keys works without any problem there. What exactly is the "trouble" you are having? Be a bit more precise and describe what you saw and, just in case, also what you expected to see. BTW: You have a line continuation there using a backslash. This isn't necessary, since the pair of {} automatically tell Python the target range. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Reply to post 'Tryign to add a valkue to a set'
Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας: >Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε: for key in sorted( months.keys() ): print(''' %s ''' % (months[key], key) ) this in fact works, it sorts the dict by its keys() No, it does not sort the dict. Please slow down, relax and take a look at the documentation of sorted(). You are jumping to conclusions based on flawed expectations and assumptions, which can only yield garbage in the end. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Sorting a set works, sorting a dictionary fails ?
Am 10.06.2013 11:48, schrieb Νικόλαος Κούρας: After many tried this did the job: for key in sorted(months.items(),key=lambda num : num[1]): print(''' %s ''' % (key[1], key[0]) ) This code is still sending a misleading message. What you are referring to as "key" here is in fact a (key, value) tuple. I'd use Fábio's suggestion and use the automatic splitting: for name, idx in sorted(months.items(), key=lambda num : num[1]): print('month #{} is {}'.format(idx, name)) but its really frustrating not being able to: for key in sorted( months.values() ): print(''' %s ''' % (months[key], key) ) Which seemed to be an abivous way to do it. You are composing three things: 1. months.values() - gives you a sequence with the month numbers 2. sorted() - gives you a sorted sequence 3. for-iteration - iterates over a sequence At which point is Python doing anything non-obvious? Also, have you considered reversing the dictionary mapping or creating a second one with the reversed mapping? Or maybe take a look at collections.OrderedDict? names set() was able to order like this why not the dictionary too? Well, why don't you use a set then, if it solves your problem? An in which place does anything behave differently? Sorry to bring you the news, but your expectations are not fulfilled because your assumptions about how things should work are already flawed, I'm afraid. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Reply to post 'Tryign to add a valkue to a set'
Am 10.06.2013 15:37, schrieb Νικόλαος Κούρας: Τη Δευτέρα, 10 Ιουνίου 2013 4:14:33 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε: Am 10.06.2013 12:57, schrieb Νικόλαος Κούρας: Τη Δευτέρα, 10 Ιουνίου 2013 12:40:01 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε: for key in sorted( months.keys() ): print(''' %s ''' % (months[key], key) ) this in fact works, it sorts the dict by its keys() No, it does not sort the dict. Please slow down, relax and take a look at the documentation of sorted(). You are jumping to conclusions based on flawed expectations and assumptions, which can only yield garbage in the end. it doe ssort the dict at least for keys() why not for values() too? Well, because it does not sort the dict, it sorts the sequence that you pass into sorted(). The dictionary that you retrieved from is not modified. Which part of the documentation is unclear to you? Did you even bother reading the docs? for key in sorted( months.keys() ): print(''' %s ''' % (months[key], key) ) this in fact works, it sorts the dict by its keys() was mistaken before but the sorting aint correct because its done alphabetically and not by integer value. Listen: Computers will always do what you tell them to. If you tell them garbage, they will do garbage. If that is not what you want them to do, it's your own fault. That means that you have to precisely(!!!) describe what you want when talking to a computer. The computer will not try to guess what you might have wanted. Now, the above claim, that "it sorts the dict by its keys()" is simply wrong. Instead, it outputs the dictionary's elements sorted by their key. There is a fine distinction between the two. I know what you mean, because I'm a human being and I can copy with your vague description, but the computer doesn't. Good luck, I'm outta here Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Receing a form variable as a list instead of as a string
Am 11.06.2013 12:38, schrieb Νικόλαος Κούρας: File "/home/nikos/public_html/cgi-bin/metrites.py", line 28, in , referer: http://xxxredactedxxx/ page = page.replace( '/home/nikos/public_html/', '' ), referer: http://xxxredactedxxx/ AttributeError: 'list' object has no attribute 'replace', referer: http://xxxredactedxxx but page is a form variable coming from a previous sumbitted form why the error says 'page' is a list? Maybe because it is a list? Try e.g. "print(type(page))" or "print(page)" to gain some insight. How to receive that form variable as a string? For that, you'd have to adjust the code that you received it from. If that's not possible, convert it to a string yourself. But didn't you want a "form variable"? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: A few questiosn about encoding
Am 12.06.2013 13:23, schrieb Νικόλαος Κούρας: So, how many bytes does UTF-8 stored for codepoints > 127 ? What has your research turned up? I personally consider it lazy and respectless to get lots of pointers that you could use for further research and ask for more info before you even followed these links. example for codepoint 256, 1345, 16474 ? Yes, examples exist. Gee, if there only was an information network that you could access and where you could locate information on various programming-related topics somehow. Seriously, someone should invent this thing! But still, even without it, you have all the tools (i.e. Python) in your hand to generate these examples yourself! Check out ord, bin, encode, decode for a start. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing Extensions for Python 3 in C
Am 18.06.2013 12:24, schrieb Aditya Avinash: Hi. This is the last place where I want to ask a question. You are probably not saying what you mean here. The meaning of your sentence is more like "Here is the forum that I dislike more than any other forum, but still I have to ask a question here (even though I don't like you)." :^) I have searched for lots of tutorials and documentation on the web but, > didn't find a decent one to develop extensions for Python 3 using a > custom compiler (mingw32, nvcc). There is even a tutorial here: http://docs.python.org/3/extending/index.html Have you tried that yet? Doing it with a different compiler is something I would save for a second step. Maybe if you described your problems with a bit more detail would help. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte
Am 04.07.2013 10:37, schrieb Νίκος: I just started to have this error without changing nothing Well, undo the nothing that you didn't change. ;) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] Premature end of script headers: metrites.py Why cant it decode the starting byte? what starting byte is that? It's the 0xb6 but it's expecting the starting byte of a UTF-8 sequence. Please do some research on UTF-8, that should clear it up. You could also search for common causes of that error. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte
Am 04.07.2013 12:38, schrieb Νίκος: Στις 4/7/2013 12:50 μμ, ο/η Ulrich Eckhardt έγραψε: Am 04.07.2013 10:37, schrieb Νίκος: Why cant it decode the starting byte? what starting byte is that? It's the 0xb6 but it's expecting the starting byte of a UTF-8 sequence. Please do some research on UTF-8, that should clear it up. You could also search for common causes of that error. So you are also suggesting that what gesthostbyaddr() returns is not utf-8 encoded too? I never said that. And do some research yourself, you were given plenty of hints. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: How to clean up socket connection to printer
Am 09.07.2013 11:39, schrieb loial: I have a socket application that is connecting to a HP printer via port 9100. Occassionally I get a "Connection reset by peer" error which I am trapping and exiting the script with an error message. Strange. Why does the remote terminate the connection? That works Ok, the issue I have is that the next time I run the script I get "Connection refused" from the printer, which > suggests that the printer still thinks the port is is busy, > though nothing is printing. I suspect that in some way my socket > connection has not been closed correctly? I'm assuming you are using TCP. Getting a "connection refused" rather means that there is no server process that is listening on that port. It sounds a bit as if the printer was kind-of rebooting itself, which first resets the existing connection and then, after a rebooting, opens the port again for connections. Question here: 1. Does the printer accept connections again after some time? 2. Does the printer accept connections if you close and re-open the Python interpreter? 3. Is there actually a limit to the number of concurrent connections? In other words, what happens when you try to create a second connection without closing the first? When I get the "Connection rest by peer" error, I attempt to close the port as follows : [...] This is useless, the connection is already closed at that point. Your description suggests that it is a remote problem. I still wouldn't rule out that it is somehow caused by your code though, but without seeing that, it's impossible to tell. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Kivy for Python 3.3
Welcome to Python! Am 11.07.2013 11:09, schrieb fronag...@gmail.com: I'm looking to write a program in Python, (and have in fact written most of it by now,) and am trying to put together a GUI for it. Kivy looks very nice, particularly with the fact that it's supposed to be compatible with most platforms (including Android, which I would like to be able to use my program on in addition to running it on my desktop) with minimal hassle. However, its current iteration is Python 2.7 only, and I've only learned Python 3.3. Last I looked, which was half a year ago, there was some Python 3 porting of Kivy underway, as you found yourself. If I were you, I would get on IRC (I think it was #kivy on irc.freenode.net) and try to contact the people there about the state of the Python 3 port. Just have some patience, there aren't hundreds of people (yet), so getting an answer could take some time. C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot fin d -lmsvcr100 collect2: ld returned 1 exit status error: command 'gcc' failed with exit status 1 'msvcr100' is the C runtime of MS Visual C++, I'm not sure if it is required for building Python modules on MS Windows. Just removing it from the commandline (or makefile) should tell you already. Alternatively, ask The Internet(R), http://bugs.python.org/issue15315 could be an answer. ;) Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Callable or not callable, that is the question!
Hello! I just stumbled over a case where Python (2.7 and 3.3 on MS Windows) fail to detect that an object is a function, using the callable() builtin function. Investigating, I found out that the object was indeed not callable, but in a way that was very unexpected to me: class X: @staticmethod def example(): pass test1 = example test2 = [example,] X.example() # OK X.test1() # OK X.test2[0]() # TypeError: 'staticmethod' object is not callable Bug or feature? Thanks! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Callable or not callable, that is the question!
Am 11.07.2013 16:11, schrieb Peter Otten: Ulrich Eckhardt wrote: Bug or feature? No bug. Missing feature if you come up with a convincing use-case. class Parser: def _handle_bool(input): # ... pass types = {'bool': _handle_bool, 'boolean': _handle_bool,} def parse(self, line): t,s,v = line.partition(':') handler = types[t] return handler(v) I want a utility function that really behaves just like a function. I'd prefer to nest it inside the class that uses it, to make the code easier to understand. Since I don't want the implicit self-binding either, I would use staticmethod to make this clear, too. Since I can live without any of this, it's not a big issue. What is to me a big issue though is the fact that Python behaves unexpectedly and reading Steven's response and the link there, it seems I'm not alone. Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Bitwise Operations
Am 30.07.2013 01:34, schrieb Devyn Collier Johnson: Typing "101 & 010" or "x = (int(101, 2) & int(010, 2))" only gives errors. What errors? Check out Eric Raymond's essay on asking smart questions, it's a real eye-opener! ;) That said, use "0b" as prefix for binary number literals (0b1000 is eight, for example). Cheers! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Python script help
Am 30.07.2013 16:49, schrieb cool1...@gmail.com: Hello, I am looking for a script that will be able to search an online document (by giving the script the URL) and find all the downloadable links in the document and then download them automatically. Well, that's actually pretty simple. Using the URL, download the document. Then, parse it in order to extract embedded URLs and finally download the resulting URLs. If you have specific problems, please provide more info which part exactly you're having problems with, along with what you already tried etc. In short, show some effort yourself. In the meantime, I'd suggest reading a Python tutorial and Eric Raymonds essay on asking smart questions. Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Mocked object returning another Mocked object
Am 13.07.2012 12:09, schrieb Jean-Michel Pichavant: I have an App object with the 'target' attribute. This target is controlling a piece of hardware. The hardware itself holds a software, hence the target object having an 'api' attribute. I hope I make sense. So basically I'd like self.target.api.() to return a Mocked object (Mocking an Api object response). ** Question ** I do I make *all* method calls return a specifik Mock? target = Mock() result = target.api.start() I'm not sure where the "target" here comes in. As I understand it, the goal is to write the "api" object so that you can call any function on it... I'd like result to be a Mock I defined with the 'returnCode' attribute print result.returnCode 1 ...and every function should just return the same return code. Right? But I would like to do it for any method of api (the list is huge, setting each of them is not an option I think) so that in the end, result = target.api.start() result = target.api.stop() result = target.api.reset() result = target.api.loadSw() return all the same Mock object (with 'returnCode') There are two options I could think of: 1. Intercept attribute lookup From the top of my head, the syntax is something like this: class TargetMock(object): def __getattr__(self, name): def default_result(*vargs, **kwargs): return ReturnCode(1) return default_result This just ignores the name and returns a function returning the mock return code. I think you get the idea. 2. Intercept individual lookups class TargetMock(object): def _default(self, *vargs, **kwargs): return ReturnCode(1) start = _default stop = _default reset = _default loadSW = _default Yes, this ignores your claim that the list of functions is too big to add every function individually. I'd just add them on demand when a test accesses a function that isn't there yet. The advantage is that it shows clearly which of the functions are just stubs if you actually implement a few of them differently. If the list functions is really that huge but you have a class (the real driver class) that defines this API, then you could extract this list programmatically. That way, you could also ensure that your mock API doesn't provide functions not supported by the original. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Diagramming code
Am 16.07.2012 03:57, schrieb hamilton: OK then, let me ask, how do you guys learn/understand large projects ? 1. Use the program. This gives you an idea what features are there and a bit how it could be structured. 2. Build the program, to see what is done to get the program running. This should give you an idea what pieces there are and where they are [from]. 3. Read design documentation (which is too often outdated) which should give you an idea what the intention of the project's structure is. 4. Read the code documentation (which is hopefully more up to date). This should give you an idea about responsibilities within the code. 5. Read the code itself. This can also be done while single-stepping through it with a debugger, just to see it run. Of course there are also secondary resources like developers' and users' mailinglists, websites, bugtrackers that provide information and help. Sometimes, drawing a few diagrams from steps 3 and 4 to document relationships between things is helpful. IMHO having a text describing the relationships in prose is superior to that though. In particular a diagram can't describe the rationale for something, you need prose for that. HTH & YMMV Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Encapsulation, inheritance and polymorphism
Welcome! Am 17.07.2012 10:45, schrieb Lipska the Kat: I was expecting (hoping) to see in depth documentation relating to Class construction, extension mechanisms and runtime polymorphism. In addition to this forum for direct help and discussion, two suggestions: Firstly, it could help if you mentioned what programming languages you are fluent in, in order to help traditional misconceptions and to draw parallels. Secondly, http://docs.python.org is the central hub to tutorials and documentation. What I actually get is a confusion of Classes, modules, scripts and whatever else. Due to the very dynamic nature of Python, types (classes), modules and functions are themselves objects that can be manipulated. Is Python truly OO or is it just one way to use the language. Python supports OOP, but it doesn't enforce it. You can use other paradigms, too. I see some documentation relating to classes but nothing on instantiation .. in fact the documentation appears to say that classes > are used in a static way e.g ClassName.method(), and command line > scripting is really outside the scope of other OO languages I have > experienced. I think you are confused. For the documentation, it would help to know which documentation exactly seems to make such claims. For the thing about commandline scripting, I'm afraid you will have to adjust your expectations. BTW: In general, you instantiate a class by just calling the class' name like a function. If e.g. ClassName is a class, ClassName() instantiates this class. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: assertraises behaviour
Am 17.07.2012 11:06, schrieb andrea crotti: import unittest class TestWithRaises(unittest.TestCase): def test_first(self): assert False def test_second(self): print("also called") assert True if __name__ == '__main__': unittest.main() in this case also the second test is run even if the first fails.. The reason for that is that the unit testing framework catches and handles the error. It calls both test functions in some unspecified order and logs the result. Calls to two separate test functions are thereby separated from each other. This is intentionally so, but I think you can also give the unit testing framework a flag that makes it abort after the first error. In no way will the exception escape from the unittest.main() call though, it is all caught and handled inside, also by intention. But that's probably easy because we just need to catch exceptions for every method call, so it's not exactly the same thing.. I don't understand what you want to say here. I also don't understand what your problem in general is. I guess there are some expectations which are not satisfied, but you haven't explained those explicitly yet. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Encapsulation, inheritance and polymorphism
Am 17.07.2012 13:01, schrieb Lipska the Kat: On 17/07/12 10:30, Ulrich Eckhardt wrote: Am 17.07.2012 10:45, schrieb Lipska the Kat: I was expecting (hoping) to see in depth documentation relating to Class construction, extension mechanisms and runtime polymorphism. In addition to this forum for direct help and discussion, two suggestions: Firstly, it could help if you mentioned what programming languages you are fluent in For the past 9 years I have been developing in Java [...] Java is usually called an OOP language, because everything you do there is put into a class. Free functions don't exist, the closest you get is class-static functions (correct me if I'm wrong, I'm not really fluent in that language). In Python, you have the choice to use OOP, but you can also use free functions or mix those. I'm not used to using variables without declaring their type As a C++ programmer (roughly 80%C++, 15%Python, 5%C) I know that feeling. Having types declared in advance just helps by having the compiler check if the passed arguments are correct. Not having this gives both freedom but also bears dangers. what's this obsession with 'correct' indentation of code ??? You'll get used to it and then start loving it. ;) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Encapsulation, inheritance and polymorphism
Am 18.07.2012 11:06, schrieb Lipska the Kat: On 18/07/12 01:46, Andrew Cooper wrote: Take for example a Linux system call handler. The general form looks a little like (substituting C for python style pseudocode) if not (you are permitted to do this): return -EPERM if not (you've given me some valid data): return -EFAULT if not (you've given me some sensible data): return -EINVAL return actually_try_to_do_something_with(data) How would you program this sort of logic with a single return statement? This is very common logic for all routines for which there is even the remotest possibility that some data has come from an untrusted source. Eeek! well if you insist (type bound) someType -EPERM someType -EFAULT sometype -EINVAL someType -EDOSOMETHING //method someType checkSomething(data){ someType result = -EINVAL //or your most likely or 'safest' result if not (you are permitted to do this): result = -EPERM if not (you've given me some valid data): result = -EFAULT if not (you've given me some sensible data): result = -EINVAL else result = -EDSOMETHING return result } //cohesive, encapsulated, reusable and easy to read This is a classic discussion topic, whether single exit (SE) functions should be used or not. There are two things I consider as problematic with them: 1. In the presence of exceptions, every function has at least two possible paths that can be taken out, one returns a value (or None, in Python), the other throws an exception. For that reason, trying to achieve SE is a dangerous illusion. The syscall handler above is C, which doesn't have exceptions like Java, C++ or Python, so it doesn't suffer those two paths. 2. The biggest problem with SE functions is that you often need to skip over lots of code before you finally find out that the fault at the very beginning causes nothing else to happen inside the function before it is finally returned to the caller. A typical symptom is deeply nested if-else structures. Another symptom is result variables that are checked multiple times to skip over effectively the rest of the function, which "unrolls" the nested if-else structures. Yet another symptom is a very fine granularity of microscopic functions, which is effectively a distributed nest of if-else structures. Coming back to Python, this would look like this: if not /you are permitted to do this/: raise NotPermitted("go awai!") if not /you've given me valid data/: raise TypeError("unexpected input") if not /you're given me sensible data/: raise ValueError("invalid input") # do stuff here... If you shoehorn this into an SE function (which you can't do if something in between might throw), then it probably looks like this: error = None if not /you are permitted to do this/: error = NotPermitted("go awai!") elif not /you've given me valid data/: raise TypeError("unexpected input") elif not /you're given me sensible data/: raise ValueError("invalid input") else: # do stuff here... if error: raise error else: return result //later if(checkSomething(data) == EDOSOMETHING){ actually_try_to_do_something_with(data) } else{ //who knows } Interestingly, you suggest to divide the original function into one that verifies some conditions and one that does the actual work. Using an early return is to me like drawing a big red line inside a function by which it can be split into two sections. This makes it IMHO equally clear, even clearer since I don't have to locate and read that other function. My bioware parses this so that if the first part succeeds, the second part can be read independently thereof, which reduces the amount of info to keep in mind at a time. Also, when changing code, I don't have to locate other places where the utility function (checkSomething) is called (Python allows local functions, which can be very(!!) useful). Since the code is inline, I know that only this one function is affected. Yes, this is in direct contrast to the reusability you mentioned. Neither ease of change nor reusability are goals in and of themselves though, so this is not a black-or-white question and a compromise can be good enough. It's a question of taste, experience, phase of the moon, coffeination levels etc. :) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: What's wrong with this code?
There is one model that has helped me much understanding how Python ticks and that is the model of name tags. The code "a = 1" creates an integer with value 1 and attaches a tag with "a" written on it using a small piece of rope. Now, if you attach the tag to a different item, it obviously doesn't change the original integer. Also, you can attach more than one tag to the integer or even none. Further, a tag doesn't even have to be attached to anything (this happens if you use a local variable before assigning to it). This operation of tagging something is done with the "=" operator. Now, coming back to your code... Am 23.07.2012 16:50, schrieb Stone Li: I'm totally confused by this code: Code: a = None b = None c = None d = None This adds the tags "a", "b", "c" and "d" to None. x = [[a,b], [c,d]] "[a, b]" creates a list, containing two anonymous tags (they don't have anything written on them but they are accessible via index) attached to what "a" and "b" are currently attached to [0]. The same happens for "[c, d]". The two lists are then put into another list with a similar mechanism, and that list of lists is then tagged "x". e,f = x[1] This takes the second element of "x" (the [c, d] above) and tags it with "e" and "f". This syntax implicitly unpacks the list so the assignment operator adds the two tags "e" and "f" to the first and second element referenced by that list. Both "e" and "f" finally end up attached to "None". c = 1 d = 2 These two remove the rope attaching "c" and "d" to "None" and instead attach them to the integers "1" and "2". I hope your Python's behaviour makes sense to you now! Uli [0] Note that in almost all cases, when referring to a tag, Python implicitly operates on the object attached to it. One case (the only one?) where it doesn't is the "del" statement. -- http://mail.python.org/mailman/listinfo/python-list
Re: What's wrong with this code?
Am 24.07.2012 10:24, schrieb Chris Angelico: On Tue, Jul 24, 2012 at 5:47 PM, Ulrich Eckhardt wrote: There is one model that has helped me much understanding how Python ticks and that is the model of name tags. The code "a = 1" creates an integer with value 1 and attaches a tag with "a" written on it using a small piece of rope. A double strand of rope, I think. If it were one strand, we'd write "a - 1". :) Two fibers, possibly twisted let's call it string! I really had to think about how to call the thing while avoiding the term string in order not to add to the confusion... (: Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.6 StreamReader.readline()
Am 24.07.2012 17:01, schrieb cpppw...@gmail.com: reader = codecs.getreader(encoding) lines = [] with open(filename, 'rb') as f: lines = reader(f, 'strict').readlines(keepends=False) where encoding == 'utf-16-be' Everything works fine, except that lines[0] is equal to codecs.BOM_UTF16_BE Is this behaviour correct, that the BOM is still present? Yes, assuming the first line only contains that BOM. Technically it's a space character, and why should those be removed? Uli -- http://mail.python.org/mailman/listinfo/python-list
from future import pass_function
Hi! I just had an idea, it occurred to me that the pass statement is pretty similar to the print statement, and similarly to the print() function, there could be a pass() function that does and returns nothing. Example: def pass(): return try: do_something() except: pass() One thing I don't like about this is the syntax class foo(object): pass() What do you think? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Am 25.07.2012 18:05, schrieb Chris Angelico: By comparison, Python 2's print statement is executable. It causes real action to happen at run-time. It makes sense to pass "print" as an argument to something; for instance: def some_generator(): yield blah map(print,some_generator()) Simple way of making the iterator display its yielded result. I cannot imagine any circumstance in which you'd want to map "pass" over everything. I have seen code that just created a list comprehension to iterate over something but was discarding the results. That could be a case for a "do nothing" function. Just having a function that does nothing would be useful in other places, too. In some cases, you want to print() some debug output in other cases you just use pass() to discard the debug output. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Am 26.07.2012 04:38, schrieb Steven D'Aprano: The examples of pass-as-a-function shown by the Original Poster don't give any clue of what advantage there is to make pass a function. Just read the text, it just struck me how similar pass and print are, i.e. that neither actually needs to be a keyword. In some cases, I would rather use "return" to replace "pass" though. It appears that the only reason for this suggested change is that he would rather write "pass()" instead of "pass", possibly because he thinks it looks cool. I have no idea where you got the "cool" from, it is not in my posting. I stated clearly that "I just had an idea", which should signal that I haven't thought about this for any extended period of time. Then I asked "What do you think?" exactly because I wanted to discuss this. No need to get defensive. ;) (Actually, I reckon that what is driving this idea is that the OP is a beginner, and he's got a syntax error a few times from writing "pass()", and so he thought it would be easier to force other people to change tens or hundreds of thousands of Python programs to use "pass()" instead of "pass" than to just learn to stop putting parentheses after it. So, and in order to force people to write parens or break their code I have considered the possibility of importing that feature from __future__ for those people that want it? Seriously, Steven, as much as I like your regular contributions here, this time you had better logged off and taken a walk, because you come across as _very_ arrogant here. But of course I could be wrong. Ulrich, if you are still reading this, if you have good examples for how pass as a function would actually be better, and how it will let you do things in Python that can't easily be done now, I'm very interested to hear them. Who knows, if the idea is good enough, some day it may even happen. No there is nothing that you strictly need a pass() function for. In summary, after reading this thread I have a lot of good arguments against this idea and few arguments supporting the idea. In any case I have many more arguments than those that I came up with myself, which is exactly what I asked for. Thanks to all that took part in this discussion! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Am 26.07.2012 07:20, schrieb Michael Hrivnak: If we want pass(), then why not break() and continue()? And also def() and class()? for(), while(), if(), with(), we can make them all callable objects! Except that they are control statements. They are not objects, they have no type, and they can never be evaluated in an expression. And most importantly, there is no value to be gained by making them objects. pass is not a control statement, it is just a placeholder to make it explicit that there is nothing else to be expected here. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Am 26.07.2012 11:26, schrieb Steven D'Aprano: On Thu, 26 Jul 2012 08:59:30 +0200, Ulrich Eckhardt wrote: Am 26.07.2012 04:38, schrieb Steven D'Aprano: (Actually, I reckon that what is driving this idea is that the OP is a beginner, and he's got a syntax error a few times from writing "pass()", and so he thought it would be easier to force other people to change tens or hundreds of thousands of Python programs to use "pass()" instead of "pass" than to just learn to stop putting parentheses after it. So, and in order to force people to write parens or break their code I have considered the possibility of importing that feature from __future__ for those people that want it? Seriously, Steven, as much as I like your regular contributions here, this time you had better logged off and taken a walk, because you come across as _very_ arrogant here. *shrug* I'm just being honest. As you admitted, you hadn't really given the idea a lot of thought. Your examples didn't show any difference except a pair of parentheses () after the pass. I made two guesses on what motivated your suggestion, based on the information I had in front of me at the time. By the way, you trimmed out my comment where I admit to also having come up with changes to Python without giving any thought to the consequences. My guesses as to your motive for wanting to change "pass" were not based on your thoughts, which are hidden to me, but on the way I used to think. I didn't say "Pass should be a function!" but asked "What do you think?". You are assuming lots of things about my goals and jumping to conclusions like that I'm complaining about the stupid Python syntax, that I'm a frustrated noob, that I want someone to fix that syntax, but that is not the case! I'm engaging in a discussion here exactly in order to test the idea I had. It took me a long time to learn that, for an established language like Python, change is nearly always for the worse, and any change that requires changing existing code better have a very good excuse. ...so what do you do when you have an idea? You think about it on your own, right? I do so, too, but I also engage in discussions with others. See? BTW: I think you missed the implications of this thread's topic and the snide remark about forcing people to change their code, i.e. that no existing code has to change (apart from the Python implementation, of course), even if pass was made a function! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: from future import pass_function
Am 26.07.2012 09:50, schrieb Mark Lawrence: And if we could persuade the BDFL to introduce braces, we could have {() and }() What do you mean "persuade"? Braces work perfectly: def foo(): {} Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting bit fields from an IEEE-784 float
Am 30.07.2012 02:44, schrieb Steven D'Aprano: I wish to extract the bit fields from a Python float, call it x. First I cast the float to 8-bytes: s = struct.pack('=d', x) i = struct.unpack('=q', s)[0] Then I extract the bit fields from the int, e.g. to grab the sign bit: (i & 0x8000) >> 63 Questions: 1) Are there any known implementations or platforms where Python floats are not C doubles? If so, what are they? The struct docs refer to C's double type, so it depends on that type probably. However, regardless of C's double type, the same docs refer to the IEEE form when packed into a byte array. Is it just the representation you are after or some specific behaviour? 2) If the platform byte-order is reversed, do I need to take any special action? I don't think I do, because even though the float is reversed, so will be the bit mask. Is this correct? Yes, the code is fine. If you have doubts, I have a big-endian system at home (Linux/PowerPC) where I could run tests. 3) Any other problems with the way I am doing this? Python docs refer to IEEE-754, not 784? Typo? Uli -- http://mail.python.org/mailman/listinfo/python-list
NameError vs AttributeError
Hi! Using Python 2.7, I stumbled across the fact that 'self.xy' raises an AttributeError if self doesn't have an 'xy' as attribute, but 'xy' will instead raise a NameError. To some extent, these two are very similar, namely that the name 'xy' couldn't be resolved in a certain context, but they don't have a common baseclass. I guess one of the reasons is behind the way that Python handles variable lookup, the plain 'xy' will find local and global names while 'self.xy' will only look into onself. However, this vague idea is far from enough to explain it to someone else. Can someone help me out? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: CRC-checksum failed in gzip
Am 01.08.2012 19:57, schrieb Laszlo Nagy: ## Open file lock = threading.Lock() fin = gzip.open(file_path...) # Now you can share the file object between threads. # and do this inside any thread: ## data needed. block until the file object becomes usable. with lock: data = fin.read() # other threads are blocked while I'm reading ## use your data here, meanwhile other threads can read Technically, that is correct, but IMHO its complete nonsense to share the file object between threads in the first place. If you need the data in two threads, just read the file once and then share the read-only, immutable content. If the file is small or too large to be held in memory at once, just open and read it on demand. This also saves you from having to rewind the file every time you read it. Am I missing something? Uli -- http://mail.python.org/mailman/listinfo/python-list
consistent input() for Python 2 and 3
Hi! I'm trying to write some code that should work with both Python 2 and 3. One of the problems there is that the input() function has different meanings, I just need the raw_input() behaviour of Python 2. My approach is to simply do this: try: # redirect input() to raw_input() like Python 3 input = raw_input except NameError: # no raw input, probably running Python 3 already pass What do you think? Any better alternatives? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling Values
Am 03.08.2012 13:49, schrieb Subhabrata: I am trying to call the values of one function in the > another function in the following way: def func1(): num1=10 num2=20 print "The Second Number is:",num2 return def func2(): num3=num1+num2 num4=num3+num1 print "New Number One is:",num3 print "New Number Two is:",num4 return I am preferring not to use argument passing or using class? You could make those variables global, see the "global" statement in the documentation of the language. However: I don't think that is a good idea and it will make your program more confusing to read than necessary, but go ahead and make that experience yourself. ;) If you want, you can post your code here when done so that others might give you hints how to do things easier and cleaner, like e.g. putting spaces around operators and using four spaces indention (See PEP 8) or dropping the implied return from functions that return nothing. Apart from that, the above code is too short and with too little info what it's supposed to achieve, I can't really give you better advise. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: On-topic: alternate Python implementations
Am 04.08.2012 15:53, schrieb Stefan Behnel: So, if a C++ compiler takes a .c file and compiles it with C language semantics, it doesn't qualify as a C compiler? That implies a rather weird definition of a C compiler, I'd say. I'd say that even a brainfuck compiler compiling a .py file with C language semantics can shamelessly call itself a C compiler. :P If a C++ compiler is given C code, it may or may not produce equivalent executables. In most non-trivial cases it will just barf on the valid C / invalid C++ code and refuse to compile it. In few rare cases, it will compile the code and produce different behaviour at runtime (e.g. for "sizeof 'a'"). Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Threads and sockets
Am 10.08.2012 15:01, schrieb loial: I am writing an application to send data to a printer port(9100) and then recieve PJL responses back on that port. Because of the way PJL works I have to do both in the same process(script). If I understand that right, you are opening a TCP connection, so obviously this must be done in the same process, regardless of what PJL (whatever that exactly is) does. At the moment I do not start to read responses until the data has been sent to the printer. However it seems I am missing some responses from the printer whilst sending the data, so I need to be able to do the 2 things at the same time. Using TCP, that shouldn't happen, so I really wonder what exactly you are doing here. Can I open a port once and then use 2 different threads, one to write to the post and one to read the responses)? Yes, definitely, take a look at the select() function of the select module. This basically looks like this: (r, w, x) = select(...) if r: # read and handle incoming data ... if w: # write pending output data ... if x: # handle connection failure ... If all this is not what you are doing and what you want (which I'm not 100% sure of) then please elaborate a bit what you're doing and what kind of connection you are using. Happy hacking! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Installing Python 3.2.3 on Win 7
Am 16.08.2012 09:17, schrieb Johan van Zyl: I installed Python 3.2.3 successfully on my work laptop (XP) but cannot seem to do it on my home PC (Win7) I click the button to install and the window just disappears o the screen. So how do I in fact install Python 3.2.3 on Win 7? I used some MSI files, IIRC. What exactly did you download and what "button to install" are you talking about? Lastly, just in case, are you running a system with 32 or 64 bits? Uli -- http://mail.python.org/mailman/listinfo/python-list
python-list@python.org
I that Outlook & Co are guilty. That and the fact that few people even think about this. Even today that makes sense, because it provides an exact context. Without that, you wouldn't be able to really understand what exactly a person is referring to. Also, it helps people to structure their thoughts better. If the above paragraph doesn't make sense to you, see it interleaved below for enlightenment. ;) Am 17.08.2012 07:19, schrieb Dennis Lee Bieber: I also tend to blame M$ (Outlook and variants) for this tendency to quote everything and top-post -- Outlook makes it almost impossible to do a trim&interleave response style. I that Outlook & Co are guilty. That and the fact that few people even think about this. Including everything as a trailing quote may be okay in an office environment, where it serves more as a photocopy included with an paper mail response. But anyone "raised" on 2400bps dial-up on a service that charged by the minute (GEnie, Compuserve, et al) rapidly learned to use as a log-in/pull/log-off/read-reply/log-in/send system, and to remove as much $$ quoted text as possible. Even today that makes sense, because it provides an exact context. Without that, you wouldn't be able to really understand what exactly a person is referring to. Also, it helps people to structure their thoughts better. I tend to disagree with the bandwidth argument, which is obsolete. To me, it's more about communication efficiency and it's only one possible way to achieve that. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: set and dict iteration
Am 17.08.2012 03:01, schrieb Paul Rubin: Ian Kelly writes: With regard to key insertion and deletion while iterating over a dict or set, though, there is just no good reason to be doing that (especially as the result is very implementation-specific), and I wouldn't mind a more complete low-level check against it as long as it's not too expensive (which is not clearly the case with the current suggestion at all). One possible approach is to freeze the dictionary against modification while any iterator is open on it. You could keep a count of active iterators in the dict structure, adjusting it whenever an iterator is created or closed/destroyed. What if there is an iterator left over from a loop that was terminated early? That could block access to the sequence even though nothing is /really/ iterating over it. I personally prefer a reliable error, at least when __debug__ is set. Someone suggested a timestamp or a list of active iterators, which both sound reasonable. The two should be O(1) and O(#iterators) in complexity on all mutating operations and O(1) on iteration, so they should be acceptable. With a C implementation it probably boils down to very few cycles (checking a pointer/incrementing an integer). I can't say if this is feasible without compromising performance though, at the very least it requires an additional member in all dicts and iterators. Uli -- http://mail.python.org/mailman/listinfo/python-list
python-list@python.org
Am 21.08.2012 00:49, schrieb Prasad, Ramit: I also tend to blame M$ (Outlook and variants) for this tendency to quote everything and top-post -- Outlook makes it almost impossible to do a trim&interleave response style. I [think] that Outlook & Co are guilty. That and the fact that few people even think about this. Nonsense, I post only from Outlook. You can do it and it is not hard. It is just requires a little effort. A good tool would reduce the effort and guide users, like e.g. giving them a hint if they leave the whole mail they're replying to as copy. Several corporate email solutions (like MS Outlook/Exchange) put very little emphasis on communication efficiency but only on eye-candy features. Their popularity and the resulting influence on people has caused decay in average communication culture, and that is what I blame them for. BTW: You omitted the attribution line for the text you quoted, whom do you blame for that? That said, "Nonsense" is a strong enough word to start a flamewar... not nice. ;^) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: color coding for numbers
Am 21.08.2012 10:38, schrieb namenobodywa...@gmail.com: what is the best way Define "best" before asking such questions. ;) using color/shading on a tkinter canvas as a visualization for a two-dimensional grid of numbers? so far my best idea is to use the same value for R,G and B (fill = '#xyxyxy'), which gives shades of gray. if possible i'd like to have a larger number of visually distinct values. The basic idea behind this is that you first normalize the values to a value between zero and one and then use that to look up an according color in an array. Of course you can also do both in one step or compute the colors in the array on the fly (like you did), but it helps keeping things simple at least for a start, and it also allows testing different approaches separately. If the different number of resulting colors isn't good enough then, it could be that the array is too small (its size determines the maximum number of different colours), that the normalization only uses a small range between zero and one (reducing the effectively used number of colours) or simply that your screen doesn't support that many different colors. > i've seen visualizations that seem to use some kind > of hot-versus-cold color coding. does anybody know how to do this? The colour-coding is just the way that above mentioned array is filled. For the hot/cold coding, you could define a dark blue for low values and a bright red for high values and then simply interpolate the RGB triple for values in between. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: color coding for numbers
Am 21.08.2012 19:07, schrieb DJC: On 21/08/12 12:55, Ulrich Eckhardt wrote: Am 21.08.2012 10:38, schrieb namenobodywa...@gmail.com: what is the best way Define "best" before asking such questions. ;) <http://matplotlib.sourceforge.net/api/colors_api.html?highlight=colors#matplotlib.colors> Sorry, that one must have been unclear. The point was that when asking for a _best_ solution to a problem, the criteria for evaluating a solution must be known. If you don't define them and they are not implicit, there is no possible answer to the question. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.6 and Sqlite3 - Slow
Am 27.08.2012 03:23, schrieb bruceg113...@gmail.com: My program uses Python 2.6 and Sqlite3 and connects to a network database 100 miles away. Wait, isn't SQLite completely file-based? In that case, SQLite accesses a file, which in turn is stored on a remote filesystem. This means that there are other components involved here, namely your OS, the network (bandwidth & latency), the network filesystem and the filesystem on the remote machine. It would help if you told us what you have there. My program reads approx 60 records (4000 bytes) from a Sqlite database in less than a second. Each time the user requests data, my program can continuously read 60 records in less than a second. However, if I access the network drive (e.g. DOS command DIR /S) while my program is running, my program takes 20 seconds to read the same 60 records. If I restart my program, my program once again takes less than a second to read 60 records. Questions here: 1. Is each record 4kB or are all 60 records together 4kB? 2. Does the time for reading double when you double the number of records? Typically you have B + C * N, but it would be interesting to know the bias B and the actual time (and size) of each record. 3. How does the timing change when running dir/s? 4. What if you run two instances of your program? 5. Is the duration is only reset by restarting the program or does it also decrease when the dir/s call has finished? What if you close and reopen the database without terminating the program? My guess is that the concurrent access by another program causes the accesses to become synchronized, while before most of the data is cached. That would cause a complete roundtrip between the two machines for every access, which can easily blow up the timing via the latency. In any case, I would try Python 2.7 in case this is a bug that was already fixed. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: How to program test(expr) ?
Am 29.08.2012 17:04, schrieb Franck Ditter: I use Python 3.2.3 + Idle. Is it possible to program test(e) which takes an expression e and whose execution produces at the toplevel an echo of e and the effects and result of its evaluation ? Yes, the key to this is using a lambda expression. # file foo.py def foo(x) : print('x =',x) return x+1 test(foo(5)) def test(exp): global print print_saved = print print = my_print res = exp() print = print_saved return res test(lambda: foo(5)) The idea is to run the callable expression inside a modified environment, in the sketch above it intercepts the calles to print() using a separate my_print() function. Note that the calling syntax is slightly different than the one you would have wanted, don't know if that is important. Things I'll leave to you: - exception handling - exception forwarding - intercepting other environment accesses - putting all that into a context manager :) Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: class object's attribute is also the instance's attribute?
Am 30.08.2012 12:55, schrieb 陈伟: class A(object): d = 'it is a doc.' t = A() print t.__class__.d print t.d the output is same. You could go even further: print id(t.__class__.d) print id(t.d) which should show you that they are not just equal but identical. so it means class object's attribute is also the instance's attribute.is it right? Yes. This is even useful sometimes: class Point(object): x = 0 y = 0 This will cause every Point to have two attributes x and y that have a default value 0. Note that setting this attribute on an instance does not change the class' attribute, just in that that was what confused you. However, if the attribute references a mutable type (e.g. a list) this can cause problems because the instance (see id() above) is the same and thus modifications affect both the class and all instances. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Beginners question
Am 30.08.2012 13:54, schrieb boltar2003@boltar.world: s = os.stat(".") print s posix.stat_result(st_mode=16877, st_ino=2278764L, st_dev=2053L, st_nlink=2, st_u id=1000, st_gid=100, st_size=4096L, st_atime=1346327745, st_mtime=1346327754, st _ctime=1346327754) What sort of object is posix.stat_result? Use the type() function to find out. I guess that this is a named tuple, which is a tuple where the attributes are not indexed but have a name, see the documentation for the namedtuple() function from the collections library. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Beginners question
Am 30.08.2012 15:27, schrieb Marco Nawijn: On Thursday, August 30, 2012 3:15:03 PM UTC+2, Ulrich Eckhardt wrote: Am 30.08.2012 13:54, schrieb boltar2003@boltar.world: What sort of object is posix.stat_result? [...] I guess that this is a named tuple, which is a tuple where the attributes are not indexed but have a name, see the documentation for the namedtuple() function from the collections library. It is not a namedtuple. Because a namedtuple "is" a tuple and therefore isinstance(s, tuple) would have returned True. from collections import namedtuple Point = namedtuple('Point', 'x y') p = Point(10,2) isinstance(p, tuple) True Hi Marco, I don't find anything wrong with what you say, the output formatting from using a type created by namedtuple would have been slightly different indeed. However, I also don't understand the point you're trying to make, in particular why it matters that a namedtuple type is derived from tuple, other than perhaps that access by name is available in addition to access by index. Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing dll
Am 06.09.2012 17:07, schrieb Helpful person: I am a complete novice to Python. Welcome! I wish to access a dll that has been written to be compatible with C and VB6. I have been told that after running Python I should enter "from ctypes import *" which allows Python to recognize the dll structure. I have placed the dll into my active directory (if that's the correct word, one on my path) for simplification. Using ctypes, you can indeed load DLLs. Take a look at http://docs.python.org, which includes the documentation for the ctypes module but also general documentation and tutorials, which should be a starting point for you. I tried: "import name.dll" but this just gave me an error telling me that there was no such module. Like in VB or C, things are not that easy. You will always have to write some code that informs Python about the names and parameters of the functions in that DLL. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: python2.7 lack necessary bit to build module
Am 13.09.2012 10:47, schrieb 钟驰宇: I'm in ubuntu10.04 and I decide to compile python2.7 from source [...] However when I run my GAE app,it comes out with no module named _ssl and _sqlite3. There are Debian-specific ways to ease this task that should work in Ubuntu, too. First is "apt-get build-dep", which will install all libraries that are needed to build Python as it was built by the distributor. The second is "apt-get source" and more specifically the file debian/rules within the unpacked sources then, which contains the command line that is used to configure the according package. Note that using dpkg-buildpackage you could even build customized Debian packages, in case you want to replace the system Python. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: main and dependent objects
Am 13.09.2012 14:51, schrieb andrea crotti: I am in a situation where I have a class Obj which contains many attributes, and also contains logically another object of class Dependent. This dependent_object, however, also needs to access many fields of the original class, so at the moment we did something like this: [...] I could of course passing all the arguments needed to the constructor of Dependent, but it's a bit tedious.. Jean-Michel already asked a good question, i.e. whether those two classes should be separate at all. I'll ask a similar question: Can't the shared data be put into a third, separate class? That way passing all the needed arguments wouldn't be tedious any more. Also, it makes clear that both outer and inner class depend on common data, but that the inner class doesn't depend on the outer beyond that. Now, just to get at least something Python-specific into this, you could override the __getitem__ of the inner class and transparently look up the item in the outer class if the inner class doesn't have it. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorators not worth the effort
Am 14.09.2012 11:28, schrieb Jean-Michel Pichavant: Decorators are very popular so I kinda already know that the fault is mine. Now to the reason why I have troubles writing them, I don't know. Every time I did use decorators, I spent way too much time writing it (and debugging it). I wrote the following one, used to decorate any function that access an equipment, it raises an exception when the timeout expires. The timeout is adapted to the platform, ASIC of FPGA so people don't need to specify everytime one timeout per platform. In the end it would replace def boot(self, timeout=15): if FPGA: self.sendCmd("bootMe", timeout=timeout*3) else: self.sendCmd("bootMe", timeout=timeout) with @timeout(15) def boot(self, timeout=None): self.sendCmd("bootMe", timeout) I wrote a nice documentation with sphinx to explain this, how to use it, how it can improve code. After spending hours on the decorator + doc, feedback from my colleagues : What the F... !! Quite honestly: I think like your colleagues in this case and that in this case the decorator doesn't improve the code. Instead, I would probably have added a _get_timeout() function that takes care of adjusting the argument passed to the function according to the underlying hardware target. To be less abstract, the particular problem I have with your approach is that I can't even guess what your code means, let alone what parameters it actually takes. If you had written @default_timeout(15) def boot(self, timeout=None): instead, I would have been able to guess. OTOH, then again I would have wondered why you used a decorator to create a default argument when there is builtin support for specifying default arguments for functions. Maybe you could get away with a decorator like this: @adjust_timeout def boot(self, timeout=2.5): The idea is that the decorator modifies the timeout value passed to the function (or maybe just modifies the default value?) according to the underlying hardware. Decorators are very python specific (probably exists in any dynamic language though, I don't know), in some environment where people need to switch from C to python everyday, decorators add python magic that not everyone is familiar with. The same could be said for classes, iterators, significant whitespace, docstrings, lambdas. I think that this was just a bad example but it doesn't prove that decorators are worthless. Decorators are useful tools if they do something to a function, like doing something before or after the actual code, or modifying the context in which the code is called. Just setting a default parameter is possible as you have proved, but it's IMHO not a good use case. A bit more specific to your case, adding a timeout decorator would actually make much more sense if it transparently invoked the actual function in a second thread and the calling thread stops waiting for completion and raises an error after that timeout. This has the distinct advantage that the code doing the actual communication doesn't have any timeout handling code inside. I'm currently doing something similar here though I only monitor a TCP connection that is used for some telnet-style requests. Every function making a request over TCP is decorated with @_check_connection. That decorator does two things: 1. It checks for an existing fatal connection error. 2. It runs the request and filters resulting errors for fatal connection errors. The decorator looks like this: def _check_connection(fn): @functools.wraps(fn) def wrapper(self, *args, **kwargs): # check for sticky connection errors if self._connection_error: raise self._connection_error # run actual function try: return fn(self, *args, **kwargs) catch RequestFailed: # The other side signalled a failure, but # further requests can still succeed. raise catch ConnectionError, e: # The connection is broken beyond repair. # Store sticky connection and forward. self._connection_error = e raise return wrapper I have had other programmers here write such requests and they blindly copied the decorator from existing code. This works because the code inside that converts/formats/parses the inputs and outputs is completely unaware of the connection monitoring. Otherwise, I don't think anyone could explain what this decorator does, but they don't have to understand it either. It just works. I wish you a nice weekend! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Functional way to compare things inside a list
Am 21.09.2012 00:58, schrieb thorso...@lavabit.com: list = [{'1': []}, {'2': []}, {'3': ['4', '5']}] I want to check for a value (e.g. '4'), and get the key of the dictionary that contains that value. Note: 1. list is a built-in type, who's name is rebound above 2. The list above contains dictionaries that all only contain a single key? 3. You have strings containing decimal representations of numbers? > (Yep, this is bizarre.) The data are really stored in a strange way and you might be able to make things clearer by reorganizing them a bit. some_magic(list, '4') => '3' What's the functional way to do it? Functional as in functional programming and an emphasis on lazy evaluation? In that case I'd write a generator that emits the keys where the values contain the requested string. Is it possible to do it with a one-liner? Yep, filter(), lambda and the 'in' operator. Question remains if this is readable. Note that you can use a local function, too, if you just want to reduce the scope/visibility. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: python file API
Am 24.09.2012 23:49, schrieb Dave Angel: And what approach would you use for positioning relative to end-of-file? That's currently done with an optional second > parameter to seek() method. Negative indices. ;) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: data attributes override method attributes?
Am 25.09.2012 16:11, schrieb alex23: On Sep 26, 12:08 am, Peter Otten <__pete...@web.de> wrote: Jayden wrote: In the Python Tutorial, Section 9.4, it is said that "Data attributes override method attributes with the same name." The tutorial is wrong here. That should be "Instance attributes override class attributes with the same name." As methods are usually defined in the class and data attributes are usually set in the instance it will look like data override method attributes. But you can assign attributes on the class, which has the same impact, so the tutorial is correct. You can assign attributes of the class or the instance, and you can assign with functions or data (actually, both functions and data are objects, Python doesn't make a distinction there). The important thing is that lookup first looks in the instance (where data attributes are usually set) before looking in the class (where method attributes are usually set). Observing typical use and deriving a rule from this is misleading though. No, you're right. Please file a bug report athttp://bugs.python.org Didn't you just demonstrate the behaviour you're now saying is a bug? I think he meant a bug in the tutorial, not in the implementation of Python. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: parse an environment file
Am 01.10.2012 02:11, schrieb Jason Friedman: $ crontab -l * * * * * env This produces mail with the following contents: [...] SHELL=/bin/sh ^^^ [...] On the other hand $ env produces about 100 entries, most of which are provided by my .bashrc; bash != sh Instead of running a script in default POSIX shell, you might be able to run it in bash, which will then read your ~/.bashrc (verify that from the docs, I'm not 100% sure). Maybe it is as easy as changing the first line to '#!/bin/bash'. I want my python 3.2.2 script, called via cron, to know what those additional variables are. To be honest, I would reconsider the approach. You could patch the cron invokation, but that still won't fix any other invokations like starting it from a non-bash shell, filemanager, atd etc. You could instead set these variables in a different place that is considered by more applications. I wonder if maybe ~/.profile would be such a place. Alternatively, assuming these environment variables are just for your Python program, you could store these settings in a separate configuration file instead. Environment variables are always a bit like using globals instead of function parameters. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
unit testing class hierarchies
Greetings! I'm trying to unittest a class hierachy using Python 2.7. I have a common baseclass Base and derived classes D1 and D2 that I want to test. The baseclass in not instantiatable on its own. Now, the first approach is to have test cases TestD1 and TestD2, both derived from class TestCase: class TestD1(unittest.TestCase): def test_base(self): ... def test_r(self): ... def test_s(self): ... class TestD2(unittest.TestCase): def test_base(self): # same as above ... def test_x(self): ... def test_y(self): ... As you see, the code for test_base() is redundant, so the idea is to move it to a baseclass: class TestBase(unittest.TestCase): def test_base(self): ... class TestD1(TestBase): def test_r(self): ... def test_s(self): ... class TestD2(TestBase): def test_x(self): ... def test_y(self): ... The problem here is that TestBase is not a complete test case (just as class Base is not complete), but the unittest framework will still try to run it on its own. One way around this is to not derive class TestBase from unittest.TestCase but instead use multiple inheritance in the derived classes [1]. Maybe it's just my personal gut feeling, but I don't like that solution, because it is not obvious that this class actually needs to be combined with a TestCase class in order to function. I would rather tell the unittest framework directly that it's not supposed to consider this intermediate class as a test case, but couldn't find a way to express that clearly. How would you do this? Uli [1] in C++ I would call that a "mixin" -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing class hierarchies
Am 02.10.2012 16:06, schrieb Thomas Bach: On Tue, Oct 02, 2012 at 02:27:11PM +0200, Ulrich Eckhardt wrote: As you see, the code for test_base() is redundant, so the idea is to move it to a baseclass: class TestBase(unittest.TestCase): def test_base(self): ... class TestD1(TestBase): def test_r(self): ... def test_s(self): ... class TestD2(TestBase): def test_x(self): ... def test_y(self): ... Could you provide more background? How do you avoid that test_base() runs in TestD1 or TestD2? Sorry, there's a misunderstanding: I want test_base() to be run as part of both TestD1 and TestD2, because it tests basic functions provided by both class D1 and D2. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: unit testing class hierarchies
Am 02.10.2012 16:06, schrieb Thomas Bach: On Tue, Oct 02, 2012 at 02:27:11PM +0200, Ulrich Eckhardt wrote: As you see, the code for test_base() is redundant, so the idea is to move it to a baseclass: class TestBase(unittest.TestCase): def test_base(self): ... class TestD1(TestBase): def test_r(self): ... def test_s(self): ... class TestD2(TestBase): def test_x(self): ... def test_y(self): ... Could you provide more background? How do you avoid that test_base() runs in TestD1 or TestD2? Sorry, there's a misunderstanding: I want test_base() to be run as part of both TestD1 and TestD2, because it tests basic functions provided by both classes D1 and D2. The instances of D1 and D2 are created in TestD1.setUp and TestD2.setUp and then used by all tests. There is no possible implementation creating such an instance for TestBase, since the baseclass is abstract. Last edit for today, I hope that makes my intentions clear... ;) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: error bluetooth
Am 05.10.2012 10:51, schrieb Luca Sanna: the code is output the error of the ubuntu from bluetooth import * [...] nearby_devices = discover_devices() [...] the error luca@luca-XPS-M1330:~/py-temperature/py-temperature$ python bluetooth.py Traceback (most recent call last): File "bluetooth.py", line 14, in from bluetooth import * File "/home/luca/py-temperature/py-temperature/bluetooth.py", line 19, in nearby_devices = discover_devices() NameError: name 'discover_devices' is not defined The module "bluetooth" doesn't export any function called discover_devices(). You could try "dir(bluetooth)" or "help(bluetooth)" (do that from an interactive prompt) to find out what is in there. I don't know why you expect such a function there, if it is mentioned in the documentation or example code that would be a bug. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: To get the accurate value of 1 - 0.999999999999999 ,how to implement the python algorithm ?
Am 08.10.2012 16:07, schrieb iMath: To get the accurate value of 1 - 0.999 ,how to implement the python algorithm ? Algorithms are generally language-agnostic, so what is your question BTW ,Windows’s calculator get the accurate value ,anyone who knows how to implement it ? You should use a library that handles arbitrary-precision floating point numbers, Python's built-in floating point type corresponds to C's double type and that is typically a IEEE float, which means a limited precision. Just search the web for one. If you really want to do it yourself, you could leverage the fact that Python's integral type has a dynamic size, so that it can represent numbers with more than the typical 32 or 64 bits width. BTW: If this is not a homework question, you should ask much more specifically. My anwers are intentionally vague in order to not spoil you the learning effect. Cheers! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a dictionary
Am 09.10.2012 13:59, schrieb arg...@gmail.com: below is the text file i have How to create Facility as a key and then assign multiple values to it The value part of a dict element can be any kind of object, like e.g. a tuple, namedtuple or even a dict. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: string contains and special characters
Am 09.10.2012 16:02, schrieb loial: I am trying to match a string that containing the "<" and ">" characters, using the string contains function, but it never seems to find the lines containing the string e.g if mystring.contains("") : I can't locate a 'contains' function anywhere, what type is 'mystring'? Do I need to escape the characters...and if so how? Maybe. Please provide some example code that unexpectedly fails. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating C++ code
Am 09.10.2012 18:00, schrieb Jean-Michel Pichavant: I'm trying to generate C++ code from an XML file. I'd like to use a template engine, which imo produce something readable and maintainable. [...] Here's my flow: XML file -> nice python app -> C++ code There is one question that you should answer (or maybe decide?) first: How close is the XML structure to C++ semantically? The syntactic level is obviously very different, as one uses XML as metaformat while the other is C++. The semantic level is rather about the question if there is e.g. a "" that directly translates to a "class foo {" in C++. If that is the case, the SAX API should help you, as it basically invokes callbacks for every XML element encountered while parsing the input stream. In those callbacks, you could then generate the according C++ code in a way that should be readable and maintainable with plain Python or some template engine. You you need to skip back-and-forth over the input, reading the whole XML as DOM tree would probably be a better approach. Still, the processing of input is separate from output generation, so you could at least divide your task before conquering it. Notes: - There is also XSLT which can generate pretty much anything from XML, but it is can't do much more than text replacements triggered by input matching. The more the output differs semantically from the input, the more difficult it becomes to use. Also, XSLT tends to become write-only code, i.e. unreadable. - I think there was a feature in GCC that allows generating XML from C++ input, maybe even the reverse. Maybe you could leverage that? Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: an error in python lib?
Am 10.10.2012 02:32, schrieb Wenhua Zhao: I just noticed that in /usr/lib/python2.7/threading.py class _Condition(_Verbose): ... def _is_owned(self): # Return True if lock is owned by current_thread. # This method is called only if __lock doesn't have # _is_owned(). if self.__lock.acquire(0): self.__lock.release() return False else: return True The return values seem to be wrong. They should be swapped: def _is_owned(self): if self.__lock.acquire(0): self.__lock.release() return True else: return False Or I understood it wrong here? I think you are correct, but there is one thing that I would audit first: The whole code there seems to use integers in places where a boolean would be appropriate, like e.g. the 'blocking' parameter to acquire(). I wouldn't be surprised to find the interpretation of "0 means no error" in some places there, so that a False translates to 0 and then to "OK, I have the lock". Also, assuming an underlying implementation where a nonblocking acquire() could still newly acquire an uncontended lock, that implementation would release the acquired lock and still return "yes I'm holding the lock", which would be dead wrong. It must verify if the lock count is at least 2 after acquiring the lock. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: an error in python lib?
Am 10.10.2012 03:16, schrieb MRAB: On 2012-10-10 01:32, Wenhua Zhao wrote: Hi list, I just noticed that in /usr/lib/python2.7/threading.py class _Condition(_Verbose): ... def _is_owned(self): # Return True if lock is owned by current_thread. # This method is called only if __lock doesn't have # _is_owned(). if self.__lock.acquire(0): self.__lock.release() return False else: return True The return values seem to be wrong. They should be swapped: def _is_owned(self): if self.__lock.acquire(0): self.__lock.release() return True else: return False Or I understood it wrong here? The .acquire method will return True if the attempt to acquire has been successful. This can occur only if it is not currently owned. The comment clearly states "owned by current thread", not "owned by any thread". The latter would also be useless, as that can change concurrently at any time when owned by a different thread, so making decisions on this state is futile. Also, acquire() can also return true when locking recursively, at least that's how I read the sources. I think that this is really a bug, but it doesn't surface often because the built-in lock has its own _is_owned() function which is used instead of this flawed logic. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: an error in python lib?
Am 12.10.2012 00:06, schrieb Wenhua Zhao: On Wed, Oct 10, 2012 at 12:21 PM, Ian Kelly wrote: Can you demonstrate an API bug that is caused by this? A simple demo of this error is: [...] print "in main cv._is_owned: ", cv._is_owned() That is kind of cheating, because as far as I can tell that function is private and not even documented in any way. You can use wait() though, which should always raise a RuntimeError: --->8>8-- import time from threading import Condition, Lock, Thread cv = Condition(Lock()) def do_acquire(): cv.acquire() print "cv acquired in thread" time.sleep(5) cv.release() print "cv released in thread" thread = Thread(target=do_acquire) thread.start() for i in range(10): try: cv.wait() print "in main cv.wait() succeeded" except RuntimeError: print "in main cv.wait() raised RuntimeError" time.sleep(1) --->8>8-- This gives me the following output: in main cv.wait() raised RuntimeErrorcv acquired in thread Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner self.run() File "C:\Python27\lib\threading.py", line 504, in run self.__target(*self.__args, **self.__kwargs) File "ttest.py", line 10, in do_acquire cv.release() error: release unlocked lock Following that, the program hangs. It seems the wait() released the lock that it didn't own, causing the error in do_acquire(). It then hung in wait(), although it should have raised a RuntimeError instead. Uli -- http://mail.python.org/mailman/listinfo/python-list
portable unicode literals
Hi! I need a little nudge in the right direction, as I'm misunderstanding something concerning string literals in Python 2 and 3. In Python 2.7, b'' and '' are byte strings, while u'' is a unicode literal. In Python 3.2, b'' is a byte string and '' is a unicode literal, while u'' is a syntax error. This actually came as a surprise to me, I assumed that using b'' I could portably create a byte string (which is true) and using u'' I could portably create a unicode string (which is not true). This feature would help porting code between both versions. While this is a state I can live with, I wonder what the rationale for this is. !puzzled thanks Uli -- http://mail.python.org/mailman/listinfo/python-list
bad httplib latency due to IPv6 use
Hi! I noticed yesterday that a single HTTP request to localhost takes roughly 1s, regardless of the actually served data, which is way too long. After some digging, I found that the problem lies in socket.create_connection(), which first tries the IPv6 ::1 and only then tries the IPv4 127.0.0.1. The first one times out after 1s, causing the long latency. What I'm wondering is this: 1. The server only serves on IPv4, changing this to IPv6 would probably help. However, I wouldn't consider this a bug, or? 2. I don't even have any IPv6 addresses configured and I'm not using IPv6 in any way, so why does it try those at all? 3. Of course I can optimize the code for IPv4, but then I would be pessimizing IPv6 and vice versa... Any other suggestions? Uli Notes: * Using 127.0.0.1 as host works without the delay. * I'm using Python 2.7 on win7/64bit -- http://mail.python.org/mailman/listinfo/python-list
Re: bad httplib latency due to IPv6 use
Some updates on the issue: The etc/hosts file contains the following lines: # localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost As I understand it, those effectively mean that localhost is not resolved via this hosts file but "within DNS itself", whatever that exactly means. Concerning the question whether ping works, the result is that "ping localhost" works and that it uses the IPv6 (sic!) address. I also tried "ping ::1" and "ping 127.0.0.1" and both work. Weird, as ipconfig doesn't list any IPv6 addresses. Concerning the question whether a firewall blocks and unnecessarily delays connection attempts to ::1, I haven't determined that yet. I'll ask our admins here to verify whether that is the case. Lastly, I tried the same using Python 3.2.3/64bit (the other was actually the 32-bit version), and the same issues are there. In summary, I guess that it's a problem with the IP configuration not one in Python's or my code. Sorry for the noise... Uli -- http://mail.python.org/mailman/listinfo/python-list
better way for ' '.join(args) + '\n'?
Hi! General advise when assembling strings is to not concatenate them repeatedly but instead use string's join() function, because it avoids repeated reallocations and is at least as expressive as any alternative. What I have now is a case where I'm assembling lines of text for driving a program with a commandline interface. In this scenario, I'm currently doing this: args = ['foo', 'bar', 'baz'] line = ' '.join(args) + '\n' So, in other words, I'm avoiding all the unnecessary copying, just to make another copy to append the final newline. The only way around this that I found involves creating an intermediate sequence like ['foo', ' ', 'bar', ' ', 'baz', '\n']. This can be done rather cleanly with a generator: def helper(s): for i in s[:-1]: yield i yield ' ' yield s[-1] yield '\n' line = ''.join(tmp(args)) Efficiency-wise, this is satisfactory. However, readability counts and that is where this version fails and that is the reason why I'm writing this message. So, dear fellow Pythonistas, any ideas to improve the original versions efficiency while preserving its expressiveness? Oh, for all those that are tempted to tell me that this is not my bottleneck unless it's called in a very tight loop, you're right. Indeed, the overhead of the communication channel TCP between the two programs is by far dwarving the few microseconds I could save here. I'm still interested in learning new and better solutions though. Cheers! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding an Object Oriented Program example
Am 29.10.2012 00:30, schrieb goldtech: class Contact: all_contacts = [] def __init__(self, name, email): self.name = name self.email = email Contact.all_contacts.append(self) Okay, a class that automatically registers all instances in a central list. OK, no I do this: c = Contact('aaa','bbb') c = Contact('ccc','ddd') c = Contact('eee','fff') for i in Contact.all_contacts: print i.name + ' ' + i.email aaa bbb ccc ddd eee fff c.name 'eee' So wouldn't be good to add a check that the var (in this case c) does not point to any object before creating an object to keep the list correct? Since you don't use "c", there is no use storing it at all! Note that you don't have to store a reference to an object that you created, just calling "Contact('fou', 'barre')" without assigning to anything is fine. Note that I don't find this example good, in reality I would prefer a factory method (e.g. called "register(name, email)") that makes clear that you are not simply creating an instance. Also, concerning OOP, classes in Python are objects, too. Therefore, this could be decorated with "@classmethod" to allow the use with derived classes. However, I think that's going a bit too far at the moment. Just wanted to mention that there are more features waiting for you to discover. Also all_contacts is a class variable. I think the author is hinting that this would be a good idea for a contact list, But I don't fully see the usage of it. How would each object use a class variable like this? What would be the dot notation? How would an object use a method defined in the class? The point is that when calling "fou.barre(42)", the expression "fou.barre" is evaluated first and then used in a call expression with the parameter 42. Note that you can even evaluate that expression without calling the resulting function, but instead assign its result to a variable. In order to evaluate that expression, Python first looks for an attribute "barre" in the instance and returns that if found. If the instance doesn't have it, it looks in the class via the instances __class__ attribute. At that point, a little case-specific magic happens. If it finds a normal function without "@classmethod" or "@staticmethod" decorator, it returns this function with the first parameter (customary called "self") bound to the instance. If it finds a non-function, that object is returned as-is instead. To sum up, you can use "Contact.all_contacts" or e.g. "c.all_contacts" to refer to the list of contacts. The second syntax also includes "self.all_contacts" when inside a memberfunction, after all the "self" is nothing magic or special. Uli -- http://mail.python.org/mailman/listinfo/python-list
calling one staticmethod from another
Hi! I can call a staticmethod f() of class C like "C.f()" or with an instance like "C().f()". Inside that staticmethod, I have neither the class (at least not the original one) nor do I have an instance, so I can't call a different staticmethod from the same class. The obvious solution is to make this a classmethod instead, with a mostly-unused "cls" parameter. Am I missing something? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: calling one staticmethod from another
Am 30.10.2012 14:47, schrieb Dave Angel: I'd think the obvious solution is to move both the functions outside of the class. I haven't figured out the justification for staticmethod, except for java or C++ converts. Although I come from a C++ background, I think static functions have solid reasons that are not just based on my habits. When I see a static function in C++, I know that it is a function, not a method, so the only context it could interact with is also static (inside a namespace, including the global namespace or statically inside the class) or passed as parameters. Further, the function itself is inside a class (possibly even private), so it should only be of interest in the context of that class or instances thereof and doesn't collide with other functions. In summary, putting utility code into a function reduces the context it interacts with. Putting that utility function as staticmethod inside a class further reduces the context of that function. Together, this also reduces the complexity of the code, making it easier to write and read. But if you like the staticmethod for other reasons, why is it you can't just use C.g() ? This works. It's just that I find it a bit inconvenient/ugly to repeat the classname inside a class. But hey, coming from C++ I have gotten used to always writing "self." to call one member function from another, so I'll probably survive this one, too. ;) Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: calling one staticmethod from another
Am 30.10.2012 18:23, schrieb Jean-Michel Pichavant: - Original Message - [snip] I haven't figured out the justification for staticmethod, http://en.wikipedia.org/wiki/Namespace + "Namespaces are one honking great idea -- let's do more of those!" Someone may successfully use only modules as namespaces, but classes can be used as well. It's up to you. Indeed, see e.g. Steven D'Aprano's approach at formalizing that: http://code.activestate.com/recipes/578279/ Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
Am 02.11.2012 09:08, schrieb Martin Hewitson: On 2, Nov, 2012, at 08:38 AM, Paul Rubin wrote: Martin Hewitson writes: So, is there a way to put these methods in their own files and have them 'included' in the class somehow? ... Is there an official python way to do this? I don't like having source files with 100's of lines of code in, let alone 1000's. That code sounds kind of smelly... why are there so many methods per class? Simply because there are many different ways to process the data. The class encapsulates the data, and the user can process the data in many ways. Of course, one could have classes which encapsulate the algorithms, as well as the data, but it also seems natural to me to have algorithms as methods which are part of the data class, so the user operates on the data using methods of that class. This is largely a matter of taste and a question of circumstances, but I'd like to point out here that your "natural" is not universal. If you take a different approach, namely that a class should encapsulate in order to maintain its internal consistency but otherwise be as small as possible, then algorithms operating on some data are definitely not part of that data. The advantage is that the data class gets smaller, and in the algorithms you don't risk ruining the internal integrity of the used data. Further, encapsulating algorithms into classes is also not natural. Algorithms are often expressed much better as functions. Shoe-horning things into a class in the name of OOP is IMHO misguided. Concerning mixins, you can put them into separate modules[1]. If it is clearly documented that class FooColourMixin handles the colour-related stuff for class Foo, and reversely that class Foo inherits FooShapeMixin and FooColourMixin that provide handling of shape and colour, then that is fine. It allows you to not only encapsulate things inside class Foo but to partition things inside Foo. Note that mixins are easier to write than in C++. If the mixin needs access to the derived class' function bar(), it just calls self.bar(). There is no type-casting or other magic involved. The same applies to data attributes (non-function attributes), basically all attributes are "virtual". The compile-time, static type checking of e.g. C++ doesn't exist. Python lets you inject new methods into existing classes (this is sometimes called duck punching) but I don't recommend doing this. Is there not a way just to declare the method in the class and put the actual implementation in another file on the python path so that it's picked up a run time? To answer your question, no, not directly. Neither is there a separation like in C++ between interface and implementation, nor is there something like in C# with partial classes. C++ interface/implementation separation is roughly provided by abstract base classes. C# partial classes are most closely emulated with mixins. That said, modifying classes is neither magic nor is it uncommon: class foo: pass import algo_x foo.algo = algo_x.function Classes are not immutable, you can add and remove things just like you can do with objects. BTW: If you told us which language(s) you have a background in, it could be easier to help you with identifying the idioms in that language that turn into misconceptions when applied to Python. Greetings! Uli [1] Actually, modules themselves provide the kind of separation that I think you are after. Don't always think "class" if it comes to encapsulation and modularization! -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
Am 02.11.2012 09:20, schrieb Martin Hewitson: Well, here we disagree. Suppose I have a class which encapsulates time-series data. Below is a list of the absolute minimum methods one would have to process that data. [...] > 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' > 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' > 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' > 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' > 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' > 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' > 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' > 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' > 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' > 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' > 'zeropad' Just as a suggestion, you can separate these into categories: 1. Things that modify the data, yielding a different (although derived) data set, e.g. import/load, split, join, plus, minus, zeropad. 2. Things that operate on the data without modifying it, e.g. export/save, average, find, plot, integrate. The latter can easily be removed from the class. Since they don't touch the content, they can't invalidate internals and can't break encapsulation. For the former, providing general means to construct or modify the data (like e.g. adding records or joining sequences) is also all that needs to remain inside the class to ensure internal consistency, everything else can be built on top of these using external functions. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Proper place for everything
Am 02.11.2012 12:20, schrieb Jason Benjamin: Anybody know of the appropriate place to troll and flame about various Python related issues? I'm kind of mad about some Python stuff and I need a place to vent where people may or may not listen, but at at least respond. Thought this would be a strange question, but I might as well start somewhere. Depending on the kind of responses you want I would try alt.comp.zoo.reptiles or maybe a PHP mailinglist. Alternatively, if you are willing to invest some real money, I would suggest a good Islay single malt or a gym membership. If health, money and time are of no importance to you, I heard of these things called girlfriends or boyfriends, these could be completely overhyped rumours though. Uli -- http://mail.python.org/mailman/listinfo/python-list
surprising += for lists
Hi everybody! I was just smacked by some very surprising Python 2.7 behaviour. I was assembling some 2D points into a list: points = [] points += (3, 5) points += (4, 6) What I would have expected is to have [(3, 5), (4, 6)], instead I got [3, 5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable, so the elements are appended one after the other. Actually, I should have used points.append(), but that's a different issue. Now, what really struck me was the fact that [] + (3, 5) will give me a type error. Here I wonder why the augmented assignment behaves so much different. Can anyone help me understand this? Thanks! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: accepting file path or file object?
Am 05.11.2012 11:54, schrieb andrea crotti: Quite often I find convenient to get a filename or a file object as argument of a function, and do something as below: def grep_file(regexp, filepath_obj): """Check if the given text is found in any of the file lines, take a path to a file or an opened file object """ if isinstance(filepath_obj, basestring): fobj = open(filepath_obj) else: fobj = filepath_obj for line in fobj: if re.search(regexp, line): return True return False This makes it also more convenient to unit-test, since I can just pass a StringIO. I do the same for the same reason, but I either pass in a file object or the actual data contained in the file, but not a path. But then there are other problems, for example if I pass a file > object is the caller that has to make sure to close the file handle.. I don't consider that a problem. If you open a file, you should do that in a with expression: with open(..) as f: found = grep_file(regex, f) That is also the biggest criticism I have with your code, because you don't close the file after use. Another things is the readability of your code: grep_file("foo", "bar") The biggest problem there is that I don't know which of the two arguments is which. I personally would expect the file to come first, although the POSIX grep has it opposite on the commandline. Consider as alternative: grep("foo", path="bar") with open(..) as f: grep("foo", file=f) with open(..) as f: grep("foo", data=f.read()) Using **kwargs, you could switch inside the function depending on the mode that was used, extract lines accordingly and match these against the regex. Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
isinstance(.., file) for Python 3
Hi! I have two problems that are related and that I'd like to solve together. Firstly, I have code that allows either a file or a string representing its content as parameter. If the parameter is a file, the content is read from the file. In Python 2, I used "isinstance(p, file)" to determine whether the parameter p is a file. In Python 3, the returnvalue of open() is of type _io.TextIOWrapper, while the built-in class file doesn't exist, so I can't use that code. Secondly, checking for the type is kind-of ugly, because it means that I can't use an object that fits but that doesn't have the right type. In other words, it breaks duck-typing. This is already broken in the Python 2 code, but since I have to touch the code anyway, I might as well fix it on the way. If possible, I'm looking for a solution that works for Pythons 2 and 3, since I'm not fully through the conversion yet and have clients that might use the older snake for some time before shedding their skin. Suggestions? Uli -- http://mail.python.org/mailman/listinfo/python-list
int.__init__ incompatible in Python 3.3
Hi! Preparing for an upgrade from 2.7 to 3, I stumbled across an incompatibility between 2.7 and 3.2 on one hand and 3.3 on the other: class X(int): def __init__(self, value): super(X, self).__init__(value) X(42) On 2.7 and 3.2, the above code works. On 3.3, it gives me a "TypeError: object.__init__() takes no parameters". To some extent, this makes sense to me, because the int subobject is not initialized in __init__ but in __new__. As a workaround, I can simple drop the parameter from the call. However, breaking backward compatibility is another issue, so I wonder if that should be considered as a bug. Bug? Feature? Other suggestions? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: int.__init__ incompatible in Python 3.3
Am 08.11.2012 21:29, schrieb Terry Reedy: On Thu, Nov 8, 2012 at 8:55 AM, Ulrich Eckhardt wrote: On 3.3, it gives me a "TypeError: object.__init__() takes no parameters". To some extent, this makes sense to me, because the int subobject is not initialized in __init__ but in __new__. As a workaround, I can simple drop the parameter from the call. Just drop the do-nothing call. Wait: Which call exactly? Do you suggest that I shouldn't override __init__? The problem is that I need to attach additional info to the int and that I just pass this to the class on contstruction. Or, do you suggest I don't call super().__init__()? That would seem unclean to me. Just for your info, the class mimics a C enumeration, roughly it looks like this: class Foo(int): def __init__(self, value, name): super(Foo, self).__init__(value) self.name = name def __str__(self): return self.name Foo.AVALUE = Foo(1, 'AVALUE') Foo.BVALUE = Foo(2, 'BVALUE') Note that even though I derive from an immutable class, the resulting class is not formally immutable. Maybe exactly that is the thing that the developers did not want me to do? I didn't understand all the implications in the bug ticket you quoted, to be honest. Thank you for your time! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: int.__init__ incompatible in Python 3.3
Am 09.11.2012 12:37, schrieb Steven D'Aprano: On Fri, 09 Nov 2012 08:56:22 +0100, Ulrich Eckhardt wrote: Or, do you suggest I don't call super().__init__()? That would seem unclean to me. On the contrary: calling super().__init__ when the superclass does something you don't want (i.e. raises an exception) is unclean. Since the superclass __init__ does nothing, you don't need to call it. Only inherit behaviour that you actually *want*. That one's hard to swallow for me, but maybe this is because I don't understand the Python object model sufficiently. The problem I have here is that not forwarding the __init__() to the baseclass could mean that necessary initializations are not performed, although in this very specify case I see that there aren't any. It still seems a bit like relying on an implementation details. Anyhow, I'll have to do some more reading on the the construction of objects in Python, maybe then it'll all make sense. Until then, thanks everybody for nudging me in the right direction! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: List comprehension for testing **params
Am 11.11.2012 23:24, schrieb Cantabile: I'm writing a small mail library for my own use, and at the time I'm testing parameters like this: Let's ignore the facts that there is an existing mail library, that you should use real parameters if they are required and that exit() is completely inappropriate. Others explained why sufficiently. [slightly shortened] def function(**params) required = ['Subject', 'From', 'To', 'msg'] for i in required: if not i in params.keys(): print "Error, \'%s\' argument is missing" %i Let's start with the last line: If you use "Error, missing {!r} argument".format(i), you get the quotes automatically, plus possibly escaped unicode characters and newlines, although it's not necessary. Also, I would "from __future__ import print_function" in any new code, to ease upgrading to Python 3. Now, concerning the test whether the required parameters are passed to the function, you can use "if i in params", which is slightly shorter and IMHO similarly expressive. Also, it doesn't repeatedly create a list, checks for a single element inside that list and then throws the list away again. Further, you don't even need a list for the parameters, since order doesn't matter and duplicates shouldn't exist, so I would use a set instead: required = {'Subject', 'From', 'To', 'msg'} The advantage is slightly faster lookup (completely irrelevant for this small number of elements) but also that it makes the intention a bit clearer to people that know what a set is. In a second step, you compute the intersection between the set of required arguments and the set of supplied arguments and verify that all are present: if required.intersection(params.keys()) != required: missing = required - set(params.keys()) raise Exception("missing arguments {}".format( ', '.join(missing))) Happy hacking! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange object identity problem
Am 12.11.2012 14:12, schrieb F.R.: Once in a while I write simple routine stuff and spend the next few hours trying to understand why it doesn't behave as I expect. Here is an example holding me up: [...snip incomplete code...] Trying something similar with a simpler class works as expected: [...snip example code...] Okay, that's almost a classic. You ask about code that fails, while providing code that works as example. Crystal balls are rare nowadays, so this is really hard to answer! In any case, here's what you could do: 1. use a debugger (import pdb...) 2. some more info could be retrieved by outputting the actual type along with the ID of the objects in question (see type() function) 3. reduce the non-working code until you have a minimal example that you can post here I'd bet that at latest while trying approach 3 above, you will find the error yourself. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: int.__init__ incompatible in Python 3.3
Am 09.11.2012 12:37, schrieb Steven D'Aprano: In Python 3.3: py> class X(int): ... def __init__(self, *args): ... super().__init__(*args) # does nothing, call it anyway ... py> x = X(22) Traceback (most recent call last): File "", line 1, in File "", line 3, in __init__ TypeError: object.__init__() takes no parameters It is apparently an oversight, or a bug, that it ever worked in older versions. I'm not really convinced that the overall behaviour is sound: py> x = 42 py> x.__init__() py> x.__init__(1) py> x.__init__(1,2) py> x.__init__(1,2,3) py> x.__init__(1,2,3,4) Neither of these seem to care about the number and type of parameters. On the other hand: py> y = object() py> y.__init__() py> y.__init__(1) Traceback (most recent call last): File "", line 1, in TypeError: object.__init__() takes no parameters So, for some reason that I don't understand yet, my call to the superclass' init function skips a class, but only when called with super(). Confused greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Supported Platforms for Python
Am 14.11.2012 10:51, schrieb Kiran N Mallekoppa: 1. Is this information available somewhere? 2. I was pointed to PEP-11, which documents the platforms that are not supported. So, can we take that all active versions of Python (2.7.3 and 3.3, i believe) are supported on all the OS flavors that Python claims to run on -- unless mentioned otherwise in the PEP-11? There is intent to support these platforms, but as with every software that relies on volunteers, the actual extent varies. If you want to be sure that a platform is actively supported, check that the platform has an available and active build bot, because only this detects bitrot to a certain extent. If you want to be sure, create build and test systems for the systems you target yourself, you will then see if it works. 3. Also, regarding the following entries listed in the PEP-11. So, any idea which OSes implement these? Name: Linux 1(Am guessing its the Linux kernel version 1.0?) Unsupported in: Python 2.3 Code removed in: Python 2.4 Yes, Linux 1 is obsolete and has been for > 10 years. Name: Systems defining __d6_pthread_create (configure.in) Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems defining PY_PTHREAD_D4, PY_PTHREAD_D6, or PY_PTHREAD_D7 in thread_pthread.h Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --with-dl-dld Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --without-universal-newlines, Unsupported in: Python 2.3 Code removed in: Python 2.4 Name: Systems using --with-wctype-functions Unsupported in: Python 2.6 Code removed in: Python 2.6 I'm not sure where these are used. Name: Systems using Mach C Threads Unsupported in: Python 3.2 Code removed in: Python 3.3 Mach is a microkernel. I'm not sure if the Mach C Threads interface is obsolete on Mach or if Mach overall isn't supported. Probably irrelevant for the desktop. Name: Systems using --with-pth (GNU pth threads) Unsupported in: Python 3.2 Code removed in: Python 3.3 I think this is targetted at early Linux threads that used fork() while sharing most of the memory space. Obsolete. Name: Systems using Irix threads Unsupported in: Python 3.2 Code removed in: Python 3.3 Irix was a Unix variant shipped with SGI workstations. I don't kknow to what extent this is relevant for you. I think that the main use cases for these machines is 3D rendering/modelling, unless they have been superseeded by common desktop machines. Kiran M N | Software Development (Rational Team Concert for Visual Studio.NET) Just out of curiosity by one of your RTC users: What nice gimmics are you planning? Cheers! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: debugging in eclipse
Am 15.11.2012 13:29, schrieb chip9m...@gmail.com: I have a python module, lets call it debugTest.py. and it contains: def test(): a=1 b=2 c=a+b c so as simple as possible. Should that be "return c" instead of "c" on a line? Now I would like to debug it in eclipse.. (I have pydev and all) so the question is how do I debug the test function? [...] I place a break point in the function, run the debugger and it stars and is terminated immediately. For a start, I would try to actually call the function. Just add "print(test())" after the function definition. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: editing conf file
Am 16.11.2012 13:06, schrieb chip9munk: I would like to use conf file to get all the variables in my code. And it works great. I use the following (simple example): execfile("example.conf", config) print config["value1"] and it works like a charm. This works, but in general importing configuration data by loading and executing code is a questionable approach. The problem is in particular that the code parser is always more strict with the syntax than a configuration file should be. Also, it presents the danger of code injection, especially when exec'ing or importing untrusted code. That said, if you really want full programmability inside that configuration and are aware of the implications, you can do that. In that case, I would rather call this a Python module though and instead "from settings.py import *" to import any setting from this module (this is similar to exec(), but less hack-ish). I use something similar to import settings for automated tests, but still wouldn't recommend it for general use. If you don't want that, use a configuration file parser instead. Python comes with one, see the section "13.2 Configuration file parser" at http://docs.python.org/2/library/, which can both read and write simple configuration files. I should also mention that I use Python 3.. so some of the solutions I came across are not compatible... No you don't, Above code clearly uses a print statement instead of a print function. :P Anyhow, concerning the link above, replace the 2 with a 3 and skip to section 14.2. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Point of idle curiosity
Am 18.11.2012 12:45, schrieb Chris Angelico: (if you'll forgive the pun) Nevarr! Is IDLE named after Eric of that name, or is it pure coincidence? Maybe. Interestingly, there is also http://eric-ide.python-projects.org/, just to add some more unfounded conspiracy theories to this discussion. :P Uli -- http://mail.python.org/mailman/listinfo/python-list