Re: [Tutor] how to print array without adding newline
On Sep 6, 2012, at 9:49 AM, vi...@thepenguin.org wrote: > Thank you for your reply. I understand that it is odd, but my program is > being called from a hubot and returning data to it as well. I have figured > out how to make the changes to get it to output the correct data in the > correct format, but now I am getting a "Premature end of script headers" > error. I have the correct #! line and the output from the command line shows > no errors that would be interfering. Is there a way to make sure it is > showing me all the errors? To increase error logging? > -- > !/usr/bin/env python > import cloudfiles > import random > import sys > import array > > conn = cloudfiles.get_connection('username', 'key') > > containers = conn.get_all_containers() > i=0 > print "Content-type: text/html"; > wholelist=containers[0].list_objects() > random.shuffle(wholelist) > newlist=[] > #newlist=wholelist[:] > try: > #print sys.argv[1] >if "=" in sys.argv[1]: sys.argv[1] = sys.argv[1].rstrip("=") > #print sys.argv[1] >del wholelist[int(sys.argv[1]):] >while i < int(sys.argv[1]): >newlist.append("http://example.com/"+wholelist[i].rstrip()) >i = i+1 > except IndexError, e: >del newlist[5] > except Exception, err: >print 'Caught an exception' > print newlist, > --- > Vicki > Python doesn't know what to do with: !/usr/bin/env python try #!usr/bin/env python so python doesn't see it, only the the shell. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] modulo
On Oct 7, 2012, at 6:49 PM, Esteban Izaguirre wrote: > Hi, I'm following coursera's learn to program: the fundamentals, which > teaches programming basics in python. Our first assignement involves the > modulo operator with a negative divident, and while I've managed to get to > understand it enough for the purposes of the assignement with help from othe > rstudents, I still don't know how the hell it works, I wouldn't know how to > use modulo in another situation if it ever arised. So, i undertand how modulo > works when only positive numbers are used, but how does modulo determine, > that, say -15 % 14 is equal to 13? Or -20 % 100 is 20? I just don't get how > modulo works, all explanations I've found online only seem to be in relation > of how this applies to perl or something, can someone explain it to me? > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor After reading all the other (good) answers, let me try one other way to think about it. First, for positive integers consider 15/12 = 1 and 15%12 = 3. So 12 * (15/12) + 15%12 = 15 and we are back where we started. In order to be able to perform the same operations on a negative dividend, it has to work the way you find puzzling. Consider: -15/12 = -2 and -15%12 = 9, which is the way it has to be in order for 12 * (-15/12) i.e. -24 plus -15%12 i.e. 9 to equal -15. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] managing memory large dictionaries in python
On Oct 16, 2012, at 12:57 PM, Abhishek Pratap wrote: > Hi Guys > > For my problem I need to store 400-800 million 20 characters keys in a > dictionary and do counting. This data structure takes about 60-100 Gb > of RAM. > I am wondering if there are slick ways to map the dictionary to a file > on disk and not store it in memory but still access it as dictionary > object. Speed is not the main concern in this problem and persistence > is not needed as the counting will only be done once on the data. We > want the script to run on smaller memory machines if possible. > > I did think about databases for this but intuitively it looks like a > overkill coz for each key you have to first check whether it is > already present and increase the count by 1 and if not then insert > the key into dbase. > > Just want to take your opinion on this. > > Thanks! > -Abhi > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor With apologies for coming to this late. Just a couple of comments - 1) If the keys were sorted before the counting operation was to take place, the problem could be run in successive batches in as small a foot print as desired. 2) Sorting in limited memory space was a highly developed art back in the early days of IBM mainframe computing (when one had to use tape rather than disk for temp store and 64k, yes "k", was a lot of memory). If you look at Knuth, Volume III: Sorting and Searching you will find several algorithms that would allow the sort to also run in as small a foot print as needed. Possibly not interesting, but just saying… Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] blackJack problem
On Oct 23, 2012, at 11:01 PM, Matthew D wrote: > Thanks Alan Gauld for your help. After taking a little break and coming back > it all seemed to make sense...i understand why there is gonna be some really > weird outputs to the interchanging of the player1.py files...that really > could get interesting LOL...I seemed to have got the program running good > > now i have only a few pretty simple questions, If you have the GetBet in the > player1.py file you have to pass in the total value right?...that is the only > way to get the total into the function? the professor told us straight out > that he doesnt really know what he is doing...he wanted us to put in > (currentBet, previousBet, and win/lose). I think it only makes sense if you > put in the total value in as prevousBet. also he wanted us to pass in > (player_hand, dealer_hand) in as parameters for the HitStand function...why > would we want to total the hand more than once if we already have the total > value? wouldn't we want to just pass in those values instead of the hand > itself? > > Im starting to think im not really learning anything from this class...if > anything its just confusing me and teaching me the wrong way to go about > things > > thanks again for your help :) The best way to learn to program is to program. Doing it in the context of a class has several advantages (even if the prof isn't the sharpest tack in the box)… 1) The class discussion of various approaches to the assignment gives you a range of possible solutions, both good and bad 2) The enforced discipline of generating a solution to the assignment makes you think 3) Learning (almost) any programming language forces you to learn how to reduce a problem to discrete tasks and then steps. Once you've learned ANY language, the next one is easier. That process iterates. 4) Python is an ideal language and environment in which to learn. That is, it's a really good starting place. Just saying… -Bill___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] running multiple concurrent processes
On Nov 3, 2012, at 9:36 PM, Oscar Benjamin wrote: > [byte] > >> >> Bill, I appreciate your comment and have given it much thought, Ramit made >> one much the same the other day. Here lies the potential problem, though it >> might not be one at all, I need to do some experimenting. While I am a fan >> of monolithic programming, I'm wondering if what I'm trying to do would work >> on, say an old netbook. That might be a requirement. I'd prefer it not to >> be, but it might. Also, thanks for reminding my addled old brain that event >> driven is called interrupts. I knew that at one point, but seem to have >> flushed it somehow. > > Who's Bill? Alan was referring to Twisted that is an event driven > framework. Event driven or asynchronous processing is a third option > (after threads or processes). The Twisted library is also capable of > launching threads and (I think) processes for you so it could > accommodate for all of the possibilities you want in one framework. > This Bill, and with apologies, - after some debate with myself I sent my comment to Richard without copying the list, since it was not relevant to either Threading or Multiprocessing - I simply pointed out that modern processors are so fast that he can probably accomplish what he wants to do by breaking his main subtasks into semi-atomic chunks and calling them round-robin. It would require keeping careful track of some shared globals, but might be a MUCH simpler initial approach. Those sub tasks could be coalesced and converted to multiprocessing later after he gets going with the robot. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] serial to parallel
On Nov 5, 2012, at 8:44 AM, Dave Angel wrote: > On 11/05/2012 06:53 AM, Bala subramanian wrote: >>> [Huge byte] >>> Thanks in advance, >>> Bala >> >> > > Before you spend too much energy on this, I'd suggest that you'll > probably see a substantial slowdown trying to write the two files in > parallel. Unless the calculations are extensive that actually format > the data for writing. > > On the other hand, if the calculations dominate the problem, then you > probably want to do multiprocessing to get them to happen in parallel. > See the recent thread "using multiprocessing efficiently to process > large data file" > > Just be sure and do some measuring before spending substantial energy > optimizing. > > -- > > DaveA > Assuming, after you take Dave's advice, that you still want to try parallel processing. Take a quick look at: http://docs.python.org/2/library/multiprocessing.html?highlight=multiprocessing#multiprocessing and in particular at section 16.6.1.5 on using a pool of workers. This might provide a simple clean way for you to hand off the work. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] LCM revisited + OOP
On Nov 27, 2012, at 8:13 PM, Ray Jones wrote: > > Part I > I am a good way through MIT's Introduction to Computer Science and > Programming as offered through edX. I'm not certain I'm going to pass > the course this first time through, the major hangup being the > understanding of OOP. > > Part II > When the LCM thread came through, I wrote some quick code do the > computation. I began by creating a function that generates a list of > primes and reversing the list high to low. Then I created a recursive > function to find the prime factors for each of the numbers and > manipulated the results to the LCM. > > What do these two things have to do with each other? I can see some of > the vast power of OOP. Unfortunately I can't see when or why I would > create objects in my everyday programming. So I went back to look at my > LCM code to see if there a way to create better code by using OOP rather > than the functions I used. I can't see a better way. I know I can use > OOP - I just don't see how setting it up with a couple of classes would > make it simpler or better than the non-OOP way. > > I'm not including code because I'm more interested in understanding the > whys and wherefores of using OOP vs. not using it than in receiving help > with my specific code. (And I'm not a particularly fine coder to begin > with... ;) ). > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Ray, I'll be VERY interested to see how others answer this question, because I'm still working through it myself. I started programming back in the days of FORTRAN IV, writing data analysis programs in support of my own work. I've moved forward since and have spent the last two years learning Python. All my initial development in Python was very much procedural and I've been quite happy to leverage its power. More recently, as I decided to start developing simple GUI front ends for some of my stuff, I took another long hard look at the sections in my various books on OOP, and have finally (I hope) started to see daylight. To be overly simplistic, OOP really shows its worth when one is creating classes that _do_ represent objects or abstractions of objects (and of course, that's why GUI widgets lend themselves to nicely to OOP). To give a slightly more concrete example, I develop a lot of network utilities, and it suddenly became obvious that a "network" class would be a very usefu l thing. That class allows me to define a network object in terms of a remote end-point, and then perform all sorts of actions (tests) on the "network" between here and there. Behind the scenes, that class can share data among its internal modules and functions, but each instance of the class is completely unique and the fact that the collection of "hops" that make up that particular "network" is also unique can be safely hidden (or exposed) as needed. So, to summarize - for a lot of purely procedural programming - there may well NOT be any advantage to OOP, but when you start developing code that needs to mimic/represent or model complicated "objects" then OOP starts to shine. Done right it simplifies the interface to the object, simplifies future debugging when objects are combined, and provides structure that helps you get the "logic" right. Good luck that class, Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Learning Python through automating web application testing.
On Dec 7, 2012, at 5:46 PM, marcusw4...@hotmail.co.uk wrote: > Hello all, > > I'm new to posting on mailing lists so hopefully I've picked the correct one > for my question(s). > > A little about my programming experience first. > [byte] > Because of this fear I've never admitted I've got a ready made project just > waiting for me to tackle... > > Until now! > > I would like to learn to automate the testing of a http(s) web > site/applications but feel slightly overwhelmed by this task so would like to > ask for some initial guidance. > Wow (!) welcome the wonderful world of Python. I have several comments - but let me start (with apologies) by encouraging you to think about another initial project. Others may well disagree with me (and I'd welcome that), but from my point of view, your choice (although by starting small can probably be done with relatively little of YOUR python code to call libraries), does require a deeper understanding of several parallel (pun intended) aspects of programming for web, ssh, sockets, python, your OS, and your chosen python libraries than you may realize. Just to mention one - simulation of pounding on the test site by several users will require multi-processing from a pool of parallel jobs. The standard Python library has the tools to handle this, but it isn't an easy subject for someone new to programming to get your mental arms wrapped around. I'm not familiar with the Chun book, so my worries may be completely misplaced (and if so, I apologize), but if you aren't really comfortable yet with OOP, and class definitions that start with '__init__(self…)' - this project isn't a good place to start. You can get some feel for your level of achievement by looking at urllib, urllib2, and httplib in the python standard documentation (and maybe looking at beautifulsoup (either BS3 or BS4). Then, if you want to go ahead - the kind of questions you will be coming up with are probably better addressed on the more general python-l...@python.org discussion group. (There are lots of folks here to read both, so you won't need to cross post.) > These are some of questions that I have. > > How do I go about this? > > Where do I start? > > There's just so much out there to help with learning Python I'm experiencing > information overload! > > How do I stop myself from trying to run before I can walk? > My suggestion would be to pick a project that yields something useful around your house or apartment (flat?) or a single-function utility that would be useful to you (maybe some sort of backup utility that is customized for the way you work). > In a perfect world a step by step guide, in automating web tests, using > Python is what I'm after but failing that(!) which sites/forums/mailing lists > are of particular interest to someone who would like to learn Python > programming initially through automating web tests? (By web tests, to begin > with, I mean automated regression testing of a site by multiple users) > When you get to the point of actually starting to tackle this - there is one more question you need to answer before you start. Are you trying to check for correct functionality at all branch points when users respond appropriately, or are you wanting to check for possible ways the site can be crashed by users typing garbage or attempting to attack the site? The latter is a MUCH more complex project. Sorry to sound so discouraging (and as I said, others may have much more encouraging suggestions for ways to approach this), -Bill > At work we use Selenium and Java so I'm aware that Selenium comes with a > Python driver. Would that be a good place to start? > > Apologies if I'm not supposed to ask more than one question per mail but > these are all closely related and could be thought of as "newbie struggling > to see the wood/forest for the trees!" > > Many thanks for your help > > Regards > Marcus > > > ___ > 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
Re: [Tutor] Python Help
On Jan 23, 2013, at 4:37 PM, Grady Trexler wrote: > > Below is my code. I am using python 2.4 It tells me I have a syntax error. > Please help! (I think the first twenty lines have what you would be looking > for. After that it just repeats itself.) > #scenario maker > #created by: Grady Trexler > #started on 1/3/13 > #last update: 1/3/13 > > def rungame() > guyone = raw_input("Please enter a name:") > [megabyte] > print "%s: EVERYTHING" % (guytwo) > print "%s pushed passed %s and ran away. The two never saw > eachother again." % (guytwo, guyone) > rungame() > -- > --T-rexmix > Check out my website- > www.thegradypage.weebly.com > It has new blog posts all the time! > Fight against Gandalf. Like a Balrog. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor In the future, please also show us the error and the traceback so we don't have to read through your whole code looking for lints. File "text.py", line 1 def rungame() ^ SyntaxError: invalid syntax You forgot the colon after the closing ) in the def statement. -Bill ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor