Re: [error] [client 178.59.111.223] (2)No such file or directory: exec of
Am 28.08.2013 13:55, schrieb Ferrous Cranus: Τη Τετάρτη, 28 Αυγούστου 2013 2:32:44 μ.μ. UTC+3, ο χρήστης Dave Angel έγραψε: You really have no directory in which you have write permissions? If so, perhaps you'd better solve that first. of cours ei ahve write permissions. Here: ni...@superhost.gr [~]# ls -ld www/ drwxr-x--- 4 nikos nobody 4096 Jul 13 10:33 www// ni...@superhost.gr [~]# ls -ld www/cgi-bin/ drwxr-xr-x 2 nikos nikos 4096 Aug 28 10:41 www/cgi-bin// whick make it a mysterya s to why with open(../err.out, a) as f: fails to write the file. ...maybe it's because the server is not running as user nikos? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Basic Python Query
Am 23.08.2013 05:28, schrieb Steven D'Aprano: On Thu, 22 Aug 2013 13:54:14 +0200, Ulrich Eckhardt wrote: When the Python object goes away, it doesn't necessarily affect thethread or file it represents. That's certainly not true with file objects. When the file object goes out of scope, the underlying low-level file is closed. Ahem, yes, but no: Calling close(fd) is not the same as destroying the file, I'm pretty sure it's still on my harddisk after that. That is also the difference to strings, where the Python object really is all there is to it. Similarly you can only customize the Python side of things with derivation, the other side will remain the same, apart from the data you write to the file or the code you run in the thread. Steven, thank you for taking the time to read and consider what I wrote, it is appreciated! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Basic Python Query
Am 21.08.2013 20:58, schrieb Johannes Bauer: On 21.08.2013 11:11, Ulrich Eckhardt wrote: That said, there is never a need for deriving from the Thread class, you can also use it to run a function without that. That way is IMHO clearer because the threading.Thread instance is not the thread, just like a File instance is not a file. Both just represent handles for manipulating the actual thing. Huh? That I find most curious. I *always* derive from threading.Thread and really like the way that thread setup works (instanciate Thread handle, call start). Very intuitive, never had the problems with clarity that you mentioned. Could you elaborate on your suggestion? I don't seem to quite get it I'm afraid. What is clear, convenient or not is largely a matter of taste. I'll try to explain my motivations though, maybe it helps... Firstly, there is one observation: The Python object of type Thread is one thing, the actual thread is another thing. This is similar to the File instance and the actual file. The Python object represents the other thing (thread or file) but it is not this thing. It is rather a handle to the file or thread. This is different for e.g. a string, where the Python object is the string. Due to this pairing between the actual thing and the handle, there is also some arity involved. For a single thread or file, there could be multiple Python objects for handling it, or maybe even none. When the Python object goes away, it doesn't necessarily affect the thread or file it represents. This already casts a doubt on the habit of deriving from the Thread type, just like deriving from the File type is highly unusual, as you are just deriving from a handle class. Secondly, a thread is even less a thing than a file but rather a process or an ongoing operation. As such, it runs code and uses data but it is neither code nor data. Also, it doesn't care which code or data it currently uses. Similarly, the code and data don't care which thread uses them (synchronization problems in multithreaded apps aside). You will find that most of the code called in a thread doesn't use the thread handle, which is another sign that it doesn't care. For that reason, it is unnecessary that self references a Thread object. This reduces coupling, as the same code could be called synchronously and asynchronously. The code shouldn't know or care from which thread it is called. In some cases, I even find it unnecessary to have a self at all, a thread can just as well run a non-member function. Also, even if it runs a memberfunction initially, it doesn't have to eventually. I find that forcing an OOP approach on things is flawed (OOP is a tool and not a goal) and prefer to make this a decision, but that is a different (although slightly related) issue. Thirdly, when you derive a class from Thread, you are exposing this baseclass' interface to the public, too, even if you don't intend to. This has both the unwanted aspect that you expose all public functions of the baseclass and that even if you mean is a thread, it actually means is a handle to a thread, which is even less expressive. Curously, you do that in order to override a single function that is only invoked once. I prefer passing instance.function as callable argument to a plain Thread instance for running this, which keeps the two nicely separate. For example, I have a TCP client class here that uses a service thread to handle the data transfer. The fact that there is a background thread should not be of concern to the user of my TCP client class. If I extended this to use two threads, it would even be impossible to derive from Thread for both of them. In summary, I find that modelling something to use a thread is much clearer than modelling it as is a thread. Greetings from Hamburg! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Basic Python Query
Am 21.08.2013 08:50, schrieb chandan kumar: class Test(threading.Thread): def StartThread(self): Lock = threading.Lock() self.start() Inconsistently indented code, this is a killer for Python. Please read PEP8 and use four spaces! That said, there is never a need for deriving from the Thread class, you can also use it to run a function without that. That way is IMHO clearer because the threading.Thread instance is not the thread, just like a File instance is not a file. Both just represent handles for manipulating the actual thing. Further, you have a local variable called Lock here (should be lowercase, see PEP 8) that you don't use. This is either a bug you missed or at least code that you didn't trim in order to produce a minimal example. class Test1(threading.Thread): def __init__(self): threading.Thread.__init__ ( self ) Check out the super() syntax. 1.Difference between def StartThread(self) and def __init__(self): __init__ is a special function that gets called automatically. Search online for the documentation and or further explanations. 3. Lets say self is passed explicitly for all the methods Like def method1(self) method2() def method2(self): method3() def method(self) method4() def method4(self) What does self holds in method4 ,Is it self argument from method1? Sorry i'm confused with self argument. self is just a name like others, only that it is customarily used for the first parameter of memberfunctions, i.e. for the instance of the according class. That said, above seven lines don't really serve to illustrate anything, because they are far from valid Python code. I think before tackling threading, you should first go through some tutorials and documentation. I'd start with http://docs.python.org and/or do some online searches. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Module for dialoging with intercative programs, sockets, files, etc.
Am 05.08.2013 21:38, schrieb Olive: I have found telnetlib which make very easy to interact with a telnet server, especially the read_until command. I wonder if something similar exits for other things that a telnet server. It's not Python and I haven't played with it extensively, but there is a tool named expect: https://en.wikipedia.org/wiki/Expect Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: outputting time in microseconds or milliseconds
Am 02.08.2013 15:17, schrieb matt.doolittl...@gmail.com: so you are saying that self.logfile.write('%s\t'%(str(time( should be: self.logfile.write('%s\t'%(str(time.time( No, I'm not saying that. What I wanted to make clear is that your code is impossible to understand as it stands, because nobody knows what time refers to. My guess was that you had from time import time, strftime somewhere in your code so that the time in your code refers to the time function from Python's time module. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Python script help
Am 01.08.2013 18:02, schrieb cool1...@gmail.com: I know I should be testing out the script myself but I did, I tried and since I am new in python and I work for a security firm that ask me to scan hundreds of documents a day for unsafe links (by opening them) I thought writing a script will be much easier. I do not know how to combine those three scripts together (the ones I wrote in my previous replay) that is why I cam to here for help. please help me build a working script that will do the job. This first option is to hire a programmer, which should give you the quickest results. If the most important thing is getting the job done, then this should be your #1 approach. Now, if you really want to do it yourself, you will have to do some learning yourself. Start with http://docs.python.org, which includes tutorials, references and a bunch of other links, in particular go through the tutorials. Make sure you pick the documentation corresponding to your Python version though, versions 2 and 3 are subtly different! Then, read http://www.catb.org/esr/faqs/smart-questions.html. This is a a bit metatopical but still important, and while this doesn't make you a programmer in an afternoon, it will help you understand various reactions you received here. hope that gets you started Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: outputting time in microseconds or milliseconds
Am 02.08.2013 12:54, schrieb matt.doolittl...@gmail.com: I am using 2.7 on Ubuntu 12.10. All I need to do is to print time with the microseconds.[...] # write date and time and microseocnds self.logfile.write('%s\t'%(str(strftime(%Y-%m-%d, self.logfile.write('%s\t'%(str(strftime(%H:%M:%S, self.logfile.write('%s\t'%(str(time( the output looks like this: 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 2013-08-02 06:01:43 00:00:00 The output here and the code are surely not the same, provided that time() is the same as Python's time.time(). That function will give you the time as a floating-point number and with as much precision as is available. You then only need the rest when dividing by one to get the fractional seconds. BTW: 1. You have a race condition, you are retrieving the time thrice, which can and should yield different results for each call. Call it once, then format the results in multiple steps if you want. 2. Python's strftime() gives you a string already, calling str() on it is redundant. Also, formatting a string in order to then format it into another string is unnecessary overhead. Further, '%s' will implicitly invoke str() on the argument. Belt and suspenders anyone? :) Good luck! 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: 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: 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: 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: 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: 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: 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: 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 module, 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: 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(''' option value=%s %s /option ''' % (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(''' option value=%s %s /option ''' % (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(''' option value=%s %s /option ''' % (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(''' option value=%s %s /option ''' % (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(''' option value=%s %s /option ''' % (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: 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: 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: 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
percent faster than format()? (was: Re: optomizations)
Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Running simultaneuos FOR loops
Am 23.04.2013 09:13, schrieb inshu chauhan: This statement is giving me the following error Statement: for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(), pixel_count.iterkeys())): Error: Traceback (most recent call last): File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in module access_segments(segimage, data) File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in access_segments for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(), pixel_count.iterkeys())): TypeError: 'dictionary-keyiterator' object is not callable Which of the statements on that line causes the error? I guess asking yourself that question will lead you to the answer already! ;) Any reason you quoted your own and several others' messages, am I missing some reference there? Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: percent faster than format()?
Am 23.04.2013 10:26, schrieb Chris “Kwpolska” Warrick: On Tue, Apr 23, 2013 at 9:46 AM, Ulrich Eckhardt ulrich.eckha...@dominolaser.com wrote: Am 23.04.2013 06:00, schrieb Steven D'Aprano: If it comes down to micro-optimizations to shave a few microseconds off, consider using string % formatting rather than the format method. Why? I don't see any obvious difference between the two... [...] $ python -m timeit a = '{0} {1} {2}'.format(1, 2, 42) 100 loops, best of 3: 0.824 usec per loop $ python -m timeit a = '%s %s %s' % (1, 2, 42) 1000 loops, best of 3: 0.0286 usec per loop Well, I don't question that for at least some CPython implementations one is faster than the other. I don't see a reason why one must be faster than the other though. In other words, I don't understand where the other one needs more time to achieve basically the same. To me, the only difference is the syntax, but not greatly so. So again, why is one faster than the other? What am I missing? Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: name lookup failure using metaclasses with unittests
Am 11.04.2013 10:19, schrieb Steven D'Aprano: if sys.version = '3': Use sys.version_info = (3,), otherwise your code breaks when upgrading to Python 10 and greater. ;^) The second question that came up was if there is a way to keep a metaclass defined inside the class or if the only way is to provide it externally. [...] Not in general, since the metaclass has to exist independently of the class. Thanks for your explanations, they are appreciated. The class is an instance of your metaclass. That means that the metaclass must exist first, so it can be instantiated when you define the class. I don't like the approach to define the code to post-process a class before defining the class. It's a bit like top-posting, it messes up the reading order. Since I really intend to post-process the class, it seems that metaclasses are simply not the right tool. At the moment, this leaves me with two options: 1. post-process the class class X: pass # attach constants to clas X for i in (1, 2, 3): setattr(X, 'f{}' % i, i) 2. generate code inline class Y: pass # generate constants in local (class-)namespace for i in (1, 2, 3): locals()['f{}' % i] = i In both cases, variables (loop variable 'i') are leaked into the surrounding namespace, which is kind-of ugly. The second approach also seems a bit hackish and I can't use the class-in-definition there, which is limiting when you want to attach e.g. constants of type X to X. Also PEP 3115 Metaclasses in Python 3000[2] seems to consider postprocessing of a class definition as better handled by a class decorator, which is something I haven't looked at yet. Generally, class decorators are less brain-melting than metaclasses. Alas, they also need to be defined before the class, messing with the mentioned order of declaration. They can be used to call a class function though which then does the necessary postprocessing... 3. post-process the class triggered with decorator def postprocess_class(cls): invoke postprocess() on the decorated object cls.postprocess() del cls.postprocess return cls @postprocess_class class Z: @classfunction def postprocess(cls): # attach constants to class for i in (1, 2, 3): setattr(cls, 'f{}' % i, i) I guess I'll stay with variant 1 for now, since it requires the least amount of code and the least amount of questions from other developers here. Thanks everybody! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: name lookup failure using metaclasses with unittests
Am 10.04.2013 11:52, schrieb Peter Otten: Ulrich Eckhardt wrote: [...] It looks like this particular invocation relies on class attribute and function __name__ being identical. Please file a bug report. Thanks for confirming this and reducing the test case even more. Now, concerning Python 3, it fails to detect any test case at all! My guess is that the unittest library was changed to use metaclasses itself in order to detect classes derived from unittest.TestCase. Therefore, overriding the metaclass breaks test case discovery. My question in that context is how do I extend metaclasses instead of overriding it? In other words, what is the equivalent to super() for class creation? Python 3 does not recognize the __metaclass__ attribute as the metaclass. You need to provide it like so: def __metaclass__(name, bases, dict): ... class X(unittest.TestCase, metaclass=__metaclass__): pass :| Doing some research[0, 1] on metaclasses in 2 and 3, I have a few more questions... The first thing I was wondering was why Python doesn't complain about a class property that is marked as special (leading and trailing double underscores) but that it knows nothing about. Worse, Python 3 should be aware of its legacy and recognize the Python 2 metaclass syntax, even if only to reject it loudly. I'm pretty sure there is a reason for that, The second question that came up was if there is a way to keep a metaclass defined inside the class or if the only way is to provide it externally. The reason I like this in-class definition is that for my case of autogenerated test functions, everything is in one place which used to be in a loop that modified the class after its creation. Maybe I'm just too brainwashed by static languages though. To get the Python2 feeling back, I have a hack in mind that involves creating a baseclass which in turn provides a metaclass that invokes a specific function to post-initialize the class, similar to the way Python 2 does it automatically, but I'm wondering if there isn't anything better. Also PEP 3115 Metaclasses in Python 3000[2] seems to consider postprocessing of a class definition as better handled by a class decorator, which is something I haven't looked at yet. Greetings from Hamburg! Uli [0] http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/ [1] http://www.artima.com/weblogs/viewpost.jsp?thread=236234 [2] http://www.python.org/dev/peps/pep-3115/ -- http://mail.python.org/mailman/listinfo/python-list
Re: name lookup failure using metaclasses with unittests
Am 10.04.2013 11:52, schrieb Peter Otten: It looks like this particular invocation relies on class attribute and function __name__ being identical. Please file a bug report. http://bugs.python.org/issue17696 Uli -- http://mail.python.org/mailman/listinfo/python-list
[issue17696] lookup fails for renamed functions
New submission from Ulrich Eckhardt: When you rename a test function, you can't explicitly specify it on the commandline any more. During normal test runs, it is automatically discovered though. The error is that the old name was not found, even though the new name was specified. The attached example changes the name attached to the function (its __name__ attribute) for demonstration. The same problem occurs if you auto-generate test functions and attach them to the class, using post-processing or a metaclass. The cases all have in common that the name in the class' dict is not the same as the function's __name__, so cls.foo.__name__ is not foo. See http://mail.python.org/pipermail/python-list/2013-April/644863.html for the initial discussion on the mailinglist. While I only tested Python 2.7 there, it also fails for 3.2 and 3.3. -- components: Tests files: name_mod.py messages: 186549 nosy: eckhardt priority: normal severity: normal status: open title: lookup fails for renamed functions type: behavior versions: Python 2.7, Python 3.2, Python 3.3 Added file: http://bugs.python.org/file29775/name_mod.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue17696 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
name lookup failure using metaclasses with unittests
Hi! I'm having problems using a metaclass to generate test functions. This works when I try to run all tests from the module or test case, but it fails when I'm trying to specify a single test function. My environment is Python 2.7.3 on MS Windows 7 at the moment. It should be upgraded to at least 2.7.4 or better to 3, but see the notes on Python 3 below. # my_module.py import unittest class X(unittest.TestCase): def __metaclass__(name, bases, dict): # attach function def test(self): pass dict['test_1'] = test dict['test_2'] = test # create class return type(name, bases, dict) The error when I'm trying to run python -m unittest my_module.X.test_1 is: Value error: no such test method in class 'my_module.X': test. The astonishing part is that it claims that test is not found while I asked it to run test_1. The name it complains about is the name of the function inside the metaclass function. In all other cases, like e.g. giving -v it reports the correct function name. My question here is whether I'm doing something wrong or whether I discovered a bug. Now, concerning Python 3, it fails to detect any test case at all! My guess is that the unittest library was changed to use metaclasses itself in order to detect classes derived from unittest.TestCase. Therefore, overriding the metaclass breaks test case discovery. My question in that context is how do I extend metaclasses instead of overriding it? In other words, what is the equivalent to super() for class creation? Thank you for your help! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku
Am 27.03.2013 06:44, schrieb Eric Parry: I downloaded the following program from somewhere using a link from Wikipedia and inserted the “most difficult Sudoku puzzle ever” string into it and ran it. It worked fine and solved the puzzle in about 4 seconds. However I cannot understand how it works. In simple terms, it is using a depth-first search and backtracking. If you really want to understand this, get a book on algorithms and graphs (or locate an online source). I can try to give you an idea though. It seems to go backwards and forwards at random. Can anyone explain how it works in simple terms? I think your interpretation of what it does is wrong or at least flawed. It does try different combinations, but some don't lead to a solution. In that case, it goes back to a previous solution and tries the next one. I'll try to document the program to make it easier to understand... def same_row(i,j): return (i/9 == j/9) def same_col(i,j): return (i-j) % 9 == 0 def same_block(i,j): return (i/27 == j/27 and i%9/3 == j%9/3) def r(a): # find an empty cell # If no empty cells are found, we have a solution that we print # and then terminate. i = a.find('0') if i == -1: print a exit(a) # find excluded numbers # Some numbers are blocked because they are already used in # the current column, row or block. This means they can't # possibly be used for the current empty cell. excluded_numbers = set() for j in range(81): if same_row(i,j) or same_col(i,j) or same_block(i,j): excluded_numbers.add(a[j]) # create possible solutions # Try all possibly numbers for the current empty cell in turn. # With the resulting modifications to the sodoku, use # recursion to find a full solution. for m in '123456789': if m not in excluded_numbers: # At this point, m is not excluded by any row, column, or block, so let's place it and recurse r(a[:i]+m+a[i+1:]) # no solution found # If we come here, there was no solution for the input data. # We return to the caller (should be the recursion above), # which will try a different solution instead. return Note: * The program is not ideal. It makes sense to find the cell with the least amount of possible numbers you could fill in, i.e. the most restricted cell. This is called pruning and should be explained in any good book, too. * The style is a bit confusing. Instead of the excluded numbers, use a set with the possible numbers (starting with 1-9) and then remove those that are excluded. Then, iterate over the remaining elements with for m in possible_numbers. This double negation and also using exit() in the middle isn't really nice. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Triple nested loop python (While loop insde of for loop inside of while loop)
Am 01.03.2013 17:28, schrieb Isaac Won: What I really want to get from this code is m1 as I told. For this purpose, for instance, values of fpsd upto second loop and that from third loop should be same, but they are not. Actually it is my main question. You are not helping yourself... In any case, please drop everything not necessary to demostrate the problem before posting. This makes it easier to see what is going wrong both for you and others. Also make sure that others can actually run the code. Read this carefully, I didn't write that to fill up empty space. Also, read Eric S. Raymond's essay on asking smart questions (you can easily locate it online), which the problems with your question in a much more general way. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Triple nested loop python (While loop insde of for loop inside of while loop)
Am 01.03.2013 09:59, schrieb Isaac Won: try to make my triple nested loop working. My code would be: c = 4 [...] while c 24: c = c + 1 This is bad style and you shouldn't do that in python. The question that comes up for me is whether something else is modifying c in that loop, but I think the answer is no. For that reason, use Python's way: for c in range(5, 25): ... That way it is also clear that the first value in the loop is 5, while the initial c = 4 seems to suggest something different. Also, the last value is 24, not 23. while d 335: d = d + 1 y = fpsd[d] y1 = y1 + [y] m = np.mean(y1) m1 = m1 + [m] Apart from the wrong indention (don't mix tabs and spaces, see PEP 8!) and the that d in range(336) is better style, you don't start with an empty y1, except on the first iteration of the outer loop. I'm not really sure if that answers your problem. In any case, please drop everything not necessary to demostrate the problem before posting. This makes it easier to see what is going wrong both for you and others. Also make sure that others can actually run the code. Greetings from Hamburg! Uli -- http://mail.python.org/mailman/listinfo/python-list
[issue4331] Can't use _functools.partial() created function as method
Ulrich Eckhardt added the comment: There is at least one thing that is missing in the patch, it lacks the necessary tests. The partialbug.py demonstrates the issue, it could be used as a base. However, even then, there is still one thing that is problematic: The fact that partial() returns something that behaves like a static method is documented and changing that is not backward compatible. I still think that something like this should become part of Python though. Jack Diederich argues that you can use lambda to achieve the same, but that is not always true. If you want to bind an argument to the current value of a variable instead of a constant, lambda fails. You need the closure created by a function call to bind those variables inside a local function. Having a dedicated function for that is IMHO preferable to people copying the Python-only equivalent of partial() to achieve the same effect or even inventing their own. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4331 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue11470] Flag inappropriate uses of callable class attributes
Changes by Ulrich Eckhardt ulrich.eckha...@dominolaser.com: -- nosy: +eckhardt ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue11470 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Awsome Python - chained exceptions
Am 15.02.2013 08:51, schrieb Rick Johnson: How could a line in the try block ever be considered offensive? My suggestion of offensive does not imply ignorance on /my/ part[...] Well, it seems to imply that you are not aware of the subtle difference between offending and offensive. The irony on that was probably lost in my last posting, since you are still repeating this mistake. Now, concerning the rest, you are relying on too many implications that others should draw from what you wrote that are not clear. This doesn't help you getting across what you want to say. Further, you wrote Which (by showing the offensive line) is quite clear to me., i.e. that there can be offensive lines, then you go on to /i/ never suggested that ANY line in ANY block was offensive. Those two statements just don't fit together, see for yourself which of them you want to clarify or not, but please stop blaming others for your slips! You're welcome. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Awsome Python - chained exceptions
Am 13.02.2013 um 17:14 schrieb Rick Johnson: Q1: How could a line in the try block ever be considered offensive? Because it throws an error? try: rrick.go_and_fuck_yourself() finally: rrick.get_lost() See, wasn't that difficult, was it? :D Are you serious? No, I just couldn't resist this invitation even though I'm making a fool of myself responding to flamers/trolls... *le sigh* Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: which situations should we use thread. join() ?
Am 08.02.2013 07:29, schrieb Chris Angelico: On Fri, Feb 8, 2013 at 3:32 PM, iMath redstone-c...@163.com wrote: which situations should we use thread. join() ? http://bpaste.net/show/yBDGfrlU7BDDpvEZEHmo/ why do we not put thread. join() in this code ? I've no idea why you don't put thread.join() in that code. Maybe because it isn't needed, maybe because someone likes to live on the edge, maybe it's not so much the edge as positively cloud cuckoo land. When should you use it? When you want to accomplish what the function does, the details of which can be found in the Fine Manual. Actually, you probably know already what it does, or you wouldn't even be asking. It isn't needed. I personally would prefer an explicit join(), but according to the documentation, The entire Python program exits when no alive non-daemon threads are left.. In other words, the initial thread is not special and the interpreter will implicitly join() all non-daemon threads. Which again makes me want to find out in what thread's context the atexit call is made... Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python programming language?
Am 08.02.2013 14:03, schrieb gmspro: One said, Python is not programming language, rather scripting language, is that true? That depends on your definition of scripting language and programming language. Python's not a language but an animal. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: puzzled by name binding in local function
Heureka! Am 06.02.2013 15:37, schrieb Dave Angel: def myfunc2(i): def myfunc2b(): print (myfunc2 is using, i) return myfunc2b Earlier you wrote: There is only one instance of i, so it's not clear what you expect. Since it's not an argument to test(), it has to be found in the closure to the function. In this case, that's the global namespace. So each time the function is called, it fetches that global. Actually, the important part missing in my understanding was the full meaning of closure and how it works in Python. After failing to understand how the pure Python version of functools.partial worked, I started a little search and found e.g. closures-in-python[1], which was a key element to understanding the whole picture. Summary: The reason the above or the pure Python version work is that they use the closure created by a function call to bind the values in. My version used a loop instead, but the loop body does not create a closure, so the effective closure is the surrounding global namespace. :) Uli [1] http://ynniv.com/blog/2007/08/closures-in-python.html -- http://mail.python.org/mailman/listinfo/python-list
[issue4331] Can't use _functools.partial() created function as method
Ulrich Eckhardt added the comment: Just for the record, the behaviour is documented, unfortunately in the very last line of the functools documentation: Also, partial objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up. Concerning how exactly they should behave during that lookup, I'd use the least surprising variant, namely that they are not treated differently from other functions: The first parameter is implicitly self. -- nosy: +eckhardt ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue4331 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: puzzled by name binding in local function
Dave and Terry, Thanks you both for your explanations! I really appreciate the time you took. Am 05.02.2013 19:07, schrieb Dave Angel: If you need to have separate function objects that already know a value for i, you need to somehow bind the value into the function object. One way to do it, as you say, is with default parameters. A function's default parameters are each stored in the object, because they're defined to be evaluated only once. That's sometimes considered a flaw, such as when they're volatile, and subsequent calls to the function use the same value. But in your case, it's a feature, as it provides a standard place to store values as known at function definition time. Yes, that was also the first way I found myself. The reason I consider this non-obvious is that it creates a function with two parameters (one with a default) while I only want one with a single parameter. This is to some extent a bioware problem and/or a matter of taste, both for me and for the other audience that I'm writing the code for. The other way to do it is with functions.partial(). I can't readily write you sample code, as I haven't messed with it in the case of class methods, but partial is generally a way to bind one or more values into the actual object. I also think it's clearer than the default parameter approach. Partial would be clearer, since it explicitly binds the parameters: import functools class Foo(object): def function(self, param): print('function({}, {})'.format(self, param)) Foo.test = functools.partial(Foo.function, param=1) f = Foo() Foo.test(f) # works f.test() # fails I guess that Python sees Foo.test and since it is not a (nonstatic) function, it doesn't create a bound method from this. Quoting the very last sentence in the documentation: Also, partial objects defined in classes behave like static methods and do not transform into bound methods during instance attribute look-up. The plain-Python version mentioned in the functools documentation does the job though, so I'll just use that with a fat comment. Also, after some digging, I found http://bugs.python.org/issue4331, which describes this issue. There is a comment from Jack Diederich from 2010-02-23 where he says that using lambda or a function achieves the same, but I think that this case shows that this is not the case. I'm also thinking about throwing another aspect in there: Unless you're using exec(), there is no way to put any variables as constants into the function, i.e. to enforce early binding instead of the default late binding. Using default parameters or functools.partial are both just workarounds with limited applicability. Also, binding the parameters now instead of later would reduce size and offer a speedup, so it could be a worthwhile optimization. The main place where I see this type of problem is in a gui, where you're defining a callback to be used by a series of widgets, but you have a value that IS different for each item in the series. You write a loop much like you did, and discover that the last loop value is the only one used. The two cures above work, and you can also use lambda creatively. Careful, lambda does not work, at least not easily! The problem is that lambda only creates a local, anonymous function, but any names used inside this function will only be evaluated when the function is called, so I'm back at step 1, just with even less obvious code. Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Opinion on best practice...
Am 05.02.2013 05:14, schrieb Anthony Correia: I need to pick up a language that would cover the Linux platform. I use Powershell for a scripting language on the Windows side of things. Very simple copy files script. Is this the best way to do it? import os objdir = (C:\\temp2) Drop the parens here. colDir = os.listdir(objdir) for f in colDir: activefile = os.path.join(objdir + \\ + f) The point of os.path.join is exactly that you don't have to spell out the system-specific file separator. In this case, it gets called with a single string, which it return as-is. print (Removing + activefile + from + objdir) Instead of using + to concat strings, use the format() functionn: removing {} from {}.format(activefile, objdir) Also, if you are using Python 2, print is a statement, not a function, so you could drop the parens here, too. I would not recommend that though! Instead, from __future__ import print_function and keep using print() as a function, just like in Python 3. In general, I would not have stored the result of os.listdir() in a separate variable but iterated over it directly. For large dirs, it could also be better to use os.path.walk(), because that doesn't first build a list and then iterate over the list but iterates over the single elements directly. This avoids the memory allocation overhead, although it is unlikely to make a difference in this case and a Premature Optimization(tm). Welcome to Python! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: autoflush on/off
Am 05.02.2013 01:09, schrieb Jabba Laci: I like the context manager idea There is a helper library for constructing context managers, see http://docs.python.org/2/library/contextlib.html. That would have made your code even shorter. setting the sys.stdout back to the original value doesn't work. [...] The problem is in __exit__ when sys.stdout is pointed to the old value. sys.stdout.write doesn't work from then on. Output: .close failed in file object destructor: sys.excepthook is missing lost sys.stderr Argh! Yes, the newly-created file object takes ownership of the filedescriptor. Once done with it, it invokes close() on it, making it unusable for the original sys.stdout. Okay, other approach: I believe that the only function regularly called on sys.stdout is write(). Just write a replacement that forwards the data to the original, followed by a call to flush. If you are ambitious, forward any other call to sys.stdout directly by catching attribute lookup (__getattribute__) in your class. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Opinion on best practice...
Am 05.02.2013 11:35, schrieb Peter Otten: Ulrich Eckhardt wrote: [...] use os.path.walk(), because that doesn't first build a list and then iterate over the list but iterates over the single elements directly. [...] Not true. os.walk() uses os.listdir() internally. Oh. 8| Thanks for proofreading what I wrote, I must have been confusing it with something else. Thankssorry! Uli -- http://mail.python.org/mailman/listinfo/python-list
puzzled by name binding in local function
Hello Pythonistas! Below you will find example code distilled from a set of unit tests, usable with Python 2 or 3. I'm using a loop over a list of parameters to generate tests with different permutations of parameters. Instead of calling util() with values 0-4 as I would expect, each call uses the same parameter 4. What I found out is that the name 'i' is resolved when Foo.test_1 is called and not substituted inside the for-loop, which finds the global 'i' left over from the loop. A simple del i after the loop proved this and gave me an according error. Now, I'm still not sure how to best solve this problem: * Spell out all permutations is a no-go. * Testing the different iterations inside a single test, is inconvenient because I want to know which permutation exactly fails and which others don't. Further, I want to be able to run just that one because the tests take time. * Further, I could generate local test() functions using the current value of 'i' as default for a parameter, which is then used in the call to self.util(), but that code is just as non-obviously-to-me correct as the current code is non-obviously-to-me wrong. I'd prefer something more stable. Any other suggestions? Thank you! Uli # example code from __future__ import print_function import unittest class Foo(unittest.TestCase): def util(self, param): print('util({}, {})'.format(self, param)) for i in range(5): def test(self): self.util(param=i) setattr(Foo, 'test_{}'.format(i), test) unittest.main() -- http://mail.python.org/mailman/listinfo/python-list
Re: autoflush on/off
Am 04.02.2013 18:12, schrieb Jabba Laci: autoflush_on = False def unbuffered(): Switch autoflush on. global autoflush_on # reopen stdout file descriptor with write mode # and 0 as the buffer size (unbuffered) if not autoflush_on: sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) autoflush_on = True Note that you have two file objects (one not reachable any more) both writing to the same file descriptor. This also means you should first flush sys.stdout before changing it, otherwise it might still contain unflushed data. I call unbuffered() once and it works well. However, when this loop is over, I'd like to set the output back to buffered. How to do that? Just set sys.stdout back to the original value. OTOH, also check if you can't tell sys.stdout not to buffer. As far as I remember, in Perl it was simply $| = 1 and $| = 0. simply ... ;) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: monolithic apps
Am 24.01.2013 18:06, schrieb tamn...@gmail.com: Any suggestions for study?..: Is is possible to take a large executable with GUI and real time data and images, to extract modules, and it can run as if it looks like a monolithic application (windows over main windows, or images over other images) but is various python script driven modules calling each other as separate apps? http://www.gnu.org/software/hurd/ Uli g,dr -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory error with quadratic interpolation
Am 23.01.2013 05:06, schrieb Isaac Won: I have tried to use different interpolation methods with Scipy. My code seems just fine with linear interpolation, but shows memory error with quadratic. I am a novice for python. I will appreciate any help. #code f = open(filin, r) Check out the with open(...) as f syntax. for columns in ( raw.strip().split() for raw in f ): For the record, this first builds a sequence and then iterates over that sequence. This is not very memory-efficient, try this instead: for line in f: columns = line.strip().split() Concerning the rest of your problems, there is lots of code and the datafile missing. However, there is also too much of it, try replacing the file with generated data and remove everything from the code that is not absolutely necessary. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: need explanation
Am 21.01.2013 17:06, schrieb kwakukwat...@gmail.com: please I need some explanation on sys.stdin and sys.stdout, and piping out http://www.catb.org/esr/faqs/smart-questions.html Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: unit selection problem
Am 14.01.2013 21:29, schrieb Paul Pittlerson: map_textures = get_sprites( (48, 48) ,spritesheet.png , (0, 0) ) You forgot to include spritesheet.png in your message. Seriously, condense your code down to a minimal example. This might help you finding the problem yourself, otherwise post the complete compilable example here. Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: interrupt the file sending if the file size over the quota...some errors here...
Am 15.01.2013 10:46, schrieb Levi Nie: i want to interrupt the file sending. but i can't change the client. so i need change the server. All things go well, but the message i wanna response seem not work. Ahem, what? It doesn't work, so does it sit on the couch all day? is the self.transport.loseConnection() (the last line) blocking the messages? in fact, i work on Cumulus(nimbus project) which based on twisted. And i use s3cmd as the client. I'm wondering if questions concerning twisted don't have a better forum. In any case, I can only comment on the general approach. For that, there are two things you can do: 1. When receiving the the request header, you have a content length. If that exceeds the allowed amount, shutdown() receiving and send an according HTTP response before closing the connection. 2. If the data exceeds the amount advertised by the content length, close the connection and discard the request. If you want to be nice, send an according response before closing, but I personally wouldn't go to that effort for broken HTTP clients. Concerning the question why your client hangs, it could also be the sending. If you try to send something before receiving the full request, client and server could enter a deadlock where each side waits for the other to receive some data. For that reason, you should shutdown() receiving in such a case. HTH Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: new to python and programming at large
Am 09.01.2013 22:05, schrieb kwakukwat...@gmail.com: pls I want to write a function that can compute for the sqrt root of any number.bt it not working pls help. Whenever describing an error, be precise. In this particular case, we have some sourcecode (which is good!) but what is still missing is what exactly you see when running that code (output and error messages) and what you expected instead. from math import sqrt def squareroot(self): x = sqrt(y) print x In this very case, I also wonder if the tutorial you are learning from assumes Python 2 while you are using Python 3. This is important, because print is a special statement in Python 2 but a normal function in Python 3. That said, I see two errors here: 1. self: This is customary used when you have a class function that takes an instance of that class. This instance is then the first parameter and called self. Python doesn't enforce this, but you should adhere to this convention to avoid confusion. Since you are not writing a class, don't name this parameter self. 2. There is no y in that code. I guess that if you renamed your self to y, you would get what you wanted. Good luck and welcome to Python! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with calling function from dll
Am 13.12.2012 08:40, schrieb deep...@poczta.fm: I have problem with using function from dll. import ctypes b = ctypes.windll.LoadLibrary(kernel32) a = b.GetComputerNameA(a,20) GetComputerNameA takes a pointer to a writable char string. You give it a pointer to an immutable string. You will have to create a buffer first and pass that to the function. Also, I would use GetComputerNameW instead, although it makes little difference for this name (provided that's the hostname, which may only contain an ASCII subset). But I got exception: Traceback (most recent call last): File pyshell#323, line 1, in module b.GetComputerNameA(a,20) WindowsError: exception: access violation reading 0x0014 Here is something else that is wrong: The address 0x0014 that the function tries to access is neither the address of a mutable nor an immutable string but simply the value 20 that you passed as second parameter. In other words, the way that the parameters are passed to the function is wrong. Even when I write: a = ctypes.c_char_p('.' * 20) I got the same result. This looks much better than passing the string above, but it still seems the (hopefully correct) parameters are passed wrongly. Here I found this function description: http://sd2cx1.webring.org/l/rd?ring=pbring;id=15;url=http%3A%2F%2Fwww.astercity.net%2F~azakrze3%2Fhtml%2Fwin32_api_functios.html Use the primary source for such info: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724295%28v=vs.85%29.aspx One possibly important point there is the WINAPI part, which describes how parameters are passed to and from the function, which might be the only cause that this doesn't work. However, instead of try-and-error, rather go to the documentation of the ctypes API and search for WINAPI. There, you will find an example that uses a function from the win32 API, too. A bit further down, you will also find a create_string_buffer() function that could be useful for you. http://docs.python.org/2/library/ctypes.html Greetings! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Error .. Please Help
Am 12.12.2012 16:00, schrieb inshu chauhan: color = image[y,x] if color == (0.0,0.0,0.0): continue else : if color == (0.0,255.0,0.0): classification = 1 elif color == (128.0, 0.0, 255.0): classification = 2 elif color == (255.0,0.0,0.0): classification = 3 elif color == (128.0, 128.0, 128.0): classification = 4 elif color == (128.0, 64.0, 0.0): classification = 5 elif color == (0.0, 0.0, 255.0): classification = 6 elif color == (255.0, 0.0, 255.0): classification = 7 Use a dict for this, it probably makes things clearer. Something like: cclasses = {( 0.0, 0.0, 0.0): None, ( 0.0, 255.0, 0.0): 1, (128.0, 0.0, 255.0): 2, } if cclasses[color] is not None: print g, x , y , color, cclasses[color] Some notes: * Some people (and I think PEP8) frown upon the table formatting of the dict. * d[color] will raise an exception if there is no mapping, but it's not clear what you want to do with those inputs anyway. * Comparing floating-point values for equality is always a bit tricky, as most operations have some rounding. This could mean that you need to use ranges instead of fixed values. A good approach is to round the input to integral values first. * Are you sure that the input uses floating point values but uses typical 8-bit ranges 0-255? I would expect floating point values between 0 and 1 or integral values between 0 and 255, but not such a mixture. * Try g.write('{} {} {} {}\n'.format(x, y, color, classification)) to get code that you will be able to reuse in Python 3. Also, consider upgrading anyway. I am getting the following error.. Traceback (most recent call last): File Z:\modules\Get_Classification.py, line 27, in module print g, x , y , color, classification NameError: name 'classification' is not defined Its simple error of name but m not getting why it should come as I have already defined Classification in between if-else loop ?? One comment here: If you don't define classification in this loop iteration, the one from the previous iteration will be used. Effectively, this tells me that the first pixel unequal to (0,0,0) already doesn't fit your expectations. Use import pdb and pdb.set_trace() to get into debugging mode, which is a useful skill anyway. Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: amazing scope?
Am 30.11.2012 12:11, schrieb andrea crotti: I wrote a script, refactored it and then introducing a bug as below: def record_things(): out.write(Hello world) This is a function. Since out is not a local variable, it is looked up in the surrounding namespace at the time the function is called. if __name__ == '__main__': with open('output', 'w') as out: record_things() This is not in a function, so binding variables affects the containing namespace. This includes binding the context manager to out. Note that you could have moved the whole code into a function (e.g. one called main()) and then you would have gotten the expected failure. What my explanation might be is that the out is declared at module level somehow, but that's not really intuitive and looks wrong, and works both on Python 2.7 and 3.2.. Other than in C/C++/Java and others, indention doesn't introduce a new scope, but I understand your intuition. Even simpler, this is how my early Python code looked like, in the absence of the C ternary operator and the distinction between a variable declaration and an assignment: foo = None if some_condition: foo = 'bar' else: foo = 'baz' More idiomatic would have been this: if some_condition: foo = 'bar' else: foo = 'baz' Summary: I'd say that everything is fine. ;) Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create an executable from python script in windows
Am 28.11.2012 07:43, schrieb Prakash: copying C:\Python24\lib\site-packages\py2exe\run_w.exe Python 2.4 was released 8 years ago and shouldn't be used for new development or learning any longer. The first step I would take is to upgrade to 2.7, which is the last in the 2.x series. Further, I would at least consider upgrading to Python 3.x, although this might require some tweaking of your sourcecode first. Keep in mind that 2.x is a dead end though! The following modules appear to be missing ['win32com.shell'] If the warning persists with 2.7 and you are actually getting errors at runtime, throw the whole message at e.g. google.com and click a few links. Hint: This is a known problem, you are not the first one! Good luck! Uli -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Frameworks Excessive Complexity
Am 21.11.2012 02:43, schrieb Steven D'Aprano: On Tue, 20 Nov 2012 20:07:54 +, Robert Kern wrote: The source of bugs is not excessive complexity in a method, just excessive lines of code. Taken literally, that cannot possibly the case. def method(self, a, b, c): do_this(a) do_that(b) do_something_else(c) def method(self, a, b, c): do_this(a); do_that(b); do_something_else(c) It *simply isn't credible* that version 1 is statistically likely to have twice as many bugs as version 2. Over-reliance on LOC is easily gamed, especially in semicolon languages. Don't indent deeper than 4 levels! OK, not indenting at all, $LANG doesn't need it anyway. Sorry, but if code isn't even structured halfway reasonably it is unmaintainable, regardless of what CC or LOC say. Besides, I think you have the cause and effect backwards. I would rather say: The source of bugs is not lines of code in a method, but excessive complexity. It merely happens that counting complexity is hard, counting lines of code is easy, and the two are strongly correlated, so why count complexity when you can just count lines of code? I agree here, and I'd go even further: Measuring complexity is not just hard, it requires a metric that you need to agree on first. With LOC you only need to agree on not semicolon-chaining lines and how to treat comments and empty lines. With CC, you effectively agree that an if statement has complexity of one (or 2?) while a switch statement has a complexity according to its number of cases, while it is way easier to read and comprehend than a similar number produced by if statement. Also, CC doesn't even consider new-fashioned stuff like exceptions that introduce yet another control flow path. LoC is much simpler, easier to understand, and easier to correct than CC. Well, sure, but do you really think Perl one-liners are the paragon of bug-free code we ought to be aiming for? *wink* Hehehe... ;) 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
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: 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: 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: 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 stdin, line 1, in module File stdin, 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 stdin, line 1, in module 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: 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 ulrich.eckha...@dominolaser.com 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
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: 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
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: 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 no.email@nospam.invalid wrote: Martin Hewitson martinhewit...@mac.com 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
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
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: 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
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
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
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
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 ian.g.ke...@gmail.com 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: ---88-- 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) ---88-- 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
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 class name='foo' 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: 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(TAG) : 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: 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: 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 module from bluetooth import * File /home/luca/py-temperature/py-temperature/bluetooth.py, line 19, in module 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
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: 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
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