from... import...

2007-02-02 Thread fatwallet961
what's the from ... import keyword use for?
for example - from contenttype import getContentType

import os
import sys
import getopt
import types
import re
import pprint
import logging
from contenttype import getContentType

In Java what kind of statement is similar this?

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


main

2007-02-02 Thread fatwallet961
is the main function in python is exact compare to Java main method?

all execution start in main which may takes arguments?

like the follow example script - 

def main(argv):

==

and what is 
__name__ 
__main__

use for in terms of Java?

thanks






==
#!/usr/bin/env python

"""
Preprocess a file.

Command Line Usage:
preprocess [...] 

Options:
-h, --help  Print this help and exit.
-v, --verbose   Give verbose output for errors.

-o Write output to the given file instead of to
stdout.
-D  Define a variable for preprocessing. 
can simply be a variable name (in which case
it
will be true) or it can be of the form
=. An attempt will be made to
convert
 to an integer so "-D FOO=0" will create
a
false value.

Module Usage:
from preprocess import preprocess
preprocess(infile, outfile=sys.stdout, defines={})

The  can be marked up with special preprocessor statement
lines
of the form:
  
where the  are the native comment
delimiters for
that file type. Examples:

...


# #if defined('FAV_COLOR') and FAV_COLOR == "blue"
...
# #elif FAV_COLOR == "red"
...
# #else
...
# #endif

Preprocessor Syntax:
- Valid statements:
#define [=]
#undef 
#if 
#elif 
#else
#endif
#error 
  where  is any valid Python expression.
- The expression after #if/elif may be a Python statement. It
is an
  error to refer to a variable that has not been defined by a
-D
  option.
- Special built-in methods for expressions:
defined(varName)Return true if given variable is
defined.  
"""

import os
import sys
import getopt
import types
import re
import pprint
import logging

from contenttype import getContentType


# exceptions
class PreprocessError(Exception):
def __init__(self, errmsg, file=None, lineno=None, line=None):
self.errmsg = str(errmsg)
self.file = file
self.lineno = lineno
self.line = line
Exception.__init__(self, errmsg, file, lineno, line)
def __str__(self):
s = ""
if self.file is not None:
s += self.file + ":"
if self.lineno is not None:
s += str(self.lineno) + ":"
if self.file is not None or self.lineno is not None:
s += " "
s += self.errmsg
return s


# global data

log = logging.getLogger("preprocess")

# Comment delimiter info.
#   A mapping of content type to a list of 2-tuples defining the line
#   prefix and suffix for a comment.
_commentGroups = {
"Python": [ ('#', '') ],
"Perl": [ ('#', '') ],
"Tcl": [ ('#', '') ],
"XML": [ ('') ],
"HTML": [ ('') ],
"Makefile": [ ('#', '') ],
"JavaScript": [ ('/*', '*/'), ('//', '') ],
"CSS": [ ('/*', '*/') ],
"C": [ ('/*', '*/') ],
"C++": [ ('/*', '*/'), ('//', '') ],
"IDL": [ ('/*', '*/'), ('//', '') ],
"Text": [ ('#', '') ],
}


# internal support stuff

def _evaluate(expr, defines):
"""Evaluate the given expression string with the given context."""
try:
rv = eval(expr, {'defined':lambda v: v in defines}, defines)
except Exception, ex:
raise PreprocessError(str(ex), defines['__FILE__'],
defines['__LINE__'])
log.debug("evaluate %r -> %s (defines=%r)", expr, rv, defines)
return rv



# module API

def preprocess(infile, outfile=sys.stdout, defines={}):
"""Preprocess the given file.

"infile" is the input filename.
"outfile" is the output filename or stream (default is
sys.stdout).
"defines" is a dictionary of defined variables that will be
understood in preprocessor statements. Keys must be strings
and,
currently, only the truth value of any key's value matters.

Returns the modified dictionary of defines or raises
PreprocessError if
there was some problem.
"""
log.info("preprocess(infile=%r, outfile=%r, defines=%r)",
 infile, outfile, defines)

# Determine the content type and comment info for the input file.
contentType = getContentType(infile)
if contentType is None:
contentType = "Text"
log.warn("defaulting content type for '%s' to '%s'",
 infile, contentType)
try:
cgs = _commentGroups[contentType]
except KeyError:
raise PreprocessError("don't know comment delimiters for
content "\
  "type '%s' (file '%s')"\
  % (contentType, infile))

# Generate statement parsing regexes.
stmts = ['#\s*(?Pif|elif|ifdef|ifndef)\s+(?P.*?)',
 '#\s*(?Pelse|endif

python bracket

2007-02-02 Thread fatwallet961
there is no bracket in python

how can i know where a loop or a function ends?

-- 
http://mail.python.org/mailman/listinfo/python-list