Re: Pygobject style question

2020-08-02 Thread Cameron Simpson
On 01Aug2020 13:32, Chris Green wrote: >Having (after lots of help from here, thank you) finally converted my >Python 2, gtk 2 program to Python 3 and pygobject gtk 3 I'm left with >a couple of what might be called style questions. > >I guess it's mostly down to what feels right to me but there

Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 03:00 am, Steven D'Aprano wrote: > On Sun, 29 May 2016 02:15 am, Gerald Britton wrote: > >> suppose I have a simple python project setup like this: [...] To which I responded: > If this is a single project, why do you set it up like this? Is there a > reason why you don't

Re: Package setup best practice style question

2016-05-28 Thread Steven D'Aprano
On Sun, 29 May 2016 02:15 am, Gerald Britton wrote: > suppose I have a simple python project setup like this: > > Project diectory > prog.py > pkg directory > __init__.py > mod1.py >class A: If this is a single project, why do you set it up like

Package setup best practice style question

2016-05-28 Thread Gerald Britton
suppose I have a simple python project setup like this: Project diectory prog.py pkg directory __init__.py mod1.py class A: In order to have class A (unqualified) available from prog.py, there are a few options that I know about. I'm currently

Re: Python Style Question

2014-11-12 Thread Anton
On Thursday, October 30, 2014 4:10:23 AM UTC-7, Steven D'Aprano wrote: I don't particularly like either version. I prefer this: def load_int(obj): if isinstance(obj, int): # Case 1), an int, e.g. 7 return obj elif isinstance(obj, str): # Case 2) and 3), a

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of 'int_value', e.g. '7' 3) a string with a json serialization of an integer value, e.g. '7' 4) something else that should be

Re: Python Style Question

2014-10-30 Thread MRAB
On 2014-10-30 11:10, Steven D'Aprano wrote: Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of 'int_value', e.g. '7' 3) a string with a json serialization of an integer value, e.g.

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
MRAB wrote: On 2014-10-30 11:10, Steven D'Aprano wrote: Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of 'int_value', e.g. '7' 3) a string with a json serialization of an

Re: Python Style Question

2014-10-30 Thread Roy Smith
In article 54521c8f$0$12982$c3e8da3$54964...@news.astraweb.com, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form

Re: Python Style Question

2014-10-30 Thread Steven D'Aprano
Roy Smith wrote: In article 54521c8f$0$12982$c3e8da3$54964...@news.astraweb.com, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value

Re: Python Style Question

2014-10-30 Thread Denis McMahon
On Fri, 31 Oct 2014 09:48:10 +1100, Steven D'Aprano wrote: MRAB wrote: How about: int(str(obj).strip('')) Absolutely not. obj = '1\n\n\n\n' # not valid JSON load_int(obj) = raises ValueError int(str(obj).strip('')) = wrongly returns 1 How about #!/usr/bin/python import re, json

Re: Python Style Question

2014-10-29 Thread Martin Kemp
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 29/10/2014 10:45, Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of 'int_value', e.g. '7' 3) a string with a json serialization of

Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add function but they have append. I think you could do better with something like == import json l = [1, -1, 0, '1', '-1', '0', json.dumps(-1), json.dumps(1), json.dumps(0), 'x', 'sqjklsqjk__', (1, 2)] values = [] for c

Re: Python Style Question

2014-10-29 Thread Rafael Romero Carmona
2014-10-29 12:25 GMT+01:00 Martin Kemp martin.k...@ingg.com: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 29/10/2014 10:45, Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following options: 1) an integer value 2) a string in form of

Re: Python Style Question

2014-10-29 Thread Martin Kemp
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 29/10/2014 12:01, Rafael Romero Carmona wrote: 2014-10-29 12:25 GMT+01:00 Martin Kemp martin.k...@ingg.com: On 29/10/2014 10:45, Anton wrote: Let's say I have an incoming list of values *l*. Every element of *l* can be one of the following

Re: Python Style Question

2014-10-29 Thread Anton
On Wednesday, October 29, 2014 4:43:33 AM UTC-7, Rafael Romero Carmona wrote: Hi, first in Python 2.7.6 and Python 3.4.0 list haven't got any add function but they have append. You are right, in my original code I use set instead of array, so it should be either values = set() or

Re: Python Style Question

2014-10-29 Thread Anton
On Wednesday, October 29, 2014 4:59:25 AM UTC-7, Rafael Romero Carmona wrote: 2014-10-29 12:25 GMT+01:00 Martin Kemp mar...@ingg.com: Actually it doesn't work because there is no add function and it doesn't catch the TypeError function to ignore other exceptions than ValueError. Doesn't it? I

Re: If/then style question

2010-12-21 Thread Francesco
I'd bet you would stress your point Steven! But you don't need to persuade me, I do already agree. I just meant to say that, when the advantage is little, there's no need to rewrite a working function. And that with modern CPUs, if tests take so little time, that even some redundant one is not

Re: If/then style question

2010-12-19 Thread Steven D'Aprano
On Sat, 18 Dec 2010 19:59:45 -0800, Carl Banks wrote: On Dec 17, 12:23 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: Even without the cleanup issue, sometimes you want to edit a function to affect all return values

Re: If/then style question

2010-12-18 Thread Francesco
On 17/12/2010 0.51, Steven D'Aprano wrote: Don't get me wrong... spaghetti code is*bad*. But there are other ways of writing bad code too, and hanging around inside a function long after you've finished is also bad: def function(arg): done = False do_something() if

Re: If/then style question

2010-12-18 Thread Steven D'Aprano
On Sat, 18 Dec 2010 12:29:31 +0100, Francesco wrote: [...] I agree to your point, but I'm afraid you chose a wrong example (AFAIK, and that's not much). Sure, the second version of function(arg) is much more readable, but why do you think the first one would do *lots* of unnecessary work?

Re: If/then style question

2010-12-18 Thread Carl Banks
On Dec 17, 12:23 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: Even without the cleanup issue, sometimes you want to edit a function to affect all return values somehow.  If you have a single exit point you just make

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Thu, 16 Dec 2010 20:32:29 -0800, Carl Banks wrote: Even without the cleanup issue, sometimes you want to edit a function to affect all return values somehow. If you have a single exit point you just make the change there; if you have mulitple you have to hunt them down and change all of

Re: If/then style question

2010-12-17 Thread Jean-Michel Pichavant
John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self, arg1, arg2

RE: If/then style question

2010-12-17 Thread Rob Richardson
-Original Message- What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if the we never hit the break

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: First, just to clarify, I don't think the indentation I saw was what was originally posted. The else must be indented to match the if, and the two statements under else are in the else block. The return statement is indented at the

Re: If/then style question

2010-12-17 Thread Jean-Michel Pichavant
Rob Richardson wrote: -Original Message- What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if the

Re: If/then style question

2010-12-17 Thread David Robinow
On Thu, Dec 16, 2010 at 6:51 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: ... Functions always have one entry. The only way to have multiple entry points is if the language allows you to GOTO into the middle of a function, and Python sensibly does not allow this. The one

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 9:38 AM, Steven D'Aprano wrote: On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote: First, just to clarify, I don't think the indentation I saw was what was originally posted. The else must be indented to match the if, and the two statements under else are in the else

RE: If/then style question

2010-12-17 Thread Rob Richardson
My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if

Re: If/then style question

2010-12-17 Thread Tim Golden
On 17/12/2010 15:53, Steve Holden wrote: [... snip example of for-else ...] This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and realise there are useful applications. I use

Re: If/then style question

2010-12-17 Thread Paul Rubin
Jean-Michel Pichavant jeanmic...@sequans.com writes: What about, def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'), ]: if not condition: break else: do_some_usefull_stuff() # executed only if

Re: If/then style question

2010-12-17 Thread Ethan Furman
Rob Richardson wrote: My thanks for pointing out the existence of the else: suite in the for statement. However, I remain confused. For reference, here's the original code: def myMethod(): for condition, exitCode in [ (cond1, 'error1'), (cond2, 'very bad error'),

Re: If/then style question

2010-12-17 Thread Steve Holden
On 12/17/2010 11:13 AM, Tim Golden wrote: On 17/12/2010 15:53, Steve Holden wrote: [... snip example of for-else ...] This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and

Re: If/then style question

2010-12-17 Thread Mark Wooding
Steve Holden st...@holdenweb.com writes: I think the choice of keyword is probably not Guido's crowning language achievement, I remember the behaviour by considering a typical application: for thing in things: if shinyp(thing): break else: raise

Re: If/then style question

2010-12-17 Thread Arnaud Delobelle
Tim Golden m...@timgolden.me.uk writes: On 17/12/2010 15:53, Steve Holden wrote: [... snip example of for-else ...] This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and

Re: If/then style question

2010-12-17 Thread python
I now remember this idiom as the break else construct: either the loop breaks, or the else: suite is executed. A perfect description. Malcolm -- http://mail.python.org/mailman/listinfo/python-list

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Stefan Sonnenberg-Carstens stefan.sonnenb...@pythonmeister.com wrote: The advantage in latter case is fewer operations, because you can skip the assignments, and it is more readable. The one entry, one exit is an advice. Not a law. Your code is OK. As long as it works ;-)

Re: If/then style question

2010-12-17 Thread Grant Edwards
On 2010-12-16, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I

RE: If/then style question

2010-12-17 Thread Rob Richardson
-Original Message- You have outlined what happens when cond1 and cond2 both evaluate to True -- what happens if, say, cond2 evaluates to False? - I reply And the light goes on! (And palm strikes forehead.) I was thinking that the error we were processing was raised

Re: If/then style question

2010-12-17 Thread Kev Dwyer
On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 10:53:45 -0500, Steve Holden wrote about for...else: This construct appears to be unpopular in actual use, and when it comes up in classes and seminars there is always interesting debate as people discuss potential uses and realise there are useful applications. Yes, I

Re: If/then style question

2010-12-17 Thread Steven D'Aprano
On Fri, 17 Dec 2010 17:26:08 +, Grant Edwards wrote: Give me code that's easy-to-read and doesn't work rather code that works and can't be read any day. Well, in that case, you'll love my new operating system, written in 100% pure Python: [start code] print(this is an operating system)

If/then style question

2010-12-16 Thread John Gordon
(This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self, arg1, arg2): if some_good_condition

Re: If/then style question

2010-12-16 Thread Ethan Furman
John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod(self, arg1, arg2

Re: If/then style question

2010-12-16 Thread Tim Harig
On 2010-12-16, John Gordon gor...@panix.com wrote: I like this style more, mostly because it eliminates a lot of indentation. However I recall one of my college CS courses stating that one entry, one exit was a good way to write code, and this style has lots of exits. So, take the good

Re: If/then style question

2010-12-16 Thread Grant Edwards
On 2010-12-16, John Gordon gor...@panix.com wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def

Re: If/then style question

2010-12-16 Thread Stefan Sonnenberg-Carstens
Am 16.12.2010 22:49, schrieb John Gordon: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like this: def myMethod

Re: If/then style question

2010-12-16 Thread Ryan Kelly
On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like

Re: If/then style question

2010-12-16 Thread Ian Kelly
On Thu, Dec 16, 2010 at 3:41 PM, Stefan Sonnenberg-Carstens stefan.sonnenb...@pythonmeister.com wrote: return [x for x,y in ((bad1,some_bad_condition),(bad2,some_other_bad_condition),(bad3,yet_another_bad_condition),(good1,do_some_useful_stuff() or True)) if x][0] This doesn't work.

Re: If/then style question

2010-12-16 Thread Steven D'Aprano
On Thu, 16 Dec 2010 21:49:07 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion will be appreciated!) When I started learning Python, I wrote a lot of methods that looked like

Re: If/then style question

2010-12-16 Thread alex23
John Gordon gor...@panix.com wrote: But lately I've been preferring this style:   def myMethod(self, arg1, arg2):     if some_bad_condition:       return bad1     elif some_other_bad_condition:       return bad2     elif yet_another_bad_condition:       return bad3    

Re: If/then style question

2010-12-16 Thread Joel Koltner
Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote in message news:4d0aa5e7$0$29997$c3e8da3$54964...@news.astraweb.com... It doesn't look like you were learning Python. It looks like you were learning C with Python syntax :( True, although in many cases one has to interface to legacy C

Re: If/then style question

2010-12-16 Thread Carl Banks
On Dec 16, 2:56 pm, Ryan Kelly r...@rfk.id.au wrote: On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere.  If so, a pointer to that discussion will be appreciated!) When I started learning

Re: If/then style question

2010-12-16 Thread Steve Holden
On 12/16/2010 11:32 PM, Carl Banks wrote: On Dec 16, 2:56 pm, Ryan Kelly r...@rfk.id.au wrote: On Thu, 2010-12-16 at 21:49 +, John Gordon wrote: (This is mostly a style question, and perhaps one that has already been discussed elsewhere. If so, a pointer to that discussion

Re: Python Style Question

2009-01-22 Thread Steve Holden
K-Dawg wrote: I am trying to become more pythonic as I learn python and get my mind around it instead of other languages I have used. I have an app that has a series of classes for objects it uses. From a style perspective, which should be done: Different py file for each class or

Re: Python Style Question

2009-01-22 Thread Terry Reedy
Steve Holden wrote: K-Dawg wrote: I am trying to become more pythonic as I learn python and get my mind around it instead of other languages I have used. I have an app that has a series of classes for objects it uses. From a style perspective, which should be done: Different py file for each

Re: docstrings style question

2008-01-10 Thread Russ P.
On Jan 9, 11:51 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: Steve Brown wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class Test3(ar_test.AR_TEST): Temperature Sense Test I don't like the duplicated

Re: docstrings style question

2008-01-10 Thread Jeroen Ruigrok van der Werven
-On [20080110 06:51], Steve Brown ([EMAIL PROTECTED]) wrote: I don't like the duplicated information: But the comment is attractive, I find it unattractive to be honest. and the docstring self.__doc__ is already in use in the test log. I've read that all modules and classes should have

RE: docstrings style question

2008-01-10 Thread Ryan Ginstrom
On Behalf Of Steve Brown What do you think? I think that comments are for maintainers, and docstrings are for users. Some of the things I use comments for: * Visually separate classes (using a syntax-highlighting editor) * Explain algorithm choices * Explain bug fixes so I don't later fix

Re: docstrings style question

2008-01-10 Thread Martin Marcher
Russ P. wrote: On Jan 9, 9:47 pm, Steve Brown [EMAIL PROTECTED] wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class Test3(ar_test.AR_TEST): Temperature Sense Test I don't like the duplicated information: But the

Re: docstrings style question

2008-01-10 Thread Steve Brown
Russ P. [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Jan 9, 11:51 pm, Fredrik Lundh [EMAIL PROTECTED] wrote: Steve Brown wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class

Re: docstrings style question

2008-01-10 Thread Neil Cerutti
On Jan 10, 2008 12:47 AM, Steve Brown [EMAIL PROTECTED] wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class Test3(ar_test.AR_TEST): Temperature Sense Test I don't like the duplicated information: But the comment

Re: docstrings style question

2008-01-10 Thread Steve Brown
Neil Cerutti [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Jan 10, 2008 12:47 AM, Steve Brown [EMAIL PROTECTED] wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class Test3(ar_test.AR_TEST):

Re: docstrings style question

2008-01-10 Thread Steve Brown
What I'm trying to do with the tests is pare them back so that the code explicitly and concisely documents the tests. It is important that the comments and doc strings NOT contain information about how Temperature Sense works because that is outside the scope of the test. More generally,

Re: docstrings style question

2008-01-10 Thread Steven D'Aprano
On Fri, 11 Jan 2008 13:09:26 +1100, Steve Brown wrote: What I'm trying to do with the tests is pare them back so that the code explicitly and concisely documents the tests. Yes, this is good. It is important that the comments and doc strings NOT contain information about how Temperature

Re: docstrings style question

2008-01-09 Thread Fredrik Lundh
Steve Brown wrote: I've got a series of modules which look like this: # # # Temperature Sense Test # # class Test3(ar_test.AR_TEST): Temperature Sense Test I don't like the duplicated information: But the comment is attractive, and the docstring

Non-ASCII languages (was: Re: style question)

2006-07-01 Thread Jorgen Grahn
On Thu, 29 Jun 2006 23:19:34 +0200, Fredrik Lundh [EMAIL PROTECTED] wrote: Jorgen Grahn wrote: ... (I like well-typeset code in print though. Bjarne Stroustrup uses an elegant system for C++ code, where identifiers and strings are in Times italic, operators in Courier, and so on.) the idea

Re: Non-ASCII languages (was: Re: style question)

2006-07-01 Thread Nick Maclaren
In article [EMAIL PROTECTED], Jorgen Grahn [EMAIL PROTECTED] writes: | | Possibly true, and definitely for Knuth. But WYSIWYG was unknown at the | time; these people all programmed using fixed-width fonts, on teletypes or | character-mapped terminals. Hell, even full-screen editors were new and

calling functions style question

2006-06-06 Thread Brian
I just have a basic style question here. Suppose you have the program: def foo1(): do something def foo2() do something else Assume that you want to call these functions at execution. Is it more proper to call them directly like: foo1() foo2() or in an if __name__ == __main__

Re: calling functions style question

2006-06-06 Thread Thomas Nelson
that the script runs its main functions when called as a standalone program, but you can also import the code and do something with it without setting off those functions. THN Brian wrote: I just have a basic style question here. Suppose you have the program: def foo1(): do something def foo2

Re: calling functions style question

2006-06-06 Thread Kay Schluehr
Brian wrote: I just have a basic style question here. Suppose you have the program: def foo1(): do something def foo2() do something else Assume that you want to call these functions at execution. Is it more proper to call them directly like: foo1() foo2