[Tutor] Cannot open SQLite database

2010-02-24 Thread Timo
Hello all, my program uses SQLite as database and everything worked fine 
until now.
I have over 3000 downloads of my program, but I got 2 mails this week 
from people who get this error:


OperationalError: unable to open database file

I searched the net, and all problems seem to be of a not writeable 
directory or passing a tilde to connect to the database.

As far as I know, this isn't the case with my program.
Here is some code:


# Get the application data folder
# This will return: C:\Documents and Settings\user\Application Data\myapp
PREFDIR = os.path.join(os.environ['APPDATA'], 'myapp')

# Connect to the database
def db_connect():
conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) # This 
line gives the error

conn.text_factory = str
cursor = conn.cursor()
return (conn, cursor)


I noticed that the 2 users that got the error are Russian, so I thought 
that the Russian characters would cause problems. I tried on my Linux 
and Windows machines with some Russian names, but thet doesn't seem the 
problem.


Some help is appreciated :-).

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


Re: [Tutor] Cannot open SQLite database

2010-02-24 Thread Christian Witts

Timo wrote:
Hello all, my program uses SQLite as database and everything worked 
fine until now.
I have over 3000 downloads of my program, but I got 2 mails this week 
from people who get this error:


OperationalError: unable to open database file

I searched the net, and all problems seem to be of a not writeable 
directory or passing a tilde to connect to the database.

As far as I know, this isn't the case with my program.
Here is some code:


# Get the application data folder
# This will return: C:\Documents and Settings\user\Application Data\myapp
PREFDIR = os.path.join(os.environ['APPDATA'], 'myapp')

# Connect to the database
def db_connect():
conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) # This 
line gives the error

conn.text_factory = str
cursor = conn.cursor()
return (conn, cursor)


I noticed that the 2 users that got the error are Russian, so I 
thought that the Russian characters would cause problems. I tried on 
my Linux and Windows machines with some Russian names, but thet 
doesn't seem the problem.


Some help is appreciated :-).

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

Is that folder write-able for the user/application under Windows Vista/7 
?  There are some older games for example that cannot be installed in 
the default locations due to being denied write access to their own data.


--
Kind Regards,
Christian Witts


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


Re: [Tutor] Cannot open SQLite database

2010-02-24 Thread Alan Gauld


"Timo"  wrote


OperationalError: unable to open database file


Does the file actually exist where you say it does?


# This will return: C:\Documents and Settings\user\Application Data\myapp
PREFDIR = os.path.join(os.environ['APPDATA'], 'myapp')


Do you check that APPDATA is actually set?


# Connect to the database
def db_connect():
conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) # This


Do you do an is file exists check before trrying to use it?
And that the permissions on both directory and file are set up correctly?

That would be my guess at likely errors.


--
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] Strange list behaviour in classes

2010-02-24 Thread Alan Gauld


"James Reynolds"  wrote


This thread inspired me to start learning object oriented as well, but it
seems I must be missing something fundamental.


No, this has nothing to do with objects, its just a broken algorithm.


   median = Statistics.stats.median(*a)
   n = (self.value[m] + self.value[m+1]) / 2
IndexError: tuple index out of range

def median(self, *value_list):
 if len(self.value) % 2 == 1:
  m = (len(self.value) - 1)/2
  n = self.value[m+1]
 else:
  m = len(self.value) / 2
  m = int(m)
  n = (self.value[m] + self.value[m+1]) / 2
 return n


Consider the case where the length of value is 2.
We will use the else part of the branch.
m will have value 1
The highest index is 1 because indexes start at zero
so value[m+1] will fail with an index error.

A similar error will happen when the list has only 1 entry
since the highest index there will be zero.

You need to rethink your rules for generating the indexes
remembering that they start at zero.


--
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] Verifying My Troublesome Linkage Claim between Python andWin7

2010-02-24 Thread Alan Gauld


"Wayne Watson"  wrote

My claim is that if one creates a program in a folder that reads a file 
in the folder it and then copies it to another folder, it will read  the 
data file in the first folder, and not a changed file in the new folder.


Thats what I would expect in any OS.
Unless you explicitly change the folder name the program will
still be using the original file. You would need to explicitly tell
Python to open the new copied file.

My experience is that if one checks the properties of the copied file, it 
will point to the original py file and execute it and not the copy.


OK, Now I'm confused. If you copied the file it should not reference the 
original.

How are you copying it? Are you using shutil.copy()?

If win7 is the culprit, I would think this is a somewhat  serious 
problem.


It seems unlikely to be W7.

It may be the sample program is not representative of the larger program 
that has me stuck. If necessary I can provide it. It uses common modules. 
(Could this be something like the namespace usage of variables that share 
a common value?)


No idea, please post it if it is reasonably short.
Working from code is always more precise than from text descriptions.


# To verify my situation use IDLE, save and run this program there.
# Put this program into a folder along with a data file
# called verify.txt. Create a single text line with a few characters in 
it

# Run this program and note the output
# Copy the program and txt file to another folder
# Change the contents of the txt file
# Run it again, and see if the output is the same as in the other folder
track_file = open("verify.txt")
aline = track_file.readline();
print aline
track_file.close()


OK, This is not what I thought you meant from your description!
You are copying the files as a user.
How are you doing that?
- from a command window?
- Or using Windows Explorer?
- Using drag n drop or copy/paste?
Still a lot of variables.

What happens if you run the program from the command line rather than IDLE?
Did you close and restart IDLE between runs?

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

2010-02-24 Thread Alan Gauld


"Shurui Liu (Aaron Liu)"  wrote in message 
news:2b9003cf1002231958s34b701b6sc69408a0b855a...@mail.gmail.com...

This time is not my assignment, I promise.

In python, when we want to list numbers, we use the command "range", 
like,
if we want to list integer from 0 to 9, we can write: range(10); if we 
want

to list integer from 10 to 29, we can write: range(10,30). I was going to
show a list of number from 1.0 to 1.9, and I did this in the same way as
integer: range(1.0,2.0,0.1), but it doesn't work. Can you help me? Thank
you!

--
Shurui Liu (Aaron Liu)
Computer Science & Engineering Technology
University of Toledo








___
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] common (sub)attributes

2010-02-24 Thread spir
Hello,

Just a really basic note:
Classes are often used to hold common or default attribute. Then, some of these 
attrs may get custom values for individual objects of a given type. Simply by 
overriding the attr on this object (see code below). But this will not only if 
the attr is a top-level one; not if it is itself part of a composite object. In 
the latter case, the (sub)attribute is still shared, so the change affects 
everybody. So, we must redefine the whole top-level attr instead. Trivial, but 
not obvious for me ;-)


=== sample code ===
#!/usr/bin/env python
# coding: utf-8

# case x is top-level attr
class C(object):
x = 0
y = 0
a = 'u'
def __str__ (self) :
return "C(%s,%s,%s)" %(self.x,self.y,self.a)
c1 = C() ; c2 = C()
c1.x = 1# change x for c1 only
c1.a = 'v'  # change a for c1 only
print c1,c2 # ==> C(1,0,v) C(0,0,u)

# case x is element of composite attr
class P:
x = 0
y = 0
class C(object):
p = P()
a = 'u'
def __str__ (self) :
return "C(%s,%s,%s)" %(self.p.x,self.p.y,self.a)
c1 = C() ; c2 = C()
c1.p.x = 1  # change x for c1, but x is actually shared
c1.a = 'v'  # change a for c1 only
print c1,c2 # ==> C(1,0,v) C(1,0,u)


Denis


la vita e estrany

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


[Tutor] Using Matplotlib - program still running when graph window is closed

2010-02-24 Thread AG
How does one ensure that once a graph has been produced by Matplotlib 
and that graph has been closed by the user that the program itself stops?


What I am currently getting is that when I close the graph pop-up window 
and then close IDLE, I get a message that the program is still running 
and am I sure that I want to stop it.  Yes, I am sure, but I don't want 
to have to keep killing the IDLE interpreter window in order to do so, 
but if I don't, then I am seemingly unable to produce another graph 
pop-up window.


How do I control this from within the script itself?

TIA

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


Re: [Tutor] Cannot open SQLite database

2010-02-24 Thread spir
On Wed, 24 Feb 2010 09:38:56 +0100
Timo  wrote:

> Hello all, my program uses SQLite as database and everything worked fine 
> until now.
> I have over 3000 downloads of my program, but I got 2 mails this week 
> from people who get this error:
> 
> OperationalError: unable to open database file
> 
> I searched the net, and all problems seem to be of a not writeable 
> directory or passing a tilde to connect to the database.
> As far as I know, this isn't the case with my program.
> Here is some code:
> 
> 
> # Get the application data folder
> # This will return: C:\Documents and Settings\user\Application Data\myapp
> PREFDIR = os.path.join(os.environ['APPDATA'], 'myapp')
> 
> # Connect to the database
> def db_connect():
>  conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db')) # This 
> line gives the error
>  conn.text_factory = str
>  cursor = conn.cursor()
>  return (conn, cursor)

I would first add debug lines

print os.path.join(PREFDIR, 'myapp.db')
print os.path.exists(os.path.join(PREFDIR, 'myapp.db'))
print open(os.path.join(PREFDIR, 'myapp.db'), 'r')
print open(os.path.join(PREFDIR, 'myapp.db'), 'a')
conn = sqlite3.connect(os.path.join(PREFDIR, 'myapp.db'))

to check name, existence, read access right, write access right, of the file.

Denis


la vita e estrany

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


[Tutor] distutils and the postinstallation script

2010-02-24 Thread Tim Brown
Hi,

I've written an installation, for Windows, using distutils to produce a .exe 
setup file.
When the setup.exe is run, can my postinstallation script can find out which 
folder the setup.exe is in ?

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


Re: [Tutor] What Editori?

2010-02-24 Thread Giorgio
>
>
>>
>> Are you sure you're not mixing spaces and tabs?
>

Yes i'm sure. I'm using the "tab button" and the same button works perfectly
in the IDLE editor.



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


Re: [Tutor] ask

2010-02-24 Thread Kent Johnson
On Tue, Feb 23, 2010 at 10:58 PM, Shurui Liu (Aaron Liu)
 wrote:
> This time is not my assignment, I promise.
>
> In python, when we want to list numbers, we use the command "range", like,
> if we want to list integer from 0 to 9, we can write: range(10); if we want
> to list integer from 10 to 29, we can write: range(10,30). I was going to
> show a list of number from 1.0 to 1.9, and I did this in the same way as
> integer: range(1.0,2.0,0.1), but it doesn't work. Can you help me? Thank
> you!

Right, range() only works with integer arguments. You can use a list
comprehension to convert to floats:

In [2]: print [ x/10.0 for x in range(10, 20) ]
[1.0, 1.1001, 1.2, 1.3, 1.3999, 1.5,
1.6001, 1.7, 1.8, 1.8999]

The rounding errors are to be expected, see this link for an explanation:
http://docs.python.org/tutorial/floatingpoint.html

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


[Tutor] DreamPie - The Python shell you've always dreamed about!

2010-02-24 Thread Kent Johnson
This looks promising and probably of interest to some on this list.

Kent

-- Forwarded message --
From: Noam Yorav-Raphael 
To: python-announce-l...@python.org
Date: Sun, 21 Feb 2010 11:39:02 +0200
Subject: DreamPie - The Python shell you've always dreamed about!
I'm pleased to announce DreamPie 1.0 - a new graphical interactive Python shell!

Some highlights:

* Has whatever you would expect from a graphical Python shell -
attribute completion, tooltips which show how to call functions,
highlighting of matching parentheses, etc.
* Fixes a lot of IDLE nuisances - in DreamPie interrupt always works,
history recall and completion works as expected, etc.
* Results are saved in the Result History.
* Long output is automatically folded so you can focus on what's important.
* Jython and IronPython support makes DreamPie a great tool for
exploring Java and .NET classes.
* You can copy any amount of code and immediately execute it, and you
can also copy code you typed interactively into a new file, with the
Copy Code Only command. No tabs are used!
* Free software licensed under GPL version 3.

Check it out at http://dreampie.sourceforge.net/ and tell me what you think!

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


Re: [Tutor] What Editori?

2010-02-24 Thread Eike Welk
On Wednesday February 24 2010 14:14:49 Giorgio wrote:

> Yes i'm sure. I'm using the "tab button" and the same button works
>  perfectly in the IDLE editor.

The editors could be configured differently with respect to the "tab button". 
Some may insert tabs some may insert spaces. 

EMACS can even mix tabs and spaces; it can replace long runs of space 
characters with the equivalent, smaller number of tabs (IMHO). Here is a 
somewhat related comic:
http://xkcd.com/378/

By the way, I'm using Eclipse/Pydev.


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


Re: [Tutor] What Editori?

2010-02-24 Thread Giorgio
> The editors could be configured differently with respect to the "tab
> button".
> Some may insert tabs some may insert spaces.
>

Ok, found the problem. I'm inserting tabs, but the original file only has
spaces.

Do you think i can look for a function that automatically inserts a certain
number of spaces when i press tab?


>
> By the way, I'm using Eclipse/Pydev.
>
>
NetBeans is a good alternative, and supports py in the new beta plugin.

I've also tried eclipse/aptana/pydev but think they're too difficult!


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


Re: [Tutor] Using Matplotlib - program still running when graph window is closed

2010-02-24 Thread Eike Welk
On Wednesday February 24 2010 11:23:18 AG wrote:
> How does one ensure that once a graph has been produced by Matplotlib
> and that graph has been closed by the user that the program itself stops?
> 
> What I am currently getting is that when I close the graph pop-up window
> and then close IDLE, I get a message that the program is still running
> and am I sure that I want to stop it.  Yes, I am sure, but I don't want
> to have to keep killing the IDLE interpreter window in order to do so,
> but if I don't, then I am seemingly unable to produce another graph
> pop-up window.

There seem to be erroneous interference between Idle and Matplotlib. Similar 
problems are reported on the Matplotlib list too. The list for Matplotlib 
users is here:
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

You could also try out the Spyder IDE, which reportedly works well with 
Matplotlib.
http://packages.python.org/spyder/


Eike.


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


Re: [Tutor] Using Matplotlib - program still running when graph window is closed

2010-02-24 Thread AG

Eike Welk wrote:

On Wednesday February 24 2010 11:23:18 AG wrote:
  

How does one ensure that once a graph has been produced by Matplotlib
and that graph has been closed by the user that the program itself stops?

What I am currently getting is that when I close the graph pop-up window
and then close IDLE, I get a message that the program is still running
and am I sure that I want to stop it.  Yes, I am sure, but I don't want
to have to keep killing the IDLE interpreter window in order to do so,
but if I don't, then I am seemingly unable to produce another graph
pop-up window.



There seem to be erroneous interference between Idle and Matplotlib. Similar 
problems are reported on the Matplotlib list too. The list for Matplotlib 
users is here:

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

You could also try out the Spyder IDE, which reportedly works well with 
Matplotlib.

http://packages.python.org/spyder/


Eike.


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

  


Thanks Eike, that was helpful.  At least it reassures me that I am not 
screwing something up.  Spyder looks interesting, but I think is 
probably far more than I need and may be a little dubious installing it 
on Debian.  But I may want to give it a more considered rethink.


Cheers

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


Re: [Tutor] ask

2010-02-24 Thread Steven D'Aprano
On Wed, 24 Feb 2010 02:58:52 pm Shurui Liu (Aaron Liu) wrote:
> This time is not my assignment, I promise.
>
> In python, when we want to list numbers, we use the command "range",
> like, if we want to list integer from 0 to 9, we can write:
> range(10); if we want to list integer from 10 to 29, we can write:
> range(10,30). I was going to show a list of number from 1.0 to 1.9,
> and I did this in the same way as integer: range(1.0,2.0,0.1), but it
> doesn't work. Can you help me? Thank you!

Hope this helps:

http://code.activestate.com/recipes/577068-floating-point-range/


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


[Tutor] Regular expression generator

2010-02-24 Thread Kent Johnson
Another interesting tool - you give it a sample string and it helps
you build a regular expression to match the string. This is not a
regex tester, it actually creates the regex for you as you click on
elements of the string.
http://txt2re.com/index-python.php3

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


Re: [Tutor] What Editori?

2010-02-24 Thread Giorgio
And, what about more powerful editors? I mean editors with features like
SVN/GIT management and  so on.

I use Netbeans, but also know Eclipse/Aptana.

>


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


Re: [Tutor] What Editori?

2010-02-24 Thread William Witteman
On Wed, Feb 24, 2010 at 04:40:07PM +0100, Giorgio wrote:
>And, what about more powerful editors? I mean editors with features like SVN/
>GIT management and  so on.

I think you'll find that there is extensive version control integration
in most/all of the "less powerful" editors.  Certainly you would find
many who would (perhaps strenuously) refute a suggestion that
vi[m]|emacs are not "powerful".

Of the many editors mentioned in this thread at least vim, emacs and geany 
have integration available for any version control system.
-- 

yours,

William

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


Re: [Tutor] What Editori?

2010-02-24 Thread Ricardo Aráoz
Lowell Tackett wrote:
> The environment [OS} of choice can do a lot to expand/enhance the
> capabilities of an editor.  I fell upon Vim from the beginning, and
> stayed with it for its' rich palate of features and adaptability (and
> of course, the often...and exhilarating "oh, Vim can do that!").  But
> beyond that, the Linux platform I work within offers its own dimension.
>
> Generally, I will split a [terminal] screen into two (or even 3)
> virtual screens with bash's 'screen' workhorse, and from there I have
> in front of me [perhaps] a; 1) script edit screen, 2) interactive
> screen, and 3) script-launching screen...all on the same physical monitor.
>
> For me, that combination creates an awfully rich & deep working
> canvas.   The whole...is at least as great as the sum of its' parts.
>

LOL
Any modern editor can do that!
In SPE I'm just now working in 7 scripts at a time (a few of them just
for checking stuff) each in it's own tab, a python shell, locals window,
output window, to do window, procedure index, object index, notes
window, directory explorer window, each in its own tab. And on top of
that I can execute a program and debug it through winpdb, test regular
expressions, check code with pyChecker, run in a terminal the current
script with or without arguments, design a GUI with wxGlade or XRC, and
many other actions all a menu choice away.
And nobody would claim this is extraordinary.
Vim has other advantages for an expert user (but you have a steep
learning curve) but what you mention are hardly outstanding issues nowadays.

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


[Tutor] strange bidi requirement

2010-02-24 Thread rick
I'm trying to write a math quiz program that replicates an old book on
arithmetic.

when it comes to summing a long column, I need to read the answer from
the user, one digit at a time.

so, if the answer should be something like

14238.83

I would need to read the .03, then the .8, and so on.   I figure all
strings, cast to int  (well, for this example, float).  Would this be
easier if I learned some GUI programming?   Can it be done at all in
just console?

I'd post some code, but I'm completely clueless on this one.  RTFM
replies are welcome, just point me to the section I need to read!

thanks,
Rick

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


Re: [Tutor] What Editori?

2010-02-24 Thread Hansen, Mike
I'm surprised no one has mentioned ActiveState's KOMODO. I primarily use VIM, 
but I hop into KOMODO for doing little scripts and watching the output. KOMODO 
comes in two flavors, KOMODO Edit which is free and KOMODO IDE which costs 
about $300. I suspect that KOMODO Edit does most of what people need. 
For those that don't want the steep learning curve of VIM or Emacs, I'd 
recommend KOMODO. 

Mike
No, I don't work for ActiveState. =)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] strange bidi requirement

2010-02-24 Thread Benno Lang
On 25 February 2010 01:34, rick  wrote:
> I'm trying to write a math quiz program that replicates an old book on
> arithmetic.
>
> when it comes to summing a long column, I need to read the answer from
> the user, one digit at a time.
>
> so, if the answer should be something like
>
> 14238.83
>
> I would need to read the .03, then the .8, and so on.   I figure all
> strings, cast to int  (well, for this example, float).  Would this be
> easier if I learned some GUI programming?   Can it be done at all in
> just console?

If you really want just console, you'll probably need to use curses:
http://docs.python.org/library/curses.html

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


Re: [Tutor] What Editori?

2010-02-24 Thread Mike
I use and love Pyscripter for Windows.  I guess I am used to the Delphi IDE.

Mike

On Tue, Feb 23, 2010 at 11:24 AM, Giorgio  wrote:
> Hi All,
> what text-editor do you use for python?
> I've always used kate/nano on Linux and Notepad++/PSPad on Win. Today i've
> installed portablepython to my pendrive, and found pyscripter in that. It's
> nice!
> Do you think it's a good editor? Do you know other names?
> Giorgio
>
> --
> --
> AnotherNetFellow
> Email: anothernetfel...@gmail.com
>
> ___
> Tutor maillist  -  tu...@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] Python-based address standardization script?

2010-02-24 Thread Serdar Tumgoren
Hey folks,
Anyone know if there's a Python script floating out there to standardize
U.S. addresses? I couldn't find anything on ActiveState or by Googling...I'm
interested in something similar to this Perl module:

http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/US.pm

Just curious if anyone's seen an implementation in Python.

As always, the pointers are appreciated.

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


Re: [Tutor] What Editori?

2010-02-24 Thread Alan Gauld


"Giorgio"  wrote


And, what about more powerful editors? I mean editors with features like
SVN/GIT management and  so on.


THose aren't really editor features but IDE features. But thats probably 
splityting hairs.
But emacs does that too and truly is a powerful editor. Certainly more so 
than

the standard editor in many of the modern IDEs


I use Netbeans, but also know Eclipse/Aptana.


I liked Netbeans but Eclipse has become the de-facto IDE at work so I now 
use that.
Its OK, but not outstanding. I like all the plugins etc but as an editor it 
is nothing

special. As an IDE its pretty neat.

Alan G.


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


Re: [Tutor] common (sub)attributes

2010-02-24 Thread Alan Gauld

"spir"  wrote


Classes are often used to hold common or default attribute.


True


Then, some of these attrs may get custom values for individual
objects of a given type. Simply by overriding the attr on this object


Yes, but then the arttribute becomes an instance attribute which masks
the class one (unless accessed via the class).

But this will not only if the attr is a top-level one; not if it is 
itself part

of a composite object.


Sorry, you lost me there!


In the latter case, the (sub)attribute is still shared, so the change
affects everybody. So, we must redefine the whole top-level attr
instead. Trivial, but not obvious for me ;-)


Nope, Didn't understand that either. Not sure what you mean by
a sub-attribute or top-level attribute?.


=== sample code ===
#!/usr/bin/env python
# coding: utf-8

# case x is top-level attr
class C(object):
x = 0
y = 0
a = 'u'
def __str__ (self) :
return "C(%s,%s,%s)" %(self.x,self.y,self.a)


Using an instance to access class attributes is IMHO a bad idea.
Better to always access class attributes via the class, it makes it
obvious what you are working with.


c1 = C() ; c2 = C()
c1.x = 1 # change x for c1 only
c1.a = 'v' # change a for c1 only


Really creating new instance attributes called x and a



print c1,c2 # ==> C(1,0,v) C(0,0,u)



# case x is element of composite attr
class P:
x = 0
y = 0
class C(object):
p = P()
a = 'u'
def __str__ (self) :
return "C(%s,%s,%s)" %(self.p.x,self.p.y,self.a)
c1 = C() ; c2 = C()
c1.p.x = 1 # change x for c1, but x is actually shared


Yes but try P.x, it will have the original value of 0
You are again creating a new instance attribute in the instance
of P which is shared by the class C.


c1.a = 'v' # change a for c1 only


Create a new instance variable a

This is consistent with how Python deals with ocal variables in a function:

x = 0

print x
def f()
   print x
   x = 5
   print x

print x

prints

0
0
5
0

The 5 is local to the function and only exists for the duration of the code 
execution.
In the case of an instance the "local" or instyance barianle lasts for the 
life of
the object. The class variable last for the life of the class. (and in 
Python you
can del() a class... to end its life - but it will exist until the last 
instance is

destroyed because each instance has a reference to it.)

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] strange bidi requirement

2010-02-24 Thread Alan Gauld
"rick"  wrote in message 
news:1267029242.9270.13.ca...@rick-desktop...

14238.83

I would need to read the .03, then the .8, and so on.   I figure all
strings, cast to int  (well, for this example, float).  Would this be
easier if I learned some GUI programming?   Can it be done at all in
just console?


It can but you probably need to fake it by storting the previous anser then
pre-pending the next character. You can do it by using one of the console
I/O libraries to delete the line and rewrite it - conio is one such.

In pseudocode

ans = ''

while not done
  ch = getch()
  ans = ch+ans
  delete line
  print line,

Hope thats enough to start.

Another option for a known length is to use cursor positioning commands
to move the cursor from right to left with each character.

Finally you could use curses, but thats just a pseudo GUI for consoles!

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] strange bidi requirement

2010-02-24 Thread Steven D'Aprano
On Thu, 25 Feb 2010 03:34:02 am rick wrote:
> I'm trying to write a math quiz program that replicates an old book
> on arithmetic.
>
> when it comes to summing a long column, I need to read the answer
> from the user, one digit at a time.
>
> so, if the answer should be something like
>
> 14238.83
>
> I would need to read the .03, then the .8, and so on.   I figure all
> strings, cast to int  (well, for this example, float).  Would this be
> easier if I learned some GUI programming?

Hell no! GUI programming is a whole new lot of stuff to learn! It's 
worth learning if you want a GUI interface, but not because it makes 
other things easier.


> Can it be done at all in just console?


Do you actually need to ask the user for one digit at a time? I don't 
imagine so, but could be wrong. So you can ask the user for the number 
at the console, and then process it in reverse:

>>> s = raw_input("Please enter a decimal number: ")
Please enter a decimal number: 123.456
>>>
>>> print s
123.456
>>>
>>> for c in reversed(s):
... print c
...
6
5
4
.
3
2
1


Hope this helps.




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


Re: [Tutor] Strange list behaviour in classes

2010-02-24 Thread ALAN GAULD
Forwarding to the list. 
Please alweays use Reply All so others can comment too.

I made a few changes, but I'm getting the same error on variance (see below):
Looks like a different error to me!

It would seem to me that it should never evaluate if the denominator is zero 
because of the if statement.
But the if statement doesn't test whether len(self.square_list) is zero. 
It tests whether self.value is zero.

The other thing I don't understand is that the object that is being passed into 
the instance is a list, but it seems to be a tuple once the object is called. 
Why does this happen? Am I doing something additional wrong?
>
>
Sorry, your code doesn't show any instances let alone lists being pased in?

Alan G.


  File "C:\Python31\Lib\COI\Project\Statistics.py", line 39, in variance
>var = sum(self.square_list) / len(self.square_list)
>ZeroDivisionError: int division or modulo by zero
>
>
>#!/usr/bin/python
># Filename: Statistics.py
>
>
>import math
>value_list = []
>class Statistics:
>def __init__(self, *value_list):
>self.value = value_list
>#self.average = mean(*value_list)
>self.square_list= []
>def mean(self, *value_list):
>if len(self.value) == 0:
>ave = 0
>else:
>ave = sum(self.value) / len(self.value)
>return ave
>
>
>def median(self, *value_list):
>if len(self.value) <= 2:
>n = self.mean(self.value)
>elif len(self.value) % 2 == 1:
>m = (len(self.value) - 1)/2
>n = self.value[m+1]
>else:
>m = len(self.value) / 2
>m = int(m)
>n = (self.value[m] + self.value[m+1]) / 2
>return n
>def variance(self, *value_list):
>if self.value == 0:
>var = 0
>else:
>average = self.mean(*self.value)
>for n in range(len(self.value)):
>square = (self.value[n] - average)**2
>self.square_list.append(square)
>var = sum(self.square_list) / len(self.square_list)
>return var
>
>
>
>On Wed, Feb 24, 2010 at 4:02 AM, Alan Gauld  wrote:
>
>
"James Reynolds"  wrote
>>
>>
>>
>This thread inspired me to start learning object oriented as well, but it
>>seems I must be missing something fundamental.
>>>
>>
>>No, this has nothing to do with objects, its just a broken algorithm.
>>
>>
>   median = Statistics.stats.median(*a)
>>>
>>   n = (self.value[m] + self.value[m+1]) / 2
>>IndexError: tuple index out of range
>>>
>>>
>>def median(self, *value_list):
>> if len(self.value) % 2 == 1:
>>  m = (len(self.value) - 1)/2
>>  n = self.value[m+1]
>> else:
>>  m = len(self.value) / 2
>>  m = int(m)
>>  n = (self.value[m] + self.value[m+1]) / 2
>> return n
>>>
Consider the case where the length of value is 2.
We will use the else part of the branch.
m will have value 1
The highest index is 1 because indexes start at zero
so value[m+1] will fail with an index error.
>>
A similar error will happen when the list has only 1 entry
since the highest index there will be zero.
>>
You need to rethink your rules for generating the indexes
remembering that they start at zero.
>>
>>
>>
-- 
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
>>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Strange list behaviour in classes

2010-02-24 Thread James Reynolds
Thanks for the reply.

I understand, but if self.value is any number other then 0, then the "for"
will append to the square list, in which case square_list will always have
some len greater than 0 when "value" is greater than 0?

I'm just trying to understand the mechanics. I'm assuming that isn't the way
the statement is evaluated?

When I get home from work I'll work around it.

Is this an occasion which is best suited for a try:, except statement? Or
should it, in general, but checked with "if's". Which is more expensive?

The rest of the code was in the prior email (please see below)

Module 1:

import Statistics

a = [1,2,3,4,5,6,7,8,9,10]

mean = Statistics.stats.mean(*a)
median = Statistics.stats.median(*a)
var = Statistics.stats.variance(*a)
print(mean, median, var)
print()

Module 2:

#!/usr/bin/python
# Filename: Statistics.py



import math
value_list = []
class Statistics:
def __init__(self, *value_list):
 self.value = value_list
#self.average = mean(*value_list)
self.square_list= []
 def mean(self, *value_list):
if len(self.value) == 0:
 ave = 0
else:
ave = sum(self.value) / len(self.value)
 return ave

def median(self, *value_list):
if len(self.value) <= 2:
 n = self.mean(self.value)
elif len(self.value) % 2 == 1:
m = (len(self.value) - 1)/2
 n = self.value[m+1]
else:
m = len(self.value) / 2
 m = int(m)
n = (self.value[m] + self.value[m+1]) / 2
return n
 def variance(self, *value_list):
if self.value == 0:
 var = 0
else:
average = self.mean(*self.value)
 for n in range(len(self.value)):
square = (self.value[n] - average)**2
 self.square_list.append(square)
var = sum(self.square_list) / len(self.square_list)
 return var

stats = Statistics(*value_list)






On Wed, Feb 24, 2010 at 5:59 PM, ALAN GAULD wrote:

> Forwarding to the list.
> Please alweays use Reply All so others can comment too.
>
> I made a few changes, but I'm getting the same error on variance (see
> below):
>
> Looks like a different error to me!
>
> It would seem to me that it should never evaluate if the denominator is
> zero because of the if statement.
>
> But the if statement doesn't test whether len(self.square_list) is zero.
> It tests whether self.value is zero.
>
> The other thing I don't understand is that the object that is being passed
> into the instance is a list, but it seems to be a tuple once the object is
> called. Why does this happen? Am I doing something additional wrong?
>
> Sorry, your code doesn't show any instances let alone lists being pased in?
>
> Alan G.
>
>   File "C:\Python31\Lib\COI\Project\Statistics.py", line 39, in variance
> var = sum(self.square_list) / len(self.square_list)
> ZeroDivisionError: int division or modulo by zero
>
> #!/usr/bin/python
> # Filename: Statistics.py
>
> import math
> value_list = []
> class Statistics:
> def __init__(self, *value_list):
>  self.value = value_list
> #self.average = mean(*value_list)
> self.square_list= []
>  def mean(self, *value_list):
> if len(self.value) == 0:
>  ave = 0
> else:
> ave = sum(self.value) / len(self.value)
>  return ave
>
> def median(self, *value_list):
> if len(self.value) <= 2:
>  n = self.mean(self.value)
> elif len(self.value) % 2 == 1:
> m = (len(self.value) - 1)/2
>  n = self.value[m+1]
> else:
> m = len(self.value) / 2
>  m = int(m)
> n = (self.value[m] + self.value[m+1]) / 2
> return n
>  def variance(self, *value_list):
> if self.value == 0:
>  var = 0
> else:
> average = self.mean(*self.value)
>  for n in range(len(self.value)):
> square = (self.value[n] - average)**2
>  self.square_list.append(square)
> var = sum(self.square_list) / len(self.square_list)
>  return var
>
>
> On Wed, Feb 24, 2010 at 4:02 AM, Alan Gauld wrote:
>
>>
>> "James Reynolds"  wrote
>>
>>
>>  This thread inspired me to start learning object oriented as well, but it
>>> seems I must be missing something fundamental.
>>>
>>
>> No, this has nothing to do with objects, its just a broken algorithm.
>>
>>median = Statistics.stats.median(*a)
>>>   n = (self.value[m] + self.value[m+1]) / 2
>>> IndexError: tuple index out of range
>>>
>>> def median(self, *value_list):
>>> if len(self.value) % 2 == 1:
>>>  m = (len(self.value) - 1)/2
>>>  n = self.value[m+1]
>>> else:
>>>  m = len(self.value) / 2
>>>  m = int(m)
>>>  n = (self.value[m] + self.value[m+1]) / 2
>>> return n
>>>
>>
>> Consider the case where the length of value is 2.
>> We will use the else part of the branch.
>> m will have value 1
>> The highest index is 1 because indexes start at zero
>> so value[m+1] will fail with an index error.
>>
>> A similar error will happen when the list has only 1 entry
>> since the highest index there will be zero.
>>
>> You need to rethink your rules for generating the indexes
>> remembering that they start at zero.
>>
>>
>>
>> --
>> Alan Gauld
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>>
>> ___
>> Tutor maillist  -  Tutor@python.org

Re: [Tutor] Python-based address standardization script?

2010-02-24 Thread Luke Paireepinart
On Wed, Feb 24, 2010 at 4:14 PM, Serdar Tumgoren wrote:

> Hey folks,
> Anyone know if there's a Python script floating out there to standardize
> U.S. addresses? I couldn't find anything on ActiveState or by Googling...I'm
> interested in something similar to this Perl module:
>
> http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/US.pm
>
> Just curious if anyone's seen an implementation in Python.
>
> I could really use this too, I haven't been able to find one.  Would you be
interested in porting the Perl one?  Maybe we could work on it if no one
knows of any Python versions?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python-based address standardization script?

2010-02-24 Thread Serdar Tumgoren
> Hey folks,
>> Anyone know if there's a Python script floating out there to standardize
>> U.S. addresses? I couldn't find anything on ActiveState or by Googling...I'm
>> interested in something similar to this Perl module:
>>
>> http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/US.pm
>>
>> Just curious if anyone's seen an implementation in Python.
>>
>> I could really use this too, I haven't been able to find one.  Would you
> be interested in porting the Perl one?  Maybe we could work on it if no one
> knows of any Python versions?
>

Hey Luke,

I'm definitely interested in porting it over and would be happy to
collaborate on that project. I know a lot of folks that would *love* to get
their hands on this type of code. Should I start a project on github? (I'm
open to other VCS's that offer free web hosting for open source...)

Anyone who wants to join in, just shout it out.

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


Re: [Tutor] Strange list behaviour in classes

2010-02-24 Thread Alan Gauld


"James Reynolds"  wrote

I understand, but if self.value is any number other then 0, then the 
"for"
will append to the square list, in which case square_list will always 
have

some len greater than 0 when "value" is greater than 0?


And if value does equal zero?

Actually I'm confused by value because you treat it as both an
integer and a collection in different places?


Is this an occasion which is best suited for a try:, except statement? Or
should it, in general, but checked with "if's". Which is more expensive?


try/except is the Python way :-)


def variance(self, *value_list):
   if self.value == 0:
var = 0
   else:
 average = self.mean(*self.value)
 for n in range(len(self.value)):
  square = (self.value[n] - average)**2
  self.square_list.append(square)
var = sum(self.square_list) / len(self.square_list)
return var



--
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] Python-based address standardization script?

2010-02-24 Thread Luke Paireepinart
On Wed, Feb 24, 2010 at 6:15 PM, Serdar Tumgoren wrote:

>
> Hey folks,
>>> Anyone know if there's a Python script floating out there to standardize
>>> U.S. addresses? I couldn't find anything on ActiveState or by Googling...I'm
>>> interested in something similar to this Perl module:
>>>
>>> http://search.cpan.org/~sderle/Geo-StreetAddress-US-0.99/US.pm
>>>
>>> Just curious if anyone's seen an implementation in Python.
>>>
>>> I could really use this too, I haven't been able to find one.  Would you
>> be interested in porting the Perl one?  Maybe we could work on it if no one
>> knows of any Python versions?
>>
>
> Hey Luke,
>
> I'm definitely interested in porting it over and would be happy to
> collaborate on that project. I know a lot of folks that would *love* to get
> their hands on this type of code. Should I start a project on github? (I'm
> open to other VCS's that offer free web hosting for open source...)
>
> Anyone who wants to join in, just shout it out.
>
> Sure, just send me a link when it's up.  I don't have any experience with
git but I've wanted to learn so go ahead and use that.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python-based address standardization script?

2010-02-24 Thread Serdar Tumgoren
> Sure, just send me a link when it's up.  I don't have any experience with
> git but I've wanted to learn so go ahead and use that.
>
>
Okay - the git repo is here:
http://github.com/zstumgoren/python-addressparser

You can clone it with the following shell command (assuming you've installed
git):

git clone git://github.com/zstumgoren/python-addressparser.git

And if it helps, here are a few git resources:

http://www.kernel.org/pub/software/scm/git/docs/
http://book.git-scm.com/
http://progit.org/book/

Anyone else interested in contributing, ping us offline or just clone away
and push back your changes.

Cheers!

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


Re: [Tutor] Strange list behaviour in classes

2010-02-24 Thread James Reynolds
Thank you! I think I have working in the right direction. I have one more
question related to this module.

I had to move everything to a single module, but what I would like to do is
have this class in a file by itself so I can call this from other modules.
when it was in separate modules it ran with all 0's in the output.

Here is the code in one module:

#import Statistics

class Statistics:
def __init__(self, *value_list):
self.value = value_list
self.square_list= []
 def mean(self, *value_list):
try :
ave = sum(self.value) / len(self.value)
except ZeroDivisionError:
ave = 0
return ave

def median(self, *value_list):
if len(self.value) <= 2:
n = self.mean(self.value)
elif len(self.value) % 2 == 1:
m = (len(self.value) - 1)/2
n = self.value[m+1]
else:
m = len(self.value) / 2
m = int(m)
n = (self.value[m-1] + self.value[m]) / 2
return n
 def variance(self, *value_list):
average = self.mean(*self.value)
for n in range(len(self.value)):
square = (self.value[n] - average)**2
self.square_list.append(square)
try:
var = sum(self.square_list) / len(self.square_list)
except ZeroDivisionError:
var = 0
return var

def stdev(self, *value_list):
var = self.variance(*self.value)
sdev = var**(1/2)
return sdev
 def zscore(self, x, *value_list):
average = self.mean(self.value)
sdev = self.stdev(self.value)
try:
z = (x - average) / sdev
except ZeroDivisionError:
z = 0
return z



a = [1,2,3,4,5,6,7,8,9,10]
stats = Statistics(*a)
mean = stats.mean(*a)
median = stats.median(*a)
var = stats.variance(*a)
stdev = stats.stdev(*a)
z = stats.zscore(5, *a)
print(mean, median, var, stdev, z)
print()



On Wed, Feb 24, 2010 at 7:33 PM, Alan Gauld wrote:

>
> "James Reynolds"  wrote
>
>  I understand, but if self.value is any number other then 0, then the "for"
>> will append to the square list, in which case square_list will always have
>> some len greater than 0 when "value" is greater than 0?
>>
>
> And if value does equal zero?
>
> Actually I'm confused by value because you treat it as both an
> integer and a collection in different places?
>
>
>  Is this an occasion which is best suited for a try:, except statement? Or
>> should it, in general, but checked with "if's". Which is more expensive?
>>
>
> try/except is the Python way :-)
>
>
>  def variance(self, *value_list):
>>   if self.value == 0:
>>var = 0
>>   else:
>> average = self.mean(*self.value)
>> for n in range(len(self.value)):
>>  square = (self.value[n] - average)**2
>>  self.square_list.append(square)
>>var = sum(self.square_list) / len(self.square_list)
>>return var
>>
>
>
> --
> 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
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Omitting lines matching a list of strings from a file

2010-02-24 Thread galaxywatcher
I am trying to output a list of addresses that do not match a list of  
State abbreviations. What I have so far is:


def main():
infile = open("list.txt", "r")
for line in infile:
state = line[146:148]
omit_states = ['KS', 'KY', 'MA', 'ND', 'NE', 'NJ', 'PR',  
'RI', 'SD', 'VI', 'VT', 'WI']

for n in omit_states:
if state != n:
print line
infile.close()
main()

This outputs multiple duplicate lines. The strange thing is that if I  
change 'if state == n:' then I correctly output all matching lines.  
But I don't want that. I want to output all lines that do NOT match  
the States in the omit_states list.


I am probably overlooking something very simple. Thanks in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Omitting lines matching a list of strings from a file

2010-02-24 Thread Christian Witts

galaxywatc...@gmail.com wrote:
I am trying to output a list of addresses that do not match a list of 
State abbreviations. What I have so far is:


def main():
infile = open("list.txt", "r")
for line in infile:
state = line[146:148]
omit_states = ['KS', 'KY', 'MA', 'ND', 'NE', 'NJ', 'PR', 'RI', 
'SD', 'VI', 'VT', 'WI']

for n in omit_states:
if state != n:
print line
infile.close()
main()

This outputs multiple duplicate lines. The strange thing is that if I 
change 'if state == n:' then I correctly output all matching lines. 
But I don't want that. I want to output all lines that do NOT match 
the States in the omit_states list.


I am probably overlooking something very simple. Thanks in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

The more pythonic way of doing it would be to use `not in` like listed 
below.  You should consider normalizing your input (state) by using 
.upper() unless you know for certain it's always upper-case.


state = line[146:148]
omit_states = ['KS', 'KY', ..., 'VT', 'WI']
if state not in omit_states:
   print line

--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: cwi...@compuscan.co.za

NOTE:  This e-mail (including attachments )is subject to the disclaimer 
published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6 



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


Re: [Tutor] Omitting lines matching a list of strings from a file

2010-02-24 Thread Reed O'Brien



def main():
   infile = open("list.txt", "r")
   for line in infile:
   state = line[146:148]
   omit_states = ['KS', 'KY', 'MA', 'ND', 'NE', 'NJ', 'PR',  
'RI', 'SD', 'VI', 'VT', 'WI']

   for n in omit_states:
   if state != n:
   print line
   infile.close()
main()


If state not in omit_states:
process_line(line)

~ro

--
Sent from a mobile device.

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