Re: [Tutor] swapping list elements based on some criterion

2011-10-11 Thread Emad Mohamed
On Tue, Oct 11, 2011 at 1:21 AM, Steven D'Aprano wrote:

> Wayne Werner wrote:
>
>> 2011/10/7 Emad Nawfal (عمـ نوفل ـاد) 
>>
>>  I want to re-structure English so that the adjectives appear after the
>>> nouns, instead of before.
>>> If I have a sentence like:
>>>
>>> The tall man plays well
>>> I need to change it to
>>> The man tall plays well
>>>
>>>
>> Others have offered plenty of help, though I noticed that no one mentioned
>> the Natural Language Tool Kit (http://www.nltk.org/) which is quite
>> powerful
>> if you're playing around with language processing.
>>
>
> I assumed that since the words were already tagged as /N /ADJ etc. that the
> poster was already using NLT.
>
>
> --
> Steven
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>

Hi,
I actually used the POS tagger available here to tag the English text.

http://www-tsujii.is.s.u-tokyo.ac.jp/~tsuruoka/postagger/

The NLTK should be worth looking into.

Best,


-- 
Emad Soliman Ali Mohamed
aka Emad Nawfal (*عماد نوفل*)
PhD in Linguistics, Computational Linguistics Track,
Department of Linguistics,
Indiana University, Bloomington
http://jones.ling.indiana.edu/~emadnawfal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] inheritance, python and pygame

2011-10-11 Thread rail shafigulin
i'm trying to learn OOP, python and pygame at the same time. so far it has
been ok, but i stumbled onto a problem

here is the code that causes problems

#!/usr/bin/python3.1
import pygame
pygame.init()

class BaseSprite(pygame.sprite.Sprite):
  def __init(self, imagefile):
super(type(self), self).__init__()
self.image = pygame.image.load(imagefile)
self.image = self.image.convert()
self.rect = self.image.get_rect()

class Cloud(BaseSprite):
  """
  cloud sprite
  scrolls down and moves side to side when it is reset
  """
  IMAGE = 'cloud.gif'
  def __init__(self):
super(type(self), self).__init__(type(self).IMAGE)

def main():
  cloud = Cloud()

if __name__ == '__main__':
  main()

when i run i get the following error:
  ...
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
  File "/usr/local/lib/python3.1/dist-packages/pygame/sprite.py", line 129,
in add
else: self.add(*group)
RuntimeError: maximum recursion depth exceeded while calling a Python object


i can't figure out where and why the recursion occurs. can someone help me
out? any advice is appreciated
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] inheritance, python and pygame

2011-10-11 Thread Hugo Arts
On Tue, Oct 11, 2011 at 4:20 PM, rail shafigulin
 wrote:
> i'm trying to learn OOP, python and pygame at the same time. so far it has
> been ok, but i stumbled onto a problem
>
> here is the code that causes problems
>
> #!/usr/bin/python3.1
> import pygame
> pygame.init()
>
> class BaseSprite(pygame.sprite.Sprite):
>   def __init(self, imagefile):
>     super(type(self), self).__init__()
>     self.image = pygame.image.load(imagefile)
>     self.image = self.image.convert()
>     self.rect = self.image.get_rect()
>
> class Cloud(BaseSprite):
>   """
>   cloud sprite
>   scrolls down and moves side to side when it is reset
>   """
>   IMAGE = 'cloud.gif'
>   def __init__(self):
>     super(type(self), self).__init__(type(self).IMAGE)
>
> def main():
>   cloud = Cloud()
>
> if __name__ == '__main__':
>   main()
>

Well, for one, you misspelled __init__ in the BaseSprite class above
(you have it written as __init). That means that method won't be
called at all, which is likely what is causing problems.

Also, when using python3, you don't need to specify any arguments to
super. Just calling super() will work.

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


Re: [Tutor] vcf_files and strings

2011-10-11 Thread Hs Hs


VCF - Variant Call Format

VCF files are nothing special but tab delim files describing the genetic 
mutations, friquencies and other base information (bases here mean ATGC 
pertaining to DNA). 

These files are generated by variety of genome sequence data analysis 
pipelines.  MIT and Haplotype Mapping Project consortium developed this format. 


Nothing special except that a genetics or any biologist will understand this - 
nothing special!

hth

cheers




From: Steven D'Aprano 
To: tutor@python.org
Sent: Sunday, October 9, 2011 10:04 PM
Subject: Re: [Tutor] vcf_files and strings

Anna Olofsson wrote:
> Hi,
> 
> I'm a beginner at Python and would really like some help in how to
> extract information from a vcf file.
> 
> The attached file consists of a lot of information on mutations, this
> one though is just 2 rows and 10 columns (the real one has a lot more
> rows).

What do you mean by a VCF file? On my computer, a VCF file is an electronic 
business card, which tries to open in an Address Book application (which 
obviously fails).

I don't know how to interpret the contents of your VCF file. After opening it 
in a hex editor, I can *guess* that it is a tab-separated file: each row takes 
one line, with the columns separated by tab characters. Column 7 appears to be 
a great big ugly blob with sub-fields separated by semi-colons. Am I right? Can 
you link us to a description of the vcf file format?



> I want to extract the mRNA ID only if the mutation is missense. These
> two rows (mutations) that I have attached happens to be missense but
> how do I say that I'm not interested in the mutations that's not
> missense (they might be e.g. synonymous).   Also, how do I say that if
> a mutation starts with a # symbol I don't want to include it
> (sometimes the chr starts with a hash).

What chr? Where is the mutation? I'm afraid your questions are assuming 
familiarity with your data that we don't have.


> vcf file: 2 rows, 10 columns.
> col 0                         col 1            col 2
> col 3              col 4      col5            col6
> col7                                     col8
> col9 chromosome          position           .
> Reference       ALT      position          .          some statistics
> and the ID:s         not important        not important
> 
> The important column is 7 where the ID is, i.e.
> refseq.functionalClass=missense. It's a missense mutation, so then I
> want to extract refseq.name=NM_003137492, or I want to extract only
> the ID, which in this case is NM_003137492.


This is what I *think* you want to do. Am I right?

* read each line of the file
* for each line, split on tabs
* extract the 7th column and split it on semi-colons
* inspect the refseq.functionalClass field
* if it matches, extract the ID from the refseq.name and store it in a list for 
later

(I have completely ignored the part about the #, because I don't understand 
what you mean by it.)


Here's some code to do it:

ids = []
f = open('vcf_file.vcf', 'r')

for row in f:
    columns = row.split('\t')  # Split on tabs
    data = columns[7]  # Huge ugly blob of data
    values = data.split(';')  # Split on semi-colons
    if values[25] == "refseq.functionalClass=missense":
        name_chunk = values[28]  # looks like "refseq.name=..."
        a, b = name_chunk.split("=")
        if a != "refseq.name":
            raise ValueError('expected refseq.name but got %s' % a)
        ids.append(b)

f.close()
print(ids)


Does this help?



-- 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] vcf_files and strings

2011-10-11 Thread Alan Gauld

On 11/10/11 18:16, Hs Hs wrote:


VCF - Variant Call Format
...

Nothing special except that a genetics or any biologist will understand
this - nothing special!


The problem is that this list, being for beginners to Python, is a bit 
short on Geneticists and Biologists! :-)


So you need to explain your problem in general terms, that the rest of 
us can make sense of, or else rely on the few who might understand being 
available/willing to respond.



--
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] inheritance, python and pygame

2011-10-11 Thread rail shafigulin
On Tue, Oct 11, 2011 at 10:56 AM, Hugo Arts  wrote:

> On Tue, Oct 11, 2011 at 4:20 PM, rail shafigulin
>  wrote:
> > i'm trying to learn OOP, python and pygame at the same time. so far it
> has
> > been ok, but i stumbled onto a problem
> >
> > here is the code that causes problems
> >
> > #!/usr/bin/python3.1
> > import pygame
> > pygame.init()
> >
> > class BaseSprite(pygame.sprite.Sprite):
> >   def __init(self, imagefile):
> > super(type(self), self).__init__()
> > self.image = pygame.image.load(imagefile)
> > self.image = self.image.convert()
> > self.rect = self.image.get_rect()
> >
> > class Cloud(BaseSprite):
> >   """
> >   cloud sprite
> >   scrolls down and moves side to side when it is reset
> >   """
> >   IMAGE = 'cloud.gif'
> >   def __init__(self):
> > super(type(self), self).__init__(type(self).IMAGE)
> >
> > def main():
> >   cloud = Cloud()
> >
> > if __name__ == '__main__':
> >   main()
> >
>
> Well, for one, you misspelled __init__ in the BaseSprite class above
> (you have it written as __init). That means that method won't be
> called at all, which is likely what is causing problems.
>
> Also, when using python3, you don't need to specify any arguments to
> super. Just calling super() will work.
>
> Hugo
>
my apologies for not writing and reading my own code correctly. Hugo you
were correct, once i fixed the spelling error it all worked. and thanks for
the hint on how to use super() in python3.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] vcf_files and strings

2011-10-11 Thread Hs Hs



http://www.1000genomes.org/node/101







From: Alan Gauld 
To: tutor@python.org
Sent: Tuesday, October 11, 2011 1:52 PM
Subject: Re: [Tutor] vcf_files and strings

On 11/10/11 18:16, Hs Hs wrote:
> 
> VCF - Variant Call Format
> ...
> 
> Nothing special except that a genetics or any biologist will understand
> this - nothing special!

The problem is that this list, being for beginners to Python, is a bit short on 
Geneticists and Biologists! :-)

So you need to explain your problem in general terms, that the rest of us can 
make sense of, or else rely on the few who might understand being 
available/willing to respond.


-- 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] inheritance, python and pygame

2011-10-11 Thread Steven D'Aprano

rail shafigulin wrote:

i'm trying to learn OOP, python and pygame at the same time. so far it has
been ok, but i stumbled onto a problem


Hugo has already pointed out that you mispelled __init__, and that in 
Python 3 you don't need to explicitly provide arguments to super().



class BaseSprite(pygame.sprite.Sprite):
  def __init(self, imagefile):
super(type(self), self).__init__()


Should be written like this in Python 3:

def __init__(self, imagefile):
super().__init__()


However, for those still using Python 2.x, you might be tempted to write 
this:


def __init__(self, imagefile):
super(type(self), self).__init__()  # NO! Don't do this!


This does not work! But you might be fooled into thinking it does work, 
because it *seems* to work if you only check a simple example:


class Base(object):
def __init__(self):
print "Base called"

class Subclass(Base):
def __init__(self):
print "Subclass called"
super(type(self), self).__init__()  # Wrong!


It seems to work:

>>> x = Subclass()
Subclass called
Base called


But watch what happens when we subclass again:

class AnotherSubclass(Subclass):
def __init__(self):
print "AnotherSubclass called"
super(type(self), self).__init__()


>>> y = AnotherSubclass()
AnotherSubclass called
Subclass called
Subclass called
Subclass called
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in __init__
  File "", line 4, in __init__
  File "", line 4, in __init__
  File "", line 4, in __init__
  ...
RuntimeError: maximum recursion depth exceeded



The problem is that inside Subclass.__init__ the call to 
super(type(self), self) does not necessary give super(Subclass, self). 
If the instance being created is from AnotherSubclass, you get 
super(AnotherSubclass, self) inside the Subclass.__init__, which then 
loops forever.


So in Python 2.x, you must explicitly give the class. You can't use 
type(self).




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


[Tutor] Keyboard Module

2011-10-11 Thread Ryan Strunk
Hello everyone,
I'm still hard at work on this boxing game; I know, I think it's taking
forever too. I've recently run into some trouble with my current keyboard
handler, and I'm hoping someone here might have a recommendation as to what
I could use to handle keyboard input for this and future projects.
The keyboard module I currently have access to is a wrapper for the wx
keyboard handler, which is fine, but it prevents me from holding one key and
pressing another. In an ideal world, I'd love something like this:

def left_hand(self):
if key_down('up'):
self.left_head_hook()
elif key_down('down'):
self.left_uppercut()
elif key_down('left'):
self.left_body_hook()
else:
self.jab()

The above functionality could be useful in other places as well. I would
like to some day create a keyboard trainer for my students, giving them the
ability to practice key combinations like capslock+t, control+alt+pgdn,
numpad0+numpad4, and so on.
I thought of using Pygame to pull this off, but I gather from my research
that I can't just use certain pieces of Pygame; in order to get the
functionality I want out of the keyboard module, I'll need other modules as
well. I don't want to work specifically in Pygame, because I've heard Pygame
is quite bloated and requires a good deal of overhead. (I wouldn't mind
being proven wrong on this)
All of which leads me to my question. Can anyone recommend a keyboard module
which will allow me to work with various facets of the keyboard to execute
functions. I would love to have access to presses, holds, and releases, and
it would be helpful to use unorthodox combinations of keys.
Thanks for any recommendations or advice you all have.
Best,
Ryan

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


Re: [Tutor] map one file and print it out following the sequence

2011-10-11 Thread lina
I tried to write one (not working one) as below, so many problems here.

#!/usr/bin/python3

import os.path

LINESTOSKIP=0
CHAINID="CDEFGHI"
INFILENAME="pdbone.pdb"
DICTIONARYFILE="itpone.itp"
mapping={}
valuefromdict={}

def sortfile():
for chainid in CHAINID:
sortoneblock(chainid)


def generatedictionary(dictfilename):
text=fetchonefiledata(DICTIONARYFILE)
for line in text:
parts=line.strip().split()
if len(parts)==8:
mapping[parts[4]]=parts[0]
print(mapping)

def sortoneblock(cID)
text=fetchonefiledata(INFILENAME)
for line in text:
blocks=line.strip().split()
if len(blocks)== 11 and  blocks[3] == "CUR" and blocks[4] == "cID":
valuefromdict[blocks[2]]=mapping[block[2]]
return


def fetchonefiledata(infilename):
text=open("infilename").readlines()
if os.path.splitext(infilename)[1]=".itp"
return text
if os.path.splitext(infilename)[1]=".pdb"
return text[LINESTOSKIP:]


if __name__=="__main__":
sortfiles()


The representative lines from itpone.itp is:
 1   CH3 1  CUR C21 10.200  15.0350
 2OA 1  CUR  O4 1   -0.400  15.9994
 3 C 1  CUR C19 10.200  12.0110

The representative lines from pdbone.pdb is:

ATOM827  C19 CUR C  85  31.891  29.624   0.280  1.00
0.00
...
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00
0.00
ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00  0.00

The expected results of pdbone.pdb will be:

ATOM832  C21 CUR C  85  32.823  27.366   0.801  1.00  0.00
ATOM831  O4  CUR C  85  31.865  28.248   0.183  1.00  0.00
ATOM827  C19 CUR C  85  31.891  29.624   0.280  1.00  0.00

Thanks for telling me the existing problems.

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