Avatar of John500
John500Flag for United States of America asked on

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

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
Python

Avatar of undefined
Last Comment
John500

8/22/2022 - Mon
SOLUTION
efn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
pepr

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.
ASKER
John500

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!
pepr

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
efn

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/
ASKER
John500

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!
efn

You can get the compilers/interpreters for free.  You can get editors for free.  What do you want the software you mention to do?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
John500

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?
pepr

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.
ASKER
John500

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
efn

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.
pepr

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?
ASKER
John500

Yes sir, that's what I want to do - what do you think ?

Thanks
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pepr

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.
ASKER
John500

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.
ASKER CERTIFIED SOLUTION
pepr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
John500

Thanks guys - appreciate the help
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck