Re: [Tutor] Parameterized Queries failing on MySQL

2010-12-16 Thread Peter Otten
Shea Grove wrote:

>> I'm using pyodbc to interact with MS SQL Server and I'm starting to
>> support mysql.  My issue is that when I use a parameterized query, it
>> works for SQL Server, but crashes when I point to MySQL.  

I assume it raise an Exception.

>> Is there a different syntax that I should be using? or module?

MySQLdb uses a different paramstyle "format" (see 
http://www.python.org/dev/peps/pep-0249/ )

>> query='insert into RestaurantTable values (?, ?, ?, ?, ?, ?, ?, ?, ?)'

Make this

query = 'insert into RestaurantTable values (%s, %s, %s, %s, %s, %s, %s, %s, 
%s)'

>> mcursor.execute(query, params)

but *never* change the above to

mcursor.execute(query % params) #WRONG

which unfortunately will work most of the time with that paramstyle while 
making your app vulnerable to sql injection as demonstrated here: 
http://xkcd.com/327/

Peter

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't even get hello to work???!!

2010-12-16 Thread Alan Gauld

"Emile van Sebille"  wrote


print 'hello'
...
SyntaxError: invalid syntax


Actually, "the print statement is deprecated" ought not be out of 
reach.


Its very unfortunate that Python 3 didn't include a special error or 
warning

message for the print statement. This kind of error for a complete
beginner is very off putting and print "hello world" is probably the 
first

thing many newbies try and many v2 tutorials start with. And even for
non-newbies print syntax errors are by far the most common issue
when migrating to Python 3 from 2 - I've been bitten several times
myself before realizing its the missing parens on print...

It is the kind of basic problem that will turn many would be
programmers away from Python. You can argue about the purity of
such a special method being inconsistent etc, but in practical terms
if we wanted people to use Python we have just introduced a big,
and largely unnecessary, barrier.

A missed opportunity I fear.

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't even get hello to work???!!

2010-12-16 Thread bob gailer

I agree. What do we do to fix the "problem"?

--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Can't even get hello to work???!!

2010-12-16 Thread wesley chun
On Thu, Dec 16, 2010 at 10:13 AM, bob gailer  wrote:
> I agree. What do we do to fix the "problem"?

something like "quit" (or friends) might be nice, as they are already
special cases in the interpreter:

$ python
Python 2.5.1 (r251:54863, Feb  9 2009, 18:49:36)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Use quit() or Ctrl-D (i.e. EOF) to exit
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> copyright
Copyright (c) 2001-2007 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.

Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
>>> license
Type license() to see the full license text

just my $0.02,
-- 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
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread Joel Schwartz
I'm new to Python and have been using IDLE for Python programming. My only
other significant text editor experience is with Notepad++ and Tinn-R, which
I've used for R programming. Vim seems more efficient, but I wanted to ask
you experts whether it makes sense (or is even possible) to use vim in
Windows (I use Windows XP-SP3). 

One more question: IDLE does not appear to have a way to review your command
history, e.g., by hitting the up arrow. I find the up-arrow to be an
incredible convenience when I'm working on the R command line, and the lack
of it in IDLE is often a source of frustration for me. Is there any way to
set up IDLE to do this? Alternatively, is there another basic Python GUI
that has the up-arrow command history feature and is also good for Python
coding in general?

Thanks,
Joel

-Original Message-
From: tutor-bounces+joel=joelschwartz@python.org
[mailto:tutor-bounces+joel=joelschwartz@python.org] On Behalf Of
Knacktus
Sent: Wednesday, December 15, 2010 11:52 PM
To: tutor@python.org
Subject: Re: [Tutor] vim as a python editor

Am 15.12.2010 23:26, schrieb Paul Griffiths:
> Hi - I'm a beginner at programming and Python.
>
> I have been looking for an editor to replace Idle and tried out a few.
> I liked Geany but decided on vim because I am also learning Linux and 
> the vim skills might be useful.  I run Python 2.6.5 on Ubuntu 10.04.
>
> How have those of you who use vim configured it?  I have looked on the 
> web but got a bit confused by the advice and options.
>
> I am currently following some of the advice in the on-line tutorial at 
> http://openbookproject.net/thinkcs/python/english2e/app_c.html and
>
> - installed the full version of vim (sudo apt-get install vim-gnome)
> - created a .vimrc file in my home directory and entered
>  syntax enable
>  filetype indent on
>  set et
>  set sw=4
>  set smarttab
>  map  :w\|!python %
>
> - added the following to .bashrc:
>  EDITOR=vim
>  export EDITOR

I forgot:
Here some more information about vim and Python:

http://wiki.python.org/moin/Vim

Some guy tuned vim to the maximum with code completion and everything. 
But start simple to learn the basics. You can extend anytime later.

Jan

>
> I open up a terminal in the folder where I keep my python files and 
> create two additional tabs in this terminal.  I use the first tab to 
> run python, the second one to run vim and the third one for running 
> linux commands such as ls to list the folder contents.
>
> Is this is a reasonable approach?  Thank you for any feed back.
>
>
> PaulG
>
>
>
> ___
> 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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread bob gailer

On 12/16/2010 1:58 PM, Joel Schwartz wrote:

I'm new to Python and have been using IDLE for Python programming. My only
other significant text editor experience is with Notepad++ and Tinn-R, which
I've used for R programming. Vim seems more efficient, but I wanted to ask
you experts whether it makes sense (or is even possible) to use vim in
Windows (I use Windows XP-SP3).
FWIW I started 10 years ago using Python For Windows as my development 
environment. I have never regretted that choice.


--
Bob Gailer
919-636-4239
Chapel Hill NC

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread Steve Willoughby

On 16-Dec-10 10:58, Joel Schwartz wrote:

I'm new to Python and have been using IDLE for Python programming. My only
other significant text editor experience is with Notepad++ and Tinn-R, which
I've used for R programming. Vim seems more efficient, but I wanted to ask
you experts whether it makes sense (or is even possible) to use vim in
Windows (I use Windows XP-SP3).


Vim works wonderfully in Windows, actually, which IMHO really needs an 
advanced text editor like vi or emacs or something.



One more question: IDLE does not appear to have a way to review your command
history, e.g., by hitting the up arrow. I find the up-arrow to be an
incredible convenience when I'm working on the R command line, and the lack
of it in IDLE is often a source of frustration for me. Is there any way to
set up IDLE to do this? Alternatively, is there another basic Python GUI
that has the up-arrow command history feature and is also good for Python
coding in general?


If you use the up-arrow to move your cursor to previous lines in the 
IDLE window and hit ENTER, you recall that line where you can edit and 
re-enter it.




--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 48A3 2621 E72C 31D9 2928 2E8F 6506 DB29 54F7 0F53
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread python
Joel,

> One more question: IDLE does not appear to have a way to review your command
> history, e.g., by hitting the up arrow.

To move through your command history, use Alt+P (previous) and Alt+N
(next).

Malcolm
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to import python dictionary into MySQL table?

2010-12-16 Thread Sean Carolan
Maybe one of you can help with this.  I've got some data that I
grabbed from a cobbler *.json file using simplejson.load().  Heres
what the data looks like:

In [120]: print mydata.keys()
['comment', 'kickstart', 'name_servers_search', 'ks_meta',
'kernel_options_post', 'image', 'redhat_management_key', 'power_type',
'power_user', 'kernel_options', 'virt_file_size', 'mtime',
'template_files', 'gateway', 'uid', 'virt_cpus', 'hostname',
'virt_type', 'mgmt_classes', 'power_pass', 'netboot_enabled',
'profile', 'virt_bridge', 'parent', 'virt_path', 'interfaces',
'power_address', 'name_servers', 'name', 'owners', 'ctime',
'virt_ram', 'power_id', 'random_id', 'server',
'redhat_management_server', 'depth']

In [121]: print mydata.values()
['Virtual Machine', '<>', ['subdomain.company.com',
'company.com'], {'puppet_environment': 'development'}, {}, '', '~',
'apc_snmp', '', {'dns': '10.10.132.12'}, 160, 1286408237.2311871, {},
'10.10.32.3', 'asIjeKJIDOOUuUIDSwOTY', 16,
'webservice-app16.subdomain.company.com', '<>', [], '', True,
'rhel5-server-64-domU', '<>', '', '<>', {'eth0':
{'dhcp_tag': '', 'subnet': '255.255.255.0', 'virt_bridge': 'xenbr0',
'static_routes': ['10.10.0.0/255.255.0.0:10.10.32.3',
'10.10.0.0/255.255.0.0:10.10.32.3'], 'dns_name':
'webservice-app16.subdomain.company.com', 'bonding': '', 'static':
True, 'bonding_opts': '', 'mac_address': '00:16:3e:aa:aa:aa',
'bonding_master': '', 'ip_address': '10.10.32.219'}}, '',
['10.10.132.12', '10.10.132.13'],
'webservice-app16.subdomain.company.com', ['scarolan'],
1277755073.144805, 11749, '', '7oWY', '<>', '~', 2]

I have a database with a table called "systems" that contains an
auto-increment id field, as well as fields for each of the keys in
mydata.keys().  But I can't seem to get the syntax to import
mydata.values() into the table.  I think the problem may be that some
of the items in my list are dictionaries or lists...

What would be the quickest and most straightforward way to do this?




My table is below if you need a reference, sorry for the ugly formatting:

mysql> show columns in systems;
+--+--+--+-+-++
| Field| Type | Null | Key | Default |
Extra  |
+--+--+--+-+-++
| id   | int(11)  | NO   | PRI | NULL|
auto_increment |
| comment  | varchar(255) | YES  | | NULL|
   |
| kickstart| varchar(255) | YES  | | NULL|
   |
| name_servers_search  | varchar(255) | YES  | | NULL|
   |
| ks_meta  | varchar(255) | YES  | | NULL|
   |
| kernel_options_post  | varchar(255) | YES  | | NULL|
   |
| image| varchar(255) | YES  | | NULL|
   |
| redhat_management_key| varchar(255) | YES  | | NULL|
   |
| power_type   | varchar(255) | YES  | | NULL|
   |
| power_user   | varchar(255) | YES  | | NULL|
   |
| kernel_options   | varchar(255) | YES  | | NULL|
   |
| virt_file_size   | int(11)  | YES  | | NULL|
   |
| mtime| int(11)  | YES  | | NULL|
   |
| template_files   | varchar(255) | YES  | | NULL|
   |
| gateway  | varchar(255) | YES  | | NULL|
   |
| uid  | varchar(255) | YES  | | NULL|
   |
| virt_cpus| smallint(64) | YES  | | NULL|
   |
| hostname | varchar(255) | YES  | | NULL|
   |
| virt_type| varchar(255) | YES  | | NULL|
   |
| mgmt_classes | varchar(255) | YES  | | NULL|
   |
| power_pass   | varchar(255) | YES  | | NULL|
   |
| netboot_enabled  | varchar(255) | YES  | | NULL|
   |
| profile  | varchar(255) | YES  | | NULL|
   |
| virt_bridge  | varchar(255) | YES  | | NULL|
   |
| parent   | varchar(255) | YES  | | NULL|
   |
| virt_path| varchar(255) | YES  | | NULL|
   |
| interfaces   | text | YES  | | NULL|
   |
| power_address| varchar(255) | YES  | | NULL|
   |
| name_servers | varchar(255) | YES  | | NULL|
   |
| name | varchar(255) | YES  | | NULL|
   |
| owners   | varchar(255) | YES  | | NULL|
   |
| ctime| int(11)  | YES  | | NULL|
   |
| virt_ram | int(11)  | YES  | | NULL|
   |
| power_id   

Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread Wayne Werner
On Thu, Dec 16, 2010 at 12:58 PM, Joel Schwartz wrote:

> 

whether it makes sense (or is even possible) to use vim in
> Windows (I use Windows XP-SP3).
> 

Alternatively, is there another basic Python GUI
> that has the up-arrow command history feature and is also good for Python
> coding in general?
>

On Windows and Linux both I use vim and IPython. They make a rather powerful
combination. Then again so do a lot of other configurations, so whatever you
feel comfortable with.

The vim keybindings are extremely powerful, though, and regardless of what
editor you choose, I recommend picking one that has good keybindings either
natively or through a plugin.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION

2010-12-16 Thread Alan Gauld

"Joel Schwartz"  wrote
you experts whether it makes sense (or is even possible) to use vim 
in

Windows (I use Windows XP-SP3).


Yes, vim is great on Windows.
When i used unix primarily I was an emacs fan, but emacs just
doesn't feel right on windows to me so I moved to vim which
works hust as well in both OS.


One more question: IDLE does not appear to have a way to
review your command history, e.g., by hitting the up arrow.


IDLE borrows from emacs and uses ALT-P and ALT-N.
(FWIW Pythonwin uses Ctrl-UP/Ctrl-Down for the same purpose)

set up IDLE to do this? Alternatively, is there another basic Python 
GUI
that has the up-arrow command history feature and is also good for 
Python

coding in general?


Pythonwin is better than IDLE for Windows users. It has integration
with COM for object browsing and also with the Windows help system.
And the debugger is much better than the IDLE one too.

And it users the same editor control as Scite.
Speaking of Scite, it is a pretty useful editor in its own right and 
adds

tabbed editing which Pythonwin doesn't have, but OTOH it loses all
the Python specific tools. my ideal Windows text editor would be a
combination of Scite and vim...

Finally, if you have a PC big enough to run it well, Eclipse and
Pydev are a good combination. Especially if you also work in Java
or C++ etc

And there are a bunch of others that folks like. IDEs and text editors
are very personal choices. Wingz, SPE, PyCrust/PyAlaMode, and more.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to import python dictionary into MySQL table?

2010-12-16 Thread Sean Carolan
> I have a database with a table called "systems" that contains an
> auto-increment id field, as well as fields for each of the keys in
> mydata.keys().  But I can't seem to get the syntax to import
> mydata.values() into the table.  I think the problem may be that some
> of the items in my list are dictionaries or lists...
>
> What would be the quickest and most straightforward way to do this?

I got this working in case anyone else comes across the same problem.
This function will pull cobbler *.json data into a MySQL database
table.  The function assumes that you already have a database set up,
and that you are dumping the data into the "systems" table:

def importJSON(targetfile):
'''
Imports JSON data from targetfile into MySQL database table.
'''
value_list = []
rawdata = json.load(open(targetfile))
for key in rawdata.keys():
strvalue = str(rawdata[key]).replace("'",'"')
value_list.append(strvalue)
valtuple = tuple(value_list)
sql = "INSERT INTO systems (comment, kickstart,
name_servers_search, ks_meta, kernel_options_post, image,
redhat_management_key, power_type, power_user, kernel_options, vi
rt_file_size, mtime, template_files, gateway, uid, virt_cpus,
hostname, virt_type, mgmt_classes, power_pass, netboot_enabled,
profile, virt_bridge, parent, virt_path, interfaces, power_address,
name_servers, name, owners, ctime, virt_ram, power_id, random_id,
server, redhat_management_server, depth) VALUES (%s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
cursor.execute(sql, valtuple)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor