Re: encoding ascii data for xml
On Fri, 03 Oct 2008 14:41:13 -0700, harrelson wrote:
> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "%s" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". I would think that
> trying to encode this to utf-8 would fail but it doesn't-- I don't get a
> failure till we get into xml land and the parser complains. My question
> is why doesn't encode() blow up? It seems to me that encode() shouldn't
> output anything that parseString() can't handle.
It's not a problem with encode IMHO but with XML because XML can't handle
all ASCII characters. XML parsers choke on every code below 32 that is
not whitespace. BTW `chr(3)` isn't "end of line" but "end of text" (ETX).
If you want to be sure that an arbitrary string can be embedded into XML
you'll have to encode it as base64 or something similar.
Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list
Re: lint for Python?
On Fri, 03 Oct 2008 17:38:13 -0400, Pat wrote: > Pylint does a decent job at checking for errors only within a single > module. > > Here's one of my problems. I have two modules. > > In module one, I have a function: > > def foo( host, userid, password ): > pass > > In module two, I call that function: > > foo( userid, password) > > lint doesn't find that error and it won't be caught until it's called > while the program is running. I don't want that error found at 3AM. Then don't run the unit tests that late at night (or early in the morning). ;-) Besides the `unittest` module from the standard library you might look into `py.test` and `nose`. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: encoding ascii data for xml
On Friday 03 October 2008, harrelson wrote:
> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "%s" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". [...] My
> question is why doesn't encode() blow up?
You just answered your question. 0x03 may not be a printing character, but it
is a valid character in the ascii character set and therefore is not a
problem. For xml, however, it is an illegal character so that's why the
parser is throwing an error.
--
http://mail.python.org/mailman/listinfo/python-list
Re: how to get a class instance name during creation?
On Oct 3, 9:46 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > dmitrey a écrit : > > > hi all, > > I have a code > > z = MyClass(some_args) > > can I somehow get info in MyClass __init__ function that user uses "z" > > as name of the variable? > > > I.e. to have __init__ function that creates field z.name with value > > "z". > > This has been debated to hell and back. To make a long story short, here > are two common use cases: > > x = MyClass() > y = x > del x > > objects = [MyClass() for i in range(100)] > > If you can come with a meaningfull answer to "what's *the* name of any > of the MyClass instance(s)" in both cases, then please let us know... I had seen the examples during google search, still I hoped for an answer to my exact situation. I know for sure there will be no renaming and creating like the above objects = [MyClass() for i in range(100)]. as for the z = MyClass(some, args, 'z') I had it in my mind but I hoped to have something automatic, w/o the arg 'z'. Regards, D. -- http://mail.python.org/mailman/listinfo/python-list
Re: "Exploding" (**myvariable) a dict with unicode keys
> Oh. I read somewhere that UTF-8 variable names we're supported. I > thought I even saw a colleague using Kanji. In source code (string literals, comments), surely. Not in variable names, not in 2.x. To rephrase Bruno's comment: Python supports UTF-8 as a source encoding. That doesn't mean that you can use all Unicode characters in all places of the code. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: encoding ascii data for xml
On Oct 4, 7:41 am, harrelson <[EMAIL PROTECTED]> wrote:
> I have a large amount of data in a postgresql database with the
> encoding of SQL_ASCII. Most recent data is UTF-8 but data from
> several years ago could be of some unknown other data type. Being
> honest with myself, I am not even sure that the most recent data is
> always UTF-8-- data is entered on web forms and I wouldn't be
> surprised if data of other encodings is slipping in.
>
> Up to the point I have just ignored the problem-- on the web side of
> things everything works good enough. But now I am required to stuff
> this data into xml datasets and I am, of course, having problems. My
> preference would be to force the data into UTF-8 even if it is
> ultimately an incorrect encoding translation but this isn't working.
> The below code represents my most recent problem:
>
> import xml.dom.minidom
> print chr(3).encode('utf-8')
> dom = xml.dom.minidom.parseString( "%s" %
> chr(3).encode('utf-8') )
>
> chr(3) is the ascii character for "end of line". I would think that
> trying to encode this to utf-8 would fail but it doesn't-- I don't get
> a failure till we get into xml land and the parser complains. My
> question is why doesn't encode() blow up? It seems to me that
> encode() shouldn't output anything that parseString() can't handle.
The encode method is doing its job, which is to encode ANY and EVERY
unicode character as utf-8, so that it can be transported reliably
over an 8-bit-wide channel. encode is *not* supposed to guess what you
are going to do with the output.
Perhaps instead of "forcing the data into utf-8", you should be
thinking about what is actually in your data. What is the context that
chr(3) appears in? Perhaps when you get around to print
repr(some_data), you might see things like "\x03harlie \x03haplin" --
it's a common enough keyboarding error to hit the Ctrl key instead of
the Shift key and unfortunately a common-enough design error for there
to be no checking at all.
BTW, there's no forcing involved -- chr(3) is *already* utf-8.
HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list
Re: how to get a class instance name during creation?
dmitrey <[EMAIL PROTECTED]> writes: > On Oct 3, 9:46 pm, Bruno Desthuilliers > <[EMAIL PROTECTED]> wrote: > > x = MyClass() > > y = x > > del x > > > > objects = [MyClass() for i in range(100)] > > > > If you can come with a meaningfull answer to "what's *the* name of > > any of the MyClass instance(s)" in both cases, then please let us > > know... > > I had seen the examples during google search, still I hoped for an > answer to my exact situation. I know for sure there will be no > renaming and creating like the above objects = [MyClass() for i in > range(100)]. You *know* this, *for sure*? The only way I can think of that would give you such certain knowledge that such a situation will not happen is an automated, full-coverage unit test suite of all code that uses your class. Which is an excellent position to be in, so I commend you on your diligence. So, why is it that you wish to restrict users of your class to never do such normal operations with instances as in the above examples? What problem are you solving by this restriction? -- \ “Holy human pressure cookers, Batman!” —Robin | `\ | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
newbie question: apply()
HI, I need an example for the usage of the apply()-function. Can you help me? Thanks. o-o Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: apply()
On Sat, Oct 4, 2008 at 2:25 AM, TK <[EMAIL PROTECTED]> wrote:
> HI,
>
> I need an example for the usage of the apply()-function. Can you help me?
Don't use the apply() function, it's deprecated and unnecessary thanks
to Python's enhanced calling syntax, which is described in depth on
http://www.python.org/doc/2.5.2/ref/calls.html
apply() was used to call an arbitrary function with arbitrary arguments.
This can now be done by e.g:
pos_args = ["spam", 1, [3]]
kwd_args = {"b":7, "c":9}
result = some_function(*pos_args, **kwd_args)
Which is equivalent to:
result = some_function("spam", 1, [3], b=7, c=9)
Which was equivalent to:
result = apply(some_function, pos_args, kwd_args)
Cheers,
Chris
>
> Thanks.
>
> o-o
>
> Thomas
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: newbie question: apply()
pos_args = ["spam", 1, [3]]
kwd_args = {"b":7, "c":9}
result = some_function(*pos_args, **kwd_args)
Which is equivalent to:
result = some_function("spam", 1, [3], b=7, c=9)
Which was equivalent to:
result = apply(some_function, pos_args, kwd_args)
Thanks a lot.
o-o
Thomas
--
http://mail.python.org/mailman/listinfo/python-list
garbage collector and slowdown (guillaume weymeskirch)
Hello everybody,
To test the python 2.5 garbage collector, I wrote a trivial script
allocating dummy objects of various sizes, then forgetting them in a loop.
The garbage collector seems working well, limiting the memory used.
But I've noticed a near linear slowdown of the execution : after a few
minutes - and several millions of allocated and freed objects, each
iteration take more and more time to execute.
Does anybody have noticed this ? Could python suffers from some memory
fragmentation in long running processes ?
I've got the same slowdown in python 2.5.2 on a 64 bits gentoo linux
box, and on a winxp machine.
FYI, the following script shows this : it prints the seconds taken for
each iteration.
--- cut here ---
import random,sys
from timeit import Timer
class Reference(object):
refcount = {}
def __init__(self):
name = self.__class__.__name__
c = self.refcount.setdefault(name,0)
self.refcount[name] = c + 1
class ClassicSmall(Reference):
def __init__(self):
super(ClassicSmall,self).__init__()
self.a = 0;
self.b = 2;
self.c = random.randint(0,500)
self.d = random.randint(0,500)
class ClassicBig(Reference):
def __init__(self):
super(ClassicBig,self).__init__()
self.mylist = range(1000)
class Tree(Reference):
def __init__(self,left,right):
super(Tree,self).__init__()
self.left = left
self.right = right
self.data = ''.join([chr(x) for x in range(65,128)])
def doit():
smalls = []
bigs = []
trees = []
for i in xrange(3):
smalls.append(ClassicSmall())
bigs.append(ClassicBig())
trees.append(Tree(1,2))
if __name__ == '__main__':
t = Timer("doit()", "from __main__ import doit; gc.enable()")
min = 0.9e300; max=0.
try:
while True:
d = t.timeit(1)
if d < min:
min = d
if d > max:
max = d
print d
except:
pass
print Reference.refcount
print "max=%f min=%f " % (max,min)
--
http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
Martin Geisler <[EMAIL PROTECTED]> wrote: > I just tried running my code using "python2.6 -3" and got a bunch of > >SyntaxWarning: tuple parameter unpacking has been removed in 3.x > > warnings. I've read PEP-3113: > >http://www.python.org/dev/peps/pep-3113/ > > but I'm still baffled as to why you guys could remove such a wonderful > feature?! I don't think many people will miss tuple unpacking in def statements. I think the warning is probably wrong anyway - you just need to remove a few parens... >ci.addCallback(lambda (ai, bi): ai * bi) >map(lambda (i, s): (field(i + 1), s), enumerate(si)) On Python 3.0rc1 (r30rc1:66499, Oct 4 2008, 11:04:33) >>> f = lambda (ai, bi): ai * bi File "", line 1 f = lambda (ai, bi): ai * bi ^ SyntaxError: invalid syntax But >>> f = lambda ai, bi: ai * bi >>> f(2,3) 6 Likewise >>> lambda (i, s): (field(i + 1), s) File "", line 1 lambda (i, s): (field(i + 1), s) ^ SyntaxError: invalid syntax >>> lambda i, s: (field(i + 1), s) at 0xb7bf75ec> >>> So just remove the parentheses and you'll be fine. I have to say I prefer named functions, but I haven't done much functional programming def f(ai, bi): return ai * bi ci.addCallback(f) def f(i, s): return field(i + 1), s map(f, enumerate(si)) PEP-3113 needs updating as it is certainly confusing here! 2to3 is doing the wrong thing also by the look of it. -- Nick Craig-Wood <[EMAIL PROTECTED]> -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
Nick Craig-Wood wrote: > Martin Geisler <[EMAIL PROTECTED]> wrote: > >> I just tried running my code using "python2.6 -3" and got a bunch of >> >>SyntaxWarning: tuple parameter unpacking has been removed in 3.x >> >> warnings. I've read PEP-3113: >> >>http://www.python.org/dev/peps/pep-3113/ >> >> but I'm still baffled as to why you guys could remove such a wonderful >> feature?! > > I don't think many people will miss tuple unpacking in def statements. > > I think the warning is probably wrong anyway - you just need to remove > a few parens... > >>ci.addCallback(lambda (ai, bi): ai * bi) >>map(lambda (i, s): (field(i + 1), s), enumerate(si)) > > On > Python 3.0rc1 (r30rc1:66499, Oct 4 2008, 11:04:33) > f = lambda (ai, bi): ai * bi > File "", line 1 > f = lambda (ai, bi): ai * bi >^ > SyntaxError: invalid syntax > > But > f = lambda ai, bi: ai * bi f(2,3) > 6 > > Likewise > lambda (i, s): (field(i + 1), s) > File "", line 1 > lambda (i, s): (field(i + 1), s) >^ > SyntaxError: invalid syntax lambda i, s: (field(i + 1), s) > at 0xb7bf75ec> > > So just remove the parentheses and you'll be fine. No, you change the function signature in the process. f = lambda (a, b): a*b is equivalent to def f((a, b)): # double parens return a*b and called as f(arg) where arg is an iterable with two items. In 3.0 it has to be rewritten as def f(ab): a, b = ab return a*b i. e. it needs a statement and an expression and is therefore no longer suitable for a lambda. Peter -- http://mail.python.org/mailman/listinfo/python-list
Is it possible to get the name of an application ?
hello, for a general debug routine, written in some module. I want to write the information to a file, with name derived from the main python file. Is it possible to get the name of the python script started first ? thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.6, GUI not working on vista?
Hi! Another way is to de-activate UAC. @-salutations -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: lint for Python?
Pat: I know about 3 different lints for Python, there's PyFlake too. But I don't know if it does what you want. > I've never used a language that didn't catch that type of error. What dynamic languages have you used in the past? > I'm quite surprised that Python is being used by a number of major > companies. How you catch these types of errors? Writing tests. You must adapt your coding style to the language you use. In Java/C++ your static type system catches those bugs for you, in dynamic languages you have to catch them with testing (or lints, if they are present). Languages like Haskell with a type system much stronger than Java/C++ ones help you catch even more bugs/problems at compile time, so you need more time to have a clean compilation, but you have less bugs later (but tests are useful in Haskell too, see QuickCheck, that has inspired lot of similar tools for other languages, Python included). Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: lint for Python?
On Oct 3, 5:38 pm, Pat <[EMAIL PROTECTED]> wrote: > I've been searching for a good multi-module lint checker for Python and > I haven't found one yet. > > Pylint does a decent job at checking for errors only within a single module. > > Here's one of my problems. I have two modules. > > In module one, I have a function: > > def foo( host, userid, password ): > pass > > In module two, I call that function: > > foo( userid, password) > > lint doesn't find that error and it won't be caught until it's called > while the program is running. I don't want that error found at 3AM. > > I've never used a language that didn't catch that type of error. I'm > quite surprised that Python is being used by a number of major > companies. How you catch these types of errors? With a decent testing suite of course. Even if this specific issue might be handled more or less by a lint-like tool (which is not trivial in a dynamic language since the f() used in module2 might not even be the function you think it is), there are dozens more potential errors that can't be checked, e.g. changing the order and/or the expected type of the arguments. There is no substitute of testing, and while this is true in statically typed languages too, it's even more critical in dynamic languages. George -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to get the name of an application ?
Stef Mientki wrote: hello, for a general debug routine, written in some module. I want to write the information to a file, with name derived from the main python file. Is it possible to get the name of the python script started first ? thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list Is sys.argv[0] what you want? If so, then os.path.basename(sys.argv[0]) is probably what you want. Thanks, Gary M. Josack -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to get the name of an application ?
Gary M. Josack wrote: Stef Mientki wrote: hello, for a general debug routine, written in some module. I want to write the information to a file, with name derived from the main python file. Is it possible to get the name of the python script started first ? thanks, Stef Mientki -- http://mail.python.org/mailman/listinfo/python-list Is sys.argv[0] what you want? thanks Gary, That's exactly what I was looking for. cheers, Stef If so, then os.path.basename(sys.argv[0]) is probably what you want. Thanks, Gary M. Josack -- http://mail.python.org/mailman/listinfo/python-list
Re: garbage collector and slowdown (guillaume weymeskirch)
guillaume> But I've noticed a near linear slowdown of the execution :
guillaume> after a few minutes - and several millions of allocated and
guillaume> freed objects, each iteration take more and more time to
guillaume> execute.
I ran your script on my Mac and let it go for over an hour. The times
printed near the end were not that different than the times near the start.
First few prints:
4.80848908424
4.17986106873
4.07436394691
4.57266497612
4.26531600952
4.1674759388
4.00401592255
4.56974005699
4.33465600014
4.17652106285
4.02282094955
4.6344628334
Last few prints:
4.0330889225
3.89743900299
4.39918708801
4.16099500656
4.06551003456
3.91927599907
4.42701005936
4.17856502533
4.07650518417
3.93242192268
4.44240498543
4.32765197754
Final summary:
{'ClassicBig': 2958, 'ClassicSmall': 2958, 'Tree': 2958}
max=5.617558 min=3.869602
This was with Python 2.6. I then ran it for awhile (though not nearly as
long - just 5-10 minutes) with Python 2.5.
First few prints:
4.50398087502
3.73511385918
3.63765382767
4.04779291153
3.87206196785
3.73774003983
3.62571001053
4.02851200104
3.90740704536
3.87872505188
3.71122288704
4.11415696144
3.95573282242
Last few prints:
5.18314695358
5.6188788414
5.40970396996
5.31133294106
5.14807605743
5.70623922348
5.47632288933
5.46225881577
5.42709779739
5.80780506134
5.53798508644
5.43349313736
5.5712749958
5.91922187805
Summary:
{'ClassicBig': 5101922, 'ClassicSmall': 5101922, 'Tree': 5101922}
max=6.006355 min=3.625710
It seems that whatever problem you saw in 2.5 is fixed in 2.6.
Skip
--
http://mail.python.org/mailman/listinfo/python-list
Recursion, generate all pyramid-paths, not working
http://projecteuler.net/index.php?section=problems&id=18 def recur(tree, pos): if not tree: return [] else: return [[tree[0][pos]] + recur(tree[1:], pos)] + \ [[tree[0][pos]] + recur(tree[1:], pos+1)] i have a list with [[1],[2,3],[4,5,6],[7,8,9,10]] it i a pyramid so first is 1 then 23 then 456 thrn 78910 from one can go to 2 or 3. from 2 to 4or5, from 3 to 5 or 6. i want to generate every path and compute the cost. But I can't get the recursion right. It generates all the paths kind of but not in a matter i want to. also having to return after each other doesnt do anything right? like so: return [[tree[0][pos]] + recur(tree[1:], pos)] return [[tree[0][pos]] + recur(tree[1:], pos+1)] -- http://mail.python.org/mailman/listinfo/python-list
csv files for download
I need to be able to offer a client "click to download" functionality on their website. Generating the data to provide to them is not an issue but I want them to be able to click a button and have the response be sent to a csv file which they are prompted to download. Can someone point me in the right direction how to do this in python. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: garbage collector and slowdown (guillaume weymeskirch)
guillaume weymeskirch wrote: Hello everybody, To test the python 2.5 garbage collector, I wrote a trivial script allocating dummy objects of various sizes, then forgetting them in a loop. The garbage collector seems working well, limiting the memory used. But I've noticed a near linear slowdown of the execution : after a few minutes - and several millions of allocated and freed objects, each iteration take more and more time to execute. On a related note, there have been past threads reporting that allocating and freeing increasingly long arrays, especially of tuples (as I remember) can take more than linearly increasing time. The solution was to turn off gc during the allocation phase so it did not get triggered and spend increasing long times searching for collectible objects when there were not any. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
Peter Otten <[EMAIL PROTECTED]> writes: > Nick Craig-Wood wrote: > >> So just remove the parentheses and you'll be fine. > > No, you change the function signature in the process. > > f = lambda (a, b): a*b > > is equivalent to > > def f((a, b)): # double parens >return a*b > > and called as f(arg) where arg is an iterable with two items. > > In 3.0 it has to be rewritten as > > def f(ab): > a, b = ab > return a*b > > i. e. it needs a statement and an expression and is therefore no > longer suitable for a lambda. Exactly! Maybe it is the callback-heavy programming style encouraged by Twisted that is at fault here, but I quite like it and am sorry to see it made more difficult. A somewhat related question: do I pay a performance penalty when I let a function define an inner function like this: def foo(): def bar() ... bar() compared to just defining it once outside: def bar(): ... def foo(): ... bar() I'm thinking that each execution of the first foo could spend a little time defining a new bar each time, or is that not how things work? I realize that defining bar as an inner function has the advantage of being able to see variables in the namespace of foo. -- Martin Geisler VIFF (Virtual Ideal Functionality Framework) brings easy and efficient SMPC (Secure Multi-Party Computation) to Python. See: http://viff.dk/. pgp8h0HQCkWkD.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Recursion, generate all pyramid-paths, not working
process wrote:
http://projecteuler.net/index.php?section=problems&id=18
def recur(tree, pos):
if not tree:
return []
else:
return [[tree[0][pos]] + recur(tree[1:], pos)] + \
[[tree[0][pos]] + recur(tree[1:], pos+1)]
The backslash is not needed here or anytime there is an open (,[,or {.
Note that tree[0][pos] unless -n <= pos < n where n = len(tree)
i have a list with [[1],[2,3],[4,5,6],[7,8,9,10]]
it i a pyramid so first is 1 then 23 then 456 thrn 78910
from one can go to 2 or 3. from 2 to 4or5, from 3 to 5 or 6.
i want to generate every path and compute the cost.
But I can't get the recursion right. It generates all the paths kind
of but not in a matter i want to.
I do not understand what function you are trying to compute from the
verbal description. It is generally best to give a specific example(s)
of what output you expect, as well as what you got.
recur([]) = []
recur([[1]],pos) = ? do you start with pos = 0?
What do you mean by 'kind of' and 'not in a manner I want'? We are not
mind (want) readers ;-).
also having to return after each other doesnt do anything right?
like so:
return [[tree[0][pos]] + recur(tree[1:], pos)]
return [[tree[0][pos]] + recur(tree[1:], pos+1)]
Useless. The second line is never executed.
tjr
--
http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
Martin Geisler wrote: A somewhat related question: do I pay a performance penalty when I let a function define an inner function like this: def foo(): def bar() ... bar() Some. The *code* for the body of bar is compiled as part of compiling the body of foo, but each call of foo creates a new *function* object. compared to just defining it once outside: def bar(): ... def foo(): ... bar() I'm thinking that each execution of the first foo could spend a little time defining a new bar each time, or is that not how things work? I realize that defining bar as an inner function has the advantage of being able to see variables in the namespace of foo. The alternative is to pass in the value(s) needed. tjr -- http://mail.python.org/mailman/listinfo/python-list
nested popen4
Hi all, I know it's a kind of bizarre question but here I go: I want to execute an script using popen4. This script executes a command in turn using popen4 too. The first one throws a 256 exit code. Any suggestions? Thanks J. -- http://mail.python.org/mailman/listinfo/python-list
Re: nested popen4
julian wrote: Hi all, I know it's a kind of bizarre question but here I go: I want to execute an script using popen4. This script executes a command in turn using popen4 too. The first one throws a 256 exit code. Any suggestions? Popen4 is gone in 3.0 and I presume deprecated in 2.6, replaced in both by new subprocess module. Latter might work better for you. -- http://mail.python.org/mailman/listinfo/python-list
Re: del and sets proposal
Larry Bates a écrit :
You can do the following:
a = [1,2,3,4,5]
del a[0]
and
a = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5'}
del a[1]
why doesn't it work the same for sets (particularly since sets are based
on a dictionary)?
a = set([1,2,3,4,5])
del a[1]
>
Yes I know that sets have a remove method (like lists), but since
dictionaries don't have a remove method, shouldn't sets behave like more
like dictionaries and less like lists? IMHO del for sets is quite
intuitive.
For lists, del a[x] means 'remove item at index x'. For dicts, del a[x]
means 'remove key:value pair which key is x'. In both cases, del a[x] is
meaningful because a[x] is meaningful. Sets are neither ordered nor
indexed, and can't be subscripted. So "del aset[x]" is IMHO as
meaningless as 'aset[x]' is. Using this syntax for an operation which
semantic is the same as alist.remove(x) would be at best inconsistent
and confusing.
--
http://mail.python.org/mailman/listinfo/python-list
Re: csv files for download
Bobby Roberts wrote in news:cdc29298-d005-4804-b407-81ecaf6bb1b4@
2g2000hsn.googlegroups.com in comp.lang.python:
> I need to be able to offer a client "click to download" functionality
> on their website. Generating the data to provide to them is not an
> issue but I want them to be able to click a button and have the
> response be sent to a csv file which they are prompted to download.
> Can someone point me in the right direction how to do this in python.
> Thanks in advance.
Assuming your using WSGI (you don't say) it would be something like this:
def wsgi( environ, start_response ):
start_response( '200 OK', [
('Content-Type','text/csv'),
('Content-Disposition', 'attachment; filename=whatever.csv')
])
...
If your using cgi it will be something like:
print "Content-Type: text/csv"
print 'Content-Disposition: attachment; filename=whatever.csv'
print
...
http://search.yahoo.com/search?p=Content-Disposition
http://www.python.org/doc/2.5.2/lib/cgi-intro.html
Rob.
--
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: self signing a py2exe windows executable
William Heath wrote:
> Hi All,
> I thought I sent an email to the list regarding a need I have to self sign
> a
> py2exe windows executable. Does anyone know how to do that?
>
> -Tim
>
You can use capicom to sign an executable (or even a .pyd):
import win32com.client
s=win32com.client.Dispatch('capicom.signedcode')
s.FileName='yourexecutable.exe'
s.Sign(None)
s.Timestamp('http://timestamp.verisign.com/scripts/timstamp.dll')
Roger
--
http://mail.python.org/mailman/listinfo/python-list
Re: encoding ascii data for xml
On Sat, 04 Oct 2008 12:18:13 -0700 Dennis Lee Bieber <[EMAIL PROTECTED]> wrote: > On 4 Oct 2008 06:59:20 GMT, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > > > not whitespace. BTW `chr(3)` isn't "end of line" but "end of text" (ETX). > > > Hmm, think I'll need to look up an ASCII chart -- I seem to recall > ETX as "end of transmission" Nope, Marc is correct. EOT, chr(4), is "end of transmission." -- D'Arcy J.M. Cain <[EMAIL PROTECTED]> | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: lint for Python?
On Oct 3, 5:38 pm, Pat <[EMAIL PROTECTED]> wrote: > I've been searching for a good multi-module lint checker for Python and > I haven't found one yet. > > Pylint does a decent job at checking for errors only within a single module. > > Here's one of my problems. I have two modules. > > In module one, I have a function: > > def foo( host, userid, password ): > pass > > In module two, I call that function: > > foo( userid, password) > > lint doesn't find that error and it won't be caught until it's called > while the program is running. I don't want that error found at 3AM. > > I've never used a language that didn't catch that type of error. I'm > quite surprised that Python is being used by a number of major > companies. How you catch these types of errors? > > I've spoken to tech support at Wing and they weren't aware of a > multi-module lint but they're considering putting their own lint into > their IDE. > > Thank you I doubt you will find a tool that can find that kind of error(at least not reliably), python is a highly dynamic language, and so foo() can easily be reassigned at any point in the execution. -- http://mail.python.org/mailman/listinfo/python-list
Re: self signing a py2exe windows executable
Hi! Very interesting. Roger, thank you very much super enormous!!! @-salutations -- Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
Dennis Lee Bieber <[EMAIL PROTECTED]> writes: > On Sat, 04 Oct 2008 13:14:40 +0200, Peter Otten <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: > >> In 3.0 it has to be rewritten as >> >> def f(ab): >> a, b = ab >> return a*b >> >> i. e. it needs a statement and an expression and is therefore no >> longer suitable for a lambda. >> > > Given that most lambda's are rather short, is it really that > much of a pain to just use (for the above example) ab[0]*ab[1] without > unpacking? Well -- it is because the code used to be short and sweet that it feels ugly to me to change lambda (a, b): a * b into lambda ab: ab[0] * ab[1] The first looks perfect -- it matches the math behind the code that I am writing. The second does not look so nice. From reading the PEP-3113 I got the impression that the author thought that this feature was unused and didn't matter. With this I wish to say that it matters to me. -- Martin Geisler VIFF (Virtual Ideal Functionality Framework) brings easy and efficient SMPC (Secure Multi-Party Computation) to Python. See: http://viff.dk/. pgpd5Qp426xvo.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Professional quality scripts/code
On Oct 3, 1:26 am, Bruno Desthuilliers wrote: > robean a crit : > > > I have been learning Python for the last 3 months or so and I have a > > working (but somewhat patchy) sense of the the language. I've been > > using a couple of the more popular Python books as well as online > > resources. > > > A question for experienced Python programmers: can you recommend > > resources where I can look at high quality Python code and scripts? > > Well... Not everything is 'high quality' in it[1], but why not start > with the stdlib ? Most of it is pure Python, opensource code and is > already installed on your machine, isn't it ?-) > > [1] IIRC, last time I had a look at the zipfile module's code, it was > more of a Q&D hack than anything else - now as long as it works fine for > what I do with it and I don't have to maintain it, well, that's fine. > > > I've spent some time athttp://code.activestate.com/recipes/but am > > concerned that the quality of what is posted there can be somewhat hit > > and miss. > > Indeed. > > > What I have in mind is a site like cpan, where one can look > > at the actual source code of many of the modules and learn a thing or > > two about idiomatic Perl programming from studying the better ones. > > Any sites like that for Python? > > Lurking here is probably a good way to see a lot of code reviews. And > even possibly to submit snippets of your own code to review. Some (if > not most) of us here like to show how good we are at improving the poor > newbies code !-) Many thanks, Mike and Bruno, The resources you mention are exactly the kind of stuff I was looking for. Soon enough I hope to give all of you many chances to improve this poor newbie's code...! - Robean -- http://mail.python.org/mailman/listinfo/python-list
having a function in a separate file
Hi all, I have a very basic question. I was wondering if I can have a function in a separate file and in my main file sort of import that function and if so are all the variables local and if I need any variables in the function I should give it as an input? I need to call a function in many different tasks and I am improving that function so I prefer to make the changes only once. If you have any example I really appreciate it. Thanks a lot, Maryam -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
On Sat, 04 Oct 2008 22:57:23 +0200, Martin Geisler wrote: > Dennis Lee Bieber <[EMAIL PROTECTED]> writes: > >> On Sat, 04 Oct 2008 13:14:40 +0200, Peter Otten <[EMAIL PROTECTED]> >> declaimed the following in comp.lang.python: >> >>> In 3.0 it has to be rewritten as >>> >>> def f(ab): >>> a, b = ab >>> return a*b >>> >>> i. e. it needs a statement and an expression and is therefore no >>> longer suitable for a lambda. >>> >>> >> Given that most lambda's are rather short, is it really that >> much of a pain to just use (for the above example) ab[0]*ab[1] without >> unpacking? > > Well -- it is because the code used to be short and sweet that it feels > ugly to me to change > > lambda (a, b): a * b > > into > > lambda ab: ab[0] * ab[1] > > The first looks perfect -- it matches the math behind the code that I am > writing. The second does not look so nice. Here's another alternative. Compare: >>> x = (2, 3) >>> (lambda (a,b): a*b)(x) 6 with this: >>> (lambda a,b: a*b)(*x) 6 > From reading the PEP-3113 I got the impression that the author thought > that this feature was unused and didn't matter. With this I wish to say > that it matters to me. Alas, I think it's too late. I feel your pain. The final release of Python 3.0 hasn't been made yet. If you really care strongly about this, you could write to the python-dev mailing list and ask if it is worth trying to change their mind about tuple unpacking. They'll almost certainly say no, but there's a chance it might be reverted in 3.1. A tiny chance. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Tuple parameter unpacking in 3.x
On Sat, 04 Oct 2008 17:07:14 +0200, Martin Geisler wrote:
> A somewhat related question: do I pay a performance penalty when I let a
> function define an inner function like this:
>
> def foo():
>
> def bar()
> ...
>
> bar()
>
> compared to just defining it once outside:
>
> def bar():
> ...
>
> def foo():
> ...
> bar()
>
> I'm thinking that each execution of the first foo could spend a little
> time defining a new bar each time, or is that not how things work?
>
> I realize that defining bar as an inner function has the advantage of
> being able to see variables in the namespace of foo.
That is the main advantage, followed by reducing namespace pollution, but
yes there is a very small performance penalty.
>>> def outer(x):
... return x+1
...
>>> def func(x):
... return outer(x+1)
...
>>>
>>> def func2(x):
... def inner(x):
... return x+1
... return inner(x+1)
...
>>> assert func(37) == func2(37)
>>> from timeit import Timer
>>> t1 = Timer('func(23)', 'from __main__ import func')
>>> t2 = Timer('func2(23)', 'from __main__ import func2')
>>> t1.repeat()
[1.5711719989776611, 0.82663798332214355, 0.82708191871643066]
>>> t2.repeat()
[1.8273210525512695, 1.1913230419158936, 1.1786220073699951]
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Re: garbage collector and slowdown (guillaume weymeskirch)
On Sat, 04 Oct 2008 10:57:25 -0400, Terry Reedy wrote: > guillaume weymeskirch wrote: >> Hello everybody, >> >> >> To test the python 2.5 garbage collector, I wrote a trivial script >> allocating dummy objects of various sizes, then forgetting them in a >> loop. The garbage collector seems working well, limiting the memory >> used. >> >> But I've noticed a near linear slowdown of the execution : after a few >> minutes - and several millions of allocated and freed objects, each >> iteration take more and more time to execute. > > On a related note, there have been past threads reporting that > allocating and freeing increasingly long arrays, especially of tuples > (as I remember) can take more than linearly increasing time. The > solution was to turn off gc during the allocation phase so it did not > get triggered and spend increasing long times searching for collectible > objects when there were not any. If you're talking about this thread: http://groups.google.co.uk/group/comp.lang.python/browse_thread/thread/77e5d747c4a727cb it turned out that gc wasn't involved, it was tuples and dicts, and it seemed to be hardware specific. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: del and sets proposal
On Sat, 04 Oct 2008 18:36:28 +0200, Bruno Desthuilliers wrote: >> Yes I know that sets have a remove method (like lists), but since >> dictionaries don't have a remove method, shouldn't sets behave like >> more like dictionaries and less like lists? IMHO del for sets is quite >> intuitive. > > For lists, del a[x] means 'remove item at index x'. For dicts, del a[x] > means 'remove key:value pair which key is x'. In both cases, del a[x] is > meaningful because a[x] is meaningful. Sets are neither ordered nor > indexed, and can't be subscripted. So "del aset[x]" is IMHO as > meaningless as 'aset[x]' is. Using this syntax for an operation which > semantic is the same as alist.remove(x) would be at best inconsistent > and confusing. Consider the mutable built-in collections set, dict and list, and how they remove elements: aset.remove(x) alist.remove(x) del adict[x] Would it really be "confusing" if sets used the same interface as dicts use? I don't think so. What else could "del aset[x]" mean other than "delete element x"? Lists are the odd one out, because del alist[x] is used to remove the element at position x, rather than removing an element x. But because sets are unordered, there's no ambiguity between element x and the element at position x. In that regard, sets are just like dicts, and it is a perfectly reasonable position to take that hypothetically sets could use "del aset[x]" instead of "aset.remove(x)". But on the other hand, if you can write "del aset[x]", why can't you write "y = aset[x]" or "aset[x] = z"? Neither of these seem useful or sensible, although at a stretch we could make "y = aset[x]" equivalent to "y = x in aset". Although there's no technical reason why sets can't have a __delitem__ method but no __getitem__ and __setitem__ methods, for consistency it does seem rather unusual. All this may seem academic, but consider the Bag (Multiset) data type. With a Bag, you need to have two commands: "remove element x entirely", and "remove one copy of element x only". To my mind, the natural API for that is: del abag[x] # remove element x entirely abag.remove(x) # remove one copy of element x only -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: What is not objects in Python?
Marc 'BlackJack' Rintsch wrote:
On Fri, 03 Oct 2008 19:10:27 +1200, greg wrote:
(BTW, try doing that with the x.len() notation!)
def size(obj):
return obj.len()
or
size = operator.methodcaller('len')
No, what I meant was that if the normal way of getting
the len of something were to write x.len() instead of
len(x), you wouldn't be able to globally substitute
that syntax with x.size().
--
Greg
--
http://mail.python.org/mailman/listinfo/python-list
Re: One class per file?
Bruno Desthuilliers wrote: Nope. But IIRC, one-class-per-file helps saving on compile/link time. A problem we don't have with dynamic languages !-) That's mostly true. Although I've noticed that if I have a very large .py file, it can take a noticeable number of moments to regenerate the .pyc after I've changed something. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: closures and dynamic binding
Hrvoje Niksic wrote: Common Lisp behaves similar to Python in this regard: * (loop for i from 0 to 2 collect (lambda () i)) I wouldn't call the CL loop macro the equivalent of a for-loop. It's more like a while-loop. The Lisp equivalent of a Python for-loop would be to use one of the mapping functions. Then, since the loop body is a lambda, you get a new scope for each iteration automatically. I don't think it has anything to do with variable leaking out of the loop. That's right, it doesn't. If I were designing a for-loop from scratch, I wouldn't let it leak, but that part seems to be necessary for backwards compatibility. > In contrast, Scheme regards iteration as a special case of recursion, and R5RS "do" prescribes assigning loop variables to "fresh locations" to match what recursion normally does. I'd say it prescribes that because it's useful behaviour, not because it has anything to do with recursion. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Earn 1000$ In Every Month From ONLINE.
hai, i am srinu from india. i am sending a blog url for yours use. click on the blog and get more information to choose yours job. the blog url is: http://earnmonthlyincome.blogspot.com/ goodluck -- http://mail.python.org/mailman/listinfo/python-list
Replacement in unicodestrings?
Dear all,
could somebody please just put an end to the unicode mysery I'm in,
men... The situation is that I have a Tkinter program that let's the
user enter data in some Entries and this data needs to be transformed
to the encoding compatible with an .rtf-file. In fact I only need to
do some of the usual symbols like ë etc.
Here's the function that I am using:
def pythonUnicodeToRTFAscii(self,s):
if isinstance(s,str):
return s
s_str=repr(s.encode('UTF-8'))
replDic={'\xc3\xa0':"\\'e0",'\xc3\xa4':"\\'e4",'\xc3\xa1':"\
\'e1",
'\xc3\xa8':"\\'e8",'\xc3\xab':"\\'eb",'\xc3\xa9':"\
\'e9",
'\xc3\xb2':"\\'f2",'\xc3\xb6':"\\'f6",'\xc3\xb3':"\
\'f3",
'\xe2\x82\xac':"\\'80"}
for k in replDic.keys():
if repr(k) in s_str:
s_str=s_str.replace(repr(k),replDic[k])
return s_str
So replDic represents the mapping from one encoding to the other. Now,
if I enter e.g. 'Arjën' in the Entry, then s_str in the above function
becomes 'Arj\xc3\xabn' and since replDic contains the key \xc3\xab I
would expect the replacement in the final lines of the function to
kick in. This however doesn't happen, there's no match.
However interactive:
>>> '\xc3\xab' in 'Arj\xc3\xabn'
True
I just don't get it, what's the difference? Is the above anyhow the
best way to attack such a problem?
Thanks & best wishes, Kees
--
http://mail.python.org/mailman/listinfo/python-list
Re: lint for Python?
On Oct 3, 4:38 pm, Pat <[EMAIL PROTECTED]> wrote: > I've been searching for a good multi-module lint checker for Python and > I haven't found one yet. > > Pylint does a decent job at checking for errors only within a single module. > > Here's one of my problems. I have two modules. > > In module one, I have a function: > > def foo( host, userid, password ): > pass > > In module two, I call that function: > > foo( userid, password) > > lint doesn't find that error and it won't be caught until it's called > while the program is running. I don't want that error found at 3AM. > > I've never used a language that didn't catch that type of error. I'm > quite surprised that Python is being used by a number of major > companies. How you catch these types of errors? You have to write extensive tests for your code. Not only does this catch trivial errors like the above, but it finds logic errors in your implementation. > > I've spoken to tech support at Wing and they weren't aware of a > multi-module lint but they're considering putting their own lint into > their IDE. > > Thank you -- http://mail.python.org/mailman/listinfo/python-list
Re: having a function in a separate file
Maryam Saeedi wrote: Hi all, I have a very basic question. I was wondering if I can have a function in a separate file Yes, the separate file become a module. and in my main file sort of import that function and Yes. either of import somemod # and use somemod.func from somemod import func # and use func if so are all the variables local The function can access built-in objects and objects in the module it is defined in. Otherwise, it has locals just as usual. and if I need any variables in the function I should give it as an input? Yes. I need to call a function in many different tasks and I am improving that function so I prefer to make the changes only once. If you have any example I really appreciate it. Look at Python coded files in the the standard library. Look in .../Pythonx.y/Lib to find them. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: One class per file?
[HCB] The book "Code Complete" recommends that you put only one class in a source file, which seems a bit extreme for me. IMO this is a misunderstanding (by the author). In Python, a file is not equivalent to a class, it is equivalent to a module. A module might contain a single class or many, just as a class might contain a single method or many. You can tell that files are not classes, because you have have variables, methods, etc at the module level, outside of class definitions. If all of the module's functionality is in a single class, then of course the file only contains one class. If the module's functionality is split over many classes (or no classes at all), then the file will not contain exactly one class. Rigidly putting every class in a separate file would mean that the next level higher than classes wouldn't be modules, it would be packages. [Roy Smith] Consider this. You're slogging through some code in a large project trying to debug a problem when you come upon the line (in pseudo-code): foo = SomeClass::SomeFunction(bar) You want to go look at the source for SomeClass. What file do you open to find it? If you follow the "one class per file" rule, the answer is easy; it's in SomeClass.xxx! Alternatively, most IDEs will let you go to the source very simply. If you don't have that facility available, then you just do "import X;print X.__file__". Cheers, Tony -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem: neither urllib2.quote nor urllib.quote encode the unicode strings arguments
On 10月4日, 上午5时38分, "Valery Khamenya" <[EMAIL PROTECTED]> wrote:
> Hi all
>
> things like urllib.quote(u"пиво Müller ") fail with error message:
> : u'\u043f'
>
> Similarly with urllib2.
>
> Anyone got a hint?? I need it to form the URI containing non-ascii chars.
> thanks in advance,
> best regards
> --
> Valery
don't unicode the str, just urllib.quote("пиво Müller ")
--
http://mail.python.org/mailman/listinfo/python-list
Re: how best to use a dictionary in this function?
Terrence Brannon <[EMAIL PROTECTED]> wrote:
>
>Now, I improved this function this way:
>
>def calc_profit(std_clicks, vip_clicks, ad_rate=200,
>upline_status=None):
>clicks = {}
>clicks['std'] = std_clicks
>clicks['vip'] = vip_clicks
You can also write it this way.
clicks = {
'std': std_clicks,
'vid': vip_clicks
}
>I know there is something like *args, or **args, but since
>docs.python.org is down, I cant check.
The problem with using **args is that you can no longer pass your
parameters positionally. Callers of calc_profit would have to use named
parameters. It also makes your default arguments a bit less natural.
--
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Replacement in unicodestrings?
> s_str=repr(s.encode('UTF-8'))
It would be easier to encode this in cp1252 here, as this is apparently
the encoding that you want to use in the RTF file, too. You could then
loop over the string, replacing all bytes >= 128 with \\'%.2x
As yet another alternative, you could create a Unicode error handler
(call it 'rtf'), and then do
return s.encode('ascii', errors='rtf')
> replDic={'\xc3\xa0':"\\'e0",'\xc3\xa4':"\\'e4",'\xc3\xa1':"\
> \'e1",
> '\xc3\xa8':"\\'e8",'\xc3\xab':"\\'eb",'\xc3\xa9':"\
> \'e9",
> '\xc3\xb2':"\\'f2",'\xc3\xb6':"\\'f6",'\xc3\xb3':"\
> \'f3",
> '\xe2\x82\xac':"\\'80"}
> for k in replDic.keys():
> if repr(k) in s_str:
> s_str=s_str.replace(repr(k),replDic[k])
> return s_str
>
> However interactive:
>
'\xc3\xab' in 'Arj\xc3\xabn'
> True
>
> I just don't get it, what's the difference?
It's the repr():
py> '\xc3\xab' in 'Arj\xc3\xabn'
True
py> repr('\xc3\xab') in repr('Arj\xc3\xabn')
False
py> repr('\xc3\xab')
"'\\xc3\\xab'"
py> repr('Arj\xc3\xabn')
"'Arj\\xc3\\xabn'"
repr('\xc3\xab') starts with an apostrophe, which doesn't
appear before the \\xc3 in repr('Arj\xc3\xabn').
HTH,
Martin
--
http://mail.python.org/mailman/listinfo/python-list
sftp problem!
I want to use sftp from paramiko to copy a file from a windows machine to a Linux in the network, I use this code : host = "LinuxComputerName" (or its Ip) port = 22 transport = paramiko.Transport((host, port)) password = "LinuxComputerPassword" username = "LinuxComputerUserName" transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport) filepath = '/home/test.jpg' localpath = '/home/test.jpg' sftp.get(filepath, localpath) sftp.close() transport.close() and after runing this code I get this error: socket.error: (10061, 'Connection refused') What is wrong with this code? please help me. -- View this message in context: http://www.nabble.com/sftp-problem%21-tp19821106p19821106.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
