Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-08 Thread Rustom Mody
On Sunday, May 7, 2017 at 1:03:57 AM UTC+5:30, Rahim Shamsy wrote:
> Hi, 
> 
> Hope you are well. I am currently in the process of learning the basics of 
> programming in Python, and was just checking if I am in the right direction. 

The tutorial https://docs.python.org/3/tutorial/ is short and good.
Generally recommended

[You can adjust the version in left top corner]

> I have experience programming in C++ and Java, and want to learn Python for 
> research work that I am doing. The research is regarding application of 
> Machine Learning as a Data Analysis tool for application in the domain of 
> smart infrastructure. 

You will be fastest if you put aside ML and Analytics until you get through the 
tutorial.
Because python has the same core across a wide range of applications
And being comfortable with that core will reward you with less time wasted
later with minor confusions
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-07 Thread Chris Angelico
On Mon, May 8, 2017 at 2:34 AM, Dennis Lee Bieber  wrote:
> I can partly agree with one aspect... In comparison to my old life 
> with
> versions of FORTRAN, wherein standards were roughly 10 years apart, each
> standard tended to accept all of a previous standard, while declaring
> things which would likely not be preserved in the next standard. That meant
> one essentially had 10 years to port, say FORTRAN-IV/-66 fully into
> FORTRAN-77 -- and that port would still be viable in Fortran-90 (giving one
> ~10 years to remove port fully into F90).
>
> Python 2 to Python 3 broke too many things! Things that, in the 
> Fortran
> example, would have worked but with a warning (in documentation, and only
> in code if a compiler option for strictness were used), with the hard break
> being between Python 2 and Python "4".

If you think Py2 -> Py3 is a big compatibility break, just have a look
over at the JavaScript world. Python has been around for roughly 25
years, and in that time has reached its third major version; Node.js
has been around for seven years, and is in its seventh. Similarly, the
popular framework React has been around for four years, and is in its
fourth major version; and where I teach JS programming, we had to
quickly update our installation instructions saying "install this
particular version", because version 4 was not compatible with what we
were doing.

It's not that hard to write code that's compatible with both Py2 and
Py3 (say, with 2.7 and 3.5+). Most of it involves doing things the Py3
way though, so if you've become accustomed to working the Py2 way
(pretending that bytes and characters are the same thing, for
instance), you WILL need to change your thinking a bit. But Python 3.x
has been around for ten years now, so that's about on par with your
Fortran time scale; the incompatibilities do exist, but they aren't
nearly as big as you might think.

Python 4.0 will not need to make as much of a compatibility break even
than 3.0 did, and that's already far less breakage than other
ecosystems see.

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


Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-07 Thread breamoreboy
On Sunday, May 7, 2017 at 8:21:01 AM UTC+1, Shivangi Motwani wrote:
> Hey!
> 
> Learn Python The Hard Way is good

I cannot recommend LPTHW after the author had this 
https://learnpythonthehardway.org/book/nopython3.html to say late last year.  
The best of the rebuttals is here 
https://eev.ee/blog/2016/11/23/a-rebuttal-for-python-3/

Kindest regards.

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


Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-07 Thread breamoreboy
On Saturday, May 6, 2017 at 8:33:57 PM UTC+1, Rahim Shamsy wrote:
> Hi, 
> 
> Hope you are well. I am currently in the process of learning the basics of 
> programming in Python, and was just checking if I am in the right direction. 
> I have experience programming in C++ and Java, and want to learn Python for 
> research work that I am doing. The research is regarding application of 
> Machine Learning as a Data Analysis tool for application in the domain of 
> smart infrastructure. 
> 
> I have the following questions:
> 
> - I have been following this pdf for practice exercises (Learn Python The 
> Hard Way) and the following online tutorial for guidance: 
> 
> https://campus.datacamp.com/courses/intro-to-python-for-data-science/
> 
> Is there a better and faster way? I need more problem solving experience 
> (keeping in mind Data Science) with python.
> 
> - What resources can I use so that I can leverage my knowledge of Java (just 
> know it from class), so that I can learn Python fast?
> 
> Thanks for the help. 
> 
> Rahim

You may find these helpful 
http://dirtsimple.org/2004/12/python-is-not-java.html and 
http://dirtsimple.org/2004/12/java-is-not-python-either.html

Kindest regards.

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


Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-07 Thread Shivangi Motwani
Hey!

Learn Python The Hard Way is good, apart from that you can take Udacitie's
Intro to Data Analysis:
https://in.udacity.com/course/intro-to-data-analysis--ud170/

This will help you learn numpy and pandas which will be very useful.

Hope that helps!

On Sun, May 7, 2017 at 1:03 AM, Rahim Shamsy  wrote:

> Hi,
>
> Hope you are well. I am currently in the process of learning the basics of
> programming in Python, and was just checking if I am in the right
> direction. I have experience programming in C++ and Java, and want to learn
> Python for research work that I am doing. The research is regarding
> application of Machine Learning as a Data Analysis tool for application in
> the domain of smart infrastructure.
>
> I have the following questions:
>
> - I have been following this pdf for practice exercises (Learn Python The
> Hard Way) and the following online tutorial for guidance:
>
> https://campus.datacamp.com/courses/intro-to-python-for-data-science/
>
> Is there a better and faster way? I need more problem solving experience
> (keeping in mind Data Science) with python.
>
> - What resources can I use so that I can leverage my knowledge of Java
> (just know it from class), so that I can learn Python fast?
>
> Thanks for the help.
>
> Rahim
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-06 Thread Michael Torrie
On 05/06/2017 01:33 PM, Rahim Shamsy wrote:
> Is there a better and faster way? I need more problem solving
> experience (keeping in mind Data Science) with python.

Probably. Depends on the person and how one learns.

The best way to learn python is to have a problem you wish to solve and
start constructing a program in Python to solve it.  If I were you I
would check into the SciPy community, which specializes in using Python
in science and analysis.  Once you have a grasp of basic Python syntax,
how to do basic I/O, just start programming. In many ways, it really is
true: Python is as close to executable pseudo-code as you can get.

> - What resources can I use so that I can leverage my knowledge of
> Java (just know it from class), so that I can learn Python fast?

Frankly if you just have Java knowledge from a class it won't help you
in Python very much, other than give you a basic understanding of logic
(if statements, loops, etc).  A lot of the boilerplate stuff in
Java, such as static classes, static members, etc, aren't necessary in
Python and if you try to code Python in a way that looks like Java
you'll just end up fighting the language and not having much fun.  In my
opinion, just forget what you know about Java as you learn Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


I want to learn Python and how to benefit from the great Data Science packages - have some questions.

2017-05-06 Thread Rahim Shamsy
Hi, 

Hope you are well. I am currently in the process of learning the basics of 
programming in Python, and was just checking if I am in the right direction. I 
have experience programming in C++ and Java, and want to learn Python for 
research work that I am doing. The research is regarding application of Machine 
Learning as a Data Analysis tool for application in the domain of smart 
infrastructure. 

I have the following questions:

- I have been following this pdf for practice exercises (Learn Python The Hard 
Way) and the following online tutorial for guidance: 

https://campus.datacamp.com/courses/intro-to-python-for-data-science/

Is there a better and faster way? I need more problem solving experience 
(keeping in mind Data Science) with python.

- What resources can I use so that I can leverage my knowledge of Java (just 
know it from class), so that I can learn Python fast?

Thanks for the help. 

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


Re: Some questions about atexit

2012-09-13 Thread Roy Smith
In article ,
 Terry Reedy  wrote:

> On 9/12/2012 8:58 PM, Roy Smith wrote:
> > The atexit docs (http://docs.python.org/library/atexit.html) are very
> > confusing.  In one place they say, "The order in which the functions are
> > called is not defined".  In another place, "all functions registered are
> > called in last in, first out order".  Which is correct?
> 
> Check the tracker (bugs.python.org) for atexit issues, open and closed. 

Thanks for the pointer.  The operative one seems to be 
http://bugs.python.org/issue15233
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about atexit

2012-09-12 Thread Terry Reedy

On 9/12/2012 8:58 PM, Roy Smith wrote:

The atexit docs (http://docs.python.org/library/atexit.html) are very
confusing.  In one place they say, "The order in which the functions are
called is not defined".  In another place, "all functions registered are
called in last in, first out order".  Which is correct?


Check the tracker (bugs.python.org) for atexit issues, open and closed. 
I believe there was one about order and whether to make a guarantee. The 
doc might have been changed one place and not another. The newer 
statement is probably correct. In any case, the wrong place should be 
corrected.



Also, it's not clear how atexit handlers interact with threading.  Do
all handlers get called in the main thread?  What if some other thread
registers a handler?  Does it get called in that thread?


Don't know about this.

--
Terry Jan Reedy

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


Some questions about atexit

2012-09-12 Thread Roy Smith
The atexit docs (http://docs.python.org/library/atexit.html) are very 
confusing.  In one place they say, "The order in which the functions are 
called is not defined".  In another place, "all functions registered are 
called in last in, first out order".  Which is correct?

Also, it's not clear how atexit handlers interact with threading.  Do 
all handlers get called in the main thread?  What if some other thread 
registers a handler?  Does it get called in that thread?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions on pow and random

2011-03-27 Thread joy99
On Mar 27, 8:52 pm, Mark Dickinson  wrote:
> On Mar 27, 3:00 pm, joy99  wrote:
>
> > (i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
> > are writing in Python as,
> > variable1=2
> > variable2=3
> > result=pow(variable1,variable2)
>
> > In my first problem p(x) a list of float/decimals and f(x) is another
> > such.
> > Here,
> > variable1=p(x)
> > variable2=f(x)
> > so that we can write, pow(variable1,variable2) but as it is a list not
> > a number and as the size is huge, so would it pow support it?
>
> No:  pow won't work on lists.  It will work on (a) numbers (pow(2, 3) -> 8),
>
> or (b) numpy arrays, e.g.:
>
> >>> import numpy as np
> >>> x = np.array([0.1, 0.5, 0.4])
> >>> y = np.array([3, 24, 18])
> >>> pow(x, y)
>
> array([  1.e-03,   5.96046448e-08,   6.87194767e-08])>>> x ** y # 
> exactly equivalent
>
> array([  1.e-03,   5.96046448e-08,   6.87194767e-08])
>
> > (ii) The second question is, if I have another set of variables,
>
> > variable1=random.random()
> > variable2=random.random()
>
> In this case 'variable1' and 'variable2' are Python floats, so yes,
> you can multiply them directly.  (BTW, you can always experiment
> directly at the Python interactive prompt to answer this sort of
> question.)
>
> Mark

Thanks Mark. Wishing you a nice day ahead.
Best Regards,
Subhabrata.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions on pow and random

2011-03-27 Thread Mark Dickinson
On Mar 27, 3:00 pm, joy99  wrote:
> (i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
> are writing in Python as,
> variable1=2
> variable2=3
> result=pow(variable1,variable2)
>
> In my first problem p(x) a list of float/decimals and f(x) is another
> such.
> Here,
> variable1=p(x)
> variable2=f(x)
> so that we can write, pow(variable1,variable2) but as it is a list not
> a number and as the size is huge, so would it pow support it?

No:  pow won't work on lists.  It will work on (a) numbers (pow(2, 3) -
> 8),
or (b) numpy arrays, e.g.:

>>> import numpy as np
>>> x = np.array([0.1, 0.5, 0.4])
>>> y = np.array([3, 24, 18])
>>> pow(x, y)
array([  1.e-03,   5.96046448e-08,   6.87194767e-08])
>>> x ** y # exactly equivalent
array([  1.e-03,   5.96046448e-08,   6.87194767e-08])

> (ii) The second question is, if I have another set of variables,
>
> variable1=random.random()
> variable2=random.random()

In this case 'variable1' and 'variable2' are Python floats, so yes,
you can multiply them directly.  (BTW, you can always experiment
directly at the Python interactive prompt to answer this sort of
question.)

Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions on pow and random

2011-03-27 Thread joy99
On Mar 27, 4:36 pm, Mark Dickinson  wrote:
> On Mar 27, 11:07 am, joy99  wrote:
>
> > (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
> > the E of EM algorithm, without going into much technical details is,
> > P0(x1,x2), P1(x1,x2) 
>
> > Now I am taking random.random() to generate both x1 and x2 and trying
> > to multiply them, is it fine? Or should I take anything else?
>
> Sorry, it's unclear to me what you're asking here.  Can you rephrase
> this as a question about Python's random.random() function?
>
> If you're asking whether it's okay to regard your generated x1 and x2
> as independent, then the answer is yes.
>
> --
> Mark

Dear Mark,
Thank you for kindly allowing your time to put your kind suggestions.
I am trying to rephrase my questions, as you might have asked it.

(i) Suppose we have 8 which is 2^3 i.e., 3 is the power of 2, which we
are writing in Python as,
variable1=2
variable2=3
result=pow(variable1,variable2)

In my first problem p(x) a list of float/decimals and f(x) is another
such.
Here,
variable1=p(x)
variable2=f(x)
so that we can write, pow(variable1,variable2) but as it is a list not
a number and as the size is huge, so would it pow support it?
As I copied the question from word processor to the post, so there was
a slight confusion.

(ii) The second question is, if I have another set of variables,

variable1=random.random()
variable2=random.random()

Now my question is, can I do
result=variable1*variable2

Or should I do anything else?

Best Regards,
Subhabrata.






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


Re: Some questions on pow and random

2011-03-27 Thread Mark Dickinson
On Mar 27, 11:07 am, joy99  wrote:

> (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
> the E of EM algorithm, without going into much technical details is,
> P0(x1,x2), P1(x1,x2) 
>
> Now I am taking random.random() to generate both x1 and x2 and trying
> to multiply them, is it fine? Or should I take anything else?

Sorry, it's unclear to me what you're asking here.  Can you rephrase
this as a question about Python's random.random() function?

If you're asking whether it's okay to regard your generated x1 and x2
as independent, then the answer is yes.

--
Mark

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


Re: Some questions on pow and random

2011-03-27 Thread Mark Dickinson
On Mar 27, 11:07 am, joy99  wrote:
> (i) By standard definition of Likelihood Estimation, we get if x EURO X,
> where X is a countable set of types, p is probability and f is
> frequency.
> L(f;p)=Ðp(x)f(x)
>
> My question is python provides two functions,
> (a)     pow for power.
> (b)     reduce(mul, list)
>
> Now, how to combine them? If any one can suggest any help?

If I'm understanding your question correctly, it sounds as though
you've got a vector p = (p_1, p_2, ..., p_n) of probabilities and a
corresponding vector f = (f_1, f_2, ..., f_n) of integer frequencies,
and you want to compute the product of the quantities p_i ** f_i.  Is
that right?

Assuming that p and f are represented by Python lists, you might do
something like this:

>>> p = [0.1, 0.5, 0.4]
>>> f = [3, 24, 18]
>>> product = 1.0
>>> for pi, fi in zip(p, f):
... product *= pi**fi
...
>>> product
4.0960005e-18

I wouldn't particularly recommend using 'reduce' for this, since it
doesn't lead to readable code, but if you must you can do:

>>> import operator
>>> reduce(operator.mul, map(pow, p, f), 1)
4.0960005e-18

or:

>>> reduce(operator.mul, [pi**fi for pi, fi in zip(p, f)], 1)
4.0960005e-18

> As p(x)f(x), would be huge would pow support it?

You'll run into problems with underflow to zero fairly quickly.  The
usual solution is to work with log likelihood instead of likelihood
itself, in which case you'd want a sum instead:

>>> sum(fi*log(pi) for pi, fi in zip(p, f))
-40.03652078615561

If you really need the likelihood itself, you might try using the
Decimal module, which allows a much wider exponent range than Python's
builtin float.

--
Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions on pow and random

2011-03-27 Thread Jason Swails
2011/3/27 joy99 

> Dear Group,
>
> I have two questions one related to pow() and other is related to
> random.
> My questions are as below:
>
> (i) By standard definition of Likelihood Estimation, we get if x EURO X,
> where X is a countable set of types, p is probability and f is
> frequency.
> L(f;p)=Πp(x)f(x)
>
> My question is python provides two functions,
> (a) pow for power.
> (b) reduce(mul, list)
>
> Now, how to combine them? If any one can suggest any help?
> As p(x)f(x), would be huge would pow support it?
>

Not quite sure what you mean by "combine".  p(x)f(x) implies multiplying to
me, but "combine" suggests you mean something like p(f(x)) or the like.  In
any case, as long as the returned result of one of the functions is a valid
argument for the other, you can use the second approach.  And as long as the
results of both functions can be multiplied together (i.e. that operation is
defined), you can do that as well.

At most, pow would be limited by the floating point number in python.
sys.float_info gives me the following:

>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)

So if the numbers may be bigger than 1.8e+308, then you'll need to find an
alternative way of doing this.  May I suggest recasting the problem using
only logs if possible (since that will increase the value of the digits that
can be used up to 10 ^ (1.8 E 308).  You can of course always back out the
full value afterwards.


> (b) Suppose we have two distributions p(x1) and p(x2), of the Model M,
> the E of EM algorithm, without going into much technical details is,
> P0(x1,x2), P1(x1,x2) 
>
> Now I am taking random.random() to generate both x1 and x2 and trying
> to multiply them, is it fine? Or should I take anything else?
>
>
I see no reason why you can't multiply them...  I'm not exactly sure what
you're trying to get here, though.

--Jason
-- 
http://mail.python.org/mailman/listinfo/python-list


A wrap for a multi-tabbed terminal. Some questions

2010-12-29 Thread Steve
http://pastie.org/763792/wrap

This in IMHO, a really useful piece of code, to wrap and run terminal
commands, on a gtk+vte python based gui.

I would to make some improvements, in order to wrap some terminal
applications.

How can SET_FOCUS (at start) to the first vte frame? (Avoiding to
click on terminal to get focus)

How can fork a command thru the menus?

thank you,

Steve,
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about PyOpenGL and wxPython

2008-09-21 Thread Mike C. Fletcher

Clay Hobbs wrote:

I am making a program with wxPython that renders objects in 3D using
PyOpenGL, and I am having some problems.  For one thing, I forgot how to
make a double-buffered hardware surface.

http://bazaar.launchpad.net/~mcfletch/openglcontext/trunk/annotate/1?file_id=wxcontext.py-20080920224554-ehwlv3u6uc6sb6e2-55

See method wxFlagsFromDefinition, particularly glcanvas.WX_GL_DOUBLEBUFFER

  For another thing,
glColor(1.0, 0.0, 0.0) just before the rendering doesn't make the object
red.  Please help, I'm a total noob to OpenGL.
  
Would have to see what you're doing to know what's wrong there.  glColor 
should be working.


HTH,
Mike

--

 Mike C. Fletcher
 Designer, VR Plumber, Coder
 http://www.vrplumber.com
 http://blog.vrplumber.com

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


Some questions about PyOpenGL and wxPython

2008-09-21 Thread Clay Hobbs
I am making a program with wxPython that renders objects in 3D using
PyOpenGL, and I am having some problems.  For one thing, I forgot how to
make a double-buffered hardware surface.  For another thing,
glColor(1.0, 0.0, 0.0) just before the rendering doesn't make the object
red.  Please help, I'm a total noob to OpenGL.

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


Re: some questions about ejabberd,spark and psi

2008-02-24 Thread Mr Shore
the url missed
http://allforces.com/2005/05/06/ichat-to-msn-through-jabber/
On 2月25日, 上午12时01分, Mr Shore <[EMAIL PROTECTED]> wrote:
> hi,every buddy
> I've now installed a jabber server,right it's ejabberd mentioned above
> and 2 jabber client,right again it's spark and psi
> and I can connect to jabber server by spark
> but failed by psi,saying 'certificate failed the authenticity
> test;Reason:certificate is self signed'
> and i follow this page to try to transport to MSN but once again
> failed
> the jabber server provided is obsolete so could anyone provide a valid
> jabber server?
> the third next is about the transport to MSN from ejabberd
> i'm running windowx XP os,with python vertion 2.5
> I downloaded the pyMSNT for windows but only to find it supports only
> vertion 2.2
> so is there any other way to transport to MSN besides pyMSNT?
> any advice will be appreciated:)

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

some questions about ejabberd,spark and psi

2008-02-24 Thread Mr Shore
hi,every buddy
I've now installed a jabber server,right it's ejabberd mentioned above
and 2 jabber client,right again it's spark and psi
and I can connect to jabber server by spark
but failed by psi,saying 'certificate failed the authenticity
test;Reason:certificate is self signed'
and i follow this page to try to transport to MSN but once again
failed
the jabber server provided is obsolete so could anyone provide a valid
jabber server?
the third next is about the transport to MSN from ejabberd
i'm running windowx XP os,with python vertion 2.5
I downloaded the pyMSNT for windows but only to find it supports only
vertion 2.2
so is there any other way to transport to MSN besides pyMSNT?
any advice will be appreciated:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-28 Thread glacier
On Jan 28, 2:31 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 28, 2:53 pm, glacier <[EMAIL PROTECTED]> wrote:
>
>
>
> > Thanks,John.
> > It's no doubt that you proved SAX didn't support GBK encoding.
> > But can you give some suggestion on how to make SAX parse some GBK
> > string?
>
> Yes, the same suggestion as was given to you by others very early in
> this thread, the same as I demonstrated in the middle of proving that
> SAX doesn't support a GBK-encoded input file.
>
> Suggestion: Recode your input from GBK to UTF-8. Ensure that the XML
> declaration doesn't have an unsupported encoding. Your handler will
> get data encoded as UTF-8. Recode that to GBK if needed.
>
> Here's a cut down version of the previous script, focussed on
> demonstrating that the recoding strategy works.
>
> C:\junk>type gbksax2.py
> import xml.sax, xml.sax.saxutils
> import cStringIO
> unistr = u''.join(unichr(0x4E00+i) + unichr(ord('W')+i) for i in
> range(4))
> gbkstr = unistr.encode('gbk')
> print 'This is a GBK-encoded string: %r' % gbkstr
> utf8str = gbkstr.decode('gbk').encode('utf8')
> print 'Now recoded as UTF-8 to be fed to a SAX parser: %r' % utf8str
> xml_template = """%s data>"""
> utf8doc = xml_template % ('utf-8', unistr.encode('utf8'))
> f = cStringIO.StringIO()
> handler = xml.sax.saxutils.XMLGenerator(f, encoding='utf8')
> xml.sax.parseString(utf8doc, handler)
> result = f.getvalue()
> f.close()
> start = result.find('') + 6
> end = result.find('')
> mydata = result[start:end]
> print "SAX output (UTF-8): %r" % mydata
> print "SAX output recoded to GBK: %r" %
> mydata.decode('utf8').encode('gbk')
>
> C:\junk>gbksax2.py
> This is a GBK-encoded string: '[EMAIL PROTECTED]'
> Now recoded as UTF-8 to be fed to a SAX parser: '\xe4\xb8\x80W
> \xe4\xb8\x81X\xe4\xb8\x82Y\xe4\xb8\x83Z'
> SAX output (UTF-8): '\xe4\xb8\x80W\xe4\xb8\x81X\xe4\xb8\x82Y
> \xe4\xb8\x83Z'
> SAX output recoded to GBK: '[EMAIL PROTECTED]'
>
> HTH,
> John

Thanks a lot John:)
I'll try it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-27 Thread John Machin
On Jan 28, 2:53 pm, glacier <[EMAIL PROTECTED]> wrote:
>
> Thanks,John.
> It's no doubt that you proved SAX didn't support GBK encoding.
> But can you give some suggestion on how to make SAX parse some GBK
> string?

Yes, the same suggestion as was given to you by others very early in
this thread, the same as I demonstrated in the middle of proving that
SAX doesn't support a GBK-encoded input file.

Suggestion: Recode your input from GBK to UTF-8. Ensure that the XML
declaration doesn't have an unsupported encoding. Your handler will
get data encoded as UTF-8. Recode that to GBK if needed.

Here's a cut down version of the previous script, focussed on
demonstrating that the recoding strategy works.

C:\junk>type gbksax2.py
import xml.sax, xml.sax.saxutils
import cStringIO
unistr = u''.join(unichr(0x4E00+i) + unichr(ord('W')+i) for i in
range(4))
gbkstr = unistr.encode('gbk')
print 'This is a GBK-encoded string: %r' % gbkstr
utf8str = gbkstr.decode('gbk').encode('utf8')
print 'Now recoded as UTF-8 to be fed to a SAX parser: %r' % utf8str
xml_template = """%s"""
utf8doc = xml_template % ('utf-8', unistr.encode('utf8'))
f = cStringIO.StringIO()
handler = xml.sax.saxutils.XMLGenerator(f, encoding='utf8')
xml.sax.parseString(utf8doc, handler)
result = f.getvalue()
f.close()
start = result.find('') + 6
end = result.find('')
mydata = result[start:end]
print "SAX output (UTF-8): %r" % mydata
print "SAX output recoded to GBK: %r" %
mydata.decode('utf8').encode('gbk')

C:\junk>gbksax2.py
This is a GBK-encoded string: '[EMAIL PROTECTED]'
Now recoded as UTF-8 to be fed to a SAX parser: '\xe4\xb8\x80W
\xe4\xb8\x81X\xe4\xb8\x82Y\xe4\xb8\x83Z'
SAX output (UTF-8): '\xe4\xb8\x80W\xe4\xb8\x81X\xe4\xb8\x82Y
\xe4\xb8\x83Z'
SAX output recoded to GBK: '[EMAIL PROTECTED]'

HTH,
John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月28日, 上午5时50分, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 28, 7:47 am, "Mark Tolonen" <[EMAIL PROTECTED]>
> wrote:
>
>
>
>
>
> > >"John Machin" <[EMAIL PROTECTED]> wrote in message
> > >news:[EMAIL PROTECTED]
> > >On Jan 27, 9:17 pm, glacier <[EMAIL PROTECTED]> wrote:
> > >> On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > >> wrote:
>
> > >*IF* the file is well-formed GBK, then the codec will not mess up when
> > >decoding it to Unicode. The usual cause of mess is a combination of a
> > >human and a text editor :-)
>
> > SAX uses the expat parser.  From the pyexpat module docs:
>
> > Expat doesn't support as many encodings as Python does, and its repertoire
> > of encodings can't be extended; it supports UTF-8, UTF-16, ISO-8859-1
> > (Latin1), and ASCII. If encoding is given it will override the implicit or
> > explicit encoding of the document.
>
> > --Mark
>
> Thank you for pointing out where that list of encodings had been
> cunningly concealed. However the relevance of dropping it in as an
> apparent response to my answer to the OP's question about decoding
> possibly butchered GBK strings is  what?
>
> In any case, it seems to support other 8-bit encodings e.g. iso-8859-2
> and koi8-r ...
>
> C:\junk>type gbksax.py
> import xml.sax, xml.sax.saxutils
> import cStringIO
>
> unistr = u''.join(unichr(0x4E00+i) + unichr(ord('W')+i) for i in
> range(4))
> print 'unistr=%r' % unistr
> gbkstr = unistr.encode('gbk')
> print 'gbkstr=%r' % gbkstr
> unistr2 = gbkstr.decode('gbk')
> assert unistr2 == unistr
>
> print "latin1 FF -> utf8 = %r" %
> '\xff'.decode('iso-8859-1').encode('utf8')
> print "latin2 FF -> utf8 = %r" %
> '\xff'.decode('iso-8859-2').encode('utf8')
> print "koi8r FF -> utf8 = %r" % '\xff'.decode('koi8-r').encode('utf8')
>
> xml_template = """%s data>"""
>
> asciidoc = xml_template % ('ascii', 'The quick brown fox etc')
> utf8doc = xml_template % ('utf-8', unistr.encode('utf8'))
> latin1doc = xml_template % ('iso-8859-1', 'nil illegitimati
> carborundum' + '\xff')
> latin2doc = xml_template % ('iso-8859-2', 'duo secundus' + '\xff')
> koi8rdoc = xml_template % ('koi8-r', 'Moskva' + '\xff')
> gbkdoc = xml_template % ('gbk', gbkstr)
>
> for doc in (asciidoc, utf8doc, latin1doc, latin2doc, koi8rdoc,
> gbkdoc):
> f = cStringIO.StringIO()
> handler = xml.sax.saxutils.XMLGenerator(f, encoding='utf8')
> xml.sax.parseString(doc, handler)
> result = f.getvalue()
> f.close
> print repr(result[result.find(''):])
>
> C:\junk>gbksax.py
> unistr=u'\u4e00W\u4e01X\u4e02Y\u4e03Z'
> gbkstr='[EMAIL PROTECTED]'
> latin1 FF -> utf8 = '\xc3\xbf'
> latin2 FF -> utf8 = '\xcb\x99'
> koi8r FF -> utf8 = '\xd0\xaa'
> 'The quick brown fox etc'
> '\xe4\xb8\x80W\xe4\xb8\x81X\xe4\xb8\x82Y\xe4\xb8\x83Z'
> 'nil illegitimati carborundum\xc3\xbf'
> 'duo secundus\xcb\x99'
> 'Moskva\xd0\xaa'
> Traceback (most recent call last):
>   File "C:\junk\gbksax.py", line 27, in 
> xml.sax.parseString(doc, handler)
>   File "C:\Python25\lib\xml\sax\__init__.py", line 49, in parseString
> parser.parse(inpsrc)
>   File "C:\Python25\lib\xml\sax\expatreader.py", line 107, in parse
> xmlreader.IncrementalParser.parse(self, source)
>   File "C:\Python25\lib\xml\sax\xmlreader.py", line 123, in parse
> self.feed(buffer)
>   File "C:\Python25\lib\xml\sax\expatreader.py", line 211, in feed
> self._err_handler.fatalError(exc)
>   File "C:\Python25\lib\xml\sax\handler.py", line 38, in fatalError
> raise exception
> xml.sax._exceptions.SAXParseException: :1:30: unknown
> encoding
>
> C:\junk>- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Thanks,John.
It's no doubt that you proved SAX didn't support GBK encoding.
But can you give some suggestion on how to make SAX parse some GBK
string?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread John Machin
On Jan 28, 7:47 am, "Mark Tolonen" <[EMAIL PROTECTED]>
wrote:
> >"John Machin" <[EMAIL PROTECTED]> wrote in message
> >news:[EMAIL PROTECTED]
> >On Jan 27, 9:17 pm, glacier <[EMAIL PROTECTED]> wrote:
> >> On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]>
> >> wrote:
>
> >*IF* the file is well-formed GBK, then the codec will not mess up when
> >decoding it to Unicode. The usual cause of mess is a combination of a
> >human and a text editor :-)
>
> SAX uses the expat parser.  From the pyexpat module docs:
>
> Expat doesn't support as many encodings as Python does, and its repertoire
> of encodings can't be extended; it supports UTF-8, UTF-16, ISO-8859-1
> (Latin1), and ASCII. If encoding is given it will override the implicit or
> explicit encoding of the document.
>
> --Mark

Thank you for pointing out where that list of encodings had been
cunningly concealed. However the relevance of dropping it in as an
apparent response to my answer to the OP's question about decoding
possibly butchered GBK strings is  what?

In any case, it seems to support other 8-bit encodings e.g. iso-8859-2
and koi8-r ...

C:\junk>type gbksax.py
import xml.sax, xml.sax.saxutils
import cStringIO

unistr = u''.join(unichr(0x4E00+i) + unichr(ord('W')+i) for i in
range(4))
print 'unistr=%r' % unistr
gbkstr = unistr.encode('gbk')
print 'gbkstr=%r' % gbkstr
unistr2 = gbkstr.decode('gbk')
assert unistr2 == unistr

print "latin1 FF -> utf8 = %r" %
'\xff'.decode('iso-8859-1').encode('utf8')
print "latin2 FF -> utf8 = %r" %
'\xff'.decode('iso-8859-2').encode('utf8')
print "koi8r FF -> utf8 = %r" % '\xff'.decode('koi8-r').encode('utf8')

xml_template = """%s"""

asciidoc = xml_template % ('ascii', 'The quick brown fox etc')
utf8doc = xml_template % ('utf-8', unistr.encode('utf8'))
latin1doc = xml_template % ('iso-8859-1', 'nil illegitimati
carborundum' + '\xff')
latin2doc = xml_template % ('iso-8859-2', 'duo secundus' + '\xff')
koi8rdoc = xml_template % ('koi8-r', 'Moskva' + '\xff')
gbkdoc = xml_template % ('gbk', gbkstr)

for doc in (asciidoc, utf8doc, latin1doc, latin2doc, koi8rdoc,
gbkdoc):
f = cStringIO.StringIO()
handler = xml.sax.saxutils.XMLGenerator(f, encoding='utf8')
xml.sax.parseString(doc, handler)
result = f.getvalue()
f.close
print repr(result[result.find(''):])

C:\junk>gbksax.py
unistr=u'\u4e00W\u4e01X\u4e02Y\u4e03Z'
gbkstr='[EMAIL PROTECTED]'
latin1 FF -> utf8 = '\xc3\xbf'
latin2 FF -> utf8 = '\xcb\x99'
koi8r FF -> utf8 = '\xd0\xaa'
'The quick brown fox etc'
'\xe4\xb8\x80W\xe4\xb8\x81X\xe4\xb8\x82Y\xe4\xb8\x83Z'
'nil illegitimati carborundum\xc3\xbf'
'duo secundus\xcb\x99'
'Moskva\xd0\xaa'
Traceback (most recent call last):
  File "C:\junk\gbksax.py", line 27, in 
xml.sax.parseString(doc, handler)
  File "C:\Python25\lib\xml\sax\__init__.py", line 49, in parseString
parser.parse(inpsrc)
  File "C:\Python25\lib\xml\sax\expatreader.py", line 107, in parse
xmlreader.IncrementalParser.parse(self, source)
  File "C:\Python25\lib\xml\sax\xmlreader.py", line 123, in parse
self.feed(buffer)
  File "C:\Python25\lib\xml\sax\expatreader.py", line 211, in feed
self._err_handler.fatalError(exc)
  File "C:\Python25\lib\xml\sax\handler.py", line 38, in fatalError
raise exception
xml.sax._exceptions.SAXParseException: :1:30: unknown
encoding

C:\junk>
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread Mark Tolonen

>"John Machin" <[EMAIL PROTECTED]> wrote in message 
>news:[EMAIL PROTECTED]
>On Jan 27, 9:17 pm, glacier <[EMAIL PROTECTED]> wrote:
>> On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]> 
>> wrote:
>
>*IF* the file is well-formed GBK, then the codec will not mess up when
>decoding it to Unicode. The usual cause of mess is a combination of a
>human and a text editor :-)

SAX uses the expat parser.  From the pyexpat module docs:

Expat doesn't support as many encodings as Python does, and its repertoire 
of encodings can't be extended; it supports UTF-8, UTF-16, ISO-8859-1 
(Latin1), and ASCII. If encoding is given it will override the implicit or 
explicit encoding of the document.

--Mark 

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

Re: Some questions about decode/encode

2008-01-27 Thread Martin v. Löwis
>> Is there any way to solve this better?
>> I mean if I shouldn't convert the GBK string to unicode string, what
>> should I do to make SAX work?
> 
> Decode it and then encode it to utf-8 before feeding it to the parser.

The tricky part is that you also need to change the encoding declaration
in doing so, but in this case, it should be fairly simple:

unicode_doc = original_doc.decode("gbk")
unicode_doc = unicode_doc.replace('gbk','utf-8',1)
utf8_doc = unicode_doc.encode("utf-8")

This assumes that the string "gbk" occurs in the encoding declaration
as



If the encoding name has a different spelling (e.g. GBK), you need to
cater for that as well. You might want to try replacing the entire
XML declaration (i.e. everything between ), or just the
encoding= parameter. Notice that the encoding declaration may include
' instead of ", and may have additional spaces, e.g.



HTH,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月27日, 下午7时20分, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 27, 9:17 pm, glacier <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
> > > En Thu, 24 Jan 2008 04:52:22 -0200, glacier <[EMAIL PROTECTED]> escribió:
>
> > > > According to your reply, what will happen if I try to decode a long
> > > > string seperately.
> > > > I mean:
> > > > ##
> > > > a='你好吗'*10
> > > > s1 = u''
> > > > cur = 0
> > > > while cur < len(a):
> > > > d = min(len(a)-i,1023)
> > > > s1 += a[cur:cur+d].decode('mbcs')
> > > > cur += d
> > > > ##
>
> > > > May the code above produce any bogus characters in s1?
>
> > > Don't do that. You might be splitting the input string at a point that is
> > > not a character boundary. You won't get bogus output, decode will raise a
> > > UnicodeDecodeError instead.
> > > You can control how errors are handled, see  
> > > http://docs.python.org/lib/string-methods.html#l2h-237
>
> > > --
> > > Gabriel Genellina
>
> > Thanks Gabriel,
>
> > I guess I understand what will happen if I didn't split the string at
> > the character's boundry.
> > I'm not sure if the decode method will miss split the boundry.
> > Can you tell me then ?
>
> > Thanks a lot.
>
> *IF* the file is well-formed GBK, then the codec will not mess up when
> decoding it to Unicode. The usual cause of mess is a combination of a
> human and a text editor :-)- 隐藏被引用文字 -
>
> - 显示引用的文字 -

I guess firstly, I should check if the file I used to test is well-
formed GBK..:)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月27日, 下午7时04分, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 27, 9:18 pm, glacier <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On 1月24日, 下午4时44分, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
> > > On Wed, 23 Jan 2008 19:49:01 -0800, glacier wrote:
> > > > My second question is: is there any one who has tested very long mbcs
> > > > decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> > > > to be very strange and cause SAX fail to parse the decoded string.
>
> > > That's because SAX wants bytes, not a decoded string.  Don't decode it
> > > yourself.
>
> > > > However, I use another text editor to convert the file to utf-8 and
> > > > SAX will parse the content successfully.
>
> > > Because now you feed SAX with bytes instead of a unicode string.
>
> > > Ciao,
> > > Marc 'BlackJack' Rintsch
>
> > Yepp. I feed SAX with the unicode string since SAX didn't support my
> > encoding system(GBK).
>
> Let's go back to the beginning. What is "SAX"? Show us exactly what
> command or code you used.
>
SAX is the package 'xml.sax' distributed with Python 2.5:)
1,I read text from a GBK encoded XML file then I skip the first line
declare the encoding.
2,I converted the string to uncode by call decode('mbcs')
3,I used xml.sax.parseString to parse the string.


f = file('e:/temp/456.xml','rb')
s = f.read()
f.close()
n = 0
for i in xrange(len(s)):
if s[i]=='\n':
n += 1
if n == 1:
s = s[i+1:]
break
s = ''+s+''
s = s.decode('mbcs')
xml.sax.parseString(s,handler,handler)



> How did you let this SAX know that the file was encoded in GBK? An
> argument to SAX? An encoding declaration in the first few lines of the
> file? Some other method? ... precise answer please. Or did you expect
> that this SAX would guess correctly what the encoding was without
> being told?
I didn't tell the SAX the file is encoded in GBK since I used the
'parseString' method.
>
> What does "didn't support my encoding system" mean? Have you actually
> tried pushing raw undecoded GBK at SAX using a suitable documented
> method of telling SAX that the file is in fact encoded in GBK? If so,
> what was the error message that you got?
I mean SAX only support a limited number of encoding such as utf-8
utf-16 etc.,which didn't include GBK.

>
> How do you know that it's GBK, anyway? Have you considered these
> possible scenarios:
> (1) It's GBK but you are telling SAX that it's GB2312
> (2) It's GB18030 but you are telling SAX it's GBK
>
Frankly speaking, I cannot tell if the file contains any GB18030
characters...^__^
> HTH,
> John- 隐藏被引用文字 -
>
> - 显示引用的文字 -

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

Re: Some questions about decode/encode

2008-01-27 Thread John Machin
On Jan 27, 9:17 pm, glacier <[EMAIL PROTECTED]> wrote:
> On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
>
>
>
> > En Thu, 24 Jan 2008 04:52:22 -0200, glacier <[EMAIL PROTECTED]> escribió:
>
> > > According to your reply, what will happen if I try to decode a long
> > > string seperately.
> > > I mean:
> > > ##
> > > a='你好吗'*10
> > > s1 = u''
> > > cur = 0
> > > while cur < len(a):
> > > d = min(len(a)-i,1023)
> > > s1 += a[cur:cur+d].decode('mbcs')
> > > cur += d
> > > ##
>
> > > May the code above produce any bogus characters in s1?
>
> > Don't do that. You might be splitting the input string at a point that is
> > not a character boundary. You won't get bogus output, decode will raise a
> > UnicodeDecodeError instead.
> > You can control how errors are handled, see  
> > http://docs.python.org/lib/string-methods.html#l2h-237
>
> > --
> > Gabriel Genellina
>
> Thanks Gabriel,
>
> I guess I understand what will happen if I didn't split the string at
> the character's boundry.
> I'm not sure if the decode method will miss split the boundry.
> Can you tell me then ?
>
> Thanks a lot.

*IF* the file is well-formed GBK, then the codec will not mess up when
decoding it to Unicode. The usual cause of mess is a combination of a
human and a text editor :-)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread John Machin
On Jan 27, 9:18 pm, glacier <[EMAIL PROTECTED]> wrote:
> On 1月24日, 下午4时44分, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
>
> > On Wed, 23 Jan 2008 19:49:01 -0800, glacier wrote:
> > > My second question is: is there any one who has tested very long mbcs
> > > decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> > > to be very strange and cause SAX fail to parse the decoded string.
>
> > That's because SAX wants bytes, not a decoded string.  Don't decode it
> > yourself.
>
> > > However, I use another text editor to convert the file to utf-8 and
> > > SAX will parse the content successfully.
>
> > Because now you feed SAX with bytes instead of a unicode string.
>
> > Ciao,
> > Marc 'BlackJack' Rintsch
>
> Yepp. I feed SAX with the unicode string since SAX didn't support my
> encoding system(GBK).

Let's go back to the beginning. What is "SAX"? Show us exactly what
command or code you used.

How did you let this SAX know that the file was encoded in GBK? An
argument to SAX? An encoding declaration in the first few lines of the
file? Some other method? ... precise answer please. Or did you expect
that this SAX would guess correctly what the encoding was without
being told?

What does "didn't support my encoding system" mean? Have you actually
tried pushing raw undecoded GBK at SAX using a suitable documented
method of telling SAX that the file is in fact encoded in GBK? If so,
what was the error message that you got?

How do you know that it's GBK, anyway? Have you considered these
possible scenarios:
(1) It's GBK but you are telling SAX that it's GB2312
(2) It's GB18030 but you are telling SAX it's GBK

HTH,
John
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread Marc 'BlackJack' Rintsch
On Sun, 27 Jan 2008 02:18:48 -0800, glacier wrote:

> Yepp. I feed SAX with the unicode string since SAX didn't support my
> encoding system(GBK).

If the `decode()` method supports it, IMHO SAX should too.

> Is there any way to solve this better?
> I mean if I shouldn't convert the GBK string to unicode string, what
> should I do to make SAX work?

Decode it and then encode it to utf-8 before feeding it to the parser.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月24日, 下午5时51分, John Machin <[EMAIL PROTECTED]> wrote:
> On Jan 24, 2:49 pm, glacier <[EMAIL PROTECTED]> wrote:
>
> > I use chinese charactors as an example here.
>
> > >>>s1='你好吗'
> > >>>repr(s1)
>
> > "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
>
> > >>>b1=s1.decode('GBK')
>
> > My first question is : what strategy does 'decode' use to tell the way
> > to seperate the words. I mean since s1 is an multi-bytes-char string,
> > how did it determine to seperate the string every 2bytes or 1byte?
>
> The usual strategy for encodings like GBK is:
> 1. If the current byte is less than 0x80, then it's a 1-byte
> character.
> 2. Current byte 0x81 to 0xFE inclusive: current byte and the next byte
> make up a two-byte character.
> 3. Current byte 0x80: undefined (or used e.g. in cp936 for the 1-byte
> euro character)
> 4: Current byte 0xFF: undefined
>
> Cheers,
> John

Thanks John, I will try to write a function to test if the strategy
above caused the problem I described in the 1st post:)

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

Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月24日, 下午4时44分, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 23 Jan 2008 19:49:01 -0800, glacier wrote:
> > My second question is: is there any one who has tested very long mbcs
> > decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> > to be very strange and cause SAX fail to parse the decoded string.
>
> That's because SAX wants bytes, not a decoded string.  Don't decode it
> yourself.
>
> > However, I use another text editor to convert the file to utf-8 and
> > SAX will parse the content successfully.
>
> Because now you feed SAX with bytes instead of a unicode string.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Yepp. I feed SAX with the unicode string since SAX didn't support my
encoding system(GBK).

Is there any way to solve this better?
I mean if I shouldn't convert the GBK string to unicode string, what
should I do to make SAX work?

Thanks , Marc.
:)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-27 Thread glacier
On 1月24日, 下午3时29分, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Thu, 24 Jan 2008 04:52:22 -0200, glacier <[EMAIL PROTECTED]> escribió:
>
> > According to your reply, what will happen if I try to decode a long
> > string seperately.
> > I mean:
> > ##
> > a='你好吗'*10
> > s1 = u''
> > cur = 0
> > while cur < len(a):
> > d = min(len(a)-i,1023)
> > s1 += a[cur:cur+d].decode('mbcs')
> > cur += d
> > ##
>
> > May the code above produce any bogus characters in s1?
>
> Don't do that. You might be splitting the input string at a point that is  
> not a character boundary. You won't get bogus output, decode will raise a  
> UnicodeDecodeError instead.
> You can control how errors are handled, see  
> http://docs.python.org/lib/string-methods.html#l2h-237
>
> --
> Gabriel Genellina

Thanks Gabriel,

I guess I understand what will happen if I didn't split the string at
the character's boundry.
I'm not sure if the decode method will miss split the boundry.
Can you tell me then ?

Thanks a lot.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-24 Thread 7stud
On Jan 24, 1:44 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 23 Jan 2008 19:49:01 -0800, glacier wrote:
> > My second question is: is there any one who has tested very long mbcs
> > decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> > to be very strange and cause SAX fail to parse the decoded string.
>
> That's because SAX wants bytes, not a decoded string.  Don't decode it
> yourself.
>

encode() converts a unicode string to a regular string.  decode()
converts a regular string to a unicode string.  So I think what Marc
is saying is that SAX needs a regular string(i.e. bytes) not a decoded
string(i.e. a unicode string).




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


Re: Some questions about decode/encode

2008-01-24 Thread John Machin
On Jan 24, 2:49 pm, glacier <[EMAIL PROTECTED]> wrote:
> I use chinese charactors as an example here.
>
> >>>s1='你好吗'
> >>>repr(s1)
>
> "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
>
> >>>b1=s1.decode('GBK')
>
> My first question is : what strategy does 'decode' use to tell the way
> to seperate the words. I mean since s1 is an multi-bytes-char string,
> how did it determine to seperate the string every 2bytes or 1byte?
>

The usual strategy for encodings like GBK is:
1. If the current byte is less than 0x80, then it's a 1-byte
character.
2. Current byte 0x81 to 0xFE inclusive: current byte and the next byte
make up a two-byte character.
3. Current byte 0x80: undefined (or used e.g. in cp936 for the 1-byte
euro character)
4: Current byte 0xFF: undefined

Cheers,
John

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

Re: Some questions about decode/encode

2008-01-24 Thread Marc 'BlackJack' Rintsch
On Wed, 23 Jan 2008 19:49:01 -0800, glacier wrote:

> My second question is: is there any one who has tested very long mbcs
> decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> to be very strange and cause SAX fail to parse the decoded string.

That's because SAX wants bytes, not a decoded string.  Don't decode it
yourself.

> However, I use another text editor to convert the file to utf-8 and
> SAX will parse the content successfully.

Because now you feed SAX with bytes instead of a unicode string.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions about decode/encode

2008-01-23 Thread glacier
On 1月24日, 下午1时49分, [EMAIL PROTECTED] wrote:
> On Jan 23, 8:49 pm, glacier <[EMAIL PROTECTED]> wrote:
>
> > I use chinese charactors as an example here.
>
> > >>>s1='你好吗'
> > >>>repr(s1)
>
> > "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
>
> > >>>b1=s1.decode('GBK')
>
> > My first question is : what strategy does 'decode' use to tell the way
> > to seperate the words.
>
> decode() uses the GBK strategy you specified to determine what
> constitutes a character in your string.
>
> > My second question is: is there any one who has tested very long mbcs
> > decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> > to be very strange and cause SAX fail to parse the decoded string.
> > However, I use another text editor to convert the file to utf-8 and
> > SAX will parse the content successfully.
>
> > I'm not sure if some special byte array or too long text caused this
> > problem. Or maybe thats a BUG of python 2.5?
>
> That's probably to vague of a description to determine why SAX isn't
> doing what you expect it to.

You mean to post a copy of the XML document?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-23 Thread Gabriel Genellina
En Thu, 24 Jan 2008 04:52:22 -0200, glacier <[EMAIL PROTECTED]> escribió:

> According to your reply, what will happen if I try to decode a long
> string seperately.
> I mean:
> ##
> a='你好吗'*10
> s1 = u''
> cur = 0
> while cur < len(a):
> d = min(len(a)-i,1023)
> s1 += a[cur:cur+d].decode('mbcs')
> cur += d
> ##
>
> May the code above produce any bogus characters in s1?

Don't do that. You might be splitting the input string at a point that is  
not a character boundary. You won't get bogus output, decode will raise a  
UnicodeDecodeError instead.
You can control how errors are handled, see  
http://docs.python.org/lib/string-methods.html#l2h-237

-- 
Gabriel Genellina

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

Re: Some questions about decode/encode

2008-01-23 Thread glacier
On 1月24日, 下午1时41分, Ben Finney <[EMAIL PROTECTED]>
wrote:
> Ben Finney <[EMAIL PROTECTED]> writes:
> > glacier <[EMAIL PROTECTED]> writes:
>
> > > I use chinese charactors as an example here.
>
> > > >>>s1='你好吗'
> > > >>>repr(s1)
> > > "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
> > > >>>b1=s1.decode('GBK')
>
> > > My first question is : what strategy does 'decode' use to tell the
> > > way to seperate the words. I mean since s1 is an multi-bytes-char
> > > string, how did it determine to seperate the string every 2bytes
> > > or 1byte?
>
> > The codec you specified ("GBK") is, like any character-encoding
> > codec, a precise mapping between characters and bytes. It's almost
> > certainly not aware of "words", only character-to-byte mappings.
>
> To be clear, I should point out that I didn't mean to imply static
> tabular mappings only. The mappings in a character encoding are often
> more complex and algorithmic.
>
> That doesn't make them any less precise, of course; and the core point
> is that a character-mapping codec is *only* about getting between
> characters and bytes, nothing else.
>
> --
>  \ "He who laughs last, thinks slowest."  -- Anonymous |
>   `\   |
> _o__)  |
> Ben Finney- 隐藏被引用文字 -
>
> - 显示引用的文字 -

thanks for your respoonse:)

When I mentioned 'word' in the previous post, I mean character.
According to your reply, what will happen if I try to decode a long
string seperately.
I mean:
##
a='你好吗'*10
s1 = u''
cur = 0
while cur < len(a):
d = min(len(a)-i,1023)
s1 += a[cur:cur+d].decode('mbcs')
cur += d
##

May the code above produce any bogus characters in s1?


Thanks :)




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

Re: Some questions about decode/encode

2008-01-23 Thread bbtestingbb
On Jan 23, 8:49 pm, glacier <[EMAIL PROTECTED]> wrote:
> I use chinese charactors as an example here.
>
> >>>s1='你好吗'
> >>>repr(s1)
>
> "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
>
> >>>b1=s1.decode('GBK')
>
> My first question is : what strategy does 'decode' use to tell the way
> to seperate the words.

decode() uses the GBK strategy you specified to determine what
constitutes a character in your string.

> My second question is: is there any one who has tested very long mbcs
> decode? I tried to decode a long(20+MB) xml yesterday, which turns out
> to be very strange and cause SAX fail to parse the decoded string.
> However, I use another text editor to convert the file to utf-8 and
> SAX will parse the content successfully.
>
> I'm not sure if some special byte array or too long text caused this
> problem. Or maybe thats a BUG of python 2.5?

That's probably to vague of a description to determine why SAX isn't
doing what you expect it to.

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

Re: Some questions about decode/encode

2008-01-23 Thread Ben Finney
Ben Finney <[EMAIL PROTECTED]> writes:

> glacier <[EMAIL PROTECTED]> writes:
> 
> > I use chinese charactors as an example here.
> > 
> > >>>s1='你好吗'
> > >>>repr(s1)
> > "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
> > >>>b1=s1.decode('GBK')
> > 
> > My first question is : what strategy does 'decode' use to tell the
> > way to seperate the words. I mean since s1 is an multi-bytes-char
> > string, how did it determine to seperate the string every 2bytes
> > or 1byte?
> 
> The codec you specified ("GBK") is, like any character-encoding
> codec, a precise mapping between characters and bytes. It's almost
> certainly not aware of "words", only character-to-byte mappings.

To be clear, I should point out that I didn't mean to imply static
tabular mappings only. The mappings in a character encoding are often
more complex and algorithmic.

That doesn't make them any less precise, of course; and the core point
is that a character-mapping codec is *only* about getting between
characters and bytes, nothing else.

-- 
 \ "He who laughs last, thinks slowest."  -- Anonymous |
  `\   |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Some questions about decode/encode

2008-01-23 Thread Ben Finney
glacier <[EMAIL PROTECTED]> writes:

> I use chinese charactors as an example here.
> 
> >>>s1='你好吗'
> >>>repr(s1)
> "'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
> >>>b1=s1.decode('GBK')
> 
> My first question is : what strategy does 'decode' use to tell the way
> to seperate the words. I mean since s1 is an multi-bytes-char string,
> how did it determine to seperate the string every 2bytes or 1byte?

The codec you specified ("GBK") is, like any character-encoding codec,
a precise mapping between characters and bytes. It's almost certainly
not aware of "words", only character-to-byte mappings.

-- 
 \   "When I get new information, I change my position. What, sir, |
  `\  do you do with new information?"  -- John Maynard Keynes |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Some questions about decode/encode

2008-01-23 Thread glacier
I use chinese charactors as an example here.

>>>s1='你好吗'
>>>repr(s1)
"'\\xc4\\xe3\\xba\\xc3\\xc2\\xf0'"
>>>b1=s1.decode('GBK')

My first question is : what strategy does 'decode' use to tell the way
to seperate the words. I mean since s1 is an multi-bytes-char string,
how did it determine to seperate the string every 2bytes or 1byte?


My second question is: is there any one who has tested very long mbcs
decode? I tried to decode a long(20+MB) xml yesterday, which turns out
to be very strange and cause SAX fail to parse the decoded string.
However, I use another text editor to convert the file to utf-8 and
SAX will parse the content successfully.

I'm not sure if some special byte array or too long text caused this
problem. Or maybe thats a BUG of python 2.5?

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

Re: some questions about Python and tkinter

2007-10-18 Thread fabdeb
On Oct 16, 9:17 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Tue, 16 Oct 2007 11:52:22 -0700, fabdeb wrote:
> > the first: what is the differences between a function and a classe?
>
> A class bundles data and functions into one object.
>
> > In which case i should use a function ?
> > In which case i should use a class ?
>
> If you have several functions that operate on the same data it might make
> sense to put all into a class to treat them as one "unit".
>
> > The second: there is some pincipals gui toolkit: tkinter , Python Mega-
> > Widgets, PyGTK, PyQt, FxPy, WxPy
>
> > what are the advantages of each one, and in which case i use each of
> > them?
>
> `Tkinter` is part of the standard library.  If you use that or one of the
> others is a matter of taste to some degree.  In a GNOME environment PyGTK
> may look more natural, under KDE a PyQt or PyKDE based GUI may feel more
> "native".  Another factor for a decision might be the license of the GUI
> toolkit.
>
> Ciao,
> Marc 'BlackJack' Rintsch

Thanks for all your links and responses .
I have a better comprehension of what i have to do for my program.

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


Re: some questions about Python and tkinter

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 11:52:22 -0700, fabdeb wrote:

> the first: what is the differences between a function and a classe?

A class bundles data and functions into one object.

> In which case i should use a function ?
> In which case i should use a class ?

If you have several functions that operate on the same data it might make
sense to put all into a class to treat them as one "unit".

> The second: there is some pincipals gui toolkit: tkinter , Python Mega-
> Widgets, PyGTK, PyQt, FxPy, WxPy
> 
> what are the advantages of each one, and in which case i use each of
> them?

`Tkinter` is part of the standard library.  If you use that or one of the
others is a matter of taste to some degree.  In a GNOME environment PyGTK
may look more natural, under KDE a PyQt or PyKDE based GUI may feel more
"native".  Another factor for a decision might be the license of the GUI
toolkit.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: some questions about Python and tkinter

2007-10-16 Thread kyosohma
On Oct 16, 1:52 pm, fabdeb <[EMAIL PROTECTED]> wrote:
> Hi every one,
> I m a sysadmin who want to know how to use python.
> I dont know anything about oriented object programation, i only know
> bash and a little perl.
> I have some simple questions about python.
>
> the first: what is the differences between a function and a classe?
>
> In which case i should use a function ?
> In which case i should use a class ?
>
> The second: there is some pincipals gui toolkit: tkinter , Python Mega-
> Widgets, PyGTK, PyQt, FxPy, WxPy
>
> what are the advantages of each one, and in which case i use each of
> them?
>
> thanks by advance,
>
> Fabrice.witkowski

For functions see:

http://docs.python.org/ref/function.html
http://www.penzilla.net/tutorials/python/functions/

For classes see:

http://docs.python.org/tut/node11.html
http://www.diveintopython.org/object_oriented_framework/defining_classes.html

As for GUI toolkits, that is a contentious subject. The two major ones
usually mentioned here are Tkinter and wxPython. Others argue for
PyGTK and pyQT. I recommend to see the following links:

http://wiki.wxpython.org/Choosing_wxPython_over_Tkinter
http://wiki.python.org/moin/GuiProgramming

Tkinter and wxPython are the ones I've used and recommend, although
currently I use wxPython the most. Both are very much cross-platform,
although wxPython may have the edge when it comes to native look on
most platforms and it has more built-in widgets than Tkinter. You
should check them both out and see what you think.

As far as I know, they can be used for pretty much any GUI interface,
so I don't think you have to worry about use cases.

Mike

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


Re: some questions about Python and tkinter

2007-10-16 Thread Bruno Desthuilliers
fabdeb a écrit :
> Hi every one,
> I m a sysadmin who want to know how to use python.
> I dont know anything about oriented object programation, i only know
> bash and a little perl.
> I have some simple questions about python.
> 
> the first: what is the differences between a function and a classe?

I suppose you do know what a function is. Else it would be time to go 
for some CS introductory material...

A class is the definition of a family of objects. If that doesn't mean 
anything to you, then just forget about classes and OOP for now.

> In which case i should use a function ?

When you have a (mostly self-contained) piece of code you want to call 
by name from different places.

> In which case i should use a class ?

If you don't know, then don't use them for the moment. First learn to 
use Python without them.

> The second: there is some pincipals gui toolkit: tkinter , Python Mega-
> Widgets, PyGTK, PyQt, FxPy, WxPy
> 
> what are the advantages of each one, and in which case i use each of
> them?

This is a FAQ IIRC.
-- 
http://mail.python.org/mailman/listinfo/python-list


some questions about Python and tkinter

2007-10-16 Thread fabdeb
Hi every one,
I m a sysadmin who want to know how to use python.
I dont know anything about oriented object programation, i only know
bash and a little perl.
I have some simple questions about python.

the first: what is the differences between a function and a classe?

In which case i should use a function ?
In which case i should use a class ?

The second: there is some pincipals gui toolkit: tkinter , Python Mega-
Widgets, PyGTK, PyQt, FxPy, WxPy

what are the advantages of each one, and in which case i use each of
them?

thanks by advance,

Fabrice.witkowski

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


Some questions about ipath error messages

2007-06-06 Thread Mitko Haralanov
Hi Dave,

I have some questions about the ipath error messages that we print out.
I am currently testing the error injection part of the matrix and I
starting with "incorrect checksum" and "incorrect HW version". I have
set the debug level on both machines to 0x83 and I am monitoring
ipathstats.

In the case of "incorrect checksum" I get an "ipathhdrerr" printed on
the console and when running "incorrect HW version" test, I get
"invalid ipathhdr"

On previous release, the error messages that we used to print were a
bit more descriptive of the problem (I remember that, at least for the
checksum part, it did mention a checksum error, and for HW version, it
did mention HW version problem).

Am I using the correct debug level? Did the error messages change on
purpose?

-- 
Mitko Haralanov  [EMAIL PROTECTED]
Senior Software Engineer 650.934.8064
System Interconnect Group   http://www.qlogic.com

==
 Professor: I've been a Harold Zoid fan since back when my 
   hips were made of bone. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Starting Python... some questions

2007-03-13 Thread jezzzz .
All,

thank you for your responses. I learned much and made some modifications in my 
small program. Currently I am attempting to put together a packet that contains 
a Python message (created using struct.pack) and a Scapy message (Ether()). 
Having a packet with combined payload from Python and Scapy doesn't seem to 
work the way I do it: 
ether = scapy.Ether(dst="01:02:03:04:05:06")
payload = ether + payload (where payload is the chassis_id)

I get as error:
  File "lldp.py", line 23, in main
fullpayload = ether + payload
TypeError: unsupported operand type(s) for +: 'Ether' and 'str'

I'm thinking instead to just use Python to create the Ethernet Frame as well 
and then send the packet off. How difficult is this? I could only find 
references online on how to send TCP/UDP packets but no information on how to 
send a packet with a Layer1 and Layer2 only. The ethernet frame should only 
contain a destination MAC, a source MAC and a type (88cc). Any advice on how to 
create such a packet and send it? (if anyone knows of an easy way to use a 
hybrid of Python and Scapy, that's fine by me as well).

Thanks!


class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(self, subtype, chassis_info):
if subtype == 4:
chassis_data_int = [int(x,16) for x in chassis_info.split(":")] 
   
chassis_data = struct.pack("6B", *chassis_data_int)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

def main():
p = lldp_class()
p.chassis_id(4, "01:80:C2:00:00:0E")
payload = p.chassis_id_tlv
ether = scapy.Ether(dst="01:02:03:04:05:06")
fullpayload = ether + payload
sendp(fullpayload)

main()






 

Never miss an email again!
Yahoo! Toolbar alerts you the instant new Mail arrives.
http://tools.search.yahoo.com/toolbar/features/mail/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Starting Python... some questions

2007-03-13 Thread Bruno Desthuilliers
Steven D'Aprano a écrit :
> On Mon, 12 Mar 2007 21:39:11 -0700, jezonthenet wrote:
> 
>> I started using Python a couple of days ago - here are a few
>> questions:
>>
>> * Doesn't the __main__() method automatically execute when I run my
>> python program?
> 
> 
> No. 
> 
> 
>> * Only when I do an import of my test.py file within python and then
>> run test.__main__() I can see where my bugs are. Is this correct?
>> (right now this is my only way of running my python program and see
>> where I have problems)
> 
> That's a good way of choosing to run __main__ manually, but there are
> other ways.
> 
> If you add a line to the end of your program:
> 
> __main__()
> 
> 
> the function will be executed whenever the program runs. That is both when
> you import it, and when you run it from the command line.
> 
> To ensure it doesn't run when you import, do this:
> 
> 
> if __name__ == "__main__":
> __main__()
> 
> 
> 
>> * Once I've done an import and then I wish to make a change to the
>> file I've imported I have to quit Python, restart and import that
>> module again in order for the module to be refreshed. Is there no "re-
>> import" ?
> 
> Yes there is. Do this:
> 
> import my_module
> # do things
> # edit the file
> # don't forget to save changes (that always catches me out)
> reload(my_module)
> 
> 
>> * Finally, could someone tell me why I'm having problems with the
>> small module below?
>>   - Python pretends I provide chassis_id() with three parameters, even
>> though I clearly only provide it with two - why?
>>
>> Thanks!
>>
>> #!/usr/bin/python
>> import scapy
>> import struct
>>
>> class lldp_class:
>>  def __init__(self):
>>  self.chassis_id_tlv = None
>>
>>  def chassis_id(subtype, chassis_info):
>>  if subtype == 4:
>>  chassis_data = struct.pack("!B",chassis_info)
>>  subtype_data = struct.pack("!B",subtype)
>>  self.chassis_id_tlv = subtype_data + chassis_data
> 
> Class methods

s/Class/Instance/

classmethods are methods that takes the class as first parameter.

> always take a first parameter that points to the
> class instance. 

(snip)

> Just remember to always put "self" as the first argument to a class
> method, 

idem.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Starting Python... some questions

2007-03-13 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> I started using Python a couple of days ago - here are a few
> questions:
> 
> * Doesn't the __main__() method automatically execute when I run my
> python program?

Which "__main__" method ???

Anyway, the answer is no. Every code at the top-level (which includes 
import, class and def statements...) is executed when the module is 
first loaded - whether as a proper module (ie: imported from somewhere 
else) or as a main program (ie-> python mymodule.py). In the first case, 
the special variable __name__ is set to the module's name, in the second 
to '__main__'. So you can rely on this to have code executed when the 
module is used as a program. The usual idiom is

# mymodule.py
(imports here)
(classes and defs here)

def main(argv):
   (code of main function here)
   return 0

if __name__ == '__main__':
   import sys
   sys.exit(main(sys.argv))


> * Only when I do an import of my test.py file within python and then
> run test.__main__() 

BTW, you should *not* use names with 2 leadings and 2 trailing 
underscores. These names are reserved for Python implementation stuff.

> I can see where my bugs are. Is this correct?

Nope. You can also spot bugs by reading the code or running it thru the 
debugger !-)

More seriously : using the above idiom (or any variant of - the 
important part being the conditional on __name__ == '__main__'), you can 
just
$ python mymodule.py

to run your program.

> (right now this is my only way of running my python program and see
> where I have problems)

> * Once I've done an import and then I wish to make a change to the
> file I've imported I have to quit Python, restart and import that
> module again in order for the module to be refreshed. Is there no "re-
> import" ?

reload(module_object)

But it's of very limited use. The best thing to do is usually to have a 
simple test file that setup the desired state (imports etc) that you 
execute after each change, passing the -i option to the python 
interpreter (this will leave the interpreter in interactive mode after 
execution of the test file, so you can inspect your objects, test things 
etc).

> * Finally, could someone tell me why I'm having problems with the
> small module below?
>   - Python pretends I provide chassis_id() with three parameters, even
> though I clearly only provide it with two - why?

Without even reading the code, I can tell you it's an instance or 
classmethod with either a wrong declaration or wrongly called.

> 
> #!/usr/bin/python
> import scapy
> import struct
> 
> class lldp_class:

Do yourself a favor: use new-style classes. Also, it would be better to 
stick to usual naming conventions (Python relies heavily on conventions):
http://www.python.org/dev/peps/pep-0008/

   class Lldp(object):

>   def __init__(self):
>   self.chassis_id_tlv = None
> 
>   def chassis_id(subtype, chassis_info):

Bingo. You need to have self as the first argument. The instance is 
passed as the first argument of a method.

 def chassis_id(self, subtype, chassis_info):

>   if subtype == 4:
>   chassis_data = struct.pack("!B",chassis_info)
>   subtype_data = struct.pack("!B",subtype)
>   self.chassis_id_tlv = subtype_data + chassis_data
> 
> def __main__():


   def main():

>   p = lldp_class()
>   p.chassis_id(4, "01:80:C2:00:00:0E")

For the record: this is interpreted as:
 lldp_class.chassis_id(p, 4, "01:80:C2:00:00:0E")


>   payload = p.chassis_id_tlv
>   ether = scapy.Ether(dst="01:02:03:04:05:06")
>   fullpayload = ether + payload
>   sendp(fullpayload)
> 

if __name__ == '__main__':
 main()

As a side note, it looks like there are a couple point where your design 
may be improved. Like passing subtype and chassis_info to the __init__ 
of your class.

HTH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Starting Python... some questions

2007-03-12 Thread Gabriel Genellina
En Tue, 13 Mar 2007 03:13:37 -0300, je . <[EMAIL PROTECTED]>  
escribió:

> I added the self parameter to the the chassis_id function, however I now  
> have a problem and question regarding encoding MAC addresses for a  
> struct. What string format am I supposed to use?

py> data = [int(x,16) for x in "2A:3B:01:9C:04:F0".split(":")]
py> print data
[42, 59, 1, 156, 4, 240]
py> struct.pack("6B", *data)
'*;\x01\x9c\x04\xf0'

That is: take the string "2A:3B:01:9C:04:F0", split it in parts at each  
":", iterate over them converting from hex to integer; the resulting list  
is called data.
Then, pack the numbers using "6B" format.

> Also, I think I will get rid of Scapy in my small program. Any  
> references I can use to easily send (no need to receive) a packet with a  
> Layer 1 and 2 only? (i.e. only Ethernet and LLDP - no need for IP, UDP,  
> TCP etc. I wish to create something like Ethernet | LLDP msg1 | LLDP  
> msg2 | ... | LLDP msgn)

I don't think you can do that with Python alone - that is, without using  
some other specific tool like scapy.

> class lldp_class:

Usually classes have UppercaseWords, and omit the word "class".
But since LLDP would be all uppercase, maybe another name would be better.
You appear to be storing just a few attributes, not implementing that  
protocol, so maybe LLDPInfo might be used.

> def chassis_id(self, subtype, chassis_info):
>
> if subtype == 4:
>
> chassis_data = struct.pack("!B",chassis_info)
>
> subtype_data = struct.pack("!B",subtype)
>
> self.chassis_id_tlv = subtype_data + chassis_data

What should happen when subtipe is not 4? (Currently you get an error).

-- 
Gabriel Genellina

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


Re: Starting Python... some questions

2007-03-12 Thread Tim Roberts
You only need three things here:

[EMAIL PROTECTED] wrote:
>
>#!/usr/bin/python
>import scapy
>import struct
>
>class lldp_class:
>   def __init__(self):
>   self.chassis_id_tlv = None
>
>   def chassis_id(subtype, chassis_info):

Make that
def chassis_id(self, subtype, chassis_info):

When you call "p.chassis_id", "p" is passed as the first parameter to
chassis_id.  By convention, we call that "self", in the same way the object
of a C++ method is called "this" (although that is enforced, rather than
convention).

>   if subtype == 4:
>   chassis_data = struct.pack("!B",chassis_info)
>   subtype_data = struct.pack("!B",subtype)
>   self.chassis_id_tlv = subtype_data + chassis_data
>
>def __main__():

Make that
  def main():

The underscores have a special meaning that you do not need here.  Note
that the name "main" is not required; some people call it something like
"test".

>   p = lldp_class()
>   p.chassis_id(4, "01:80:C2:00:00:0E")
>   payload = p.chassis_id_tlv
>   ether = scapy.Ether(dst="01:02:03:04:05:06")
>   fullpayload = ether + payload
>   sendp(fullpayload)

Now, at the end, add:

if __name__=="__main__":
main()

That should do it.
-- 
Tim Roberts, [EMAIL PROTECTED]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Starting Python... some questions

2007-03-12 Thread jezzzz .
Jordan, Thinker, Steven,





Thanks for your responses. Makes more sense now! I can run my program from the 
console.



I added the self parameter to the the chassis_id function, however I now have a 
problem and question regarding encoding MAC addresses for a struct. What string 
format am I supposed to use?



Also, I think I will get rid of Scapy in my small program. Any references I can 
use to easily send (no need to receive) a packet with a Layer 1 and 2 only? 
(i.e. only Ethernet and LLDP - no need for IP, UDP, TCP etc. I wish to create 
something like Ethernet | LLDP msg1 | LLDP msg2 | ... | LLDP msgn)

Thanks!


This is what I got now:



#!/usr/bin/python

import scapy

import struct



class lldp_class:

def __init__(self):

self.chassis_id_tlv = None



def chassis_id(self, subtype, chassis_info):

if subtype == 4:

chassis_data = struct.pack("!B",chassis_info)

subtype_data = struct.pack("!B",subtype)

self.chassis_id_tlv = subtype_data + chassis_data



def __main__():

p = lldp_class()

p.chassis_id(4, "01:80:C2:00:00:0E")

payload = p.chassis_id_tlv

ether = scapy.Ether(dst="01:02:03:04:05:06")

fullpayload = ether + payload

sendp(fullpayload)



__main__()









 

Expecting? Get great news right away with email Auto-Check. 
Try the Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_tools.html 
-- 
http://mail.python.org/mailman/listinfo/python-list


Program __main__ function (was: Starting Python... some questions)

2007-03-12 Thread Ben Finney
[EMAIL PROTECTED], "Greenberg <"@bag.python.org, 
"greenbergj\""@NOSPAM.xs4all.nl, [EMAIL PROTECTED], ">"@bag.python.org writes:

> [EMAIL PROTECTED] wrote:
> > * Doesn't the __main__() method automatically execute when I run
> > my python program?
>
> No, there is no special __main__ function, to the best of my
> knowledge.

Guido van Rossum has mused on the idiom of the 'main' function in modules:

http://www.artima.com/weblogs/viewpost.jsp?thread=4829>

My idiom for this is:

=
def __main__(argv=None):
""" Mainline code for this program """

from sys import argv as sys_argv
if argv is None:
argv = sys_argv

foo = Foo(argv) # Foo is the main thing I need
exitcode = None
try:
foo.do_the_main_thing()
except SystemExit, e:
exitcode = e.code

return exitcode

if __name__ == "__main__":
import sys
exitcode = __main__(argv=sys.argv)
sys.exit(exitcode)
=

The reason for the local 'sys' imports is that I can boilerplate this
code and not have to worry about whether the rest of the program
actually uses the 'sys' module.

The reason for parameterising 'argv', but defaulting it to the system
argv, is that I can unit-test the '__main__' function
programmatically, while still letting it pick up the real argv when
the program is run.

Same reason for catching SystemExit, and then going on to call
'sys.exit()' myself: it allows the __main__ function to be unit-tested
easily, while still letting me use 'sys.exit()' in my application code
whenever it makes sense.

Now, when I write unit tests for my program (i.e. a Python module
designed to be run as a command), it can still be imported safely into
my unit tests, and all the code gets covered by test cases except the
three-line stanza at the end.
Htag.pl 0.0.23 -  Simon Huggins <[EMAIL PROTECTED]>  Released under GPL
Copyright (C) 1999-2002 Project Purple. http://www.earth.li/projectpurple/

Got sigmatch == ^/home/bignose/\.sigs/news.*$

-- 
 \"The right to search for truth implies also a duty; one must |
  `\ not conceal any part of what one has recognized to be true."  |
_o__)   -- Albert Einstein |
Ben Finney

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


Re: Starting Python... some questions

2007-03-12 Thread Steven D'Aprano
On Mon, 12 Mar 2007 21:39:11 -0700, jezonthenet wrote:

> I started using Python a couple of days ago - here are a few
> questions:
> 
> * Doesn't the __main__() method automatically execute when I run my
> python program?


No. 


> * Only when I do an import of my test.py file within python and then
> run test.__main__() I can see where my bugs are. Is this correct?
> (right now this is my only way of running my python program and see
> where I have problems)

That's a good way of choosing to run __main__ manually, but there are
other ways.

If you add a line to the end of your program:

__main__()


the function will be executed whenever the program runs. That is both when
you import it, and when you run it from the command line.

To ensure it doesn't run when you import, do this:


if __name__ == "__main__":
__main__()



> * Once I've done an import and then I wish to make a change to the
> file I've imported I have to quit Python, restart and import that
> module again in order for the module to be refreshed. Is there no "re-
> import" ?

Yes there is. Do this:

import my_module
# do things
# edit the file
# don't forget to save changes (that always catches me out)
reload(my_module)


> * Finally, could someone tell me why I'm having problems with the
> small module below?
>   - Python pretends I provide chassis_id() with three parameters, even
> though I clearly only provide it with two - why?
> 
> Thanks!
> 
> #!/usr/bin/python
> import scapy
> import struct
> 
> class lldp_class:
>   def __init__(self):
>   self.chassis_id_tlv = None
> 
>   def chassis_id(subtype, chassis_info):
>   if subtype == 4:
>   chassis_data = struct.pack("!B",chassis_info)
>   subtype_data = struct.pack("!B",subtype)
>   self.chassis_id_tlv = subtype_data + chassis_data

Class methods always take a first parameter that points to the
class instance. By convention, it should be called "self". So you should
write this as:

def chassis_id(self, subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data


When you call the method:

my_instance = lldp_class()
my_instance.chassis_id(4, "01:80:C2:00:00:0E")

Python automatically fills in a reference to the instance and calls this:

lldp_class.chassis_id(my_instance, 4, "01:80:C2:00:00:0E")


Just remember to always put "self" as the first argument to a class
method, and you will (almost never) go wrong!



-- 
Steven D'Aprano 

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


Re: Starting Python... some questions

2007-03-12 Thread Thinker
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

[EMAIL PROTECTED] wrote:
> I started using Python a couple of days ago - here are a few
> questions:
>
> * Doesn't the __main__() method automatically execute when I run my
> python program?
> * Only when I do an import of my test.py file within python and then
> run test.__main__() I can see where my bugs are. Is this correct?
> (right now this is my only way of running my python program and see
> where I have problems)
> * Once I've done an import and then I wish to make a change to the
> file I've imported I have to quit Python, restart and import that
> module again in order for the module to be refreshed. Is there no "re-
> import" ?
> * Finally, could someone tell me why I'm having problems with the
> small module below?
> - Python pretends I provide chassis_id() with three parameters, even
> though I clearly only provide it with two - why?
No, Python is executing a module. You should explicit call __main__()
at top scope of
your module. For ex.

if __name__ == '__main__':
__main__()

This snippet calls __main__() when the module is executed directly,
but not imported as a module.

Once a module had been imported, you should invoke reload() function
to reload it from filesystem.


- --
Thinker Li - [EMAIL PROTECTED] [EMAIL PROTECTED]
http://heaven.branda.to/~thinker/GinGin_CGI.py
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.6 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF9i/j1LDUVnWfY8gRAgidAKCxHUWf/XTuT8fRQmeL3hnsO4fohQCgt27R
jCNoz4hgp2CxD7H5HPwkfgM=
=4g/m
-END PGP SIGNATURE-

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

Re: Starting Python... some questions

2007-03-12 Thread Greenberg
[EMAIL PROTECTED] wrote:
> I started using Python a couple of days ago - here are a few
> questions:
> 
> * Doesn't the __main__() method automatically execute when I run my
> python program?
> * Only when I do an import of my test.py file within python and then
> run test.__main__() I can see where my bugs are. Is this correct?
> (right now this is my only way of running my python program and see
> where I have problems)

No, there is no special __main__ function, to the best of my knowledge. 
I think the idiom you're thinking of is:

if __name__="__main__":
#Your main stuff here

But that won't execute if you do an import. Only when you call the 
script directly, like:

python myscript.py

for example.

> * Once I've done an import and then I wish to make a change to the
> file I've imported I have to quit Python, restart and import that
> module again in order for the module to be refreshed. Is there no "re-
> import" ?

There is. The reload command. Usage is similar to import:

import MyModule
#Do your edit, and reload thusly:
reload MyModule

> * Finally, could someone tell me why I'm having problems with the
> small module below?
>   - Python pretends I provide chassis_id() with three parameters, even
> though I clearly only provide it with two - why?
Python expects self as the first parameter. You might want to take 
another look at the class bits of your tutorial of choice, or:
http://docs.python.org/tut/node11.html

The function definition should look more like:

def chassis_id(self, subtype, chassis_info):

HTH.
-Jordan Greenberg
-- 
http://mail.python.org/mailman/listinfo/python-list


Starting Python... some questions

2007-03-12 Thread jezonthenet
I started using Python a couple of days ago - here are a few
questions:

* Doesn't the __main__() method automatically execute when I run my
python program?
* Only when I do an import of my test.py file within python and then
run test.__main__() I can see where my bugs are. Is this correct?
(right now this is my only way of running my python program and see
where I have problems)
* Once I've done an import and then I wish to make a change to the
file I've imported I have to quit Python, restart and import that
module again in order for the module to be refreshed. Is there no "re-
import" ?
* Finally, could someone tell me why I'm having problems with the
small module below?
  - Python pretends I provide chassis_id() with three parameters, even
though I clearly only provide it with two - why?

Thanks!

#!/usr/bin/python
import scapy
import struct

class lldp_class:
def __init__(self):
self.chassis_id_tlv = None

def chassis_id(subtype, chassis_info):
if subtype == 4:
chassis_data = struct.pack("!B",chassis_info)
subtype_data = struct.pack("!B",subtype)
self.chassis_id_tlv = subtype_data + chassis_data

def __main__():
p = lldp_class()
p.chassis_id(4, "01:80:C2:00:00:0E")
payload = p.chassis_id_tlv
ether = scapy.Ether(dst="01:02:03:04:05:06")
fullpayload = ether + payload
sendp(fullpayload)

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


Re: PyMedia - some questions

2006-12-06 Thread Kjell Magne Fauske
I'm not familiar with PyMedia, but this blog entry should be of
interest:

[Video Blogging using Django and Flash(tm) Video (FLV)]
   http://blog.go4teams.com/?p=56

It describes a toolchain for publishing AVI files as FLV on the web.
ffmpeg is used together with a few other tools, but Python is used to
glue it all togheter.

- Kjell Magne Fauske

Lad wrote:
> Hi,
> Can anyone answer the following questions?
>
> Question 1.
> Can pyMedia create/convert FLV format? I explain in details.
> As I would like
> to publish videos for viewing in a browser , I need a good video
> format.
> I learned that  FLV (Flash(tm) Video) format could be a good choice.
> Or does anybody suggest a better format???
>
>
> Question 2 .
> So, I need to convert a file captured from a video camera into that
> FLV (Flash(tm) Video) format .
> Can pyMedia do that or must I use ffmpeg directly
> like this( to convert from avi to FLV )
> ffmpeg -i [sourcefile.avi] -acodec mp3 -ar 22050 -ab 32 -f flv -s
> 320×240 [destfile.flv]
>
>
> Question 3,
>
> This command creates a simple FLV format file, containing the video and
> audio streams. In addition, FLV files need meta-information such as
> duration, frames, etc. FLV movie players use this information to
> calculate progress bar sliders and allow the user to fast-forward or
> reverse through the video.
> Can PyMedia add such meta- information?
> 
> 
> Thank you for help
> Lad.

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


PyMedia - some questions

2006-12-06 Thread Lad
Hi,
Can anyone answer the following questions?

Question 1.
Can pyMedia create/convert FLV format? I explain in details.
As I would like
to publish videos for viewing in a browser , I need a good video
format.
I learned that  FLV (Flash(tm) Video) format could be a good choice.
Or does anybody suggest a better format???


Question 2 .
So, I need to convert a file captured from a video camera into that
FLV (Flash(tm) Video) format .
Can pyMedia do that or must I use ffmpeg directly
like this( to convert from avi to FLV )
ffmpeg -i [sourcefile.avi] -acodec mp3 -ar 22050 -ab 32 -f flv -s
320×240 [destfile.flv]


Question 3,

This command creates a simple FLV format file, containing the video and
audio streams. In addition, FLV files need meta-information such as
duration, frames, etc. FLV movie players use this information to
calculate progress bar sliders and allow the user to fast-forward or
reverse through the video.
Can PyMedia add such meta- information?


Thank you for help
Lad.

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


Re: some questions on how to study the bittorent.

2006-07-08 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Moore Liu
wrote:

> Hi all,
> 
> I am very interesting in the detailed implement of bittorrent and have
> read some p2p protocol document, but I want to know more of the
> technique implementation. It's time-consuming to reverse-engineering
> the code to understand the design intention of it. So my question is:
> 
> Is there any well written "Design document" we can find, which descibe
> the detail technical in the code. For example: the seed format, the
> algorithm of how to divide the files into blocks, how to schdule
> threads, how to decide which part of the file should be receive first,
> the credit system(something like that in the eMule), and so on...

You can start with the `protocol description`_ on the BitTorrent website
and the `BitTorrent Economics Paper`_.

.. _protocol description: http://www.bittorrent.org/protocol.html
.. _BitTorrent Economics Paper:
   http://www.bittorrent.com/bittorrentecon.pdf

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


some questions on how to study the bittorent.

2006-07-08 Thread Moore Liu
Hi all,

I am very interesting in the detailed implement of bittorrent and have
read some p2p protocol document, but I want to know more of the
technique implementation. It's time-consuming to reverse-engineering
the code to understand the design intention of it. So my question is:

Is there any well written "Design document" we can find, which descibe
the detail technical in the code. For example: the seed format, the
algorithm of how to divide the files into blocks, how to schdule
threads, how to decide which part of the file should be receive first,
the credit system(something like that in the eMule), and so on...

Could you pls tell me the methology to study the code or show me some
link to these document or some specific forum to delve in?

Thanks,
Moore

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


Re: first post: new to pythong. some questions.

2005-12-08 Thread Simon Brunning
On 12/8/05, shawn a <[EMAIL PROTECTED]> wrote:
> Hello. Im brand new to this list and to python.  Ive recently started
> reading about it
>   and am now in the tinkering stage.

Welcome to Python!

>  I have a script im working on that i
> need some
>  asistance debugging. Its super small and should be a snap for you gurus =)
>
>  I have 2 files in a dir off my home dir:
>  mkoneurl.py
>  make_ou_class.py
>
>  --mkoneurl.py--
>  #! /usr/bin/env python
>
>  import make_ou_class
>
>  run = makeoneurl()

You need:

run = make_ou_class.makeoneurl()

See  for why.

(BTW, you would probably have got more of a response with a better
subject line.  is
worth a read.)

--
Cheers,
Simon B,
[EMAIL PROTECTED],
http://www.brunningonline.net/simon/blog/
-- 
http://mail.python.org/mailman/listinfo/python-list


first post: new to pythong. some questions.

2005-12-07 Thread shawn a
Hello. Im brand new to this list and to python.  Ive recently started reading about it
 and am now in the tinkering stage.  I have a script im working on that i need some
asistance debugging. Its super small and should be a snap for you gurus =)

I have 2 files in a dir off my home dir:
mkoneurl.py
make_ou_class.py

--mkoneurl.py--
#! /usr/bin/env python

import make_ou_class

run = makeoneurl()
-

--make_ou_class.py--
class makeoneurl:
    def __init__():
    self.commandline()

    def commandline():
    com = str(raw_input(":"))
   
#Parse out any params and aguements - reg expressions
    #params[] array to hold paramters
    params = 0
    if com == "ou":
   
self.ou(params)
    else:
   
print com + " unknown command."

    def ou(parameter):
    print "hello world"
    self.commandline():
---

Why i run mkoneurl.py by typing "python mkonurl.py" i  get the following error:
 Traceback (innermost last):
   File "mkoneurl.py", line 5, in ?
 run = makeoneurl()
 NameError: makeoneurl

am i missing something here? Any help would be greatly appreciated. Thank you

--Shawn




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

Re: Some questions

2005-08-20 Thread Jorgen Grahn
On Thu, 18 Aug 2005 08:53:56 GMT, Alessandro Bottoni <[EMAIL PROTECTED]> wrote:
> Titi Anggono wrote:
...
>> 2. I use gnuplot.py module for interfacing with
>> gnuplot in linux. Can we make the plot result shown in
>> web ? I tried using cgi, and it didn't work.
>
> The ability to display a image (in this case a GNUPlot plot) on a web page
> depends on the browser. Normally, you have to install a specific plug-in
> for displaying not-standard types of images on a web page, like it happens
> with Macromedia Flash.
>
> I do not know if exists any GNUPlot plug-in for the most common web
> browsers. Maybe you can save your plot in a format that is compatible with
> the existing viewers, like GIF, TIFF or JPEG. Have a look at GNUPlot
> documentation for this.

There is AFAIK no specific gnuplot image format, so saving to some other
format would have to be involved anyway. My version of Gnuplot includes
support for PNG, jpeg, encapsulated PostScript and SVG among other things[1].
PNG is probably the best overall choice.

For bonus points, one could generate the format the browser says it
prefers ...

/Jorgen
[1] "set terminal" at the gnuplot prompt lists them.

-- 
  // Jorgen GrahnR'lyeh wgah'nagl fhtagn!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions

2005-08-19 Thread Magnus Lycka
Thomas Ganss wrote:
> My blind guess would have been that Tkinter was *not* the GUI of choice
> for *J*ython. 

With Jython you'd probably use Swing or SWT. It's certainly less
coding to get something working in Jython/Swing than with Java/Swing,
but I suspect that there is a cost in runtime performance which
might be a problem in some cases.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions

2005-08-18 Thread Thomas Ganss
Cameron Laird schrieb:
> In article <[EMAIL PROTECTED]>,
> Alessandro Bottoni  <[EMAIL PROTECTED]> wrote:
> 
>>Titi Anggono wrote:
>>>1. Can we use Tkinter for web application such as Java?
...
> ... or, if you mean, "is Python an apt language for client-side
> Web development in the way Java is, with the market-leading 
> browsers all embedding JVMs which can interpret class definitions",
> the answers is, "No."  And also "Yes".  
> 
> Standard Python is *not* good for client-side Web work.  Jython is,
> though.
My blind guess would have been that Tkinter was *not* the GUI of choice
for *J*ython. A quick googling turned up only Tkinter for *JP*ython 1.1
- am I [status relative python/jython newbie] missing here something ?


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


Re: Some questions

2005-08-18 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
Alessandro Bottoni  <[EMAIL PROTECTED]> wrote:
>Titi Anggono wrote:
>> 1. Can we use Tkinter for web application such as Java?
>
>What do you mean? If you want to build up a GUI (something like a HTML page)
>for a web-based application, you can do it with TKinter. Your TKinter app
>can connect to a web server (or any other kind of server) using the
>standard Python networking libraries.
>
>If you want to make a web application (a server-side application similar to
>the ones you can create with JSP or EJB) you do not need Tkinter at all.
>Just use Python itself (see Albatross, Webware and Quixote for a few
>web-app frameworks for Python).
.
.
.
... or, if you mean, "is Python an apt language for client-side
Web development in the way Java is, with the market-leading 
browsers all embedding JVMs which can interpret class definitions",
the answers is, "No."  And also "Yes".  

Standard Python is *not* good for client-side Web work.  Jython is,
though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions

2005-08-18 Thread Alessandro Bottoni
Titi Anggono wrote:
> 1. Can we use Tkinter for web application such as Java?

What do you mean? If you want to build up a GUI (something like a HTML page)
for a web-based application, you can do it with TKinter. Your TKinter app
can connect to a web server (or any other kind of server) using the
standard Python networking libraries.

If you want to make a web application (a server-side application similar to
the ones you can create with JSP or EJB) you do not need Tkinter at all.
Just use Python itself (see Albatross, Webware and Quixote for a few
web-app frameworks for Python).

> 2. I use gnuplot.py module for interfacing with
> gnuplot in linux. Can we make the plot result shown in
> web ? I tried using cgi, and it didn't work.

The ability to display a image (in this case a GNUPlot plot) on a web page
depends on the browser. Normally, you have to install a specific plug-in
for displaying not-standard types of images on a web page, like it happens
with Macromedia Flash.

I do not know if exists any GNUPlot plug-in for the most common web
browsers. Maybe you can save your plot in a format that is compatible with
the existing viewers, like GIF, TIFF or JPEG. Have a look at GNUPlot
documentation for this.

HTH

---
Alessandro Bottoni
-- 
http://mail.python.org/mailman/listinfo/python-list


Some questions

2005-08-18 Thread Titi Anggono
Hi all,

I have some questions:

1. Can we use Tkinter for web application such as Java
?
2. I use gnuplot.py module for interfacing with
gnuplot in linux. Can we make the plot result shown in
web ? I tried using cgi, and it didn't work.

Thanks




Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Considering moving from Delphi to Python [Some questions]

2005-07-06 Thread malkarouri
Dark Cowherd wrote:
> Stupid of me.
>
> I want some feedback on folllwing:
> anybody who has experience in writing SOAP servers in Python and data
> entry heavy web applications.
> Any suggestions?
> darkcowherd

Check ZSI, or SOAPPY, both on Python Web Services
http://pywebsvcs.sourceforge.net/
I usually use ZSI. You cannot generate wsdl files from python code, so
if I were in your shoes I would write the Delphi side and generate the
wsdl file then use it in python.

I never tested interoperability of Delphi & ZSI by the way, though I
would love to. Please inform us if you do try it.

btw, elementtree has an attempt at elementsoap but i don't think they
went very far..

cheers,
k

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


Re: Considering moving from Delphi to Python [Some questions]

2005-07-05 Thread Benji York
Dark Cowherd wrote:
> I want some feedback on folllwing:
> anybody who has experience in writing [...] data
> entry heavy web applications.
> Any suggestions?

You might be interested in Zope 3's ability to generate data entry/edit 
forms via schemas.
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Considering moving from Delphi to Python [Some questions]

2005-07-05 Thread Harald Armin Massa
> I want some feedback on folllwing:
> anybody who has experience in writing SOAP servers in Python and data
> entry heavy web applications.
> Any suggestions?
> darkcowherd
>
I have never written SOAP Servers. But I have very very good experience
in creating entry heavy web application using Python and Quixote;
Webserver Medusa.

The current entry forms have at maximum 700 distinct entries, of which
groups can be multiplied via XMLHTTPRequest and DOM-Modifikation
without page reloading.
Speed is great. Because of very slow rendering and various buggies
within IE (beast took longer to render then me to deliver) we switched
to Firefox.

System is performing adequately well and is being constantly extended;
Pythons clear structure makes that easy.

Hope that helps,

Harald

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


Re: Considering moving from Delphi to Python [Some questions]

2005-07-04 Thread Dark Cowherd
Stupid of me. 

I want some feedback on folllwing:
anybody who has experience in writing SOAP servers in Python and data
entry heavy web applications.
Any suggestions?
darkcowherd

On 7/4/05, Dark Cowherd <[EMAIL PROTECTED]> wrote:
> Hi,
> 
> We program in Delphi in our shop and are generally very happy with it.
> We are now looking at cross-platform options especially for middle
> tier and web server.
> 
> I have been doing a lot of reading and testing of Python, I am falling
> in love with the language :-)
> 
> But seems to be be very difficult for what I am trying to do :-(
> 
> So looking for advise here
> We would like to work as following.
> 
> Front ends in Delphi.
> Middle tier in Python working as SOAP servers:
> Serious work on Twisted needed here ?elementree? are there any other
> options. Lots of learning to do. Doesnt seem to be simple way to do
> this.
> 
> Most of our applications are business applications so we basically we
> want to do a lot of data entry forms including entry in grid like
> interfaces etc. At this point i am looking at nevow with twisted.
> Again lot of learning to do here.
> 
> 
> darkcowherd
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Considering moving from Delphi to Python [Some questions]

2005-07-04 Thread Terry Reedy

"Dark Cowherd" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> We program in Delphi in our shop and are generally very happy with it.
> We are now looking at cross-platform options especially for middle
> tier and web server.
>
> I have been doing a lot of reading and testing of Python, I am falling
> in love with the language :-)

If you have not, Google 'Python Delphi'.  The second entry is Python (2.1+) 
for Delphi (6).

For what it is worth, the fifth is a Wiki entry :
"Python was the oracular serpent of Delphi. It was the offspring of Gaia 
and the mud that was left over after the flood of Deucalion"

Terry J. Reedy



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


Re: Considering moving from Delphi to Python [Some questions]

2005-07-04 Thread flamesrock
The curve may take a while, but I'd definately recommend it.

While you're at it, why not switch th front end to python too?

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


Considering moving from Delphi to Python [Some questions]

2005-07-04 Thread Dark Cowherd
Hi,

We program in Delphi in our shop and are generally very happy with it.
We are now looking at cross-platform options especially for middle
tier and web server.

I have been doing a lot of reading and testing of Python, I am falling
in love with the language :-)

But seems to be be very difficult for what I am trying to do :-(

So looking for advise here
We would like to work as following.

Front ends in Delphi.
Middle tier in Python working as SOAP servers:
Serious work on Twisted needed here ?elementree? are there any other
options. Lots of learning to do. Doesnt seem to be simple way to do
this.

Most of our applications are business applications so we basically we
want to do a lot of data entry forms including entry in grid like
interfaces etc. At this point i am looking at nevow with twisted.
Again lot of learning to do here.


darkcowherd
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Grant Edwards
On 2005-06-25, Jp Calderone <[EMAIL PROTECTED]> wrote:

>>[I've never figured out why one would do a shutdown RDWR rather
>>than close the connection, but I haven't put a lot of thought
>>into it.]
>
> shutdown actually tears down the TCP connection; close
> releases the file descriptor.
>
> If there is only one file descriptor referring to the TCP
> connection, these are more or less the same.  If there is more
> than one file descriptor, though, the difference should be
> apparent.

Ah yes.  I hadn't thought of that.  I think the only time I had
ever used shutdown was to shutdown just the transmit half.

-- 
Grant Edwards   grante Yow!  An INK-LING? Sure --
  at   TAKE one!! Did you BUY any
   visi.comCOMMUNIST UNIFORMS??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread pwilkins
On Fri, 24 Jun 2005 21:42:48 -0400, Jp Calderone wrote:


> shutdown actually tears down the TCP connection; close releases the file
> descriptor.
> 
> If there is only one file descriptor referring to the TCP connection,
> these are more or less the same.  If there is more than one file
> descriptor, though, the difference should be apparent.
> 
> Jp


Yes I think you are right. I checked with the "Socket Programming HOWTO"
by Gordon McMillan @ http://www.python.org/doc/howto.

Here's a quote:
"Strictly speaking, you're supposed to use shutdown on a socket before you
close it. The shutdown is an advisory to the socket at the other end.
Depending on the argument you pass it, it can mean "I'm not going to send
anymore, but I'll still listen", or "I'm not listening, good riddance!".
Most socket libraries, however, are so used to programmers neglecting to
use this piece of etiquette that normally a close is the same as
shutdown(); close(). So in most situations, an explicit shutdown is not
needed."

However my first understanding from the python docs is quite different..
it was that like a unix pipe, a socket could be made to communicate in one
direction only. Like a RD-only pipe a socket could be made RD-only and
another socket WR-only...perhaps like ftp keeps the control channel open
but transmits over a read only socket???



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


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Jp Calderone
On Sat, 25 Jun 2005 01:36:56 -, Grant Edwards <[EMAIL PROTECTED]> wrote:
>On 2005-06-25, Giovanni Tumiati <[EMAIL PROTECTED]> wrote:
>
>> (2)Does one have to do a socket.shutdown() before one does a
>> socket.close??
>
>No.
>
>[I've never figured out why one would do a shutdown RDWR
>rather than close the connection, but I haven't put a lot of
>thought into it.]

shutdown actually tears down the TCP connection; close releases the file 
descriptor.

If there is only one file descriptor referring to the TCP connection, these are 
more or less the same.  If there is more than one file descriptor, though, the 
difference should be apparent.

Jp
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Grant Edwards
On 2005-06-25, Giovanni Tumiati <[EMAIL PROTECTED]> wrote:

> However some of my questions still remain from earlier post:
> (1) What is the difference between 
>  - setdefaulttimeout(timeout)

That sets the timeout for any sockets created in the future.

>  - settimeout(value)

That sets the timeout for an individual socket.

> how should they be used?

Use the former if you want the same timeout for all sockets.
Use the latter if if you want to set them individually for each
socket.

Is the documentation unclear?

> (2)Does one have to do a socket.shutdown() before one does a
> socket.close??

No.

[I've never figured out why one would do a shutdown RDWR
rather than close the connection, but I haven't put a lot of
thought into it.]

-- 
Grant Edwards   grante Yow!  -- I love KATRINKA
  at   because she drives a
   visi.comPONTIAC. We're going
   awaynow. I fed the cat.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Peter Hansen
Giovanni Tumiati wrote:
> However some of my questions still remain from earlier post:
> (1) What is the difference between / how should they be used?
>  - setdefaulttimeout(timeout)
>  - settimeout(value)

I think it's basically as you surmised.  Calling 
socket.setdefaulttimeout() (where "socket" is the module itself) sets up 
the default timeout value for all subsequently created sockets.  Calling 
sock.settimeout() (where "sock" is a specific socket object) changes the 
timeout value on that socket alone.

> (2)Does one have to do a socket.shutdown() before one does a socket.close??

Not normally, AFAIK.

-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread Giovanni Tumiati
To all those that replied - thank you.
I solved the problem I posted earlier.
I'm embarrassed to admit that it was caused by the following:
...
while 1:  ## wait for a connection
  try:
  #...waiting for connection
  (client, address)=sa.accept()
except sa.timeout: <--there is no such exception for a socket!
 #...waiting for connection timeout
 continue
except:
 continue  ## for now ignore this!
...

There is no "timeout exception" on a socket so the exception was failing.
I don't remember where I read about it, but now I cannot find such an
exception...I guess I dreamt it up :-)

However some of my questions still remain from earlier post:
(1) What is the difference between / how should they be used?
 - setdefaulttimeout(timeout)
 - settimeout(value)

(2)Does one have to do a socket.shutdown() before one does a socket.close??

Again thanks!
pete

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


Re: Some questions...

2005-02-10 Thread bruno modulix
Mario Lacunza wrote:
Hello,
Im new in Python, please  I need some information:
- Somebody know if: is possible use Python within Net Framework in
windows environment??
http://www.ironpython.com/
http://www.zope.org/Members/Brian/PythonNet/
- Where found info about reports in Python? exist some program like
Crystal Reports??
ReportLab is probably the place to go
http://www.reportlab.org/
-  Database access: Firebird , I dont found correct information about
this tools and his conection/working with Python.
http://kinterbasdb.sourceforge.net/

Thanks!!!
HTH
--
bruno desthuilliers
ruby -e "print '[EMAIL PROTECTED]'.split('@').collect{|p| 
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
--
--
http://mail.python.org/mailman/listinfo/python-list


Some questions...

2005-02-09 Thread Mario Lacunza
Hello,

Im new in Python, please  I need some information:

- Somebody know if: is possible use Python within Net Framework in
windows environment??
- Where found info about reports in Python? exist some program like
Crystal Reports??
-  Database access: Firebird , I dont found correct information about
this tools and his conection/working with Python.

Thanks!!!

-- 
Saludos / Best regards

Mario Lacunza
Email: [EMAIL PROTECTED]
Lima - Peru
-- 
http://mail.python.org/mailman/listinfo/python-list