Re: [Tutor] Doubts about Pylint

2008-04-09 Thread Alex Ezell
On Wed, Apr 9, 2008 at 11:43 AM, Dick Moores [EMAIL PROTECTED] wrote:
  Comments?

Since we started using code profilers and checkers like pyLint etc.,
we've had a motto:

This is a guide. It is not the gospel.

Take from pylint what you think helps and ignore the rest. It's just a
tool and you can choose how to use it.

That is, unless you want to actually change pylint. I'm sure there's
opportunity to do that, as well, if you are so inclined.

All that said, your az example seems a little silly on pylint's part. :)

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


[Tutor] Truncate First Line of File

2008-02-27 Thread Alex Ezell
I must be missing some simple method on a file object or something.

What I need to do is to truncate the first line of a file which has an
unknown number of lines and an unknown size.

The only thing I can think to do is to readlines() and then slice off
the first line in the resulting list, then writelines().

pseduo-code:
my_file = open('file.txt', 'wb')
lines = my_file.readlines()
del lines[0]
my_file.writelines()
my_file.close()

Is there a better way?

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


Re: [Tutor] Truncate First Line of File

2008-02-27 Thread Alex Ezell
On Wed, Feb 27, 2008 at 5:01 PM, Kent Johnson [EMAIL PROTECTED] wrote:

 Alex Ezell wrote:
   I must be missing some simple method on a file object or something.
  
   What I need to do is to truncate the first line of a file which has an
   unknown number of lines and an unknown size.
  
   The only thing I can think to do is to readlines() and then slice off
   the first line in the resulting list, then writelines().
  
   pseduo-code:
   my_file = open('file.txt', 'wb')
   lines = my_file.readlines()
   del lines[0]
   my_file.writelines()
   my_file.close()
  
   Is there a better way?

  No, you have to rewrite the file, that is the way the filesystem works.

  Your code above is pretty buggy though, you should at least
  open file for read
  readlines
  close file
  open file for write
  writelines
  close file

  Even safer is to write to a new file, then rename. The fileinput module
  makes it convenient to safely overwrite a file with a new one.

Oops, forgot to send this to the list before:

Thanks Kent and Bill. I typed that out really quickly, hence the pseudo-code
disclaimer. I know it wasn't pseudo enough :)

I might do something like this:
os.system(sed -i '1d' %s % filename)

I suspect it will be much faster on large files, but I haven't tested that yet.

Of course, it's not Python ;) and it'd be cool to know a Python way to do it.

Thanks again for the help.

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


Re: [Tutor] Truncate First Line of File

2008-02-27 Thread Alex Ezell
Thanks to everyone for the help. My coworker seems to really prefer
doing it via some system call.

She seems to think it's possible quickly with csplit, which I've never
used. I'll be investigating it in the morning, because she's really
good at what she does. :)

/alex

On Wed, Feb 27, 2008 at 6:53 PM, Tiger12506 [EMAIL PROTECTED] wrote:
 This is bill's method written out in code which is the python you seek,
  young warrior!

  inname = 'inputfile'
  outname = 'outfile'

  infile = open(inname,'r')
  outfile = open(outname,'w')
  infile.readline()
  line = infile.readline()
  while line != :
   outfile.write(line)
  infile.close()
  outfile.close()
  os.rename(inname,outname)



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

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


Re: [Tutor] New Introductory Book

2007-11-06 Thread Alex Ezell
On 11/6/07, Chris Calloway [EMAIL PROTECTED] wrote:
 Michael H. Goldwasser wrote:
 We are pleased to announce the release of a new Python book.

 Why is this book $102?

Supply and demand aside, I suspect the market for this, based on both
the publisher and the author's employment, is mostly
educational/collegiate. Therefore, this book is likely to be assigned
as a textbook and can command a premium price from buyers who have
little to no choice but to buy it. Additionally, it may not be
marketed on the wider bookstore shelves, so must make the most of the
market which it does reach.

That's all conjecture. What I do know is fact is that I can't afford it.

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


[Tutor] Calling a Method with a Reserved Name

2007-10-24 Thread Alex Ezell
I am working on building a SOAP client. Unfortunately, one of the
methods the SOAP server provides is named import. The SOAP server is
written in PHP.

So, my problem is that Python really doesn't like me using the word
import to call the SOAP method. The call should look something like
this:

self.call_response = self.soap.import(self.soap_auth, file_name,
import_groups, soap_flags)

Is there any way to call this method despite it's name being a reserved word.

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


Re: [Tutor] Calling a Method with a Reserved Name

2007-10-24 Thread Alex Ezell
On 10/24/07, Kent Johnson [EMAIL PROTECTED] wrote:
 Alex Ezell wrote:
  I am working on building a SOAP client. Unfortunately, one of the
  methods the SOAP server provides is named import. The SOAP server is
  written in PHP.
 
  So, my problem is that Python really doesn't like me using the word
  import to call the SOAP method. The call should look something like
  this:
 
  self.call_response = self.soap.import(self.soap_auth, file_name,
  import_groups, soap_flags)
 
  Is there any way to call this method despite it's name being a reserved 
  word.

 You could try introspection:

 importFunc = getattr(self.soap, 'import')
 self.call_response = importFunc(self.soap_auth, file_name,
 import_groups, soap_flags)

Thanks Kent. I tried it and it seem like importFunc is now something
like 'import.__str__'. I could maybe do some string operations to just
get import out of that, but is there something I could do with
getattr() for that reference to come back the way I need.

Thanks again.

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


Re: [Tutor] Calling a Method with a Reserved Name

2007-10-24 Thread Alex Ezell
Oops, meant to send to the list. Sorry, Kent.

I am working on building a SOAP client. Unfortunately, one of the
methods the SOAP server provides is named import. The SOAP server is
written in PHP.
   
So, my problem is that Python really doesn't like me using the word
import to call the SOAP method. The call should look something like
this:
   
self.call_response = self.soap.import(self.soap_auth, file_name,
import_groups, soap_flags)
   
Is there any way to call this method despite it's name being a 
reserved word.
You could try introspection:
   
importFunc = getattr(self.soap, 'import')
self.call_response = importFunc(self.soap_auth, file_name,
import_groups, soap_flags)
   
Thanks Kent. I tried it and it seem like importFunc is now something
like 'import.__str__'. I could maybe do some string operations to just
get import out of that, but is there something I could do with
getattr() for that reference to come back the way I need.
  
   Hmm, with a quick look at the code for SOAPpy (v 0.11.6) I don't see why
   the getattr() method would not work. Can you show the code you tried and
   why you think the result was a string?
  
   BTW pulling 'import' out of the string won't help; you need the import
   *function*.
 
  Got ya on the string bit. That was actually my fault. I think I am the
  victim of my own poorly written exception handling method. Or at
  least, I can't correctly read the errors that it tells me. :)
 
  The introspection bit you offered seems to work fine. The error is now
  within the call to the SOAP server.
 
  Sorry for getting everyone confused. I'm off to ask my fellow
  developer if the SOAP server really does what it says does since she
  wrote it :)
 
  /alex

Heh, I am still having problems with this. This is the whole method:

def importer(self, file_name, import_groups, import_flags):
   soap_flags = self.dict_to_key_value(import_flags)
   try:
   # TODO figure out how to call this soap method with reserved name
   self.call_response = self.soap.import(self.soap_auth,
file_name, import_groups, soap_flags)
   except Exception, e:
   method_name = sys._getframe().f_code.co_name
   method_args = '(%s,%s,%s)'
%(file_name,str(import_groups),str(import_flags))
   self.handle_exception(method_name + method_args,e)
   raise
   return self.call_response

I tried to replace the import() call with these two lines:
importFunc = getattr(self.soap, 'import')
self.call_response = self.soap.importFunc(self.soap_auth, file_name,
import_groups, soap_flags)

and this was the error:

AttributeError: importFunc

module body   in sync_members.py at line 143
function main in sync_members.py at line 138
function sforce_to_membersin sync_members.py at line 80
function do_importin sync_members.py at line 70
function importer in emma_ws.py at line 84
function __getattr__  in WSDL.py at line 96

Thanks again for working with me this far. I am certainly on the very
precipitous edge of my Python knowledge.

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


Re: [Tutor] Calling a Method with a Reserved Name

2007-10-24 Thread Alex Ezell
On 10/24/07, Kent Johnson [EMAIL PROTECTED] wrote:
 Alex Ezell wrote:

 # TODO figure out how to call this soap method with reserved name
 self.call_response = self.soap.import(self.soap_auth,
  file_name, import_groups, soap_flags)

  I tried to replace the import() call with these two lines:
  importFunc = getattr(self.soap, 'import')
  self.call_response = self.soap.importFunc(self.soap_auth, file_name,
  import_groups, soap_flags)

 this should be
 self.call_response = importFunc(self.soap_auth, file_name,
 import_groups, soap_flags)

 importFunc *is* the function you want to call, it is not an attribute of
 self.soap.

 Kent

Awesome. I knew I had just been looking at it for too long.

Thanks so much, Kent!

Maybe one day, I will help you with something. ;)

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


[Tutor] Fun with Cookies

2007-09-07 Thread Alex Ezell
Hi all,
I am trying to create a cookie and send it a long with a request.

I searched the archives and found this code from Kent Johnson:

import cookielib, urllib2

cj = cookielib.CookieJar()
cookie = cookielib.Cookie(...your cookie data here...)
cj.set_cookie(cookie)

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

data = urllib2.urlopen(...).read()

It seemed to solve a problem I was having with SimpleCookie(), but I
cannot figure out what I should put where Kent has written ...your
cookie data here I have tried strings, SimpleCookie instances,
etc. and I always get this error on that line:

__init__() takes at least 17 arguments (2 given)

Thanks for your time.

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


Re: [Tutor] Fun with Cookies

2007-09-07 Thread Alex Ezell
Kent,
Thanks so much. I will give that a try. Your name is all over these
kinds of questions on the web. I guess you fought through it a while
back?

Here's where I show off my Python newb status. What's the best way to
specify those attributes? If I only include the 4 you mention (name,
value, domain and path), it seems messy to have a bunch of Nones in
there.

Thanks again,
Alex

On 9/7/07, Kent Johnson [EMAIL PROTECTED] wrote:
 Alex Ezell wrote:
  Hi all,
  I am trying to create a cookie and send it a long with a request.
 
  I searched the archives and found this code from Kent Johnson:
 
  import cookielib, urllib2
 
  cj = cookielib.CookieJar()
  cookie = cookielib.Cookie(...your cookie data here...)
  cj.set_cookie(cookie)
 
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  urllib2.install_opener(opener)
 
  data = urllib2.urlopen(...).read()
 
  It seemed to solve a problem I was having with SimpleCookie(), but I
  cannot figure out what I should put where Kent has written ...your
  cookie data here I have tried strings, SimpleCookie instances,
  etc. and I always get this error on that line:
 
  __init__() takes at least 17 arguments (2 given)

 The Cookie constructor is

  def __init__(self, version, name, value,
   port, port_specified,
   domain, domain_specified, domain_initial_dot,
   path, path_specified,
   secure,
   expires,
   discard,
   comment,
   comment_url,
   rest,
   rfc2109=False,
   )

 You should specify at least name, value, domain and path (all strings).
 The rest can be None.

 Kent

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