Re: append
On 10 May, 18:02, HMS Surprise <[EMAIL PROTECTED]> wrote: > Trying not to be a whiner but I sure have trouble finding syntax in > the reference material. I want to know about list operations such as > append. Is there a pop type function? I looked in tutorial, language > reference, and lib for list, append, sequence. Is there a place where > us doofi ( who may not have our heads out in the sunlight) may find > all related syntax grouped together? > >>> dir(list()) ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] This doesn't give syntax, but if you have any questions, try: >>> help(list().append) -- http://mail.python.org/mailman/listinfo/python-list
Re: a newbi problem: can't find complete python curses library
krishnakant Mane wrote: > I will like to use the curses or ncurses library for the menus and the > input forms with add, save, delete, update and cancel buttens. > I also need to create drop down menus with a top level menu bar. > I read a few articles about python wrapping curses but can't find some > thing very comprehencive. > my question is, > does python possess a complete wrapper to ncurses and its related > libraries like menu, panel and form? > if yes then where can I find documentation? Try section 14.11.2 of the library reference. http://docs.python.org/lib/curses-panel-objects.html -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: unloading extension library
Bill Pursell wrote: > I've got a simple extension module that contains two functions: > void hi(void) __attribute__((constructor)); > void hi(void) { printf("Hi!\n");} > void bye(void) __attribute__((destructor)); > void bye(void) { printf("Bye!\n");} > > > When I run in the interpreter: > > >>> import spam > Hi! > >>> del spam > >>> > > Notice that the destructor isn't called. How can I force python > to dlclose() the library and ensure that my destructors get called? > There is something about my google-fu that only allows me to find things 25 seconds after I post >>> del sys.modules["spam"] >>> del spam Should remove all the references, but I still don't want to wait for garbage collection. I need to be sure that the dlclose() happens and the destructors are called. Can I do that? (without relying on ctypes, preferrably.) -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
unloading extension library
I've got a simple extension module that contains two functions: void hi(void) __attribute__((constructor)); void hi(void) { printf("Hi!\n");} void bye(void) __attribute__((destructor)); void bye(void) { printf("Bye!\n");} When I run in the interpreter: >>> import spam Hi! >>> del spam >>> Notice that the destructor isn't called. How can I force python to dlclose() the library and ensure that my destructors get called? -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: Starting out.
Grant Edwards wrote: > > Perl has syntax? ROTFLMAO. In fact, that even motivated: Psychotically Engineered Random Language. -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get file name of the running .py file
Jason Jiang wrote: > Hi, > > How to get the name of the running .py file like the macro _FILE_ in C? There are many ways--IMO the easiest is with __file__: >>> print __file__ /home/bill/.pystart >>> [tmp]$ cat foo.py #!/usr/bin/env python print "The name of the file is:%s"%__file__ [tmp]$ ./foo.py The name of the file is:./foo.py -- http://mail.python.org/mailman/listinfo/python-list
Re: List Splitting
Steven wrote: > Hello everyone, > > I'm trying to work through a bit of a logic issue I'm having with a > script I'm writing. Essentially, I have a list that's returned to > me from another command that I need to regroup based on some aribitrary > length. > > For the purposes of this question, the list will be: > > t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] > > Now, I know that every 3rd element of the list belongs together: > > Group 1 = 0, 3, 6 > Group 2 = 1, 4, 7 > Group 3 = 2, 5, 8 > > I'm trying to sort this list out so that I get a list of lists that > contain the correct elements: > > Goal = [ [ "a", "n", "t"], [ "b", "a", "t"], > ["c", "a", "t" ] ] > > The actual data isn't as simple as this, but if I can get the logic > sorted out, I can handle the other part. > > Anyone have any good ideas on how to do this? how about: >>> t = [ "a", "b", "c", "n", "a", "a", "t", "t", "t" ] >>> [t[i::3] for i in range(0,len(t)/3)] [['a', 'n', 't'], ['b', 'a', 't'], ['c', 'a', 't']] -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: import
Georg Brandl wrote: > 01 wrote: > > Georg Brandl wrote: > >> [EMAIL PROTECTED] wrote: > >> > bugnthecode 写道: > >> > > >> >> How are you trying to import it? Is it in the same directory as your > >> >> other script? If not is your python path set correctly? > >> >> > >> >> When importing a module that you have written you exlude the .py > >> >> extension. You should be using: > >> >> import hello > >> >> > >> >> Hope that helps, > >> >> Will > >> > > >> > i am on a windows platform. i have written scrip named 123.py. it can > >> > be run. ok i save it to C:\Python24 ,exactly the same dir where python > >> > works. but " import 123" doesnt work. > >> > >> You can't import modules whose names have non-identifier names with > >> plain "import". Or would you like "123" to refer to a module? > >> > >> If you have to do this (and I have a strong feeling that you haven't) > >> use the built-in function __import__(). > >> > > you have to name your program with the name mymodule,or something like > > that when you use "import". does python have a clear declaration on > > how to name a module? > > A module name can be everything also usable as a Python identifier. This > means that it contains only letters, digits and underscores and doesn't > start with a digit. > > Running a file as the main file, using "python 123.py" works because > Python then won't need to provide the file name as a name in the script. On an almost totally unrelated note...(except that it definitely belongs in a thread entitled "import")... I've occasionally run into difficulty when I fail to include a she-bang in my script. If the first word of the script is import, which it often is for quickly written scripts that contain an import line before any doc string and that doesn't have "#!/usr/bin/env python", bash doesn't report a syntax error. Instead, it annoyingly runs the ImageMagick command import, which dutifully sits there waiting for a mouse click. The first time this happened, it took me a good 15 minutes to figure out what was happening. (The first instance was more subtle than forgetting the she-bang, as I'd merely forgotten the "bang" and the "#/usr/bin/env python" looked pretty good when I looked at the file.) I'm just curious if anyone else has ever bumped into that mistake. -- http://mail.python.org/mailman/listinfo/python-list
Re: sum and strings
Georg Brandl wrote: > Paul Rubin wrote: > > Sybren Stuvel <[EMAIL PROTECTED]> writes: > >> Because of "there should only be one way to do it, and that way should > >> be obvious". There are already the str.join and unicode.join methods, > > > > Those are obvious??? > > Why would you try to sum up strings? Besides, the ''.join idiom is quite > common in Python. One could extend this argument to dissallow the following: >>> "foo" + "bar" -- http://mail.python.org/mailman/listinfo/python-list
Re: PySequence_SetItem
Bill Pursell wrote: > The following code is pretty much straight out of > section 1.2.1.1 of the Python/C reference manual: > > #include > > int > main(void) > { > PyObject *l, *x; > > Py_Initialize(); > > l = PyList_New(3); > x = PyInt_FromLong(1L); > > if (l == NULL || x == NULL) { > PyErr_Print(); > exit (EXIT_FAILURE); > } > PySequence_SetItem(l, 0, x); > Py_DECREF(x); > > Py_Finalize(); > return EXIT_SUCCESS; > } Also note that the problem goes away if I replace the call to PySequence_SetItem with: PySequence_SetItem(l, 0, PyInt_FromLong(1L)); but the problem persists if I add a Py_INCREF(x) after the assignment of x. (Which seems like a completely silly thing to do, but I'm grasping at strings...) -- http://mail.python.org/mailman/listinfo/python-list
PySequence_SetItem
The following code is pretty much straight out of section 1.2.1.1 of the Python/C reference manual: #include int main(void) { PyObject *l, *x; Py_Initialize(); l = PyList_New(3); x = PyInt_FromLong(1L); if (l == NULL || x == NULL) { PyErr_Print(); exit (EXIT_FAILURE); } PySequence_SetItem(l, 0, x); Py_DECREF(x); Py_Finalize(); return EXIT_SUCCESS; } Unforunately, it doesn't work. It segfaults, and the error occurs in list_ass_item() when the Py_DECREF macro is applied to old_value, which was set at line 699 of Objects/listobject.c with old_value = a->ob_item[i]; (old_value is being set to NULL, and Py_DECREF is being applied to NULL...boom!) I'm totally new to embedding/extending python, so I'm not sure if I'm doing something incredibly stupid, but it certainly looks like PySequence_SetItem() was expecting that there should already be an item at the desired index. Am I doing something stupid? -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: Using a dictionary to pass data to/from embedded python functions
Alex Martelli wrote: > > I wrote the following file za.c: > and proceeded to compile and execute as follows: [[Note: it does not > matter that I'm using 2.5, the code is just as fine with previous > versions -- it just happens that 2.5 is what I'm using right now in > order to help out with 2.5's beta testing]]: > > brain:~/pyex alex$ gcc -c za.c -I/usr/local/include/python2.5 > brain:~/pyex alex$ gcc -o za za.o -L/usr/local/lib/python2.5/config/ > -lpython2.5 > brain:~/pyex alex$ ./za I needed to include a lot more flags to make the example compile. In particular: -L /usr/local/lib/python2.4/config/ -lpython2.4 -lrt -lm -ldl -lutil Why did you not need them? Is this a misconfiguration on my box, or something entirely different? I've seen this type of thing come up a lot, and I can't tell if it is simply the author omitting flags for the sake of brevity, or if I'm actually missing something. -- Bill Pursell -- http://mail.python.org/mailman/listinfo/python-list
Re: Nested function scope problem
Gerhard Fiedler wrote: >There's no Python equivalent to "int*p=345; *p++;". Sure there is: os.kill(os.getpid(), signal.SIGSEGV) :) -- http://mail.python.org/mailman/listinfo/python-list
Re: reading specific lines of a file
Yi Xing wrote: > I want to read specific lines of a huge txt file (I know the line #). > Each line might have different sizes. Is there a convenient and fast > way of doing this in Python? Thanks. #!/usr/bin/env python import os,sys line = int(sys.argv[1]) path = sys.argv[2] os.system("sed -n %dp %s"%(line,path)) Some might argue that this is not really doing it in Python. In fact, I would argue that! But if you're at a command prompt and you want to see line 7358, it's much easier to type % sed -n 7358p than it is to write the python one-liner. -- http://mail.python.org/mailman/listinfo/python-list
Re: attaching debugger to runinng python program
alf wrote: > Hi, > > I have a two fold question: > -how to attach the debugger to running multi threaded program > -the objective is to find an infinite loop in one of threads which > makes the whole thingy going craze (100%CPU) > > The program itself is not easy, in fact quite hude and sometimes it > takes hours to get to that infloop state. On linux: % python >>> import os >>> os.getpid() 54321 Now, in another shell, % gdb (gdb) attach 54321 If you want debugging symbols, recompile python with -g. -- http://mail.python.org/mailman/listinfo/python-list
Re: Guide to using python for bash-style scripting
4zumanga wrote: > Yes, there is a stupid mistake in that script, last line should be: > > diff new_out1 new_out2 > > However, this is hopefully not important, what is important is the > general kind of (very simple) things I'm trying to do. I have been hoping for a good solution to this. An easy way to handle simple commands is: #!/usr/bin/env python import os s = "echo foo | sed 's/foo/gap/' > file\n" s += "wc -c file\n" s += "cat file\n" print s, "***" os.system(s) end (Apologies for the lameness of the commands above). However, I have some bash scripts that rely on things like PIPESTATUS, and I have no idea how to emulate that behavior easily. How can one most easily emulate a simple pipe as readily as in bash? I've seen a few recipes for doing something like that, but I haven't yet seen one that i really like. Is it possible to execute: os.system( " a | b | c | d | e") and retrieve the value of PIPESTATUS? -- http://mail.python.org/mailman/listinfo/python-list
Re: number of different lines in a file
r.e.s. wrote: > I have a million-line text file with 100 characters per line, > and simply need to determine how many of the lines are distinct. > > On my PC, this little program just goes to never-never land: > > def number_distinct(fn): > f = file(fn) > x = f.readline().strip() > L = [] > while x<>'': > if x not in L: > L = L + [x] > x = f.readline().strip() > return len(L) > > Would anyone care to point out improvements? > Is there a better algorithm for doing this? Have you tried cat file | sort | uniq | wc -l ? sort might choke on the large file, and this isn't python, but it might work. You might try breaking the file into smaller peices, maybe based on the first character, and then process them seperately. The time killer is probably the "x not in L" line, since L is getting very large. By subdividing the problem initially, that time constraint will be better. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tabs versus Spaces in Source Code
Peter Decker wrote: > On 17 May 2006 06:51:19 -0700, Bill Pursell <[EMAIL PROTECTED]> wrote: > > > In my experience, the people who complain about the use > > of tabs for indentation are the people who don't know > > how to use their editor, and those people tend to use > > emacs. > > In my experience, whenever there is a 'religious' issue like this, one > side tends to be quick to pronounce the other as 'evil', and insist > that everyone do things their way, while the other tends to feel > comfortable with people having their own preferences. If I ever find > myself on the side of the former, I always wonder if I'm missing > something obvious. I think you unfairly snipped context on me. I was directly responding to the assertion that vi is unable to handle tabs well. As far as tabs vs. spaces, I don't think the spacers are evil, and in fact I am one for pragmatic reasons. I would much prefer to use tabs for indentation, especially since in the last few weeks I've decided to upgrade from 4 to 8, and now all of my spaced code requires much more effort than ":ts=8" to fix. But in today's world, it's easier to go with the flow and use spaces. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tabs versus Spaces in Source Code
Xah Lee wrote: > Tabs versus Spaces in Source Code > > Xah Lee, 2006-05-13 > > In coding a computer program, there's often the choices of tabs or > spaces for code indentation. > (2) Due to the first reason, they have created and > propagated a massive none-understanding and mis-use, to the degree that > many tools (e.g. vi) does not deal with tabs well :set ts= Yeah, that's really tough. vi does just fine handling tabs. vim does an even better job, with mode-lines, = and :retab. In my experience, the people who complain about the use of tabs for indentation are the people who don't know how to use their editor, and those people tend to use emacs. -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming Tutorial for absolute beginners
Clodoaldo Pinto wrote: > I'm starting a programming tutorial for absolute beginners using Python > and I would like your opinions. > > http://programming-crash-course.com Very nicely laid out. Overall, a really nice presentation. 2 minor points: 1) in the section on the interactive interpreter you have the sentence: "In Linux open a shell and type python (must be lower case)". It would be nice if the word 'python' were in a different font, or perhaps in quotes, or something. You're targetting the "absolute beginner", so you should assume the reader is not familiar with the CLI. 2) In the section on installing, you begin with: "Python is an interpreted, interactive, object-oriented programming language.". The complete novice sees those words and expects them to be explained, but there is no definition given. I would recommend simplifying that sentence, or explaining the terms. -- http://mail.python.org/mailman/listinfo/python-list
Re: Assignment in a while?
none wrote: > import pgdb; > > dbh = pgdb.connect(database = 'test') > sth = dbh.cursor() > sth.execute("SELECT * FROM capitals") > #while 1: > #results = sth.fetchone() > #if results == None: > #break > #print results > while results = sth.fetchone(): > print results > > If I try to run the above code, I get a SyntaxError indicating that I > can't do an assignment in the while loop. I found a way around this > (see the commented out while loop), but it seems hackish. Assignment > within a while loop seems like a pretty standard thing, so I'm just > curious what I'm missing. A more pythonic way to do that is something like: for results in sth.fetchall(): print results (I'm not familiar with pgdb, but if it's a reasonable module it will have some function that returns an iterator.) In the beginning of my python experience, I was a bit irritated at being unable to assign and check a condition in one statement, but the irritation really doesn't last very long. Python has a huge amount of inherent beauty, and is well worth the time. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to learn python if I'm very familar with C++
[EMAIL PROTECTED] wrote: > Hi, > > I've been using C++ for a few years and have developed a few projects > in C++. And I'm familar with OO and template metaprogramming. > > There are some book like "Learning Perl". It is a little bit tedious > for me, because more material in that book seems obvious for me. I want > some book describe the difference between C++ and python such that I > could grasp python quickly. Would you please give me some infomation on > this? > Take a few hours to read through the tutorial at http://docs.python.org/tut/tut.html Then spend some time browsing the rest of the documentation. After you have a basic understanding of the syntax, browse the Python Cookbook. -- http://mail.python.org/mailman/listinfo/python-list