Re: Unrecognized escape sequences in string literals
On Mon, 10 Aug 2009 15:17:24 -0700, Douglas Alan wrote: > From: Steven D'Aprano wrote: > >> On Mon, 10 Aug 2009 00:32:30 -0700, Douglas Alan wrote: > >> > In C++, if I know that the code I'm looking at compiles, then I never >> > need worry that I've misinterpreted what a string literal means. > >> If you don't know what your string literals are, you don't know what >> your program does. You can't expect the compiler to save you from >> semantic errors. Adding escape codes into the string literal doesn't >> change this basic truth. > > I grow weary of these semantic debates. The bottom line is that C++'s > strategy here catches bugs early on that Python's approach doesn't. It > does so at no additional cost. > > From a purely practical point of view, why would any language not want > to adopt a zero-cost approach to catching bugs, even if they are > relatively rare, as early as possible? Because the cost isn't zero. Needing to write \\ in a string literal when you want \ is a cost, and having to read \\ in source code and mentally translate that to \ is also a cost. By all means argue that it's a cost that is worth paying, but please stop pretending that it's not a cost. Having to remember that \n is a "special" escape and \y isn't is also a cost, but that's a cost you pay in C++ too, if you want your code to compile. By the way, you've stated repeatedly that \y will compile with a warning in g++. So what precisely do you get if you ignore the warning? What do other C++ compilers do? Apart from the lack of warning, what actually is the difference between Python's behaviour and C++'s behaviour? > (Other than the reason that adopting it *now* is sadly too late.) > > Furthermore, Python's strategy here is SPECIFICALLY DESIGNED, according > to the reference manual to catch bugs. I.e., from the original posting > on this issue: > > Unlike Standard C, all unrecognized escape sequences are left in > the string unchanged, i.e., the backslash is left in the string. > (This behavior is useful when debugging: if an escape sequence is > mistyped, the resulting output is more easily recognized as > broken.) You need to work on your reading comprehension. It doesn't say anything about the motivation for this behaviour, let alone that it was "SPECIFICALLY DESIGNED" to catch bugs. It says it is useful for debugging. My shoe is useful for squashing poisonous spiders, but it wasn't designed as a poisonous-spider squashing device. >> The compiler can't save you from typing 1234 instead of 11234, or 31.45 >> instead of 3.145, or "My darling Ho" instead of "My darling Jo", so why >> do you expect it to save you from typing "abc\d" instead of "abc\\d"? > > Because in the former cases it can't catch the the bug, and in the > latter case, it can. I'm not convinced this is a bug that needs catching, but if you think it is, then that's a reasonable argument. >> Perhaps it can catch *some* errors of that type, but only at the cost >> of extra effort required to defeat the compiler (forcing the programmer >> to type \\d to prevent the compiler complaining about \d). I don't >> think the benefit is worth the cost. You and your friend do. Who is to >> say you're right? > > Well, Bjarne Stroustrup, for one. Then let him design his own language *wink* > All of these are value judgments, of course, but I truly doubt that > anyone would have been bothered if Python from day one had behaved the > way that C++ does. If I'm reading this page correctly, Python does behave as C++ does. Or at least as Larch/C++ does: http://www.cs.ucf.edu/~leavens/larchc++manual/lcpp_47.html >> In C++, if you see an escape you don't recognize, do you care? > > Yes, of course I do. If I need to know what the program does. Precisely the same as in Python. >> Do you go running for the manual? If the answer is No, then why do it >> in Python? > > The answer is that I do in both cases. You deleted without answer my next question: "And if the answer is Yes, then how is Python worse than C++?" Seems to me that the answer is "It's not worse than C++, it's the same" -- in both cases, you have to memorize the "special" escape sequences, and in both cases, if you see an escape you don't recognize, you need to look it up. >> No. \z *is* a legal escape sequence, it just happens to map to \z. > >> If you stop thinking of \z as an illegal escape sequence that Python >> refuses to raise an error for, the problem goes away. It's a legal >> escape sequence that maps to backslash + z. > > (1) I already used that argument on my friend, and he wasn't buying it. > (Personally, I find the argument technically valid, but commonsensically > invalid. It's a language-lawyer kind of argument, rather than one that > appeals to any notion of real aesthetics.) I disagree with your sense of aesthetics. I think that having to write \\y when I want \y just to satisfy a bondage-and-discipline compiler is
Re: ElementTree - Howto access text within XML tag element...
In article <[email protected]>, cmalmqui wrote: > I am writing on a small XML parser and are currently stuck as I am not > able to get the whole element name in ElementTree. > > Please see the below example where "print root[0][0]" returns > "" > > Is there a way to get hold of the "Running" string in the tag using > elementTree? > > > > 2009-07-10T14:48:00Z > > . "Running" is the value of the "Sport" attribute of the "Activity" element. The documentation for the Element interface lists several ways to access element attributes; in your example, >>> elem = root[0][0] >>> elem.get("Sport") 'Running' >>> elem.attrib {'Sport': 'Running'} >>> elem.items() [('Sport', 'Running')] See http://docs.python.org/library/xml.etree.elementtree.html -- Ned Deily, [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pickle a lambda function?
Terry wrote: > I'm trying to implement something like: > > remote_map(fun, list) > > to execute the function on a remove machine. But the problem is I > cannot pickle a lambda function and send it to the remote machine. > > Is there any possible way to pickle (or other method) any functions > including lambda? > You can pickle any named functions that are declared at module scope. You cannot pickle anonymous functions, methods, or functions declared nested inside other functions. The function must be present in the same module when you unpickle it, and if the definition has changed between pickling and unpickling the new definition will be used (just as other instances will use the current class definition not the one they were pickled with). You probably could pickle some of the components needed to create your lambda and construct a new function from it when unpickling: try the code object, the name of the module to be used for the globals, and default arguments. I don't think you can pickle the closure so better make sure your lambda doesn't need one, and be very careful to ensure that you restore the pickle in the same version of Python otherwise the code object might break. Best just avoid this and use named functions for anything that needs pickling. -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Social problems of Python doc [was Re: Python docs disappointing]
r gmail.com> writes: > > On Aug 9, 11:02 pm, David Lyon wrote: > > Since you're talking about documentation, which is a part of python, > > don't you think you should be discussing it on python-dev ? > > Yea, them's be a friendly bunch to noob ideas ;). Hey i got a better > idea, lets go to the IRS and see if we can persuade them to stop > taxing us... You know, the most interesting thing in this thread is certainly its title : « Social problems of Python doc » Yes, the little social problem here should be clear: if you have complaints to voice or improvements to suggest to the Python docs, you should do so on the issue tracker (*). For most topics, this is the only reasonable way to signal problems to the Python developers community, and so it is in most free software / open source projects. Just because you are able to write tongue-in-cheek (**) comments on python-list or, even worse, on a third party website, and generate a long thread about how Python doc (supposedly) s*cks 1) doesn't mean there is a legitimate issue (we all know how people can quickly inflame about empty subjects) 2) even though there can be a legitimate issue, doesn't mean Python developers will go out of their way and parse the entirety of the messages to find potentially useful data in them. The bug tracker is the place for this, and it's your task, if you want to help, to submit suggestions in it. FYI, the Python doc is very actively maintained nowadays, and bug reports /are/ taken into account. If you think you've got a lot of time for ranting about how the doc sucks, but don't want to spend the couple of minutes needed to post issues on the bug tracker, it speaks a lot about your motivation. Admittedly, in every successful community, there are attention seekers who are not interested in actual participation. (*) http://bugs.python.org (**) yes, humour is fine, but it doesn't replace actual, informational content Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree - Howto access text within XML tag element...
cmalmqui schrieb: Hi, I am writing on a small XML parser and are currently stuck as I am not able to get the whole element name in ElementTree. Please see the below example where "print root[0][0]" returns "" Is there a way to get hold of the "Running" string in the tag using elementTree? 2009-07-10T14:48:00Z . For those of you that know how to program XML I have another question: I am currently "hardcoding" my XML parser using brackets, is this a good approach or should I build it using a "search on tag" approach. What do you mean by that - hardcoding by brackets? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Social problems of Python doc [was Re: Python docs disappointing]
Antoine Pitrou wrote: r gmail.com> writes: On Aug 9, 11:02 pm, David Lyon wrote: Since you're talking about documentation, which is a part of python, don't you think you should be discussing it on python-dev ? Yea, them's be a friendly bunch to noob ideas ;). Hey i got a better idea, lets go to the IRS and see if we can persuade them to stop taxing us... You know, the most interesting thing in this thread is certainly its title : « Social problems of Python doc » Yes, the little social problem here should be clear: if you have complaints to voice or improvements to suggest to the Python docs, you should do so on the issue tracker (*). For most topics, this is the only reasonable way to signal problems to the Python developers community, and so it is in most free software / open source projects. Just because you are able to write tongue-in-cheek (**) comments on python-list or, even worse, on a third party website, and generate a long thread about how Python doc (supposedly) s*cks 1) doesn't mean there is a legitimate issue (we all know how people can quickly inflame about empty subjects) 2) even though there can be a legitimate issue, doesn't mean Python developers will go out of their way and parse the entirety of the messages to find potentially useful data in them. The bug tracker is the place for this, and it's your task, if you want to help, to submit suggestions in it. FYI, the Python doc is very actively maintained nowadays, and bug reports /are/ taken into account. If you think you've got a lot of time for ranting about how the doc sucks, but don't want to spend the couple of minutes needed to post issues on the bug tracker, it speaks a lot about your motivation. Admittedly, in every successful community, there are attention seekers who are not interested in actual participation. (*) http://bugs.python.org (**) yes, humour is fine, but it doesn't replace actual, informational content Antoine. Thank you for this fine, cultured, reasonable response. Seriously!!! -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pickle a lambda function?
On Aug 11, 3:42 pm, Duncan Booth wrote: > Terry wrote: > > I'm trying to implement something like: > > > remote_map(fun, list) > > > to execute the function on a remove machine. But the problem is I > > cannot pickle a lambda function and send it to the remote machine. > > > Is there any possible way to pickle (or other method) any functions > > including lambda? > > You can pickle any named functions that are declared at module scope. > > You cannot pickle anonymous functions, methods, or functions declared > nested inside other functions. The function must be present in the same > module when you unpickle it, and if the definition has changed between > pickling and unpickling the new definition will be used (just as other > instances will use the current class definition not the one they were > pickled with). > > You probably could pickle some of the components needed to create your > lambda and construct a new function from it when unpickling: try the code > object, the name of the module to be used for the globals, and default > arguments. I don't think you can pickle the closure so better make sure > your lambda doesn't need one, and be very careful to ensure that you > restore the pickle in the same version of Python otherwise the code object > might break. Best just avoid this and use named functions for anything that > needs pickling. > > -- > Duncan Boothhttp://kupuguy.blogspot.com Yes, I'm think of pickle (actually marshal) the code object. Otherwise I have to use string and eval:-( The reason I need to be able to pickle any function is because I want my remote machine knows nothing about the function before receiving it, so I don't need to update the source code in the remote machine very often. br, terry -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem Regarding Handling of Unicode string
On Aug 10, 9:26 pm, joy99 wrote:
> Dear Group,
>
> I am using Python26 on WindowsXP with service pack2. My GUI is IDLE.
> I am using Hindi resources and get nice output like:
> एक
> where I can use all the re functions and other functions without doing
> any transliteration,etc.
> I was trying to use Bengali but it is giving me output like:
WHAT is giving you this output?
> '\xef\xbb\xbf\xe0\xa6\x85\xe0\xa6\xa8\xe0\xa7\x87\xe0\xa6\x95'
In a very ordinary IDLE session (Win XP SP3, Python 2.6.2, locale:
Australia/English, no "Hindi resources"):
>>> x = '\xef\xbb\xbf\xe0\xa6\x85\xe0\xa6\xa8\xe0\xa7\x87\xe0\xa6\x95'
>>> ux = x.decode('utf-8')
>>> ux
u'\ufeff\u0985\u09a8\u09c7\u0995'
>>> print ux
অনেক # looks like what you wanted; please confirm
>>> import unicodedata
>>> for c in ux:
print unicodedata.name(c)
ZERO WIDTH NO-BREAK SPACE # this is a BOM
BENGALI LETTER A
BENGALI LETTER NA
BENGALI VOWEL SIGN E
BENGALI LETTER KA
>>>
> I wanted to see Bengali output as
> অনেক
> and I like to use all functions including re.
> If any one can help me on that.
"I am using Hindi resources" doesn't tell us much ... except to prompt
the comment that perhaps if you want to display Bengali script, you
may need Bengali resources. However it looks like I can display your
Bengali data without any special resources.
It seems like you are not doing the same with Bengali as you are doing
with Hindi. We can't help you very much if you don't show exactly what
you are doing.
Have you considered asking in an Indian Python forum? Note: you will
still need to say what you are doing that works with Hindi but not
with Bengali.
Cheers,
John
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
r a écrit : (snip) A little note for tutorial writers: == Dear Expert, Whilst writing any tutorial on any subject matter please remember, you may be an expert, but mostly *non-experts* will be reading your material... I can only second Paul on this : just like the K&R, Python's official tutorial targets experienced programmers, not total noobies. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On Fri, Jul 31, 2009 at 6:12 PM, Kee Nethery wrote: > I too find the Python docs not very useful and it really slows down my > learning curve. > > I wonder if it would make sense to find good tech writers, get a quotes, > and get some professionally written documentation WITH LOTS OF EXAMPLES > added to the standard Python documentation tree. > > I'd chip in money for that task. I've certainly spent enough buying Python > books to where it would be very reasonable to chip in the cost of one book > towards this project. Get enough people ... could be a great thing. > > Even though it is not the version I use, I would suggest that the really > detailed docs with lots of examples be written against the latest python > version. > > Just a thought. > > Kee Nethery > -- > http://mail.python.org/mailman/listinfo/python-list > One thing I really like about the PHP docs is the built in forums. User feedback on documentation is invaluable. -- http://mail.python.org/mailman/listinfo/python-list
Re: With or without leading underscore...
Carl Banks a écrit :
(snip)
class A(object):
def __init__(self,*args,**kwargs):
raise TypeError('Type not callable; use factory function
instead')
@classmethod
def _create_object(cls,initial_value):
self = object.__new__(cls) # avoid __init__
self.value = initial_value
I assume there's a missing "return self" line here ?-)
--
http://mail.python.org/mailman/listinfo/python-list
Is there any package implanation the following arithmetics?
Hi, I wonder if there has any package can check whether two rectangles are overlap, is a dot inside or outside a polygon, etc. Thanks a lot! -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help in configuration for TimedRotatingFileHandler
On Aug 10, 3:52 pm, Dave Angel wrote: > Lokesh Maremalla wrote: > > Traceback (most recent call last): > > File "c:\Python25\lib\logging\handlers.py", line 74, in emit > > self.doRollover() > > File "c:\Python25\lib\logging\handlers.py", line 274, in doRollover > > os.rename(self.baseFilename, dfn) > > WindowsError: [Error 32] The process cannot access the file because it is > > being used by another process > > Generally speaking, this error on os.rename() will occur if you haven't > properly closed the file first, and if I recall correctly, Unix would > have permitted a rename on an open file. You're on Windows. However, I > don't know how that works with theloggingpackage. If the program has > terminated, and you run it a second time on the following day (or after > changing the system time), then I'd expect no trouble. But I'm guessing > your program is still running, and you just want to change fromlogging > to yesterday's file tologgingto today's file. > > If I had to guess, I'd say that you have two instances of the logger > running, and the second one is still holding the handle open. But > that's just a wild guess. > It's certainly possible that there are two instances of the handler, which would cause this problem. Other things which might cause this problem are the file handle inherited by a child process, an anti- virus scanner having the file open for scanning (anti-virus scanners often scan files which have just been written, like log files) and indexing software such as Google Desktop or Windows' own indexing service. I would advise using sysinternals tools such as FileMon and Handle to see what's holding the file open. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
UML for Python or some other Graphical Notation Language
This is more an academic question right now but was there ever some work in progress how UML could be made better for Python or script languages in general. It is so extremely deep interwoven with Java/C++ language implementations that there are a lot of modified notiations necessary. Or is there anything else then UML? Eiffel still has BON for example. -- http://mail.python.org/mailman/listinfo/python-list
Search and write to .txt file
Hi everyone,
I am writing some python script that should find a line which contains
'1' in the data.txt file, then be able to move a certain number of
lines down, before replacing a line. At the moment, I am able to find
the line '1', but when I use f.seek to move, and then rewrite, what I
write goes to the end of the .txt file, instead of being adjusted by
my f.seek.
Do you know what way I should take?
Data.txt is a file of 3 lines:
line1
line2
line3
Code:
with open('data.txt', 'r+') as f:
firstread = f.readlines() # Take a snapshot of initial file
f.seek(0,0)# Go back to beginning and search
for line in f:
print line
if line.find('1'):
print 'line matched'
f.seek(1,1) # Move one space along
f.write('house\n') # f.write overwrites the exact
number of bytes.
break# leave loop once '1' is found
f.seek(0,0) # Go back to beginning, and read
data.txt again
lastread = f.readlines()
print 'firstread is', firstread
print 'lastread is', lastread
This shouldn't be too difficult, but I don't know how. > <
Help appreciated!
--
http://mail.python.org/mailman/listinfo/python-list
Re: Search and write to .txt file
On Tue, Aug 11, 2009 at 4:52 PM, Helvin wrote:
> Hi everyone,
>
> I am writing some python script that should find a line which contains
> '1' in the data.txt file, then be able to move a certain number of
> lines down, before replacing a line. At the moment, I am able to find
> the line '1', but when I use f.seek to move, and then rewrite, what I
> write goes to the end of the .txt file, instead of being adjusted by
> my f.seek.
>
> Do you know what way I should take?
>
It might be easier to read the file into a list of lines (using
readlines, as you do in your code already), make your change there and
write it back to a file. If your file is indeed as small as you
indicate below, that should be significantly easier.
> Data.txt is a file of 3 lines:
> line1
> line2
> line3
>
> Code:
>
> with open('data.txt', 'r+') as f:
> firstread = f.readlines() # Take a snapshot of initial file
>
> f.seek(0,0) # Go back to beginning and search
> for line in f:
> print line
> if line.find('1'):
> print 'line matched'
> f.seek(1,1) # Move one space along
> f.write('house\n') # f.write overwrites the exact
> number of bytes.
> break # leave loop once '1' is found
>
> f.seek(0,0) # Go back to beginning, and read
> data.txt again
> lastread = f.readlines()
>
> print 'firstread is', firstread
> print 'lastread is', lastread
>
> This shouldn't be too difficult, but I don't know how. > <
> Help appreciated!
--
http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Krishna Pacifici wrote:
Hi,
kind of a newbie here, but I have two questions that are probably pretty simple.
1. I need to get rid of duplicate values that are associated with different
keys in a dictionary. For example I have the following code.
s={}
s[0]=[10,2,3]
s[10]=[22,23,24]
s[20]=[45,5]
s[30]=[2,4]
s[40]=[6,7,8]
Now I want to be able to loop through the primary keys and get rid of
duplicates (both in keys and values) so that I would have either a new
dictionary or the same dictionary but with the following values:
s[0]=[3]
s[10]=[22,23,24]
s[20]=[45,5]
s[30]=[2,4]
s[40]=[6,7,8]
It doesn't matter which value gets removed as long as there is only one
remaining, so in this example it doesn't matter that 2 got removed from s[0] or
from s[30] as long as there is only one 2 in the dictionary.
2. I need to be able to loop over the values in the dictionary when there are
multiple values assigned to each key like above and assign new values to those
values. Taking the above example I would want to assign a new value so that
when you called s[0] it would equal [3,4] say if 4 was the new value. I think
this should be as simple as adding a value, but I kept on having difficulty.
Any suggestions would be greatly appreciated.
Thank you very much,
Krishna
Sounds like homework. If it was for an unconstrained project, I'd
design a different data structure, one that directly enforced the data
constraints. So far, I can't imagine a useful reason for this
particular set of constraints.
Let's break the problems down.
1a) Do you know how to write a loop that visits all the keys of a
dictionary?
1b) Do you know how to safely check if a particular key exists?
e.g. if key in s:
1c) Do you know how to collect a set of values, so that when a
duplicate is found, it can be recognized as such?
1d) Do you know how to remove an item from a list?
2a) Like 1a)
2b) Do you know how to append a value to the end of a list? Is s[key] a
list?
DaveA
--
http://mail.python.org/mailman/listinfo/python-list
add fields in a existing dbf
Hi!
I'm trying to modify a dbf adding a new field in a python script, but I
can't.
Just I can add a field in new dbf created in the same script.
I tryed with:
db = dbf.Dbf("../filesource.dbf",new =False, readOnly=False)
...
db.addField(("PESO","N",32,8))
and return error:
Traceback (most recent call last):
File "script.py", line 34, in
db.addField(("PESO","N",32,8))
File "/usr/lib/python2.5/site-packages/dbfpy/dbf.py", line 229, in
addField
"structure can't be changed")
TypeError: ('At least one record was added, ', "structure can't be
changed")
I have not idea of what error I make.
If someone can help me, I'll so gratefull.
Thanks!
--
http://mail.python.org/mailman/listinfo/python-list
Re: UML for Python or some other Graphical Notation Language
llothar wrote: > This is more an academic question right now but was there ever some > work in progress how UML could be made better for Python or script > languages in general. > > It is so extremely deep interwoven with Java/C++ language > implementations that there are a lot of modified notiations necessary. > > Or is there anything else then UML? > Eiffel still has BON for example. http://fptiny.blogspot.com/2008/03/rad-with-tiny-erp.html Diez -- http://mail.python.org/mailman/listinfo/python-list
test message
Sorry for the low content email. Testing the mail-to-news gateway on mail.python.org. Don't flame me for not using a test newsgroup. ;-) -- Skip Montanaro - [email protected] - http://www.smontanaro.net/ Getting old sucks, but it beats dying young -- http://mail.python.org/mailman/listinfo/python-list
Re: test message
On Aug 11, 7:40 am, [email protected] wrote: > Sorry for the low content email. Testing the mail-to-news gateway on > mail.python.org. Don't flame me for not using a test newsgroup. ;-) Posting a followup to test the return path (news-to-mail). S -- http://mail.python.org/mailman/listinfo/python-list
Re: Search and write to .txt file
> Helvin (H) wrote:
>H> Hi everyone,
>H> I am writing some python script that should find a line which contains
>H> '1' in the data.txt file, then be able to move a certain number of
>H> lines down, before replacing a line. At the moment, I am able to find
>H> the line '1', but when I use f.seek to move, and then rewrite, what I
>H> write goes to the end of the .txt file, instead of being adjusted by
>H> my f.seek.
>H> Do you know what way I should take?
>H> Data.txt is a file of 3 lines:
>H>line1
>H>line2
>H>line3
>H> Code:
>H>with open('data.txt', 'r+') as f:
>H>firstread = f.readlines() # Take a snapshot of initial file
>H>f.seek(0,0)# Go back to beginning and search
>H>for line in f:
>H>print line
>H>if line.find('1'):
>H>print 'line matched'
>H>f.seek(1,1) # Move one space along
>H>f.write('house\n') # f.write overwrites the exact
>H> number of bytes.
>H>break# leave loop once '1' is found
Mixing an iterator on the file with direct calls (seek/write) isn't
going to work. The iterator does read ahead which causes the file
position not to be what you think it is.
See:
>>> with open('data.txt', 'r+') as f:
... for line in f:
... print line, f.tell()
...
line1
18
line2
18
line3
18
--
Piet van Oostrum
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list
Extracting matrix from a text file
Hello to all!!
I am new in python, and I am running it on Mac with Smultron editor. I
need to read a textfile that includes numbers (in a matrix form),
indexes, and strings, like this:
Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange
Q/R1 2 3
45
10 4.545924.36685
5.294633.85728
24.545920 3.97667
5.021514.64284
34.366853.976670
4.987434.83683
45.294635.021514.98743
0 6.04393
53.857284.642844.836836.043930
My code to get this information is:
matrix = open("dmatrix_5.txt");
while 1:
mat = matrix.readline()
if not mat:
break
pass # do something
print mat
So I just want to keep the matrix in the "middle" for math
computations.
0 4.545924.36685
5.294633.85728
4.545920 3.97667
5.021514.64284
4.366853.976670
4.987434.83683
5.294635.021514.98743
0 6.04393
3.857284.642844.836836.043930
I've seen and tried a lot of ways, like split or isinstance.. but
never get the wanted result does anyone have an idea, or hint?
Thank you once more for your help!
Best Regards,
Bea
--
http://mail.python.org/mailman/listinfo/python-list
Re: Search and write to .txt file
Helvin wrote:
Hi everyone,
I am writing some python script that should find a line which contains
'1' in the data.txt file, then be able to move a certain number of
lines down, before replacing a line. At the moment, I am able to find
the line '1', but when I use f.seek to move, and then rewrite, what I
write goes to the end of the .txt file, instead of being adjusted by
my f.seek.
Do you know what way I should take?
Data.txt is a file of 3 lines:
line1
line2
line3
Code:
with open('data.txt', 'r+') as f:
firstread = f.readlines() # Take a snapshot of initial file
f.seek(0,0)# Go back to beginning and search
for line in f:
print line
if line.find('1'):
print 'line matched'
f.seek(1,1) # Move one space along
f.write('house\n') # f.write overwrites the exact
number of bytes.
break# leave loop once '1' is found
f.seek(0,0) # Go back to beginning, and read
data.txt again
lastread = f.readlines()
print 'firstread is', firstread
print 'lastread is', lastread
This shouldn't be too difficult, but I don't know how. > <
Help appreciated!
You can't mix and match the file iterator "for line in f" with the seek
method. You already have the data in firstread, why not process it from
there, i.e. "for line in firstread". Also look at the docs for the find
method, it doesn't return what you think it does.
--
Kindest regards.
Mark Lawrence.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Unrecognized escape sequences in string literals
> Steven D'Aprano (SD) wrote: >SD> If I'm reading this page correctly, Python does behave as C++ does. Or at >SD> least as Larch/C++ does: >SD> http://www.cs.ucf.edu/~leavens/larchc++manual/lcpp_47.html They call them `non-standard escape sequences' for a reason: that they are not in standard C++. test.cpp: char* temp = "abc\yz"; TEMP> g++ -c test.cpp test.cpp:1:1: warning: unknown escape sequence '\y' -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting matrix from a text file
[email protected] wrote: Hello to all!! I am new in python, and I am running it on Mac with Smultron editor. I need to read a textfile that includes numbers (in a matrix form), indexes, and strings, like this: Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange Q/R1 2 3 45 10 4.545924.36685 5.294633.85728 24.545920 3.97667 5.021514.64284 34.366853.976670 4.987434.83683 45.294635.021514.98743 0 6.04393 53.857284.642844.836836.043930 My code to get this information is: matrix = open("dmatrix_5.txt"); while 1: mat = matrix.readline() if not mat: break pass # do something print mat So I just want to keep the matrix in the "middle" for math computations. 0 4.545924.36685 5.294633.85728 4.545920 3.97667 5.021514.64284 4.366853.976670 4.987434.83683 5.294635.021514.98743 0 6.04393 3.857284.642844.836836.043930 I've seen and tried a lot of ways, like split or isinstance.. but never get the wanted result does anyone have an idea, or hint? Thank you once more for your help! Best Regards, Bea I think your best bet is to read and action the responses you got to your original email from three days ago. If these have got lost in the post simply search online, they're bound to be archived somewhere. -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Search and write to .txt file
Piet van Oostrum wrote:
Helvin (H) wrote:
H> Hi everyone,
H> I am writing some python script that should find a line which contains
H> '1' in the data.txt file, then be able to move a certain number of
H> lines down, before replacing a line. At the moment, I am able to find
H> the line '1', but when I use f.seek to move, and then rewrite, what I
H> write goes to the end of the .txt file, instead of being adjusted by
H> my f.seek.
H> Do you know what way I should take?
H> Data.txt is a file of 3 lines:
H>line1
H>line2
H>line3
H> Code:
H>with open('data.txt', 'r+') as f:
H>firstread = f.readlines() # Take a snapshot of initial file
H>f.seek(0,0)# Go back to beginning and search
H>for line in f:
H>print line
H>if line.find('1'):
H>print 'line matched'
H>f.seek(1,1) # Move one space along
H>f.write('house\n') # f.write overwrites the exact
H> number of bytes.
H>break# leave loop once '1' is found
Mixing an iterator on the file with direct calls (seek/write) isn't
going to work. The iterator does read ahead which causes the file
position not to be what you think it is.
See:
with open('data.txt', 'r+') as f:
... for line in f:
... print line, f.tell()
...
line1
18
line2
18
line3
18
In addition to the buffering involved in the read loop, trying to
position ahead some number of lines would be rather error prone, since
this is a text file, with varying length lines, and the \n character
might occupy one byte on some OS, and two bytes on others (Windows). If
you feel you must do it in-place, then switch the file mode to binary,
and use read(), not readline(), keeping track of your own position at
all times.
As was already suggested by Kushal, if the file is small enough to just
use readlines() and manipulate that list, I'd do that. If not, I'd scan
through the file, creating a new one, creating a new one as you go, then
rename the new one back when finished. Actually, I'd create a new one
even in the first case, in case of a crash while rewriting the file.
DaveA
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
David Lyon wrote: On Mon, 10 Aug 2009 09:13:34 -0700, Ethan Furman wrote: As someone who relies heavily on the docs I will also say that the idea of giving the ability to modify the official documentation to somebody who is /learning/ the language is, quite frankly, terrifying. What is more terrifying is the way feedback from newbies is handled. Your statement implies that the only way feedback can be handled is to throw the keys down in discust and walk away. That's primative behaviour. And misleading, because that isn't going to happen. Allow me to put back the sentence you unfairly snipped: I have no issues with a seperate system, some of which have been suggested, but good reference documentation is crucial. If you find examples lacking, there are plenty of web-sites, or even (dare I say it?) actual hard-copy books! ;) To be clear, what I am advocating is that *official documentation not be opened up to everybody,* _especially not people who don't yet grok the language_. My bookshelf currently has Learning Python, Programming Python, Python Cookbook, Python Programming on Win32, and Regular Expressions. All great books, and not too pricey if you can get them used. So, what you're advocating is let things stay how they are... Ignore feedback... tell people to freak off... I had not addressed feedback before, but I shall do so now: Discuss concern on the Python list first to make sure it is not a lack of understanding, then, if a legitimate issue with the docs exists, use the bug tracker. If one can't be bothered to take the time to be a Responsible Citizen, I am not going to be bothered by lacking that one's comments/concerns/feed-back. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Social problems of Python doc [was Re: Python docs disappointing]
On 08/11/2009 01:47 AM, Antoine Pitrou wrote: > r gmail.com> writes: >> On Aug 9, 11:02 pm, David Lyon wrote: >>> Since you're talking about documentation, which is a part of python, >>> don't you think you should be discussing it on python-dev ? >> Yea, them's be a friendly bunch to noob ideas ;). Hey i got a better >> idea, lets go to the IRS and see if we can persuade them to stop >> taxing us... > > You know, the most interesting thing in this thread is certainly its title : > « Social problems of Python doc » > > Yes, the little social problem here should be clear: if you have complaints to > voice or improvements to suggest to the Python docs, you should do so on the > issue tracker (*). For most topics, this is the only reasonable way to signal > problems to the Python developers community, and so it is in most free > software > / open source projects. "the *only* reasonable way"? That's clearly wrong (unless you want to wiggle in the room provided by "reasonable" or "most"). There is discussion on the dev list, there is discussion here. The issue tracker is fine for many things, but the process it provides is equivalent to peep-hole optimization. How does one submit a tracker issue for something like the overall organization of the docs (for example, the mis-placement of things like data- types in the library manual rather than the language manual)? The big problem with the docs is poor writing (this includes not using examples when they can help explain something more clearly than verbiage alone). It is understandable why this should be so -- most programmers are good at and enjoy programming, not writing and the python community is, say, about 99.9% programmers. This is why all the attempts (at least that I've noticed) are programming focused -- build a wiki, write a new doc processing framework, etc. Unfortunately none of those addresses the real problem -- the need for clear, well written, well organized *content*. I don't see how telling people to submit tracker issues will solve this problem. I can rewrite some section so it sounds good to me, but likely it will be just as bad (perhaps in different ways) that what is there. The post that started this thread proposed something like paying professional writers to improve the docs. This may or may not be a practical suggestion. But the immediate response it engendered was an auto-immune-like group response: the docs are great already, don't complain, fix them yourself, yada, yada; the same response that any criticism of free software produces. It's really too bad the the python community can't seriously discuss ways to improves the docs. There are other possibilities for instance the Fedora Docs project (can't say I'm impressed by what they've produced but that doesn't mean their model is useless). There is a need for an approval process managed by someone who actually understands what good technical writing is. And perhaps editors who can polish or work with programmers who provide the raw technical description of some module. Some kind of higher level more global process is the only way I can see that the docs will be improved to any sort or professional standard. I don't see how issues like this will be addressed by submitting tracker items. Personally, I have given up on this issue. The social factors involved really pretty much determine that the Python docs will never reach a very high quality -- that's just the way it is and I've come to accept that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unrecognized escape sequences in string literals
Steven D'Aprano wrote: On Mon, 10 Aug 2009 08:21:03 -0700, Douglas Alan wrote: But you're right, it's too late to change this now. Not really. There is a procedure for making non-backwards compatible changes. If you care deeply enough about this, you could agitate for Python 3.2 to raise a PendingDepreciation warning for "unexpected" escape sequences like \z, Python 3.3 to raise a Depreciation warning, and Python 3.4 to treat it as an error. It may even be possible to skip the PendingDepreciation warning and go straight for Depreciation warning in 3.2. And once it's fully depreciated you have to stop writing it off on your taxes. *wink* ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting matrix from a text file
[email protected] wrote: Hello to all!! I am new in python, and I am running it on Mac with Smultron editor. I need to read a textfile that includes numbers (in a matrix form), indexes, and strings, like this: Marsyas-kea distance matrix for MIREX 2007 Audio Similarity Exchange Q/R1 2 3 45 10 4.545924.36685 5.294633.85728 24.545920 3.97667 5.021514.64284 34.366853.976670 4.987434.83683 45.294635.021514.98743 0 6.04393 53.857284.642844.836836.043930 My code to get this information is: matrix = open("dmatrix_5.txt"); while 1: mat = matrix.readline() if not mat: break pass # do something print mat So I just want to keep the matrix in the "middle" for math computations. 0 4.545924.36685 5.294633.85728 4.545920 3.97667 5.021514.64284 4.366853.976670 4.987434.83683 5.294635.021514.98743 0 6.04393 3.857284.642844.836836.043930 I've seen and tried a lot of ways, like split or isinstance.. but never get the wanted result does anyone have an idea, or hint? Thank you once more for your help! Best Regards, Bea numpy provides an answer. See the little script below: import numpy as _n import string as _s a= "0 4.54592 4.36685 5.29463 3.85728 " + \ "4.54592 0 3.97667 5.02151 4.64284 " + \ '4.36685 3.97667 0 4.98743 4.83683 ' + \ '5.29463 5.02151 4.98743 0 6.04393 ' + \ '3.85728 4.64284 4.83683 6.04393 0' d= _n.mat(_n.reshape(_n.array(a.split(), dtype= _n.float), (5, 5))) print repr(d) print 'Default printoptions:', _n.get_printoptions() for p in range(9): _n.set_printoptions(precision= p) print 'precision= ', p, '\n', d When you run this, you'll see that there is a numpy glitch for precisions > 5. Colin W. -- http://mail.python.org/mailman/listinfo/python-list
Profiling a Callback Function
Hello,
I am trying to profile a Python program that primarily calls a C
extension. From within the C extension, a callback Python function is
then called concurrently in several threads.
When I tried to profile this application with
import c_extension
def callback_fn(args):
# Do all sorts of complicated, time consuming stuff
pass
def main():
c_extension.call_me_back(callback_fn, some_random_args)
cProfile.run('main', 'profile.dat')
I only got results for main(), but no information at all about
callback_fn.
What is the proper way to profile such an application?
I already thought about this:
import c_extension
def callback_fn(args):
# Do all sorts of complicated, time consuming stuff
pass
def callback_wrapper(args):
def doit():
callback_fn(args)
cProfile.run('doit', 'profile.dat')
c_extension.call_me_back(callback_wrapper, some_random_args)
but that probably overwrites the profiling information whenever
callback_wrapper is called, instead of accumulating them over several
calls (with different arguments).
Best,
-Nikolaus
--
»Time flies like an arrow, fruit flies like a Banana.«
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
--
http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Thanks for the help.
Actually this is part of a much larger project, but I have unfortunately
pigeon-holed myself into needing to do these things without a whole lot of
flexibility.
To give a specific example I have the following dictionary where I need to
remove values that are duplicated with other values and remove values that are
duplicates of the keys, but still retain it as a dictionary. Each value is
itself a class with many attributes that I need to call later on in the
program, but I cannot have duplicates because it would mess up some estimation
part of my model.
d =
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22: [21, 23,
12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
So I want a new dictionary that would get rid of the duplicate values of 21,
22, 36 and 20 and give me back a dictionary that looked like this:
new_d=
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
32], 26: [25, 27, 16], 30: [40]}
I understand that a dictionary may not be the best approach, but like I said I
have sort of pigeon-holed myself by the way that I am simulating my data and
the estimation model that I am using. Any suggestions or comments about the
above problem would be greatly appreciated.
Thanks again,
Krishna
>>> Dave Angel 08/11/09 7:38 AM >>>
Krishna Pacifici wrote:
> Hi,
> kind of a newbie here, but I have two questions that are probably pretty
> simple.
>
> 1. I need to get rid of duplicate values that are associated with different
> keys in a dictionary. For example I have the following code.
> s={}
> s[0]=[10,2,3]
> s[10]=[22,23,24]
> s[20]=[45,5]
> s[30]=[2,4]
> s[40]=[6,7,8]
>
> Now I want to be able to loop through the primary keys and get rid of
> duplicates (both in keys and values) so that I would have either a new
> dictionary or the same dictionary but with the following values:
>
> s[0]=[3]
> s[10]=[22,23,24]
> s[20]=[45,5]
> s[30]=[2,4]
> s[40]=[6,7,8]
>
> It doesn't matter which value gets removed as long as there is only one
> remaining, so in this example it doesn't matter that 2 got removed from s[0]
> or from s[30] as long as there is only one 2 in the dictionary.
>
> 2. I need to be able to loop over the values in the dictionary when there
> are multiple values assigned to each key like above and assign new values to
> those values. Taking the above example I would want to assign a new value so
> that when you called s[0] it would equal [3,4] say if 4 was the new value. I
> think this should be as simple as adding a value, but I kept on having
> difficulty.
>
> Any suggestions would be greatly appreciated.
>
> Thank you very much,
> Krishna
>
>
>
Sounds like homework. If it was for an unconstrained project, I'd
design a different data structure, one that directly enforced the data
constraints. So far, I can't imagine a useful reason for this
particular set of constraints.
Let's break the problems down.
1a) Do you know how to write a loop that visits all the keys of a
dictionary?
1b) Do you know how to safely check if a particular key exists?
e.g. if key in s:
1c) Do you know how to collect a set of values, so that when a
duplicate is found, it can be recognized as such?
1d) Do you know how to remove an item from a list?
2a) Like 1a)
2b) Do you know how to append a value to the end of a list? Is s[key] a
list?
DaveA
--
http://mail.python.org/mailman/listinfo/python-list
Re: Search and write to .txt file
On Aug 11, 7:22 am, Helvin wrote:
> Hi everyone,
>
> I am writing some python script that should find a line which contains
> '1' in the data.txt file, then be able to move a certain number of
> lines down, before replacing a line. At the moment, I am able to find
> the line '1', but when I use f.seek to move, and then rewrite, what I
> write goes to the end of the .txt file, instead of being adjusted by
> my f.seek.
>
> Do you know what way I should take?
>
> Data.txt is a file of 3 lines:
> line1
> line2
> line3
>
> Code:
>
> with open('data.txt', 'r+') as f:
> firstread = f.readlines() # Take a snapshot of initial file
>
> f.seek(0,0) # Go back to beginning and search
> for line in f:
> print line
> if line.find('1'):
> print 'line matched'
> f.seek(1,1) # Move one space along
> f.write('house\n') # f.write overwrites the exact
> number of bytes.
> break # leave loop once '1' is found
>
> f.seek(0,0) # Go back to beginning, and read
> data.txt again
> lastread = f.readlines()
>
> print 'firstread is', firstread
> print 'lastread is', lastread
>
> This shouldn't be too difficult, but I don't know how. > <
> Help appreciated!
There's a bug in this line:
if line.find('1'):
the string find() method returns an integer, which will be -1 if the
substring is not found. In python -1 is treated as True (only 0 is
False) so your if statement will always succeed unless '1' is the
first character on the line.
HTH,
~Simon
P.S. you can use the help() command in the python interpreter to get
docs on most things:
>>> help(str.find)
Help on method_descriptor:
find(...)
S.find(sub [,start [,end]]) -> int
Return the lowest index in S where substring sub is found,
such that sub is contained within s[start:end]. Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.
--
http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Krishna Pacifici wrote:
Thanks for the help.
Actually this is part of a much larger project, but I have unfortunately
pigeon-holed myself into needing to do these things without a whole lot
of flexibility.
To give a specific example I have the following dictionary where I need
to remove values that are duplicated with other values and remove values
that are duplicates of the keys, but still retain it as a dictionary.
Each value is itself a class with many attributes that I need to call
later on in the program, but I cannot have duplicates because it would
mess up some estimation part of my model.
d =
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
[21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
So I want a new dictionary that would get rid of the duplicate values of
21, 22, 36 and 20 and give me back a dictionary that looked like this:
new_d=
{36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
12, 32], 26: [25, 27, 16], 30: [40]}
I understand that a dictionary may not be the best approach, but like I
said I have sort of pigeon-holed myself by the way that I am simulating
my data and the estimation model that I am using. Any suggestions or
comments about the above problem would be greatly appreciated.
>>> d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
>>> new_d = {}
>>> seen = set(d.keys())
>>> for k, v in d.items():
... new_d[k] = [x for x in v if x not in seen]
... seen |= set(new_d[k])
...
>>> new_d
{36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
32], 26: [25, 27, 16], 30: [40]}
--
http://mail.python.org/mailman/listinfo/python-list
Re: add fields in a existing dbf
Alonso Luján Torres Taño wrote:
Hi!
I'm trying to modify a dbf adding a new field in a python script, but I
can't.
Just I can add a field in new dbf created in the same script.
I tryed with:
db = dbf.Dbf("../filesource.dbf",new =False, readOnly=False)
...
db.addField(("PESO","N",32,8))
and return error:
Traceback (most recent call last):
File "script.py", line 34, in
db.addField(("PESO","N",32,8))
File "/usr/lib/python2.5/site-packages/dbfpy/dbf.py", line 229, in
addField
"structure can't be changed")
TypeError: ('At least one record was added, ', "structure can't be
changed")
I have not idea of what error I make.
If someone can help me, I'll so gratefull.
Thanks!
I am not familiar with dbfpy, but the dbf files I am familiar with do
not support 32 digit Numerics. Did you mean 12?
At any rate, if you use http://groups.google.com/group/python-dbase
the commands would be:
import dbf
db = dbf.Table("../filesource.dbf")
db.add_fields("peso N(12.8)")
db.close()
and you'll have your field -- assuming, of course, you have write
priveleges, etc.
Hope this helps!
~Ethan~
Disclaimer: I am the author of python-dbase.
--
http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
On Aug 11, 11:51 am, MRAB wrote:
> Krishna Pacifici wrote:
> > Thanks for the help.
>
> > Actually this is part of a much larger project, but I have unfortunately
> > pigeon-holed myself into needing to do these things without a whole lot
> > of flexibility.
>
> > To give a specific example I have the following dictionary where I need
> > to remove values that are duplicated with other values and remove values
> > that are duplicates of the keys, but still retain it as a dictionary.
> > Each value is itself a class with many attributes that I need to call
> > later on in the program, but I cannot have duplicates because it would
> > mess up some estimation part of my model.
>
> > d =
> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
> > [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
>
> > So I want a new dictionary that would get rid of the duplicate values of
> > 21, 22, 36 and 20 and give me back a dictionary that looked like this:
>
> > new_d=
> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
> > 12, 32], 26: [25, 27, 16], 30: [40]}
>
> > I understand that a dictionary may not be the best approach, but like I
> > said I have sort of pigeon-holed myself by the way that I am simulating
> > my data and the estimation model that I am using. Any suggestions or
> > comments about the above problem would be greatly appreciated.
>
> >>> d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
> 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
> >>> new_d = {}
> >>> seen = set(d.keys())
> >>> for k, v in d.items():
> ... new_d[k] = [x for x in v if x not in seen]
> ... seen |= set(new_d[k])
> ...
> >>> new_d
> {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
> 32], 26: [25, 27, 16], 30: [40]}
Ha ha, MRAB beat me to it:
d = {
36: [35, 37, 26, 46],
75: [74, 76, 65, 85],
21: [20, 22, 11, 31],
22: [21, 23, 12, 32],
26: [25, 27, 16, 36],
30: [20, 31, 40],
}
new_d = { # Given, and apparently incorrect.
36: [35, 37, 26, 46], # 26 is a key and should be gone.
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}
expected = {
36: [35, 37, 46],
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}
def removeDuplicates(D):
'''
Remove values that are duplicated with other values
and remove values that are duplicates of the keys.
Assumes that values in the lists are already unique within
each list. I.e. duplicates are only in the keys or in other
lists.
This function works "in place" on D, so it doesn't return
anything. Caller must keep a reference to D.
'''
seen = set(D) # Get a set of the keys.
for key, values_list in D.iteritems():
# Filter out values that have already been seen.
filtered_values = [
value
for value in values_list
if not value in seen
]
# Remember newly seen values.
seen.update(filtered_values)
D[key] = filtered_values
## Example:
##
##>>> d == expected
##False
##>>> removeDuplicates(d)
##>>> d == expected
##True
--
http://mail.python.org/mailman/listinfo/python-list
better way?
Hello,
I'd like to make insert into db if record not exist otherwise update.
to save typing list of columns in both statements I do following
query = "SELECT location FROM table WHERE location = %s AND id = %s;"
result = self._getResult(db, query, [location,id])
fields = ['id', 'location', 'wl', 'modified', 'counter', 'name']
if result:
t = map(lambda s: s + ' = %s', fields)
temp = ", ".join(t)
query = "UPDATE table SET " + temp + " WHERE location = %s AND id
= %s;"
self._execQuery(db, query, [id, location, self.wl, 'NOW()', 1,
name, location, id])
else:
f = ", ".join(fields)
query = """
INSERT INTO table
(""" + f + """)
VALUES
(%s,%s,%s,NOW()- interval '1 day', 1, %s)
"""
self._execQuery(db, query, [id, location, self.wl, name])
is there better or more readable way to do it?
Pet
--
http://mail.python.org/mailman/listinfo/python-list
GOZERBOT 0.9.1 released
here it is .. GOZERBOT 0.9.1 !! Main change this time is the distribution method, we now provide a tar.gz with all the dependencies included. This means that you can run the bot locally without any root required. Python 2.5 or higher needed, see http://gozerbot.org Enjoy ! about GOZERBOT: GOZERBOT is a channel bot that aids with conversation in irc channels and jabber conference rooms. its mainly used to serve rss feeds and to have custom commands made for the channel. More then just a channel bot GOZERBOT aims to provide a platform for the user to program his own bot and make it into something thats usefull. This is done with a plugin structure that makes it easy to program your own. But GOZERBOT comes with some batteries included, there are now over 100 plugins already written and ready for use. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any package implanation the following arithmetics?
On 8/11/2009 1:49 AM zhongshq said... Hi, I wonder if there has any package can check whether two rectangles are overlap, is a dot inside or outside a polygon, etc. PythonCad at http://sourceforge.net/projects/pythoncad/ has intersections built in. Emile -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On Aug 11, 1:47 am, Steven D'Aprano wrote: > On Mon, 10 Aug 2009 20:05:00 -0400, David Lyon wrote: > > Ignore feedback... tell people to freak off... > > Only useless feedback. And who decides what is useless and what isn't Steven?. You?, alex23?, Bruno?, Paul? Carl? Who makes these decisions and do *they* make them without pride or prejudice? Do they approve an idea by someone they hate because it it good, or do they toss it in the trash just to spite them, because they have the power to do so? As we can see much resistace exists against even the ideas of change. How will change ever take place with such defiance! I am sorry but i feel many here would not judge fairly based on the merits of an idea without allowing "buddy-systems" or "pecking-orders" to get in the way. Sad really, only Python suffers in the end. Some say the tutorial is not meant for non-programmers, but for programmers with no Python experience. So! How does that justify obstruction of the tut? Why not present the same information in a way both can easily understand? I thought Pythons original vision was to allow easy entry into programming for anybody -- experienced or not! Anybody remember "CP4E"? Is this an ivory tower thing? i dunno, but it seems to be...??? -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Wow, thanks MRAB and Simon, you guys are good.
I guess I will go ahead and ask the next question that has also stumped me for
awhile now.
So basically I need to loop through the values in the new dictionary and append
attributes of a class object. Each of the values (and keys) represent a block
in a grid with a specific location (e.g. 35 is the block in row 3 col 5) and
each block is an object with several attributes associated with it. I want to
call that block and append two separate attributes to the dictionary within
that same key.
So again it would look something like this:
block 35 has 2 attributes, say a and b, a=2, b=5
block 37 has a=1, b=3
block 46 has a=3, b=8
the two attributes come from two different definitions within the class
statement,
def detections
...
return a
def abundance
...
return b
so I would want to append to key 36 those two attributes for each block so that
the resulting dictionary item would look like this:
a b
{36:[35,37,46], [2,1,3], [5,3,8] ...}
Any help with this would be greatly appreciated. And thank you so much for all
of your help thus far, I'm still pretty new to python and am enjoying all of
the flexibility associated with a scripting and programming language.
Thanks again,
Krishna
>>> Simon Forman 08/11/09 12:15 PM >>>
On Aug 11, 11:51 am, MRAB wrote:
> Krishna Pacifici wrote:
> > Thanks for the help.
>
> > Actually this is part of a much larger project, but I have unfortunately
> > pigeon-holed myself into needing to do these things without a whole lot
> > of flexibility.
>
> > To give a specific example I have the following dictionary where I need
> > to remove values that are duplicated with other values and remove values
> > that are duplicates of the keys, but still retain it as a dictionary.
> > Each value is itself a class with many attributes that I need to call
> > later on in the program, but I cannot have duplicates because it would
> > mess up some estimation part of my model.
>
> > d =
> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11, 31], 22:
> > [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
>
> > So I want a new dictionary that would get rid of the duplicate values of
> > 21, 22, 36 and 20 and give me back a dictionary that looked like this:
>
> > new_d=
> > {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23,
> > 12, 32], 26: [25, 27, 16], 30: [40]}
>
> > I understand that a dictionary may not be the best approach, but like I
> > said I have sort of pigeon-holed myself by the way that I am simulating
> > my data and the estimation model that I am using. Any suggestions or
> > comments about the above problem would be greatly appreciated.
>
> >>> d = {36: [35, 37, 26, 46], 75: [74, 76, 65, 85], 21: [20, 22, 11,
> 31], 22: [21, 23, 12, 32], 26: [25, 27, 16, 36], 30: [20, 31, 40]}
> >>> new_d = {}
> >>> seen = set(d.keys())
> >>> for k, v in d.items():
> ... new_d[k] = [x for x in v if x not in seen]
> ... seen |= set(new_d[k])
> ...
> >>> new_d
> {36: [35, 37, 46], 75: [74, 76, 65, 85], 21: [20, 11, 31], 22: [23, 12,
> 32], 26: [25, 27, 16], 30: [40]}
Ha ha, MRAB beat me to it:
d = {
36: [35, 37, 26, 46],
75: [74, 76, 65, 85],
21: [20, 22, 11, 31],
22: [21, 23, 12, 32],
26: [25, 27, 16, 36],
30: [20, 31, 40],
}
new_d = { # Given, and apparently incorrect.
36: [35, 37, 26, 46], # 26 is a key and should be gone.
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}
expected = {
36: [35, 37, 46],
75: [74, 76, 65, 85],
21: [20, 11, 31],
22: [23, 12, 32],
26: [25, 27, 16],
30: [40],
}
def removeDuplicates(D):
'''
Remove values that are duplicated with other values
and remove values that are duplicates of the keys.
Assumes that values in the lists are already unique within
each list. I.e. duplicates are only in the keys or in other
lists.
This function works "in place" on D, so it doesn't return
anything. Caller must keep a reference to D.
'''
seen = set(D) # Get a set of the keys.
for key, values_list in D.iteritems():
# Filter out values that have already been seen.
filtered_values = [
value
for value in values_list
if not value in seen
]
# Remember newly seen values.
seen.update(filtered_values)
D[key] = filtered_values
## Example:
##
##>>> d == expected
##False
##>>> removeDuplicates(d)
##>>> d == expected
##True
--
http://mail.python.org/mailman/listinfo/python-list
--
http://mail.python.org/mailman/listinfo/python-list
Any built-in function for smallest positive floating point number?
Hello everybody, Is somebody aware of built-in Python's function that would return a value for smallest positive double precision floating point number (analogous to 'realmin' in Matlab). Python has built-in sys.maxint but I could not find anything for float. Any help would be greatly appreciated. Thanks, Masha [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Scraping Wikipedia with Python
I plan on making a geography-learning Anki [1] deck, and Wikipedia has the information that I need in nicely formatted tables on the side of each country's page. Has someone already invented a wheel to parse and store that data (scrape)? It is probably not difficult to code, and within the Wikipedia license, but if that wheel has already been invented then I don't want to redo it. I tried googling for a Wikipedia-specific solution but found none, is there a general purpose solution that I could use? Note that I am a regular Wikipedia contributor and plan on staying within the realm of Wikipedia's rules. [1] http://ichi2.net/anki/ -- Dotan Cohen http://what-is-what.com http://gibberish.co.il -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
r wrote: On Aug 11, 1:47 am, Steven D'Aprano wrote: On Mon, 10 Aug 2009 20:05:00 -0400, David Lyon wrote: Ignore feedback... tell people to freak off... Only useless feedback. [snip] I am sorry but i feel many here would not judge fairly based on the merits of an idea without allowing "buddy-systems" or "pecking-orders" to get in the way. Sad really, only Python suffers in the end. I disagree with these comments. I do not believe that Python has a buddy system or a pecking order getting in the way of anything. As you are making thsse accusations, either provide hard evidence that can persuade me that your perspective on this is correct or shut up. [snip] -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is "feedparser" deprecated?
Gabriel Genellina wrote: En Fri, 07 Aug 2009 16:07:48 -0300, John Nagle escribió: Feedparser requires SGMLlib, which has been removed from Python 3.0. Feedparser hasn't been updated since 2007. Does this mean Feedparser is dead? Since we have generic and easy of use XML parsers like ElementTree and lxml, specialized rss parsers like feedparser are not so much required. I've used ElementTree when I had to parse an rss feed in the past (not so many times). Take a look at what Feedparser has to do to deal with real-world RSS feeds. There are several hundred lines of code just to figure out the source encoding. There's gzip support, Atom support, base 64 encoding support... Then there's the RSS polling protocol, which also requires some support. A basic XML parser only does part of the job. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Any built-in function for smallest positive floating point number?
A bit more of googling gave me an answer: >>> import numpy as np >>> np.finfo(np.double).tiny array(2.2250738585072014e-308) >>> Thanks, Masha [email protected] On Aug 11, 2009, at 10:29 AM, Maria Liukis wrote: Hello everybody, Is somebody aware of built-in Python's function that would return a value for smallest positive double precision floating point number (analogous to 'realmin' in Matlab). Python has built-in sys.maxint but I could not find anything for float. Any help would be greatly appreciated. Thanks, Masha [email protected] -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Social problems of Python doc [was Re: Python docs disappointing]
On Tue, 11 Aug 2009 07:57:28 -0700, rurpy wrote: > On 08/11/2009 01:47 AM, Antoine Pitrou wrote: >> r gmail.com> writes: >>> On Aug 9, 11:02 pm, David Lyon wrote: Since you're talking about documentation, which is a part of python, don't you think you should be discussing it on python-dev ? >>> Yea, them's be a friendly bunch to noob ideas ;). Hey i got a better >>> idea, lets go to the IRS and see if we can persuade them to stop >>> taxing us... >> >> You know, the most interesting thing in this thread is certainly its >> title : « Social problems of Python doc » >> >> Yes, the little social problem here should be clear: if you have >> complaints to voice or improvements to suggest to the Python docs, you >> should do so on the issue tracker (*). For most topics, this is the >> only reasonable way to signal problems to the Python developers >> community, and so it is in most free software / open source projects. > > "the *only* reasonable way"? That's clearly wrong (unless you want to > wiggle in the room provided by "reasonable" or "most"). There is > discussion on the dev list, there is discussion here. Discussion here is spitting into the wind. The noise-to-signal ratio is high enough that the people who can fix an issue aren't likely to see it here. Unless somebody raises a report in the bug tracker, it will go nowhere, fast. The same happens for issues on the dev list: I can't count how many times people there have said "put it in the bug tracker, or it will be forgotten". > The issue tracker is fine for many things, but the process it provides > is equivalent to peep-hole optimization. How does one submit a tracker > issue for something like the overall organization of the docs (for > example, the mis-placement of things like data- types in the library > manual rather than the language manual)? The same way you would submit a tracker issue for anything. "Documentation bug: Data types are misplaced in the library manual instead of the language manual. Suggested alternative: move page docs.python.org/xyz.html to abc.html" > The big problem with the docs is poor writing (this includes not using > examples when they can help explain something more clearly than verbiage > alone). There's a difference between insufficiently good writing and poor writing: think of grading the docs, where 100 is "perfect in every way" and -100 is "perfectly awful in every possible way". (It's not a linear scale.) A score of 0 is "mediocre, just barely acceptable". Poor writing has a negative score. In my opinion, Python's docs are perhaps a 40 or 50, significantly better than most technical docs I've seen. [...] > I can rewrite some section so it sounds good to me, but likely it will > be just as bad (perhaps in different ways) that what is there. Bug reports are valuable even if you don't have the skills to provide a patch. Bug reports with patches are even more valuable, but that doesn't mean that the failure to provide a patch *necessarily* dooms your report to oblivion. > The post that started this thread proposed something like paying > professional writers to improve the docs. This may or may not be a > practical suggestion. But the immediate response it engendered was an > auto-immune-like group response: the docs are great already, don't > complain, fix them yourself, yada, yada; the same response that any > criticism of free software produces. I haven't seen any such knee-jerk responses. What I've seen is a set of sensible, *practical* advice: - complaining on its own, especially here, is pointless; - Python is a community effort, if you see something that needs fixing, do something about it; - many of us DON'T want arbitrary people "correcting" the official docs without oversight, and believe that would be a disaster (arbitrary people aren't give check-in privileges to add code to Python's standard library, nor should they be given check-in privileges to add docs); - if people are keen on a Python wiki, then by all means publish one, just don't expect the Python dev team to build and manage it for you; - bug reports and patches to the docs are ALWAYS welcome, even if they are rejected; - if you don't have the skills to fix a bug (including doc bugs), an alternative is to pay somebody else to do so instead. > It's really too bad the the python community can't seriously discuss > ways to improves the docs. There are other possibilities for instance > the Fedora Docs project (can't say I'm impressed by what they've > produced but that doesn't mean their model is useless). There is a need > for an approval process managed by someone who actually understands what > good technical writing is. And perhaps editors who can polish or work > with programmers who provide the raw technical description of some > module. Yes, you're correct. [sarcasm] Now that we've agreed on what needs to be done, the problem is solved!!! [end sarcasm] > Some kind of
Re: Unrecognized escape sequences in string literals
On Tue, 11 Aug 2009 15:50:01 +0200, Piet van Oostrum wrote: >> Steven D'Aprano (SD) wrote: > >>SD> If I'm reading this page correctly, Python does behave as C++ does. >>Or at SD> least as Larch/C++ does: > >>SD> http://www.cs.ucf.edu/~leavens/larchc++manual/lcpp_47.html > > They call them `non-standard escape sequences' for a reason: that they > are not in standard C++. > > test.cpp: > char* temp = "abc\yz"; > > TEMP> g++ -c test.cpp > test.cpp:1:1: warning: unknown escape sequence '\y' Isn't that a warning, not a fatal error? So what does temp contain? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Networked Broadcast Messaging
I'm not exactly sure what the term for this would be, but I was wondering if there were any Python packages that supported some kind of ad-hoc message broadcasting. What I'd like to do is something like this: * On a number of workhorse machines, a process listens for network messages from our broadcast service. It subscribes to a certain sub- set of them and will only respond to the messaging events that it is subscribed to. * Any machine can broadcast a message out to the network of machines without specifying an IP address. * Machines can come and go. Since messages are not directly sent to a specific IP address from our Python script, the messages are simply broadcasted to those who are there to listen. If nobody is subscribed to the message type being sent, nothing happens. I know XML-RPC and other friends are an option, but I'm looking for something that doesn't require managing a set of IP addresses or hostnames. I'm not sure what to Google for such a package/module, any direction would be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: looping through values in a dictionary and appending to a list
Nevermind, got it. Sorry. >>> Krishna Pacifici 08/11/09 2:12 PM >>> Hi, I want to append the values of a dictionary to a list. I have a dictionary sec_dict_clean and I want to append the values to a list, but am having a hard time looping through the values in the dictionary. I have tried something like this: lista=[] for i in sec_dict_clean.values(): for j in sec_dict_clean.values()[i]: lista.append(sec_dict_clean.values()[i]) But I keep on getting an error: TypeError: list indices must be integers Any ideas on how to loop through values in a dictionary? Thanks, Krishna -- http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
Dotan Cohen wrote: I plan on making a geography-learning Anki [1] deck, and Wikipedia has the information that I need in nicely formatted tables on the side of each country's page. Has someone already invented a wheel to parse and store that data (scrape)? Wikipedia has an API for computer access. See http://www.mediawiki.org/wiki/API John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
> Wikipedia has an API for computer access. See > > http://www.mediawiki.org/wiki/API > Yes, I am aware of this as well. Does anyone know of a python class for easily interacting with it, or do I need to roll my own. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il -- http://mail.python.org/mailman/listinfo/python-list
Re: Networked Broadcast Messaging
On Aug 11, 2:14 pm, "[email protected]" wrote: > I'm not exactly sure what the term for this would be, but I was > wondering if there were any Python packages that supported some kind > of ad-hoc message broadcasting. What I'd like to do is something like > this: > > * On a number of workhorse machines, a process listens for network > messages from our broadcast service. It subscribes to a certain sub- > set of them and will only respond to the messaging events that it is > subscribed to. > * Any machine can broadcast a message out to the network of machines > without specifying an IP address. > * Machines can come and go. Since messages are not directly sent to a > specific IP address from our Python script, the messages are simply > broadcasted to those who are there to listen. If nobody is subscribed > to the message type being sent, nothing happens. > > I know XML-RPC and other friends are an option, but I'm looking for > something that doesn't require managing a set of IP addresses or > hostnames. I'm not sure what to Google for such a package/module, any > direction would be greatly appreciated. Offhand I'd suggest binding a specific UDP port on the listening workstations. Then the broadcasting workstation(s) could just pull a standard list of IP's based on its own subnet. Then there'd be no hard- coded machine names or IP addresses to manage. Just send the message to all hosts on the same subnet as the broadcaster(s). Plus the UDP connection would be stateless and not care if the receivers actually got the message or not... -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
On Tue, Aug 11, 2009 at 10:03 PM, someone wrote: > Hello, > > I'd like to make insert into db if record not exist otherwise update. > to save typing list of columns in both statements I do following > > > > > is there better or more readable way to do it? > Well, mysql, in particular, offers an "on duplicate key update" clause that you can take a look at. Don't know about about other databases. -- kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: Social problems of Python doc [was Re: Python docs disappointing]
Steven D'Aprano writes: > - if people are keen on a Python wiki, then by all means publish one, > just don't expect the Python dev team to build and manage it for you; There are already some nice ones at: http://en.wikibooks.org/wiki/Python -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
[Paul Rubin] > I think the Python tutorial is aimed at users who are newbies to > Python but not newbies to programming. Writing a tutorial for total > newbies is a completely different problem, that would result in a much > different document that's less useful to the existing tutorial's > intended audience. There is more than one right answer to "what is the best tutorial for different people". The www.python.org website lists a number of tutorials. Here is the page specifically marked for those who are new to programming: http://wiki.python.org/moin/BeginnersGuide Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
On 11 Aug., 20:39, Kushal Kumaran wrote: > On Tue, Aug 11, 2009 at 10:03 PM, someone wrote: > > Hello, > > > I'd like to make insert into db if record not exist otherwise update. > > to save typing list of columns in both statements I do following > > > > > > is there better or more readable way to do it? > > Well, mysql, in particular, offers an "on duplicate key update" clause > that you can take a look at. Don't know about about other databases. > > -- > kushal Oh, forgotten to mention. It's PostGres -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
r writes: > Some say the tutorial is not meant for non-programmers, but for > programmers with no Python experience. So! How does that justify > obstruction of the tut? Why not present the same information in a way > both can easily understand? I agree that a tutorial for non-programmers would be useful, but it's better to have it as a separate document. The existing tutorial helps experienced programmers get started with Python very quickly, by checking off familiar features from other languages and saying how Python implements them. Someone familiar with (say) Java or C++ can read it in a few minutes and start contributing to a Python project. Pitching it to complete beginners would require covering a lot of ground that is already well known to someone who programs in other languages, and would slow it down too much. -- http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
On Tue, 11 Aug 2009 21:29:40 +0300, Dotan Cohen wrote: >> Wikipedia has an API for computer access. See >> >> http://www.mediawiki.org/wiki/API >> >> > Yes, I am aware of this as well. Does anyone know of a python class for > easily interacting with it, or do I need to roll my own. Try reading a little there! Starting there I went to http://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot where I found a section on existing bots, comments on how the "scraping" is not what you want, and even a Python section with a link to something labelled PyWikipediaBot... -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
Raymond Hettinger writes: > Here is the page specifically marked for those who are new to programming: > http://wiki.python.org/moin/BeginnersGuide Oh cool, I didn't know about that one. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On Sat, Aug 8, 2009 at 12:25 AM, Steven D'Aprano wrote: > On Fri, 07 Aug 2009 13:35:26 -0700, Kee Nethery wrote: > > >> > Why exactly is posting an open comment on a bug tracker somehow >> > inferior to posting an open comment on a wiki? >> >> It's a good question and deserves a good answer. >> >> * Fewer Steps >> * Immediate >> * Does not need to be formally reviewed >> * Easier to search > > The last one is actually wrong, because the Python bug tracker is indexed > by Google. > > As for the rest, you're right that the current bug-tracker puts up > barriers to people submitting comments and bugs. That's actually a good > thing. The only thing worse than not enough information is too much > information, and the current situation does a good job of discouraging > the sorts of people who submit bad bug reports (e.g. duplicates of bug > reports, bug reports for things fixed years ago, bug reports that are due > to mistakes in their code, etc.). > > >> For example >> purposes, I'll use the following page: >> http://docs.python.org/reference/lexical_analysis.html >> Lets say the user is in section 2.1.3 Comments > > Disclaimer: python.org is down at the moment, so I can't check that page > to see precisely what you're talking about. > > >> Here's the scenario: The user wanted to include "#" in one of their >> strings and the IDE kept interpreting it as a comment. But they really >> need to use that character in the string. Eventually they find out that >> they can escape the character in their string so that Python stops >> thinking it is the beginning of a comment. > > Er, that would be a bug in the IDE, surely? Inside strings, # is an > ordinary character with no special meaning. > > s = "this is a string with an unescaped # in it" s > 'this is a string with an unescaped # in it' > > > The Python docs are supposed to be about Python the language, not work- > arounds for IDE bugs. > > > [...] >> Lets assume that Python experts all agree that >> the docs should get updated with this gotcha (which as a newbie, they >> are not sure that is a valid assumption and would probably just halt >> in their quest to get the docs updated). > > Good. > > As a newbie, you SHOULD assume that anything you think is a bug is > probably a bug in YOUR code, not Python, and the same goes for > documentation bugs. If you don't understand something in the docs, > chances are that it's a problem with you, not the docs. Your first port > of call should be the tutor list, or here, and not to "fix" the docs by > putting in noise that just gets in the way of the intended audience, > namely experienced developers. > I assume I am misunderstanding you here and you meant something else? Python is paraded as a good language for beginners. Is this a false statement or a secondary objective? Or are the docs only meant for experienced developers? That just doesn't sound right. -- http://mail.python.org/mailman/listinfo/python-list
Re: Networked Broadcast Messaging
On Wed, Aug 12, 2009 at 12:04 AM, gregarican wrote: > On Aug 11, 2:14 pm, "[email protected]" > wrote: >> I'm not exactly sure what the term for this would be, but I was >> wondering if there were any Python packages that supported some kind >> of ad-hoc message broadcasting. What I'd like to do is something like >> this: >> >> * On a number of workhorse machines, a process listens for network >> messages from our broadcast service. It subscribes to a certain sub- >> set of them and will only respond to the messaging events that it is >> subscribed to. >> * Any machine can broadcast a message out to the network of machines >> without specifying an IP address. >> * Machines can come and go. Since messages are not directly sent to a >> specific IP address from our Python script, the messages are simply >> broadcasted to those who are there to listen. If nobody is subscribed >> to the message type being sent, nothing happens. >> >> I know XML-RPC and other friends are an option, but I'm looking for >> something that doesn't require managing a set of IP addresses or >> hostnames. I'm not sure what to Google for such a package/module, any >> direction would be greatly appreciated. > > Offhand I'd suggest binding a specific UDP port on the listening > workstations. Then the broadcasting workstation(s) could just pull a > standard list of IP's based on its own subnet. Then there'd be no hard- > coded machine names or IP addresses to manage. Just send the message > to all hosts on the same subnet as the broadcaster(s). Plus the UDP > connection would be stateless and not care if the receivers actually > got the message or not... You could use the socket module to broadcast. Using INADDR_BROADCAST as the destination should do it. I fail to recollect whether that will need root privileges... -- kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On Thu, Aug 6, 2009 at 2:36 PM, Kee Nethery wrote: > As someone trying to learn the language I want to say that the tone on this > list towards people who are trying to learn Python feels like it has become > anti-newbies. > > Learning a new language is difficult enough without seeing other newbies > getting shamed for not knowing everything there is to know about Python > before asking their questions. Well I have to say your right about the tone but using google before you ask is not an unreasonable expectation. I have of course broken this golden rule myself on mailing lists myself , however a response like use google is totally acceptable to me. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On 2009-08-11, Paul Rubin wrote: > r writes: >> Some say the tutorial is not meant for non-programmers, but for >> programmers with no Python experience. So! How does that justify >> obstruction of the tut? Why not present the same information in a way >> both can easily understand? > > I agree that a tutorial for non-programmers would be useful, but it's > better to have it as a separate document. There are plenty of docs aimed at non-programmers. I've heard this one recommended many times: http://www.greenteapress.com/thinkpython/ -- Grant Edwards grante Yow! Am I having fun yet? at visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Networked Broadcast Messaging
On Aug 11, 3:00 pm, Kushal Kumaran wrote: > You could use the socket module to broadcast. Using INADDR_BROADCAST > as the destination should do it. I fail to recollect whether that > will need root privileges... Awesome, I think this is exactly what I'm looking for. Much appreciated! Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
> Try reading a little there! Starting there I went to > > http://en.wikipedia.org/wiki/Wikipedia:Creating_a_bot > > where I found a section on existing bots, comments on how the "scraping" > is not what you want, and even a Python section with a link to something > labelled PyWikipediaBot... > Thanks. I read the first bit of that page, but did not finish it. Grepping it for Python led to to what I need. Sorry for the noise. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
On 2009-08-11, max bianco wrote: > I assume I am misunderstanding you here and you meant something else? > Python is paraded as a good language for beginners. I believe it is a good language for beginners. > Is this a false statement or a secondary objective? Objective of what? > Or are the docs only meant for experienced developers? Some are, some aren't. The "standard" docs (most of the docs you can reach directly from http://www.python.org/doc/ -- including the tutorial) aren't intended to teach somebody how to program and assume the reader has a basic grounding in programming and language theory. There are plenty of books and tutorials that are aimed at teaching programming and Python at the same time. You can find some of via the links listed under "Additional documentation" at http://www.python.org/doc/ -- particularly here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > That just doesn't sound right. -- Grant Edwards grante Yow! My mind is making at ashtrays in Dayton ... visi.com -- http://mail.python.org/mailman/listinfo/python-list
for key, value in dict.() - how to get? (which func)
hi all, which method should I use to get iterator over (key, value) pairs for Python dict, Python v 2.6 and above? Of course I could use for key in myDict.keys(): value = myDict.values() # do something with the pair key, value but searching each time for the value take some cputime that is serious for the task involved IIRC in python 2.5 I have something like keyvalues(), but I don't see something like that in current dir(myDict). Thank you in advance, D. -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
IIRC Postgres has had ON DUPLICATE KEY UPDATE functionality longer than MySQL... On Tue, 11 Aug 2009 11:45:50 -0700, Pet wrote: On 11 Aug., 20:39, Kushal Kumaran wrote: On Tue, Aug 11, 2009 at 10:03 PM, someone wrote: > Hello, > I'd like to make insert into db if record not exist otherwise update. > to save typing list of columns in both statements I do following > > is there better or more readable way to do it? Well, mysql, in particular, offers an "on duplicate key update" clause that you can take a look at. Don't know about about other databases. -- kushal Oh, forgotten to mention. It's PostGres -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: for key, value in dict.() - how to get? (which func)
Hi Dmitrey, I think what you're looking for is myDict.items(), or myDict.iteritems(). Cheers, Rami On Tue, 11 Aug 2009 12:15:13 -0700, dmitrey wrote: hi all, which method should I use to get iterator over (key, value) pairs for Python dict, Python v 2.6 and above? Of course I could use for key in myDict.keys(): value = myDict.values() # do something with the pair key, value but searching each time for the value take some cputime that is serious for the task involved IIRC in python 2.5 I have something like keyvalues(), but I don't see something like that in current dir(myDict). Thank you in advance, D. -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming by Contract
Ethan Furman wrote: Greetings! I have seen posts about the assert statement and PbC (or maybe it was DbC), and I just took a very brief look at pycontract (http://www.wayforward.net/pycontract/) and now I have at least one question: Is this basically another way of thinking about unit testing, or is the idea of PbC more along the lines of *always* checking the input/output of functions to ensure they are correct? (*Contstant vigilance!* as Prof Moody would say ;) I know asserts can be turned off, so they obviously won't work for the latter case, and having seen the sample of pycontract it seems it only does its thing during debugging. So is Design (Programming) by Contract a fancy way of saying "Document your inputs/outputs!" or is there more to it? ~Ethan~ Hmmm... Well, from the (apparently) complete lack of interest, I shall take away the (better?) documentation ideas and unit testing ideas, and not worry about the rest. :) ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: for key, value in dict.() - how to get? (which func)
Yes, thank you, items() is the correct approach, on the other hand I have already get rid of the cycle. Regards, D. On Aug 11, 10:26 pm, "Rami Chowdhury" wrote: > Hi Dmitrey, > > I think what you're looking for is myDict.items(), or myDict.iteritems(). > > Cheers, > Rami > > On Tue, 11 Aug 2009 12:15:13 -0700, dmitrey > wrote: > > > > > hi all, > > which method should I use to get iterator over (key, value) pairs for > > Python dict, Python v 2.6 and above? > > > Of course I could use > > > for key in myDict.keys(): > > value = myDict.values() > > # do something with the pair key, value > > > but searching each time for the value take some cputime that is > > serious for the task involved > > > IIRC in python 2.5 I have something like keyvalues(), but I don't see > > something like that in current dir(myDict). > > > Thank you in advance, D. > > -- > Rami Chowdhury > "Never attribute to malice that which can be attributed to stupidity" -- > Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
On 11 Aug., 21:23, "Rami Chowdhury" wrote: > IIRC Postgres has had ON DUPLICATE KEY UPDATE functionality longer than > MySQL... > Ok, I've completely failed here :) Thanks, man > > > > > On Tue, 11 Aug 2009 11:45:50 -0700, Pet wrote: > > On 11 Aug., 20:39, Kushal Kumaran > > wrote: > >> On Tue, Aug 11, 2009 at 10:03 PM, someone > >> wrote: > >> > Hello, > > >> > I'd like to make insert into db if record not exist otherwise update. > >> > to save typing list of columns in both statements I do following > > >> > > > >> > is there better or more readable way to do it? > > >> Well, mysql, in particular, offers an "on duplicate key update" clause > >> that you can take a look at. Don't know about about other databases. > > >> -- > >> kushal > > > Oh, forgotten to mention. It's PostGres > > -- > Rami Chowdhury > "Never attribute to malice that which can be attributed to stupidity" -- > Hanlon's Razor > 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
On Tue, Aug 11, 2009 at 12:29 PM, Dotan Cohen wrote: > >Wikipedia has an API for computer access. See > > > >http://www.mediawiki.org/wiki/API > > > > Yes, I am aware of this as well. Does anyone know of a python class > for easily interacting with it, or do I need to roll my own. > > -- > Dotan Cohen > https://mwclient.svn.sourceforge.net/svnroot/mwclient/trunk/mwclient/ -- http://mail.python.org/mailman/listinfo/python-list
adding multiple new values to the same key in a dictionary
Hi,
I want to be able to add multiple new values to a key in a dictionary.
I have tried the following:
sec_dict_clean=
{88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85]}
for i in range(len(sec_dict_clean.values())):
for j in range(len(sec_dict_clean.values()[i])):
sec_dict_clean.setdefault(key,[]).append(blocks[sec_dict_clean.values()[i][j]].abundance)
where blocks[...].abundance gives me a single value from an object,
but this gives me the following:
sec_dict_clean=
{88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85, 4, 12,
11, 6, 9, 12, 11, 7, 10, 10, 12, 9, 6, 12, 15, 9, 8, 12, 15, 12, 12]}
instead I want each abundance (starts with 4, 12...) to be associated with each
of the values so that it would look like this:
sec_dict_clean=
{88: ([87, 89, 78, 98], [4,12,11,6]), 58: ([57, 59, 48, 68], [9,12,11,7]), 69:
([79], [10])...}
You can see there are several errors here (I have more things being appended
than there are values in the dictionary), but I really just want to know how to
add multiple values to the same key in a dictionary.
Thanks for any help,
Krishna
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
Ok people follow me here. Open your winders help file and click the "Tutorial" link. What is this FLUFF doing here!?!?! Where is the damn index? Where is the damn tutorial? I want to learn Python not read the HISTORY OF THE WORLD. Upon clicking the "Tutorial" link pre 2.6, a nice menu was placed before your eyes, WHERE IS IT!?!??? Good thing i know the language or i would have given up already -- http://mail.python.org/mailman/listinfo/python-list
Need cleanup advice for multiline string
Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?
Help is appreciated!
def RunCommand( commandList ):
commandString =
print( 'Running Command:', )
cmd = subprocess.Popen( commandList )
returnCode = cmd.wait()
if returnCode:
failMsg = '''\
*
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
{}
*
'''
commandString = ' '.join( commandList )
raise CommandFailure( failMsg.format( returnCode,
commandString ) )
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
r wrote: Ok people follow me here. Open your winders help file and click the "Tutorial" link. What is this FLUFF doing here!?!?! Where is the damn index? Where is the damn tutorial? I want to learn Python not read the HISTORY OF THE WORLD. Upon clicking the "Tutorial" link pre 2.6, a nice menu was placed before your eyes, WHERE IS IT!?!??? Good thing i know the language or i would have given up already As I keep on repeating, my version works perfectly. Perhaps this is because I have a computing IQ of approximately 2**32, so realised that something was wrong, went here and downloaded the corrected version of the file, which has presumably been available for months! http://www.python.org/download/releases/2.6.2/ And you want newbies let loose on the python docs. As good ole Santa would say, ho, ho, ho! -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
"Rami Chowdhury" writes: > IIRC Postgres has had ON DUPLICATE KEY UPDATE functionality longer than > MySQL... PostgreSQL does not have ON DUPLICATE KEY UPDATE. The SQL standard way to do what the OP wants is MERGE. PostgreSQL doesn't have that either. -M- -- http://mail.python.org/mailman/listinfo/python-list
Re: better way?
Ah, my apologies, I must have been getting it confused with ON UPDATE [things]. Thanks for correcting me. On Tue, 11 Aug 2009 13:10:03 -0700, Matthew Woodcraft wrote: "Rami Chowdhury" writes: IIRC Postgres has had ON DUPLICATE KEY UPDATE functionality longer than MySQL... PostgreSQL does not have ON DUPLICATE KEY UPDATE. The SQL standard way to do what the OP wants is MERGE. PostgreSQL doesn't have that either. -M- -- Rami Chowdhury "Never attribute to malice that which can be attributed to stupidity" -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Re: Need cleanup advice for multiline string
On Aug 11, 3:08 pm, Robert Dailey wrote:
> Hey guys. Being a C++ programmer, I like to keep variable definitions
> close to the location in which they will be used. This improves
> readability in many ways. However, when I have a multi-line string
> definition at function level scope, things get tricky because of the
> indents. In this case indents are serving two purposes: For syntax and
> actual text output. The tabs for function scope should not be included
> in the contents of the string. Below is the code I am trying to
> improve. Notice how it looks ugly/unreadable because of the way the
> string contents are shifted all the way to the left edge of the
> document. This breaks the flow of scope. Would you guys solve this
> problem by moving failMsg into global scope? Perhaps through some
> other type of syntax?
>
> Help is appreciated!
>
> def RunCommand( commandList ):
> commandString =
> print( 'Running Command:', )
> cmd = subprocess.Popen( commandList )
> returnCode = cmd.wait()
> if returnCode:
> failMsg = '''\
> *
> The following command returned exit code [{:#x}].
> This represents failure of some form. Please review
> the command output for more details on the issue.
>
> {}
> *
> '''
> commandString = ' '.join( commandList )
> raise CommandFailure( failMsg.format( returnCode,
> commandString ) )
And yes, I recognize there are syntax errors. Ignore those for now.
--
http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree - Howto access text within XML tag element...
On Aug 11, 9:51 am, "Diez B. Roggisch" wrote:
> cmalmqui schrieb:
> > Hi,
>
> > I am writing on a small XML parser and are currently stuck as I am not
> > able to get the whole element name in ElementTree.
>
> > Please see the below example where "print root[0][0]" returns
> > ""
>
> > Is there a way to get hold of the "Running" string in the tag using
> > elementTree?
>
> >
> >
> > 2009-07-10T14:48:00Z
> >
> > .
>
> > For those of you that know how to program XML I have another
> > question:
> > I am currently "hardcoding" my XML parser using brackets, is this a
> > good approach or should I build it using a "search on tag" approach.
>
> What do you mean by that - hardcoding by brackets?
>
> Diez
Indeed, my current approach has been to hardcode the XML parser using
brackets. Is there a more elegant way?
I am parsing a garmin xml file from a handheld GPS and as you can see
in the below script, I am hardcoding each node:
import xml.etree.cElementTree as etree
def gettext(elem):
text = elem.text or ""
for e in elem:
text += gettext(e)
if e.tail:
text += e.tail
return text
tree = etree.parse('10_07_2009 16_48_00_history.tcx')
root = tree.getroot()
elem = root[0][0]
# ID Tag
print "type of exercise: " + elem.get("Sport")
print "excercise starttime : " + gettext(elem[0])
# iterate over all laps
for i in range(1, len(elem)-1):
# LAP TAG
print "\nlap number : " + str(i)
print "lap start time : " + str(elem[i].get("StartTime"))
print "lap duration (s): " + gettext(elem[i][0])
print "lap length (m) : " + gettext(elem[i][1])
print "max speed (km/h): " + gettext(elem[i][2])
print "number of calories : " + gettext(elem[i][3])
print "average heartbeat : " + gettext(elem[i][4][0])
print "max heartbeat : " + gettext(elem[i][5][0])
print "number of records : " + str(len(elem[i][8])-1)
for j in range(1, len(elem[i][8])-1):
time = gettext(elem[i][8][j][0])#time
lat = gettext(elem[i][8][j][1][0]) #lat
lon = gettext(elem[i][8][j][1][1]) #lon
alt = gettext(elem[i][8][j][2])#alt
dist = gettext(elem[i][8][j][3])#distance from start
bpm = gettext(elem[i][8][j][4][0]) #beats per minute
#print time + " " + lat + " " + lon + " " + alt + " " + dist +
" " + bpm
print "\nReceiver Info : " + gettext(elem[len(elem)-1][0])
--
http://mail.python.org/mailman/listinfo/python-list
Re: Unrecognized escape sequences in string literals
On Aug 11, 2:00 pm, Steven D'Aprano wrote: > > test.cpp:1:1: warning: unknown escape sequence '\y' > > Isn't that a warning, not a fatal error? So what does temp contain? My "Annotated C++ Reference Manual" is packed, and surprisingly in Stroustrup's Third Edition, there is no mention of the issue in the entire 1,000 pages. But Microsoft to the rescue: If you want a backslash character to appear within a string, you must type two backslashes (\\) (From http://msdn.microsoft.com/en-us/library/69ze775t.aspx) The question of what any specific C++ does if you ignore the warning is irrelevant, as such behavior in C++ is almost *always* undefined. Hence the warning. |>ouglas -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree - Howto access text within XML tag element...
On Aug 11, 9:13 am, Ned Deily wrote: > In article > <[email protected]>, > > cmalmqui wrote: > > I am writing on a small XML parser and are currently stuck as I am not > > able to get the whole element name in ElementTree. > > > Please see the below example where "print root[0][0]" returns > > "" > > > Is there a way to get hold of the "Running" string in the tag using > > elementTree? > > > > > > > 2009-07-10T14:48:00Z > > > > . > > "Running" is the value of the "Sport" attribute of the "Activity" > element. The documentation for the Element interface lists several ways > to access element attributes; in your example, > > >>> elem = root[0][0] > >>> elem.get("Sport") > 'Running' > >>> elem.attrib > > {'Sport': 'Running'}>>> elem.items() > > [('Sport', 'Running')] > > Seehttp://docs.python.org/library/xml.etree.elementtree.html > > -- > Ned Deily, > [email protected] Excellent! Thanks! The XML magic is getting there slowly... -- http://mail.python.org/mailman/listinfo/python-list
httplib incredibly slow :-(
Hi All,
I'm using the following script to download a 150Mb file:
from base64 import encodestring
from httplib import HTTPConnection
from datetime import datetime
conn = HTTPSConnection('localhost')
headers = {}
auth = 'Basic '+encodestring('username:password').strip()
headers['Authorization']=auth
t = datetime.now()
print t
conn.request('GET','/somefile.zip',None,headers)
print 'request:',datetime.now()-t
response = conn.getresponse()
print 'response:',datetime.now()-t
data = response.read()
print 'read:',datetime.now()-t
The output shows it takes over 20 minutes to do this.
However, this is on a local network, and downloading the same file in IE
takes under 3 seconds!
I saw this issue:
http://bugs.python.org/issue2576
I tried changing the buffer size to 4096 in a subclass as the issue
suggested, but I didn't see the reported speed improvement.
I'm using Python 2.6.2.
Does anyone know of an alternative library for creating http requests
and getting their responses that's faster but hopefully has a similar
interface?
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list
Bug in format specification in Python 3?
Hello,
According to the Python 3.1 documentation, I can have a format
specification like so:
print( 'This is a hex number: {:#08x}'.format( 4 ) )
This will print:
This is a hex number: 0x04
I notice that the '0x' portion is counted in the width, which was
specified as 8. This seems wrong to me. Is this by design? If so, why?
I expect that the width portion to only apply to the input + padding
only. I don't consider the '0x' portion part of the padding. But maybe
it is...
--
http://mail.python.org/mailman/listinfo/python-list
Re: Need cleanup advice for multiline string
Robert Dailey wrote:
On Aug 11, 3:08 pm, Robert Dailey wrote:
Hey guys. Being a C++ programmer, I like to keep variable definitions
close to the location in which they will be used. This improves
readability in many ways. However, when I have a multi-line string
definition at function level scope, things get tricky because of the
indents. In this case indents are serving two purposes: For syntax and
actual text output. The tabs for function scope should not be included
in the contents of the string. Below is the code I am trying to
improve. Notice how it looks ugly/unreadable because of the way the
string contents are shifted all the way to the left edge of the
document. This breaks the flow of scope. Would you guys solve this
problem by moving failMsg into global scope? Perhaps through some
other type of syntax?
Help is appreciated!
def RunCommand( commandList ):
commandString =
print( 'Running Command:', )
cmd = subprocess.Popen( commandList )
returnCode = cmd.wait()
if returnCode:
failMsg = '''\
*
The following command returned exit code [{:#x}].
This represents failure of some form. Please review
the command output for more details on the issue.
{}
*
'''
commandString = ' '.join( commandList )
raise CommandFailure( failMsg.format( returnCode,
commandString ) )
And yes, I recognize there are syntax errors. Ignore those for now.
For starters take a look at http://tinyurl.com/o2o8r8 , just about every
combination of string concatenation going there. I assume that one of
these will let you leave failMsg where it belongs.
--
Kindest regards.
Mark Lawrence.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Scraping Wikipedia with Python
Dotan Cohen writes: > Thanks. I read the first bit of that page, but did not finish it. > Grepping it for Python led to to what I need. maybe you want dbpedia. -- http://mail.python.org/mailman/listinfo/python-list
Re: Unrecognized escape sequences in string literals
Douglas Alan wrote: On Aug 11, 2:00 pm, Steven D'Aprano wrote: test.cpp:1:1: warning: unknown escape sequence '\y' Isn't that a warning, not a fatal error? So what does temp contain? My "Annotated C++ Reference Manual" is packed, and surprisingly in Stroustrup's Third Edition, there is no mention of the issue in the entire 1,000 pages. But Microsoft to the rescue: If you want a backslash character to appear within a string, you must type two backslashes (\\) (From http://msdn.microsoft.com/en-us/library/69ze775t.aspx) The question of what any specific C++ does if you ignore the warning is irrelevant, as such behavior in C++ is almost *always* undefined. Hence the warning. |>ouglas Almost always undefined? Whereas with Python, and some memorization or a small table/list nearby, you can easily *know* what you will get. Mind you, I'm not really vested in how Python *should* handle backslashes one way or the other, but I am glad it has rules that it follows for consitent results, and I don't have to break out a byte-code editor to find out what's in my string literal. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
Ah Ha! the docs are broken and i was right all along! Are the good folks at Python dev rolling a new installer as we speak, or we must wait for new version? -- http://mail.python.org/mailman/listinfo/python-list
Re: Need cleanup advice for multiline string
Robert Dailey: > This breaks the flow of scope. Would you guys solve this > problem by moving failMsg into global scope? > Perhaps through some other type of syntax? There are gals too here. This may help: http://docs.python.org/library/textwrap.html#textwrap.dedent Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing - group effort to hire writers?
r wrote: Ah Ha! the docs are broken and i was right all along! Are the good folks at Python dev rolling a new installer as we speak, or we must wait for new version? As I pointed out a few minutes ago thicko, the new version has been available for months. -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
New 2.6 and wx install => DLL load failed: The specified procedure could not be found
I just installed python-2.6.2.msi from Python.org and wxPython2.8-win32-ansi-2.8.10.1-py26.exe and now can't import this wx. (I had 2.4, but uninstalled) This reminds me of a basic question I had before: what are the compilers used for the Win32 binaries? Is this a compiler compatibility issue? a wx bug? C:\projects>python Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import wx Traceback (most recent call last): File "", line 1, in File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\__init__.py", line 45, in from wx._core import * File "C:\Python26\lib\site-packages\wx-2.8-msw-ansi\wx\_core.py", line 4, in < module> import _core_ ImportError: DLL load failed: The specified procedure could not be found. >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Programming by Contract
On Aug 11, 2009, at 3:30 PM, Ethan Furman wrote: Ethan Furman wrote: Greetings! I have seen posts about the assert statement and PbC (or maybe it was DbC), and I just took a very brief look at pycontract (http://www.wayforward.net/pycontract/ ) and now I have at least one question: Is this basically another way of thinking about unit testing, or is the idea of PbC more along the lines of *always* checking the input/output of functions to ensure they are correct? (*Contstant vigilance!* as Prof Moody would say ;) I know asserts can be turned off, so they obviously won't work for the latter case, and having seen the sample of pycontract it seems it only does its thing during debugging. So is Design (Programming) by Contract a fancy way of saying "Document your inputs/outputs!" or is there more to it? ~Ethan~ Hmmm... Well, from the (apparently) complete lack of interest, I shall take away the (better?) documentation ideas and unit testing ideas, and not worry about the rest. :) Design by contract is complementary to unit testing (I notice that the author of PEP 316 appears confused about this). DbC is, roughly speaking, about explicit allocation of responsibility. Consider this contrived example. def foo(s): require(s is not None) //code ensure(hasattr(returnValue, '__iter__')) The require condition tells you that it's the caller's responsibility to pass a non-nil argument to foo. The ensure condition is a guarantee that foo will return something suitable for iteration, if the precondition in the require condition is satisfied. These conditions can be enforced at runtime, but may not be, for reasons of performance. DbC is in fact about not *always* checking the input/output of functions; on the contrary, Bertrand Meyer, the inventor of DbC, claims that DbC allows one to eliminate such redundancy, and the resulting overhead. Charles Yeomans -- http://mail.python.org/mailman/listinfo/python-list
Re: Need cleanup advice for multiline string
On Aug 11, 3:40 pm, Bearophile wrote: > Robert Dailey: > > > This breaks the flow of scope. Would you guys solve this > > problem by moving failMsg into global scope? > > Perhaps through some other type of syntax? > > There are gals too here. > This may help:http://docs.python.org/library/textwrap.html#textwrap.dedent > > Bye, > bearophile It's a figure of speech. And besides, why would I want programming advice from a woman? lol. Thanks for the help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in format specification in Python 3?
Robert Dailey wrote:
Hello,
According to the Python 3.1 documentation, I can have a format
specification like so:
print( 'This is a hex number: {:#08x}'.format( 4 ) )
This will print:
This is a hex number: 0x04
I notice that the '0x' portion is counted in the width, which was
specified as 8. This seems wrong to me. Is this by design? If so, why?
I expect that the width portion to only apply to the input + padding
only. I don't consider the '0x' portion part of the padding. But maybe
it is...
The width portion is the width of what is produced by the specification.
The '#' tells it to add '0x', so that's part of what's produced. If you
don't want the '0x' to be counted then don't include '#' in the
specification. It makes perfect sense! :-)
--
http://mail.python.org/mailman/listinfo/python-list
Re: dictionary help
Krishna Pacifici wrote:
Wow, thanks MRAB and Simon, you guys are good.
I guess I will go ahead and ask the next question that has also stumped
me for awhile now.
So basically I need to loop through the values in the new dictionary and
append attributes of a class object. Each of the values (and keys)
represent a block in a grid with a specific location (e.g. 35 is the
block in row 3 col 5) and each block is an object with several
attributes associated with it. I want to call that block and append two
separate attributes to the dictionary within that same key.
So again it would look something like this:
block 35 has 2 attributes, say a and b, a=2, b=5
block 37 has a=1, b=3
block 46 has a=3, b=8
the two attributes come from two different definitions within the class
statement,
def detections
...
return a
def abundance
...
return b
so I would want to append to key 36 those two attributes for each block
so that the resulting dictionary item would look like this:
a b
{36:[35,37,46], [2,1,3], [5,3,8] ...}
That doesn't look like a dictionary. Perhaps want you want is for the
value to be a list of lists:
{36: [[35,37,46], [2,1,3], [5,3,8]] ...}
although you'd have parallel lists, ie a list of blocks, a list of 'a',
and a list of 'b'. A better format might be to keep a block's attributes
with the block itself.
{36: [[35,2,5], [37,1,3], [46,3,8]] ...}
Any help with this would be greatly appreciated. And thank you so much
for all of your help thus far, I'm still pretty new to python and am
enjoying all of the flexibility associated with a scripting and
programming language.
[snip]
--
http://mail.python.org/mailman/listinfo/python-list
run all scripts in sub-directory as subroutines?
I want to have a program which will form a list of all *.py scripts in
a sub-directory, and then call some standard messages on them. So I
can add a new data source modularly by just dropping a new file into
the sources directory with the appropriate methods in it.
For example:
path = sys.path[0]
print "Starting Directory:: " + path
getDir=path + "\Sources"# point to directory of data
sources
for name in os.listdir(getDir): # run collection from each source
src = imp.load_source("data",getDir,open(getDir+"\\"+name, 'rb'))
src.getData()
src.doGraph()
This works fine, but in the sub-modules the sys.path appropriately
returns the same as from the parent, I want them to know their own
file names. How?? I can pass it to them, but wondered if there is a
more self-sufficient way for a module to know from where it was
invoked.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Programming by Contract
Charles Yeomans wrote: On Aug 11, 2009, at 3:30 PM, Ethan Furman wrote: Ethan Furman wrote: Greetings! I have seen posts about the assert statement and PbC (or maybe it was DbC), and I just took a very brief look at pycontract (http://www.wayforward.net/pycontract/ ) and now I have at least one question: Is this basically another way of thinking about unit testing, or is the idea of PbC more along the lines of *always* checking the input/output of functions to ensure they are correct? (*Contstant vigilance!* as Prof Moody would say ;) I know asserts can be turned off, so they obviously won't work for the latter case, and having seen the sample of pycontract it seems it only does its thing during debugging. So is Design (Programming) by Contract a fancy way of saying "Document your inputs/outputs!" or is there more to it? ~Ethan~ Hmmm... Well, from the (apparently) complete lack of interest, I shall take away the (better?) documentation ideas and unit testing ideas, and not worry about the rest. :) Design by contract is complementary to unit testing (I notice that the author of PEP 316 appears confused about this). DbC is, roughly speaking, about explicit allocation of responsibility. Consider this contrived example. def foo(s): require(s is not None) //code ensure(hasattr(returnValue, '__iter__')) The require condition tells you that it's the caller's responsibility to pass a non-nil argument to foo. The ensure condition is a guarantee that foo will return something suitable for iteration, if the precondition in the require condition is satisfied. These conditions can be enforced at runtime, but may not be, for reasons of performance. DbC is in fact about not *always* checking the input/output of functions; on the contrary, Bertrand Meyer, the inventor of DbC, claims that DbC allows one to eliminate such redundancy, and the resulting overhead. Charles Yeomans Many thanks! So if I understand -- Python's EAFP fits well with DbC, as DbC seems well suited to say, "This is your responsibility, and this is mine," stated in programming terms (who needs comments? ;) Combined with unit testing (which should be easier to design correctly given the DbC code), healthy code seems more attainable. Interesting. Thank you for the information. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
