Saturday, March 28, 2009

Metal Gas Cans For Sale In Canada

dependencies in Python - Python Dependencies getClass

Here we are on the second piece of code. Today we see the getClass function that reads a line of code (from a file. Py) and identifies the class defined:
 
final getClass (line): # Check if the first five letters of the line are the same 'class' if line [: 5] == "class": # Search the brackets to see where the class name ends end = line.find ("(") # Check if you have found the parentheses if end ==- 1: # If the search does not find the brackets ':' end = line.find (":") # Returns class name that stands between # 6 and end the character return line [6: end] # Returns -1 if not found the word 'class' else: return -1
all for today to the next. For the functions already seen click below:
TrovaNelFile

Monday, March 23, 2009

Green And Pink On Tv Screen

1 - Find the

Now began a series of posts concerning the dependencies in python. In practice, we will interpret the code needed to build a python file and the dependencies between files in your program.

The first thing to do is to identify the classes and functions found in different files. To do this we need three functions:

TrovaNelFile (filename, string)
getClass (line)
GetFunction (line)

TrovaNelFile: look for a file within a given text string and returns the rows where found the string

 
final TrovaNelFile (filename, string): # Open the file f = open (filename, "r") # reads all lines f.readlines lines = () # Initialize a new list # (required to do append ()) Occur = [] # Do the loop on lines * f or in the range ( len (lines)): # Check if string is # in the line read if string in lines [i]: # If you add the line number occur.append (i) # Close the file (it's always good to do) f.close () # returns the list of rows # string containing return Occur

* You could do also: < …for line in lines:…> however, lost count of the number of auto row.

all for today, we'll see in the next post the other two functions.

Wednesday, March 11, 2009

Where Can I Get Playmobil Instructions

Save your files with file owner file format with Python

I'm writing a software and I want to save what I have done so that i can re-open the software and restart from where I stopped. How can I do it? Surfing on the net I find Some doc about objects serialization, so they can be converted in bytes...What it means? I can't understand very well.

Python is very powerful because it does almost everything by itself, but it is even very difficult.
There is a package, named Pickles, which takes tho objects and sves them to a file. Pay attention because it does not save everything. For other information here is the link .
So there are different possibilities:

  1. You can save the list of the operation you did, quite easy to code, but it has a low efficiency on opening.
  2. You can use the pickles and you don't save everything, it's a little bit more difficult, but it is even more efficient.
  3. You can optimize the code so that the opening is simple, it's hard to code, but very efficient.
Is there Any other consideration? Writing Am I wrong? Can you help me? I'm waiting your comment.

Get Extra Credits Poptropica

Save files in their format with Python

I'm writing my program and I want to save what I do and then you can pick up where I left off. How it works? Search the Web and find various guides concerning the serialization of objects that you want to save to turn them into byte ... What does this mean? I barely understand. Python is very powerful because it does almost everything himself, but the speech is very complicated. In practice there is a package, the Pikler, which takes the objects you and saves them to a file. But be careful because it does not save everything. For more information, here's the link. At this point the roads open is different:
  1. You save the list of operations performed and then repeated, fairly easy to write, but not very efficient when you reopen the file. You use the
  2. Pickler and you do not save everything, but only what you can save, harder to write, but more efficient to reopening the file.
  3. trying to optimize the functions so you can then reopen everything in a simple, very complicated to write, and much more efficient than previous.
Anything else? I wrote the nonsense? Can you help? I am open to your comments / advice.

Monday, March 9, 2009

Economic Stimulus Act Mortgage

Resize Images with GDP

Here is a function, which can be called somewhere to resize images. It works resizing the image to user defined width and height, or setting width (or height) to -1, resize the new dimensions maintaining the same aspect ratio.

It is possible to set even the quality of the image.



def Resize(file,width,height,quality=80):

path,ext=os.path.splitext(file)

name=os.path.basename(path)

try:

image=Image.open(file)

except:

print "Error! -> file:",name+ext

return

w,h=image.size

if width==-1 and height!=-1:

w=height*w/h

h=height

if height==-1 and width!=-1:

h=width*h/w

w=width

if width!=-1 and height!=-1:

w=width

h=height

fmt=image.format

try:

image=image.resize((w,h),Image.ANTIALIAS).save(file,fmt,quality=quality)

except:

print "Error! -> file:",name+ext

return

print name+ext,"...Ok!"



A more complex code is here , it’s a script that use wx as a graphical user interface.

Friday, March 6, 2009

Greyhound Bus Cambridge On

Rename Files Rename Files

Here we are. This is my first post ... in Inglese. We Can Start From Some little script Which sitrng delete from file-names:

import os

daSostituire = raw_input ("Enter the string to replace>") =

SostituisciCon raw_input ("Enter the string to replace> ")

for path in os.listdir (os.getcwd ()): = NewName

path.replace (daSostituire, SostituisciCon)

os.rename (path, newname)

print "I renamed% s to% s"% (path, newname)

Well, now we'll see what we Have Done:

import os : I load the package to mange files and folders.

daSostituire = raw_input ("Enter the string to replace>") : Ask the user to the string to replace.

SostituisciCon = raw_input ("Enter the string to replace>") : Ask the user to the string That has to be used to replace the above.

os.getcwd () : this Gives me the folder of the script.

os.listdir () : this one Gives me the list of files and folders.

for ... in ... : this is a cycle and takes all the element in the list. I.e.: if I want all the apples in a bag I'll do: for apple in bag.

for path in os.listdir(os.getcwd()) : this does a cycle on all files and folders in the current directory.

nuovoNome = path.replace(daSostituire,SostituisciCon) : makes the new name of the file, by replacing the string defined above.

os.rename(path,nuovoNome) : renames the file.

print "Ho rinominato %s in %s"%(path,nuovoNome) : prints what it did.

Warning: this code is only as example if you want a most useful script goes here

Monday, March 2, 2009

Ozark Trail Replacement Air Mattress

String Substituting Substituting Strings

So here we are at the first post. Where to start, I'd say you might start with a small script that removes certain characters from file names:

import os

daSostituire = raw_input ("Enter the string to replace>") =

SostituisciCon raw_input ("Enter the string to replace>")

for path in os.listdir (os.getcwd ()): = NewName

path.replace (daSostituire, SostituisciCon)

os.rename (path, newname)

print "I renamed% s to% s"% (path, newname)

Well now we see that what I did:

import os : load library to the files and folders

daSostituire = raw_input ("Enter the string to replace>") : ask user to input the string you want to replace

SostituisciCon = raw_input ("Enter the string to replace>") : ask for input to 'string with which the user wants to replace the first

os.getcwd () : This gives me the folder where the script is

os.listdir () : This gives me a list of all files and folders in the path

for ... in ... : This is a loop that takes all the elements in the list. Example: if I wanted to take one by one all the apples in a basket would do: for apple in the basket

for path in os.listdir (os.getcwd ()) : I do a loop over all files and folders in the current directory

NewName = path.replace (daSostituire, SostituisciCon) : I define a new name replacing the strings with previous

os.rename (path, newname) : rename the file

print "renamed% s to% s"% (path, newname) : print to video what it did.

Attention all this code is an example if you want a more functional script download it from here