Re: integer to binary 0-padded

2011-06-17 Thread John S
On Jun 15, 9:33 am, Olivier LEMAIRE 
wrote:
> You're right, I use Python 2.6.6

This works great in 2.6.5 and later (and probably earlier). You just
have to number your placeholders. The first set of braces formats i
(your value), the second set specifies the field with (i.e., 8):

>>> for i in xrange(10):
... print "{0:0{1}b}".format(i,8)
...

0001
0010
0011
0100
0101
0110
0111
1000
1001
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pattern matching

2011-02-24 Thread John S
On Feb 23, 9:11 pm, monkeys paw  wrote:
> if I have a string such as '01/12/2011' and i want
> to reformat it as '20110112', how do i pull out the components
> of the string and reformat them into a DDMM format?
>
> I have:
>
> import re
>
> test = re.compile('\d\d\/')
> f = open('test.html')  # This file contains the html dates
> for line in f:
>      if test.search(line):
>          # I need to pull the date components here
What you need are parentheses, which capture part of the text you're
matching. Each set of parentheses creates a "group". To get to these
groups, you need the match object which is returned by re.search.
Group 0 is the entire match, group 1 is the contents of the first set
of parentheses, and so forth. If the regex does not match, then
re.search returns None.


DATA FILE (test.html):

David02/19/1967
Susan05/23/1948
Clare09/22/1952
BP08/27/1990
Roger12/19/1954



CODE:
import re
rx_test = re.compile(r'(\d{2})/(\d{2})/(\d{4})')

f = open('test.html')
for line in f:
m = rx_test.search(line)
if m:
new_date = m.group(3) + m.group(1) + m.group(2)
print "raw text: ",m.group(0)
print "new date: ",new_date
print

OUTPUT:
raw text:  02/19/1967
new date:  19670219

raw text:  05/23/1948
new date:  19480523

raw text:  09/22/1952
new date:  19520922

raw text:  08/27/1990
new date:  19900827

raw text:  12/19/1954
new date:  19541219



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


Re: Weird problem matching with REs

2011-05-29 Thread John S
On May 29, 10:35 am, Andrew Berg  wrote:
> On 2011.05.29 09:18 AM, Steven D'Aprano wrote:> >> What makes you think it 
> shouldn't match?
>
> > > AFAIK, dots aren't supposed to match carriage returns or any other
> > > whitespace characters.
>
> I got things mixed up there (was thinking whitespace instead of
> newlines), but I thought dots aren't supposed to match '\r' (carriage
> return). Why is '\r' not considered a newline character?

Dots don't match end-of-line-for-your-current-OS is how I think of
it.

While I almost usually nod my head at Steven D'Aprano's comments, in
this case I have to say that if you just want to grab something from a
chunk of HTML, full-blown HTML parsers are overkill. True, malformed
HTML can throw you off, but they can also throw a parser off.

I could not make your regex work on my Linux box with Python 2.6.

In your case, and because x264 might change their HTML, I suggest the
following code, which works great on my system.YMMV. I changed your
newline matches to use \s and put some capturing parentheses around
the date, so you could grab it.

>>> import urllib2
>>> import re
>>>
>>> content = urllib2.urlopen("http://x264.nl/x264_main.php";).read()
>>>
>>> rx_x264version= 
>>> re.compile(r"http://x264\.nl/x264/64bit/8bit_depth/revision\s*(\d{4})\s*/x264\s*\.exe")
>>>
>>> m = rx_x264version.search(content)
>>> if m:
... print m.group(1)
...
1995
>>>


\s is your friend -- matches space, tab, newline, or carriage return.
\s* says match 0 or more spaces, which is what's needed here in case
the web site decides to *not* put whitespace in the middle of a URL...

As Steven said, when you want match a dot, it needs to be escaped,
although it will work by accident much of the time. Also, be sure to
use a raw string when composing REs, so you don't run into backslash
issues.

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


Re: Weird problem matching with REs

2011-05-29 Thread John S
On May 29, 12:16 pm, Andrew Berg  wrote:
>
> I've been meaning to learn how to use parenthesis groups.
> > Also, be sure to
> > use a raw string when composing REs, so you don't run into backslash
> > issues.
>
> How would I do that when grabbing strings from a config file (via the
> configparser module)? Or rather, if I have a predefined variable
> containing a string, how do change it into a raw string?
When reading the RE from a file it's not an issue. Only literal
strings can be raw. If the data is in a file, the data will not be
parsed by the Python interpreter. This was just a general warning to
anyone working with REs. It didn't apply in this case.

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


Breaking the barrier of a broken paradigm... part 1

2008-03-24 Thread john s.
Hi Everyone,

I hope not to offend you with too many question.. I've been lurking a
long time. I'm not keen on keeping e-mails or data stored in the
public space so this is my first post to the group.

That being said, I'm keen on refining my skills, I hope that I can
prove that I've move on beyond the: check the tut, read the faq..

Now as it's out of the way I hope that my questions can be sited as
examples for others breaking the procedural barrier.

I've seen examples using reduce, map and lamba that cause me to emit a
internal "WTFIZDISHI?"

_

With out any further adiou... IsMyKodeHotOrNot and how can I
improve it... hopefully it will help me with part two...

I'm sure anyone with 2 mins and solid skill of python can ballpark
this one...


_
#/usr/bin/enviro python

#Purpose - a dropped in useracct/pass file is on a new server to build
a cluster... Alas there are no home #directories..  Let me rip through
the passfile, grab the correct field (or build it) and use it to make
the directory!

import os, sys, string, copy, getopt, linecache
from traceback import format_exception

#The file we read in...
fileHandle = "/etc/passwd"
srcFile = open(fileHandle,'r')
srcList = srcFile.readlines()

#yah, a for loop that "iterates" through the file of "lines"
for i in srcList:
strUsr = string.split(i,":")
theUsr = strUsr[0]
usrHome = "/expirt/home/",theUsr,"/"
usrHome = ''.join(usrHome)

print "printing usrHome:",usrHome

print "is it a dir?: ", os.path.isdir(usrHome)

# try to test if it's a dir... for some reason this mis-behaves...
maybe I'm not thinking about it correctly..

if os.path.isdir(usrHome) != 'False':

print "User Home dir doesn't exist creating."

try:
os.makedirs('usrHome' )
except Exception, e:
print e

print "usrHome is: ",usrHome
print "theUsr is: ",theUsr

os.system('/usr/bin/chmod 750 ' + usrHome)
os.system('/usr/bin/chown ' + theUsr + ' ' + usrHome)

#OMG, there is directory that happens to already exist! well, due
diligence and love for the queen dictates we #provide "due
dilligence", (e.g. wipe our asses properly)

else:
print "User Home dir exist, checking and fixing permissions."

print "usrHome is: ",usrHome
print "theUsr is: ",theUsr

os.system('/usr/bin/chmod 750 ' + usrHome)
os.system('/usr/bin/chown ' + theUsr + ' ' + usrHome)

#I know that I can't optimize the line below further... or... can I?

sys.exit("Thanks for using pyDirFixr...")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Breaking the barrier of a broken paradigm... part 1

2008-03-25 Thread john s.
On Mar 24, 9:39 pm, "Ryan Ginstrom" <[EMAIL PROTECTED]> wrote:
> > On Behalf Of john s.
> > import os, sys, string, copy, getopt, linecache
> > from traceback import format_exception
>
> > #The file we read in...
> > fileHandle = "/etc/passwd"
> > srcFile = open(fileHandle,'r')
> > srcList = srcFile.readlines()
>
> > #yah, a for loop that "iterates" through the file of "lines"
> > for i in srcList:
> > strUsr = string.split(i,":")
> > theUsr = strUsr[0]
> > usrHome = "/expirt/home/",theUsr,"/"
> > usrHome = ''.join(usrHome)
>
> How about for starters:
>
> import os
>
> for line in open("/etc/passwd"):
> user, _pwd = line.split(":")
   ^- Ok this one here we are taking a string spliting it
into to variables...
   user gets one (the first) and _pwd gets to hold the
"leftovers"?

> user_home = os.path.join("/expirt/home", user)
>
> > try:
> > os.makedirs('usrHome' )
> > except Exception, e:
> > print e
>
> if os.path.exists(user_home):
^- are(n't) exists and os.path.isadir
interchangable?
*nods in Bryan O. direction*

> print "User Home dir exists, checking and fixing permissions."
> else:
> print "Do other stuff"
>
> Regards,
> Ryan Ginstrom

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


Re: Breaking the barrier of a broken paradigm... part 1

2008-03-25 Thread john s.
On Mar 25, 6:34 am, Bryan Olson <[EMAIL PROTECTED]> wrote:
> john s. wrote:
> > #/usr/bin/enviro python
>
> > #Purpose - a dropped in useracct/pass file is on a new server to build
> > #a cluster... Alas there are no home directories..  Let me rip through
> > #the passfile, grab the correct field (or build it) and use it to make
> > #the directory!
>
> > import os, sys, string, copy, getopt, linecache
> > from traceback import format_exception
>
> > #The file we read in...
> > fileHandle = "/etc/passwd"
> > srcFile = open(fileHandle,'r')
> > srcList = srcFile.readlines()
>
> > #yah, a for loop that "iterates" through the file of "lines"
> > for i in srcList:
>
> Convention is that the name i is for an integer.
just a bit of silly walk on my part in the comment field...
>
> > strUsr = string.split(i,":")
> > theUsr = strUsr[0]
> > usrHome = "/expirt/home/",theUsr,"/"
> > usrHome = ''.join(usrHome)
>
> As Ryan noted, os.path is the favored way.
>
> > print "printing usrHome:",usrHome
> > print "is it a dir?: ", os.path.isdir(usrHome)
> > #try to test if it's a dir... for some reason this mis-behaves...
> > #maybe I'm not thinking about it correctly..
>
> > if os.path.isdir(usrHome) != 'False':
>
> That should always evaluate true. False != 'False'. I think you want:
ok... yes I do

>
>  if not os.path.exists(usrHome):
> > print "User Home dir doesn't exist creating."
> > try:
> > os.makedirs(usrHome)
> > os.system('/usr/bin/chmod 750 ' + usrHome)
> > os.system('/usr/bin/chown ' + theUsr + ' ' + usrHome)
> > except Exception, e:
> > print e
>
> I don't think you want to catch the exception there. If creating the dir
> fails, the next bits of code should not execute.
ok. I see that now.

>
> > #OMG, there is directory that happens to already exist! well,
> > #love for the queen dictates we provide "due
> > #dilligence", (e.g. wipe our asses properly)
>
> The path could exist but not be a directory.

So isadir is slightly better then exists?
It felt like it was not working right...
(probably because I was double testing the exp I see now)

>
> > else:
> > print "User Home dir exist, checking and fixing permissions."
> > sys.exit("Thanks for using pyDirFixr...")
>
> Given the Unixy nature of your code, you probably want to sys.exit(0)
> for success and 1 or 2 for failure.
got it. I'm guessing I'm better off with a last line print "g'bye"
then sys.exit versus trying both at once...

>
> Happy hacking,
> --
> --Bryan
Thanks Bryan :)
-John

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


Re: how to match whole word

2008-07-16 Thread John S
On Jul 16, 9:38 am, Peng Yu <[EMAIL PROTECTED]> wrote:
> On Jul 15, 10:29 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
>
>
>
> > Peng Yu wrote:
> > > Hi,
>
> > > The following code snippet is from /usr/bin/rpl. I would like the it
> > > to match a word, for example, "abc" in ":abc:". But the current one
> > > would not match "abc" in ":abc:". I tried to modify it myself. Would
> > > you please let me know what is the corrected way to do it?
>
> > > Thanks,
> > > Peng
>
> > >if opts.whole_words:
> > >regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + 
> > > r"(?=\s|
> > > $)",
> > >   opts.ignore_case and re.I 
> > > or 0)
> > > --
> > >http://mail.python.org/mailman/listinfo/python-list
>
> > The regular expression "\w+" will match (what might be your definition
> > of) a word, and in particular will match abc in :abc:.   Regular
> > expressions have lots of other special \-sequences that might be worth
> > your while to read about:  http://docs.python.org/lib/re-syntax.html
>
> > Gary Herron
>
> I didn't read the docs and tried the following code.
>
> regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
> opts.ignore_case and re.I or 0)
>
> But I'm not sure why it is not working.
>
> Thanks,
> Peng

Not sure why you picked \A and \Z -- they are only useful if you are
using the re.M flag.
What you want is \b -- match word boundary, on either side of your
word:

regex = re.compile(r"\b" + re.escape(old_str) + r"\b",re.I)

re.I is the same as re.IGNORECASE. More than one option may be OR'ed
together. There's no such thing as "re.O" in Python.  I can understand
where you get the idea, as there is an 'o' modifier for REs in Perl.

To summarize, \A and \Z match the beginning and end of a STRING, while
\b matches the beginning or end of a WORD.

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


Re: trying to match a string

2008-07-18 Thread John S
On Jul 18, 7:51 am, Andrew Freeman <[EMAIL PROTECTED]> wrote:
> Andrew Freeman wrote:
> > oj wrote:
> >> On Jul 18, 12:10 pm, John Machin <[EMAIL PROTECTED]> wrote:
>
> >>> On Jul 18, 9:05 pm, oj <[EMAIL PROTECTED]> wrote:
>
>  On Jul 18, 11:33 am, [EMAIL PROTECTED] wrote:
>
> > Hi,
> > Hi,
> > I am taking a string as an input from the user and it
> > should only
> > contain the chars:L , M or R
> > I tried the folllowing in kodos but they are still not
> > perfect:
> > [^A-K,^N-Q,^S-Z,^0-9]
> > [L][M][R]
> > [LRM]?L?[LRM]? etc but they do not exactly meet what I need.
> > For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N'
> > .like that.
> > regards,
> > SZ
> > The string may or may not have all the three chars.
>
>  With regular expressions, [^LRM] matches a character that isn't L, R
>  or M. So:
>    import re
>    var = "LRLRLRLNR"
>    if re.search(r'[^LRM]', var):
>  print "Invalid"
>
> >>> Fails if var refers to the empty string.
>
> >> No it doesn't, it succeeds if var is an empty string. An empty string
> >> doesn't contain characters that are not L, R or M.
>
> >> The OP doesn't specify whether an empty string is valid or not. My
> >> interpretation was that an empty string would be valid.
>
> > Why not just use * instead of + like:
>
> > if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
> > string; $ means end of string
> >print "Invalid"
>
> > This will *only* print invalid when there is a character other than L,
> > R, or M or a empty string.
>
> Sorry, forget the beginning and ending markers, I just tried it out, it
> doesn't work.
> use this instead:
>
> if re.search(r'[^LRM]*', var):
>print "Invalid"

This won't work -- every string in the universe contains 0 or more
characters which are not 'L', 'R', or 'M'. That is, the regular
expression X* could match the empty string, which can be found in all
strings.
--
http://mail.python.org/mailman/listinfo/python-list


Re: finding out the number of rows in a CSV file [Resolved]

2008-08-27 Thread John S
[OP] Jon Clements wrote:
> On Aug 27, 12:54 pm, SimonPalmer <[EMAIL PROTECTED]> wrote:
>> after reading the file throughthe csv.reader for the length I cannot
>> iterate over the rows.  How do I reset the row iterator?

A CSV file is just a text file. Don't use csv.reader for counting rows
-- it's overkill. You can just read the file normally, counting lines
(lines == rows).

This is similar to what Jon Clements said, but you don't need the csv
module.

num_rows = sum(1 for line in open("myfile.csv"))

As other posters have said, there is no free lunch. When you use
csv.reader, it reads the lines, so once it's finished you're at the
end of the file.

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


Re: Replace and inserting strings within .txt files with the use of regex

2010-08-07 Thread John S
On Aug 7, 8:20 pm, Νίκος  wrote:
> Hello dear Pythoneers,
>
> I have over 500 .php web pages in various subfolders under 'data'
> folder that i have to rename to .html and and ditch the ''
> tages from within and also insert a very first line of 
> where id must be an identification unique number of every page for
> counter tracking purposes. ONly pure html code must be left.
>
> I before find otu Python used php and now iam switching to templates +
> python solution so i ahve to change each and every page.
>
> I don't know how to handle such a big data replacing problem and
> cannot play with fire because those 500 pages are my cleints pages and
> data of those filesjust cannot be messes up.
>
> Can you provide to me a script please that is able of performing an
> automatic way of such a page content replacing?
>
> Thanks a million!

If the 500 web pages are PHP only in the sense that there is only one
pair of  tags in each file, surrounding the entire content, then
what you ask for is doable.

from os.path import join
import os

id = 1  # id number
for currdir,files,dirs in os.walk('data'):
for f in files:
if f.endswith('php'):
source_file_name = join(currdir,f)# get abs path to
filename
source_file = open(source_file_name)
source_contents = source_file.read()  # read contents of
PHP file
source_file.close()

# replace tags
source_contents = source_contents.replace('<%','')
source_contents = source_contents.replace('%>','')

# add ID
source_contents = ( '' % id ) + source_contents
id += 1

# create new file with .html extension
source_file_name =
source_file_name.replace('.php','.html')
dest_file = open(source_file_name,'w')
dest_file.write(source_contents)  # write contents
dest_file.close()

Note: error checking left out for clarity.

On the other hand, if your 500 web pages contain embedded PHP
variables or logic, you have a big job ahead. Django templates and PHP
are two different languages for embedding data and logic in web pages.
Converting a project from PHP to Django involves more than renaming
the template files and deleting "';
}
?>

In Django, you would typically put this logic in a Django *view*
(which btw is not what is called a 'view' in MVC term), which is the
code that prepares data for the template. The logic would not live
with the HTML. The template uses "template variables" that the view
has associated with a Python variable or function. You might create a
template variable (created via a Context object) named 'browser' that
contains a value that identifies the browser.

Thus, your Python template (HTML file) might look like this:

{% if browser == 'IE' %}You are using Internet Explorer{% endif %}

PHP tends to combine the presentation with the business logic, or in
MVC terms, combines the view with the controller. Django separates
them out, which many people find to be a better way. The person who
writes the HTML doesn't have to speak Python, but only know the names
of template variables and a little bit of template logic. In PHP, the
HTML code and all the business logic lives in the same files. Even
here, it would probably make sense to calculate the browser ID in the
header of the HTML file, then access it via a variable in the body.

If you have 500 static web pages that are part of the same
application, but that do not contain any logic, your application might
need to be redesigned.

Also, you are doing your changes on a COPY of the application on a non-
public server, aren't you? If not, then you really are playing with
fire.


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


Re: Replace and inserting strings within .txt files with the use of regex

2010-08-07 Thread John S
Even though I just replied above, in reading over the OP's message, I
think the OP might be asking:

"How can I use RE string replacement to find PHP tags and convert them
to Django template tags?"

Instead of saying

source_contents = source_contents.replace(...)

say this instead:

import re


def replace_php_tags(m):
   ''' PHP tag replacer
   This function is called for each PHP tag. It gets a Match object as
   its parameter, so you can get the contents of the old tag, and
should
   return the new (Django) tag.
   '''

   # m is the match object from the current match
   php_guts = m.group(1)  # the contents of the PHP tag

   # now put the replacement logic here

   # and return whatever should go in place of the PHP tag,
   # which could be '{{ python_template_var }}'
   # or '{% template logic ... %}
   # or some combination

source_contents = re.sub('',replace_php_tags,source_contents)






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


Re: Replace and inserting strings within .txt files with the use of regex

2010-08-08 Thread John S
On Aug 8, 10:59 am, Thomas Jollans  wrote:
> On 08/08/2010 04:06 PM, Νίκος wrote:
>
>
>
>
>
> > On 8 Αύγ, 15:40, Thomas Jollans  wrote:
> >> On 08/08/2010 01:41 PM, Νίκος wrote:
>
> >>> I was so dizzy and confused yesterday that i forgot to metnion that
> >>> not only i need removal of php openign and closing tags but whaevers
> >>> data lurks inside those tags as well ebcause now with the 'counter.py'
> >>> script i wrote the html fiels would open ftm there and substitute the
> >>> tempalte variabels like %(counter)d
>
> >> I could just hand you a solution, but I'll be a bit of a bastard and
> >> just give you some hints.
>
> >> You could use regular expressions. If you know regular expressions, it's
> >> relatively trivial - but I doubt you know regexp.
>
> > Here is the code with some try-and-fail modification i made, still non-
> > working based on your hints:
> > ==
>
> > id = 0  # unique page_id
>
> > for currdir, files, dirs in os.walk('varsa'):
>
> >     for f in files:
>
> >         if f.endswith('php'):
>
> >             # get abs path to filename
> >             src_f = join(currdir, f)
>
> >             # open php src file
> >             print 'reading from %s' % src_f
> >             f = open(src_f, 'r')
> >             src_data = f.read()         # read contents of PHP file
> >             f.close()
>
> >             # replace tags
> >             print 'replacing php tags and contents within'
> >             src_data = src_data.replace(r'', '')             #
> > the dot matches any character i hope! no matter how many of them?!?
>
> Two problems here:
>
> str.replace doesn't use regular expressions. You'll have to use the re
> module to use regexps. (the re.sub function to be precise)
>
> '.'  matches a single character. Any character, but only one.
> '.*' matches as many characters as possible. This is not what you want,
> since it will match everything between the *first* .
> You want non-greedy matching.
>
> '.*?' is the same thing, without the greed.
>
>
>
> >             # add ID
> >             print 'adding unique page_id'
> >             src_data = ( '' % id ) + src_data
> >             id += 1
>
> >             # add template variables
> >             print 'adding counter template variable'
> >             src_data = src_data + '''  Αριθμός
> > Επισκεπτών: %(counter)d  '''
> >             # i can think of this but the above line must be above  > body> NOT after but how to right that?!?
>
> You will have to find the  tag before inserting the string.
> str.find should help -- or you could use str.replace and replace the
>  tag with you counter line, plus a new .
>
>
>
> >             # rename old php file to new with .html extension
> >             src_file = src_file.replace('.php', '.html')
>
> >             # open newly created html file for inserting data
> >             print 'writing to %s' % dest_f
> >             dest_f = open(src_f, 'w')
> >             dest_f.write(src_data)      # write contents
> >             dest_f.close()
>
> > This is the best i can do.
>
> No it's not. You're just giving up too soon.

When replacing text in an HTML document with re.sub, you want to use
the re.S (singleline) option; otherwise your pattern won't match when
the opening tag is on one line and the closing is on another.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need help in Python regular expression

2009-06-11 Thread John S
On Jun 11, 10:30 pm, meryl  wrote:
> Hi,
>
> I have this regular expression
> blockRE = re.compile(".*RenderBlock {\w+}")
>
> it works if my source is "RenderBlock {CENTER}".
>
> But I want it to work with
> 1. RenderTable {TABLE}
>
> So i change the regexp to re.compile(".*Render[Block|Table] {\w+}"),
> but that breaks everything
>
> 2. RenderBlock (CENTER)
>
> So I change the regexp to re.compile(".*RenderBlock {|\(\w+}|\)"),
> that also breaks everything
>
> Can you please tell me how to change my reg exp so that I can support
> all 3 cases:
> RenderTable {TABLE}
> RenderBlock (CENTER)
> RenderBlock {CENTER}
>
> Thank you.

Short answer:

r = re.compile(r"Render(?:Block|Table)\s+[({](?:TABLE|CENTER)[})]")

s = """
blah blah blah
blah blah blah RenderBlock {CENTER} blah blah RenderBlock {CENTER}
blah blah blah RenderTable {TABLE} blah blah RenderBlock (CENTER)
blah blah blah
"""

print r.findall(s)



output:
['RenderBlock {CENTER}', 'RenderBlock {CENTER}', 'RenderTable
{TABLE}', 'RenderBlock (CENTER)']



Note that [] only encloses characters, not strings; [foo|bar] matches
'f','o','|','b','a', or 'r', not "foo" or "bar".
Use (foo|bar) to match "foo" or "bar"; (?xxx) matches xxx without
making a backreference (i.e., without capturing text).

HTH

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


Re: Lexical scope: converting Perl to Python

2009-06-13 Thread John S
On Jun 13, 8:29 am, Nick Craig-Wood  wrote:
> Andrew Savige  wrote:
>
> >  I'd like to convert the following Perl code to Python:
>
> >   use strict;
> >   {
> >     my %private_hash = ( A=>42, B=>69 );
> >     sub public_fn {
> >   my $param = shift;
> >   return $private_hash{$param};
> >     }
> >   }
> >   print public_fn("A");    # good:  prints 42
> >   my $x = $private_hash{"A"};  # error: good, hash not in scope
>
> >  The real code is more complex; the above is a simplified example.
>
> >  Notice that this code uses Perl's lexical scope to hide the
> >  %private_hash variable, but not the public_fn() function.
>
> >  While I could convert this code to the following Python code:
>
> >   private_hash = dict( A=42, B=69 )
> >   def public_fn(param):
> >     return private_hash[param]
> >   print public_fn("A") # good:  prints 42
> >   x = private_hash["A"]    # works: oops, hash is in scope
>
> >  I'm not happy with that because I'd like to limit the scope of the
> >  private_hash variable so that it is known only inside public_fn.
>
> >  Of course, I could hide the hash like so:
>
> >   def public_fn(param):
> >     private_hash = dict( A=42, B=69 )
> >     return private_hash[param]
>
> >  yet I'm not happy with that either because of the repeated
> >  initialization the hash each time the function is called.
>
> >  What is the Pythonic equivalent of Perl's lexical scope, as
> >  illustrated by the code snippet above?
>
> Either
>
> _private_hash = dict( A=42, B=69)
>
> def public_fn(param):
>     return _private_hash[param]
>
> Or
>
> def public_fn(param, _private_hash = dict( A=42, B=69)):
>     return _private_hash[param]
>
> Is probably the pythonic equivalents.  Note that private_hash starts
> with an underscore which means it won't be exported from a module by
> default and it is a convention that it is private and shouldn't be
> fiddled with.  I'd probably go with the latter of the two examples.
>
> --
> Nick Craig-Wood  --http://www.craig-wood.com/nick

Another approach is to just use a class to hold the data, and a class
method (AKA classmethod) to access the data:

# class definition
class my_util(object):
_private_hash = { 'A':42,'B':69 }

@classmethod
def public_fn(cls,index):
return cls._private_hash[index]

# usage
print my_util.public_fn('A')
print my_util.public_fn('B')

This keeps pretty close to the OP's intention. It does require you to
use the class name, but that's.OK, and better than an anonymous
block IMHO.

Note: I'm a recovering Perl hacker.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to insert string in each match using RegEx iterator

2009-06-13 Thread John S
On Jun 10, 12:13 am, "504cr...@gmail.com" <504cr...@gmail.com> wrote:
> By what method would a string be inserted at each instance of a RegEx
> match?
>
> For example:
>
> string = '123 abc 456 def 789 ghi'
> newstring = ' INSERT 123 abc INSERT 456 def INSERT 789 ghi'
>
> Here's the code I started with:
>
> >>> rePatt = re.compile('\d+\s')
> >>> iterator = rePatt.finditer(string)
> >>> count = 0
> >>> for match in iterator:
>
>         if count < 1:
>                 print string[0:match.start()] + ' INSERT ' + 
> string[match.start
> ():match.end()]
>         elif count >= 1:
>                 print ' INSERT ' + string[match.start():match.end()]
>         count = count + 1
>
> My code returns an empty string.
>
> I'm new to Python, but I'm finding it really enjoyable (with the
> exception of this challenging puzzle).
>
> Thanks in advance.

I like using a *callback* function instead of *plain text* with the
re.sub() method. To do this, call the sub() function in the normal
way, but instead of specifying a string as the replacement, specify a
function. This function expects the same match object returned by
re.search() or re.match(). The text matched by your RE is replaced by
the return value of the function. This gives you a lot of flexibility;
you can use the matched text to look up values in files or databases,
or online, for instance, and you can do any sort of text manipulation
desired.

8<---
import re

# original string
oldstring = '123 abc 456 def 789 ghi'

# RE to match a sequence of 1 or more digits
rx_digits = re.compile(r"\d+")

# callback function -- expects a Match object, returns the replacement
string
def repl_func(m):
return 'INSERT ' + m.group(0)

# do the substitution
newstring =  rx_digits.sub(repl_func,oldstring)

print "OLD:",oldstring
print "NEW:",newstring
-
Output:
OLD: 123 abc 456 def 789 ghi
NEW: INSERT 123 abc INSERT 456 def INSERT 789 ghi


You could also do it with a lambda function if you didn't want to
write a separate function:
newstring =  rx_digits.sub(lambda m: 'INSERT ' + m.group(0),oldstring)

I understand that for this simple case, '
'INSERT ' + \1
is sufficient, and a callback is overkill; I wanted to show the OP a
more generic approach to complex substitutions.

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


Problem installing 3.2.1 on a Linux server

2011-08-06 Thread John S James
My ISP (Bluehost) does not yet support Python version 3, but gave me shell
access (no sudo) so that I can install Python 3 myself on my server account
(running Apache). So I uploaded the standard Python.org installation package
(Python 3.2.1 for Linux) and followed instructions, starting with
./configure. These instructions had worked for me before, to install Python3
on a Linux netbook.

But on the server, ./configure starts fine, but later stops with an error.
Here is the section of the output where the error occurred:
*checking for build directories... done*
*configure: creating ./config.status*
*config.status: creating Makefile.pre*
*config.status: creating Modules/Setup.config*
*config.status: error: cannot find input file: `Misc/python.pc.in'*

Then the ./configure step stopped. Needless to say, the next step, Make, did
not work because there was no Makefile.

Others have received the same message, in different contexts. But I haven't
found a solution.

Two action items:

* For the Python community, it would help to fix this problem in the
standard Python distribution, if possible. People do want to run Python3 on
more servers. I could help by testing the fix to see if the Python3 install
now works.

* Meanwhile I'd appreciate any advice on a temporary fix (such as changing
the configure file, or where to find a package that I could install on the
server before installing Python3). All I need for now is the most basic
installation, to support CGI and Web forms.

John

-- 
John S. James
www.RepliCounts.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem installing 3.2.1 on a Linux server

2011-08-14 Thread John S James
Thanks, your reply helped. The problem seems to be the autoconfig -- I was
using version 2.59 instead of 2.68. However, the host will not let users
install 2.68.

So I'm sending them feedback, suggesting a number of reasons for supporting
3.2 for their server accounts, including:

* Popularity of Python, TIOBE recognition;
* Wide consensus that new Python programmers should learn version 3, unless
they must maintain legacy software;
* 3.2 will be around for at least another year before 3.3 is released;
* Lack of 3.2 in low-cost hosts (3.1 is available) -- a competitive
advantage;
* 3.2 improves future library compatibility, vs. 3.1;
* Installing 3.2 should not affect any existing customers, unless they
choose to use it.

We should explore why hosting services are resistant to supporting the
current version of Python (plus an important older version, 2.7). Other
supported versions could be deprecated -- and eventually removed by the
ISPs, except for the few customers who choose to install it themselves.

John S James
www.replicounts.org



-- Forwarded message --
From: Ned Deily 
To: python-list@python.org
Date: Sat, 06 Aug 2011 15:09:39 -0700
Subject: Re: Problem installing 3.2.1 on a Linux server
In article
,
 John S James  wrote:
> My ISP (Bluehost) does not yet support Python version 3, but gave me shell
> access (no sudo) so that I can install Python 3 myself on my server
account
> (running Apache). So I uploaded the standard Python.org installation
package
> (Python 3.2.1 for Linux) and followed instructions, starting with
> ./configure. These instructions had worked for me before, to install
Python3
> on a Linux netbook.
>
> But on the server, ./configure starts fine, but later stops with an error.
> Here is the section of the output where the error occurred:
> *checking for build directories... done*
> *configure: creating ./config.status*
> *config.status: creating Makefile.pre*
> *config.status: creating Modules/Setup.config*
> *config.status: error: cannot find input file: `Misc/python.pc.in'*
>
> Then the ./configure step stopped. Needless to say, the next step, Make,
did
> not work because there was no Makefile.

Works for me on a current Debian system.  Are you downloading the source
from python.org?  Did you verify that the file is created by tar?

$ curl -O http://www.python.org/ftp/python/3.2.1/Python-3.2.1.tgz
$ tar xzf Python-3.2.1.tgz
$ cd ./Python-3.2.1/
$ ls -l Misc/python.pc.in
-rw-r- 1 nad nad 293 Jul  8 23:58 Misc/python.pc.in
$ ./configure
[...]
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

If you are still having problems, chances are you have an out-of-date or
incomplete version of GNU autotools which contains the macros and tools
that are used by the ./configure script.  On my system:

$ autoconf --version
autoconf (GNU Autoconf) 2.68

--
 Ned Deily,
 n...@acm.org


-- 
John S. James
www.aidsnews.org
www.RepliCounts.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Installed 3.5.0 successfully on Windows 10, but where is DDLs, Doc, Lib, etc?

2015-10-14 Thread John S. James
I installed 3.5.0 today and it's working fine -- either from the command 
prompt, or running a .py script.

But the Python 3.4 that was previously installed on the computer had a Python34 
folder, which contained DDLs, Doc, include, Lib, and various other folders and 
files. I haven't found a comparable Python35 folder anywhere. I'd like to find 
the 3.5 Doc folder at least.

I looked for the installation directory using the command prompt, but at 
c:\Users\(my name)\ there is no AppData.

Where can I find that folder? Or can I just ignore it for now (and get the 
documentation elsewhere)?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Installed 3.5.0 successfully on Windows 10, but where is DDLs, Doc, Lib, etc?

2015-10-15 Thread John S James
Thank you, this is very helpful.

John

On Wed, Oct 14, 2015 at 3:31 PM, Zachary Ware  wrote:

> On Wed, Oct 14, 2015 at 2:05 PM, John S. James 
> wrote:
> > I installed 3.5.0 today and it's working fine -- either from the command
> prompt, or running a .py script.
> >
> > But the Python 3.4 that was previously installed on the computer had a
> Python34 folder, which contained DDLs, Doc, include, Lib, and various other
> folders and files. I haven't found a comparable Python35 folder anywhere.
> I'd like to find the 3.5 Doc folder at least.
> >
> > I looked for the installation directory using the command prompt, but at
> c:\Users\(my name)\ there is no AppData.
> >
> > Where can I find that folder? Or can I just ignore it for now (and get
> the documentation elsewhere)?
>
> Python 3.5 changed the default install directory on Windows to better
> fit in with other Windows software and to alleviate security concerns
> (C:\Python34, for example, is world-writable, whereas C:\Program
> Files\Python 3.5\, which is the new default all-users install
> location, can only be written to by administrators).  True per-user
> installs are now also possible, and install to your user directory.
>
> You can find where Python is installed using Python itself: try `py
> -3.5 -c "import sys, os;os.system('explorer ' + sys.prefix)"` at the
> Command Prompt, which uses the Python Launcher for Windows to start
> Python 3.5 and execute a command to start a Windows Explorer instance
> in the directory containing Python.
>
> By the way, C:\Users\(your name)\AppData does exist, but is hidden by
> default.  It will tab-complete, though; at Command Prompt do `dir
> C:\Users\(your name)\App`.
>
> You can also get always-up-to-date documentation from
> https://docs.python.org/3.5/.  There's also a download page at
> https://docs.python.org/3.5/download.html if you prefer a local copy
> of one of the various formats available there.
>
> Hope this helps,
> --
> Zach
>



-- 
John S. James
www.AgeTreatmentNews.org <http://www.agetreatmentnews.org/> -  Biomedical
research to slow or reverse aging; also on Twitter at AgeTreatment
<http://twitter.com/agetreatment>
Other projects: www.affinity99.orgwww.MassSponsorship.org
<http://www.masssponsorship.org/>
-- 
https://mail.python.org/mailman/listinfo/python-list