Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread Dave Angel

Alan Gauld wrote:

div class=moz-text-flowed style=font-family: -moz-fixed
TGW galaxywatc...@gmail.com wrote


I go the program functioning with
lines = [line for line in infile if line[149:154] not in match_zips]

But this matches records that do NOT match zipcodes. How do I get 
this  running so that it matches zips?



Take out the word 'not' from the comprehension?

That's one change.  But more fundamental is to change the file I/O.  
Since there's no seek() operation, the file continues wherever it left 
off the previous time.


I'd suggest reading the data from the match_zips into a list, and if the 
format isn't correct, doing some post-processing on it.  But there's no 
way to advise on that since we weren't given the format of either file.


zipdata = match_zips.readlines()
Then you can do an  if XXX in zipdata with assurance.

DaveA

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


Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread ALAN GAULD
Please use Reply All whern responding to the list.

 

 lines = [line for line in infile if line[149:154] not in match_zips]

Nope. I tried that. I actually modified your comprehension 
that you provided about a month ago. 
Works great for NOT matching, but can't figure out how to match. 
Do you have another suggestion?def main():

 infile = open(/Users/tgw/NM_2010/NM_APR.txt, r)
 outfile = open(zip_match_apr_2010.txt, w)
 match_zips = open(zips.txt, r)

You probably are best to read the zips file into a list, 
stripping the newlines: 


matchzips = [match.strip() for match in open('zips.txt')]

then

 lines = [line for line in infile if line[149:154] in match_zips] 
Should work...
Either that or add a newline to the end of the slice.

HTH,

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


Re: [Tutor] Menu data from file

2010-04-05 Thread Neven Goršić
Thank you for mentioning the possible options.
I already use option where I import .py module,
but I run into troubles when making executable with py2exe.

I suppose that XML approach is the most general method.
Can you recommend a good introduction text (not for experts :-))
and give more details about the tool ElementCTree.

Maybe it will be educational and interesting for other beginners too.

Thanks,

Neven

-

On Mon, Apr 5, 2010 at 2:19 AM, Lie Ryan lie.1...@gmail.com wrote:

 On 04/05/10 08:54, Alan Gauld wrote:
  Thats right you will need to parse the data to convert it into the
  format you want.
  This is one reason you might find it easier to use XML for storing the
 data
  and use a tool like ElementCTree to parse it.

 s/ElementCTree/ElementTree/?

 ___
 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] Matching zipcode in address file

2010-04-05 Thread bob gailer

On 4/5/2010 1:15 AM, TGW wrote:

Sorry - my mistake - try:

infile = open(filex)
match_zips = open(zippys)
result = [line for line in infile if line in match_zips]
print result
When I apply the readlines to the original file, It is taking a lot 
longer to process and the outfile still remains blank. Any suggestions?


OK - you handled the problem regarding reading to end-of-file. Yes it 
takes a lot longer, because now you are actually iterating through 
match_zips for each line.


How large are these files? Consider creating a set from match_zips. As 
lists get longer, set membership test become faster than list membership 
test.


If the outfile is empty that means that line[149:154] is never in 
match_zips.


I suggest you take a look at match_zips. You will find a list of strings 
of length 6, which cannot match line[149:154], a string of length 5.




#!/usr/bin/env python
# Find records that match zipcodes in zips.txt

import os
import sys

def main():
infile = open(/Users/tgw/NM_2010/NM_APR.txt, r)
outfile = open(zip_match_apr_2010.txt, w)
zips = open(zips.txt, r)
match_zips = zips.readlines()
lines = [ line for line in infile if line[149:154] in match_zips ]

outfile.write(''.join(lines))
#print line[149:154]
print lines
infile.close()
outfile.close()
main()






--
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] Menu data from file

2010-04-05 Thread Lie Ryan
On 04/05/10 17:39, Neven Goršić wrote:
 Thank you for mentioning the possible options.
 I already use option where I import .py module,
 but I run into troubles when making executable with py2exe.

Maybe you should elaborate what problems you're experiencing with
py2exe? Probably we can solve that instead of developing workarounds.

Many popular UI toolkit provides an XML-based menu file.
Here is one for PyGTK:
http://www.pygtk.org/pygtk2tutorial/sec-UIManager.html

You should better use your GUI toolkit's version instead of writing your
own.

 I suppose that XML approach is the most general method.
 Can you recommend a good introduction text (not for experts :-))
 and give more details about the tool ElementCTree.
 
 Maybe it will be educational and interesting for other beginners too.

ElementTree is quite simple, though I don't think there are many good
tutorials about it (at least I can't find one). In the simplest use
case, you just use xml.etree.ElementTree.parse(menu.xml) and you can
iterate the tree like so:

import xml.etree.cElementTree as Et
menuxml = 
menubar
  menu title=File
menuitem title=Open File... action=open_file/
menu title=Recent Files
  menuitem title=placeholder_1 action=recent_1/
  menuitem title=placeholder_2 action=recent_2/
  menuitem title=placeholder_3 action=recent_3/
/menu
menuitem title=Exit My Program action=quit/
  /menu
  menu title=Edit
menuitem title=Copy action=copy_clipboard/
menuitem title=Paste action=paste_clipboard/
  /menu
/menubar

# root = Et.parse(menu.xml)
root = Et.fromstring(menuxml)

def parse(menutk, element):
for menu in mbar:
if menu.tag == 'menu':
# have submenus, recurse
submenutk = add_menu(menutk)
parse(submenutk, menu)
elif emnu.tag == 'menuitem':
add_menuitem(menutk, menuitem)

or something like that.

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


[Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis
I am working an a open source project and would like to add feature to a
class.
Current action:
in:b = BString.new('I am a BString object')
out:  b
in:BString - Python:0x1044846c8 / R:0x105c86f50
in:print(b)
out:   21-letter BString instance
   seq: I am a BString object

What I would like is to be able to
in   b
out 21-letter BString instance
   seq: I am a BString object

I have 2 questions
1, how do I do this?
2, how does print know what to do?

I have a lot to learn so pointing me in the right direction or to
documentation is as useful as the correct code.

Thanks

  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog http://vincentdavis.net |
LinkedInhttp://www.linkedin.com/in/vincentdavis
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Wayne Werner
On Mon, Apr 5, 2010 at 12:08 PM, Vincent Davis vinc...@vincentdavis.netwrote:

 I am working an a open source project and would like to add feature to a
 class.
 Current action:
 in:b = BString.new('I am a BString object')
 out:  b
 in:BString - Python:0x1044846c8 / R:0x105c86f50
 in:print(b)
 out:   21-letter BString instance
seq: I am a BString object

 What I would like is to be able to
 in   b
 out 21-letter BString instance
seq: I am a BString object

 I have 2 questions
 1, how do I do this?
 2, how does print know what to do?

 I have a lot to learn so pointing me in the right direction or to
 documentation is as useful as the correct code.


Take a look at the repr and str methods:
 http://docs.python.org/reference/datamodel.html#basic-customization

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


[Tutor] Scraping gov site: site looking for Flash player

2010-04-05 Thread Roy Hinkelman
Interesting.

I am using urllib2 to open some government pages, and they have some js
checking for Flash on my computer.

Is there a way to show them that I have flash? Or possibly another solution?

My code:
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = {'User-Agent' : user_agent}
req = urllib2.Request(_URL, None, headers)
data = mechanize.urlopen(req)
_soup = B_S(data)

And what I get back from 'print _soup':
!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN
html
headtitleWelcome to OurDocuments.gov/title
!-- Prevent caching in netscape. Very important because netscape tends to
mangle cached code --
meta http-equiv=expires content=0 /
meta http-equiv=Pragma content=no-cache /
script language=JavaScript type=text/javascript

!--

// moock fpi [f.lash p.layer i.nspector]

// version: 1.3.5

// written by colin moock

// code maintained at:
http://www.moock.org/webdesign/flash/detection/moockfpi/

// terms of use posted at: http://www.moock.org/terms/



//
=

// These are the user defined globals.

// Modify the following variables to customize the inspection behaviour.



var requiredVersion = 6;   // Version the user needs to view site (max 6,
min 2)



var useRedirect = true;// Flag indicating whether or not to load a
separate

   // page based on detection results. Set to true
to

   // load a separate page.



// Only set next three vars if useRedirect is true...which it is...

var flashPage = /doc.php?flash=truedoc=2; // The location of the
flash movie page

var noFlashPage = /doc.php?flash=falsedoc=2;  // Page displayed if the
user doesn't have the

  // plugin or we can't detect it.



var upgradePage = /doc.php?flash=olddoc=2;  // Page displayed if we
detect an old plugin

//
=



// *

// Everything below this point is internal until after the BODY tag.

// Do not modify! Proceed to the BODY tag for further instructions.

// *



// System globals

var flash2Installed = false;// boolean. true if flash 2 is installed

var flash3Installed = false;// boolean. true if flash 3 is installed

var flash4Installed = false;// boolean. true if flash 4 is installed

var flash5Installed = false;// boolean. true if flash 5 is installed

var flash6Installed = false;// boolean. true if flash 6 is installed

var maxVersion = 6; // highest version we can actually detect

var actualVersion = 0;  // will become the version the user really
has

var hasRightVersion = false;// boolean. will become true if it's safe to
embed the flash movie in the page

var jsVersion = 1.0;// the version of javascript supported



// --

/script
script language=JavaScript1.1 type=text/javascript

!--



// Check the browser...we're looking for ie/win

var isIE = (navigator.appVersion.indexOf(MSIE) != -1) ? true : false;
// true if we're on ie

var isWin = (navigator.appVersion.indexOf(Windows) != -1) ? true : false;
// true if we're on windows



// This is a js1.1 code block, so make note that js1.1 is supported.

jsVersion = 1.1;



// Write vbscript detection on ie win. IE on Windows doesn't support regular

// JavaScript plugins array detection.

if(isIE  isWin) {

  document.write('SCR' + 'IPT LANGUAGE=VBScript\ \n');

  document.write('on error resume next \n');

  document.write('flash2Installed =
(IsObject(CreateObject(ShockwaveFlash.ShockwaveFlash.2))) \n');

  document.write('flash3Installed =
(IsObject(CreateObject(ShockwaveFlash.ShockwaveFlash.3))) \n');

  document.write('flash4Installed =
(IsObject(CreateObject(ShockwaveFlash.ShockwaveFlash.4))) \n');

  document.write('flash5Installed =
(IsObject(CreateObject(ShockwaveFlash.ShockwaveFlash.5))) \n');

  document.write('flash6Installed =
(IsObject(CreateObject(ShockwaveFlash.ShockwaveFlash.6))) \n');

  document.write('/scr' + 'ipt\ \n'); // break up end tag so it doesn't
end our script

}

// --

/script
script language=JavaScript type=text/javascript

!--

// Next comes the standard javascript detection that uses the

// navigator.plugins array. We pack the detector into a function so it loads

// before we call it.



function detectFlash() {

  // If navigator.plugins exists...

  if (navigator.plugins) {

// ...then check for flash 2 or flash 3+.

if (navigator.plugins[Shockwave Flash 2.0] ||
navigator.plugins[Shockwave Flash]) {



  // Some version of Flash was found. Time to figure out which.



  // Set convenient references to flash 2 and the plugin description.

  var isVersion2 = navigator.plugins[Shockwave Flash 2.0] ?  2.0 :
;

  var flashDescription = navigator.plugins[Shockwave Flash +
isVersion2].description;



  // DEBUGGING: uncomment next line to see the actual 

Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis
Take a look at the repr and str methods:
 http://docs.python.org/reference/datamodel.html#basic-customization

Ok so I am still a little confused, It seems that __str__ is used for print
and str()
byt what allows whats below. That is why does just entering the name of the
instance return print(b). I have tried a few combinations but not the right
one.
in   b
out 21-letter BString instance
   seq: I am a BString object

*Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog http://vincentdavis.net |
LinkedInhttp://www.linkedin.com/in/vincentdavis


On Mon, Apr 5, 2010 at 11:24 AM, Wayne Werner waynejwer...@gmail.comwrote:



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


Re: [Tutor] New class, how return value of x in interactive shell

2010-04-05 Thread Vincent Davis

 That' a very strange idiom in Python.
 Can you show us the class definition?


It's a bioconductor extension to rpy2
http://www.bitbucket.org/lgautier/rpy2-bioc-extensions/overview/
I am trying to learn R and at they same time more about python and R
bioconductor packages. So no it is not homework but I am trying to learn
something. I am sure the answer is obvious when you know it :)
Here is the class, although it is obviously part of something bigger, you
can checkout the full code at bitbuckit.org

class BString(XString):
 Biological string 

_bstring_constructor = biostrings.BString

@classmethod
def new(cls, x):
 :param x: a (biological) string 
res = cls(cls._bstring_constructor(conversion.py2ri(x)))
_setExtractDelegators(res)
return res

*Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog http://vincentdavis.net |
LinkedInhttp://www.linkedin.com/in/vincentdavis


On Mon, Apr 5, 2010 at 1:08 PM, Alan Gauld alan.ga...@btinternet.comwrote:

 Vincent Davis vinc...@vincentdavis.net wrote

 I am working an a open source project and would like to add feature to a

 class.
 Current action:
 in:b = BString.new('I am a BString object')


 That' a very strange idiom in Python.
 Can you show us the class definition?


  out:  b
 in:BString - Python:0x1044846c8 / R:0x105c86f50
 in:print(b)
 out:   21-letter BString instance
  seq: I am a BString object

 What I would like is to be able to
 in   b
 out 21-letter BString instance
  seq: I am a BString object

 I have 2 questions
 1, how do I do this?
 2, how does print know what to do?


 If you look at your class definition that should become obvious.
 Are you sure this isn't a homework?


 --
 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] New class, how return value of x in interactive shell

2010-04-05 Thread Alan Gauld


Vincent Davis vinc...@vincentdavis.net wrote


Take a look at the repr and str methods:
http://docs.python.org/reference/datamodel.html#basic-customization

Ok so I am still a little confused, It seems that __str__ is used for 
print

and str()


That's right and repr() is used when evaluating the object, as at the  
prompt.


So


print b # calls b.__str__()


whereas


b # calls b.__repr__()


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] New class, how return value of x in interactive shell

2010-04-05 Thread Alan Gauld

Vincent Davis vinc...@vincentdavis.net wrote


class BString(XString):
Biological string 

   _bstring_constructor = biostrings.BString

   @classmethod
   def new(cls, x):
:param x: a (biological) string 
   res = cls(cls._bstring_constructor(conversion.py2ri(x)))
   _setExtractDelegators(res)
   return res


OK, I see what they are doing but as far as I can tell this could be 
put in a __new__ method just as easily which would retain the 
usual instantiation style.


Any gurus out there able to explain why they have used an 
explicit new() class method rather than __new__()?


--
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] Menu data from file

2010-04-05 Thread Neven Goršić
OK, I will describe my case.

I use menu date stored in list shape in file CMFlowData.py in the same
directory as my main program x.py. In the beginning of the program I have
command:

import CMFlowData

and everything works fine till I make executable with py2exe. There are no
error during compilation and exe are created in ...\dist directory. I copy
all my external files to that directory, together with CMFlowData.py file,
but when I run x.exe I get x.exe.log file with message:

Traceback (most recent call last):
  File x.py, line 8, in module
ImportError: No module named CMFlowData
Traceback (most recent call last):
  File x.py, line 8, in module
ImportError: No module named CMFlowData

All other files that I read from disk are created with relative paths and
work fine, except when I import
my own module.

Is there some procedure to announce my module to the Python or System?

That is all.

Neven



On Mon, Apr 5, 2010 at 4:03 PM, Lie Ryan lie.1...@gmail.com wrote:

 On 04/05/10 17:39, Neven Goršić wrote:
  Thank you for mentioning the possible options.
  I already use option where I import .py module,
  but I run into troubles when making executable with py2exe.

 Maybe you should elaborate what problems you're experiencing with
 py2exe? Probably we can solve that instead of developing workarounds.

 Many popular UI toolkit provides an XML-based menu file.
 Here is one for PyGTK:
 http://www.pygtk.org/pygtk2tutorial/sec-UIManager.html

 You should better use your GUI toolkit's version instead of writing your
 own.

  I suppose that XML approach is the most general method.
  Can you recommend a good introduction text (not for experts :-))
  and give more details about the tool ElementCTree.
 
  Maybe it will be educational and interesting for other beginners too.

 ElementTree is quite simple, though I don't think there are many good
 tutorials about it (at least I can't find one). In the simplest use
 case, you just use xml.etree.ElementTree.parse(menu.xml) and you can
 iterate the tree like so:

 import xml.etree.cElementTree as Et
 menuxml = 
 menubar
  menu title=File
menuitem title=Open File... action=open_file/
menu title=Recent Files
  menuitem title=placeholder_1 action=recent_1/
  menuitem title=placeholder_2 action=recent_2/
  menuitem title=placeholder_3 action=recent_3/
/menu
menuitem title=Exit My Program action=quit/
  /menu
  menu title=Edit
menuitem title=Copy action=copy_clipboard/
menuitem title=Paste action=paste_clipboard/
  /menu
 /menubar
 
 # root = Et.parse(menu.xml)
 root = Et.fromstring(menuxml)

 def parse(menutk, element):
for menu in mbar:
if menu.tag == 'menu':
# have submenus, recurse
submenutk = add_menu(menutk)
parse(submenutk, menu)
elif emnu.tag == 'menuitem':
add_menuitem(menutk, menuitem)

 or something like that.

 ___
 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] Simple bank account oriented object

2010-04-05 Thread Alan Gauld

I don't speak French so I'm struggling a bit with the variable
names, however...

Marco Rompré marcodrom...@gmail.com wrote in message

class CompteBancaire:
   def __init__(self,nom,solde,interet):   #Nous allons instancier et
   self.nom, self.solde, self.interet = nom,solde,interet

   def depot(self,somme=0):#Méthode pour additionner
   self.solde=self.solde+somme

   def retrait(self,somme=0):  #Méthode pour soustraire 
les

   if self.nom == 'Sandon':
   if self.solde-somme0:
   print Les fonds sont insuffisants. Essayez un autre 
montant

   else:
   self.solde=self.solde-somme
   elif self.nom =='Étudiant': #Vérifie s'il s'agit d'un
   if self.solde - somme  -self.margeCre:  # Vérifie si le
   print Désolé, votre retrait dépasse la marge de crédit
   else:   # sinon déuit le montant
   self.margeCre = self.solde - somme
   self.solde = 0

   def calculInteret(self,calculInteret=0): #Méthode qui calcule les
   self.interet=self.solde*calculInteret/100
   self.solde=(self.solde*calculInteret/100)+self.solde

   def affiche_solde(self):#Méthode qui affiche le
   print Le solde du compte bancaire de %s est de %d $CAD
%(self.nom,self.solde)
   print Vous avez récolté %d $CDN en intérêt%(self.interet)

class CompteEtudiant(CompteBancaire):
   définition du compte bancaire pour étudiant dérivé du compte bancaire
   def __init__(self, nom='', solde=0, margeCre=0):
   CompteBancaire.__init__(self, nom='Nom', solde=0, interet=0)
   self.nom, self.solde, self.margeCre = nom, solde, margeCre

   def affiche_solde(self, somme=0):   #Méthode constructeur qui
   print %s--Votre solde bancaire est de %d $CAD
%(self.nom,self.solde)
   print Le solde de votre marge de crédit est de %d $CAD
%(self.margeCre)

This second class does not seem to override any of the original
methods so does not modify the balance based on the overdraft2 value.
This the status method prints out the balance based on a simple
calculation without taking the overdraft into account.

If you want to change that I think you will need to add override versions
of the withdrawal method to your subclass.

But I admit my understanding of the code is not deep, I didn't spend
enough time deciphering it for that...

Alan G.






When I run it,  it gives me this:




Les fonds sont insuffisants. Essayez un autre montant pour votre retrait!
(Try to witthdraw 1200 with a balance of 880$) This is good!
Le solde du compte bancaire de Sandon est de 880 $CAD (This is fine too)
Vous avez récolté 80 $CDN en intérêt (This too)
Étudiant--Votre solde bancaire est de 0 $CAD (Trying to withdraw 1100$ with
a cash balance of 800 plus a credit line of 1000$)
Le solde de votre marge de crédit est de -300 $CAD (It would be supposed to
say that my credit line balance is 700= 1000-300 knowing that this 300 was
the amount missing from mu cash balance)


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


[Tutor] Getting traceback info from C-API

2010-04-05 Thread Shu
Hi,

I have a CAPI extension module that is giving me MemoryError
exceptions from once in a while with no other information, so clearly
none of my exception handlers are catching it. Is there any way I can
traceback information for the extension module?

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


Re: [Tutor] Simple bank account oriented object

2010-04-05 Thread Steven D'Aprano
On Tue, 6 Apr 2010 06:31:30 am Marco Rompré wrote:
 Hi im doin a programmin course at university and in one on my
 exercise i have to do that

 I had to define a class CompteBancaire(CompteBancaire is bankaccount
 in french), that would allow me to create objects Compte1,
 Compte2,etc.
[...]

I do not understand French, so if I have misunderstood something, please 
excuse me. My comments are added in between yours.


 #Exercice 3,2 - Gestion d'un compte bancaire pour étudiant avec marge
 de crédit disponible de 1000$CDN

 class CompteBancaire:
 définition d'un compte bancaire

This docstring is not very useful. The reader already knows that you are 
defining a CompteBancaire. What else can you tell them? How about basic 
usage?

class CompteBancaire:
définition d'un compte bancaire

Example: create a cheque account with $1000 balance.

 compte = CompteBancaire('cheque', 1000)

Deposit $100, then withdraw $80:

 compte.depot(100)
 compte.retrait(80)




 def __init__(self,nom,solde,interet):   #Nous allons
 instancier et initialiser les objets à la classe
 self.nom, self.solde, self.interet = nom,solde,interet

It is not clear to me whether interet (French for interest?) is meant to 
be a percentage rate, or an amount. I will assume it is an amount (how 
much interest the bank has paid you). Every new account will always 
start with interet = 0, so we should write this:

def __init__(self, nom, solde):
self.nom, self.solde = nom, solde
self.interet = 0

It is not clear to me what nom (name?) is for, but I have left it in.



 def depot(self,somme=0):#Méthode pour
 additionner les dépôts au compte
 self.solde=self.solde+somme

Do you need the default value for deposits? Will it ever be useful for 
the user to call compte.depot() without an amount?

I would think it would be more useful to add the default values to the 
initialiser __init__:

def __init__(self, nom='', solde=0):
...

and take it away from the depot and retrait methods:

def depot(self, somme):
...
def retrait(self,somme):
...


But this is just my opinion.


 def retrait(self,somme=0):  #Méthode pour
 soustraire les retraits au compte
 if self.nom == 'Sandon':
 if self.solde-somme0:
 print Les fonds sont insuffisants. Essayez un autre
 montant pour votre retrait!
 else:
 self.solde=self.solde-somme

This comment might be more advanced than you have learned. If so, you 
can safely ignore this part. If you have not learned about exceptions 
yet, you can safely ignore this.

When programming, you should separate the back-end from 
the front-end. The back-end should report errors using exceptions, in 
a form which is useful to the programmer, and the front-end should 
catch those exceptions and print an error message in a form which is 
useful to the user.

So we should write:

def retrait(self, somme):
if somme  self.solde:
raise ValueError('fonds sont insuffisants')
else:
self.solde = self.solde - somme


 elif self.nom =='Étudiant': #Vérifie s'il s'agit
 d'un compte étudiant

This part is not good. The CompteBancaire class MUST NOT know anything 
about the CompteEtudiant subclass. Each class should only know about 
itself, and superclasses (parent classes).

So all the code dealing with the Étudiant account must go inside the 
CompteEtudiant class, not the CompteBancaire.


 if self.solde - somme  -self.margeCre:  # Vérifie si
 le retrait dépasse la marge de crédit maximum
 print Désolé, votre retrait dépasse la marge de
 crédit autorisé
 else:   # sinon déuit le
 montant retirer de la marge de crédit
 self.margeCre = self.solde - somme
 self.solde = 0


 def calculInteret(self,calculInteret=0): #Méthode qui calcule
 les intérêts et le solde résiduel
 self.interet=self.solde*calculInteret/100
 self.solde=(self.solde*calculInteret/100)+self.solde

There is a small problem with Python here, depending on what version you 
are using. In older versions of Python, division is integer division, 
so that:

1/2 - 0
9/4 - 2

and so forth. This will give you the wrong result for calculating 
interest. In newer versions of Python (version 3.0 and better) division 
is true division:

1/2 - 0.5
9/4 - 2.5

The easiest way to make your code work correctly is to change 100 to 
100.0 (a float instead of an int) and then it will calculate as you 
expect in all versions.


 def affiche_solde(self):#Méthode qui affiche
 le solde et le montant d'intérêt accumulé
 print Le solde du compte bancaire de %s est de %d $CAD
 %(self.nom,self.solde)
 print Vous avez récolté %d $CDN en intérêt%(self.interet)


Now we look at the Étudiant account, and add the extra functionality.


 

Re: [Tutor] Scraping gov site: site looking for Flash player

2010-04-05 Thread Benno Lang
On 6 April 2010 03:31, Roy Hinkelman royh...@gmail.com wrote:
 I am using urllib2 to open some government pages, and they have some js
 checking for Flash on my computer.
 Is there a way to show them that I have flash? Or possibly another solution?

From reading the JavaScript, you should fetch the URL
domain.tld/doc.php?flash=truedoc=2 instead of domain.tld/ as the
first URL.

 var flashPage = /doc.php?flash=truedoc=2; // The location of the
 flash movie page

This is the JS that specifies the desired redirect location. You could
open the original URL in a flash-capable browser to work out where to
go as well.

However, if the site is flash-based, you probably won't be able to get
any useful info from it.

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


Re: [Tutor] Menu data from file

2010-04-05 Thread Lie Ryan
On 04/06/10 08:05, Neven Goršić wrote:
 OK, I will describe my case.
 
 I use menu date stored in list shape in file CMFlowData.py in the same
 directory as my main program x.py. In the beginning of the program I
 have command:
 
 import CMFlowData
 
 and everything works fine till I make executable with py2exe. There are
 no error during compilation and exe are created in ...\dist directory.
 I copy all my external files to that directory, together with
 CMFlowData.py file, but when I run x.exe I get x.exe.log file with message:
 
 Traceback (most recent call last):
   File x.py, line 8, in module
 ImportError: No module named CMFlowData
 Traceback (most recent call last):
   File x.py, line 8, in module
 ImportError: No module named CMFlowData
 
 All other files that I read from disk are created with relative paths
 and work fine, except when I import
 my own module.
 
 Is there some procedure to announce my module to the Python or System?

I've never used py2exe myself, but from what I can gather py2exe should
be able to collect the imported modules automatically. Did you follow
everything in the py2exe tutorial:
http://www.py2exe.org/index.cgi/Tutorial ? Are there any deviations of
the things you did from the tutorial?

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


Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread TGW

OK - you handled the problem regarding reading to end-of-file. Yes it
takes a lot longer, because now you are actually iterating through
match_zips for each line.

How large are these files? Consider creating a set from match_zips. As
lists get longer, set membership test become faster than list  
membership

test.

If the outfile is empty that means that line[149:154] is never in
match_zips.

I suggest you take a look at match_zips. You will find a list of  
strings

of length 6, which cannot match line[149:154], a string of length 5.


I am still struggling with thisI have simplified the code, because  
I need to understand the principle.


#!/usr/bin/env python

import string

def main():
 infile = open(filex)
 outfile = open(results_testx, w)
 zips = open(zippys, r)
 match_zips = zips.readlines()
 lines = [line for line in infile if line[0:3] + '\n' in  
match_zips]

 outfile.write(''.join(lines))
 print line[0:3]
 zips.close()
 infile.close()
 outfile.close()
main()

filex:
112332424
23423423423
34523423423
456234234234
234234234234
5672342342
683824242

zippys:
123
123
234
345
456
567
678
555


I want to output records from filex whose first 3 characters match a  
record in zippys. Ouptut:

23423423423
34523423423
456234234234
234234234234
5672342342

I am not sure where I should put a '\n' or tweak something that I just  
cannot see.


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


Re: [Tutor] Matching zipcode in address file

2010-04-05 Thread TGW

I got it. I was comparing '345' to '345\n'

Adding the '\n' to the slice did indeed do the trick.

#!/usr/bin/env python

import string

def main():
 infile = open(filex)
 outfile = open(results_testx, w)
 zips = open(zippys, r)
 match_zips = zips.readlines()
 lines = [line for line in infile if (line[0:3] + '\n')  in  
match_zips]

 outfile.write(''.join(lines))
# print lines[0:2]
 zips.close()
 infile.close()
 outfile.close()
main()

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