Re: Newby Python help needed with functions
On Fri, Jun 3, 2011 at 7:42 AM, Cathy James wrote: > I need a jolt here with my python excercise, please somebody!! How can I > make my functions work correctly? I tried below but I get the following > error: > > if f_dict[capitalize]: > > KeyError: > This error is because the function capitalize is not a key in the f_dict dict. > Code below: > > > > def capitalize (s): > """capitalize accepts a string parameter and applies the capitalize() > method""" > s.capitalize() > def title(s): > """accepts a string parameter and applies the title() method""" > s.title() > def upper(s): > """accepts a string parameter and applies the upper() method""" > s.upper() > def lower(s): > """accepts a string parameter and applies the lower() method""" > s.lower() As Andrew mentioned, these functions return nothing when they are called, which is probably not what you want. >>> lower("ABCD") (nothing) You need to return the value you want the function to return explicitly. def lower(s): return s.lower() Note that your functions are just calling string methods. Why define them at all? Just use the methods of the str. For instance: >>> str.lower("ABCD" 'abcd' >>> lower = str.lower >>> lower("ABCD") 'abcd' > def exit(): > """ends the program""" > import sys > sys.exit() you can just use sys.exit for exit, no need to define a new function here either. > if __name__ == "__main__": > f_dict = {'capitalize': 'capitalize(s)', > 'title': 'title(s)', > 'upper': 'upper(s)', > 'lower': 'lower(s)', > 'exit': 'exit(s)'} I think this is where things go really bad for you. You are defining a new dict here, f_dict. It has keys that are the strings 'capitalize', 'title', etc... These have values that are strings 'capitalize(s)', etc... I doubt this is what you want. Don't you want the values to be the functions corresponding to the names in the keys? If so, just tell Python so. f_dict = {'capitalize', str.capitalize, ...} > options = f_dict.keys() > prompt = 'Enter a function name from the list (%s): ' % ', > '.join(options) Note that you can use %r to format the repr() of the value. prompt = 'Enter a function name from the list %r: ' % (options,) > while True: > inp = input(prompt) > option =f_dict.get(inp, None)#either finds the function in question or > returns a None object > s = input ("Enter a string: ").strip() > if not (option): > print ("Please enter a valid selection") > else: > if f_dict[capitalize]: > capitalize(s) > elif f_dict [title]: > title(s) > elif f_dict[upper]: > upper(s) > elif f_dict [lower]: > lower(s) > elif f_dict[exit]: > print ("Goodbye!! ") Here is where things are also probably not what you intended. What you are asking Python to do is: 1. Test if f_dict has a key that is the function capitalize; if so, call capitalize on s and throw the result away. 2. If not, then test if f_dict has a key that is the function title; if so, call title on s and throw the result away. etc... First, you can use the dict to retrieve the function you've stored as the value in the dict. fn = f_dict.get(inp, None) That returned value is actually callable! That is, you can then do something like: fn("This is the input string") Of course, as you already know, you should test fn to see if it is None. If so, they typed in an option you don't recognize. Secondly, what are you doing with the formatted string? I assume you want to print it. If so, be explicit. The above lines can be roughly condensed down to: f_dict = {'capitalize':str.capitalize, ...} while True: inp = input(prompt) fn = f_dict.get(inp, None) if fn is not None: s = input("Input a string: ").strip() print "Output is: %s" % fn(s) else: print "Please input a valid selection" Since this is homework, I've purposefully left important bits out that you should be able to figure out on your own. -- Jonathan Gardner jgard...@deal-digger.com http://deal-digger.com 1 (855) 5-DIGGER -- http://mail.python.org/mailman/listinfo/python-list
Writing a MUD Console
I had this idea on an easy way to write a MUD console. Basically, you start a program written in Python. This will connect to the MUD of your choice, and otherwise behave like Telnet. In fact, it will just spawn telnet to do all the hard work for you. As you type commands, they are piped directly into the telnet process as MUD input commands. Commands that start with ! or maybe # will be intercepted, however, and interpreted as special commands Also, as output comes back, you can put hooks in to react to the data and execute Python functions, or pipe the data to subprocesses you may spawn. For instance, you might spawn a subprocess that opens up a window to show your current map location. When the MUD sends the map, it is intercepted and directed to this map subprocess. Your status line could be interpreted and displayed in another window in graphical format. I have the feeling that this isn't that hard to do. It's just a matter of getting the right combination of subprocesses working. My preliminary experiments with the telnet subprocess have had curious results: CODE: import subprocess import sys import traceback host = 'midkemiaonline.com' port = '23' conn = subprocess.Popen( ['/usr/bin/telnet', host, port], stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr) def do_command(cmd): print "\n>>> {}".format(cmd) try: result = eval(cmd) except Exception: traceback.print_exc() else: if result is not None: print repr(result) while True: cmd = raw_input() if cmd[:1] == '!': do_command(cmd[1:]) else: conn.stdin.write(cmd) conn.terminate() END CODE It seems all goes well, except certain input sequences are being intercepted. For instance, CTRL-C throws a KeyboardInterrupt. Also, I only get to enter one command: $ ./telnetsubprocess.py Trying 209.212.147.74... Connected to midkemiaonline.com. Escape character is '^]'. Rapture Runtime Environment v2.1.6 -- (c) 2010 -- Iron Realms Entertainment Multi-User License: 100-0004-000 * -- Midkemia Online -- * [Open Beta] Based on the novels by Raymond E. Feist Midkemia Online's IP address is 209.212.147.74 For general questions e-mail supp...@midkemiaonline.com There are 20 people currently on-line. 1. Enter the game. 2. Create a new character. 3. Quit. Enter an option or your character's name. 1 Traceback (most recent call last): File "./telnetsubprocess.py", line 17, in cmd = raw_input() EOFError Connection closed by foreign host. Any ideas on what is going on here? -- Jonathan Gardner jgard...@jonathangardner.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing a MUD Console
On Fri, Jul 22, 2011 at 2:25 AM, Chris Angelico wrote: > Rather than attach it to this post, I've tossed the script onto my > MUD's web site. (Yes, I run a MUD. Your subject line grabbed my > attention!) Grab it from http://minstrelhall.com/RosMudAndroid.py and > give it a whirl! > That code is surprisingly simple. Let me write one that uses asyncore so that the looping can incorporate other async processes as well. -- Jonathan Gardner jgard...@jonathangardner.net -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get started in GUI Programming?
I would argue with your assertion that either TKinter of PyGTK are the best. There are several other good alternatives, including wxPython and PyQt, which are very comparable, if not better. I would strongly suggest starting with PyQt. It's my personal favorite. I wrote a short tutorial on the Python Wiki. http://wiki.python.org/moin/JonathanGardnerPyQtTutorial I find that the Qt API is a lot simpler than the alternatives. Plus, it has great documentation. -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I do this in Python?
I don't know what engine you are using. Basically all you have to do is render the login form at the url of the page that needs the login. You're going to have to hook in the authentication code before you render the page normally. For instance, this is a common way I've been doing it in various engines I use: (1) Check to see if the user is logging in by looking at the parameters. If they login successfully, record that in the session and continue as normal. (2) Normal operation being... check to see if they are logged in according to their session. (The code in (1) will set up their session to be logged in if they just logged in.) If they aren't and they need to be, show the login page. Otherwise, show the regular content. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is this a good idea or a waste of time?
Simon Forman wrote: > > If you have a reason to restrict your code to using only ints (perhaps > you're packing them into an array of some sort, or passing them to a C > extension module) then yes, of course it's appropriate. I politely disagree. Rather than an interface that demands an actual int, demand something that can be typecast as an int. For instance: def needsInt(i): i = int(i) ... pass i to an internal c function that requires an int ... # Or better yet, write your internal c function to take any Python object and cast it into an int. If you absolutely need a particular type of thing, then cast it into that thing. -- http://mail.python.org/mailman/listinfo/python-list
Re: still a valid book?
You may want to read http://python.org/doc/2.4.2/whatsnew/whatsnew24.html to get an idea of what has changed from 2.3 to 2.4 if you buy the 2.3 book. -- http://mail.python.org/mailman/listinfo/python-list
Soduku
Here at my job, Python gets no respect. A programmer here wanted to see which language among Python, Perl, and Java was better. So he wrote a Python, perl, and Java version. Except the Python version was 20x slower than the perl and Java versions. Well, luckily he asked around in our little Python group about what he did wrong. He simply misindented a block of code, making it run 10x as often as it needed to. With that one fix, it was running faster than the Java and perl versions, about 2x as fast. A few other minor tweaks made it run even faster. And we've been playing with different algorithms for soduku solutions. I had it down to 27 ms to solve the puzzle. So, one more story on why Python is really good. I think, at least with 2.4, we should start bragging about Python's speed. I mean, it beats Java AND perl! -- http://mail.python.org/mailman/listinfo/python-list
Re: Soduku
27ms == 0.027s How do you have a 16x16 grid for soduku? Are you using 16 digits? 0-F? The one I am using has 9 digits, 9 squares of 9 cells each, or 9x9 cells. The least efficient part of the algorithm is that part to calculate what to put in a hole (as anyone might've guessed.) My algorithm calculates this by using a set of possible values. Then I visit each cell in the same row, column, and square, removing values from the set. What's left is the possible values. If there is only one possible value, then I've found what belongs there. The soduku puzzle I was using for a test can be solved without making any guesses in this way. The guy's algorithm was visiting each empty square and then trying out numbers from 1-9. If they didn't violate the rules (by examining each cell in the row, column, and square for each guess), then he would move to the next empty cell and try again, until he got the solution. There was a lot of backtracking and it would even try to find all possible answers. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to cat None
You can just surround the offending value with str(...). You should probably be doing that anyway, because the value might be a number or something else not stringish. -- http://mail.python.org/mailman/listinfo/python-list
Re: Databases and python
I'm no expert in BDBs, but I have spent a fair amount of time working with PostgreSQL and Oracle. It sounds like you need to put some optimization into your algorithm and data representation. I would do pretty much like you are doing, except I would only have the following relations: - word to word ID - filename to filename ID - word ID to filename ID You're going to want an index on pretty much every column in this database. That's because you're going to lookup by any one of these columns for the corresponding value. I said I wasn't an expert in BDBs. But I do have some experience building up large databases. In the first stage, you just accumulate the data. Then you build the indexes only as you need them. Let's say you are scanning your files. You won't need an index on the filename-to-ID table. That's because you are just putting data in there. The word-to-ID table needs an index on the word, but not ID (you're not looking up by ID yet.) And the word ID-to-filename ID table doesn't need any indexes yet either. So build up the data without the indexes. Once your scan is complete, then build up the indexes you'll need for regular operation. You can probably incrementally add data as you go. As far as filename ID and word IDs go, just use a counter to generate the next number. If you use base255 as the number, you're really not going to save much space. And your idea of hundreds of thousands of tables? Very bad. Don't do it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Databases and python
About the filename ID - word ID table: Any good database (good with large amounts of data) will handle the memory management for you. If you get enough data, it may make sense to get bothered with PostgreSQL. That has a pretty good record on handling very large sets of data, and intermediate sets as well. Again, I can't speak about BDBs, but something in the back of my head is saying that the entire table is loaded into memory. If so, that's not good for large sets of data. About base255: You can think of strings as numbers. At least, that's how your computer sees them. Converting a number to base255 is really silly. If it's a series of bytes (like a Python string is) then base255 is a string. You want to use an incremented number for the ID because there are some indexes that deal with this kind of data very, very well. If you have your number spread out like a shotgun, with clusters here and there, the index might not be very good. About using many tables: The best answer you are going to get is a bunch of files---one for each word---with the names or IDs of the files. You can store these in a single directory, or (as you'll find out to be more efficient) a tree of directories. But this kind of data is useless for reverse lookups. If you really do have so much data, start using a real database that is designed to handle this kind of data efficiently. The overhead of SQL parsing and network latency soon gets dwarfed by lookup times anyway. -- http://mail.python.org/mailman/listinfo/python-list
Re: Databases and python
About indexes everywhere: Yes, you don't have to be a DB expert to know that indexes everywhere is bad. But look at this example. There are really two ways that the data is going to get accessed in regular use. Either they are going to ask for all files that have a word (most likely) or they are going to see which words are in a file. I'm going to have to name the tables now, aren't I? Here's a simple schema: words word_id word files -- file_id filename word_files -- file_id word_id If you are going to lookup by word, you'll need an index on words.word. You'll also need an index on word_files.word_id. And then you'll need an index on files.file_id. If you are going to lookup by file, you'll need an index on files.filename, word_files.file_id, and words.word_id. So it ends up in this situation you need indexes everywhere. Now, when you are doing the initial population, you should drop all the indexes you don't need during population. That means everything but words.word has to go. (You'll need to find the word_id for previously seen words.) After the initial population, then is the time to build and add the indexes. it's much faster to build an index when you have the entire set of data in front of you than to do it piece-by-piece. Some indexes actually get built better than they would've piecemeal. Unfortunately this is no longer strictly topical to Python. But if you free your mind from thinking in terms of SQL databases and look at indexes as dicts or whatnot, then you can see that this is really a general programming problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Merging two lists of data (Pythonic way)
codes = map(lambda x: x[0], list1) for d in list2: if d['code'] in codes: d['VERIFIED'] = 1 Is this what you were looking for? -- http://mail.python.org/mailman/listinfo/python-list
Re: define loop statement?
No, not in the way you think it is. What you can do instead is something like this: def do_something(i): ... do_something ... def loop(n, func): for i in range(n): func(i) loop(10, do_something) -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference: __iadd__ and __add__
I would like to point out that it isn't entirely obvious where to find documentation for this particular thing. I know from experience where to go, but I remember spending a long time trying to hunt this down. For reference, you may want to check out the index of the language reference manual. http://www.python.org/doc/2.4.2/ref/genindex.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Best python module for Oracle, but portable to other RDBMSes
On database portability... While it is noble to try to have a generic interface to these libraries, the end result is that the databases are always different enough that the interface just has to work differently. My experience in going from one database to another is that you should revisit your entire database interface anyway. (I remember going from Sybase to Oracle when we had Perl's DBI way back when. It was still very, very messy.) So, pick a good module, learn it inside and out, and plan on using a completely different module if you use a different database, with perhaps at least a slightly different interface. -- http://mail.python.org/mailman/listinfo/python-list
Re: Playing with modules
You're going to have to create the home.base module somewhere. If you want to put in that some code that basically imports another module's namespace into the home.base module's namespace, then that may do what you want. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best python module for Oracle, but portable to other RDBMSes
I've never seen the points of those tools. Just lay it out on paper or document it somewhere. Be consistant with your naming scheme and it shouldn't be hard to see the relations. If found that the people who don't understand how tables should relate to one another are also the same people who don't understand the special arrows DBAs like to use. -- http://mail.python.org/mailman/listinfo/python-list
Re: type = "instance" instead of "dict"
You should probably spend a bit more time in the documentation reading things for yourself. Read http://www.python.org/doc/2.4.2/ref/types.html under "Class Instances" for your answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Catch exceptions
(A) You'll have to be more specific about your problem if you want us to help. Under which circumstances did it work or not? What exactly is the problem? (B) Where you put your error-handling is up to you. It depends on what the function does and how it is to respond. Do you want the function to throw an exception if the file is not found, or do you want it to do something else? Think about the interface of the functions (which is the sum of the parameters, response, possible exceptions, and expected behavior) and it should be clear where to put the exception handling. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to say $a=$b->{"A"} ||={} in Python?
On Aug 16, 3:35 pm, beginner <[EMAIL PROTECTED]> wrote: > > In perl it is just one line: $a=$b->{"A"} ||={}. > a = b.setdefault('A', {}) This combines all two actions together: - Sets b['A'] to {} if it is not already defined - Assigns b['A'] to a More info on dict methods here: http://docs.python.org/lib/typesmapping.html -- http://mail.python.org/mailman/listinfo/python-list
Re: confused about why i get a type error when i call an object's method
On Aug 21, 11:07 am, [EMAIL PROTECTED] wrote: > I'm confused about why i get a type error when i call an object's > method. Here's the example code:>>> class Foo: > > def __init__(self): > self.foo = [] > def foo(self): > print "in foo!" > > >>> f = Foo() > >>> dir(f) > > ['__doc__', '__init__', '__module__', 'foo'] > > >>> f.foo() > > Traceback (most recent call last): > File "", line 1, in > f.foo() > TypeError: 'list' object is not callable > > You've redefined what "f.foo" means in __init__. It's no longer a class method, but an attribute of the instance--the list. You can't call a list. Do this to show what I mean: >>> f.foo [] -- http://mail.python.org/mailman/listinfo/python-list
Re: REGULAR EXPRESSION
On Sep 4, 6:32 am, AniNair <[EMAIL PROTECTED]> wrote: > hi.. I am trying to match '+ %&/-' etc using regular expression in > expressions like 879+34343. I tried \W+ but it matches only in the > beginning of the string Plz help Thanking you in advance... You may want to read the page describing the regex syntax a little closer. http://docs.python.org/lib/re-syntax.html \W would match anything but \w. That is, it would match spaces and tabs as well as the weird characters. I don't think that's what you want. Also, don't forget to use raw strings. r"\W+" is preferred over "\W+". -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing long `To' and 'Cc' from email
On Sep 6, 1:25 pm, Gerardo Herzig <[EMAIL PROTECTED]> wrote: > the email.* package dont seems to parse that kind of headers > `correctly'. What i want is to get a list with all the email address in > the `To' header. > > Someone know if there is a more sofisticated parser for doing this? > If you're not interested in parsing the entire email message, you may just want to run a regex on the message itself, looking for the "to" header. Here's a good start: r"^to:\s*(.*)$" You'll want to use the multi-line and case-insensitive options when you use it. -- http://mail.python.org/mailman/listinfo/python-list
Re: initializing cooperative method
On Sep 6, 11:18 am, jelle <[EMAIL PROTECTED]> wrote: > Ai, calling super(Abstract) is just getting object, sure... > > However, I'm still curious to know if there's a good idiom for > repeating the argument in __init__ for the super(Concrete, > self).__init__ ? > If you don't know what the arguments are, you can use *args and **kwargs. class Concrete(Abstract): def __init__(self, *args, **kwargs): super(Concrete, self).__init__(*args, **kwargs) -- http://mail.python.org/mailman/listinfo/python-list
Re: getting the current function
On Sep 6, 8:43 am, Gary Robinson <[EMAIL PROTECTED]> wrote: > > I welcome feedback of any type. > This all seems a bit too complicated. Are you sure you want to do this? Maybe you need to step back and rethink your problem. Your example can be rewritten a number of different ways that are easier to think about. The simple approach: _x_counter = 0 def x(): _x_counter += 1 return _x_counter The generator approach: def counter_gen(): x = 0 while True: x += 1 yield x counter = counter_gen() counter.next() counter.next() # etc... A class-based approach: class Counter(object): def __init__(self): self._x = 0 def count(self): self._x += 1 return self._x In general, there really isn't a need to mess with stack frames unless you are really interested in looking at the stack frame. -- http://mail.python.org/mailman/listinfo/python-list
Re: why should I learn python
On Sep 6, 2:32 pm, windandwaves <[EMAIL PROTECTED]> wrote: > Can someone tell me why I should learn python? I am a webdeveloper, > but I often see Python mentioned and I am curious to find out what I > am missing out on. The reason I use Python is because I get more done quicker with fewer bugs. I used to be a huge perl fan after being a huge C/C++ fan, and Python is where I am right now and have been for the past 6 or more years. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing data in an QAbstractListModel
On Sep 6, 9:12 am, "exhuma.twn" <[EMAIL PROTECTED]> wrote: > I defined a simple "update" method in the model which I call on > certain events to fetch the new data in the DB. I tried to "emit" the > "dataChanged()" signal of the Model without success. I don't know > where I should get the two required "index" parameters from. > > Any hints? > Shouldn't the index method give you the indexes you need? -- http://mail.python.org/mailman/listinfo/python-list
Re: SSL Issue
On Sep 5, 10:47 pm, Harry George <[EMAIL PROTECTED]> wrote: > Jurian Botha <[EMAIL PROTECTED]> writes: > > Sorry if this is a real dumb question, but I'm totally stumped. > > > I'm trying to connect to a https url in order to do some xml-rpc method > > calls, but I'm getting the following error: > > > Error Type: sslerror > > Error Value: (6, 'TLS/SSL connection has been closed') > > > What could be causing this error, any clues would be greatly appreciated. > > > Thanks > > -- > > View this message in > > context:http://www.nabble.com/SSL-Issue-tf4388062.html#a12510772 > > Sent from the Python - python-list mailing list archive at Nabble.com. > > I don't have a complete story, but here are some hints: > > 1. The message is from:http://www.openssl.org/docs/ssl/SSL_get_error.html > (see "ZERO RETURN") > probably as filtered by > PyOpenSSL, which has its own > issues:http://mail.python.org/pipermail/python-dev/2007-August/074322.html > > 2. Chances are that your certificates are out of whack, or you are > misusing the SSL context parameters, or are not telling the HTTP > Connection object about the SSL Connection properly. > > 3. Debugging at the python layer is easier (put print statements in > M2Crypto's SSL/Context and SSL/Connection) but if necessary, dive into > openssl: > http://www.mail-archive.com/[EMAIL PROTECTED]/msg49287.html > > 4. You can check for the "hello" handshake using WireShark. > > 5. I haven't found a tutorial for full Python client/server over HTTPS > with verification of both client and server certificates. If that is > where you are going, let me know what works. > Try the "openssl s_client :" command to get some handy info on what is happening. It's kind of like telnet but for SSL. This will help isolate SSL issues from the Python layer. -- http://mail.python.org/mailman/listinfo/python-list
Re: Changing data in an QAbstractListModel
On Sep 7, 1:24 am, "exhuma.twn" <[EMAIL PROTECTED]> wrote: > Still, nothing is happening when I call this method. Do I still need > to handle the "dataChanged" signal somehow? Or does the ListView take > care of this? You might have better luck asking these kinds of questions in the Qt or PyQt forums. -- http://mail.python.org/mailman/listinfo/python-list
Re: getting the current function
On Sep 7, 9:19 am, Gary Robinson <[EMAIL PROTECTED]> wrote: > > This all seems a bit too complicated. Are you sure you want to do > > this? Maybe you need to step back and rethink your problem. > > In version 2.1 Python added the ability to add function attributes -- > seehttp://www.python.org/dev/peps/pep-0232/for the justifications. A counter > probably isn't one of them, I just used that as a quick example of using > thisfunc(). > > I've just never liked the fact that you have to name the function when > accessing those attributes from within the function. And I thought there > might be other uses for something like thisfunc(). > If I had a scenario where I needed a function that would change its behavior based on its attributes I would use an object instead. If I really, really needed it to behave like a function, then I would override __call__. Using a function's attributes to modify its behavior seems like using a singleton or a module variable to modify the behavior of functions. I would avoid it if I wanted to write code that someone else (including myself) may want to use one day. Otherwise, it would be hard to keep different bits of code separate from one another. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Database Apps
On Sep 11, 1:07 pm, Tom Brown <[EMAIL PROTECTED]> wrote: > > I have had a lot of good luck with PostgreSQL. It is easy to install and use. > It is also very stable. It maybe overkill for a client side database. The > psycopg package makes interfacing to PostgreSQL very easy and there is a > package for Linux and Windows to make cross-platform development a breeze. > SQLAlchemy works wonderfully with PostgreSQL. I absolutely love it. For client-side apps, managing a PostgreSQL installation might be asking too much. But for a web site or web service, I absolutely recommend it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Database Apps
On Sep 11, 1:39 pm, Jonathan Gardner <[EMAIL PROTECTED]> wrote: > > For client-side apps, managing a PostgreSQL installation might be > asking too much. But for a web site or web service, I absolutely > recommend it. I should mention that I wrote a medical billing software app (client side--PyQt) in Python with a PostgreSQL backend and there were no problems (aside from managing the database--which was easy.) That was over 4 years ago though. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Database Apps
On Sep 11, 5:56 am, Harry George <[EMAIL PROTECTED]> wrote: > I use postgresql as well. I wonder if Pythonistas do so out of > concern for rigor, clarity, and scalability. It works fine for a > quick one-off effort and still works fine after scaling to a DBMS > server supporting lots of clients, and running 10's of GBs of data. > I can only speak for myself, but I use it for the reasons you listed. I also appreciate the concise and clear documentation, the helpfulness of the community, and the clarity of the code. It's also pretty easy to get back into PostgreSQL after leaving it for years. There's not too many weird details you have to remember to get your job done. > Of course, as long as you write DBI2 compliant code, your app doesn't > much care which DBMS you use. The postgresql payoff is in admin > functionality and scaling and full ACID. > It's not true that DBI2 compliance meands plug-and-play. Among the various databases, the featuresets are different, and the way certain features are implemented is different. You need something like SQLAlchemy on top to make it truly portable. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Database Apps
On Sep 12, 9:38 pm, Prateek <[EMAIL PROTECTED]> wrote: > Have you checked out Brainwave?http://www.brainwavelive.com > > We provide a schema-free non-relational database bundled with an app > server which is basically CherryPy with a few enhancements (rich JS > widgets, Cheetah/Clearsilver templates). Free for non-commercial use. > You might want to rethink how you are handling databases. What sets your database apart from hierarchical or object-oriented databases? Do you understand why people prefer relational databases over the other options? There's a reason why SQL has won out over all the other options available. You would do well to understand it rather than trying out things we already know do not work. -- http://mail.python.org/mailman/listinfo/python-list
Re: plotting pixels in python
On Sep 13, 11:37 am, [EMAIL PROTECTED] wrote: > No currently I am using a canvas from the Tkinter module > What I actually want is to show how a line is plotted pixel by pixel > using a delay loop. I want functions something like putpixel not > draw_line Try drawing 1px wide rectangles. -- http://mail.python.org/mailman/listinfo/python-list
Re: "sem_post: Invalid argument"
On Oct 25, 12:56 pm, robert <[EMAIL PROTECTED]> wrote: > On a server the binary (red hat) installed python2.4 and also a > fresh compiled python2.5 spits "sem_post: Invalid argument". > What is this and how can this solved? > ... > Python 2.4.3 (#1, Jun 6 2006, 21:10:41) > [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)] on linux2 > ... > server [~]# uname -a > Linux server 2.4.34.1-p4-smp-bigmem-JWH #1 SMP Mon Mar 19 03:26:57 > JST 2007 i686 i686 i386 GNU/Linux Are you sure you have compatible binaries? Or did you install a random RPM without checking for dependencies? -- http://mail.python.org/mailman/listinfo/python-list
Re: "sem_post: Invalid argument"
On Oct 25, 2:19 pm, robert <[EMAIL PROTECTED]> wrote: > Jonathan Gardner wrote: > > On Oct 25, 12:56 pm, robert <[EMAIL PROTECTED]> wrote: > >> On a server the binary (red hat) installed python2.4 and also a > >> fresh compiled python2.5 spits "sem_post: Invalid argument". > >> What is this and how can this solved? > >> ... > >> Python 2.4.3 (#1, Jun 6 2006, 21:10:41) > >> [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-54)] on linux2 > >> ... > >> server [~]# uname -a > >> Linux server 2.4.34.1-p4-smp-bigmem-JWH #1 SMP Mon Mar 19 03:26:57 > >> JST 2007 i686 i686 i386 GNU/Linux > > > Are you sure you have compatible binaries? Or did you install a random > > RPM without checking for dependencies? > > Should be compatible - but I am not sure if the kernel was > recompiled on this machine. And at least the fresh ./configure'ed > and compiled py2.5, which yields the same problem, should be > maximum compatible. Maybe because this machine is a "smp-bigmem" .. > At this point, I would start digging into the error messages themselves. Maybe a shout out to the developers of whatever code is generating that error message. When you understand under what conditions that error message is thrown, perhaps it will yield some insight into what python is doing differently than everything else. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to/help installing impure packages
On Oct 26, 10:35 am, owl <[EMAIL PROTECTED]> wrote: > I love easy_install. > > I love watching it search and complete. > It's when it doesn't complete that causes me grief. > I don't program a lot these days and am relatively new to python but I > often wind up installing packages on both unix and windows machines > and often the 'build' part of the install fails with not being able to > find the compiler or compile problems. > > If there are any articles/pointers that talk about this, i.e. how to > troubleshoot, I'd love to know. > > If I was writing such an article I'd start with: > - how easy_install uses distutils (even this is an assumption on my > part) > - where it looks in the env. for stuff on both unix/linux/windows > - what files to tweak to help it find stuff if your env. is different. > - i.e. can I config some file to have packages not try and compile but > use pure python if possible? > - i.e. w/o speedups > - how to work around problems such as easy_install failing because a > site is down > - i.e. how to manually download, 'unpack' & 'build' > - since packages fail to build, how to set things up to download & > keep things around when they fail so you can tinker with it until you > get it working! I would enjoy seeing an article like this as well. However, in the meantime, feel free to report your error messages to the authors of the modules in question or even here. I am sure someone would be willing to give some advice. At the very least, they need to know that all is not perfect out in user-land. -- http://mail.python.org/mailman/listinfo/python-list
Re: why did these companies choose Tcl over Python
On Oct 30, 2:25 pm, chewie54 <[EMAIL PROTECTED]> wrote: > > I would prefer to use Python but can't deny how popular Tcl is, as > mentioned above, so my question is why wasn't Python selected by > these companies as the choice of scripting languages for their > product? > Here are some reasons why people choose language X over Y: - History. We've always used X, so why should we use Y? - Peer pressure. Everyone else uses X. - Licensing. Language X's license fits our business better than language Y. - Support. It is easier/safer/etc... to get support for language X over language Y. (In some cases, the company that wrote X provides support contracts while language Y doesn't have such a company.) - Customer demands. Our customers want language X support, not Y. - Features. Language X does A, B, and C really well, while language Y only does it a little bit or not very well. You'd have to ask them why they do what they do, but it's probably a combination of the above. Oftentimes, the reasons cited are based on assumptions and not facts. Of course, objectively measuring any language is difficult at best, so there is little hope of this ever getting any better. As far as performance, memory footprint, cross-platform support, I don't believe Tcl is better than Python (and probably not worse), at least compared to C. Tcl might also be a more natural language for hardware people who have a hard time accepting the more esoteric concepts of software engineering. It's a straight-forward language with little in the way of what they may consider fluff. -- http://mail.python.org/mailman/listinfo/python-list
Re: Arp request to get MAc Adress with IP address
On Oct 30, 1:57 pm, [EMAIL PROTECTED] wrote: > Hello, > I have a network on same subnet. I have an ip address of a machine. > but i need to get its MAC Adress. > Sendingf ARP request is the way i am taking. > > IS there any other way to get this MAC Adress in python.?? > > Also does python go down to level 2 of TCP/IP model?? > When you start talking about this kind of low-level stuff, you really need to start thinking about your OS, not Python. How would you do this task if you had to write in in C? Which system calls would you need to make? Once you figure that out, then it's a question how to make those system calls from Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: shelve.open() and error 22: invalid argument
On Nov 1, 1:08 pm, [EMAIL PROTECTED] wrote: > Hi everyone > > I've come across the following problem: on two different linux > machines, both running python 2.5 (r25:51908), I have the same file > 'd.dat'. The md5 checksums are the same. > > Now, on one machine the following code works > > >>> import shelve > >>> d=shelve.open('d.dat') > > while on the other... > > Traceback (most recent call last): > File "", line 1, in > File "local/lib/python2.5/shelve.py", line 225, in open > return DbfilenameShelf(filename, flag, protocol, writeback) > File "local/lib/python2.5/shelve.py", line 209, in __init__ > Shelf.__init__(self, anydbm.open(filename, flag), protocol, > writeback) > File "local/lib/python2.5/anydbm.py", line 83, in open > return mod.open(file, flag, mode) > File "local/lib/python2.5/dbhash.py", line 16, in open > return bsddb.hashopen(file, flag, mode) > File "local/lib/python2.5/bsddb/__init__.py", line 299, in hashopen > e = _openDBEnv(cachesize) > File "local/lib/python2.5/bsddb/__init__.py", line 355, in > _openDBEnv > e.set_lk_detect(db.DB_LOCK_DEFAULT) > bsddb.db.DBInvalidArgError: (22, 'Invalid argument') > > What is happening? I am running the same Python interpreter on the > same file! Why different results? (To make things weirder, this > actually fails on the machine in which I created the d.dat file using > the shelve module!) > This comes up outside of Python as well. Look into your BDB setup. I bet you that the two machines have different versions of BDB libraries. -- http://mail.python.org/mailman/listinfo/python-list
Re: just a crazy question
On Nov 1, 7:16 am, Robert LaMarca <[EMAIL PROTECTED]> wrote: > So.. how is Python for memory management? ... > Terrible. If you have a memory-intensive application, use ASM (perhaps C), not Python (or any other high-level language for that matter.) > My plan is to try measuring the memory usage of my python code elements and > take a guess as to how it might perform on a cell-system.. . naturally a good > idea in any event.. But.. Just thought maybe someone out in Python world > would have an idea? > This probably won't work at all as expected. If you wanted to get Python processes to run on the cells, you'd probably need a special interpreter written with that in mind. My memory of how the cell technology works is that there is really a tiny amount of memory in each cell and you need to think at a fundamentally low level to get anything useful done. I'm extremely interested in what you discover, and I'm sure others are well. I'd love to be proven wrong in my assumptions. So keep us posted when you find something interesting or useful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Portrait of a "real life" __metaclass__
On Nov 9, 7:12 pm, Mark Shroyer <[EMAIL PROTECTED]> wrote: > I guess this sort of falls under the "shameless plug" category, but > here it is: Recently I used a custom metaclass in a Python program > I've been working on, and I ended up doing a sort of write-up on it, > as an example of what a "real life" __metaclass__ might do for those > who may never have seen such a thing themselves. > > http://markshroyer.com/blog/2007/11/09/tilting-at-metaclass-windmills/ > > So what's the verdict? Incorrect? Missed the point completely? > Needs to get his head checked? I'd love to hear what > comp.lang.python has to (anthropomorphically) say about it. > Kinda wordy. Let me see if I got the point: - You already had a bunch of classes that did age matching on date time objects. - You were building a class that matched emails. - You wanted to reuse the code for age matching to do email matching (based on the message's age) - So you wrote a metaclass that replaced the match() method with a proxy that would either dispatch to the old match() method (if it was a datetime object) or dispatch to the new match() method (which matched based on the message's date.) Sounds Java-y, if that's even a word. Too many classes, not enough functions. You can tell you are doing Java in Python when you feel the urge to give everything a name that is a noun, even if it is completely based of a verb, such as "matcher". My opinion is that if you want to talk about doing something in Python such as matching, starting writing functions that match and forget the classes. Classes are for real nouns, nouns that can do several distinct things. What would I have done? I wouldn't have had an age matching class. I would have had a function that, given the datetime and a range specification, would return true or false. Then I would've written another function for matching emails. Again, it takes a specification and the email and returns true or false. If I really wanted to pass around the specifications as objects, I would do what the re module does: have one generic object for all the different kinds of age matching possible, and one generic object for all the email objects possible. These would be called, "AgeMatchSpecification", etc... These are noun-y things. Here, however, they are really a way of keeping your data organized so you can tell that that particular dict over there is an AgeMatchSpecification and that one is an EmailMatchSpecification. And remember, the specifications don't do the matching--they merely tell the match function what it is you wanted matched. Now, part of the email match specification would probably include bits of the date match specification, because you'd want to match the various dates attached to an email. That's really not rocket science though. There wouldn't be any need to integrate the classes anymore if I did it that way. Plus, I wouldn't have to remember a bunch of class names. I'd just have to remember the various parameters to the match specification for age matching and a different set of parameters for the email matching. -- http://mail.python.org/mailman/listinfo/python-list
Re: Portrait of a "real life" __metaclass__
On Nov 10, 3:34 am, Mark Shroyer <[EMAIL PROTECTED]> wrote: > On 2007-11-10, Jonathan Gardner <[EMAIL PROTECTED]> wrote: > > What would I have done? I wouldn't have had an age matching class. I > > would have had a function that, given the datetime and a range > > specification, would return true or false. Then I would've written > > another function for matching emails. Again, it takes a specification > > and the email and returns true or false. > > There isn't much difference between > > match_calendar_month(2007, 11, message) > > and > > m = CalendarMonthMatcher(2007, 11) > m.match(message) Yes, there isn't a world of difference between the two. But there is a world of difference between those and: match(message, before=date(2007, 12, 1), after=date(2007, 11, 1)) And you can add parameters as needed. In the end, you may have a lot of parameters, but only one match function and only one interface. > But take for example two of my app's mailbox actions -- these aren't > their real names, but for clarity let's call them ArchiveByMonth and > SaveAttachmentsByMonth. The former moves messages from previous > months into an archival mbox file ./archives//MM.mbox > corresponding to each message's month, and the latter saves message > attachments into a directory ./attachments//MM/. Each of these > actions would work by using either match_calendar_month() or > CalendarMonthMatcher().match() to perform its action on all messages > within a given month; then it iterates through previous months and > repeats until there are no more messages left to be processed. > > In my object-oriented implementation, this iteration is performed by > calling m.previous() on the current matcher, much like the > simplified example in my write-up. Without taking the OO approach, > on the other hand, both types of actions would need to compute the > previous month themselves; sure that's not an entirely burdensome > task, but it really seems like the wrong place for that code to > reside. (And if you tackle this by writing another method to return > the requisite (year, month) tuple, and apply that method alongside > wherever match_calendar_month() is used... well, at that point > you're really just doing object-oriented code without the "class" > keyword.) > > Furthermore, suppose I want to save attachments by week instead of > month: I could then hand the SaveAttachmentsByPeriod action a > WeekMatcher instead of a MonthMatcher, and the action, using the > matcher's common interface, does the job just as expected. (This is > an actual configuration file option in the application; the nice > thing about taking an OO approach to this app is that there's a very > straightforward mapping between the configuration file syntax and > the actual implementation.) > > It could be that I'm still "thinking in Java," as you rather > accurately put it, but here the object-oriented approach seems > genuinely superior -- cleaner and, well, with better encapsulated > functionality, to use the buzzword. > Or it could be that you are confusing two things with each other. Let me try to explain it another way. Think of all the points on a grid that is 100x100. There are 10,000 points, right? If you wanted to describe the position of a point, you could name each point. You'd have 10,000 names. This isn't very good because people would have to know all 10,000 names to describe a point in your system. But it is simple, and it is really easy to implement. But hey, we can just number the points 0 to and it gets even simpler, right? OR you could describe the points as an (x,y) pair. Now people only have to remember 200 different names--100 for the columns, 100 for the rows. Then if you used traditional numbers, they'd only have to be able to count to 100. Computer science is full of things like this. When you end up with complexity, it is probably because you are doing something wrong. My rule of thumb is if I can't explain it all in about 30 seconds, then it is going to be a mystery to everyone but myself no matter how much documentation I write. How do you avoid complexity? You take a step back, identify patterns, or pull different things apart from each other (like rows and columns), and try to find the most basic principles to guide the entire system. The very fact that you are talking about months (and thus days and weeks and years and centuries, etc...) and not generic dates means you have some more simplifying to do in your design elsewhere as well. Rewrite the SaveAttachmentsByMonth so that it calls a more generic SaveAttachmentsByDateRange function. Or better yet, have it FilterEmailsByDateRange and ExtractAttachment and Sa
Re: Newbie design problem
On Dec 13, 11:32 am, [EMAIL PROTECTED] wrote: > Is there a pythonic design I'm overlooking? Well, if using something like PLY ( http://www.dabeaz.com/ply/ ) is considered more Pythonic than writing your own parser and lexer... Python doesn't have all of life's answers unfortunately. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding overlapping times...
On Dec 13, 3:45 pm, Breal <[EMAIL PROTECTED]> wrote: > I have a list that looks like the following > [(10, 100010), (15, 17), (19, 100015)] > > I would like to be able to determine which of these overlap each > other. So, in this case, tuple 1 overlaps with tuples 2 and 3. Tuple > 2 overlaps with 1. Tuple 3 overlaps with tuple 1. > > In my scenario I would have hundreds, if not thousands of these > ranges. Any nice pythonic way to do this? > Sure. Start with a piece of paper and a pencil. Write down under what conditions two tuples of numbers will overlap. Be specific. (Hint: two numbers can be equal, less than, or greater than each other. That's 3 conditions. Then you can compare the first of the first tuple to the first of the second tuple, or the first to the second, or the second to the first, or the second to the second. That's 4 conditions. 3x4=12 so you have 12 possible conditions when comparing two tuples of two numbers. Describe what the result should be for each one. Being graphical will help you get it right.) Once you have that written down, translate it to python. In python, write a loop that goes through each item in the list and compares it to every other item. Remember which items compare favorably by storing them in a list. When the loop finishes, you should have a list of all the pairs that match your conditions. Since this sounds like a homework assignment, the rest is left as an exercise to the reader. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie design problem
On Dec 14, 8:02 am, [EMAIL PROTECTED] wrote: > > Lex is very crude. I've found that it takes about half a day to > organize your token definitions and another half day to write a > tokenizer by hand. What's the point of the second half-day's work? > As someone who has earned a BS in Physics, I have learned one powerful truth: No matter how smart you are, you are not as smart as everyone else. See, the scientific arena of Physics has gotten to the point where it is because people carefully built on each other's work. They spend a great deal of time trying to understand what everyone else is doing and why they do it that way and not another way, and very little time trying to outsmart each other. The brightest bulbs in the physics community don't think they are the brightest bulbs. They are just really good at understanding everyone else and putting it all together. This is summed up in Isaac Newton's statement about seeing farther because he has stood on the shoulders of giants. The same applies to computer science. Either you can take a few days and study about how parsers and lexers really work and why you need them to make your life easier and which implementations are worthwhile, or you can go off and do things on your own and learn the hard way that everyone that went before you was really smarter than you think you are. Five months later, maybe you will have made up the time you would have "wasted" by reading a good booking on formal languages, lexers, and parsers. At that point, you will opt to use one of the existing libraries, perhaps even Bison and Flex. It's your time that is at stake, man. Don't waste it trying to reinvent the wheel, even if you think you need an upside-down one. -- http://mail.python.org/mailman/listinfo/python-list
Re: New+old-style multiple inheritance
On Dec 18, 7:08 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > We are trying to monkey-patch a third-party library that mixes new and > old-style classes with multiple inheritance. In so doing we have > uncovered some unexpected behaviour: > > > I know this level of messing with python internals is a bit risky but > I'm wondering why the above code doesn't work. > You've already discovered why--you're mixing old and new style classes. Monkey patching is definitely unpythonic. You must be a Ruby guy. Why don't you try doing something else to get the behavior you want, something more explicit? -- http://mail.python.org/mailman/listinfo/python-list
Re: Does fileinput.input() read STDIN all at once?
On Dec 18, 5:55 am, Adam Funk <[EMAIL PROTECTED]> wrote: > I'm using this sort of standard thing: > >for line in fileinput.input(): > do_stuff(line) > > and wondering whether it reads until it hits an EOF and then passes > lines (one at a time) into the variable line. This appears to be the > behaviour when it's reading STDIN interactively (i.e. from the > keyboard). > > As a test, I tried this: > >for line in fileinput.input(): > print '**', line > > and found that it would print nothing until I hit Ctl-D, then print > all the lines, then wait for another Ctl-D, and so on (until I pressed > Ctl-D twice in succession to end the loop). > There is probably a 1024 byte buffer. Try filling it up and see if you get something before you hit CTRL-D. Otherwise, the writers have to flush the buffer if they want a line to be sent before the buffer fills up. C++ endl would do this, I believe, in addition to printing '\n'. Note that terminals (what you get if you are typing from the command line) are terminals and behave quite differently than open files. > Is it possible to configure this to pass each line of input into line > as it comes? > It's up to the writer to flush the buffers. There's not much you can do, so just learn to live with it. It sounds like you want to write some kind of interactive program for the terminal. Do yourself a favor and use curses or go with a full- blown GUI. -- http://mail.python.org/mailman/listinfo/python-list
Re: checking a string against multiple patterns
On Dec 18, 4:41 am, tomasz <[EMAIL PROTECTED]> wrote: > Is there an alternative to it? Am I missing something? Python doesn't > have special variables $1, $2 (right?) so you must assign the result > of a match to a variable, to be able to access the groups. > > I'd appreciate any hints. > Don't use regexes for something as simple as this. Try find(). Most of the time I use regexes in perl (90%+) I am doing something that can be done much better using the string methods and some simple operations. Plus, it turns out to be faster than perl usually. -- http://mail.python.org/mailman/listinfo/python-list
Re: Allowing Arbitrary Indentation in Python
On Dec 18, 2:16 pm, Sam <[EMAIL PROTECTED]> wrote: > layouts = ['column', 'form', 'frame'] > cmds.window(t='gwfUI Builder') > cmds.paneLayout(configuration='vertical3', ps=((1, 25, 100), (3, 20, > 100))) > cmds.paneLayout(configuration='horizontal2') > cmds.frameLayout(l='Layouts') > cmds.scrollLayout(cr=True) > cmds.columnLayout(adj=True, cat=('both', 2)) > for i in layouts: > cmds.button(l=i) > cmds.setParent('..') > cmds.setParent('..') > cmds.setParent('..') > cmds.setParent('..') > cmds.setParent('..') > cmds.showWindow() > While Grant is pulling his hair out and yelling obscenities at the moon, let me try to explain why you'd never want to indent code that way, let alone write code that way. In cases where you have to run a sequence of code in a nested way, like this, it's best to create functions and then nest the functions. Of course, I'm thinking more of a lisp solution and less of a C one. In this case, you're going to have to have objects (or data structures) that know what to do to execute the commands necessary. When you get them all assembled, you simply run them in a recursive way. For instance: cmd.build(('pane', dict(configuration='horizontal'), ('frame', dict(l='Layouts'), ( You can indent these arbitrarily. Plus, you don't have to worry too much about matching setParent and the other commands. -- http://mail.python.org/mailman/listinfo/python-list
Re: object vs class oriented -- xotcl
On Jan 24, 12:35 pm, William Pursell <[EMAIL PROTECTED]> wrote: > I'm not sure that describes the method well. Basically, you can > instantiate an object A of class Foo, and later change A to be an > object of class Bar. Does Python support this type of flexibility? > As I stated above, I've been away from Python for awhile now, and am a > bit rusty, but it seems that slots or "new style" objects might > provide this type of behavior. The ability to have an object change > class is certainly (to me) a novel idea. Can I do it in Python? Short answer: yes, easily. Long answer: observe. >>> class Foo(object): ... def foo(self): print "Foo.foo" ... >>> class Bar(object): ... def foo(self): print "Bar.foo" ... >>> a = Foo() >>> a.__class__ = Bar >>> a.foo() Bar.foo -- http://mail.python.org/mailman/listinfo/python-list
Re: Ignore exceptions
On Jan 24, 12:13 pm, SMALLp <[EMAIL PROTECTED]> wrote: > Hy. Is there any way to make interrupter ignore exceptions. I'm working > on bigger project and i used to put try catch blocks after writing and > testing code what's boring and it's easy to make mistake. I remember of > something like that in C++ but I cant find anythin like that for python. > Hello. Two points with exceptions. * Only write try blocks when you are actually going to do something interesting with the exception. Otherwise, let the exception bubble up. * If you have unwanted exceptions, fix the root cause of the exception rather than try to hide the exception. The exception is saying something is wrong and needs attention. Provide that attention. I have seen too many people write code in Java that simply masks all exceptions because they don't want to write the interface to their functions that describes what exceptions are possible. I have seen comments around this code saying, "This should always work." Of course, it doesn't always work and when it doesn't work, they don't know about it and they don't even know how to tell what went wrong. The emotion driving this exception masking practice I see in the Java world is laziness, not correctness. This is not the good form of laziness (where you want to write good code so you end up doing less work) but the bad form (where you don't want to work at all). There is no need to mask exceptions in Python. In fact, it is more work to mask exceptions, and you should feel bad about all the extra typing you are doing. Once again: All try blocks should do something interesting when they catch an exception. No exception should be ignored or thrown away. A few sample good uses of try/except blocks: (1) Do something else if an expected exception occurs. try: # There is a good chance an exception will be thrown. If so, I want to do something else. d['foo'] += 5 except KeyError: d['foo'] = 5 (2) Show a friendly error message when an exception occurs over a significant chunk of the program. (Useful for websites and GUI apps.) try: # There is a very complicated piece of code. Any of a million exceptions could occur. ... except: # Show a nicely formatted error message with hints on how to debug the error. ... Here are some bad examples: (BAD) try: # I don't know what is happening in here, but it always throws an exception. # I don't want to think about it because it makes my brain hurt. ... except: pass (WORSE) The alternate form--try N times, masking the error each time-- is equally bad. while True: try: # Something could go wrong. What could go wrong? Who cares? ... break except: # We'll just keep trying forever pass -- http://mail.python.org/mailman/listinfo/python-list
Re: a newbie regex question
On Jan 24, 12:14 pm, Shoryuken <[EMAIL PROTECTED]> wrote: > Given a regular expression pattern, for example, \([A-Z].+[a-z]\), > > print out all strings that match the pattern in a file > > Anyone tell me a way to do it? I know it's easy, but i'm completely > new to python > > thanks alot You may want to read the pages on regular expressions in the online documentation: http://www.python.org/doc/2.5/lib/module-re.html The simple approach works: import re # Open the file f = file('/your/filename.txt') # Read the file into a single string. contents = f.read() # Find all matches in the string of the regular expression and iterate through them. for match in re.finditer(r'\([A-Z].+[a-z]\)', contents): # Print what was matched print match.group() -- http://mail.python.org/mailman/listinfo/python-list
Re: Python seems to be ignoring my except clause...
On Feb 19, 6:14 am, "Adam W." <[EMAIL PROTECTED]> wrote: > So I deleted my .pyc files and reran, same thing, but then I closed all > open windows and reran it, and it recompiled the pyc and the code > "worked". > ... > But now I know I have to keep deleting my > pyc files or else I will run into trouble. What editor are you using? It sounds like it doesn't set the timestamp on the files you are editing properly. That is, every time you save your file it should update the timestamp of the .py file so that python can see that there is an older .pyc next to a newer .py. But that is probably the least of your worries right now... -- http://mail.python.org/mailman/listinfo/python-list
Re: Web Development Project
On Feb 21, 3:34 pm, john_sm3853 <[EMAIL PROTECTED]> wrote: > Hey guys, I am interested in knowing, what new Web Development projects you > are doing, curious to know, what data base you use, and if you are using > Linux or Windows platform. Also, will like to know, if there are any > alternatives to Adobe products, that you may be using Linux (Red Hat Fedora) Apache (for production) Pylons (with Mako, SQLAlchemy) PostgreSQL I don't do image work, however. If I really need to, I will use Gimp. For vector editing, I have played with some of the open source alternatives and found them satisfying. -- http://mail.python.org/mailman/listinfo/python-list
Asynchronous urllib (urllib+asyncore)?
So, I ran into a problem that I would like to write as little code as possible to solve. The problem is that I would like to send out a bunch of HTTP requests simultaneously, using asynchronous techniques, and then do stuff with the results in parallel. Think of something like Google's map-reduce. It sounds like this would be something someone else has done before. I searched google, but I don't think there is any discussion anywhere of anyone doing something like this. I am sure Twisted folks can code this up in 2 lines or less, or it probably already exists there by default. But I need this to exist in a non-asynchronous environment. I can't let Twisted take over everything. So, has someone done something like this? Is it something where I can bring the urllib and asyncore together? Or is it much more involved? -- http://mail.python.org/mailman/listinfo/python-list
Re: Beautiful Code in Python?
On Mar 2, 8:35 am, Michele Simionato <[EMAIL PROTECTED]> wrote: > On Mar 2, 5:23 pm, js <[EMAIL PROTECTED]> wrote: > > > Hi, > > > Have you ever seen Beautiful Python code? > > Zope? Django? Python standard lib? or else? > > > Please tell me what code you think it's stunning. > > The doctest module in the standard library. > > M.S. The first thing of beauty I found in Python (coming from C, C++, and perl) was the way Python handled variables, or as someone recently described them, names. Python's "for" statement is always beautiful to look at. Especially when someone uses the else clause rather than trying to detect if the list was exhausted. I sometimes avoid using the comprehensions just to get an excuse to write another for loop in Python. There can never be enough for loops written in Python! Also, Python's iterator interface is by far the most beautiful thing I have ever seen in the world of programming. Of course, the reason why the for loop is so beautiful is because iterators are so beautiful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Edit and continue for debugging?
This is an interesting issue because we who write web applications face the same problem. Except in the web world, the application state is stored in the browser so we don't have to work our way back to where we were. We just keep our url, cookies, and request parameters handy. Before I go on, I would suggest one thing: unit tests. If you have a hard time writing unit tests, then you need to rethink your code so that you can write unit tests. Having unit tests, running them, and debugging them is a far easier way to catch bugs and prevent them from coming back. If you don't know what a "regression test" is, you should look into the field of software testing. You'll thank yourself for it. I'm no lisp programmer, but my understanding of how lisp and lisp-like programs do this is that they replace the function with the new version. What this does to threads that are already running the function, I guess they just finish and continue on. What about closures? Well, you're out of luck there. I guess lisp programmers don't use closures in that way too often. I guess you could do the same in your debug session, although it would be hacky and difficult at best. You're out of luck if there's more than a handful of things referring to the object. A better solution would probably be to fix the offending line in the offending file and somehow magically reload that file. Well, you still have the same problem. See, a lot of your program depends on the functions and classes and objects already in that file. Those dependencies don't get magically fixed to point to the new objects just loaded. You'll have to go throughout the entire universe of variables and make them point to the new stuff. This is not an easy feat to do. In the end, you're going to realize that unless you design the system to allow you to "reload" files, whatever that means (and you'll have to define that as well), you aren't going to be able to do that. There just isn't a straightforwad, one-size-fits-all solution to this problem, except for stopping the process altogether and restarting from scratch. On Mar 7, 6:44 am, "Bronner, Gregory" <[EMAIL PROTECTED]> wrote: > I haven't seen much on this for a few years: > > I'm working on a GUI application that has lots of callbacks. Testing it > is very slow and quite boring, as every time I find an error, I have to > exit it, restart it, and repeat the series of clicks. It would be really > amazing if python supported a reasonable form of edit and continue. > > Is there any way to do this? I'd like to be able to change my code and > have it apply to a running instance of a class. I wonder if it would be > possible to do this by configuring the interpreter to parse classes and > functions rather than whole modules, and to re-parse as necessary; also > to have byte-compiled modules be singletons rather than be standard > reference counted objects. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there Python equivalent to Perl BEGIN{} block?
On Mar 12, 6:37 pm, Carl Banks <[EMAIL PROTECTED]> wrote: > On Mar 12, 8:11 pm, Justus Schwabedal <[EMAIL PROTECTED]> > wrote: > > > What do you need it for anyway? I just read about it and I think it's > > useless > > in python. > > Perl, like Python, has a separate compilation and run times. One day, > someone who was trying to use Perl for something asked, "You know, > wouldn't it be neat-o if you could execute some Perl code *before* you > compiled the script?" And so (since that's usually enough reason to > add something to Perl) was borne the BEGIN block. > > I believe the official rationale was that they wanted to add some > magic variables that affected Perl's compilation and couldn't do it > without a magic block that executed at compile time. > It's a bit different. Python's "import" and perl's "use" statements aren't very similar at all. See, perl looks for any "use" statements and runs those first. That's not the way for Python at all. It's ok to import a module right before you need it in a function. If you never call the function, you'll never import the module. What if you want to reprogram the search path before you use a module? Well, in Python, you just work on sys.path before the import statement. But in perl, if you put a statement that mucks with the perl path, then it will be ignored until it is too late. So you have to throw it into the BEGIN block to have it do anything at all. What was an obvious task in Python that uses only constructs and concepts you already know about, requires a new concept and syntax that wasn't needed before in perl. Also, Jeff has it right. Those who don't get it should look closely at the '-p' option passed to perl. (It has a cousin '-n' that is also just as useful.) Here, he wants to initialize some code before the loop starts, but he can't specify that initializing code outside of the loop without a BEGIN block. BEGIN is a dirty, ugly, stupid bandage to an underlying weakness in perl. That is, there is magic that needs more magic to override it. (And then you'll need magic to override the overriding magic, ad infinitum.) Rather than adding more magic, what you really need to do is get rid of magic that gets in people's way, or change the magic so it is actually useful for everyone. Python has it right. Tokenize, parse, (skip compile-link-import magic), and run. And leave out the magical -p and -n. If you want to iterate through a file, "for line in sys.stdin:". -- http://mail.python.org/mailman/listinfo/python-list
Re: %x unsigned?
On Mar 14, 8:00 am, Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > The %x conversion specifier is documented > inhttp://docs.python.org/lib/typesseq-strings.htmlas "Unsigned > hexadecimal (lowercase)." What does "unsigned" refer to? > > >>> '0x%x' % 10 > '0xa' Somewhat unrelated, but have you seen the # modifier? >>> '%#x' % 10 '0xa' -- http://mail.python.org/mailman/listinfo/python-list
Re: Monitoring SSHd and web servers?
On Mar 13, 11:32 pm, Gilles Ganault <[EMAIL PROTECTED]> wrote: > I'd like to monitor connections to a remote SSH and web server. Does > someone have some code handy that would try to connect every 5mn, and > print an error if the script can't connect? from time import sleep while True: # Try to connect. May want to spawn a subprocess running a simple shell script # Handle success / failure appropriately sleep 5*60 # Sleep for 5 minutes What you monitor is up to you. At a basic level, you can see if the server is accepting connections. At a higher level, see if you can get a page or see if you can login as a specific person. At a higher level, you may want to check what is on the page or what happens when you log in. It's all up to you. -- http://mail.python.org/mailman/listinfo/python-list
Re: eval and unicode
On Mar 20, 5:20 am, Laszlo Nagy <[EMAIL PROTECTED]> wrote: > How can I specify encoding for the built-in eval function? Here is the > documentation: > > http://docs.python.org/lib/built-in-funcs.html > > It tells that the "expression" parameter is a string. But tells nothing > about the encoding. Same is true for: execfile, eval and compile. > > The basic problem: > > - expressions need to be evaluated by a program > - expressions are managed through a web based interface. The browser > supports UTF-8, the database also supports UTF-8. The user needs to be > able to enter string expressions in different languages, and store them > in the database > - expressions are for filtering emails, and the emails can contain any > character in any encoding > > I tried to use eval with/without unicode strings and it worked. Example: > > >>> eval( u'"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) == eval( '"徹底し > たコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) > True > > The above test was made on Unbuntu Linux and gnome-terminal. > gnome-terminal does support unicode. What would happen under Windows? > > I'm also confused how it is related to PEP 0263. I always get a warning > when I try to enter '"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' in a source > file without "# -*- coding: " specified. Why is it not the same for > eval? Why it is not raising an exception (or why the encoding does not > need to be specified?) > Encoding information is only useful when you are converting between bytes and unicode data. If you already have unicode data, you don't need to do any more work to get unicode data. Since a file can be in any encoding, it isn't apparent how to decode the bytes seen in that file and turn them into unicode data. That's why you need the # -*- coding magic to tell the python interpreter that the bytes it will see in the file are encoded in a specific way. Until we have a universal way to accurately find the encoding of every file in an OS, we will need that magic. Who knows? Maybe one day there will be a common file attribute system and one of the universal attributes will be the encoding of the file. But for now, we are stuck with ancient Unix and DOS conventions. When you feed your unicode data into eval(), it doesn't have any encoding or decoding work to do. -- http://mail.python.org/mailman/listinfo/python-list
Re: Change user on UNIX
On Mar 20, 4:51 am, "Giampaolo Rodola'" <[EMAIL PROTECTED]> wrote: > Hi all. > Is there any way to su or login as a different user within a python > script? I mainly need to temporarily impersonate another user to > execute a command and then come back to the original user. > I tried to google a little bit about it but I still didn't find a > solution. In the unix world, this is highly discouraged. You shouldn't have to change your user. The only user who can change roles---and who should change roles for security reasons---is root. The only reason sudo is around is for those people who really are root but who don't like logging in as root to do root work. With a very limited permission set for sudo, it is very, very easy to get full root access. $ sudo cp /bin/cp /bin/cp.old; sudo cp /bin/su /bin/cp; sudo cp - # If you want a different user to access files that another user created, that's what groups are for. You should create a common group and then share the files by assigning them to that group and setting the appropriate permissions. Yes, this is painful, and every once in a while you get files that don't have the right permissions or the group is set to the wrong group. But this is the cost of running on a system where multiple users can be doing things all at once, and the cost of trying to make sure that users can't hurt each other. Someone somewhere has to say, "You are allowed to do this much, but no more". If that's not what you need, then you need to run the process as root. It can change its user and even chroot to a jail if need be. This is how apache, for instance, works. It starts as root and spawns the server processes as the apache user. (Apache does have an interesting problem with home directories, and it has a very special solution that is very insecure. Even there, the better solution is to put all the user's files under a common group in a common folder outside of their home directories.) -- http://mail.python.org/mailman/listinfo/python-list
Re: eval and unicode
On Mar 20, 2:20 pm, Laszlo Nagy <[EMAIL PROTECTED]> wrote: > > >> >>> eval( u'"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) == eval( '"徹底し > >> たコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) > >> True > > > When you feed your unicode data into eval(), it doesn't have any > > encoding or decoding work to do. > > Yes, but what about > > eval( 'u' + '"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) > Let's take it apart, bit by bit: 'u' - A byte string with one byte, which is 117 '"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' - A byte string starting with " (34), but then continuing in an unspecified byte sequence. I don't know what encoding your terminal/file/whatnot is written in. Assuming it is in UTF-8 and not UTF-16, then it would be the UTF-8 representation of the unicode code points that follow. Before you are passing it to eval, you are concatenating them. So now you have a byte string that starts with u, then ", then something beyond 128. Now, when you are calling eval, you are passing in that byte string. This byte string, it is important to emphasize, is not text. It is text encoded in some format. Here is what my interpreter does (in a UTF-8 console): >>> u"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова" u'\u5fb9\u5e95\u3057\u305f\u30b3\u30b9\u30c8\u524a\u6e1b \xc1\xcd \u0170\u0150\xdc\xd6\xda\xd3\xc9 \u0442\u0440\u0438\u0440\u043e \u0432\u0430' The first item in the sequence is \u5fb9 -- a unicode code point. It is NOT a byte. >>> eval( '"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) '\xe5\xbe\xb9\xe5\xba\x95\xe3\x81\x97\xe3\x81\x9f \xe3\x82\xb3\xe3\x82\xb9\xe3\x83\x88\xe5\x89\x8a\xe6\xb8\x9b \xc3\x81\xc3\x8d\xc5\xb0\xc5\x90\xc3\x9c\xc3\x96\xc3\x9a \xc3\x93\xc3\x89 \xd1\x82\xd1\x80\xd0\xb8\xd1\x80\xd0\xbe \xd0\xb2\xd0\xb0' The first item in the sequence is \xe5. This IS a byte. This is NOT a unicode point. It doesn't represent anything except what you want it to represent. >>> eval( 'u"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) u'\xe5\xbe\xb9\xe5\xba\x95\xe3\x81\x97\xe3\x81\x9f \xe3\x82\xb3\xe3\x82\xb9\xe3\x83\x88\xe5\x89\x8a\xe6\xb8\x9b \xc3\x81\xc3\x8d\xc5\xb0\xc5\x90\xc3\x9c\xc3\x96\xc3\x9a \xc3\x93\xc3\x89 \xd1\x82\xd1\x80\xd0\xb8\xd1\x80\xd0\xbe \xd0\xb2\xd0\xb0' The first item in the sequence is \xe5. This is NOT a byte. This is a unicode point-- LATIN SMALL LETTER A WITH RING ABOVE. >>> eval( u'u"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова"' ) u'\u5fb9\u5e95\u3057\u305f\u30b3\u30b9\u30c8\u524a\u6e1b \xc1\xcd \u0170\u0150\xdc\xd6\xda\xd3\xc9 \u0442\u0440\u0438\u0440\u043e \u0432\u0430' The first item in the sequence is \u5fb9, which is a unicode point. In the Python program file proper, if you have your encoding setup properly, the expression u"徹底したコスト削減 ÁÍŰŐÜÖÚÓÉ трирова" is a perfectly valid expression. What happens is the Python interpreter reads in that string of bytes between the quotes, interprets them to unicode based on the encoding you already specified, and creates a unicode object to represent that. eval doesn't muck with encodings. I'll try to address your points below in the context of what I just wrote. > The passed expression is not unicode. It is a "normal" string. A > sequence of bytes. Yes. > It will be evaluated by eval, and eval should know > how to decode the byte sequence. You think eval is smarter than it is. > Same way as the interpreter need to > know the encoding of the file when it sees the u"徹底したコスト削減 > ÁÍŰŐÜÖÚÓÉ трирова" byte sequence in a python source file - before > creating the unicode instance, it needs to be decoded (or not, depending > on the encoding of the source). > Precisely. And it is. Before it is passed to eval/exec/whatever. > String passed to eval IS python source, and it SHOULD have an encoding > specified (well, unless it is already a unicode string, in that case > this magic is not needed). > If it had an encoding specified, YOU should have decoded it and passed in the unicode string. > Consider this: > > exec(""" > import codecs > s = u'Ű' > codecs.open("test.txt","w+",encoding="UTF8").write(s) > """) > > Facts: > > - source passed to exec is a normal string, not unicode > - the variable "s", created inside the exec() call will be a unicode > string. However, it may be Û or something else, depending on the > source encoding. E.g. ASCII encoding it is invalid and exec() should > raise a SyntaxError like: > > SyntaxError: Non-ASCII character '\xc5' in file c:\temp\aaa\test.py on > line 1, but no encoding declared; > seehttp://www.python.org/peps/pep-0263.htmlfor details > > Well at least this is what I think. If I'm not right then please explain > why. > If you want to know what happens, you have to try it. Here's what happens (again, in my UTF-8 terminal): >>> exec(""" ... import codecs ... s = u'Ű' ... codecs.open("test.txt","w+",encoding="UTF8").write(s) ... """) >>> s u'\xc5\xb0' >>> print s Ű >>> file('test.txt').read() '\xc3\x85\xc2\xb0' >>> print file('test.txt').read() Ű Note that s is a unicode string with 2 unicode code points. Note that the file has 4 bytes--since it is that 2-code sequence encoded in UTF-8, and both
Re: eval and unicode
On Mar 21, 1:54 am, Laszlo Nagy <[EMAIL PROTECTED]> wrote: > >>> eval( "# -*- coding: latin2 -*-\n" + expr) > u'\u0170' # You can specify the encoding for eval, that is cool. > I didn't think of that. That's pretty cool. > I hope it is clear now. Inside eval, an unicode object was created from > a binary string. I just discovered that PEP 0263 can be used to specify > source encoding for eval. But still there is a problem: eval should not > assume that the expression is in any particular encoding. When it sees > something like '\xdb' then it should raise a SyntaxError - same error > that you should get when running a .py file containing the same expression: > > >>> file('test.py','wb+').write(expr + "\n") > >>> ^D > [EMAIL PROTECTED]:~$ python test.py > File "test.py", line 1 > SyntaxError: Non-ASCII character '\xdb' in file test.py on line 1, but > no encoding declared; seehttp://www.python.org/peps/pep-0263.htmlfor > details > > Otherwise the interpretation of the expression will be ambiguous. If > there is any good reason why eval assumed a particular encoding in the > above example? > I'm not sure, but being in a terminal session means a lot can be inferred about what encoding a stream of bytes is in. I don't know off the top of my head where this would be stored or how Python tries to figure it out. > > My problem is solved anyway. Anytime I need to eval an expression, I'm > going to specify the encoding manually with # -*- coding: XXX -*-. It is > good to know that it works for eval and its counterparts. And it is > unambiguous. :-) > I would personally adopt the Py3k convention and work with text as unicode and bytes as byte strings. That is, you should pass in a unicode string every time to eval, and never a byte string. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is this doable
On Mar 21, 4:48 am, fkallgren <[EMAIL PROTECTED]> wrote: > Hi. > > I have a little problem. I have a script that is in the scheduler > (win32). But every now and then I update this script and I dont want > to go to every computer and update it. So now I want the program to 1) > check for new version of the script, 2) if there is a new version, > copy that verision from server to local drive, 3) shutdown the program > and start it up again as the new version. > > The problem is that I can't run this script directly from server so it > have to run it locally. > > Anyone having any bright ideas?? > Allow me to differ with all of the previous posts... Why don't you setup an SVN repository (or Git or DARCS, etc...) that has the current version you want to run? Then, execute the script in two steps: 1) Update the local copy from the repository 2) Run the script The only hard part is writing the script to do the above two things. Of course, that can be done with a python script (but if you were in the Unix world, I would suggest a shell script.) Or you can separate out the two steps. Update the SVN version once a day, or once and hour, or something like that. In general, my gut says to avoid writing new code when you can get away with it. -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib leaves connections/sockets waiting. BIG problem!!!
On Mar 24, 6:57 am, binaryj <[EMAIL PROTECTED]> wrote: > hi i am using urllib2 to do some automated web thing. > basically i hit on sites and check the price what they are offering > for their product and then decide if i want to lower or increase my > pricing.so in short i have to hit hundreds of sites! > > for the problem: > = > i run 20 threads all do the same stuff. (hit and run :) ) > after around 10-15 hits(per thread) hits the thread does nothing. it > freezes. slowely but STEADILY all the threads end up with the same > fate :( > > i did some netstat and found out that the connecton(sockets) the > program had opened are waiting the CLOSE_WAIT state !! > > netstat -t > tcp 1 0 192.168.1.2:4882 host-blabla:www > CLOSE_WAIT > tcp 1 0 192.168.1.2:4884 host-blabla:www > CLOSE_WAIT > tcp 1 0 192.168.1.2:4375 host-blabla:www > CLOSE_WAIT > > OUTPUT OF PROGRAM: > THREAD: #Thread-2 getting price from webi-d 7511975 DONE !!! > THREAD: #Thread-1 getting price from webi-d 4449152 DONE !!! > THREAD: #Thread-2 getting price from webi-d 7466091 DONE !!! > THREAD: #Thread-1 getting price from webi-d 8641914 DONE !!! > THREAD: #Thread-2 getting price from webi-d 7745289 DONE !!! > THREAD: #Thread-1 getting price from webi-d 6032442 DONE !!! > THREAD: #Thread-2 getting price from webi-d 8149873 DONE !!! > no-price-on-page error > THREAD: #Thread-1 getting price from webi-d 5842934 DONE !!! > no-price-on-page error > THREAD: #Thread-2 getting price from webi-d 3385778 DONE !!! > THREAD: #Thread-1 getting price from webi-d 4610122 DONE !!! > THREAD: #Thread-2 getting price from webi-d 8641536 DONE !!! > THREAD: #Thread-1 getting price from webi-d 4219935 DONE !!! > -and thats it, it freezes. i have waited 1hr the sockets have > not changed their states! :( > > please help :) I think we'll need more details before being able to assess what is wrong. Can you supply some sample code that has the same bug? -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding search queries, semantics, and "Meaning" ...aren't we all looking for meaning?
On Dec 30, 12:35 pm, 5lvqbw...@sneakemail.com wrote: > I have Section 4.4.1 of SICP rattling around in my head (database > queries), and I'm trying to come up with a simple dictionary-based > database in Python to represent circuit diagrams. My main confusion > isn't one of implementation, but a matter of "big thinking", > fundamentally, about the problem. Please don't suggest using a SQL > library, as I'm looking to learn to fish, so to speak, and to learn a > bit about the biology of fish. > I'm going to break rule #1 of your requirements but in an unexpected way. Rather than studying PostgreSQL, MySQL, or Oracle, why don't you crack open the topic of relational database theory and relational algebra? That should help with the "big thinking" bit in the same way understanding 1+1 helps you understand how to add any two numbers together. No, SQL is not the be-all-end-all of relational theory. Yes, it does a pretty dang good job at it, though, which is why it is still around. > I've subclassed dict to hdict ("hashable dict") and rewritten the > __hash__ function so I can include a dict into a set. Thanks to a > previous poster here for providing that suggestion. > > A circuit component looks like this for example: > > comp1 = hdict(value=1e-6, footprint='0402', vendor='digikey') > comp2 = hdict(value=10e3, footprint='0603', vendor='mouser') > etc, etc. > > The database holds the values like this: > db = dict() # normal dict > db['value'] = ([1e-6, 10e3], [comp1, comp2]) #ordered set for fast > lookup/insertion > db['footprint'] = {'0402':set[comp1], '0603':comp2} # unordered uses > normal dict for fast lookup > db['vendor'] = {'digikey':[comp1], 'mouser':[comp2]} > > So basically the keys are the component parameters, and the values is > the list of components with that value. Stuff that is comparable is > ordered; stuff that is discrete is not ordered, using either 2-tuples > or dicts, respectively. > > This allows extremely fast lookup of components based on their > properties, with O(1) performance for non-ordered stuff, and O(log n) > performance for ordered stuff (using bisect methods). The set > operations are extremely fast, so I can do AND and OR operations on > compound queries of this nature without worry. > > I need this speed not so much for selecting components when the user > types in a query, but for when the mouse is hovering over the > schematic and I need things to light up underneath, if the GUI is > generating hundreds of mouse messages per second, and for inspector > windows to appear quickly when you click, etc. If you have ever used > Altium, you can see how effective this is in terms of creating a good > interactive user experience. > OK, turn it around in your head. Consider the indexes you built above as yet another table. Consider what a table or a database really is. When you see how they are all really the same thing, either you've mastered relational algebra of you've seen the big picture. Kind of like the cons cell being enough to describe any data structure in the universe, a table is enough to describe everything in the relational world. > My question is what happens when I choose to search for NOT > footprint='0402'. > > Should this return a blank list? This happens by default., and is in > fact true: a blank list satisfies "not anything" actually. > Should this return everything that is NOT footprint 0402 (ie returns > 0603 components)? This *strongly implies* a pre-selection of *all* > objects before performing the negating function, or of looking at the > ordering of other queries in the overall search term, and then > applying NOT to the results of another query. > > But I'm suspicious of a brute force preselection of all objects > whenever I see a NOT, and anyway it messes up the clean query/combiner > method of search I'm doing, and it requires an implied sequence of > search, where I'm pretty sure it should not rely on sequencing. Even > though this is single threaded, etc., the semantics of the query > should not rely on ordering of the search term: > > footprint='0402' and NOT vendor='mouser' should return the same as > NOT vendor='mouser' and footprint='0402'. > > So this is my philosophical quandary. I'm not sure what the correct > thing is. In SICP they are using nondeterministic stuff which I don't > quite get, so it's hard to follow. Also they are not using > dictionaries and hashes, so I'm not sure if their generate-and-test > method would work here anyway. Generate-and-test seems extremely > inefficient. > > Can a wise guru please enlighten me? > (I don't think I qualify as a guru, but I think I see how I can help.) In a typical SQL database, when you type in "SELECT foo FROM bar WHERE baz='bo'", you are not writing a program, at least not in the sense of Python or C or Java or Perl where you give instructions on HOW to run the program. You are writing a program in the sense of Lisp or Scheme or Haskell in that you are giving instructions on
Re: need help with list/variables
On Dec 30, 11:41 am, 5lvqbw...@sneakemail.com wrote: > > >>> conc = lambda x,y: x[:] + y # concatenate 2 lists without side effects > >>> mylist = ['something\n', 'another something\n', 'something again\n'] > >>> myvar = reduce(conc, mylist) > >>> print myvar > "conc"? "side effects"? Missing Lisp much? ;-) Let's try to Pythonize your lisp. One: Assigning a lambda to a variable? Just use def. It's pretty much the same thing. >>> def conc(x,y): return x[:]+y Two: I don't think x+y affects x at all when x and y are lists. (Is that a lispism?) So x[:]+y has an unnecessary array copy. >>> def conc(x,y): return x+y Three: Python may still be slow at string concatenation. (Perl is fast.) Rather than concatenating one at a time, it's better, I understand, to build up a list and then join() them together. The other posters got the right answer in the pythonic way. -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding search queries, semantics, and "Meaning" ...aren't we all looking for meaning?
On Dec 30 2008, 3:25 pm, 5lvqbw...@sneakemail.com wrote: > > In a typical SQL database, when you type in "SELECT foo FROM bar WHERE > > baz='bo'", you are not writing a program, at least not in the sense of > > Python or C or Java or Perl where you give instructions on HOW to run > > the program. You are writing a program in the sense of Lisp or Scheme > > or Haskell in that you are giving instructions on WHAT the program is. > > I've gotten a strong inkling that parsing a query yields new code, > (lambdas) that are created on the fly to do the search. > More on this at the very end. Just smile to know that you're very close. > > course. Instead, it transforms the query (the WHAT) into a set of > > procedures that describe HOW to get the result. > > For now I'm not parsing actual text queries... my real "search query" > is coded directly in python like this: > p1 = lambda: db.search_leaf('x location', 'lte', 5) > p2 = lambda: db.search_leaf('footprint', 'eq', '0603') > p3 = lambda: db.search(db.AND, p1, p2) > > p4 = lambda: db.search_leaf('x location', 'gte', 19) > p5 = lambda: db.search_leaf('footprint', 'eq', '0402') > p6 = lambda: db.search(db.AND, p1, p2) > > fc = db.search(db.OR, p3, p4) > > this particular example doesn't necessarily make any sense, but in > effect I'm trying to string together lambda functions which are > created explicitly for the individual query, then strung together with > combiner functions. > If only compilers were so simple... Again, you're writing the "HOW" when you're doing what you're doing above, when you really want to write the "WHAT". Oh, and BTW, lambdas are just an expression to generate a function quickly. You're confusing the python lambda expression with lambdas the theoretical concepts. Lambdas the theoretical concept are really Python's callable objects. Python just expresses them in a very weird way that is seemingly unrelated to lambda the theoretical concept (but really is). > > Oh, by the way, this step is nondeterministic. Why? Well, no one can > > really say what the BEST way to run any sufficiently complicated > > program is. We can point out good ways and bad ways, but not the best > > way. It's like the travelling salesman problem in a way. > > The nondeterministic stuff... wow, I've come across (call/cc...), > (require...), and different variants of this, both in sicp, teach > yourself scheme, the plt docs, other places, etc., but it still eludes > me. I'm afraid that unless I understand it, I'll wind up creating > some type of cargo-cult mimcry of a database without doing it right > (http://en.wikipedia.org/wiki/Cargo_cult) > Sorry, I got confused on the meaning of non-deterministic programming. I forgot that this was a specific thing in the Scheme world. Yes, you've got to understand this. It makes writing a compiler much, much easier, almost trivial. > > programmer, will be infinitely better for it. When you understand it, > > you will really unlock the full potential of Python and Scheme and > > whatever other language is out there because you will see how to go > > from HOW languages to WHAT languages. > > I'm under the impression python doesn't have the internal guts for > nondeterministic programming, specifcially that its lambdas are > limited to single-line expressions, but I may be wrong here. > (Recall what I mentioned about lambdas above. It applies here as well.) > Still, at the begining of the nondeterministic section of SICP > (section 4.3), it says "nondeterministic computing... is useful for > 'generate and test' applications", which almost categorically sounds > like an element-by-element search for what you're looking for. This > was my initial intention behind (prematurely?) optimizing the database > by using parameter keys instead of something like [x for x in stuff if > pred(x, val)] filtering. > Yes, you certainly can't use list comprehensions to solve your problems. They're too primitive and part of the interface is that they actually iterate through the sequence. > > Programmers who can write compilers are GOOD programmers. Programmers > > who can understand someone else's compilers are even better. > > Does incorporating a search capability in an application necessarily > mean I'm writing a search compiler? That seems overkill for the > specific case, but may be true generally. For instance, if a word > processing app allows you to search for characters with a certain > font, is that incorporating a search compiler and query language? Or > is it just brute-force filtering? > Ok, here's some big picture hand-waving. I'm going to make an assertion, and it turns out to be fundamental. I won't explain it here but I hope you'll learn what it means as time goes on. The assertion is this: All programs simply transform one program's code into another program's code. By program, I mean, a set of instructions to a computer of some sort that are meant to be actually evaluated somehow. By program's code, I mean a de
Re: Measuring bytes of packet sent from python application
On Jan 5, 2:26 pm, Kangkook Jee wrote: > I'd like to measure number of bytes sent(or recv'd) from my python > application. Does anyone have any idea how can I achieve this? > > I tried to do this by tracing some socket calls (send, sendto, sendAll) > using 'metaclass' but I could find exactly place that I can put this in. > > My application runs some number of protocols (bittorrent, xmlrpc ..) in > it and will be measured for a couple of hours. > A good universal tool on the Linux platform is tcpdump. It takes some learning, but is very useful for this kind of task. You can use a tool like ethereal to visualize the data that tcpdump gathers. -- http://mail.python.org/mailman/listinfo/python-list
Re: Measuring bytes of packet sent from python application
On Jan 5, 3:23 pm, Kangkook Jee wrote: > Jonathan Gardner wrote: > > A good universal tool on the Linux platform is tcpdump. It takes some > > learning, but is very useful for this kind of task. You can use a tool > > like ethereal to visualize the data that tcpdump gathers. > > That seems like a good solution for my issue but how can I distinguish > traffics from my application to others? > There are a variety of ways to distinguish traffic with tcpdump and ethereal. Usually you should have some idea of what ports or hosts your traffic is going to. If not, then you will have some indicator within the packets themselves. Note that tcpdump is remarkable since it can identify TCP sessions, and not just individual packets. > I'm still struggling to solve it within python process since it looks > cleaner but it doesn't seems to be easy at all. Replacing a lower-level component of a 3rd party library is difficult at best. If you can get it to work without rewriting the library, congratulations. At the very least, you'll probably have to do some low-level things within the libraries themselves. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 6, 8:13 am, sturlamolden wrote: > On Jan 6, 4:32 pm, mark wrote: > > > > Is it possible to > > switch between the custom DSL and the standard Python interpreter? > > > > - Write the DSL interpreter in Python. > There are Python modules out there that make writing a language interpreter almost trivial, provided you are familiar with tools like Bison and the theories about parsing in general. I suggest PLY, but there are other really good solution out there. If you are familiar enough with parsing and the syntax is simple enough, you can write your own parser. The syntax you describe above is really simple, so using str.split and then calling a function based on the first item is probably enough. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 6, 8:18 am, sturlamolden wrote: > On Jan 6, 4:32 pm, mark wrote: > > > I want to implement a internal DSL in Python. I would like the syntax > > as human readable as possible. > > Also beware that Python is not Lisp. You cannot define new syntax (yes > I've seen the goto joke). This isn't really true. You can, for instance, write a program (in Python) that takes your pseudo-Python and converts it into Python. This is what a number of templating libraries such as Mako do. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 6, 12:24 pm, J Kenneth King wrote: > Jonathan Gardner writes: > > On Jan 6, 8:18 am, sturlamolden wrote: > >> On Jan 6, 4:32 pm, mark wrote: > > >> > I want to implement a internal DSL in Python. I would like the syntax > >> > as human readable as possible. > > >> Also beware that Python is not Lisp. You cannot define new syntax (yes > >> I've seen the goto joke). > > > This isn't really true. You can, for instance, write a program (in > > Python) that takes your pseudo-Python and converts it into Python. > > This is what a number of templating libraries such as Mako do. > > Which is not even close to being the same. > > Lisp - the program source is also the data format > > Python - the program source is a string > > I could go on a really long rant about how the two are worlds apart, but > I'll let Google tell you if you're really interested. I get that Lisp is special because you can hack on the reader as it is reading the file in. This is strongly discouraged behavior, as far as I know, despite the number of cute hacks you can accomplish with it. But consider that this really isn't different than having a program read in the lisp-with-modification source and spitting out pure lisp, to be read by an honest-to-gosh lisp program later. If that's the case, then Lisp and Python really aren't that different in this regard, except that you don't have the option of modifying the reader as it reads in the file. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 7, 7:50 am, J Kenneth King wrote: > Jonathan Gardner writes: > > On Jan 6, 12:24 pm, J Kenneth King wrote: > >> Jonathan Gardner writes: > >> > On Jan 6, 8:18 am, sturlamolden wrote: > >> >> On Jan 6, 4:32 pm, mark wrote: > > >> >> > I want to implement a internal DSL in Python. I would like the syntax > >> >> > as human readable as possible. > > >> >> Also beware that Python is not Lisp. You cannot define new syntax (yes > >> >> I've seen the goto joke). > > >> > This isn't really true. You can, for instance, write a program (in > >> > Python) that takes your pseudo-Python and converts it into Python. > >> > This is what a number of templating libraries such as Mako do. > > >> Which is not even close to being the same. > > >> Lisp - the program source is also the data format > > >> Python - the program source is a string > > >> I could go on a really long rant about how the two are worlds apart, but > >> I'll let Google tell you if you're really interested. > > > I get that Lisp is special because you can hack on the reader as it is > > reading the file in. This is strongly discouraged behavior, as far as > > I know, despite the number of cute hacks you can accomplish with it. > > It is generally discouraged unless there's a reason for it. > > > But consider that this really isn't different than having a program > > read in the lisp-with-modification source and spitting out pure lisp, > > to be read by an honest-to-gosh lisp program later. > > > If that's the case, then Lisp and Python really aren't that different > > in this regard, except that you don't have the option of modifying the > > reader as it reads in the file. > > I think you are missing the distinction. > > Lisp expressions are also data structures. A Lisp expression can be > passed to functions and macros to be operated on before being > executed. When you're writing Lisp source, you're basically looking at > the AST on one level and when you start writing macros for your program, > you're creating a "DSL" or interface to that AST. Lisp source is > eventually expanded to a giant list that is consed by the evaluator (as > far as I understand it. I'm just getting into the compiler stuff > myself). > I think you misunderstood what I was trying to explain. Yes, you can do those wonderful things with Lisp. You can also do wonderful things with Python. Consider programs that take some text written in some other language besides Python. Those programs interpret and translate the text to Python. Then the programs feed the translations to the Python interpreter. Tada! You have a DSL in Python. No, it's not built in, nor is there any standard, but it is entirely possible and people are doing it today. That's how the variety of templating solutions work in the Python world. It's why I can write ${x +y} in Mako and get a Python program that will do the right thing. Alternatively, you can skip the Python interpreter altogether, and write your own interpreter for the language. If it's a simple language (like the original poster hinted at), this is very easy to do. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 7, 9:16 am, "Chris Mellon" wrote: > > The OP wants a Ruby-style DSL by which he means "something that lets > me write words instead of expressions". The ruby syntax is amenable to > this, python (and lisp, for that matter) syntax is not and you can't > implement that style of internal DSL in those languages. > > The answer to the OP is "you can't - use Ruby or modify your requirements". > As far as putting the code into Python, yeah, you can't put it in Python. The best you can do is store it in a string and then interpret the string with some function later on. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making a decorator a staticmethod
On Jan 8, 11:18 am, "Zac Burns" wrote: > > In my use case (not the example below) the decorator returns a > function of the form def f(self, *args, **kwargs) which makes use of > attributes on the instance self. So, it only makes sense to use the > staticmethod in the class and in the baseclass. Making this decorator > a module level function doesn't make sense here. > I don't think you should be using staticmethod in this case since you are relying on information in the class itself. This really looks like it should be a classmethod. Granted, the example you gave can be a staticmethod, but it sounds like you want to incorporate some of the information in the class. (Aside: I really can't think of any reason to use staticmethods in Python other than to organize functions into namespaces, and even then, that's what modules are for, right?) I think you need to show a better example of what it is you are trying to do. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree views - Best design practices
On Jan 8, 8:16 am, MRAB wrote: > Filip Gruszczyński wrote: > > Hi! > > > I have certain design problem, which I cannot solve elegantly. Maybe > > you know some good design patterns for this kind of tasks. > > > Task: > > > We have a model which has two kinds of objects: groups and elements. > > Groups can hold other groups (subgroups) and elements. It's a simple > > directory tree, for example. We would like to display it in a tree > > view (which sound good for this kind of model). What is more required, > > for groups and elements there are different sets of operations, which > > should be available under right click. For example for group, there > > should be operations: 'add element' and 'add group', and for element > > there should be 'change properties'. > > > Do you know any smart way to achieve this? The simplest way is to ask > > for the class and display operations accordingly. But from the first > > day with OO programming I have heard, that asking for class is wrong. > > But I can hardly see any easy and more maintainable solution for this > > problem. Could you help me with this? > > You could ask the object what the operations are. Here's an example > using strings: > > >>> class Element(object): > operations = "Element operations" > > >>> class Group(object): > operations = "Group operations" > > >>> e = Element() > >>> g = Group() > >>> > >>> e.operations > 'Element operations' > >>> g.operations > 'Group operations' When faced with this kind of scenario, I usually write boolean methods like "is_leaf" or "is_branch". class Element(object): @staticmethod def is_leaf(): return True @staticmethod def is_branch(): return False class Group(object): @staticmethod def is_leaf(): return False @staticmethod def is_branch(): return True Of course, you have to give priority to one or the other, in case an object thinks it is both. if thing.is_branch(): # Treat it like a branch elif thing.is_leaf(): # Treat it like a leaf I believe this is a simpler method than checking a single attribute for a name. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree views - Best design practices
On Jan 8, 1:00 pm, "Filip Gruszczyński" wrote: > > Is it really any better than asking for class? I mean, if I need to > add another class to a hierarchy which behaves differently, will it be > more flexible (actually you have to add another method to every class > and check for in the gui). I believe it's just the same as asking for > the class, but we hide it under static methods. It's no different > though. > Yes. There is a difference between the interface of an object (namely, what methods and attributes it has and what their semantic meaning is) and the class of an object (what methods and attributes it has and how they are implemented.) In general, you shouldn't be asking about an object's class. Down the road, you may want to use an object that isn't of the same class but does support the interface. Consider how the file object is used in Python. Pretty much every place you can use a file object you can use a StringIO, right? That's because StringIO supports the file interface while it isn't a file. You may want to read up on 'duck typing' to get a better sense of why this is important. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree views - Best design practices
On Jan 8, 1:00 pm, "Filip Gruszczyński" wrote: > > Is it really any better than asking for class? I mean, if I need to > add another class to a hierarchy which behaves differently, will it be > more flexible (actually you have to add another method to every class > and check for in the gui). I believe it's just the same as asking for > the class, but we hide it under static methods. It's no different > though. > One additional note: Given that the interface and class of an object are two, orthogonal and independent things, how do you tell what interfaces an object supports? There are a variety of methods. I can break them down into 3. (1) The user of the object keeps track of which classes support which interfaces. This is bad because you can't anticipate new classes properly. Sometimes it is necessary when the other two options aren't feasible. (2) The implementor of the object provides information on what interfaces it supports through a method or attribute of some sort. This is bad because there may be new interfaces that come into existence that the object does support but the implementor doesn't know about it and so the object says it doesn't support the interface. (3) Some 3rd Party registration of interfaces and classes keeps track of which classes support which interfaces, and vice-versa. When you add a new interface, you have to list all the existing classes that also support that interface. When you add a new class, you list all the existing interfaces that it supports. This is just plain hard to do, of course. None of these solutions are perfect, of course. Duck typing tries to solve this problem with option (4): Nobody really keeps track of interfaces at all, and you just kind of wing it hoping for the best. This solution is also far from perfect, but it suggests that you never look at the class of an object, or really, even its interface. You just start using it. So my solution is the "just use it" bit. All the user really needs to know is "Are you a leaf or a branch?" And the objects simply have to answer that question. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tree views - Best design practices
On Jan 8, 1:50 pm, "Filip Gruszczyński" wrote: > > But I am looking for a different type of flexibility. I would like to > be able to add more classes to my hierarchy and not have to change my > code in many places when I add new class to the hierarchy. If I have > to change every class in the hierarchy because I add new class, then > it's not something I would like to do. And idea how I can avoid this? > I don't understand why you have to change other classes when you add a new class under duck typing. The whole point of duck typing is you don't look at the class or the interface of an object so you don't have to keep track of it. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 8, 7:25 am, J Kenneth King wrote: > Jonathan Gardner writes: > > It seems we're defining "DSL" in two different ways. > > You can't write a DSL in Python because you can't change the syntax and > you don't have macros. > > You can write a compiler in Python that will compile your "DSL." > Yes, that's what I'm saying. You can get the same results even thought you can't manipulate the Python language itself as it's compiling Python because you can feed it Python code that you've generated. > As another poster mentioned, eventually PyPy will be done and then > you'll get more of an "in-Python" DSL. Of course, such a language wouldn't be Python anymore because Python doesn't have such features. -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python
On Jan 8, 8:03 am, Kay Schluehr wrote: > On 8 Jan., 16:25, J Kenneth King wrote: > > > As another poster mentioned, eventually PyPy will be done and then > > you'll get more of an "in-Python" DSL. > > May I ask why you consider it as important that the interpreter is > written in Python? I see no connection between PyPy and syntactical > Python extensions and the latter isn't an objective of PyPy. You can > write Python extensions with virtually any Python aware parser. > M.A.Lemburg already mentioned PLY and PLY is used for Cython. Then > there is ANTLR which provides a Python grammar. I also know about two > other Python aware parsers. One of them was written by myself. If you're going to manipulate the Python compiler/interpreter from the Python program itself, it's only reasonable that the Python compiler/ interpreter be written in Python so that it can be manipulated. If you haven't already made it through SICP, you really should. It will help you understand why being able to write a language in itself is a big deal and why having the language around to be manipulated from within the language is very useful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Measuring bytes of packet sent from python application
On Jan 5, 6:08 pm, Grant Edwards wrote: > On 2009-01-05, Kangkook Jee wrote: > > I'm still struggling to solve it within python process since > > it looks cleaner but it doesn't seems to be easy at all. > > I don't why adding bunches of code to your app would be > "cleaner" than gathering data using external programs. > Those external programs may not be available on all the platforms he might want to run in. Or he may have some specific requirements that those tools can't satisfy. Software reuse is wonderful when it works. But it doesn't work all the time, even though it probably should. -- http://mail.python.org/mailman/listinfo/python-list
Re: Question
On Oct 13, 9:31 pm, "Aditi Meher" <[EMAIL PROTECTED]> wrote: > > I am connecting database using python,and i am inserting some data into it. > e.g.name and roll nos(some 100 records are stored) > > My question is "I want to display 10 records at a time and want to > store remaining records into buffer and after displaying 10 records > again i want to display next 10 records." > I have no idea what platform you are using to display the data. Is it a GUI? A Web app? A command-line utility? I won't give you code, but I'll give you a general description. Most SQL-based databases allow you to write a clause for your SELECT statement such as "LIMIT 10". You'll want to read the documentation for the database and the connection library to figure out how best to do this. If you want to load all the records and only show ten, this snippet may come in handy: results[10:20] # Get a list of results 10, 11, 12, 13, ..., 18, 19. 0-based, of course. As for how to display the data, that depends on your platform. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I count the distance between strings in a list?
On Feb 23, 9:35 pm, collin wrote: > For example, if I were to have the code > > randomlist = ["1", "2", "3", "4"] > > And I want to count the distance between strings "1" and "4" which is > 3, what command can I use to do this? You'd have to get the indexes of the two items and subtract them. There is an 'index' method on the List object for that. -- http://mail.python.org/mailman/listinfo/python-list
Re: code challenge: generate minimal expressions using only digits 1,2,3
On Feb 20, 6:31 am, Trip Technician wrote: > anyone interested in looking at the following problem. > > we are trying to express numbers as minimal expressions using only the > digits one two and three, with conventional arithmetic. so for > instance > > 33 = 2^(3+2)+1 = 3^3+(3*2) > > are both minimal, using 4 digits but > > 33 = ((3+2)*2+1)*3 > > using 5 is not. > > I have tried coding a function to return the minimal representation > for any integer, but haven't cracked it so far. The naive first > attempt is to generate lots of random strings, eval() them and sort by > size and value. this is inelegant and slow. > > I have a dim intuition that it could be done with a very clever bit of > recursion, but the exact form so far eludes me. Actually, representing 33 has an even shorter answer: '33' There is actually a really easy solution for this. What you are really doing is finding the shortest path from point A (the expression '') to another expression that evaluates to the target number. >From each point, you can take steps that (a) add a digit to the end or (b) add an operator---but only if it makes sense. Since operators don't count, those steps don't add anything to the distance, while the digits do. What you do is you start walking the map from point A to some mysterious point that evaluates to the result you want. Actually, you send out walkers in all directions, and tell only the walkers that have taken the fewest steps to take another step. Once a walker gets to an expression with the result, you have your answer since he is the first walker to get there. When a walker gets to an expression that has already been visited, you can tell that walker to go home since his path was no good. When a walker gets to an expression that evaluates to a value you have already seen, that walker too should go home since he has just found a longer path to the same result. So you have a set of walkers, and each walker has the expression they used to get to where they are and how many steps they took to get there. You also have a set of values you have seen before and thus values that if a walker finds you are no longer interested in. For each iteration, you take each surviving walker and spawn a new walker that takes a step in each possible direction. Then you check if any of those walkers found the value you are looking for. If so, you've found the answer. If they hit a value you've already seen, you drop that walker from the set. The only hanging point is parentheses. What you can do here is instead of building a linear expression, build a tree expression that shows the operations and the values they operate. It should be trivial to calculate all the different new trees that are one digit longer than a previous tree. -- http://mail.python.org/mailman/listinfo/python-list
Re: suggestion on a complicated inter-process communication
On Apr 27, 8:59 pm, Way wrote: > Hello friends, > > I have a little messy situation on IPC. Please if you can, give me > some suggestion on how to implement. Thanks a lot! > > -> denotes create > > MainProcess -> Process1 -> Process3 (from os.system) > | > -> Process2 (from os.system) -> Process4 (from > os.system) ->Process5 > > I would like to make the communication between Process1 and Process5. > Process1 needs Process5's output to provide argument to generate > Process3, and in turn Process5 needs to wait Process3 finished. > > Thank you very much if you can give a hint. Abstraction should resolve this. What I mean is that Process2 shouldn't be defined in terms of what it actually does, but what it appears to do. If you look at each process and think only what its child processes do and what its parent process expects it to do, then your job should get a lot simpler. Process1 expects Process2 to deliver a set of parameters to spawn Process3, and then it will wait until Process3 terminates. Looking at it this way, questions come to mind: Why can't Process2 run Process3 itself? It's unusual to have one process tell another process what to do when it can simply do it itself. By the way, I've never seen a time when this kind of design is necessary. There are other ways around your problem than spawning a bunch of processes. Each process should really be independent of all the other processes, doing only a very well-specified task in a well- specified way. The only time I could think of doing something like this is when you're getting a webserver setup and Process5 needs to communicate with Process3 to render the page or something like that. But even in that case, each process is really independently defined and implemented. -- http://mail.python.org/mailman/listinfo/python-list
Re: What replaces `` in py3k?
On May 21, 10:45 am, bukzor <[EMAIL PROTECTED]> wrote: > What are backticks going to be translated into? repr -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does python not have a mechanism for data hiding?
On Jun 10, 11:21 am, "Russ P." <[EMAIL PROTECTED]> wrote: > I took a risk in choosing Python, and I would > feel better about it if Python would move up to the next level with > more advanced features such as (optional) static typing and private > declarations. But every time I propose something like that, I get all > kinds of flak from people here who do their hacking and care little > about anyone else's needs. Let me share my personal insight. I used Python for a mission-critical application that needed, in effect, almost 100% uptime with superior throughput. In other words, it was a very fine piece of art that needed to be precise and correct. In the end, Python delivered, under budget, under schedule, and with superbly low maintenance costs (practically 0 compared to other systems written in Java and C). I didn't have to use any of the features you mentioned, and I can't imagine why you would need them. In fact, having them in the language would encourage others to use them and make my software less reliable. You may think we are all a bunch of hackers who are too stupid to understand what you are saying, but that is your loss. Now, let me try to explain something that perhaps the previous 166 post may not have thoroughly explained. If I am duplicating what everyone else has already said, then it's my own fault. Short answer: You don't need these features in Python. You do need to use the right tools for the right tasks. Long answer: Who cares what the type of an object is? Only the machine. Being able to tell, in advance, what the type of a variable is is a premature optimization. Tools like psyco prove that computers (really, programmers) nowadays are smart enough to figure things out the right way without any hints from the developer. Static typing is no longer necessary in today's world. Who cares about private declarations, or interface declarations at all? It is only a message to the developers. If you have a problem with your users doing the right thing, that is a social problem, not a technical one, and the solution is social, not technical. Yes, it is work, but it is not coding---it is explaining to other living, breathing human beings how to do a specific task, which is what you should have been doing from the start. When all you have is a hammer, the world seems full of nails. Think about that. You have more tools than Python to solve these problems, and Python will never be the panacea you wish it was. The panacea is you, putting the right tools to use for the right tasks. -- http://mail.python.org/mailman/listinfo/python-list
Re: go to specific line in text file
On Jun 17, 3:10 am, Patrick David <[EMAIL PROTECTED]> wrote: > > I am searching for a way to jump to a specific line in a text file, let's > say to line no. 9000. > Is there any method like file.seek() which leads me to a given line instead > of a given byte? > As others have said, no. But if you're wondering how other file formats do this, like BDB or PostgreSQL data files, which absolutely have to jump to the magical spot in the file where the data is, they keep an index at the beginning of the file that points to what byte the data they are looking for is in. So if it's really important to be able to do this, consider that. -- http://mail.python.org/mailman/listinfo/python-list
Re: ask for a RE pattern to match TABLE in html
On Jun 26, 11:07 am, Grant Edwards <[EMAIL PROTECTED]> wrote: > On 2008-06-26, Stefan Behnel <[EMAIL PROTECTED]> wrote: > > > > Why not use an HTML parser instead? > > > > Stating it differently: in order to correctly recognize HTML > tags, you must use an HTML parser. Trying to write an HTML > parser in a single RE is probably not practical. > s/practical/possible It isn't *possible* to grok HTML with regular expressions. Individual tags--yes. But not a full element where nesting is possible. At least not properly. Maybe we need some notes on the limits of regular expressions in the re documentation for people who haven't taken the computer science courses on parsing and grammars. Then we could explain the necessity of real parsers and grammars, at least in layman's terms. -- http://mail.python.org/mailman/listinfo/python-list
Re: ask for a RE pattern to match TABLE in html
On Jun 26, 3:22 pm, MRAB <[EMAIL PROTECTED]> wrote: > Try something like: > > re.compile(r'.*?', re.DOTALL) So you would pick up strings like "foo"? I doubt that is what oyster wants. -- http://mail.python.org/mailman/listinfo/python-list
Re: ask for a RE pattern to match TABLE in html
On Jun 27, 10:32 am, "David C. Ullrich" <[EMAIL PROTECTED]> wrote: > (ii) The regexes in languages like Python and Perl include > features that are not part of the formal CS notion of > "regular expression". Do they include something that > does allow parsing nested delimiters properly? > In perl, there are some pretty wild extensions to the regex syntax, features that make it much more than a regular expression engine. Yes, it is possible to match parentheses and other nested structures (such as HTML), and the regex to do so isn't incredibly difficult. Note that Python doesn't support this extension. See http://www.perl.com/pub/a/2003/08/21/perlcookbook.html -- http://mail.python.org/mailman/listinfo/python-list
Re: x, = y (???)
On Jul 17, 12:55 pm, kj <[EMAIL PROTECTED]> wrote: > I still don't get it. If we write > > y = 'Y' > x, = y > > what's the difference now between x and y? And if there's no > difference, what's the point of performing such "unpacking"? > Try: y = "abc" x, = y You were unpacking y into ('Y',) -- http://mail.python.org/mailman/listinfo/python-list
Re: x, = y (???)
On Jul 17, 12:55 pm, kj <[EMAIL PROTECTED]> wrote: > what's the point of performing such "unpacking"? record = [name, address, telephone] ... name, address, telephone = record -- http://mail.python.org/mailman/listinfo/python-list