Re: Noob trying to parse bad HTML using xml.etree.ElementTree

2012-12-30 Thread Chris Angelico
On Sun, Dec 30, 2012 at 8:52 PM, Morten Guldager
 wrote:
> Question is if it's possible to tweak xml.etree.ElementTree to accept, and
> understand sloppy html, or if you have suggestions for similar easy to use
> framework, preferably among the included batteries?
>

Check out BeautifulSoup, it's fairly good at dealing with messy input.

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


Re: Noob trying to parse bad HTML using xml.etree.ElementTree

2012-12-30 Thread Peter Otten
Morten Guldager wrote:

> 'Aloha Friends!
> 
> I'm trying to process some HTML using xml.etree.ElementTree
> Problem is that the HTML I'm trying to read have some not properly closed
> tags, as the  shown in line 8 below.
> 
>   1 from xml.etree import ElementTree
>   2
>   3 tree = ElementTree
>   4 e = tree.fromstring(
>   5 """
>   6 
>   7 
>   8 
>   9 
>  10 
>  11 """)
> 
> Python whines: xml.etree.ElementTree.ParseError: mismatched tag: line 5,
> column 14
> 
> I definitely do want to work DOM style, having the whole shebang loaded
> into a nice structure before I start the real work.
> 
> Question is if it's possible to tweak xml.etree.ElementTree to accept, and
> understand sloppy html, or if you have suggestions for similar easy to use
> framework, preferably among the included batteries?

The  tag doesn't have a closing counterpart in HTML. That implies that 
valid HTML isn't valid XML and that you cannot use xml.etree with HTML.

While it is not in the standard library a good alternative for XML that can 
deal with HTML, too, is lxml. See .
It also provides a way to cope with really broken html, modeled after 
BeautifulSoup.

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


re: ignore case only for a part of the regex?

2012-12-30 Thread Helmut Jarausch
Hi,

is there a means to specify that 'ignore-case' should only apply to a part
of a regex?

E.g.

the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.

I've tried the pattern
r'^Msg-(?:(?i)id):'
but (?i) makes the whole pattern ignoring case.

In my simple case I could say
r'Msg-[Ii][Dd]:'
but that's a bit clumsy.

Is there a more elegant way? Is there a way to compose a pattern
from subpatterns which are compiled with different flags?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New to python, do I need an IDE or is vim still good enough?

2012-12-30 Thread Jamie Paul Griffin
* Yuvraj Sharma  [2012-12-28 01:37:23 -0800]:

> Use IDLE
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

The OP is already a proficient C, C++, perl, ... hacker using console based 
tools and hardcore UNIX editors like vi(1) - I doubt he'll stay with IDLE for 
very long - once you've got accustomed to the UNIX environment and the 
command-line it's difficult and undesirable, in my experience, to switch to GUI 
stuff. 

Stick with what you've been using for the last couple of decades. These tools 
have stood the test of time for a good reason: they're powerful, efficient and 
made for the task of programming. 

Jamie

-- 
Primary Key: 4096R/1D31DC38 2011-12-03
Key Fingerprint: A4B9 E875 A18C 6E11 F46D  B788 BEE6 1251 1D31 DC38
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ignore case only for a part of the regex?

2012-12-30 Thread Roy Smith
Helmut Jarausch  wrote:

> is there a means to specify that 'ignore-case' should only apply to a part
> of a regex?

Not that I'm aware of.
 
> the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.

What's the use-case for this?

The way I would typically do something like this is build my regexes in 
all lower case and .lower() the text I was matching against them.  I'm 
curious what you're doing where you want to enforce case sensitivity in 
one part of a header, but not in another.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ignore case only for a part of the regex?

2012-12-30 Thread Joel Goldstick
On Sun, Dec 30, 2012 at 10:20 AM, Roy Smith  wrote:

> Helmut Jarausch  wrote:
>
> > is there a means to specify that 'ignore-case' should only apply to a
> part
> > of a regex?
>

Python has excellent string methods.  There seems to be a split between
people who first always grab regex for string parsing, and those who  might
not.  If you go with your regex, I think you can comment what you have and
move on.  I glaze over looking at regexes.  That's just me. The code to
first search for "Msg-", then check what follows would take a couple of
lines, but might be easier to understand later.  I've been writing python
for a couple of years, and although I feel comfortable with it, there is
much more more me to learn.  One thing I have learned over many years of
programming is that figuring out what a piece of code is trying to
accomplish takes more time than writing it originally.

Do you really want to match "Msg-iD" (lower case i)? Or are you only
allowing "ID" or "Id"?

>
> Not that I'm aware of.
>
> > the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.
>
> What's the use-case for this?
>
> The way I would typically do something like this is build my regexes in
> all lower case and .lower() the text I was matching against them.  I'm
> curious what you're doing where you want to enforce case sensitivity in
> one part of a header, but not in another.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: changing process name

2012-12-30 Thread Adam Tauno Williams
On Mon, 2012-11-19 at 10:39 +, andrea crotti wrote:
> I have very long processes to spawn which I want to lauch as separate
> processes (and communicate with ZeroMQ), but now the problem is that the
> forked process appears in "ps" with the same name as the launcher
> process.
> This is a simplified version of what I'm trying to do:
> def long_sleep():
> sys.argv[:] = [sys.argv[0]] + ['daemon', 'arguments']
> time.sleep(20)
> so both the sys.argv reassignment don't work so far, any other way to
> fix this?
> On the real machine I also get zombie processes, but on my machine I
> can't reproduce this, and would also be nice to fix that..


The Python module "procname" allows you change the process name so you
can see it in ps, top, etc...

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


Re: ignore case only for a part of the regex?

2012-12-30 Thread Vlastimil Brom
2012/12/30 Helmut Jarausch :
> Hi,
>
> is there a means to specify that 'ignore-case' should only apply to a part
> of a regex?
>
> E.g.
>
> the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.
>
> I've tried the pattern
> r'^Msg-(?:(?i)id):'
> but (?i) makes the whole pattern ignoring case.
>
> In my simple case I could say
> r'Msg-[Ii][Dd]:'
> but that's a bit clumsy.
>
> Is there a more elegant way? Is there a way to compose a pattern
> from subpatterns which are compiled with different flags?
>
> Many thanks for a hint,
> Helmut.
> --
> http://mail.python.org/mailman/listinfo/python-list

Hi,
you may check the new regex implementation for python
http://pypi.python.org/pypi/regex
which allows (among many other improvements) for scoped flags:

>>> import regex
>>> regex.findall(r"Msg-(?i:id):", "the regex should match  Msg-id:, Msg-Id:, 
>>> ...  but not  msg-id:, MSG-ID:  and so on")
['Msg-id:', 'Msg-Id:']
>>>

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


Re: ignore case only for a part of the regex?

2012-12-30 Thread Roy Smith
In article ,
 Vlastimil Brom  wrote:

> you may check the new regex implementation for python
> http://pypi.python.org/pypi/regex

Wow, I wasn't aware of such an effort.

At first reading, I'm amused by the concept of "strict fuzzy matching".

Those of us with long memories will be confused by the name of the 
module, however.  To me, "regex" is the old version, and "re" is the new 
(http://docs.python.org/release/1.5.1/lib/module-regex.html).  Now, it's 
going to be "regex" is either the really old, or the really new version, 
and "re" is what came in between.  I suppose the next iteration after 
this will be called "re" again :-)

I'm not sure I like the fuzzy matching stuff.  On the one hand, it can 
be useful.  On the other hand, people already complain about how 
difficult it can be to read regexes.  Once we add things like 
{i<=1,d<=1,s<=1,2i+2d+1s<=4} in the mix, it's going to be total line 
noise.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ignore case only for a part of the regex?

2012-12-30 Thread MRAB

On 2012-12-30 17:32, Roy Smith wrote:

In article ,
  Vlastimil Brom  wrote:


you may check the new regex implementation for python
http://pypi.python.org/pypi/regex


Wow, I wasn't aware of such an effort.

At first reading, I'm amused by the concept of "strict fuzzy matching".

Those of us with long memories will be confused by the name of the
module, however.  To me, "regex" is the old version, and "re" is the new
(http://docs.python.org/release/1.5.1/lib/module-regex.html).  Now, it's
going to be "regex" is either the really old, or the really new version,
and "re" is what came in between.  I suppose the next iteration after
this will be called "re" again :-)

I'm not sure I like the fuzzy matching stuff.  On the one hand, it can
be useful.  On the other hand, people already complain about how
difficult it can be to read regexes.  Once we add things like
{i<=1,d<=1,s<=1,2i+2d+1s<=4} in the mix, it's going to be total line
noise.


The request for fuzzy matching came after someone saw the TRE library:

http://laurikari.net/tre/

The difference is that the syntax is a bit clearer, IMHO. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Tarfile and usernames

2012-12-30 Thread Nicholas Cole
Dear List,

I'm hoping to use the tarfile module in the standard library to move some
files between computers.

I can't see documented anywhere what this library does with userids and
groupids.  I can't guarantee that the computers involved will have the same
users and groups, and would like the archives to be extracted so that the
files are all owned by the extracting user.

Essentially, I do *not* with to preserve the owner and groups specified in
the archives.

What is the right way to achieve this?

Best wishes,

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


Re: Tarfile and usernames

2012-12-30 Thread Albert Hopkins






On Sun, Dec 30, 2012, at 01:57 PM, Nicholas Cole wrote:

Dear List,

I'm hoping to use the tarfile module in the standard library to move
some files between computers.

I can't see documented anywhere what this library does with userids and
groupids.  I can't guarantee that the computers involved will have the
same users and groups, and would like the archives to be extracted so
that the files are all owned by the extracting user.

Essentially, I do *not* with to preserve the owner and groups specified
in the archives.


Each "member" in the tar file has misc. metadata associated with it,
which can be retrieved with the get_info() method.  You can add/modify
this metadata if creating a TarFile.

However, it should be stated that by default (on *nix anyway) if the
user is not root then user/groups are assigned to the user exctracting
the file (because only root can assign userids/non-member-groups).
The TarFile extract*() methods pretty much inherit the same behavior as
the *nix tar command.  So if you are extracting as a non-root user, you
should expect the same behavoir.  If you are extracting as root but
don't want to change user/groups may have to extract it manually or
create your own class by inheriting TarFile and overriding the .chown()
method.

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


Re: Tarfile and usernames

2012-12-30 Thread Michael Ross

On Sun, 30 Dec 2012 19:57:31 +0100, Nicholas Cole  wrote:Dear List,I'm hoping to use the tarfile module in the standard library to move some files between computers. I can't see documented anywhere what this library does with userids and groupids.  I can't guarantee that the computers involved will have the same users and groups, and would like the archives to be extracted so that the files are all owned by the extracting user. 
Essentially, I do *not* with to preserve the owner and groups specified in the archives.As long as you don't extract as root, doesn't extracting set the extracting user's uid/gid anyway?Regards,MichaelWhat is the right way to achieve this?
Best wishes,Nicholas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tarfile and usernames

2012-12-30 Thread Nicholas Cole
On Sun, Dec 30, 2012 at 8:07 PM, Albert Hopkins wrote:
>
>
> On Sun, Dec 30, 2012, at 01:57 PM, Nicholas Cole wrote:
>
> Dear List,
>
> I'm hoping to use the tarfile module in the standard library to move some
> files between computers.
>
> I can't see documented anywhere what this library does with userids and
> groupids.  I can't guarantee that the computers involved will have the same
> users and groups, and would like the archives to be extracted so that the
> files are all owned by the extracting user.
>
> Essentially, I do *not* with to preserve the owner and groups specified in
> the archives.
>
>
> Each "member" in the tar file has misc. metadata associated with it, which
> can be retrieved with the get_info() method.  You can add/modify this
> metadata if creating a TarFile.
>
> However, it should be stated that by default (on *nix anyway) if the user
> is not root then user/groups are assigned to the user exctracting the file
> (because only root can assign userids/non-member-groups).   The TarFile
> extract*() methods pretty much inherit the same behavior as the *nix tar
> command.  So if you are extracting as a non-root user, you should expect
> the same behavoir.  If you are extracting as root but don't want to change
> user/groups may have to extract it manually or create your own class by
> inheriting TarFile and overriding the .chown() method.
>
>

Thank you for this.  I guess that since the behaviour is not defined in the
documentation, it would probably be safest to inherit and change chown into
a no op.

I know that on *most* systems these days ordinary users cannot chown, and
also that people shouldn't be running things as root.  But you can't be too
careful - and predicting what a given user will do is always dangerous!

Best wishes,

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


Re: Tarfile and usernames

2012-12-30 Thread Hans Mulder
On 30/12/12 19:57:31, Nicholas Cole wrote:
> Dear List,
> 
> I'm hoping to use the tarfile module in the standard library to move
> some files between computers. 
> 
> I can't see documented anywhere what this library does with userids and
> groupids.  I can't guarantee that the computers involved will have the
> same users and groups, and would like the archives to be extracted so
> that the files are all owned by the extracting user. 
> 
> Essentially, I do *not* with to preserve the owner and groups specified
> in the archives.
> 
> What is the right way to achieve this?

I would agree that this ought to be documented.

>From reading the code: the way to achieve this, is to run as a user
other than root. Or monkeypatch the TarFile.chown method to be a no-op.


Hope this helps,

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


Re: instructor's solutions manual for Understanding Analysis by Stephen Abbott

2012-12-30 Thread santoshlinkha
On Saturday, September 3, 2011 4:47:43 AM UTC+5:45, peter kalvin wrote:
> I have solutions manuals to all problems and exercises in these
> textbooks. To get one in an electronic format contact me at:
> kalvinmanual(at)gmail(dot)com and let me know its title, author and
> edition. Please this service is NOT free.
> 
> 
> 
> instructor's solutions manual for A First Course in Differential
> Equations  (7th ed.)  Zill &  Diferential Equations  (5th ed.)Zill &
> Cullen
> instructor's solutions manual for  Multivariable Calculus, 4th
> Edition, JAMES STEWART
> instructor's solutions manual for  Principles of Foundation
> Engineering, 6E by Braja M.Das
> instructor's solutions manual for A Course in Game Theory by Osborne,
> Rubinstein
> instructor's solutions manual for A Course in Modern Mathematical
> Physics by Peter Szekeres
> instructor's solutions manual for A Course in Ordinary Differential
> Equations by Swift, Wirkus
> instructor's solutions manual for A First Course in Abstract Algebra
> (7th Ed., John B. Fraleigh)
> instructor's solutions manual for A First Course in Differential
> Equations -  The Classic Fifth Edition By Zill, Dennis G
> instructor's solutions manual for A First Course In Probability 7th
> Edition by Sheldon M. Ross
> instructor's solutions manual for A First Course in Probability
> Theory, 6th edition, by S. Ross.
> instructor's solutions manual for A First Course in String Theory,
> 2004, Barton Zwiebach
> instructor's solutions manual for A First Course in the Finite Element
> Method, 4th Edition  logan
> instructor's solutions manual for A Practical Introduction to Data
> Structures and Algorithm Analysis 2Ed by Shaffer
> instructor's solutions manual for A Quantum Approach to Condensed
> Matter Physics (Philip L. Taylor & Olle Heinonen)
> instructor's solutions manual for A Short Course in General Relativity
> 2e by J. Foster and J. D. Nightingale
> instructor's solutions manual for A Short Introduction to Quantum
> Information and Quantum Computation by Michel Le Bellac
> instructor's solutions manual for A Transition to Advanced Mathematics
> 5th E by Smith, Eggen, Andre
> instructor's solutions manual for Accounting Principles 8e by Kieso,
> Kimmel
> instructor's solutions manual for Accounting principles 8th Ed by
> Weygandt
> instructor's solutions manual for Accounting, 23 Ed by Carl S. Warren,
> James M. Reeve, Jonathan Duchac
> instructor's solutions manual for Accounting,8th Ed by
> Horngren,Harrison, Oliver
> instructor's solutions manual for Adaptive Control, 2nd. Ed., by
> Astrom, Wittenmark
> instructor's solutions manual for Adaptive Filter Theory (4th Ed.,
> Simon Haykin)
> instructor's solutions manual for Advanced Accounting 10E
> international ED by Beams , Clement, Anthony, Lowensohn
> instructor's solutions manual for Advanced accounting 9th Ed by Hoyle,
> Schaefer
> instructor's solutions manual for Advanced Calculus Gerald B. Folland
> instructor's solutions manual for Advanced Digital Design with the
> Verilog HDL by Michael D. Ciletti
> instructor's solutions manual for Advanced Dynamics (Greenwood)
> instructor's solutions manual for Advanced Engineering
> Electromagnetics by Constantine A. Balanis
> instructor's solutions manual for Advanced Engineering Mathematics 3rd
> ed zill
> instructor's solutions manual for Advanced Engineering Mathematics 8Ed
> Erwin Kreyszig
> instructor's solutions manual for Advanced Engineering Mathematics by
> Erwin Kreyszig, 9th ed
> instructor's solutions manual for Advanced Engineering Mathematics,
> 6th Edition by Peter V. O'Neil
> instructor's solutions manual for Advanced Engineering Mathematics,2E,
> by Zill, Cullen
> instructor's solutions manual for Advanced Engineering Thermodynamics,
> 3rd Edition by Adrian Bejan
> instructor's solutions manual for Advanced Financial Accounting  by
> Baker
> instructor's solutions manual for Advanced Financial Accounting 5 Ed
> by Baker
> instructor's solutions manual for Advanced Industrial Economics by
> Martin
> instructor's solutions manual for Advanced Industrial Economics, 2nd
> ED Stephen Martin
> instructor's solutions manual for Advanced Macroeconomics 2nd edition
> by David Romer
> instructor's solutions manual for Advanced Macroeconomics, by David
> Romer
> instructor's solutions manual for Advanced Mechanics of Materials 6th
> ed by Boresi, Schmidt
> instructor's solutions manual for Advanced Modern Engineering
> Mathematics 3rd Ed Glyn James
> instructor's solutions manual for Advanced Modern Engineering
> Mathematics, 3rd Ed., by G. James
> instructor's solutions manual for Advanced Organic Chemistry Part A-
> Structure and Mechanisms 5th E by Carey, Sundberg
> instructor's solutions manual for Aircraft Structures for Engineering
> Students (4th Ed., T.H.G. Megson)
> instructor's solutions manual for Algebra & Trigonometry and
> Precalculus, 3rd Ed By Beecher, Penna, Bittinger
> instructor's solutions manual for Algebra Baldor
> instructor's solutions manual for 

Re: Python lists

2012-12-30 Thread Hans Mulder
On 28/12/12 18:46:45, Alex wrote:
> Manatee wrote:
> 
>> On Friday, December 28, 2012 9:14:57 AM UTC-5, Manatee wrote:
>>> I read in this:
>>>
>>> ['C100, C117', 'X7R 0.033uF 10% 25V  0603', '0603-C_L, 0603-C_N',
>>> '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
>>> 'Digi-Key', '490-1521-1-ND', '']
>>>
>>>
>>>
>>> Then I need to convert it to this:
>>>
>>> [['C100', 'C117'], 'X7R 0.033uF 10% 25V  0603', '0603-C_L',
>>> '0603-C_N', '10', '2', '', '30', '15463-333', 'MURATA',
>>> 'GRM188R71E333KA01D', 'Digi-Key', '490-1521-1-ND', '']]
>>>
> 
> l = ['C100, C117', 'X7R 0.033uF 10% 25V  0603', '0603-C_L, 0603-C_N',
> '10', '2', '', '30', '15463-333', 'MURATA', 'GRM188R71E333KA01D',
> 'Digi-Key', '490-1521-1-ND', '']
> 
> [item.split(',') if i == 0 else item for i, item in enumerate(l)]

If it's okay to modify the original list, you can simply do:

l[0] = split(l[0], ", ")

If modifying the original is not okay, the simple solution would
be to copy it first:

l2 = l
l2[0] = split(l2[0], ", ")


Hope this helps,

-- HansM

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


Re: Re: Python lists

2012-12-30 Thread Evan Driscoll
On 12/30/2012 4:19 PM, Hans Mulder wrote:
> If it's okay to modify the original list, you can simply do:
> 
> l[0] = split(l[0], ", ")
> 
> If modifying the original is not okay, the simple solution would
> be to copy it first:
> 
> l2 = l
> l2[0] = split(l2[0], ", ")

Um, that doesn't copy the list:

>>> l = ["C100, C117", "X7R ..."]
>>> l2 = l
>>> import string
>>> l2[0] = string.split(l2[0], ", ")
>>> l
[['C100', 'C117'], 'X7R ...']
>>> l2
[['C100', 'C117'], 'X7R ...']

To make a copy of a list you can either use slice syntax (l2 = l[:]) or
call the list constructor (l2 = list(l)).


Evan


> 
> 
> Hope this helps,
> 
> -- HansM
> 
> 


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


Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Alvaro Lacerda
The code I wrote is supposed to ask the user to enter a number;
Then tell the user what's going to happen to that number (x / 2 + 5) ;
Then give the user an answer;

I succeeded getting results from even numbers, but when I try diving an uneven 
number (i.e. 5) by 2, I get only the whole number (i.e. 2) 

I'm trying to get full number result using the %d command, I've tried to add 
the line "from __future__ import division" to the beginning of my code, but I 
haven't been succeeded.

I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
 

Below is my program and thanks for the help :)



from __future__ import division;

def decimal_percent_test():
number = input("Enter a number: ");
print number, "will be divided by 2 then added by 5!";
print " %d divided by %d is %d plus %d is... %d !" 
%(number,2,number/2,5,number/2+5);


   

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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Joel Goldstick
On Sun, Dec 30, 2012 at 5:37 PM, Alvaro Lacerda wrote:

> The code I wrote is supposed to ask the user to enter a number;
> Then tell the user what's going to happen to that number (x / 2 + 5) ;
> Then give the user an answer;
>

Try x / 2.5 + 5

>
> I succeeded getting results from even numbers, but when I try diving an
> uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
>
> I'm trying to get full number result using the %d command, I've tried to
> add the line "from __future__ import division" to the beginning of my code,
> but I haven't been succeeded.
>
> I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
>
>
> Below is my program and thanks for the help :)
>
>
>
> from __future__ import division;
>
> def decimal_percent_test():
> number = input("Enter a number: ");
> print number, "will be divided by 2 then added by 5!";
> print " %d divided by %d is %d plus %d is... %d !"
> %(number,2,number/2,5,number/2+5);
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Irmen de Jong
On 30-12-2012 23:37, Alvaro Lacerda wrote:
> 
> I'm trying to get full number result using the %d command

Try %f instead. %d is the formatting symbol for integer numbers.
See http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

Or have a look at what string.format() can do:
http://docs.python.org/2/library/string.html#format-examples


-irmen


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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Vlastimil Brom
2012/12/30 Alvaro Lacerda :
> The code I wrote is supposed to ask the user to enter a number;
> Then tell the user what's going to happen to that number (x / 2 + 5) ;
> Then give the user an answer;
>
> I succeeded getting results from even numbers, but when I try diving an 
> uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
>
> I'm trying to get full number result using the %d command, I've tried to add 
> the line "from __future__ import division" to the beginning of my code, but I 
> haven't been succeeded.
>
> I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
>
>
> Below is my program and thanks for the help :)
>
> from __future__ import division;
>
> def decimal_percent_test():
> number = input("Enter a number: ");
> print number, "will be divided by 2 then added by 5!";
> print " %d divided by %d is %d plus %d is... %d !" 
> %(number,2,number/2,5,number/2+5);
>
> --
> http://mail.python.org/mailman/listinfo/python-list


Hi,
it seems, you are converting the numbers to integer decimals using %d
try simply %s (together with from __future__ import division
cf.:
>>> "%d" % (1.5,)
'1'
>>> "%s" % (1.5,)
'1.5'
>>>

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Peter Otten
Alvaro Lacerda wrote:

> The code I wrote is supposed to ask the user to enter a number;
> Then tell the user what's going to happen to that number (x / 2 + 5) ;
> Then give the user an answer;
> 
> I succeeded getting results from even numbers, but when I try diving an
> uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
> 
> I'm trying to get full number result using the %d command, I've tried to
> add the line "from __future__ import division" to the beginning of my
> code, but I haven't been succeeded.
> 
> I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
>  
> 
> Below is my program and thanks for the help :)
> 
> 
> 
> from __future__ import division;
> 
> def decimal_percent_test():
> number = input("Enter a number: ");
> print number, "will be divided by 2 then added by 5!";
> print " %d divided by %d is %d plus %d is... %d !"
> %(number,2,number/2,5,number/2+5);

Let's try it in the interactive interpreter:


>>> from __future__ import division
>>> 5/2
2.5

So the caculation part works.

>>> "%d" % 2.5
'2'

But the decimal part is dropped by the formatting operation. You have to use 
another format. The simplest is %s:

>>> "%s" % 2.5
'2.5'

%f is also possible and offers more control over the output:

>>> "%f" % 2.5
'2.50'
>>> "%.2f" % 2.5
'2.50'

See 
for more.

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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Hans Mulder
Hello,

Python does not support REAL numbers.  It has float number, which
are approximations of real numbers.  They behave almost, but not
quite, like you might expect.

It also has Decimal numbers.  They also approximate real numbers,
but slightly differently.  They might behave more like you'd expect
if you're used to an electronic calculator.


On 30/12/12 23:37:53, Alvaro Lacerda wrote:
> The code I wrote is supposed to ask the user to enter a number;
> Then tell the user what's going to happen to that number (x / 2 + 5) ;
> Then give the user an answer;
> 
> I succeeded getting results from even numbers, but when I try diving
> an uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2) 
> 
> I'm trying to get full number result using the %d command, I've tried
> to add the line "from __future__ import division" to the beginning of
> my code, but I haven't been succeeded.
> 
> I also tried making my numbers decimals (i.e. 2.0 instead of just 2)

Errrhm, 2.0 is a float.

To get decimal numbers, you'd need to import the Decimal module.



> Below is my program and thanks for the help :)
> 
> 
> 
> from __future__ import division;
> 
> def decimal_percent_test():
> number = input("Enter a number: ");
> print number, "will be divided by 2 then added by 5!";
> print " %d divided by %d is %d plus %d is... %d !" %(
>   number,2,number/2,5,number/2+5);

The %d code will convert your number to a whole number.
as you found out.

The simplest way to make this program work, is to use the %s
code, which doesn't convert to a different type of number:

from __future__ import division;

def decimal_percent_test():
number = input("Enter a number: ")
print number, "will be divided by 2 then added by 5!"
print " %s divided by %s is %s plus %s is... %s !" % (
number, 2, number/2, 5, number/2+5)

while True:
decimal_percent_test()


Enter a number: 10
10 will be divided by 2 then added by 5!
 10 divided by 2 is 5.0 plus 5 is... 10.0 !
Enter a number: 11
11 will be divided by 2 then added by 5!
 11 divided by 2 is 5.5 plus 5 is... 10.5 !
Enter a number: x
Traceback (most recent call last):
  File "test.py", line 11, in 
decimal_percent_test()
  File "test.py", line 5, in decimal_percent_test
number = input("Enter a number: ")
  File "", line 1, in 
NameError: name 'x' is not defined



Hope this helps,

-- HansM

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


Re: Python lists

2012-12-30 Thread Hans Mulder
On 30/12/12 23:25:39, Evan Driscoll wrote:
> On 12/30/2012 4:19 PM, Hans Mulder wrote:
>> If it's okay to modify the original list, you can simply do:
>>
>> l[0] = split(l[0], ", ")
>>
>> If modifying the original is not okay, the simple solution would
>> be to copy it first:
>>
>> l2 = l
>> l2[0] = split(l2[0], ", ")
> 
> Um, that doesn't copy the list:

Oops, you're right.


 l = ["C100, C117", "X7R ..."]
 l2 = l
 import string
 l2[0] = string.split(l2[0], ", ")
 l
> [['C100', 'C117'], 'X7R ...']
 l2
> [['C100', 'C117'], 'X7R ...']
> 
> To make a copy of a list you can either use slice syntax (l2 = l[:]) or
> call the list constructor (l2 = list(l)).

Thanks for correcting me,

-- HansM

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


Re: Beginner: Trying to get REAL NUMBERS from %d command

2012-12-30 Thread Alvaro Lacerda
%s got the job done!!!

Thank you all for the info and links,
I appreciate it!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ignore case only for a part of the regex?

2012-12-30 Thread Cameron Simpson
On 30Dec2012 12:32, Roy Smith  wrote:
| In article ,
|  Vlastimil Brom  wrote:
| > you may check the new regex implementation for python
| > http://pypi.python.org/pypi/regex
[...]
| I'm not sure I like the fuzzy matching stuff.  On the one hand, it can 
| be useful.  On the other hand, people already complain about how 
| difficult it can be to read regexes.  Once we add things like 
| {i<=1,d<=1,s<=1,2i+2d+1s<=4} in the mix, it's going to be total line 
| noise.

One might argue that people are not obliged to use the fuzzy matching
syntax (or any other part, really). It should be a matter of taste.
Of course, some people have bad taste:-)

However if you need fuzzy matching, it is very handy if there's a library to
hand that offers it.

Cheers,
-- 
Cameron Simpson 

Yes, sometimes Perl looks like line-noise to the uninitiated, but to the
seasoned Perl programmer, it looks like checksummed line-noise with a mission
in life.- The Llama Book
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 3.3, gettext and Unicode problems

2012-12-30 Thread Marcel Rodrigues
I'm using Python 3.3 (CPython) and am having trouble getting the standard
gettext module to handle Unicode messages.
My problem can be isolated as follows:

I have 3 files in a folder: greeting.py, greeting.po and msgfmt.py.

-- greeting.py --
import gettext

t = gettext.translation("greeting", "locale", ["pt"])
_ = t.lgettext

print("_charset = {0}\n".format(t._charset))
print(_("hello"))
-- EOF --

-- greeting.po --
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "hello"
msgstr "olá"
-- EOF --

msgfmt.py was downloaded from
http://hg.python.org/cpython/file/9e6ead98762e/Tools/i18n/msgfmt.py, since
this tool apparently isn't included in the python3 package available on
Arch Linux official repositories.

It's probably also worth noting that the file greeting.po is encoded itself
as UTF-8.

>From that folder, I run the following commands:

$ mkdir -p locale/pt/LC_MESSAGES
$ python msgfmt.py -o !$/greeting.mo greeting.po
$ python greeting.py

The output is:
_charset = UTF-8

Traceback (most recent call last):
  File "greeting.py", line 7, in 
print(_("hello"))
  File "/usr/lib/python3.3/gettext.py", line 314, in lgettext
return tmsg.encode(locale.getpreferredencoding())
UnicodeEncodeError: 'ascii' codec can't encode character '\xe1' in position
2: ordinal not in range(128)

My interpretation of this output is that even though gettext correctly
detects the MO file charset as UTF-8, it tries to encode the translated
message with the system's "preferred encoding", which happens to be ASCII.

Anyone know why this happens? Is this a bug on my code? Maybe I have
misunderstood gettext...

Thanks,

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


Re: Python 3.3, gettext and Unicode problems

2012-12-30 Thread Terry Reedy

On 12/30/2012 7:39 PM, Marcel Rodrigues wrote:

I'm using Python 3.3 (CPython) and am having trouble getting the
standard gettext module to handle Unicode messages.


I have never even looked at the doc before, but I will take a look.


My problem can be isolated as follows:

I have 3 files in a folder: greeting.py, greeting.po and msgfmt.py.

-- greeting.py --
import gettext

t = gettext.translation("greeting", "locale", ["pt"])
_ = t.lgettext


gettext.lgettext(message)
Equivalent to gettext(), but the translation is returned in the 
preferred system encoding, if no other encoding was explicitly set with 
bind_textdomain_codeset().


Giving that 'preferred system encoding' apparent means 
'locale.getpreferredencoding' and that seems to not be what you want, 
why are you using the 'l' version?




print("_charset = {0}\n".format(t._charset))
print(_("hello"))


A strong suggestion: whenever you want to print a string and the 
computation of the string (or bytes) involves encoding/decoding, 
separate the computation and the printing (on two separate line).


s = _("hello")
print(s)

The reason is that printing also requires encoding for the output device 
and that process can also generate a UnicodeError that may be hard to 
distinguish from an error in the computation of s itself.



-- EOF --

-- greeting.po --
msgid ""
msgstr ""
"Project-Id-Version: 1.0\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

msgid "hello"
msgstr "olá"
-- EOF --

msgfmt.py was downloaded from
http://hg.python.org/cpython/file/9e6ead98762e/Tools/i18n/msgfmt.py,
since this tool apparently isn't included in the python3 package
available on Arch Linux official repositories.

It's probably also worth noting that the file greeting.po is encoded
itself as UTF-8.

 From that folder, I run the following commands:

$ mkdir -p locale/pt/LC_MESSAGES
$ python msgfmt.py -o !$/greeting.mo greeting.po
$ python greeting.py

The output is:
_charset = UTF-8

Traceback (most recent call last):
   File "greeting.py", line 7, in 
 print(_("hello"))
   File "/usr/lib/python3.3/gettext.py", line 314, in lgettext
 return tmsg.encode(locale.getpreferredencoding())
UnicodeEncodeError: 'ascii' codec can't encode character '\xe1' in
position 2: ordinal not in range(128)


In particular, we have seen, in previous posts here, this exact error 
generated during printing rather than during the string computation and 
posters have wasted time looking for the error in the string or bytes 
computation itself.



My interpretation of this output is that even though gettext correctly
detects the MO file charset as UTF-8, it tries to encode the translated
message with the system's "preferred encoding", which happens to be ASCII.


Just as you seem to have requested ;-)


Anyone know why this happens? Is this a bug on my code? Maybe I have
misunderstood gettext...


You used lgettext (l = locale). As I said, I am new to this.

--
Terry Jan Reedy


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


Re: Python 3.3, gettext and Unicode problems

2012-12-30 Thread Terry Reedy

On 12/30/2012 8:48 PM, Terry Reedy wrote:

On 12/30/2012 7:39 PM, Marcel Rodrigues wrote:

I'm using Python 3.3 (CPython) and am having trouble getting the
standard gettext module to handle Unicode messages.


Addition to previous response.


import gettext

t = gettext.translation("greeting", "locale", ["pt"])


Reading further, I see that this returns a GNUTranslations instance


_ = t.lgettext


So this calls its method:
'''
GNUTranslations.gettext(message)
Look up the message id in the catalog and return the corresponding 
message string, as a Unicode string. If there is no entry in the catalog 
for the message id, and a fallback has been set, the look up is 
forwarded to the fallback’s gettext() method. Otherwise, the message id 
is returned.


GNUTranslations.lgettext(message)
Equivalent to gettext(), but the translation is returned as a bytestring 
encoded in the selected output charset, or in the preferred system 
encoding if no encoding was explicitly set with set_output_charset().

'''
So if you want the unicode translation to be utf-8 encoded, either use 
.gettext and encode it yourself, or use "t.set_output_charset('utf-8')" 
to have it done automatically.


>> print("_charset = {0}\n".format(t._charset))
>> print(_("hello"))

But since you are printing to screen, I suggest using .gettext and let 
print do the encoding to the screen encoding. If that still raises an 
encoding error, then the problem is the console emulator. On windows, 
for instance, IDLE windows handle the entire BMP charset while the 
stupid Windows Command Prompt window does not (certainly not by default, 
and not yet, as far I know).


The encoding of the translations file on disk determines how the bytes 
of the translation table should be *decoded when read, to create unicode 
strings. It does not determine how those strings should be *encoded* 
when sent to a particular destination. That may depend on the 
destination. Multilingual international sites used to encode pages in 
different limited national encodings, according to the language and 
destination. Now many encode and send *everything* as utf-8. I think 
this is the proper policy now. .lgettext seems oriented to the older, 
pre utf-8, national locale encoding way of doing things.


--
Terry Jan Reedy


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


Re: Python 3.3, gettext and Unicode problems

2012-12-30 Thread Marcel Rodrigues
Thank you Terry!

I was trying to follow the documentation but somehow didn't payed attention
to the lgettext/gettext distinction until I read your first response.
Changing lgettext to gettext solved the problem.

It prints correctly to my console because I have to environmental
variable PYTHONIOENCODING set to utf8, which is what my console emulator
uses. But that's actually not necessary in my original application (it has
a web interface), just for this isolated test. I really should have
separated the call to print() as you suggested tough, if only to make the
problem clearer.

As for the "multiple different limited national encodings" vs "everything
as UTF-8", I agree with you and am definitely going for the latter because
the former seems to be unnecessarily complicated.

Thanks again for the help! Problem solved.
-- 
http://mail.python.org/mailman/listinfo/python-list


father class name

2012-12-30 Thread contro opinion
here is my haha  class
class  haha(object):
  def  theprint(self):
print "i am here"

>>> haha().theprint()
i am here
>>> haha(object).theprint()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object.__new__() takes no parameters

why   haha(object).theprint()  get wrong output?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: father class name

2012-12-30 Thread Roy Smith
In article ,
 contro opinion  wrote:

> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
> 
> >>> haha().theprint()
> i am here
> >>> haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
> 
> why   haha(object).theprint()  get wrong output?

Please, when asking questions, let us know what version of python you're 
using.  I'm guessing 2.x?

In any case, the problem is that you defined a class whose constructor 
takes no arguments:

> class  haha(object):
>   def  theprint(self):
> print "i am here"

You didn't define an __init__() method, so it inherits the one from the 
base class, object.

Then here:

> >>> haha(object).theprint()

you try to create an instance of your class and give an argument to the 
constructor.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: father class name

2012-12-30 Thread Andrew Berg
On 2012.12.30 22:18, contro opinion wrote:
> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
> 
 haha().theprint()
> i am here
 haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
> 
> why   haha(object).theprint()  get wrong output?
> 
> 
You have no __init__() method defined that would take parameters. What
are you trying to do?
-- 
CPython 3.3.0 | Windows NT 6.2.9200.16461
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the source of html in lxml?

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 10:32 PM, contro opinion  wrote:
> import urllib
> import lxml.html
> down='http://blog.sina.com.cn/s/blog_71f3890901017hof.html'
> file=urllib.urlopen(down).read()
> root=lxml.html.document_fromstring(file)
> body=root.xpath('//div[@class="articalContent  "]')[0]
> print body.text_content()
>
> When i run the code, what i get is the text content ,how can i get the html
> source code of it?

print lxml.html.tostring(body)

Did you read through the lxml.html documentation?
http://lxml.de/lxmlhtml.html
It includes several examples that make use of lxml.html.tostring().

RTFineM-ly Yours,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the source of html in lxml?

2012-12-30 Thread Dave Angel
On 12/31/2012 01:32 AM, contro opinion wrote:
> import urllibimport lxml.html
> down='http://blog.sina.com.cn/s/blog_71f3890901017hof.html'
> file=urllib.urlopen(down).read()
> root=lxml.html.document_fromstring(file)
> body=root.xpath('//div[@class="articalContent  "]')[0]print 
> body.text_content()
>
> When i run the code, what i get is the text content ,how can i get the html
> source code of it?
>
>

That's got several syntax errors, but if you remove the parts with
errors, you'll find the html source in the misnamed variable 'file'. 
The read() method returns a string.



-- 

DaveA

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


Re: class problem

2012-12-30 Thread Dave Angel
On 12/31/2012 01:36 AM, contro opinion wrote:
> here is my haha  class

You posted the same question twice before, and it was answered two hours
ago.  Read the first thread, instead of starting spurious ones.



-- 

DaveA

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


Re: class problem

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 10:36 PM, contro opinion  wrote:
> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
>
 haha().theprint()
> i am here
 haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
>
> why   haha(object).theprint()  get wrong output?

This marks the third time today that you've posted *this exact same
question* to python-list AKA comp.lang.python.
You received multiple answers the first time you posted it. Please
desist from posting it any further, lest ye get plonk-ed.
If you did not understand the responses you obtained, please reply in
the original thread and ask for clarification, rather than re-posting.

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


Re: father class name

2012-12-30 Thread Chris Rebert
On Sun, Dec 30, 2012 at 8:18 PM, contro opinion  wrote:
> here is my haha  class
> class  haha(object):
>   def  theprint(self):
> print "i am here"
>
 haha().theprint()
> i am here
 haha(object).theprint()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: object.__new__() takes no parameters
>
> why   haha(object).theprint()  get wrong output?

The fact that `haha(object)` is textually part of the *declaration*
`class haha(object):` has no bearing on how one instantiates an
instance of the class `haha`.
In the `class` statement, `haha` is being declared to be a subclass of
class `object` (that's what it means for `object` to be in the
parentheses after the class name in a `class` statement; the syntax is
"class ():").

In the first part of the *expression* `haha().theprint()`, you are
using the function-call operator on the `haha` class itself, which has
the effect of instantiating it; since you gave no arguments in the
function call, haha's initializer (i.e. its __init__() method) was
given no arguments. Since you didn't define an __init__() method for
haha, haha inherited the default __init__() method from class
`object`, which takes no arguments, so your call was fine and worked
as expected.

By contrast, in the first part of the *expression*
`haha(object).theprint()`, you passed an argument (namely, `object`).
Since __init__() wasn't expecting any arguments whatsoever, you
therefore got an error.

The parentheses in a `class` statement do NOT signify a function call;
they are part of the syntax of the `class` statement itself.

Cheers,
Chris
--
Note: I'm oversimplifying things a bit for the sake of understandability.
-- 
http://mail.python.org/mailman/listinfo/python-list