[Tutor] Truncated urlopen

2013-02-11 Thread Válas Péter
Hi tutors,

aboard again after a long time.
http://..._export.php?mehet=1 is a link which exports me some data in CSV
correctly when I click on it in my browser. (It contains personal data,
that's why I dotted.) I want to process it directly from Python, excluding
the browser phase.

The following piece of code in Python 3.3 prints the first n-1 lines
nicely, then the beginning of the last line truncated. I mean there are 12
fields in CSV and in the last line only 1.5 fields of them are displayed. I
suspect some closing/caching error.
In the first times it worked well but after a certain time it spoiled
without PHP code being modified. I guess it may somehow be connected to
quantity of lines.
How could I read the last line?
Thx, Péter

from urllib.request import urlopen
x=urlopen('http://..._export.php?mehet=1')
y=x.read().decode('windows-1250')
print(y)

Output looks like this:
159;Name of customer159;x...@gmail.com;phone;22;0;0;0;1;0;0;2013-02-09
20:20:26
160;Name of customer160;y...@gmail.com;phone;14;0;0;1;0;0;0;2013-02-09
20:38:16
161;Name of c
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to log process handles and GDI objects in python

2013-02-11 Thread eryksun
On Sun, Feb 10, 2013 at 11:25 PM, Pai, Yogesh M
 wrote:
> I want to log the process threads and GDI objects (the one you see in
> the Windows task manager) in my python code- The idea is to use this
> data to generate a plot to check if the application leaks memory in a
> long run. Although I can use a third-party app to log this, is it
> possible to import certain library in python and log these windows
> attributes during the execution?

psutil can query the number of handles and threads for a Windows
process, but not GDI objects.

http://code.google.com/p/psutil

Getting the number of handles and gdi/user objects via the Win32 API
isn't too hard. Required functions:

OpenProcess
http://msdn.microsoft.com/en-us/library/ms684320

CloseHandle
http://msdn.microsoft.com/en-us/library/ms724211

GetProcessHandleCount
http://msdn.microsoft.com/en-us/library/ms683214

GetGuiResources
http://msdn.microsoft.com/en-us/library/ms683192

Here's a basic example with ctypes. In practice, set argtypes for each
function call (a TypeError is better than a segfault) and error check
the return value per the above docs. You can map the last error (i.e.
GetLastError) to an exception using ctypes.WinError.

(I haven't checked, but all of this is probably simple using pywin32.
Just search around the net for examples.)

>>> import os
>>> from ctypes import *
>>> from ctypes.wintypes import *

>>> PQI = 0x400 # PROCESS_QUERY_INFORMATION
>>> pid = os.getpid()
>>> h = windll.kernel32.OpenProcess(PQI, 0, pid)
>>> hndcnt = DWORD()
>>> windll.kernel32.GetProcessHandleCount(h, byref(hndcnt))
1
>>> hndcnt.value
72L
>>> GR_GDIOBJECTS, GR_USEROBJECTS = 0, 1
>>> windll.user32.GetGuiResources(h, GR_GDIOBJECTS)
4
>>> windll.user32.GetGuiResources(h, GR_USEROBJECTS)
1
>>> windll.kernel32.CloseHandle(h)
1

The number of threads can be found by iterating over a system snapshot
up to the desired PID. Here are the required functions and data
struct:

CreateToolhelp32Snapshot
http://msdn.microsoft.com/en-us/library/ms682489

Process32First[W]
http://msdn.microsoft.com/en-us/library/ms684834

Process32Next[W]
http://msdn.microsoft.com/en-us/library/ms684836

PROCESSENTRTY32
http://msdn.microsoft.com/en-us/library/ms684839

class PROCESSENTRY32(Structure):
_fields_ = [
  ('dwSize', DWORD),
  ('cntUsage', DWORD),
  ('th32ProcessID', DWORD),
  ('th32DefaultHeapID', c_void_p),
  ('th32ModuleID', DWORD),
  ('cntThreads', DWORD),
  ('th32ParentProcessID', DWORD),
  ('pcPriClassBase', c_long),
  ('dwFlags', DWORD),
  ('szExeFile', WCHAR * MAX_PATH),
]

def __init__(self, *args, **kwds):
Structure.__init__(self, *args, **kwds)
self.dwSize = sizeof(self)

TH32CS_SNAPPROCESS = 2
th32cs = windll.kernel32.CreateToolhelp32Snapshot

def get_num_threads(pid):
num_threads = 0
hsnap = th32cs(TH32CS_SNAPPROCESS, 0)
pe = PROCESSENTRY32()
r = windll.kernel32.Process32FirstW(hsnap, byref(pe))
while r:
if pe.th32ProcessID == pid:
num_threads = int(pe.cntThreads)
break
r = windll.kernel32.Process32NextW(hsnap, byref(pe))
windll.kernel32.CloseHandle(hsnap)
return num_threads

Example:

>>> from threading import Timer
>>> pid = os.getpid()
>>> get_num_threads(pid)
1
>>> for i in range(30): Timer(30, lambda: None).start()
>>> get_num_threads(pid)
31
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Truncated urlopen

2013-02-11 Thread Dave Angel

On 02/11/2013 05:58 AM, Válas Péter wrote:

Hi tutors,

aboard again after a long time.


Welcome back.


http://..._export.php?mehet=1 is a link which exports me some data in CSV
correctly when I click on it in my browser. (It contains personal data,
that's why I dotted.) I want to process it directly from Python, excluding
the browser phase.

The following piece of code in Python 3.3 prints the first n-1 lines
nicely, then the beginning of the last line truncated. I mean there are 12
fields in CSV and in the last line only 1.5 fields of them are displayed. I
suspect some closing/caching error.
In the first times it worked well but after a certain time it spoiled
without PHP code being modified. I guess it may somehow be connected to
quantity of lines.
How could I read the last line?
Thx, Péter

from urllib.request import urlopen
x=urlopen('http://..._export.php?mehet=1')
y=x.read().decode('windows-1250')


I'd suggest splitting that expression into two separate statements, one 
that does the read, and the other that decodes.  Then you can print the 
byte-string, and see if it also is truncated, or whether that happened 
during the decoding.


(Though I can't imagine how decoding one of the windows-xxx character 
sets could fail to do the whole thing)


Also, if you use print( repr(mystring) )
you might discover that there are funny escape sequence characters that 
are fooling your console.



print(y)

Output looks like this:
159;Name of customer159;x...@gmail.com;phone;22;0;0;0;1;0;0;2013-02-09
20:20:26
160;Name of customer160;y...@gmail.com;phone;14;0;0;1;0;0;0;2013-02-09
20:38:16
161;Name of c



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Truncated urlopen

2013-02-11 Thread Válas Péter
2013/2/11 Dave Angel 

> I'd suggest splitting that expression into two separate statements, one
> that does the read, and the other that decodes.  Then you can print the
> byte-string, and see if it also is truncated, or whether that happened
> during the decoding.
>
> Also, if you use print( repr(mystring) )
> you might discover that there are funny escape sequence characters that
> are fooling your console.
>

Thanks, Dave,
none of these helps. They are truncated as well.

But I noticed a strange phenomenon. I am supposed to login to this page,
but I can reach CVS data without login if I use the direct link to export.
This may be a bug, but I like it. :-) Now, If I use the same link in
browser, I get different results depending on I have logged in or not. (But
in the first times of gathering data in this table, my Python script worked
well!)

So I should try to log in, but I have always difficulties with logging in
from Python. I know http://docs.python.org/3/howto/urllib2.html#id6, but I
couldn't use it in practice.
Another way would be to log in in the browser correctly, then export CVS
and process it with Python after saving, but I am too lazy to repeat this
all the time. :-)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Truncated urlopen

2013-02-11 Thread Joel Goldstick
On Mon, Feb 11, 2013 at 10:00 AM, Válas Péter  wrote:

> 2013/2/11 Dave Angel 
>
>> I'd suggest splitting that expression into two separate statements, one
>> that does the read, and the other that decodes.  Then you can print the
>> byte-string, and see if it also is truncated, or whether that happened
>> during the decoding.
>>
>> Also, if you use print( repr(mystring) )
>> you might discover that there are funny escape sequence characters that
>> are fooling your console.
>>
>
> Thanks, Dave,
> none of these helps. They are truncated as well.
>
> But I noticed a strange phenomenon. I am supposed to login to this page,
> but I can reach CVS data without login if I use the direct link to export.
> This may be a bug, but I like it. :-) Now, If I use the same link in
> browser, I get different results depending on I have logged in or not. (But
> in the first times of gathering data in this table, my Python script worked
> well!)
>
> So I should try to log in, but I have always difficulties with logging in
> from Python. I know http://docs.python.org/3/howto/urllib2.html#id6, but
> I couldn't use it in practice.
> Another way would be to log in in the browser correctly, then export CVS
> and process it with Python after saving, but I am too lazy to repeat this
> all the time. :-)
>
>
> There is a library called 'requests'
http://docs.python-requests.org/en/latest/ that I have used to access
websites.  It seems much less confusing than urllib/urllib2 to me.

>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which pip for Ubuntu 12.04

2013-02-11 Thread Jim Byrnes

On 02/10/2013 01:10 PM, Joel Goldstick wrote:

On Sun, Feb 10, 2013 at 1:53 PM, Timo  wrote:


Op 10-02-13 17:01, Jim Byrnes schreef:

  On 02/09/2013 05:46 AM, Albert-Jan Roskam wrote:



- Original Message -

  From: Jim Byrnes  To: tutor@python.org Cc:

Sent: Saturday, February 9, 2013 3:02 AM Subject: [Tutor] Which pip
for Ubuntu 12.04

How important is it to have the latest pip installed?

Initially I want to use it to install the latest pymongo driver for
mongoDB. The pip version in the Ubuntu 12.04 repositories is 1.0.1.
I see on 
http://www.pip-installer.org/**en/latest/the
 version is
1.2.1.

It certainly would be easier to install from the repositories but
will that version work to install the latest python packages?

Thanks,  Jim



You could just try it? And downloading it and then doing sudo tar
-xvr pip.tar.gz, cd ..., sudo python setup.py isn't that hard, is
it?



I usually install from the repositories or maybe a ppa so I don't believe
I have ever done an install from a tar.  If necessary I will familiarize
myself with the command you gave (yes I saw your followup changing -xvr to
-xvf) and install that way.

However, I still wonder if using the outdated pip from the repository
will allow me to install the latest python packages? Will trying to use an
outdated pip cause me problems?


I doubt it will. Have a look at the pip changelog to see what has been
changed.

Timo





  But you're right, I also nnoticed that the Canonical repositories are

a little outdated sometimes. Would be nice if it's possible to add
pypi.org, so updating would be easier.

Albert-Jan




Thanks,  Jim

__**_
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/**mailman/listinfo/tutor



__**_
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/**mailman/listinfo/tutor





You can upgrade pip with pip i believe:

jcg@jcg-desktop:~/code/python$ pip install pip
Requirement already satisfied (use --upgrade to upgrade): pip in
/usr/lib/pymodules/python2.7
Cleaning up...
jcg@jcg-desktop:~/code/python$


jcg@jcg-desktop:~/code/python$ pip install --upgrade pip


Be sure to sudo before if you don't have permissions.




I tried this on my laptop with the following results:

jfb@jfb-tp:~$ pip --version
pip 1.0 from /usr/lib/python2.7/dist-packages (python 2.7)
jfb@jfb-tp:~$ pip install pip
Requirement already satisfied (use --upgrade to upgrade): pip in 
/usr/lib/python2.7/dist-packages

Cleaning up...

jfb@jfb-tp:~$ sudo pip install --upgrade pip
[sudo] password for jfb:
Downloading/unpacking pip
  Running setup.py egg_info for package pip

warning: no files found matching '*.html' under directory 'docs'
warning: no previously-included files matching '*.txt' found under 
directory 'docs/_build'
no previously-included directories found matching 
'docs/_build/_sources'

Installing collected packages: pip
  Found existing installation: pip 1.0
Uninstalling pip:
  Successfully uninstalled pip
  Running setup.py install for pip

warning: no files found matching '*.html' under directory 'docs'
warning: no previously-included files matching '*.txt' found under 
directory 'docs/_build'
no previously-included directories found matching 
'docs/_build/_sources'

Installing pip script to /usr/local/bin
Installing pip-2.7 script to /usr/local/bin
Successfully installed pip
Cleaning up...
jfb@jfb-tp:~$ pip --version
bash: /usr/bin/pip: No such file or directory
jfb@jfb-tp:~$

I not sure what the significance of the missing *.txt and *.html files 
is but it looks like I have a path problem. I would appreciate any 
pointers from anyone who has already solved this.


Thanks,  Jim

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2013-02-11 Thread Pravya Reddy
Can You Plaese help me with the code.

#converts temperature to fahrenheit or celsius

def print_options():
print ("Options:")
print (" 'p' print options")
print (" 'c' convert from celsius")
print (" 'f' convert from fahrenheit")
print (" 'q' quit the program")

def celsius_to_fahrenheit(c_temp):
return 9.0/5.0*c_temp+32

def fahrenheit_to_celsius(f_temp):
return (f_temp - 32.0)*5.0/9.0

choice = "p"
while choice != "q":
if choice == "c":
temp = input(input("Celsius temperature:"))
print ("Fahrenheit:",celsius_to_fahrenheit(temp))
elif choice == "f":
temp = input("Fahrenheit temperature:")
print ("Celsius:",fahrenheit_to_celsius(temp))
elif choice != "q":
print_options()
choice = input(input("option:"))

The Output i got is:
Options:
 'p' print options
 'c' convert from celsius
 'f' convert from fahrenheit
 'q' quit the program
option:c
c
Options:
 'p' print options
 'c' convert from celsius
 'f' convert from fahrenheit
 'q' quit the program
option:
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2013-02-11 Thread Pravya Reddy
Can you please help me with the code.

#!/usr/bin/env python
"""
inchtocm.py

"""

def Inchtocm(inches):
"""Returns 2.54 * inches"""
return (2.54 * float(inches_number1))

inches = None
while True:
try:
inches_number1 = input(input("How many inches you want to convert:
"))
inches = float(inches_number1)
print ("You got", Inchtocm(inches), "cm.")
print ("You converted", inches, "inches to cm.")
break
except ValueError:
print ("This is not a number!")

The code is incomplete and i am not getting a proper output:

How many inches you want to convert: 455
455
This is not a number!
How many inches you want to convert:
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with inch to cms conversion .

2013-02-11 Thread Pravya Reddy
Can you please help me with the code.

#!/usr/bin/env python
"""
inchtocm.py

"""

def Inchtocm(inches):
"""Returns 2.54 * inches"""
return (2.54 * float(inches_number1))

inches = None
while True:
try:
inches_number1 = input(input("How many inches you want to convert:
"))
inches = float(inches_number1)
print ("You got", Inchtocm(inches), "cm.")
print ("You converted", inches, "inches to cm.")
break
except ValueError:
print ("This is not a number!")

The code is incomplete and i am not getting a proper output:

How many inches you want to convert: 455
455
This is not a number!
How many inches you want to convert:

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2013-02-11 Thread Joel Goldstick
First, pick a good subject header.

By having caught excep ValueError in your mainline code you lose the
opportunity to find out where it came from.  If you comment it out you will
see that the function inchtocm(inches) uses inches_number1 in its code.  So
you are ignoring the inches that you passed in.  Is that what you want?


On Mon, Feb 11, 2013 at 10:58 AM, Pravya Reddy wrote:

> Can you please help me with the code.
>
> #!/usr/bin/env python
> """
> inchtocm.py
>
> """
>
> def Inchtocm(inches):
> """Returns 2.54 * inches"""
> return (2.54 * float(inches_number1))
>

The line above here is suspect since you likely want to return 2.54 *
inches

>
> inches = None
> while True:
> try:
> inches_number1 = input(input("How many inches you want to convert:
> "))
> inches = float(inches_number1)
> print ("You got", Inchtocm(inches), "cm.")
> print ("You converted", inches, "inches to cm.")
> break
> except ValueError:
> print ("This is not a number!")
>
> The code is incomplete and i am not getting a proper output:
>
> How many inches you want to convert: 455
> 455
> This is not a number!
> How many inches you want to convert:
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which pip for Ubuntu 12.04

2013-02-11 Thread Jim Byrnes

On 02/11/2013 09:31 AM, Jim Byrnes wrote:

On 02/10/2013 01:10 PM, Joel Goldstick wrote:

On Sun, Feb 10, 2013 at 1:53 PM, Timo  wrote:


Op 10-02-13 17:01, Jim Byrnes schreef:

  On 02/09/2013 05:46 AM, Albert-Jan Roskam wrote:



- Original Message -

  From: Jim Byrnes  To: tutor@python.org Cc:

Sent: Saturday, February 9, 2013 3:02 AM Subject: [Tutor] Which pip
for Ubuntu 12.04

How important is it to have the latest pip installed?

Initially I want to use it to install the latest pymongo driver for
mongoDB. The pip version in the Ubuntu 12.04 repositories is 1.0.1.
I see on
http://www.pip-installer.org/**en/latest/the
version is
1.2.1.

It certainly would be easier to install from the repositories but
will that version work to install the latest python packages?

Thanks,  Jim



You could just try it? And downloading it and then doing sudo tar
-xvr pip.tar.gz, cd ..., sudo python setup.py isn't that hard, is
it?



I usually install from the repositories or maybe a ppa so I don't
believe
I have ever done an install from a tar.  If necessary I will
familiarize
myself with the command you gave (yes I saw your followup changing
-xvr to
-xvf) and install that way.

However, I still wonder if using the outdated pip from the repository
will allow me to install the latest python packages? Will trying to
use an
outdated pip cause me problems?


I doubt it will. Have a look at the pip changelog to see what has been
changed.

Timo





  But you're right, I also nnoticed that the Canonical repositories are

a little outdated sometimes. Would be nice if it's possible to add
pypi.org, so updating would be easier.

Albert-Jan




Thanks,  Jim

__**_
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/**mailman/listinfo/tutor




__**_
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/**mailman/listinfo/tutor






You can upgrade pip with pip i believe:

jcg@jcg-desktop:~/code/python$ pip install pip
Requirement already satisfied (use --upgrade to upgrade): pip in
/usr/lib/pymodules/python2.7
Cleaning up...
jcg@jcg-desktop:~/code/python$


jcg@jcg-desktop:~/code/python$ pip install --upgrade pip


Be sure to sudo before if you don't have permissions.




I tried this on my laptop with the following results:

jfb@jfb-tp:~$ pip --version
pip 1.0 from /usr/lib/python2.7/dist-packages (python 2.7)
jfb@jfb-tp:~$ pip install pip
Requirement already satisfied (use --upgrade to upgrade): pip in
/usr/lib/python2.7/dist-packages
Cleaning up...

jfb@jfb-tp:~$ sudo pip install --upgrade pip
[sudo] password for jfb:
Downloading/unpacking pip
   Running setup.py egg_info for package pip

 warning: no files found matching '*.html' under directory 'docs'
 warning: no previously-included files matching '*.txt' found under
directory 'docs/_build'
 no previously-included directories found matching
'docs/_build/_sources'
Installing collected packages: pip
   Found existing installation: pip 1.0
 Uninstalling pip:
   Successfully uninstalled pip
   Running setup.py install for pip

 warning: no files found matching '*.html' under directory 'docs'
 warning: no previously-included files matching '*.txt' found under
directory 'docs/_build'
 no previously-included directories found matching
'docs/_build/_sources'
 Installing pip script to /usr/local/bin
 Installing pip-2.7 script to /usr/local/bin
Successfully installed pip
Cleaning up...
jfb@jfb-tp:~$ pip --version
bash: /usr/bin/pip: No such file or directory
jfb@jfb-tp:~$

I not sure what the significance of the missing *.txt and *.html files
is but it looks like I have a path problem. I would appreciate any
pointers from anyone who has already solved this.

Thanks,  Jim



Apparently I was wrong about the path problem.  I closed the terminal I 
used for the update and opened another session and now I get:


$pip --version
pip 1.2.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

I guess the real test will come when I attempt to use it to install a 
package.


Regards,  Jim


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with inch to cms conversion .

2013-02-11 Thread Dave Angel

On 02/11/2013 11:06 AM, Pravya Reddy wrote:

Can you please help me with the code.

#!/usr/bin/env python
"""
inchtocm.py

"""


First, remove that try/except until the code is free of obvious bugs. 
It's masking where the error actually occurs.  Alternatively, include a 
variable there, and print the stack trace yourself.






def Inchtocm(inches):
 """Returns 2.54 * inches"""
 return (2.54 * float(inches_number1))



As Joel pointed out, you're using a global instead of the parameter that 
was passed.  Call float() on  inches, not on some non-local variable.




inches = None
while True:
 try:
 inches_number1 = input(input("How many inches you want to convert:
"))


Calling input(input())  doesn't do any favors.  It echoes the first 
response, and waits for another one.  The user doesn't probably realize 
that he has to type the number 455 twice.



 inches = float(inches_number1)


Since you're presumably getting the exception on this line, you should 
print out the value you're trying to convert.  You can remove the print 
after it works.



 print ("You got", Inchtocm(inches), "cm.")
 print ("You converted", inches, "inches to cm.")
 break
 except ValueError:
 print ("This is not a number!")

The code is incomplete and i am not getting a proper output:

How many inches you want to convert: 455
455
This is not a number!
How many inches you want to convert:


--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with inch to cms conversion .

2013-02-11 Thread Danny Yoo
On Mon, Feb 11, 2013 at 9:06 AM, Pravya Reddy  wrote:
> Can you please help me with the code.
>
> #!/usr/bin/env python
> """
> inchtocm.py
>
> """
>
> def Inchtocm(inches):
> """Returns 2.54 * inches"""
> return (2.54 * float(inches_number1))


I don't know if your curriculum talks about writing test cases for
functions.  If your instructors do not mention it, ask them, because
it's a crucial concept.  And if they have no clue about it (which is
unfortunately possible), you probably will need to stretch a little to
learn about them yourself.



Python's default testing framework, unittest, unfortunately requires
knowledge on how to use "classes", which you haven't probably seen
yet.  And it's a bit heavyweight, to boot.

In lieu of that, you can use something simpler: just run your
Inchtocm() with a few sample inputs first, and "assert" that they have
the right value, like this:


def Inchtocm(inches):
"""Returns 2.54 * inches"""
return (2.54 * float(inches_number1))

assert Inchtocm(0) == 0


Note that we don't just call Inchtocm(), but check to see that it has
the right value.  This is important: otherwise, you are not really
"testing" the function, but just making it run.  The code above just
has one test: usually, you want to add a few more.  And you want to
write them _before_ you code up the function.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to log process handles and GDI objects in python

2013-02-11 Thread Alan Gauld

On 11/02/13 04:25, Pai, Yogesh M wrote:

Hi Alan,

Here are the additional details:
Python version: 2.7.3
OS version: Windows 7 -64 bit

I have an application that has a automation layer built in python.
During every subsequent runs, I want to log the process threads and GDI objects

> ( the one you see in the Windows task manager) in my python code

You can definitely do it from Python using the Win32 API (which is in 
turn available in Python via ctypes or the pythonwin package).


But its a bit beyond the scope of this list, I'd suggest you will get 
more results on the Win32 mailing list (also on Gmane)...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code to open a website

2013-02-11 Thread Alan Gauld

On 11/02/13 01:14, Steven D'Aprano wrote:


exit() and quit() (as added by the site.py module) are for interactive use.


aha!


They're mostly there for the benefit of newbies. Experienced developers (at
least in the Unix/Linux world) usually know to exit interactive terminal
apps with Ctrl-D. I believe you use Ctrl-Z  under Windows.


Indeed, and Ctrl-D is how I exit the prompt...


For programmatic use in scripts, use sys.exit(), since the site module is
not guaranteed to run.


Thanks for that clarification. No need to change my coding habits after 
all then :-)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] temp conversion problem was: Re: (no subject)

2013-02-11 Thread Alan Gauld
Please always provide a meaningful subject line, it helps us find 
related posts more easily in future and will stimulate more focused 
responses than a no subject message (which implies we are dealing
with a newbie with no net experience and therefore requiring a lot of 
time/effort to help)...



On 11/02/13 15:56, Pravya Reddy wrote:

Can You Plaese help me with the code.


Try to give us a bit more clue about what you think is wrong. What did 
you expect? What did you get? Otherwise we have to cut n paste the code 
and run it and then guess what you think is the problem...


The more work we have to do the less likely we will bother.



def print_options():
 print ("Options:")
 print (" 'p' print options")
 print (" 'c' convert from celsius")
 print (" 'f' convert from fahrenheit")
 print (" 'q' quit the program")


You could have had this return the menu as a string instead of printing it.
Or you could have got the user input inside the function and returned 
the choice directly.

Either would be better than this.


def celsius_to_fahrenheit(c_temp):
 return 9.0/5.0*c_temp+32

def fahrenheit_to_celsius(f_temp):
 return (f_temp - 32.0)*5.0/9.0

choice = "p"


You could have printed the menu and got the users real choice here
rather than set a default that you never use...


while choice != "q":
 if choice == "c":
 temp = input(input("Celsius temperature:"))
 print ("Fahrenheit:",celsius_to_fahrenheit(temp))
 elif choice == "f":
 temp = input("Fahrenheit temperature:")
 print ("Celsius:",fahrenheit_to_celsius(temp))
 elif choice != "q":
 print_options()


This prints the menu if the user did not choose f or q.
But wouldn't you want the menu displayed every time
round the loop?


 choice = input(input("option:"))


Why two inputs?
This prints option and reads the input.
It then prints the option and reads another input.


The Output i got is:
Options:
  'p' print options
  'c' convert from celsius
  'f' convert from fahrenheit
  'q' quit the program
option:c
c


It prints the menu
Then it prints options and reads the value
Then it prints that value and reads another value
which it sets choice to.

If your print_menu() function returned the menu as a string
(and was called get_menu) you could do

choice = input(get_menu())

or if it got the choice in the menu function it would look like

choice = get_menu_item()

Which is a lot simpler to read.

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Alan Gauld

On 11/02/13 05:14, neubyr wrote:


I have a text file with each line in following format:

Book Name, Author Name, Genre, Publication Date

I would like to perform following queries on this file:
  * Get all books written by an author
  * Remove all books of an author
  * Get information about a book (pretty print matching line!)
  * Get books of particular genre

Also, I would like to add and delete entries in this file. I am not
planning to use any database for this purpose and would like to get
better grasp on file parsing and classes/OOP. I need some help in
creating classes and following are my initial thoughts:

# Create a class for Book object
class Book:
   atributes: name, author_name, genre, publication-date



Yep, that looks like all you need.



# Create
Author:
  attribute(s): name


No idea why you want this. A waste of space ...


# Create class for reading and writing to the file
class Booksfile:
   methods: ??


You do not create classes for functions. The functions are part of the 
class. You can have a list of Books read from the file as an attribute 
of your Books class. Populate it at program startup and write the edited 
list back at termination. (Or any other time you make a

change for improved robustness)


* How do I associate/relate Book and Author classes so that it will help
me in getting information like 'get list of books written by an author'?
Data attribute?


I woudn't have a separate Author class but, if you must, something like:

class Book:
  def __init__(self, theAuthor,theTitle):
  self.Author = theAuthor
  self.title = theTitle

class Author:
  def __init__(self,aName):
 self.name = aName

myBook = Book(Author('Jane Austin'), 'Pride & Prejudice')


* Should I create a new Booksfile object for reading, writing and
deleting entries in the file OR add corresponding methods to the book
object itself?


Instance methods in the Book for reading/writing single lines
Class methods in Book for reading/writing the entire file
(or standalone functions could also be used) into a bookList class 
attribute.



I am not planning to use SQLite or any database and would like to use
text file only. Appreciate any help on designing such application.


As a learning exercise that's fine, for any real world problem it would 
be foolish. This is what databases were built for...


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Dave Angel

On 02/11/2013 01:19 PM, Alan Gauld wrote:

On 11/02/13 05:14, neubyr wrote:







* How do I associate/relate Book and Author classes so that it will help
me in getting information like 'get list of books written by an author'?
Data attribute?


I woudn't have a separate Author class but, if you must, something like:

class Book:
   def __init__(self, theAuthor,theTitle):
   self.Author = theAuthor
   self.title = theTitle

class Author:
   def __init__(self,aName):
  self.name = aName

myBook = Book(Author('Jane Austin'), 'Pride & Prejudice')


Big problem with that;  then there may be multiple instances of Author, 
representing the same Author.  Instead, there needs to be a factory 
function which reuses the same Author objects if an additional book with 
the same author is encountered.  Without such an approach, one might as 
well stick with strings, which is what we each recommended.



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] calling and returning functions.

2013-02-11 Thread Pravya Reddy
Can you please complete the code.

#!/usr/bin/env python
""" One function receives a value in inches and returns the equivalent
value in
cms like cm = 2.54 * in.The other function receives a value in cms and
returns
the equivalent value in inches like in = cm / 2.54."""

def conversion(inch,cm):
"""returns the value in cm and in."""
return (2.54 * float(inches))
return (float(cm) / 2.54)
def GetInt(prompt):
"""Returns a number, or None if user doesn't answer."""
while True:
said = input(input(prompt))
if not said:
return None
try:
number = int(said)
except ValueError:
print (said, "is not a number.")
continue
return number
def Test():
first = GetInt('Please enter inches:')
if first:
second = GetInt('Please enter cms:')
print(first, "*", 2.54, "=", "cms")
print(second, "/", 2.54, "=", "in")
Test()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling and returning functions.

2013-02-11 Thread Dave Angel

On 02/11/2013 03:07 PM, Pravya Reddy wrote:

Can you please complete the code.

#!/usr/bin/env python
""" One function receives a value in inches and returns the equivalent
value in
cms like cm = 2.54 * in.The other function receives a value in cms and
returns
the equivalent value in inches like in = cm / 2.54."""

def conversion(inch,cm):
 """returns the value in cm and in."""
 return (2.54 * float(inches))
 return (float(cm) / 2.54)


This second line does nothing, since the first line always returns.
But it doesn't matter much, since you never call it.  Don't you want two 
functions?



def GetInt(prompt):
 """Returns a number, or None if user doesn't answer."""
 while True:
 said = input(input(prompt))


This has been corrected several times, by various people.  What do you 
think it does, and what do you wish it would do?  Is there ever a reason 
to call input with the results of a call to input?



 if not said:
 return None
 try:
 number = int(said)
 except ValueError:
 print (said, "is not a number.")
 continue
 return number


The return never gets executed, since it follows an unconditional 
continue statement.  Do you perhaps want it dedented?



def Test():
 first = GetInt('Please enter inches:')
 if first:
 second = GetInt('Please enter cms:')


Do you always want to do a pair of conversions?  if not, why are you 
asking for both values before doing anything?



 print(first, "*", 2.54, "=", "cms")


This prints an equation without actually printing a result.


 print(second, "/", 2.54, "=", "in")


Ditto.


Test()





--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Additional help

2013-02-11 Thread Ahmet Can KEPENEK
Hello,
I used regular expression module of python. I checked binary and denary
numbers. If input is invalid i ask again. I edited your code. Code is below
follow as .


import re
def show_menu():
 print("===")
 print("1-binary to denary")
 print("2-denary to binary")
 print("3-exit")
 print("===")


while True:
show_menu()

choice = raw_input("please enter an option: ")

if choice == '1':
binary = raw_input("Please enter a binary number: ")
if re.match("^[0-1]*$", binary):
denary = 0
place_value = 1

for i in binary [::-1]:
denary += place_value * int(i)
place_value *= 2

print("The result is",denary)

else:
print("Warning! Please enter a binary number")
continue

elif choice == '2':
denary2 = raw_input("Please enter a denary number: ")
if re.match("^[0-9]*$", denary2):
denary2 = int(denary2)
remainder = ''
while denary2 > 0:
remainder = str(denary2 % 2) + remainder
denary2 >>= 1
print("The result is",remainder)

else:
print("Warning! Please enter a denary number")
continue

elif choice == '3':
 break

elif choice not in '1' or '2' or '3':
print("Invalid input-try again!")






Output is
--
===
1-binary to denary
2-denary to binary
3-exit
===
please enter an option: 1
Please enter a binary number: 110
('The result is', 6)
===
1-binary to denary
2-denary to binary
3-exit
===
please enter an option: 1
Please enter a binary number: 12
Warning! Please enter a binary number
===
1-binary to denary
2-denary to binary
3-exit
===
please enter an option: 3
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread neubyr
Thank you for your inputs Dave. That's really helpful. Reply in-line below:


On Sun, Feb 10, 2013 at 11:56 PM, Dave Angel  wrote:

> On 02/11/2013 12:14 AM, neubyr wrote:
>
>> I have a text file with each line in following format:
>>
>> Book Name, Author Name, Genre, Publication Date
>>
>> I would like to perform following queries on this file:
>>   * Get all books written by an author
>>   * Remove all books of an author
>>   * Get information about a book (pretty print matching line!)
>>   * Get books of particular genre
>>
>> Also, I would like to add and delete entries in this file. I am not
>> planning to use any database for this purpose and would like to get better
>> grasp on file parsing and classes/OOP.
>>
>
> I take it from this that this is a class assignment, and that neither
> speed nor enormous data capacity nor concurrent operation is a requirement.
>  But your professor may have other requirements that you haven't told us
> about.


Not a class assignment, just picked up this problem as a learning exercise.


>
>
> I need some help in creating classes
>
>> and following are my initial thoughts:
>>
>> # Create a class for Book object
>> class Book:
>>atributes: name, author_name, genre, publication-date
>>
>> # Create
>> Author:
>>   attribute(s): name
>>
>
> Is this in order to save space, since each Book instance can then have a
> reference to an Author object, rather than a reference to a string
> containing the Author's name.
>
> If you deem this class useful, then don't you also want one for Genre ?



Hmm.. I didn't think about saving space by reference an object. I created
separate class thinking from database/ORM perspective. I am not planning to
use it right now anyway, but if I was creating a non-trivial application
with database models then I would have had Books tables, Authors tables
etc..

I think I don't need it here though.



>
>
>
>
>> # Create class for reading and writing to the file
>> class Booksfile:
>>methods: ??
>>
>
> Why ?  Are you transliterating this from Java ?



Scrapped that class - adding add/list/delete methods in Book class. Thanks
for pointing it out.


>
>
>
>>
>> * How do I associate/relate Book and Author classes so that it will help
>> me
>> in getting information like 'get list of books written by an author'? Data
>> attribute?
>>
>
> If performance doesn't matter, then create one list of Book instances, and
> search it for whichever characteristics you like.
>
>
>
>  * Should I create a new Booksfile object for reading, writing and deleting
>> entries in the file OR add corresponding methods to the book object
>> itself?
>>
>
> Neither, a pair of regular functions will do fine.  One that loads when
> you start the program, and another that saves the file when you exit. If
> you really expect to update the file incrementally, deleting entries in it,
> then think hard about your decision to roll your own database.  A text file
> isn't random access.
>
>
>
>> I am not planning to use SQLite or any database and would like to use text
>> file only. Appreciate any help on designing such application.
>>
>>
>>
> The real question is where you might proceed after meeting these first
> requirements.  For example, if you expect the list to grow to a few hundred
> million entries, then you'll need to be very careful about layout.  And
> starting and exiting the program would be very slow, since you can do
> nothing till all the data has been loaded in and converted to objects.
>
> Or perhaps you'll want to have another file with additional author data,
> associated with the first by carefully spelling the author's name the same
> in all cases.  In that case, having a separate Author class makes great
> sense.  So if you expect to grow in that direction, then you should create
> the class now, even though it has only one attribute.
>
>
> --
> DaveA
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>


- N
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread neubyr
On Mon, Feb 11, 2013 at 12:36 PM, Dave Angel  wrote:

> On 02/11/2013 01:19 PM, Alan Gauld wrote:
>
>> On 11/02/13 05:14, neubyr wrote:
>>
>>>
>>> 
>>>
>>>
>>  * How do I associate/relate Book and Author classes so that it will help
>>> me in getting information like 'get list of books written by an author'?
>>> Data attribute?
>>>
>>
>> I woudn't have a separate Author class but, if you must, something like:
>>
>> class Book:
>>def __init__(self, theAuthor,theTitle):
>>self.Author = theAuthor
>>self.title = theTitle
>>
>> class Author:
>>def __init__(self,aName):
>>   self.name = aName
>>
>> myBook = Book(Author('Jane Austin'), 'Pride & Prejudice')
>>
>
> Big problem with that;  then there may be multiple instances of Author,
> representing the same Author.  Instead, there needs to be a factory
> function which reuses the same Author objects if an additional book with
> the same author is encountered.  Without such an approach, one might as
> well stick with strings, which is what we each recommended.
>
>
> --
> DaveA
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



Thank you for suggestions - Mitya, Dave and Alan.

I am doing it as a learning exercise and it's not a class assignment. I
have little experience with Ruby and now trying to learn Python. It's not
going to be any production application and I haven't thought about
concurrency problems yet.

Based on suggestions, following is a code snippet that I have right now. I
agree that there isn't any need of separate Author object right now, so I
may remove it as well. I have created a class method 'list_by_author' to
return list of books. I am not sure if class method is right approach to
implement 'list_by_author' function as a class method is typically used as
an alternative constructor. Here I am returning list of objects and not
just an object.

Any suggestions for improving this code will be really useful.


class Book(object):
  def __init__(self,name,author,genre,pubdate):
self.name = name
self.author   = author
self.genre= genre
self.pubdate  = pubdate

  # TODO: use csv module
  # write/add method
  def add(self,uname,kname):
""" Write book info to a file """
pass

  @classmethod
  def list_by_author(self,author):
""" Return list of books of an author """
bookfile = config.bookfile
books = [] # empty list - used as list of Books
# TODO: improve regex
regex = re.compile(author)
with open (bookfile,'r') as f:
  for line in f:
if regex.findall(line):
  # error prone - if name contains comma
  l = line.split(',')
  # create book object and append it to a list
  book = self(*l)
  books.append(book)
return books # return list of books



class Author(object):
  def __init__(self,name):
self.name = name

  def books(self):
""" Get list of books """
books = Book.list_by_author(self.name)
return books



- N
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Alan Gauld

On 11/02/13 22:49, neubyr wrote:


is right approach to implement 'list_by_author' function as a class
method is typically used as an alternative constructor.


Not at all that is only one use case for class methods.
A class method is *any* method that operates on the whole class of 
objects - i.e. all instances(potentially including those still to be 
created)


Now I agree that in some lanmguages they tend to be limited to factory 
metjods(eg Objective C implicitly goes that way) but in others  like 
Smalltalk search methods and persistence etc are often found as class 
methods. In Python, because of it's hybrid nature, these are often 
"demoted" to global scope functions.




Here I am
returning list of objects and not just an object.


Which is to say a subset of the class Book.
Therefore quite reasonably a class method.


   @classmethod
   def list_by_author(self,author):
 """ Return list of books of an author """
 bookfile = config.bookfile
 books = [] # empty list - used as list of Books
 # TODO: improve regex
 regex = re.compile(author)
 with open (bookfile,'r') as f:
   for line in f:
 if regex.findall(line):
   # error prone - if name contains comma
   l = line.split(',')
   # create book object and append it to a list
   book = self(*l)
   books.append(book)
 return books # return list of books


I'd probably, for a small dataset, load a list of books into the class 
at startup and save it at termination. Which makes the search code 
easier and potentially can be data driven so you pass the filter 
attribute as a parameter. You can then use getattr() to find the value:


def findBooks(cls, param, value):
return [book for book in books if getattr(book, param) == value]

You could make it more flexible still by defining the filter as a 
function and passing a lambda:


def findBooks(cls, filterExp):
return [book for book in books if filterExp(book)]


Usage example:

DickensBooks = Book.findBook(lambda b: 'Dickens' in b.author)


Of course if you have a huge dataset then that won't work - which brings 
us back to a database :-)


HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling and returning functions.

2013-02-11 Thread Alan Gauld

On 11/02/13 20:07, Pravya Reddy wrote:


def conversion(inch,cm):
 """returns the value in cm and in."""
 return (2.54 * float(inches))
 return (float(cm) / 2.54)


return exits the function so the second return statement never gets 
executed.



def GetInt(prompt):
 """Returns a number, or None if user doesn't answer."""
 while True:
 said = input(input(prompt))


You are still using double input. That is almost never the
right thing to do.


 if not said:
 return None
 try:
 number = int(said)
 except ValueError:
 print (said, "is not a number.")
 continue
 return number


The continue will jump right back to the loop start so the return will 
never be executed.


The only way this function will ever exit is if the user hits Enter 
twice and then it will return None.


You need to rethink the logic here.


def Test():
 first = GetInt('Please enter inches:')
 if first:
 second = GetInt('Please enter cms:')
 print(first, "*", 2.54, "=", "cms")
 print(second, "/", 2.54, "=", "in")


Since GetInt() can only ever return None the if will always fail and 
nothing will ever be printed.


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread neubyr
On Mon, Feb 11, 2013 at 5:16 PM, Alan Gauld wrote:

> On 11/02/13 22:49, neubyr wrote:
>
>  is right approach to implement 'list_by_author' function as a class
>> method is typically used as an alternative constructor.
>>
>
> Not at all that is only one use case for class methods.
> A class method is *any* method that operates on the whole class of objects
> - i.e. all instances(potentially including those still to be created)
>
> Now I agree that in some lanmguages they tend to be limited to factory
> metjods(eg Objective C implicitly goes that way) but in others  like
> Smalltalk search methods and persistence etc are often found as class
> methods. In Python, because of it's hybrid nature, these are often
> "demoted" to global scope functions.
>
>
>
>  Here I am
>> returning list of objects and not just an object.
>>
>
> Which is to say a subset of the class Book.
> Therefore quite reasonably a class method.
>
>
> @classmethod
>>def list_by_author(self,author):
>>  """ Return list of books of an author """
>>  bookfile = config.bookfile
>>  books = [] # empty list - used as list of Books
>>  # TODO: improve regex
>>  regex = re.compile(author)
>>  with open (bookfile,'r') as f:
>>for line in f:
>>  if regex.findall(line):
>># error prone - if name contains comma
>>l = line.split(',')
>># create book object and append it to a list
>>book = self(*l)
>>books.append(book)
>>  return books # return list of books
>>
>
> I'd probably, for a small dataset, load a list of books into the class at
> startup and save it at termination. Which makes the search code easier and
> potentially can be data driven so you pass the filter attribute as a
> parameter. You can then use getattr() to find the value:
>
> def findBooks(cls, param, value):
> return [book for book in books if getattr(book, param) == value]
>
> You could make it more flexible still by defining the filter as a function
> and passing a lambda:
>
> def findBooks(cls, filterExp):
> return [book for book in books if filterExp(book)]
>
>
> Usage example:
>
> DickensBooks = Book.findBook(lambda b: 'Dickens' in b.author)
>
>
> Of course if you have a huge dataset then that won't work - which brings
> us back to a database :-)
>
>
> HTH
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>


That's really helpful Alan. Thank you for your inputs on class methods and
how can I modify my existing find/list method.

I am not using any database/ORM as I am trying to learn basic search/filter
operations on file and enumerator objects. Also, ORMs generally add magic
methods based on associations (at least with Ruby ActiveRecord: e.g.
has_many and belongs_to associations). I would like to do it 'manually'
before using any other library.

--
N
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Steven D'Aprano

On 11/02/13 16:14, neubyr wrote:

I have a text file with each line in following format:

Book Name, Author Name, Genre, Publication Date

I would like to perform following queries on this file:
  * Get all books written by an author
  * Remove all books of an author
  * Get information about a book (pretty print matching line!)
  * Get books of particular genre

Also, I would like to add and delete entries in this file. I am not
planning to use any database for this purpose and would like to get better
grasp on file parsing and classes/OOP. I need some help in creating classes
and following are my initial thoughts:

# Create a class for Book object
class Book:
   atributes: name, author_name, genre, publication-date



You could use a class. But since Books don't have any behaviour, a simple
struct or record would be better than a class:


from collections import namedtuple
Book = namedtuple("Book", "name author genre date")

lotr = Book("The Hobbit", "J.R.R. Tolkien", "Fantasy", "1937")


This has the advantage of simplicity. But if you need to add behaviour to the
Book class, e.g. validation of the fields, you should be able to inherit from
a named tuple. Untested:


class Book(namedtuple("Book", "name author genre date")):
@property
def genre(self):
return super(Book, self).genre
@genre.setter(self, value):
super(Book, self).genre = value.title()  # 'fantasy' -> 'Fantasy'



# Create
Author:
  attribute(s): name



As Alan suggested, a waste of time. Since the Author has no behaviour and
only a single field, why not just use a string?




# Create class for reading and writing to the file
class Booksfile:
   methods: ??


Why should this be a class? This is not Java.

http://steve-yegge.blogspot.com.au/2006/03/execution-in-kingdom-of-nouns.html


Just write a function that reads a file and returns a list of Books.

Or perhaps I should say:


Programmer().getwriter().write(MakeCallable(FileReader).setmethod("read",
return_type=list, return_item_values=Book)




* How do I associate/relate Book and Author classes so that it will help me
in getting information like 'get list of books written by an author'? Data
attribute?


You want to map authors to books. Whenever you want a mapping, use a dict:


data = {
'J.R.R. Tolkien': [Book("The Hobbit"), Book("The Lord of the Rings")],
'Tom Clancy': [Book("The Hunt for Red October")],
'Terry Pratchett': [Book('Small Gods'), Book('Night Watch'),
Book('Nation')],
'Stephenie Meyer': [
Book('Something about abusive boyfriends but that's okay because they 
sparkle')],
}




* Should I create a new Booksfile object for reading, writing and deleting
entries in the file OR add corresponding methods to the book object itself?


Heavens no. Why should the book know about the library catalog it is listed in?
Your book class should be responsible for the book, and nothing but the book.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread neubyr
On Mon, Feb 11, 2013 at 6:58 PM, Steven D'Aprano wrote:

> On 11/02/13 16:14, neubyr wrote:
>
>> I have a text file with each line in following format:
>>
>> Book Name, Author Name, Genre, Publication Date
>>
>> I would like to perform following queries on this file:
>>   * Get all books written by an author
>>   * Remove all books of an author
>>   * Get information about a book (pretty print matching line!)
>>   * Get books of particular genre
>>
>> Also, I would like to add and delete entries in this file. I am not
>> planning to use any database for this purpose and would like to get better
>> grasp on file parsing and classes/OOP. I need some help in creating
>> classes
>> and following are my initial thoughts:
>>
>> # Create a class for Book object
>> class Book:
>>atributes: name, author_name, genre, publication-date
>>
>
>
> You could use a class. But since Books don't have any behaviour, a simple
> struct or record would be better than a class:
>
>
> from collections import namedtuple
> Book = namedtuple("Book", "name author genre date")
>
> lotr = Book("The Hobbit", "J.R.R. Tolkien", "Fantasy", "1937")
>
>
> This has the advantage of simplicity. But if you need to add behaviour to
> the
> Book class, e.g. validation of the fields, you should be able to inherit
> from
> a named tuple. Untested:
>
>
> class Book(namedtuple("Book", "name author genre date")):
> @property
> def genre(self):
> return super(Book, self).genre
> @genre.setter(self, value):
> super(Book, self).genre = value.title()  # 'fantasy' -> 'Fantasy'
>
>
>
>  # Create
>> Author:
>>   attribute(s): name
>>
>
>
> As Alan suggested, a waste of time. Since the Author has no behaviour and
> only a single field, why not just use a string?
>
>
>
>
>  # Create class for reading and writing to the file
>> class Booksfile:
>>methods: ??
>>
>
> Why should this be a class? This is not Java.
>
> http://steve-yegge.blogspot.**com.au/2006/03/execution-in-**
> kingdom-of-nouns.html
>
>
> Just write a function that reads a file and returns a list of Books.
>
> Or perhaps I should say:
>
>
> Programmer().getwriter().**write(MakeCallable(FileReader)**
> .setmethod("read",
> return_type=list, return_item_values=Book)
>
>
>
>
>  * How do I associate/relate Book and Author classes so that it will help
>> me
>> in getting information like 'get list of books written by an author'? Data
>> attribute?
>>
>
> You want to map authors to books. Whenever you want a mapping, use a dict:
>
>
> data = {
> 'J.R.R. Tolkien': [Book("The Hobbit"), Book("The Lord of the Rings")],
> 'Tom Clancy': [Book("The Hunt for Red October")],
> 'Terry Pratchett': [Book('Small Gods'), Book('Night Watch'),
> Book('Nation')],
> 'Stephenie Meyer': [
> Book('Something about abusive boyfriends but that's okay because
> they sparkle')],
>
> }
>
>
>
>  * Should I create a new Booksfile object for reading, writing and deleting
>> entries in the file OR add corresponding methods to the book object
>> itself?
>>
>
> Heavens no. Why should the book know about the library catalog it is
> listed in?
> Your book class should be responsible for the book, and nothing but the
> book.
>
>
>
>
> --
> Steven
>
>

Thanks Steven!

I have used namedtuple like approach in few Ruby programs (not the same
problem) using Structs, but it didn't strike me for this exercise [1]. I am
going to try this approach soon.

I haven't added any validation methods for fields yet, but I am planning to
add them soon - e.g. validate alphabets or alphanumeric characters etc. It
may bring up new questions from my side, but I am sure you all will be glad
to help.. :)

1. http://www.ruby-doc.org/core-1.9.3/Struct.html

-N
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Steven D'Aprano

On 12/02/13 10:16, Alan Gauld wrote:

On 11/02/13 22:49, neubyr wrote:


is right approach to implement 'list_by_author' function as a class
method is typically used as an alternative constructor.


Not at all that is only one use case for class methods.
A class method is *any* method that operates on the whole class of
objects - i.e. all instances(potentially including those still to be
created)



Strictly speaking, a class method is just a method which takes as its
first argument the class itself, not the instance. What it does with
that is completely open.

The usual thing is to use class methods for alternative constructors,
e.g. Decimal.fromfloat.

If the class keeps a list of all instances, then the class method
could walk the list and operate on each instance in turn. (But why
would you do that?)

If the class method modifies the class itself, then it could indirectly
have an effect on each instance.

Although class methods could do anything, it is hard to think of
actually useful things for them to do apart from being used as a
constructor.

[...]

Here I am
returning list of objects and not just an object.


Which is to say a subset of the class Book.
Therefore quite reasonably a class method.


Just a minute, I think that is completely wrong. A Book is not a set,
so how can you have subset of it?

What is a subset of "Pride and Prejudice"? Perhaps chapter 5.

There are more problems with this idea that you query the Book to get
a list of books by some author. Suppose you did this:

prpr = Book("Pride and Prejudice", "Jane Austin")
prpr.list_by_author()

Now *each and every* book is responsible for tracking all the other
books by the same author. This is a lousy design. Even worse:

prpr.list_by_author("Jane Austin")


since now books are responsible for tracking ALL books by ALL authors,
since the caller could say:

prpr.list_by_author("Leo Tolstoy")


Of course, books should know their own author, not the authors of other
books, but authors should know all their own books:

author = prpr.author  # --> Author("Jane Austin")

author.get_books()  # --> return a list of books by this author


This is an argument for making Authors a class, with behaviour, rather
than just a string.



@classmethod
def list_by_author(self,author):
""" Return list of books of an author """
bookfile = config.bookfile
books = [] # empty list - used as list of Books

[snip]


First off, by convention the first argument to a class method should be
called "cls", not "self".

Secondly, here you are relying on a mysterious global "config", which
points to a bookfile. What does this have to do with a book?

- Does a nail keep track of the packet it came from?

- Why should a book keep track of the catalog it was listed in?

This should be a top level function, not a Book method.

The rest of the method's design is also poor. You have already read
the file once, to get the initial set of books. So why read the file
again, every time you want to get some piece of information.

Big databases, capable of holding billions of pieces of data, have
to use disk-based storage because you can't keep that much data in
memory at once. For anything smaller, you should only read and write
to disk for persistence, everything else should use in-memory data
structures. In this case, that means a dict.



return books # return list of books


Really? Thank goodness for the comment, I wouldn't have understood
that line of code otherwise!

*wink*



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] associating two objects without ORM and processing a text file

2013-02-11 Thread Alan Gauld

On 12/02/13 01:34, Steven D'Aprano wrote:


If the class keeps a list of all instances, then the class method
could walk the list and operate on each instance in turn. (But why
would you do that?)


Because its how classes work in some languages (like smalltalk).
The class object represents all instances of the class.


Although class methods could do anything, it is hard to think of
actually useful things for them to do apart from being used as a
constructor.


Again its the norm in Smalltalk apps to implement electors(searches) and 
other class wide operations in the class methods. Most Smalltalk classes 
have at least 5 or 6 class methods.



Which is to say a subset of the class Book.
Therefore quite reasonably a class method.


Just a minute, I think that is completely wrong. A Book is not a set,
so how can you have subset of it?


A Book is an instance of the class Book.
Book is a class of object encompassing all books.
So returning a subset of all instances of the class is normal class 
method behaviour.



What is a subset of "Pride and Prejudice"? Perhaps chapter 5.


That might be a chapter or two? But Pride and Prejudice is not the class 
Book, it's an instance of Book.



There are more problems with this idea that you query the Book to get
a list of books by some author. Suppose you did this:

prpr = Book("Pride and Prejudice", "Jane Austin")
prpr.list_by_author()


No, you don't query the instance, you should query the class.

Book.list_by_author()   # -> a dict of lists keyed by author name?

(One of the unfortunate features of Python's implementation of
class methods is that you can call them from an instance which
doesn't really make sense! IMHO)


Now *each and every* book is responsible for tracking all the other
books by the same author. This is a lousy design. Even worse:

prpr.list_by_author("Jane Austin")

since now books are responsible for tracking ALL books by ALL authors,


Which is why it should be a class method not an instance one.
And in the real world we'd do that by building class methods
querying a database.


Of course, books should know their own author, not the authors of other
books, but authors should know all their own books:


instances should, I agree.

The Book class should know about all books in the class.


First off, by convention the first argument to a class method should be
called "cls", not "self".


Yes, my bad.
Although it is of course only a Python convention.


Secondly, here you are relying on a mysterious global "config", which
points to a bookfile. What does this have to do with a book?


The storage mechanism is an implementation detail. In Smalltalk its part 
of the infrastructure of the runtime.



- Does a nail keep track of the packet it came from?


No, but the nail factory may keep track of the nails it
produces, or at least the batches...


- Why should a book keep track of the catalog it was listed in?


A book wouldn't. but the Book class might.


The rest of the method's design is also poor. You have already read
the file once, to get the initial set of books. So why read the file
again, every time you want to get some piece of information.


This I agree with. It would be better to persist the data somewhere for 
a small dataset.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] sockets and networking

2013-02-11 Thread richard kappler
I want to be able to communicate between a raspberry pi and a laptop
running ubuntu, transferring text from the pi to the laptop. Specifically
the Pi would be running poscketsphinx speech rec and would send the
generated text through the crossover between the two ethernet ports to a
python program on the laptop. Someone suggest using sockets, with which I
am completely unfamiliar. I've read the socket docs and admit I find them
quite confusing. Can someone point me to a/some good beginner tutorials on
sockets/networking for me to look at?

regards, Richard

-- 

quando omni flunkus moritati
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sockets and networking

2013-02-11 Thread eryksun
On Mon, Feb 11, 2013 at 11:27 PM, richard kappler  wrote:
> I've read the socket docs and admit I find them quite confusing. Can someone
> point me to a/some good beginner tutorials on sockets/networking for me to
> look at?

Try Doug Hellmann's Python Module of the Week:

http://www.doughellmann.com/PyMOTW/socket
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor