Can local function access local variables in main program?
I am confused by the following program: def f(): print x x=12345 f() result is: 12345 however: def f(): print x x=0 x=12345 f() result is: Traceback (most recent call last): File ...\test.py, line 5, in ? f() File ...\test.py, line 2, in f print x UnboundLocalError: local variable 'x' referenced before assignment I am using Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 I also tested it on python 2.5, which gives the same result. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can local function access local variables in main program?
Actually I am quite satisfied with and error, which is my expectation. But the implicit global variable access seems quite uncomfortable to me. Why is that necessary? On Nov 3, 3:39 am, Stargaming [EMAIL PROTECTED] wrote: On Sat, 03 Nov 2007 07:18:17 +, Sullivan WxPyQtKinter wrote: I am confused by the following program: def f(): print x x=12345 f() result is: 12345 If python can't discover x in your current scope and you do not bind to it there, it will automatically access that global name x. -- http://mail.python.org/mailman/listinfo/python-list
Seek the one billionth line in a file containing 3 billion lines.
I have a huge log file which contains 3,453,299,000 lines with different lengths. It is not possible to calculate the absolute position of the beginning of the one billionth line. Are there efficient way to seek to the beginning of that line in python? This program: for i in range(10): f.readline() is absolutely every slow Thank you so much for help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Seek the one billionth line in a file containing 3 billion lines.
On Aug 8, 2:35 am, Paul Rubin http://[EMAIL PROTECTED] wrote: Sullivan WxPyQtKinter [EMAIL PROTECTED] writes: This program: for i in range(10): f.readline() is absolutely every slow There are two problems: 1) range(10) builds a list of a billion elements in memory, which is many gigabytes and probably thrashing your machine. You want to use xrange instead of range, which builds an iterator (i.e. something that uses just a small amount of memory, and generates the values on the fly instead of precomputing a list). 2) f.readline() reads an entire line of input which (depending on the nature of the log file) could also be of very large size. If you're sure the log file contents are sensible (lines up to several megabytes shouldn't cause a problem) then you can do it that way, but otherwise you want to read fixed size units. Thank you for pointing out these two problem. I wrote this program just to say that how inefficient it is to use a seemingly NATIVE way to seek a such a big file. No other intention -- http://mail.python.org/mailman/listinfo/python-list
Python CGI problem: correct result, but incorrect browser response.
title:Python CGI problem: correct result, but incorrect browser response. In one of my CGI program,named 'login.py', the script return a HEADER to web browser: Set-Cookie: sessionID=LAABUQLUCZIQJTZDWTFE; Set-Cookie: username=testuser; Status:302 Location:edit.py (blank line) but the IE prompted to let me choose to save the 'login.py'. When I save it, the file is just the header. That means the IE failed to parse the header. My IE has already enabled cookie read and write. I also tried Firefox, but the result is the same. How does this happen? -- http://mail.python.org/mailman/listinfo/python-list
debug CGI with complex forms
When the form in one HTML is very complex with a lot of fields(input, button,radio,checkbox etc.), setting the environment is quite burdernsome, so I usually change the stdout and stderr of the submit processing script to a file object to see the output directly from that file. This just can do, but still inconvinient. Anyone has more suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Does anyone know where is the Berkeley XML Database documentation for its Python API?
I have been looking for python API documentation of BDBXML for quite a few days but I could not find it. Anyone has any idea where it is? Or if there is not such a thing at all, how could I get started? In addition, in the previous posts I have seen some grumble about python API's lack of XMLexception and some basic programming and debugging elements for project engineering. Are they added or fixed in the lastest version of BSBXML? Thank you for help. -- http://mail.python.org/mailman/listinfo/python-list
Re: DOM and HTML
I do not know much about the HTML DOMBut I think if you just mean treating HTML like XML and build it into a DOM tree and (Very important) the HTML file is not a 1 lines or even longer one, then go ahead to xml.dom.minidom module for help. It has a basic (and great) implementation for light-weighted DOM implementation. -- http://mail.python.org/mailman/listinfo/python-list
Berkeley DB XML vs 4suite for fast searching in XML DB?
Storing XML in relational database with indexing feature is exactly what I need. But 4suite is mentioned from time to time and seemingly holding better support for python. I have no idea if 4 suite has provide strong support for random access or relatively random access for XML database and with indexing features, to support fast search. Who could compare the efficiency different between the two? -- http://mail.python.org/mailman/listinfo/python-list
How to search HUGE XML with DOM?
a relation database has admiring search efficiency when the database is very big (several thousands or tens of thousands of records). But my current project is based on XML, for its tree-like data structure has much more flexibility; and DOM, which could be manipulated just like a tree. However, how to establish such a XML data base for search when it contains 10,000 records (One record usually contain 10~30 tags) or more? My search needs: 1. Search and return all the record (an element) with specific id. 2. Search and return all the record whose child nodes has a specific id or attribute. the xml.dom.minidom object is too slow when parsing such a big XML file to a DOM object. while pulldom should spend quite a long time going through the whole database file. How to enhance the searching speed? Are there existing solution or algorithm? Thank you for your suggetion... -- http://mail.python.org/mailman/listinfo/python-list
Re: How to search HUGE XML with DOM?
Perhaps what you have said is correct. But XML is more direct for programmers and readers in my view point. bayerj 写道: Mind, that XML documents are not more flexible than RDBMS. You can represent any XML document in a RDBMS. You cannot represent any RDBMS in an XML document. RDBMS are (strictly spoken) relations and XML documents are trees. Relations are superior to trees, at least mathematically speaking. Once you have set up your system in a practicable way (e.G. not needing to create a new table via SQL Queries for a new type of node, which would be a pain) SQL is far superior to XML. Anyway, cElementTree seems to be the best way to go for you now. Its performance is untopped by any other python xml library, as far as I know. -- http://mail.python.org/mailman/listinfo/python-list
Very stupid question.
How to get the length of a file via build-in file object support? In Visual Basic there is len(file) of something like that. But in python, where is this property? Sorry for this stupid question, if it is. Thank you for help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Very stupid question.
In addition, f=file('filename','r');len(f.read()) is quite expensive in my point of view, If the file is serveral MB or larger. -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI redirection: let us discuss it further
Well, in that case, the internal direction is just what I need. Thank you so much for help. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do clients(web browser) close a python CGI program that is not responding?
Actually my project is converting certain specially costomized XML file to HTML to display and edit. Sometimes the XML file is too big, or the client upload a very huge file for the server to process, which exceeds the processing ability of my server.(after all, it is a small server on my poor laptopwhich use winXP and IIS..not professional, huh?) I configures IIS to terminate CGI program if it do not complete in 20 sec. But it does not workPerhaps I should go to a IIS or apache forum for answer. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to inplement Session in CGI
bruno at modulix 写道: Sullivan WxPyQtKinter wrote: Python disappointly failed to provide a convinient cgi session management module. Probably because there are much better options for web programming in Python ? Really? Then what is it? -- http://mail.python.org/mailman/listinfo/python-list
No Cookie: how to implement session?
I do not want to use Cookies in my site since not all web browser support it well and sometimes people close cookie functioning for security reasons. I tried to add hidden field with a sessionID in every python CGI script generated web pages, so everytime my client POST a request, the server will retrieve the sessionID and decide if it is in the same session. However, since python cgi do not have a function for redirecting to a page, I use Location: url http head or body onload=document.location=\'%s\'/body javascript for redirecting.in this case, hidden field could not be used any more. Really wish python would have session management or equivalent in standard CGI module -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI redirection: let us discuss it further
Just read the name of the server (os.environ['SERVER_NAME']) to work out what absolute URL to redirect to, whist still being portable. Here's some code I dug up that should also cope with non-default ports and SSL, if that's of any use: ssl= os.environ.get('HTTPS', 'off') not in ('', 'off', 'false', 'no') scheme= ['http', 'https'][ssl] port= ['80', '443'][ssl] host= os.environ.get('SERVER_NAME', 'localhost') url= '%s://%s:%s' % (scheme, host, os.environ.get('SERVER_PORT', port)) if url.endswith(':'+port): server= server[:-(len(port)+1)] url+= path (You *can* pass relative URLs back to the web server in a Location: header, but this should do an internal redirect inside the server, which may not be what you want.) Sorry I do not quite understand what is the difference between an internal redirection and an external one? -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: No Cookie: how to implement session?
As you said, There is no solution? I mean, tracing a real session without using tricks like hidden field and cookies in CGI script? Dennis Lee Bieber 写道: On 28 Mar 2006 09:40:24 -0800, Sullivan WxPyQtKinter [EMAIL PROTECTED] declaimed the following in comp.lang.python: I do not want to use Cookies in my site since not all web browser support it well and sometimes people close cookie functioning for security reasons. Yes... And watch them flounder on sites that use cookies /for/ a form of security (ie, those sites that require logins...) Cookies can be set to expire, so the session can time-out... whereas... I tried to add hidden field with a sessionID in every python CGI script generated web pages, so everytime my client POST a request, the server This would imply that a client could start a session today, and finally submit tomorrow... There's no real time-out capability unless you run some background timer thread for each session ID... will retrieve the sessionID and decide if it is in the same session. However, since python cgi do not have a function for redirecting to a page, I use Location: url http head or body Isn't redirect normally the responsibility of the web server /before/ invoking the CGI script itself? I'll concede I'm weak on that level of detail. Really wish python would have session management or equivalent in standard CGI module The standard CGI module is only the lowest common base for dynamic web pages. The technology goes back decades, possibly even predating cookies. Look at the name: Common Gateway Interface... It's a building block responsible for getting submitted form data, as passed by the web server environment, and returning generated data -- the interface between an application and the web server. All else must be built on top of it -- hence separate modules for Cookie control, etc. -- == [EMAIL PROTECTED] | Wulfraed Dennis Lee Bieber KD6MOG [EMAIL PROTECTED] | Bestiaria Support Staff == Home Page: http://www.dm.net/~wulfraed/ Overflow Page: http://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list
CGI redirection: let us discuss it further
I am now programming python scripts for CGI environment. The redirection has been discussed in this forum for over one hundred times. I have seen most of them, but still have some questions: 1. Are there any method (in python of course) to redirect to a web page without causing a Back button trap(ie, when user click the back button on their web browser, they are redirect to their current page, while their hope is probably to go back to the last page they have seen, rather than the redirection page with a Location: url head and blank content.)? 2. Are there any method to use relative path, rather than full absolute URI path in Location: url? It is very essential for later transplant work, e.g.,transplant a folder of cgi scripts from one web server to another, with different URL. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
To my clients force to terminate a python CGI program?
Hi,there. Sometimes a python CGI script tries to output great quantities of HTML responce or in other cases, it just falls into a dead loop. How could my client close that CGI script running on the server? I tried to use the STOP button in the web browser button, but it does not work. In addition, how could I configure that if a CGI program do not finish its task in 20sec or so, it will be automatically terminated? -- http://mail.python.org/mailman/listinfo/python-list
How do clients(web browser) close a python CGI program that is not responding?
Hi,there. Sometimes a python CGI script tries to output great quantities of HTML responce or in other cases, it just falls into a dead loop. How could my client close that CGI script running on the server? I tried to use the STOP button in the web browser button, but it does not work. In addition, how could I configure that if a CGI program do not finish its task in 20sec or so, it will be automatically terminated? -- http://mail.python.org/mailman/listinfo/python-list
How to inplement Session in CGI
Python disappointly failed to provide a convinient cgi session management module. Not willing to use external modules, I would like to implement a simplest Session object on my own. The basic problem is: how could a python CGI program understand several requests are in the same session? Definately, request from different IP would be easy to identified to be in different sessions, but request from the same IP would not. Anyone has any idea? Thank you:-) -- http://mail.python.org/mailman/listinfo/python-list
How to search XML? Are there special libs?
I am now using XML to record my lab records in quite a complex way, in which about 80 tags are used to identify different types of data. I think it is a good idea to search for a popular and mature XML search engine before I started to program it myself: I need the following functions: Search and get all the records that share the same tree structures. Search records containing a specific node or sub tree structres. Does anyone know about something suitable? Or other powerful XML search engine in Python? Thank you so much for help. PS: I do not care about the learning curve, as long as it is not a cliff to climb. -- http://mail.python.org/mailman/listinfo/python-list
Is xml.dom.minidom.Element.cloneNode(deep=True) really fixed already?
Hi, I am now using minidom for my current development. I use cloneNode method in Element object, but it just does not work. The test code is very simple as follows: =CODE== from xml.dom.minidom import * a=Element('see') print a.toprettyxml() b=a.cloneNode(True) print b.toprettyxml() ---END CODE--- see i=sullivan/ Traceback (most recent call last): File D:\__Apply\test8XML.py, line 10, in -toplevel- b=a.cloneNode(True) File C:\Python24\lib\xml\dom\minidom.py, line 211, in cloneNode return _clone_node(self, deep, self.ownerDocument or self) File C:\Python24\lib\xml\dom\minidom.py, line 1814, in _clone_node if node.ownerDocument.isSameNode(newOwnerDocument): AttributeError: 'NoneType' object has no attribute 'isSameNode' --result end-- Since the prototype of cloneNode is cloneNode(deep), which does not indicate the type of deep, I also tied other non-None value like cloneNode(1), cloneNode('a') etc.None of them work. Referencing the document, I have seen in python 2.0, this function was very broken. But my Python version is quite up-to-date: version 2.4.1. Is it a bug still not fixed?? -- http://mail.python.org/mailman/listinfo/python-list
Can not find a file in CMD model python when everything is OK in IDLE
I use python in Windows XP platform. I find that if I write a .py file in a directory, such as windows desktop, in which a file named 'ticket.txt' is located: f=open(\ticket.txt) print f.read() In IDLE, this py file work all right. But if I launch python interpretor in the command shell like this: C:\Documents and Settings\Xiaozhong Zhengpython C:\Documents and Settings\Xiao zhong Zheng\Desktop\t.py The interpretor would not find the file. Traceback (most recent call last): File C:\Documents and Settings\Xiaozhong Zheng\Desktop\t.py, line 1, in ? f=open(ticket.txt) IOError: [Errno 2] No such file or directory: 'ticket.txt' Anyone knows why? In addition, if I start IIS web service that runs .py file as CGI program, then this .py file also works. -- http://mail.python.org/mailman/listinfo/python-list
Can not find a file in CMD model python when everything is OK in IDLE
I use python on Windows XP platform. I find that if I write a .py file in a directory, such as windows desktop, in which a file named 'ticket.txt' is located: f=open(ticket.txt) print f.read() In IDLE, this py file work all right. But if I launch python interpretor in the command shell like this: C:\Documents and Settings\Xiaozhong Zhengpython C:\Documents and Settings\Xiaozhong Zheng\Desktop\t.py The interpretor would not find the file. Traceback (most recent call last): File C:\Documents and Settings\Xiaozhong Zheng\Desktop\t.py, line 1, in ? f=open(ticket.txt) IOError: [Errno 2] No such file or directory: 'ticket.txt' Anyone knows why? In addition, if I start IIS web service that runs .py file as CGI program, then this .py file also works. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not find a file in CMD model python when everything is OK in IDLE
Sorry, I mistyped the line. In the program it IS: f=open(ticket.txt), no '\' included. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not find a file in CMD model python when everything is OK in IDLE
I see. I once was a VB programmer. In VB, the current directory is always set to where the module locates before it runs. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not find a file in CMD model python when everything is OK in IDLE
I see. I once was a VB programmer. In VB, the current directory is always set to where the module locates before it runs. -- http://mail.python.org/mailman/listinfo/python-list
How to refer to the function object itself in the function per se?
When debugging using 'print' statement, I usually want to print some important values together with the function name as the context of the values printed out. So my hope is that I could get the name of the function. Since every function object actually has a private __name__ attribute that gives its name, but when I print __name__ in a function, it usually print the public module-level __name__ attribute, ie, 'main', rather than the function level __name__. So how could I refer to the function object per se, in the body of the function itself? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to refer to the function object itself in the function per se?
I have Google the whole thing and find another way for alternative implementation of getting the function's name. But all they returns are just strings. If I would like to refer to the function object in order to call it recursively, what shall I do then? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to refer to the function object itself in the function per se?
I am sorry but you misunderstood my idea. What I want is a generalized method to print out the function name, or refer to the name of a function. If I use f.__name__, I think I should just use print f to save my keyboard. What I expect is using a method, or attribute, or another function to get the name of a function. Kay Schluehr 写道: Sullivan WxPyQtKinter wrote: So how could I refer to the function object per se, in the body of the function itself? Just use the name. def f(): print f.__name__ f() f -- http://mail.python.org/mailman/listinfo/python-list
Python IDE: great headache....
IDLE is no longer satisfactory for me. Other IDEs make me very confused. Really do not know which one to use. I use WinXP sp2 for current development. So far as I know, Eclipse + PyDev + PyDev Extension is perfect for source code editing. Since I am really not sure how to use the debugger module, I really do not know how to add watch to variables etc. Anyone knows if this platform is a good one? I hope that an IDE should be featured with: 1. Grammar Colored highlights. 2. Manage project in a tree view or something alike, ie, a project file navigator. 3. Code collapse and folding. 4. Code auto-completion: especially prompting function parameters when I am typing a function previously defined by myself. Like the one in Visual Studio series. 5. Debugging: Breakpoints, conditional pause. watch for variables.step into, over and out of a function. What about other IDEs? Since I do not need GUI development. More over, the free-of-charge IDE is highly preferred. 6.Indentation management like in IDLE: press ctrl+[/] to modify the identation of a line or a block. In addition, I have seen quite a few editors, which are definitely not what I want. Thank you so much for suggestions. -- http://mail.python.org/mailman/listinfo/python-list
Any python HTML generator libs?
Hi, everyone. Simply put, what I need most now is a python lib to generate simple HTML. I am now using XML to store my lab report records. I found python really convinient to manipulate XML, so I want to make a small on-line CGI program to help my colleagues to build their lab report records into XML, for storage, HTML display (for others to browse) and search. With python's standard lib, the DOM object could realize the XML storage and search quite easily, but for HTML generation, it is a great headache. I tried to turn to the in-line server-side python script PSP(something like asp and php) instead of CGI. However, since the report data is always of very complex data structures, it is really hard to write most things in-line. For example, a PCR reaction is expected to be shown in this format or alike on a web browser: PCR Sample: Sm1032 Operater: SullivanZ TimeStamp: hh:mm mm-dd- Reaction: Reagent1: Name: Concentration: mM Volumn:XXX uL Reagent2: Since there are hundreds of PCR reaction and other operations in the lab report, in-line PSP is not a suitable solution. But writing HTML directly with print statement is a great pain. Will XSTL be useful? Is my problem somewho related with XML-SIG? Looking forward to your precious suggestion. -- http://mail.python.org/mailman/listinfo/python-list
Re: Any python HTML generator libs?
Sorry I am completely a green-hand in HTML. What is HTMLTemplate and ElementTree? Would you please post some source code as an example? Of course I would Google them to find out more. Thank you so much. -- http://mail.python.org/mailman/listinfo/python-list
Re: Any python HTML generator libs?
That lib can help. But still, I have to code a lot using that lib. Maybe my program is quite strange, far from common. Thank you, after all~! -- http://mail.python.org/mailman/listinfo/python-list
Confused by Method(function) of a module and method of a class/instance
In python, these expression seems yields the same result: inputstring='ABC' print inputstring.lower() print lower(inputstring) print string.lower(inputstring) result: abc abc abc Question: Is the method lower() just a method for the inputstring instance( an instrance object of a string class object), or a function in the module string.py or a build-in function or sth else? Why do the three expression yield the same result abc? -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused by Method(function) of a module and method of aclass/instance
Yes, I checked out that I have already run from string import *. So the lower() means string.lower() function. However, something else came out just now: instr='a' instr.join('b') 'b' instr.lower() 'A' instr 'a' Both as the method of the type str, join never use the instr instance object as method parameters while lower do. Compared with the .lower() method, the instr.join() looks like an independent function, which use the parameters in the parenthesis and return a value without instr being changed. So why should it be programmed into the str type? More confusing things came out to me: str().lower() '' #well, this is understandable. str.lower(str(),'A') 'a' How do you explain this result? Sincerely, thank you so much for help. -- http://mail.python.org/mailman/listinfo/python-list
It is fun.the result of str.lower(str())
Guess what would be the result of these functions: str.lower('ASFA') str.join(str(),['1','1','1']) str.join('a','b') If you guess them correctly, please explain. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making a tiny script language using python: I need a string processing lib
I have gone over the shlex and cmd and so, but none of them are satisfactory. However, I tried to program this this function on my own and found it pretty easy. Thank you so much for the suggestion, after all. -- http://mail.python.org/mailman/listinfo/python-list
Making a tiny script language using python: I need a string processing lib
I do not know if there is any lib specially designed to process the strings in scipt language. for example: I hope to process the stringprint a,b,c,d,e in the formcommand argumentlist and return: {'command'='print', 'argumentlist'=['a','b','c','d','e']} Are there any lib to implement this? Ideally , the lib should have a function like below: def extract(commandstring, splitformat) eg: extract([EMAIL PROTECTED],[EMAIL PROTECTED]) return: {what:see,whom:you,when:tonight} extract(1;2;3;4;5,list[;]) return: {list:['1','2','3','4','5']} extract(print a,b,c,d,e,command arglist[,]) return: {command:print,arglist:['a','b','c','d','e']} extract(fruit:apple~orgrange~pear~grape#name:tom;sam;dim;ham ,class1:instance1[~]#class2:instance2[;]) return: {class1:fruit, instance1:['apple','orange','pear','grape'], class2:name, instance2:['tom','sam','dim','ham'] } ### # # # # ## # # # ## # # # ##### ## # # # ## # # # ## # # # -- http://mail.python.org/mailman/listinfo/python-list
Re: Any Tkinker based rich text widget?
yes, I have tried Text Widget for quite some time. However, its speed is far from satisfying. When it holds more than 20,000 characters, it starts to response quite slow. When you drag your mouse over some text to select, the selection is usually done after 0.5second or so. My intent to use this widget is to compose and process report as long as 30 to 60 pages. Is it the problem of the Text widget or my usage? the following is the code for testing Text widget. Run it and paste 20,000 characters, and then it is retardant ===Code Start= from Tkinter import * root=Tk() root.title(Lab Report Editor) text=Text(root,height=25) text.grid(row=0,column=0,sticky=N+S+W+E) root.columnconfigure(0,weight=1) root.rowconfigure(0,weight=1) root.mainloop() ===Code End=== -- http://mail.python.org/mailman/listinfo/python-list
Re: Any Tkinker based rich text widget?
Thank you so much for help. It is my honor to get a reply from a prestigious figure like you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Any Tkinker based rich text widget?
In addiiton, I hope it directly support basic HTML grammar. [EMAIL PROTECTED] 写道: Hi all I am using the standard python GUI Tkinter as my program's main interface. Although I know wxPython has some widget to support rich text widget, but I do not have time to shift to wx series. Does anyone know any Tkinter based widget that support: 1. Blod, Italic, Underline and their combinations. 2. Several most commonly used fonts, like Times New Roman and Arial 3. Multiline text 4. Cross platform support. Available in Linux-RedHat and Mac OS series and Windows 2000 or above. 5.Image embedding. Support jpeg, gif, bmp. The more the better. and better support: Hyperlink, Text color, the more the better. Thank you so much for help! -- http://mail.python.org/mailman/listinfo/python-list