Re: Convert '165.0' to int

2011-07-23 Thread Chris Angelico
On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman  wrote:
> The problem with that is that it will silently ignore any non-zero
> digits after the point. Of course int(float(x)) does the same, which I
> had overlooked.

If you know that there will always be a trailing point, you can trim
off any trailing 0s, then trim off a trailing '.', and then cast to
int:

int(s.rstrip('0').rstrip('.'))

It'll remove only zeroes and then only periods, as opposed to
s.rstrip('.0') which would give a wrong result for (say) '40.000', so
this should be safe. If there's any non-zero digits after the decimal,
they and the decimal will be kept, which will cause the int() cast to
throw an exception.

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


Re: Convert '165.0' to int

2011-07-23 Thread Steven D'Aprano
Frank Millman wrote:

> To recap, the original problem is that it would appear that some third-
> party systems, when serialising int's into a string format, add a .0
> to the end of the string. I am trying to get back to the original int
> safely.
> 
> The ideal solution is the one I sketched out earlier - modify python's
> 'int' function to accept strings such as '165.0'.
> 
> Do you think this would get any traction if I proposed it? Or would it
> fall foul of the moratorium?

No, and no. It would not get any traction -- indeed, many people, including
myself, would oppose it. And it would not fall foul of the moratorium,
because that is over.

I can only point you to what I wrote in reference to somebody else's idea
that changing Python was the most "convenient solution":

http://www.mail-archive.com/python-list%40python.org/msg315552.html

Python is a general purpose programming language. If int("1.0") does not do
what you want, write a function that does, and use it instead! You said
that you want a function that ignores a trailing .0 but warns you if
there's some other decimal value. Easy:

def my_int(astring):
# Untested
if astring.endswith(".0"):
astring = astring[:-2]
return int(astring)

my_int("165.0") will return 165, as expected, while still raising an
exception for float values "165.1" or "165.1E9".

90% of programming is deciding *precisely* what behaviour you want. Once
you've done that, the rest is easy.

Apart from debugging and writing documentation and making it fast enough,
which is the other 90%.

*wink*


-- 
Steven

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


PyCon Australia 2011: Registration Deadlines

2011-07-23 Thread Ryan Kelly


Hi Everyone,


Registrations for PyCon Australia 2011 are closing soon!

The conference is now less than a month away, so we need to start
finalising numbers for shirts, catering and the venue itself.  If 
you're planning to attend, please register now so you don't miss out.

PyCon Australia is Australia's only conference dedicated exclusively to
the Python programming language, and will be held at the Sydney Masonic
Center over the weekend of August 20 and 21. See below for more
information and updates on:
 
 1. Registration Deadlines
 2. More Sponsors Announced
 
Please pass this message on to those you feel may be interested.

 

Registration Deadlines
==

Registrations for the conference will be closing soon, as we have the
following deadlines approaching fast:

  29th July:T-Shirt order finalised
  8th August:   Special dietary needs finalised
  15th August:  Last chance to get tickets!


If you're looking forward to adding a PyCon Au 2011 T-Shirt to your
collection, please complete your registration *this week* so we can get
one in your size.  We will be sending the order through to the printers
based on the sizes requested so far.

If you have special dietary needs (e.g. vegetarian, vegan, gluten-free)
then please complete your registration by Monday the 8th of August.  We
cannot accommodate special dietary orders submitted after this time.

All registrations will close on Monday the 15th of August, so that we 
can confirm final numbers with the venue and catering.  There will be
*no* registrations accepted after this date.

In particular, we will *not* accept registrations at the door.

So don't delay, register now at:

http://pycon-au.org/reg



More Sponsors Announced
===


We are delighted to confirm that Microsoft will be joining us this year
as a Silver Sponsor.  Thanks once again to the following companies for
their continuing support of Python and for helping to make PyCon
Australia 2011 a reality:


Gold:  Google  
Gold:  ComOps  
 
Silver:  Anchor
Silver:  Enthought 
Silver:  Python Software Foundation
Silver:  WingWare  
Silver:  Arclight  
Silver:  Bitbucket by Atlassian
Silver:  Microsoft 


Thanks also to Linux Australia, who provide the overarching legal and
organisational structure for PyCon Australia.
 



Ryan Kelly
PyCon Australia 2011


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


Re: Convert '165.0' to int

2011-07-23 Thread Frank Millman
On Jul 23, 9:42 am, Chris Angelico  wrote:
> On Sat, Jul 23, 2011 at 4:53 PM, Frank Millman  wrote:
> > The problem with that is that it will silently ignore any non-zero
> > digits after the point. Of course int(float(x)) does the same, which I
> > had overlooked.
>
> If you know that there will always be a trailing point, you can trim
> off any trailing 0s, then trim off a trailing '.', and then cast to
> int:
>
> int(s.rstrip('0').rstrip('.'))
>

I like it. 100% solution to the problem, and neater than the
alternatives.

Thanks

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


Re: Convert '165.0' to int

2011-07-23 Thread Frank Millman
On Jul 23, 10:23 am, Steven D'Aprano  wrote:
> Frank Millman wrote:
> > To recap, the original problem is that it would appear that some third-
> > party systems, when serialising int's into a string format, add a .0
> > to the end of the string. I am trying to get back to the original int
> > safely.
>
> > The ideal solution is the one I sketched out earlier - modify python's
> > 'int' function to accept strings such as '165.0'.
>
> > Do you think this would get any traction if I proposed it? Or would it
> > fall foul of the moratorium?
>
> No, and no. It would not get any traction -- indeed, many people, including
> myself, would oppose it. And it would not fall foul of the moratorium,
> because that is over.
>
> I can only point you to what I wrote in reference to somebody else's idea
> that changing Python was the most "convenient solution":
>
> http://www.mail-archive.com/python-list%40python.org/msg315552.html
>
> Python is a general purpose programming language. If int("1.0") does not do
> what you want, write a function that does, and use it instead! You said
> that you want a function that ignores a trailing .0 but warns you if
> there's some other decimal value. Easy:
>
> def my_int(astring):
>     # Untested
>     if astring.endswith(".0"):
>         astring = astring[:-2]
>     return int(astring)
>
> my_int("165.0") will return 165, as expected, while still raising an
> exception for float values "165.1" or "165.1E9".
>
> 90% of programming is deciding *precisely* what behaviour you want. Once
> you've done that, the rest is easy.
>
> Apart from debugging and writing documentation and making it fast enough,
> which is the other 90%.
>
> *wink*
>
> --
> Steven

No argument with any of that.

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


Re: I am fed up with Python GUI toolkits...

2011-07-23 Thread Gregory Ewing

Tim Roberts wrote:

Gregory Ewing  wrote:


sturlamolden wrote:


Or should modern deskop apps be written with something completely
different, such as HTML5?


I hope not! HTML is great for web pages, but not
everything should be a web page.


I don't think your glibness is justified.  There is a legitimate appeal to
this notion.  The fact is that MANY APIs can be completely and adequately
described by HTML.


My brain raises a TypeError on that statement. According to
my understanding of the world, describing APIs is not something
that HTML does. (Or did you mean GUI rather than API?)

There might be some sense in using something HTML-like to
describe the layout of widgets in a GUI. But laying out
widgets is only a tiny part of what's involved in creating
an application with a GUI. You still need a GUI toolkit to
provide the widgets being laid out, and application code
behind that to provide the desired functionality.


With style sheets, you can
get very complete control over the look and feel.


CSS is designed for graphic artists who want complete
control over every aspect of appearance. Again, this is
(arguably) okay for web pages, but I don't think it
applies to GUI applications. You *don't* want every
application looking completely different from every other
on the whim of the author -- quite the opposite.

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


Re: Convert '165.0' to int

2011-07-23 Thread Billy Mays

On 7/23/2011 3:42 AM, Chris Angelico wrote:


int(s.rstrip('0').rstrip('.'))



Also, it will (in?)correct parse strings such as:

'16500'

to 165.

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


Re: Convert '165.0' to int

2011-07-23 Thread Chris Angelico
On Sun, Jul 24, 2011 at 1:12 AM, Billy Mays  wrote:
> On 7/23/2011 3:42 AM, Chris Angelico wrote:
>>
>> int(s.rstrip('0').rstrip('.'))
>>
>
> Also, it will (in?)correct parse strings such as:
>
> '16500'
>
> to 165.

Yes, it will, but is that an issue to the OP? Programming is about
solving the problem you have, not the problems you don't have.
Sometimes it's better to have a simple solution that does the job than
an extremely complex one that isn't excessively lenient. Is it such a
problem for it to be lenient?

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


Re: Is there a way to customise math.sqrt(x) for some x?

2011-07-23 Thread John Nagle

On 7/16/2011 2:14 AM, Chris Angelico wrote:

On Sat, Jul 16, 2011 at 6:35 PM, Steven D'Aprano
  wrote:

I have a custom object that customises the usual maths functions and
operators, such as addition, multiplication, math.ceil etc.

Is there a way to also customise math.sqrt? I don't think there is, but I
may have missed something.


Only thing I can think of is:

import math
math.sqrt=lambda(x) x.__sqrt__(x) if x.whatever else math.sqrt(x)

I don't suppose there's a lambda version of try/catch?

ChrisA


Why use a lambda?  Just use a def.

A lambda with an "if" is un-Pythonic.

John Nagle

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


Re: Convert '165.0' to int

2011-07-23 Thread rantingrick

On Jul 23, 1:53 am, Frank Millman  wrote:
>--
> The problem with that is that it will silently ignore any non-zero
> digits after the point. Of course int(float(x)) does the same, which I
> had overlooked.
>--

Wait a minute; first you said all you wanted was to cast "string
floats" to integers NOW your changing the rules.

>--
> I do not expect any non-zero digits after the point, but if there are,
> I would want to be warned, as I should probably be treating it as a
> float, not an int.
>--
Then the solution is a try:except.

py> def castit(value):
... try:
... v = int(value)
... return v
... except ValueError:
... return float(value)
...
py> castit('165')
165
py> castit('165.0')
165.0
py> castit('165.333')
165.333
py> castit('3.3')
3.3

>--
> To recap, the original problem is that it would appear that some third-
> party systems, when serialising int's into a string format, add a .0
> to the end of the string. I am trying to get back to the original int
> safely.
>--

But you also said you wanted floats too, i am confused??

>--
> The ideal solution is the one I sketched out earlier - modify python's
> 'int' function to accept strings such as '165.0'.
>--

NO! You create your OWN casting function for special cases.

PythonZEN: "Special cases aren't special enough to break the rules."
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: python-ldap 2.4.3

2011-07-23 Thread Michael Ströder
Find a new release of python-ldap:

  http://pypi.python.org/pypi/python-ldap/2.4.3

python-ldap provides an object-oriented API to access LDAP directory
servers from Python programs. It mainly wraps the OpenLDAP 2.x libs for
that purpose. Additionally it contains modules for other LDAP-related
stuff (e.g. processing LDIF, LDAPURLs and LDAPv3 schema).

Project's web site:

  http://www.python-ldap.org/

Ciao, Michael.


Released 2.4.3 2011-07-23

Changes since 2.4.2:

Lib/
* Mostly corrected/updated __doc__ strings

Doc:
* Corrected rst files
* Added missing modules, functions, classes, methods, parameters etc.
  at least as auto-generated doc


Released 2.4.2 2011-07-21

Changes since 2.4.1:

Lib/

Logging:
* pprint.pformat() is now used when writing method/function
  arguments to the trace log

ldap.schema.subentry:
* SubSchema.__init__() now has new key-word argument check_uniqueness
  which enables checking whether OIDs are unique in the subschema subentry
* Code-cleaning: consequent use of method SubSchema.getoid() instead of
  accessing SubSchema.name2oid directly.
* SubSchema.getoid() and SubSchema.getoid() now have key-word argument
  raise_keyerror=0 and raise KeyError with appropriate description.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to customise math.sqrt(x) for some x?

2011-07-23 Thread rantingrick
On Jul 16, 3:35 am, Steven D'Aprano  wrote:
> I have a custom object that customises the usual maths functions and
> operators, such as addition, multiplication, math.ceil etc.
>
> Is there a way to also customise math.sqrt? I don't think there is, but I
> may have missed something.

Hmm, this question should win ambiguous award of the year. Here, allow
me to answer by proxy.


 Ambiguos Caller Sketch

SCENE: (A car owner calls a custom body shop to request work on his
car)
SHOP: "Custom Car Works, how may we help you?"
OWNER: Hello sir. I would like to customize my car.
SHOP: Sure. What kind of car is it exactly?
OWNER: Well i would say it's a normal car.
SHOP: Okay. What size engine does it have?
OWNER: I would say it's a fairy large engine.
SHOP: Interesting. So what kind of work are you needing?
OWNER: Something custom because i want it to look very "customized".
SHOP: Well Sir we are after all a CUSTOM shop! *rolls-eyes*
OWNER: Great, because i need some custom work!
SHOP: Okay Sir, would you like us to PAINT your car?
OWNER: Sure, that would be great.
SHOP: Okay, so what color should we PAINT your car?
OWNER: A light color would be fine.
SHOP: I see. I think we can handle this job for you just fine however
I just have one more question.
OWNER: Yes...
SHOP: Tell me, does this sound like a phone hanging up? *click*
OWNER: Yes.
...
OWNER: Hello?
...
OWNER: Are you there?


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


Strings show as brackets with a 'u'.

2011-07-23 Thread goldtech

Hi,

>>> n
[u'174']
>>>

Probably newbie question but not sure how suppress the brackets and
the 'u' ? I assume pyhon is telling me it's a unicode string in the n
variable.

 I'm using using Idle on winXP, activestate 2.7. Is there a way to
suppress this and just show 174  in the shell ?
A script reading data and assigns 174 to n via some regex. Links on
this appreciated - I've tried to understand unicode before, will keep
trying...thanks.


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


Re: Strings show as brackets with a 'u'.

2011-07-23 Thread rantingrick
On Jul 23, 7:33 pm, goldtech  wrote:
>
> >>> n
> [u'174']
>
> Probably newbie question but not sure how suppress the brackets and
> the 'u' ? I assume pyhon is telling me it's a unicode string in the n
> variable.

Try type(n) and see what happens. Then report back. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strings show as brackets with a 'u'.

2011-07-23 Thread Dan Stromberg
It's probably a list containing a single unicode string.

You can pull the first element from the list with n[0].

To print a unicode string in 2.x without the u stuff:

print u'174'.encode('ISO-8859-1')

On Sat, Jul 23, 2011 at 5:33 PM, goldtech  wrote:

>
> Hi,
>
> >>> n
> [u'174']
> >>>
>
> Probably newbie question but not sure how suppress the brackets and
> the 'u' ? I assume pyhon is telling me it's a unicode string in the n
> variable.
>
>  I'm using using Idle on winXP, activestate 2.7. Is there a way to
> suppress this and just show 174  in the shell ?
> A script reading data and assigns 174 to n via some regex. Links on
> this appreciated - I've tried to understand unicode before, will keep
> trying...thanks.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strings show as brackets with a 'u'.

2011-07-23 Thread Chris Angelico
On Sun, Jul 24, 2011 at 10:33 AM, goldtech  wrote:
>
>  I'm using using Idle on winXP, activestate 2.7. Is there a way to
> suppress this and just show 174  in the shell ?
> A script reading data and assigns 174 to n via some regex. Links on
> this appreciated - I've tried to understand unicode before, will keep
> trying...thanks.

There's two things there. Firstly, your regex is returning a list, not
a string; and secondly, you are seeing repr(n) instead of just its
content. Try:
>>> print(n[0])

This should print just the value.

(Pro tip: rantingrick is a troll. You can safely ignore him.)

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


Re: Strings show as brackets with a 'u'.

2011-07-23 Thread Thomas Jollans
On 24/07/11 02:52, Dan Stromberg wrote:
> 
> It's probably a list containing a single unicode string.
> 
> You can pull the first element from the list with n[0].
> 
> To print a unicode string in 2.x without the u stuff:
> 
> print u'174'.encode('ISO-8859-1')

just
>>> print u'174'
will do.

Encoding the string by hand is only useful if Python doesn't know the
terminal's encoding, but you do.

> 
> On Sat, Jul 23, 2011 at 5:33 PM, goldtech  > wrote:
> 
> 
> Hi,
> 
> >>> n
> [u'174']
> >>>
> 
> Probably newbie question but not sure how suppress the brackets and
> the 'u' ? I assume pyhon is telling me it's a unicode string in the n
> variable.
> 
>  I'm using using Idle on winXP, activestate 2.7. Is there a way to
> suppress this and just show 174  in the shell ?
> A script reading data and assigns 174 to n via some regex. Links on
> this appreciated - I've tried to understand unicode before, will keep
> trying...thanks.
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> 

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


Re: Convert '165.0' to int

2011-07-23 Thread Billy Mays

On 7/23/2011 2:28 PM, rantingrick wrote:


On Jul 23, 1:53 am, Frank Millman  wrote:

--
The problem with that is that it will silently ignore any non-zero
digits after the point. Of course int(float(x)) does the same, which I
had overlooked.
--


Wait a minute; first you said all you wanted was to cast "string
floats" to integers NOW your changing the rules.


--
I do not expect any non-zero digits after the point, but if there are,
I would want to be warned, as I should probably be treating it as a
float, not an int.
--

Then the solution is a try:except.

py>  def castit(value):
... try:
... v = int(value)
... return v
... except ValueError:
... return float(value)
...
py>  castit('165')
165
py>  castit('165.0')
165.0
py>  castit('165.333')
165.333
py>  castit('3.3')
3.3


--
To recap, the original problem is that it would appear that some third-
party systems, when serialising int's into a string format, add a .0
to the end of the string. I am trying to get back to the original int
safely.
--


But you also said you wanted floats too, i am confused??


--
The ideal solution is the one I sketched out earlier - modify python's
'int' function to accept strings such as '165.0'.
--


NO! You create your OWN casting function for special cases.

PythonZEN: "Special cases aren't special enough to break the rules."


I'll probably get flak for this, but damn the torpedoes:

def my_int(num):
import re
try:
m = re.match('^(-?[0-9]+)(.0)?$', num)
return int(m.group(1))
except AttributeError:
#raise your own error, or re raise
raise


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


Re: I am fed up with Python GUI toolkits...

2011-07-23 Thread Cameron Simpson
On 23Jul2011 22:21, Gregory Ewing  wrote:
| Tim Roberts wrote:
| >Gregory Ewing  wrote:
| >>sturlamolden wrote:
| >>>Or should modern deskop apps be written with something completely
| >>>different, such as HTML5?
| >>
| >>I hope not! HTML is great for web pages, but not
| >>everything should be a web page.
| >
| >I don't think your glibness is justified.  There is a legitimate appeal to
| >this notion.  The fact is that MANY APIs can be completely and adequately
| >described by HTML.
[...]
| There might be some sense in using something HTML-like to
| describe the layout of widgets in a GUI. But laying out
| widgets is only a tiny part of what's involved in creating
| an application with a GUI. You still need a GUI toolkit to
| provide the widgets being laid out, and application code
| behind that to provide the desired functionality.

Sure, but if a suitable API is presented for javascript to hook into
over HTTP an HTML widget kit isn't an unreasonable thing to build.
And then you have the cross platform nirvana. Except for the browsers'
various differences and bugs etc etc...

| >With style sheets, you can
| >get very complete control over the look and feel.
| 
| CSS is designed for graphic artists who want complete
| control over every aspect of appearance. Again, this is
| (arguably) okay for web pages, but I don't think it
| applies to GUI applications. You *don't* want every
| application looking completely different from every other
| on the whim of the author -- quite the opposite.

You can override site specific CSS in the firefox browser, possibly
others. There are extensions to make it easier rather than mega-awkward
and undocumented. It is still a bit of a dobge, in not small part
because CSS lacks a "not" - you can't say "style everything except
blah", which means you have to enumerate a bazillion combinations and
you're still playing guessing games.

So, yes, the every author's look'n'feel is gratuitously different chaos
still applies :-(

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

But what ... is it good for?
  --Engineer at the Advanced Computing Systems Division of IBM,
1968,  commenting on the microchip.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert '165.0' to int

2011-07-23 Thread Dan Stromberg
On Sat, Jul 23, 2011 at 8:53 PM, Billy Mays  wrote:

> I'll probably get flak for this, but damn the torpedoes:
>
> def my_int(num):
>import re
>try:
>m = re.match('^(-?[0-9]+)(.0)?$', num)
>return int(m.group(1))
>except AttributeError:
>#raise your own error, or re raise
>raise
>

I'd just been thinking, when I read your post, "It galls me a bit, but a
regex might be good for this".  I'd use a slightly different one though:

>>> import re
>>> m = re.match('^(-?\d+)(\.0*)?', '123')
>>> m.groups(1)
('123', 1)
>>>

This way the meaning of "a digit" is localized, and 0 or more trailing 0's
work.  BTW, Are there any cultures that don't use arabic numerals?  They
seem pretty ubiquitous.  I guess China uses arabic numerals most of the time
and their own traditional system sometimes, but I'm not sure that counts for
this issue.

Another decent way to go, would be to use:

>>> '123.00'.partition('.')
('123', '.', '00')
>>> '123'.partition('.')
('123', '', '')
>>>

...and then check that the 3rd element of the tuple is zero or more 0's,
perhaps using rstrip and comparing to a null string afterward.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I am fed up with Python GUI toolkits...

2011-07-23 Thread Chris Angelico
On Sun, Jul 24, 2011 at 1:56 PM, Cameron Simpson  wrote:
> And then you have the cross platform nirvana. Except for the browsers'
> various differences and bugs etc etc...
>

The "platform" ceases to be Windows/Linux/Mac, ceases to be Qt/GTK/Tk,
and instead becomes Webkit/Gecko/Trident. It's still not perfectly
cross-platform. (Although the recent ones are tending to comply with
the standards better than during the old IE vs Netscape browser wars.)

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


Re: I am fed up with Python GUI toolkits...

2011-07-23 Thread Dan Stromberg
On Wed, Jul 20, 2011 at 12:20 AM, Stefan Behnel  wrote:

> Steven D'Aprano, 20.07.2011 06:28:
>
>  Python has a GIL.
>>>
>>
>> Except for Jython, IronPython and PyPy.
>>
>
> PyPy has a GIL, too.


There's been talk of removing PyPy's GIL using transactional memory though.
-- 
http://mail.python.org/mailman/listinfo/python-list