[Tutor] Test Drive Development, DocTest, UnitTest
Hi All, In my journey from a hacker to a professional software developer, I have started to learn the finer points of Test Drive Development via Django (no questions about Django though). I am torn between the DocTest and UnitTest. I like the one fileness of the DocTest, but am concerned about the length of my tests being several orders of magnitude bigger than the actual code. I like the UnitTest having a separate file but am worried about the tests getting lost or never getting executed because a junior developer doesn't know about them. I'm wondering in respect to TDD, which is better or is it a matter of taste? TIA, Tino ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Opening C++ binary files
I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded -Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening C++ binary files
On Tue, Sep 21, 2010 at 12:03 PM, Joe Bennett jammer10...@gmail.com wrote: I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded -Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor fileobj = open(filename, mode='wb') will open the file. Do you have any documentation on the file structure? -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening C++ binary files
On 9/21/2010 12:34 PM, Joel Goldstick wrote: On Tue, Sep 21, 2010 at 12:03 PM, Joe Bennett jammer10...@gmail.com mailto:jammer10...@gmail.com wrote: I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded -Joe ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor fileobj = open(filename, mode='wb') Did you mean rb? OP says files were written by another program. will open the file. Do you have any documentation on the file structure? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening C++ binary files
On Tue, Sep 21, 2010 at 1:23 PM, bob gailer bgai...@gmail.com wrote: On 9/21/2010 12:34 PM, Joel Goldstick wrote: On Tue, Sep 21, 2010 at 12:03 PM, Joe Bennett jammer10...@gmail.comwrote: I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded -Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor fileobj = open(filename, mode='wb') Did you mean rb? OP says files were written by another program. oops.. of course! will open the file. Do you have any documentation on the file structure? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening C++ binary files
On Tue, Sep 21, 2010 at 1:23 PM, bob gailer bgai...@gmail.com wrote: On 9/21/2010 12:34 PM, Joel Goldstick wrote: On Tue, Sep 21, 2010 at 12:03 PM, Joe Bennett jammer10...@gmail.com wrote: I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded -Joe ___ Tutor maillist - tu...@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor fileobj = open(filename, mode='wb') Did you mean rb? OP says files were written by another program. will open the file. Do you have any documentation on the file structure? I have not done this, but googling around I found a python library called struct which has methods for reading C structured data, which sounds like what you are looking for. Go to your python shell, import struct and type help(struct) to start. Obviously you won't get far unless you have a spec for how your data is structured in the file -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intercepting and recored I/O function calls
I found this recipe quite related http://code.activestate.com/recipes/198078/ it logs all methods calls with their parameters and return values.. Can this be modified to include any other outputs of the module, which are not specified with the return statement. cheers On Fri, Sep 17, 2010 at 6:47 PM, Alan Gauld alan.ga...@btinternet.comwrote: Jojo Mwebaze jojo.mweb...@gmail.com wrote My applogies to begin with, it seems i didnt state my problem clearly for this particular case - perharps I/O was not the best way to describe my problem. Hmmm, perhaps not! :-) Specifically, i would like to track all inputs/output to modules/functions - if a module retrieved and used files and run some analysis on them and produced other files in return, i would like to take not of this. i.e what i want is to recored input and outputs to a module. and also to record all paramaters, attribute vaules used by the same module. You could do some of that with static code analysis I think. And while I'm sure you could do this by delving into the innards of Python it will be a lot of work and I'm not sure what the impact would be on the performance of your code. I thought i would build a wrapper around the orignial python program or probably pick this information at OS level. I don't think the OS will have much of what you want you will need to look inside Python itself I think. It might be best to tweak the interpreter to trap those details but that would need to be done in C. And it would slow the interpreter down for everything. The OS knows how your app is interacting with the network and file system but it doesn't know about the interactons between modules inside Python. Maybe there are some of the more obscure modules in the standard library that allow monitoring. The debug and profiling modules might yield clues too. An interesting challenge and I'm not sure how I'd go about it myself. Might be a good one to try on the main comp.lang.python list, its certainly not typical newbie stuff! Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] trouble with a small Tkinter example
Hello, I'm having trouble with this small example program: http://en.literateprograms.org/Bresenham%27s_line_algorithm_%28Python%29 When I run it, I only get a blank grey window. I'm running Python 2.6 under Windows XP. If there's a problem with the code, I can't see it... it seems like it should work. Can anyone else try this and see if it works for them? If I stick this line in before the loop, it does draw a red line, so I know my copy of Python/Tkinter is working: canvas.create_line(10, 100, 5, 50 , fill='red') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with a small Tkinter example
I'm having trouble with this small example program: http://en.literateprograms.org/Bresenham%27s_line_algorithm_%28Python%29 When I run it, I only get a blank grey window. I'm running Python 2.6 under Windows XP. If there's a problem with the code, I can't see it... it seems like it should work. Can anyone else try this and see if it works for them? It appears that Tkinter (or probably the underling tcl/tk) is smart: if you create a line with the same start and end points, it won't draw a line. Try it: create_line(10, 100, 10, 100, fill='red') won't work. If I stick this line in before the loop, it does draw a red line, so I know my copy of Python/Tkinter is working: canvas.create_line(10, 100, 5, 50 , fill='red') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with a small Tkinter example
On 9/21/2010 1:50 PM Chuck Mayers said... Hello, I'm having trouble with this small example program: http://en.literateprograms.org/Bresenham%27s_line_algorithm_%28Python%29 When I run it, I only get a blank grey window. I'm running Python 2.6 under Windows XP. It looks like they've got you assembling the program from bits and pieces as it's presented and reviewed. Can you post your program as you've reassembled it so we can see what's actually coded? Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with a small Tkinter example
There is a not-very-obvious 'Download Code' link at the very bottom of the article. Direct link: http://en.literateprograms.org/Special:Downloadcode/Bresenham%27s_line_algorithm_%28Python%29 On Tue, Sep 21, 2010 at 4:13 PM, Emile van Sebille em...@fenx.com wrote: It looks like they've got you assembling the program from bits and pieces as it's presented and reviewed. Can you post your program as you've reassembled it so we can see what's actually coded? Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with a small Tkinter example
On 9/21/2010 1:50 PM Chuck Mayers said... Hello, I'm having trouble with this small example program: http://en.literateprograms.org/Bresenham%27s_line_algorithm_%28Python%29 When I run it, I only get a blank grey window. I'm running Python 2.6 under Windows XP. If there's a problem with the code, I can't see it... it seems like it should work. Can anyone else try this and see if it works for them? If I stick this line in before the loop, it does draw a red line, so I know my copy of Python/Tkinter is working: canvas.create_line(10, 100, 5, 50 , fill='red') Using this form of the line, canvas.create_line(xstart, ystart, xend, yend, fill=red) seems to do it, although I can't say what versions allowed the author to (apparently successfully) write canvas.draw_line(xstart, ystart, xend, yend, color=blue) HTH, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
Currently I'm grappling with the concept of object orientating programming and have a question about setting getting attributes. Oops, just re-read that email. Anyone spot the obvious typo... Ahemmm of course I meant object orientated ;-) Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
Currently I'm grappling with the concept of object orientating Oops, just re-read that email. Anyone spot the obvious typo... Ahemmm of course I meant object orientated ;-) Actually I believe you mean object *oriented*. -Wayne Wow, spelling lessons too. And I thought this place only helped people out with Python ;-) I hope you'll forgive my mistake. I'm English, although we invented the language I guess sometimes we have to Americanise our speech to fit in with common technical convention :-P. http://www.english-for-students.com/Oriented-or-Orientated.html Thanks Wayne, Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
On 9/21/2010 5:06 PM, lists wrote: Hi tutors, I'm trying to each myself programming and come from a background in system administration with some experience in scripting (so I'm very new to it). Currently I'm grappling with the concept of object orientating programming and have a question about setting getting attributes. As I understand it, it makes most sense to set/get the attribute of an object using a method rather than doing it directly. My opinion - unless there is some verification or translation or action required it is better (easier, clearer) to just access and assign the attribute directly. I've been reading various ways of doing this, and the information seems a little contradictory. Example, please? I've muddled my way through the code below to try and force setting or getting the 'address' attribute through the address method rather than allowing direct access. Just because you have a getter and setter does not prohibit direct reference to _address. Does this make sense to you? class Computer(object): def __init__(self): instantiate the class with default values self.address = I suggest (if you want to go the setter/getter route that you initialize _address, just in case someone tries to reference it without setting it. @property # use the property.getter decorator on this method def address(self): return self._address @address.setter #use the property.setter decorator on this method def address(self, addrvalue): self._address = addrvalue computer1 = Computer() computer1.address = test print computer1.address -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Opening C++ binary files
On Wed, 22 Sep 2010 02:03:41 am Joe Bennett wrote: I have some binary files created by a program written in C++... Anyone have any experience with this and willing to share? ACSII test is easy, but not sure how the rest is encoded What difference does the language the program was written in make? What matters is the *type* of binary file: .avi, .mp3, .jpg, .png, .doc, .zip, or any one of a million other kinds of binary file. If the binary file is a standard data type, like .mp3 or .zip, your best strategy is to find a library specially desigined for that data type, and use it. If it's a non-standard type, you can open and read the file yourself with: f = open(name-of-binary-file, rb) # you MUST have the b data = f.read() f.close() and then do whatever you want with the data. The struct module may be useful for that. If you don't know what the structure of the data is, then there's probably not much you can do with it except make a mess. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
On Wed, 22 Sep 2010 07:06:50 am lists wrote: As I understand it, it makes most sense to set/get the attribute of an object using a method rather than doing it directly. Heavens no!!! That's backwards! It might be justified in languages like Java, where you can't easily change your mind about direct attribute access. There's also a school of thought that disapproves of direct attribute access for a number of philosophical reasons, but if you strongly agree with that philosophy you'll probably find Python isn't a good language for you. (It has to do how much the compiler should forbid the programmer from doing, and how much software reliability that buys you.) 90% of the time, stick to direct attribute access, especially for short scripts and pre-release versions of software. If and only if you find that too limiting, or perhaps not limiting enough, then change to using getter and setter functions and properties. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] list.append(x) but at a specific 'i'
hello, how do i extend a python list but from a given [i], for example: a = ['a', 'b', 'e'] b = ['c', 'd'] a + b ['a', 'b', 'e', 'c', 'd'] but i want to put the items of 'b' at [-2] for example. thanks -- ˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ ǝq s,ʇǝן ʇǝʎ % .join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] ) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
My opinion - unless there is some verification or translation or action required it is better (easier, clearer) to just access and assign the attribute directly. This makes sense. I guess my current way of doing it just reflects my inexperience. At the moment I'm just fumbling this out so I guess I'm being over-cautious. I've been reading various ways of doing this, and the information seems a little contradictory. Example, please? Forgive me for any mistakes in this explanation, I'm still learning, but as far as I can figure out, in some earlier versions (before 2.2 perhaps?) it wasn't possible to assign a decorator directly to a method using the at sign, and it was necessary to do this elsewhere within the class itself. I think this link explains what I mean: http://docs.python.org/release/2.4.2/whatsnew/node6.html It seems some tutorials I've been reading were written for this older syntax. I've muddled my way through the code below to try and force setting or getting the 'address' attribute through the address method rather than allowing direct access. Just because you have a getter and setter does not prohibit direct reference to _address. I had read something about that. I wonder if the idea behind doing it in the way I've shown is to help prevent the programmer from doing it accidentally perhaps? class Computer(object): def __init__(self): instantiate the class with default values self.address = I suggest (if you want to go the setter/getter route that you initialize _address, just in case someone tries to reference it without setting it. Do you mean like: class Computer(object): def __init__(self): address = self.address = address Or am I missing the point? Thanks for your time Bob, Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
On Tue, Sep 21, 2010 at 4:16 PM, bob gailer bgai...@gmail.com wrote: On 9/21/2010 5:06 PM, lists wrote: Hi tutors, I'm trying to each myself programming and come from a background in system administration with some experience in scripting (so I'm very new to it). Currently I'm grappling with the concept of object orientating programming and have a question about setting getting attributes. As I understand it, it makes most sense to set/get the attribute of an object using a method rather than doing it directly. My opinion - unless there is some verification or translation or action required it is better (easier, clearer) to just access and assign the attribute directly. I've been reading various ways of doing this, and the information seems a little contradictory. Example, please? I've muddled my way through the code below to try and force setting or getting the 'address' attribute through the address method rather than allowing direct access. Just because you have a getter and setter does not prohibit direct reference to _address. Does this make sense to you? class Computer(object): def __init__(self): instantiate the class with default values self.address = I suggest (if you want to go the setter/getter route that you initialize _address, just in case someone tries to reference it without setting it. @property # use the property.getter decorator on this method def address(self): return self._address @address.setter #use the property.setter decorator on this method def address(self, addrvalue): self._address = addrvalue computer1 = Computer() computer1.address = test print computer1.address -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Hello Bob, Here is a working example of what I think you are trying to achieve. In this example the address is set via the setter and some simple validation is there and the private var isn't available as __address but get rewritten to _Computer__address (so not private but not obvious) class Computer(object): def __init__(self): self.__address = None # see note on private vars in Python http://docs.python.org/tutorial/classes.html?highlight=private#private-variables @property def address(self): return self.__address @address.setter def address(self, value): if value not in (available, criteria): raise AttributeError(Nope) self.__address = value Hope that helps, Vince Spicer -- Sent from Ubuntu ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting/setting attributes
As I understand it, it makes most sense to set/get the attribute of an object using a method rather than doing it directly. Heavens no!!! That's backwards! It might be justified in languages like Java, where you can't easily change your mind about direct attribute access. There's also a school of thought that disapproves of direct attribute access for a number of philosophical reasons, but if you strongly agree with that philosophy you'll probably find Python isn't a good language for you. (It has to do how much the compiler should forbid the programmer from doing, and how much software reliability that buys you.) Heh, at the moment I don't have a programming philosophy. It's slowly being formed by conversations with people like you :-) 90% of the time, stick to direct attribute access, especially for short scripts and pre-release versions of software. If and only if you find that too limiting, or perhaps not limiting enough, then change to using getter and setter functions and properties. This definitely seems to be the consensus. My instinct is telling me that it's probably best to use getters and setters when requiring input from a user that needs to be validated perhaps, but that my initial reading on the matter seems to have over emphasised the importance of accessing attributes via methods. Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Test Drive Development, DocTest, UnitTest
On Wed, 22 Sep 2010 01:37:42 am Tino Dai wrote: I am torn between the DocTest and UnitTest. I like the one fileness of the DocTest, but am concerned about the length of my tests being several orders of magnitude bigger than the actual code. I like the UnitTest having a separate file but am worried about the tests getting lost or never getting executed because a junior developer doesn't know about them. I'm wondering in respect to TDD, which is better or is it a matter of taste? Neither is better. They're just different, with different purposes. The *primary* purpose of doctests are to be executable examples. When you write documentation, including example code is the most natural thing in the world. doctest lets you execute those examples, to ensure that they work. They're certainly not meant as an exhaustive test of every single feature in the program, but as *documentation* that happens to also work as tests. Unit tests can be a little heavyweight, but they're designed for exhaustive tests of the *entire* program, not just the parts with user-documentation. You should write whitebox tests, not just blackbox tests. That means don't just write tests for the published interface, but write tests for the unpublished internal details as well. E.g. if your function has special processing to deal with lists of strings, then you need a test for input that is a list of strings. But it's not necessary to document that fact in the doc string. What do the users care that your function calls a special subroutine to deal with lists of strings? So it would be inappropriate to draw attention to this fact with a doctest. Doctests don't just execute themselves. If your developers, junior or otherwise, don't know about the tests, don't keep the tests up to date, and don't run the tests, then it doesn't matter what testing framework you use. Doctests and unittests are complementary. They work well together. In fact, the unittest module can even execute doctests. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list.append(x) but at a specific 'i'
On 9/21/2010 6:30 PM, Norman Khine wrote: a = ['a', 'b', 'e'] b = ['c', 'd'] i want to put the items of 'b' at [-2] for example. a[-2:-2]=b a ['a', 'c', 'd', 'b', 'e'] -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor