Re: [Tutor] Iterate Suggestion

2012-04-15 Thread Peter Otten
Tom Tucker wrote:

> Hello all.  Any suggestions how I could easily iterate over a list and
> print the output 3 across (when possible)?  One method I was considering
> was removing the recently printed item from the list, checking list
> length,
> etc.  Based on the remaining length of the list I would then print X
> across. Yah? Is their and easier approach I might be overlooking?
> 
> 
> For example...
> 
> mylist = ['serverA', 'serverB', 'serverC', 'serverD',' serverE',
> 'serverF', 'serverG']
> 
> 
> Desired Output
> 
> serverA  serverB  serverC
> serverD  serverE  serverF
> serverG

Here's another approach that works with arbitrary iterables, not just lists:

>>> from itertools import islice
>>> def chunks(items, n, rowtype=tuple):
... items = iter(items)
... while True:
... row = rowtype(islice(items, n))
... if not row: break
... yield row
... 
>>> for row in chunks(range(7), 3):
... print row
... 
(0, 1, 2)
(3, 4, 5)
(6,)
>>> mylist = ["server" + c for c in "ABCDEFG"]
>>> for row in chunks(mylist, 3, " ".join):
... print row
... 
serverA serverB serverC
serverD serverE serverF
serverG


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


Re: [Tutor] Help with regular expression

2012-04-15 Thread syed zaidi


Thanks for the help
I need the whole line starting from 'D' but in seperate columns.like KO, EC, 
Gene ID, Enzyme Name etc

> Date: Mon, 16 Apr 2012 00:24:17 +1000
> From: st...@pearwood.info
> To: tutor@python.org
> Subject: Re: [Tutor] Help with regular expression
> 
> syed zaidi wrote:
> > Dear Steve,Tutor doesn't allow attachment of huge files. I am attaching
> > the files I am taking as input, code and the output CSV file. I hope then
> > you would be able to help me. DOT keg files open in file viewer, you can
> > also view them in python. The CSV file is the desired output file.
> 
> 
> There is no need to send four files when one will do. Also no need to send a 
> file with multiple thousands of lines long when a dozen or so lines should be 
> sufficient.
> 
> It would also help if you told us what the fields in the file should be 
> called. You are probably familiar with them, but we aren't.
> 
> Since I don't know what the fields are called, I'm going to just make up some 
> names.
> 
> def parse_d_line(line):
>  # Expects a line like this:
>  # DSBG_0147 aceE; xxx xxx\tK00163 xxx xxx [EC:1.2.4.1]
>  a, b = line.split('\t')  # split on tab character
>  c, d = a.split(';')
>  letter, sbg_code, other_code = c.split()
>  compound1 = d.strip()
>  words = b.split()
>  k_code = words[0]
>  ec = words[-1]
>  compound2 = " ".join(words[1:-1])
>  return (letter, sbg_code, other_code, compound1, k_code, compound2, ec)
> 
> 
> kegfile = open('something.keg')
> # skip lines until a bare exclamation mark
> for line in kegfile:
>  if line.strip() == '!':
>  break
> 
> # analyse D lines only, skipping all others
> for line in kegfile:
>  if line.startswith('D'):
>  print(parse_d_line(dline))
>  elif line.strip() == '!':
>  break  # stop processing
> 
> 
> You will notice I don't use regular expressions in this.
> 
>  Some people, when confronted with a problem, think "I know,
>  I'll use regular expressions." Now they have two problems.
>  -- Jamie Zawinski
> 
> 
> 
> 
> -- 
> Steven
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Iterate Suggestion

2012-04-15 Thread bob gailer

On 4/14/2012 11:27 AM, Tom Tucker wrote:


Hello all.  Any suggestions how I could easily iterate over a list and 
print the output 3 across (when possible)?  One method I was 
considering was removing the recently printed item from the list, 
checking list length, etc.  Based on the remaining length of the list 
I would then print X across. Yah? Is their and easier approach I might 
be overlooking?



For example...

mylist = ['serverA', 'serverB', 'serverC', 'serverD',' serverE', 
'serverF', 'serverG']



Desired Output

serverA  serverB  serverC
serverD  serverE  serverF
serverG


print '\n'.join(' '.join(mylist[i:i+3]) for i in range(0,len(mylist),3))

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] re.search() help

2012-04-15 Thread Walter Prins
Hi Michael,

On 16 April 2012 01:20, Michael Lewis  wrote:

> So that you can find the section of a long string that
>> first matches your regex.
>>
>>
> Why not use re.findall() for that? It seems like re.findall() can fill
> this need and I wouldn't need to use re.search()? Can you compare an
> example circumstance where one would be better suited than the other?
>

To add to what Alan's said:  Regular expressions is a textual specification
language that is often used in the specification of tokens, e.g. when
designing (for example) some type of computer language, or perhaps a
preprocessor for a computer language, the tokens themselves will typically
be specified using regular expressions (or something similar), while the
grammar of the language will typically be expressed using something like
BNF or more typically EBNF (which stands for Extended Backus-Naur Form,
after the creators of the syntax.)

Anyway, so as an example then of where you would not use re.findall() (or
indeed re.search()), in terms of compilers  you typically have a scanner
and parser component where the scanner has the job of taking the input text
which can be seen as a sequence of text characters and and converting it
into a sequence of tokens, and this tokenization process may well involve
the use of regular expressions, where it only makes sense to match only at
the beginning of the text being tokenized/scanned.  So in such a context
you'd definitely not even want to use re.search() or re.findall() but
rather would probably use re.match() since you're expressly trying to
recognize the next "word" (token) from the text being scanned (and
ultimately, parsed.)

Another example: Suppose you're implementing a preprocessor for a
programming language, and you therefore want to ignore most of the actual
programming language text (since you're really only interested in the
pre-processor language that interspersed with the "normal" programming
language.)  In such a scenario the first (or next) pre-processor language
token will likely not be at the beginning of your current program text
block, so in such a case re.match() or re.findall() would not be helpful
since you want to then find the first token in the text, which may or may
not be at the beginning of the string. In such a case you'd therefore like
to use re.search().

HTH,

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


Re: [Tutor] re.search() help

2012-04-15 Thread Alan Gauld

On 16/04/12 00:20, Michael Lewis wrote:


 > What is the purpose of this?

So that you can find the section of a long string that
first matches your regex.


Why not use re.findall() for that? It seems like re.findall() can fill
this need and I wouldn't need to use re.search()? Can you compare an
example circumstance where one would be better suited than the other?


Performance.
It's faster to find just the first occurrence.
Especially if the string is long - say several million characters.

If you only need the first one why search for all the rest?


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] re.search() help

2012-04-15 Thread Michael Lewis
>
> Message: 6
> Date: Sun, 15 Apr 2012 08:48:10 +0100
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] re.search() help
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 15/04/12 08:10, Michael Lewis wrote:
> > Hi everyone,
> >
> > I am a bit confused on how one would ever use re.search(). It
> > essentially tells me the location on (RAM?) if the pattern matches?
>
> No it returns a MatchObject instance.
> You can then perform various operations on the
> MatchObject to, for example find the substring
> which actually matched.
>
>  > What is the purpose of this?
>
> So that you can find the section of a long string that
> first matches your regex.
>

Why not use re.findall() for that? It seems like re.findall() can fill this
need and I wouldn't need to use re.search()? Can you compare an example
circumstance where one would be better suited than the other?

>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>

-- 
Michael J. Lewis
mjole...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with regular expression

2012-04-15 Thread Steven D'Aprano

syed zaidi wrote:

Dear Steve,Tutor doesn't allow attachment of huge files. I am attaching
the files I am taking as input, code and the output CSV file. I hope then
you would be able to help me. DOT keg files open in file viewer, you can
also view them in python. The CSV file is the desired output file.



There is no need to send four files when one will do. Also no need to send a 
file with multiple thousands of lines long when a dozen or so lines should be 
sufficient.


It would also help if you told us what the fields in the file should be 
called. You are probably familiar with them, but we aren't.


Since I don't know what the fields are called, I'm going to just make up some 
names.


def parse_d_line(line):
# Expects a line like this:
# DSBG_0147 aceE; xxx xxx\tK00163 xxx xxx [EC:1.2.4.1]
a, b = line.split('\t')  # split on tab character
c, d = a.split(';')
letter, sbg_code, other_code = c.split()
compound1 = d.strip()
words = b.split()
k_code = words[0]
ec = words[-1]
compound2 = " ".join(words[1:-1])
return (letter, sbg_code, other_code, compound1, k_code, compound2, ec)


kegfile = open('something.keg')
# skip lines until a bare exclamation mark
for line in kegfile:
if line.strip() == '!':
break

# analyse D lines only, skipping all others
for line in kegfile:
if line.startswith('D'):
print(parse_d_line(dline))
elif line.strip() == '!':
break  # stop processing


You will notice I don't use regular expressions in this.

Some people, when confronted with a problem, think "I know,
I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski




--
Steven

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


Re: [Tutor] Problem with mechanize and forms

2012-04-15 Thread Andreas Perstinger

On 2012-04-14 17:46, Karim Gorjux wrote:

But I can't get any of these forms! I need the first form so I tried

br.select_form(nr=0)

but I get None!


With "br.select_form()" you set the current form which is accessible 
through the "br.form" attribute. The method itself doesnt't return 
anything and thus you get "None" if you do "print br.select_form(nr=0)".


For further processing you have to work with "br.form" and its methods. 
See also help(br.select_form) and help(br.form).


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


Re: [Tutor] Python Fails to Change Directory

2012-04-15 Thread Steven Buroff
Just to support Andrew, I've got several scripts that use os.chdir
and they all run fine with 3.2.3.

Steve

> -Original Message-
> From: tutor-bounces+sburoff=optonline@python.org [mailto:tutor-
> bounces+sburoff=optonline@python.org] On Behalf Of Steven D'Aprano
> Sent: Sunday, April 15, 2012 1:32 AM
> To: tutor@python.org
> Subject: Re: [Tutor] Python Fails to Change Directory
> 
> Andrew Jahn wrote:
> > Hi all,
> >
> > I am attempting to use a Python program to change into a specified
> > directory before executing some commands. However, when I call the
> > Python program from my Unix shell (tcsh) using a command such as
> >
> > "python myprogram.py"
> >
> > It runs without changing directory. Just to clarify, the lines of code
> > in question are the following:
> >
> > import os
> > MyDir = "/usr/local/myDir"
> > os.system("cd "+myDir)
> 
> That's not the code you are running, because it gives a NameError. Please
> copy and paste any code snippets you give, don't retype them (especially
> not from
> memory!) since you will likely introduce errors. The above error is
trivial
> to fix, but who knows what other errors you have introduced?
> 
> In any case, os.system can't help you, because that starts a new external
> process, it doesn't change the directory of the current process (your
> Python script).
> 
> 
> > I have also tried "os.chdir(MyDir)", but that doesn't work either - it
> > just runs without actually changing directory.
> 
> I find that very hard to believe. Can you explain why you think it doesn't
> change directory? Try this:
> 
> 
> import os
> print os.getcwd()
> os.chdir("/usr/local/myDir")
> print os.getcwd()
> 
> What does it print?
> 
> 
> 
> 
> --
> Steven
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Python Fails to Change Directory

2012-04-15 Thread Dave Angel
On 04/15/2012 01:32 AM, Steven D'Aprano wrote:
> Andrew Jahn wrote:
>> Hi all,
>>
>> I am attempting to use a Python program to change into a specified
>> directory before executing some commands. However, when I call the
>> Python
>> program from my Unix shell (tcsh) using a command such as
>>
>> "python myprogram.py"
>>
>> It runs without changing directory. Just to clarify, the lines of
>> code in
>> question are the following:
>>
>> import os
>> MyDir = "/usr/local/myDir"
>> os.system("cd "+myDir)
>
> That's not the code you are running, because it gives a NameError.
> Please copy and paste any code snippets you give, don't retype them
> (especially not from memory!) since you will likely introduce errors.
> The above error is trivial to fix, but who knows what other errors you
> have introduced?
>
> In any case, os.system can't help you, because that starts a new
> external process, it doesn't change the directory of the current
> process (your Python script).
>
>

I interpreted Andrew's description somewhat differently.  He's executing
a Python program before executing some other commands.  So those other
commands are shell commands, not Python lines.  In that case, Python has
nothing to do with the problem.

Andrew, are you trying to do this sort of thing ?

davea@think:~/tmp$ python program.py
davea@think:~/tmp$ myother
davea@think:~/tmp$ programs
davea@think:~/tmp$

where 'myother' and 'programs' are other "commands" or (programs and
shell scripts)?

In that case, nothing that your python program can normally do can alter
either the cwd or the environment.  The shell explicitly creates a new
environment for the python program, and tosses it away when it exits.

You can demonstrate it for yourself, easily enough.  The following   in
my (bash) environment.  With an executable file called change.sh, as
follows:

davea@think:~/tmp$ cat change.sh
#!/bin/bash
cd other
pwd
davea@think:~/tmp$ ./change.sh
/home/davea/tmp/other
davea@think:~/tmp$ pwd
/home/davea/tmp

( Notice the pwd of the shell did NOT change. )

davea@think:~/tmp$ source ./change.sh
/home/davea/tmp/other
davea@think:~/tmp/other$ pwd
/home/davea/tmp/other
davea@think:~/tmp/other$

Unfortunately, I don't know of any workaround for this.  The obvious
extension would be:

davea@think:~/tmp$ source python myprogram.py
bash: source: /usr/bin/python: cannot execute binary file

When I've had this type of problem I've resorted to creating a text file
from the Python script, and doing a chmod +x on that file and source'ing
it from bash.

-- 

DaveA

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


Re: [Tutor] Questions Regarding Sockets

2012-04-15 Thread Khalid Al-Ghamdi
Thanks a lot. You've been, as usual, very helpful.

On Sat, Apr 14, 2012 at 8:32 PM, Alan Gauld wrote:

> On 14/04/12 17:41, Khalid Al-Ghamdi wrote:
>
>  1- In line (15), what are these variables tcpCliSock,addr supposed to
>> hold and do?
>>
>
> The socket object and the IP address of the client that is connecting to
> the server. When a client connects to a server the server assigns a new
> temporary socket connection that the client  uses. Each connection gets a
> new temporary socket assignment. What happens to the old one is
> implementation dependent and you should not try to reuse it.
>
>
>  2- Why do I have to specify the buffer size and what does it mean?
>>
>
> A buffer is an area of memory used as a kind of holding bay into which
> data is put, usually temporarily. You need to specify where the incoming
> data will go and how much space you expect to use.
>
>
>  3- When I try to run the below code and its corresponding client it
>> works ok for the first time, but then it gives me this error:
>>
>> Traceback (most recent call last):
>>   File "C:\Python32\Khalid Stuff\tsTserv3.py", line 12, in 
>> tcpSerSock.bind(ADDR)
>> socket.error: [Errno 10048] Only one usage of each socket address
>> (protocol/network address/port) is normally permitted
>>
>> I thought it had to do with  the address so I changed the port and it
>> worked ok. so,:
>>
>> A/ isn't the optional tcpSerSock.close() supposed to close the
>> connection for later reuse?
>>
>
> Yes, but there is sometimes a delay before the OS cleans up, it may be
> that which you are seeing.
>
>
>  B/ why is it when i go to the IDLE and enter tcpSerSock.close() and it
>> accepts it, it still gives the same error and doesn't close the
>> connection for reuse by my code?
>>
>
> It may be an OS level thing. But I'm by no means an expert on the OS
> networking layers! Which OS are you running under?
>
>
>   HOST = ''
>>  PORT = 21567
>>  BUFSIZ = 1024
>>  ADDR =(HOST, PORT)
>>  tcpSerSock = socket(AF_INET, SOCK_STREAM)
>>  tcpSerSock.bind(ADDR)
>>  tcpSerSock.listen(5)
>>
>>  while True:
>>   print('waiting for connection ...')
>>   tcpCliSock, addr = tcpSerSock.accept()
>>   print('...connected from: ', addr)
>>   while True:
>>   data = tcpCliSock.recv(BUFSIZ)
>>   if not data:
>>break
>>   tcpCliSock.send(bytes('[{}]
>>   {}'.format(ctime(),data.**
>> decode('utf-8')),'utf-8'))
>>   tcpCliSock.close()
>>  tcpSerSock.close()
>>
>
> I can't help but think you should check if there actually is a connection
> before starting the second loop... What do you expect
> if the accept() fails to find anything?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with regular expression

2012-04-15 Thread Steven D'Aprano

syed zaidi wrote:


Dear all Can someone please tell me how to solve the following problem.  I
have developed a python code to extract specific information from more than
1000 files which have slightly different format. The problem I am facing is
that I have to develop specific RE for each of the file which is very
difficult when it comes to handle 1000s of files. can someone please tell
me how to solve this problem. 



Change the files so that they all have the same format.

Without knowing what the format is, how it differs from file to file, and what 
RE you use, what sort of answer did you expect?



--
Steven

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


[Tutor] Help with regular expression

2012-04-15 Thread syed zaidi


Dear all
Can someone please tell me how to solve the following problem.  I have 
developed a python code to extract specific information from more than 1000 
files which have slightly different format. The problem I am facing is that I 
have to develop specific RE for each of the file which is very difficult when 
it comes to handle 1000s of files.
can someone please tell me how to solve this problem.
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] re.search() help

2012-04-15 Thread Alan Gauld

On 15/04/12 08:10, Michael Lewis wrote:

Hi everyone,

I am a bit confused on how one would ever use re.search(). It
essentially tells me the location on (RAM?) if the pattern matches?


No it returns a MatchObject instance.
You can then perform various operations on the
MatchObject to, for example find the substring
which actually matched.

> What is the purpose of this?

So that you can find the section of a long string that
first matches your regex.


Can you give me a good example of where it would
be useful?


Searching for the first occurence of a regex in a long string.


Scan through /string/ looking for a location where the regular
expression /pattern/ produces a match, and return a corresponding
MatchObject


Try reading the docs on MatchObject, here is a simple example:

>>> s = "Here is a string"
>>> m = re.search("is", s)
>>> m
<_sre.SRE_Match object at 0x153b780>
>>> dir(m)
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 
'groups', 'span', 'start']

>>> m.start

>>> m.start()
5
>>> m.end()
7
>>> m.group()
'is'
>>> m.span()
(5, 7)


Hopefully that gives you the basic idea?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] re.search() help

2012-04-15 Thread Michael Lewis
Hi everyone,

I am a bit confused on how one would ever use re.search(). It essentially
tells me the location on (RAM?) if the pattern matches? What is the purpose
of this? Can you give me a good example of where it would be useful?

Thanks!

re.search(*pattern*, *string*,
*flags=0*)

Scan through *string* looking for a location where the regular expression *
pattern* produces a match, and return a corresponding
MatchObject
instance.
Return None if no position in the string matches the pattern; note that
this is different from finding a zero-length match at some point in the
string.

-- 
Michael J. Lewis
mjole...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor