Re: WANT: bad code in python (for refactoring example)

2017-02-16 Thread alister
On Wed, 15 Feb 2017 19:08:59 -0800, Paul Rubin wrote:

> Antoon Pardon  writes:
>> On reason to use this is for some easy "logging"
> 
> I think it's better to use the actual logging module.  I generally start
> a new program with print statements but convert them to logging after
> there's enough code to want to be more organized about it.

my default python template file includes basic logging setup from the 
start
(all 3 lines of it )



-- 
"...if the church put in half the time on covetousness that it does on 
lust,
 this would be a better world."  - Garrison Keillor, "Lake Wobegon Days"
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: WANT: bad code in python (for refactoring example)

2017-02-16 Thread Cecil Westerhof
On Thursday 16 Feb 2017 10:43 CET, alister wrote:

> On Wed, 15 Feb 2017 19:08:59 -0800, Paul Rubin wrote:
>
>> Antoon Pardon  writes:
>>> On reason to use this is for some easy "logging"
>>
>> I think it's better to use the actual logging module. I generally
>> start a new program with print statements but convert them to
>> logging after there's enough code to want to be more organized
>> about it.
>
> my default python template file includes basic logging setup from
> the start (all 3 lines of it )

You should have shared your template, or at least those three lines.
:-P

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: WANT: bad code in python (for refactoring example)

2017-02-16 Thread alister
On Thu, 16 Feb 2017 12:00:36 +0100, Cecil Westerhof wrote:

> On Thursday 16 Feb 2017 10:43 CET, alister wrote:
> 
>> On Wed, 15 Feb 2017 19:08:59 -0800, Paul Rubin wrote:
>>
>>> Antoon Pardon  writes:
 On reason to use this is for some easy "logging"
>>>
>>> I think it's better to use the actual logging module. I generally
>>> start a new program with print statements but convert them to logging
>>> after there's enough code to want to be more organized about it.
>>
>> my default python template file includes basic logging setup from the
>> start (all 3 lines of it )
> 
> You should have shared your template, or at least those three lines.
> :-P

import logging
logger=logging.getLogger()
logging.basicConfig(level=logging.DEBUG)

now simply add logger.debug('debug message')
wherever needed instead of print statements

alternatively
import logging
logger=logging.getLogger()
logging.basicConfig(filename='logfile',level=logging.DEBUG)

if you would prefer to log to a file instead.

simples :-)



-- 
A New Way of Taking Pills
A physician one night in Wisconsin being disturbed by a burglar, 
and
having no ball or shot for his pistol, noiselessly loaded the weapon with
small, hard pills, and gave the intruder a "prescription" which he thinks
will go far towards curing the rascal of a very bad ailment.
-- Nevada Morning Transcript, January 30, 1861
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PTH files: Abs paths not working as expected. Symlinks needed?

2017-02-16 Thread Terry Reedy

On 2/15/2017 7:42 AM, poseidon wrote:


what are pth files for?


They are for extending (mainly) lib/site-packages.  To repeat what I 
have posted before: every time I install a new version of Python, I add 
(copy) python.pth containing 'F:/python' (without the quotes).  This 
makes my  directory of modules and packages, F:/python, an extension of 
the new site-packages.  In effect, the .pth file 'installs', all at 
once, all my personal projects in the new version site-packages. This is 
done without symlinking each individual project directory.  It is also 
done *without* uninstalling projects from previous versions.


As a result, files can be run with -m from a command line just as they 
could be when in site-packages.  For instance, 'py -3.6 -m xploro' runs 
F:/python/xploro/__main__ with python 3.6.  And I can replace '3.6' with 
3.5 or 2.7'


I can also use absolute imports within a project/package the same as if 
the xploro directory were in each versions site-packages.  For instance, 
'from xplore import test' imports F:/python/xplore/test.  There is no 
hassle with relative imports, and if I ever put the package on PyPI, no 
change to imports will be needed.


I just verified that dropping python.pth into the site-packages 
directory of my locally built python 3.7.0a0 lets me easily test that 
xploro tests run on the next version of python.


--
Terry Jan Reedy


--
https://mail.python.org/mailman/listinfo/python-list


Re: WANT: bad code in python (for refactoring example)

2017-02-16 Thread Makoto Kuwata
Thanks Erik,


On Thu, Feb 16, 2017 at 6:53 AM, Erik  wrote:

> (which is what I think you mean by "proper size")
>

As I explained at my first post, I'm teaching Python to novice programmers.
Therefore refactoring example code should be in the size what they can
understand.
It is hard for them to read and understand over than thousand lines of code.

Almost of all projects have code over than thousand lines of code,
but it is possible to quote a part of them for refactoring example, I think.



>
> (Python code examples of what you think is "bad" vs "good" would be
> useful).
>

You are right.

Bad code Example:

#
https://codewords.recurse.com/issues/one/an-introduction-to-functional-programming

from random import random

def move_cars(car_positions):
return map(lambda x: x + 1 if random() > 0.3 else x,
   car_positions)

def output_car(car_position):
return '-' * car_position

def run_step_of_race(state):
return {'time': state['time'] - 1,
'car_positions': move_cars(state['car_positions'])}

def draw(state):
print ''
print '\n'.join(map(output_car, state['car_positions']))

def race(state):
draw(state)
if state['time']:
race(run_step_of_race(state))

race({'time': 5,
  'car_positions': [1, 1, 1]})


Refactoring example:

from random import random

class Car(object):

def __init__(self):
self.position = 1

def move(self):
if random() > 0.3:
self.position += 1
return self.position

class Race(object):

def __init__(self, n_cars=3):
self._cars = [ Car() for _ in range(n_cars) ]

def round(self):
for car in self._cars:
car.move()

def report(self):
print("")
for car in self._cars:
print('-' * car.position)

def run(self, n_rounds=5):
self.report()
for _ in range(n_rounds):
self.round()
self.report()

if __name__ == '__main__':
Race(3).run(5)


--
regards,
makoto kuwata
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PTH files: Abs paths not working as expected. Symlinks needed?

2017-02-16 Thread Rustom Mody
On Friday, February 17, 2017 at 3:24:32 AM UTC+5:30, Terry Reedy wrote:
> On 2/15/2017 7:42 AM, poseidon wrote:
> 
> > what are pth files for?
> 
> They are for extending (mainly) lib/site-packages.  


Hey Terry!
This needs to get into more public docs than a one-off post on a newsgroup/ML
-- 
https://mail.python.org/mailman/listinfo/python-list