Alan Gauld | 1 Dec 01:57 2008

Re: Reading gzip files


"Dinesh B Vadhia" <dineshbvadhia <at> hotmail.com> wrote

> I'm reading gzip files and writing the content out to a text file 
> line by line.
>  File "C:\Python25\lib\gzip.py", line 275, in _read
>    self._read_eof()
>  File "C:\Python25\lib\gzip.py", line 311, in _read_eof
>    raise IOError, "CRC check failed"
> IOError: CRC check failed
>
> I've checked the Python docs and online but cannot find a
> solution to the problem.  Thanks.

At great risk of stating the obvious but have you actually checked
that the CRC of the gzipped file is correct? Does the problem only
happen with this file or with all gzipped files?

And are the files actually zipped by gzip or by some other
"compatible" program? If other does it work for genuine
gzipped files?

Just some ideas to play with.

Alan G 

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

(Continue reading)

David | 1 Dec 02:17 2008

Re: Reading gzip files

Dinesh B Vadhia wrote:
> I'm reading gzip files and writing the content out to a text file line
> by line.  The code is simply:
>  
> import gzip
> list_zipfiles  = dircache.listdir(zipfolder)
> writefile = "out_file.txt"
> fw = open(writefile, 'w')
>  
> for ziparchive in list_zipfiles:
>     zfile = gzip.GzipFile(zipfolder + ziparchive, "r")
>     for line in zfile:
>         fw.write(line)
>     zfile.close()
> fw.close()
I am learning also. I came up with this;
#!/usr/bin/python
import tarfile
tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r")
for f in tFile.getnames():
    print f
    tFile.close()
#fname = "out.txt"
#fobj = open(fname, 'w')
#for line in f:
#    fobj.write(line + '/n')
#tFile.close()
#fobj.close()

My problem is I can not figure out how to write to the file with new
(Continue reading)

David | 1 Dec 02:35 2008

Re: Reading gzip files

David wrote:

> #!/usr/bin/python
> import tarfile
> tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r")
> for f in tFile.getnames():
>     print f
>     tFile.close()
> #fname = "out.txt"
> #fobj = open(fname, 'w')
> #for line in f:
> #    fobj.write(line + '/n')
> #tFile.close()
> #fobj.close()
> 
> My problem is I can not figure out how to write to the file with new
> lines, as you can see my attempts at the bottom. This works fine in Linux;
> ./py_list_zip.py >> out.txt
> 
> but I want to learn how to do it within python, plus to understand.
> 
> any hints :)
> 
> thanks
> -david
> 
> 
> 
oops should have been;
    fobj.write(line + '\n')
(Continue reading)

Dinesh B Vadhia | 1 Dec 03:42 2008
Picon

Re: Reading gzip files

Hi Alan
 
A bunch of gzipped files are read with the majority working but a few don't.  I don't know if these files were originally zipped with gzip but I'd guess that they were.
 
Strangely, for the files that don't work I can read/print the file almost to the end and then it falls over with the CRC error.
 
Dinesh
 
 
Message: 5
Date: Mon, 1 Dec 2008 00:57:04 -0000
From: "Alan Gauld" <alan.gauld <at> btinternet.com>
Subject: Re: [Tutor] Reading gzip files
To: tutor <at> python.org
Message-ID: <ggvcov$eio$1 <at> ger.gmane.org>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original


"Dinesh B Vadhia" <dineshbvadhia <at> hotmail.com> wrote

> I'm reading gzip files and writing the content out to a text file
> line by line.
>  File "C:\Python25\lib\gzip.py", line 275, in _read
>    self._read_eof()
>  File "C:\Python25\lib\gzip.py", line 311, in _read_eof
>    raise IOError, "CRC check failed"
> IOError: CRC check failed
>
> I've checked the Python docs and online but cannot find a
> solution to the problem.  Thanks.

At great risk of stating the obvious but have you actually checked
that the CRC of the gzipped file is correct? Does the problem only
happen with this file or with all gzipped files?

And are the files actually zipped by gzip or by some other
"compatible" program? If other does it work for genuine
gzipped files?

Just some ideas to play with.

Alan G
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Kent Johnson | 1 Dec 12:35 2008
Picon

Re: Reading gzip files

On Sun, Nov 30, 2008 at 9:42 PM, Dinesh B Vadhia
<dineshbvadhia <at> hotmail.com> wrote:

> Strangely, for the files that don't work I can read/print the file almost to
> the end and then it falls over with the CRC error.

That's because the CRC isn't checked until after the file is read; as
you can see from the traceback, the error is in _read_eof(). You could
put a print statement or breakpoint in that function to get more
information about the failure.

Kent
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

spir | 1 Dec 13:35 2008
Picon

Re: attribute of built-in type

Kent Johnson a écrit :

[...big snip...]

 > Do you know that you can probably just assign a __name__ attribute to
 > the objects? Or name, or whatever you like?
 >
 > In [13]: class Foo(object): pass
 >    ....:
 >
 > In [14]: f=Foo()
 >
 > In [15]: f.name
 > ---------------------------------------------------------------------------
 > AttributeError                            Traceback (most recent call last)
 >
 > /Users/kent/≤ipython console> in <module>()
 >
 > AttributeError: 'Foo' object has no attribute 'name'
 >
 > In [16]: f.name = 'f'
 >
 > In [17]: f.name
 > Out[17]: 'f'
 >
 > This will work for custom objects that do not have a __slots__
 > attribute. Perhaps you could wrap the creation of the objects in a
 > function that gives them a name?

Exactly. I presently use a tool func (actually, a Classmethod) that executes 
this (pseudocode):
def set_names(scope):
	for name,obj in scope.__dict__.items():
		# exclude not_to_be_named objects of the scope
		if name_has_the_proper_form:
			# 'name' attr already used for other purpose
			obj.id = name

Which works. I will use this method if I cannot find a way to let the objects 
natively have __name__ attributes. Conceptually , it is not the same thing -- 
at least for me. That a class of objects has such an attribute may be seen as 
an interface characteristics (comparable to 'iterable' or 'ordered') that could 
be inherited. If I want a class to define/construct ordered containers, I will 
simply let it inherit list's interface. The facts that python sets __name__ 
attributes at a low level (as you explain below) and that workarounds are not 
satisfying, both confirm that this is a fondamental interface charasteristics, 
not a superficial one.

Also, I need a name format rule to distinguish to_be_named from not_to_be_named 
objects. This is not a big issue, as this rule has only to be followed inside a 
specific scope. But maybe you understand that I do not find this conceptually 
/satisfying/. A logical feature that should not depend on such a random detail, 
rather it should be a basic property. Imagine a case where you would need to 
give objects characteristics such as iterable or ordered (or mutable / 
immutable!), by implementing the proper __xxx__ methods, based on the fact that 
their name has this or that format. Which may happen if such characteristics 
could not be inherited in python.

[...smaller snip...]

 >> It is an illustration of what i'm trying to do: let a class inherit from
 >> 'function' so that its instances get a __name__. This wouldn't be a harmful
 >> overload I guess, as these objects have a __call__ method anyway (the reason
 >> why I chose 'function').
 >
 > I doubt this would do what you want. AFAICT the function name is
 > assigned by the compiler, not by the function constructor. (That is a
 > bit of oversimplification but close enough. I think the compiler
 > creates a code object, passing its name to the constructor; when a
 > function object is wrapped around the code object, it pulls its name
 > from the code object.)

This let me play a bit further with functions. The following confirms both that 
  __name__ is a very low-level attribute, and that it lies in the code itself:
=============================
import types ; Function = types.FunctionType

def typ(obj): return obj.__class__.__name__
def f():pass
g = f
print id(f)
print "%s: %s at %s \n" % ( g.__name__,typ(g),id(g) )

print dir(f)
cod = g.func_code
print "cod is a '%s' object\n" % typ(cod)

h = Function(cod,{})
print "%s: %s at %s \n" % ( h.__name__,typ(h),id(h) )

==>

10464752
f: function at 10464752

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__name__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 
'func_globals', 'func_name']
cod is a 'code' object

f: function at 10464816
==========================

g points to the same, unique, function as f. So that we can understand that g's 
__name__ actually is 'f'. But this shows that an object's "birthname" is 
somehwhat different from further name to which the object may be bound.
Now, h points to brand new object (as confirmed by its id); still, its __name__ 
is 'f'. This attr is well carried by the code and retrieved there, but it is 
not an attribute of code objects (cod.__name__ does not exist).
As I see it, the __name__ is somehow a third kind of name/id for objects. In 
fact, if 'id()' was called 'address()' instead, then __name__ could well be 
called __id__.
I also find highly meaningful that, among built_in types, the bool, int, float, 
str,... series do not have a __name__. (But this is probably an off-topic 
subject for the python_tutor list.)

 >>> Kent

Salutation,
denis

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
WM. | 1 Dec 21:20 2008
Picon

Re: Tutor Digest, Vol 58, Issue 2

Stooges.py

i,j,k = 3,3,3
while i != 1:
     print 'Larry, Moe & Curly Joe!'
     i -= 1
     while j != 1:
         print 'Go Mad!!'
         j -= 1
while k != 1:
     print 'Go-go bad-bad!!'
     k -= 1
print '\nBye-bye.'

I am trying to learn loops.  These nested 'whiles' work OK but I would 
like to wrap this script in a 'for' loop. I have not been able to make 
that work.

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

W W | 1 Dec 21:38 2008
Picon

Re: Tutor Digest, Vol 58, Issue 2

Try this:

 for x in xrange(3, 0, -1):
   ....:     print x
   ....:    
   ....:    
3
2
1

HTH,
Wayne

On Mon, Dec 1, 2008 at 2:20 PM, WM. <wferguson1 <at> socal.rr.com> wrote:
Stooges.py

i,j,k = 3,3,3
while i != 1:
   print 'Larry, Moe & Curly Joe!'
   i -= 1
   while j != 1:
       print 'Go Mad!!'
       j -= 1
while k != 1:
   print 'Go-go bad-bad!!'
   k -= 1
print '\nBye-bye.'

I am trying to learn loops.  These nested 'whiles' work OK but I would like to wrap this script in a 'for' loop. I have not been able to make that work.


_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor



--
To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
Steve Willoughby | 1 Dec 22:02 2008

Re: converting processing code to python code

On Mon, Dec 01, 2008 at 12:59:23PM -0800, Steve Willoughby wrote:
> > void drawSquare1() {
> >  if(x1<0 || x1>width-size) {
> >   x1Speed = -x1Speed;
> >  }
> > 
> >  if(y1<0 || y1>height-size) {
> >   y1Speed = -y1Speed;
> >  }
> 
> def drawSquare1():
>   if (x1 < 0 or x1 > width-size):
>     x1 += x1Speed
>     y1 += y1Speed
>     rect(x1, y1, size, size)

I think my eyes skipped somewhere while copying that
over.  The exact translation of that code snippet would
of course have been:

def drawSquare1():
  if x1 < 0 or x1 > width-size:
    x1Speed = -x1Speed
  if y1 < 0 or y1 > height-size:
    y1Speed = -y1Speed

Sorry 'bout that.

--

-- 
Steve Willoughby    |  Using billion-dollar satellites
steve <at> alchemy.com   |  to hunt for Tupperware.
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 2 Dec 00:24 2008

Re: Tutor Digest, Vol 58, Issue 2


"W W" <srilyk <at> gmail.com> wrote

> for x in xrange(3, 0, -1):
>   ....:     print x
>   ....:
>   ....:
> 3
> 2
> 1

Since the OP isn't using the loop counter a simpler solution
is simply

for x in range(3):

>> i,j,k = 3,3,3
>> while i != 1:
>>    print 'Larry, Moe & Curly Joe!'
>>    i -= 1
>>    while j != 1:
>>        print 'Go Mad!!'
>>        j -= 1
>> while k != 1:
>>    print 'Go-go bad-bad!!'
>>    k -= 1
>> print '\nBye-bye.'
>>
>> I am trying to learn loops.  These nested 'whiles' work OK but I 
>> would like
>> to wrap this script in a 'for' loop. I have not been able to make 
>> that work.

To the OP:
It helps if you show us what you tried so that we can see where
the misunderstanding is. What did you do and what happened?

--

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane