Re: Has color "Green" changed from Python 33 to 34 ?

2014-10-31 Thread Christian Gollwitzer

Am 30.10.14 12:23, schrieb ast:

I just updated this morning my Python from a 3.3rc to 3.4 (Windows) and
I noticed that the 'Green' color in tkinter GUI is not the same at all.

'Green' in 3.4 is very dark. I had to replace it with 'Lime' to
get back a nice 'Green'.


If you are dependent on the exact RGB value, just use a hex triplet, 
this will be passed unmodified to the OS and never change.


A 100% green would be '#00FF00'.

Note that still the OS may choose to apply color profiles etc. to this 
color, but this is outside of the control of the application.


Christian

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


Re: pySerial works in miniterm but not in my app

2014-10-31 Thread Dario
Il giorno giovedì 30 ottobre 2014 23:57:40 UTC+1, Dennis Lee Bieber ha scritto:

> >sw o01 + <--- I send this

> How do you "send this"... 

I just type or paste it in miniterm and hit return. Miniterm sends the return 
as CR, but it also works with CRLF.

> >sw o01 + Command OK <--- device does what it should *and* I receive this

> Is that a new line, or somehow part of the same line?

This is the response from the device, saying that my command was ok, and it 
also does what it should (it's an hdmi 4-4 matrix, it switches the first output 
to the next input).

> >s.write('sw o01 +\r')

> What happens if you use \n... or \r\n ?

In my app, that doesn't make any difference, never works. In miniterm, only CR 
and CRLF work, LF alone does not.

This evening I'll try with com0com to see what is really sent...

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


Re: Classes

2014-10-31 Thread ast


"Seymore4Head"  a écrit dans le message de 
news:51755at03r0bidjqh3qf0hhpvjr8756...@4ax.com...

class pet:
   def set_age(self,age):
   self.age=age
   def get_age(self):
   return self.age
pax=pet
pax.set_age(4)

Traceback (most recent call last):
 File "C:\Functions\test.py", line 18, in 
   pax.set_age(4)
TypeError: set_age() missing 1 required positional argument: 'age'

I am trying to pass 4 as the age.  Obviously I am doing it wrong.



Hi

I am a beginner too, but I find it strange that your
pet class has no __init__ method to construct
instances 


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


Re: Classes

2014-10-31 Thread ast


"ast"  a écrit dans le message de 
news:545350c3$0$23449$426a7...@news.free.fr...


I am a beginner too, but I find it strange that your
pet class has no __init__ method to construct
instances


It works anyway because __init__ is taken in the
parent Class, probably Object.

But this means that a method can create a new
attribute not previously created with __init__.
Is that correct ? 


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


Re: Classes

2014-10-31 Thread Chris Angelico
On Fri, Oct 31, 2014 at 8:18 PM, ast  wrote:
>
> But this means that a method can create a new
> attribute not previously created with __init__.
> Is that correct ?

That's always the case. There's nothing special about __init__ for
creating attributes.

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


Re: Classes

2014-10-31 Thread alister
On Thu, 30 Oct 2014 17:34:57 -0400, Seymore4Head wrote:

> On Thu, 30 Oct 2014 14:28:19 -0700, Larry Hudson 
> wrote:
> 
>>On 10/30/2014 01:16 PM, Seymore4Head wrote:
>>> class pet:
>>>  def set_age(self,age):
>>>  self.age=age
>>>  def get_age(self):
>>>  return self.age
>>> pax=pet pax.set_age(4)
>>>
>>> Traceback (most recent call last):
>>>File "C:\Functions\test.py", line 18, in 
>>>  pax.set_age(4)
>>> TypeError: set_age() missing 1 required positional argument: 'age'
>>>
>>> I am trying to pass 4 as the age.  Obviously I am doing it wrong.
>>>
>>You have already received the answer -- pax=pet should be pax=pet(), but
>>I have a simple side-comment about style.  It is common Python
>>convention to capitalize class names, IOW make this class Pet instead of
>>class pet.  This is convention not a requirement, but it does help
>>distinguish class names from ordinary variable names -- especially to
>>others reading your code (as well as yourself a few days later).   ;-)
>>
>>  -=- Larry -=-
> 
> I try to take typing shortcuts and it bites me in the behind.
> Good suggestion Thanks

If shortcuts were safe and easy they would not be shortcuts they would be 
the way.

It is best to avoid shortcuts whilst learning untill you know the full 
implications. you will find your learning goes much smother & faster.




-- 
We are all in the gutter, but some of us are looking at the stars.
-- Oscar Wilde
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Build Question: How to Add -Wl, --option Before Objects In Setup.py?

2014-10-31 Thread Cyd Haselton
On Tue, Oct 28, 2014 at 4:01 PM, Ned Deily  wrote:
> In article
> ,
>  Cyd Haselton  wrote:
>
>> On Tue, Oct 28, 2014 at 3:11 AM, Ned Deily  wrote:
>> > In article
>> > ,
>> >  Cyd Haselton  wrote:
>> > [...]
>> >> I'm building python on an Android device in the KBOX
>> >> environment...which simulates a Unix type filesystem.
>> >> Python isn't installed; I'm building from sources (2.7.8) with GCC
>> >> 4.8.0 and make.
>> >>
>> >> The problem with the LDFLAGS approach is that some of the libraries
>> >> that must be linked (-lc -ldl) do not need the --allow-shlib-undefined
>> >> option...it's only the lpython2.7 that does.  Any way to do this?
>> >
>> > Sorry, I have no experience with that environment nor an understanding
>> > of why you need to specify --allow-shlib-undefined (it seems to be the
>> > default in some versions of ld).  You could look at and, if necessary,
>> > modify Lib/distutils, the part of the Python standard library that
>> > builds extension modules from setup.py.
>
>> No need to apologize.  Also no need to have an understanding of the
>> environment; with a few rare exceptions it behaves just like a
>> Unix/Linux one.
>>
>> The reason why I need to specify --allow-shlib-undefined is for the
>> python library; it's throwing undefined references to sincos even
>> though the symbol is there. Specifying that option is the only fix
>> I've found.
>>
>> As mentioned earlier, i'm not familiar with python or its build
>> system; which file in the distutils dir do I need to modify?
>
> Perhaps I should apologize for not asking earlier what you are really
> trying to do before suggesting heading down the path of modifying
> Distutils :)  So the issue is with an undefined reference to sincos?  It
> appears that that routine is often supplied in libm.  Is that the case
> on your platform?  And, if so, the right fix might be to supply it
> manually or, better, ensure that Python supplies it as a result of
> running ./configure.  Also, what version of Python are you building?
>
> --
>  Ned Deily,
>  n...@acm.org
>
> --
> https://mail.python.org/mailman/listinfo/python-list

So, after trying various ways to add that flag before lpython in
setup.py, I stripped all --allow-shlib-undefined
--no-allow-shlib-undefined feom the Makefile, ran make clean, and
make.

I still get the following error:
Modules/python.o \
-lc -ldl -lm  -L. -lpython2.7   -lm
./libpython2.7.so: undefined reference to `sincos'
collect2: error: ld returned 1 exit status
make: *** [python] Error 1

Should I repost this to the dev mailing list?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Gisle Vanem

"Artur Bercik"  wrote:


I have to set several variables so it would be easier if I could set them
from Python.


You can write a Python-script that generates a .bat file (in your %TEMP-dir?).
And run this .bat file when Python ends. Put it all in an alias or another .bat
file. E.g. untested:
alias set_env=python  -o %TEMP%\set_env.bat & %TEMP%\set_env.bat

--gv

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


Re: Saving a file "in the background" -- How?

2014-10-31 Thread Akira Li
Virgil Stokes  writes:

> While running a python program I need to save some of the data that is
> being created. I would like to save the data to a file on a disk
> according to a periodical schedule  (e.g. every 10
> minutes). Initially, the amount of data is small (< 1 MB) but after
> sometime the amount of data can be >10MB. If a problem occurs during
> data creation, then the user should be able to start over from the
> last successfully saved data.
>
> For my particular application, no other file is being saved and the
> data should always replace (not be appended to) the previous data
> saved. It is important that  the data be saved without any obvious
> distraction to the user who is busy creating more data. That is, I
> would like to save the data "in the background".
>
> What is a good method to perform this task using Python 2.7.8 on a
> Win32 platform?

There are several requirements:

- save data asynchroniously -- "without any obvious distraction to the
  user"
- save data durably -- avoid corrupting previously saved data or
  writing only partial new data e.g., in case of a power failure
- do it periodically -- handle drift/overlap gracefully in a documented
  way 

A simple way to do asynchronios I/O on Python 2.7.8 on a Win32 platform
is to use threads:

  t = threading.Thread(target=backup_periodically, kwargs=dict(period=600))
  t.daemon = True # stop if the program exits
  t.start()

where backup_periodically() backups data every period seconds: 

  import time
  
  def backup_periodically(period, timer=time.time, sleep=time.sleep):
  start = timer()
  while True:
  try:
  backup()
  except Exception: # log exceptions and continue
  logging.exception() 
  # lock with the timer 
  sleep(period - (timer() - start) % period) 

To avoid drift over time of backup times, the sleep is locked with the
timer using the modulo operation. If backup() takes longer than *period*
seconds (unlikely for 10MB per 10 minutes) then the step may be
skipped. 

backup() makes sure that the data is saved and can be restore at any
time. 

  def backup():
  with atomic_open('backup', 'w') as file: 
  file.write(get_data())

where atomic_open() [1] tries to overcome multiple issues with saving
data reliably:

- write to a temporary file so that the old data is always available
- rename the file when all new data is written, handle cases such as:
  * "antivirus opens old file thus preventing me from replacing it"

either the operation succeeds and 'backup' contains new data or it fails
and 'backup' contains untouched ready-to-restore old data -- nothing in
between. 

[1]: https://github.com/mitsuhiko/python-atomicfile/blob/master/atomicfile.py

I don't know how ready atomicfile.py but you should be aware of the
issues it is trying to solve if you want a reliable backup solution.


--
Akira

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


Re: Emulating py2exe for python version 3 and above

2014-10-31 Thread Akira Li
Ian Dickinson  writes:

> Can i emulate py2exe for python version 3 and above i also use pygame any
> suggestions for a basic staring script would be greatly appreciated
>
>

py2exe supports Python 3.3+ Actually, default installers from pypi
support only Python 3.

I see unofficial pygame's Windows installers for Python 3 at
http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 


--
Akira

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


Re: Saving a file "in the background" -- How?

2014-10-31 Thread Chris Angelico
On Fri, Oct 31, 2014 at 11:07 PM, Akira Li <4kir4...@gmail.com> wrote:
> where atomic_open() [1] tries to overcome multiple issues with saving
> data reliably:
>
> - write to a temporary file so that the old data is always available
> - rename the file when all new data is written, handle cases such as:
>   * "antivirus opens old file thus preventing me from replacing it"
>
> either the operation succeeds and 'backup' contains new data or it fails
> and 'backup' contains untouched ready-to-restore old data -- nothing in
> between.

Sounds like a lot of hassle, and a lot of things that could be done
wrongly. Personally, if I need that level of reliability and
atomicity, I'd rather push the whole question down to a lower level:
maybe commit something to a git repository and push it to a remote
server, or use a PostgreSQL database, or something of that sort. Let
someone else have the headaches about "what if AV opens the file". Let
someone else worry about how to cope with power failures at arbitrary
points in the code. (Though, to be fair, using git for this doesn't
fully automate failure handling; what it does is allow you to detect
issues on startup, and either roll back ("git checkout -f") or apply
("git commit -a") if it looks okay.)

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


Popen class?

2014-10-31 Thread satishmlmlml
What is Popen class?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Dave Angel

On 10/30/2014 10:40 PM, Artur Bercik wrote:


On Fri, Oct 31, 2014 at 11:30 AM, Zachary Ware <
zachary.ware+pyl...@gmail.com> wrote:


On Thursday, October 30, 2014, Artur Bercik  wrote:


Dear Dave Angel

Thanks for your answer.

I am using Python 2.7

I want to set it permanently.
I have to set several variables so it would be easier if I could set them
from Python.



Depending on how "permanently" you mean, about your only solutions would
be "os.system('setx <...>')" or manually manipulating the registry with the
_winreg module.





> could you please elaborate 'setx <...>'?
>

SETX is a Windows utility.  I don't run Windows, but I found a Win7 
machine and played again.  I used to be quite knowledgeable about 
Windows, but I could get parts of the following wrong.


To see what SETX does, type (at a cmd line):

SETX /?

Or google it.  (I used duckduckgo, and here are the first two useful 
links:  Obviously the second one is an official Microsoft page.


http://ss64.com/nt/setx.html
http://technet.microsoft.com/en-us/library/cc755104.aspx

There are several variants of "permanent" that are relevant here.

1) Nothing you change in one process will change another process that's 
already running.  That includes any shells you might have already 
started, such as DOS boxes.  (cmd windows)  And the term shell here also 
includes any other already-running process that is going to launch 
another program.  The shell controls its child's environment, and in 
general do not check the registry for changes you might have made.


2) SETX will affect new processes launched directly from Windows (eg. 
from Explorer), because it changes the registry itself.


3) SETX will affect *new* shells launched, and thus indirectly affect 
their children.


4) SETX effects will survive a reboot.  So when you restart your system, 
you'll still see the new values.  Incidentally, that's the first time 
when you'll be sure that all processes will see the new values.


5) SETX can be used on remote (networked) systems, and the rules are 
somewhat different there.


6) If you have (or ever will have) multiple users, you have to consider 
whether you want to affect the system environment variables or just one 
user.  And if just one user, whether it's yourself or some other 
account, such as Administrator.  Every time a process is launched by the 
system, it builds an environment from TWO sets of registry items.  So if 
you want to affect all users, you need to change the system variables.



If you are just going to do this once, and you want to affect all the 
processes that ever run in the future, you'll be much safer using the 
control panel:


Control Panel | System | Advanced | Environment Variables




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


Re: Popen class?

2014-10-31 Thread Cameron Simpson

On 31Oct2014 05:28, satishmlm...@gmail.com  wrote:

What is Popen class?


It's a class from the subprocess module:

  https://docs.python.org/3/library/subprocess.html#module-subprocess

used for starting external programs, waiting for them to execute, and using 
their input and outputs.


Cheers,
Cameron Simpson 

manual, n.:
A unit of documentation. There are always three or more on a given item.
One is on the shelf; someone has the others.
The information you need is in the others.   - Ray Simard
--
https://mail.python.org/mailman/listinfo/python-list


Re: problem with pefile

2014-10-31 Thread Kiuhnm
On Friday, October 31, 2014 2:23:26 AM UTC+1, Cameron Simpson wrote:
> On 30Oct2014 17:58, Kiuhnm  wrote:
> >On Friday, October 31, 2014 1:33:07 AM UTC+1, Cameron Simpson wrote:
> >> On 29Oct2014 08:34, gandalf23 wrote:
> >> >OT: how can I hide my email in these posts?
> >> >Every time I try to send a post, google warns me that my email is visible 
> >> >and so I edit it out.
> >>
> >> Why would you want to hide your email?
> >
> >I don't want more spam.
> 
> You just need to filter your email better.
> Most of us do not hide our addresses.
> 
> I manage to filter most of my spam by filing anything which gets past all my 
> rules that catch mailing lists and which do not come from addresses in my 
> "known" group into a probably-spam folder. It is surprisingly effective.
> 
> Basicly:
>- to me (me in to/cc/bcc), from a "known" author ==> inbox
>- matches one of my mailing list rules ==> appropriate-folder
>- otherwise ==> probably-spam

If it's *probably*-spam, you need to check it anyway, so that doesn't solve the 
problem. What happens if I miss an important email?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Saving a file "in the background" -- How?

2014-10-31 Thread Steven D'Aprano
Chris Angelico wrote:

> Sounds like a lot of hassle, and a lot of things that could be done
> wrongly. Personally, if I need that level of reliability and
> atomicity, I'd rather push the whole question down to a lower level:
> maybe commit something to a git repository and push it to a remote
> server, 

How is pushing something to a remote server more reliable than writing to a
local drive?

> or use a PostgreSQL database, or something of that sort. Let 
> someone else have the headaches about "what if AV opens the file". Let
> someone else worry about how to cope with power failures at arbitrary
> points in the code. 

That "someone else" is still you though. And "use a PostgreSQL database" is
no solution to the problem "how do I reliably write a 250MB video file?".

Or for that matter, a 2KB text file. Databases are great as databases, but
they aren't files.

> (Though, to be fair, using git for this doesn't 
> fully automate failure handling; what it does is allow you to detect
> issues on startup, and either roll back ("git checkout -f") or apply
> ("git commit -a") if it looks okay.)

I don't see how this sort of manual handling is less trouble than using a
proper, reliable save routine. To start with, what do you mean "detect
issues on startup" -- startup of what?


-- 
Steven

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 10:05:03 +0100, "ast"  wrote:

>
>"Seymore4Head"  a écrit dans le message de 
>news:51755at03r0bidjqh3qf0hhpvjr8756...@4ax.com...
>> class pet:
>>def set_age(self,age):
>>self.age=age
>>def get_age(self):
>>return self.age
>> pax=pet
>> pax.set_age(4)
>>
>> Traceback (most recent call last):
>>  File "C:\Functions\test.py", line 18, in 
>>pax.set_age(4)
>> TypeError: set_age() missing 1 required positional argument: 'age'
>>
>> I am trying to pass 4 as the age.  Obviously I am doing it wrong.
>>
>
>Hi
>
>I am a beginner too, but I find it strange that your
>pet class has no __init__ method to construct
>instances 

Because the topic of that lesson was getter setter.
I can construct an __init___  but I was practicing get/set.
This stuff is coming to me slowly.  I need to rinse and repeat quite a
few more times, before I follow what is going on.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Mark Lawrence

On 31/10/2014 09:05, ast wrote:


"Seymore4Head"  a écrit dans le message de
news:51755at03r0bidjqh3qf0hhpvjr8756...@4ax.com...

class pet:
   def set_age(self,age):
   self.age=age
   def get_age(self):
   return self.age
pax=pet
pax.set_age(4)

Traceback (most recent call last):
 File "C:\Functions\test.py", line 18, in 
   pax.set_age(4)
TypeError: set_age() missing 1 required positional argument: 'age'

I am trying to pass 4 as the age.  Obviously I am doing it wrong.



Hi

I am a beginner too, but I find it strange that your
pet class has no __init__ method to construct
instances


__init__ initialises instances, __new__ constructs them

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 09:59:30 GMT, alister
 wrote:

>On Thu, 30 Oct 2014 17:34:57 -0400, Seymore4Head wrote:
>
>> On Thu, 30 Oct 2014 14:28:19 -0700, Larry Hudson 
>> wrote:
>> 
>>>On 10/30/2014 01:16 PM, Seymore4Head wrote:
 class pet:
  def set_age(self,age):
  self.age=age
  def get_age(self):
  return self.age
 pax=pet pax.set_age(4)

 Traceback (most recent call last):
File "C:\Functions\test.py", line 18, in 
  pax.set_age(4)
 TypeError: set_age() missing 1 required positional argument: 'age'

 I am trying to pass 4 as the age.  Obviously I am doing it wrong.

>>>You have already received the answer -- pax=pet should be pax=pet(), but
>>>I have a simple side-comment about style.  It is common Python
>>>convention to capitalize class names, IOW make this class Pet instead of
>>>class pet.  This is convention not a requirement, but it does help
>>>distinguish class names from ordinary variable names -- especially to
>>>others reading your code (as well as yourself a few days later).   ;-)
>>>
>>>  -=- Larry -=-
>> 
>> I try to take typing shortcuts and it bites me in the behind.
>> Good suggestion Thanks
>
>If shortcuts were safe and easy they would not be shortcuts they would be 
>the way.
>
>It is best to avoid shortcuts whilst learning untill you know the full 
>implications. you will find your learning goes much smother & faster.

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


Re: set environmental variable from python

2014-10-31 Thread Mark Lawrence

On 31/10/2014 02:36, Rustom Mody wrote:

On Friday, October 31, 2014 8:01:08 AM UTC+5:30, Zachary Ware wrote:

On Thursday, October 30, 2014, Artur Bercik  wrote:

Dear Dave Angel


Thanks for your answer.


I am using Python 2.7


I want to set it permanently.
I have to set several variables so it would be easier if I could set them from 
Python.


Depending on how "permanently" you mean, about your only solutions
would be "os.system('setx <...>')" or manually manipulating the
registry with the _winreg module.


Or dont do it from python but directly with regedit??

The question really is: Why do you wish to do this from within python?




Or do it directly with something like the Rapid Environment Editor 
http://www.rapidee.com/en/about


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: Saving a file "in the background" -- How?

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 1:06 AM, Steven D'Aprano
 wrote:
> Chris Angelico wrote:
>
>> Sounds like a lot of hassle, and a lot of things that could be done
>> wrongly. Personally, if I need that level of reliability and
>> atomicity, I'd rather push the whole question down to a lower level:
>> maybe commit something to a git repository and push it to a remote
>> server,
>
> How is pushing something to a remote server more reliable than writing to a
> local drive?

It's not, as far as the program's concerned, but when I need
reliability I usually also need replication/backups.

>> or use a PostgreSQL database, or something of that sort. Let
>> someone else have the headaches about "what if AV opens the file". Let
>> someone else worry about how to cope with power failures at arbitrary
>> points in the code.
>
> That "someone else" is still you though. And "use a PostgreSQL database" is
> no solution to the problem "how do I reliably write a 250MB video file?".

If the commit happens, it's saved, and everything else isn't my
problem. And yes, Postgres isn't ideal for a 250MB file, but then,
neither is any sort of autosave good for that. I'm assuming the
content isn't that big.

> Or for that matter, a 2KB text file. Databases are great as databases, but
> they aren't files.

A database can handle a 2KB text file, no problem. That's how most CMSes work.

>> (Though, to be fair, using git for this doesn't
>> fully automate failure handling; what it does is allow you to detect
>> issues on startup, and either roll back ("git checkout -f") or apply
>> ("git commit -a") if it looks okay.)
>
> I don't see how this sort of manual handling is less trouble than using a
> proper, reliable save routine. To start with, what do you mean "detect
> issues on startup" -- startup of what?

Because fundamentally it's impossible to write a reliable save
routine. The best you can do is detect, on subsequent restart of your
program, that there was an incomplete save in a previous session, and
either roll it back or complete it. In some cases that's trivial - if
there's a temporary file, you have the partial save, so you delete the
temporary (rolling back) - or you just ignore it and overwrite, and
hope that it's not going to get stuck anywhere. But it's still the
same broad technique, whichever way you do it.

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


Re: Classes

2014-10-31 Thread Ian Kelly
On Fri, Oct 31, 2014 at 8:05 AM, Seymore4Head
 wrote:
> Because the topic of that lesson was getter setter.
> I can construct an __init___  but I was practicing get/set.

Doesn't sound like a very good lesson to me. Getters and setters are
the Java way of doing things. The Pythonic way is to just use an
attribute, and then replace it with a property in the unlikely event
that getter/setter logic needs to be added.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Grant Edwards
On 2014-10-31, Ian Kelly  wrote:
> On Fri, Oct 31, 2014 at 8:05 AM, Seymore4Head
> wrote:
>> Because the topic of that lesson was getter setter.
>> I can construct an __init___  but I was practicing get/set.
>
> Doesn't sound like a very good lesson to me.

It's not.  It's teaching java or C++ or some other language while
using a Python compiler.

> Getters and setters are the Java way of doing things. The Pythonic
> way is to just use an attribute, and then replace it with a property
> in the unlikely event that getter/setter logic needs to be added.

Exactly.

-- 
Grant Edwards   grant.b.edwardsYow! I have the power to
  at   HALT PRODUCTION on all
  gmail.comTEENAGE SEX COMEDIES!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 15:49:43 + (UTC), Grant Edwards
 wrote:

>On 2014-10-31, Ian Kelly  wrote:
>> On Fri, Oct 31, 2014 at 8:05 AM, Seymore4Head
>> wrote:
>>> Because the topic of that lesson was getter setter.
>>> I can construct an __init___  but I was practicing get/set.
>>
>> Doesn't sound like a very good lesson to me.
>
>It's not.  It's teaching java or C++ or some other language while
>using a Python compiler.
>
>> Getters and setters are the Java way of doing things. The Pythonic
>> way is to just use an attribute, and then replace it with a property
>> in the unlikely event that getter/setter logic needs to be added.
>
>Exactly.

In this class, we will follow the practice of accessing the contents
of objects using methods known as getters and setters. While not
required by Python, this practice encourages the user of the class to
manipulates class objects solely via class methods. The advantage of
following this practice is that the implementer of the class
definition (often someone other than the user of the class) may
restructure the organization of the data fields associated with the
object while avoiding the need to rewrite code that uses the class.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 3:31 AM, Seymore4Head
 wrote:
[presumably quoting his course material]
> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters. While not
> required by Python, this practice encourages the user of the class to
> manipulates class objects solely via class methods. The advantage of
> following this practice is that the implementer of the class
> definition (often someone other than the user of the class) may
> restructure the organization of the data fields associated with the
> object while avoiding the need to rewrite code that uses the class.

Yep, that's teaching C++/Java coding using Python. Isn't it time you
got onto a better course? Time and time again you've been going down
inadvisable paths, and it seems to me you could do better with the
inbuilt tutorial rather than this kind of course.

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


Implementing pdfkit and wkhtmltopdf on windows fails

2014-10-31 Thread robert brook
I am able to install both of these packages on my mac at home and it works well.

I am trying to install on windows 7 at work and it fails.  PDFKit is trying to 
find the wkh package and it cannot. I have entered the full path to the exe for 
the environment variables and the error below is spit out.

If I explicitly cd into the directory that has the executable the script works 
fine

>>> import os
>>> path='C:\\wkhtmltopdf\\bin\\'
>>> os.chdir(path)
>>> pdfkit.from_string('Hello!', 'out.pdf')
Loading pages (1/6) #this works after cd into the exe directory


Where can I specify the path to the executable?

*
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\api.py", line 66, in from_string
  File "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\pdfkit.py", line 39, in __init__
  File "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\configuration.py", line 27, in __init__
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please
 install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing
-wkhtmltopdf

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Sat, 1 Nov 2014 03:37:29 +1100, Chris Angelico 
wrote:

>On Sat, Nov 1, 2014 at 3:31 AM, Seymore4Head
> wrote:
>[presumably quoting his course material]
>> In this class, we will follow the practice of accessing the contents
>> of objects using methods known as getters and setters. While not
>> required by Python, this practice encourages the user of the class to
>> manipulates class objects solely via class methods. The advantage of
>> following this practice is that the implementer of the class
>> definition (often someone other than the user of the class) may
>> restructure the organization of the data fields associated with the
>> object while avoiding the need to rewrite code that uses the class.
>
>Yep, that's teaching C++/Java coding using Python. Isn't it time you
>got onto a better course? Time and time again you've been going down
>inadvisable paths, and it seems to me you could do better with the
>inbuilt tutorial rather than this kind of course.
>
>ChrisA
inbuilt tutorial?

The course is free.  You can't beat the price.  It is only for a few
more weeks.

Trying to learn from reading the Internet has no set direction.  I
need a little nudge.  More like a shove.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 10:05:03 +0100, "ast"  wrote:

>
>"Seymore4Head"  a écrit dans le message de 
>news:51755at03r0bidjqh3qf0hhpvjr8756...@4ax.com...
>> class pet:
>>def set_age(self,age):
>>self.age=age
>>def get_age(self):
>>return self.age
>> pax=pet
>> pax.set_age(4)
>>
>> Traceback (most recent call last):
>>  File "C:\Functions\test.py", line 18, in 
>>pax.set_age(4)
>> TypeError: set_age() missing 1 required positional argument: 'age'
>>
>> I am trying to pass 4 as the age.  Obviously I am doing it wrong.
>>
>
>Hi
>
>I am a beginner too, but I find it strange that your
>pet class has no __init__ method to construct
>instances 

What material have you used to take you up to classes?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implementing pdfkit and wkhtmltopdf on windows fails

2014-10-31 Thread Dave Angel

On 10/31/2014 12:46 PM, robert brook wrote:

I am able to install both of these packages on my mac at home and it works well.

I am trying to install on windows 7 at work and it fails.  PDFKit is trying to 
find the wkh package and it cannot. I have entered the full path to the exe for 
the environment variables and the error below is spit out.

If I explicitly cd into the directory that has the executable the script works 
fine


import os
path='C:\\wkhtmltopdf\\bin\\'
os.chdir(path)
pdfkit.from_string('Hello!', 'out.pdf')

Loading pages (1/6) #this works after cd into the exe directory


Where can I specify the path to the executable?

*
Traceback (most recent call last):
   File "", line 1, in 
   File 
"C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\api.py", line 66, in from_string
   File 
"C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\pdfkit.py", line 39, in __init__
   File 
"C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
g\pdfkit\configuration.py", line 27, in __init__
OSError: No wkhtmltopdf executable found: "b''"
If this file exists please check that this process can read it. Otherwise please
  install wkhtmltopdf - 
https://github.com/JazzCore/python-pdfkit/wiki/Installing
-wkhtmltopdf

**



I don't know your particular module, but generally, you want modules 
installed on your sys.path.  Either install it there, or add the 
directory to sys.path (which is a list)


I'd suggest doing the latter, then figure whether a reinstall is called 
for.  It looks like you installed it in the root directory of C:, rather 
than in the site-packages.



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


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 3:47 AM, Seymore4Head
 wrote:
> inbuilt tutorial?
>
> The course is free.  You can't beat the price.  It is only for a few
> more weeks.
>
> Trying to learn from reading the Internet has no set direction.  I
> need a little nudge.  More like a shove.

https://docs.python.org/3/tutorial/

Same price.

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


Re: Classes

2014-10-31 Thread Steven D'Aprano
Seymore4Head wrote:

> Because the topic of that lesson was getter setter.
> I can construct an __init___  but I was practicing get/set.

What lesson is that? Using getters/setters is discouraged in Python.

> This stuff is coming to me slowly.  I need to rinse and repeat quite a
> few more times, before I follow what is going on.

Start with the simplest class possible:

class MyClass:
pass

Right now, that class has a name, "MyClass", no methods, and no data. But we
can still create an instance. Call the class, as if it were a function, to
create the instance:

obj = MyClass()

What's the relationship between instances and classes? Classes are a general
type of entity, instances are a specific example of that entity. You can
have many instances from a class. So:

Class: Dog
Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
   Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.

Class: Wizard
Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.

Class: int
Instances: 0, 1, 2, -5, 23, 19874023, etc.


You can confirm that obj is now an instance of MyClass:

print(isinstance(obj, MyClass))

will print True.

What can you do with obj? It has no interesting methods, and no data. But we
can give it some! Python, unlike some languages, allows you to dynamically
add data attributes to instances on the fly, without pre-defining them.

obj.value = 23.0
obj.message = "hello world!"
print(obj.value)
print(obj.message)


will associate the data 23.0 and "hello world" to the attributes "value"
and "message" of the instance obj.

Let's make the class a bit easier to use, at the expense of doing a bit more
work up front:

class MyClass:
def __init__(self, value, message):
self.value = value
self.message = message

obj = MyClass(23.0, "hello world")
print(obj.value)
print(obj.message)


The __init__ method is automatically called when you call the class as if it
were a function. Because the __init__ method has two arguments (plus the
special "self" argument), you have to call the class with two arguments.
They get used as the value and message respectively.


Or we can give it getters and setters:

class MyClass:
def set_value(self, value):
self.value = value
def get_value(self):
return self.value
def set_message(self, message):
self.message = message
def get_message(self):
return self.message

obj = MyClass()
obj.set_value(23.0)
obj.set_message("hello world")
print(obj.get_value())
print(obj.get_message())


If you're thinking that's a lot of extra work for not much benefit, 99.99%
of the time you're right.


-- 
Steven

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


Re: Popen class?

2014-10-31 Thread Steven D'Aprano
satishmlm...@gmail.com wrote:

> What is Popen class?

Googling could answer that:

https://duckduckgo.com/html/?q=python+popen+class

If you have a specific question, please be more detailed when you ask your
question, then we can give more specific answers.


-- 
Steven

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


Re: Teaching Python

2014-10-31 Thread Seymore4Head
On Mon, 29 Sep 2014 11:44:01 -0400, Seymore4Head
 wrote:

>On Mon, 29 Sep 2014 15:18:31 +0200, Gabor Urban 
>wrote:
>
>>Hi,
>>
>>my 11 years old son and his classmate told me, that they would like to
>>learn Python. They did some programming in Logo and turtle graphics, bat
>>not too much.
>>
>>Doesn anybody has an idea how to start?
>
>I ordered this book from the library a few weeks ago.  It just came in
>yesterday.  
>Python for kids.  Jason R Briggs
>
>This was also a good page for starters.
>http://www.practicepython.org/

BTW I found that book online.
https://www.google.com/search?as_q=python+for+kids&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=&as_filetype=pdf&as_rights=&gws_rd=ssl

I don't know how to post the link to the web page so I just posted the
search method I used to find it.

I wanted to review it again and I noticed something that was a little
humorous to me.  Installing Python on Ubuntu on page 9 and 10 (of the
book) shows that what ever kid is using this machine for Python is
also doing Multiplayer online poker.

Trying to parlay his/her lunch money would be my guess.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Dave Angel

On 10/31/2014 12:31 PM, Seymore4Head wrote:


In this class, we will follow the practice of accessing the contents
of objects using methods known as getters and setters. While not
required by Python, this practice encourages the user of the class to
manipulates class objects solely via class methods. The advantage of
following this practice is that the implementer of the class
definition (often someone other than the user of the class) may
restructure the organization of the data fields associated with the
object while avoiding the need to rewrite code that uses the class.



Written by somebody who doesn't understand a fundamental point about 
Python. It's just not true that the user would have to rewrite code if 
the implementer change the members.


The Python way of writing getters and setters (if/when you get to that 
stage of a Python class) is through the property decorator or its 
equivalent.


class Pet():
def __init__(self, ):
self.internal details set here

@property()
def age(self):
return some_complicated_expression

And the user of the class fetches this by using

mypet = Pet()
hisage = mypet.age

No stupid parentheses needed.  When the class is first written, no work 
is needed.  When it's updated to multiply the internal age by 7, just 
write the function, and decorate it to look like a regular attribute.


More complicated things can be done.  But the point is that the user 
should just get the age, by accessing the age attribute, and if the 
implementation needs to change, it can change.


The time to make a class complicated, is when it needs to be.  And the 
user should not have to pay the price for "just in case."


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


Re: Classes

2014-10-31 Thread Seymore4Head
On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano
 wrote:

>Seymore4Head wrote:
>
>> Because the topic of that lesson was getter setter.
>> I can construct an __init___  but I was practicing get/set.
>
>What lesson is that? Using getters/setters is discouraged in Python.
>
>> This stuff is coming to me slowly.  I need to rinse and repeat quite a
>> few more times, before I follow what is going on.
>
>Start with the simplest class possible:
>
>class MyClass:
>pass
>
>Right now, that class has a name, "MyClass", no methods, and no data. But we
>can still create an instance. Call the class, as if it were a function, to
>create the instance:
>
>obj = MyClass()
>
>What's the relationship between instances and classes? Classes are a general
>type of entity, instances are a specific example of that entity. You can
>have many instances from a class. So:
>
>Class: Dog
>Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
>   Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.
>
>Class: Wizard
>Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.
>
>Class: int
>Instances: 0, 1, 2, -5, 23, 19874023, etc.
>
>
>You can confirm that obj is now an instance of MyClass:
>
>print(isinstance(obj, MyClass))
>
>will print True.
>
>What can you do with obj? It has no interesting methods, and no data. But we
>can give it some! Python, unlike some languages, allows you to dynamically
>add data attributes to instances on the fly, without pre-defining them.
>
>obj.value = 23.0
>obj.message = "hello world!"
>print(obj.value)
>print(obj.message)
>
>
>will associate the data 23.0 and "hello world" to the attributes "value"
>and "message" of the instance obj.
>
>Let's make the class a bit easier to use, at the expense of doing a bit more
>work up front:
>
>class MyClass:
>def __init__(self, value, message):
>self.value = value
>self.message = message
>
>obj = MyClass(23.0, "hello world")
>print(obj.value)
>print(obj.message)
>
>
>The __init__ method is automatically called when you call the class as if it
>were a function. Because the __init__ method has two arguments (plus the
>special "self" argument), you have to call the class with two arguments.
>They get used as the value and message respectively.
>
>
>Or we can give it getters and setters:
>
>class MyClass:
>def set_value(self, value):
>self.value = value
>def get_value(self):
>return self.value
>def set_message(self, message):
>self.message = message
>def get_message(self):
>return self.message
>
>obj = MyClass()
>obj.set_value(23.0)
>obj.set_message("hello world")
>print(obj.get_value())
>print(obj.get_message())
>
>
>If you're thinking that's a lot of extra work for not much benefit, 99.99%
>of the time you're right.

I agree it is more work.  But more work means more practice.  I need
more practice figuring out how these commands work.

>obj = MyClass()
>obj.set_value(23.0)
>obj.set_message("hello world")
>print(obj.get_value())
>print(obj.get_message())

I don't know here to get more (simple) practice problems.  I am trying
to invent my own.
-- 
https://mail.python.org/mailman/listinfo/python-list


EuroPython 2015 Call for Participation: On-site Teams

2014-10-31 Thread M.-A. Lemburg
The EuroPython Society (EPS) is happy to announce the Call for
Participation (CFP) for EuroPython 2015. The purpose of this call is
to select teams willing to help organize the EuroPython conference
on-site at a suitable location.


Call for Participation (CFP)


Please see the online versions of the Call for Participation posting
for details:

 * Call for Participation blog post:

http://www.europython-society.org/post/101421890815/europython-2015-call-for-participation-on-site-teams

 * Call for Participation PDF file:
   
https://www.dropbox.com/s/6u78zb4v95h2lq5/EuroPython%20CFP%202015%20-%20Final.pdf?dl=1

We could also like to remind everyone interested in helping with the
EuroPython 2015 organization, that the Call for Volunteers is still
open:

 * EuroPython Workgroups: Call for Volunteers
   
http://www.europython-society.org/post/99718376575/europython-workgroups-call-for-volunteers


Timeline for Proposals
--

The Call for Participation will run until the following deadline for
submissions. Proposals must be submitted until midnight UTC on the
deadline day.

 * 2014-11-28 - Deadline for submissions (announcement + 4 weeks)

 * 2014-12-05 - Deadline for EPS to review proposals (1 week)

 * 2014-12-12 - Deadline for amended proposals (1 week)

 * 2014-12-26 - Decision on the next EP host (within 2 weeks)


Thank you,
--
EuroPython Society
http://www.europython-society.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implementing pdfkit and wkhtmltopdf on windows fails

2014-10-31 Thread robert brook
On Friday, October 31, 2014 1:02:44 PM UTC-4, Dave Angel wrote:
> On 10/31/2014 12:46 PM, robert brook wrote:
> > I am able to install both of these packages on my mac at home and it works 
> > well.
> >
> > I am trying to install on windows 7 at work and it fails.  PDFKit is trying 
> > to find the wkh package and it cannot. I have entered the full path to the 
> > exe for the environment variables and the error below is spit out.
> >
> > If I explicitly cd into the directory that has the executable the script 
> > works fine
> >
>  import os
>  path='C:\\wkhtmltopdf\\bin\\'
>  os.chdir(path)
>  pdfkit.from_string('Hello!', 'out.pdf')
> > Loading pages (1/6) #this works after cd into the exe directory
> >
> >
> > Where can I specify the path to the executable?
> >
> > *
> > Traceback (most recent call last):
> >File "", line 1, in 
> >File 
> > "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
> > g\pdfkit\api.py", line 66, in from_string
> >File 
> > "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
> > g\pdfkit\pdfkit.py", line 39, in __init__
> >File 
> > "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-py3.3.eg
> > g\pdfkit\configuration.py", line 27, in __init__
> > OSError: No wkhtmltopdf executable found: "b''"
> > If this file exists please check that this process can read it. Otherwise 
> > please
> >   install wkhtmltopdf - 
> > https://github.com/JazzCore/python-pdfkit/wiki/Installing
> > -wkhtmltopdf
> >
> > **
> >
> 
> I don't know your particular module, but generally, you want modules 
> installed on your sys.path.  Either install it there, or add the 
> directory to sys.path (which is a list)
> 
> I'd suggest doing the latter, then figure whether a reinstall is called 
> for.  It looks like you installed it in the root directory of C:, rather 
> than in the site-packages.
> 
> 
> -- 
> DaveA

I see the problem.  The installation of the package did not drop down the files 
that the executable is looking for.

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Sat, 1 Nov 2014 04:02:33 +1100, Chris Angelico 
wrote:

>On Sat, Nov 1, 2014 at 3:47 AM, Seymore4Head
> wrote:
>> inbuilt tutorial?
>>
>> The course is free.  You can't beat the price.  It is only for a few
>> more weeks.
>>
>> Trying to learn from reading the Internet has no set direction.  I
>> need a little nudge.  More like a shove.
>
>https://docs.python.org/3/tutorial/
>
>Same price.
>
I run across this page frequently.  To me, this is examples.  While
examples can be quite useful, I don't call this a tutorial.  I have
found the answer to my question by searching this page several times,
but the biggest problem with this page is that you can't copy and
paste the examples into IDLE.

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


Re: Classes

2014-10-31 Thread Marko Rauhamaa
Seymore4Head :

> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters.

The biggest problem in this OO disease is that it makes you think of
objects as data containers instead of dynamic agents.

An object is defined through its behavior, not what the black box
contains.


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


Re: Classes

2014-10-31 Thread Zachary Ware
On Fri, Oct 31, 2014 at 12:31 PM, Seymore4Head
 wrote:
> I run across this page frequently.  To me, this is examples.  While
> examples can be quite useful, I don't call this a tutorial.  I have
> found the answer to my question by searching this page several times,
> but the biggest problem with this page is that you can't copy and
> paste the examples into IDLE.

Sure you can, just click the little '>>>' button in the upper right of
the example box.

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


Re: Classes

2014-10-31 Thread Rob Gaddi
On Fri, 31 Oct 2014 13:31:44 -0400
Seymore4Head  wrote:

> On Sat, 1 Nov 2014 04:02:33 +1100, Chris Angelico 
> wrote:
> 
> >On Sat, Nov 1, 2014 at 3:47 AM, Seymore4Head
> > wrote:
> >> inbuilt tutorial?
> >>
> >> The course is free.  You can't beat the price.  It is only for a few
> >> more weeks.
> >>
> >> Trying to learn from reading the Internet has no set direction.  I
> >> need a little nudge.  More like a shove.
> >
> >https://docs.python.org/3/tutorial/
> >
> >Same price.
> >
> I run across this page frequently.  To me, this is examples.  While
> examples can be quite useful, I don't call this a tutorial.  I have
> found the answer to my question by searching this page several times,
> but the biggest problem with this page is that you can't copy and
> paste the examples into IDLE.
> 
> JMO

No, but you can retype them.  Copying and pasting teaches nothing.
Figuring out how to hunt down the syntax errors your retyping has
induced teaches much.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Grant Edwards
On 2014-10-31, Seymore4Head  wrote:
> On Fri, 31 Oct 2014 15:49:43 + (UTC), Grant Edwards
> wrote:
>
>>On 2014-10-31, Ian Kelly  wrote:
>>> On Fri, Oct 31, 2014 at 8:05 AM, Seymore4Head
>>> wrote:
 Because the topic of that lesson was getter setter.
 I can construct an __init___  but I was practicing get/set.
>>>
>>> Doesn't sound like a very good lesson to me.
>>
>>It's not.  It's teaching java or C++ or some other language while
>>using a Python compiler.
>>
>>> Getters and setters are the Java way of doing things. The Pythonic
>>> way is to just use an attribute, and then replace it with a property
>>> in the unlikely event that getter/setter logic needs to be added.
>>
>>Exactly.
>
> In this class, we will follow the practice of accessing the contents
> of objects using methods known as getters and setters.

IOW...

"In this class, we're not going to learn Python.  We're going to learn
Java. But actually _using_ Java is too much hassle, so we'll write
Java using Python instead."

> While not required by Python, this practice encourages the user of
> the class to manipulates class objects solely via class methods.

Which is a Java/C++ thing.

> The advantage of following this practice is that the implementer of
> the class definition (often someone other than the user of the class)
> may restructure the organization of the data fields associated with
> the object while avoiding the need to rewrite code that uses the
> class.

That's wrong.  That statement about using getter/setter having that
advantage is false (if you're talking about Python).  It may be true
in Java or C++ or whatever language the lesson's author is teaching,
but it's not true of Python.

-- 
Grant Edwards   grant.b.edwardsYow! A shapely CATHOLIC
  at   SCHOOLGIRL is FIDGETING
  gmail.cominside my costume..
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Grant Edwards
On 2014-10-31, Seymore4Head  wrote:
> On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano

>>Let's make the class a bit easier to use, at the expense of doing a bit more
>>work up front:
>>
>>class MyClass:
>>def __init__(self, value, message):
>>self.value = value
>>self.message = message
>>
>>obj = MyClass(23.0, "hello world")
>>print(obj.value)
>>print(obj.message)
>>
>>
>>The __init__ method is automatically called when you call the class as if it
>>were a function. Because the __init__ method has two arguments (plus the
>>special "self" argument), you have to call the class with two arguments.
>>They get used as the value and message respectively.
>>
>>Or we can give it getters and setters:
>>
>>class MyClass:
>>def set_value(self, value):
>>self.value = value
>>def get_value(self):
>>return self.value
>>def set_message(self, message):
>>self.message = message
>>def get_message(self):
>>return self.message
>>
>>obj = MyClass()
>>obj.set_value(23.0)
>>obj.set_message("hello world")
>>print(obj.get_value())
>>print(obj.get_message())
>>
>>
>>If you're thinking that's a lot of extra work for not much benefit, 99.99%
>>of the time you're right.
>
> I agree it is more work.  But more work means more practice. I need
> more practice figuring out how these commands work.

Except you're practicing doing things the "wrong" way.  If you want to
learn Java, then use Java.  If you want to learn Python, then don't
write Java.

-- 
Grant Edwards   grant.b.edwardsYow! HOORAY, Ronald!!
  at   Now YOU can marry LINDA
  gmail.comRONSTADT too!!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Rob Gaddi
On Fri, 31 Oct 2014 13:18:00 -0400
Seymore4Head  wrote:

> On Sat, 01 Nov 2014 04:06:44 +1100, Steven D'Aprano
>  wrote:
> 
> >Seymore4Head wrote:
> >
> >> Because the topic of that lesson was getter setter.
> >> I can construct an __init___  but I was practicing get/set.
> >
> >What lesson is that? Using getters/setters is discouraged in Python.
> >
> >> This stuff is coming to me slowly.  I need to rinse and repeat quite a
> >> few more times, before I follow what is going on.
> >
> >Start with the simplest class possible:
> >
> >class MyClass:
> >pass
> >
> >Right now, that class has a name, "MyClass", no methods, and no data. But we
> >can still create an instance. Call the class, as if it were a function, to
> >create the instance:
> >
> >obj = MyClass()
> >
> >What's the relationship between instances and classes? Classes are a general
> >type of entity, instances are a specific example of that entity. You can
> >have many instances from a class. So:
> >
> >Class: Dog
> >Instances: Rin-Tin-Tin, Lassie, Hooch (from the "Turner and Hooch" movie),
> >   Marmaduke, Gaspode the Wonder Dog, Spike the Bulldog, etc.
> >
> >Class: Wizard
> >Instances: Gandalf, Dumbledore, the Wizard of Oz, Rincewind, etc.
> >
> >Class: int
> >Instances: 0, 1, 2, -5, 23, 19874023, etc.
> >
> >
> >You can confirm that obj is now an instance of MyClass:
> >
> >print(isinstance(obj, MyClass))
> >
> >will print True.
> >
> >What can you do with obj? It has no interesting methods, and no data. But we
> >can give it some! Python, unlike some languages, allows you to dynamically
> >add data attributes to instances on the fly, without pre-defining them.
> >
> >obj.value = 23.0
> >obj.message = "hello world!"
> >print(obj.value)
> >print(obj.message)
> >
> >
> >will associate the data 23.0 and "hello world" to the attributes "value"
> >and "message" of the instance obj.
> >
> >Let's make the class a bit easier to use, at the expense of doing a bit more
> >work up front:
> >
> >class MyClass:
> >def __init__(self, value, message):
> >self.value = value
> >self.message = message
> >
> >obj = MyClass(23.0, "hello world")
> >print(obj.value)
> >print(obj.message)
> >
> >
> >The __init__ method is automatically called when you call the class as if it
> >were a function. Because the __init__ method has two arguments (plus the
> >special "self" argument), you have to call the class with two arguments.
> >They get used as the value and message respectively.
> >
> >
> >Or we can give it getters and setters:
> >
> >class MyClass:
> >def set_value(self, value):
> >self.value = value
> >def get_value(self):
> >return self.value
> >def set_message(self, message):
> >self.message = message
> >def get_message(self):
> >return self.message
> >
> >obj = MyClass()
> >obj.set_value(23.0)
> >obj.set_message("hello world")
> >print(obj.get_value())
> >print(obj.get_message())
> >
> >
> >If you're thinking that's a lot of extra work for not much benefit, 99.99%
> >of the time you're right.
> 
> I agree it is more work.  But more work means more practice.  I need
> more practice figuring out how these commands work.
> 
> >obj = MyClass()
> >obj.set_value(23.0)
> >obj.set_message("hello world")
> >print(obj.get_value())
> >print(obj.get_message())
> 
> I don't know here to get more (simple) practice problems.  I am trying
> to invent my own.

Fine.  Practice problem that is actually Pythonic.  Define a Rectangle
class.  Give it length and width attributes, no unnecessary
getters or setters.  Give it perimeter() and area() methods.

Define a Square class, subclassed from Rectangle.  Use getters/setters
to enforce that the length and width must be equal.  Confirm that
length and width remain locked, and that perimeter() and area() work
correctly.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Implementing pdfkit and wkhtmltopdf on windows fails

2014-10-31 Thread Peter Otten
robert brook wrote:

> I am able to install both of these packages on my mac at home and it works
> well.
> 
> I am trying to install on windows 7 at work and it fails.  PDFKit is
> trying to find the wkh package and it cannot. I have entered the full path
> to the exe for the environment variables and the error below is spit out.
> 
> If I explicitly cd into the directory that has the executable the script
> works fine
> 
 import os
 path='C:\\wkhtmltopdf\\bin\\'
 os.chdir(path)
 pdfkit.from_string('Hello!', 'out.pdf')
> Loading pages (1/6) #this works after cd into the exe directory
> 
> 
> Where can I specify the path to the executable?
> 
> *
> Traceback (most recent call last):
>   File "", line 1, in 
>   File
>   "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-
py3.3.eg
> g\pdfkit\api.py", line 66, in from_string
>   File
>   "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-
py3.3.eg
> g\pdfkit\pdfkit.py", line 39, in __init__
>   File
>   "C:\Applications\python_33_32_bit\lib\site-packages\pdfkit-0.4.1-
py3.3.eg
> g\pdfkit\configuration.py", line 27, in __init__
> OSError: No wkhtmltopdf executable found: "b''"
> If this file exists please check that this process can read it. Otherwise
> please
>  install wkhtmltopdf -
>  https://github.com/JazzCore/python-pdfkit/wiki/Installing
> -wkhtmltopdf
> 
> **

Following the link in the traceback I find the installation instruction for 
windows:

"""
Download the installer from the wkhtmltopdf downloads list and add folder 
with wkhtmltopdf binary to PATH.
"""

This PATH has nothing to do with Python's sys.path which specifies where 
Python looks for modules, and you have to set it according to the rules of 
your OS, e. g.

http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx

Once you have added "the directory you had to cd into" follwing the above 
instructions your script should work in a newly opened shell window.

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 12:39:34 -0500, Zachary Ware
 wrote:

>On Fri, Oct 31, 2014 at 12:31 PM, Seymore4Head
> wrote:
>> I run across this page frequently.  To me, this is examples.  While
>> examples can be quite useful, I don't call this a tutorial.  I have
>> found the answer to my question by searching this page several times,
>> but the biggest problem with this page is that you can't copy and
>> paste the examples into IDLE.
>
>Sure you can, just click the little '>>>' button in the upper right of
>the example box.

Ah..so you can.  
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: accents in windows

2014-10-31 Thread Nobody
On Thu, 30 Oct 2014 08:30:49 -0400, C@rlos wrote:

> thanks U, but the real problem is:
> 
> i have a path C:\Users\yanet\Desktop\áaaéeeíiiióooúuuñnn this path
> is correct, áaaéeeíiiióooúuuñnn is the name of a directory but when
> i try to use os.walk() usin this path, dont work, for os this path dont
> exist, i try every things but nothing works.

http://www.joelonsoftware.com/articles/Unicode.html

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
 wrote:


>Define a Square class, subclassed from Rectangle.  Use getters/setters
>to enforce that the length and width must be equal.  Confirm that
>length and width remain locked, and that perimeter() and area() work
>correctly.

class Rectangle:
def __init__(self,length,width):
self.length=length
self.width=width
def area(self):
return self.length*self.width
def perimeter(self):
return 2*self.length+2*self.width
class Square(Rectangle):
def set_side (self):
if self.length!=self.width:


a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Rectangle(4,4)
print (c.area())
print (c.perimeter())

I bombed on the rest.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: set environmental variable from python

2014-10-31 Thread Zachary Ware
On Fri, Oct 31, 2014 at 1:11 PM, Dennis Lee Bieber
 wrote:
> On Thu, 30 Oct 2014 22:00:33 -0500, Zachary Ware
>  declaimed the following:
> >From a Command Prompt, do 'help setx' for details on how to use setx.
>
> Really? 
>
> C:\Users\Wulfraed\Documents>help setx
> This command is not supported by the help utility.  Try "setx /?".

Oops, should have tried it before posting.  It at least tells you how
to get what I meant to give, though ;)

> OTOH: I didn't know about this command before -- and setting user 
> level
> environment variables on Win7 wasn't working for me...

I came across it after becoming very frustrated with having to jump
through all the hoops to find the environment variable GUI at one
point.

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


Re: Classes

2014-10-31 Thread ast


"Seymore4Head"  a écrit dans le message de 
news:rbf75ah9l1jp9e72gqr0ncu7bau8cnt...@4ax.com...



What material have you used to take you up to classes?


It's a french classroom on the web
http://openclassrooms.com/courses/apprenez-a-programmer-en-python 


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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 19:31:01 +0100, "ast"  wrote:

>
>"Seymore4Head"  a écrit dans le message de 
>news:rbf75ah9l1jp9e72gqr0ncu7bau8cnt...@4ax.com...
>
>> What material have you used to take you up to classes?
>
>It's a french classroom on the web
>http://openclassrooms.com/courses/apprenez-a-programmer-en-python 

I am going to try to learn Python before I learn French, so I won't be
going there.  :)

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


Re: Classes

2014-10-31 Thread Joel Goldstick
On Fri, Oct 31, 2014 at 2:31 PM, ast  wrote:
>
> "Seymore4Head"  a écrit dans le message de
> news:rbf75ah9l1jp9e72gqr0ncu7bau8cnt...@4ax.com...
>
>> What material have you used to take you up to classes?
>
>
> It's a french classroom on the web
> http://openclassrooms.com/courses/apprenez-a-programmer-en-python
> --
> https://mail.python.org/mailman/listinfo/python-list

My suggestion for a free tutorial is Learn Python the Hard Way.

You won't learn by cutting and pasting.  You have to type it in
yourself.  It will make you learn more because every typo you make
will lead you to a new discovery


-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
 wrote:

>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
> wrote:
>
>
>>Define a Square class, subclassed from Rectangle.  Use getters/setters
>>to enforce that the length and width must be equal.  Confirm that
>>length and width remain locked, and that perimeter() and area() work
>>correctly.
>
>class Rectangle:
>def __init__(self,length,width):
>self.length=length
>self.width=width
>def area(self):
>return self.length*self.width
>def perimeter(self):
>return 2*self.length+2*self.width
>class Square(Rectangle):
>def set_side (self):
>if self.length!=self.width:
>
>
>a=Rectangle(3,5)
>print (a.area())
>print (a.perimeter())
>b=Rectangle(5,7)
>print (b.area())
>print (b.perimeter())
>c=Rectangle(4,4)
>print (c.area())
>print (c.perimeter())
>
>I bombed on the rest.
class Rectangle:
def __init__(self,length=0,width=0):
self.length=length
self.width=width
def area(self):
return self.length*self.width
def perimeter(self):
return 2*self.length+2*self.width
class Square(Rectangle):
def set_side (self,side):
self.side=side
return side*side


a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Rectangle(4,4)
print (c.area())
print (c.perimeter())

A little closer.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Teaching Python

2014-10-31 Thread sohcahtoa82
On Friday, October 31, 2014 10:10:33 AM UTC-7, Seymore4Head wrote:
> On Mon, 29 Sep 2014 11:44:01 -0400, Seymore4Head
>  wrote:
> 
> >On Mon, 29 Sep 2014 15:18:31 +0200, Gabor Urban 
> >wrote:
> >
> >>Hi,
> >>
> >>my 11 years old son and his classmate told me, that they would like to
> >>learn Python. They did some programming in Logo and turtle graphics, bat
> >>not too much.
> >>
> >>Doesn anybody has an idea how to start?
> >
> >I ordered this book from the library a few weeks ago.  It just came in
> >yesterday.  
> >Python for kids.  Jason R Briggs
> >
> >This was also a good page for starters.
> >http://www.practicepython.org/
> 
> BTW I found that book online.
> https://www.google.com/search?as_q=python+for+kids&as_epq=&as_oq=&as_eq=&as_nlo=&as_nhi=&lr=&cr=&as_qdr=all&as_sitesearch=&as_occt=any&safe=images&tbs=&as_filetype=pdf&as_rights=&gws_rd=ssl
> 
> I don't know how to post the link to the web page so I just posted the
> search method I used to find it.
> 
> I wanted to review it again and I noticed something that was a little
> humorous to me.  Installing Python on Ubuntu on page 9 and 10 (of the
> book) shows that what ever kid is using this machine for Python is
> also doing Multiplayer online poker.
> 
> Trying to parlay his/her lunch money would be my guess.

http://www.amazon.com/gp/product/1593274076/ref=as_li_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1593274076&linkCode=as2&tag=other00d-20&linkId=CR3CFBFYUAUVSHVM

For people that want to buy the book.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 14:41:08 -0400, Joel Goldstick
 wrote:

>On Fri, Oct 31, 2014 at 2:31 PM, ast  wrote:
>>
>> "Seymore4Head"  a écrit dans le message de
>> news:rbf75ah9l1jp9e72gqr0ncu7bau8cnt...@4ax.com...
>>
>>> What material have you used to take you up to classes?
>>
>>
>> It's a french classroom on the web
>> http://openclassrooms.com/courses/apprenez-a-programmer-en-python
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>My suggestion for a free tutorial is Learn Python the Hard Way.
>
>You won't learn by cutting and pasting.  You have to type it in
>yourself.  It will make you learn more because every typo you make
>will lead you to a new discovery

I have watched a few of those videos.  And I do have the PDF.  I will
give it a closer look.
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: .write() behavior

2014-10-31 Thread Alan Bawden
Marko Rauhamaa  writes:

> Let me mention a related problem I ran into a couple of weeks ago.
> Linux's standard C library (glibc) implements fread(3) differently in
> RHEL 5 and RHEL 6/7. In RHEL 5, it blocks in a loop until it has read in
> the desired number of records. In RHEL 6 and 7, it appears to block
> until it can return at least one whole record.

By my reading of the POSIX standard, that latter behavior would be
incorrect.  The "RETURN VALUE" section of the POSIX description for
fread() starts:

   Upon successful completion, fread() shall return the number of
   elements successfully read which is less than nitems only if a read
   error or end-of-file is encountered. ...

If in whatever version of glibc comes with RHEL 6/7, it is the case that
a call to fread() can return less than the desired number of records,
and you have not reached EOF, and no read error has occurred, that would
be a serious bug.  The world is full of C programs that call fread() and
fwrite() without wrapping them in defensive
maybe-I-didn't-get-everything loops.

>> Am I missing something? There seem to be some primitive IO facilities
>> in Python 3 that make a distinction between blocking and non-blocking
>> mode, but that distinction doesn't seem to be available when I just
>> call open().

Since I wrote this, I have discovered and read PEP-3116, and using that
as a road-map to understanding the Python 3 documentation it is now
clear to me that in Python 3, if you have not gone out of your way to
enable non-blocking, a call to the .write() method on any object that
might normally be returned by a call to open(), will always write _all_
of your data (or raise an exception).  There is never any need for a
defensive loop.

As further evidence that this is the case, note that PEP-3116 says right
at the end of its "Rationale and Goals" section:

   ...  Programmers who don't want to muck about in the new I/O world
   can expect that the open() factory method will produce an object
   backwards-compatible with old-style file objects.

Since old-style file objects didn't return a count, this goal can only
be achieved if programmers are safe in _ignoring_ that count in Python 3.

I am relieved to discover that when I am finally able to port my Python
code from Python 2 to Python 3, I will not have to worry that .write()
might perform an incomplete write where it previously did not.

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


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 4:43 AM, Rob Gaddi
 wrote:
> Define a Square class, subclassed from Rectangle.  Use getters/setters
> to enforce that the length and width must be equal.  Confirm that
> length and width remain locked, and that perimeter() and area() work
> correctly.

Here we go again...

http://en.wikipedia.org/wiki/Circle-ellipse_problem

Square and Rectangle are just as "obviously" the other way around in
the hierarchy, and it's wrong whichever way you do it. Not a good
example.

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


Re: Classes

2014-10-31 Thread sohcahtoa82
On Friday, October 31, 2014 1:51:23 PM UTC-7, Chris Angelico wrote:
> On Sat, Nov 1, 2014 at 4:43 AM, Rob Gaddi
>  wrote:
> > Define a Square class, subclassed from Rectangle.  Use getters/setters
> > to enforce that the length and width must be equal.  Confirm that
> > length and width remain locked, and that perimeter() and area() work
> > correctly.
> 
> Here we go again...
> 
> http://en.wikipedia.org/wiki/Circle-ellipse_problem
> 
> Square and Rectangle are just as "obviously" the other way around in
> the hierarchy, and it's wrong whichever way you do it. Not a good
> example.
> 
> ChrisA

I've never heard of the Circle-Ellipse problem, and my first instinct to Rob's 
post was to ask, why would you want to sub-class Rectangle into a Square class? 
 A square is just a special case of a Rectangle.  Attempting that kind of 
sub-classing would just cause problems.  The only thing you gain is a slight 
optimization in calculating perimeter by turning two multiplications and an 
addition into a single multiplication which really wouldn't have an effect on 
performance unless you were doing that calculation millions of times per second.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: problem with pefile

2014-10-31 Thread Cameron Simpson

On 31Oct2014 06:12, Kiuhnm  wrote:

On Friday, October 31, 2014 2:23:26 AM UTC+1, Cameron Simpson wrote:

On 30Oct2014 17:58, Kiuhnm  wrote:
>On Friday, October 31, 2014 1:33:07 AM UTC+1, Cameron Simpson wrote:
>> On 29Oct2014 08:34, gandalf23 wrote:
>> >OT: how can I hide my email in these posts?
>> >Every time I try to send a post, google warns me that my email is visible 
and so I edit it out.
>>
>> Why would you want to hide your email?
>
>I don't want more spam.

You just need to filter your email better.
Most of us do not hide our addresses.

I manage to filter most of my spam by filing anything which gets past all my
rules that catch mailing lists and which do not come from addresses in my
"known" group into a probably-spam folder. It is surprisingly effective.

Basicly:
   - to me (me in to/cc/bcc), from a "known" author ==> inbox
   - matches one of my mailing list rules ==> appropriate-folder
   - otherwise ==> probably-spam


If it's *probably*-spam, you need to check it anyway, so that doesn't solve the 
problem. What happens if I miss an important email?


I review that folder ("unknown") every so often (a few times a day). But it 
keeps most of the noise out of everything else. The "probably spam" effect is 
one reason I don't discard things outright.


I also find it easier to review a single folder whose contents are almost 
always spam than to constantly have little bits of spam floating by in my 
normal mail folders.


If you want to pursue this discussions we should take it off-list to private 
email; we're not talking about Python any more.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 8:24 AM,   wrote:
> I've never heard of the Circle-Ellipse problem, and my first instinct to 
> Rob's post was to ask, why would you want to sub-class Rectangle into a 
> Square class?  A square is just a special case of a Rectangle.  Attempting 
> that kind of sub-classing would just cause problems.  The only thing you gain 
> is a slight optimization in calculating perimeter by turning two 
> multiplications and an addition into a single multiplication which really 
> wouldn't have an effect on performance unless you were doing that calculation 
> millions of times per second.
>

Yep. A square is indeed a special case of rectangle (and a circle is a
special case of ellipse), so it would make sense to have an
"is_square()" method (or isSquare or squarep or whatever you want to
call it). The hairiness of subclassing either to make the other means
it's an unideal example.

In fact, I would say the Python @property decorator is less well
suited to an inheritance situation than to a chronological change.
"This used to be a simple attribute, now it has
validation/formatting/logging/whatever." But it's hard to come up with
an example to illustrate this cleanly for a student. Personally, I'd
be inclined to skip properties altogether; learn about them later,
much later, and only if you actually need them.

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


Re: Classes

2014-10-31 Thread Rob Gaddi
On Fri, 31 Oct 2014 14:24:11 -0700 (PDT)
sohcahto...@gmail.com wrote:

> On Friday, October 31, 2014 1:51:23 PM UTC-7, Chris Angelico wrote:
> > On Sat, Nov 1, 2014 at 4:43 AM, Rob Gaddi
> >  wrote:
> > > Define a Square class, subclassed from Rectangle.  Use getters/setters
> > > to enforce that the length and width must be equal.  Confirm that
> > > length and width remain locked, and that perimeter() and area() work
> > > correctly.
> > 
> > Here we go again...
> > 
> > http://en.wikipedia.org/wiki/Circle-ellipse_problem
> > 
> > Square and Rectangle are just as "obviously" the other way around in
> > the hierarchy, and it's wrong whichever way you do it. Not a good
> > example.
> > 
> > ChrisA
> 
> I've never heard of the Circle-Ellipse problem, and my first instinct to 
> Rob's post was to ask, why would you want to sub-class Rectangle into a 
> Square class?  A square is just a special case of a Rectangle.  Attempting 
> that kind of sub-classing would just cause problems.  The only thing you gain 
> is a slight optimization in calculating perimeter by turning two 
> multiplications and an addition into a single multiplication which really 
> wouldn't have an effect on performance unless you were doing that calculation 
> millions of times per second.

As a practical tool?  Entirely and completely useless, no doubt
about it.  But it's easily understood as an example, even if the reason
that a Square is a subclass of Rectangle is "because I said so".

For this specific exercise, a Square is a subclass of Rectangle because
the point of Rectangle is to demonstrate that extraneous get/set
functions are completely unnecessary in Python.  The point of
Square is to demonstrate that get/set functions can be useful in
certain circumstances where you need to implement non-trivial behaviors,
such as making the "width" property into an alias for the "length" true
data member.

As a learning tool for thinking about inheritance hierarchies it's kind
of rubbish.  But then again, most inheritance hierarchies are ambigious
at best, which is why "has a" is often a better choice than "is a".

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 8:40 AM, Rob Gaddi
 wrote:
> For this specific exercise, a Square is a subclass of Rectangle because
> the point of Rectangle is to demonstrate that extraneous get/set
> functions are completely unnecessary in Python.  The point of
> Square is to demonstrate that get/set functions can be useful in
> certain circumstances where you need to implement non-trivial behaviors,
> such as making the "width" property into an alias for the "length" true
> data member.

As the Wikipedia article explains, this has its own consequences. A
reasonable test suite for Rectangle would quite probably fail if given
a Square. Hence my belief that this makes for a less-than-ideal
example. But I can't think of *any* good example of @property for a
tutorial. ANY.

> As a learning tool for thinking about inheritance hierarchies it's kind
> of rubbish.  But then again, most inheritance hierarchies are ambigious
> at best, which is why "has a" is often a better choice than "is a".

Agreed. There certainly are times when "is a" is the right choice, but
there are a lot more times when "has a" is the better choice. Usually,
when I subclass, it's because I want to tweak the behaviour of an
existing type (for instance, subclass int and change its repr() to
return hex(self)), so it really truly is a , in every way
except that one tiny change. Otherwise, new type with a member. Much
simpler. Much safer.

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


Re: Classes

2014-10-31 Thread Gregory Ewing

Seymore4Head wrote:

The course is free.  You can't beat the price.  It is only for a few
more weeks.


But if it's teaching you things that are blatantly wrong
in relation to Python, it may be doing more harm than
good.

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


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 18:57:31 -0400, Dennis Lee Bieber
 wrote:

>On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
> declaimed the following:
>
>>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
>> wrote:
>>
>>
>>>Define a Square class, subclassed from Rectangle.  Use getters/setters
>>>to enforce that the length and width must be equal.  Confirm that
>>>length and width remain locked, and that perimeter() and area() work
>>>correctly.
>>
>>class Rectangle:
>>def __init__(self,length,width):
>>self.length=length
>>self.width=width
>>def area(self):
>>return self.length*self.width
>>def perimeter(self):
>>return 2*self.length+2*self.width
>>class Square(Rectangle):
>>def set_side (self):
>>if self.length!=self.width:
>>   
>   Where's the rest of that -- not to mention you called it "set_side" but
>never pass a side into it for use.
> 
>-=-=-=-=-=-=-=-
>
>class Rectangle(object):
>def __init__(self, length, width=None):
>self.length = length
>if width is None:
>self.width = length
>else:
>self.width = width
>def area(self):
>return self.length * self.width
>def perimeter(self):
>return 2 * (self.length + self.width)
>
Thanks for posting that.  I had given up on trying it.  I follow the
changes you made up this point.  I will have to think some more to get
the rest of this.
The way you provided a catch for not having a width, I don't
understand the purpose of a Square subclass.  Couldn't side just be
length?
BTW I am willing to forget any mention of getter/setter.  We can just
pretend that never happened.

>class Square(Rectangle):
>def __init__(self, side):
>self.side = side
>def _getLength(self):
>return self.side
>def _getWidth(self):
>return self.side
>def _setLength(self, vlu):
>self.side = vlu
>def _setWidth(self, vlu):
>self.side = vlu
>length = property(_getLength, _setLength)
>width = property(_getWidth, _setWidth)
>
>aRect = Rectangle(2, 4)
>print aRect.length, aRect.width, aRect.area(), aRect.perimeter()
>aRect.length = 9
>print aRect.length, aRect.width, aRect.area(), aRect.perimeter()
>
>
>aSqr = Square(3)
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.length = 4
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.width = 5
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>aSqr.side = 7
>print aSqr.side, aSqr.length, aSqr.width, aSqr.area(), aSqr.perimeter()
>
>-=-=-=-=-=-=-=-=-
>Microsoft Windows [Version 6.1.7601]
>Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
>
>C:\Users\Wulfraed\Documents>cd "Python Progs"
>
>C:\Users\Wulfraed\Documents\Python Progs>property2.py
>2 4 8 12
>9 4 36 26
>3 3 3 9 12
>4 4 4 16 16
>5 5 5 25 20
>7 7 7 49 28
>
>C:\Users\Wulfraed\Documents\Python Progs>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Seymore4Head
On Fri, 31 Oct 2014 19:22:13 -0400, Seymore4Head
 wrote:

>On Fri, 31 Oct 2014 18:57:31 -0400, Dennis Lee Bieber
> wrote:
>
>>On Fri, 31 Oct 2014 14:18:44 -0400, Seymore4Head
>> declaimed the following:
>>
>>>On Fri, 31 Oct 2014 10:43:19 -0700, Rob Gaddi
>>> wrote:
>>>
>>>
Define a Square class, subclassed from Rectangle.  Use getters/setters
to enforce that the length and width must be equal.  Confirm that
length and width remain locked, and that perimeter() and area() work
correctly.
>>>
>>>class Rectangle:
>>>def __init__(self,length,width):
>>>self.length=length
>>>self.width=width
>>>def area(self):
>>>return self.length*self.width
>>>def perimeter(self):
>>>return 2*self.length+2*self.width
>>>class Square(Rectangle):
>>>def set_side (self):
>>>if self.length!=self.width:
>>>   
>>  Where's the rest of that -- not to mention you called it "set_side" but
>>never pass a side into it for use.
>> 
>>-=-=-=-=-=-=-=-
>>
>>class Rectangle(object):
>>def __init__(self, length, width=None):
>>self.length = length
>>if width is None:
>>self.width = length
>>else:
>>self.width = width
>>def area(self):
>>return self.length * self.width
>>def perimeter(self):
>>return 2 * (self.length + self.width)
>>
>Thanks for posting that.  I had given up on trying it.  I follow the
>changes you made up this point.  I will have to think some more to get
>the rest of this.
>The way you provided a catch for not having a width, I don't
>understand the purpose of a Square subclass.  Couldn't side just be
>length?
>BTW I am willing to forget any mention of getter/setter.  We can just
>pretend that never happened.
>
I took your instruction and change it to this:
That answers my own question.
Thanks again

class Rectangle(object):
def __init__(self, length, width=None):
self.length = length
if width is None:
self.width = length
else:
self.width = width
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)

class Square(Rectangle):
def area(self):
return self.length * self.width
def perimeter(self):
return 2 * (self.length + self.width)

a=Rectangle(3,5)
print (a.area())
print (a.perimeter())
b=Rectangle(5,7)
print (b.area())
print (b.perimeter())
c=Square(4)
print (c.area())
print (c.perimeter())
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Steven D'Aprano
Gregory Ewing wrote:

> Seymore4Head wrote:
>> The course is free.  You can't beat the price.  It is only for a few
>> more weeks.
> 
> But if it's teaching you things that are blatantly wrong
> in relation to Python, it may be doing more harm than
> good.

Like all good Pythonistas[1], we hate Java and think that getter/setter
methods are pointless. But come on, they're not *wrong*, as in incorrect.
They do the job they're supposed to do, at the expense of an awkward
interface. Using properties is *exactly the same as Java getters/setters*,
except that the interface is less awkward.

And there are times when using getters and setters is the right choice.
Properties should only be used for quite lightweight calculations, because
attribute access is supposed to be fast. If your calculation is complex,
time-consuming or might fail, using a property is a bad idea and you should
use an explicit getter method, possibly with a setter if needed.

There are lots of things in Python which are written as getter methods or
functions, rather than attributes or properties. E.g.:

py> (23).bit_length()
5
py> len("hello world!")  # calls __len__ method
12

Files have quite a few examples. fileno, isatty and writable could all be
written as read-only properties, and seek/tell could be written as a
file.position property. But they're not, they're written as methods.





[1] Except for Jythonistas, who are allowed to enjoy using Java.

-- 
Steven

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


Re: Classes

2014-10-31 Thread Steven D'Aprano
Dennis Lee Bieber wrote:

> What you are being taught is NOT PYTHON.

Of course it is. It uses Python syntax, Python terminology, and the Python
compiler. It might not be the preferred Python idiom or best practice, but
it's still Python code.

Exaggeration does not help anyone, it just makes us look like religious
fanatics.


-- 
Steven

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


Challenge: optimizing isqrt

2014-10-31 Thread Steven D'Aprano
There is an algorithm for calculating the integer square root of any
positive integer using only integer operations:

def isqrt(n):
if n < 0: raise ValueError
if n == 0:
return 0
bits = n.bit_length()
a, b = divmod(bits, 2)
x = 2**(a+b)
while True:
y = (x + n//x)//2
if y >= x:
return x
x = y

This returns the integer part of the square root of n, that is, the greatest
whole number less than or equal to the square root of n:

py> isqrt(35)
5
py> isqrt(36)
6


That makes it equivalent to int(math.sqrt(n)), which also happens to be
much, much faster, at least for small values of n. However, for large
values of n, using floating point intermediate calculations fail:

py> import math
py> int(math.sqrt(2**3000))
Traceback (most recent call last):
  File "", line 1, in 
OverflowError: long int too large to convert to float

Another problem is that, above a certain size, the resolution of floats is
larger than 1, so you can't convert every int into a float without loss:

py> float(2**90-1) == 2**90-1
False

which means that using math.sqrt is not correct:

py> isqrt(2**90-1)
35184372088831
py> int(math.sqrt(2**90-1))  # Off by one.
35184372088832


So, the challenge is to identify when it is safe to optimise isqrt(n) as
int(math.sqrt(n)):

Q1: What is the largest value of M, such that 

all(isqrt(i) == int(math.sqrt(n)) for n in range(M))

returns True?

I have done a brute force test, and in nine hours confirmed that M is at
least 7627926244. That took nine hours, and I expect that a brute force
test of every int representable as a float would take *months* of
processing time.

Q2: For values above M, is there a way of identifying which values of n are
okay to use the optimized version?

Q3: What is the largest value of n beyond which you can never use the float
optimization?


You can assume that Python floats are IEEE-754 C doubles, and that
math.sqrt() is correctly rounded.


-- 
Steven

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


Re: Challenge: optimizing isqrt

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 12:29 PM, Steven D'Aprano
 wrote:
> Q1: What is the largest value of M, such that
>
> all(isqrt(i) == int(math.sqrt(n)) for n in range(M))
>
> returns True?
>
> I have done a brute force test, and in nine hours confirmed that M is at
> least 7627926244. That took nine hours, and I expect that a brute force
> test of every int representable as a float would take *months* of
> processing time.

Here's something that should be faster than pure brute-force:

import math
for i in range(2**34): # 2**34 should take about nine hours
sqrt = int(math.sqrt(i))
if sqrt * sqrt > i:
print("Too high at %d" % i)
break
if (sqrt+1) * (sqrt+1) <= i:
print("Too low at %d" % i)
break

There are two ways int(math.sqrt(i)) can be wrong: it can return a
number that's too high, or one that's too low. If it's too high,
squaring it (btw, int*int seems to be faster than int**2) will produce
a number higher than the original. If the result was too low, then the
next number up would have been within the correct bounds. There's no
need to actually calculate isqrt here.

I was able to probe up to 2**29 in seven minutes on my computer
(allocating one core, CPython 3.5 from trunk). If it's linear, going
as far as 2**34 should take no more than the nine hours you spent. I
expect time will be a bit worse than linear (working with small
numbers is marginally faster than working with big numbers), but if
it's at least reasonably close, probing as far as 2**53 would take
81555 days. So, uhh, a brute force by your method is going to take a
lot more than months. (And 2**53 isn't technically where ints stop
being representable, but I'd use that as the final cut-off, as it's
where *all* ints stop being representable. In any case, there's a
failure at 2**54-1, so there's no need to go past there.)

Hmm. The speed difference between your brute force and mine isn't all
that much. I'm estimating maybe doing at best twice as much in the
same time. So you're probably already using something similar to the
above, and this isn't adding much to the discussion. :(

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


Re: Classes

2014-10-31 Thread Ian Kelly
On Fri, Oct 31, 2014 at 7:06 PM, Steven D'Aprano
 wrote:
> And there are times when using getters and setters is the right choice.
> Properties should only be used for quite lightweight calculations, because
> attribute access is supposed to be fast. If your calculation is complex,
> time-consuming or might fail, using a property is a bad idea and you should
> use an explicit getter method, possibly with a setter if needed.

I agree except that in this scenario you should probably use a verb
other than "get", since getters should also be fast.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Classes

2014-10-31 Thread Chris Angelico
On Sat, Nov 1, 2014 at 5:12 PM, Ian Kelly  wrote:
> On Fri, Oct 31, 2014 at 7:06 PM, Steven D'Aprano
>  wrote:
>> And there are times when using getters and setters is the right choice.
>> Properties should only be used for quite lightweight calculations, because
>> attribute access is supposed to be fast. If your calculation is complex,
>> time-consuming or might fail, using a property is a bad idea and you should
>> use an explicit getter method, possibly with a setter if needed.
>
> I agree except that in this scenario you should probably use a verb
> other than "get", since getters should also be fast.

Maybe. "get_track_length()" might be a reasonable method name for
something that opens an audio file, reads its header, and returns its
length in seconds. It should be a method rather than a property
(attribute access should be fast), but "get" makes sense here, as it's
not calculating or anything, it's just retrieving information. But
yes, if it's a calculation, "calc" would be more common.

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