Re: [Tutor] Percentage
On 07 Nov 2005 11:50:05 -0200, Jorge Godoy <[EMAIL PROTECTED]> wrote: > Johan Geldenhuys <[EMAIL PROTECTED]> writes: > > > What is the syntax if I want to work out what percentage 42 is out of 250? > > If you want it as a factor to multiply / divide by something: > > perc = 42/250 Don't you need to specify one of these numbers as a float? >>> print 42/250 0 >>> print 42/250.0 0.168 I recall reading somewhere that all division would be 'true division' from >3.0 but this isn't the case in 2.x is it? > Jorge Godoy <[EMAIL PROTECTED]> S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Iterate over letters in a word
Hello, I'm trying to work on some programs to help me understand ciphers and ultimately cryptography. I've understood so far, that a simple form of bit-level cryptography is to split the original message into chunks the same length as a 'key' and then do an xor. I'm trying to keep this really simple so I can understand from first principles - so eg: "Hello Tutors!" could be split into: "Hell" "o Tut" "ors!" and xor'd with "beer" I think I understand how xor works (thanks to an earlier post) but I'm not sure how to iterate over each letter in a string. What is the recommended way to do this? Thanks, S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterate over letters in a word
On 3/14/06, Danny Yoo <[EMAIL PROTECTED]> wrote: > The idea is to unpack four single characters as a single 4-byte integer. That's really useful, thanks, as I was planning to iterate over each letter and call ord() > This kind of transformation is reversable: Highly useful. Thanks very much indeed. > Does this make sense? Absolutely. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterate over letters in a word
On 3/14/06, Matthew Webber <[EMAIL PROTECTED]> wrote: > As a side note, remember that that xor-ing a key with a message is trivial > to break (it's just a variation on the Vigenere cipher first published in > 1568). So don't use if for any real applications. Yes - at the moment this is just a way for me to begin to get my head around how cryptography works from anabsolutely ludicrously basic position. This all started because I couldn't get my head around the difference between an encryption algorithm and the key. I thought that by writing my own, I would work it out! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterate over letters in a word
On 3/14/06, Steve Nelson <[EMAIL PROTECTED]> wrote: > On 3/14/06, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > The idea is to unpack four single characters as a single 4-byte integer. > > That's really useful, thanks, as I was planning to iterate over each > letter and call ord() Ok, so experimenting a little further, and looking at the documentation, it seems that len(string) and calcsize (i) must be the same. Is there a reason why 'i' is a 4 byte integer? Doesn't this mean that this method wouldn't scale if I then decided I wanted to use, eg, a 6 byte key instead of a four? Or do I misunderstand? I am also struggling to understand why a 4 byte integer is so large? >>> mystring = "Hello I am Steve" >>> import struct >>> struct.unpack('i', mystring[0:4]) (1819043144,) I can see that the largest number I can generate in a 1 byte integer is 255 - which is (2^8)-1. Is the point that with a 4 byte number I can actually get (2^32)-1 , ie 4294967295? This just seems like a huge number! I suppose I've answered my question... but any comments or clarifications would help a lot. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Splitting a string into n-sized bytes
Hello all, Further to my previous puzzling, I've been working out the best way to chop a string up into n-sized words: I'm aware that I can use a slice of the string, with, eg, myWord[0:4]. I am also aware that I can do blob = myWord.pop(0) to take (and remove) the first character. I can botch these together, eg myFourLetterWord = myWord.pop(0)+myWord.pop(0)+myWord.pop(0)+myWord.pop(0) but that is really horrid. I then tried doing something suitably silly, as follows: 1) Find the length of string. 2) Create a list using range(0, length, 4) 3) We now have the end points for each 'word', eg 4, 8, 12. 4) Now create a list of tuples that represent the slices we need, ie (0,4), (5,8) etc 5) Iterate over this list, grabbing the slices as depicted in the tuples. 6) Use these tuples to grab slices. The code looked like this: #!/usr/bin/env python myString = "Sir, you are an egotistical rhetorician!!!" length=len(myString) extra=length%4 if extra > 0: myString = myString+("#"*extra)+"#" r = range(0, len(myString), 4) wordRange=[] for i in r: if i>1: wordRange.append((int(i-4),i)) for t in wordRange: print myString[t[0]:t[1]] Surely there's an easier way? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Splitting a string into n-sized bytes
On 3/14/06, Adam <[EMAIL PROTECTED]> wrote: > Hopefully that should point you in the right direction to do n-sized > words as well. Indeed - as I now have a function: def nsplit(s, n): return [s[i:i+n] for i in range(0, len(s), n)] Incidentally I am currently going with: def nsplit(s, n): while s: yield s[:n] s = s[n:] As my friend just showed me how generators work, and also that the beginning and end of lists are implicit. Very cool. Thanks for all your help! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Alternative to nested loops
Hi All, I had a feeling I could do this: >>> foo [[1, 2, 3], [1, 2, 3], [1, 2, 3]] >>> for c in foo: ... for b in c: ... print b ... 1 2 3 1 2 3 1 2 3 Using a list comprehension, as it seemed to me like I was saying: b for c in foo, but I can't see how to do this. Ultimately I want to sum each number and produce a total. I know how to do this as above, but was wondering if there is an alternative / better way? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Alternative to nested loops
On 3/19/06, Karl Pflästerer <[EMAIL PROTECTED]> wrote: >>> reduce(lambda s, L: s + sum(L), foo, 0) Ah ok - well that looks pretty cryptic to me, as I've never used either lambda or reduce(). However, this looks to be a 'functional' way of doing what I was doing procedurally, which is, I suppose, what I was asking. Would you mind helping to explain how the above works? Incidentally, I had a look at: http://www-128.ibm.com/developerworks/linux/library/l-prog.html As an introduction to functional programming in Python - it looks interesting. I've never done any functional programming at all, so it all seems a little foreign! Can you recommend another gentle introduction? Or will this fry my brain, as I start trying to mix methodologies? Thanks! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Alternative to nested loops
On 3/19/06, John Fouhy <[EMAIL PROTECTED]> wrote: > What you're doing is called "flattening" a list. You can do it with a > list comprehension: > > >>> foo = [[1,2,3], [4,5,6], [7,8,9]] > >>> [x for y in foo for x in y] > [1, 2, 3, 4, 5, 6, 7, 8, 9] Ah yes, that was the sort of thing I was thinking of. > If you want to sum, you could also use a generator expression > (requires Python 2.4): I am on 2.3.5 so I can do sum([x for y in foo for x in y]) So now looking at both your and Karl's ways, how do I catch exceptions. My current (working) code looks like this: def fleetHealth(self): """Iterate through each square to see if the whole fleet has been sunk.""" s = 0 for c in self.ranks: for b in c: try: s += b.contents.size() except: pass return s I guess this could be significantly refactored! But... readability counts! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Alternative to nested loops
On 3/19/06, Alan Gauld <[EMAIL PROTECTED]> wrote: > > interesting. I've never done any functional programming at all, so it > > all seems a little foreign! > > > > Can you recommend another gentle introduction? > > Try the functional programming topic in the Advanced section of my tutor. > It covers the concepts and how to do it in Python at the basic level. Thanks - that makes things much clearer. I love the fact that Python can be used in so many ways! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie exercises
On 3/27/06, josip <[EMAIL PROTECTED]> wrote: > Can someone give me exercises to do with loops, maybe functions to? How about a program that produces truth tables for the basic gates? AND, NAND, NOT, OR, XOR? You could write a function for each gate, and one to produce a truth table. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie exercises
On 3/29/06, Keo Sophon <[EMAIL PROTECTED]> wrote: > Is it bitwise operator? Could you give one example? >>> for a in range(2): ... for b in range(2): ... print a, b, a&b ... 0 0 0 0 1 0 1 0 0 1 1 1 > Sophon S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does it function
On 3/29/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote: > Hi ALL > > Just wanted to know the detailed explanation about the below statement > > if __name__ == "__main__": Simple answer - any python program you write is effectively a 'module'. Modules have an attribute __name__. If you've imported the module from elsewhere, the __name__ is set to the name of the module, otherwise it is __name__. This means that you can write a test that says:If the code we're trying to run is the main program, go ahead and start running the functions we need. You can read more about it here: http://swaroopch.info/text/Byte_of_Python:Modules and also here: http://diveintopython.org/getting_to_know_python/testing_modules.html More detailed info here: http://www.python.org/doc/current/ref/import.html S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How does it function
On 3/30/06, Terry Carroll <[EMAIL PROTECTED]> wrote: > On Wed, 29 Mar 2006, Steve Nelson wrote: > > > Simple answer - any python program you write is effectively a > > 'module'. Modules have an attribute __name__. If you've imported the > > module from elsewhere, the __name__ is set to the name of the module, > > otherwise it is __name__. > > I don't mean to nitpick, but I see Steve had a small but crucial slip > here. I think he means, " If you've imported the module from elsewhere, > the __name__ is set to the name of the module, otherwise it is "__main__". Yes absolutely - well spotted, and sorry for the typo. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Inverted Index Algorithm
Hello All, I've been reading about "Inverted Indexing" - I'd like to try to write something in Python that illustrates the concpet, as I've got to give a presentation about it. Where would be a good place to start? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Inverted Index Algorithm
On 3/31/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > Steve Nelson wrote: > > Do you need help getting started with Python or with inverted indexing > in particular? Sorry - I should have been clearer. I'm reasonably confident in Python, and if I get stuck with that side of things will ask for help. I was more struggling with the Inverted Indexing bit. I think I want to write a program that will do text searches on documents, to learn about and compare the two approaches I've heard of - Inverted Indexing and Signature Files. SO I think I am asking for help at the logical / implementation level. My understanding so far goes something like this: Suppose I have three documents - as a trivial but fun example, we could make them song lyrics. The simplest thing that could possibly work would be to supply one or two words and literally scan every word in the song for a match. This has two disadvantages - firstly it is likely to produce false positives, and secondly it is likely to be very inefficient. The next step would be to introduce an index. I think again, the simplest thing that could possibly work would be a literal index of every word and every document in which it appears. This would save processing time, but wouldn't be very intelligent. This is where I think the inverted indexing comes in. As I understand it we can now produce an index of key words, with document name and location in document for each key word. This makes the search more involved, and more intelligent. Finally we could have some logic that did some set analysis to return only results that make sense. Am I thinking along the right lines, or have I misunderstood? Could someone perhaps come up with some code snippets that make clearer examples? I also need to think about signature files, but I havn't really any idea how these work. Thanks all! S. > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi
On 4/4/06, Kaushal Shriyan <[EMAIL PROTECTED]> wrote: > Hi ALL > > A simple query is that the python mailing List is python powered > > What does "python powered" means The list, and many like it, use a piece of software called Mailman, which is written in Python. A few years back, the tool of choice was Majordomo, but Mailman is now very popular, partly because of its easy-to-use web interface. > Kaushal S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python video?
On 4/13/06, Danny Yoo <[EMAIL PROTECTED]> wrote: > > > On Wed, 12 Apr 2006, Hoffmann wrote: > > > I read this week on this forum about a kind of Python video in its > > website. Which view is that, and where could I find it? I search in > > Python website, but I didn't find it. Is it a 'demo' of the language? > > It's a video with gushing praise over Python, made in the style of a Monty > Python skit. On a similar line, I've recently discovered "podcasts". I spend a lot of time driving, and have been listening through the "Security Now" broadcasts, and the last few days some stuff on Evolutionary Theory. Does anyone know of some good sources for programming-type discussions - talks or lectures I could download and listen to? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] checking diagonals on a chessboard
On 4/13/06, Matthew Singletary <[EMAIL PROTECTED]> wrote: > I'm NOT looking for any answers, just some tips to an _elegant_ method to > solve this. My tutor/mentor and I wrote a chess program a while back - diaganols were the hardest bit to get working! Essentially diagonals are treated as though they were ranks and files on a board rotated through 45 degrees. Doing bitboard lookups then becomes fairly simple. A bitboard solution to the n-queens problem would actually be rather neat for a genetic algorithm, as the genes could correspond directly to the bitboards. Both are just large integers. If you haven't already, take a look at Bob Hyatt's bitboard paper. In this case, though, it is probably simple enough to work out if two pieces are on the same diagonal: 1. Find the difference in their ranks 2. Find the difference in their files 3. If the answer is the same (regardless of sign) then the pieces share a diagonal. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] functions in Python
Sorry - including list. On 4/17/06, Payal Rathod <[EMAIL PROTECTED]> wrote: > what is the difference between, > > def func(): > > > and > > def func(x): > When you define a function, you are writing a block of code which you can ask to perform a task. The task may be simple, and not require any additional information, or it may be more complex and need information. Take a simple analogy. Let's suppose I gave you the instruction "Switch off the light" and there was only one light, you wouldn't need any more information. We could call that: def toggleLight(): Now let's take a more complicated examle - making tea. If I give you the instruction "Make me a cup of tea" you might ask questions like "Milk? Sugar? Kind of tea?" I need to give you this information before you can perform the task. Similarly in a function - you may need to pass information (arguments) to the function. Here's the tea example: def makeTea(sugar, milk, kind): We might call the function as follows: makeTea(2, "none", "darjeeling") The internals of the function would be able to parse the arguments you gave it, and carry out the task you've asked it to do. As to when to use which, I think you'll find that most of the functions you write will require information if they're to be of much use. Only a very simple function doesn't take any arguments. Hope that helps :o) S. > > When to use which? (please do not say "returns a value" for I do not > understand the meaning > of the same) > > With warm regards, > -Payal > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Books
On 5/3/06, John Connors <[EMAIL PROTECTED]> wrote: > G'day, > > I know this is a difficult question to answer because it's probably more a > matter of personal taste than anything else. It is also a VFAQ. Check the archives - I'm not aware of any radical new books that would render the most recent incarnation of this subject outdated. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Beer Bottles
A bunch of my friends and I have been chatting about "99 bottles of beer" - and how to make the shortest code to do it. I have: for i in range(100,0,-1): print "%s bottles of beer on the wall, %s bottles of beer\nTake on down, pass it around.."%(i,i) print "Go to the store, buy some more" I'm curious to know other ways to handle this - could it be done functionally? How could we obfuscate this (not that we'd want to in real life)? Or make it a (close to) one liner? Thanks. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Test
I got a bounce... but have been on this list for months... S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Functional Programming
Hi All, I've just today starting thinking more about programming in a more functional style, and came across the following article which I thought was really good: http://www.amk.ca/python/writing/functional I know there's a section in Alan's tutorial on FP too, but can anyone else recommend some good references? It is all a very new approach for me, but it is also fascinating, and rather fun! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python on AIX
Hello all, Just started a new job - most of the machines I am administering are AIX, and don't have Python at all. What is going to me the most pain-free, scaleable and supportable way of getting Python onto these machines? I need to take this request to change advisory board. Also, I could find myself facing a barrage of questions along the lines of "Why not use perl or just shell?" Has anyone had much joy in winning over such people? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python on AIX
On 7/12/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > According to this page Python binaries for AIX are available. If you > click through the links, the versions actually available are more recent > than those listed on the first page. > http://www.python.org/download/other/ Thanks - that's perfect - there's lots of other useful stuff there too. Now to construct a convincing argument that I should be allowed to install it across the environment... > Kent S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Regular Expression Misunderstanding
Hello, I am reading the "Regular Expression HOWTO" at http://www.amk.ca/python/howto/regex/ I am at the bit where "greediness" is discussed with respect to metacharacters enabling repetition of sections of a RE. I understand the concept. The author gives a step by step example of how the matching engine goes through the RE step by step, and when the repitition metacharacter appears it tries the maximum first, and then effectively reels back until the last step of the RE will pass. This made sense after a bit of time with pen and paper. What I don't understand is how in the end the RE *does* actually match - which may indicate a serious misunderstanding on my part. >>> re.match("a[bcd]*b", "abcbd") <_sre.SRE_Match object at 0x186b7b10> I don't see how abcbd matches! It ends with a d and the RE states it should end with a b! What am I missing? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular Expression Misunderstanding
On 7/14/06, John Fouhy <[EMAIL PROTECTED]> wrote: > It doesn't have to match the _whole_ string. Ah right - yes, so it doesn't say that it has to end with a b - as per your comment about ending with $. > If you look at the match object returned, you should se that the match > starts at position 0 and is four characters long. How does one query a match object in this way? I am learning by fiddling interactively. > John. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular Expression Misunderstanding
On 7/14/06, John Fouhy <[EMAIL PROTECTED]> wrote: > >>> m = re.match(...) > >>> dir(m) > > It will tell you what attributes the match object has. Useful - thank you. I am now confuse on this: I have a file full of lines beginning with the letter "b". I want a RE that will return the whole line if it begins with b. I find if I do eg: >>> m = re.search("^b", "b spam spam spam") >>> m.group() 'b' How do I get it to return the whole line if it begins with a b? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular Expression Misunderstanding
On 7/14/06, Kent Johnson <[EMAIL PROTECTED]> wrote: > But for this particular application you might as well use > line.startswith('b') instead of a regex. Ah yes, that makes sense. Incidentally continuing my reading of the HOWTO I have sat and puzzled for about 30 mins on the difference the MULTILINE flag makes. I can't quite see the difference. I *think* it is as follows: Under normal circumstances, ^ matches the start of a line, only. On a line by line basis. With the re.M flag, we get a match after *any* newline? Similarly with $ - under normal circumstances, $ matches the end of the string, or that which precedes a newline. With the MULTILINE flag, $ matches before *any* newline? Is this correct? > Kent S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Comparing times
Hello there, I need to be able to compare time on a process tree with system time, and take action accordingly. Here's how I get the time on the process tree: >>> for line in os.popen("ps -ef", "r"): ... if "telnet" in line: ... print line.split()[4] ... 11:00:25 11:01:31 10:01:25 09:57:38 09:15:15 I can get the system time like this: >>> print time.ctime(time.time()).split()[3] 11:02:58 What I want to do is establish if the time of the process is *later* than the system date. For example, we might have a process with a time of 11:15:00, when the system time is 10:00:00. In practice this means that the process is from 11am yesterday. Other than splitting the string up more and saying is 11 bigger than 10, how can I go about this? Also, is my time getting method above ok? Or are there better / more elegant / more pythonic ways? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Comparing times
On 7/18/06, John Fouhy <[EMAIL PROTECTED]> wrote: > On 18/07/06, Steve Nelson <[EMAIL PROTECTED]> wrote: > > What I want to do is establish if the time of the process is *later* > > than the system date. For example, we might have a process with a > > time of 11:15:00, when the system time is 10:00:00. In practice this > > means that the process is from 11am yesterday. > > > > Other than splitting the string up more and saying is 11 bigger than > > 10, how can I go about this? > > Have a look at time.strptime. Yes - I've worked out how to do this with a combination of time.strptime() and time.mktime(), although the problem I face now is that datetime objects need a date, and my way of getting the time doesn't include a way of specifying the date. Perhaps I should be clearer: I have an application which connects using telnet to a server to communicate. The application is largely user-driven - ie such a connection represents a real user in real time. If they don't log out of the system, the pty that is associated with the process will remain used. There are only 256 available at present, and it only takes a few dozen lazy users, and we run out of ptys. Until we increase the number of ptys, and for housekeeping, even after, we have a method of checking the STIME property in ps to see if the format has changed from eg from 12:44:23 to Jan 7. If it has changed we kill that process. I've been asked to rewrite this script into something more capabale, because we're still seeing sessions connected from the previous day that could reasonably be killed off. Eg at 0900 today if I see a telnetd process with an STIME of 1000 I know it is 23 hours old, and has been left connected all night, and I can kill it. My task therefore is to find the STIME from ps, and somehow assign a date to it... perhaps I just assign it a date of today, and if the STIME is *later* that the system time, I know it is actuallly yesterday's? Just thinking aloud here... but thoughts / advice most welcome. Incidentally when I get to killing the process, any recommended ways? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Filesystem Usage
Hello chums, How can I go about getting info similar to that which the UNIX df command provides - of filesystem usage and inode usage? I could just shell out and run a df command, but I would rather use python bindings. What's the recommendation? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filesystem Usage
On 9/22/06, wesley chun <[EMAIL PROTECTED]> wrote: > this sounds like it will require some work to implement 'df' in > Python Mmm... although I have discovered a debian package called pydf whose source made interesting reading. > i'd use the one of > the {os,popen2}.popen*() functions or the subprocess module and > actually call 'df' but use Python to communicate with it (sending it > stuff via stdin and receiving output from it [stdout or stderr]). That sounds fascinating... and something to play with. In the end I just did: def fsUsage(dir): """Returns the % usage of a given filesystem""" stat = os.statvfs(dir) from statvfs import F_BLOCKS, F_BFREE total = stat[F_BLOCKS] avail = stat[F_BFREE] used = total-avail percent = used/total*100 return percent S. > > hope this helps a little! > -- wesley > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 > http://corepython.com > > wesley.j.chun :: wescpy-at-gmail.com > python training and technical consulting > cyberweb.consulting : silicon valley, ca > http://cyberwebconsulting.com > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Extract from Word Doc
Hello, Is there a way to pull "strings" out of a word document? Not unlike the way the UNIX command "strings" does? I want this to be OS-portable, so shelling out is not an option. I tried opening the word doc and then looking at it using the object's methods, but it is all binary info, and can't be parsed. Any suggestions? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting GID info
Hello all, I want to produce stats on file ownership. I am aware that I can use stat to obtain a file statistics tuple, and with the pwd method I can convert the UID to username. However, is there a similar method that will tell me that GID 1 == "staff"? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting GID info
On 1/19/07, Steve Nelson <[EMAIL PROTECTED]> wrote: > Hello all, > > I want to produce stats on file ownership. I am aware that I can use > stat to obtain a file statistics tuple, and with the pwd method I can > convert the UID to username. However, is there a similar method that > will tell me that GID 1 == "staff"? Hrm... and an inspired guess import grp is what I needed. > S. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filesystem Usage
On 9/22/06, Steve Nelson <[EMAIL PROTECTED]> wrote: > In the end I just did: > > def fsUsage(dir): > """Returns the % usage of a given filesystem""" > stat = os.statvfs(dir) > from statvfs import F_BLOCKS, F_BFREE > total = stat[F_BLOCKS] > avail = stat[F_BFREE] > used = total-avail > percent = used/total*100 > return percent Can someone explain how I manged to import F_BLOCKS and F_BFREE? I want to do the same with pwd and grp: >>> gstat = grp.getgrgid(1) >>> dir(gstat) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__str__', 'gr_gid', 'gr_mem', 'gr_name', 'gr_passwd', 'n_fields', 'n_sequence_fields', 'n_unnamed_fields'] >>> gstat[0] 'staff' >>> gstat[GR_GID] Traceback (most recent call last): File "", line 1, in ? NameError: name 'GR_GID' is not defined What's the difference? It just seems that specifying the location in the tuple is not very clear or self-documenting, and using GR_GID is better. S. > S. > > > > hope this helps a little! > > -- wesley > > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > > "Core Python Programming", Prentice Hall, (c)2007,2001 > > http://corepython.com > > > > wesley.j.chun :: wescpy-at-gmail.com > > python training and technical consulting > > cyberweb.consulting : silicon valley, ca > > http://cyberwebconsulting.com > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Filesystem Usage
On 1/19/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > The attribute access uses . notation and an attribute name. You can do > stat.f_blocks and gstat.gr_gid. Python takes care of looking up the > actual attribute value. Excellent - thank you. > I suggest you use the attribute form for both, it is more compact and > readable, consistent between both types of objects, and doesn't require > the import of statvfs. Done. > Kent S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] MD5 Digest for files
Hello, I want to create a dictionary of files and md5sums for a given directory. It seems, however, that md5 works with strings or read-only buffers, and can't be passed a file. What I want to do is something like: for f is os.listdir("."): d[f] = someFunctionThatReturnsMD5Sum(f) Has this wheel already been invented? I can't see how to operate on the file itself. S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Better printing?
Hello, See below a program that will go through a directory and for each file print user, group, size and md5sum. A few questions: 1) It seems to fall over if I pass "." as a directory - how can I stop this? 2) When I wrote the methods I decided to do isafile() checks there, but subsequently realised that in __main__ it would print out None for each non-file, so I put the check in here too. Which is better? Or is there a better approach? 3) How can I get this to print in a format that makes it easy to read? ie tabulated so that the info is presented in aligned columns? Code follows: #!/usr/bin/env python # Standard library imports import unittest, os, sys, pwd, grp, md5 # Global defines wp = sys.argv[1] # Exception classes # Utility functions def getSize(fn): """Get the size of a file in kb""" fp = os.path.join(wp, fn) if os.path.isfile(fp): return os.stat(fp).st_size def getUser(fn): """Get username of file owner.""" fp = os.path.join(wp, fn) if os.path.isfile(fp): uid = os.stat(fp).st_uid return pwd.getpwuid(uid).pw_name def getGroup(fn): """Get the group name of the file owner.""" fp = os.path.join(wp, fn) if os.path.isfile(fp): gid = os.stat(fp).st_gid return grp.getgrgid(gid).gr_name def md5Sum(fn): """Get md5sum for a file.""" fp = os.path.join(wp, fn) if os.path.isfile(fp): try: fo = open(fp, 'rb') except (IOError, OSError): print "Could not open file '%s', skipping..." % fp return None else: m = md5.new(fo.read()).hexdigest() fo.close() return m # Classes # Unit tests class UnitTests(unittest.TestCase): """Do not taunt unit tests.""" def testGetSize(self): """Get the size of a file in kb""" self.assertEqual(getSize("foo"), 24) def testGetUser(self): """Get the username of the file owner""" self.assertEqual(getUser("foo"), "nelsonst") def testGetGroup(self): """Get the group name of the file owner""" self.assertEqual(getGroup("foo"), "staff") def testMd5Sum(self): """Get md5sum for file.""" self.assertEqual(md5Sum("foo"), "faac88479f39ba498e827622a2a4d649") def doTests(): suite = unittest.makeSuite(UnitTests,'test') runner = unittest.TextTestRunner() result = runner.run(suite) if __name__ == "__main__": doTests() for f in os.listdir(wp): if os.path.isfile(os.path.join(wp, f)): print f, getSize(f), getUser(f), getGroup(f), md5Sum(f) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Variable Swap
Hello, I understand the use of xor to do a variable swap without a temporary variable: >>> x=2 >>> y=3 >>> y=x^y >>> x=x^y >>> y=x^y >>> x 3 >>> y 2 However, why do I see this? >>> y=x^y >>> y 1 S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Variable Swap
On 1/29/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > Because 2 ^ 3 == 1, right? Are you sure you understand what xor does? It > is a bitwise exclusive or: Yes... at a binary level, it returns true if either input is true, but not both: A B Q 0 0 0 0 1 1 1 0 1 1 1 0 Thus it has the effect of swapping / reversing. For some reason I had not been thinking straight, and had thought we might just see a toggle of the value of the variable, which is, of course, nonsense. So in my example: 2 = 0010 3 = 0011 xor: Q = 0001 Lesson: think first... was just me being blind. > x, y = y, x Doesn't this use temporary variables? > Kent S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] VOT - Similar list for Ruby?
Hello all, I may be about to switch jobs to an environment in which the main utility language is Ruby. I've found this group to be brilliant in the last few years, and wondered if anyone on the list is also a Ruby user, and could recommend a similarly helpful, patient and informative list? Thanks, S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] MapReduce
Hello, I have to give a presentation this week on how the MapReduce (of Google and Hadoop fame) algorithm works. I understand how map() works, and how reduce() works, and having read the google papers, I have an idea of their implementation (which I must say takes certain liberties with FP-derived terminology). As I understand it, we have a "map" function which is passed a set of key/value pairs, which, by applying a user function, produces an intermediate set of key-value pairs. I've come up with an example... return the url and how frequently occurs if the url contains a given word. I'm planning to pass a "map" function an indexed list of urls: >>> stuff [(1, 'http://www.beer.com'), (2, 'http://www.ban-beer.com'), (3, 'http://www.bbc.co.uk'), (4, 'http://www.beer.com'), (5, 'http://.kernel.org')] And I have a map function with reverses the keys and values if the word is present: def myMap(stuff): return [(url, key) for key, url in stuff if "beer" in url] This does as expected: >>> myMap(stuff) [('http://www.beer.com', 1), ('http://www.ban-beer.com', 2), ('http://www.beer.com', 4)] What I want to do is now "group" these urls so that repeated urls have as their "partner" a lsit of indexes. To take a test example of the method I have in mind: def testGrouper(self): """Group occurences of a record together""" test_list = [('fred', 1), ('jim', 2), ('bill', 3), ('jim', 4)] grouped_list = [('fred', 1), ('jim', [2, 4]), ('bill' ,3)] self.assertEqual(myGroup(test_list), grouped_list) I have seen some code which purports to do this, but I thought it was ugly, and it seemed to return a generator object rather than a list. I refactored it for clarity thus: def myGroup(stuff): sorted_list = sorted(stuff) if not sorted_list: return reduce_key, reduce_list = sorted_list[0][0], sorted_list[0][1] for key, url in sorted_list[1:]: if key == reduce_key: reduce_list.append(value) else: yield (remote_key, remote_list) remote_key, remote_list = k, [v] yield(remote_key, remote_list) Does this make sense? I would like a clearer, more attractive way of making the test pass. If this can be done in functional style, even better. Your help is always appreciated! S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MapReduce
On 2/5/07, Steve Nelson <[EMAIL PROTECTED]> wrote: > What I want to do is now "group" these urls so that repeated urls have > as their "partner" a lsit of indexes. To take a test example of the > method I have in mind: > > def testGrouper(self): > """Group occurences of a record together""" > test_list = [('fred', 1), ('jim', 2), ('bill', 3), ('jim', 4)] > grouped_list = [('fred', 1), ('jim', [2, 4]), ('bill' ,3)] > self.assertEqual(myGroup(test_list), grouped_list) > I would like a clearer, more attractive way of > making the test pass. If this can be done in functional style, even > better. I now have: def myGroup(stuff): return [(key, map(lambda item: item[1], list(group))) for key, group in groupby(sorted(stuff), lambda item: item[0] )] Not sure I fully understand how groupby objects work, nor what a sub-iterator is, though. But I more or less understand it. I understand I could use itemgetter() instead of the lambda... Can anyone clarify? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MapReduce
On 2/5/07, Kent Johnson <[EMAIL PROTECTED]> wrote: > You can also do this operation easily with dicts (not tested!): Thank you - code now complete and tests passing. Would appreciate comments / criticisms. I did wonder if I should create a UrlAnalyser Class rather than have hanging methods: #!/usr/bin/python import unittest def myMap(data, search): """Take list of tuples of record number and url accessed and return list of tuples keyed by url with record number as value if search is in the url""" return [(value, key) for key, value in data if search in value] def myGroup(data): """Take list of tuples keyed by url with record number as value, and group together same urls with list of record numbers as value.""" groups = {} for value, index in data: groups.setdefault(value, []).append(index) return sorted(groups.items()) def myReduce(data): """Process list of tuples of url and record number list and return list of url and frequency of occurence.""" return [(value, len(occurences)) for value, occurences in data] class UnitTests(unittest.TestCase): """Do not taunt unit tests.""" def setUp(self): pass def tearDown(self): pass def testMapper(self): """Produce set of intermediate key value pairs, with record content as key and record number as value, if a condition is met.""" test_pairs = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'a'), (5, 'd')] intermediate_list = [('a', 1), ('a', 4)] self.assertEqual(myMap(test_pairs, "a"), intermediate_list) def testGrouper(self): """Group occurences of a record together: [('fred', 1), ('jim', 2), ('bill', 3), ('jim', 4)] -> [(fred, 1), ('jim', [2, 4]), ('bill' ,3)]""" test_list = [('fred', 1), ('jim', 2), ('bill', 3), ('jim', 4)] grouped_list = [('bill', [3]), ('fred', [1]), ('jim', [2, 4])] self.assertEqual(myGroup(test_list), grouped_list) def testReduce(self): """Aggregate results of map and group functions to produce value and frequency.""" test_intermediate = [('bill', [3]), ('fred', [1]), ('jim', [2, 4])] test_summary = [('bill', 1), ('fred', 1), ('jim', 2)] self.assertEqual(myReduce(test_intermediate), test_summary) def doTests(): """Run our test suite""" suite = unittest.makeSuite(UnitTests,'test') runner = unittest.TextTestRunner() result = runner.run(suite) return result def main(): """Main program here""" print "Analysing URL data:\n" url_data = [(1, 'http://www.beer.com'), (2, 'http://www.ban-beer.com'), (3, 'http://www.bbc.co.uk'), (4, 'http://www.beer.com'), (5, 'http://.kernel.org')] print myReduce(myGroup(myMap(url_data, "beer"))) if __name__ == "__main__": result = doTests() if result.wasSuccessful(): main() else: print "Error - check test output." ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python for Sysadmins
Hello chaps, So further to the MapReduce question, it helped greatly, and I got the job, so I'll now be programming Ruby for a living... Before I leave my present job, I've been asked to put together a day's course on Python for Sysadmins. This is mainly to enable them to maintain my code, and give them a flavour for what Python is, how it works, and give them somewhere to begin going on to learn. I'd like some suggestions for a course outline - bear in mind I'll only have a day for the course. The attendees are all sysadmins with a UNIX background, and are reasonably comfortable with shell, but nothing else. Any ideas? S. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor