[Tutor] New to Python

2012-05-17 Thread Jeremy Traurig
Hello,

I very new to python and have been playing around with some simple code
that I would eventually use in the real word. Below is the code I have
created. I want the code to read data from a file called SIL633.txt and
then output that data into another file called test.txt. below is my code:

#! /usr/bin/env python
# Read NRG Text File
def readNRGtxt():
import numpy as np
f1 = open('SIL633_original.txt','r')
data = np.genfromtxt(f1,delimiter='\t',skip_header=141)
f1.close
f2 = open('test.txt','w')
np.savetxt(f2,data,fmt='%6.2f',delimiter='\t')
f2.close

I'm running this on mac 10.5.8. When I run this script from the command
line, there is no file output. However, if I remove the "def" statement in
the script and run again from the command line, a test.txt file is output.
I guess I'm trying to understand why using the def statement changes how
the script operates and since it does, how do I correct this code so it
runs from the command line with the def statement.

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


[Tutor] Installing graphics module on python 2.7

2012-05-17 Thread Emeka
Hello All,

I would like to install graphics.
So that I can do this:
import graphics

Please help me. I tried "pip install graphics" nothing came out of it.

Regards, \Janus


-- 
*Satajanus  Nig. Ltd


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


Re: [Tutor] How to create self contained Windows executables (.exe files) from python scripts

2012-05-17 Thread Alan Gauld

On 18/05/12 00:02, Brad Hudson wrote:

Can someone point me to information on how to create Windows .exe files
from python 2.7.3? The py2exe method (www.py2exe.org
) appears to only be available for python 2.6
from the sites I've seen.



You can try PyInstaller:

http://www.pyinstaller.org/

It claims to work on 2.7.

Rumour has it that its slightly more complex to use than py2exe but I 
can't vouch for that since I've never found a need to create exe files...


There are several other such tools too, Google will find them for you.

--
Alan G
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 read blog posts in python

2012-05-17 Thread Brian van den Broek
On 17 May 2012 16:31, "Surya K"  wrote:
>
> Hi,
>
> I want to write a python code which read blog's RSS/ Atom feeds and gives
us the all post's content of blog...
>
> I am currently trying to use FeedParser (Universal Feed Parser). I am
able to get all post's titles and URL's but not a content..
>
> I tried to use certain functions mentioned in documentation but couldn't
really understand how to do..
>
> Can anyone help me how to do that? (I



Hi,

You are much more likely to get help if you include the code you tried.
It's fine to say "what I have isn't working; I've pasted it at the end of
this message" or the like.

Also, with evidence of your attempt, those who know UFP (not me, I am
afraid), will be better able to aim their efforts to help at your level.

Best,

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


[Tutor] How to create self contained Windows executables (.exe files) from python scripts

2012-05-17 Thread Brad Hudson
Can someone point me to information on how to create Windows .exe files
from python 2.7.3? The py2exe method (www.py2exe.org) appears to only be
available for python 2.6 from the sites I've seen.

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


[Tutor] shutil.rmtree clarification

2012-05-17 Thread Garry Willgoose
I'm trying to remove a directory that has within it a directory tree structure 
and files and thought the following would work (the online docs seem to suggest 
it will remove the underlying directory tree and included files)

shutil.rmtree('test_directory')

but I got back an error (Python 2.7 on OSX)


Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 253, in rmtree
onerror(os.rmdir, path, sys.exc_info())
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 251, in rmtree
os.rmdir(path)
OSError: [Errno 66] Directory not empty: 'test_directory'


which seems to suggest that the directory has to be empty to work ... sort of 
defeats the purpose of wanting to removing the tree in one command. What do 
people suggest short of calling os.system('rm -R test_directory').

-
Prof Garry Willgoose
Australian Professorial Fellow,
School of Engineering,
The University of Newcastle,
Callaghan, NSW, 2308.
Australia

Phone: +61 2 4921 6050 (Tues-Thurs)
 +61 2 6545 9574 (Fri-Mon)
FAX: +61 2 4921 6991
email: 
garry.willgo...@newcastle.edu.au
g.willgo...@telluricresearch.com
---
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] shutil.rmtree clarification

2012-05-17 Thread Garry Willgoose
I'm trying to remove a directory that has within it a directory tree structure 
and files and thought the following would work (the online docs seem to suggest 
it will remove the underlying directory tree and included files)

shutil.rmtree('test_directory')

but I got back an error (Python 2.7 on OSX)


Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 253, in rmtree
onerror(os.rmdir, path, sys.exc_info())
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 251, in rmtree
os.rmdir(path)
OSError: [Errno 66] Directory not empty: 'test_directory'


which seems to suggest that the directory has to be empty to work ... sort of 
defeats the purpose of wanting to removing the tree in one command. What do 
people suggest short of calling os.system('rm -R test_directory').

-
Prof Garry Willgoose
Australian Professorial Fellow,
School of Engineering,
The University of Newcastle,
Callaghan, NSW, 2308.
Australia

Phone: +61 2 4921 6050 (Tues-Thurs)
 +61 2 6545 9574 (Fri-Mon)
FAX: +61 2 4921 6991
email: 
garry.willgo...@newcastle.edu.au
g.willgo...@telluricresearch.com
---
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Mark Lawrence

On 17/05/2012 10:35, Steven D'Aprano wrote:

On Thu, May 17, 2012 at 08:27:07AM +0100, Russel Winder wrote:


Should we be promoting use of the format method in strings rather than
the % operator? % is deprecated now.


It most certainly is not.

There are no plans to deprecate the string % operator any time in the
foreseeable future. It may, hypothetically, wither away from lack of use
(although I doubt it -- so long as there are C programmers, there will
be people who like % formatting).

If you think it is deprecated, please show me in the official Python
documentation where it says so.

As far as I am concerned, the final word on deprecation belongs to the
creator of Python, and BDFL, Guido van Rossum, who hopes that *at best*
the format method will gradually replace % formatting over the next
decade or so before the (as yet hypothetical) Python 4:

http://mail.python.org/pipermail/python-dev/2009-September/092399.html

Rather than repeat myself, I will just point to what I wrote back in
January:

http://mail.python.org/pipermail/python-list/2012-January/1285894.html





You beat me to it :)

--
Cheers.

Mark Lawrence.

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


Re: [Tutor] shutil.rmtree clarification

2012-05-17 Thread Glen Zangirolami
Garry,

It should be able to remove the tree. I am not able to replicate this on
OSX .. yet. What are the contents of the directory and the permissions of
the directories/files? Although it should throw an exception if it is
a symbolic link or a file it can't remove.


On Thu, May 17, 2012 at 8:32 AM, Garry Willgoose <
garry.willgo...@newcastle.edu.au> wrote:

> /Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread bob gailer

On 5/17/2012 3:27 AM, Russel Winder wrote:

Should we be promoting use of the format method in strings rather than
the % operator? % is deprecated now.
I for one do not like seeing % deprecated. Why? It is not broken, and 
IMHO the easiest to use of all formatting options.


--
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


[Tutor] shutil.rmtree clarification

2012-05-17 Thread Garry Willgoose
I'm trying to remove a directory that has within it a directory tree structure 
and files and thought the following would work (the online docs seem to suggest 
it will remove the underlying directory tree and included files)

shutil.rmtree('test_directory')

but I got back an error (Python 2.7 on OSX)


Traceback (most recent call last):
  File "", line 1, in 
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 253, in rmtree
onerror(os.rmdir, path, sys.exc_info())
  File 
"/Library/Frameworks/Python.framework/Versions/7.0/lib/python2.7/shutil.py", 
line 251, in rmtree
os.rmdir(path)
OSError: [Errno 66] Directory not empty: 'test_directory'


which seems to suggest that the directory has to be empty to work ... sort of 
defeats the purpose of wanting to removing the tree in one command. What do 
people suggest short of calling os.system('rm -R test_directory').

-
Prof Garry Willgoose
Australian Professorial Fellow,
School of Engineering,
The University of Newcastle,
Callaghan, NSW, 2308.
Australia

Phone: +61 2 4921 6050 (Tues-Thurs)
 +61 2 6545 9574 (Fri-Mon)
FAX: +61 2 4921 6991
email: 
garry.willgo...@newcastle.edu.au
g.willgo...@telluricresearch.com
---
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How to read blog posts in python

2012-05-17 Thread Surya K

Hi,
I want to write a python code which read blog's RSS/ Atom feeds and gives us 
the all post's content of blog...
I am currently trying to use FeedParser (Universal Feed Parser). I am able to 
get all post's titles and URL's but not a content..
I tried to use certain functions mentioned in documentation but couldn't really 
understand how to do..
Can anyone help me how to do that? (I want to print the "content" exactly the 
way it has been written. I don't mind about fonts/ styles..)


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


Re: [Tutor] ssh socks proxy

2012-05-17 Thread Adam Gold
Many thanks Kushal (it's always the silly things one has to look for!).  The 
answer to your question is I don't have shell access to the proxy server, I can 
only access it for the purpose of setting up a socks proxy.

Btw, I'm probably pushing my luck here but any idea what the same script would 
look like if one used paramiko?


On 17 May 2012, at 04:01, kushal.kumaran+pyt...@gmail.com wrote:

kushal.kumaran+pyt...@gmail.com wrote:

> Adam Gold  wrote:
> 
>> I'm trying to write a 'simple' script that will set up a socks proxy
>> over ssh and maintain the connection until manually terminated.   It's
>> not possible to use key-based authentication so a password will need
> to
>> be supplied.  Also, note, the user is presented with a list of servers
>> to choose from at the beginning.  The actual ssh command is: 'ssh
> -vNCD
>> 2 user@host'.
>> 
>> I've been tinkering with both pexpect and paramiko but fear I'm making
>> a mountain out of a mole hill.  I'm aware both have example scripts
> for
>> ssh forwarding but, to be honest, they are both too complicated (aka I
>> don't know how to customise them).  Here is one script I've attempted
>> to get working and the associated error listing:
>> 
>> http://pastebin.com/jj8Fgvwm - script
>> http://pastebin.com/jRA8zpzi - error
>> 
>> Could anyone help me either correct the script I've started or suggest
>> an ockham's-razor-adherent alternative!  Many thanks.
> 
> The error message indicates that there hostname HOST could not be
> resolved. You need to replace that with the value of the variable HOST.
> 
> COMMAND = "ssh -vNDR 2 {}@{}".format(USER, HOST)
> 
> I'm curious to know why you can't use keys. They make things much
> simpler.

Excuse the incorrect ssh command. New mail client tripped me up.


-- 
regards,
kushal


Use the link below to report this message as spam.
https://lavabit.com/apps/teacher?sig=3087369&key=478586400



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


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Steven D'Aprano
On Thu, May 17, 2012 at 08:27:07AM +0100, Russel Winder wrote:

> Should we be promoting use of the format method in strings rather than
> the % operator? % is deprecated now.

It most certainly is not.

There are no plans to deprecate the string % operator any time in the 
foreseeable future. It may, hypothetically, wither away from lack of use 
(although I doubt it -- so long as there are C programmers, there will 
be people who like % formatting).

If you think it is deprecated, please show me in the official Python 
documentation where it says so.

As far as I am concerned, the final word on deprecation belongs to the 
creator of Python, and BDFL, Guido van Rossum, who hopes that *at best* 
the format method will gradually replace % formatting over the next 
decade or so before the (as yet hypothetical) Python 4:

http://mail.python.org/pipermail/python-dev/2009-September/092399.html

Rather than repeat myself, I will just point to what I wrote back in 
January:

http://mail.python.org/pipermail/python-list/2012-January/1285894.html



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


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Alan Gauld

On 17/05/12 08:39, Russel Winder wrote:


Interesting, or not, the "Big Data" people are rapidly realizing that
data mining and SQL are mutually incompatible.


After many years working with big data mining teams/apps my considered 
opinion is use SAS or one of its peers! It costs money but it works.
And the savings in development time far outweigh the costs. And if you 
have to manage 100TB or more then the costs of SAS licenses are likely 
to be the least of your worries! And if you are really serious about it

run SAS on top of a Teradata $torage $ystem...

Note:
I have no commercial connection with either company, just an admirer of 
their technology solutions.


:-)

--
Alan G
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] table to dictionary and then analysis

2012-05-17 Thread Russel Winder
On Wed, 2012-05-16 at 16:03 +0100, Alan Gauld wrote:
[...]
> I agree, but in this case SQL seemed like the most likely fit of the 
> ones I knew. however:

Which raises the point that the best design of a given problem in a
given context is the one that is most comprehensible to the people
directly involved.

> >  SQL
> >  MongoDB
> 
> I know about these

I have it on good authority yesterday that MongDB is only properly
useful in a single store context, i.e. not a replicated cluster.  Along
withthis comes news that Riak is very good and has a Python API.

> 
> >  CouchDB
> >  Cassandra
> >  Neo
> 
> These are new to me.

CouchDB is an Erlang implemented system. Ubuntu One uses this for
example.  Cassandra is an Apache project, a JVM-based system. MongoDB,
CouchDB and Cassandra are "document stores". Neo4J is a graph repository
so of a very different architecture and performance characteristics. And
then there is Redis :-)

> 
> > etc. Python only has SQLite3 as standard but there are alternatives. I
> > have been using PyMongo quite successfully.
> 
> Python comes with several storage/access options including shelve, gdbm, 
> ldap, cobfig files, XML, in addition to SQL.

Indeed. The problem I have had with shelve for this sort of thing is
that is is critically dependent on the pickling algorithm and so
potentially Python version dependent.

[...]
> on flexiblity of data format. The OPs requirements suggested intelligent 
> filtering of a fixed record format which is one of the areas where SQL 
> works well. The other side of the coin is that the data is essentially 
> single table so the relationship management aspects of SQL would not be 
> needed. So I agree we don't have enough detail
> to be 100% sure that another option would not work as well or better.

The signpost here is that the table as is is likely not in third normal
form, and that if the problem currently being solved was actually a
small part of a bigger problem, this issue would need to be addressed.

> But most other options require learning new (often bespoke) query 
> languages and have limited user tools. All of these factors need to be 
> included too. Mongo et al tend to be better suited, in my experience, to 
> machine access applications rather than end user access.

Agreed. Unfortunately, vendor commercial issues often get in the way of
experimenting to find out where the NoSQL systems are genuinely better
than SQL ones. We will get there though.

Interesting, or not, the "Big Data" people are rapidly realizing that
data mining and SQL are mutually incompatible. The current trend is
towards streaming whole databases through dataflow programs. But Java
rather than Python is the default language in that world.

> > There are various articles around the Web comparing and contrasting
> > these various models. Some of the articles are even reasonable :-)
> 
> Wikipedia is my friend :-)

:-)

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] table to dictionary and then analysis

2012-05-17 Thread Russel Winder
On Wed, 2012-05-16 at 12:57 -0400, Joel Goldstick wrote:
[...]
> I think the OP is just learning and this thread may have gotten of track.

I didn't realize discussion of immediate side issues and alternatives,
and allowing people to exchange information was OT in this mailing list.
Also of course, OP didn't mention databases, but was asking how to do it
with lists and dictionaries. I think there is irony somewhere in here.

> Here is some code to get started.  I decided to use sqlite3 since its
> easy to use with python -- no finding and learning to load packages.
> 
> 
> #!/usr/bin/env python
> 
> import sqlite3 as db
> 
> # Ideally this shouldn't be global, but in this short code snippet it
> gets the job done
> # here we create a database and get a cursor
> conn = db.connect('climate.db')
> cursor = conn.cursor()
> print cursor

I believe that there are more problems than just global data here. One
obvious thing is this code is not safe against exceptions. I appreciate
this is a trivially small program, but I think it important that sample
code presents good style or explicitly states what is wrong so as to
present what not to do. Your comment about global sits well with this,
but for me doesn't go far enough. Python introduced context managers and
the with statement exactly for this sort of situation, following the
lead of C++ with RAII. I think we should all get into the habit of using
the with statement automatically in this situation.

> # this will create a table for our data
> sql_create = """CREATE TABLE if not exists rain (
> id INTEGER PRIMARY KEY,
> year INTEGER,
> month TEXT(3),
> rainfall FLOAT,
> fire_area FLOAT
> )"""
> 
> # this will read the data file and put it in our database
> def populate_climate_table(file_name):
> """
> reads the file_name and insert data into sqlite table
> """
> sql_insert_string = "insert into rain (year, month, rainfall,
> fire_area) values (%d, '%s', %f, %f)"
> 
> f = open(file_name)

Same comment about context managers and with statement applies here:
this code is not exception safe.

> f.readline() # get rid of column headers
> for l in f.readlines():
> data_list = l.split()
> print data_list
> sql_insert = sql_insert_string % (int(data_list[0]),
> data_list[1], float(data_list[2]), float(data_list[3]))

Should we be promoting use of the format method in strings rather than
the % operator? % is deprecated now.

Although not an issue here, this sort of SQL string manipulation is at
the heart of SQL injection attacks and so should be frowned upon. Hence
SQLAlchemy's expression languages, which goes some way to avoiding the
whole issue.  At the expense of having to load an additional package.
With package management on Debian/Fedora/Ubuntu/MacPorts or the pip
command this is not difficult to add. 

> print sql_insert
> cursor.execute(sql_insert)
> conn.commit()
> 
> 
> if __name__ == '__main__':
> 
> print sql_create
> cursor.execute(sql_create)
> populate_climate_table('data.txt')
> 
> 
> So, I haven't solved all of the questions with this code.  The next
> thing to do is to read a little about sqlite select statements.
> for example: sqlite> select sum(rainfall)/count(*) from rain;
> 3.97352768125
> 
> This statement will give the average rainfall over the complete dataset.
> To get the ave rainfall for a given year do this:
> sqlite> select sum(rainfall)/count(*) from rain where year = 1983;
> 
> Come back with more questions

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


signature.asc
Description: This is a digitally signed message part
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor