Re: [Tutor] sqlite3 Python25 parameter binding problemwithUPDATEplease help
aivars [EMAIL PROTECTED] wrote I fully agree with you - I still need to understand basic Python (I actually warned that I am a complete noob) and how it works. And probably I need a couple of goof books. To be honest you don;t need many books on Python, the web resources are adequate. Pesonally I only use about 4 or 5 books asnd they are almost all on specific topics: Tkinter, wxPythn,TurboGears, Networks etc. Python in a Nutshell is the only general book I use as a reference. Maybe I am trying to do the things which are too advanced for my level Just on the edge given your knowledge level. are considered as advanced topics but the reason is that for me I need a real project to work with to learn a programming language. Thats a good thing. But be aware of the dangers - you will learn a lot of bad habits that will cause you problems later. Its liker learning to play a musical instrument by ear just by puicking out the notes of your favourite tune. Unless you learn chords and proper finger positions etc you will never get beyond being a beginner/intermediate level. Same with programming, you do need to knuckle down and learn some of the basic theory and principles if you ever want to progress beyond that stage. Also I am completely self-taught in programming. Not in itself a problem provided you do take the time to actually learn rather than just picking up bits n pieces ad hoc. it now. And I will try not to bother people on this list too often. Its not the frequency of asking thats an issue, we don't ,mind answering questions, thats what we are here for. But do take the time to *understand* the answer, don't just think OK I'll stick [] around values and that'll mayubermake it work Take the time to really understand why [] worked in the first case but actually caused the problem in the second. And if you don't understand, ask for the explanation. 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] Making a dictionary of dictionaries from csv file
Judith Flores [EMAIL PROTECTED] wrote I have been trying to create a dictionary of dictionaries (and more dictionaries) from a csv file. Your code below suffers from sloppiness in the number of [] which makes it hard to know exactly what the problem might be. But in principle what you are doing is fine and should work beyond 2 levels. However I would suggest that it might be esier to use classes instead of dictionaries. Dictionaries are fine for many things but can become a bit tortuous to navigate and maintain. NameDayweighttemp name114537 name135536 name215936 I assume there should be some commas (or other separators) in there? Othewise how do you expect the csv module to separate the data? row={} maindict={} reader=DictReader(f) for row in reader: maindict[row['Name']=row I assume you have a second closing ] after Name? Also you are overwriting the row for each name. So you only store the last entry. So you need to separate the data further. Something like maindict[row[name]] [row[day]] = (row.weight, row,temp) Which would look like {name1: {day1: (w,t), day2 : (w,t), day2: (w,t)}, name2: {day3: (w,t) Which looks a bit like what you want I think? then I can access the weight of a given name like this: wg=int(maindict[['name1']['weight']) I assume thats a single open { before name1? My question is the following: How can I convert the csv to a dictionary that would have the following structure? maindict = { 'name1' : { 'Day' : { 1 : { 'weight' : '45', 'temp' : '37' } , 3 : { 'weight' : '55', 'temp' : '36' } } }, See above 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] Making a dictionary of dictionaries from csv file
On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld [EMAIL PROTECTED] wrote: Also you are overwriting the row for each name. So you only store the last entry. So you need to separate the data further. Something like maindict[row[name]] [row[day]] = (row.weight, row,temp) This will not quite work. It will give KeyErrors because the second-level dicts don't exist in maindict. One way to fix this is to use collections.defaultdict: from collections import defaultdict maindict = defaultdict(dict) Now maindict uses an empty dictionary as its default value. For another level of nesting (the 'Day' level you show in your example) this becomes a bit more complex, see this discussion and the link: http://thread.gmane.org/gmane.comp.python.tutor/46006 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Making a dictionary of dictionaries from csv file
How about this: def recursiveDictFactory(): return defaultdict(recursiveDictFactory) dictOfDictsOfDictsEtc = defaultdict(recursiveDictFactory) --- Richard Roadie Rich Lovely Part of the JNP|UK Famille www.theJNP.com (Sent from my iPod - please allow me a few typos: it's a very small keyboard) On 3 Dec 2008, at 11:43, Kent Johnson [EMAIL PROTECTED] wrote: On Wed, Dec 3, 2008 at 3:34 AM, Alan Gauld [EMAIL PROTECTED] wrote: Also you are overwriting the row for each name. So you only store the last entry. So you need to separate the data further. Something like maindict[row[name]] [row[day]] = (row.weight, row,temp) This will not quite work. It will give KeyErrors because the second-level dicts don't exist in maindict. One way to fix this is to use collections.defaultdict: from collections import defaultdict maindict = defaultdict(dict) Now maindict uses an empty dictionary as its default value. For another level of nesting (the 'Day' level you show in your example) this becomes a bit more complex, see this discussion and the link: http://thread.gmane.org/gmane.comp.python.tutor/46006 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
[Tutor] using windows wide proxy settings
Hello, is there any possibility in python to retrieve the system wide internet connection settings? I would like to access the proxy settings stored in Internet Explorer - Extras - Options - Connection - LAN settings. This would later be used by urllib. My aim is not to bother the user with another place to set the proxy when being behind a corporate firewall. Thanks in advance regards, Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Re: Making a dictionary of dictionaries from csv file]
Judith Flores a écrit : Dear Python community, I have been trying to create a dictionary of dictionaries (and more dictionaries) from a csv file. The csv file contains longitudinal data corresponding to names. The following is just a very (very) simple example of how the data looks: NameDayweighttemp name114537 name135536 name215936 name233436.5 name316637 name338736.8 Apart from the lack of ',', I'm not really sure to understand the logical structure of you data: for instance, can there be the same day for several names? If not, then the problem is simpler as the day uniquely identifiesyour data. Anyway, it seems clear that a (name+day) key is unique for a (weight,temp) data pair. right? So far I have written this: from csv import * f=open('myfile.csv',rt) row={} maindict={} reader=DictReader(f) for row in reader: maindict[row['Name']=row then I can access the weight of a given name like this: wg=int(maindict[['name1']['weight']) My question is the following: How can I convert the csv to a dictionary that would have the following structure? maindict = { 'name1' : { 'Day' : { 1 : { 'weight' : '45', 'temp' : '37' } , 3 : { 'weight' : '55', 'temp' : '36' } } }, 'name2' : { . # and we repeat the same structure for the rest of the names. } From my code above you can of course guess that it doesn't make beyondthe level of name. If I understand well, you can clarify your problem for instance with such a data type (all pseudo code, untested): class Measure(object): def __init__(self,weight,temp): self.weight = weight self.temp = temp This will remove one dict level. You can create a data object x = Measure(weight,temp) which weight and temp attributes can be set, changed, read individually using 'x.weight' and 'x.temp'. Much nicer, I think. At higher level, you are then able to put such measure objects in a simple dict with day keys and Measure values. So that an individual Measure object will actually be accessed as a dict value, e.g.: day_measures = {day:measure, day:measure...} measure = day_measures[day] temp = day_measures[day].temp (=measure.temp) At top-level, you then need a construct to hold the set of name data objects. As they are named, it could be a nesting dictionary of (name:day_measures) pairs. This will be especially efficient if you frequently need accessing the data through the 'name' key. An alternative (which I would chose to avoid nested dicts) is to use the python ability to create attributes which names are known at runtime only, using the setattr() built-in function: setattr(obj,name,val) will give the object 'obj' an attribute which full name will be obj.name, and value will be val. Very nice (*). That way, you can have an overall Data object (chose an appropriate name) with a whole bunch of attributes called by their 'name' and which individual value is a day_measures dict: data name1 : day_measures dict name2 : day_measures dict ... You need to create an attribute for each set of (named) day_measures dict you read from the CSV file. class Data(object): pass data = Data() # new empty object while not end_of_file: identify name read the set of (day:measure) pairs from CSV into dict # create attribute to store the measures into data object setattr(data, name, dict) # done Now, from the outest scope, an individual data item is spelled e.g.: data.name[day].temp (which I find much more legible) Thank you very much, Judith Hope I haven't totally miusunderstood your problem. Salutation, denis (*) The ordinary way to create an attr beeing to write obj.name = value you need to know the name at coding time. The setattr() syntax instead allows to set the name at runtime like a dict key. So that any ordinary object becomes an alternative to dicts. [A major redondance, indeed -- I personly do not really know anymore what the proper use of a dict should be. Even less, as attr are implemented asdict, there shouldn't even be a performance loss. What do you think, list members?] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using windows wide proxy settings
Tim Michelsen [EMAIL PROTECTED] wrote is there any possibility in python to retrieve the system wide internet connection settings? Not as such, but Python does provide the hooks to get it from the OS. I would like to access the proxy settings stored in Internet Explorer - Extras - Options - Connection - LAN settings. Assuming IE implies Windows and not MacOS you can read the registry using the Windows API or alternativey you can use Windows Script Host and access it via the Registry object. In either case you will need the Pythonwin extensions or ctypes. Its also possible to get at most(all?) of what you need via the subprocess module and the Windows command line tools. 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] using windows wide proxy settings
Tim Michelsen [EMAIL PROTECTED] wrote is there any possibility in python to retrieve the system wide internet connection settings? Further to my earlier mail I had a thought and sure enough there is a winreg module that makes accessing the registry much easier direct from Python. Now all you need to do is find the right keys in the registry... and ninternet search shild throw it up or just search for a key definition using regedit.. Batteries included... Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using windows wide proxy settings
On 04/12/2008, Tim Michelsen [EMAIL PROTECTED] wrote: Hello, is there any possibility in python to retrieve the system wide internet connection settings? I would like to access the proxy settings stored in Internet Explorer - Extras - Options - Connection - LAN settings. Install pythonwin, then look here: http://www.microsoft.com/technet/scriptcenter/scripts/python/network/client/list/nwlspy02.mspx?mfr=true General Microsoft Windows python script repository: http://www.microsoft.com/technet/scriptcenter/scripts/python/default.mspx?mfr=true HTH! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using windows wide proxy settings
Tim Michelsen wrote: Hello, is there any possibility in python to retrieve the system wide internet connection settings? I would like to access the proxy settings stored in Internet Explorer - Extras - Options - Connection - LAN settings. I wouldn't believe it if I didn't just see it work this way, but... On windows, proxy settings configured the way you describe above (if no auth is required) are retrieved from the registry and used automatically by urllib(2).urlopen. from the urllib docs, http://www.python.org/doc/2.5.2/lib/module-urllib.html The urlopen() function works transparently with proxies which do not require authentication. In a Unix or Windows environment, set the http_proxy, ftp_proxy or gopher_proxy environment variables to a URL that identifies the proxy server before starting the Python interpreter. For example (the % is the command prompt): ... In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry's Internet Settings section. 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:'} HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting a dictionary on a value in a list.
I am working on a reducer that needs to produce a sorted output of files sorted on their overall bandwidth use. I create a dictionary with the file name as the key (it is always unique) and in the values I am populating a list with the two values of bytes and bytes sent. Each entry looks like {filename:[bytes, bytes_sent]} how would I sort on bytes sent? how would I make this more efficient? 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 = {} 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] # input comes from STDIN for line in sys.stdin: # remove leading and trailing whitespace and split on tab words = line.rstrip().split('\t') file = words[0] bytes = words[3] bytes_sent = int(words[7]) update_dict(file, bytes, bytes_sent) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Graph theory
Hallo all! It's a long time since I last wrote here. I have been thinking about a problem, and I'm wondering what the best approach for a pythonic solution would be. The actual problem is very complex, but the very first step in the solution would be to come up with a simple way of handling graphs. For example, given that: Definition 1: A network is a figure made up of points (vertices) connected by non-intersecting curves (arcs). Definition 2: A vertex is called odd if it has an odd number of arcs leading to it, other wise it is called even. Definition 3: An Euler path is a continuous path that passes through every arc once and only once. Given the following theorems: Theorem 1: If a network has more than two odd vertices, it does not have an Euler path. Theorem 2: If a network has two or zero odd vertices, it has at least one Euler path. In particular, if a network has exactly two odd vertices, then its Euler paths can only start on one of the odd vertices, and end on the other -- (Euler circuit). Which would be the best approach for representing figures such as those found here ( http://mathforum.org/isaac/problems/bridges2.html ) such that one could elaborate a script capable of finding and describing paths in the figure? For example, Euler paths. I realize that it is quite feasable to do this, but would take a lot of coding - vertices and arcs could be represented as instances of respective classes, and so forth. But is there an elegant and simple solution already out there? -- Michele Alzetta ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Graph theory
On 04/12/2008, Michele Alzetta [EMAIL PROTECTED] wrote: I have been thinking about a problem, and I'm wondering what the best approach for a pythonic solution would be. The actual problem is very complex, but the very first step in the solution would be to come up with a simple way of handling graphs. Interestingly, Guido wrote an essay on implementing graphs in python using dictionaries and lists: http://python.org/doc/essays/graphs/ I'm sure if you googled around you will be able to find more full-featured graph libraries (unless, of course, building your own is part of the attraction :-) ). -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting a dictionary on a value in a list.
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] using windows wide proxy settings
On Wed, Dec 3, 2008 at 6:37 PM, Martin Walsh [EMAIL PROTECTED] wrote: 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:'} And, since urllib is a Python module, you can easily look at the source to see where it comes from. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Graph theory
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. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Confused about __setitem__
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? *_# 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 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 module 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