Re: [Tutor] sqlite3 Python25 parameter binding problem withUPDATEplease help
Thanks, Alan, for your advice. 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. I once told you that I consider your website an excellent one and I am going there from time to time and will do it again. Maybe I am trying to do the things which are too advanced for my level of python knowledge - databases, web development, which I understand 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. That was the case also with VBA (visual basic for applications) - learning for the sake of learning almost never worked with me. It does not mean I am an expert in VBA - far from it! But I had this project working with MS Excel VBA+ MS SQL Server Express 2005 T-SQL - an automated financial accounting consolidated reporting application. I am pumping data from two remote Interbase/Firebird databases, putting it in a local MS SQL Server DB, processing with T-SQL and doing the reporting with Excel. Now I am working to do the same with Python and SQLite at the same time learning on my way them both. And if you work on a real thing there are many bits and pieces which are coming up - and its not like this 'spam' and 'egg' thing all the time. Also I am completely self-taught in programming. I see what was my problem in particular in this case and I understand it now. And I will try not to bother people on this list too often. Or If I do please feel free tell me to [EMAIL PROTECTED] off and do some more reading! Again, thank you Alan for the help! Aivars 2008/12/3 Alan Gauld <[EMAIL PROTECTED]>: > "aivars" <[EMAIL PROTECTED]> wrote > >> Finally I managed to get it working! >> >> I had to remove [year] braces from the second argument - year. > > You really need to do some reading on basicv Python before > you go any further, otherwise you will never understand what > you are doing. > > That is what I told you was wrong in the first place - you can't > pass a list of values (even of one value) into a database field. > But you obviously didn't realize that putting square brackets > (not braces which are {} and signify a dictionary) indicates > a list. > >> reason I used [] was the posts we exchanged recently (7th of November) >> about CGI script where it was recommended by Kent that I put [] around >> string argument: > > But that was an entirely different scenario, again you need to do some > basic level reading so that you understand what we are telling you. > Blindly using "poke and hope" techniques leads to an excercise > in frustration for you and us both. > >> The second argument to execute() is a *sequence* of parameter values. >> A string is a sequence of characters, so by passing a plain string you >> are saying that each character of the string is a parameter to the >> SQL. >> >> Try adding braces around the parameter to make list: >> cur.execute("insert into test (name) values (?)", [sPath]) > > Notice, Kent told you to "make a list" by putting[] around > your string. But in that case the arument was looking for a sequence > (eg list) of values. In your case you were trying to insert a list of values > into a single field which you can't do in normal SQL > >> Now the year argument is string so I thought I should do the same >> again but it is not working. > > Yes but it is a single string so you need to pass a single string. > In the previous case the SQL was looking for a list of arguments > but you passed a single string so the execute interpreted that > as a list of letters. > > You must ensure that the arguments in the execute match > what the SQL is expecting. And that usually means both matching > what you defined the database to be and the nature of the operation. > >> The reason is of course is that I am new to python and there are so >> many things to learn so the question really is why in this case there >> were no [] or () necessary? > > See above, but you really need to take the time out to understand > the basics of Python. > > Try reading my tutorial, in particular the "raw materials" topic > which covers the different Python data types. Then try my database > topic near the end. But ideally just take a few hours - literally an > afternoon will do - to go through any beginners tutorial. They will > all cover these elementary issues. > > > Perhaps but I think it is the list parameter that it doesn't like. > Unless I misunderstand the syntax. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld > > > Alan G. > > ___ > 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] Making a dictionary of dictionaries from csv file
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 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 beyond the >level of name. Thank you very much, Judith ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary looping problem
"Jeremiah Jester" <[EMAIL PROTECTED]> wrote I've created a function for each dictionary. However, when i print out the dictionary I don't get all the items. Any ideas? You should only get a very small number indeed! for filename in names: hash=os.system("md5 "+ filename) dict[hash] = filename os.system only returns the exit code of the command not the output of the command. In most cases that will be 0 to indicate no errors. You need to use the subprocess module (or any of the older alternatives such as popen) to fetch the actual output of the command from stdout and assign it to hash. But better still use one of the python modules to do the hashing instead of calling the OS! I think the hashlib module's md5 function does what you want. -- 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] sqlite3 Python25 parameter binding problem withUPDATEplease help
"aivars" <[EMAIL PROTECTED]> wrote Finally I managed to get it working! I had to remove [year] braces from the second argument - year. You really need to do some reading on basicv Python before you go any further, otherwise you will never understand what you are doing. That is what I told you was wrong in the first place - you can't pass a list of values (even of one value) into a database field. But you obviously didn't realize that putting square brackets (not braces which are {} and signify a dictionary) indicates a list. reason I used [] was the posts we exchanged recently (7th of November) about CGI script where it was recommended by Kent that I put [] around string argument: But that was an entirely different scenario, again you need to do some basic level reading so that you understand what we are telling you. Blindly using "poke and hope" techniques leads to an excercise in frustration for you and us both. The second argument to execute() is a *sequence* of parameter values. A string is a sequence of characters, so by passing a plain string you are saying that each character of the string is a parameter to the SQL. Try adding braces around the parameter to make list: cur.execute("insert into test (name) values (?)", [sPath]) Notice, Kent told you to "make a list" by putting[] around your string. But in that case the arument was looking for a sequence (eg list) of values. In your case you were trying to insert a list of values into a single field which you can't do in normal SQL Now the year argument is string so I thought I should do the same again but it is not working. Yes but it is a single string so you need to pass a single string. In the previous case the SQL was looking for a list of arguments but you passed a single string so the execute interpreted that as a list of letters. You must ensure that the arguments in the execute match what the SQL is expecting. And that usually means both matching what you defined the database to be and the nature of the operation. The reason is of course is that I am new to python and there are so many things to learn so the question really is why in this case there were no [] or () necessary? See above, but you really need to take the time out to understand the basics of Python. Try reading my tutorial, in particular the "raw materials" topic which covers the different Python data types. Then try my database topic near the end. But ideally just take a few hours - literally an afternoon will do - to go through any beginners tutorial. They will all cover these elementary issues. Perhaps but I think it is the list parameter that it doesn't like. Unless I misunderstand the syntax. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dictionary looping problem
Thanks for clearing this up for me. On Tue, 2008-12-02 at 13:25 -0800, Steve Willoughby wrote: > On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > > Hello, > > > > I'm trying to gather a list of files and md5 hash them to do a > checksum. > > I've created a function for each dictionary. However, when i print > out > > the dictionary I don't get all the items. Any ideas? > > Yep. Don't use os.system() there. > > 1. you're running the "md5" program externally when you don't need to, >since Python has the ability to compute md5 checksums on its own, >which you already know because you imported that module at the top >of your script (and then didn't use). > > 2. The return value from os.system() is NOT the hash, so what you're >storing in the dictionary is not going to be that unique, and so >each call which yields the same return value (0, usually) will >overwrite that element in the dict. > > -- > Steve Willoughby| Using billion-dollar satellites > [EMAIL PROTECTED] | to hunt for Tupperware. > > > 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
Re: [Tutor] dictionary looping problem
On Tue, Dec 02, 2008 at 01:08:09PM -0800, Jeremiah Jester wrote: > Hello, > > I'm trying to gather a list of files and md5 hash them to do a checksum. > I've created a function for each dictionary. However, when i print out > the dictionary I don't get all the items. Any ideas? Yep. Don't use os.system() there. 1. you're running the "md5" program externally when you don't need to, since Python has the ability to compute md5 checksums on its own, which you already know because you imported that module at the top of your script (and then didn't use). 2. The return value from os.system() is NOT the hash, so what you're storing in the dictionary is not going to be that unique, and so each call which yields the same return value (0, usually) will overwrite that element in the dict. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] dictionary looping problem
Hello, I'm trying to gather a list of files and md5 hash them to do a checksum. I've created a function for each dictionary. However, when i print out the dictionary I don't get all the items. Any ideas? Thanks< JJ CODE: #!/usr/bin/python import os import md5 import sys source={} target={} source_path="/path/source" target_path="/path/target" def gather_files(dir,dict): os.chdir(dir) names=os.listdir(dir) for filename in names: hash=os.system("md5 "+ filename) dict[hash] = filename print dict gather_files(source_path,source) gather_files(target_path,target) OUTPUT: # python new.py b481bb58b296a62d744d3006d0156f61 1 e07910a06a086c83ba41827aa00b26ed 3 749a92a5ba327db2f2711b1a8d3cc0ab 2 {0: '2'} b481bb58b296a62d744d3006d0156f61 1 e6139f93eb47c9fe7eb7fc5ddb586511 3 7ce1a3a4baf75dea4397e36c97e1fc0b 2 {0: '2'} 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
Re: [Tutor] importing images and sound
On Tue, Dec 2, 2008 at 2:02 PM, Steve Willoughby <[EMAIL PROTECTED]> wrote: > > That depends on what GUI toolkit you're using. If you're > going to be doing a lot with sound and images, particularly > video clips, you might want to look at pymedia or pygame Pyglet is another option. -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] importing images and sound
On Tue, Dec 02, 2008 at 02:56:33PM -0500, Daniel J Kramer wrote: > Hi > > I am writing a program based around a pub quiz. I have been able to write > the program so it runs through a series of questions and gives users a score > at the end. > > I need to import a specific font, images and sound. Does anyone know a good > place to look for tutorials for this? At the moment, I do not know where to > begin That depends on what GUI toolkit you're using. If you're going to be doing a lot with sound and images, particularly video clips, you might want to look at pymedia or pygame. -- Steve Willoughby| Using billion-dollar satellites [EMAIL PROTECTED] | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] importing images and sound
Hi I am writing a program based around a pub quiz. I have been able to write the program so it runs through a series of questions and gives users a score at the end. I need to import a specific font, images and sound. Does anyone know a good place to look for tutorials for this? At the moment, I do not know where to begin thank you -- Daniel J Kramer Constant Fables 249 12th st #3 Brooklyn, NY 11215 (h) 347 223 4571 (m) 646 427 7430 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite3 Python25 parameter binding problem with UPDATEplease help
Hello again, Finally I managed to get it working! I had to remove [year] braces from the second argument - year. The reason I used [] was the posts we exchanged recently (7th of November) about CGI script where it was recommended by Kent that I put [] around string argument: Quote The second argument to execute() is a *sequence* of parameter values. A string is a sequence of characters, so by passing a plain string you are saying that each character of the string is a parameter to the SQL. Try adding braces around the parameter to make list: cur.execute("insert into test (name) values (?)", [sPath]) Kent Unquote Now the year argument is string so I thought I should do the same again but it is not working. The reason is of course is that I am new to python and there are so many things to learn so the question really is why in this case there were no [] or () necessary? Thanks in advance Aivars 2008/12/2 aivars <[EMAIL PROTECTED]>: > Interestingly, when simply testing like this: > > import sqlite3, sys > sPATH=r'e:\pythonexamples\aivars2.db' > oCon=sqlite3.connect(sPATH) > > cur=oCon.cursor() > > oCon.execute("""UPDATE rezerve SET latusaldo = ? WHERE gads = ? > """,(6000.0,'2006')) > > oCon.commit() > > > it works. Therefore I am stuck since it looks like there is something > wrong in below function. > > Thanks, > > Aivars > > > 2008/12/2 aivars <[EMAIL PROTECTED]>: >> Alan, >> Thanks. >> >> Ok I should have been more precise and give more code >> There is what I do: >> >> def starpiba(year, month, day): >>datumsno=str(year+'-01-01') #beginning of year date >>datumsuz=str(year+'-'+month+'-'+day) #period end date >> >>result = (atlikumiBeiguKurss(year, month, day)- >> atlikumiDienasKurss(year, month, day)) >>##print result >>oCon=sqlite3.connect(sPATH) >>if result<=0: >>print abs(result) >>oCon.execute("UPDATE rezerve SET latusaldo =? where gads >> =?;",(result, [year])) >>oCon.commit() >>else: >>##print 'aivars' >>oCon.execute("UPDATE rezerve SET latusaldo =? where gads >> =?;",(result, [year])) >> >> Please bear in mind I am a noob in Python and I write spaggeti code. >> >> There is a table in my sqlite database called rezerve which shows the >> breakdown of a consolidated reserves item in a consolidated balance >> sheet report (if that says anything to you) by years. For the previous >> years the numbers does not change since they are already reported but >> for the current year it may change month by month until reported. >> Therefore, I wanted to update the numbers for the current year. >> >> The above python function works OK. >> >> Thanks >> >> Aivars >> >> >> 2008/12/2 Alan Gauld <[EMAIL PROTECTED]>: >>> >>> "aivars" <[EMAIL PROTECTED]> wrote >>> oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, [year])) oCon.commit() it throws me the error: sqlite3.InterfaceError: error binding parameter 1 probably unsupported type >>> >>> I assume its the [year] value it objects to. >>> I'm not sure what you expect SQLite to do with a list as a value, it does >>> not support a list type field. >>> All works OK when using INSERT with the same parameters. >>> >>> Are you sure? You can insert a list into a field? >>> Maybe I should as this question on sqlite list? >>> >>> Perhaps but I think it is the list parameter that it doesn't like. >>> Unless I misunderstand the syntax. >>> >>> -- >>> 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 >>> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite3 Python25 parameter binding problem with UPDATEplease help
Interestingly, when simply testing like this: import sqlite3, sys sPATH=r'e:\pythonexamples\aivars2.db' oCon=sqlite3.connect(sPATH) cur=oCon.cursor() oCon.execute("""UPDATE rezerve SET latusaldo = ? WHERE gads = ? """,(6000.0,'2006')) oCon.commit() it works. Therefore I am stuck since it looks like there is something wrong in below function. Thanks, Aivars 2008/12/2 aivars <[EMAIL PROTECTED]>: > Alan, > Thanks. > > Ok I should have been more precise and give more code > There is what I do: > > def starpiba(year, month, day): >datumsno=str(year+'-01-01') #beginning of year date >datumsuz=str(year+'-'+month+'-'+day) #period end date > >result = (atlikumiBeiguKurss(year, month, day)- > atlikumiDienasKurss(year, month, day)) >##print result >oCon=sqlite3.connect(sPATH) >if result<=0: >print abs(result) >oCon.execute("UPDATE rezerve SET latusaldo =? where gads > =?;",(result, [year])) >oCon.commit() >else: >##print 'aivars' >oCon.execute("UPDATE rezerve SET latusaldo =? where gads > =?;",(result, [year])) > > Please bear in mind I am a noob in Python and I write spaggeti code. > > There is a table in my sqlite database called rezerve which shows the > breakdown of a consolidated reserves item in a consolidated balance > sheet report (if that says anything to you) by years. For the previous > years the numbers does not change since they are already reported but > for the current year it may change month by month until reported. > Therefore, I wanted to update the numbers for the current year. > > The above python function works OK. > > Thanks > > Aivars > > > 2008/12/2 Alan Gauld <[EMAIL PROTECTED]>: >> >> "aivars" <[EMAIL PROTECTED]> wrote >> >>> oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, >>> [year])) >>> oCon.commit() >>> >>> it throws me the error: >>> sqlite3.InterfaceError: error binding parameter 1 probably unsupported >>> type >> >> I assume its the [year] value it objects to. >> I'm not sure what you expect SQLite to do with a list as a value, it does >> not support a list type field. >> >>> All works OK when using INSERT with the same parameters. >> >> Are you sure? You can insert a list into a field? >> >>> Maybe I should as this question on sqlite list? >> >> Perhaps but I think it is the list parameter that it doesn't like. >> Unless I misunderstand the syntax. >> >> -- >> 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 >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Python Course]
This was forwarded to me from one of our user group members: "Hi everyone! I just signed up for an online course in "Web Development with Python" through DePaul University, which I think you might find interesting: http://ipd.cdm.depaul.edu/wdp/Prog_WDP.htm I talked to the folks in the admissions department and they said they needed 10 students enrolled in order to go through with the course. So I told them I would mention this to the python programmers I knew to try to drum up enough applicants. Best regards, John T." If anyone has any objection to posting this type of information, please let me know. I thought members might be interested in available Python courses. -- Jeff Jeff Johnson [EMAIL PROTECTED] Phoenix Python User Group - [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] noise function
There's probably something like what you want within the "random" module. at the interactive prompt, try: import random help(random) --- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com 2008/12/2 Alan Gauld <[EMAIL PROTECTED]>: > > "Christopher Spears" <[EMAIL PROTECTED]> wrote >> >> Does anyone know if python has a noise function? > > What kind of noise function? > What would you expect it to produce? > A stream of random numbers perhaps? > A single number each time it is called? > > And which noise profile should it follow? > Or would it be a multi dimensional noise model? > > I don't know of any such function and I'm not sure wiothout more details how > it would work in a general way even if it did exist. Can you give a > speciofic example of what you expect? > > -- > 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 > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cgi on novell/Appache
"Jim Morcombe" <[EMAIL PROTECTED]> wrote running under Novel. I know absolutely nothing about novell. I need to have the first line of the script point to the Python interpretter, I don't think that will do anything under Novell, assuming you mean the Novell network OS and not Novell Linux? If its NovelWare then you need to set the PATH to Python and set up the file associatiion between your .py file and python. Then you need to use the cgi-bin folder to store the file rather than your web app space. At least that's what I did the last time I used a DOS based server - but that was about 12 years ago! If it's Novell Linux the standard /env/python should work. around on it. I have to change the script, give it to the admin guy and let him load it. He knows nothing about cgi, python, etc. ) 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] sqlite3 Python25 parameter binding problem with UPDATEplease help
"aivars" <[EMAIL PROTECTED]> wrote oCon.execute("UPDATE rezerve SET latusaldo =? where gads =?;",(result, [year])) oCon.commit() it throws me the error: sqlite3.InterfaceError: error binding parameter 1 probably unsupported type I assume its the [year] value it objects to. I'm not sure what you expect SQLite to do with a list as a value, it does not support a list type field. All works OK when using INSERT with the same parameters. Are you sure? You can insert a list into a field? Maybe I should as this question on sqlite list? Perhaps but I think it is the list parameter that it doesn't like. Unless I misunderstand the syntax. -- 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] noise function
"Christopher Spears" <[EMAIL PROTECTED]> wrote Does anyone know if python has a noise function? What kind of noise function? What would you expect it to produce? A stream of random numbers perhaps? A single number each time it is called? And which noise profile should it follow? Or would it be a multi dimensional noise model? I don't know of any such function and I'm not sure wiothout more details how it would work in a general way even if it did exist. Can you give a speciofic example of what you expect? -- 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
[Tutor] cgi on novell/Appache
I have a cgi script that I want to get running on an Apache server running under Novel. I know absolutely nothing about novell. I need to have the first line of the script point to the Python interpretter, but have no idea where it is. (I don't have access to the server to play around on it. I have to change the script, give it to the admin guy and let him load it. He knows nothing about cgi, python, etc. ) What is the novel equivalent of "/usr/bin/python"? Jim ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor