File: Location.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (34 lines) | stat: -rw-r--r-- 1,353 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import compression

class Location:
    """Handle for a record (use 'text' to get the record's text)"""
    def __init__(self, namespace, name, filename, startpos, length):
        self.namespace = namespace
        self.name = name
        self.filename = filename
        self.startpos = startpos
        self.length = length
    def __repr__(self):
        return "Location(namespace = %r, name = %r, filename = %r, startpos = %r, length = %r)" % (self.namespace, self.name, self.filename, self.startpos, self.length)
    def __str__(self):
        return "Location(%s:%s at %s: %s, %s)" % \
               (self.namespace, self.name,
                self.filename,self.startpos, self.length)
    def __getattr__(self, key):
        if key == "text":
            infile = compression.open_file(self.filename)
            if hasattr(infile, "seek"):
                infile.seek(self.startpos)
                return infile.read(self.length)
            # read 1MB chunks at a time
            CHUNKSIZE = 1000000
            count = 0
            while count + CHUNKSIZE < self.startpos:
                infile.read(CHUNKSIZE)
                count += CHUNKSIZE
            infile.read(self.startpos - count)
            return infile.read(self.length)
        elif key == "__members__":
            return ["text"]
        raise AttributeError(key)