Re: [Tutor] need to automate connection
Hi Payal, I see you're connecting to an smtp server Any particular reason yoou can't use smtplib? http://www.python.org/doc/current/lib/module-smtplib.html Here's a sample script using smtplib to send an email - import smtplib ip = "127.0.0.1" txt = "This is an email message" c = smtplib.SMTP(ip) c.sendmail("[EMAIL PROTECTED]", "[EMAIL PROTECTED]", txt) c.close() Although it's better to use an email.Message object for this - http://www.python.org/doc/current/lib/module-email.Message.html Here's the same script with an email.Message import smtplib import email.Message as Mg ip = "127.0.0.1" msg = Mg.Message() msg["To"] = "[EMAIL PROTECTED] msg["From"] = "[EMAIL PROTECTED]" msg["Subject"] = "Test message" msg["Reply-To"] = "[EMAIL PROTECTED]" txt = "This is an email message" msg.set_payload(txt) c = smtplib.SMTP(ip) c.sendmail("[EMAIL PROTECTED]", "[EMAIL PROTECTED]", msg.as_string()) c.close() Regards, Liam Clarke On 4/25/06, Payal Rathod <[EMAIL PROTECTED]> wrote: > Hi, > I need to automate connection to a IP like this. The IP (or domain name) > is taken from command line or from user (whichever is easier for me to > code). It should emulate, > > telnet 127.0.0.1 25 > > mail from: > 250 ok > rcpt to: <[EMAIL PROTECTED]> > 250 ok > quit > > Can Python do this for me? How do I start? > > With warm regards, > -Payal > > ___ > 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] Invoking Excel Macro
Hi Arun et al, Google says - http://support.microsoft.com/default.aspx?scid=kb;en-us;210684 "This problem can occur when you give the workbook a defined name and then copy the worksheet several times without first saving and closing the workbook, as in the following sample code..." (code is in above article) """To resolve this problem, save and close the workbook periodically while the copy process is occurring, as in the following sample code""" ditto above The issue is your macro. As such, there are some excellent Excel and VBA related help forums on the net. Good luck! Regards, Liam Clarke On 4/25/06, Mike Hansen <[EMAIL PROTECTED]> wrote: > I guess I'm confused. You mentioned that it still throws and error > while trying to save the workbook. hmmm Do you get this error when > trying to close Excel in the macro? Many times, Excel pops up a dialog > box when closing it if there's still a workbook open even if it has > been saved. That's where the Application.DisplayAlerts comes in. So I > may have been a little off on the part of saving. It might be the > closing of Excel. Other than that, I'm stumped. > > Mike > > On Apr 24, 2006, at 7:32 AM, arun wrote: > > > Hi Mike, > > It doesn't display any dialog box rather it gives a temporary name > > to the file. > > Ex > > newsheet.xls as newsheet1.xls > > So i'm not able to save it. Is there an alternative to invoke the > > macro and save the file from my script itself .(saving it through > > macro wud be a better option : ) ) > > > > Thanx and Regards, > > arun > > > > > > On 4/21/06, Mike Hansen <[EMAIL PROTECTED]> wrote: In the macro, > > you might try Application.DisplayAlerts = False and reset it toTrue > > after you save. > >> I think it might be trying to display a dialog box before it saves. > >> > >> Mike > >> > >>> From: [EMAIL PROTECTED] [mailto: [EMAIL PROTECTED] On > >>> Behalf Of arun > >>> Sent: Friday, April 21, 2006 1:44 AM > >>> To: tutor@python.org > >>> Subject: Re: [Tutor] Invoking Excel Macro > >>> > >>> I'm sorry , there is something realy wrong with 'Run-time error > >>> 1004':. I have fixed it now but still it throws an error while > >>> trying to save the workbook. > >> > >> On 4/21/06, arun <[EMAIL PROTECTED]> wrote: Hi, > >>> I tried invoking a macro from my python script and It is throwing > >>> an error message that reads 'Run-time error 1004': > >>> > >>> "This operation requires the merged cells to be identically sized" > >>> > >>> My script looks like this > >>> > >>> from win32com.client import Dispatch > >>> xl = Dispatch('Excel.Application') > >>> xl.Workbooks.Add('E:\Templates\sample.xls') > >>> xl.Run('Import_file') # name of the macro > >>> > >>> Also while (running the macro)opening the workbook itnames it as > >>> "sample1.xls' ,and so > >>> It says there is no such file sample.xls > >>> > >>> " > >>> Filename = Myexcel.xls > >>> Workbooks("sample.xls").SaveAs Filename > >>> " > >>> > >>> Can somebody help me with this issue : ( > >>> > >>> > >>> Thanx > >>> arun > >>> > >>> > >>> > >>> > >>> > >> > >> > >> -- > >> arun > > > > > > > > -- > > arun___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > ___ > 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] Importing Modules
John Connors wrote: > G'day, > > I'm having trouble understanding the difference between, > > import sys > and > from sys import * The second style is strongly discouraged. As Alan pointed out, it can lead to surprises when you import more than you expect. I was once surprised to find out that when I write something like a.py foo = 1 bar = 2 b.py from a import * c.py from b import * Now foo and bar are defined in module c!! The other reason to avoid this style is it removes clues about where a name is defined. If in module c above you want to know where foo comes from, it would be hard to find out. On the other hand if I wrote from a import foo then a search within c would show me where foo is defined. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to compile _tkinter in Fedora Core 4
Hi I'd very much appreciate your help if you can advise me how to compile _tkinter in Fedora 4. I installed Fedora core 4 in my machine recently, but struggled to get Tk working for me. The Tcl/Tk is working O.K. because I can run demo programs from /usr/share/tk8.4/demos. The error is caused by missing _tkinter library, and clearly there is no Tkinter module to be imported. I managed to download and recompile a new version of Python (2.4.3), then run "make test". The error message says: ... test_tcl test_tcl skipped -- No module named _tkinter ... 1 skip unexpected on linux2: test_tcl ... John Hsu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and aggregation
> However here's what I'm now trying to do: > > 1) Not have to rely on using awk at all. > > > 2) Create a dictionary with server names for keys e.g. server001, > server002 etc and the aggregate of the request for that server as the value > part of the pairing. > > > I got this far with part 1) > > lbstat = commands.getoutput("sudo ~/ZLBbalctl --action=cells") > tmpLst = lbstat.split('\n') > > rLst = [] > for i in tmpLst: > m = re.search(' server[0-9]+', i) > if m: > rLst.append(i) > > for i in rLst: > print i, type(i) > > server001 alive 22.3% 6 requests/s 14527762 total > server002 alive 23.5% 7 requests/s 14833265 total > server003 alive 38.2%14 requests/s 14872750 total > server004 alive 15.6% 4 requests/s 15083443 total > server001 alive 24.1% 8 requests/s 14473672 total > server002 alive 23.2% 7 requests/s 14810866 total > server003 alive 30.2% 8 requests/s 14918322 total > server004 alive 22.1% 6 requests/s 15137847 total > > At this point I ran out of ideas and began to think that there must be > something fundamentally wrong with my approach. Not least of my concerns was > the fact that I needed integers and these were strings. Don't get discouraged, you are on the right track! You had one big string that included some data you are interested in and some you don't want, you have converted that to a list of strings containing only the lines of interest. That is a good first step. Now you have to extract the data you want out of each line. Use line.split() to split the text into fields by whitespace: In [1]: line = ' server001 alive 22.3% 6 requests/s 14527762 total' In [2]: line.split() Out[2]: ['server001', 'alive', '22.3%', '6', 'requests/s', '14527762', 'total'] Indexing will pull out the field you want: In [3]: line.split()[5] Out[3]: '14527762' It's still a string: In [4]: type(line.split()[5]) Out[4]: Use int() to convert a string to an integer: In [5]: int(line.split()[5]) Out[5]: 14527762 Then you have to figure out how to accumulate the values in a dictionary but get this much working first. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need to automate connection
On Tue, Apr 25, 2006 at 06:59:29PM +1200, Liam Clarke wrote: > Hi Payal, > > I see you're connecting to an smtp server Any particular reason yoou > can't use smtplib? > http://www.python.org/doc/current/lib/module-smtplib.html Because I don't know it exists :) But I don't want to send any mail. I just want to establish a connection send MAIL TO: and RCPT TO: and exit. Any ideas with that? With warm regards, -Payal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to compile _tkinter in Fedora Core 4
G'day John, There is a problem with a recent python update and fedora, if you go to http://forums.fedoraforum.org/ and do a search in the forum for tkinter and/or idle you will find the fix. I'm running Fedora Core 5 and found I had to install tkinter, I had to do a yum install tkinter. John From: John Hsu <[EMAIL PROTECTED]> To: Python Tutor Subject: [Tutor] How to compile _tkinter in Fedora Core 4 Date: Tue, 25 Apr 2006 17:11:32 +1200 Hi I'd very much appreciate your help if you can advise me how to compile _tkinter in Fedora 4. I installed Fedora core 4 in my machine recently, but struggled to get Tk working for me. The Tcl/Tk is working O.K. because I can run demo programs from /usr/share/tk8.4/demos. The error is caused by missing _tkinter library, and clearly there is no Tkinter module to be imported. I managed to download and recompile a new version of Python (2.4.3), then run "make test". The error message says: ... test_tcl test_tcl skipped -- No module named _tkinter ... 1 skip unexpected on linux2: test_tcl ... John Hsu ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor _ New year, new job there's more than 100,00 jobs at SEEK http://a.ninemsn.com.au/b.aspx?URL=http%3A%2F%2Fninemsn%2Eseek%2Ecom%2Eau&_t=752315885&_r=Jan05_tagline&_m=EXT ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need to automate connection
Payal Rathod wrote: > On Tue, Apr 25, 2006 at 06:59:29PM +1200, Liam Clarke wrote: > >>Hi Payal, >> >>I see you're connecting to an smtp server Any particular reason yoou >>can't use smtplib? >>http://www.python.org/doc/current/lib/module-smtplib.html > > > Because I don't know it exists :) > > But I don't want to send any mail. I just want to establish a connection > send MAIL TO: and RCPT TO: and exit. > Any ideas with that? With warm regards, > -Payal What about telnetlib? ;-) http://docs.python.org/lib/module-telnetlib.html HTH, Wolfram ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need to automate connection
Payal Rathod wrote: > On Tue, Apr 25, 2006 at 06:59:29PM +1200, Liam Clarke wrote: >> Hi Payal, >> >> I see you're connecting to an smtp server Any particular reason yoou >> can't use smtplib? >> http://www.python.org/doc/current/lib/module-smtplib.html > > Because I don't know it exists :) > > But I don't want to send any mail. I just want to establish a connection > send MAIL TO: and RCPT TO: and exit. > Any ideas with that? With warm regards, smtplib.SMTP has a docmd() method. Something like this (not tested so probably not quite right): import smtplib smtp = smtplib.SMTP('127.0.0.1') print smtp.docmd('mail', 'from: ') print smtp.docmd('rcpt', 'to: <[EMAIL PROTECTED]>') Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem installing MySQLdb under windows
I am running Python 2.4.3, MySQL 5.0.On trying to install MySQLdb 1.2.1 I ran into the following problem."D:\Python24\Lib\site-packages\MySQLdb>setup.py buildrunning buildrunning build_pycopying MySQLdb\release.py -> build\lib.win32- 2.4\MySQLdbrunning build_exterror: The .NET Framework SDK needs to be installed before building extensions for Python."Is it really necessary to install The .NET Framework SDK (354Mb) or is there a simpler way? Thanks for any helpPeter Jessop ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem installing MySQLdb under windows
Hi Peter, you can use a different compiler if you have one, such as mingw32, but I find compiling extensions on Windows is very problematic. Any particular features of MySQLdb 1.2.1 you need? 1.2.0 has a Windows version precompiled - http://prdownloads.sourceforge.net/mysql-python/MySQL-python.exe-1.2.0.win32-py2.4.zip?download If you really need to compile it, grab mingw32 and use the --compiler option for setup.py i.e. setup.py build --compiler mingw32, but I've had heaps of trouble with including Python headers, so YMMV. Regards, Liam Clarke On 4/26/06, Peter Jessop <[EMAIL PROTECTED]> wrote: > I am running Python 2.4.3, MySQL 5.0. > On trying to install MySQLdb 1.2.1 I ran into the following problem. > " > D:\Python24\Lib\site-packages\MySQLdb>setup.py build > running build > running build_py > copying MySQLdb\release.py -> build\lib.win32- 2.4\MySQLdb > running build_ext > error: The .NET Framework SDK needs to be installed before building > extensions f > or Python. > > " > Is it really necessary to install The .NET Framework SDK (354Mb) or is there > a simpler way? > > Thanks for any help > > Peter Jessop > > ___ > 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] Problem installing MySQLdb under windows
Peter Jessop wrote: > I am running Python 2.4.3, MySQL 5.0. > On trying to install MySQLdb 1.2.1 I ran into the following problem. > " > D:\Python24\Lib\site-packages\MySQLdb>setup.py build > running build > running build_py > copying MySQLdb\release.py -> build\lib.win32- 2.4\MySQLdb > running build_ext > error: The .NET Framework SDK needs to be installed before building > extensions f > or Python. > > " > Is it really necessary to install The .NET Framework SDK (354Mb) or is > there a simpler way? Download the windows binary, for example http://prdownloads.sourceforge.net/mysql-python/MySQL-python.exe-1.2.0.win32-py2.4.zip?download The latest version with a Windows build is 1.2.0. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem installing MySQLdb under windows
Thanks Liam and Kent.Problem is now sorted.Which brings me on to my next question... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Generating static WEB sites
I am looking at generating static web pages.What I wish to do is periodically regenerate a WEB site form HTML, text and MySQL data.I have been experimenting with HTMLgen and this seems to be adequate for what I need. However HTMLgen does not seem to have been updated for a while and I was wondering if I should be looking at any other packages.Any suggestions?ThanksPeter Jessop ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem installing MySQLdb under windows
Kent Johnson wrote: > Peter Jessop wrote: >> I am running Python 2.4.3, MySQL 5.0. >> On trying to install MySQLdb 1.2.1 I ran into the following problem. >> " >> D:\Python24\Lib\site-packages\MySQLdb>setup.py build >> running build >> running build_py >> copying MySQLdb\release.py -> build\lib.win32- 2.4\MySQLdb >> running build_ext >> error: The .NET Framework SDK needs to be installed before building >> extensions f >> or Python. >> >> " >> Is it really necessary to install The .NET Framework SDK (354Mb) or is >> there a simpler way? > > Download the windows binary, for example > http://prdownloads.sourceforge.net/mysql-python/MySQL-python.exe-1.2.0.win32-py2.4.zip?download > > The latest version with a Windows build is 1.2.0. > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Greetings Python group, I have a question related to the MySQLdb, I have recently installed the 1.2.0 version and it works fine. However, I am running ver. 4.1 of MySQL and the problem is that the python client version does not support the new password protocol of MySQL 4.1+. This only means that one must go into the MySQL and set the password using the OLD_PASSWORD function of MySQL. The question, do any of the newer versions of MySQLdb support the new password protocol? And if not, are there plans to implement this? Disclosure: this is my first post in this group, also it may be worth noting that I'm learning python as a side effect of developing a Plone site. So if my python knowledge comes across as a bit warped... well, Plone can sometimes be a strange thing ;) Regards, Andy Koch ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generating static WEB sites
Peter Jessop wrote: > I am looking at generating static web pages. > What I wish to do is periodically regenerate a WEB site form HTML, text > and MySQL data. > > I have been experimenting with HTMLgen and this seems to be adequate for > what I need. > > However HTMLgen does not seem to have been updated for a while and I was > wondering if I should be looking at any other packages. > > Any suggestions? Many. See this page, the sections Templating Engines, HTML Shorthand Processors and HTML Generation class libraries: http://wiki.python.org/moin/WebProgramming Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and aggregation
Kent Johnson writes: >> However here's what I'm now trying to do: >> >> 1) Not have to rely on using awk at all. >> >> >> 2) Create a dictionary with server names for keys e.g. server001, >> server002 etc and the aggregate of the request for that server as the value >> part of the pairing. >> >> >> I got this far with part 1) >> >> lbstat = commands.getoutput("sudo ~/ZLBbalctl --action=cells") >> tmpLst = lbstat.split('\n') >> >> rLst = [] >> for i in tmpLst: >> m = re.search(' server[0-9]+', i) >> if m: >> rLst.append(i) >> >> for i in rLst: >> print i, type(i) >> >> server001 alive 22.3% 6 requests/s 14527762 total >> server002 alive 23.5% 7 requests/s 14833265 total >> server003 alive 38.2%14 requests/s 14872750 total >> server004 alive 15.6% 4 requests/s 15083443 total >> server001 alive 24.1% 8 requests/s 14473672 total >> server002 alive 23.2% 7 requests/s 14810866 total >> server003 alive 30.2% 8 requests/s 14918322 total >> server004 alive 22.1% 6 requests/s 15137847 total >> >> At this point I ran out of ideas and began to think that there must be >> something fundamentally wrong with my approach. Not least of my concerns was >> the fact that I needed integers and these were strings. > > Don't get discouraged, you are on the right track! You had one big > string that included some data you are interested in and some you don't > want, you have converted that to a list of strings containing only the > lines of interest. That is a good first step. Now you have to extract > the data you want out of each line. > > Use line.split() to split the text into fields by whitespace: > In [1]: line = ' server001 alive 22.3% 6 requests/s 14527762 > total' > > In [2]: line.split() > Out[2]: ['server001', 'alive', '22.3%', '6', 'requests/s', '14527762', > 'total'] > > Indexing will pull out the field you want: > In [3]: line.split()[5] > Out[3]: '14527762' > > It's still a string: > In [4]: type(line.split()[5]) > Out[4]: > > Use int() to convert a string to an integer: > In [5]: int(line.split()[5]) > Out[5]: 14527762 > > Then you have to figure out how to accumulate the values in a dictionary > but get this much working first. > > Kent > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Thanks very much for the steer. I've made a fair bit of progress and look to be within touching distance of getting the problem cracked. Here's the list I'm starting with: >>> for i in rLst: >>> print i, type(i) server001 alive 17.1% 2 requests/s 14805416 total server001 alive 27.2% 7 requests/s 14851125 total server002 alive 22.9% 6 requests/s 15173311 total server002 alive 42.0% 8 requests/s 15147869 total server003 alive 17.9% 4 requests/s 15220280 total server003 alive 22.0% 4 requests/s 15260951 total server004 alive 18.5% 3 requests/s 15484524 total server004 alive 31.6% 9 requests/s 15429303 total I've split each string in the list, extracted what I want and feed it into an empty dictionary. >>> rDict ={} >>> i = 0 >>> while i < (len(rLst)): >>> x, y = rLst[i].split()[0], int(rLst[i].split()[3]) >>> rDict[x] = y >>> print x, y, type(x), type(y) >>> i += 1 server001 4 server001 5 server002 5 server002 9 server003 4 server003 6 server004 8 server004 12 I end up with this. >>> for key, value in rDict.items(): >>> print key, value server001 5 server003 6 server002 9 server004 12 As I understand things this is because the keys must be unique and are being replaced by the final key value pair being feed in from the loop. What I'm hoping to be able to do is update the value, rather than replace it, so that it gives me the total i.e. server001 9 server003 10 server002 14 server004 20 Regards, Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and aggregation
On 25 Apr 2006, [EMAIL PROTECTED] wrote: [...] > Here's the list I'm starting with: > for i in rLst: print i, type(i) > > server001 alive 17.1% 2 requests/s 14805416 total > server001 alive 27.2% 7 requests/s 14851125 total > server002 alive 22.9% 6 requests/s 15173311 total > server002 alive 42.0% 8 requests/s 15147869 total > server003 alive 17.9% 4 requests/s 15220280 total > server003 alive 22.0% 4 requests/s 15260951 total > server004 alive 18.5% 3 requests/s 15484524 total > server004 alive 31.6% 9 requests/s 15429303 total > > I've split each string in the list, extracted what I want and feed it into > an empty dictionary. > rDict ={} i = 0 while i < (len(rLst)): x, y = rLst[i].split()[0], int(rLst[i].split()[3]) rDict[x] = y print x, y, type(x), type(y) i += 1 [...] > What I'm hoping to be able to do is update the value, rather than replace > it, so that it gives me the total i.e. > server001 9 > server003 10 > server002 14 > server004 20 This is easily done. .>>> rdict = {} .>>> for line in lst: ans = line.split() rdict[ans[0]] = rdict.get(ans[0], 0) + int(ans[3]) .>>> rdict .{'server002': 14, 'server003': 8, 'server001': 9, 'server004': 12} Dictionaries have a get() method, which has an optional second argument, which gets returned if there is no such key in the dictionary. Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and aggregation
Right think I've got the idea now. Thanks for all contributions on this. Paul -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Karl "Pflästerer" Sent: 25 April 2006 22:28 To: tutor@python.org Subject: Re: [Tutor] Dictionaries and aggregation On 25 Apr 2006, [EMAIL PROTECTED] wrote: [...] > Here's the list I'm starting with: > for i in rLst: print i, type(i) > > server001 alive 17.1% 2 requests/s 14805416 total > server001 alive 27.2% 7 requests/s 14851125 total > server002 alive 22.9% 6 requests/s 15173311 total > server002 alive 42.0% 8 requests/s 15147869 total > server003 alive 17.9% 4 requests/s 15220280 total > server003 alive 22.0% 4 requests/s 15260951 total > server004 alive 18.5% 3 requests/s 15484524 total > server004 alive 31.6% 9 requests/s 15429303 total > > I've split each string in the list, extracted what I want and feed it into > an empty dictionary. > rDict ={} i = 0 while i < (len(rLst)): x, y = rLst[i].split()[0], int(rLst[i].split()[3]) rDict[x] = y print x, y, type(x), type(y) i += 1 [...] > What I'm hoping to be able to do is update the value, rather than replace > it, so that it gives me the total i.e. > server001 9 > server003 10 > server002 14 > server004 20 This is easily done. .>>> rdict = {} .>>> for line in lst: ans = line.split() rdict[ans[0]] = rdict.get(ans[0], 0) + int(ans[3]) .>>> rdict .{'server002': 14, 'server003': 8, 'server001': 9, 'server004': 12} Dictionaries have a get() method, which has an optional second argument, which gets returned if there is no such key in the dictionary. Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] seq looping
I have a loop that process each item in a sequence and after each item some updating is done to some variables. However i don't what these variable updated if we are processing the last item in the list. i could put in a conditional and test if the list is now empty after popping items from the list... but testing for an empty list and the bookkeeping of maintaining the popped list seems horribly inefficient and this is for a real time multimedia playback type situation so learning a more efficient idiom for this seems worth while: You won't have all the dependancies... including the player (STEREO) and some tables of durations.. but you get the gist: def playall(startime, amp, wet_percent, rest_percent, duty_factor, smpl_lst): ''' a play-loop that plays all samples in a directory, just once with some temporal padding and also returns the end of the last duration so that the begining of the next section can be determined''' event = 1; inskip = 0; inchan = 0; incr = 0 for sample in smpl_lst: splt = os.path.split(sample) rtinput(sample) loc = random.random() dur = DUR() STEREO(startime, inskip, dur, amp, loc) print "event no. %d @ %.2f (dur: %.2f, end: %.2f) --> sf: %s : [flag: %.2f]" % (event, startime, dur, startime+dur, splt[1], dry) incr = (dur * duty_factor) + kptools.windex(kptools.durations) startime = startime + incr restflag = random.random() if (restflag < rest_percent): rest = kptools.windex(kptools.rest) print "\n", "<>-" * 8, "[ rest : ", rest, "]", "-<>" * 8, "\n" startime = startime + rest event = event + 1 print '\n', 'Next start = ', startime, '\n\n' so what i am trying to do its skip that if (restflag < rest_percent): biz on the last item if we are on our last sequence item. The rests (which are random) is for padding between events and since we have just played our last event, we don't want any extra padding so that our next call of the loop starts at the proper time (just at the last event of this loop is over). The loop function will calculate the next start time, and return it so that we can use it as the start time argument for our next call of the loop. so if i want to play 7 items i might get something like: loop_call_01: item_1 item_2 rest item_3 rest item_4 item_5 item_6 rest item_7 (but we don't want any rest here ever! cause we might want our next loop to start w/o any pause) gosh.. i hope this is clear anyway that's my query .. hehe ... cheers, kevin ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding the use of files to store intermediate results
On 4/25/06, Hugo González Monteverde <[EMAIL PROTECTED]> wrote: > Remember duck typing. An object just needs to look like a file in order > to be used like one. > > Guido's time machine has already forseen your problem. Take a look at > the StringIO module. It allows you to use a string where you would > normally pass a file object. > Since I do something like os.open("python some_file.py > some_output") I don't see how I can pass a file-like object. As far as I can tell "python" (the command) looks for a real file on the current path. André > Hope that helps, > > Hugo > > > ## > > While this works, I find it "messy", as it creates some intermediate > > files. I was wondering if there was a better way to do things all in > > memory, in an OS independent way. > > > > [Note that the complete application is approximately 665 lines long > > ... a bit too much > > to post all here :-)] > > > > André > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] A Python idiom that I don't get
I am trying to get some existing CPython 2.4 code working under Jython (2.1) and I am puzzled by a line in the following function. It seems to be a Python 2.4 idiom that is opaque to me. The line is: prefix = os.path.commonprefix(filter( bool, lines )) and I don't understand what that 'bool' is doing. Or rather, I think that I see what it is doing, but I am not sure - and I don't much like it. filter is the built-in filter and it requires a callable returning a bool as the first argument. It seems that 'bool' without arguments is a callable that always evaluates to True (or at least non-zero) so this 'bool' always returns True. Is this really true (sic) by intention or is it just an implemenation artifact? I tried replacing 'bool' with 'True' but that won't work because True is not callable. I replaced 'bool' with 'lambda True: True' as in: prefix = os.path.commonprefix(filter( lambda True: True, lines )) and that does seem to work - and pass its unit tests. Have I got this right and can I replace 'bool' with the lambda expression? Or is there a clearer way to do this? Thanks, Don. The full function is: def find_common( lines ): """find and return a common prefix to all the passed lines. Should not include trailing spaces """ if not lines: return "" # get the common prefix of the non-blank lines. This isn't the most # efficient way of doing this by _far_, but it _is_ the shortest prefix = os.path.commonprefix(filter( bool, lines )) if not prefix: return "" # this regular expression will match an 'interesting' line prefix matched = re.match("(\s*\w{1,4}>|\W+)", prefix) if not matched: return "" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A Python idiom that I don't get
Don Taylor wrote: > I am trying to get some existing CPython 2.4 code working under Jython > (2.1) and I am puzzled by a line in the following function. It seems to > be a Python 2.4 idiom that is opaque to me. > > The line is: > prefix = os.path.commonprefix(filter( bool, lines )) > > and I don't understand what that 'bool' is doing. Or rather, I think > that I see what it is doing, but I am not sure - and I don't much like it. > > filter is the built-in filter and it requires a callable returning a > bool as the first argument. It seems that 'bool' without arguments is a > callable that always evaluates to True (or at least non-zero) so this > 'bool' always returns True. Is this really true (sic) by intention or > is it just an implemenation artifact? No, bool() doesn't always return true, it returns true for arguments that would evaluate to true in a boolean context, and false otherwise. In [2]: bool(0) Out[2]: False In [3]: bool(1) Out[3]: True In [4]: bool([]) Out[4]: False In [5]: bool(42) Out[5]: True > > I tried replacing 'bool' with 'True' but that won't work because True is > not callable. > > I replaced 'bool' with 'lambda True: True' as in: > prefix = os.path.commonprefix(filter( lambda True: True, lines )) > and that does seem to work - and pass its unit tests. This works but it isn't doing what you think it is. lambda True: True is the same as lambda x: x i.e. it is just an identity function. > > Have I got this right and can I replace 'bool' with the lambda expression? > > Or is there a clearer way to do this? Try filter(None, lines) or use a list comprehension: [ line for line in lines if line ] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Avoiding the use of files to store intermediate results
Andre Roberge wrote: > On 4/25/06, Hugo González Monteverde <[EMAIL PROTECTED]> wrote: > >> Remember duck typing. An object just needs to look like a file in order >> to be used like one. >> >> Guido's time machine has already forseen your problem. Take a look at >> the StringIO module. It allows you to use a string where you would >> normally pass a file object. >> >> > > Since I do something like > os.open("python some_file.py > some_output") > I don't see how I can pass a file-like object. As far as I can tell > "python" (the command) looks for a real file on the current path. > How about using one of the os.popen functions, that returns a file-like object for stdout? > André > > >> Hope that helps, >> >> Hugo >> >> >>> ## >>> While this works, I find it "messy", as it creates some intermediate >>> files. I was wondering if there was a better way to do things all in >>> memory, in an OS independent way. >>> >>> [Note that the complete application is approximately 665 lines long >>> ... a bit too much >>> to post all here :-)] >>> >>> André >>> ___ >>> Tutor maillist - Tutor@python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> > ___ > 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] A Python idiom that I don't get
> > prefix = os.path.commonprefix(filter( bool, lines )) that is an interesting and yes, not very optimal way of saving the set of non-blank lines. the commonprefix() function takes a list of pathnames and returns the longest prefix that all strings have in common, presumably for disk filenames, although this sounds like just a string processing function that would work with non-pathnames too. > > and I don't understand what that 'bool' is doing. Or rather, I think > > that I see what it is doing, but I am not sure - and I don't much like it. >: > > I tried replacing 'bool'... it sounds like you did not develop the original code. it seems to work... why are you trying to replace it? are you refactoring? > This works but it isn't doing what you think it is. > lambda True: True > is the same as > lambda x: x kent is right. True is not a keyword, so you are just using "True" as a variable name. scarily, one can do stuff like this: >>> True, False = False, True >>> True False >>> False True > Try filter(None, lines) or use a list comprehension: > [ line for line in lines if line ] i like both of these better... they will perform better than with bool(). of the two, it would be interesting to study which one's faster... and why. -wesley ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] getting system temporary directory.
Hi all, Does python has any function to get a system tempdir of an OS? Thanks, Phon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor