Re: (n00b) Tkinter trouble
On Mon, Nov 14, 2011 at 6:11 PM, Jason Swails wrote: > Then, I can reactivate all of the buttons in the destroy() method before > calling the destroy() method of Toplevel on self. Small side point that might save you some work: Instead of disabling and enabling all the buttons, disable the whole window. I don't know Tkinter well enough to know if there's an easy way to create a modal window, but the thing to do is to disable the entire window rather than just its command buttons - that's the least astonishing[1] user interface technique. ChrisA [1] http://en.wikipedia.org/wiki/Principle_of_least_astonishment -- http://mail.python.org/mailman/listinfo/python-list
Multilevel dicts/arrays v. tuples as keys? [Was: Re: Get keys from a dicionary]
Dne 11.11.2011 14:31, macm napsal(a): def Dicty( dict[k1][k2] ): When looking at this I returned to the question which currently rolls in my mind: What's difference/advantage-disadvantage betweeng doing multi-level dicts/arrays like this and using tuple as a key? I.e., is it more Pythonic to have dict[k1,k2] instead? Best, Matěj -- http://mail.python.org/mailman/listinfo/python-list
Re: Multilevel dicts/arrays v. tuples as keys? [Was: Re: Get keys from a dicionary]
On 14/11/2011 10:05, Matej Cepl wrote: Dne 11.11.2011 14:31, macm napsal(a): def Dicty( dict[k1][k2] ): When looking at this I returned to the question which currently rolls in my mind: What's difference/advantage-disadvantage betweeng doing multi-level dicts/arrays like this and using tuple as a key? I.e., is it more Pythonic to have dict[k1,k2] instead? For me, it would depend on what the data meant. To give an obvious example: if you were storing things which were based on coords, then clearly map[x, y] is more meaningful than map[x][y]. Conversely, if your dictionary structure was, say, a set of preferences for users, then prefs[username][prefname] is probably a more useful model. Sometimes it's not so clear, in which case one person might opt for one approach while another opted for another while modelling the same data concepts. If, for example, you were modelling a set of book reviews where each review might have one or more genres (which you could display as a tag-cloud, say) then you could consider the model to be a sort of book-genre tag cloud: book_genres[title, genre] or a list of books in each genre: genres[genre][title] or a list of genres for each book: books[title][genre] or even multiple ways if that made it easier to use in one situation or another. Stating-the-obvious-ly yours, TJG -- http://mail.python.org/mailman/listinfo/python-list
my new project, is this the right way?
Hi all, i'm developing a new program. Mission: learn a bit of database management Idea: create a simple, 1 window program that show me a db of movies i've seen with few (<10) fields (actors, name, year etc) technologies i'll use: python + gtk db: that's the question since i'm mostly a new-bye for as regard databases, my idea is to use sqlite at the beginning. Is that ok? any other db to start with? (pls don't say mysql or similar, they are too complex and i'll use this in a second step) is there any general tutorial of how to start developing a database? i mean a general guide to databases you can suggest to me? Thank you all MedeoTL P.s. since i have a ods sheet files (libreoffice calc), is there a way to easily convert it in a sqlite db? (maybe via csv) -- http://mail.python.org/mailman/listinfo/python-list
Re: Multilevel dicts/arrays v. tuples as keys?
Matej Cepl wrote: > Dne 11.11.2011 14:31, macm napsal(a): >> def Dicty( dict[k1][k2] ): > > When looking at this I returned to the question which currently rolls in > my mind: > > What's difference/advantage-disadvantage betweeng doing multi-level > dicts/arrays like this and using tuple as a key? I.e., is it more > Pythonic to have > > dict[k1,k2] > > instead? If you need lookup only I'd prefer tuples, but sometimes you may want to retrieve all values with a certain k1 and d[k1] is certainly more efficient than [(k2, v) for (k1, k2), v in d.items() if k1 == wanted] -- http://mail.python.org/mailman/listinfo/python-list
Re: my new project, is this the right way?
On Mon, Nov 14, 2011 at 9:41 PM, Tracubik wrote: > Hi all, > i'm developing a new program. > Mission: learn a bit of database management If your goal is to learn about databasing, then I strongly recommend a real database engine. > since i'm mostly a new-bye for as regard databases, my idea is to use > sqlite at the beginning. > > Is that ok? any other db to start with? (pls don't say mysql or similar, > they are too complex and i'll use this in a second step) The complexity, in most cases, is a direct consequence of the job at hand. I recommend PostgreSQL generally, although I've never used it with Python and can't speak for the quality of the APIs. The most important thing to consider is a separation of the back end (the "guts") from the front end (the "interface"). Since your goal is to explore databases, the guts of your code will basically just be working with the database (no heavy computation or anything). Make sure you can work with that, separately from your GUI. You may find it easier to dispense with GTK and just work through the console; you can always change later to make a pretty window. If you've never worked with databases before, it may be best to skip Python altogether and explore the fundamentals of relational database engines. There's plenty of excellent tutorials on the web. Get to know how things are done generally, and you'll be able to figure out how things are done in Python. All the best! ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to write beautifulsoup result to a file and get error message
On 2011-11-13 23:37, goldtech wrote: If I try: ... soup = BeautifulSoup(ft3) f = open(r'c:\NewFolder\clean4.html', "w") f.write(soup) f.close() I get error message: Traceback (most recent call last): File "C:\Documents and Settings\user01\Desktop\py\tb1a.py", line 203, in f.write(soup) TypeError: expected a character buffer object I want to write beautiful soup's result to a file, I am doing something wrong. Help appreciated. BeautifulSoup takes a html document in the form of a string or file-like oject and creates an internal data-structure after parsing it: Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from BeautifulSoup import BeautifulSoup >>> html = "Demo" >>> soup = BeautifulSoup(html) >>> type(soup) To write the modified document into a file, you have to convert this structur back into a string: >>> new_html = str(soup) >>> type(new_html) >>> new_html '\n \n Demo\n \n' HTH, Andreas -- http://mail.python.org/mailman/listinfo/python-list
new cars2011,2012,2013
Honda Insight (2012) http://newscarsblogspotcom.blogspot.com/#!/2011/11/honda-insight-2012... Audi A2 concept (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/audi-a2-concept-20... Infiniti FX designed by Sebastian Vettel (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/infiniti-fx-design... Smart Forvision concept (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/smart-forvision-co... Honda Civic (2012) http://newscarsblogspotcom.blogspot.com/#!/2011/11/honda-civic-2012.html Ford Evos concept car (2011) news and pictures http://newscarsblogspotcom.blogspot.com/#!/2011/11/ford-evos-concept-... Fiat Panda (2012) http://newscarsblogspotcom.blogspot.com/#!/2011/11/fiat-panda-2012.html Peugeot HX1 concept car (2011) first news more picsere http://newscarsblogspotcom.blogspot.com/#!/2011/11/peugeot-hx1-concep... Citroen Tubik concept car (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/citroen-tubik-conc... Skoda MissionL concept car (2011) unveiled http://newscarsblogspotcom.blogspot.com/#!/2011/11/skoda-missionl-con... Audi A5 DTM (2012) http://newscarsblogspotcom.blogspot.com/#!/2011/11/audi-a5-dtm-2012.html Lambo Super Trofeo Stradale (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/lambo-super-trofeo... Mercedes SLK55 AMG (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/mercedes-slk55-amg... Lotus Exige S (2011) http://newscarsblogspotcom.blogspot.com/#!/2011/11/lotus-exige-s-2011... Ford Fiesta ST concept (2011) at the Frankfurt motor show http://newscarsblogspotcom.blogspot.com/#!/2011/11/ford-fiesta-st-con... -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
In "W. eWatson" writes: > I just pushed aside the python25 folder by renaming it, and installed py > 2.5.2. However, when I try to open the simplest of py programs with > IDLE, I get an error from Win7. > c:\Users\blah\...\junk.py is not a valid Win 32 app. Are you double-clicking on the .py file? What application is associated with .py files? -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
On 11/14/2011 7:24 AM, John Gordon wrote: In "W. eWatson" writes: I just pushed aside the python25 folder by renaming it, and installed py 2.5.2. However, when I try to open the simplest of py programs with IDLE, I get an error from Win7. c:\Users\blah\...\junk.py is not a valid Win 32 app. Are you double-clicking on the .py file? Yes. What application is associated with .py files? Application? Simple ones, including the one i put here that you removed to answer my question. -- http://mail.python.org/mailman/listinfo/python-list
Re: my new project, is this the right way?
> since i'm mostly a new-bye for as regard databases, my idea is to use > sqlite at the beginning. > > Is that ok? any other db to start with? (pls don't say mysql or similar, > they are too complex and i'll use this in a second step) I know it's a lot of work to learn initially, but I would recommend taking the time to install and become familiar with a database engine such as MySQL or PostgresQL. While SQLite is easy to install and easy to use, it also cuts a lot of corners and allows you to do things that would die in a real database. For example, in SQLite column types are merely suggestions; there is nothing in SQLite to keep you from storing a string in an integer field or vice versa. For example: dan@dan:~/work$ sqlite3 SQLite version 3.7.3 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> create table test ( ...> id int, ...> name varchar(64), ...> comment varchar(256) ...> ); sqlite> .schema test CREATE TABLE test ( id int, name varchar(64), comment varchar(256) ); sqlite> insert into test values (1, 'Dan', 'Normal insert with valid types'); sqlite> insert into test values ('Bogosity', 2, 'Record with invalid types'); sqlite> insert into test values ('This field should be an int but is really a long string instead', 12.45, 'A really screwy record'); sqlite> select * from test; 1|Dan|Normal insert with valid types Bogosity|2|Record with invalid types This field should be an int but is really a long string instead|12.45|A really screwy record sqlite> This is not to say that SQLite is useless. Far from it - I use it frequently myself. But I've been working with SQL databases for a bunch of years, and I am familiar with the tradeoffs involved in working with SQLite. A decent tutorial on working with SQL in general can be found at http:// www.w3schools.com/sql/sql_intro.asp Installing MySQL or Postgres can be fairly simple, if you use the tools provided with your Linux distro (assuming you're running on Linux). "sudo apt-get install mysql mysql-server" or "yum install mysql mysql-server" should get you what you need to start using MySQL, "sudo apt-get install postgresql" or "yum install postgresql" should get you started with PostgresQL. -- http://mail.python.org/mailman/listinfo/python-list
Py2.7/FreeBSD: maximum number of open files
I am trying to convince Python to open more than 32k files .. this is on FreeBSD. Now I know I have to set appropriate limits .. I did: $ sysctl kern.maxfiles kern.maxfiles: 204800 $ sysctl kern.maxfilesperproc kern.maxfilesperproc: 20 $ sysctl kern.maxvnodes kern.maxvnodes: 20 $ ulimit unlimited Here is what happens with a Python freshly built from sources .. it'll tell me I can open 200k files .. but will bail out at 32k: $ ./local/bin/python -V Python 2.7.2 $ ./local/bin/python Python 2.7.2 (default, Nov 14 2011, 16:41:56) [GCC 4.2.1 20070719 [FreeBSD]] on freebsd8 Type "help", "copyright", "credits" or "license" for more information. >>> import resource >>> resource.getrlimit(resource.RLIMIT_NOFILE) (20L, 20L) >>> resource.getrlimit(resource.RLIMIT_OFILE) Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'RLIMIT_OFILE' >>> import resource >>> >>> max = resource.getrlimit(resource.RLIMIT_NOFILE) >>> cnt = 0 >>> print "maximum FDs", max maximum FDs (20L, 20L) fds = [] while cnt < max: f = open("/tmp/test1/test_%d" % cnt, "w") f.write("test") fds.append(f) cnt += 1 if cnt % 1000 == 0: print "opened %d files" % cnt print "ok, created %d files" % cnt >>> >>> fds = [] >>> >>> while cnt < max: ...f = open("/tmp/test1/test_%d" % cnt, "w") ...f.write("test") ...fds.append(f) ...cnt += 1 ...if cnt % 1000 == 0: ... print "opened %d files" % cnt ... opened 1000 files opened 2000 files opened 3000 files opened 4000 files opened 5000 files opened 6000 files opened 7000 files opened 8000 files opened 9000 files opened 1 files opened 11000 files opened 12000 files opened 13000 files opened 14000 files opened 15000 files opened 16000 files opened 17000 files opened 18000 files opened 19000 files opened 2 files opened 21000 files opened 22000 files opened 23000 files opened 24000 files opened 25000 files opened 26000 files opened 27000 files opened 28000 files opened 29000 files opened 3 files opened 31000 files opened 32000 files Traceback (most recent call last): File "", line 2, in IOError: [Errno 24] Too many open files: '/tmp/test1/test_32765' >>> print "ok, created %d files" % cnt ok, created 32765 files >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
In "W. eWatson" writes: > > What application is associated with .py files? > Application? Simple ones, including the one i put here that you > removed to answer my question. Eh? I can't see anywhere that you mentioned your Windows settings as to what application is associated with .py files. (You mentioned running IDLE, but I don't see that it was given as the default application for opening .py files.) -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
Am 14.11.2011 16:57, schrieb Tobias Oberstein: > I am trying to convince Python to open more than 32k files .. this is on > FreeBSD. > > Now I know I have to set appropriate limits .. I did: > > $ sysctl kern.maxfiles > kern.maxfiles: 204800 > $ sysctl kern.maxfilesperproc > kern.maxfilesperproc: 20 > $ sysctl kern.maxvnodes > kern.maxvnodes: 20 > $ ulimit > unlimited > > Here is what happens with a Python freshly built from sources .. it'll tell > me I can open 200k files .. but will bail out at 32k: I'm not familiar with BSD but Linux has similar Kernel options. The kernel options might be *global* flags to set the total upper limit of open file descriptors for the entire system, not for a single process. Also on Linux "ulimit" doesn't display the fd limit. You have to use "ulimit -n". Why do you need more than 32k file descriptors anyway? It's an insanely high amount of FDs. Most programs need less than 100 and the default value of 1024 on my Linux servers is usually high enough. I've never increased the fd limit over 8192 and our biggest installation servers more than 80 TB data in about 20 to 25 million files. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: my new project, is this the right way?
On Nov 14, 10:41 am, Tracubik wrote: > Hi all, > i'm developing a new program. > Mission: learn a bit of database management > Idea: create a simple, 1 window program that show me a db of movies i've > seen with few (<10) fields (actors, name, year etc) > technologies i'll use: python + gtk > db: that's the question > > since i'm mostly a new-bye for as regard databases, my idea is to use > sqlite at the beginning. > > Is that ok? any other db to start with? (pls don't say mysql or similar, > they are too complex and i'll use this in a second step) > > is there any general tutorial of how to start developing a database? i > mean a general guide to databases you can suggest to me? > Thank you all > > MedeoTL > > P.s. since i have a ods sheet files (libreoffice calc), is there a way to > easily convert it in a sqlite db? (maybe via csv) I would recommend working through the book "SQL for Dummies". I found it very clear, and slowly leads you into how to think about design, not just how to manipulate databases. Instead of using Python to start with consider using OOo Base or MS Access (retching noise), so you can use RAD to play with structure and manipulation of your data and create data-entry forms -- this'll allow you to enter data, and play with queries and the structure -- as you won't get it right the first time! You will be able to get either of these programs to give you the SQL that constructs tables, or makes queries etc... That'd be enough to keep you going for a couple of weeks I guess. Also, some things make more sense in a NoSQL database, so have a look at something like MongoDB or CouchDB and how their design works differently. That's probably another couple of weeks. Also worth checking out would be http://dabodev.com hth Jon. -- http://mail.python.org/mailman/listinfo/python-list
AW: Py2.7/FreeBSD: maximum number of open files
> I'm not familiar with BSD but Linux has similar Kernel options. The kernel > options might be *global* flags to set the total upper limit of open file > descriptors for the entire system, not for a single process. > Also on Linux "ulimit" doesn't display the fd limit. You have to use "ulimit > -n". This is a dedicated machine doing nothing else .. I'm monitoring global FD usage sysctl kern.openfiles and it's way beyond the configured limit $ ulimit -n 20 > > Why do you need more than 32k file descriptors anyway? It's an insanely high It's not for files: This is a network service .. I tested it with up to 50k TCP connections .. however at this point, when the service tries to open a file, it'll bail out. Sockets+Files both contribute to open FDs. I need 50k sockets + 100 files. Thus, this is even more strange: the Python (a Twisted service) will happily accept 50k sockets, but as soon as you do open() a file, it'll bail out. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
Am 14.11.2011 17:36, schrieb Tobias Oberstein: > This is a dedicated machine doing nothing else .. I'm monitoring global FD > usage > > sysctl kern.openfiles > > and it's way beyond the configured limit > > $ ulimit -n > 20 Apparently you did everything right here. Well, it was worth the try. ;) > It's not for files: > > This is a network service .. I tested it with up to 50k TCP connections .. > however > at this point, when the service tries to open a file, it'll bail out. > > Sockets+Files both contribute to open FDs. > > I need 50k sockets + 100 files. > > Thus, this is even more strange: the Python (a Twisted service) will happily > accept 50k sockets, but as soon as you do open() a file, it'll bail out. A limit of 32k smells like a overflow in a signed int. Perhaps your system is able and configured to handle more than 32k FDs but you hit an artificial limit because some C code or API has a overflow. This seems to be a known bug in FreeBSD http://lists.freebsd.org/pipermail/freebsd-bugs/2010-July/040689.html Christian -- http://mail.python.org/mailman/listinfo/python-list
AW: Py2.7/FreeBSD: maximum number of open files
> > I need 50k sockets + 100 files. > > > > Thus, this is even more strange: the Python (a Twisted service) will > > happily accept 50k sockets, but as soon as you do open() a file, it'll bail > > out. > > A limit of 32k smells like a overflow in a signed int. Perhaps your system is > able and configured to handle more than 32k FDs but you hit an artificial > limit > because some C code or API has a overflow. This seems to be a known bug in > FreeBSD http://lists.freebsd.org/pipermail/freebsd-bugs/2010- > July/040689.html This is unbelievable. I've just tested: the bug (in libc) is still there on FreeBSD 8.2 p3 ... both on i386 _and_ amd64. Now I'm f***d;( A last chance: is it possible to compile Python for not using libc fopen(), but the Posix open()? Thanks anyway for this hint! -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
Am 14.11.2011 18:03, schrieb Tobias Oberstein: > This is unbelievable. > > I've just tested: the bug (in libc) is still there on FreeBSD 8.2 p3 ... both > on i386 > _and_ amd64. > > Now I'm f***d;( > > A last chance: is it possible to compile Python for not using libc fopen(), > but the Posix open()? > > Thanks anyway for this hint! A fix would break the ABI compatibility. I guess that you won't see a fix for the issue until FreeBSD 9.0 is released. And no, you can't re-compile Python to use the open() API instead of fopen(). The built-in file type is developed around the file pointer API, not the file descriptor API. Luckily Python 2.7 has a backport of Python 3.x new IO library. The new IO API doesn't use file pointers at all and implements its own layer around file descriptors. Good luck! Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
On 11/14/2011 8:15 AM, John Gordon wrote: In "W. eWatson" writes: What application is associated with .py files? Application? Simple ones, including the one i put here that you removed to answer my question. Eh? I can't see anywhere that you mentioned your Windows settings as to what application is associated with .py files. (You mentioned running IDLE, but I don't see that it was given as the default application for opening .py files.) I would think the install would make the association of py to Python, either IDLE or the interpreter. If I right-click on a py file, it asks me for "Open with". If I select idle.pyw, it gives me the message I posted earlier. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
On Nov 14, 5:03 pm, Tobias Oberstein wrote: > > > I need 50k sockets + 100 files. > > > > Thus, this is even more strange: the Python (a Twisted service) will > > > happily accept 50k sockets, but as soon as you do open() a file, it'll > > > bail out. > > > A limit of 32k smells like a overflow in a signed int. Perhaps your system > > is > > able and configured to handle more than 32k FDs but you hit an artificial > > limit > > because some C code or API has a overflow. This seems to be a known bug in > > FreeBSDhttp://lists.freebsd.org/pipermail/freebsd-bugs/2010- > > July/040689.html > > This is unbelievable. > > I've just tested: the bug (in libc) is still there on FreeBSD 8.2 p3 ... both > on i386 > _and_ amd64. > > Now I'm f***d;( > > A last chance: is it possible to compile Python for not using libc fopen(), > but the Posix open()? > > Thanks anyway for this hint! Have you tried/or is it possible to get your 100 or whatever files first, before your sockets? hth Jon -- http://mail.python.org/mailman/listinfo/python-list
AW: Py2.7/FreeBSD: maximum number of open files
> > > > I need 50k sockets + 100 files. > > > > > > Thus, this is even more strange: the Python (a Twisted service) > > > > will happily accept 50k sockets, but as soon as you do open() a file, > > > > it'll > bail out. > > > > > A limit of 32k smells like a overflow in a signed int. Perhaps your > > > system is able and configured to handle more than 32k FDs but you > > > hit an artificial limit because some C code or API has a overflow. > > > This seems to be a known bug in > > > FreeBSDhttp://lists.freebsd.org/pipermail/freebsd-bugs/2010- > > > July/040689.html > > > > This is unbelievable. > > > > I've just tested: the bug (in libc) is still there on FreeBSD 8.2 p3 > > ... both on i386 _and_ amd64. > > > > Now I'm f***d;( > > > > A last chance: is it possible to compile Python for not using libc > > fopen(), but the Posix open()? > > > > Thanks anyway for this hint! > > Have you tried/or is it possible to get your 100 or whatever files first, > before > your sockets? If I only needed to open a fixed set of files, that might be a workaround. However, this is not the case. I.e. Twisted will do log switching and create/open a new file when the 50k sockets are already there. I just confirmed that the bug is even there for FreeBSD 9 RC1 ! This is most unfortunate. Seriously. I am running out of options, since I am willing to make my stuff Python 3 compatible, but Twisted is not yet there. Using the backported new IO on Python 2.7 will not make open() automatically use the new IO, will it? -- http://mail.python.org/mailman/listinfo/python-list
Re: my new project, is this the right way?
> since i'm mostly a new-bye for as regard databases, my idea is to use > sqlite at the beginning. > > Is that ok? I think sqlite3 makes sense since it's already there and has SQL interface. > is there any general tutorial of how to start developing a database? i > mean a general guide to databases you can suggest to me? The docs (http://docs.python.org/library/sqlite3.html) have some basic instructions. > P.s. since i have a ods sheet files (libreoffice calc), is there a way to > easily convert it in a sqlite db? (maybe via csv) This can be your first exercise :) But it should be easy to import with the csv module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
Am 14.11.2011 18:46, schrieb Tobias Oberstein: > I just confirmed that the bug is even there for FreeBSD 9 RC1 ! > > This is most unfortunate. Seriously. W00t, that sucks! You could migrate to another BSD (NetBSD) or Linux ... :) > I am running out of options, since I am willing to make my stuff Python 3 > compatible, > but Twisted is not yet there. > > Using the backported new IO on Python 2.7 will not make open() automatically > use the new IO, will it? No, the open() function of Python 2.7 will still use the file class which in return uses fopen(). You could try to monkey patch the built-in open() function. It's mostly API compatible with the current open() function: >>> import io, __builtin__ >>> __builtin__.open = io.open It works as long as no codes checks for isinstance(obj, file). If your app only has to worry about log files, you might want to overwrite the _open() method of logging.FileHandler and its subclasses. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
In "W. eWatson" writes: > I would think the install would make the association of py to Python, > either IDLE or the interpreter. I would hope so too, however you did mention that you moved the python executable to a different directory and installed a newer version, so verifying that the .py file association points to the correct application might be worthwhile. -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, "The Gashlycrumb Tinies" -- http://mail.python.org/mailman/listinfo/python-list
Re: Uninstalling Py 2.5.2 from Windows 7 (Invalid on Win 32 app)
On 11/14/2011 10:00 AM, John Gordon wrote: In "W. eWatson" writes: I would think the install would make the association of py to Python, either IDLE or the interpreter. I would hope so too, however you did mention that you moved the python executable to a different directory and installed a newer version, so verifying that the .py file association points to the correct application might be worthwhile. Note though I had uninstalled Python. The folder remained, so I renamed it, then installed. I see no way that under that arrangement that py would be associated with the renamed contents. The renamed folder has no exe file for the interpreter. There is, of course, a new Python25 that shows python.exe and pythonw.exe. python.exe will bring up a command window. -- http://mail.python.org/mailman/listinfo/python-list
AW: Py2.7/FreeBSD: maximum number of open files
> > I just confirmed that the bug is even there for FreeBSD 9 RC1 ! > > > > This is most unfortunate. Seriously. > > W00t, that sucks! You could migrate to another BSD (NetBSD) or Linux ... :) No, thanks;) > > I am running out of options, since I am willing to make my stuff > > Python 3 compatible, but Twisted is not yet there. > > > > Using the backported new IO on Python 2.7 will not make open() > automatically use the new IO, will it? > > No, the open() function of Python 2.7 will still use the file class which in > return uses fopen(). You could try to monkey patch the built-in > open() function. It's mostly API compatible with the current open() > function: > > >>> import io, __builtin__ > >>> __builtin__.open = io.open > > It works as long as no codes checks for isinstance(obj, file). If your app > only > has to worry about log files, you might want to overwrite the > _open() method of logging.FileHandler and its subclasses. > Thanks! This is probably the most practical option I can go. I've just tested: the backported new IO on Python 2.7 will indeed open >32k files on FreeBSD. It also creates the files much faster. The old, non-monkey-patched version was getting slower and slower as more files were opened/created .. There seem to be slight differences though: Non-monkey patched: I can write to the file a non-Unicode string, even when the file was opened non-Binary. With monkey patch: either open the file Binary-mode, or write Unicode strings .. I need to see if / what breaks in Twisted. I can handle my own code .. no problem. Thanks alot!! import io, __builtin__ __builtin__.open = io.open import resource max = resource.getrlimit(resource.RLIMIT_NOFILE) cnt = 0 print "maximum FDs", max max = 33000 fds = [] while cnt < max: f = open("/tmp/test1/test_%d" % cnt, "wb") f.write("test") fds.append(f) cnt += 1 if cnt % 1000 == 0: print "opened %d files" % cnt print "ok, created %d files" % cnt -- http://mail.python.org/mailman/listinfo/python-list
Re: can't decompress data; zlib not available
Sorry about that. Ubuntu 11.10. I used Plone-4.1.2-UnifiedInstaller.tar which installed o.k. I'm serving a webpage on my LAN. I did a search on files named "python" on my machine. There are 23 not including the ones in the Plone buildout-cache in my account. Seems like a lot of applications install their own copy of python. There are also ./usr/lib/x86_64-linux-gnu/libz.so ./usr/lib/libz.so which, I believe, are the zlib libraries. I've read that you can reinstall python with configure using the "--with-zlib" option, but configure isn't in /usr/local/Plone/Python-2.6/lib/python2.6/config I think the python interpreter for the command line is the one in /usr/bin/python. Would this be the one I reconfigure for zlib? Should I simply install python from Python-3.2.2.tgz? On 11/13/2011 02:55 PM, Tim Wintle wrote: On Sun, 2011-11-13 at 11:17 -0700, Steve Edlefsen wrote: which appears to install zlib when python is reinstalled. Except I can't run make without errors and there is no configuration file. How do I reinstall python to include zlib? Which OS are you on? Linux? BSD? How did you install Plone? First I'd check if there's a module shadowing the builtin zlib module - i.e. if you've got a local file called "zlib.py" which is getting imported by mistake. Fairly much all *nix systems will have a python installation out of the box - it looks like you need python2.6 I've never had a missing zlib module - but it's possible that it might be missing if you don't have the zlib/deflate headers installed - if they're not available then I'd try installing them and then reinstalling the package you started with. Tim Wintle -- http://mail.python.org/mailman/listinfo/python-list
Re: Py2.7/FreeBSD: maximum number of open files
Am 14.11.2011 19:28, schrieb Tobias Oberstein: > Thanks! This is probably the most practical option I can go. > > I've just tested: the backported new IO on Python 2.7 will indeed > open >32k files on FreeBSD. It also creates the files much faster. > The old, non-monkey-patched version was getting slower and > slower as more files were opened/created .. I wonder what's causing the O(n^2) behavior. Is it the old file type or BSD's fopen() fault? > There seem to be slight differences though: > > Non-monkey patched: I can write to the file a non-Unicode string, > even when the file was opened non-Binary. > > With monkey patch: either open the file Binary-mode, or > write Unicode strings .. Python 3.x doesn't collate data and text but distinguishes between bytes (str type in Python 2.x) and unicode. You can't write unicode to a binary file and str to a text file. See it as an opportunity! You are taking the first step towards Python 3 compatibility. :) Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: can't decompress data; zlib not available
Am 14.11.2011 20:30, schrieb Steve Edlefsen: > Sorry about that. Ubuntu 11.10. > > I used > > Plone-4.1.2-UnifiedInstaller.tar > > which installed o.k. I'm serving a webpage on my LAN. > > I did a search on files named "python" on my machine. > There are 23 not including the ones in the Plone > buildout-cache in my account. Seems like a lot of > applications install their own copy of python. > > There are also > > ../usr/lib/x86_64-linux-gnu/libz.so > ../usr/lib/libz.so > > which, I believe, are the zlib libraries. > > I've read that you can reinstall python with configure > using the "--with-zlib" option, but configure isn't in > > /usr/local/Plone/Python-2.6/lib/python2.6/config > > I think the python interpreter for the command line is > the one in /usr/bin/python. Would this be the one I > reconfigure for zlib? You might have run into this [1] issue with Ubuntu 11.04+ and Python before 2.7.2. Christian [1] http://lipyrary.blogspot.com/2011/05/how-to-compile-python-on-ubuntu-1104.html -- http://mail.python.org/mailman/listinfo/python-list
Re: can't decompress data; zlib not available
On Mon, 2011-11-14 at 12:30 -0700, Steve Edlefsen wrote: > I did a search on files named "python" on my machine. > There are 23 not including the ones in the Plone > buildout-cache in my account. Seems like a lot of > applications install their own copy of python. > > There are also > > ./usr/lib/x86_64-linux-gnu/libz.so > ./usr/lib/libz.so > > which, I believe, are the zlib libraries. but do you have the headers? On my ubuntu it's /usr/include/zlib.h As Christian pointed out, Ubuntu 11.04 introduced some changes which may have broken the installation - there is a patch on the python bug tracker which will provide a work-around, but it is not going to be applied to 2.6 or lower as they are not actively maintained branches any more. You'll need to run through the steps that the plone installer makes and patch the extracted python directory before it makes it's local python. > I've read that you can reinstall python with configure > using the "--with-zlib" option, but configure isn't in > > /usr/local/Plone/Python-2.6/lib/python2.6/config > > I think the python interpreter for the command line is > the one in /usr/bin/python. Would this be the one I > reconfigure for zlib? Should I simply install python from > > Python-3.2.2.tgz? no - you need to use python2.6 - python3 is effectively a different language. python is a core part of *nix these days, so playing around recompiling the system python can cause a lot of pain. Tim -- http://mail.python.org/mailman/listinfo/python-list
Python Developer needed for Catalina Marketing - St. Petersburg
We, at Catalina Marketing, are in need of a Python Developer for 3 - 6 months. You'd be writing highly sophisticated scripting in OO classes (cron) for our coupon campaigns. Also using SQLite, Oracle, and Netezza. Prefer someone who has at least 3-5 years of Python and database experience. The other skills are secondary. You would be a direct Catalina temporary employee, or we can do a C2C arrangement. Also if you know of someone who might be interested, I would muchly appreciate the referral. Please contact Chris Martel if interested: chris.mar...@catalinamarketing.com, 727-579-5434. Catalina Marketing Corp. provides behavior-based marketing solutions for brand manufacturers, retailers, and healthcare providers. It offers campaign management services; Catalina Category Marketing, a behavior-based merchandising event; Catalina Interactive, which allows the user to communicate with consumers online; Catalina Product Alert, which tracks down actual recalled product buyers; Checkout Coupon, which targets consumers at the transaction level; Checkout Direct, which allows brands and retailers to target households based on past purchase behavior; Checkout Prizes, which builds product volume and generates in-store/brand excitement by pre-announcing sweepstakes events; and Checkout Rx, a household and transactional database. The company also provides competitive benchmarks; Connection Builder, a Web-based business intelligence application designed to support analytical needs; data mining services; Loyalty Quotient, a strategic application; new product launch programs; PatientLink, which delivers healthcare messages; PatientLinkMC, which delivers PatientLink messages in the patient's language of choice; Pay for Performance, which allows brand manufacturers to influence consumers before they reach the point of sale; PharmAware, which delivers educational messages about pharmaceutical products for pharmacists and their staff; Rebate Max, a program that simplifies customer participation in various manufacturer-funded rebate offers; and RxConx, an integrated wellness marketing solution. In addition, it offers retail consulting services. The company serves various brands, retailers, and healthcare markets. Catalina Marketing Corp. was founded in 1983 and is based in St. Petersburg, Florida with locations in the United States, the United Kingdom, the Netherlands, Japan, Italy, Germany, France, and Belgium. -- http://mail.python.org/mailman/listinfo/python-list
else in try/except
The code in 'else' in a 'try/except/else[/finally]' block seems pointless to me, as I am not seeing any difference between having the code in the 'else' suite vs having the code in the 'try' suite. Can anybody shed some light on this for me? ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: else in try/except
On 14/11/2011 21:53, Ethan Furman wrote: The code in 'else' in a 'try/except/else[/finally]' block seems pointless to me, as I am not seeing any difference between having the code in the 'else' suite vs having the code in the 'try' suite. Can anybody shed some light on this for me? The difference is that if an exception occurs in the else block it won't be caught by the exception handlers of the try statement. -- http://mail.python.org/mailman/listinfo/python-list
Re: else in try/except
On 14 November 2011 21:53, Ethan Furman wrote: > The code in 'else' in a 'try/except/else[/finally]' block seems pointless to > me, as I am not seeing any difference between having the code in the 'else' > suite vs having the code in the 'try' suite. > > Can anybody shed some light on this for me? Exceptions in the else clause will not be caught. Look at the difference between the two try clauses below: >>> def f(): raise TypeError ... >>> try: ...print("try") ...f() ... except TypeError: ...print("except") ... try except >>> try: ...print("try") ... except TypeError: ...print("except") ... else: ...f() ... try Traceback (most recent call last): File "", line 6, in File "", line 1, in f TypeError >>> HTH -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: else in try/except
On Mon, Nov 14, 2011 at 2:59 PM, MRAB wrote: > On 14/11/2011 21:53, Ethan Furman wrote: >> >> The code in 'else' in a 'try/except/else[/finally]' block seems >> pointless to me, as I am not seeing any difference between having the >> code in the 'else' suite vs having the code in the 'try' suite. >> >> Can anybody shed some light on this for me? >> > The difference is that if an exception occurs in the else block it > won't be caught by the exception handlers of the try statement. Consider the examples: try: a raise RuntimeError() except RuntimeError: pass vs try: a except RuntimeError: pass else: raise RuntimeError() The first example will not raise an exception, while the second will result in a RuntimeError. Effectively, the first block will either: 1) If the "a" block raises a RuntimeError, continue, 2) If the "a" block raised any other error, propergate it, 3) If the "a" block does not raise an exception, continue. while the second block will either: 1) If the "a" block raises a RuntimeError, continue, 2) If the "a" block raised any other error, propergate it, 3) If the "a" block does not raise an exception, raise a RuntimeError. Note the difference in the 3rd case. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Opportunity missed by Python ?
What I don't get is, having seen Python's syntax with indentation instead of open and closing puncuation and other -readability- structures in Python's syntax, is if someone is going to invent any new language, how could they NOT take Python's visual structures (read as readability) and copy it, whether it be a compiled language, explicidly typed checked or whatever underlying mechanism they want to make that code executable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Opportunity missed by Python ?
On Tue, Nov 15, 2011 at 10:59 AM, DevPlayer wrote: > What I don't get is, having seen Python's syntax with indentation > instead of open and closing puncuation and other -readability- > structures in Python's syntax, is if someone is going to invent any > new language, how could they NOT take Python's visual structures (read > as readability) and copy it, whether it be a compiled language, > explicidly typed checked or whatever underlying mechanism they want to > make that code executable. What I would say is: How could they NOT be aware of Python's visual structures. There are tradeoffs, and just because something works for one language doesn't mean it's right for every other. I doubt the Dart developers were unaware of Python's structural style, so the choice to not use such was most likely conscious. (It may have been as simple as "let's keep the syntax mostly JS-like, to make it easier for JS developers to grok" though, rather than a major language-design choice.) ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: else in try/except
Thanks, all! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator question: prefer class, but only function works
In article , Ian Kelly wrote: > On Thu, Nov 10, 2011 at 2:52 PM, Russell E. Owen wrote: > > I am trying to write a decorator that times an instance method and > > writes the results to a class member variable. For example: > > > > def timeMethod(func): > > def wrapper(self, *args, **keyArgs): > > t1 = time.time() > > res = func(self, *args, **keyArgs) > > duration = time.time() - t1 > > self.timings[func.__name__] = duration > > return res > > return wrapper > > > > This works, but I'm not very happy with the way self.timings is obtained. > > What do you feel is wrong with it? Oops, I stripped so much out of my example that I stripped the ugly bit. This is closer to the original and demonstrated the issue: def timeMethod(func): name = func.__name__ + "Duration" def wrapper(self, *args, **keyArgs): t1 = time.time() res = func(self, *args, **keyArgs) duration = time.time() - t1 self.timings[name] = duration return res return wrapper I don't like the way name is passed into wrapper. It works, but it looks like magic. A class offers an obvious place to save the information. Or I could just generate the name each time. I realize I'm showing the limits of my understanding of python binding of variable names, but I also think that if I find it confusing then others will, as well. > sum(os.times()[:2]) instead, which (assuming your script is > single-threaded) will more accurately count the actual CPU time spent > in the function rather than real time, which could be quite different > if the CPU is busy. Thanks for the suggestion. I decided to use time.clock(), which I understand gives basically the same information (at a resolution that is sufficient for my needs). > Also, why do you need this? If you're just trying to evaluate the > speed of your code, you should consider using a proper profiler or the > timeit module. The former will tell you how much time is spent in > each function, while the latter runs the code a large number of times > in a loop, which gives you better precision for quick methods. It is for timing stages of a data processing pipeline. Only long-running tasks will be timed. Repeatedly running to get better times is neither practical nor necessary to get a good feeling of where the time is being spent. > > I first tried to write this as a class (for readability), and this did > > NOT work: > > > > class timeMethod(object): > > def __init__(self, func): > > self.func = func > > def __call__(self, *args, **keyArgs): > > t1 = time.time() > > res = self.func(*args, **keyArgs) > > duration = time.time() - t1 > > args[0].timings.set(self.func.__name__, duration) > > return res > > > > In the first case the wrapper is called as an unbound function, so it > > works. But in the second case the wrapper is called as a bound method -- > > thus args[0] is not func's class instance, and I can't get to the > > timings attribute. > > I prefer the function version myself, but to make this work you could > add something like this (untested) to make your wrapper class a > descriptor that tacks on the self argument: > > def __get__(self, instance, owner): > from functools import partial > if instance is None: > return self > return partial(self, instance) Thank you very much. I'll stick to the function, since it works, but it's nice to know how to work around the problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Opportunity missed by Python ?
I believe Occam had a visual structure and was compiled. In fact it was even more picky than Python in this respect IIRC. On 11/14/2011 4:28 PM, Chris Angelico wrote: On Tue, Nov 15, 2011 at 10:59 AM, DevPlayer wrote: What I don't get is, having seen Python's syntax with indentation instead of open and closing puncuation and other -readability- structures in Python's syntax, is if someone is going to invent any new language, how could they NOT take Python's visual structures (read as readability) and copy it, whether it be a compiled language, explicidly typed checked or whatever underlying mechanism they want to make that code executable. What I would say is: How could they NOT be aware of Python's visual structures. There are tradeoffs, and just because something works for one language doesn't mean it's right for every other. I doubt the Dart developers were unaware of Python's structural style, so the choice to not use such was most likely conscious. (It may have been as simple as "let's keep the syntax mostly JS-like, to make it easier for JS developers to grok" though, rather than a major language-design choice.) ChrisA -- Dominic Binks: dbi...@codeaurora.org Employee of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum -- http://mail.python.org/mailman/listinfo/python-list
Re: Opportunity missed by Python ?
On Mon, 14 Nov 2011 15:59:39 -0800, DevPlayer wrote: > What I don't get is, having seen Python's syntax with indentation > instead of open and closing puncuation and other -readability- > structures in Python's syntax, is if someone is going to invent any new > language, how could they NOT take Python's visual structures (read as > readability) and copy it, whether it be a compiled language, explicidly > typed checked or whatever underlying mechanism they want to make that > code executable. Because sometimes people have priorities other than readability. Or they actually *like* braces. (I know, crazy!) In fairness, the significant indentation is not all peaches and cream, it has a dark side too. For example, the difference between visually indistinguishable spaces and tabs can be meaningful (in Python 2, your code may be broken if you mix spaces and tabs; in Python 3, the compiler will give you an error). If you work in a noisy environment that eats whitespace, such as crappy web forums, people using HTML email, editors with unfriendly word-wrapping rules, and the like, you might miss having braces. I occasionally see emails with code looking something like this: print(something) while x < 100: print(something_else) x -= 1 if x % 2 == 1: some_function() else: another_function() third_function() print(x) So the "off-side rule", as it is called, is not uncontroversial. Languages developed by old-school language developers or academics, like Google's Go language, tend to be very conservative and hence require braces or even semicolons. http://en.wikipedia.org/wiki/Off-side_rule So many people keep asking for braces in Python that one of the developers (Guido?) gave in and added it to the language. Just run from __future__ import braces at the top of your file, or at the interactive interpreter, and you're done. My own opinion is that the time I save with significant indentation is a hundred times greater than the time I lose due to mangled code without braces. So I consider Python's design a big win, but other people may make other value judgments. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Decorator question: prefer class, but only function works
On Mon, 14 Nov 2011 17:00:38 -0800, Russell E. Owen wrote: > Oops, I stripped so much out of my example that I stripped the ugly bit. > This is closer to the original and demonstrated the issue: > > def timeMethod(func): > name = func.__name__ + "Duration" > def wrapper(self, *args, **keyArgs): > t1 = time.time() > res = func(self, *args, **keyArgs) > duration = time.time() - t1 > self.timings[name] = duration > return res > return wrapper > > I don't like the way name is passed into wrapper. It works, but it looks > like magic. A class offers an obvious place to save the information. Or > I could just generate the name each time. The pattern you are seeing is called a closure, and it is a very important, if advanced, part of Python programming. Of course, you don't *have* to use closures, but it isn't scarily-advanced (like metaclass programming) but only moderately advanced, and there will be a lot of code out there using closures. http://en.wikipedia.org/wiki/Closure_(computer_science) Basically a closure is a function which remembers just enough of its current environment (the value of non-local variables) so that it can continue to work in the future, even if those non-locals change or disappear. I take it you are aware that timing code as shown is only suitable for long-running code, and not small snippets? For small snippets, you should use the timeit module. You might also find this recipe useful: http://code.activestate.com/recipes/577896/ -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Multilevel dicts/arrays v. tuples as keys?
Peter Otten <__pete...@web.de> wrote: > If you need lookup only I'd prefer tuples, but sometimes you may want to > retrieve all values with a certain k1 and > > d[k1] > > is certainly more efficient than > > [(k2, v) for (k1, k2), v in d.items() if k1 == wanted] This was the hidden cost of the tuple/reverse-dictionary solution I offered. The solution will of course depend on what the OP requires to be more efficient: looking up keys from values, or working with subsets of the data. -- http://mail.python.org/mailman/listinfo/python-list
Re: else in try/except
I thought that the point of the else clause is that it is reached only if there is no exception in the try clause. -- http://mail.python.org/mailman/listinfo/python-list
Re: (n00b) Tkinter trouble
On Mon, Nov 14, 2011 at 3:49 AM, Chris Angelico wrote: > On Mon, Nov 14, 2011 at 6:11 PM, Jason Swails > wrote: > > Then, I can reactivate all of the buttons in the destroy() method before > > calling the destroy() method of Toplevel on self. > > Small side point that might save you some work: Instead of disabling > and enabling all the buttons, disable the whole window. I don't know > Tkinter well enough to know if there's an easy way to create a modal > window, but the thing to do is to disable the entire window rather > than just its command buttons - that's the least astonishing[1] user > interface technique. > Of course! Windows are widgets just like everything else is, and so can be configured to be in the DISABLED state just like a button can. I'm not used to this hierarchy in which the root window presides over all, yet is still a widget just like everything else. But there's a lot of GUI programming that I haven't wrapped my head around yet (which is why I'm running with this pet project -- but it's bound to be ugly :)). Thanks! Jason -- http://mail.python.org/mailman/listinfo/python-list
Re: (n00b) Tkinter trouble
On Tue, Nov 15, 2011 at 4:18 PM, Jason Swails wrote: > Of course! Windows are widgets just like everything else is, and so can be > configured to be in the DISABLED state just like a button can. I'm not used > to this hierarchy in which the root window presides over all, yet is still a > widget just like everything else. > > But there's a lot of GUI programming that I haven't wrapped my head around > yet (which is why I'm running with this pet project -- but it's bound to be > ugly :)). Heh, I grew up on OS/2 and everything was an object. (With a few exceptions; the minimize/maximize/close buttons are all a single object, rather than being three.) It's not normal to disable the title bar while leaving the window enabled, but if you want to, you can! As a general rule, if any parent is invisible, you won't see the child, and if any parent is disabled, you can't access the child. You may find that there's even a one-line command that will disable the window, open a new window, wait for the new window to close, and automatically reenable the window - an "open modal dialog" function or something. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: simple import hook
An alternative approach: http://pastebin.com/z6pNqFYE or: # devpla...@gmail.com # 2011-Nov-15 # recordimports.py # my Import Hook Hack in response to: # http://groups.google.com/group/comp.lang.python/browse_thread/thread/5a5d5c724f142eb5?hl=en # as an initial learning exercise # This code needs to come before any imports you want recorded # usually just once in the initial (main) module # Of course you can excluding the if __name__ == '__main__': demo code # barely tested: #only tested with modules that had no errors on import #did not need/use/expect/try reload() #ran with Python 2.7 on Win32 # create two fake modules moda.py and modb.py and stick some imports in them ''' Exerpt from PEP 302 -- New Import Hooks ... Motivation: - __import__ gets called even for modules that are already in sys.modules, which is almost never what you want, unless you're writing some sort of monitoring tool. Note the last two words.''' # === # place to save Collected imports imported = [] # save old __builtins__.__import__() __builtins__.__dict__['__old_import__'] = __builtins__.__dict__['__import__'] # match __builtins__.__import__() function signature def __myimport(name, globals={}, locals={}, fromlist=[], level=-1): global imported # I don't know why this works. __name__ = locals['__name__'] __file__ = locals['__file__'] __package__ = locals['__package__'] __doc__ = locals['__doc__'] # call original __import__ module = __builtins__.__old_import__(name, globals, locals, fromlist, level) # save import module name into namespace __builtins__.__dict__[name] = module tag = (name, __name__, __file__, __package__, module) # do not append duplicates if tag not in imported: imported.append( tag ) return module # store the new __import__ into __builtins__ __builtins__.__dict__['__import__'] = __myimport # erase unneed func name del __myimport # === # demo if __name__ == '__main__': # import some random packages/modules import sys import moda# a test module that does some other imports import modb# a test module that does some imports from pprint import pprint # imported has name, __name__, __file__, __package__ # show each import for n, __n, __f, __p, m in imported: print n print ' ', __n print ' ', __f print ' ', __p print del n, __n, __f, __p, m print 'recordimports.py' pprint(dir(), None, 4, 1) print print 'moda.py' pprint(dir(moda), None, 4, 1) print print 'modb.py' pprint(dir(modb), None, 4, 1) # print imported print -- http://mail.python.org/mailman/listinfo/python-list