LZO decompressing with Python 2.5x on Windows for C dummies
Hi list, I have some binary data files which contain embedded LZO compressed payloads mixed with normal C-struct like headers. I would like to have the ability to LZO decompress these payloads using a python 2.5.x script. I was hoping that there was an up-to-date plug-and-play LZO library available for Windows somewhere. However, I have not managed to find such a one. The closest I have gotten is on the official LZO home page: http://www.oberhumer.com/opensource/lzo/ There is a link to some old LZO 1.08 bindings for Python 2.2 there as a tar.gz (released back in 2002!) http://www.oberhumer.com/opensource/lzo/download/LZO-v1/python-lzo-1.08.tar.gz I managed to unpack that using the python tarfile module (very handy for the purpose) In essence, the tarball contains a setup.py lzomodule.c Makefile and some documentation It does not contain anything that appears to be binaries for Windows. The instructions indicate that I am supposed to build it myself using the Makefile. I do not have make nor a C-compiler installed, and I fear that if I were to compile it, it would be a very tedious process for me, and I would rather like to avoid it if at all possible. Concerning the Windows LZO binaries I have previously struggled with pytables and using LZO on Windows with HDF5 files, and in relation to that Francesc Alted was so kind to build Windows LZO ver. 1.08 binaries as discussed here http://www.pytables.org/moin/FAQ#A.5BWindows.5DCan.27tfindLZObinariesforWindows and available here as a zip file http://www.pytables.org/download/lzo-win Amongst others, this zip file has a Gwin32\bin\lzo1.dll file. By copying that to the ptables filder in my python25 installation I have managed to get LZO working with pytables. Getting back to the python bindings, the setup.py does contain a section, which indicates that it adresses a Windows platform also: ... if sys.platform == "win32": # Windows users have to configure the LZO_DIR path parameter to match # their LZO source installation. The path set here is just an example # and thus unlikely to match your installation. LZO_DIR = r"c:\src\lzo-1.08" include_dirs.append(os.path.join(CURL_DIR, "include")) extra_objects.append(os.path.join(CURL_DIR, "lzo.lib")) ... If I were clever enough I now guess I should be able to unpack the pytables lzo Windows binaries to some suitable place (any recommendations?) and modify the setup.py such that it correctly "binds the dll" (this is black magic to me). One thing, which concerns me, is that a file lzo.lib is mentioned in the current setup.py, but there is not a file with that name in the GnuWin32 zip zip file from Francesc in the lib folder of the zip I do find two lib files.: liblzo.lib liblzo-bcc.lib but are any of these related to the lzo.lib mentioned in the setup.py? It does not help that I have a very poor understanding of how Python binds to native libraries, nor how things should be "installed" manually. My next question, if somehow someone is able to guide me a little in the process, then how do I use the library once available. The README indicates that I should use the internal Python documentation- So I guess i should just open an IPython shell do an import lzo? and then tab on lzo. and see what is available? Sorry for the long post. Kind regards, Kim -- http://mail.python.org/mailman/listinfo/python-list
Re: list of tuples with dynamic change in position
sajuptpm wrote: > i want to find the loaded machine based on cpu and mem and desk > utilization by changing this order. > > I created a UI using that i can change the order of item in the tuple. > But the problem is asc and desc sorting How about only changing the order in which the elements are displayed? In other words, the internal data is fixed, but both the sorting of the list and the order in which the parts of the records are displayed can be changed. Just my 2c Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
Detailed Description - l1 = [] l2 = [ ((3,8),(1,2)), ((1,3),(1,7)), ((7,0),(1,8)), ((4,2),(1,2)), ((2,9),(9,1)) ] I need to take each item from l2 and insert into l1 with first element(column)(3,1,7,4,2) sorted in ascending order and second element(column)(8,3,0,2,9) sorted in descending order. #SORTING for k in l2: flag=True for i, v in enumerate(l1): if v <= k: l1.insert(i,k) flag = False break if flag: l1.append(k) for a in l1: print a output --- ((7, 0), (1, 8)) ((4, 2), (1, 2)) ((3, 8), (1, 2)) ((2, 9), (9, 1)) ((1, 3), (1, 7)) This will not give l1 with first element(column)(3,1,7,4,2) sorted in ascending order and second element(column)(8,3,0,2,9) sorted in descending order. -- I added a -ve signe to all first elements l2 = [ ((-3,8),(1,2)), ((-1,3),(1,7)), ((-7,0),(1,8)), ((-4,2),(1,2)), ((-2,9),(9,1)) ] #SORTING for k in l2: flag=True for i, v in enumerate(l1): if v <= k: l1.insert(i,k) flag = False break if flag: l1.append(k) for a in l1: print a output --- ((-1, 3), (1, 7)) ((-2, 9), (9, 1)) ((-3, 8), (1, 2)) ((-4, 2), (1, 2)) ((-7, 0), (1, 8)) Now output is similar to first elements asc and second elements desc.But the problem is the -ve sign, i dont need that. Have any other method to do it?? -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, Sep 7, 2010 at 9:35 PM, Phlip wrote: > Exceptions are very dangerous by themselves, because if you don't trap > them just right they can cause side-effects. And returning None on failure is dangerous, because if the programmer does not take care to handle that case, the program may attempt to regard it as actual data. This in turn results in hard-to-track bugs elsewhere in the program, a fate much worse than an unhandled exception. It's better to fail noisily than to fail silently. > They are worse than GOTO. This assertion is questionable at best. Exceptions are structured; goto is unstructured, giving you much more rope to hang yourself with. -- http://mail.python.org/mailman/listinfo/python-list
Re: mail sending -- smtplib
Got it fixed. It was a very silly mistake. mssg variable had each line with indent. Removed the indent and it worked. Regards, Kurian Thayil. On Tue, Sep 7, 2010 at 9:57 AM, Kurian Thayil wrote: > Hi All, > > I am a newbie in python. Just 2-3 days old wanting to learn this amazing > programming language. I was trying to send mails using smtplib module, so > did some google and found a code snippet. The mail gets sent, but doesn't > come in the right format when a for-loop is introduced (Not MIME standard?). > Without the for-loop the script works fine. Can anyone advice? > * > #!/usr/bin/env python > > import smtplib > > for i in range(1,5): > print "I is ",i > fromaddr='kurianmtha...@gmail.com' > toaddr='kurianmtha...@gmail.com' > print toaddr > mssg="""From: Kurian Thayil > To: Kurian Thayil > MIME-Version: 1.0 > Content-type: text/html > Subject: 12345 -- Reloaded :) > > Hey dude.. how are you > > > Regards, > > Kurian > """ > print "message is ",mssg > > smail=smtplib.SMTP('smtp.gmail.com',587) > > smail.ehlo() > smail.starttls() > smail.ehlo() > smail.login(fromaddr,'***') > > smail.sendmail(fromaddr,toaddr,mssg) > print "Over" > * > > Regards, > > Kurian Thayil. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python 2.6 urlencode
On 9/7/2010 9:56 PM, Ned Deily wrote: In article<4c87013f$0$1625$742ec...@news.sonic.net>, John Nagle wrote: On 9/7/2010 5:43 PM, Terry Reedy wrote: On 9/7/2010 3:02 PM, John Nagle wrote: There's a bug in Python 2.6's "urllib.urlencode". If you pass in a Unicode character outside the ASCII range, instead of it being encoded properly, an exception is raised. In other words, we're in the version suckage period. It took me all of one minute to find where a similar issue was reported previously (http://bugs.python.org/issue1349732). One of the comments on the issue explains how to use the "doseq" form and an explicit encode to handle Unicode items. I don't see where that part of the suggestion made it into the documentation. I'm sure if you make a specific doc change suggestion, it will be incorporated into the 2.7 docs. If you think a code change is needed, suggest a specific patch. That's a very funny bug report. The report was created back in 2005: Title: urllib.urlencode provides two features in one param Type: feature request Stage: committed/rejected It wasn't listed as an actual bug. On 2005-12-29, "Mike Brown" writes "However, I was unable to reproduce your observation that doseq=0 results in urlencode not knowing how to handle unicode. The object is just passed to str()." This was back in the Python 2.4 days, when "str" restriction to ASCII wasn't enforced. Perhaps the original reporter and the developer were using different versions. Five years later (!) Terry J. Reedy writes '"put something somewhere" will not get action.' In July 2010, Senthil Kumaran writes "This was fixed as part of Issue8788. Closing this." Issue 8788 is a documentation fix only. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On Tue, 7 Sep 2010 12:46:36 -0700 (PDT), Baba wrote: >level: beginner > >word= 'even' >dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} > >i want to know if word is entirely composed of letters in dict2 > >my approach: >step 1 : convert word to dictionary(dict1) > >step2: >for k in dict1.keys(): > if k in dict2: >if dict1[k] != dict2[k]: > return False > return True > return False >return True > Assign letters to their corresponding primes with the following function - def alphaprime(c) : if c == 'a' return 2 elif c == 'b' return 3 elif c == 'c' return 5 ... elif c == 'y' return 97 elif c == 'z' return 101 else : return 0 Using above calculate a composite for the letters in the dictionary. Of course begin with 1 and multiply by the associated prime for each letter and repeat for each recurrence of that letter. Call this dictionarycomposite. Do the same for the word, parsing sequentially and multiplying by the prime for each letter. Call this wordcomposite. Now if dictionarycomposite % wordcomposite == 0 the word can be spelled with the letters in the dictionary. I used this in a word game : works fast. The Fundamental Theorum of Arithmetic may apply. Dave WB3DWE -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python 2.6 urlencode
In article <4c87013f$0$1625$742ec...@news.sonic.net>, John Nagle wrote: > On 9/7/2010 5:43 PM, Terry Reedy wrote: > > On 9/7/2010 3:02 PM, John Nagle wrote: > >> There's a bug in Python 2.6's "urllib.urlencode". If you pass > >> in a Unicode character outside the ASCII range, instead of it > >> being encoded properly, an exception is raised. > >> > >> File "C:\python26\lib\urllib.py", line 1267, in urlencode > >> v = quote_plus(str(v)) > >> UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in > >> position 0: ordinal not in range(128) > >> > >> This will probably work in 3.x, because there, "str" converts > >> to Unicode, and quote_plus can handle Unicode. This is one of > >> those legacy bugs left from the pre-Unicode era. > >> > >> There's a workaround. Call urllib.urlencode with a second > >> parameter of 1. This turns on the optional feature of > >> accepting tuples in the argument to be encoded, and the > >> code goes through a newer code path that works. > >> > >> Is it worth reporting 2.x bugs any more? Or are we in the > >> version suckage period, where version N is abandonware and > >> version N+1 isn't deployable yet. > > > > You may report 2.7 bugs, but please verify that the behavior is a bug in > > 2.7. However, bugs that have been fixed by the switch to switch to > > unicode for text are unlikely to be fixed a second time in 2.7. You > > might suggest an enhancement to the doc for urlencode if that workaround > > is not clear. Or perhaps that workaround suggests that in this case, a > > fix would not be too difficult, and you can supply a patch. > > > > The basic deployment problem is that people who want to use unicode text > > also want to use libraries that have not been ported to use unicode > > text. That is the major issue for many porting projects. > > In other words, we're in the version suckage period. It took me all of one minute to find where a similar issue was reported previously (http://bugs.python.org/issue1349732). One of the comments on the issue explains how to use the "doseq" form and an explicit encode to handle Unicode items. I don't see where that part of the suggestion made it into the documentation. I'm sure if you make a specific doc change suggestion, it will be incorporated into the 2.7 docs. If you think a code change is needed, suggest a specific patch. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
> > > I'm trying to rewrite a c program in python & encountered several problems. > I have some data structures in my c program like below: > > typedef struct > { > unsigned short size; > > unsigned short reserved:8; > unsigned short var_a1:2; > unsigned short var_a2:2; > unsigned short var_a3:2; > unsigned short var_a4:2; > > unsigned int var_a5; > }structa; > > typedef struct > { > unsigned short size; > > unsigned char reserved:4; > unsigned char var_b1:1; > unsigned char var_b2:1; > unsigned char var_b3:1; > unsigned char var_b4:1; > > structa var_structa; > }structb; > > I tried to code the above in python but only got this far: > > class StructA(object): > def __init__(self, size=0) > self.size = size > > class StructB(object): > def __init__(self, size=0) > > Any equivalent for c data structures & bit fields in python? And how do I > define var_structa (in structb) in python? > > Bitfields are most commonly used for extreme space optimization - i.e. shoving several variables and flags with carefully limited ranges into a single work. In Python you rarely work this way (where such an optimization is warranted, Python isn't the best tool for the job). However, as in your use case, it is sometimes needed in Python in order to communicate with other devices over the network or some other link. In my work with Python and embedded devices I've found the construct library (http://construct.wikispaces.com/) very useful. It allows to you very easily define complex formats for frames/messages on the bit and byte level. The idea is to use construct to encode and decode messages being sent to an embedded device. It works great. If you have further questions about this approach, feel free to ask. Eli -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
BartC, 08.09.2010 03:45: Getting back to the OP's code again (trivial and pointless as it might seem), I got these results: C (gcc 3.4.5 -O3) 0.8 secs C (DMC-o) 2.3 secs C (lccwin32 -O) 2.9 secs [...] I've seen LuaJIT in action. It's timing for this test is 1.5 secs: forget being only 10x slower than C, it's faster than some C versions! (I'm sure it must be cheating somewhere...) Sure it does. C is statically compiled, while LuaJIT is a JIT compiler. It unjustly uses *runtime* information to compile the code. You can get a similar advantage with some C compilers by using profile based optimisation. BTW, I wonder why the code takes a whole 0.8 seconds to run in your gcc test. Maybe you should use a newer GCC version. It shouldn't take more than a couple of milliseconds (for program startup, OS calls, etc.), given that the output is a constant. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Phlip writes: > On Sep 7, 4:38 pm, Benjamin Kaplan wrote: > > > When you're using a language, you should use the style that the > > language emphasizes. > > You mean like this? > > uri = reverse('my_uri_name', kwargs=dict(pk=record.pk)) Do you think that style is emphasised by Python? What gives you that impression? -- \“Program testing can be a very effective way to show the | `\presence of bugs, but is hopelessly inadequate for showing | _o__) their absence.” —Edsger W. Dijkstra | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 4:38 pm, Benjamin Kaplan wrote: > When you're using a language, you should use the style that the > language emphasizes. You mean like this? uri = reverse('my_uri_name', kwargs=dict(pk=record.pk)) That 'kwargs' there is ... a lapse of judgement. It is exposing a technical detail (the "keyword arguments") instead of naming the variable after its intent. It should be 'params=', at least, to match the URI standards. I'm just sayin'... -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 5:51 pm, Terry Reedy wrote: > On 9/7/2010 2:53 PM, Phlip wrote: > > > They are for situations which the caller should care not to handle. > > Python is simply not designed that way. Exception raising and catching > is a common flow-control method in Python. If you cannot stand that, > Python is not for you. While I'm at it, I'm going to log into comp.lang.java.misc and explain to everyone why static typing is overkill, and implementation inheritance is good for you. Everyone gets defensive about the design flaws in their own language. But the django.db situation is not even a design flaw; just a misinterpretation of the Samurai Principle. int('yo') shall throw an exception, but a missing record could be the result you were looking for, so it's not exceptional. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
Lawrence D'Oliveiro wrote: But alone of all of these, garbage collection still remains just as costly to implement as ever. That should tell you something about how poorly it matches the characteristics of modern computing hardware. So maybe we need to redesign the hardware. Perhaps reference counts could be stored in their own special area of memory, updated in parallel with main memory accesses so that they don't slow anything down. Make it multiported so that all your cores can access it at once without locking. Maybe even build it out of counters instead of registers, so there's no data bus, only an address bus and inc/dec control lines. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 6:23 pm, Lawrence D'Oliveiro wrote: > Does catching the exception not defeat the “Samurai Principle”? Read my comic: http://c2.com/cgi/wiki?SamuraiPrinciple Exceptions are very dangerous by themselves, because if you don't trap them just right they can cause side-effects. They are worse than GOTO. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Lawrence D'Oliveiro wrote: Does catching the exception not defeat the “Samurai Principle”? Not if it lets you turn defeat into victory. Or redefine victory so that it includes defeat. Or something. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python 2.6 urlencode
On 9/7/2010 5:43 PM, Terry Reedy wrote: On 9/7/2010 3:02 PM, John Nagle wrote: There's a bug in Python 2.6's "urllib.urlencode". If you pass in a Unicode character outside the ASCII range, instead of it being encoded properly, an exception is raised. File "C:\python26\lib\urllib.py", line 1267, in urlencode v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 0: ordinal not in range(128) This will probably work in 3.x, because there, "str" converts to Unicode, and quote_plus can handle Unicode. This is one of those legacy bugs left from the pre-Unicode era. There's a workaround. Call urllib.urlencode with a second parameter of 1. This turns on the optional feature of accepting tuples in the argument to be encoded, and the code goes through a newer code path that works. Is it worth reporting 2.x bugs any more? Or are we in the version suckage period, where version N is abandonware and version N+1 isn't deployable yet. You may report 2.7 bugs, but please verify that the behavior is a bug in 2.7. However, bugs that have been fixed by the switch to switch to unicode for text are unlikely to be fixed a second time in 2.7. You might suggest an enhancement to the doc for urlencode if that workaround is not clear. Or perhaps that workaround suggests that in this case, a fix would not be too difficult, and you can supply a patch. The basic deployment problem is that people who want to use unicode text also want to use libraries that have not been ported to use unicode text. That is the major issue for many porting projects. In other words, we're in the version suckage period. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
On 08/09/2010 02:45, BartC wrote: "David Cournapeau" wrote in message news:mailman.546.1283897932.29448.python-l...@python.org... On Sun, Sep 5, 2010 at 8:28 PM, BartC wrote: One order of magnitude (say 10-20x slower) wouldn't be so bad. That's what you might expect for a dynamically typed, interpreted language. 10/20x slower than C is only reached by extremely well optimized dynamic languages. It would be a tremendous achievement. If that's Well, that is what I do (mess around with languages and stuff). Getting back to the OP's code again (trivial and pointless as it might seem), I got these results: C (gcc 3.4.5 -O3) 0.8 secs C (DMC-o) 2.3 secs C (lccwin32 -O) 2.9 secs My last interpreter 12.6 secs dynamically typed language (or 4.5 secs when told the type of 'a'; but that's cheating a little..) Python 3 177.0 secs That's why I was questioning the latter's performance in for-loops. But now that I know a bit more about Python (having dynamic everything) the figure is not so surprising. However, it's still slow! what you are after, look at LUA with its JIT, or scheme + stalin. I've seen LuaJIT in action. It's timing for this test is 1.5 secs: forget being only 10x slower than C, it's faster than some C versions! (I'm sure it must be cheating somewhere...) If you'd like to direct your skills to making CPython faster, without diminishing its flexibility, I'm sure it'll be welcomed. The source is all public, you know! :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
"David Cournapeau" wrote in message news:mailman.546.1283897932.29448.python-l...@python.org... On Sun, Sep 5, 2010 at 8:28 PM, BartC wrote: One order of magnitude (say 10-20x slower) wouldn't be so bad. That's what you might expect for a dynamically typed, interpreted language. 10/20x slower than C is only reached by extremely well optimized dynamic languages. It would be a tremendous achievement. If that's Well, that is what I do (mess around with languages and stuff). Getting back to the OP's code again (trivial and pointless as it might seem), I got these results: C (gcc 3.4.5 -O3) 0.8 secs C (DMC-o) 2.3 secs C (lccwin32 -O) 2.9 secs My last interpreter 12.6 secs dynamically typed language (or4.5 secs when told the type of 'a'; but that's cheating a little..) Python 3177.0 secs That's why I was questioning the latter's performance in for-loops. But now that I know a bit more about Python (having dynamic everything) the figure is not so surprising. However, it's still slow! what you are after, look at LUA with its JIT, or scheme + stalin. I've seen LuaJIT in action. It's timing for this test is 1.5 secs: forget being only 10x slower than C, it's faster than some C versions! (I'm sure it must be cheating somewhere...) -- bartc -- http://mail.python.org/mailman/listinfo/python-list
Re: what should __iter__ return?
Thomas Jollans wrote: Hmm. Modifying an object while iterating over it isn't a great idea, ever: I wouldn't say never. Algorithms that calculate some kind of transitive closure can be expressed rather neatly by appending items to a list being iterated over. You can accommodate that kind of thing by writing the iterator like this: def __iter__(self): i = 0 while i < len(self): yield self[i] i += 1 -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
In message <74587da9-8861-4400-bbd7-1ea4f8182...@l38g2000pro.googlegroups.com>, Phlip wrote: > Pythonistas: > > The "Samurai Principle" says to return victorious, or not at all. This > is why django.db wisely throws an exception, instead of simply > returning None, if it encounters a "record not found". Does catching the exception not defeat the “Samurai Principle”? -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On 9/7/2010 2:53 PM, Phlip wrote: They are for situations which the caller should care not to handle. Python is simply not designed that way. Exception raising and catching is a common flow-control method in Python. If you cannot stand that, Python is not for you. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python 2.6 urlencode
On 9/7/2010 3:02 PM, John Nagle wrote: There's a bug in Python 2.6's "urllib.urlencode". If you pass in a Unicode character outside the ASCII range, instead of it being encoded properly, an exception is raised. File "C:\python26\lib\urllib.py", line 1267, in urlencode v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 0: ordinal not in range(128) This will probably work in 3.x, because there, "str" converts to Unicode, and quote_plus can handle Unicode. This is one of those legacy bugs left from the pre-Unicode era. There's a workaround. Call urllib.urlencode with a second parameter of 1. This turns on the optional feature of accepting tuples in the argument to be encoded, and the code goes through a newer code path that works. Is it worth reporting 2.x bugs any more? Or are we in the version suckage period, where version N is abandonware and version N+1 isn't deployable yet. You may report 2.7 bugs, but please verify that the behavior is a bug in 2.7. However, bugs that have been fixed by the switch to switch to unicode for text are unlikely to be fixed a second time in 2.7. You might suggest an enhancement to the doc for urlencode if that workaround is not clear. Or perhaps that workaround suggests that in this case, a fix would not be too difficult, and you can supply a patch. The basic deployment problem is that people who want to use unicode text also want to use libraries that have not been ported to use unicode text. That is the major issue for many porting projects. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
On 9/7/2010 6:00 AM, BartC wrote: Why should it? But if you want it, you can do it: xrange = range There, that wasn't hard, was it? I think I just learned more about Python than from months of reading this group. So 'range' is just a class like any other. And that a class is something you can blithely copy from one variable to another. There is no copying of the class object. A new name is associated with the object. Any object can be given any number of names (aliases) that refer to the one and same object. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba writes: > However the following Wiki excerpt seems to go in my direction: No, it doesn't. It advises that people show kindness; as I've been arguing, that's exactly what you were shown. You haven't shown how the information being imparted could have been fully imparted in a way that's kinder, nor that it would be reasonable to do so. To put it another way: if you feel offended by an utterance, then insufficient kindness on the part of the speaker is not the only explanation. -- \ “Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.” —Anthony Taylor | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: (Webinar) Connecting the Dots: US SEC, ABS Mandates, Financial Modeling and Python
Will this webcast/webinar perform on Linux? Jason On Tue, 2010-09-07 at 14:08 -0700, Kendra Penrose wrote: > Connecting the Dots: US SEC, ABS Mandates, Financial Modeling and Python > > Date: Wednesday September 22, 2010python-announce-l...@python.org, > Time: 10:00am PST/1:00pm EST/ 17:00 UTC > Space is limited so register now at > https://www1.gotomeeting.com/register/151429928 > > Join us for a webinar co-hosted by Ann Rutledge, R&R Consulting, and > Diane Mueller, ActiveState, focused on the recent US SEC regulation, ABS > Mandates, Financial Modeling and Python. > > Recently, the US SEC published a proposal (33-9117) covering a series of > new initiatives to address the current lack of transparency of > asset-backed securities (ABS). This new series of SEC proposed > initiatives are a way of putting "teeth" into Reg AB that would give the > SEC (and the market) enough data to police and prevent events like the > recent subprime crisis. > > In this webinar, Ann Rutledge and Diane Mueller will discuss a new key > technical requirement in the SEC proposal; the provision of a Python > computer program and the market data required to properly monitor and > analyse these complex ABS transactions. The role of the proposed program > is to enable the capture of all the complicated terms of an ABS deal in > code that can be used to analyze the cash flows in each deal and how the > returns will get split up between different parties. Currently, > investors, fund managers, and investment managers receive a complex, > textual description of this information in the prospectus, which makes > it difficult to perform or visualize a rigorous quantitative or if-then > analysis of the asset-backed securities. > > By attending this webinar you will learn: > * some historical challenges regarding regulation of the asset-backed > securities (ABS) market > * technical benefits of Python and XML for capturing essential financial > information in ABS transactions > * about opportunities for collaboration between between regulators, > accounting standards bodies, and the open source software community > > If you are in the finance industry and are affected by the new SEC > regulations, you don't want to miss this webinar! > > Register at https://www1.gotomeeting.com/register/151429928 -- http://mail.python.org/mailman/listinfo/python-list
Re: include a file in a python program
Thanks all for your response i will try out this week, you have give me sufficient hint. Thanks again. Bussiere On Mon, Sep 6, 2010 at 9:50 AM, Niklasro(.appspot) wrote: > On Sep 5, 10:57 pm, bussiere bussiere wrote: >> i've got a python.txt that contain python and it must stay as it (python.txt) >> >> how can i include it in my program ? >> import python.txt doesn't work >> is there a way : >> a) to make an include("python.txt") >> b) tell him to treat .txt as .py file that i can make an import python ? >> i'am using python3 >> Regards >> Bussiere >> fan of torchwood >> Google Fan boy > > You can do it with tkinter to also enable GUI. > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, Sep 7, 2010 at 6:20 PM, Phlip wrote: > On Sep 7, 1:06 pm, Bruno Desthuilliers > wrote: > >> try: >> return Model.objects.get(pk=42) >> except Model.DoesNotExist: >> return sentinel > > Visual Basic Classic had a Collection Class, which worked essentially > like a real language's Hash, Map, or Dict. > > Except - it had no operation to test membership. It also could not > enumerate by key and value (which would be 'for k,v in dict.items()'). > To test for membership, you _had_ to catch an exception - using VB's > tragically clumsy exception model. > > Hours of fun. That leads us to this topic: > > http://www.google.com/search?q=don't+use+exceptions+for+normal+control+flow > -- An experienced C programmer can program C in any language, but that doesn't mean it's a good idea to. When you're using a language, you should use the style that the language emphasizes. While you shouldn't use exceptions for control flow in C++, Java, or C#, there's nothing wrong with using them as such in Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
Paul Rubin wrote: Now extrapolate that error rate from 30 lines to a program the size of Firefox (something like 5 MLOC), and you should see how fraught with danger that style of programming is. But you don't write 5 MLOC of code using that programming style. You use it to write a small core something along the lines of, oh, I don't know, a Python interpreter, and then write the rest of the code on top of that platform. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 07/09/2010 22:36, Baba wrote: On 7 sep, 22:37, MRAB wrote: On 07/09/2010 21:06, Paul Rubin wrote: Babawrites: word= 'even' dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} i want to know if word is entirely composed of letters in dict2 set(word)<= set(dict2.keys()) Do the numbers in dict2 represent the maximum number of times that the letter can be used? If yes, then build a similar dict for the word with the number of times that each letter occurs in the word and then check for every pair in the dict whether the key (ie, letter) occurs in dict2 and that the value (number of occurrences) isn't too many. Hi MRAB Thanks for the hint. In my case i need to do the opposite: the number of times that each letter ocurs in the word needs to be smaller or equal to the number of times it apears in dict2. That way i am guaranteed that word is entirely made up of elements of dict2. Your hint pointed me in the right direction. for k in word.keys(): if k not in hand: return False elif k in hand: if word[k]> hand[k]: return False return True If the first condition is True then the second will be False, so there's no need to check it: for k in word.keys(): if k not in hand: return False else: if word[k] > hand[k]: return False return True This can be shortened still further. -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 09/07/2010 01:26 PM, Baba wrote: On 7 sep, 22:08, Gary Herron wrote: On 09/07/2010 12:46 PM, Baba wrote: word= 'even' dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} Just go through each letter of word checking for its existence in dict2. Return False if one misses, and True if you get through the whole word: def ...(): for c in word: if c not in dict2: return False #if any character is not in dict return True # otherwise If you know of generator expressions, and remember that True and False are 1 and 0 respectively, then this works def ...(): return sum(c in dict2 for c in word) == len(word) Gary Herron -- Gary Herron, PhD. Department of Computer Science DigiPen Institute of Technology (425) 895-4418 ok but how do we address the fact that letter e needs to have the value 2 in the dictionary if it was to be True? in my example this condition is not met so the check would return False. Word is not entirely composed of letters in dict2, one of the letter is not in dict2 i.e. the 2nd e Huh??? I answered the problem as it was stated in the email -- it said nothing about *counting* the occurrences of letters. In order to not waste our (voluntary) time, perhaps you should carefully re-state the problem you'd liked solved. Then we'll see what we can come up with. So finding a matching key seems to be the easy part, checking if the number of ocurrences of letter in 'word' == letter.value seems to be the tricky part -- Gary Herron, PhD. Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted input
Bob (roberto.pagli...@gmail.com) wrote: > Hi All, > I have another question about formatted input. Suppose I am reading a > text file, and that I want it to be something like this > > word11 = num11, word12 = num12, word13 = num13 etc... > word21 = num21, word22 = num12, word23 = num23 etc... > etc... > > where wordx1 belongs to a certain dictionary of words, say dic1, while > wordx2 belongs to dic2, the numbers within some range and so on. I was > wondering if there is something in the standard library I may use to > check whether the file I am reading has a correct syntax according to > my rules/dictionaries instead of implementing my own routine that > would look like Python's `re` module > (pseudocode) > for each line > put words into a list > check condition for each word import re match_1 = re.compile("^words1") match_2 = re.compile("^words2") # Return a match object each re.match(match_1, "word11") re.match(match_2, "word21") I'm sure there are might be other ways to do the same thing. -- - Kenny Meyer To understand recursion, we must first understand recursion. -- -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 1:06 pm, Bruno Desthuilliers wrote: > try: > return Model.objects.get(pk=42) > except Model.DoesNotExist: > return sentinel Visual Basic Classic had a Collection Class, which worked essentially like a real language's Hash, Map, or Dict. Except - it had no operation to test membership. It also could not enumerate by key and value (which would be 'for k,v in dict.items()'). To test for membership, you _had_ to catch an exception - using VB's tragically clumsy exception model. Hours of fun. That leads us to this topic: http://www.google.com/search?q=don't+use+exceptions+for+normal+control+flow -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
On Sun, Sep 5, 2010 at 8:28 PM, BartC wrote: > > One order of magnitude (say 10-20x slower) wouldn't be so bad. That's what > you might expect for a dynamically typed, interpreted language. 10/20x slower than C is only reached by extremely well optimized dynamic languages. It would be a tremendous achievement. If that's what you are after, look at LUA with its JIT, or scheme + stalin. For cases where vectorization is indeed not applicable (recursive algorithms), like in some signal processing, there are specialized tools which are very expressive while retaining good speed (faust is an interesting one for audio signal processing). > That would simply be delegating Python to a scripting language. That's a strange thing to say if you compare it to matlab. > It would be > nice if you could directly code low-level algorithms in it without relying > on accelerators It would be nice, but the fact is that python cannot do it - and is quite far from being able to do it. I don't think it is as important as you think it is, because things like numpy are extremely powerful in many cases. cheers, David -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Phlip a écrit : > On Sep 7, 10:36 am, Ian Kelly wrote: >> On Tue, Sep 7, 2010 at 10:02 AM, Phlip wrote: >>> Back to the topic, I tend to do this: >>> for record in Model.objects.filter(pk=42): >>> return record >>> return sentinel >> How is that any better than just catching the exception? >> >> try: >> return Model.objects.get(pk=42) >> except Model.DoesNotExist: >> return sentinel >> >> The flow of control is much clearer this way. > > It reminds me of Visual Basic. Strange enough, your own code snippet reminds me of what I used to find when fixing VB programs some ten years ago. > And no it's not "much clearer". It is for any Python programmer - it's even TheOneObviousWay. > Exceptions are for catastrophic errors Chapter and verse, please ? Exceptions are for exceptional situations. When you call queryset.get, you do expect to have one single instance matching the lookup - specialy when doing a pk lookup. > AAAND you need to test that the DoesNotExist occurs for the exact > reason you expect. Bullshit. The only reason you'd get this exception is because there's no record matching your where clause. > Your except is not complete. Why so ? > Making it complete is > very hard, and will break as soon as the model changes. Why so ? -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Phlip a écrit : > On Sep 7, 10:12 am, Bruno Desthuilliers 42.desthuilli...@websiteburo.invalid> wrote: >> Phlip a écrit : >> >>> Back to the topic, I tend to do this: >>> for record in Model.objects.filter(pk=42): >>> return record >>> return sentinel >> WTF alert here... > > I don't see how anyone could WTF that. Are you pretending to be a newb > who doesn't understanding it? F'em. F'... newbies is definitly not the pythonic mindset. Python's mindset is about doing the most obvious thing, no trying to be smart. The obvious code here is: try: return Model.objects.get(pk=42) except Model.DoesNotExist: return sentinel so yes, your above snippet is bordering on WTF since it's not immediatly obvious - it takes at least one more second for me to parse, and I'm definitly not a Python nor Django newbie. That's something I'd immediatly rewrite if I had to work on this code. > I would guess that Django provides some basic rules for avoiding name > collisions. yes : common sense. > Nobody should call a field "pk__in" Nope, but "default" - which would be the obvious keyword here - is also a perfectly legitimate field name. >> But if you feel like you found the correct name, you can of course >> monkeypatch queryset !-) > > Know I gotta learn to add a new method to an existing class! It's as straightforward as possible once you know Python's object model: def somefunc(self, whatever): self.do_something_with(whatever) import somemodule somemodule.SomeClass.mymethod = somefunc -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted input
Bob writes: > Hi All, > I have another question about formatted input. Suppose I am reading a > text file, and that I want it to be something like this > > word11 = num11, word12 = num12, word13 = num13 etc... > word21 = num21, word22 = num12, word23 = num23 etc... > etc... > > where wordx1 belongs to a certain dictionary of words, say dic1, while > wordx2 belongs to dic2, the numbers within some range and so on. I was > wondering if there is something in the standard library I may use to > check whether the file I am reading has a correct syntax according to > my rules/dictionaries instead of implementing my own routine that > would look like > (pseudocode) > for each line > put words into a list > check condition for each word No, there is no such thing. Either write something from scratch using string methods, or use pyparsing which is certainly up to the task (and much more) Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
Baba writes: > for k in word.keys(): > if k not in hand: > return False > elif k in hand: > if word[k] > hand[k]: > return False > return True Untested: all(word[k] <= hand.get(k,0) for k in word) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed - function apparently global cannot be called.
Hi Rami, Stefan, Bruno. First a big thanks for your replies. On 07/09/2010 20:54, Rami Chowdhury wrote: Hi Ian, I think I see where you're going wrong -- this bit me too when I was learning Python, having come from PHP. Unlike PHP, when you import a module in Python it does *not* inherit the importing module's namespace. So the "log" function you're accessing in DelNotePrinter.py is not the one you defined above the import statement. Hmm. My php background shows that much. Huh? :) I guess so. http://docs.python.org/reference/executionmodel.html#naming-and-binding Thanks for the pointer. What a truly execrable piece of writing - full of over-long sentences and multiple subordinate clauses. It routinely uses terms before definition, and sometimes without definition. It is astonishingly bad. Perhaps you could help rewrite it? I'm sure the maintainers would be very happy to get a documentation patch. I would be happy to re-write it but I do need to understand the subject better before that can happen. Clear and wrong will not help anyone. :) Do I really have to move a 4 line function into its own file and import it again and again and again? The short answer is yes. If you have this and similar functions that you call from various modules, then it's ideal to put them in a utility module of their own. However, as imported modules are cached in Python, this isn't as expensive as you might think if you are more used to other languages. OK. Now I understand the need, the solution is easy. My first exploration of the wonders of writing windows services has had five things wrong with it: 1) Its windows - spit, crash, complicated, can't start a command line with elevated permissions, nash teeth, rip hair out. 2) Its services - so no stdout, or stderr, errors not reported, no "current directory" - burn and crash programming all over again. Deep joy! 3) pythonservice.exe - so module not called __main__ and initialisation not performed (unseen - see 2) - More fun. 4) multi-threading - I got really confused from this example http://docs.python.org/library/multiprocessing.html#exchanging-objects-between-processes becasue I got the wrong sort of Queue, so it stalled when it failed to call task_done() (there isn't such a method - but no error reported see 2). 5) Names pipes. The latest is that writing - yes writing - to the named pipe causes the reader to fail with (232, 'ConnectNamedPipe', 'The pipe is being closed.') if I write using php - yet I can write with python no trouble. There are down sides to "batteries included". When there are so many batteries, it can be hard to know if what you have is "good enough" or should you search for a more suitable one. Logging would have helped me a lot (assuming it would work in service environment with the permissions it would have had - a serious assumption). If it failed, it would have failed and hidden the problem. Oh to get on to proper GUI programming with python and PyQt! The next project. Wey Hey! Thanks again Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 7 sep, 22:37, MRAB wrote: > On 07/09/2010 21:06, Paul Rubin wrote: > > > Baba writes: > >> word= 'even' > >> dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} > > >> i want to know if word is entirely composed of letters in dict2 > > > set(word)<= set(dict2.keys()) > > Do the numbers in dict2 represent the maximum number of times that the > letter can be used? > > If yes, then build a similar dict for the word with the number of times > that each letter occurs in the word and then check for every pair in > the dict whether the key (ie, letter) occurs in dict2 and that the > value (number of occurrences) isn't too many. Hi MRAB Thanks for the hint. In my case i need to do the opposite: the number of times that each letter ocurs in the word needs to be smaller or equal to the number of times it apears in dict2. That way i am guaranteed that word is entirely made up of elements of dict2. Your hint pointed me in the right direction. for k in word.keys(): if k not in hand: return False elif k in hand: if word[k] > hand[k]: return False return True Baba -- http://mail.python.org/mailman/listinfo/python-list
formatted input
Hi All, I have another question about formatted input. Suppose I am reading a text file, and that I want it to be something like this word11 = num11, word12 = num12, word13 = num13 etc... word21 = num21, word22 = num12, word23 = num23 etc... etc... where wordx1 belongs to a certain dictionary of words, say dic1, while wordx2 belongs to dic2, the numbers within some range and so on. I was wondering if there is something in the standard library I may use to check whether the file I am reading has a correct syntax according to my rules/dictionaries instead of implementing my own routine that would look like (pseudocode) for each line put words into a list check condition for each word Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
Baba wrote: > On 7 sep, 22:08, Gary Herron wrote: >> On 09/07/2010 12:46 PM, Baba wrote: >> >> > word= 'even' >> > dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} >> >> Just go through each letter of word checking for its existence in >> dict2. Return False if one misses, and True if you get through the >> whole word: >> >> def ...(): >> for c in word: >> if c not in dict2: >> return False #if any character is not in dict >> return True # otherwise >> >> If you know of generator expressions, and remember that True and False >> are 1 and 0 respectively, then this works >> >> def ...(): >> return sum(c in dict2 for c in word) == len(word) >> >> Gary Herron >> >> -- >> Gary Herron, PhD. >> Department of Computer Science >> DigiPen Institute of Technology >> (425) 895-4418 > > ok but how do we address the fact that letter e needs to have the > value 2 in the dictionary if it was to be True? in my example this > condition is not met so the check would return False. Word is not > entirely composed of letters in dict2, one of the letter is not in > dict2 i.e. the 2nd e > > So finding a matching key seems to be the easy part, checking if the > number of ocurrences of letter in 'word' == letter.value seems to be > the tricky part Just compare the two dictionaries dict1 == dict2 Or, if you want to allow dict2 to contain higher but not lower values all(v <= dict2.get(k, 0) for k, v in dict1.iteritems()) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On Wed, Sep 8, 2010 at 1:56 AM, Baba wrote: > On 7 sep, 22:08, Gary Herron wrote: > > On 09/07/2010 12:46 PM, Baba wrote: > > > > > word= 'even' > > > dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} > > > > Just go through each letter of word checking for its existence in > > dict2. Return False if one misses, and True if you get through the > > whole word: > > > > def ...(): > >for c in word: > > if c not in dict2: > >return False #if any character is not in dict > >return True # otherwise > > > > If you know of generator expressions, and remember that True and False > > are 1 and 0 respectively, then this works > > > > def ...(): > > return sum(c in dict2 for c in word) == len(word) > > > > Gary Herron > > > > -- > > Gary Herron, PhD. > > Department of Computer Science > > DigiPen Institute of Technology > > (425) 895-4418 > > ok but how do we address the fact that letter e needs to have the > value 2 in the dictionary if it was to be True? in my example this > condition is not met so the check would return False. Word is not > entirely composed of letters in dict2, one of the letter is not in > dict2 i.e. the 2nd e > Seems You did not understood Gary's solution. It is correct. Also, you asked for 'i want to know if word is entirely composed of letters in dict2' which you are getting. Did you tried running the code ? > So finding a matching key seems to be the easy part, checking if the > number of ocurrences of letter in 'word' == letter.value seems to be > the tricky part > > -- > http://mail.python.org/mailman/listinfo/python-list > -- ~l0nwlf -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 07/09/2010 21:06, Paul Rubin wrote: Baba writes: word= 'even' dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} i want to know if word is entirely composed of letters in dict2 set(word)<= set(dict2.keys()) Do the numbers in dict2 represent the maximum number of times that the letter can be used? If yes, then build a similar dict for the word with the number of times that each letter occurs in the word and then check for every pair in the dict whether the key (ie, letter) occurs in dict2 and that the value (number of occurrences) isn't too many. -- http://mail.python.org/mailman/listinfo/python-list
Re: can't send email
In article , Bob wrote: >[...] > The error I get is this > > python email.py > Traceback (most recent call last): > File "email.py", line 2, in > import smtplib > File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/smtplib.py", line 46, in > import email.utils > File "/Users/Bob/Code/email.py", line 5, in > from email.mime.text import MIMEText > ImportError: No module named mime.text Your module email.py is hiding the standard library's email package. Rename your file from email.py to something that doesn't conflict. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: can't send email
On Sep 7, 10:27 pm, Chris Rebert wrote: > On Tue, Sep 7, 2010 at 1:12 PM, Bob wrote: > > Hello. > > I'm trying to send email using python 2.6.1 under snow leopard, but I > > can't get it to work. I'm trying one of the many examples I found on > > the web > > > The error I get is this > > > python email.py > > Traceback (most recent call last): > > File "email.py", line 2, in > > import smtplib > > File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/ > > python2.6/smtplib.py", line 46, in > > import email.utils > > File "/Users/Bob/Code/email.py", line 5, in > > from email.mime.text import MIMEText > > ImportError: No module named mime.text > > > I'm using the native python version installed by apple, so I don't > > know why email and email.utils and mime are not found. I checked on > > the filesystem and they are present... > > Your /Users/Bob/Code/email.py file is shadowing the std lib "email" > module. Rename your file to something else so that it no longer > conflicts. > > Cheers, > Chris > --http://blog.rebertia.com yes I just realized that... it works now!! thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 7 sep, 22:08, Gary Herron wrote: > On 09/07/2010 12:46 PM, Baba wrote: > > > word= 'even' > > dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} > > Just go through each letter of word checking for its existence in > dict2. Return False if one misses, and True if you get through the > whole word: > > def ...(): > for c in word: > if c not in dict2: > return False #if any character is not in dict > return True # otherwise > > If you know of generator expressions, and remember that True and False > are 1 and 0 respectively, then this works > > def ...(): > return sum(c in dict2 for c in word) == len(word) > > Gary Herron > > -- > Gary Herron, PhD. > Department of Computer Science > DigiPen Institute of Technology > (425) 895-4418 ok but how do we address the fact that letter e needs to have the value 2 in the dictionary if it was to be True? in my example this condition is not met so the check would return False. Word is not entirely composed of letters in dict2, one of the letter is not in dict2 i.e. the 2nd e So finding a matching key seems to be the easy part, checking if the number of ocurrences of letter in 'word' == letter.value seems to be the tricky part -- http://mail.python.org/mailman/listinfo/python-list
Re: can't send email
On Tue, Sep 7, 2010 at 1:12 PM, Bob wrote: > Hello. > I'm trying to send email using python 2.6.1 under snow leopard, but I > can't get it to work. I'm trying one of the many examples I found on > the web > The error I get is this > > python email.py > Traceback (most recent call last): > File "email.py", line 2, in > import smtplib > File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/ > python2.6/smtplib.py", line 46, in > import email.utils > File "/Users/Bob/Code/email.py", line 5, in > from email.mime.text import MIMEText > ImportError: No module named mime.text > > > I'm using the native python version installed by apple, so I don't > know why email and email.utils and mime are not found. I checked on > the filesystem and they are present... Your /Users/Bob/Code/email.py file is shadowing the std lib "email" module. Rename your file to something else so that it no longer conflicts. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
On 09/07/2010 12:46 PM, Baba wrote: word= 'even' dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} Just go through each letter of word checking for its existence in dict2. Return False if one misses, and True if you get through the whole word: def ...(): for c in word: if c not in dict2: return False #if any character is not in dict return True # otherwise If you know of generator expressions, and remember that True and False are 1 and 0 respectively, then this works def ...(): return sum(c in dict2 for c in word) == len(word) Gary Herron -- Gary Herron, PhD. Department of Computer Science DigiPen Institute of Technology (425) 895-4418 -- http://mail.python.org/mailman/listinfo/python-list
can't send email
Hello. I'm trying to send email using python 2.6.1 under snow leopard, but I can't get it to work. I'm trying one of the many examples I found on the web EXAMPLE 1 import smtplib fromaddr = 'fromu...@gmail.com' toaddrs = 'tou...@gmail.com' msg = 'There was a terrible error that occured and I wanted you to know!' # Credentials (if needed) username = 'username' password = 'password' # The actual mail send server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit() EXAMPLE 2 # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.mime.text import MIMEText # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. fp = open(textfile, 'rb') # Create a text/plain message msg = MIMEText(fp.read()) fp.close() # me == the sender's email address # you == the recipient's email address msg['Subject'] = 'The contents of %s' % textfile msg['From'] = me msg['To'] = you # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP() s.sendmail(me, [you], msg.as_string()) s.quit() The error I get is this python email.py Traceback (most recent call last): File "email.py", line 2, in import smtplib File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/ python2.6/smtplib.py", line 46, in import email.utils File "/Users/Bob/Code/email.py", line 5, in from email.mime.text import MIMEText ImportError: No module named mime.text I'm using the native python version installed by apple, so I don't know why email and email.utils and mime are not found. I checked on the filesystem and they are present... Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: compare dictionaries
Baba writes: > word= 'even' > dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} > > i want to know if word is entirely composed of letters in dict2 set(word) <= set(dict2.keys()) -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed - function apparently global cannot be called.
Hi Ian, On 2010-09-07 12:18, Ian Hobson wrote: > f = open('d:\logfile.txt','a') Just a note: Using a backslash in a non-raw string will get you in trouble as soon as the backslash is followed by a character which makes a special character sequence, like "\n". For example, f = open('d:\nice_filename.txt', 'a') will give surprising results. :-) Either double the backslash, use a raw string, or, in the special case of file system paths, possibly use a forward slash. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed - function apparently global cannot be called.
Hi Ian, On Tue, Sep 7, 2010 at 20:00, Ian wrote: > On 07/09/2010 11:50, Bruno Desthuilliers wrote: > > note the order of the above - log is defined before the import. > > And ? Do you think it will affect the imported module in any way ? Like, > say, magically "inject" your log function in the DelNotePrinter module ?-) > > Just that log is defined before the global log is encountered to that if > the compiler needed to set up a link at compile time it was able to do so. > > I think I see where you're going wrong -- this bit me too when I was learning Python, having come from PHP. Unlike PHP, when you import a module in Python it does *not* inherit the importing module's namespace. So the "log" function you're accessing in DelNotePrinter.py is not the one you defined above the import statement. > In Python, "global" means "module-level", and it's only necessary when you > want to rebind a module-level name from within a function or method. > > Exactly! I want to bind the name log to the function I wrote. > As Bruno pointed out, the "global" statement is only necessary if you want to "rebind" a name -- i.e. if you want to modify how an object appears to the global scope. It's not necessary if you simply want to call a function. > > I have read http://docs.python.org/reference/simple_stmts.html#globalvery > carefully and I still don't understand. > > > The statement definition makes no sense if you don't understand namespaces > and bindings: > > http://docs.python.org/reference/executionmodel.html#naming-and-binding > > Thanks for the pointer. What a truly execrable piece of writing - full of > over-long sentences and multiple subordinate clauses. It routinely uses > terms before definition, and sometimes without definition. It is > astonishingly bad. > Perhaps you could help rewrite it? I'm sure the maintainers would be very happy to get a documentation patch. > > The third sentence reads "Each occurrence of a name in the program text > refers to the *binding* of that name established in the innermost function > block containing the use." What does that mean? It appears to mean that it > is the assignment in the innermost function block that binds for all uses, > not the first, not module blocks, not class blocks and not code blocks - but > the innermost function block. That might be correct, but somehow I doubt it, > for it would mean that earlier bindings are ignored or invalidated or not > used or something - even if the inner block is not executed. > > I am not stupid and English is my mother tongue, and I have read that page > many times. The implications of the passage are still opaque to me. > > How can I call a global function placed at the top of the source. Do I > really have to move a 4 line function into its own file and import it again > and again and again? > The short answer is yes. If you have this and similar functions that you call from various modules, then it's ideal to put them in a utility module of their own. However, as imported modules are cached in Python, this isn't as expensive as you might think if you are more used to other languages. -- Rami Chowdhury "Never assume malice when stupidity will suffice." -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
compare dictionaries
level: beginner word= 'even' dict2 = {'i': 1, 'n': 1, 'e': 1, 'l': 2, 'v': 2} i want to know if word is entirely composed of letters in dict2 my approach: step 1 : convert word to dictionary(dict1) step2: for k in dict1.keys(): if k in dict2: if dict1[k] != dict2[k]: return False return True return False return True by adding a print statement i can see that this simply ends too early e.g. as soon as the first IF condition is met the loop exits i think this is easy but google and python doc didn't return any good hints so i'm trying here. Thanks Baba -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On 09/07/10 13:53, Phlip wrote: On Sep 7, 11:36 am, Tim Chase wrote: And no it's not "much clearer". Exceptions are for catastrophic errors that the caller should care not to handle. A "record not found" is not a catastrophe. Exceptions are not limited to catastrophic errors, simply exceptional (not the common) cases. E.g. iterators raising StopException when exhausted. Exceptions are not "because we should only return one type of thing". They are for situations which the caller should care not to handle. Exceptions are for propagating. A "record not found" is an exemplary example of a situation the caller _should_ handle. Um...first you state "Exceptions are for catastrophic errors that the caller should not care to handle. A 'record not found' is not a catastrophe" and then you contradictingly go on to state "A 'record not found' is an exemplary example of a situation the caller _should_ handle". I'm not sure I follow your logic here. Exceptions allow both (1) the ability to handle the exceptional condition locally if you want to (including suppressing it) and (2) propagate the exception if you want to make the caller handle it. And if you really want, you can just subclass QuerySet to provide your own get_or_none() method to return your sentinel. items = list(MyModel.objects.filter(...)) if len(items) == 1: do_something(items[0]) else: what_the(...) Both your version and mine read an entire cursor. But mine only rezzed the first object, whereas yours rezzed every object in the cursor, just to throw most of them away! If a .get() returns more than one object (non-unique criteria are used), what _should_ it return? Agreed, if it pulls back a bajillion records, that's bad, so if you're concerned your conditions might do that despite the expectation they bring back 1-and-only-1 (.get() currently raises an exception if it brings back more than one result db/models/query.py around line 342 where MultipleObjectsReturned is raised), then I'd just slice them: items = list(MyModel.objects.filter(...)[:1]) if items: do_something(items[0]) else: what_the(...) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Queue cleanup
Lawrence D'Oliveiro writes: > But you’ll notice that Free Software comes with no such > restrictions. In fact, it is contrary to commonly-accepted Free > Software guidelines to impose any sort of restrictions on areas of use. Free software comes with an explicit disclaimer of liability (you get what you pay for). The Sun stuff ($) may have either an express or implied warranty that could mean they get hit up for damages if the software is wrong. IANAL YMMV etc. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in Python 2.6 urlencode
In article <4c868c2d$0$1581$742ec...@news.sonic.net>, John Nagle wrote: > Is it worth reporting 2.x bugs any more? Or are we in the > version suckage period, where version N is abandonware and > version N+1 isn't deployable yet. Yes!! 2.7 is being actively maintained for bug fixes. (2.6 only for any security issues that might arise.) It's easy enough to see this if you take a glance at current activity on any of several Python development related mailing lists: http://www.python.org/community/lists/ -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
On 9/6/2010 11:55 PM, Stefan Behnel wrote: Kwan Lai Cheng, 07.09.2010 06:06: I'm trying to rewrite a c program in python& encountered several problems. I have some data structures in my c program like below: def __init__(self, size=0) Any equivalent for c data structures& bit fields in python? If you can tell us what these structs are being used for in the original C code, we might be able to point you to a suitable way to implement the same thing efficiently in Python. Python has the "struct" module for formatting binary data. http://docs.python.org/library/struct.html But it doesn't handle bit fields. Its lowest resolution is one byte. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Bug in Python 2.6 urlencode
There's a bug in Python 2.6's "urllib.urlencode". If you pass in a Unicode character outside the ASCII range, instead of it being encoded properly, an exception is raised. File "C:\python26\lib\urllib.py", line 1267, in urlencode v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xa9' in position 0: ordinal not in range(128) This will probably work in 3.x, because there, "str" converts to Unicode, and quote_plus can handle Unicode. This is one of those legacy bugs left from the pre-Unicode era. There's a workaround. Call urllib.urlencode with a second parameter of 1. This turns on the optional feature of accepting tuples in the argument to be encoded, and the code goes through a newer code path that works. Is it worth reporting 2.x bugs any more? Or are we in the version suckage period, where version N is abandonware and version N+1 isn't deployable yet. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 11:36 am, Tim Chase wrote: > > And no it's not "much clearer". Exceptions are for catastrophic errors > > that the caller should care not to handle. A "record not found" is not > > a catastrophe. > > Exceptions are not limited to catastrophic errors, simply > exceptional (not the common) cases. E.g. iterators raising > StopException when exhausted. Exceptions are not "because we should only return one type of thing". They are for situations which the caller should care not to handle. Exceptions are for propagating. A "record not found" is an exemplary example of a situation the caller _should_ handle. > items = list(MyModel.objects.filter(...)) > if len(items) == 1: > do_something(items[0]) > else: > what_the(...) Both your version and mine read an entire cursor. But mine only rezzed the first object, whereas yours rezzed every object in the cursor, just to throw most of them away! -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On 09/07/10 12:52, Phlip wrote: try: return Model.objects.get(pk=42) except Model.DoesNotExist: return sentinel The flow of control is much clearer this way. It reminds me of Visual Basic. And no it's not "much clearer". Exceptions are for catastrophic errors that the caller should care not to handle. A "record not found" is not a catastrophe. Exceptions are not limited to catastrophic errors, simply exceptional (not the common) cases. E.g. iterators raising StopException when exhausted. >>> i = iter(range(2)) >>> i.next() 0 >>> i.next() 1 >>> i.next() Traceback (most recent call last): File "", line 1, in StopIteration >>> i = iter(range(2)) >>> for v in i: ... print v ... 0 1 Running out of things to iterate over is pretty non-catastrophic in my book. :) Using exceptions as in the grandparent's post seem perfectly fine to me. The other option would be to LBYL: items = list(MyModel.objects.filter(...)) if len(items) == 1: do_something(items[0]) else: what_the(...) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Bit fields in python?
On 9/7/2010 12:06 AM, Kwan Lai Cheng wrote: Hi, I'm trying to rewrite a c program in python & encountered several problems. I have some data structures in my c program like below: typedef struct { unsigned short size; unsigned short reserved:8; unsigned short var_a1:2; unsigned short var_a2:2; unsigned short var_a3:2; unsigned short var_a4:2; unsigned int var_a5; }structa; In Python, we cannot directly name bitfields within an int. However, we can *read, set, and flip bits with the bit operators and bit masks *define a dict that maps bitfield names to bit indexes *define named functions that use the above. *wrap or subclass int with a class that has attributes that map to a bitfield. I am pretty sure there is public code that does all of the above. Searching pypi.python.org for 'bitfield', I found BitDecoder 0.5.1 Decode bit-fields to human readable description This program (and Python module) will decode a value as per a bitfield definition. This is very useful for hardware registers that have meanings to parts of the bits in seperate. Google for more. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, Sep 7, 2010 at 11:52 AM, Phlip wrote: > And no it's not "much clearer". It's clearer because it does exactly what it says it does, unlike your approach that masquerades as a loop. > Exceptions are for catastrophic errors No, they're for flagging "exceptional" states. /Errors/ are for catastrophic errors. The fact that errors are a subset of exceptions is just for convenience in handling. > AAAND you need to test that the DoesNotExist occurs for the exact > reason you expect. I'm not following you here. The only possible reason the exception can occur is if no matching row exists. If there were some other reason for raising an exception, then a different exception would be raised. > Your except is not complete. Making it complete is > very hard, and will break as soon as the model changes. Still not following you. What is it missing, and how will it break? -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
On 9/7/2010 9:24 AM, sajuptpm wrote: I have a list of tuples. l = [((30,50),(70)), ((50,20),(20))] for i in range(10): k = ((i+30,i+50),(i+70)) The (i+70) parens do nothing, as already explained to (20) Your set of test data are not very good as they do not test all the insertion possibilities. The value of the third number is not relevant to the problem and might as well be 0 for testing purposes. #suppose creating new tuple in each iteration using some random value and in sert it into list. flag=True for i, v in enumerate(l): You are reusing 'i' as index variable when it is already in use as index variable for the outer loop. This is a confusing and a bad idea in general, even if it happens to work. if v>= k: l.insert(i,k) flag = False break This block should be indented under the inner for loop if flag: l.append(k) flay can be avoided by using else clause of for statement This code will give a list of tuples sorted in asc order. I need to change this code to sort this list by k[0][0] ascending and k[0][1] descending. What are the modifications needed in this code. Change the v>=k test to be the one you want. Here is my code: l = [((30,51),0), ((30,49),0), ((32,20),0)] for i in range(5): k = ((i+29,i+51), 0) for i, v in enumerate(l): v00 = v[0][0] k00 = k[0][0] if v00 > k00 or v00==k00 and v[0][1] <= k[0][1]: l.insert(i,k) break else: l.append(k) print(l) # prints [((29, 51), 0), ((30, 52), 0), ((30, 51), 0), ((30, 49), 0), ((31, 53), 0), ((32, 54), 0), ((32, 20), 0), ((33, 55), 0)] I dont want to user sort() method of list. Why not? Even for the example above, .sort is faster than doing the insert sort in Python code! Indent the above with def f1():. Then def f2(): l = [((30,51),0), ((30,49),0), ((32,20),0)] for i in range(5): l.append(((i+29,i+51), 0)) l.sort(key=lambda x:(x[0][0], -x[0][1])) import timeit print(timeit.timeit(f1,'from __main__ import f1,f2',number=10)) print(timeit.timeit(f2,'from __main__ import f1,f2',number=10)) #prints 2.51296240165 1.63514413145 i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in That should be -x[0][1] It is a good idea to test code before posting. Any reason for the 'need' other than a wrong idea about relative speed? -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: What the \xc2\xa0 ?!!
On Sep 7, 11:01 am, Brian D wrote: > In an HTML page that I'm scraping using urllib2, a \xc2\xa0 > bytestring appears. > > The page's charset = utf-8, and the Chrome browser I'm using displays > the characters as a space. > > The page requires authentication:https://www.nolaready.info/myalertlog.php > > When I try to concatenate strings containing the bytestring, Python > chokes because it refuses to coerce the bytestring into ascii. > > wfile.write('|'.join(valueList)) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 163: ordinal not in range(128) > > In searching for help with this issue, I've learned that the > bytestring *might* represent a non-breaking space. > > When I scrape the page using urllib2, however, the characters print > as   in a Windows command prompt (though I wouldn't be surprised if > this is some erroneous attempt by the antiquated command window to > handle something it doesn't understand). > > If I use IDLE to attempt to decode the single byte referenced in the > error message, and convert it into UTF-8, another error message is > generated: > > >>> weird = unicode('\xc2', 'utf-8') > > Traceback (most recent call last): > File "", line 1, in > weird = unicode('\xc2', 'utf-8') > UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 0: > unexpected end of data > > If I attempt to decode the full bytestring, I don't obtain a human- > readable string (expecting, perhaps, a non-breaking space): > > >>> weird = unicode('\xc2\xa0', 'utf-8') > >>> par = ' - '.join(['This is', weird]) > >>> par > > u'This is - \xa0' > > I suspect that the bytestring isn't UTF-8, but what is it? Latin1? > > >>> weirder = unicode('\xc2\xa0', 'latin1') > >>> weirder > u'\xc2\xa0' > >>> 'This just gets ' + weirder > > u'This just gets \xc2\xa0' > > Or is it a Microsoft bytestring? > > >>> weirder = unicode('\xc2\xa0', 'mbcs') > >>> 'This just gets ' + weirder > > u'This just gets \xc2\xa0' > > None of these codecs seem to work. > > Back to the original purpose, as I'm scraping the page, I'm storing > the field/value pair in a dictionary with each iteration through table > elements on the page. This is all fine, until a value is found that > contains the offending bytestring. I have attempted to coerce all > value strings into an encoding, but Python doesn't seem to like that > when the string is already Unicode: > > valuesDict[fieldString] = unicode(value, 'UTF-8') > TypeError: decoding Unicode is not supported > > The solution I've arrived at is to specify the encoding for value > strings both when reading and writing value strings. > > for k, v in valuesDict.iteritems(): > valuePair = ':'.join([k, v.encode('UTF-8')]) > [snip] ... > wfile.write('|'.join(valueList)) > > I'm not sure I have a question, but does this sound familiar to any > Unicode experts out there? > > How should I handle these odd bytestring values? Am I doing it > correctly, or what could I improve? > > Thanks! Since it's UTF-8, one should go to one of the UTF-8 pages that describes how to decode it. As it turns out, its unicode hex value is A0, which is indeed a non-breaking space. This is probably as good as any page: http://en.wikipedia.org/wiki/UTF-8 John Roth -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 10:36 am, Ian Kelly wrote: > On Tue, Sep 7, 2010 at 10:02 AM, Phlip wrote: > > Back to the topic, I tend to do this: > > > for record in Model.objects.filter(pk=42): > > return record > > > return sentinel > > How is that any better than just catching the exception? > > try: > return Model.objects.get(pk=42) > except Model.DoesNotExist: > return sentinel > > The flow of control is much clearer this way. It reminds me of Visual Basic. And no it's not "much clearer". Exceptions are for catastrophic errors that the caller should care not to handle. A "record not found" is not a catastrophe. Read my original post. AAAND you need to test that the DoesNotExist occurs for the exact reason you expect. Your except is not complete. Making it complete is very hard, and will break as soon as the model changes. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 7 sep, 16:50, Grant Edwards wrote: > On 2010-09-07, Baba wrote: > > > Sloppy wording, I apologise. This should say: If you find the > > question you're reading too easy then just don't answer. Noone is the > > owner of a democratic forum where freedom to ask the question one > > likes is paramount (as long of course as it is related to the > > group)...so let me repeat that, to say "Please do us a favour and at > > least try to figure things out on your own" is in my view > > inappropriate. > > You need to read this: > > http://www.catb.org/esr/faqs/smart-questions.html > > > To me it sounds "Do me a favur and get lost". > > No, it means "Do yourself a favor, learn how to do things yourself." > > Remember: you're then one asking people to give you something for > free. It's not up to them to conform to your expectations, rather you > need to conform to theirs. Otherwise, they'll just ignore you. > > -- > Grant Edwards grant.b.edwards Yow! FOOLED you! Absorb > at EGO SHATTERING impulse > gmail.com rays, polyester poltroon!! "Please do us a favour" sounds condescending to me at least but maybe we Europeans are a bit touchy... However the following Wiki excerpt seems to go in my direction: "When someone makes a mistake -- whether it's a spelling error or a spelling flame, a stupid question or an unnecessarily long answer -- be kind about it. If it's a minor error, you may not need to say anything. Even if you feel strongly about it, think twice before reacting. Having good manners yourself doesn't give you license to correct everyone else. If you do decide to inform someone of a mistake, point it out politely, and preferably by private email rather than in public. Give people the benefit of the doubt; assume they just don't know any better. And never be arrogant or self-righteous about it." http://en.wikipedia.org/wiki/Netiquette Baba -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, Sep 7, 2010 at 10:02 AM, Phlip wrote: > Back to the topic, I tend to do this: > > for record in Model.objects.filter(pk=42): > return record > > return sentinel How is that any better than just catching the exception? try: return Model.objects.get(pk=42) except Model.DoesNotExist: return sentinel The flow of control is much clearer this way. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
In article , Roy Smith wrote: > >Imagine that you're looking at some code which was written years ago, by >people who are no longer around to answer questions. In one place, you >see: > >for i in range(n): > blah > >and in another, you see: > >for j in xrange(n): > blah > >If you are truly a Python expert, you'll say to yourself, "range and >xrange are synonyms", and move on to other things. If, however, you're >not really an expert, you'll look at this and say, "Hmmm, in one place >they used range(), and in another they used xrange(). Clearly, there's >some reason for the difference, I need to figure out what it is, because >that's probably key to my understanding why this code isn't working". >So, you spend the next two hours pouring over reference manuals trying >to understand the subtle difference, until your friend comes over and >says, "You dolt, you just wasted half the afternoon. They're the same >thing. Move on, this is not the bug you're looking for". ...and if you're a Python guru, you might spend a little bit of time trying to figure out if the range() is causing the problem due to allocating a large chunk of memory -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "...if I were on life-support, I'd rather have it run by a Gameboy than a Windows box." --Cliff Wells -- http://mail.python.org/mailman/listinfo/python-list
datetime questions
Hello Learning python datetime somewhat similar to SQL type timestamp my attempt creating a 24 h 2 months ago is str(datetime.now () - timedelta (days = 60)) +' cron '+ str(datetime.now () - timedelta (days = 59)) Do you agree? Can I improve this declaration? Regards Niklas Rosencrantz -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Sep 7, 10:12 am, Bruno Desthuilliers wrote: > Phlip a écrit : > > > Back to the topic, I tend to do this: > > > for record in Model.objects.filter(pk=42): > > return record > > > return sentinel > > WTF alert here... I don't see how anyone could WTF that. Are you pretending to be a newb who doesn't understanding it? F'em. > > Having lots of short methods helps, because return provides both > > control-flow and a result value. But it abuses 'for' to mean 'if'. I > > feel _rally_ guilty about that! > > But I miss this, from (cough) RoR: > > > record = Model.find(42) || sentinel > > > Django should provide this: > > > record = Model.objects.get(pk=42, _if_does_not_exist=sentinel) > > queryset.get can be used with multiple conditions - it's not necessarily > restricted to pk lookups. However you name your "_if_does_not_exist" > kwarg, it will be difficult to garantee that there will never be no > nameclash with any possible valid model lookup argument... it can also be another method - .if_does_not_exist(sentinel). With a less sucky name. I would guess that Django provides some basic rules for avoiding name collisions. Nobody should call a field "pk__in", but if they do, they are screwed! But that's not important right now. > But if you feel like you found the correct name, you can of course > monkeypatch queryset !-) K now I gotta learn to add a new method to an existing class! -- http://mail.python.org/mailman/listinfo/python-list
Re: What the \xc2\xa0 ?!!
Brian D writes: > In an HTML page that I'm scraping using urllib2, a \xc2\xa0 > bytestring appears. > > The page's charset = utf-8, and the Chrome browser I'm using displays > the characters as a space. > > The page requires authentication: > https://www.nolaready.info/myalertlog.php > > When I try to concatenate strings containing the bytestring, Python > chokes because it refuses to coerce the bytestring into ascii. > > wfile.write('|'.join(valueList)) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position > 163: ordinal not in range(128) > > In searching for help with this issue, I've learned that the > bytestring *might* represent a non-breaking space. It in fact does. > > When I scrape the page using urllib2, however, the characters print > as   in a Windows command prompt (though I wouldn't be surprised if > this is some erroneous attempt by the antiquated command window to > handle something it doesn't understand). Yes, it's trying to interpret that as two cp1252 (or whatever) bytes instead of one unbreakable space. > > If I use IDLE to attempt to decode the single byte referenced in the > error message, and convert it into UTF-8, another error message is > generated: > weird = unicode('\xc2', 'utf-8') > > Traceback (most recent call last): > File "", line 1, in > weird = unicode('\xc2', 'utf-8') > UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 0: > unexpected end of data Which is to be expected, as you ripped a UTF-8 escape sequence in half. > > If I attempt to decode the full bytestring, I don't obtain a human- > readable string (expecting, perhaps, a non-breaking space): You obtain a non-breakable space. What do you think it should look like in your terminal? It looks like ... nothing. Because it looks like a space. > weird = unicode('\xc2\xa0', 'utf-8') par = ' - '.join(['This is', weird]) par > u'This is - \xa0' > > I suspect that the bytestring isn't UTF-8, but what is it? Latin1? No, it is UTF-8 > weirder = unicode('\xc2\xa0', 'latin1') weirder > u'\xc2\xa0' 'This just gets ' + weirder > u'This just gets \xc2\xa0' > > Or is it a Microsoft bytestring? This is not weird, this is the python interpreter giving you the representation of a unicode-object when you do not print, so you can see what it looks like. And because you wrongly decoded it as latin1, it's garbage anyway. > weirder = unicode('\xc2\xa0', 'mbcs') 'This just gets ' + weirder > u'This just gets \xc2\xa0' > > None of these codecs seem to work. UTF-8 worked just fine. > > Back to the original purpose, as I'm scraping the page, I'm storing > the field/value pair in a dictionary with each iteration through table > elements on the page. This is all fine, until a value is found that > contains the offending bytestring. I have attempted to coerce all > value strings into an encoding, but Python doesn't seem to like that > when the string is already Unicode: > > valuesDict[fieldString] = unicode(value, 'UTF-8') > TypeError: decoding Unicode is not supported > > The solution I've arrived at is to specify the encoding for value > strings both when reading and writing value strings. > > for k, v in valuesDict.iteritems(): > valuePair = ':'.join([k, v.encode('UTF-8')]) > [snip] ... > wfile.write('|'.join(valueList)) > > I'm not sure I have a question, but does this sound familiar to any > Unicode experts out there? > > How should I handle these odd bytestring values? Am I doing it > correctly, or what could I improve? The overall solution is to decode the page or parts of it in whatever decoding it is delivered. You mentioned that the page is delivered in UTF-8, so you should use whatever gives you that information to decode the returned body. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Phlip a écrit : Back to the topic, I tend to do this: for record in Model.objects.filter(pk=42): return record return sentinel WTF alert here... Having lots of short methods helps, because return provides both control-flow and a result value. But it abuses 'for' to mean 'if'. I feel _rally_ guilty about that! But I miss this, from (cough) RoR: record = Model.find(42) || sentinel Django should provide this: record = Model.objects.get(pk=42, _if_does_not_exist=sentinel) queryset.get can be used with multiple conditions - it's not necessarily restricted to pk lookups. However you name your "_if_does_not_exist" kwarg, it will be difficult to garantee that there will never be no nameclash with any possible valid model lookup argument... But if you feel like you found the correct name, you can of course monkeypatch queryset !-) -- http://mail.python.org/mailman/listinfo/python-list
What the \xc2\xa0 ?!!
In an HTML page that I'm scraping using urllib2, a \xc2\xa0 bytestring appears. The page's charset = utf-8, and the Chrome browser I'm using displays the characters as a space. The page requires authentication: https://www.nolaready.info/myalertlog.php When I try to concatenate strings containing the bytestring, Python chokes because it refuses to coerce the bytestring into ascii. wfile.write('|'.join(valueList)) UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 163: ordinal not in range(128) In searching for help with this issue, I've learned that the bytestring *might* represent a non-breaking space. When I scrape the page using urllib2, however, the characters print as   in a Windows command prompt (though I wouldn't be surprised if this is some erroneous attempt by the antiquated command window to handle something it doesn't understand). If I use IDLE to attempt to decode the single byte referenced in the error message, and convert it into UTF-8, another error message is generated: >>> weird = unicode('\xc2', 'utf-8') Traceback (most recent call last): File "", line 1, in weird = unicode('\xc2', 'utf-8') UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 0: unexpected end of data If I attempt to decode the full bytestring, I don't obtain a human- readable string (expecting, perhaps, a non-breaking space): >>> weird = unicode('\xc2\xa0', 'utf-8') >>> par = ' - '.join(['This is', weird]) >>> par u'This is - \xa0' I suspect that the bytestring isn't UTF-8, but what is it? Latin1? >>> weirder = unicode('\xc2\xa0', 'latin1') >>> weirder u'\xc2\xa0' >>> 'This just gets ' + weirder u'This just gets \xc2\xa0' Or is it a Microsoft bytestring? >>> weirder = unicode('\xc2\xa0', 'mbcs') >>> 'This just gets ' + weirder u'This just gets \xc2\xa0' None of these codecs seem to work. Back to the original purpose, as I'm scraping the page, I'm storing the field/value pair in a dictionary with each iteration through table elements on the page. This is all fine, until a value is found that contains the offending bytestring. I have attempted to coerce all value strings into an encoding, but Python doesn't seem to like that when the string is already Unicode: valuesDict[fieldString] = unicode(value, 'UTF-8') TypeError: decoding Unicode is not supported The solution I've arrived at is to specify the encoding for value strings both when reading and writing value strings. for k, v in valuesDict.iteritems(): valuePair = ':'.join([k, v.encode('UTF-8')]) [snip] ... wfile.write('|'.join(valueList)) I'm not sure I have a question, but does this sound familiar to any Unicode experts out there? How should I handle these odd bytestring values? Am I doing it correctly, or what could I improve? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On Tue, Sep 7, 2010 at 4:39 AM, Baba wrote: > On 7 sep, 02:18, Ben Finney wrote: >> Ben Finney writes: >> > We value respect for people here, and that's what you've been shown >> > consistently. But respect for opinions, or for delicacy about >> > learning, is not welcome here. >> >> Sloppy wording, I apologise. This should say “… is not respect for a >> person”. >> >> > In other words, we treat people as adults by default. I hope you'll >> > continue to participate in that spirit. >> >> This is the main thrust of the message. >> >> -- >> \ “What if the Hokey Pokey IS what it's all about?” —anonymous | >> `\ | >> _o__) | >> Ben Finney > > Yes Master :) This is in almost impressively poor taste. I know I'm done helping you now. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Back to the topic, I tend to do this: for record in Model.objects.filter(pk=42): return record return sentinel Having lots of short methods helps, because return provides both control-flow and a result value. But it abuses 'for' to mean 'if'. I feel _rally_ guilty about that! But I miss this, from (cough) RoR: record = Model.find(42) || sentinel Django should provide this: record = Model.objects.get(pk=42, _if_does_not_exist=sentinel) sentinel could be a lambda that concocts a new record (where such a record should not be created with get_or_create()). That would be efficient when you don't spend time constructing it just so the happy- path of .get() can throw it away. Or sentinel could be None, or a NullObject that efficiently behaves like a record but provides stubbed-out behaviors. My committees will be submitting these proposals to the Django committees shortly... C-: -- Phlip -- http://mail.python.org/mailman/listinfo/python-list
audio time-stretching?
Does anyone know of a Python module for *moderate* "time-stretching"[1] an MP3 (or AIFF) file? FWIW, the audio I want to time-stretch is human speech. TIA! ~K [1] By "moderate time stretching" I mean, for example, taking an audio that would normally play in 5 seconds, and stretch it so that it plays in 7.5 seconds, keeping the pitch unchanged. A lot of software out there does this badly; e.g. the time-stretched audio springs extraneous "beats" of intensity that are very obtrusive and annoying; I guess it's some weird wave self-interference effect. Also, I stress *moderate* time stretching to explicitly rule out the extreme (~50X) time-stretching that software like PaulStretch is designed to accomplish. -- http://mail.python.org/mailman/listinfo/python-list
Re: Minimum and Maximum of a list containing floating point numbers
On Sep 6, 10:31 pm, Steven D'Aprano wrote: > On Tue, 07 Sep 2010 11:00:45 +1000, Ben Finney wrote: > > If you're going to use the list of float objects, you can convert them > > all with a list comprehension. > [...] > > >>> numbers_as_float = [float(x) for x in numbers_as_str] > > That's awfully verbose. A map is simpler: > > numbers_as_float = map(float, numbers_as_str) > > -- > Steven In Python 3.x it has one disadvantage: >>> numbers_as_float = map(float, numbers_as_str) >>> max(numbers_as_float) 10.24 >>> min(numbers_as_float) Traceback (most recent call last): File "", line 1, in min(numbers_as_float) ValueError: min() arg is an empty sequence >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
On 2010-09-07, Baba wrote: > Sloppy wording, I apologise. This should say: If you find the > question you're reading too easy then just don't answer. Noone is the > owner of a democratic forum where freedom to ask the question one > likes is paramount (as long of course as it is related to the > group)...so let me repeat that, to say "Please do us a favour and at > least try to figure things out on your own" is in my view > inappropriate. You need to read this: http://www.catb.org/esr/faqs/smart-questions.html > To me it sounds "Do me a favur and get lost". No, it means "Do yourself a favor, learn how to do things yourself." Remember: you're then one asking people to give you something for free. It's not up to them to conform to your expectations, rather you need to conform to theirs. Otherwise, they'll just ignore you. -- Grant Edwards grant.b.edwardsYow! FOOLED you! Absorb at EGO SHATTERING impulse gmail.comrays, polyester poltroon!! -- http://mail.python.org/mailman/listinfo/python-list
Volunteer help with porting
Hi everyone, My name is Prashant Kumar and I wish to contribute to the Python development process by helping convert certain existing python over to python3k. Is there anyway I could obtain a list of libraries which need to be ported over to python3k, sorted by importance(by importance i mean packages which serve as a dependency for larger number of packages being more important). I had originally mailed the python-dev mailing list and was pointed to this ML so that I could get details regarding 3rd party libraries which need to be ported. Thanks, Prashant Kumar -- http://mail.python.org/mailman/listinfo/python-list
Re: knowing the caller of an import && exec question
bussiere bussiere a écrit : i've got toto.py : import titi def niwhom(): pass and titi.py : def nipang(): pass how can i know in titi.py that's it's toto.py that is calling titi.py and the path of toto ? You'd have to inspect the call stack. Not for the faint at heart... And why : bidule.py : class bidetmusique: pass The naming convention is to capitalize class names, ie "Bidetmusique" or "BidetMusique" Heureusement qu'il n'y a pas grand monde ici pour comprendre le français, parce que comme nommage, ça bat des records, là !-) truc.py : X = __import__("bidule") why exec("X.bidetmusique()") return none exec doesn't "return" anything - it executes code in a given context, eventually modifying the context. Now given your above code, a new bidetmusique instance is indeed created, but since it's not bound to anything, it's immediatly discarded. and X.bidetmusique() return an object ? cf above How could i do to make this string "X.bidetmusique()" return an object ? exec is 99 time out of 10 (nope, not a typo) the wrong solution. You already found how to dynamically import a module by name (I mean, name given as as string), all you need know is to find out how to dynamically retrieve a module attribute given it's name as string. And the answer is "getattr": # truc.py : X = __import__("bidule") cls = getattr(X, "bidetmusique") obj = cls() print obj HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
de...@web.de writes: > Objects can be mutable or immutable. For example, in Python, integers, > strings, floats and tuples are immutable. That means that you can't > change their value. Yes. Importantly, wherever you see code that you *think* is changing the value of an immutable object, you're thinking incorrectly. (There's no shame in that; other languages give us preconceptions that can be hard to shake off.) The only way to get a different value from an integer object is to ask Python for a different integer object; the original is unchanged. The same goes for tuples, strings, and all the other immutable types. > Mutable objects OTOH can be changed. […] Some good articles to explain Python's object model: http://effbot.org/zone/python-objects.htm> http://docs.python.org/reference/datamodel.html#objects-values-and-types> -- \ “We can't depend for the long run on distinguishing one | `\ bitstream from another in order to figure out which rules | _o__) apply.” —Eben Moglen, _Anarchism Triumphant_, 1999 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
In article , BartC wrote: >"Steven D'Aprano" wrote in message >news:4c85adfe$0$5$c3e8...@news.astraweb.com... >> >> xrange = range >> >> There, that wasn't hard, was it? > >I think I just learned more about Python than from months of reading this >group. > >So 'range' is just a class like any other. And that a class is something you >can blithely copy from one variable to another. And whenever you see 'range' >anywhere, you can't always be certain that someone hasn't done: > >range = 42 > >at some point. That explains a lot about the difficulties of implementing >Python efficiently. (And the xrange=range trick works well thanks.) Actually, range() is a function. But the same point applies, squared -- you really can never know what kind of object is hiding behind a name in the general case. -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "...if I were on life-support, I'd rather have it run by a Gameboy than a Windows box." --Cliff Wells -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba writes: > to say "Please do us a favour and at least try to figure things out on > your own" is in my view inappropriate. That's what the person wanted you to see. How would you prefer that exact information to be imparted to you? How could it have been communicated so that it was not misunderstood? > To me it sounds "Do me a favur and get lost". Can you not understand > that? Honestly, I cannot read that meaning into the messages you've received in this forum. Please help us by showing how you think people could say what was said above without losing information. > no offence now ok, it's not all that serious...open your mind, let > lose all that righteousness and let's enjoy life :) All well and good, but let's try to see what went wrong in communication here and fix it. -- \ “Faith, n. Belief without evidence in what is told by one who | `\ speaks without knowledge, of things without parallel.” —Ambrose | _o__) Bierce, _The Devil's Dictionary_, 1906 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed - function apparently global cannot be called.
Hi Bruno, Thanks for your quick response. I still do not understand. On 07/09/2010 11:50, Bruno Desthuilliers wrote: Ian Hobson a écrit : Hi all you experts, This has me beat. Has anyone any ideas about what might be going wrong? This is code from within a windows service (hence no print statements - no sys.stdout to print on!). I am trying to trace through to find where the code is not working. No stdout so I have to log to a file. Then you'd be better using the logging module from the stdlib. And FWIW, you should try to make you code testable in a non-service context... Thanks for the tip. "Batteries included" means there are so many batteries you miss some. The Module I am trying to use works perfectly as a free standing routine. I have the following code fragments. def log(message): f = open('d:\logfile.txt','a') f.write(message + "\n") f.close() from DelNotePrinter import DelNotePrinter The convention is to use all_lower_names for modules - having modules and classes with the same (case-sensitive) name can be very misleading. The convention is a good one. note the order of the above - log is defined before the import. And ? Do you think it will affect the imported module in any way ? Like, say, magically "inject" your log function in the DelNotePrinter module ?-) Just that log is defined before the global log is encountered to that if the compiler needed to set up a link at compile time it was able to do so. Later in the source Where ? About 350 lines further down. I have log('disPrint is:'+disPrint) log('count is:'+count) Do yourself a favor and learn string formating... Agreed. :) - These are debug statements to discover what is going on. I have had so much trouble with problems NOT being reported, that I avoid anything that I am not 100% sure must work. log(repr(DelNotePrinter)) printer = DelNotePrinter(disPrint,int(count)) The DelNotePrinter.py file cannot us log even though it is declared as global. In Python, "global" means "module-level", and it's only necessary when you want to rebind a module-level name from within a function or method. Exactly! I want to bind the name log to the function I wrote. So I carefully placed log in global scope and told the compiler that when I referred to log I meant the global one I wrote. I expected that to bind log to the function I wrote. The compiler did not complain it was undefined (which would have caused a trackback on the Event log). But neither was the routine called. :( I still don't understand what is going wrong. The code is... # coding=utf8 #DelNotePrinter = code to print delivery notes assorted imports removed for space reasons Some of these imports surely explain why you don't just get a NameError when trying to call log() - wild guess : you have some "from xxx import *" statement that does import another callable named 'log'. I don't think so. The lines are import sys import datetime from PyQt4.QtCore import * from PyQt4.QtGui import * from DataBaseClass import DataBase I know all PyQt classes begin with Q. Database I wrote, and it has no log in it. class DelNotePrinter(object): ''' Print Delivery Note on A5 in portrait ''' def __init__(self,printer,copies): ''' create printer and painter ''' global font,sm,log log('DelNotePrinter: starting') self.printer = QPrinter(QPrinter.HighResolution) If you want to access a name (function, class, whatever) defined in another module, you have to explicitely import it. The file the log writes contains.. disPrint is:HP Deskjet 6940 series count is:1 The > is followed by a newline and end of file! Where is the DelNotePrinter: starting message? We can't tell - but you can get at least some hint, cf below If I replace the opening of __init__ with global font,sm,log f = open('d:\logfile.txt','a') f.write('DelNotePrinter: starting' + "\n") f.close() self.printer = QPrinter(QPrinter.HighResolution) then the message IS written to the log file. Obviously, yes. Now please add this to your code: class DelNotePrinter(object): ''' Print Delivery Note on A5 in portrait ''' def __init__(self,printer,copies): ''' create printer and painter ''' global font,sm,log f = open('d:\logfile.txt','a') f.write('DelNotePrinter: starting' + "\n") # check what "log" is bound to in the currrent namespace f.write( "DelNotePrinter : log is '%s' from '%s'" % ( log, log.__module__ )) f.close() self.printer = QPrinter(QPrinter.HighResolution) I tried that (using copy/paste) and got no output! So I modified is slightly to global log f = open('d:\logfile.txt','a') f.write("test message\n") f.write( "DelNotePrinter : log is '%s' from '%s'" % (
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
sajuptpm wrote: > On Sep 7, 7:03 pm, Peter Otten <__pete...@web.de> wrote: >> sajuptpm wrote: >> > i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in >> > another way .I want to know what the modification needed in the 'if' >> > check to sort this list of tuples in k[0][0] ascending and k[0][1] >> > descending. >> >> It seems you are not getting any closer to your goal. Perhaps it would >> help if you could explain that goal clearly rather than describing the >> means you are employing to achieve it. >> >> > I have a list of tuples. >> >> > l = [((30,50),(70)), ((50,20),(20))] >> >> By the way, (42) is not a tuple, it's an integer. To turn it into a >> 1-tuple you have to add a ',': >> >> >>> (42) >> 42 >> >>> (42,) >> (42,) >> >>> 42, >> >> (42,) >> >> Peter > > > I have a list of tuples. > > l = [((30,50),(70,)), ((50,20),(20,))] > > for i in range(10): > k = ((i+30,i+50),(i+70))#suppose creating new tuple in each > iteration > using some random value and in sert it into list. > > flag=True > for i, v in enumerate(l): > if v >= k: > l.insert(i,k) > flag = False > break > if flag: > l.append(k) > > This code will give a list of tuples sorted in asc order. > I need to change this code to sort this list by k[0][0] ascending and > k[0][1] descending. > What are the modifications needed in this code. > I dont want to user sort() method of list. > > i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in > another way .I want to know what the modification needed in the 'if' > check to sort this list of tuples in k[0][0] ascending and k[0][1] > descending. As a thought experiment assume that your audience had never heard of tuples or even Python. How would you then explain your goal? Peter -- http://mail.python.org/mailman/listinfo/python-list
knowing the caller of an import && exec question
i've got toto.py : import titi def niwhom(): pass and titi.py : def nipang(): pass how can i know in titi.py that's it's toto.py that is calling titi.py and the path of toto ? And why : bidule.py : class bidetmusique: pass truc.py : X = __import__("bidule") why exec("X.bidetmusique()") return none and X.bidetmusique() return an object ? How could i do to make this string "X.bidetmusique()" return an object ? Regards and thanks Bussiere Eat the strawberry Google Fan boy -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
On Sep 7, 7:03 pm, Peter Otten <__pete...@web.de> wrote: > sajuptpm wrote: > > i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in > > another way .I want to know what the modification needed in the 'if' > > check to sort this list of tuples in k[0][0] ascending and k[0][1] > > descending. > > It seems you are not getting any closer to your goal. Perhaps it would help > if you could explain that goal clearly rather than describing the means you > are employing to achieve it. > > > I have a list of tuples. > > > l = [((30,50),(70)), ((50,20),(20))] > > By the way, (42) is not a tuple, it's an integer. To turn it into a 1-tuple > you have to add a ',': > > >>> (42) > 42 > >>> (42,) > (42,) > >>> 42, > > (42,) > > Peter I have a list of tuples. l = [((30,50),(70,)), ((50,20),(20,))] for i in range(10): k = ((i+30,i+50),(i+70))#suppose creating new tuple in each iteration using some random value and in sert it into list. flag=True for i, v in enumerate(l): if v >= k: l.insert(i,k) flag = False break if flag: l.append(k) This code will give a list of tuples sorted in asc order. I need to change this code to sort this list by k[0][0] ascending and k[0][1] descending. What are the modifications needed in this code. I dont want to user sort() method of list. i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in another way .I want to know what the modification needed in the 'if' check to sort this list of tuples in k[0][0] ascending and k[0][1] descending. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
On Tue, Sep 7, 2010 at 6:56 AM, Bruno Desthuilliers wrote: > Phlip a écrit : >>> >>> How does that compare to, say, the "Kamikaze Principle"? ;) >> >> Return victorious AND not at all! >> >> (All return values are packed up and thrown...;) > > ... and then it raises a SystemError !-) general protection fault Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
Baba writes: > Hi > > I am working on an exercise which requires me to write a funtion that > will check if a given word can be found in a given dictionary (the > hand). > > def is_valid_word(word, hand, word_list): > """ > Returns True if word is in the word_list and is entirely > composed of letters in the hand. Otherwise, returns False. > Does not mutate hand or word_list.""" > > I don't understand this part: Does not mutate hand or word_list > > I tried to google "python mutate list input" but to no avail > > It would be great if someone could give me a brief explanantion of the > mutation concept. Objects can be mutable or immutable. For example, in Python, integers, strings, floats and tuples are immutable. That means that you can't change their value. Mutable objects OTOH can be changed. For example, a list is mutable: l = ["foo"] l.append("bar") # mutating method print l #-> ['foo', 'bar'] That's all there is to it. So for the example at hand, don't use anything that mutates the passed arguments. E.g, if word_list really is a list, and for faster lookup of "word", you want to sort it, you are not allowed to do this: word_list.sort() # mutating!! Instead, you need to do new_word_list = sorted(word_list) # creates a *copy* of word_list, which is sorted. Actually, you can also try & use the module copy's "deepcopy"-function to ensure that you don't mutate the passed objects. Please not that this is *not* a mutating operation: l = [1, 2] h = l l = [3, 4] print h #-> [1, 2] The original list in l is still preserved un-modified, and referenced by the name h. Just binding a different object to an existing name doesn't change anything about the old object referenced by the name. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
sajuptpm wrote: > i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in > another way .I want to know what the modification needed in the 'if' > check to sort this list of tuples in k[0][0] ascending and k[0][1] > descending. It seems you are not getting any closer to your goal. Perhaps it would help if you could explain that goal clearly rather than describing the means you are employing to achieve it. > I have a list of tuples. > > l = [((30,50),(70)), ((50,20),(20))] By the way, (42) is not a tuple, it's an integer. To turn it into a 1-tuple you have to add a ',': >>> (42) 42 >>> (42,) (42,) >>> 42, (42,) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Help needed - function apparently global cannot be called.
Ian Hobson a écrit : (snip) you may also want to read the recent "using modules" thread... -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
Phlip a écrit : How does that compare to, say, the "Kamikaze Principle"? ;) Return victorious AND not at all! (All return values are packed up and thrown...;) ... and then it raises a SystemError !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
Baba a écrit : Hi I am working on an exercise which requires me to write a funtion that will check if a given word can be found in a given dictionary (the hand). def is_valid_word(word, hand, word_list): """ Returns True if word is in the word_list and is entirely composed of letters in the hand. Otherwise, returns False. Does not mutate hand or word_list.""" I don't understand this part: Does not mutate hand or word_list Everything in Python is an object. A few objects are immutable (ints, strings, tuples...), ie you there's no way to modify the state of the object in place. Most objects are mutable, and this obviously includes lists and dicts (you can add / remove / replace list or dicts elements). Now the point is that when passing an object as argument to a function, you don't pass a copy of the object but the object itself, so if the object is mutable, you can mutate if from within the function. A simple (and really dumb) example being worth a thousand words: # mutate.py def update_dict(dic, key, value): print "in update_dic : dic id is %s" % id(dic) dic[key] = value def main(): dic = dict(a=1, b=2) lst = [1, 2, 3] print "in main : dic id is %s" % id(dic) print "dic : %s" % dic print "calling update_dict" update_dict(dic, "c", 3) print "after update_dict" print "in main : dic id is %s" % id(dic) print "dic : %s" % dic if __name__ == '__main__': main() I know that a ditionary is unordered. How Can i however avoid 'acidental' mutation? This has nothing to do with dicts being ordered or not. And there's NO "accidental" mutation - you have to explicitely call a method or operator that mutate the dict. -- http://mail.python.org/mailman/listinfo/python-list
Re: accessing a text file
Baba a écrit : (snip) If i had received a friendly response from Benjamin (as opposed to "Please do us a favor and at least try to figure things out on your own") According to usenet standards and given your initial question, this is a _very_ friendly answer. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Samurai Principle
> How does that compare to, say, the "Kamikaze Principle"? ;) Return victorious AND not at all! (All return values are packed up and thrown...;) -- http://mail.python.org/mailman/listinfo/python-list
another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))
I have a list of tuples. l = [((30,50),(70)), ((50,20),(20))] for i in range(10): k = ((i+30,i+50),(i+70))#suppose creating new tuple in each iteration using some random value and in sert it into list. flag=True for i, v in enumerate(l): if v >= k: l.insert(i,k) flag = False break if flag: l.append(k) This code will give a list of tuples sorted in asc order. I need to change this code to sort this list by k[0][0] ascending and k[0][1] descending. What are the modifications needed in this code. I dont want to user sort() method of list. i need to implement l.sort(key=lambda x:(x[0][0], -x[1][0])) in another way .I want to know what the modification needed in the 'if' check to sort this list of tuples in k[0][0] ascending and k[0][1] descending. -- http://mail.python.org/mailman/listinfo/python-list
Re: mutate dictionary or list
On 7 September 2010 22:05, Baba wrote: > > It would be great if someone could give me a brief explanantion of the > mutation concept. > In this case, to mutate is to change. If you must not mutate the list, you must not change it. In another words, reading from the list is fine. Writing to it is not. Cheers, Xav -- http://mail.python.org/mailman/listinfo/python-list
Pydev 1.6.2 Released
Hi All, Pydev 1.6.2 has been released Details on Pydev: http://pydev.org Details on its development: http://pydev.blogspot.com Release Highlights: --- * Pydev is now also distributed with Aptana Studio 3, so it can be gotten in a version that doesn't require installing it as a separate plugin. Get it at: http://aptana.com/products/studio3/download * Django templates editor (requires Aptana Studio 3) o Supports HTML files with HTML, CSS and Javascript o Supports CSS files o Outline page o Code-completion for Django templates based on templates (window > preferences > pydev > django templates editor > templates) o Code-completion for HTML, CSS and Javascript o Syntax highlighting based on the templates with the 'Django tags' context o Colors based on the Aptana themes * Python 2.7 grammar supported * Fixed indexing issue on contents getting getting stale in the cache * Fixed issue where the partitioning became wrong when entering a multiline string * Colors in the compare editor are now correct when using the Aptana themes * Extract method refactoring now works with "import" and "from ... import" inside a method * Source folders now appear before other folders * Fixed False positive on code analysis when using the property decorator What is PyDev? --- PyDev is a plugin that enables users to use Eclipse for Python, Jython and IronPython development -- making Eclipse a first class Python IDE -- It comes with many goodies such as code completion, syntax highlighting, syntax analysis, refactor, debug and many others. Cheers, -- Fabio Zadrozny -- Software Developer Aptana http://aptana.com/ Pydev - Python Development Environment for Eclipse http://pydev.org http://pydev.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Speed-up for loops
In article , "BartC" wrote: > (BTW why doesn't Python 3 just accept 'xrange' as a > synonym for 'range'?) If you've ever tried to maintain a legacy code base, you'll understand why "there's only one way to do it" is A Good Thing. Imagine that you're looking at some code which was written years ago, by people who are no longer around to answer questions. In one place, you see: for i in range(n): blah and in another, you see: for j in xrange(n): blah If you are truly a Python expert, you'll say to yourself, "range and xrange are synonyms", and move on to other things. If, however, you're not really an expert, you'll look at this and say, "Hmmm, in one place they used range(), and in another they used xrange(). Clearly, there's some reason for the difference, I need to figure out what it is, because that's probably key to my understanding why this code isn't working". So, you spend the next two hours pouring over reference manuals trying to understand the subtle difference, until your friend comes over and says, "You dolt, you just wasted half the afternoon. They're the same thing. Move on, this is not the bug you're looking for". If you think stuff like that can't happen, you've probably never jumped into the middle of a legacy code base written in a language you don't know very well. -- http://mail.python.org/mailman/listinfo/python-list