Re: [Tutor] using windows wide proxy settings
Hello all, thanks a lot for your answers. > > The urllib and urllib2 modules also provide a helper function for > > retrieving proxy info... > > > import urllib2 > urllib2.getproxies() > > {'ftp': 'ftp://10.0.0.100:', 'http': 'http://10.0.0.100:'} This seems to be the fastest method. Very neat. Since we use pac scripts I refined my search. Here is another module tailored to this: http://code.google.com/p/pacparser There is a python example at the wiki: http://code.google.com/p/pacparser/wiki/PacFilesInPythonWebclients It seems that the script can even test if connection is available. I was looking for this earlier. KInd regards, Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confused about __setitem__
"Jim Hodgen" <[EMAIL PROTECTED]> wrote Why can't I fill the dictionary-as-a-matrix with an instance of class Square? You can but you are not trying to do that in your code. You are assigning Square to the class that contains the dictionary, not the dictionary itself. class Square: mined = True # False if empty, True if mined, no other marked = 0# False if 0, marked-as-mined if 1, displaystate = 0 # covered if 0, marked if 1, exposed if 2, minedneighbors = 0 # number of mined neighboring squares, max legit These are all class level variables so they will be shared if you create more than one instance of the class. That's probably not what you want?. You should initialise them inside an __init__ method if you want each square to have its own values. class Baseboard: boardsquares = {} # dictionary used to emulate an addressable 2 boardsquares is the dictionary not Baseboard def initializeboard(xdim, ydim, minenum): gameboard = Baseboard() for yct in range(ydim): for xct in range(xdim): gameboard[(xct,yct)] = Square() So this needs to be gameboard.boardsquares[(xct,yct)] = Square() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confused about __setitem__
On Thu, Dec 4, 2008 at 3:54 AM, Alan Gauld <[EMAIL PROTECTED]> wrote: > These are all class level variables so they will be shared if you > create more than one instance of the class. That's probably > not what you want?. You should initialise them inside an __init__ > method if you want each square to have its own values. And likewise for the board variables. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] To print docstrings
Hello friends. I am new to programing.I am learning Python. I failed in my attempts to retrive doc strings of methods in a module. """ for x in dir(logging): print x,x.__doc__ = for x in dir(collections): print x,collections.x.__doc__ == >>> def dd(o): zx=dir (o) for x in zx: ax=o+x print ax.__doc__ >>> dd(collections) def dd(o): zx=dir (o) for x in zx: ax=str(o)+x print ax.__doc__ >>> >>> dd(collections) """ Above snippets of code generates str.__doc__ for every iteration. Please someone tell me how I can retrive all the docstrings ina module thanks in advance Prasad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To print docstrings
prasad rao wrote: Hello friends. I am new to programing.I am learning Python. I failed in my attempts to retrive doc strings of methods in a module. """ for x in dir(logging): print x,x.__doc__ = for x in dir(collections): print x,collections.x.__doc__ == I am not sure what you are aiming for. If you just want to collect them for reading, http://docs.python.org/library/ is much easier. If you want to produce documentation of your own modules, you may want to take a look at pydoc or epydoc. If you want to collect the doc strings just for the sake of it, add them in a list instead of printing: docs = [] for x in dir(collections): docs.append(x.__doc__) or in one line: docs = [x.__doc__ for x in dir(collections)] if you want to make a single (possibly long string of text from that list, join the strings together as in text = "\n".join(docs) or all in one line (with a generator to suppress creation of the intermediate list): text = "\n".join(x.__doc__ for x in dir(collections)) def dd(o): zx=dir (o) for x in zx: ax=o+x print ax.__doc__ I have no idea what you are doing with 'ax' here. You seem to be adding a module and one of its functions and assign the result to ax, an operation I don't understand. If it is important for you, could you please provide some explanation of what you trying to do here? Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To print docstrings
On Thu, Dec 4, 2008 at 9:46 AM, prasad rao <[EMAIL PROTECTED]> wrote: > > Hello friends. > I am new to programing.I am learning Python. > I failed in my attempts to retrive doc strings of methods in > a module. > """ > for x in dir(logging): > print x,x.__doc__ Here x is a string, that is why you print out str.__doc__. Try this: print x, getattr(logging, x).__doc__ or, just help(logging) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To print docstrings
On Thu, Dec 4, 2008 at 10:10 AM, A.T.Hofkamp <[EMAIL PROTECTED]> wrote: > docs = [] > for x in dir(collections): > docs.append(x.__doc__) This will not work, see my previous email for solution. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confused about __setitem__
Jim Hodgen wrote: I am learning Python with a minesweeper-engine project. Future activities turn my attention to exploring the use of dictionaries as sparse matrices, hence the representation of the gameboard. Why can't I fill the dictionary-as-a-matrix with an instance of class Square? Why do I need a custom method to make the assignment? What am I not understanding? You don't have a sparse matrix here. There is a dictionary entry for every square. You might as well use a nested list or an array from one of the array modules. If you want a Baseboard instance to directly accept keyed assignment use: class Basebaord(dict) and drop boardsquares. *_# basics module (mineswobj1)contents..._* class Square: mined = True # False if empty, True if mined, no other values allowed marked = 0# False if 0, marked-as-mined if 1, marked-as-unknown if 2, no other values allowed displaystate = 0 # covered if 0, marked if 1, exposed if 2, selected if 3, no other values allowed marked and display state are in conflict and overlap. Consider instead: selected = False displaystate is one of covered-blank, covered-marked, mined, exposed minedneighbors = 0 # number of mined neighboring squares, max legit value = 8 class Baseboard: xdimension = 0 # x-dimension of the gameboard, integer value greater than 0 ydimension = 0 # y-dimension of the gameboard, integer value greater than 0 minenumber = 0# number of mines on the board for play, value from 0 to (xdimension * ydimension) state = 0 # playable if 0, not-playable-exploded if 1, not-playable-solved if 2 boardsquares = {} # dictionary used to emulate an addressable 2 dimensional grid where an instance of class Square # is inserted with a key consisting of a tuple describing its zero-based address in the grid def initializeboard(xdim, ydim, minenum): gameboard = Baseboard() for yct in range(ydim): for xct in range(xdim): gameboard[(xct,yct)] = Square() print 'gameboard[(',xct,yct, ')] =', gameboard[(xct,yct)] *_# driver module contents_* import basics mineswobj1.initializeboard(3, 4, 5) _*#error message*_ Traceback (most recent call last): File "C:\jmh\Python\minedriver0.py", line 6, in mineswobj1.initializeboard(3, 4, 5) File "C:\jmh\Python\mineswobj1.py", line 26, in initializeboard gameboard[(xct,yct)] = Square() AttributeError: Baseboard instance has no attribute '__setitem__' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Optimise Code
* Richard Lovely <[EMAIL PROTECTED]> [081123 11:35]: > I've tried a the sieve of erath-whatever as in test_generator, > implemented using itertools functions, but it hit max recusion depth > somewhere before 1000 primes, and I'm after millions of primes. I found an old implementation for some exercise of the "sieve of Eratosthenes" in my backups and its not recursive but iterative: #!/usr/bin/env python l=1*[1] for i in range(2,len(l)): if l[i] == 1: print i for j in range(i+1,len(l)): if j%i == 0: l[j]=0 Yes, it is pretty slow for a range of a million, but it speeds up a little after half an hour or so :-) You might want to have a look at the bsd-games package, which includes a program named primes. It prints out primes at a reasonable speed - up to 4294967295 (the default). The manpage says: " BUGS primes won't get you a world record. " If you can get it to work faster in python? I doubt that. primes uses IIRC atkin-sieve: "http://cr.yp.to/papers/primesieves.pdf"; -- You have an ambitious nature and may make a name for yourself. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help Optimise Code
On Thu, Dec 4, 2008 at 11:38 AM, Jörg Wölke <[EMAIL PROTECTED]> wrote: > #!/usr/bin/env python > > l=1*[1] > for i in range(2,len(l)): >if l[i] == 1: > print i > for j in range(i+1,len(l)): > if j%i == 0: for j in range(2*i, len(l), i): would be much faster, avoiding all the division. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To print docstrings
"prasad rao" <[EMAIL PROTECTED]> wrote Please someone tell me how I can retrive all the docstrings in a module Unless its just as an exercise use help() Its much easier! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting a dictionary on a value in a list.
Thanks for the help I think I got it. As far as lines go I believe it will be processing hundreds of thousands of lines if not a million or more lines per run. I haven't gotten to do a full run but it has been running acceptably fast on my test files. I ended up putting it into a main function and adding: if __name__ == "__main__": main() On Dec 3, 2008, at 5:42 PM, Kent Johnson wrote: On Wed, Dec 3, 2008 at 7:58 PM, Lawrence Wickline <[EMAIL PROTECTED]> wrote: how would I sort on bytes sent? You can't actually sort a dictionary; what you can do is sort the list of items. In this case each item will look be a tuple (filename, (bytes, bytes_sent)) and dict.items() will be a list of such tuples. The best way to sort a list is to make a key function that extracts a key from a list item, then pass that to the list sort() method. In your case, you want to extract the second element of the second element, so you could use the function def make_key(item): return item[1][1] Then you can make a sorted list with sorted(dict.items(), key=make_key) how would I make this more efficient? It looks pretty good to me. A few minor notes below. code: # Expect as input: # URI, 1,return_code,bytes,referer,ip,time_taken,bytes_sent,ref_dom # index 0 1 2 3 45 6 78 import sys dict = {} Don't use dict as the name of a variable, it shadows the built-in dict() function. def update_dict(filename, bytes, bytes_sent): # Build and update our dictionary adding total bytes sent. if dict.has_key(filename): bytes_sent += dict[filename][1] dict[filename] = [bytes, bytes_sent] else: dict[filename] = [bytes, bytes_sent] If you really want to squeeze every bit of speed, filename in dict is probably faster than dict.has_key(filename) and you might try also using a try / catch block instead of has_key(). You could also try passing dict as a parameter, that might be faster than having it as a global. None of these will matter unless you have many thousand lines of input. How many lines do you have? How long does it take to process? # input comes from STDIN for line in sys.stdin: # remove leading and trailing whitespace and split on tab words = line.rstrip().split('\t') rstrip() removes only trailing white space. It is not needed since you don't use the last field anyway. file = words[0] bytes = words[3] bytes_sent = int(words[7]) update_dict(file, bytes, bytes_sent) If you put all this into a function it will run a little faster. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Graph theory
On Wed, Dec 03, 2008 at 08:49:12PM -0500, Kent Johnson wrote: > On Wed, Dec 3, 2008 at 8:16 PM, John Fouhy <[EMAIL PROTECTED]> wrote: > > > Interestingly, Guido wrote an essay on implementing graphs in python > > using dictionaries and lists: > > > > http://python.org/doc/essays/graphs/ > > Also googling "python graph" finds many alternatives. And, if you are also interested in the visual display of graphs, then look for graphviz and python-pygraphviz. - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Graph theory
On Thu, Dec 04, 2008 at 01:26:02PM -0800, Dave Kuhlman wrote: > On Wed, Dec 03, 2008 at 08:49:12PM -0500, Kent Johnson wrote: > > On Wed, Dec 3, 2008 at 8:16 PM, John Fouhy <[EMAIL PROTECTED]> wrote: > > > > > Interestingly, Guido wrote an essay on implementing graphs in python > > > using dictionaries and lists: > > > > > > http://python.org/doc/essays/graphs/ > > > > Also googling "python graph" finds many alternatives. > > And, if you are also interested in the visual display of graphs, > then look for graphviz and python-pygraphviz. > Networkx works fine for me. Try it! http://networkx.lanl.gov/ Santiago -- Santiago Pay� i Miralta 607 070 992 mailto:[EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] rrdtool examples.
Is anyone on here using the python-rrdtool module for graphing and analysis? If so, do you have some sample scripts you could show me. There doesn't seem to be a lot out there as far as real world python examples. Thanks, JJ Disclaimer: The information contained in this transmission, including any attachments, may contain confidential information of Panasonic Avionics Corporation. This transmission is intended only for the use of the addressee(s) listed above. Unauthorized review, dissemination or other use of the information contained in this transmission is strictly prohibited. If you have received this transmission in error or have reason to believe you are not authorized to receive it, please notify the sender by return email and promptly delete the transmission. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor