Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, April 10, 2007

Counting number of lines in a file @ python

f = open(filename)
lines = f.readlines()
f.close()

print "%s has %d lines." % (filename, len(lines))

Monday, March 26, 2007

File Handling @ Python

A file object maintains state about the file it has open. The tell method of a file object tells you your current position in the open file. Since you haven't done anything with this file yet, the current position is 0, which is the beginning of the file.
The seek method of a file object moves to another position in the open file. The second parameter specifies what the first one means; 0 means move to an absolute position (counting from the start of the file), 1 means move to a relative position (counting from the current position), and 2 means move to a position relative to the end of the file. Since the MP3 tags you're looking for are stored at the end of the file, you use 2 and tell the file object to move to a position 128 bytes from the end of the file.
The tell method confirms that the current file position has moved.
The read method reads a specified number of bytes from the open file and returns a string with the data that was read। The optional parameter specifies the maximum number of bytes to read. If no parameter is specified, read will read until the end of the file. (You could have simply said read() here, since you know exactly where you are in the file and you are, in fact, reading the last 128 bytes.) The read data is assigned to the tagData variable, and the current position is updated based on how many bytes were read.

import os
def filesnsubfiles(root):
file = os.listdir(root)
for i in file:
if os.path.isdir(root + "\\" + i):
filesnsubfiles(root + "\\" + i)
else:
print (root + "\\" + i)

Wednesday, December 27, 2006

Print methods and doc strings @ Python

def info(object, spacing=10, collapse=1):
"""
Takes module, class, list, dictionary, or string."""
methodList =[method for method in dir(object) if callable(getattr(object, method))]
processFunc =collapse and (lambda s: " ".join(s.split())) or (lambda s:s)
print "\n".join(["%s=%s" % (method.ljust(spacing),processFunc(str(getattr(object, method).__doc__)))
for method in methodList])
if __name__=="__main__":
print info.__doc__

Saturday, December 23, 2006

PySpotting

Choose Python. Choose readability.
Choose the simple over the complex and
the complex over the complicated. Choose
dynamic typing. Choose duck typing.
Choose decorators. Choose generators.
Choose metaclasses if you don't value
your sanity. Choose to import this. Choose
an almost-fanatical devotion to the BDFL,
unless he comes up with something like
optional static typing, in which case choose
to whine about it in your blog until he stops.
Choose Effbot. Choose Timbot. Choose
wx. Choose to come up with a bloody implementation
before spouting off on
comp.lang.python or Python-Dev. Choose
the explicit rather than the implicit. Choose
one obvious way to do it, especially if you
are Dutch. Choose list comprehensions.
Choose Paul Graham's essays and
s/LISP/Python/g. Choose Jython when
your marketing people choose Java.
Choose speed of development over speed
of execution, but when in doubt, import
psyco. Choose to finish early and laugh at
your colleagues as they waste their miserable
lives bowing down in subservience to
that sadistic little C++ compiler.
Choose Python.
Choose your __future__

List Manipulation @ Python

Just think of python variables as labels to objects, not as storing stuff or even references to something.

In fact you don't event need copy module... since when you slice a list it returns a new list object with the objects needed, you can use a slice to give a new list with all objects, a copy:

>>> a = [1, 2, 3]
>>> b = [a[:], a[:], a[:]]
>>> b
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> b[1][1]
2
>>> b[1][1] = 4
>>> b
[[1, 2, 3], [1, 4, 3], [1, 2, 3]]

You get changed only the 2nd list, because you created three copies:

>>> a is a
True
>>> a[:] is a[:]
False
>>> a[:] == a[:]
True


The same happen for elementary types, they are no exception, but the behavior with them may be different because they are immutable and may be the same object. With integers, for instance, in this case, with small integers,

>>> x = 10
>>> y = x - 1
>>> z = x - 1
>>> y == z
True
>>> y is z
True

Both y and z are integers with the same value and are the same object. But with large integers, the same may not happen:

>>> x = 123456789
>>> y = x - 1
>>> z = x - 1
>>> y == z
True
>>> y is z
False

Here they have the same value, but they are not the same object. Since they are immutable, it doesn't matter, just to keep in mind to always think if you want to make comparisons by value (==) or by identity (is)

"==" compare by value, if both objects have the same value, while "is" compare by identity, if they are the same object.

Example, here x and y have the same value, but are not the same objects.

>>> x = [1, 2, 3]
>>> y = [1, 2, 3]
>>> x == y
True
>>> x is y
False


Here x and y are the same object, and therefore have the same value:
>>> x = y = [1, 2, 3]
>>> x == y
True
>>> x is y
True

But, when you deal with immutable objects, Python have an internal cache, so they may be the same object, but you have no guarantee, so be careful if you want to check by value or identity...