[Tutor] Need a Python tutor

2012-11-30 Thread Oxford Learning Centre
Hi there
My son is in first year Engineering & he needs help with reviewing for his 
final next week on computer programming using Python language. Please if u have 
or know any one who can help him that will be a great help. He is in school in 
Hamilton Ontario , but you can reach me either by phone 9052571207 or by e-mail.

Thanks

Saly Beshara
Centre Director
Oxford Learning Centre Oakville N/E
380 Dundas St. East Unit D-8
905-257-1207
www.oxfordlearning.com
Sent from my iPad
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pyXML i Python2.6

2012-11-30 Thread Sreenivasulu
Hi Ramith,

Thank you for your reply .

I have made some modifications in pyXMl  and it is working fine .


I have file named as Test.py and below is the code in the file


from SOAPpy import WSDL

SecurityServerUrlTemplate = "http://
%s/SpinSecurity/SpinInfoExchange.asmx?WSDL"
secser1="Coputername"
url = SecurityServerUrlTemplate % secser1
server = WSDL.Proxy(url)
print "hello"

when am running  python2.6 Test.py , am getting below error :

xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1,
column 5

But when am running with sudo python2.6 Test.py . it was running without
error .

Do you have any idea on this issue.

Regards,
Sreenu.



On Tue, Nov 20, 2012 at 11:59 PM, Prasad, Ramit
wrote:

> Sreenivasulu wrote:
> >
> > Hi,
> >
> > Am unable to install pyXML in Ubuntu usig python2.6
> >
> > Could you please help me
> >
> > Regards,
> > Sreenu
>
> Since you do not provide a link I am guessing you are referring
> to the very outdated package. You use the ElementTree class or the
> reputed third party module lxml. The ElementTree class is in the
> Python 2.6 standard library at xml.etree.ElementTree.ElementTree.
>
>
> You can try modifying the pyXML source (seems like a small change),
> but I do not recommend this approach.
> http://stackoverflow.com/questions/4953600/pyxml-on-ubuntu
>
>
> ~Ramit
>
>
>
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New to Python - simple question

2012-11-30 Thread Unaiza Ahsan
I'm on Chapter 1 of Solem's book. The following function definition needs
to be added to imtools.py: [I did paste this I think in my first email].

import os
from numpy import *

def histeq(im,nbr_bins=256):
""" Histogram equalization of a grayscale image. """
# get image histogram
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() # cumulative distribution function
cdf = 255 * cdf / cdf[-1] # normalize
# use linear interpolation of cdf to find new pixel values
im2 = interp(im.flatten(),bins[:-1],cdf)
return im2.reshape(im.shape), cdf


Now this file is saved as imtools.py in my main C:\Python27 folder.

In IDLE, I'm supposed to write:

from PIL import Image
from numpy import *
im = array(Image.open(’AquaTermi_lowcontrast.jpg’).convert(’L’))
im2,cdf = imtools.histeq(im)

That's it. That's the code I'm using. But it's giving me that error that
"histogram" is not found.

Er, I just checked the numpy version I have downloaded. It indeed doesn't
have any histogram (although its documentation says it should :s). Sorry. I
had checked the documentation first (and thought that this version contains
histogram).

I think the function histogram is not defined in numpy at all. It's in
something called "mahotas" (No idea what that is).

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


[Tutor] (no subject)

2012-11-30 Thread Tara Nicholson

Hi, im trying to write a script which randomly generates 10,000 points(x,y) in 
the unit square(so range between 0 and 1 for both x and y).so far I have 
written the code below in red, however it only produces one random point. How 
do I get it to repeat this so it produces 10,000 different random 
points?Thankyouuu, Tara.
import mathimport randomrandom.seed() x=random.uniform(0,1)y=random.uniform(0,1)
for i in range(0,1):for j in range(0,1):print (x,y)
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] FW: (no subject)

2012-11-30 Thread leon zaat



From: tara.nichol...@live.co.uk
To: tutor@python.org
Date: Tue, 20 Nov 2012 14:39:06 +
Subject: [Tutor] (no subject)





Hi, im trying to write a script which randomly generates 10,000 points(x,y) in 
the unit square(so range between 0 and 1 for both x and y).so far I have 
written the code below in red, however it only produces one random point. How 
do I get it to repeat this so it produces 10,000 different random 
points?Thankyouuu, Tara.
import mathimport randomrandom.seed() x=random.uniform(0,1)y=random.uniform(0,1)
for i in range(0,1):for j in range(0,1):print (x,y)
  

___
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pypi entry points

2012-11-30 Thread Albert-Jan Roskam
Hi,

I am reading The Hitchhiker's Guide to Packaging 1.0 documentation, 
specifically http://guide.python-distribute.org/creation.html. I am struggling 
to understand the section about Entry Points. This appears to be a way to 
include extra functionality in a program, without the need to actually include 
it in the tar.gz file. Or, as the website says: "[A] pretty easy and simple way 
to allow other packages to register something
that you want to know. Extra plugins, extra render methods, extra functionality
you want to register in your web application, etcetera." I find the code 
examples hard to understand. I'd expect that it'd be steps like "in the middle 
of the setup, download and unzip file .zip from website http://blaaah.eu. 
How does this work?

 
Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~ 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] FW: (no subject)

2012-11-30 Thread Steven D'Aprano

Hello Tara, or is it Leon?

On 30/11/12 20:21, leon zaat wrote:


Hi, im trying to write a script which randomly generates 10,000 points(x,y)
in the unit square(so range between 0 and 1 for both x and y).so far I have
written the code below in red,


What red? I see no red.

Please do not rely on colour in email, for at least two reasons:

1) For colour to be visible in your email, you must have so-called "Rich Text"
   (actually HTML mail) turned on. Many people have HTML mail turned off,
   because it is a security and privacy threat, or simply do not appreciate
   having somebody else's choice of colour and font being forced on them.

2) About 8% of men and 0.5% of women have red-green colour-blindness, with other
   forms of colour-blindness being less common. All up, about 1 in 10 men and 1
   in 100 women cannot easily or at all distinguish colours. Others may be
   partially or completely blind. Using a screen reader, they can still "read"
   the words you send, but will have no idea at all about colours.




however it only produces one random point. How do I get it to repeat this so it
produces 10,000 different random points?




import math
import random
random.seed()
x=random.uniform(0,1)
y=random.uniform(0,1)

This generates one random point, because it gets executed once. After 
generating that
single random point, you then loop 1x1 times:


for i in range(0,1):  # this line loops once
for j in range(0,1):  # and this also loops once
print (x,y)


so it only 1x1 = 1 time.

The solution is simple: if you want to generate 10,000 points, you must put the
code that chooses a random point inside something that will loop 10,000 times.
Here's an example that will loop 20 times:


for i in range(20):
# Pick an x and y ar random
x = random.uniform(0,1)
y = random.uniform(0,1)
print (x, y)




All you have to do now is extend that to loop 1 times instead of 20.




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


Re: [Tutor] Need a Python tutor

2012-11-30 Thread Steven D'Aprano

Hi Saly,

My comments below.


On 29/11/12 18:03, Oxford Learning Centre wrote:

Hi there
My son is in first year Engineering&  he needs help with reviewing
for his final next week on computer programming using Python
language. Please if u have or know any one who can help him that
will be a great help. He is in school in Hamilton Ontario , but
you can reach me either by phone 9052571207 or by e-mail.



Are you looking for a paid tutor who can come out and work with your
son in person? I'm not sure if anyone here is from Canada, let alone
Hamilton, but you may be lucky.

If not, with your permission I can pass your details on if I can find
anyone in the area.

Otherwise, we're more than happy to help your son via email, on this
mailing list.



Regards,




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


Re: [Tutor] FW: (no subject)

2012-11-30 Thread Albert-Jan Roskam



>> Hi, im trying to write a script which randomly generates 10,000 points(x,y)
>> in the unit square(so range between 0 and 1 for both x and y).so far I have
>> written the code below in red,
>
>What red? I see no red.
>
>Please do not rely on colour in email, for at least two reasons:
>
>1) For colour to be visible in your email, you must have so-called "Rich Text"
>   (actually HTML mail) turned on. Many people have HTML mail turned off,
>   because it is a security and privacy threat, or simply do not appreciate
>   having somebody else's choice of colour and font being forced on them.
>
>2) About 8% of men and 0.5% of women have red-green colour-blindness, with 
>other
>   forms of colour-blindness being less common. All up, about 1 in 10 men and 1
>   in 100 women cannot easily or at all distinguish colours. Others may be
>   partially or completely blind. Using a screen reader, they can still "read"
>   the words you send, but will have no idea at all about colours.
>


Aside from colour-blindness, using (saturated) red and blue is an ergnomical 
no-no as the human eye is relatively insensitive to those colors: 

http://www.lawrence-najjar.com/papers/Using_color_effectively.html

http://hyperphysics.phy-astr.gsu.edu/hbase/vision/colcon.html

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


[Tutor] how to struct.pack a unicode string?

2012-11-30 Thread Albert-Jan Roskam
Hi,

How can I pack a unicode string using the struct module? If I simply use packed 
= struct.pack(fmt, hello) in the code below (and 'hello' is a unicode string), 
I get this: "error: argument for 's' must be a string". I keep reading that I 
have to encode it to a utf-8 bytestring, but this does not work (it yields 
mojibake and tofu output for some of the languages). It's annoying if one needs 
to know the encoding in which each individual language should be represented. I 
was hoping "unicode-internal" was the way to do it, but this does not reproduce 
the original string when I unpack it.. :-(



# Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] 
on win32

import sys
import struct

greetings = \
    [['Arabic', [1575, 1604, 1587, 1604, 1575, 1605, 32, 1593, 1604, 1610, 
1603,
 1605], 'cp1256'], # 'cp864' 'iso8859_6'
 ['Assamese', [2472, 2478, 2488, 2509, 2453, 2494, 2544], 'utf-8'],
 ['Bengali', [2438, 2488, 2488, 2494, 2482, 2494, 2478, 2497, 32, 2438,
  2482, 2494, 2439, 2453, 2497, 2478], 'utf-8'],
 ['Georgian', [4306, 4304, 4315, 4304, 4320, 4335, 4317, 4305, 4304], 
'utf-8'],
 ['Kazakh', [1057, 1241, 1083, 1077, 1084, 1077, 1090, 1089, 1110, 
1079, 32,
 1073, 1077], 'utf-8'],
 ['Russian', [1047, 1076, 1088,1072, 1074, 1089, 1090, 1074, 1091, 1081,
  1090, 1077], 'utf-8'],
 ['Spanish', [161, 72, 111, 108, 97, 33], 'cp1252'],
 ['Swiss German', [71, 114, 252, 101, 122, 105], 'cp1252'],
 ['Thai', [3626, 3623, 3633, 3626, 3604, 3637], 'cp874'],
 ['Walloon', [66, 111, 110, 100, 106, 111, 251], 'cp1252']] 
for greet in greetings:
    language, chars, encoding = greet
    hello = "".join([unichr(i) for i in chars])
    #print language, hello, encoding  # prints everything as it should look
    endianness = "<" if sys.byteorder == "little" else ">"
    fmt = endianness + str(len(hello)) + "s"
    #https://code.activestate.com/lists/python-list/301601/
    #http://bytes.com/topic/python/answers/546519-unicode-strings-struct-files
    #packed = struct.pack(fmt, hello.encode('utf_32_le'))
    #packed = struct.pack(fmt, hello.encode(encoding))
    #packed = struct.pack(fmt, hello.encode('utf_8'))
    packed = struct.pack(fmt, hello.encode("unicode-internal"))
    print struct.unpack(fmt, packed)[0].decode("unicode-internal")  # 
UnicodeDecodeError: 'unicode_internal' codec can't decode byte 0x00 in position 
12: truncated input


Thank you in advance!

 
Regards,
Albert-Jan


~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a 
fresh water system, and public health, what have the Romans ever done for us?
~~ 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to struct.pack a unicode string?

2012-11-30 Thread Peter Otten
Albert-Jan Roskam wrote:

> How can I pack a unicode string using the struct module? If I simply use
> packed = struct.pack(fmt, hello) in the code below (and 'hello' is a
> unicode string), I get this: "error: argument for 's' must be a string". I
> keep reading that I have to encode it to a utf-8 bytestring, but this does
> not work (it yields mojibake and tofu output for some of the languages).

You keep reading it because it is the right approach. You will not get 
mojibake if you decode the "packed" data before using it. 

Your code basically becomes

for greet in greetings:
language, chars, encoding = greet
hello = "".join([unichr(i) for i in chars])
packed = hello.encode("utf-8")
unpacked = packed.decode("utf-8")
print unpacked

I don't know why you mess with byte order, perhaps you can tell a bit about 
your actual use-case.

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


Re: [Tutor] FW: (no subject)

2012-11-30 Thread Ed Owens
Hi, im trying to write a script which randomly generates 10,000 points(x,y)
in the unit square(so range between 0 and 1 for both x and y).

so far I have written the code below in red, however it only produces one
random point. How do I get it to repeat this so it produces 10,000 different
random points?

Thankyouuu, Tara.

 

import math

import random

random.seed()

 

x=random.uniform(0,1)

y=random.uniform(0,1)

 

-  Doesn't the above just produce a single X and Y?

 

for i in range(0,1):

for j in range(0,1):

print (x,y)

 

-  And this only gives a single I, and single j,
resulting in one 'print' execution.

 

If you want 10K points, then the point-generating statements (X=., Y=.)
needs to be enclosed in a loop that increments the number of times you want.
Where's that range(0,1) loop?

 

Ed

 

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


Re: [Tutor] how to struct.pack a unicode string?

2012-11-30 Thread Steven D'Aprano

On 01/12/12 03:43, Albert-Jan Roskam wrote:

Hi,

How can I pack a unicode string using the struct module? If I
simply use packed = struct.pack(fmt, hello) in the code below
(and 'hello' is a unicode string), I get this:
"error: argument for 's' must be a string".


To be precise, it must be a *byte* string, not a Unicode string.



I keep reading that I have to encode it to a utf-8 bytestring,


To be precise, you can use any encoding you like, with the
following provisos:

* not all encodings are capable of representing every character
  (e.g. the ASCII encoding only represents 127 characters);

* some encodings may not quite round-trip exactly, that is, they
  may lose some information;

* some encodings are more compact than others (e.g. Latin-1 uses
  one byte per character, while UTF-32 uses four bytes per
  character).



but this does not work (it yields mojibake and tofu output for
some of the languages).


It would be useful to see an example of this.

But if you do your encoding/decoding correctly, using the right
codecs, you should never get mojibake. You only get that when
you have a mismatch between the encoding you think you have and
the encoding you actually have.



It's annoying if one needs to know the encoding in which each
individual language should be represented. I was hoping
"unicode-internal" was the way to do it, but this does not
reproduce the original string when I unpack it.. :-(


Yes, encodings are annoying. The sooner that all encodings other
than UTF-8 and UTF-32 disappear the better :)

The beauty of using UTF-8 instead of one of the many legacy
encodings is that UTF-8 can represent any character, so you don't
need to care about the individual language, and it is compact (at
least for Western European languages).

Why are you using struct for this? If you want to convert Unicode
strings into a sequence of bytes, that's exactly what the encode
method does. There's no need for struct.



greetings = [
('Arabic', 
u'\u0627\u0644\u0633\u0644\u0627\u0645\u0020\u0639\u0644\u064a\u0643\u0645', 
'cp1256'),
('Assamese', u'\u09a8\u09ae\u09b8\u09cd\u0995\u09be\u09f0', 'utf-8'),
('Bengali', u'\u0986\u09b8\u09b8\u09be\u09b2\u09be\u09ae\u09c1 
\u0986\u09b2\u09be\u0987\u0995\u09c1\u09ae', 'utf-8'),
('English', u'Greetings and salutations', 'ascii'),
('Georgian', u'\u10d2\u10d0\u10db\u10d0\u10e0\u10ef\u10dd\u10d1\u10d0', 
'utf-8'),
('Kazakh', 
u'\u0421\u04d9\u043b\u0435\u043c\u0435\u0442\u0441\u0456\u0437 \u0431\u0435', 
'utf-8'),
('Russian', 
u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', 
'utf-8'),
('Spanish', u'\xa1Hola!', 'cp1252'),
('Swiss German', u'Gr\xfcezi', 'cp1252'),
('Thai', u'\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35', 'cp874'),
('Walloon', u'Bondjo\xfb', 'cp1252'),
]
for language, greet, encoding in greetings:
print u"Hello in %s: %s" % (language, greet)
for enc in ('utf-8', 'utf-16', 'utf-32', encoding):
bytestring = greet.encode(enc)
print "encoded as %s gives %r" % (enc, bytestring)
if bytestring.decode(enc) != greet:
print "*** round-trip encoding/decoding failed ***"


Any of the byte strings can then be written directly to a file:

f.write(bytestring)

or embedded into a struct. You need a variable-length struct, of course.

My advice: stick to Python unicode strings internally, and always write
them to files as UTF-8.



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


Re: [Tutor] how to struct.pack a unicode string?

2012-11-30 Thread eryksun
On Fri, Nov 30, 2012 at 11:43 AM, Albert-Jan Roskam  wrote:
>
> How can I pack a unicode string using the struct module?

struct.pack is for packing an arbitrary sequence of data into a C-like
struct. You have to manually add pad bytes. Alternatively you can use
a ctypes.Structure.

The struct module supports plain byte strings, not Unicode. UTF-8 was
designed to encode all of Unicode in a way that can seamlessly pass
through libraries that process C strings (i.e. an array of non-null
bytes terminated by a null byte). Byte values less than 128 are ASCII;
beyond ASCII, UTF-8 uses 2-4 bytes, and all byte values are greater
than 127, with standardized byte order. In contrast, UTF-16 and UTF-32
have null bytes in the string and platform-determined byte order. The
length and order of the optional byte order mark (BOM) distinguishes
UTF-16LE, UTF-16BE, UTF-32LE, and UTF-32BE. There's also a UTF-8 BOM
used on Windows. Python calls this encoding "utf-8-sig".

> fmt = endianness + str(len(hello)) + "s"

That's the wrong length. Use the length of the encoded string.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to struct.pack a unicode string?

2012-11-30 Thread eryksun
A clarification: in the default mode ('@'), struct uses native
alignment padding, but not if you override this with <, >, =, or !, as
you did.

>> fmt = endianness + str(len(hello)) + "s"
>
> That's the wrong length. Use the length of the encoded string.

Generally, however, you'd use a fixed size set by the struct
definition. For example:

typedef struct _point {
unsigned int x;
unsigned int y;
char label[8];
} point;


Python:

>>> struct.pack('II8s', *[1, 2, b'12345678This is ignored'])
b'\x01\x00\x00\x00\x02\x00\x00\x0012345678'

Null termination may or may not be required. Python will pad out the
rest of the string with nulls if it's less than the specified length:

>>> struct.pack('II8s', *[1, 2, b'1234'])
b'\x01\x00\x00\x00\x02\x00\x00\x001234\x00\x00\x00\x00'
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to struct.pack a unicode string?

2012-11-30 Thread Steven D'Aprano

On 01/12/12 12:28, eryksun wrote:


UTF-8 was
designed to encode all of Unicode in a way that can seamlessly pass
through libraries that process C strings (i.e. an array of non-null
bytes terminated by a null byte). Byte values less than 128 are ASCII;
beyond ASCII, UTF-8 uses 2-4 bytes, and all byte values are greater
than 127, with standardized byte order. In contrast, UTF-16 and UTF-32
have null bytes in the string and platform-determined byte order. The
length and order of the optional byte order mark (BOM) distinguishes
UTF-16LE, UTF-16BE, UTF-32LE, and UTF-32BE.


That's not quite right. The UTF-16BE and UTF-16LE character sets do
not take BOMs, because the encoding already specifies the byte order:

py> s = u'abçЙ'
py> s.encode('utf-16LE')
'a\x00b\x00\xe7\x00\x19\x04'
py> s.encode('utf-16BE')
'\x00a\x00b\x00\xe7\x04\x19'


In contrast, plain ol' UTF-16 with no BE or LE suffix is ambiguous without
a BOM, so it uses one:

py> s.encode('utf-16')
'\xff\xfea\x00b\x00\xe7\x00\x19\x04'


The same applies to UTF-32.



There's also a UTF-8 BOM used on Windows. Python calls this encoding
 "utf-8-sig".


UTF-8-sig, an abomination, but sadly not just a Microsoft abomination.
Google Docs also uses it.

Although the Unicode standard does allow using a BOM (not actually a
Byte Order Mark, more of a "UTF-8 signature"), doing so is annoying
and silly.



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