Solved

How to write a simple program in Python (opening files and parsing text)

Posted on 2007-10-25
17
2,305 Views
Last Modified: 2008-01-09
Greetings:

I recently was asked to become familar with Python.  I figure the best way to do that is to take a simple application in C/C++ and make it work in Python.  Hence, my questions are these:

1)  What makes Python different from C/C++?
2)  Will Python perform the same basic functions as C/C++ like opening files and string manipulations
3)  Is there an editor available for download that will enable me to test a product or do I need to purchase one?

Thanks
0
Comment
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
17 Comments
 
LVL 15

Assisted Solution

by:efn
efn earned 100 total points
ID: 20153418
1)  Some of the differences:

a)  C and  C++ and compiled, Python is interpreted.  So C and C++ can be more efficient, but you can use Python more interactively.
b)  Indentation matters for control flow in Python, not in C or C++.
c)  C++ is much more complex than Python.
d)  Python has more handy, useful, portable libraries than C or C++.
e)  Python is dynamically typed.  In C or C++, a name always refers to an object of a fixed type.  In Python, you can attach a name to any kind of object.
f)  Python has more handy, built-in easy-to-use facilities for data structures and string manipulation.

2)  Yes.

3)  There are many free text editors you can download.  I use the Crimson Editor a lot, but there are many others.  I don't know what your special requirements for testing a product are, if you have any.
0
 
LVL 29

Expert Comment

by:pepr
ID: 20153452
ad 1) Python is interpreted, C++ is compiled. Python is similar to C++ in being "multiparadigm". While supporting well the OOP, it can be nicely used also for procedural aproach (i.e. unlike in Java, you are not forced to define a class with static member only to have a simple action). Source file in Python defines represents a module and also a namespace in the C++ sense.

Everything in Python is represented as objects -- the application class instances, but technically also things like function definitions, class definitions... Whatever is passed, you are passing a reference. A reference does not have information about the type of the object. The type is bound to the instance that is refered. It means that you can easily build heterogeneous structures. Python is much more flexible here.

The main compound data structures are dictionaries (lookup tables), lists, tuples, strings, and sequences of bytes.

Blocks of code are separated by indentation. Do not consider this a problem. You will get used to really fast.

ad 2) Yes. In C++ the result will be faster when running, Python will be faster during development and reasonably fast when running.

     f = open('myfile.txt')
     for line in f:
         process(line)
     f.close()

String is one of the standard built-in classes. It can be Unicode string (all strings will be unicode in near future; the new type "sequence of bytes" is introduced).

ad 3) You can use any reasonable text editor that you like. Many of them have support for Python. I personally use jEdit (www.jedit.org).

I personally use Python and C++. You can embed Python interpreter into C/C++, you can implement modules for Python in C/C++, but I simply use them side by side. If you like C++, you will like Python. If you do not know C++, you will also like Python ;)

Go for it!

Ask specific things. Too much things to be answered in one question.
0
 

Author Comment

by:John500
ID: 20170998
pepr/efn,

I'm going to split the points on this one.  Before I close it out, can either of you tell me whether Visual Studio 2005 supports Python?

I feel the best way to ramp up to Python is to take a Visual C++ application and convert it.  This application opens files, stores all necessary text to variables and then passes those variables to an MSSQL database via stored procedures.

Will I be able to do the same thing with Python?  I'm guessing Python doesn't support MSSQL ODBC calls but is it possible to spawn a process if necessary?

Finally, I'd like to open a new question and post the code for the VC app I'm talking about.  Maybe either or both of you can help me convert it.  If the whole thing can't be converted to perform the same goal, at least I'll understand the limitations as we go.

Thanks!
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 29

Expert Comment

by:pepr
ID: 20173351
Python is interpreted. The main implementation is also called CPython and it is implemented in C. The simplest form of Python is to download and install the interpreter from http://www.python.org/download/.

Python supports also working with databases (http://www.python.org/topics/database/), and you will find also ways for working with MS SQL.

It depends how complicated C++ application do you want to convert. If the application is large, it may be easier to embed the Python interpreter into it (http://www.python.org/doc/ext/embedding.html). You can also use Boost Python for C++.

Another way is to convert your C/C++ module into Python module and use it from Python program.
0
 
LVL 15

Expert Comment

by:efn
ID: 20174706
I doubt that Visual Studio 2005 supports Python out of the box.

There is a version of Python called IronPython that is built on .NET.  It apparently can be integrated into Visual Studio 2005:

http://blogs.msdn.com/aaronmar/archive/2006/02/16/a-bit-more-on-ironpython.aspx

It's certainly possible to spawn a process from Python.

It appears that there's an MSSQL module for Python:

http://pymssql.sourceforge.net/
0
 

Author Comment

by:John500
ID: 20188415
I'm thinking I need to buy software that will support the following three languages:

Python, Java and Jython?

Is there such an animal out there?

Thanks!
0
 
LVL 15

Expert Comment

by:efn
ID: 20188596
You can get the compilers/interpreters for free.  You can get editors for free.  What do you want the software you mention to do?
0
 

Author Comment

by:John500
ID: 20188923
I want to be able to compile and debug on a level par with Visual Studio.  I did down load a Java compiler but compiling is done from a crude MS DOS command prompt.

As I mentioned above I need to get familiar with Python, Java and Jython and I want to do it with a product (if it exists) that will as robust as Visual Studio.  If I have to buy three compilers - well then I guess I'll do that.  But if I can get away with two or even one - that's the ticket I'm looking for.

I've been told about the 'Adobe's Flex Builder V2-Win'  product but I don't know if it will handle all three.

Any suggestions?
0
 
LVL 29

Expert Comment

by:pepr
ID: 20189054
The "interpreted language" is the synonym for "you do not compile before running the program". For the most widespread implementation of Python you only need hardware (a computer), the operating system (like MS Windows or Linux), the Python iterpreter (downloaded for free).

You can use Visual Studio IDE as an editor if you want. Probably, it is possible to set the IDE so that Ctrl-F5 will launch the script. If you are going to use IronPython, then there probably is some support (IronPython is .NET language). In other cases, it is more usual to use other editors.

Java is another interpreted language. You have to install its runtime (downloadable for free). It is usual to use IDE for Java or some other decent editor.

Jython is Python implemented in Java.

In my opinion, it is far better to start with simple Python program using even as simple editors as Notepad is than to search first for the Rolls-Royce among the various IDEs. You need to understand first what going on first. Then you can think about how to be more productive in editing the code.
0
 

Author Comment

by:John500
ID: 20189302
pepr,

>> In my opinion, it is far better to start with simple Python
>> program using even as simple editors as Notepad

I understand what you're saying but I'd rather rebuild a previous program of mine in order to ramp up to each language - (Java, python, jython)

You mentioned above - "Python supports also working with databases"  and I know Java supports MSSQL .  Thus, I will take a C++ project that parses files and populates an MSSQL database.  If there is an editor out there that will enable me to do this in all three languages or plugins for Visual Studio 2005 that's what I need.

Thanks for the help
0
 
LVL 15

Expert Comment

by:efn
ID: 20190470
I'm not particularly expert on the IDE market, but I'd suggest you look at Eclipse.

http://www.eclipse.org/

I believe it was originally for Java, and there are add-ons for Python and Jython.

http://pydev.sourceforge.net/

The Adobe product you mentioned is based on Eclipse.
0
 
LVL 29

Expert Comment

by:pepr
ID: 20191221
To make it clear... You have a C++ solution and you want to get the Python solution that does the same, the Java solution that does the same, and the Jython solution that does the same. Is it correct?
0
 

Author Comment

by:John500
ID: 20192311
Yes sir, that's what I want to do - what do you think ?

Thanks
0
 
LVL 29

Expert Comment

by:pepr
ID: 20193330
I think that the approach should be to learn all the languages, get the idea of the C++ solution, rethink the idea in the context of the other language, and retype the rethinked solution manually in any editor that you like.

The solution in the other language should reflect the features of that language. This cannot be done automatically, by machine. Well, it can, but the result would not be nice, and the solution could also be inefficient.
0
 

Author Comment

by:John500
ID: 20193584
pepr,

You wrote:

>> I think that the approach should be to learn all the languages

The way I want to 'learn' them is to take an application in C++ which I am extremely familiar with and convert this app in each of the other languages.  The only thing that keeps me from doing this is knowing the development environments.

Ideally, I'd like to purchase one that will give me the robust features of Visual Studio by way of comparison.  Money is not any issue.  I do realize that Eclipse is free and that there are Eclipse plug-ins for Python and Jython:

http://wiki.python.org/moin/EclipsePythonIntegration
http://pydev.sourceforge.net/
http://www.ibm.com/developerworks/library/os-ecant/

My biggest concern is being able to obtain the right editor(s) that will allow easy access to the various 'methods' I need.  I don't want to use a crude MS DOS compiler for each of these languages and if there is one editor for all three - terrific!

Stated another way, I don't want to learn these languages by using second rate editors.  I don't want to learn all the limitations of the various editors while I'm learning the languages.  I want the best editors for each language, and when all things are considered, if its possible to get a three in one deal - that's what I want.
0
 
LVL 29

Accepted Solution

by:
pepr earned 400 total points
ID: 20198822
O.K. People differ and use a different approach to solving things. "My editor" is the thing very close to the heart of the developer. Logical arguments do not work here as people have different expectations and feelings.

Another editor that I like and that supports Python sources is (free) jEdit http://www.jedit.org/  It is written in Java, thus cross-platform. Because of that (written in Java, as also Eclipse is) it is a bit slower than editors written in compiled languages, but (in my opinion) its friendliness to the user and the features overweight the negatives. It supports more than 130 (programming) languages and there is a lot of plugins that can make your life easier. Some people say it is not so heavy-weight as Eclipse. Read the reviews of jEdit at http://www.jedit.org/index.php?page=reviews.
0
 

Author Comment

by:John500
ID: 20200411
Thanks guys - appreciate the help
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question