[Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread amit sethi
Hi , i am trying to learn python and this is my first time with any
databases . I am using sqlite3 to create a database of my music files and
its
metadata tags so here is what i wanted to do . Two list one of the
attributes and one of their values ,how do i put it in the database.Here is
a simple code i think should work but isn't?
>>> import sqlite3
>>> conn=sqlite3.connect('/tmp/example2')
>>> c = conn.cursor()

>>> list1=['hello','hi']
>>> list2=['a','b']
>>>c.execute('''create table ABC(hello text,hi text)''')
>>> list1_value= ",".join(list1)
>>> list2_value= ",".join(list2)
>>> c.execute('''insert into ABC (%s) values
(%s)''')%(list1_value,list2_value)
This is the error it generates
sqlite3.OperationalError: near "%": syntax error
why doesn't this work . Can someone please explain


A-M-I-T S|S
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread शंतनू (Shantanoo)
On Wed, Nov 19, 2008 at 4:48 PM, amit sethi <[EMAIL PROTECTED]>
wrote:
> Hi , i am trying to learn python and this is my first time with any
> databases . I am using sqlite3 to create a database of my music files and
> its
> metadata tags so here is what i wanted to do . Two list one of the
> attributes and one of their values ,how do i put it in the database.Here
is
> a simple code i think should work but isn't?
 import sqlite3
 conn=sqlite3.connect('/tmp/example2')
 c = conn.cursor()
>
 list1=['hello','hi']
 list2=['a','b']
c.execute('''create table ABC(hello text,hi text)''')
 list1_value= ",".join(list1)
 list2_value= ",".join(list2)
 c.execute('''insert into ABC (%s) values
 (%s)''')%(list1_value,list2_value)

You may try:
c.execute("insert into ABC(%s) values('%s')" % (list1_value, list2_value))

regards,
shantanoo
-- 

Fred Allen  - "An associate producer is the only guy in Hollywood who will
associate with a producer."
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread Kent Johnson
On Wed, Nov 19, 2008 at 6:18 AM, amit sethi <[EMAIL PROTECTED]> wrote:

 list1=['hello','hi']
 list2=['a','b']
c.execute('''create table ABC(hello text,hi text)''')
 list1_value= ",".join(list1)
 list2_value= ",".join(list2)
 c.execute('''insert into ABC (%s) values
 (%s)''')%(list1_value,list2_value)

The parenthesis are in the wrong place to do what you intend, and the
double quotes are not needed. But this is not the right way to do it.
You should pass the values separately, not in the sql string. This
allows the database program to correctly escape values containing
special characters such as quote or comma, and it prevents sql
injection attacks. There is probably no reason to put the field names
in a list. Try this:

c.execute('insert into ABC hello, hi values ?, ?', list2)

Notice that list2 is passed as a parameter to execute.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread Kent Johnson
On Wed, Nov 19, 2008 at 7:21 AM, amit sethi <[EMAIL PROTECTED]> wrote:
> Thanks Kent , very useful reply but the thing is i actually want to use this
> in a program that stores ID3 tags and they are broken more usually than not
> .. so I actually don't know what keys/attributes i would be sending can I
> send attribute list as a parameter?
> I believe this is more elegant.
c.execute("insert into Music_tags (%s) values (%s)") %(audio_keys,
 audio_values)

Again, the parentheses are in the wrong place for this to work. It should be
c.execute("insert into Music_tags (%s) values (%s)" % (audio_keys,
audio_values))

but I don't recommend this. It's OK to supply the field names by
string interpolation, it's the values that should be supplied as a
separate sequence.

> however i have to use this instead
c.execute("insert into Music_tags (File,Artist,Album,Title,date,Genre)
 values(?,?,?,?,?,?)"\
> ,(iter,audio['Artist'].pop() or None,audio['Album'].pop() or
> None,audio['Title'].pop() or None,audio['date'].pop() or
> None,audio['Genre'].pop() or None))
>
> Notice the audio['Artist'].pop() or None
> None has to added to account for a case where their are no tags.

I don't understand this requirement. If 'Artist' is not present in the
audio dict, then audio['Artist'] will raise an exception. So I think
there is already a value for Artist. It may be an empty string rather
than None, so this would change it to None. I don't know why you need
the pop() either.

Kent

PS Please use Reply All to reply to the list.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 lists to database conversion/ using python variables in sqlite3

2008-11-19 Thread Kent Johnson
On Wed, Nov 19, 2008 at 6:48 AM, शंतनू (Shantanoo) <[EMAIL PROTECTED]> wrote:

> You may try:
> c.execute("insert into ABC(%s) values('%s')" % (list1_value, list2_value))

This is a bad idea for reasons I gave in a previous email.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help Optimise Code

2008-11-19 Thread Richard Lovely
I'm pretty new to code optimisation, so I thought I'd ask you all for advice.

I'm making an iterative prime number generator. This is what I've got so far:

Code: Select all
import math, array

def count2(start_at=0):
'Yield every third integer, beginning with start_at'
# this has been tested as faster than using itertools.count
while True:
yield start_at
start_at += 2

def iprimes():
'generate an endless sequence of prime numbers'
yield 2
yield 3
yield 5
sqrt = math.sqrt
knownPrimes = array.array("L",(3,5)) # 'L' for unsigned long - not
tested if using a smaller type is faster
for x in count2(7):
sqrtX = sqrt(x) # take extra function calls out of the inner loop
for p in knownPrimes):
test = (not x % p) and -1 or p > sqrtX
if test == -1: # (not x % p) == true
break
elif test: # (p > sqrtX) == true
yield x
knownPrimes.append(x)
break


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'm not particularly bothered about startup overheads, just the loops.

Quick thought: would the following work (I'm on a public computer
without python, so can't test):

Code: Select all
def iprimes():
'generate an endless sequence of prime numbers'
yield 2
yield 3
yield 5
sqrt = math.sqrt
knownPrimes = array.array("L",(3,5)) # 'L' for unsigned long - not
tested if using a smaller type is faster
for x in count2(7):
sqrtX = sqrt(x) # take extra function calls out of the inner loop
for test in ((not x % p) and -1 or p > sqrtX for p in
knownPrimes)): # a generator _should_ be faster...
if test == -1: # (not x % p) == true
break
elif test: # (p > sqrtX) == true
yield x
knownPrimes.append(x)
break


Please don't suggest changing languages. I like python. Although if
you want to write an extension for me, and provide the source and a
makefile, please feel free. I have a MinGW install that's doing
nothing.  (Just kidding - almost.)

This is NOT homework.

-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Optimise Code

2008-11-19 Thread Kent Johnson
On Wed, Nov 19, 2008 at 8:13 AM, Richard Lovely
<[EMAIL PROTECTED]> wrote:
> I'm pretty new to code optimisation, so I thought I'd ask you all for advice.
>
> I'm making an iterative prime number generator.


You might be interested in this recipe and discussion:
http://code.activestate.com/recipes/366178/

According to Wikipedia, the siev of Atkin is faster than sieve of Eratosthenes:
http://en.wikipedia.org/wiki/Sieve_of_Atkin

> This is what I've got so far:

>test = (not x % p) and -1 or p > sqrtX
>if test == -1: # (not x % p) == true
>break
>elif test: # (p > sqrtX) == true
>yield x
>knownPrimes.append(x)
>break

You are duplicating your tests, why not
if (not x % p):
  break
elif p > sqrtX:
  ...
?

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Optimise Code

2008-11-19 Thread Lie Ryan
On Wed, 19 Nov 2008 13:13:18 +, Richard Lovely wrote:

> I'm pretty new to code optimisation, so I thought I'd ask you all for
> advice.
> 
> I'm making an iterative prime number generator. This is what I've got so
> far:
> 
> Code: Select all
> import math, array
> 
> def count2(start_at=0):
> 'Yield every third integer, beginning with start_at' 
> # this has been
> tested as faster than using itertools.count 
> while True:
> yield start_at
> start_at += 2
> 
> def iprimes():
> 'generate an endless sequence of prime numbers' 
> yield 2
> yield 3
> yield 5
> sqrt = math.sqrt
> # 'L' for unsigned long - not tested if 
> # using a smaller type is faster
> knownPrimes = array.array("L",(3,5)) 
> for x in count2(7):
> # take extra function calls out of the inner loop
> sqrtX = sqrt(x) 
> for p in knownPrimes:
> test = (not x % p) and -1 or p > sqrtX 
> if test == -1: # (not > x % p) == true
> break
> elif test: # (p > sqrtX) == true
> yield x
> knownPrimes.append(x)
> break
> 

Do you know that every prime number is in the form 6*x+1 or 6*x-1, except 
2 and 3. This means that instead of checking all odd numbers, you could 
loop over 6 numbers then yield n - 1 and n + 1.

def count6(start):
while True:
start += 6
yield start - 1
yield start + 1

And I've seen that you generated prime by dividing things up (actually 
modulus). Division and modulus is the slowest arithmetic operator, avoid 
it if you can. If you knows the upper bound beforehand, it is faster to 
use multiplication and an array of fixed size, i.e. "Sieve of 
Erasthotenes". If you intend to generate primes without known upper bound 
though, using sieve complexify things up.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scrolling through output in shell

2008-11-19 Thread Lie Ryan
On Mon, 17 Nov 2008 09:20:55 -0500, Shawn Milochik wrote:

> On Sun, Nov 16, 2008 at 1:21 PM, Mike Hoy <[EMAIL PROTECTED]> wrote:
>> I'm writing a small program that writes to a text file. I want to be
>> able to view the contents of the text file inside of shell. But the
>> file is too large for a small shell window. Is there a way for the user
>> to 'scroll' through the contents of file that has been read into the
>> program? I noticed that on the man pages that you can do that although
>> I'm sure it's not written in python. Do I need to find a new language
>> to write this in? Maybe use a different language for the output and
>> still use python? Any help appreciated.
>>
>> --
>> Mike Hoy
>> http://www.mikehoy.net
> 
> 
> 
> As Alan has noted, your request isn't perfectly clear. So, I'm going to
> change your question and answer it. If I picked the wrong question,
> please be more explicit in your next reply.
> 
> Question: How can I read a text file from the command line if the file
> is too large to fit on the screen at once?
> 
> Answer: more or less
> If you're in Windows, you can use the more command: more file.txt That
> will allow you to scroll up and down.
> 
> If you're on pretty much any other OS, you can use more or less. I
> prefer less, because it has more features. You use it the same way you
> use more:  less file.txt

If that explanations mixing up "more" and "less" as names of programs and 
more and less for real more and less doesn't confuse you, I think you 
must already know what "more" and "less" is.

"more" and "less" is a pager program, used to provide scroll facility to 
a file or a stream. Windows only have "more", many Unix-like OS provide 
both "more" and "less". The most striking difference between "more" and 
"less" is that "more" is simple forward-only, you can't scroll up, only 
down. "less" support both backward and forward navigation.

As the manpage of "less" explains: 'less - opposite of more'

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] what do you use @staticmethod for?

2008-11-19 Thread spir

Good night,

I have not yet found any use for this feature.

Also, I do not really understand the difference with @classmethod, from the 
programmer's points of view (even if I get the difference on the python side).
As I see it, a classmethod is a very ordinary method, except its 'owner' is a 
type. [But we cannnot express its definition with standard syntax, because it 
conflicts with instance method definition syntax.]


I would be pleased to read your views about staticmethods, and the use cases 
you have for them -- that could not be done (the same way) with classmethods.


Thank you,
Denis


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Scrolling through output in shell

2008-11-19 Thread Alan Gauld


"Lie Ryan" <[EMAIL PROTECTED]> wrote

both "more" and "less". The most striking difference between "more" 
and
"less" is that "more" is simple forward-only, you can't scroll up, 
only

down. "less" support both backward and forward navigation.


On very early Unices that was true but for the last 20 years more
has had two direction paging plus searching etc. more also usually
supports the v key which takes you into view (which is read-only vi)
and some mores even have an e key to take you to the EDITOR.

less is like many of the GNU tools an early equivalent to more
which grew extra features and some slight inconsistencies. The
biggest difference that I find vbetween more and less is that you
always have to exit from less whereas more usually exits
automatically at the end of file - which is a real pain if you want
to go back to the second last page! And for that reason alone
I usually use less. (I believe less can also be configured to
use the emacs keystrokes rather than the vi keys of more
although I've never tried that.)

Alan G.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] what do you use @staticmethod for?

2008-11-19 Thread Alan Gauld

"spir" <[EMAIL PROTECTED]> wrote


I have not yet found any use for this feature.


While there are subtle differences I believe the biggest
reason for both being present is history. static methods
were there first then class methods were added soon after
and are slightly more flexible. But statics were kept
because people were already using them.

Personally I usually use classmethod nowadays.

Also, I do not really understand the difference with @classmethod, 
from the programmer's points of view (even if I get the difference 
on the python side).
As I see it, a classmethod is a very ordinary method, except its 
'owner' is a type.


Which makes it very different to an instance method. instance
methods act on instances. class methods act on the entire
class - ie they can affect all of the instances or none.

You don't need to use class methods(or statics) very often
but when you do they are invaluable. The example of a factory
method, or a selection method (from a database say), or a cache
of instances. All of these can be done elegantly with class methods.

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] what do you use @staticmethod for?

2008-11-19 Thread wesley chun
On 11/19/08, Alan Gauld <[EMAIL PROTECTED]> wrote:
> "spir" <[EMAIL PROTECTED]> wrote
>
>> I have not yet found any use for this feature.
>
> Which makes it very different to an instance method. instance
> methods act on instances. class methods act on the entire
> class - ie they can affect all of the instances or none.
>
> You don't need to use class methods(or statics) very often
> but when you do they are invaluable.


the good news is that all of these fancy features are *optional*. if
you don't know what they're useful for, that probably means you don't
need them yet, so no need to stress that you *have* to learn what they
are as you're learning the language.

at some point, you'll come across a situation where you *wished* that
Python had some feature you wanted, like a function used only in
relation to a class or its instances but don't want to define it as an
external function (@staticmethod) or to have a method where the class
object itself is passed in for you to be able to modify a class value
global to all instances (@classmethod), and to discover that features
*are* there!

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
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] Scrolling through output in shell

2008-11-19 Thread Mike Hoy
os.system("cat textfile | less")

did the trick, thanks everyone.

On Wed, Nov 19, 2008 at 2:34 PM, Lie Ryan <[EMAIL PROTECTED]> wrote:
> On Mon, 17 Nov 2008 09:20:55 -0500, Shawn Milochik wrote:
>
>> On Sun, Nov 16, 2008 at 1:21 PM, Mike Hoy <[EMAIL PROTECTED]> wrote:
>>> I'm writing a small program that writes to a text file. I want to be
>>> able to view the contents of the text file inside of shell. But the
>>> file is too large for a small shell window. Is there a way for the user
>>> to 'scroll' through the contents of file that has been read into the
>>> program? I noticed that on the man pages that you can do that although
>>> I'm sure it's not written in python. Do I need to find a new language
>>> to write this in? Maybe use a different language for the output and
>>> still use python? Any help appreciated.
>>>
>>> --
>>> Mike Hoy
>>> http://www.mikehoy.net
>>
>>
>>
>> As Alan has noted, your request isn't perfectly clear. So, I'm going to
>> change your question and answer it. If I picked the wrong question,
>> please be more explicit in your next reply.
>>
>> Question: How can I read a text file from the command line if the file
>> is too large to fit on the screen at once?
>>
>> Answer: more or less
>> If you're in Windows, you can use the more command: more file.txt That
>> will allow you to scroll up and down.
>>
>> If you're on pretty much any other OS, you can use more or less. I
>> prefer less, because it has more features. You use it the same way you
>> use more:  less file.txt
>
> If that explanations mixing up "more" and "less" as names of programs and
> more and less for real more and less doesn't confuse you, I think you
> must already know what "more" and "less" is.
>
> "more" and "less" is a pager program, used to provide scroll facility to
> a file or a stream. Windows only have "more", many Unix-like OS provide
> both "more" and "less". The most striking difference between "more" and
> "less" is that "more" is simple forward-only, you can't scroll up, only
> down. "less" support both backward and forward navigation.
>
> As the manpage of "less" explains: 'less - opposite of more'
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Mike Hoy
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor