Problem with NAT Traversal
Hi, All I'm working with a small program to realize P2P file transfer. Therefore, I have to accomplish the function of NAT traversal. From the searching result, I know that it always requires a public server to initialize the transfer, but I don't have one. Now, my idea is that, we already have many communication ways such as the gtalk, so one side of P2P file transfer program can just run a 'touch' function to know some parameters, such as the port number and the public IP address, and just display them to the users. And the users will guarantee that the other side P2P file transfer program will know these parameters. However, I don't know any function in python can achieve the 'touch' function. Any one has some experience on this? Really appreciated. Best, Hanks -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
On 26.03.2012, Steven D'Aprano wroted: >>> How can we generate a 6 digit random number from a given number ? >> what about this? >> > given_number=123456 > def rand_given_number(x): >> ... s = list(str(x)) >> ... random.shuffle(s) >> ... return int(''.join(s)) >> ... > print (rand_given_number(given_number)) >> 653421 > > > That's not very random. In fact, it is *terrible* as a random number > generator. But isn't it what the OP requested, i.e. "6 digit random number *from a given number*"? That is, a random permutation of the set of its digits? GS -- Grzegorz Staniak -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
Nikhil Verma wrote: > I want something to achieve like this :- > > def random_number(id): # I am passing it from request > # do something > return random_number > > Output > > random_number(5) > AXR670 That's normally not called a number (though it could be base 36 or similar). > One input that is a number in return you are getting 6 digit alphanumeric > string. > > I tried this > s = '%06d' % random.randint(0, 99) > > it gives : '192862' (a string ) Your question is not very clear. Can you give some more information, what you want to do with your "random" "number", and how the id argument should influence possible results, e. g. can the possible outcomes of random_number(x) and random_number(y) overlap for x != y? -- http://mail.python.org/mailman/listinfo/python-list
Re: bdb.Bdb (Debugger Base Class) / unittest Interaction (MRAB)
From: MRAB To: python-list@python.org Cc: Date: Sun, 25 Mar 2012 22:14:28 +0100 Subject: Re: bdb.Bdb (Debugger Base Class) / unittest Interaction On 25/03/2012 21:42, Ami Tavory wrote: > Hello, > > I'm having some difficulties with the interaction between bdb.Bdb and > scripts which contain unittest. Following are two simplified scenarios > of a GUI debugger Gedit plugin I'm writing based on bdb.Bdb, and a > script that is being debugged by it. > > --Scenario A-- > > The script being debugged is foo.py; its content is > > print 'Hello, world!' > > > The "debugger" (a simplified version of it) is > > import bdb > > g = {} > g['__name__'] = '__main__' > g['__file__'] = 'foo.py' > statement = 'execfile("%s", %s)' % ('foo.py', str(g)) > bdb.Bdb().run(statement) > > > it indeed prints 'Hello, world'. > > --Scenario B-- > > The script being debugged is bar.py; its content is > > import unittest > > class test(unittest.TestCase): > def test(self): > print 'Hello, world!' > > def suite(): > return unittest.TestLoader().**loadTestsFromTestCase(test) > > if __name__ == '__main__': > unittest.main() > > > The "debugger" is identical to before, but with 'foo.py' replaced by > 'bar.py'. It does not print 'Hello, world'. In fact, it prints > > --**--**-- > Ran 0 tests in 0.000s > > > However: > > 1. Running bar.py as a script, indeed prints 'Hello, world'. > 2. Running pdb (which internally uses bdb.Bdb) on bar.py, also indeed >prints 'Hello, world'. > > I've looked at the code of pdb to see what I'm doing wrong (at least > in the second case), but couldn't find the difference. Help would be > much appreciated. > > With Python 2.7 I'm getting this: Hello, world! . --**--**-- Ran 1 test in 0.000s OK -- Hi, Many thanks for your reply. Unfortunately, I'm consistently getting a different output than yours on scenario B, namely -- Ran 0 tests in 0.000s This happens on separate computers, one running 2.6, and one running 2.7. Is it possible to ask whether you ran in scenario B bar.py or the "debugger" running on bar.py? Running it through the "debugger" causes the problem; without it it works fine. If the problem is only on my (two separate) installations, is there something I can do to specify the problem further? Thanks & Bye, Ami -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm wrote: > On 3/25/2012 15:48, Tim Chase wrote: >> >> The old curmudgeon in me likes the Pascal method of using "=" for >> equality-testing, and ":=" for assignment which feels a little closer to >> mathematical use of "=". > > > Unfortunately, ":=" means "is defined as" in mathematics. The "right" > operator would have been "<-". "Is defined as" is actually pretty reasonable. "Define this to be that" is a common way to speak about assignment. Its only difference is the present tense. For example, in Python, "def" stands for "define", but we can overwrite previous definitions:: def f(x): return x def f(x): return 2 f(3) == 2 In fact, in pretty every programming language that I know of with a "define" assignment verb, this is so. For example, in Scheme, x is 2 at the end:: (define x 1) (define x 2) x -- Devin -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
* Nikhil Verma [2012-03-26 08:49]: > Hi > > I want something to achieve like this :- > > def random_number(id): # I am passing it from request > # do something > return random_number > > Output > > random_number(5) > AXR670 > > One input that is a number in return you are getting 6 digit alphanumeric > string. > > I tried this > s = '%06d' % random.randint(0, 99) > > it gives : '192862' (a string ) > > Thanks in advance. ah - so I misunderstood - I thought you want a permutation of a given 6-digit number It's still not quite clear to me what role 'id' is playing ... so let's check this one; and Steven, who is maybe more experienced than I am will help us ufrther >>> import random, string >>> def random_number(id): ... characters = list(string.ascii_lowercase + ... string.ascii_uppercase + ... string.digits) ... coll_rand = [] ... for i in range(6): ... random.shuffle(characters) ... coll_rand.append(characters[0]) ... return ''.join(coll_rand) ... >>> id = 5 >>> print (random_number(id)) puMHCr >>> regards Michael > > On Mon, Mar 26, 2012 at 12:10 PM, Michael Poeltl < > michael.poe...@univie.ac.at> wrote: > > > * Nikhil Verma [2012-03-26 08:09]: > > > Hi All > > > > > > How can we generate a 6 digit random number from a given number ? > > what about this? > > > > >>> given_number=123456 > > >>> def rand_given_number(x): > > ... s = list(str(x)) > > ... random.shuffle(s) > > ... return int(''.join(s)) > > ... > > >>> print (rand_given_number(given_number)) > > 653421 > > > > > > -- > Regards > Nikhil Verma > +91-958-273-3156 -- Michael Poeltl Computational Materials Physics voice: +43-1-4277-51409 Univ. Wien, Sensengasse 8/12 fax: +43-1-4277-9514 (or 9513) A-1090 Wien, AUSTRIA cmp.mpi.univie.ac.at --- ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12 --- -- http://mail.python.org/mailman/listinfo/python-list
Re: Stream programming
Kiuhnm wrote: [snip] numbers - push - avrg - 'med' - pop - filter(lt('med'), ge('med'))\ - ['same', 'same'] - streams(cat) - 'same' It reads as "take a list of numbers - save it - compute the average and named it 'med' - restore the flow - create two streams which have, respect., the numbers less than 'med' and those greater or equal to 'med' - do the /entire/ 'same' process on each one of the two streams - concat the resulting streams - name all this /entire/ process 'same'. Not readable enough? Replace 'same' with 'qsort'. Is that readable or am I going crazy? [note: that's a rhetorical question whose answer is "That's very readable!"] Kiuhnm Here's a rhetorical answer to your question : whatever you're taking, I want some ! JM -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
On 3/26/12 8:50 AM, Grzegorz Staniak wrote: On 26.03.2012, Steven D'Aprano wroted: How can we generate a 6 digit random number from a given number ? what about this? given_number=123456 def rand_given_number(x): ... s = list(str(x)) ... random.shuffle(s) ... return int(''.join(s)) ... print (rand_given_number(given_number)) 653421 That's not very random. In fact, it is *terrible* as a random number generator. But isn't it what the OP requested, i.e. "6 digit random number *from a given number*"? That is, a random permutation of the set of its digits? I would consider that to be a very odd interpretation of that request. But it *is* an extraordinarily vague request. I'm not sure if even the OP knows what he wants. I suspect he really wants something like a hash. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: How to decide if a object is instancemethod?
Jon Clements wrote: On Wednesday, 14 March 2012 13:28:58 UTC, Cosmia Luna wrote: class Foo(object): def bar(self): return 'Something' func = Foo().bar if type(func) == : # This should be always true pass # do something here What should type at ? Thanks Cosmia import inspect if inspect.ismethod(foo): # ... Will return True if foo is a bound method. hth Jon another alternative : import types if type(func) == types.MethodType: pass or possibly better if isinstance(func, types.MethodType): pass JM -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
Hi Thanks Michael I want exactly wanted this. Great def random_number(id) ...characters = list(string.ascii_lowercase +string.ascii_uppercase +string.digits) I used this this earlier and tried then by using choice . This is great. On Mon, Mar 26, 2012 at 2:54 PM, Michael Poeltl wrote: > * Nikhil Verma [2012-03-26 08:49]: > > Hi > > > > I want something to achieve like this :- > > > > def random_number(id): # I am passing it from request > > # do something > > return random_number > > > > Output > > > > random_number(5) > > AXR670 > > > > One input that is a number in return you are getting 6 digit alphanumeric > > string. > > > > I tried this > > s = '%06d' % random.randint(0, 99) > > > > it gives : '192862' (a string ) > > > > Thanks in advance. > ah - so I misunderstood - I thought you want a permutation of a given > 6-digit number > > It's still not quite clear to me what role 'id' is playing ... so let's > check this one; > and Steven, who is maybe more experienced than I am will help us ufrther > > >>> import random, string > >>> def random_number(id): > ... characters = list(string.ascii_lowercase + > ... string.ascii_uppercase + > ... string.digits) > ... coll_rand = [] > ... for i in range(6): > ... random.shuffle(characters) > ... coll_rand.append(characters[0]) > ... return ''.join(coll_rand) > ... > >>> id = 5 > >>> print (random_number(id)) > puMHCr > >>> > > regards > Michael > > > > > > On Mon, Mar 26, 2012 at 12:10 PM, Michael Poeltl < > > michael.poe...@univie.ac.at> wrote: > > > > > * Nikhil Verma [2012-03-26 08:09]: > > > > Hi All > > > > > > > > How can we generate a 6 digit random number from a given number ? > > > what about this? > > > > > > >>> given_number=123456 > > > >>> def rand_given_number(x): > > > ... s = list(str(x)) > > > ... random.shuffle(s) > > > ... return int(''.join(s)) > > > ... > > > >>> print (rand_given_number(given_number)) > > > 653421 > > > > > > > > > > > -- > > Regards > > Nikhil Verma > > +91-958-273-3156 > > > -- > Michael Poeltl > Computational Materials Physics voice: +43-1-4277-51409 > Univ. Wien, Sensengasse 8/12 fax: +43-1-4277-9514 (or 9513) > A-1090 Wien, AUSTRIA cmp.mpi.univie.ac.at > > --- > ubuntu-11.10 | vim-7.3 | python-3.2.2 | mutt-1.5.21 | elinks-0.12 > > --- > -- Regards Nikhil Verma +91-958-273-3156 -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
On 3/26/12 10:45 AM, Nikhil Verma wrote: Hi Thanks Michael I want exactly wanted this. Great def random_number(id) ...characters = list(string.ascii_lowercase +string.ascii_uppercase +string.digits) I used this this earlier and tried then by using choice . This is great. Note that the id parameter is not used by the function at all. If you call this function multiple times with the same input, you will get different results each time. Is that what you want? What role did you expect the id parameter to play? On Mon, Mar 26, 2012 at 2:54 PM, Michael Poeltl mailto:michael.poe...@univie.ac.at>> wrote: It's still not quite clear to me what role 'id' is playing ... so let's check this one; and Steven, who is maybe more experienced than I am will help us ufrther >>> import random, string >>> def random_number(id): ... characters = list(string.ascii_lowercase + ... string.ascii_uppercase + ... string.digits) ... coll_rand = [] ... for i in range(6): ... random.shuffle(characters) ... coll_rand.append(characters[0]) ... return ''.join(coll_rand) ... >>> id = 5 >>> print (random_number(id)) puMHCr >>> -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On 03/25/12 17:59, Dennis Lee Bieber wrote: On Sun, 25 Mar 2012 08:48:31 -0500, Tim Chase Yeah, it has the same structure internally, but I'm somewhat surprised that the DB connection object doesn't have an __iter__() that does something like this automatically under the covers. I believe being able to use the connection object directly for queries is considered a short-cut feature... If you use the longer form con = db.connect() cur = con.cursor() the cursor object, in all that I've worked with, does function for iteration for rec in cur: #do stuff Interesting. Either this is something special for a particular DB back-end, or has been added since I went hunting (back with mxODBC and Python2.4). I wouldn't be surprised if the latter was the case, as my code is nigh identical to yours. conn = db.DriverConnect(connection_string) cursor = conn.cursor() cursor.execute(sql, params) for row in cursor: # in the above 2.4 + mxODBC, this fails process(row) I'd be interested to know the underlying implementation's efficiency, hopefully using .fetchmany() under the hood. My understanding is that the .fetchmany() loop is the best way to do it, as the .fetchone() gets chatty with the DB (but is more efficient if you only need one row from a multi-result query), and the .fetchall() can blow out memory on large datasets. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On 3/26/2012 10:52, Devin Jeanpierre wrote: On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm wrote: On 3/25/2012 15:48, Tim Chase wrote: The old curmudgeon in me likes the Pascal method of using "=" for equality-testing, and ":=" for assignment which feels a little closer to mathematical use of "=". Unfortunately, ":=" means "is defined as" in mathematics. The "right" operator would have been "<-". "Is defined as" is actually pretty reasonable. "Define this to be that" is a common way to speak about assignment. Its only difference is the present tense. For example, in Python, "def" stands for "define", but we can overwrite previous definitions:: def f(x): return x def f(x): return 2 f(3) == 2 In fact, in pretty every programming language that I know of with a "define" assignment verb, this is so. For example, in Scheme, x is 2 at the end:: (define x 1) (define x 2) x When you write (define x 1) (define x 2) x or, in F# and OCaml, let x = 1 let x = 2 x you're saying x = 1 { x = 2 x } You don't modify 'x': you hide it by defining another "value" (not variable) with the same name. Indeed, let x = 1 let x = 2 x is shorthand for let x = 1 in let x = 2 in x Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
Kiuhnm writes: > On 3/26/2012 10:52, Devin Jeanpierre wrote: > > On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm > > wrote: > >> On 3/25/2012 15:48, Tim Chase wrote: > >>> > >>> The old curmudgeon in me likes the Pascal method of using "=" for > >>> equality-testing, and ":=" for assignment which feels a little closer to > >>> mathematical use of "=". > >> > >> > >> Unfortunately, ":=" means "is defined as" in mathematics. The "right" > >> operator would have been "<-". > > > > > > "Is defined as" is actually pretty reasonable. "Define this to be > > that" is a common way to speak about assignment. Its only difference > > is the present tense. For example, in Python, "def" stands for > > "define", but we can overwrite previous definitions:: > > > > def f(x): return x > > def f(x): return 2 > > f(3) == 2 > > > > In fact, in pretty every programming language that I know of with a > > "define" assignment verb, this is so. For example, in Scheme, x is 2 > > at the end:: > > > > (define x 1) > > (define x 2) > > x > > When you write >(define x 1) >(define x 2) >x > or, in F# and OCaml, >let x = 1 >let x = 2 >x > you're saying >x = 1 >{ > x = 2 > x >} > You don't modify 'x': you hide it by defining another "value" (not > variable) with the same name. > Indeed, >let x = 1 >let x = 2 >x > is shorthand for >let x = 1 in >let x = 2 in >x No, Devin is right about Scheme. On "top level" re-definition is interpreted as assignment. The following mean the same: (define x 1) (define x 2) x (define x 1) (set! x 2) x Local definitions in the beginning of a "body" do not allow duplicate names at all. The following mean the same: (let () (define x 1) (define y 2) x) (letrec* ((x 1) (y 2)) x) ;letrec in older versions (not sure of R6RS) But (let () (define x 1) (define x 2) x) is still an error. Some implementations may give it a meaning. Not sure. -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
Dennis Lee Bieber wrote: > On Sun, 25 Mar 2012 19:09:12 -0400, mwil...@the-wire.com declaimed the > following in gmane.comp.python.general: > > >> Most of my database programs wind up having the boilerplate (not tested): >> >> def rowsof (cursor): >> names = [x[0] for x in cursor.description] >> r = cursor.fetchone() >> while r: >> yield dict (zip (names, r)) >> r = cursor.fetchone() >> > > In my (limited) experience, the main loop above could be replaced > with: > > for r in cursor: > yield dict(zip(names, r)) I think your experience is more recent than mine. I'll change my boilerplate next time around. Mel. -- http://mail.python.org/mailman/listinfo/python-list
Is there any difference between print 3 and print '3' in Python ?
I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Stream programming
On 3/26/2012 11:27, Jean-Michel Pichavant wrote: Kiuhnm wrote: [snip] numbers - push - avrg - 'med' - pop - filter(lt('med'), ge('med'))\ - ['same', 'same'] - streams(cat) - 'same' It reads as "take a list of numbers - save it - compute the average and named it 'med' - restore the flow - create two streams which have, respect., the numbers less than 'med' and those greater or equal to 'med' - do the /entire/ 'same' process on each one of the two streams - concat the resulting streams - name all this /entire/ process 'same'. Not readable enough? Replace 'same' with 'qsort'. Is that readable or am I going crazy? [note: that's a rhetorical question whose answer is "That's very readable!"] Kiuhnm Here's a rhetorical answer to your question : whatever you're taking, I want some ! LOL. I should have titled my post "A new obfuscation technique" then :) Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On 3/26/2012 13:13, Jussi Piitulainen wrote: Kiuhnm writes: On 3/26/2012 10:52, Devin Jeanpierre wrote: On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm wrote: On 3/25/2012 15:48, Tim Chase wrote: The old curmudgeon in me likes the Pascal method of using "=" for equality-testing, and ":=" for assignment which feels a little closer to mathematical use of "=". Unfortunately, ":=" means "is defined as" in mathematics. The "right" operator would have been "<-". "Is defined as" is actually pretty reasonable. "Define this to be that" is a common way to speak about assignment. Its only difference is the present tense. For example, in Python, "def" stands for "define", but we can overwrite previous definitions:: def f(x): return x def f(x): return 2 f(3) == 2 In fact, in pretty every programming language that I know of with a "define" assignment verb, this is so. For example, in Scheme, x is 2 at the end:: (define x 1) (define x 2) x When you write (define x 1) (define x 2) x or, in F# and OCaml, let x = 1 let x = 2 x you're saying x = 1 { x = 2 x } You don't modify 'x': you hide it by defining another "value" (not variable) with the same name. Indeed, let x = 1 let x = 2 x is shorthand for let x = 1 in let x = 2 in x No, Devin is right about Scheme. On "top level" re-definition is interpreted as assignment. The following mean the same: (define x 1) (define x 2) x (define x 1) (set! x 2) x Local definitions in the beginning of a "body" do not allow duplicate names at all. The following mean the same: (let () (define x 1) (define y 2) x) (letrec* ((x 1) (y 2)) x) ;letrec in older versions (not sure of R6RS) But (let () (define x 1) (define x 2) x) is still an error. Some implementations may give it a meaning. Not sure. Thanks for the correction. I haven't written a line of code in Scheme for 15 years and it shows :( Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On Mon, Mar 26, 2012 at 10:45 PM, wrote: > I know the print statement produces the same result when both of these two > instructions are executed ,I just want to know Is there any difference > between print 3 and print '3' in Python ? One of them takes the integer 3, converts it into a string, and prints it. The other takes the string '3' and prints it. There's a lot of difference under the covers, but both will print a 3, followed by a newline. ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Puzzled by FiPy's use of "=="
In FiPy (a finite volume PDE solver), equations are "magically" set up as eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D) and solved via eqX.solve(...) How can eqX be anything than True or False?... This must be via a redefinition of "==" but I can't see how that is done. I did look at many of the source files, thinking that it must be via a redefinition of "__eq__" somewhere but with no luck. Any pointers would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On 3/26/2012 13:45, redstone-c...@163.com wrote: I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? The former prints a number while the latter a string. Kiuhnm -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? This is a non-question. The input is the same, the output is the same, what else matters? On the other hand, if you want to dig deeper, there are lots of differences: 1) the former has a shorter source file 2) different C code is utilized inside the interpreter 3) different machine code executes 4) the temporary objects created have different id's and types 5) different execution times (by a trivial amount) 6) it takes different keystrokes to edit the two source files once you want to make it do something useful 7) the processor works a little harder on one than the other, possibly resulting in a different power consumption 8) different byte code is produced Or you could be asking about Python version 3, in which case 1) the syntax error message points to a different character -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On 3/26/12 12:45 PM, redstone-c...@163.com wrote: I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? Yes, there is a difference, but not much. [~] |6> import dis [~] |7> dis.disassemble(compile('print 3', '', 'exec')) 1 0 LOAD_CONST 0 (3) 3 PRINT_ITEM 4 PRINT_NEWLINE 5 LOAD_CONST 1 (None) 8 RETURN_VALUE [~] |8> dis.disassemble(compile('print "3"', '', 'exec')) 1 0 LOAD_CONST 0 ('3') 3 PRINT_ITEM 4 PRINT_NEWLINE 5 LOAD_CONST 1 (None) 8 RETURN_VALUE As you can see, the only difference is in the first instruction. Both of these put the object that you specified by the literal onto the stack. The difference is that one is the int object specified by the literal 3 and the other is the str object specified by the literal "3". Both of these objects happen to give the same __str__ output, so that's what gets printed. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Puzzled by FiPy's use of "=="
On 3/26/12 12:47 PM, André Roberge wrote: In FiPy (a finite volume PDE solver), equations are "magically" set up as eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D) and solved via eqX.solve(...) How can eqX be anything than True or False?... This must be via a redefinition of "==" but I can't see how that is done. I did look at many of the source files, thinking that it must be via a redefinition of "__eq__" somewhere but with no luck. Any pointers would be appreciated. It's in the root base class Term: http://matforge.org/fipy/browser/trunk/fipy/terms/term.py#L374 -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Puzzled by FiPy's use of "=="
On Monday, 26 March 2012 09:16:07 UTC-3, Robert Kern wrote: > On 3/26/12 12:47 PM, André Roberge wrote: > > In FiPy (a finite volume PDE solver), equations are "magically" set up as > > > > eqX = TransientTerm() == ExplicitDiffusionTerm(coeff=D) > > > > and solved via > > > > eqX.solve(...) > > > > How can eqX be anything than True or False?... This must be via a > > redefinition of "==" but I can't see how that is done. I did look at many > > of the source files, thinking that it must be via a redefinition of > > "__eq__" somewhere but with no luck. Any pointers would be appreciated. > > It's in the root base class Term: > >http://matforge.org/fipy/browser/trunk/fipy/terms/term.py#L374 > I thought I looked at terms.py ... but I must have missed that. Thanks! > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." >-- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: verbs in comments [OT]
Kiuhnm wrote: Why do you write // Print the number of words... def printNumWords(): ... and not // Prints the number of words... def printNumWords(): ... where "it" is understood? Is that an imperative or a base form or something else? Kiuhnm http://www.python.org/dev/peps/pep-0257/ "The docstring is a phrase ending in a period. It prescribes the function or method's effect as a command ("Do this", "Return that"), not as a description; e.g. don't write "Returns the pathname ..." I apply the same rule to comments, I now don't loose time asking myself how to write these docs/comments. JM -- http://mail.python.org/mailman/listinfo/python-list
help needed to understand an error message.
Hi, I am learning Python and do not have programming experience. I was following an exercise from http://learnpythonthehardway.org/book/ex2.html and made a mistake in entry : *Print"I like typing this."* and got the following error message: *In [2]: Print"I like typing this."* ** * File "", line 1* * Print"I like typing this."* *^* *SyntaxError: invalid syntax* I feel the error is in Capital P in print . However the error indicated with "*^*" hints at quote at the end of the line. *Can any one please help me understand this.* -- A.K.Ghosh -- http://mail.python.org/mailman/listinfo/python-list
Re: help needed to understand an error message.
Aloke Ghosh wrote: Hi, I am learning Python and do not have programming experience. I was following an exercise from http://learnpythonthehardway.org/book/ex2.html and made a mistake in entry : *Print"I like typing this."* and got the following error message: *In [2]: Print"I like typing this."* ** * File "", line 1* * Print"I like typing this."* *^* *SyntaxError: invalid syntax* I feel the error is in Capital P in print . However the error indicated with "*^*" hints at quote at the end of the line. *Can any one please help me understand this.* -- A.K.Ghosh Why don't you just try with a lowercase p ? The correct syntax would be print "I like typing this" or in Python 3: print ("I like typing this") Anyway, the hint indicates the last quote because this is the location where the python interpreter realizes it won't be able to execute the code. You should not worry about that too much. JM -- http://mail.python.org/mailman/listinfo/python-list
Question about collections.defaultdict
I created a new class called CaseInsensitiveDict (by stealing from code I found on the web, thank you very much). The new class inherits from dict. It makes it so that if the key has a 'lower' method, it will always access the key using lower I'd like to change the place where I previously declared a dict self.lookup = defaultdict(list) so that the new code will allow this new dict to be used instead. But then I realized I may have painted myself into a small corner: Is there a way to use defaultdict so that I can override what *kind* of dict it will use? I would like the value to still be a list be default, but it seems like I can't tell defaultdict to use *my* new dict. Do I give up on defaultdict? BTW, 2.6 if it matters. TIA :-) -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about collections.defaultdict
On 3/26/12 2:33 PM, Steven W. Orr wrote: I created a new class called CaseInsensitiveDict (by stealing from code I found on the web, thank you very much). The new class inherits from dict. It makes it so that if the key has a 'lower' method, it will always access the key using lower I'd like to change the place where I previously declared a dict self.lookup = defaultdict(list) so that the new code will allow this new dict to be used instead. But then I realized I may have painted myself into a small corner: Is there a way to use defaultdict so that I can override what *kind* of dict it will use? No. I would like the value to still be a list be default, but it seems like I can't tell defaultdict to use *my* new dict. Do I give up on defaultdict? Assuming that your CaseInsensitiveDict subclasses from dict or UserDict, it's relatively easy to make a subclass of your CaseInsensitiveDict act like a defaultdict. Just implement the __missing__(key) method appropriately (and modify the constructor to take the callable, of course). http://docs.python.org/library/stdtypes.html#dict http://docs.python.org/library/collections.html#collections.defaultdict.__missing__ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
On Mar 26, 2012 12:28 AM, "Steven D'Aprano" < steve+comp.lang.pyt...@pearwood.info> wrote: > > On Mon, 26 Mar 2012 08:40:00 +0200, Michael Poeltl wrote: > > > * Nikhil Verma [2012-03-26 08:09]: > A truly random six digit number will include any number between 10 > through 99. There are exactly 90 (nine hundred thousand) such > numbers. Actually, considering that 00 would still fit the parameter of a 6 "digit" number, there are exactly one million such numbers. -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
Am 25.03.2012 15:03 schrieb Tim Chase: Perhaps a DB example works better. With assignment allowed in an evaluation, you'd be able to write while data = conn.fetchmany(): for row in data: process(row) whereas you have to write while True: data = conn.fetchmany() if not data: break for row in data: process(row) Or simpler for data in iter(conn.fetchmany, []): for row in data: process(row) provided that a block of rows is returned as a list - which might be different among DB engines. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
Am 26.03.2012 00:59 schrieb Dennis Lee Bieber: If you use the longer form con = db.connect() cur = con.cursor() the cursor object, in all that I've worked with, does function for iteration I use this form regularly with MySQLdb and am now surprised to see that this is optional according to http://www.python.org/dev/peps/pep-0249/. So a database cursor is not required to be iterable, alas. Thomas -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: > > I know the print statement produces the same result when both of these two > > instructions are executed ,I just want to know Is there any difference > > between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: > > I know the print statement produces the same result when both of these two > > instructions are executed ,I just want to know Is there any difference > > between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? > > On the other hand, if you want to dig deeper, there are lots of differences: > > 1) the former has a shorter source file > 2) different C code is utilized inside the interpreter > 3) different machine code executes > 4) the temporary objects created have different id's and types > 5) different execution times (by a trivial amount) > 6) it takes different keystrokes to edit the two source files once you > want to make it do something useful > 7) the processor works a little harder on one than the other, possibly > resulting in a different power consumption > 8) different byte code is produced > > Or you could be asking about Python version 3, in which case > 1) the syntax error message points to a different character > > -- > > DaveA Oh ,God ! I think this is what I really want to know ,thank you very much ! -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
> I know the print statement produces the same result when both of these two > instructions are executed ,I just want to know Is there any difference > between print 3 and print '3' in Python ? Sure there is. The first converts the integer 3 to a string ("3"), the second just prints the given string "3". Does that make sense? -- Colton Myers -- http://mail.python.org/mailman/listinfo/python-list
Re: Inconsistency between os.getgroups and os.system('groups') after os.setgroups()
On Sunday, March 25, 2012 6:22:10 PM UTC-6, Ben Finney wrote: > jeff writes: > > > On Sunday, March 25, 2012 4:04:55 PM UTC-6, Heiko Wundram wrote: > > > Am 25.03.2012 23:32, schrieb jeff: > > > > but I have to be able to get back to root privilege so I can't use > > > > setgid and setuid. > > > > > > Simply not possible (i.e., you can't drop root privileges, be it by > > > setuid()/setgid() or removing yourself from groups with setgroups()), > > > and later reacquire them _in the same process_. See the discussion of > > > how to implement privilege separation at > > > > > > http://www.citi.umich.edu/u/provos/ssh/privsep.html > > > > os.system("su -m -c ''") > > > > seems to do the trick. > > Yes, because ‘os.system’ explicitly starts a new process. > > It can't be done in the same process, as Heiko correctly said. > > -- > \ “Faith, n. Belief without evidence in what is told by one who | > `\ speaks without knowledge, of things without parallel.” —Ambrose | > _o__) Bierce, _The Devil's Dictionary_, 1906 | > Ben Finney I didn't ask how to do it in the same process, but thanks to both of you for that information. By the way, are you guys aware of seteuid and setegid? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
redstone-c...@163.com, 26.03.2012 16:28: > 在 2012年3月26日星期一UTC+8下午8时11分03秒,Dave Angel写道: >> On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: >>> I know the print statement produces the same result when both of these two >>> instructions are executed ,I just want to know Is there any difference >>> between print 3 and print '3' in Python ? >> >> This is a non-question. The input is the same, the output is the same, >> what else matters? >> >> On the other hand, if you want to dig deeper, there are lots of differences: >> >> 1) the former has a shorter source file >> 2) different C code is utilized inside the interpreter >> 3) different machine code executes >> 4) the temporary objects created have different id's and types >> 5) different execution times (by a trivial amount) >> 6) it takes different keystrokes to edit the two source files once you >> want to make it do something useful >> 7) the processor works a little harder on one than the other, possibly >> resulting in a different power consumption >> 8) different byte code is produced >> >> Or you could be asking about Python version 3, in which case >> 1) the syntax error message points to a different character > > Oh ,God ! I don't think she takes any responsibility for the list above. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about collections.defaultdict
On 3/26/2012 9:44 AM, Robert Kern wrote: On 3/26/12 2:33 PM, Steven W. Orr wrote: I created a new class called CaseInsensitiveDict (by stealing from code I found on the web, thank you very much). The new class inherits from dict. It makes it so that if the key has a 'lower' method, it will always access the key using lower I'd like to change the place where I previously declared a dict self.lookup = defaultdict(list) so that the new code will allow this new dict to be used instead. But then I realized I may have painted myself into a small corner: Is there a way to use defaultdict so that I can override what *kind* of dict it will use? No. I would like the value to still be a list be default, but it seems like I can't tell defaultdict to use *my* new dict. Do I give up on defaultdict? Assuming that your CaseInsensitiveDict subclasses from dict or UserDict, it's relatively easy to make a subclass of your CaseInsensitiveDict act like a defaultdict. Just implement the __missing__(key) method appropriately (and modify the constructor to take the callable, of course). http://docs.python.org/library/stdtypes.html#dict http://docs.python.org/library/collections.html#collections.defaultdict.__missing__ I'm not quite getting what you're telling me, but I'm sure you have the right idea. Here's the beginning of my class: class CaseInsensitiveDict(dict): def __init__(self, init=None): if isinstance(init, (dict, list, tuple)): for kk, vv in init.items(): self[self.key_has_lower(kk)] = vv It sounds like you want me to subclass defaultdict to create something like this? class CaseInsensitiveDictDef(defaultdict): def __init__(self, init=None): super(CaseInsensitiveDictDef, self).__init__(list) self.__missing__ = list I think I'm way off base. I'm not clear on what the calling sequence is for defaultdict or how to get it to use my CaseInsensitiveDict instead of regular dict. Can you help? -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net -- http://mail.python.org/mailman/listinfo/python-list
Re: why did GMPY change the names of its functions?
On 3/26/2012 12:59 AM, Mensanator wrote: OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? Guess: Either the functions changed or they want to regularize their names. What's the justification for that? I use those functions extensively in my library of Collatz utilities and I had to re-edit them for no obvious reason. If GMPY is coded in Python with a C? backup, GMPY.scan0 = GMPY.bit_scan0 should work. Or you could write a GMPY.py wrapper for GMPY2 from GMPY2 import * scan0=bit_scan0 scan1=bit_scan1 and leave your user code alone. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On 3/26/2012 7:45 AM, redstone-c...@163.com wrote: I know the print statement produces the same result when both of these two instructions are executed ,I just want to know Is there any difference between print 3 and print '3' in Python ? If you want to see the difference between the number and string representation thereof, use repr(x). >>> print(repr(3), repr('3'), [3, '3']) 3 '3' [3, '3'] Note that printing a collection prints the repr() of each item precisely so one can tell the difference between the item being a number or a string. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Question about collections.defaultdict
On 3/26/12 4:33 PM, Steven W. Orr wrote: On 3/26/2012 9:44 AM, Robert Kern wrote: On 3/26/12 2:33 PM, Steven W. Orr wrote: I created a new class called CaseInsensitiveDict (by stealing from code I found on the web, thank you very much). The new class inherits from dict. It makes it so that if the key has a 'lower' method, it will always access the key using lower I'd like to change the place where I previously declared a dict self.lookup = defaultdict(list) so that the new code will allow this new dict to be used instead. But then I realized I may have painted myself into a small corner: Is there a way to use defaultdict so that I can override what *kind* of dict it will use? No. I would like the value to still be a list be default, but it seems like I can't tell defaultdict to use *my* new dict. Do I give up on defaultdict? Assuming that your CaseInsensitiveDict subclasses from dict or UserDict, it's relatively easy to make a subclass of your CaseInsensitiveDict act like a defaultdict. Just implement the __missing__(key) method appropriately (and modify the constructor to take the callable, of course). http://docs.python.org/library/stdtypes.html#dict http://docs.python.org/library/collections.html#collections.defaultdict.__missing__ I'm not quite getting what you're telling me, but I'm sure you have the right idea. Here's the beginning of my class: class CaseInsensitiveDict(dict): def __init__(self, init=None): if isinstance(init, (dict, list, tuple)): for kk, vv in init.items(): self[self.key_has_lower(kk)] = vv It sounds like you want me to subclass defaultdict to create something like this? class CaseInsensitiveDictDef(defaultdict): def __init__(self, init=None): super(CaseInsensitiveDictDef, self).__init__(list) self.__missing__ = list I think I'm way off base. I'm not clear on what the calling sequence is for defaultdict or how to get it to use my CaseInsensitiveDict instead of regular dict. Can you help? You need to make a subclass of CaseInsensitiveDict, implement the __missing__(key) method, and override the __init__() method to take the factory function as an argument instead of data. defaultdict is just a subclass of dict that does this. class CaseInsensitiveDictDef(CaseInsensitiveDict): def __init__(self, default_factory): super(CaseInsensitiveDictDef, self).__init__() self.default_factory = default_factory def __missing__(self, key): return self.default_factory() -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On 3/26/2012 1:36 AM, Steven D'Aprano wrote: (I seem to recall a language that used a single = for both assignment and equality testing, guessing which one you meant from context. BASIC perhaps? Right. In some Basics, such as MS GW-Basic (I still have their book), a = b = c meant a = (b = c), or in Python, a = b==c. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
A play about the Indian IT industry
My name is Sathyaish. I am a software engineer. Last year, i.e. in 2011, I wanted to do some theater. No one took me, so I announced that I would start my own group. I wrote a script. Then, I wrote a screen play from that. Now, I am almost ready to begin the auditions. The play will be a comedy with a sad undertone profiling the life (or rather the lack) of programmers and how they are generally taken for granted and treated poorly by people who should really have been working in a bread making factory baking bread, or people who should have been making biscuits or whatever, i.e. the non-technical people in the software development industry who have become managers and architects and what-not. Here is a teaser: http://www.youtube.com/watch?v=6V-Lchu7aiA I hope you like it. I'll be posting more stuff about this play on the Web page at http://sathyaish.net/acting The play will be performed at a theater in New Delhi, India. The group that I am organizing to perform this play will rehearse only over weekends so that they may keep their day jobs. I expect that the auditions will run through April 2012. We will start rehearsing in May 2012 and will carry on with the rehearsals for May, June, July, August and September 2012 only over the weekends. We should perform in the last week of September 2012 or some time in October 2012. -- http://mail.python.org/mailman/listinfo/python-list
Re: random number
On Mon, Mar 26, 2012 at 3:24 AM, Michael Poeltl wrote: import random, string def random_number(id): > ... characters = list(string.ascii_lowercase + > ... string.ascii_uppercase + > ... string.digits) > ... coll_rand = [] > ... for i in range(6): > ... random.shuffle(characters) > ... coll_rand.append(characters[0]) > ... return ''.join(coll_rand) You don't need to do all that list manipulation. This is probably quicker: def random_number(): # Unused "id" parameter omitted characters = (string.ascii_lowercase + string.ascii_uppercase + string.digits) return ''.join(random.choice(characters) for i in range(6)) Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
As others have pointed out, the output is the same, because the result of converting an integer to a string is the string of that integer. However, other numeric literals might not do what you want, due to the fact that they are converted to an internal numeric representation, then converted back to a string in a canonical format. >>> print 3, '3' 3 3 >>> print 3.00, '3.00' 3.0 3.00 >>> print 0x3, '0x3' 3 0x3 >>> print 03, '03' 3 03 >>> print 3e0, '3e0' 3.0 3e0 You might think that the take away message is to use the string representation, since it prints what you tell it to print. However, if you use a number, you can specify the output formatting with more fine-grained control, and even exert that control on calculated number: >>> print '%0.2f' % (3,) 3.00 >>> print '%0.2f' % (2 + 1) 3.00 This is better because you can't perform math on a string: >>> print '2' + '1' 21 >>> print '2.00' + '1.00' 2.001.00 print '2 + 1' 2 + 1 So in general, you should use numbers, and then format them using standard string formatting operations when you want to print them. There's more information on how to do formatting here: http://docs.python.org/library/stdtypes.html#string-formatting Cheers, Cliff On Mon, 2012-03-26 at 04:45 -0700, redstone-c...@163.com wrote: > I know the print statement produces the same result when both of these > two instructions are executed ,I just want to know Is there any > difference between print 3 and print '3' in Python ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
redstone-c...@163.com wrote: > I know the print statement produces the same result when both of these two > instructions are executed ,I just want to know Is there any difference > between print 3 and print '3' in Python ? The question you really wanted to ask is: under what circumstances will the two statements produce different output? Here's what I found: $ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals print 3' 3 $ PYTHONIOENCODING=hex python -c'from __future__ import unicode_literals print "3"' 33 $ :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Documentation, assignment in expression.
On 03/26/12 08:59, Thomas Rachel wrote: Am 25.03.2012 15:03 schrieb Tim Chase: while True: data = conn.fetchmany() if not data: break for row in data: process(row) Or simpler for data in iter(conn.fetchmany, []): for row in data: process(row) Nice! That's the first time I've seen the 2-parameter version of iter() improve things :-) (I've seen it once or twice before in the 2-param form, and wondered why it wasn't written in a clearer way). Also, I was surprised to learn that the sentinel was available as far back as 2.2 (I knew about the 1-param version as far back as 2.3 when I became more serious about Python). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: why did GMPY change the names of its functions?
On Mar 26, 10:39 am, Terry Reedy wrote: > On 3/26/2012 12:59 AM, Mensanator wrote: > > > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > Guess: Either the functions changed or they want to regularize their names. > > > What's the justification for that? I use those functions extensively > > in my library of Collatz utilities and I had to re-edit them for no > > obvious reason. > > If GMPY is coded in Python with a C? backup, GMPY.scan0 = GMPY.bit_scan0 > should work. > > Or you could write a GMPY.py wrapper for GMPY2 > > from GMPY2 import * > scan0=bit_scan0 > scan1=bit_scan1 > > > and leave your user code alone. > > -- > Terry Jan Reedy Oh, similar to an "import as", but this would allow me to change individual functions. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: why did GMPY change the names of its functions?
On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > What's the justification for that? I use those functions extensively > in my library of Collatz utilities and I had to re-edit them for no > obvious reason. I'll speak up as the maintainer of GMPY and GMPY2. (My comments apply to the beta1 release which should be out in a couple of days.) GMPY2 introduces many changes: 1) the limited "mpf" type that is based on GMP has been replaced with the "mpfr" type from the MPFR library 2) support for multiple-precision complex arithmetic based on the MPC library 3) support for a mutable integer type optimized for in-place bit manipulations 4) support for addition number theory functions (generalized Lucas sequences and more primality tests I began to encounter name collisions; for example, should sqrt() only return integer square roots. I chose to call it a new name (gmpy2) and update the API to reflect new choices I made. For example, sqrt() now returns an "mpfr" and isqrt() returns an "mpz". As part of the documentation for the beta release, I will document the name changes. "import gmpy2 as gmpy; gmpy.scan0=gmpy.bit_scan0; etc" should work just fine. If you encounter problems with the alpha release, please open an issue on gmpy's site. Thanks, casevh On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > What's the justification for that? I use those functions extensively > in my library of Collatz utilities and I had to re-edit them for no > obvious reason. On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > What's the justification for that? I use those functions extensively > in my library of Collatz utilities and I had to re-edit them for no > obvious reason. On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > What's the justification for that? I use those functions extensively > in my library of Collatz utilities and I had to re-edit them for no > obvious reason. -- http://mail.python.org/mailman/listinfo/python-list
RE: help needed to understand an error message.
>> I feel the error is in Capital P in print . >> However the error indicated with "*^*" >> hints at quote at the end of the line. > > Anyway, the hint indicates the last quote because this is the location > where the python interpreter realizes it won't be able to execute the > code. You should not worry about that too much. Often when I see errors the problem can be earlier in the line or a line or two before. Just look backwards from where the error says the problem was found. Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
perldoc: the key to perl
〈Perl Documentation: The Key to Perl〉 http://xahlee.org/perl-python/key_to_perl.html plain text follows - So, i wanted to know what the option perl -C does. So, here's perldoc perlrun. Excerpt: -C [*number/list*] The -C flag controls some of the Perl Unicode features. As of 5.8.1, the -C can be followed either by a number or a list of option letters. The letters, their numeric values, and effects are as follows; listing the letters is equal to summing the numbers. I 1 STDIN is assumed to be in UTF-8 O 2 STDOUT will be in UTF-8 E 4 STDERR will be in UTF-8 S 7 I + O + E i 8 UTF-8 is the default PerlIO layer for input streams o16 UTF-8 is the default PerlIO layer for output streams D24 i + o A32 the @ARGV elements are expected to be strings encoded in UTF-8 L64 normally the "IOEioA" are unconditional, the L makes them conditional on the locale environment variables (the LC_ALL, LC_TYPE, and LANG, in the order of decreasing precedence) -- if the variables indicate UTF-8, then the selected "IOEioA" are in effect a 256 Set ${^UTF8CACHE} to -1, to run the UTF-8 caching code in debugging mode. For example, -COE and -C6 will both turn on UTF-8-ness on both STDOUT and STDERR. Repeating letters is just redundant, not cumulative nor toggling. The "io" options mean that any subsequent open() (or similar I/O operations) in the current file scope will have the ":utf8" PerlIO layer implicitly applied to them, in other words, UTF-8 is expected from any input stream, and UTF-8 is produced to any output stream. This is just the default, with explicit layers in open() and with binmode() one can manipulate streams as usual. -C on its own (not followed by any number or option list), or the empty string "" for the "PERL_UNICODE" environment variable, has the same effect as -CSDL. In other words, the standard I/ O handles and the default "open()" layer are UTF-8-fied *but* only if the locale environment variables indicate a UTF-8 locale. This behaviour follows the *implicit* (and problematic) UTF-8 behaviour of Perl 5.8.0. You can use -C0 (or "0" for "PERL_UNICODE") to explicitly disable all the above Unicode features. The read-only magic variable "${^UNICODE}" reflects the numeric value of this setting. This variable is set during Perl startup and is thereafter read-only. If you want runtime effects, use the three-arg open() (see "open" in perlfunc), the two-arg binmode() (see "binmode" in perlfunc), and the "open" pragma (see open). (In Perls earlier than 5.8.1 the -C switch was a Win32- only switch that enabled the use of Unicode-aware "wide system call" Win32 APIs. This feature was practically unused, however, and the command line switch was therefore "recycled".) Note: Since perl 5.10.1, if the -C option is used on the "#!" line, it must be specified on the command line as well, since the standard streams are already set up at this point in the execution of the perl interpreter. You can also use binmode() to set the encoding of an I/O stream. reading that is like a adventure. It's like this: The -C is a key to unlock many secrets. Just get it, and you'll be all good to go, except in cases you may need the inner key. You'll find a hinge in the key, open it, then there's a subkey. On the subkey, there's a number. Take that number to the lock, it will open with keyX. When you use keyX, it must be matched with the previous inner key with 8th bit. keyX doesn't have a ID, but you can make one by finding the number at the place you found the key C. Key C is actually optional, but when inner key and keyX's number matches, it changes the nature of the lock. This is when you need to turn on keyMode … Xah -- http://mail.python.org/mailman/listinfo/python-list
best way to create warning for obsolete functions and call new one
Hi, I'm working on a module, which needs rather heavy renaming of functions and methods (naming style, change of functionality, understandability, orthography) As these modules are used by quite some projects and as I do not want to force everybody to rename immediately I just want to warn users, that they call functions, that have been renamed and that will be obsoleted. function example: def get_thyme(a='high'): # old name return a + 'noon' # Should have been get_time(a, b) method example: class C(): def set_with(self, value=0): # should be set_width self.width = value What I basically want to do for each function is following: def get_time(a='high'): return a + 'noon' def get_thyme(a): # ideally the next line should display old and new function name # but even with out I 'd be fine logging.warning('obsolate function. please use the new function') return get_time(a) Assuming I want to rename loads of functions / metthods, what would be the laziest option to so? (and have no run time penalty for the new call) One option I though of would be: def obsolete_func(func): def call_old(*args, **kwargs): print "func is old psl use new one" return func(*args, **kwargs) return call_old and def get_time(a='high'): return a + 'noon' get_thyme = obsolete_func(get_time) class C(): def set_width(self, value=0): # should be set_width self.width = value set_with = obsolete_func(set_width) Another idea might be something like: add_obsolete_functions(get_tyme=get_time, get_dayt=get_date) Not sure though how to do this best for Classes Thanks for any ideas -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to create warning for obsolete functions and call new one
On Mon, 26 Mar 2012 22:26:11 +0200 Gelonida N wrote: > As these modules are used by quite some projects and as I do not want > to force everybody to rename immediately I just want to warn users, > that they call functions, that have been renamed and that will be > obsoleted. You want a DeprecationWarning. HTH, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to create warning for obsolete functions and call new one
On Tue, Mar 27, 2012 at 7:26 AM, Gelonida N wrote: > One option I though of would be: > > def obsolete_func(func): > def call_old(*args, **kwargs): > print "func is old psl use new one" > return func(*args, **kwargs) > return call_old > > and > > def get_time(a='high'): > return a + 'noon' That's a reasonable idea. Incorporate Dan's suggestion of using DeprecationWarning. You may want to try decorator syntax: def was(oldname): def _(func): globals()[oldname]=func return func return _ @was("get_thyme") def get_time(a='high'): return a + 'noon' That won't raise DeprecationWarning, though. It's a very simple assignment. The was() internal function could be enhanced to do a bit more work, but I'm not sure what version of Python you're using and what introspection facilities you have. But if you're happy with the old versions coming up with (*args,**kwargs) instead of their parameter lists, it's not difficult: def was(oldname): def _(func): def bounce(*args,**kwargs): # raise DeprecationWarning return func(*args,**kwargs) globals()[oldname]=bounce return func return _ I've never actually used the Python warnings module, but any line of code you fill in at the comment will be executed any time the old name is used. In any case, it's still used with the same convenient decorator. You could even unify multiple functions under a single new name: @was("foo") @was("bar") def quux(spam,ham): return ham.eat() Hope that helps! ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Advise of programming one of my first programs
Hi guys just wanted to share one of my first programs. Could you please tell me, do I use a right logic ? It works fine what I wanted to do, but is it writen in the right way? My next step is to make it write the changes of the dictionary on the file :) ## DB tbook = {'goodie':['Christian','Van Eckel','Bruxelles','Forest','02 344 33 33','This is a test note :)'], 'osvaldo':['Osvaldo','Rios','Liege','Centrum','023758832',''], 'ronaldo':['Diego','Aspanda','Brussels','Vorst','03 443 23 23','']} ## Edit selected nickname def edit(): sb = tbook[select] fn = raw_input('New name for ' + sb[0] + ' : ') sb[0] = fn ln = raw_input('New name for ' + sb[1] + ' : ') sb[1] = ln raw_input('\n\n\nPress to return') details() ## Details of nickname def details(): sb = tbook[select] print 'Nickname: ', select, ' is selected\n' print 'First name:\t', sb[0], '\n' print 'Last name:\t', sb[1], '\n' print 'Country:\t', sb[2], '\n' print 'City:\t\t', sb[3], '\n' print 'Phone number:\t',sb[4], '\n' print 'Memos:\n' print sb[5] print '\n\n(E)dit\n\n' print '(B)ack to phonebook list\n\n' menu = raw_input('What you wana do? ') if menu == 'e': edit() if menu == 'b': listpb() ## Select nickname def selectm(): global select select = raw_input('Type nickname and press : ') if select == '': listpb() if select in tbook: details() else: listpb() ## List all contacts def listpb(): print '_' *45, ' Phonebook ', '_' *45,'\n\n\n' print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel' print '_' * 105,'\n','\t' * 13 for val in tbook.keys(): print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t', tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n' print '_'*105,'\n\n' selectm() ## Call list names listpb() Thanks a lot Anatoli -- http://mail.python.org/mailman/listinfo/python-list
RE: Advise of programming one of my first programs
> Hi guys just wanted to share one of my first programs. Could you please > tell me, do I use a right logic ? > It works fine what I wanted to do, but is it writen in the right way? My > next step is to make it write the changes of the dictionary on the file :) > When you do get that far, you should look at the pickle library. It is amazing how easy it is to store data with Python. > > ## DB > tbook = {'goodie':['Christian','Van Eckel','Bruxelles','Forest','02 344 33 > 33','This is a test note :)'], > 'osvaldo':['Osvaldo','Rios','Liege','Centrum','023758832',''], > 'ronaldo':['Diego','Aspanda','Brussels','Vorst','03 443 23 > 23','']} > > ## Edit selected nickname > def edit(): > sb = tbook[select] > fn = raw_input('New name for ' + sb[0] + ' : ') > sb[0] = fn > ln = raw_input('New name for ' + sb[1] + ' : ') > sb[1] = ln > raw_input('\n\n\nPress to return') > details() > > > ## Details of nickname > def details(): > sb = tbook[select] > print 'Nickname: ', select, ' is selected\n' > print 'First name:\t', sb[0], '\n' > print 'Last name:\t', sb[1], '\n' > print 'Country:\t', sb[2], '\n' > print 'City:\t\t', sb[3], '\n' > print 'Phone number:\t',sb[4], '\n' > print 'Memos:\n' > print sb[5] > > print '\n\n(E)dit\n\n' > print '(B)ack to phonebook list\n\n' > menu = raw_input('What you wana do? ') > if menu == 'e': > edit() > if menu == 'b': > listpb() > Minor nitpick, but what if the user types 'B' or 'E' like in your printed menu? > > ## Select nickname > def selectm(): > global select > select = raw_input('Type nickname and press : ') > if select == '': > listpb() > if select in tbook: > details() > else: > listpb() Remove all global variables when your program starts to work. Instead pass them as arguments and return them from functions. So do 'details( select )' instead of 'details()' and then in details, you would do edit( select ). > > ## List all contacts > def listpb(): > print '_' *45, ' Phonebook ', '_' *45,'\n\n\n' > > print 'Nick\t\tF.Name\t\tL.Name\t\tCity\t\t\tRegion\t\tTel' > print '_' * 105,'\n','\t' * 13 > for val in tbook.keys(): > print val, '\t\t', tbook[val][0], '\t', tbook[val][1], '\t', > tbook[val][2], '\t\t', tbook[val][3], '\t\t', tbook[val][4],'\t\t\n' > print '_'*105,'\n\n' > selectm() > > ## Call list names > listpb() if __name__ == "__main__": listpb() This way you can import the module and not run it on import; it is useful when you start wanting to reuse functions from a different project. It is better than copy-pasting functions everywhere because when you improve the function all the programs will pick it up. Otherwise you will have to go back to each pasted function and pick it up. A few items I would work to improve: 1. Remove global variables (already mentioned above) 2. You should separate any menu / navigation from your application code. details() should only print the details and not take the next menu choice. You probably want 2 separate menu functions. One that returns a 'select'-ed book and one that returns next choice. This will also help you when you implement my next point. 3. You should also change the structure of your program to loop instead of calling listpb each time you want to restart. It is a much better practice and while it will not affect this program unless you do not exit for 10s or 100s of thousands of details but if you write something that *does* navigate that many times it can crash. Looping is probably your next programming lesson anyway :) 4. This is more of a side note but instead of using \t\t all the time, you would be better off learning to use the string formatting operations. It is a little more effort to learn, but tends to be a lot more reliable on different systems (and with different data trying to be printed) than manually trying to align everything. http://docs.python.org/library/string.html#format-string-syntax Keep on working, you have made a good start and now it is time to refactor (programming equivalent of rewriting an essay) and make everything better! Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
concurrent file reading/writing using python
Hi Guys I am fwding this question from the python tutor list in the hope of reaching more people experienced in concurrent disk access in python. I am trying to see if there are ways in which I can read a big file concurrently on a multi core server and process data and write the output to a single file as the data is processed. For example if I have a 50Gb file, I would like to read it in parallel with 10 process/thread, each working on a 10Gb data and perform the same data parallel computation on each chunk of fine collating the output to a single file. I will appreciate your feedback. I did find some threads about this on stackoverflow but it was not clear to me what would be a good way to go about implementing this. Thanks! -Abhi -- Forwarded message -- From: Steven D'Aprano Date: Mon, Mar 26, 2012 at 3:21 PM Subject: Re: [Tutor] concurrent file reading using python To: tu...@python.org Abhishek Pratap wrote: > > Hi Guys > > > I want to utilize the power of cores on my server and read big files > (> 50Gb) simultaneously by seeking to N locations. Yes, you have many cores on the server. But how many hard drives is each file on? If all the files are on one disk, then you will *kill* performance dead by forcing the drive to seek backwards and forwards: seek to 12345678 read a block seek to 9947500 read a block seek to 5891124 read a block seek back to 12345678 + 1 block read another block seek back to 9947500 + 1 block read another block ... The drive will spend most of its time seeking instead of reading. Even if you have multiple hard drives in a RAID array, performance will depend strongly the details of how it is configured (RAID1, RAID0, software RAID, hardware RAID, etc.) and how smart the controller is. Chances are, though, that the controller won't be smart enough. Particularly if you have hardware RAID, which in my experience tends to be more expensive and less useful than software RAID (at least for Linux). And what are you planning on doing with the files once you have read them? I don't know how much memory your server has got, but I'd be very surprised if you can fit the entire > 50 GB file in RAM at once. So you're going to read the files and merge the output... by writing them to the disk. Now you have the drive trying to read *and* write simultaneously. TL; DR: Tasks which are limited by disk IO are not made faster by using a faster CPU, since the bottleneck is disk access, not CPU speed. Back in the Ancient Days when tape was the only storage medium, there were a lot of programs optimised for slow IO. Unfortunately this is pretty much a lost art -- although disk access is thousands or tens of thousands of times slower than memory access, it is so much faster than tape that people don't seem to care much about optimising disk access. > What I want to know is the best way to read a file concurrently. I > have read about file-handle.seek(), os.lseek() but not sure if thats > the way to go. Any used cases would be of help. Optimising concurrent disk access is a specialist field. You may be better off asking for help on the main Python list, comp.lang.python or python-list@python.org, and hope somebody has some experience with this. But chances are very high that you will need to search the web for forums dedicated to concurrent disk access, and translate from whatever language(s) they are using to Python. -- Steven ___ Tutor maillist - tu...@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor -- http://mail.python.org/mailman/listinfo/python-list
RE: perldoc: the key to perl
> > 〈Perl Documentation: The Key to Perl〉 > http://xahlee.org/perl-python/key_to_perl.html > > plain text follows > - > > So, i wanted to know what the option perl -C does. So, here's perldoc > perlrun. Excerpt: [snip] Maybe I missed something, but what does this have to do with Python? Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. -- http://mail.python.org/mailman/listinfo/python-list
[ANNOUNCE] pypiserver 0.5.2 - minimal pypi server
Hi, I've just uploaded pypiserver 0.5.2 to the python package index. pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages and eggs to easy_install or pip. pypiserver is easy to install (i.e. just easy_install pypiserver). It doesn't have any external dependencies. http://pypi.python.org/pypi/pypiserver/ should contain enough information to easily get you started running your own PyPI server in a few minutes. The code is available on github: https://github.com/schmir/pypiserver Changes in version 0.5.2 - - provide a way to get the WSGI app - improved package name and version guessing - use case insensitive matching when removing archive suffixes - fix pytz issue #6 -- Cheers, Ralf -- http://mail.python.org/mailman/listinfo/python-list
Re: Your Regex Brain
On Sat, 24 Mar 2012 16:30:28 -0700 (PDT), Xah Lee wrote: >?Your Regex Brain? >http://xahlee.org/comp/your_regex_brain.html > That's more like a brain cell. This is more like a regex brain. ' "\']|"[^"]*"|\'[^\']*\')*? (?<=\s) width \s*= (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} ) | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) ) ) ) (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) src \s*= (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} ) | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) ) ) ) (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) height \s*= (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} ) | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) ) ) ) (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) alt \s*= (?: (?> \s* ([\'"]) \s* (?.*?) \s* \g{-2} ) | (?> (?!\s*[\'"]) \s* (?[^\s>]*) (?=\s|>) ) ) ) (?> \s+ (?:".*?"|\'.*?\'|[^>]*?)+ > ) (?) ' -sln -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse ConfigureAction problem
> ./plot_stuff2.py --plot stuff1 stuff2 > [...] > plot_stuff2.py: error: argument --plot/--with-plot/--enable-plot/--no-plot/-- > without-plot/--disable-plot: invalid boolean value: 'stuff1' > > Problem is --plot takes an optional argument, and so the positional arg is > assumed to be the arg to --plot. Not sure how to fix this. Maybe have a look at nargs='*' in http://docs.python.org/py3k/library/argparse.html#nargs. -- http://mail.python.org/mailman/listinfo/python-list
Re: concurrent file reading/writing using python
On Mar 26, 3:56 pm, Abhishek Pratap wrote: > Hi Guys > > I am fwding this question from the python tutor list in the hope of > reaching more people experienced in concurrent disk access in python. > > I am trying to see if there are ways in which I can read a big file > concurrently on a multi core server and process data and write the > output to a single file as the data is processed. > > For example if I have a 50Gb file, I would like to read it in parallel > with 10 process/thread, each working on a 10Gb data and perform the > same data parallel computation on each chunk of fine collating the > output to a single file. > > I will appreciate your feedback. I did find some threads about this on > stackoverflow but it was not clear to me what would be a good way to > go about implementing this. > Have you written a single-core solution to your problem? If so, can you post the code here? If CPU isn't your primary bottleneck, then you need to be careful not to overly complicate your solution by getting multiple cores involved. All the coordination might make your program slower and more buggy. If CPU is the primary bottleneck, then you might want to consider an approach where you only have a single thread that's reading records from the file, 10 at a time, and then dispatching out the calculations to different threads, then writing results back to disk. My approach would be something like this: 1) Take a small sample of your dataset so that you can process it within 10 seconds or so using a simple, single-core program. 2) Figure out whether you're CPU bound. A simple way to do this is to comment out the actual computation or replace it with a trivial stub. If you're CPU bound, the program will run much faster. If you're IO-bound, the program won't run much faster (since all the work is actually just reading from disk). 3) Figure out how to read 10 records at a time and farm out the records to threads. Hopefully, your program will take significantly less time. At this point, don't obsess over collating data. It might not be 10 times as fast, but it should be somewhat faster to be worth your while. 4) If the threaded approach shows promise, make sure that you can still generate correct output with that approach (in other words, figure out out synchronization and collating). At the end of that experiment, you should have a better feel on where to go next. What is the nature of your computation? Maybe it would be easier to tune the algorithm then figure out the multi-core optimization. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tools for refactoring/obfuscation
Hi, (sorry for replying to the old topic) On Tue, Mar 6, 2012 at 10:29 PM, Javier wrote: > I am looking for an automated tool for refactoring/obfuscation. > Something that changes names of functions, variables, or which would > merge all the functions of various modules in a single module. > The closest I have seen is http://bicyclerepair.sourceforge.net/ > > Does somebody know of something that can work from the command line or > simple data structures/text files?, like a python dictionary of functions > {"old":"new",...} I am not surprised what nobody answers. I think that such tool is nearly impossible given the dynamic Python's nature. But if you put little discipline/restrictions in your source code, when doing obfuscation could be much more easier. Almost trivial I would say. Javier, you can contact me directly if you are interested in this topic. Vladimir Ignatov kmisoft at gmail com -- http://mail.python.org/mailman/listinfo/python-list
OAuth 2.0 implementation
Hi all, I'm getting close to an alpha release of an OAuth 2.0 implementation (https://github.com/demianbrecht/py-sanction). High level features include: * Support for multiple providers (protocol deviations). This didn't seem to be supported by any library. * Actually an OAuth 2.0 implementation (python-oauth2 is a 2nd version of python-oauth, not an OAuth 2.0 implementation) * Support for the entire OAuth 2.0 spec. Most provide support for the authorization code grant flow (employed by all web server providers), but lacked support or extensibility for any other flows, credentials or other provider extensions) * 100% unit test coverage. Some employ TDD, others didn't. Current implementation includes support for Authorization Code Grant flow but can be extended to support others (including extensions) quite trivially. Current adapter implementations include: * Google * Facebook * Stack Exchange * Deviant Art * Foursquare It has yet to be heavily used or functionally tested (basic tests have been done in example/server.py) and documentation is being worked on. Just wanted to share some of my work and hopefully someone other than me can find some use for it :) P.S. For those interested, cloc stats are: http://cloc.sourceforge.net v 1.53 T=0.5 s (28.0 files/s, 1818.0 lines/s) --- Language files blankcomment code --- Python 14239196474 --- SUM:14239196474 --- -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
Demian Brecht writes: > I'm getting close to an alpha release of an OAuth 2.0 implementation > (https://github.com/demianbrecht/py-sanction). Thank you for doing this work. As someone who uses OpenID, what can I read about why OAuth is better? Everything I read is targeted toward either people *implementing* OAuth, or people who use “social networking”. Nothing much for people who want to control their own identity provider (in OpenID terms). Is OAuth not possible without relying on “social networking” centralised services? Can we use OAuth services without some Google or Facebook or other gatekeeper imposing itself in the transaction? -- \ “Never use a long word when there's a commensurate diminutive | `\available.” —Stan Kelly-Bootle | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
In article <87haxahh51@benfinney.id.au>, Ben Finney wrote: > Demian Brecht writes: > > > I'm getting close to an alpha release of an OAuth 2.0 implementation > > (https://github.com/demianbrecht/py-sanction). > > Thank you for doing this work. > > As someone who uses OpenID, what can I read about why OAuth is better? OpenID is for people who worry about things like how OpenID is different from OAuth. Oauth is for people who have no idea what OAuth is and just want to be able to log into web sites using their Facebook account. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote: > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: >> I know the print statement produces the same result when both of these >> two instructions are executed ,I just want to know Is there any >> difference between print 3 and print '3' in Python ? > > This is a non-question. The input is the same, the output is the same, > what else matters? def fib1(n): if n == 0: return 0 elif n == 1: return 1 f2, f1 = 0, 1 for _ in range(2, n+1): f2, f1 = f1, f2 + f1 return f1 def fib2(n): if n == 0: return 0 elif n == 1: return 1 else: return fib2(n-1) + fib2(n-2) Try calling fib1(35) and fib2(35). Still think only the input and output matter? :) For the record, fib2(35) ends up making a total of 29860703 function calls, compared to 35 iterations for fib1. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
Roy Smith writes: > In article <87haxahh51@benfinney.id.au>, > Ben Finney wrote: > > As someone who uses OpenID, what can I read about why OAuth is better? > > OpenID is for people who worry about things like how OpenID is different > from OAuth. Oauth is for people who have no idea what OAuth is and just > want to be able to log into web sites using their Facebook account. So, if I want to be free to choose an identity provider I trust, and it's not Facebook or Google or Twitter or other privacy-hostile services, how does OAuth help me do that? What can I read for how to become an OAuth user that doesn't assume I want a “social networking” provider involved in my identity transactions? -- \ “It is difficult to get a man to understand something when his | `\ salary depends upon his not understanding it.” —Upton Sinclair, | _o__) 1935 | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
In article <878vimhfdp@benfinney.id.au>, Ben Finney wrote: > Roy Smith writes: > > > In article <87haxahh51@benfinney.id.au>, > > Ben Finney wrote: > > > As someone who uses OpenID, what can I read about why OAuth is better? > > > > OpenID is for people who worry about things like how OpenID is different > > from OAuth. Oauth is for people who have no idea what OAuth is and just > > want to be able to log into web sites using their Facebook account. > > So, if I want to be free to choose an identity provider I trust, and > it's not Facebook or Google or Twitter or other privacy-hostile > services, how does OAuth help me do that? It doesn't. Well, in theory, it could, but in practice everybody's OAuth implementation is different enough that they don't interoperate. -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
Roy Smith writes: > In article <878vimhfdp@benfinney.id.au>, > Ben Finney wrote: > > So, if I want to be free to choose an identity provider I trust, and > > it's not Facebook or Google or Twitter or other privacy-hostile > > services, how does OAuth help me do that? > > It doesn't. Well, in theory, it could, but in practice everybody's > OAuth implementation is different enough that they don't interoperate. Thanks. So OAuth is a pseudo-standard that is implemented incompatibly to the extent that it doesn't actually give users the freedom to migrate their existing data and identity at will to any other OAuth implementor? -- \ “Money is always to be found when men are to be sent to the | `\ frontiers to be destroyed: when the object is to preserve them, | _o__) it is no longer so.” —Voltaire, _Dictionnaire Philosophique_ | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there any difference between print 3 and print '3' in Python ?
On Mar 27, 8:43 am, Steven D'Aprano wrote: > On Mon, 26 Mar 2012 08:11:03 -0400, Dave Angel wrote: > > On 03/26/2012 07:45 AM, redstone-c...@163.com wrote: > >> I know the print statement produces the same result when both of these > >> two instructions are executed ,I just want to know Is there any > >> difference between print 3 and print '3' in Python ? > > > This is a non-question. The input is the same, the output is the same, > > what else matters? > > def fib1(n): > if n == 0: return 0 > elif n == 1: return 1 > f2, f1 = 0, 1 > for _ in range(2, n+1): > f2, f1 = f1, f2 + f1 > return f1 > > def fib2(n): > if n == 0: return 0 > elif n == 1: return 1 > else: return fib2(n-1) + fib2(n-2) > > Try calling fib1(35) and fib2(35). Still think only the input and output > matter? :) > > For the record, fib2(35) ends up making a total of 29860703 function > calls, compared to 35 iterations for fib1. > > -- > Steven http://www.cse.buffalo.edu/~rapaport/intensional.html -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
On Tue, Mar 27, 2012 at 12:24 AM, Ben Finney wrote: > Roy Smith writes: > >> In article <878vimhfdp@benfinney.id.au>, >> Ben Finney wrote: >> > So, if I want to be free to choose an identity provider I trust, and >> > it's not Facebook or Google or Twitter or other privacy-hostile >> > services, how does OAuth help me do that? >> >> It doesn't. Well, in theory, it could, but in practice everybody's >> OAuth implementation is different enough that they don't interoperate. > > Thanks. So OAuth is a pseudo-standard that is implemented incompatibly > to the extent that it doesn't actually give users the freedom to migrate > their existing data and identity at will to any other OAuth implementor? Pretty much. It is nice that it is published as a standard at all but the standard is just whatever people are actually doing. It seems less hostile when you think of it as vigorous documentation instead of protocols set in stone. -Jack -- http://mail.python.org/mailman/listinfo/python-list
Re: OAuth 2.0 implementation
On Monday, 26 March 2012 21:24:35 UTC-7, Ben Finney wrote: > Roy Smith writes: > > > In article <878vimhfdp@benfinney.id.au>, > > Ben Finney wrote: > > > So, if I want to be free to choose an identity provider I trust, and > > > it's not Facebook or Google or Twitter or other privacy-hostile > > > services, how does OAuth help me do that? > > > > It doesn't. Well, in theory, it could, but in practice everybody's > > OAuth implementation is different enough that they don't interoperate. > > Thanks. So OAuth is a pseudo-standard that is implemented incompatibly > to the extent that it doesn't actually give users the freedom to migrate > their existing data and identity at will to any other OAuth implementor? > > -- > \ “Money is always to be found when men are to be sent to the | > `\ frontiers to be destroyed: when the object is to preserve them, | > _o__) it is no longer so.” —Voltaire, _Dictionnaire Philosophique_ | > Ben Finney OAuth 2.0 is the emerging standard (now passed on to IETF) to deal with providing access to protected resources. OpenID is a standard used to deal with authentication. While there is some overlap (OAuth can be used for authentication as well), the goals of the two protocols are different. OAuth 2.0 is still in draft status (draft 25 is the current one I believe) and yes, unfortunately every single server available at this point have varying degrees of separation from the actual spec. It's not a pseudo-standard, it's just not observed to the letter. Google is the closest and Facebook seems to be the farthest away (Stack Exchange is in close second due to building theirs to work like Facebook's). That was pretty much how this work was born. I wanted to be able to implement authentication and resource access over multiple providers with a single code base. So, in answer to your questions: 1) If you're only looking for a solution to authentication, OAuth is no better than OpenID. Having said that, with the apparent popularity of OAuth 2.0, more providers may support OAuth than will OpenID (however, that's just my assumption). 2) OAuth is all about centralized services in that it is how providers allow access to protected resources. Whether it's a social network or SaaS (such as Harvest: http://www.getharvest.com/), if there isn't exposure to protected resources, then OAuth becomes pointless. 3) If you're looking to implement OAuth authentication with a provider that you trust, grab the sanction source, implement said provider and send a pull request ;) 4) Data migration doesn't happen with OAuth. As the intent is to allow access to protected resources, migrating Google to say, Facebook just wouldn't happen :) Hope that makes sense and answers your questions. - Demian -- http://mail.python.org/mailman/listinfo/python-list
Re: why did GMPY change the names of its functions?
On Mar 26, 1:33 pm, cas...@gmail.com wrote: > On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > > What's the justification for that? I use those functions extensively > > in my library of Collatz utilities and I had to re-edit them for no > > obvious reason. > > I'll speak up as the maintainer of GMPY and GMPY2. > > (My comments apply to the beta1 release which should be out in a couple of > days.) > > GMPY2 introduces many changes: > > 1) the limited "mpf" type that is based on GMP has been replaced with the > "mpfr" type from the MPFR library > 2) support for multiple-precision complex arithmetic based on the MPC library > 3) support for a mutable integer type optimized for in-place bit manipulations > 4) support for addition number theory functions (generalized Lucas sequences > and more primality tests > > I began to encounter name collisions; for example, should sqrt() only return > integer square roots. I chose to call it a new name (gmpy2) and update the > API to reflect new choices I made. For example, sqrt() now returns an "mpfr" > and isqrt() returns an "mpz". > > As part of the documentation for the beta release, I will document the name > changes. "import gmpy2 as gmpy; gmpy.scan0=gmpy.bit_scan0; etc" should work > just fine. > > If you encounter problems with the alpha release, please open an issue on > gmpy's site. > > Thanks, > casevh Thanks for the explanation. Sorry if I flew off the handle, but I've been sick recently and am trying to get back into Python after 2 years. I just bought a new laptop, downloaded Python 3.2 & GMPY2 only to see my Collatz library crash. At first, given my current condition, I was afraid I'd never get it to work again. But luckily, the fix is simple. I, for one, really appreciate your maintaining GMPY, otherwise, I might have to abandon Python. I'll keep my eyes open for any further problems. Thanks Gain. > > > > On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > > What's the justification for that? I use those functions extensively > > in my library of Collatz utilities and I had to re-edit them for no > > obvious reason. > On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > > What's the justification for that? I use those functions extensively > > in my library of Collatz utilities and I had to re-edit them for no > > obvious reason. > On Sunday, March 25, 2012 9:59:56 PM UTC-7, Mensanator wrote: > > OK, GMPY is now called GMPY2. No big deal, I can import as GMPY. > > > But why were scan0 and scan1 changed to bit_scan0 and bit_scan1? > > > What's the justification for that? I use those functions extensively > > in my library of Collatz utilities and I had to re-edit them for no > > obvious reason. -- http://mail.python.org/mailman/listinfo/python-list
Re: concurrent file reading/writing using python
Thanks for the advice Dennis. @Steve : I haven't actually written the code. I was thinking more on the generic side and wanted to check if what I thought made sense and I now realize it can depend on then the I/O. For starters I was just thinking about counting lines in a line without doing any computation so this can be strictly I/O bound. I guess what I need to ask was can we improve on the existing disk I/O performance by reading different portions of the file using threads or processes. I am kind of pointing towards a MapReduce task on a file in a shared file system such as GPFS(from IBM). I realize this can be more suited to HDFS but wanted to know if people have implemented something similar on a normal linux based NFS -Abhi On Mon, Mar 26, 2012 at 6:44 PM, Steve Howell wrote: > On Mar 26, 3:56 pm, Abhishek Pratap wrote: >> Hi Guys >> >> I am fwding this question from the python tutor list in the hope of >> reaching more people experienced in concurrent disk access in python. >> >> I am trying to see if there are ways in which I can read a big file >> concurrently on a multi core server and process data and write the >> output to a single file as the data is processed. >> >> For example if I have a 50Gb file, I would like to read it in parallel >> with 10 process/thread, each working on a 10Gb data and perform the >> same data parallel computation on each chunk of fine collating the >> output to a single file. >> >> I will appreciate your feedback. I did find some threads about this on >> stackoverflow but it was not clear to me what would be a good way to >> go about implementing this. >> > > Have you written a single-core solution to your problem? If so, can > you post the code here? > > If CPU isn't your primary bottleneck, then you need to be careful not > to overly complicate your solution by getting multiple cores > involved. All the coordination might make your program slower and > more buggy. > > If CPU is the primary bottleneck, then you might want to consider an > approach where you only have a single thread that's reading records > from the file, 10 at a time, and then dispatching out the calculations > to different threads, then writing results back to disk. > > My approach would be something like this: > > 1) Take a small sample of your dataset so that you can process it > within 10 seconds or so using a simple, single-core program. > 2) Figure out whether you're CPU bound. A simple way to do this is > to comment out the actual computation or replace it with a trivial > stub. If you're CPU bound, the program will run much faster. If > you're IO-bound, the program won't run much faster (since all the work > is actually just reading from disk). > 3) Figure out how to read 10 records at a time and farm out the > records to threads. Hopefully, your program will take significantly > less time. At this point, don't obsess over collating data. It might > not be 10 times as fast, but it should be somewhat faster to be worth > your while. > 4) If the threaded approach shows promise, make sure that you can > still generate correct output with that approach (in other words, > figure out out synchronization and collating). > > At the end of that experiment, you should have a better feel on where > to go next. > > What is the nature of your computation? Maybe it would be easier to > tune the algorithm then figure out the multi-core optimization. > > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list