Re: sufficiently pythonic code for testing type of function
On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > > Now the real question : what if the object is not an instance of any of > the types, but still support the expected interface ? > one possible answer: Use ZopeInterfaces (and ask objects 'do you implement interface X' rather than 'are you type Y') Not sure what options you have when dealing with builtin data types however. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: sufficiently pythonic code for testing type of function
On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > A.T.Hofkamp wrote: >> On 2006-10-11, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: >>> Now the real question : what if the object is not an instance of any of >>> the types, but still support the expected interface ? >>> >> >> one possible answer: Use ZopeInterfaces >> (and ask objects 'do you implement interface X' rather than 'are you type Y') >> >> Not sure what options you have when dealing with builtin data types however. > > This was mostly a rethorical question... > And even for rethorical questions, Python already provides a solution.. :-) Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: python under the hood
On 2006-10-20, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello. > > Is there any good information about python's internals and semantic? I > mean "under the hood". Not exactly 'under the hood', but very close is the Python/C API. You can learn a great deal by reading the Extending&Embedding manual as well as the Python/C API reference manual. > Unfortynatly, I'm too lazy to read python's source code :) (and I > guess, it's not very easy :) ) After the above, some parts should be familiar. The functions in the Python/C API are also being used inside Python. I also found the source code pretty understandable, although I read only a small part of it. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Import Issue
On 2006-06-02, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi all, > After doing import file i am updating that file. Later i am > accessing a dictionary contained in that > file. Eventhough changes are reflected in the file... When i > access a dictionary those changes are > not there. I believe that it is accessing from the object > file that is created when i did import at > the start of the script. I will be kind enough if somebody > suggest solution to this problem. The easiest suggestion I can make is "don't do that". An import statement is intended for importing static Python code, not for importing dynamic data such as dictionaries that you update while running. Your application is much easier to understand if you keep Python code and data in seperate files. For this, there are several options: A. If you stick to Python syntax of your data, you can load such files with 'open' and 'read', followed by 'eval' fp = open('datafile','r') datatext = fp.read() fp.close() print eval datatext B. If you don't care about accessing the data outside Python, you can use the pickle module to load and save the data. (please read http://docs.python.org/lib/module-pickle.html for details). C. Last but not least, you can define your own data format, and write custom load and save routines for accessing the data file. This approach is highly flexible, but it does cost effort to write the routines. Hope this answer your question, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: XML, JSON, or what?
On 2006-06-08, Frank Millman <[EMAIL PROTECTED]> wrote: > I would rather make a decision now, otherwise I will have a lot of > changes to make later on. Does anyone have any recommendations? Did you consider XMPP? With XMPP you create XML streams between your server and the client. XMPP is an open standard (by the IETF). Jabber (jabber.org) is based on the XMPP standard. They develop protocols on XMPP for all kinds of data exchange. Currently, it is mainly used for chatting, but we use it in-house as publish/subscribe to control some machines too. They do a lot of thinking about scalabilty, privacy, authentication, and all that other stuff you need to do when you put some service on the internet. They also have protocols for using a GUI interactively (much like the wizards of Win* do, you get one page of things to decide, you press 'next', you get the next page, etc). Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: generating a tree-like structure
On 2007-05-31, Thorsten Kampe <[EMAIL PROTECTED]> wrote: > * (31 May 2007 12:15:48 -0700) >> On May 31, 12:44 pm, Thorsten Kampe <[EMAIL PROTECTED]> wrote: >> > This is a fairly general question: is there some kind of module or >> > framework that allows building a tree like structure from certain kind >> > of data? >> > >> > To be specific: I have a program that dumps the content of a LDAP >> > directory including all properties and values and groups the result >> > from the LDAP search by objClass. >> > >> > Now I was thinking: would it be possible to generate from the totally >> > unordered output that the LDAP server gives me, a tree like >> > representation that displays the hierarchy (omitting the values or >> > even properties if necessary)? >> > >> > It should be a textual representation of what you see in GUI programs >> > like "LDAP Administrator" but the output should be represented like >> > the "tree" program in Linux or Windows "tree.com". >> >> I think you might be able to use ElementTree. The website for the >> module claims it can be used for hierarchical data structures: >> http://effbot.org/zone/element-index.htm >> >> Did you look at any of the Python LDAP tools? They might be useful >> too. See some of the links below: >> http://linuxjournal.com/article/6988 >> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303336 >> >> Hopefully they'll give some guidance. I've not used LDAP myself as of >> yet. > > I already have the LDAP output part working - with python-ldap under > Cygwin - and I generate HMTL output with markup.py. Pretty simple. But > a tree structure output would be even prettier... I would probably generate a DOT file to get a 2D visualization. DOT is part of Graphviz (graphviz.org), and there are quite a few graphviz front-ends available in Python to make DOT generation easier (pydot, yapgvb, and probably a few others). Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: interesting take on python OO
On 2007-06-01, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > > FWIW, I still don't get why people insist on believing that C++ and Java > have anything to do with OO !-) > > (and yes, it's friday...) Good marketing. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: subexpressions
On 2007-06-01, Sergey Dorofeev <[EMAIL PROTECTED]> wrote: > Hello all! > > Please help, is there way to use sub-expressions in lambda? > For example, if I want to calculate sin(x^2)+cos(x^2) I must code: > lambda x: sin(x*x)+cos(x*x) > How to make x*x to be evaluated once? lambda x: (lambda y: sin(y) + cos(y))(x*x) Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Too many 'self' in python.That's a big flaw in this language.
On 2007-06-27, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > HI > I'm currently using Python. I find that a instance variable must > confined with self, > for example: > class a: > def __init__(self): > self.aa=10 > def bb(self): > print self.aa # See .if in c++,I could use aa to change that > variable c++ is a much more static language (for example, you cannot create new fields in your class at run time), so it can decide in advance what you mean. In other words, it is a cost you pay for the increased flexibility. You may not be using that flexibility, but it is there, and people use it. > That's a big inconvenience in coding ,especially when you have lot of > variable I have switched from c++ to Python several years ago, and was surprised about having to explicitly write 'self' each time. However, I never considered it "a big inconvenience". As years went by I have come to like the explicit notation in Python. > Or any way to make programmer's life easier? Others have already pointed out that leaving out 'self' is more bad than good. I think they are right. In the past I also thought that Python was badly designed, and until now, in the end it appeared that I was always in error. [off-topic: I think that again now with the default implementation of the object.__eq__ and object.__hash__ methods. I believe these methods should not exist until the programmer explicitly defines them with a suitable notion of equivalence. Anybody have a good argument against that? :-) ] Another suggestion may be to look at your code again, and check whether all self's are really needed. In other words, can you improve your code by reducing use of instance variables? In Python, The "a=b" statement is extremely cheap, because you don't copy data. Exploit that feature. An alternative may be to copy a self variable into a local variable one and use the local variable in the method. Another option may be to collect results in a local variable first and then assign it to a self.X variable. If you really have a lot of variables, are you sure that they should all be seperate (flat) variables in one class, ie would it be possible to merge some of them together in another object and have more structure in the variables? (classes are much cheaper in Python than in c++ w.r.t. amount of code) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)
On 2007-06-27, Alex Martelli <[EMAIL PROTECTED]> wrote: > A.T.Hofkamp <[EMAIL PROTECTED]> wrote: > >> I think that again now with the default implementation of the >> object.__eq__ and object.__hash__ methods. I believe these methods should >> not exist until the programmer explicitly defines them with a suitable >> notion of equivalence. >> >> Anybody have a good argument against that? :-) > > It's very common and practical (though not ideologically pure!) to want > each instance of a class to "stand for itself", be equal only to itself: > this lets me place instances in a set, etc, without fuss. Convenience is the big counter argument, and I have thought about that. I concluded that the convenience advantage is not big enough, and the problem seems to be what "itself" exactly means. In object oriented programming, objects are representations of values, and the system shouldn't care about how many instances there are of some value, just like numbers in math. Every instance with a certain value is the same as every other instance with the same value. You can also see this in the singleton concept. The fact that it is a pattern implies that it is special, something not delivered by default in object oriented programming. This object-oriented notion of "itself" is not what Python delivers. Python 2.4.4 (#1, Dec 15 2006, 13:51:44) [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class Car(object): ... def __init__(self, number): ... self.number = number ... def __repr__(self): ... return "Car(%r)" % self.number ... >>> 12345 == 12345 True >>> Car(123) == Car(123) False So in Python, the default equivalence notion for numbers is based on values, and the default equivalence notion for objects assumes singleton objects which is weird from an object oriented point of view. Therefore, I concluded that we are better off without a default __eq__ . The default existence of __hash__ gives other nasty surprises: >>> class Car2(object): ...def __init__(self, number): ... self.number = number ...def __repr__(self): ... return "Car2(%r)" % self.number ...def __eq__(self, other): ... return self.number == other.number ... Above I have fixed Car to use value equivalence (albeit not very robust). Now if I throw these objects naively in a set: >>> a = Car2(123) >>> b = Car2(123) >>> a == b True >>> set([a,b]) set([Car2(123), Car2(123)]) I get a set with two equal cars, something that never happens with a set my math teacher once told me. Of course, I should have defined an appropiate __hash__ method together with the __eq__ method. Unfortunately, not every Python programmer has always had enough coffee to think about that when he is programming a class. Even worse, I may get a class such as the above from somebody else and decide that I need a set of such objects, something the original designer never intended. The problem is then that something like "set([Car2(123), Car2(124)])" does the right thing for the wrong reason without telling me. Without a default __hash__ I'd get at least an error that I cannot put Car2 objects in a set. In that setup, I can still construct a broken set, but I'd have to write a broken __hash__ function explicitly rather than implicitly inheriting it from object. > I don't want, in order to get that often-useful behavior, to have to > code a lot of boilerplate such as > def __hash__(self): return hash(id(self)) > and the like -- so, I like the fact that object does it for me. I'd I understand that you'd like to have less typing to do. I'd like that too if only it would work without major accidents by simple omission such as demonstrated in the set example. Another question can be whether your coding style would be correct here. Since you apparently want to have singleton objects (since that is what you get and you are happy with them), shouldn't you be using "is" rather than "=="? Then you get the equivalence notion you want, you don't need __eq__, and you write explicitly that you have singleton objects. In the same way, sets have very little value for singleton objects, you may as well use lists instead of sets since duplicate **values** are not filtered. For lists, you don't need __hash__ either. The only exception would be to filter multiple inclusions of the same object (that is what sets are doing by default). I don't know whether that would be really important for singleton objects **in general**. (ie wouldn't it be better to explicitly write a __hash__ based on identity for those cases?) > have no objecti
Re: equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)
On 2007-06-28, Alan Isaac <[EMAIL PROTECTED]> wrote: > A.T.Hofkamp wrote: > >>>>>a = Car2(123) >>>>>b = Car2(123) >>>>>a == b >> >> True >> >>>>>set([a,b]) >> >> set([Car2(123), Car2(123)]) >> >> I get a set with two equal cars, something that never happens with a set >> my math teacher once told me. > > > Then your math teacher misspoke. > You have two different cars in the set, > just as expected. Use `is`. > http://docs.python.org/ref/comparisons.html > > This is good behavior. Hmm, maybe numbers in sets are broken then? >>> a = 12345 >>> b = 12345 >>> a == b True >>> a is b False >>> set([a,b]) set([12345]) Numbers and my Car2 objects behave the same w.r.t. '==' and 'is', yet I get a set with 1 number, and a set with 2 cars. Something is wrong here imho. The point I intended to make was that having a default __hash__ method on objects give weird results that not everybody may be aware of. In addition, to get useful behavior of objects in sets one should override __hash__ anyway, so what is the point of having a default object.__hash__ ? The "one should override __hash__ anyway" argument is being discussed in my previous post. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)
On 2007-06-29, Steve Holden <[EMAIL PROTECTED]> wrote: > Just the same there are sound reasons for it, so I'd prefer to see you > using "counterintuitive" or "difficult to fathom" rather than "broken" > and "wrong". You are quite correct, in the heat of typing an answer, my wording was too strong, I am sorry. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: equality & comparison by default (was Re: Too many 'self' in python.That's a big flaw in this language.)
On 2007-06-28, Roy Smith <[EMAIL PROTECTED]> wrote: > In article <[EMAIL PROTECTED]>, > "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: > >> In object oriented programming, objects are representations of values, and >> the >> system shouldn't care about how many instances there are of some value, just >> like numbers in math. Every instance with a certain value is the same as >> every >> other instance with the same value. > > Whether two things are equal depends on the context. Is one $10 note equal > to another? It depends. > > If the context is a bank teller making change, then yes, they are equal. > What's more, there are lots of sets of smaller notes which would be equally > fungible. > > If the context is a district attorney showing a specific $10 note to a jury > as evidence in a drug buy-and-bust case, they're not. It's got to be > exactly that note, as proven by a recorded serial number. > > In object oriented programming, objects are representations of the real > world. In one case, the $10 note represents some monetary value. In > another, it represents a piece of physical evidence in a criminal trial. > Without knowing the context of how the objects are going to be used, it's > really not possible to know how __eq__() should be defined. I can see your point, but am not sure I agree. The problem is that OO uses models tailored to an application, ie the model changes with each application. In a bank teller application, one would probably not model the serial number, just the notion of $10 notes would be enough, as in "Note(value)". The contents of a cash register would then for example be a dictionary of Note() objects to a count. You can merge two of such dictionaries, where the 'value' data of the Note objects would be the equivalence notion. In an evidence application one **would** record the serial number, since it is a relevant distinguishing feature between notes, ie one would model Note(value, serialnumber). In this application the combination of value and serial number together defines equivalence. However, also in this situation we use values of the model for equivalence. If we have a data base that relates evidence to storage location, and we would like to know where a particular note was stored, we would compare Note objects with each other based in the combination of value and serial number, not on their id()'s. > You tell me. This is really just the "does 1 == (1 + 0j)" question in > disguise. There's reasonable arguments to be made on both sides, but there > is no one true answer. It depends on what you're doing. While we don't agree on how OO programming handles equality (and it may well be that there are multiple interpretations possible), wouldn't your argument also not lead to the conclusion that it is better not to have a pre-defined __eq__ method? Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: equality & comparison by default
On 2007-06-29, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > En Thu, 28 Jun 2007 11:38:56 -0300, A.T.Hofkamp <[EMAIL PROTECTED]> > escribió: > >> The point I intended to make was that having a default __hash__ method on >> objects give weird results that not everybody may be aware of. >> In addition, to get useful behavior of objects in sets one should >> override >> __hash__ anyway, so what is the point of having a default >> object.__hash__ ? > > __hash__ and equality tests are used by the dictionary implementation, and > the default implementation is OK for immutable objects. I like the fact I don't understand exactly how mutability relates to this. The default __eq___ and __hash__ implementation for classes is ok if you never have equivalent objects. In that case, == and 'is' are exactly the same function in the sense that for each pair of arguments, they deliver the same value. This remains the case even if I mutate existing objects without creating equivalent objects. As soon as I create two equivalent instances (either by creating a duplicate at a new address, or by mutating an existing one) the default __eq__ should be redefined if you want these equivalent objects to announce themselves as equivalent with the == operator. > that I can use almost anything as dictionary keys without much coding. Most data-types of Python have their own implementation of __eq__ and __hash__ to make this work. This is good, it makes the language easy to use. However for home-brewn objects (derived from object) the default implementation of these functions may easily cause unexpected behavior and we may be better off without a default implementation for these functions. That would prevent use of such objects in combination with == or in sets/dictionaries without an explicit definition of the __eq__ and __hash__ functions, but that is not very bad, since in many cases one would have to define the proper equivalence notion anyway. > This must always be true: (a==b) => (hash(a)==hash(b)), and the > documentation for __hash__ and __cmp__ warns about the requisites (but > __eq__ and the other rich-comparison methods are lacking the warning). I don't know exactly what the current documentation says. One of the problems is that not everybody is reading those docs. Instead they run a simple test like "print set([Car(1),Car(2)])". That gives the correct result even if the "(a==b) => (hash(a)==hash(b))" relation doesn't hold due to re-definition of __eq__ but not __hash__ (the original designer never expected to use the class in a set/dictionary for example) , and the conclusion is "it works". Then they use the incorrect implementation for months until they discover that it doesn't quite work as expected, followed by a long debugging session to find and correct the problem. Without default __eq__ and __hash__ implementations for objects, the program would drop dead on the first experiment. While it may be inconvenient at that moment (to get the first experiment working, one needs to do more effort), I think it would be preferable to having an incorrect implementation for months without knowing it. In addition, a developer has to think explicitly about his notion of equivalence. Last but not least, in the current implementation, you cannot see whether there is a __eq__ and/or __hash__ equivalence notion. Lack of an explicit definition does not necessarily imply there is no such notion. Without default object implementation this would also be uniqly defined. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How to close a program I execute with subprocess.Popen?
On 2007-06-29, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I want to end the execution of the command when the user closes my > application. > > Right now I'm using an object my_child of type subprocess.Popen to > execute the command, inside a thread with an infinite loop where we > constantly ask for its output. > > To end the program when the user closes the application, I send a > SIGTERM to the process with pid my_child.pid using os.kill. But I also > have to send a SIGTERM to my_child.pid + 1 because my_child.pid is the Maybe that holds for a system recently started and mostly idle, but it need not be the case. If you have more users active, and each user is forking processes, (or one user is forking several processes concurrently), the order of assigning process IDs is not defined. For systems that run for a longer time, unused process IDs are re-used, skipping over process IDs that are still living. > pid of /bin/sh -c which is the one which calls the command, because > when I try to run Popen with shell=False, it sends an exception and > says the file or directory doesn't exist. The shell performs a search over PATH to find the command executable (ie it maps 'ls' to '/bin/ls'). If you provide a full path to the command you are starting, you don't need the shell. > Anyone knows of a better way to close the command than using a > SIGTERM? I just can't help myself thinking this is an ugly dirty hack. In principle, you should only kill your own child processes, your child process should handle its own childs (your grant child processes). SIGTERM is one way. Another solution often adopted is to close the stdin of the child. This notifies the child that no more data will arrive, and many commands react on that message by terminating. Try running command < /dev/null /dev/null is the empty input stream. Last but not least, many commands do something special when sent -HUP. Look in the manual page of the command for clean ways to close the command down. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: adjust
On 2007-04-24, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > How do i right adjust my output using python.I need a output > something like this: > DID= 0x01,0x02,0x03,0x05,0x06,0x07,0x2B,0x30,0x31,0x4D,0x4E, > 0x51,0x52,0x53,0x55, > minlength= 3, 3, 4, 2, 10, 10, 40, 2, 150, 4, 1, > 2, 2, 1, 2, 1, 6, 3, 17, 1, > maxlength= 3, 3, 4, 2, 10, 10, 40, 2, 150, 4, 1, > 2, 2, 1, 2, 30, 6, 3, 17, 20 > > I am printing from a list, and the values shld print below each id > like shown. > Thx Collect the data to print in rows underneath each other in a list for each column, decide how wide each column should become, and finally, print out each line using the computed widths. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: c macros in python.
On 2007-05-06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hey, > > I'm writing a script to generate code. I'm a bit tired of typing > outfile.write(). Does python have a way to c-like macros? Every > instance of o(...) in the code will be replaced by outfile.write(...)? Just in case you don't know, you can write an arbitrary number of lines in one write. Below is how I format a usage() message of a program in one write call: def usage(fp): fp.write("Usage: convert options [infile] [outfile]\n" "with\n" "options\n" "\t--prefix=\t(obligatory)\n" "\t--write-size\t\tWrite a line containing the size\n" "\t--append-zero\t\tAppend a terminating 0 byte\n") ie one multi-line write call. Pyhon concatenates two consequtive string literals for us. Unfortunately, this gets less pretty when inserting variable values. In other code generation code, I normally use a list of lines. Rather than writing everything directly to file, I make a list data structure containing lines, then dump the list to file, as in: lines = [] gencode_first(lines) gencode_second(lines) lines.append("the end") write_lines(lines) where write_lines() is def write_lines(lines): for line in lines: outfile.write(line) outfile.write('\n') (i left out the opening and closing of outfile). I normally do not include the \n in the list but instead add it while writing it to file. This makes life much easier since there are no special last-value problems in the code generator itself. The nice thing here is that 'lines' is a normal data structure which you can manipulate if you like. For more demanding code generators (ie C or C++ code) I use the concept 'sections'. At a global level, the generated code has an 'include', 'declarations', 'definitions', and 'main' section, each section is a list of lines. I use a dictionary for this, like output = { 'incl': [], 'decl': [], 'def': [], 'main': [] } then pass around this in the code generator. Each part of the generator can write in each section, for example when defining a C function, you can write the declaration in the decl section and the definition in the def section at the same time. For example def write_c_function(output): output['decl'].append('int mycfunc(void);') output['def'].extend(['int myfunc(void)', '{' 'return 131;', }' ]) Reducing such a dictionary to a list is then something like def make_lines(sec_list, output): lines = [] for sec in sec_list: lines.extend(output[sec]) return lines And outputting the code is then something like write_lines(make_lines(['incl', 'decl', 'def', 'main'], output)) In this way you can abstract away from the order of code as required by the target language and instead generate code in a nicer order. Note that this section trick can be done recursively. for example, a function can be thought of as a number of sections like funcoutput = { 'header': [], 'opening-bracket' : [], 'localvars':[], 'closing-bracket': [] } so you can generate a function using sections as well, then at the end reduce funcoutput to a list of lines, and insert that in a section of the global 'output'. Last but not least, if you replace the lists by an object, you can do much smarter things. For example, in general you don't want to have double #include lines in the 'incl' section. Instead of worrying about generation of doubles, just make an object that behaves like a list but silently drops doubles. In the same way, you can create a list-like object that handles indenting for you. The possibilities here are endless!! Good luck with your code generation problem, I hope I gave you some ideas of alternative solutions that are available. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Who told str() to round my int()'s!!!
On 2007-08-11, Adam W. <[EMAIL PROTECTED]> wrote: > After a fair amount of troubleshooting of why my lists were coming > back a handful of digits short, and the last digit rounded off, I > determined the str() function was to blame: > foonum > 0.0071299720384678782 str(foonum) > '0.00712997203847' > > Why in the world does str() have any business rounding my numbers, and > how do I get around this? You have got a few good reactions already. What is not mentioned yet however is the difference between str() and repr(). Python has two ways to stringify an object: str() is intended to deliver a 'human-readable' representation of its argument, and typically, humans think a few digits of a float is enough. The repr() on the other hand is intended to deliver a 'machine-reproducible' string representation of its argument, ie after "y = eval(repr(x))" y == x should hold. Note that no rounding occurs with the floating point number, arguments of both str() and repr() are not changed. So, depending on your intentions of str(foonum), you should either explicitly format your floating point number yourself (ie if you want a more precise human-readable representation of the number), or you should use repr() (if you intend to use the string representation for reproducing the same object elsewhere by a machine). Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Retry: Question about FutureWarning
On 2007-08-15, Paul McGuire <[EMAIL PROTECTED]> wrote: > On Aug 14, 8:49 pm, Erik Max Francis <[EMAIL PROTECTED]> wrote: > > So if by '0x' you meant -1, then change this line to use -1. > Otherwise, if you really meant 4294967295L, leave it at 0x and > move on. A third option is to specify 0x as 0xL in the latter case to get rid of the warning. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Using python for dynamic behavior from C++
On 2007-08-15, Jorgen Bodde <[EMAIL PROTECTED]> wrote: > Hi all, > > I am looking into using Python to introduce dynamic behavior in my > C++, e.g. something like a simulation where objects can interact with > eachother. I know Python can be called from C++, but is it possible to > call a binary compiled Python file / array from C++ ? The reason I ask > is that if my objects call a script every tick to do something, I > would not like to let the python interpreter recompile the same code > over and over again. > > Any pointers to how I can do that? It is called 'embedding the Python Interpreter' and is in the second part of the 'Extending and Embedding the Python Interpreter' document at python dot org. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Who told str() to round my int()'s!!!
On 2007-08-15, Larry Bates <[EMAIL PROTECTED]> wrote: > > What are they teaching in schools these days? I see questions like this and > the > equally perplexing "why don't floats represent numbers exactly?" or the mildy > amusing "how do I write bytes not characters to a file" questions at least > once > a week on this forum. Who says that schools teach that to every body? Python is a popular language, so there are a lot non-cs people here. I can imagine that eg a user with a background in history or geography has never encountered these things before. (Python got to start programming, and now they suddenly have to deal with inaccurate floats of the otherwise so precise computer :-) ) Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: desperately in need of a tool
On 2007-08-19, yagyala <[EMAIL PROTECTED]> wrote: > Hi. > > one of those standards is that the comments for each routine must > indicate every other routine that it calls. As I try to keep my > to do this by hand. Does anyone know of a tool that could do this for > me, or at least a tool that can tell what other routines a given > routine calls that I could program against? (Preferably something that > works under pydev, but I'm not going to be choosy.) Wouldn't a regular expression be enough here? Something like # x.py import re, sys fcallpat = re.compile(r'\w+\(') data = sys.stdin.read() for match in fcallpat.findall(data): print match $ python x.py < x.py compile( read( findall( You may want to have a wider matching criterium than \w+ Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular expression use
On 2007-08-24, Nick Maclaren <[EMAIL PROTECTED]> wrote: > people actually use regular expressions for. Not the subject > domain, but the construction of the regular expressions. This is easy. I use RE for checking whether some input matches a certain pattern, and optionally, to extract some special part from the text. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How to parse this line of code manually
On 2007-08-28, Davy <[EMAIL PROTECTED]> wrote: > On Aug 28, 11:00 am, Davy <[EMAIL PROTECTED]> wrote: >> Hi all, >> >> It is well known that Python is appreciated for its merit of concise. >> However, I found the over concise code is too hard to understand for >> me. >> >> Consider, for instance, >> def known_edits2(word): >> return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in >> NWORDS) >> >> Shall I understand the code in set() as >> for e2 in edits1(e1) { >> if e2 in NWORDS { >> for e1 in edits1(word) { >> e2 >> } >> } >> >> } >> > [SNIP] > Hi all, I figured it myself. It is left to righ parse, right? > So the above one is like > for e1 in edits1(word) { > for e2 in edits1(e1) { > if e2 in NWORDS { > push e2 to set > } > } > } This is correct, although I am not sure what language you are using here, it looks like a strange mix of Python and C to me. >> Any suggestions are welcome! The idea is known as List comprehension (for lists, obviously), and comes from functional programming, Bird & Wadler used it in their book. The notation is very close to mathematics: { e2 | e1: edits(word), e2: edits(e1) in NWORDS } or in LaTeX: $\{ e_2 | \forall e_1: \mathrm{edits}(\mathrm{words}), \forall e_2: \mathrm{edits}(e_1) \in \mathrm{NWORDS} \}$ :-) (which in words is something like: collect values e2, where e1 comes from 'edits(word)', e2 comes from 'edits(e1)', and e2 in NWORDS) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Printing lists in columns (was: TypeError: 'module object is not callable')
On 2007-09-04, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Thanks guys. Changing to how Python does things has a lot of geting > used to! That's part of the fun :-) > Do any of you have any ideas on the best way to do the following > problem: > > Each loop I perform, I get a new list of Strings. > I then want to print these lists as columns adjacent to each other > starting with the first > created list in the first column and last created list in the final > column. Use zip: >>> x = ['1', '2'] >>> y = ['3', '4'] >>> for row in zip(x,y): ... print ', '.join(row) ... 1, 3 2, 4 zip() constructs a list of rows, like [('1', '3'), ('2', '4')] which is then quite easy to print Good luck, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling a matlab script from python
On 2007-09-05, n o s p a m p l e a s e <[EMAIL PROTECTED]> wrote: > Suppose I have a matlab script mymatlab.m. How can I call this script > from a python script? use the mlabwrap module Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Parse or Pass?
On 2007-09-05, Martin P. Hellwig <[EMAIL PROTECTED]> wrote: > Martin v. Löwis wrote: > Eingeben = Input: (A bit of) data from outside the function > Ausgeben = Output: (A bit of) data to display, network > connection or file > Zurückgeben = Return: (altered)(bits of) data (from Input) > to Output > > Can I assume that Return in general always means that the particular > function has exited with that? If not what is the difference then > between Output and Return? It depends on your point of view imho. You can describe a function call from 'outside', ie I give it values for its parameters, and it returns me a computed return value. You can describe the same thing from 'inside', ie I get values from my caller, compute a result, and output the result. > And then we have "Übergeben" which translates to throughput (giving > over), which in my view is just something that gets data in and puts it > out, contextually unaltered. But would that do that with exiting the I would consider this yet another view, namely performance. Function calls are stil happening, but you focus more on the #calls/second, ie throughput. So depending on what one is interested in, I think, one structures and describes what is happening in a different way. Wouldn't that be a possible explanation for all the subtle different ways of describing the same thing? Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Python wrapper, problem with subprocess read/write
On 2007-09-07, NeoGregorian <[EMAIL PROTECTED]> wrote: > I tried instead to use: > > lines = [] > line = proc.stdout.readline() > while line : > lines.append(line) > line = proc.stdout.readline() > > This prints out everything except the ">" line, which is good. But > then freezes while waiting for input, which is bad. > > Any suggestions on how to solve this in a good way? 'readline()' reads a line, that is, some text ending with a new-line. Since your last line, the ">" prompt has no ending new-line, the call blocks, waiting for the new-line character. So the simple anser is "don't use readline()". You have to fall back to reading characters, such as "read(1)" (which block until it receives a character). In addition, you will have to do analysis on whether the line you are currently reading is a prompt, and if so, stop reading to prevent blocking. (and instead, give the program a command by writing to proc.stdin). In case you don't know, pexpect (Python expect) does all (and more) that you are trying to do. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Using a time duration to print out data where every 2 seconds is a pixel
On 2007-09-10, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I want to be able print out the Steps as a visual representation so > that I can show > 1. The order the steps started > 2. The duration of the steps > > i.e. a print out such as: > > > [a] >[ b ] > [ c ] This graph is commonly known as a Gantt Chart. The most common use case of such charts is project planning. The newest GNU plot program has support for these charts afaik. > Another related question is that I can't seem to do arithmetic when > the variables are in String format > of %H:%M:%S or converted to type struct_time True (but you already detected that). Either convert your struct_times to floats (time.mktime), or use a date/time module specialized in computing with dates and times. (I have seen references to a module called datetime, but I have never used such a module so no idea what it is or where to get it). Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Problem
On 2007-09-11, Wiseman <[EMAIL PROTECTED]> wrote: > > Hi, > > OK - it works in WindowsXP. > I installed "enchant" on my SuSE 10.0 (using YAST). > The enchant Suse package looks like a general Linux package, not a > Python specific. You'd seem to be right judging by this web-page: http://www.novell.com/products/linuxpackages/suselinux/enchant.html As you can see, there is no file installed at a path with "python" in it. > What am I missing? Python bindings to the library perhaps? You may be more lucky with an enchant-dev package (if it exists). I am however not a Suse user and not an echant user (well, not from my Python code at least). Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Parallel/Multiprocessing script design question
On 2007-09-13, Amit N <[EMAIL PROTECTED]> wrote: > Hi guys, > > I tend to ramble, and I am afraid none of you busy experts will bother > reading my long post, so I will try to summarize it first: I haven't read the details, but you seem to aim for a single python program that does 'it'. A single sequential thread is not fast enough, so you want parallel execution (and yes there are a zillion ways of doing that). Why don't you start at the other end? Write a program for each task that you have, then fork/spawn/chain/whatever enough processes at OS level to eat all your data. The OS is usually much better at balancing CPU's. The Python module 'subprocess' would be your friend in that case. In addition, you can run each program independently, which will come in handy one day. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: distributed queue?
On 2007-03-10, Paul Rubin wrote: > Does anyone have an implementation of a distributed queue? I.e. I > have a long running computation f(x) and I'd like to be able to > evaluate it (for different values of x) on a bunch of different batchlib and the underlying exec_proxy are designed to handle exactly this type of problem. Both of them are in PyPI (and available at my site http://se.wtb.tue.nl/~hat). Alnert -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing text in blocks and line too
On 2007-04-12, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Goodmorning people :) > I have just started to learn this language and i have a logical > problem. > I need to write a program to parse various file of text. > Here two sample: > > --- > trial text bla bla bla bla error > bla bla bla bla bla > bla bla bla on more lines > trial text bla bla bla bla warning bla > bla bla more bla to be grouped with warning > bla bla bla on more lines > could be one two or ten lines also withouth the tab beginning > again text > text can contain also blank lines > text no delimiters > -- > Apr 8 04:02:08 machine text on one line > Apr 8 04:02:09 machine this is an error > Apr 8 04:02:10 machine this is a warning > -- I would first read groups of lines that belong together, then decide on each group whether it is an error, warning, or whatever. To preserve order in a group of lines, you can use lists. >From your example you could first compute a list of lists, like [ [ "trial text bla bla bla bla error", " bla bla bla bla bla", " bla bla bla on more lines" ], [ "trial text bla bla bla bla warning bla", " bla bla more bla to be grouped with warning", " bla bla bla on more lines", " could be one two or ten lines also withouth the tab beginning" ], [ "again text" ], [ "text can contain also blank lines" ], [ ], [ "text no delimiters" ] ] Just above the "text no delimiters" line I have added an empty line, and I translated that to an empty group of lines (denoted with the empty list). By traversing the groups (ie over the outermost list), you can now decide for each group what type of output it is, and act accordingly. > Hope someone could give me some tips. Sure, however, in general it is appreciated if you first show your own efforts before asking the list for a solution. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I know how much to read from a subprocess
On 2007-09-17, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > It seems that another solution is gobject.io_add_watch, but I don't > see how it tells me how much I can read from the file - if I don't > know that, I won't know the argument to give to the read() method in > order to get all the data: > > http://www.pygtk.org/docs/pygobject/gobject-functions.html#function-gobject--io-add-watch > Usually, gobject only tells you that data is there (that is all it knows). Therefore a read(1) should be safe. If that is too slow, consider os.read() which reads all data available (afaik, never tried it myself). Albert -- http://mail.python.org/mailman/listinfo/python-list
cannot create my own dict
Hello all, This morning I tried to create my own read-only dictionary, and failed miserably. I don't understand why, can somebody enlighten me? Below is a brute-force experiment that cannot deal with "x in obj", plz read the explanation below the code: class myowndict(object): def __init__(self, mydict): self.mydict = mydict # Below is produced with # print '\n'.join(['self.%s = self.mydict.%s' % (v,v) # for v in dir(dict)]) # commented-out functions done by hand # #self.__class__ = self.mydict.__class__ self.__cmp__ = self.mydict.__cmp__ self.__contains__ = self.mydict.__contains__ self.__delattr__ = self.mydict.__delattr__ self.__delitem__ = self.mydict.__delitem__ #self.__doc__ = self.mydict.__doc__ self.__eq__ = self.mydict.__eq__ self.__ge__ = self.mydict.__ge__ self.__getattribute__ = self.mydict.__getattribute__ self.__getitem__ = self.mydict.__getitem__ self.__gt__ = self.mydict.__gt__ self.__hash__ = self.mydict.__hash__ #self.__init__ = self.mydict.__init__ self.__iter__ = self.mydict.__iter__ self.__le__ = self.mydict.__le__ self.__len__ = self.mydict.__len__ self.__lt__ = self.mydict.__lt__ self.__ne__ = self.mydict.__ne__ #self.__new__ = self.mydict.__new__ self.__reduce__ = self.mydict.__reduce__ self.__reduce_ex__ = self.mydict.__reduce_ex__ self.__repr__ = self.mydict.__repr__ self.__setattr__ = self.mydict.__setattr__ self.__setitem__ = self.mydict.__setitem__ self.__str__ = self.mydict.__str__ self.clear = self.mydict.clear self.copy = self.mydict.copy self.fromkeys = self.mydict.fromkeys self.get = self.mydict.get self.has_key = self.mydict.has_key self.items = self.mydict.items self.iteritems = self.mydict.iteritems self.iterkeys = self.mydict.iterkeys self.itervalues = self.mydict.itervalues self.keys = self.mydict.keys self.pop = self.mydict.pop self.popitem = self.mydict.popitem self.setdefault = self.mydict.setdefault self.update = self.mydict.update self.values = self.mydict.values # end of __init__ if __name__ == '__main__': fd = myowndict({1:10}) print 1 in fd # FAILS! (with "TypeError: iterable argument required") I wanted to make my own dictionary. However, a simple element test failed (after implementing various __*__ functions), and I cannot figure out why. The above code is a brute force attempt, where I forward all methods (except __class__, __doc__, __init__, and __new__) to my local 'mydict' object. IT STILL FAILS. So if copying all methods of a native dictionary is not enough, what should I do to make my class work as a dictionary WITHOUT deriving from dict (which will obviously work). Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I know how much to read from a subprocess
On 2007-09-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On Sep 18, 1:48 pm, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: >> On 2007-09-17, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> > It seems that another solution is gobject.io_add_watch, but I don't >> > see how it tells me how much I can read from the file - if I don't >> > know that, I won't know the argument to give to the read() method in >> > order to get all the data: >> >> >http://www.pygtk.org/docs/pygobject/gobject-functions.html#function-g... >> >> Usually, gobject only tells you that data is there (that is all it knows). >> Therefore a read(1) should be safe. > > But even if it's fast enough, how do you know how many times you > should call read(1)? If you do it too much, you'll be blocked until > more output is available. after reading 1 byte, wait for gobject again. In other words, when gobject sends a notice, read 1 byte. When there is more, gobject will tell you (probably very often :-) ). > >> If that is too slow, consider os.read() which reads all data available >> (afaik, >> never tried it myself). >> > I tried it now, and it blocks just like the normal file.read(). So "os.read(handle.fileno(), 100)" blocks after gobject tells you there is data? That is different than I heard, but you are probably correct, I cannot easily test this. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: cannot create my own dict
On 2007-09-19, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > A.T.Hofkamp a écrit : >> So if copying all methods of a native dictionary is not enough, what should I >> do to make my class work as a dictionary WITHOUT deriving from dict (which >> will >> obviously work). >> Hello all, Thanks for all the suggestions, the cause of the problem seems to be that I assumed that I can export magic methods from a member. Apparently, that is not true :-( > Sorry, I missed this last requirement. BTW, why don't you want to > subclass dict ? The reason for this problem is that we are busy re-structuring a large Python program, and we want to ensure nobody is messing up our data structures (at least not until we are finished). Most data types have a nice frozen variant (set -> frozenset, list -> tuple, __setattr__ override), but dictionaries do not. As a result I wanted to have a read-only dictionary. There is one in the Cook book, but it uses property's that I don't understand and they are also not in de Python language reference (at least I couldn't find them in the index and not in the table of contents). I don't like code that I don't understand (in particular when bugs in that code will be nasty to debug), so I decided to write my own, not in the last place, because I expected it to be simple in Python. I can derive from dict, but the problem with that is that I start with a read/write dictionary, and I can only hope to plug all holes to prevent my data from leaking out. By starting from 'object', I certainly don't have that problem, I start with a closed bucket and punch holes in it in a controlled way. (I rather have the program drop dead due to not having enough access than have it continue with too much access causing havoc 500 statements later in a totally unrelated area.) Rather than write a bunch of code like def __contains__(self, val): return val in self.mydict I thought I'd simply do self.__contains__ == self.d.__contains__ which is exactly the same but less work (or so I thought), and possibly slightly faster. Well, no such luck thus :-( Tnx for clearing up the problem, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: An Editor that Skips to the End of a Def
On 2007-09-25, Lawrence D'Oliveiro <[EMAIL PROTECTED]> wrote: > In message <[EMAIL PROTECTED]>, Neil Cerutti wrote: > That's like saying, about a program that, when given "2 + 2", outputs "5", > that _of course_ it knows the correct answer is "4", it just chooses > to "modify" the answer before outputting it. > > Why does it "choose" to modify your position when you exit insert mode? Does > the phrase "broken as designed" mean anything to you? Try to insert 1 character in the middle of a line. You'll end up at the same position. Now press 'j' (one line down), then '.' (do it again). I believe that's why. Great when you have nicely formatted columns of code underneath each other. (try doing that with your GUI editor with 2 strokes per line of code). Of course, the same works for a find/edit cycle ('n', check whether edit is appropiate, and if so: '.') This combining of commands is what makes the editor powerful. The cost of that power is a command/insert mode and a steep learning curve. You may not like the keyboard interface, but that doesn't mean the editor is not well-designed for its task. In the same way, having the ability to use multiple input devices doesn't automatically make the editor better. For example, ever wondered why you on earth you need CTL-C and CTL-V to copy/paste? Why not simply select with the mouse, then right-click to paste? All editors have their good and their bad sides. Assuming that anything you don't like is badly designed is a bit too simple in my opinion. > And the downside is that the largest single proportion of those commands end > up being variations on "enter insert mode". Because most of the keystrokes > you enter during an editing session are in fact text to be input into the > file, not commands to manipulate that text. So in a modal editor, having to Depends on what you are doing. When entering new code, yes. When maintaining code, no (lots of small changes). In one way or another, you'll have to tell the editor what you want. The cost of that is either a command/insert mode (vi/vim), a CTL/SHIFT/META-key combination (emacs), or mouse/menu/submenu/subsubmenu (GUI editors). I don't know what is best. Probably a matter of taste. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: sorteddict PEP proposal [started off as orderedict]
On 2007-09-25, Mark Summerfield <[EMAIL PROTECTED]> wrote: > If there is positive feedback I will submit the PEP to the reviewers, > so if you think it is a good idea please say so. (I'm sure that if you > _don't_ like it you'll tell me anyway:-) I like the idea, ie +1. > This PEP proposes the addition of a sorted dictionary class to the > standard library's collections module. You don't seem to mention the sort criterium. I'd suggest to use the __lt__ operator, which you probably intended since it is commonly used. Also, can I specify a custom sort function, as with list.sort() ? > In addition, the keys() method has two optional arguments: > > keys(firstindex : int = None, secondindex : int = None) -> list of Not sure this is a good idea. Wouldn't simply mysorteddict.keys()[firstindex:secondindex] be much better? It can do all you propose, and more (with iterators/generators and such). I see no need to implement it inside the sorteddict as well. > Since the sorteddict's data is always kept in key order, indexes > (integer offsets) into the sorteddict make sense. Five additional > methods are proposed to take advantage of this: > > key(index : int) -> value > > item(index : int) -> (key, value) > > value(index : int) -> key > > set_value(index : int, value) > > delete(index : int) I wouldn't do this. It breaks compability with the normal dict. A beginning user will expect dict and sorteddict to behave the same (except for sortedness), including their set of supporting functions imho. Otherwise, please make a case for them (and maybe even a new PEP to get them in both types of dicts). > Examples > > To keep a collection of filenames on a case-insensitive file > system in > sorted order, we could use code like this: > > files = collections.sorteddict.sorteddict() > for name in os.listdir("."): > files[name.lower()] = name The more interesting case would be to you preserve case of the files within the keys. I usually need this data structure with an A* algorithm implementation, where I need to obtain the cheapest solution found so far. > for item in listOfItems: > itemsByDate["%s\t%17X" % (item.date, id(item))] = item > itemsByName["%s\t%17X" % (item.name, id(item))] = item > itemsBySize["%s\t%17X" % (item.size, id(item))] = item Wouldn't a line like "itemsBySize[(item.size, id(item))] = item" do as well? (or with a custom sort function on items?) > Now we can iterate in date or name order, for example: > > for item in itemsByDate: > print item.name, item.date Hmm, not with dict: >>> for x in {1:10, 2:20}: ... print x ... 1 2 So you should get your "%s\t%17X" strings here. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Asynchronous Messaging
On 2007-09-26, wink <[EMAIL PROTECTED]> wrote: > Hello, > > I'm getting my feet wet in Python and thought I'd try to see how well > Python works for asynchronous messaging. I've been using asynchronous Have a look at Twisted (www.twistedmatrix.com) Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Really basic problem
On 2007-10-08, Andreas Tawn <[EMAIL PROTECTED]> wrote: >> i know this example is stupid and useless, but that's not the answer >> to my question. >> here it goes: >> > You've just discovered the joys of floating point number comparisons. > > Consider this snippet: > > status = 0.0 > print (repr(status)) > > for i in range(10): > status += 0.1 > print (repr(status)) > > Output: > > 0.0 > 0.10001 > 0.20001 > 0.30004 > 0.40002 > 0.5 > 0.59998 > 0.69996 > 0.79993 > 0.89991 > 0.99989 > > The issue is that 0.1 etc don't have an exact representation as floating > point. > > Interestingly: > 0.10001 == 0.1 > True 0.30004 == 0.3 > False > > I guess this means that Python has some concept of "close enough", but > I'll have to defer to someone more knowledgeable to explain that. It's not Python, it is the computer, ie you have this problem with any program that uses floating point calculations. However, some programs hide it from you so you think there is no problem... Plz read the FAQ for an answer to this and many other questions: http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Noob questions about Python
On 2007-10-19, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On Oct 19, 1:44 am, MRAB <[EMAIL PROTECTED]> wrote: >> On Oct 18, 7:05 am, Michele Simionato <[EMAIL PROTECTED]> >> >> if number == 0: >> return "0" >> > > Hey, > > Isn't > if not number: > return "0" > > faster? Depends on who is parsing it. If a computer, possibly a few micro-seconds (if the Python byte-code generator doesn't optimize it away). If a human, then it is slower by at least a second. Since I prefer to optimize on my time rather than CPU time, I'd prefer the first version Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Set operations on object attributes question
On 2007-10-23, TheSeeker <[EMAIL PROTECTED]> wrote: > Hi, > > I have run into something I would like to do, but am not sure how to > code it up. I would like to perform 'set-like' operations (union, > intersection, etc) on a set of objects, but have the set operations > based on an attribute of the object, rather than the whole object. > > For instance, say I have (pseudo-code): > > LoTuples1 = [(1,1,0),(1,2,1),(1,3,3)] > Set1=set(LoTuples1) > LoTuples2 = [(2,1,3),(2,2,4),(2,3,2)] > Set2=set(LoTuples2) > > What I would like to be able to do is: > > Set3 = Set1union(Set2) > Set3.intersection(Set2, ) > > to return: > set([(2,1,3), (1,3,3)]) > > How can one do this operation? Put your data in a class, and implement __hash__ and __eq__ Finally, put your objects in sets. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Better writing in python
> On 2007-10-24, Alexandre Badez <[EMAIL PROTECTED]> wrote: > I'm just wondering, if I could write a in a "better" way this > code > > lMandatory = [] > lOptional = [] > for arg in cls.dArguments: > if arg is True: > lMandatory.append(arg) > else: > lOptional.append(arg) > return (lMandatory, lOptional) > > I think there is a better way, but I can't see how... You can do it shorter, not sure that it also qualifies as better d = { True : [] , False : [] } for arg in cls.arguments: d[arg == True].append(arg) return d[True], d[False] One potential problem here is that 'arg == True' may not be the same as 'if arg:'. I couldn't come up with a better equivalent expression, maybe one of the other readers knows more about this? Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: local variable referenced before assignment
On 2007-10-25, Pete Bartonly <[EMAIL PROTECTED]> wrote: > > Quick question, probably quite a simple matter. Take the follow start of > a method: > > > def review(filesNeedingReview): > > for item in filesNeedingReview: > (tightestOwner, logMsg) = item > > if (logMsg != None): > for logInfo in logMsg.changed_paths: > > > This generates the error: > >UnboundLocalError: local variable 'logMsg' referenced before assignment This should work, are you sure you didn't make a typo in one of the names? Another way to make this fail would be when the if-condition is outside the loop (is the indentation correct in your code?). A short demontration: >>> def r(fnr): ... for item in fnr: ... w,m = item ... if m == 2: ... print w ... >>> fnr = [(1,2), (3,4)] >>> r(fnr) 1 With respect to compactness and style, you can move your multi-assignment statement in the for loop, as in for tightestOwner, logMsg in filesNeedingReview: Also, brackets around conditions (in the if) are not needed, and comparing against None is usually done with 'is' or 'is not' instead of '==' or '!='. The result is then if logMsg is not None: > I thought I'd assigned it in the "(tightestOwner, logMsg) = item" line - > so in the python interpreter complaining about the fact this assignment > might not go well? No, you'd get an error at that point in that case. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: local variable referenced before assignment
On 2007-10-25, Tim Williams <[EMAIL PROTECTED]> wrote: > On 25/10/2007, A.T.Hofkamp <[EMAIL PROTECTED]> wrote: >> On 2007-10-25, Pete Bartonly <[EMAIL PROTECTED]> wrote: >> > >> Also, brackets around conditions (in the if) are not needed, and comparing >> against None is usually done with 'is' or 'is not' instead of '==' or '!='. >> The result is then >> >> if logMsg is not None: > > Or just > >>>> if logMsg: > do_something() > >:) That is not the same. If logMsg is 0, False, or empty string, the second variant would be False and not True. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Readline and record separator
On 2007-10-30, Johny <[EMAIL PROTECTED]> wrote: > Is it possible to change record separator when using readline? > As far as I know readline reads characters until found '\n' and it is > the end of record for readline. > My problem is that my record consits several '\n' and when I use > readline it does NOT read the whole my record. > So If I could change '\n' as a record separator for readline, it > would solve my problem. > Any idea? Read large enough blocks of data, then use block.find() (in the string module) to find your delimiter, and split the block. Albert -- http://mail.python.org/mailman/listinfo/python-list
Distributed computation of jobs (was: Parallel Python)
On 2007-01-12, robert <[EMAIL PROTECTED]> wrote: >> >> [1] http://www.python.org/pypi/parallel > > I'd be interested in an overview. > For ease of use a major criterion for me would be a pure python > solution, which also does the job of starting and controlling the > other process(es) automatically right (by default) on common > platforms. Let me add a few cents to the discussion with this announcement: About three years ago, I wrote two Python modules, one called 'exec_proxy', which uses ssh to run another exec_proxy instance at a remote machine, thus providing ligh-weight transparent access to a machine across a network. The idea behind this module was/is that by just using ssh you have network transparency, much more light weight than most other distributed modules where you have to start deamons at all machines. Recently, the 'rthread' module was announced which takes the same approach (it seems from the announcement). I have not compared both modules with each other. The more interesting Python module called 'batchlib' lies on top of the former (or any other module that provides transparency across the network). It handles distribution of computation jobs in the form of a 'start-computation' and 'get-results' pair of functions. That is, you give it a set of machines it may use, you say to the entry-point, compute for me this-and-this function with this-and-this parameters, and batchlib does the rest. (that is, it finds a free machine, copies the parameters over the network, runs the job, the result is transported back, and you can get the result of a computation by using the same (uniq) identification given by you when the job was given to batchlib.) We used it as computation backend for optimization problems, but since 'computation job' may mean anything, the module should be very generically applicable. Compared to most other parallel/distributed modules, I think that the other modules more-or-less compare with exec_proxy (that is, they stop with transparent network access), where exec_proxy was designed to have minimal impact on required infra structure (ie just ssh or rsh which is generally already available) and thus without many of the features available from the other modules. Batchlib starts where exec_proxy ends, namely lifting network primitives to the level of providing a simple way of doing distributed computations (in the case of exec_proxy, without adding network infra structure such as deamons). Until now, both modules were used in-house, and it was not clear what we wanted to do further with the software. Recently, we have decided that we have no further use for this software (we think we want to move into a different direction), clearing the way to release this software to the community. You can get the software from my home page http://seweb.se.wtb.tue.nl/~hat Both packages can be downloaded, and include documentation and an example. The bad news is that I will not be able to do further development of these modules. The code is 'end-of-life' for us. Maybe you find the software useful, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: help
On 2007-11-05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > please open this link this is will help you > > http://www.55a.net > This one might help as well: http://www.python.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Design Patterns - composition vs. inheritance
On 2007-11-15, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > inheritance when an object's relationship to another object is 'is-a' > and composition when the relationship is 'has-a'. > > Since this is all new and I'm still learning, I was hoping someone can > give me some pointers on best practices on applying these ideas. If my > logic is incorrect on anything, please don't hesitate to tell me where > I'm wrong - I'm completely open to any help offered. > > As a very simplified example, if I had two classes, Pet and Owner, it > seems that I would not have Pet inherit from Owner, since a pet 'has > an' owner, but not 'is an' owner. If this is correct, does my code > below reflect this? I passed the owner object into the pet object's > constructor - is this the right way to do it? This is indeed one way. One of the things you enforce in this way that there is no Pet object without an owner. > Also, I've seen talk that ideally you shouldn't have too many "dots" > in your method calls, instead using delegates to the methods and > attributes. Can anyone elaborate on this? Ideally, should I be writing As with most design stuff, they are right and they are also wrong. It's a trade-off. They are right in the sense that if you change the structure of your links, you will break potentionally a lot of code. Imagine you have to ask a data base for the owner rather than having a direct link. All "pet.owner" references will need to be changed then. If you hide the connection inside your Pet, there is only one place that needs to be changed. They are wrong in the sense that it is not always appropiate for a Pet object to perform such a function. By hiding "self.owner.address" inside Pet, you are designing a smart pet that knows how to find the address of its owner. Unfortunately, smarter also means more complex, and at some point you will defeat the advantage of OO, namely spreading responsibilities, and having several simple objects that work together to realize your application. So there is a trade-off. There is no universal rule, it highly depends on your application, your future plans, and the lifetime of the app (to name a few). > getattr() methods so I can do pet.address instead of > pet.owner.address? Should I be doing the same with owner's methods > like I did with get_foo()? I'd say no. One of the 'rules' (guide lines) of Python is "Be explicit rather than implicit" [1]. You may save a few dots, on the other hand, you obfuscate how the link is realized (that is "pet.address" doesn't say it uses owner to make the connection, unlike "pet.owner.address"). In the long run, the latter may be more important. In general, I think you shouldn't need advanced trickery like __getattr__() for your design. If you do, your design is most likely wrong. [1]: The Zen of Python: http://www.python.org/dev/peps/pep-0020/ Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Web update library in python?
On 2007-11-20, Jorgen Bodde <[EMAIL PROTECTED]> wrote: > Hi all, > > I want to provide my users the ability to download a repository from > the web, and after that check for updates. I thought of a mechanism > that could do that, but since there is patch and diff readily > available I wondered if there is a python solution that allows me to > download a file, and let the patch be applied locally. In the patch > there are binaries and sources I am not sure if patch can handle them > both though. there is difflib > I want to have a solution that is very easy for the user, so point and > click kind of work. Is there a tool / library around that can provide > me a base for this problem? However, from your description, it looks like you are trying to achieve what many modern SCM tools do already out-of-the-box, so why are you trying to re-invent the wheel? For example, SVN handles anonymous download of working copies with HTTP, distributed SCM tools (bzr, git, mercurial, to name a few) allow you to copy complete repositories over the internet, and merge/update them (as well as sending patches back by eg email). Such a solution reduces your problem mostly to a one line script like 'svn update'. You may want to add a nice GUI front-end around it (although given the fact you are offering them a repository with sources (which, presumably is more advanced than point-and-click), I don't really see the need for a point-and-click solution). Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Web update library in python?
On 2007-11-20, Jorgen Bodde <[EMAIL PROTECTED]> wrote: > The 'repositories' can be created by anyone who likes to share their > code templates, and let the end user configure this template and > create a customized code base of it, on which they can code their > entire app. My tool supports incremental configuration which means if > there are updates to this repository, the user simply has to click > configure/generate again and merge the changes into their working > copy. SVN seems not useful in your situation, exactly because of the assumption of a single authorative repository that everybody uses, as you described. Instead, I think you should have a look at distributed SCM tools. Distributed SCMs do not have the concept of a central repo (that's why they are called 'distributed'), everybody can start their own repo (or copy one from elsewhere), hack away, and offer their changes to others. The system manages exchange of patches between the repositories. I have currently very limited experience with distributed SCMs. I just started experimenting locally with bzr. Please note that 'bzr repository' is comparable to a 'svn working copy'. There are no doubt subtle and not-so-subtle differences, but as I said, I have limited experience... The problem that I solved was that I wanted to have several personal sub-projects inside a SVN-based project (that I do not own and have no write access to). The changes of my sub-projects are then feed back into the main project and (hopefully) committed. Of course, these diffs should be against a recent SVN trunk of the project. I created a project_trunk directory with the anonymous SVN checkout. On top of svn, I added bzr management. (after 'svn update' I add/commit the changes also in bzr in the same directory). This directory now functions as an authorative source of bzr updates. Now, each personal sub-project is a copy of that bzr repo. I can do arbitrary code hacking, committing, running diff etc in each sub-project. I can also merge changes from the project_trunk directory with 'bzr merge' to pull in new updates from the SVN project. Although I haven't tried it, it should also be possible to copy/merge changes between my personal sub-projects. [hmm, while answering your post, I read some of the bzr website, and found that bzr has a standard tool for doing what I do... nice ] > I really thought about SVN but I would not like end-users to be > bothered with maintaining or installing SVN or a SVN server, and have > intermediate .svn local files inside every directory ;-) Now in your case, my 'project_trunk' would be your central web-site, and my personal sub-projects would be your users. Of course, the bandwidth between your website and users is a bit smaller than my local file system bandwidth, but given the fact that these SCMs are designed to work in a distributed way across the Internet, so I would expect them to handle this. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: marked-up Python code
On 2007-11-20, Luc Goossens <[EMAIL PROTECTED]> wrote: > Hi Tim, > > thanks for your suggestions > > I have two questions. > 1. can I color the background of the text keeping the normal syntax > coloring for actual text? can you give some hints on how to do that > in vim? :help syntax > 2. will the # mark-up lines show in the editor? is there some visual > clue that something has been hidden? yep > I will gladly settle for some pointer into the vim documentation, > which I found already. :help marker Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Web update library in python?
On 2007-11-20, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > Jorgen Bodde wrote: > >> Hi, A.T.Hofkamp (sorry for not knowing your first name ;-), Well Jorgen, it is at the bottom of each post (usually)... ;-) >> SCM sounds like a term I can google for, if the tool needed is very >> easy to install, maybe even accompany with my application to run as a >> service for the updating, it is worth a try to find something that can >> handle the synchronizing of distributed repositories. >> >> Talking a bit more about it and hearing various solutions gave me more >> insight in how it might be solved. Thanks everybody for your input! > > SCM mean source code management - like SVN or CVS. > > Diez Diez is right, except my choice of the phrase 'SCM' was not exactly right. SCM is a general term for two kinds of activities, namely version control (VC) and configuration control. VC is what everybody does with SVN, CVS, darcs, bzr, git, mercurial, etc in the context of software development (hence these tools are known as version control systems (VCS)). You may want to add 'distributed' to your search term. Configuration management is much less often done. It is about controlling deployment of some system or software. Imagine you are managing a few (hundreds) of web sites. They all use LAMP, but exactly what Apache, Py-Mod, Linux, hardware, is different each time. This of course also holds for the various initialization and configuration files. Keeping track of this data over time is called configuration management. (and if you think this is complicated, consider what Boeing is doing for all its air-planes... :) ). > SVN isn't distributed, but AFAIK darcs is. > As Diez already guessed, SVN and CVS are organized around a central repository, darcs, bzr, git, and mercurial do not need a central repository (although they often can handle one if the project wants it). The nice thing is that at least bzr and mercurial are written in Python!! What I found very enlightening was to find the web-site of these tools, and then read the docs about their strong and weak points w.r.t. their competitors. Of course each of these is biased, but if you read them all, it kind of balances out :) Also, they all explain what their world model is, you should check whether that matches with your problem. Good luck with your search, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: the annoying, verbose self
On 2007-11-22, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote: > >> Is there any trick to get rid of having to type the annoying, >> character-eating "self." prefix everywhere in a class? You got this highly flexible language, very good for rapid programming, no more clutter from block brackets and variable declarations, and now this 'self' pops up. So much overhead Can the computer not decide by itself what I want stored in the object? It can read my code too! > Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a > trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying. Yes annoying isn't it? Last week I was programming in C++ again, and now I have this terrible sur-plus of lowercase T H I and S letters. I don't understand how they do it (I used to, but not any more). Maybe we should setup an exchange program so everybody can trade letters with each other. >> Sometimes I avoid OO just not to deal with its verbosity. > > There are other values than brevity. In fact, brevity is one of the less > important values. NO! You got it all wrong here! It is not about brevity, it is about SPEED. With less letters, there is less to read, so it can be read faster, so it must be better! Just like "if x:" is MUCH BETTER than "if x != 0:" The thing that should be fixed imho, is that the brain of some of us tries to deduce higher levels of abstractions from what is essentially a very long line of simple instructions. >> But things grow -- is there any metaprogramming tricks or whatnot we can >> throw on the self? > > Oh yeah, that's just what I like to see! Complicated, brittle, hard to > debug, difficult to understand metaprogramming tricks in preference to a > simple, easy-to-read naming convention. Maybe we should state first which variables we want to store in self, and then have a meta-programming hook that automatically puts assignments to those variables into the object. And while we are at it, maybe we should also state the type of data we want to put in it. That should help too. Now that would be progress. Ah well, I guess we will have to wait a while before that happens. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Code Management
On 2007-11-24, BlueBird <[EMAIL PROTECTED]> wrote: > On Nov 21, 7:05 am, "Sergio Correia" <[EMAIL PROTECTED]> wrote: > And then you do your development in python-dev. But how do you manage > multiple development branches of the same program ? If you are using SVN, you may want to check out 'combinator' by Divmod (divmod.org). That tool manages SVN branches, and also switches Python when you switch branches. Very handy. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How to suggest a new Python list? Was: Science list
On 2007-11-29, J. Robertson <[EMAIL PROTECTED]> wrote: > Francesco Pietra wrote: >> I was trying to suggest a more specific mail-list in order not to be >> floaded. I >> am the opinion that python-list@python.org is very informative and useful, >> though it is hard to find the time for so many mails. >> f. >> > > I agree with Francesco: Python is increasingly used in sciences (or, at > least, I use it increasingly :-) and it would be of some use to have a > communication channel focused on this: there are a few mailing lists for > specific niche topics (CAD, XML) already, so sciences could be one more. So what is the focus in the list? I think 'science' is both too broad and in many cases not at all relevant for the problem at hand. For example, Should I be a scientist before I can post there? (If yes, what about: I work as supporting staff, am a PhD student, am a regular student, have a home work question, think of going to college, just learning Python that I may need at school). Any particular brand of science (CS, Math, Physics, Bio, Chemistry, other)? Should I use Python for programming a science problem? Should I have a problem in science and want to use Python for solving it? How are science problems any different from 'normal' problems? I am writing a parser for a language used in science, can I put it there? (and if yes, how is that different from writing a language for a non-science problem?) I am trying to run program XYZ as a sub-shell, or in a thread, or distributed. Does it make any difference whether XYZ solves a science problem or not? I don't see a clear useful line called 'science', but YMMV. > Some folks that do not want to be flooded by 200 posts/day are going to > pop up there and explain how they replaced 60,000 lines of FORTRAN by > 100 lines of Python in one afternoon (before the tea break), surely that > can't hurt. If the sheer number of posts is a problem, skip subjects that you don't like, just like everybody else. If your mailinglist idea fails you have a nicely quiet corner (but is otherwise useless). That is however more easily achieved by not reading c.l.p. If it succeeds, you may easily get the same amount of traffic as you have now here. So how is 'science' here a good criterium? > Anyway, I do not see how to suggest a new mailing list on > http://www.python.org/community/lists/ - does anyone know? Ask at c.l.p. ? :) Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Why Python 3?
On 2007-12-05, Chris Gonnerman <[EMAIL PROTECTED]> wrote: > I spent some time today reading about Python 3, and specifically the > differences between Python 3 and Python 2, and I was left with a > question... why? Why bother to change to Python 3, when the CPython > implementation is slower, and probably will be for a while? I think to preserve the smoothness of switching versions, like you do now. As user, you can smoothly 'upgrade' to using new Python features in the pace that you like. Unfortunately for the developers, not all users do that at the same rate and switch to the same sub-set (and that is good, I think). As a result, the developers have to be very backwards compatible. In other words, you can probably run a lot of the Python 1.5.2 code you wrote several years ago using today's Python 2.5. To make that possible, all 1.5.2 stuff is still in today's Python 2.5 interpreter, even though there are not many users that code in 1.5.2 style any more. As you can imagine, this old stuff piles up as we progress in Python versions. Python 3 is the breaking point where the old stuff (that (almost) nobody uses any more, since everybody is using new coding styles) is really gone. That gives room for a new design of the interpreter from the ground up, using today's coding practices and ideas as starting point. For you as user, the transition to Python 3 will probably be smooth too. Unless you stopped reading about new Python versions after 1.5.2, and are still using that old 1.5.2 book as ultimate Python reference, your coding style has changed too towards newer Python versions (and ultimately towards Python 3). In addition, knowing the Python development cycle, as the features of Python 3 become more clear, they will first be implemented in the Python 2.x range for testing and for giving users the chance to already pick up the new coding styles, so by the time the last 2.x version is retired, Python 3 will have (almost) no sudden transitions for you. > But... almost all of my old 1.5 code ported painlessly to 2.x. No need > for a "1.5to2" script, whereas I see that there is a "2to3" script for Well, Python 3 design is from the ground up, and aimed at the future, so they are quite a few steps ahead of today's coding practice, let alone today's code base (which is still 1.5 compatible as you discovered). To run any form of practical experiments, one needs a way to quickly convert the current code to the new conventions/ideas. Since programmers rather let the computer do boring repetitive tasks, they write a script for the conversions. Since Python is open source, you may also want to experiment with Python 3, and in that case the script is very handy. Imho, existence of such a script today does not automatically mean that you will need to use a script at the moment you (or I) switch to Python 3. > programming knowledge. In fact, the things I rarely or never use in > Python tend to be those things I find hardest to read (like list > comprehensions). Few of the changes along the way have required me to I use them a lot, and they are very powerful. On the other hand, I never use generators, which is probably a loss for me. > change how I *write* code; probably the worst was the integer division I think you change the way you write code continuously. I have been programming computers for 20+ years, and are still changing the way I code. > change, which I disagreed with, but I went along with the community. Some changes are for your own good, even though you do not realize it now :) For you it is the integer division. For me, I have a problem with new-style classes, where the __eq__ method is already implemented, blurring the difference between 'is' and '=='. Ah well, no language is perfect, and there is probably a very good reason for the change even if I don't see it. (and if there is really none, the change will be undone with Python 4... :) ) > I don't see myself using Python 3 for a long time. Probably as long as > I can hold out. Where are my goodies? What is my payoff for learning > how to write code the new way? I can't see it. Many things seem a lot A lot of the new goodies give you more punch per line, ie less lines to express what you want to calculate. This is good, since the chance making an error is a constant per line, so less lines is less errors. > less obvious... like, what was wrong with .keys() returning a > list? Now it returns some strange object type. In addition, Python is generalizing programming. Why would you want to write a for-loop yourself if you can push the loop into that strange object type? This may look useless, but what actually happens here is that the level of abstraction in programming is raised further (just like the main benefit of switching from eg C to Python is the step up in abstraction (you get eg dictionaries and lists built in, rather than having to program them yourself from struct's for the umpteenth time). Ultimately, wh
Re: My very first python web app (no framework)
On 2007-12-10, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > On 9 Dic, 15:43, [EMAIL PROTECTED] wrote: >> Is it the right way to go? Is it safe in a web production >> environment ? Is it thread-friendly (since flup is threaded) ? >> >> tnx > > Any hint ? If you as author are asking, my bet is on "no" for safety. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: __iadd__ useless in sub-classed int
On 2007-12-06, samwyse <[EMAIL PROTECTED]> wrote: > On Dec 6, 1:12 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > And that's my complaint. The value in is being replaced by > something almost, but not quite, identical to the original value. > Python's internal implementation of __iadd__ for isn't returning >, it's returning a new value belonging to the super-class. My > whole point is overloading was that I'd hoped to avoid having to > write a bunch of methods to perform in-place modifications. Looks > like I stuck, however. I think you don't want this. Suppose I keep track of addition information (eg a boolean "is_even = value == value//2") Since the base class doesn't know about this, it may return an incorrect instance. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Python too slow?
On 2008-01-09, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I'm pretty new to Python, and even newer to Image/Video processing, > and trying to get started on a project similar to GRL Vienna's laser > marker. I found some sample code here > http://janto.blogspot.com/2006/01/motion-capture-in-python.html, > but after running the code with the included sample input file, it > seems quite slow (1-2 seconds from start to finish to do the 800 by > 600 gif image). > Is there a better way to do color tracking, or is Python just too slow > as an interpreted language to do any effective color tracking? People seem quite obsessed with execution speed of CPU's. I have come to believe that it is only one part of the equation, development time and ease of maintenance or modification is another one, which I believe is much more interesting to perform as fast as possible. 1-2 seconds may be slower than some alternative solution (you could in principle develop custom hardware for it, and do the trick in a few nano-seconds). The question that arises (for me at least) is, how fast can you write, maintain, and modify what you want in that other solution. Suppose you can do color-tracking in 0.5 seconds. So, you gained 1.5 seconds CPU time. Now the question you need to answer for yourself, is how much more worth is your own time compared to the gain in CPU time. If you think they are equal (ie the problem as a whole should be solved as fast as possible, thus the sum of development time + execution time should be as short as possible), you can spend an additional 1.5 seconds development in the alternative solution. Of course, if you do more color-tracking, the differences between alternative solutions become larger, and your custom hardware solution may become a feasible alternative :) In my experience however, differences in CPU execution time are usually meaningless compared to differences in development time. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed
On 2008-01-11, tijo <[EMAIL PROTECTED]> wrote: > Hi mate > i created the socket and the connection with tcp and udp i dont know > how to check the bytes send and time > could you help me with this Have a look at the time or timeit modules. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: reading a specific column from file
On 2008-01-11, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > I have a file containing four columns of data separated by tabs (\t) > and I'd like to read a specific column from it (say the third). Is > there any simple way to do this in Python? > > I've found quite interesting the linecache module but unfortunately > that is (to my knowledge) only working on lines, not columns. > > Any suggestion? the csv module may do what you want. -- http://mail.python.org/mailman/listinfo/python-list
Re: where do my python files go in linux?
On 2008-01-12, Jorgen Bodde <[EMAIL PROTECTED]> wrote: > Question 1. Where do I put the bulk of python scripts in a normal > linux environment? > Question 2. Should I use *.pyc rather then *.py files to speed up > executing as the user cannot write to /usr/bin or any other dir in the > system and everytime my app runs it will recompile it > > Thanks for any advice or maybe a good tutorial how to set up files in > a linux environment Rather than re-inventing the wheel, please have a look at distutils: http://docs.python.org/lib/module-distutils.html It does most if not all of the things you want to do. If you want something more advanced, read about eggs. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: __init__ explanation please
On 2008-01-13, Erik Lind <[EMAIL PROTECTED]> wrote: > I'm new to Python, and OOP. I've read most of Mark Lutz's book and more > online and can write simple modules, but I still don't get when __init__ > needs to be used as opposed to creating a class instance by assignment. For > some strange reason the literature seems to take this for granted. I'd > appreciate any pointers or links that can help clarify this. I think you mean the following: You'd like to do p = Person('me', 'here', 31) and you are wondering why you need the __init__() function in class Person(object): def __init__(self, name, addres, age): self.name = name self.address = address self.age = age right? If so, the answer is that while you think you are doing "Person('me', 'here', 31)", you are in reality executing "Person.__init__(self, 'me', 'here', 31)", where 'self' is refers to a shiny new, empty object created for you. (and the 'self' is obtained by the Person.__new__ function I think, but others here have much better knowledge about this). Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create graphs an embed them in GUI?
On 2008-01-17, Heiko Niedermeyer <[EMAIL PROTECTED]> wrote: > As I'm learning Python from scratch, I don't care wether to use (=learn) > TKinter or PyQt or whatever, I just need some advice, which suits my > needs best. > It would be nice to have the programm working under win and linux > (shouldn't be a big Problem) and my requirements concerning the standard PyGTK is a 3rd option, and wxWindows + Python is a 4th option. TKinter is supplied with Python, which means everybody with Python also has TKinter. Main draw-backs are that it is quite old. Also, it has a peculiar way of getting stuff drawn at a canvas. PyQt is available free with some additional restriction (plz read the license) for the Linux system, I don't know whether you can also get a Win version under the same conditions (you couldn't when I looked the last time). PyGTK is said to be usable for both platforms. I know it works with Linux, and there exists a PyGTK installer for Win, but I hacve never used it. No recent experience with wxWindows. > My problem is, that I want to add graph (simple, line connected X,Y- > scatter plots) and if possible the 3D representation of atoms in a > molecule (-> coloured spheres in space). You should probably seperate both problems, in particular if you want to have the program do the layout for you. For 2D layout, Graphviz is one of the better known packages, run it as a child process. There are several graphviv/dot Python libraries available, search PyPI for them. For 3D, I don't know any programs. > I think it would take me years to program those by myself, so I would ne > ready to use packages, if available. > Long story short: Are there packages that could do this, and does it > matter which GUI I want to embed them in? If you want a GUI that understands how to layout chemical structures, you won't have many options (on the other hand, you never know, have you tried searching PyPI already?). On the other hand, once you have the coordinates, drawing them is kind of trivial in just about any GUI toolkit. (An alternative may be to have the user lay them out by dragging them with the mouse. Programming that is however probably a lot more work.) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a HTML parser who can reconstruct the original html EXACTLY?
On 2008-01-23, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, I am looking for a HTML parser who can parse a given page into > a DOM tree, and can reconstruct the exact original html sources. Why not keep a copy of the original data instead? That would be VERY MUCH SIMPLER than trying to reconstruct a parsed tree back to original source text. sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a HTML parser who can reconstruct the original html EXACTLY?
On 2008-01-23, kliu <[EMAIL PROTECTED]> wrote: > On Jan 23, 7:39 pm, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: >> On 2008-01-23, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> > Hi, I am looking for a HTML parser who can parse a given page into >> > a DOM tree, and can reconstruct the exact original html sources. >> >> Why not keep a copy of the original data instead? >> >> That would be VERY MUCH SIMPLER than trying to reconstruct a parsed tree back >> to original source text. > > Thank u for your reply. but what I really need is the mapping between > each DOM nodes and > the corresponding original source segment. Why do you think there is a simple one-to-one relation between nodes in some abstract DOM tree, and pieces of source?, For example, the outermost tag ... is not an explicit point in the tree. If if it is, what piece of source should be attached to it? Everything? Just the text before and after it? If so, what about the source text of the second tag? Last but not least, what do you intend to do with the source-text before the and after the tags? In other words, you are going to have a huge problem deciding what "corresponding original source segment" means for each tag. This is exactly the reason why current tools do not do what you want. If you really want this, you probably have to do it yourself mostly from scratch (ie starting with a parsing framework and writing a custom parser yourself). That usually boils down to attaching source text to tokens in the lexical parsing phase. If you have a good understanding of the meaning of "corresponding original source segment", AND you have perfect HTML, this is doable, but not very nice. There exist parsers that can do what you want IF YOU HAVE PERFECT HTML, but using those tools implies a very steep learning curve of about 2-3 months under the assumption that you know functional languages (if you don't, add 2-3 months or so steep learning curve :) ). If you don't have perfect HTML, you are probably more or less lost. Most tools cannot deal with that situation, and those that can do smart re-shuffling to make things parsable, which means there is really no one-to-one mapping any more (after re-shuffling). In other words, I think you really don't want what you want, at least not in the way that you consider now. Please give us information about your goal, so we can think about alternative approaches to solve your problem. sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Test driven development
On 2008-01-24, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I like the concept of TDD but find it difficult to put into practice > most of the time. I think this primarily because I tend to like top- > down development and functional/object decomposition and TDD feels > more like a bottom-up approach. It is not bottom-up imho. The main difference imho is in abstract, far-away goals versus running code for a small concrete sub-set of functionality in the short-term. How you provide that "running code for a small concrete sub-set of functionality in the short-term" is not defined by the approach. > So my question is when approaching a project that you want to employ > test driven development on how and where do you start? And also if 1. Define a small step of extended functionality that you can finish by the end of the week. 2. Write tests that should run after implementing the functionality. 3. Write code that make the tests work. 4. goto 1. > anyone uses top-down design with TDD I would be interested in how you > do it (does it involve lots of mock objects/ is the first test you > write the last one to pass)? No, with TTD there is only one goal, namely increasing the percentage of passed tests to 100%. (which you then break again by doing steps 1 and 2, and fix again in step 3) TTD is about DESIGNing for WHAT YOU NEED TODAY, not for what you might need tomorrow. You may want to do a small top-down design (and next week another slightly larger one, and another slightly larger one in 2 weeks, etc), but you should NOT DESIGN FOR MORE FUNCTIONALITY THAN WHAT YOU NEED for all tests to pass. Sincerely, Albert PS The above is purely from a TTD perspective. I personally do not really think you can SOLVE ALL PROBLEMS in the most efficient way using TTD. On the other hand, the concrete, short-term approach does produce running code fast for the implemented sub-set of functionality, which may be nice in a project. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python noob SOS (any [former?] Perlheads out there?)
On 2008-01-30, grflanagan <[EMAIL PROTECTED]> wrote: > On Jan 29, 5:39 pm, kj <[EMAIL PROTECTED]> wrote: > For command line options I get a long way with this: > > [code python] > def _getargs(): > allargs = sys.argv[1:] > args = [] > kwargs = {} > key = None > while allargs: > arg = allargs.pop(0) > if arg.startswith('--'): > key, arg = arg.split('=', 1) > key = key[2:] > elif arg.startswith('-'): > key = arg[1:] > if not allargs or allargs[0].startswith('-'): > allargs.insert(0, 'yes') > continue > if key is None: > args.append(arg) > else: > kwargs[key] = arg > key = None > return args, kwargs > > ARGS, KWARGS = _getargs() > [/code] Have a look at getopt (old) or optparse (newer) packages in the Python library instead. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: CVS access with Python
On 2008-02-07, Steve Holden <[EMAIL PROTECTED]> wrote: > Ravi Kumar wrote: >> I have to design a Web-based CVS client. I could not find any module, >> cvs-binding in python. There isn't any afaik. CVS was never designed with scripting in mind. You'll have to issue the command, then parse the textual ouput. Some time ago, a new CVS clone was started with the BSD OSes. You may want to check that out. >> an archive (tar.gz/zip) and put that for downloading by the user. Since >> there is many-to-many relation between projects and developers, it will >> consume a lot of disk space on Web-server, creating 'n' copies of a >> single project for 'n' users, and assume all developers are checking out >> all sources. God knows. Euh, why? You have a CVS repository sitting around the corner, just check out what you need when you need it (and throw it away afterwards, it is reproducible). If you need to keep special versions for each dev (in each project maybe), set up a tagging system, and commit the new versions tagged to the repository. >> So is there any good implementation for such situation. Yes, it is called Subversion (SVN, http://subversion.tigris.org/), which does all that CVS does (in a much much nicer way), and more, including networking over the HTTP protocol (securely, if you want). It works out of the box, is used with thousands of projects, and will save you and others hundreds of hours time. Also, there are several graphical clients for it (both Windows and Linux, and quite likely Mac), and there is a cvs2svn tool. > Suppose a user has her own CVS client repository on the web, how is she > supposed to edit and compile the files? Or is this not a programming > application? Ravi Kumar: Another problem, how are you going to deal with merge conflicts? I don't see a useful way of resolving those through a web interface. Maybe a webinterface is not the answer to your problem? Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary of operators
On 2008-02-14, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, > > In the standard library module "operator", it would be nice to have a > dictionary > mapping operators strings with their respective functions. Something like: > > { > '+': add, > '-': sub, > 'in': contains, > 'and': and_, > 'or': or_, > ... > } > > Does such a dictionary already exist? Is it really a good and useful idea? How would you handle changes in operator syntax? - I have 'add' instead of '+' - I have U+2208 instead of 'in' I don't think this is generally applicable. Why don't you attach the function to the +/-/in/... token instead? Then you don't need the above table at all. sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: List behaviour
On 2008-05-15, Gabriel <[EMAIL PROTECTED]> wrote: > Hi all > > Just wondering if someone could clarify this behaviour for me, please? > tasks = [[]]*6 tasks > [[], [], [], [], [], []] tasks[0].append(1) tasks > [[1], [1], [1], [1], [1], [1]] > > Well what I was expecting to end up with was something like: > ?>>> tasks > [[1], [], [], [], [], []] > > > I got this example from page 38 of Beginning Python. This is a more complicated case of a = [] b = a a.append(1) print b # Will print "[1]" This is the case, because both a and b refer to the same list data-value. In your case, basically what you are doing is a = [] # a is an empty list (introduced for easier explanation) tasks = [a] # tasks is a list with 1 'a' tasks = tasks*6 # you create 5 additional references to 'a' in 'tasks ie tasks is now the equivalent of [a, a, a, a, a, a]. It refers to the same 'a' list 6 times. When you print 'tasks', you actually print the same 'a' value 6 times. in particular, tasks is **NOT** a,b,c,d,e,f = [], [], [], [], [], [] # create 6 different empty list values tasks2 = [a, b, c, d, e, f] although when you print both tasks, you won't see the difference. Next, 'tasks[0]' refers to the first list element, that is, value 'a'. To that list you append an element. In other words, you do "a.append(1)". However, since tasks has 6 references to the same list 'a', all its members appear to be changed (but you are really displaying the same value 6 times). You can query this equality with 'is': print tasks[0] is tasks[1] # will print 'True' print tasks2[0] is tasks2[1] # Will print 'False' Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: managing properties/configurations
On 2008-05-16, Venkatraman.S. <[EMAIL PROTECTED]> wrote: > Or a better example would be: Indeed, this is concrete enough to make some suggestions. > I have the params in a config file and import this module: > myconfig.py > a=10 > b=30 > c=31 > d=40 The big problem imho with coding such stuff directly in Python is that you have no way to extract the data from the code to display it to your users without exposing them to Python code. Why not instead throw this in a ConfigParser file like below, and load the data values from it (each time they have changed?) [score] a=10 b=30 c=31 d=40 By picking better names, the config gets much more readable. The big advantage here is that a config file is something readable and editable without appearing it to be Python. If this is too low level for your users, you can use it as a data exchange format between the processing application and the frontend application where users can change the values. > import myconfig > def checkCutoff(self,up,down): >.do some processing >if (a <= score <= b): > result="Bad" >elif (c <= score <= d): > result="Good" >.do some processing >return result > > Now when i 'manually' make some changes to the value of a,b,c,d then > the the checkCutoff func should refer to the new values. Maybe reload the file each time you run the program? Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
default object comparison considered harmful?
Hello all, Yesterday we found the cause of a bug that has caused problems for a long time. It appeared to be the following: class A(object): pass print min(1.0, A()) which is accepted by Python even though the A() object is not numerical in nature. The cause of this behavior seems to be the compare operation of the object class. Is there a way to disable this behavior in Python (other than deriving a new 'object-like' class that doesn't do comparisons?) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: managing properties/configurations
On 2008-05-16, Venkatraman.S. <[EMAIL PROTECTED]> wrote: > The problem being, if i change the config file, then the configobj has > to reload this file again. I do not want to 'refresh' the config obj > per transaction but only when the config params change. If you have trustable time stamps at your file system, you could check the time stamp before loading. > I was thinking along the terms of an Interrupt driven program, which > when getting a custom interrupts reloads the file - any potential > loopholes/falls that anyone can anticipate? Many unix deamons use SIGHUP signal to reload configs. Maybe that would be an option? Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: default object comparison considered harmful?
On 2008-05-16, Kay Schluehr <[EMAIL PROTECTED]> wrote: > On 16 Mai, 10:03, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote: >> Hello all, >> >> Yesterday we found the cause of a bug that has caused problems for a long >> time. >> It appeared to be the following: >> >> class A(object): >> pass >> >> print min(1.0, A()) >> >> which is accepted by Python even though the A() object is not numerical in >> nature. >> >> The cause of this behavior seems to be the compare operation of the object >> class. >> >> Is there a way to disable this behavior in Python (other than deriving a new >> 'object-like' class that doesn't do comparisons?) >> >> Sincerely, >> Albert > > Are you sure you don't want to use a statically typed language that > captures all type errors just by inspecting your source code? yes. The problem here is that it isn't caught at all, neither at 'compile' time nor at run-time. That means that the Python language considers this proper code. Whether you make that decision by inspecting source code or at run-time is irrelevant here. Unfortunaly, we have to maintain Python 2.3 compability. As a solution, I have created a new BaseObject class as follows: class BaseObject(object): """ Generic base class without default compare and hashing functions. """ def __cmp__(self, other): """ Disable default compare method """ raise NotImplementedError def __hash__(self): """ Disable default hashing method """ raise NotImplementedError("Implement me in class '%s'" % self.__class__.__name__) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Organizing a Python project
On 2008-05-19, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello all, > > I'm starting work on what is going to become a fairly substantial > Python project, and I'm trying to find the best way to organize > everything. The project will consist of: > > - A few applications > - Several small scripts and utilities > - Unit tests and small interactive test programs > - A number of custom libraries and modules that may be shared and > referenced among all of the above > > I have the following general structure in mind: > > myproject/ > app1/ > main.py > file1.py > file2.py > tests/ > test_abc.py > test_xyz.py > app2/ > ... > scripts/ > script1.py > script2.py > shared/ > mylib1/ > file1.py > file2.py > tests/ > test_foo.py > test_bar.py > mylib2/ > ... > > > The files that you might want to execute directly are: > - Any of the "main.py" files under app*/ > - Any of the files under shared/ > - Any of the files under app*/tests or shared/mylib*/tests > > So, my questions: > First of all, does this look like a reasonable overall structure, or > are there better alternatives? You could make a 'bin' directory next to 'myproject' with executable programs which would usually do something like #!/usr/bin/env python from myproject.app1 import main main.run() to make a more clear seperation between code that can be executed and code that is imported in an application. Also, why do you make a distinction between shared and non-shared code? You could simply eliminate 'shared' directory, and put its contents directly under myproject. > Second (and the thing I'm primarily interested in), what is the best > way to deal with importing the shared modules in the applications, > scripts, test programs, and possibly other shared modules? I think the > most obvious solution is to add /path/to/myproject to PYTHONPATH. > However, this seems like an annoying little dependency that you are > likely to forget whenever you move your workspace to a new path, open > up a branch of the project in a different directory, or download and > work on the project using a different computer. What I am missing here is how you plan to do the development. If you want to do branch-based development, you may want to have a look at Combinator (at divmod.org). It handles branch management, adds executable programs from bin to your path (in your current branch), and extends PYTHONPATH (with your current branch). Even if you have just 1 branch (namely 'trunk') it may be useful. > Is there a way to set this up that is a bit more self contained? For > example, at first I was somewhat hopeful that Python could ascend > parent directories until it reached a directory that did not include > an __init__.py file, and it could use this as a root for referring to > packages and modules from any file contained within. (e.g. in the > example project above, any file could refer to myproject.shared.mylib1 > so long as 'myproject' and all subdirectories contained an > __init__.py, and the parent of 'myproject' didn't contain such a > file). Evidently this is not the case, but it seems like it could be a > useful feature in these situations. Work is being done on relative imports. Not sure of its state. > Anyway, I'm sure this is not an unusual situation, so I'm curious to > hear how other people have handled it. Most people probably run scripts from the root, ie where 'myproject' is a sub-directory. Since Python automatically adds '.' to its path, it will work. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: how to proccess the blank in the path on linux
On 2008-05-21, zhf <[EMAIL PROTECTED]> wrote: > I want ro walk a directory and its sub directory on linux, > to find some shell script file, and run them, but I found some path belong > blank charactor, such as '8000 dir', if I write as follow, I got error > "no such file" > path = '8000 dir' > for root, dirs, files in os.walk(path): > cmd = ' ' > cmd = 'cd ' + root > os.system(cmd) > > How can I repair it? Escape the space to prevent the shell from interpreting it as a word seperator. This of course also holds for all other shell meta characters, such as * [ ] \ > < & and a few others I probably have forgotten. I am not sure why you execute 'cd' commands in a sub-shell, as it is not very useful. (that is, you start a new sub-process, in that sub-process you run the 'cd' command (causing the cwd of the sub-process to change directory), and then the sub-process ends) You most likely want to change the cwd of the Python process. If so, have a look at os.chdir(). That function has the added advantage that there is no shell in between that interprets all those meta characters (that is, "os.chdir('8000 dir')" will work without further effort). Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Code For Five Threads To Process Multiple Files?
On 2008-05-21, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I'd appreciate any help. I've got a list of files in a directory, and > I'd like to iterate through that list and process each one. Rather > than do that serially, I was thinking I should start five threads and > process five files at a time. > > Is this a good idea? I picked the number five at random... I was Depends what you are doing. If you are mainly reading/writing files, there is not much to gain, since 1 process will already push the disk IO system to its limit. If you do a lot of processing, then more threads than the number of processors is not much use. If you have more 'burtsy' behavior (first do lot of reading, then lot of processing, then again reading, etc), then the system may be able to do some scheduling and keep both the processors and the file system busy. I cannot really give you advice on threading, I have never done that. You may want to consider an alternative, namely multi-tasking at OS level. If you can easily split the files over a number of OS processes (written in Python), you can make the Python program really simple, and let the OS handle the task-switching between the programs. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Loading contents behind the scenes
On 2008-05-22, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi, I wanted to know how cautious it is to do something like: > > f = file("filename", "rb") > f.read() > > for a possibly huge file. When calling f.read(), and not doing > anything with the return value, what is Python doing internally? Is it > loading the content of the file into memory (regardless of whether it > is discarding it immediately)? I am not a Python interpreter developer, but as user, yes I'd expect that to happen. The method doesn't know you are not doing anything with its return value. > In my case, what I'm doing is sending the return value through a > socket: > > sock.send(f.read()) > > Is that gonna make a difference (memory-wise)? I guess I'm just > concerned with whether I can do a file.read() for any file in the > system in an efficient and memory-kind way, and with low overhead in > general. (For one thing, I'm not loading the contents into a > variable.) Doesn't matter. You allocate a string in which the contents is loaded (the return value of 'f.read()', and you hand over (a reference to) that string to the 'send()' method. Note that memory is allocated by data *values*, not by *variables* in Python (they are merely references to values). > Not that I'm saying that loading a huge file into memory will horribly > crash the system, but it's good to try to program in the safest way > possibly. For example, if you try something like this in the Depends on your system, and your biggest file. At a 32 bit platform, anything bigger than about 4GB (usually already at around 3GB) will crash the program for the simple reason that you are running out of address space to store bytes in. To fix, read and write blocks by specifying a block-size in the 'read()' call. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get all the variables in a python shell
On 2008-05-29, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi! > > I'm currently working on a scientific computation software built in > python. > What I want to implement is a Matlab style command window <-> > workspace interaction. ok, although I personally favor the style of writing and running a script/program, since it scales much better (you can easier automate steps), and it is much easier reproducible (something you probably want in scientific software) and storable (in a VCS). > For example, you type 'a=1' in the command window, and you see a list > item named 'a' in the workspace. > You double click the icon of the item, and you see its value. You can > modify the value of the list item, > 1 -> 100 etc, after which if you go back to the command window and > type 'a' and press enter, you see that > varable a's value has been changed to 100. I do hope you have made a fair estimate of the amount of work that it costs to change the value of a variable in this way. I would propose to simply use the interactive Python prompt. It doesn't give you popup icons for clicking, but you do get the entire Python interpreter, and all its libraries for free. The Python library has a frame work for customizing the interpreter. Have a look at 'cmd' module. > So my question is : if you have two DOS command windows running under > WINDOWS OS, how can you make them share the same internal variable > buffer? Or is there any easier way to implement such kind of > interaction? Now you have lost me. One window is not enough for interaction? Obviously, you'll need to have a common interpreter/storage backend. One solution may be to have a common execution back-end, and for each window a 'frontend' which passes commands entered to the back-end, and echoes results from the back-end to the terminal. > Maybe I could just build a small database to store all the values and > access them from both programs, but chances are sometimes I have to > deal with big arrays, and they will eat extra memory if I keep them in They eat memory when you keep them in a data base? It seems, you are making assumptions about implementations here without telling them. (ie pick a data base that uses a disk, and your problem is solved. Why is that not an option?) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with PEXPECT in Python
On 2008-06-04, Mallikarjun Melagiri <[EMAIL PROTECTED]> wrote: > Hi Noah, > > I am new to python. I'm trying to use pexpect. > > Following is my problem definition: > > I should have a script on my machine A, which > should 'ssh' to machine B and from there it shud copy a file to machine > C thru 'scp'. > > Please help me. We don't do your work, unless you pay us. Instead, start solving the problem. Think about it, start experimenting/programming, for example first a ssh connection to B (or even a ssh connection to A would already do as first step). When you get stuck, post - the code - if Python produces an error, the PRECISE and COMPLETE error message, - a description of why you think it is a problem - a description of what you expect/want. On such specific problems you will receive good and useful answers. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: configure fails
On 2008-06-05, Mathieu Prevot <[EMAIL PROTECTED]> wrote: > I have the following error on a OSX.5 OS with CC=icc and using the > python-svn files: > > checking size of wchar_t... configure: error: cannot compute sizeof (wchar_t) > > I would like to help so we can compile python with icc/OSX. This looks like a autoconf bug (./configure is normally created by autoconf) . You should check whether this wchar_t configure computation is part of the standard autoconf macro's or a Python-project specific autoconf macro. Then fix the problem and supply a patch to the right community. You may want to subscribe to some autoconf mailing list Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Dumb idea?
On 2008-06-10, Peter Hunt <[EMAIL PROTECTED]> wrote: > Hi everyone - > > I like playing around with language syntax and semantics. I'm thinking > about pulling down the PyPy code and messing around to see what I can > accomplish. My first idea is most succinctly described by example: > > class IBlockProtocol: > def __block__(self, func): > # NO RETURN VALUES! > pass You have lost me here. What is this supposed to be doing? To me you introduce a new magic __block__ function in a class that does nothing. And then you go on and on with implementation details and mappings of some BlockProtocol to other syntax without an explanation of what you are blocking (in English text), or what you aim to achieve by blocking. In other words, your post is to me the same as class A(object): def q(self, t): # do nothing pass and the question "did anybody invent q_protocol" already? Your guess is as good as mine (and probably better since you have a semantics for the blocking protocol in the back of your mind). Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Showing a point in Gnuploy.py
On 2008-06-16, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hello. Could some1 tell me how i could "display" a specific point in > gnuplot.py. Supposingly if i have a point of intersection (2,3). How > can i show this on the graph? As in how can i write near the point of > intersection the value :(2,3). > Thnx 1. Find out in the Gnuplot manual what command to enter 2. Send the command to Gnuplot using g() function-call -- http://mail.python.org/mailman/listinfo/python-list
Re: Numeric type conversions
On 2008-06-17, John Dann <[EMAIL PROTECTED]> wrote: > I'm reading in a byte stream from a serial port (which I've got > working OK with pyserial) and which contains numeric data in a packed > binary format. Much of the data content occurs as integers encoded as > 2 consecutive bytes, ie a 2-byte integer. [snipperdesnip] > Can anyone point me in the right direction towards a suitable function > please? The ctypes module should be helpful here Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: dict order
On 2008-06-18, Robert Bossy <[EMAIL PROTECTED]> wrote: > Hi, > > I wish to know how two dict objects are compared. By browsing the > archives I gathered that the number of items are first compared, but if > the two dict objects have the same number of items, then the comparison > algorithm was not mentioned. You could consider hashing, if the order of hashing is independent on the end result and it is fast enough. Different hash results are different dicts, equal hashes give you no conclusion. Then, you are probably down to comparing keys (since keys are unique and the number of keys in both dicts are the same, one direction of comparing is enough) and values. Since dict is designed to be fast in accessing keys, the performance should be not entirely disastreus. > domain-specific language where there's a data structure similar to > python dict and I need an source of inspiration for implementing > comparisons. If you have a choice in data structures, you may want to have an ordered dictionary (or sorted dict), where keys are sorted, making it much easier to decide about equality. The price you pay at least is less fast dictionary modifications and possibly also less fast key access although that also depends on the speed of hashing the key. There are also discussions about ordered dictionaries in Python (I believe they are called OrderedDict or SortedDict). There is also a PEP about them. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting Python exit code when calling Python script from Java program
On 2008-06-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I have a Python script which is used to load data into a database. Up to > now this script has been run by customers from the Windows command > prompt using "python edg_loader.pyc". Any error messages generated are > written to a log file. A project team working in the same company as me > here would like to use this loading utility. They write UI applications > for Windows using Java. They were able to launch the Python script from > within Java by creating a Process using Java ProcessBuilder class. > However, the way the error handling is currently implemented isn't > really suitable for use in a UI application. As I'm sure you can > appreciate it's not really feasible to tell users of a UI program to > keep checking the log files while the loading is underway!!. Ideally > they would like the Python loading utility to return an error code and > error message - the error message could then be displayed on a message > box on the UI. The first thing to do is decide whether this is a Java problem, a Python problem, or a OS problem. Then post the question in the appropiate forum. One simple experiment may be to write a C function that returns a non-zero exit code, and run that as job. > I seem to be having problems implementing this. I tried using the > sys.exit() method in my script and passed non -zero values. However the > value wasn't picked up the by Java Process.exitValue() method - it kept What did the OS say? Run a program like below in OS ('python x.py'), then query the OS about the exit code of the program. In that way, you can narrow down your search. > picking up 0. On investigation it turned out that the exit value being > read is from python.exe process, not from the Python script. Is there > any way I can obtain the return value of a python script from a Java This is not what I see happening here: x.py: import sys sys.exit(138) % python2.4 x.py % echo $? 138 as you can see, the mechanism works at my Linux system. > Java Process.getErrorSteam() method. > However I would really like to get the return codes working if possible > and would appreciate any suggestions on how to implement this. I'd suggest to first find out where in the Java->OS->Python->OS->Java chain things go wrong. As for how to handle a child-process from Java, try asking in a Java news group. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: dict order
On 2008-06-18, Robert Bossy <[EMAIL PROTECTED]> wrote: > Lie wrote: >>> Whoops, I think I misunderstood the question. If what you're asking >>> whether two dictionary is equal (equality comparison, rather than >>> sorting comparison). You could do something like this: >>> > Testing for equality and finding differences are trivial tasks indeed. > It is the sort order I'm interested in. The meaning of the order is not > really an issue, I'm rather looking for a consistent comparison function > (in the __cmp__ sense) such as: > if d1 > d2 and d2 > d3, > then d1 > d3 > > I'm not sure the hashing method suggested by Albert guarantees that. I read the post as the desire to test equality between dictionary-like data structures. With some care (see below) (and the ability to compute the hash fast), you could use hashing as way to decide that two such structures are not equal. Afaik you cannot use hashing for testing order ('<' or '>'). If I gave that impression, sorry; it was not my intention. In the equality case, you need special care with computing the hash value of the keys (and values), since you want to have the same hash result of the dictionary independent of the order of the keys. One way of achieving that is to use XOR (^) to combine hash-values of the elements. Unfortunately, XOR may not always produce good hash values. If you want ordered dictionaries (as in you can compare dictionaries with each other, and decide which is larger), I'd suggest to keep the keys of the dictionaries sorted. Dictionary comparing can then be done by comparing keys in increasing order (for example). If you encounter two non-equal keys, you immediately can use the order of those two keys as the order of the dictionaries. (ie the order of two dictionaries is decided by the order of the first non-equal keys). This gives you the transitive property (d1 > d2 and d2 > d3 implies d1 > d3). (and len() is a cheap first order filter here; dictionaries are ordered by length first, and by first non-equal keys second then.) I have used this trick to define ordered sets (which are basically dictionaries without value part). It worked like a charm. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: very large graph
On 2008-06-24, MRAB <[EMAIL PROTECTED]> wrote: > On Jun 24, 1:26 am, [EMAIL PROTECTED] wrote: >> I need to represent the hyperlinks between a large number of HTML >> files as a graph. My non-directed graph will have about 63,000 nodes >> and and probably close to 500,000 edges. >> >> I have looked into igraph (http://cneurocvs.rmki.kfki.hu/igraph/doc/ >> python/index.html) and networkX (https://networkx.lanl.gov/wiki) for >> generating a file to store the graph, and I have also looked into >> Graphviz for visualization. I'm just not sure which modules are >> best. I need to be able to do the following: Afaik Graphviz is not good at abstracting the graph, which you may need here. A page with 63,000 circles on it, and 500,000 edges will probably come out of the printer as a black sheet of paper. (8"x11" paper, 1 circle is 1/5", then you have only 2200 circles at one sheet. You need a factor 28 more circles which leads to a circle of about 0.007".) Even when actual paper format would not be a problem, you will need some abstraction and/or tools, as you will not find anything manually in an ocean of 63,000 elements. One area you may want to start looking for tools is in state graphs, where the set of possible states of an entire system is unfolded. These things go up to over a million states, so you only have a 'small' problem there... >> 1) The names of my nodes are not known ahead of time, so I will >> extract the title from all the HTML files to name the nodes prior to >> parsing the files for hyperlinks (edges). >> >> 2) Every file will be parsed for links and nondirectional connections >> will be drawn between the two nodes. >> >> 3) The files might link to each other so the graph package needs to >> be able to check to see if an edge between two nodes already exists, >> or at least not double draw connections between the two nodes when >> adding edges. >> >> I'm relatively new to graph theory so I would greatly appreciate any >> suggestions for filetypes. I imagine doing this as a python >> dictionary with a list for the edges and a node:list paring is out of >> the question for such a large graph? > > Perhaps a dictionary where the key is a node and the value is a set of > destination nodes? For undirected edges, you could make an Edge class and have a set of Edge's (where two Edge objects are equal when they connect the same nodes). I don't expect 500,000 elements in a set to be a problem. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between two dates
On 2008-06-24, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Hi! > > I am new in Python, so I count for your help. I need to get difference > in months between two dates. How to do it in python? I am substracting > two dates, for example date1 - date2 and I got result in days, how to > change it? Check the 'datetime' module. (in general, for any given problem you have a 70% chance that somebody has written a module for it. Check the standard library, or else PyPI.) Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem found in tutorial
On 2008-06-25, John W. Hamill <[EMAIL PROTECTED]> wrote: > 20JUN2008 > By John W. Hamill > > > Errata found in Python tutorial > http://www.python.org Bugs and other problems should be reported in bugs.python.org Otherwise they will probably get lost. > Error Found by John W. Hamill > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > - - > C:\__jh\ftp\python\2_5_2\doc\tutorial\node11.html Next time, please also add a URL that is usable for a larger group of people than those that have access to your C: drive. (Generator expression examples, Section 9.11, see http://docs.python.org/tut/node11.html#SECTION000) unique_words = set(word for line in page for word in line.split()) > valedictorian = max((student.gpa, student.name) for student in > graduates) > > NOTE: page and graduates are not defined and this won't run without them. >I defined them like so to make this work: Correctly seen. Report at bugs.python.org ! > page = ("The quick brown","fox jumped over","the lazy dog's","back 123 > times." ) > > class Graduate: Always derive new classes from object as in class Graduate(object): > def __init__(self, name, gpa): > self.name = name > self.gpa = gpa and indentation is normally 4 spaces, at least in public Python documentation. > gpa = 0 > name = "" Why do you introduce two class variables with the same name? (not useful, you can delete them) > > graduates = (Graduate("Charlie Brown",8.09), Graduate("Snoopy",3.7), > Graduate("Lucy Brown",3.5)) > Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: insertion sorts...
On 2008-06-25, python_newbie <[EMAIL PROTECTED]> wrote: > On 24 Haziran, 04:33, Terry Reedy <[EMAIL PROTECTED]> wrote: > Thanks for all answers. At the end i ve only one point. If a decide to > copy list to iterate when will i have to do this ? Before the > iteration ? And then iterate through one list and change value of the > other ? Before starting the iteration would be a good point I usually do in such cases: for x in mylist[:]: ... making a copy just before the for loop starts. Lately, I have started avoiding in-place modification of lists. Instead, I construct a new list from scratch inside the for-loop, and replace the old list with the newly constructed list afterwards like: new_list = [] for x in mylist: new_list.append(x) mylist = new_list by appending a different value than the original or by not appending, you can influence the contents of the new list. I find this solution nicer than in-place modification of an existing list. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: python -regular expression - list element
On 2008-06-25, antar2 <[EMAIL PROTECTED]> wrote: > I am a beginner in Python and am not able to use a list element for > regular expression, substitutions. > > list1 = [ 'a', 'o' ] > list2 = ['star', 'day', 'work', 'hello'] > > Suppose that I want to substitute the vowels from list2 that are in > list1, into for example 'u'. > In my substitution, I should use the elements in list1 as a variable. I read this as: for each string in list1, search (and replace with 'u') the matching substrings of each string in list2. Since list1 contains only strings instead of regular expressions, you could use string search and replace here. This makes matters much simpler. > I thought about: > > for x in list1: >re.compile(x) re.compile() returns a compiled version of the RE x. Above you don't save that value. Ie you do something similar to 1 + 2 * 3 where the value 7 is computed but not saved (and thus immediately discarded after computing by the Python interpreter). Use something like compiled_x = re.compile(x) instead. 'compiled_x' is assigned the computed compiled version of string x now. > for y in list2: >re.compile(y) The RE module finds matches in strings, not in compiled RE expressions. Since you want to search through y, it has to be a string. > if x in y: Here you test whether the letter in x occurs in y (both x and y are not changed by the re.compile() call, since that function does not alter its arguments, and instead produces a new result that you do not save). Maybe you thought you were checking whether a RE pattern match would occur. If so, it is not useful. Testing for a match takes about the same amount of time as doing the replacement. > z = re.sub(x, 'u', y) > but this does not work Instead of "re.sub(x, 'u', y)" you should use "compiled_x.sub('u', y)" since the former repeats the computation you already did with the re.compile(x). Otherwise, the code does work, and the new string (with replacements) is saved in "z". However, since you don't save that new value, it gets lost (overwritten). You should save "z" in the original list, or (recommended) create a new list with replaced values, and replace list2 after the loop. Sincerely, Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: shorten path to files
On 2008-06-27, cesco <[EMAIL PROTECTED]> wrote: > Hi, > > I need to retrieve the content of some files which are placed on a > network drive so in order to open them I need the full path to the > file. > Unfortunately some times the path is longer than 256 characters and in > Windows such a path is too long with the result that the file is not > found (though it is there). > > Is there any way around this? >From your description, it sounds like a OS problem, and not a Python problem. You may have better luck if you ask in a Win* newsgroup. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: How make regex that means "contains regex#1 but NOT regex#2" ??
On 2008-07-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I'm looking over the docs for the re module and can't find how to > "NOT" an entire regex. (?! R) > How make regex that means "contains regex#1 but NOT regex#2" ? (\1|(?!\2)) should do what you want. Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: Required items in a form
On 2008-07-01, Brandon <[EMAIL PROTECTED]> wrote: > What I'm trying to do is essentially force a user to fill in required items > in a form, which will be saved to a database. How can I get it so that once > the user clicks "OK" on the dialog box, it transfers control back to the > form, and not save the empty fields into the database? Follow the golden rule of not trusting user input. When you get back values for the fields, check them, and if not correct, ask again, give an error, abort the program, erase the data base, reboot the system, whatever is appropiate to handle such an error. Sincerely Albert -- http://mail.python.org/mailman/listinfo/python-list
Re: n00bie wants advice.
On 2008-07-02, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > This simple script writes html color codes that can be viewed in a > browser. I used short form hex codes (fff or 000, etc) and my list > has only six hex numbers otherwise the results get rather large. I > invite criticism as to whether my code is "pythonic". Are there other > ways to generate the hex combos besides the nested "for" loops? Thanks > in advance, Bill ok. variable names of 1 letter are very bad. Use more meaningful names like 'red' 'green' etc. 'list' is better, but also a name reserved by Python, so change that too. Indenting is normally 4 spaces in Python You can see "a + b +c" twice, compute it once, and assign it to a intermediate variable Use string formatting for better readability. In this case, you could also open the file earlier, and write all strings directly to file instead of first creating a string in memory Otherways of creating the colour code permutations: In this case, this is most Pythonic imho. You could write a list comprehension of even a recursive function, but I think it wouldn't increase readability. Albert > list = ['3','6','9','b','d','f'] > > s = 'h1{margin:0}\n' > > for a in list: > for b in list: > for c in list: > s += ''+ a + b > + c +' > \n' > > s += '' > > f = open('c:/x/test.htm', 'w') > f.write(s) > f.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with a for loop and a list
On 2008-07-02, Alexnb <[EMAIL PROTECTED]> wrote: > I have no idea what "list assignment index out of range means?!?! You are assigning a value to a non-existing list element, as in >>> x = [1] >>> x[2] = 4 Traceback (most recent call last): File "", line 1, in ? IndexError: list assignment index out of range Albert -- http://mail.python.org/mailman/listinfo/python-list