Re: [Tutor] String formatting expression g conversion type case.

2013-01-24 Thread Barnaby Scott

On 24/01/2013 13:29, Krupkina Lesya Olegovna wrote:

Hello!
I’m newcomer to Python and I’m on documentation reading stage and trying some 
of examples.
I’m using Win7 x64 OS and Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC 
v.1500 64 bit (AMD64)].
I try to understand how string format expression (%)works. Everything is almost 
clear but except one case: using ofg(G) conversion type and # flag.
Let’s take a look at documentation here:
http://docs.python.org/release/2.7.3/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
Document declares for g(G) conversion type in case of using # flag (4th note):
“The precision determines the number of significant digits before and after the 
decimal point and defaults to 6”.

I have noticed behavior that does not meet documentation declaration and looks 
like a bug in case when using g(G) conversion type with # flag
with  omitted  precision  and  zero integer part of the decimal. Could
someone, please comment the case it is a bug or right use case result? If it is 
correct, please explain why.

Steps to reproduce the case:

1.Start python interactive mode
2.Enter string with g(G) conversion type and using #flag like this: %#g%0.3 – 
precision parameter is omitted and integer part of the decimal is zero.
3.Watch the output results

Actual result:

Python outputs decimal as declared as but with more significant digits than 
default value of 6 - if integer part of the decimal is equal to zero.

%#g%0.3

'0.30'

%#G%0.3

'0.30'

%#G%0.004

'0.0040'




Expected results:
As declared in documentation – there will be 6 significant digits before and 
after decimal point by default.

Thanks,
Regards, Lesya.

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




My experience is that things that look odd at first sight are VERY 
rarely bugs - especially in something as mature as Python 2.7.


Maybe the documentation could be slightly clearer, but I see it as 
saying the following:


'#g' and '#G' both convert to exponential format (with some exceptions) 
with a default precision of 6 *significant figures*. It is certainly 
talking about significant figures, not decimal places. So your final 
example may look odd, but it really is doing what it says - there are 6 
s.f. shown, and it has not converted to exponential form because the 
exponent would not be less than -4.


With significant digits before the decimal point, it appears to convert 
to exponential form only if the number of them exceeds the precision.


Best

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


Re: [Tutor] miniwiki 1.3 BETA bugs

2007-02-26 Thread Barnaby Scott
Kirk Z Bailey wrote:
 RE leaves me totally confuzzzeddded. Yep, so confuised I'm having 
 trouble spelling it. Sp this one line will replace both words and give a 
 reliable result?
 
 Barnaby Scott wrote:
 [snip]
 No idea if it has anything to do with your problem, but it struck me 
 that the iswikiword() function (and processword() which seems to be a 
 helper for it) could be replaced with one line, and it would be reliable!

 def iswikiword(word):
 return bool(re.match('^([A-Z][a-z]+){2,}$', word))

 Of course you need to import re, but that seems a small price to pay!

 HTH

 Barnaby Scott


 

As far as I know this is 100% reliable - at least it works for me 
(www.waywood.co.uk/MonkeyWiki/). I suggest you test the function to your 
own satisfaction - feed it tricky 'possible' WikiWords, and see how it does!

I know what you mean - RE syntax is an unruly beast to try and wrestle 
with, but I *so* glad I made the effort. I don't claim to be anything 
like an expert, but I now find it very useful indeed.

Here's how the function's statement works in case you're interested:

bool(re.match('^([A-Z][a-z]+){2,}$', word))

re.match() will look for a match for us, according to the RE given as
the first argument, and the string you want to match against as the second

^ means we demand that the pattern matches from the beginning of the 
string to be tested - we don't want to say yes to 
anEmbeddedWikiWordLikeThis. (In fact because we are using re.match 
instead of re.search this is not strictly necessary, but makes it clearer)

([A-Z][a-z]+) means we want a group of letters, starting with a one in 
the range [A-Z] i.e. a capital, followed by [a-z]+ , meaning one or more 
lowercase letters ('one or more' is specified by the +). That whole 
pattern is parenthesised because we want the next element to refer to 
the whole thing

{2,} means we want a match only if our preceding pattern (i.e. a 
capitalised word) occurs a minimum of 2 times in a row, and a maximum of 
- well, we don't want to specify a maximum, so we leave it out. 
(YouMightInFactWantToSpecifyTheMaximumNumberOfWordsThatAreAllowedToAppearInYourWikiLinksToStopPeopleDoingSillyThingsLikeThis).

$ means we want a match only if the pattern reaches the end of the test 
string - i.e. we don't want to match a WordLikeThis62734.

As for bool() - nothing to do with RE, but if a match occurs, the result 
returned by re.match() is a MatchObject instance, otherwise None. I have 
used bool() to convert these two possible results into True or False 
though I guess this is not strictly necessary - the truth testing would 
happen implicitly outside the function anyway. However it seems right to 
return a boolean if that's what the function's obvious intent is.

HTH


Barnaby Scott







___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] miniwiki 1.3 BETA bugs

2007-02-23 Thread Barnaby Scott
Kirk Z. Bailey wrote:
 ok, let's post this again. last time went into purgatory instead of the list. 
 hmmm
 
 I am working on updating miniwiki. the current beta code has rendering 
 problems with wikiwords and external sites under some circumstances. Here is 
 a link to the latest code:
 
 http://www.tinylist.org/MW.txt
 
 
 
 
 
 
 
 
 Blessed Be!
- Kirk Bailey
  Largo FL USA
 
 
  kniht  
 +-+ http://www.mylemonadestand.biz/ - play the lemonade game!
 | BOX | http://www.tinylist-org/ Freedom software
 +-+ In HER Service- http://www.pinellasintergroupsociety.org/
  think  http://www.seaxtradusa.org/ - The Seax Wica Trad in the USA!
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
 


No idea if it has anything to do with your problem, but it struck me 
that the iswikiword() function (and processword() which seems to be a 
helper for it) could be replaced with one line, and it would be reliable!

def iswikiword(word):
 return bool(re.match('^([A-Z][a-z]+){2,}$', word))

Of course you need to import re, but that seems a small price to pay!

HTH

Barnaby Scott
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string.uppercase: too many for locale

2007-01-11 Thread Barnaby Scott
Kent Johnson wrote:
 Barnaby Scott wrote:
 Can anyone explain the following: I was getting string.uppercase 
 returning an unexpected number of characters, given that the Python 
 Help says that it should normally be A-Z. Being locale-dependent, I 
 checked that my locale was not set to something exotic, and sure 
 enough it is only what I expected - see below:


 IDLE 1.1   No Subprocess 
   import locale, string
   locale.getlocale()
 ['English_United Kingdom', '1252']
   print string.uppercase
 ABCDEFGHIJKLMNOPQRSTUVWXYZŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ
   print string.lowercase
 abcdefghijklmnopqrstuvwxyzƒšœžßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
  

 What am I missing here? Surely for UK English, I really should just be 
 getting A-Z and a-z. In case it is relevant, the platform is Windows 
 2000.
 
 Interesting. Here is what I get:
   import locale, string
   locale.getlocale()
 (None, None)
   string.uppercase
 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
 Somehow the locale for your system has changed from the 'C' locale. If I 
 set the default locale I get similar results to yours:
   locale.setlocale(locale.LC_ALL, '')
 'English_United States.1252'
   locale.getlocale()
 ('English_United States', '1252')
   print string.uppercase
 ABCDEFGHIJKLMNOPQRSTUVWXYZèîă└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╪┘┌█▄▌▐
 
 which doesn't print correctly because my console encoding is actually 
 cp437 not cp1252.
 
 It looks like string.uppercase is giving you all the characters which 
 are uppercase in the current encoding, which seems reasonable. You can 
 use string.ascii_uppercase if you want just A-Z.
 
 Kent
 
Thanks, but this raises various questions:

Why would my locale have 'changed' - and from what?
What *would* be the appropriate locale given that I am in the UK and use
English, and how would I set it?
Why on earth does the ['English_United Kingdom', '1252'] locale setting
consider ŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ to be appropriate?
Is this less to do with Python than the operating system?
Where can I read more on the subject?

Sorry for all the open-ended questions, but I am baffled by this and can
find no information. Sadly, just using string.ascii_uppercase is not a
solution because I am trying to develop something for different locales,
but only want the actual letters that a particular language uses to be
returned - e.g. English should be A-Z only, Swedish should be A-Z + ÅÄÖ
(only) etc. The thing I really want to avoid is having to hard-code for
every language on the planet - surely this is the whole point of locale
settings, and locale-dependent functions and constants?

Thanks

Barnaby Scott

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string.uppercase: too many for locale

2007-01-10 Thread Barnaby Scott
Can anyone explain the following: I was getting string.uppercase 
returning an unexpected number of characters, given that the Python Help 
says that it should normally be A-Z. Being locale-dependent, I checked 
that my locale was not set to something exotic, and sure enough it is 
only what I expected - see below:


IDLE 1.1   No Subprocess 
  import locale, string
  locale.getlocale()
['English_United Kingdom', '1252']
  print string.uppercase
ABCDEFGHIJKLMNOPQRSTUVWXYZŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ
  print string.lowercase
abcdefghijklmnopqrstuvwxyzƒšœžßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
 

What am I missing here? Surely for UK English, I really should just be 
getting A-Z and a-z. In case it is relevant, the platform is Windows 2000.

Thanks

Barnaby Scott
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Communicating with Win2000 runas.exe

2006-08-17 Thread Barnaby Scott
I have a problem which I was hoping that Python could solve for me, but
I have become stuck for days now after only 2 lines of code.

My son has a Microsoft game on a shared family computer, which Microsoft
in its infinite wisdom requires you to run as 'administrator'. Call me
old-fashioned but I don't want to promote an 8 year-old to administrator
just so he can run his game!

Enter 'runas.exe'...

However, because we are on Windows 2000, runas does not allow you to
save a password - it has to be entered every time: not much further forward.

So I'm thinking along these lines:

import subprocess
sp = subprocess.Popen(r'C:\WINNT\SYSTEM32\runas.exe /user:administrator
C:\Program Files\Microsoft Games\Age of Mythology\aom.exe')
#some sort of code to send the password here...
#help!

Sure enough, this brings up a prompt asking for the administrator's
password, but I can't get anything to work in terms of getting the 
script to provide the password.

Am I barking up the wrong tree here? Any clues would be gratefully
received. (Even if I do get this to work, my next trick is to hide the
password from any prying eyes looking at the script...)

Thanks

Barnaby Scott


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Communicating with Win2000 runas.exe

2006-08-17 Thread Barnaby Scott
Tim Golden wrote:
 [Barnaby Scott]
 
 | So I'm thinking along these lines:
 | 
 | import subprocess
 | sp = subprocess.Popen(r'C:\WINNT\SYSTEM32\runas.exe 
 | /user:administrator
 | C:\Program Files\Microsoft Games\Age of Mythology\aom.exe')
 | #some sort of code to send the password here...
 | #help!
 
 I *think* -- and I'm happy to be wrong -- that there's
 no way you're going to get that password in there. One
 place to start looking might be:
 
 pywinauto - http://pywinauto.pbwiki.com/
 
 which lets you automate Windows in general; don't know
 how much use it'll be here. 
 
 Alternatively, look into the pywin32 package, and in 
 particular at the win32security functions which let you 
 impersonate another user. They're not trivial to use, 
 but their use has been explained a few times over the 
 years I think. Mostly by Roger Upole who wrote most if 
 not all of the Python bindings.
 
 Here's a post which looks useful; you'll have to hunt
 around for others:
 
 http://groups.google.com/group/comp.lang.python/msg/6bbefb9d4d45d253
 
 I suggest you ask this question again on the main
 python / python-win32 lists; it's a bit too platform-specific
 for the tutor list, I would say.
 
 TJG

Thanks for your tips. In fact the first link you gave put me onto 
Sendkeys (http://www.rutherfurd.net/python/sendkeys/), which is a 
prerequisite for pywinauto. In the end that was all I needed. In case 
anyone else is interested here is my code now (with SendKeys installed):


import subprocess, SendKeys

subprocess.Popen(r'C:\WINNT\system32\runas.exe /user:administrator 
C:\Program Files\Microsoft Games\Age of Mythology\aom.exe')

SendKeys.SendKeys('{PAUSE 1}MyAdministratorPassword{ENTER}')


Worth knowing about - might be quite useful for all sorts of things, 
however 'quick and dirty' it feels as a technique!

Thanks again

BDS
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] database applications with Python - where to start

2005-05-05 Thread Barnaby Scott
Hi, this is one of those difficult questions about where to start!

I want to create a book-keeping/accounting application for my own use
1. because I can't find any that suits me, and
2. because I want to improve and extend my knowledge of Python.

Clearly this is going to be a database application - the trouble is, that
despite reading loads of stuff (including previous posts here on the topic),
I get a Catch-22 feeling that I need to be an expert with 'the big picture'
before I can even take my first stumbling steps towards becoming that
expert! Also the trouble with reading stuff on the web is that you don't
know who is an out-on-a-limb lunatic, and who is talking sense backed up
with concrete experience. And of course, quite understandably, everyone
wants you to use *their* module/driver/database/whatever.

Here's where I am: I have a reasonable grasp of Python (but realise that I
have a lot still to learn). I have written database applications before, but
only using MS Access (both with its own Jet database and with
MSDE/SQL-Server) - no major boasts here, let's just say they *worked*! The
thing is, Access rather pampers you with visual tools for doing many aspects
of the work, and even a neat little environment to manage your code.

Now I want to move on, and use Python, probably with a RDBMS. I haven't
chosen the database - difficult again, because although this will be a small
application, it is accounting data, so its integrity is paramount, and
certain inviolable constraints must be built in at a very fundamental level
(the data needs protection from my code!!). I will also obviously need a UI,
probably a GUI (but it would be nice to keep my options open to do a web UI
version at some point).

So here's the thing. Even though I have quite a clear idea of what the
database schema will look like, and what the UI will *do* (even though I
don't know what it will look like), I'm having real trouble understanding
how/where to start. I'm tempted to try to put together a 'kit' of tools (as
visual as possible) to emulate what I'm used to - but is this a good idea?
and if so, which tools? What on earth is my application's model going to
look like? should I get involved with object-relational mapping? how much
work should I delegate to the RDBMS, and how much logic should I code in
Python? Should I even be thinking radically and ditch the RDBMS in favour of
something like a 'Prevalence Layer' that I have read about? what should
inform these decisions?

I just don't know where to start! A book perhaps, but then, which one? Or
maybe an example app for me to pick apart and learn from?

Sorry it is such a vague question, but any pointers gratefully received.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Code review

2005-01-25 Thread Barnaby Scott
If anyone has the time to look through an entire script, I would would be
very grateful for any comments, tips or suggestions on a wiki-engine script
I am working on.

http://www.waywood.co.uk/cgi-bin/monkeywiki.py (this will download rather
than execute)

It does work, but I have not been using Python very long, and am entirely
self-taught in computer programming of any sort, so I have huge doubts about
my 'style'. I am also aware that I probably don't 'think like a programmer'
(being, in fact a furniture maker!)

I did post a previous version of this about a year(?) ago, and received some
very welcome suggestions, but I have changed it quite a lot since then.
Also, please ignore the licensing stuff - I do intend to make the thing
available like this when I am more confident about it, and I am just getting
a bit ahead of myself: you guys are the first people who know it's there.

Many thanks

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Re: glob or filter help

2005-01-22 Thread Barnaby Scott
For anyone who doesn't like lambda, how about

import os
def get_fles(exts, upd_dir):
return [i for i in os.listdir(upd_dir) if i.split('.')[-1] in exts]



   -Original Message-
   From: [EMAIL PROTECTED]
   [mailto:[EMAIL PROTECTED] 
   Behalf Of Javier
   Ruere
   Sent: 22 January 2005 16:25
   To: tutor@python.org
   Subject: [Tutor] Re: glob or filter help
   
   
   Jay Loden wrote:
Thanks! That's exactly the kind of feedback I was looking for.  If
   it's not
too much trouble, do you think you could explain how 
   lambda works, or
   just
point me towards a lambda explanation/tutorial that a new 
   programmer can
understand?  It seems to give you some great power but I 
   really don't
understand how it works.
   
 Lambda defines anonymous functions. It has some 
   restrictions like that
   the function must be expressed in one line and there can be no
   assigments. The syntax can be found in [1].
 Though I personaly like this keyword, I must say that it 
   can make code
   less readable so I tend to use it for only for extremely simple
   snippets. The example given in the previous mail is almost 
   too big. But
   then again, one must try things out to learn so use it and 
   find your own
   balance.
 Also the BDFL has said he is unhappy with lambda (and 
   filter, reduce
   and map) so he may remove this keyword in the future (but not before
   Python3000)[2].
   
   Javier
   
   [1] http://www.python.org/doc/2.4/ref/lambdas.html
   [2] Look in the user list if you want to learn more about 
   this topics.
   
   ___
   Tutor maillist  -  Tutor@python.org
   http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] class instance with identity crisis

2005-01-12 Thread Barnaby Scott
This is probably a very easy question, and one that I was shocked to find I
was stuck on, having thought I understood classes!

I was wondering how you can get an instance of a class to change itself into
something else (given certain circumstances), but doing so from within a
method. So:

class Damson:
def __str__(self):
return 'damson'

def dry(self):
self = Prune()

class Prune:
def __str__(self):
return 'prune'

weapon = Damson()
weapon.dry()
print weapon

All the old hands will of course know this will produce the output

damson

but something in me suggests it should produce

prune

After all, 'self' refers to the instance 'weapon'.

Obviously one could reassign weapon to a Prune outside the class definition,
but I was hoping to write something in which, given certain circustances
arising, the instance would change itself into something else.

Time to go back to school!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] class instance with identity crisis

2005-01-12 Thread Barnaby Scott
Thanks to all who responded. I see my biggest mistake was not spotting the
assignment of 'self' being local to the method - very dumb indeed,
especially as I have seen this many times before.

However I am glad I was not necessarily dumb to want to do what I was
thinking of.

I am learning lots! Thanks


   -Original Message-
   From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
   Behalf Of Alan Gauld
   Sent: 12 January 2005 20:13
   To: Alan Gauld; Barnaby Scott; 'Tutor'
   Subject: Re: [Tutor] class instance with identity crisis
 
 
   Whoops, I forgot to do an assignment...
 
So try
   
 def dry(self): return Prune()
   
 class Prune:
 def __str__(self):
 return 'prune'

 weapon = Damson()
 weapon.dry()
 
   weapon = weapon.dry()
 
 print weapon
   
Should work as expected...
 
   Alan G
   ___
   Tutor maillist  -  Tutor@python.org
   http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor