Re: Problem reading csv files
XLS != CSV XLS is M$'s format for spreadsheets whereas CSV is essentially a text document with comma-delimited fields. If you open it up in OpenOffice and go File - Save As then in the 'Save as type:' drop-down list select 'Text CSV (.csv)' and ofc change your code to point to the new file. If you want to retain it in XLS Format and rather parse that, take a look at 'xlrd' and 'pyExcelerator' Thanks for the reply. I made the mistake of assuming XLS = CSV :) Ram -- http://mail.python.org/mailman/listinfo/python-list
Problem reading csv files
Hi, I am trying to read a csv file using csv.reader. The file is created using Open Office and saved in Excel format. import csv reader = csv.reader(open('test.xls')) for row in reader: print row It however throws the exception _csv.Error: class '_csv.Error': line contains NULL byte Any idea whats going wrong here? Thanks in advance, Ram -- http://mail.python.org/mailman/listinfo/python-list
Understanding closures
Hi, I want to use variables passed to a function in an inner defined function. Something like- def fun1(method=None): def fun2(): if not method: method = 'GET' print '%s: this is fun2' % method return fun2() fun1() However I get this error- UnboundLocalError: local variable 'method' referenced before assignment This however works fine. def fun1(method=None): if not method: method = 'GET' def fun2(): print '%s: this is fun2' % method return fun2() fun1() Is there a simple way I can pass on the variables passed to the outer function to the inner one without having to use or refer them in the outer function? Thanks, Ramashish -- http://mail.python.org/mailman/listinfo/python-list
Re: SMTPAuthenticationError
To help debug this, you may want to try the following. 1) Copy smptlib.py into your local directory. On my box, you can find it here, or import sys; print sys.path to help find it on your box: /usr/local/lib/python2.3 2) Go the login() method, add some print statements there to see what's going on. I admit to not being an SMTP expert nor fully understanding the code at first glance, but here is some code iin smtplib.py that suggests you're close to getting this working, to the extent that your server wants base64 encoding: def encode_cram_md5(challenge, user, password): challenge = base64.decodestring(challenge) response = user + + hmac.HMAC(password, challenge).hexdigest() return encode_base64(response, eol=) Hope this helps. Thanks Steve, that helped a lot. smtplib was trying to a CRAM-MD5 auth which wasn't working. I don't know why. But when I made it do a LOGIN auth by changing its preferred_auth list, it worked. Login has its own preferred list of auth methods which is nice except that not all servers which advertise a particular method may be capable of handling that. It would have been good if there was optionally a way to specify in login() what method to use. For now, I am simply going to derive SMTP to form a class that does LOGIN auth. Ram -- http://mail.python.org/mailman/listinfo/python-list
Periodic tasks.
Hi, I am trying to execute some tasks periodically, those familiar with unix can think of it as equivalent to cron jobs. I have tried looking around, but couldn't find a way. Would appreciate any pointers or clues.. Thanks, -Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: Periodic tasks.
I am trying to execute some tasks periodically, those familiar with unix can think of it as equivalent to cron jobs. Can you not use cron? If not, why not? Is there an equivalent service you can use? I can, but the work I want to do is written in Python. This is not an issue but I would have more than one such tasks running at different periods and it will be desirable to control or monitor all of them from a single process. If I use cron, all of them will execute independently. I have tried looking around, but couldn't find a way. Using the services provided by the operating system would be far preferable to re-inventing a scheduler service. Agreed, but my requirement is a little different. There is a TaskKit package (http://webware.sourceforge.net/Webware-0.7/TaskKit/Docs/ QuickStart.html) that has such a scheduler. Does anyone have any experience with it? Ram -- http://mail.python.org/mailman/listinfo/python-list
SMTPAuthenticationError
Hi, I am trying to send a mail using smtplib. My server requires me to authenticate, for this I'm using SMTP.login function. However it fails- server = smtplib.SMTP(host='mail.domain', port=25) server.login('username', 'password') Traceback (most recent call last): File stdin, line 1, in ? File /usr/lib/python2.4/smtplib.py, line 587, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, 'authorization failed (#5.7.0)') I am sure that I am giving the correct credentials. The same works in Thunderbird. Am I missing something here or am I supposed to use some other library for this? Thanks in advance, Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: How to set a class inheritance at instance creation?
On May 29, 8:52 pm, glomde [EMAIL PROTECTED] wrote: Hi I wonder if you can set what subclass a class should have at instance creation. The problem is that I have something like: class CoreLang(): def AssignVar(self, var, value): pass class Lang1(CoreLang): def AssignVar(self, var, value): return var, =, value class Lang2(CoreLang): def AssignVar(self, var, value): return var, =, value class WriteStruct(): def Generate(self, vars): for var in vars: print self.AssignVar() The problem is that I want WriteStruct to sometimes be a subclass of Lang1 and sometimes of Lang2. In the above example I could but the Generate Method in CoreLang. But in my real example I also want to able to subclass WriteStruct to be able to easy customize WriteStruct. Which I wouldnt be able to do if it was a method in CoreLang. So in code I would like to write something like: WriteStruct(Lang1).Generate(vars) class WriteStruct: def __init__(self, SubClass): self._sub = SubClass() def Generate(self, vars): for var in vars: print self._sub.AssignVar() This does what you want but isn't inheritance. Even better would be that if I in the Lang1 class could just do WriteStruct().Generate(vars) and Lang1 class would magically make WriteStruct a subclass of itself. I don't think I understood what you want here. Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: How to set a class inheritance at instance creation?
On May 29, 8:52 pm, glomde [EMAIL PROTECTED] wrote: Hi I wonder if you can set what subclass a class should have at instance creation. The problem is that I have something like: class CoreLang(): def AssignVar(self, var, value): pass class Lang1(CoreLang): def AssignVar(self, var, value): return var, =, value class Lang2(CoreLang): def AssignVar(self, var, value): return var, =, value class WriteStruct(): def Generate(self, vars): for var in vars: print self.AssignVar() The problem is that I want WriteStruct to sometimes be a subclass of Lang1 and sometimes of Lang2. In the above example I could but the Generate Method in CoreLang. But in my real example I also want to able to subclass WriteStruct to be able to easy customize WriteStruct. Which I wouldnt be able to do if it was a method in CoreLang. So in code I would like to write something like: WriteStruct(Lang1).Generate(vars) class WriteStruct: def __init__(self, SubClass): self._sub = SubClass() def Generate(self, vars): for var in vars: print self._sub.AssignVar() Even better would be that if I in the Lang1 class could just do WriteStruct().Generate(vars) and Lang1 class would magically make WriteStruct a subclass of itself. I don't think I understood what you want here. Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: SMTPAuthenticationError
I am trying to send a mail using smtplib. My server requires me to authenticate, for this I'm using SMTP.login function. However it fails- server = smtplib.SMTP(host='mail.domain', port=25) server.login('username', 'password') Traceback (most recent call last): File stdin, line 1, in ? File /usr/lib/python2.4/smtplib.py, line 587, in login raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, 'authorization failed (#5.7.0)') I am sure that I am giving the correct credentials. The same works in Thunderbird. Am I missing something here or am I supposed to use some other library for this? Thanks in advance, Ram Are you sure that your SMTP server uses this type of authentication? Some SMTP servers use POP3 followed by SMTP to authenticate instead. use telnet to verify, this link might help. http://www.computerperformance.co.uk/exchange2003/exchange2003_SMTP_A... Hi Larry, Thanks for the reply. I have worked according to the steps in the link you provided. From that it seems my server accepts base64 encoded username and password. I am able to login this way. How to give the same in smtplib? Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: Module listing in order.
I want a way to get the contents in the order of their declaration, i.e. [B, A, D]. Does anyone know a way to get it? My suggestion would be to actually parse the text of the module. Brute force is what it's called ;). But doing so with, say, pyparsing shouldn't be *very* difficult. Just out of curiosity: Why do you need the order? Thank you for your replies, and sorry for my late response. Gabriel, unfortunately I am not a python expert so don't know how to play with module creation. I tried to look into __import__ function, but can't see a way to get what I want. Wildemar, your approach seems workable. I am going to have a look at it. Well, my requirement doesn't turn out to be an actual requirement now.:) I am using a web framework Django, that lets you define classes for database tables. The classes so defined can refer to other classes representing db tables. It also allows you to export those table data in a db-neutral format e.g. xml via the python classes so defined. Exporting does not require an order, but I thought that importing the data back may require data of classes which are referred by other classes to be present. I just verified that its not so. So I don't need to do it immediately. Nevertheless, it would be interesting to see how it can be done.:) -Ram -- http://mail.python.org/mailman/listinfo/python-list
Module listing in order.
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in alphabetical order. As an example- # mymodule.py class B: pass class A: pass class D: pass # test.py import mymodule # This returns['A', 'B', 'D', '__builtins__', '__doc__', '__file__', '__name__'] contents = dir(mymodule) I want a way to get the contents in the order of their declaration, i.e. [B, A, D]. Does anyone know a way to get it? Thanks, Ram -- http://mail.python.org/mailman/listinfo/python-list
setDaemon problem.
Hi, I am facing an issue in daemonizing a thread using setDaemon method. Here is my code- import time from threading import Thread class MThread(Thread): def run(self): f = open('/tmp/t.log', 'w') for i in range(10): f.write('Iteration %d\n' % i) time.sleep(1) f.close() if __name__ == __main__: t = MThread() t.setDaemon(True) print 'Starting thread' t.start() The scripts runs all fine, but nothing gets logged to /tmp/t.log. However when I run the same script without setting thread as daemon (no call to t.setDaemon), everything works fine. Am I missing anything? Thanks in advance, Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: How to upgrade python from 2.3 to 2.4
On Apr 20, 2:03 am, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, Red hat 4 comes with python 2.3, and I am trying to upgrade to python 2.4. So I download and compile the source of python2.4. But as I run it I get the following error, can you please tell me how to fix it? # /root/src/Python-2.4.4/python ./nov/scripts/stressTestServlet.py ./ nov/scripts/stressTestInputFile 10 127.0.0.1 Traceback (most recent call last): File ./nov/scripts/stressTestServlet.py, line 6, in ? import time ImportError: No module named time The best way to do it will be to use your operating system package manager. If you have yum (a package manager for red hat distributions) installed, it might be as simple as- # yum update python HTH Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: setDaemon problem.
Hi, I am facing an issue in daemonizing a thread using setDaemon method. Here is my code- import time from threading import Thread class MThread(Thread): def run(self): f = open('/tmp/t.log', 'w') for i in range(10): f.write('Iteration %d\n' % i) time.sleep(1) f.close() if __name__ == __main__: t = MThread() t.setDaemon(True) print 'Starting thread' t.start() The scripts runs all fine, but nothing gets logged to /tmp/t.log. However when I run the same script without setting thread as daemon (no call to t.setDaemon), everything works fine. Am I missing anything? Yes. You miss the documentation of setDaemon. It is _not_ a PROCESS-daemonization recipe - which you can get here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 Instead, it means that the thread started is considered not to be worth waiting for when the main thread exits. Which it does immediately in your code, resulting in nothing written in the logfile. I think the name setDaemon is somewhat unfortunate. Thanks Diez, I was also wondering about daemonizing a thread, but I interpreted that it would daemonize the process which it didn't. I think setDaemon should be renamed to setDetached or something similar. Ram -- http://mail.python.org/mailman/listinfo/python-list
Accessing class variables in staticmethods.
Hi, I want to access a static variable in a staticmethod. The variable can be redefined by derived classes and that should be reflected in base's staticmethod. Consider this trivial example- class Base: staticvar = 'Base' @staticmethod def printname(): # this doesn't work # print staticvar # this does work but derived classes wouldn't behave as I want print Base.staticvar class Derived(Base): staticvar = 'Derived' Base.printname() # should print 'Base' Derived.printname() # should print 'Derived' Any idea on how to go about this? Also from a staticmethod how can I find out other attributes of the class (not objects)? Do static methods get some classinfo via some implicit argument(s)? Thanks in advance, Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessing class variables in staticmethods.
Sam wrote: On 21 Jan 2007 12:49:17 -0800, Ramashish Baranwal [EMAIL PROTECTED] wrote: class Base: staticvar = 'Base' @staticmethod def printname(): # this doesn't work # print staticvar # this does work but derived classes wouldn't behave as I want print Base.staticvar class Derived(Base): staticvar = 'Derived' Base.printname() # should print 'Base' Derived.printname() # should print 'Derived' Any idea on how to go about this? Also from a staticmethod how can I find out other attributes of the class (not objects)? Do static methods get some classinfo via some implicit argument(s)? No, staticmethods get told nothing about the class they're being defined in. What you want is a classmethod, which gets passed the class to work with. Using classmethods, your code becomes: #untested, bear in mind class Base: staticvar = 'Base' @classmethod def printname(cls): print cls.staticvar class Derived(Base): staticvar = 'Derived' Base.printname() #prints 'Base' Derived.printname() #prints 'Derived' Incidentally, you can also use cls.__name__ for this purpose, but I guess that your actual motivation for this is more complicated than class names. Thanks Sam, using classmethod works. You guessed it correctly, my actual motivation is more complicated but on the same line. -Ram -- http://mail.python.org/mailman/listinfo/python-list
Passing variable number of named arguments
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this simple example, def fun1(**kwargs): print kwargs.keys() def fun2(**kwargs): # get id param id = kwargs.pop('id', '') # pass on remaining to fun1 fun1(kwargs) When I try to call fun2 I get the following error- TypeError: fun1() takes exactly 0 arguments (1 given) It seems that the arguments are not passed to fun1 as named arguments. How can I go about this? Using a dictionary in place of kwargs would be a way, but I can't modify fun1, so thats ruled out for me. Thanks, Ram -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing variable number of named arguments
Carsten Haese wrote: On Wed, 2006-12-27 at 10:37 -0800, Ramashish Baranwal wrote: [...] def fun2(**kwargs): # get id param id = kwargs.pop('id', '') # pass on remaining to fun1 fun1(kwargs) When I try to call fun2 I get the following error- TypeError: fun1() takes exactly 0 arguments (1 given) It seems that the arguments are not passed to fun1 as named arguments. You have to call fun1 like this: fun1(**kwargs). Wow. thanks Carsten.. -Ram -- http://mail.python.org/mailman/listinfo/python-list