Use of the Python Language [closed] - python

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have been studying Python at work for a week now and it seems very interesting, I started using it in order to create Extra Search functions for Splunk.
My Question is, what else can you do with it?, is it only a Script Language?, if so what type of scripts can I create and use?
To what extent should I learn Python in order to begin writing usable scripts?
I can write a Shell Korn script to Query a MySql Database, Can I do the same with Python?

You can pretty much do anything with Python that you can do with ksh, only easier (shells have a lot of stuff built in but many scripts will "reach out" to helpers like grep, sed awk, and so forth, to do grunt work).
Python also comes with an insanely large library (just like Java and C#) and plenty of add-on modules, both of which also include database stuff.
You don't have to learn Python to be able to script successfully, there's a large array of solutions for this. But Python is a useful weapon to have in your arsenal.
Bottom, it's quite able to handle a huge number of tasks, including the specific one you mentioned.

Aloha!
The obnoxious answer is that Python like other languages are turing complete and you can write program in it. But that probably doesn't answer your question. ;-)
First however, a tip: Stop think in terms of scripting vs real programming languages. Python, just like Java, Perl, Ruby, LISP, Scala, Lua, Cloujure are just as much real programming languages as C, C++, Fortran etc. The main difference is what they are running on. C for example can run on "bare metal", that is basically be translated to sequences of instructions that can be executed by the native/real CPU. Python, Java etc relies on a virtual machine to execute their instructions. The VM is then run by the real CPU.
The VM costs some performance, but add things like portability, dynamic behaviour (introspection, duck typing etc) that make the language and systems written in it very efficent in ways it is harder to do (but not impossible - remember Turing completeness) in for example C.
With that in min (Python is a real programming language) you might not be surprised to learn that Python can be and is used for things like:
Network servers and network applications. Look at the great Twisted networking framework and look and beautiful applications like Trac.
Large scale, professional distributed version control. Mercurial (Hg) is written entirely in Python and is able to handle huge, active projects with developers on a global level.
Compilers, languages and virtual machines. There is actually a virtual machine able to run Python written in Python(!). The machine called Pypy use some of the dynamic functionality of the language to enhance the performance.
Pypy is really a groovy concept - having the machine you are running your code in a language that can be modified at runtime - basically rebuilding the "hardware" from your application to enhance the machine to at the whim of the application. (And yes, I'm aware that all LISP guys yawn at this point - nothing new but still very, very groovy)
There you have it, some IMHO good, everything but simplistic, non-professional applications. Now start reading up on Python.org about the advanced features of the language and amazing functionality readily available in the standard lib. Your programming life will thank you for it.
Good luck!

To answer your questions in order:
Everything.
No.
Any.
As much as possible.
Yes.

Start here

Related

what is the sole purpose of python being an interpreter? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
What exactly is the sole purpose of python being an interpreter.
It doesn't provide executable files (how a commercial software developer use it?)
If any part of the code had bugs, it doesn't show up unless python
goes to that line at run it. In large projects, all parts of code
doesn't get interpreted every time, so, there would be a lot of
hidden bugs inside project
Every system should have a python installed in it to run those software's...
I am using py2exe, and I find myself puzzled to just look at the executable file size (too large).
First, answers to your questions.
They can use it for parts of their system for which they don't mind the source being visible (e.g. extensions) or they can Open Source their application. They can also use it to develop backend services for something which they're providing as a service (e.g. Youtube). They can also use it for internal tools which they don't plan to release(e.g. with Google).
That's why you need to write tests, exercise discipline and measure test coverage regularly. You sacrifice the compilers ability to check for things and some speed for advantages which I've detailed below.
Yes but it's not too hard to bundle Python along with your app. The entire interpreter + libraries is not that big. Python is pretty much a standard on most UNIX environments today. This is usually not a practical problem. The same issue is there with (say) Java (you need the JVM installed).
py2exe bundles all the modules into a single executable. It will be big. If you want to do compiled programs that are lean, don't use Python. Wrong fit.
Now, a few reasons on why "interpreted".
Faster development time. Programmer time is costlier than computer time so we should optimise for that.
No compilation cycle. Very easy to make incremental changes and check. Quick turnaround.
Introspection and dynamic typing allows certain kinds of coding not possible with some compiled languages like C.
Cross platform. If you have an interpreter for your platform, the program will run there even if it was written on a different platform.
You bring up a few different issues, here are some responses:
1) Technically, Python isn't interpreted (usually) - it is compiled to bytecode and that bytecode is run on a virtual machine.
So Python doesn't provide executables because it runs bytecode, not machine code.
You could just as well ask why Java doesn't produce executables.
The standard advantages of virtual machines apply: A big one being a simplified cross-platform development experience.
You could distribute just the .pyc (compiled bytecode) files if you don't want your source to be available. See this reference.
2) Here, you are talking about dynamic vs. static languages. There are tradeoffs, of course. One disadvantage of dynamic languages, as you mention, is that you get more run-time errors rather than compile-time errors.
There are, of course, corresponding advantages. I'll point you to some resources discussing both sides:
Dynamic type languages versus static type languages
What do people find so appealing about dynamic languages?
http://research.microsoft.com/en-us/um/people/emeijer/Papers/RDL04Meijer.pdf
3) Quite right. Just as you need the Java VM installed to run Java, perl to run Perl, etc.
Regarding your last point:
The whole idea of running in a VM is that you can install that VM once, then run many different apps. By bundlig the whole VM with every app (such as with py2exe), you are going against that concept. So yes, you have to pay the cost in terms of size.
Sole purpose of python is to provide a beautiful language to program in.
Your point #1 and #3 are similar and answer is that professional programmers use py2exe/pyinstaller etc to bundle their programs and distribute, in cases of frameworks/libraries they even don't need to do that.
Your point number #2 is also valid for statically compiled languages, something compiles correctly in C++ doesn't mean it will not crash at run-time or business logic is correct, you anyway need to test each part of your code, so with good unittests and functional tests python is at par with other languages in finding bugs, and as it doesn't need to be comiled and being dynamic means better productivity.
IMO
Python is not an interpreter, but an interpreted language.
This question is more about interpreted language VS compiled languages which has actually no other answer that the usual "it depends of your need".
See Noufal Ibrahim for details, but I'm not sure this question is a good fit for SO.
(1) You can provide installers for Python code (which may install the Python environment). This doesn't prevent you from having commercial code. Note that you can also have Java (also "interpreted" or JIT-compiled) commercial or desktop code and require your users to install a JRE.
(2) Any language, even compiled and strongly type, may produce errors that only show up when you get to that given code (e.g. division by zero). I guess you may be referring to strongly v.s. loosely typed languages. It's not just a matter of compilation, but the fact that strongly-typed languages generally make it easier to find "structural" bugs (e.g. trying to use a string as a number) during the compilation process. In contrast, loosely-typed language often lead to shorter code, which may be easier to manage. What to use really depends on the goal of your application.
Interactivity is good. I find it encourages making small, easily testable functions that build together to make an application.
Unless you are writing simple, statically linked applications, you will usually have some run-time baggage that must be included or installed (mfc, dot net, etc.) for compiled languages. Look at the winsxs folder. Sure, you get to "share" that stuff most of the time, but there is still a lot of space taken up by "needed", if hidden, requirements.
And as far as bugs, run-time bugs will be the same no matter what. Any good programmer would test as much as possible when making changes. This should catch what would be compile time bugs in other languages as well as testing run-time behavior.
A python .exe has to necessarily include a complete copy of the python interpreter. As you say, since it's interpreted it won't touch every line of code until that line is actually run. Some parts may actually invoke a python parse/compile sequence (e.g. eval(), some regexes, etc...). These would fail in a compiled .exe unless the full interpreter was available.

Should i switch to Python? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I recently have been considering switching to the Python programming language. Currently, Matlab is the language of choice in my department for rapid development and prototyping of code. It’s very good at this, but Mathworks (the company who produces Matlab) have been tinkering with the licencing terms, leading to hassles where none should exist.
I have found python+numpy+scipy+matplotlib+IPython+[random sci/numerical packages] to be almost a complete replacement for matlab. There are certainly some packages missing, so if there is a specialized set of tools that are available in matlab but not in python, this might be an issue, but otherwise, I haven't looked back.
I'll mention a couple of distinct advantages of python over matlab
True programming language instead of a hack of a language thrown over a numerical package (numpy came to python instead of the other way around). Managing large python projects is a complete joy vs matlab. How many different oop systems have been mangled into matlab?
Totally free and portable. I can use python on just about any machine without licensing issues. This is the biggest advantage from my perspective.
Wrapping other libraries in C,C++, Fortran is fairly straightforward using SWIG, Cython,f2py,etc vs the ugliness that is a mex file
Cython for accelerating slow code (although I find that numpy is nearly as fast if not faster than matlab)
mpi4py vs some other package that I have to buy from matlab to run parallel applications
Personally I use the Enthought Python Distribution for a lot of my work, because it packages everything and is free for individuals in academia. I've alternatively built python and all of its libraries and modules from scratch. Module management is perhaps the largest weakness in python, but there are some nice things to help out like virtualenv and pip
The choice comes down to cost. If you are happy paying for Matlab - especially if you use the toolboxes - you will likely find Python doesn't provide such an integrated package. Having a matrix as the basic data type makes Matlab an intuitive language for many mathematical tasks. Personally, I find the this coupled with the debugger invaluable.
Python, through NumPy, SciPy and the like do provide the same functionality. There will of course be a learning curve to overcome.
If you are performing general programming tasks, that are not particularly applied math solutions then Python is an extremely easy to use and adaptable language. It is also free - which may be a deciding factor.
If you're looking for a wholesale Matlab replacement, you may want to take a look at Python(x, y). It aims to provide a more cohesive experience, rather than leaving new users to trawl the internet themselves looking for the right components to use.
Another option is GNU Octave, which is essentially an open-source clone of MATLAB.
Python certainly is useable as a replacement for Matlab for many cases, by using NumPy, SciPy, and Matplotlib (see my development environment setup guide for how to install all these packages). However, there are some things that Matlab does better, such as providing libraries for interfacing with data collection hardware. So you will need to try Python and see if it meets your requirements.
Should you switch or not depends on what you think of Python. I myself love Python, and I know it is extremely effective for rapid prototyping. The syntax is clean and crisp, and very easy to learn.
To make your decision I recommend visiting the home page, Python.org, and having a look at the docs.
Just download scipy and give it a try.

Developing PyPy's Rpython as a general programming language [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Is there any interest in developing Rpython (Restricted Python) from the PyPy project as a general purpose programming language? Perhaps it could be a fork from the PyPy project. Does such a project exist? Since the programs are compiled, one could simply contribute modules written in Rpython, and it could compete with other python implementations including CPython and PyPy.
I can't speak for everyone else, but I personally am extremely interested in using RPython as a general-purpose language. To answer some other people's questions:
Why? Because Cython is a pain to figure out how to use. If you don't put in a lot of tricky type declarations just right, you don't get any speedup. With RPython, it will run fast or it won't run at all.
Using PyPy offers a good speedup, but currently not nearly as much as RPython.
RPython might be a good way to get super-fast, somewhat Pythonic code. Here's an example to help you get started. I'm not aware of any large projects to do this, unfortunately.
Yes, there is already a project to use the translation tool chain of PyPy to create standalone executables and libraries using RPython. It is called RPythonic.
A general purpose RPython would not be a competitor for CPython and PyPy. It wouldn't even be a competitor for things like Cython.
Why? Because RPython is not Python!! RPython is merely a language which shares some syntax with Python; enough that a Python interpreter can execute RPython code, but you cannot take Python code and compile it as RPython.
The restrictions on Python that are added to enable RPython to be statically typed and compiled (indeed, to have static type inference) are so severe that they completely change the language. If you want to write an RPython program, you have to decide to do so up front and write it in RPython. You can't write a program in Python and then decide to compile it as RPython, and you can't even tweak a realistic Python program a bit to make it RPython. Normal Python code is nothing like normal RPython code; writing RPython is more similar to writing Java or C#.
So if you want to write general programs in Python but you want them to go faster, RPython has very little to offer you. That's the niche PyPy's Python interpreter is trying to fill.
If you want to write general programs in a lower level compiled language because you need your program to run faster than Python, then there are existing very mature languages and libraries for that, like Java or C#.
The reason to code in RPython would be to do something that is particularly made better by RPython. Like writing interpreters to which you can add garbage collectors and JIT compilers without having to write them by hand! Here RPython shines, and yes I would be very interested in a more polished and usable RPython interpreter-writing environment. But as a general purpose programming language for writing programs that don't particularly benefit from RPython's specialities, it would simply be a monstrous amount of work to get it to the point where it could compete with existing languages that already fill that role better. It barely even has a standard library now (because almost none of Python's extensive standard library is usable in RPython), for example.
From the looks of it, the restrictions are quite severe and on the whole it's a lot less to program in, I imagine. That's necessary for implementing PyPy, but generally if you want fast compiled code that can interact with Python, you'd use Cython (which is targeted at CPython extensions and supports pretty much all of Python seamlessly) or write code in one of the more common languages that can do this. And if you just want fast, compiled code... well, RPython may be more pleasant than e.g. C, but still, I don't see a significant advantage here (at least none that would warrant the effort to create a usable, stable language).
Why would I want to write directly in RPython?
It seems so much simpler to Python code and run PyPy.
Why would I want to write C code?
It seems so much simpler to write Python and have PyPy be implemented in C
Why would I want to write assembler code?
It seems so much simpler to write Python and have PyPy implemented in C and C implemented in Assembler.
I guess it really is turtles all the way down.
Why would I want to stop using the most convenient language and switch to a less convenient language?
What's the value in giving up a nice language?

Scripting Languages [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I am looking for a good scripting language to link to my program.
I am looking for 2 important attributes:
Scripting language should be hard linked into the executable (not requiring 3rd party
installations). This is important to me to simplify distribution.
Scripting should allow some run-time debugging option (When running a script inside my program I would like to easily run it inside a debugger while it is running in the context of my program)
Can python,lua or some other language supply me with this?
Both Lua and Python can provide the features you mention, so choosing one of them will depend on other criteria.
Lua is a lighter weight solution, it will have a much smaller disk footprint and likely a smaller memory overhead than Python too. For some uses it may be faster. Python has a much richer standard library, more mature third party libraries and a more expressive language.
Both have been embedded into major applications. Python can be found in Blender, OpenOffice and Civilization 4. Lua can be found in World of Warcraft and Adobe Lightroom. I'd recommend looking at a few tutorials for each and the facilities available to embed them in your application and just choose the one that fits your brain best.
Lua is designed for this:
static linking? check!
debugging? check!
Typically, Lua is the better choice for embedding into another project. Python is better as a standalone platform because the library support is so much broader. See Lua Versus Python for more details.
Personally, I use both very frequently depending on the job at hand. I always use Lua for embedding into C/C++ applications or scripting functionality that is imported from C/C++ shared libraries (i.e. a DLL). Python is always my first choice for standalone tasks that do not require low-level C/C++ code.
I'd put my two cents in for python. I don't know a lot of the details, but the computer graphics suite blender does a wonderful job of implementing python scripting.
As far as I can tell in blender 2.5 the interpreter is run from inside the executable,
import sys
sys.executable
shows /blender/blender.exe
and there is good debugging support, it even has a full interactive interpreter inside.
For more info check out: http://www.blender.org/
I really like Lua for embedding, but just as another alternative, JavaScript is easily embeddable in C, C++ (SpiderMonkey and V8) and Java (Rhino) programs.
In addition to Tcl, Lua, and Javascript (all already mentioned), Guile is another language designed explicitly for this.
I'll add Tcl to the mix. It's designed to be easily embedded into other programs.

How should I go about learning Python? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am currently learning PHP and want to learn about OOP.
I know Python is a well-organized and is all OOP, so would learning Python be a wise choose to learn OOP?
The thing is I am more towards web development then just general programming, and I know Python is just a general purpose language, but there is Django.
So how should I go about learning Python if I am lending towards web development?
Is there any good books/websites that help me learn Python for web development?
Is there any free webhosting companies that allow Python? I never used Python before, only PHP, and not sure how it works? Is there like a "xampp" for python?
I would pick up a good O'Reilly book on Python and build a strong understanding of the fundamentals before delving into more web specific ventures. Once you've got the essentials then I'd branch out to things like Django.
Here's a good starting page:
O'Reilly - Python
And here's a good tutorial if you'd rather do your research on the web:
Python Tutorial
I learned Python reading the book Learning Python. I read almost the whole thing on a plane trip, and when I got home I was able to start building applications immediately. There are newer versions out since I read it (and it's longer), but I found it very easy to follow.
As mentioned by others, Django is definitely the place to start for Web development.
Work through the examples on www.pythonchallenge.com. Refer to the language documentation when you get stuck.
As long as you stay within their quota Google Apps Engine provides free hosting for Python.
Django is a great framework when you want to do webdevelopment with Python. Django also has great documention with http://www.djangobook.com/ and the official Django website.
If you want to learn about Object Oriented Programming in general, you may want to look at the answers to this question, although many of the books are higher level (and some are aimed at Java/C# like languages instead of Python-like languages).
Here's some answers to your questions:
Python is an excellent language for beginners looking to learn OO design/programming.
As far as books and websites, the best python book I've read is available free online Mark Pilgrim's Dive into Python.
For web programming there are many many options. You mention Django which is the most popular although I like Turbogears, Cherrypy and web.py. All of these have their own webserver built-in (Based on paste or cherrypy)
For hosting, it's usually based on fastcgi or Apache's mod_python.
I've heard really good reports of webfaction for python based hosting.
Hope this helps, but if you are learning php why not go for Apress's PHP Objects, Patterns, and Practice that's a good book.
If it is your basics in OOPS that you wish to strengthen, Java is a good option(provided you know c++ or any other non-web-based language which supports OOPS). However, if you are looking towards web-development, Python should be your best option.
Yes, Python is a good option
Yes, Django is a very good web application framework(and they have awesome documentation and tutorials put up at their site)
To learn Python I definitely recommend reading "The Python Cookbook" cover-to-cover. Its fun, and covers some very important concepts. However, there really is no substitute for the standard python documentation. Its well written, but it might take a while through a major portion of it. Using it as just reference material is also a fine idea.
Well I have seen domains which allow Django to be hosted; also you should try out the GAE(google app engine) once you are comfortable with django. Its a great place to host your apps.
You could learn using books, but nothing beats practical hands-on approach - so make sure you have Python installed in a computer to help you learn. If you decide to buy a Python book, I strongly suggest you DO NOT buy a copy of Vernon Ceder's Python Book, it has very bad reviews. I bought a copy and was also disappointed.
If you'd like to join a mailing list, we have a good community at Python Tutor. Sign up and post your questions there as well.
Good luck
Get ipython. Use it as your shell. This means move, copy, view, change, edit files from ipython. Day to day shell stuff anywhere has enough little problems that one ordinarily solves by piping, but are just as easily solvable by python. The real bonus is that your eye for syntax and simple solutions will develop quickly.
Need to find files? use os.walk,
Running grep? try to 'open' the file instead, try some regex while you are there. Those uses of the language will serve you in any type of python programming.
( Good news, PHP and python use the same underlying regex lib PCRE, so although there are some additions, it'll be familiar to you, )
The nice thing about having this in the language , which is not really the case in PHP or Perl, is that you can just mess around with functions, not full programs.
Why ipython and not the standard REPL or bpython? Easier to use as a shell out of the box. That's all.
I recently learnt Python and had very little programming experience before. I found that doing a little bit of Python first then diving into Django worked for me. USing Django, looking through its reference material and Googling individual problems when I needed the help was really good.
Django has a built in Development server for you to use a bit like xampp, however, to make things like installing Django, installing Python, installing plugins etc a lot easier, use a unix based OS. I am developing on Mac OS and I have had no problems. Most Linux distributions will be the same. I wouldn't want to try Django development on Windows, there are just too many hacks you need to do to get it working, plus, it is more difficult for when you then publish the site (on a unix server).
Learn some Python, there are some good books suggested here, but don't get too deeply stuck into it if your focus will be Django. Go and do the official Django tutorial and then Google around for one or two more.
I use a book called 'The Definitive Guide to Django'. It is great for learning Django in the first place, but after the first few chapters, I stopped following it and started my own projects instead. Now it is a really good reference book to have.
It takes a while, but its worth it. I started working at a company as a Django developer recently and it is great.
Good Luck!

Categories

Resources