Friday, June 22, 2012

Files in jython


Files
File objects are handled with built-in functions. The open method is used to open a file for reading or writing operations.

The following table shows the most common methods used for files
F
Operation
Description
f = open(filename [, mode='r'])
Opens a file in the proper mode:
mode :
'r' : Read
'w' : Write. Create the file if it does not exist
'a' : Append.
'+' : (added to the previous modes - example 'a+') opens the file for updates
'b' : (added to the previous modes - example 'rb') open the file in binary mode
f.close()
Closes the f file
f.fileno()
Returns descriptor of the f file
f.flush()
Empties the internal buffer of f
f.isatty()
Returns true if f is a TTY
f.read([size])
Reads a maximum of size bytes from the file and returns a string
f.readline()
Reads a line from f
f.readlines()
Reads until the end of file (EOF) and returns a list of lines
f.xreadlines()
Returns a sequence without reading until the end of file (preferred to readlines()
f.seek(offset[, whence=0])
Sets the file's current position to 'offset' bytes from 'whence':
0: from the beginning of the file
1: from the current location
2: from the end of the file
f.tell()
Returns the current location in the file
f.write(str)
Writes str into the file
f.writelines(list)
Writes a list of strings in the file

Tuples in jython


Tuples

Tuples are non modifiable object arrays parsed with an index.
A tuple is handled as a series of values separated by commas and within brackets.
·         () is an empty tuple
·         (0,1,2,3) is a 4 elements tuple, indexed from 0 to 3
·         tuple = (0, (1, 2), (4,5,6)) is a tuple that contains other tuples. tuple[1][0] returns 1

Operations on sequences are available for the tuples.

Some methods to handle dictionaries in jython


Some methods to handle dictionaries

Method
Description
adict.has_key(k)
Returns TRUE (1) if the key k exists in the dictionary
adict.keys()
Returns the list of dictionary keys
adict.values()
Returns a list of dictionary values
adict.items()
Returns a list of tuples (key, value) for each element of the dictionary
adict.clear()
Deletes all elements of adict
adict.copy()
Returns a copy of adict
dic1.update(dic2)
Update the dictionary dic1 with the values of dic2  based on the values of the keys
adict.get(k[,default])
Equivalent to adict[k] but returns default if k cannot be found.
adict.popitem()
Retrieves an element and deletes it from the dictionary

Dictionaries in jython


Dictionaries

Dictionaries are arrays of objects indexed on a key (string value) and not by an index.
A dictionary is accessed with a tuple key:value separated by commas and in brackets.
·         {} is an empty dictionary
·         {'P1':'Watch', 'P2': 'Birds', 'P3':'Horses'} is a dictionary with 3 elements with the following keys: P1, P2 and P3
·         adict = {'FR_US':{'Bonjour':'Hello', 'Au revoir':'Goodbye'}, 'US_FR':{'Hello': 'Bonjour','Goodbye':'Au Revoir'}} is a dictionary that contains other dictionaries. To translate 'Hello' in French:: adict['US_FR']['Hello']

Some list functions in jython


Some list functions

Method
Description
mylist.append(x)
Adds an element at the end of the list
mylist.sort([function])
Sorts the list with the optional [function] comparison function
mylist.reverse()
Reverses the list (from last to first)
mylist.index(x)
Seeks the index x
mylist.insert(i, x)
Inserts x at index i
mylist.count(x)
Returns the number of occurrences of x in the list
mylist.remove(x)
Deletes the first occurrence of x in the list
mylist.pop([i])
Deletes and return the last element in the list or the element at index i