Objective-C functions vs. Python modules? - python

Just another newbie question I'm afraid. I am doing a course on python on udacity and pretty much straight away have been shown modules, such as time, and webbrowser, which make it very easy to do such tasks as time delay, opening webbrower, etc.
Is there anything so straightforward and easy to use as this in Objective-C, or what would be the equivalent to the python modules? Is it just the case that you need to write a lot more code in Objective-C alongside their provided library functions to achieve the same thing as a Python module would provide you with?
It just seems like Python modules have more im-built code with less for developer to write in code?

Related

Python sample project (as guide) suggestions for beginner

everyone, I am new in python. I am working on a python project and day by day my script is getting bigger and bigger. Now I have written up to 1000 lines of code in the single script. so it is looking messy and even it is hard to find some functions in my script.
I already have read Hitchhiker's guide and pepe8, but it would be more understandable if I will use one good python written python project as a guide or reference.
please suggest me some good project(for a beginner), which I can use to structure my script and my whole project. I am not a professional programmer but I want to be one.
I think that there are some ways to start to improve your scripts, and your habits:
First, build a entry point to your scripts and become them in python modules as splited as you can to be easy reuse in the future, maybe a lot of your scripts share similar code.
Resources:
Python Entry Points Explained
Structuring, Testing, and Maintaining Python Programs
Python Apps The Right Way
For another hand you maybe must start to test your code using Python's built-in unittest framework (the easiest).
At at the end as you say you need to start to write code using the standards, and I think that the best way is using some pylint in your prefer editor to do not forget the most important as the correct syntax, blank lines, docs, var names, etc. There are a lot of resources about this, you know.
I think is enough for a beginner.

Control an existing application with Python

I have been programming with Python for some time and noticed that it's possible to interact, for example, with MS Excel files through the library XLWT.
Now I would like to know if it's possible to use Python to control other applications, such as the Calculator.exe which is on the standard Windows path C:\Windows\system32.
Is there any way to write a script with Python, let's say, to make the calculator opening and executing 9+3=? I usually like to write some code myself first and ask for help later, but here I've no clue even if it's possible and my researches on Internet have yield only this script to launch the program:
import subprocess
subprocess.call("C:\Windows\system32\calc.exe")
Any help, suggestion or even just "no, it's not possible" would be highly appreciated.
It will always depend on the cooperativity of the other program. If it allows being tweaked, it will offer an API for this (and hopefully a documentation telling you how to use it).
This is not really a Python question because it rather depends on how the API is written. If this API comes as a C library, you will have to write at least a bit of C code to access it via Python. If it is a way of calling the program (special options, etc.) then Python will have as much or less trouble providing these as any other programming language.

How to port a module for Ruby to Python

Or how can I learn Ruby quickly? Is there a pattern to port a ruby module to python?
I want to add scope system to Sass, but I know little about ruby, and when I started, I found the contents of a module are in several files, there are a lot of variables or classes undefined but referenced.
What's the difference between require keyword and the python's import?
Or at least, is there a ruby IDE that can tell me where is a module, class, variable defined like PyDev does?
There probably are IDE out there that have a bit of code help built in, but I usually don't find I need it. Most Ruby libraries have a certain structural pattern to them. Especially gems by design. You might not notice it at first if you are new to Ruby but once you get the hang of it; things are quite easy to navigate by the naming alone.
To the original intent of your question. I don't think there is an easy way to port a piece of Ruby code like SASS to python in a snap. It would require adequate knowledge of both languages to translate it well. In a way it's kind of like the job of a translator, if you don't know either language well you won't make a lot of sense by what you are producing.
In Ruby require loads a file and executes it. A library will usually have a file named like the library that will include other parts of its components, to make the modules within known.
# req.rb
puts 'Hello World'
# irb
1.9.2p290 :001 > require './req.rb'
Hello World
=> true
So it will basically do the same for you as import in python. You name a module you would like to use and it will work for you after requiring/importing it(given it is known to your load paths).
If you are looking for an equivalent to the python
from <modulename> import *
you should refer to
include <modulename>
Which isn't frowned upon as much in ruby as it is in python.
Hope this answers most of your questions.

Python newbie - help needed in choosing modules/libraries

i am learning python.. i want to do certain kind of scripting in python.. like,
i want to communicate 'wmic' commands through dos promt.. store the result a file..
access some sqlite database, take the data it has and compare with the result i stored..
now, what i dont get is that, how should i proceed? is there any specific frameworks or modules/libraries? like, win32api / com or what else?
pls guide me what things i should follow/learn to accomplish what i intend to do..
thanks
for your project look here
http://tgolden.sc.sabren.com/python/wmi/index.html
http://docs.python.org/library/sqlite3.html
here is list of generic python modules
http://docs.python.org/modindex.html
http://docs.python.org/ - here you can find all information you need with examples.
One of the particularly attractive features of Python is the "batteries included" philosophy: The standard library is huge, and extremely well thought out in 90% of the modules I've ever used. Conversely, this means that a good approach is to learn it first before branching out and installing third-party libraries that may be much less well supported and, ultimately, have no advantage.
In practice, this means I'd recommend keeping https://docs.python.org/release/2.6.5/library/index.html close to your heart, and delve into the documentation for the sqlite3 and probably the subprocess modules. You may or may not want win32api later (I've never worked with wmic, so I'm not sure what you'd need), or come back here when you have concrete questions and already explored the standard library offering.

Why use Python interactive mode?

When I first started reading about Python, all of the tutorials have you use Python's Interactive Mode. It is difficult to save, write long programs, or edit your existing lines (for me at least). It seems like a far more difficult way of writing Python code than opening up a code.py file and running the interpreter on that file.
python code.py
I am coming from a Java background, so I have ingrained expectations of writing and compiling files for programs. I also know that a feature would not be so prominent in Python documentation if it were not somehow useful. So what am I missing?
Let's see:
If you want to know how something works, you can just try it. There is no need to write up a file. I almost always scratch write my programs in the interpreter before coding them. It's not just for things that you don't know how they work in the programming language. I never remember what the correct arguments to range are to create, for example, [-2, -1, 0, 1]. I don't need to. I just have to fire up the interpreter and try stuff until I figure out it is range(-2, 2) (did that just now, actually).
You can use it as a calculator.
Python is a very introspective programming language. If you want to know anything about an object, you can just do dir(object). If you use IPython, you can even do object.<TAB> and it will tab-complete the methods and attributes of that object. That's way faster than looking stuff up in documentation or even in code.
help(anything) for documentation. It's way faster than any web interface.
Again, you have to use IPython (highly recommended), but you can time stuff. %timeit func1() and %timeit func2() is a common idiom to determine what is faster.
How often have you wanted to write a program to use once, and then never again. The fastest way to do this is to just do it in the Python interpreter. Sure, you have to be careful writing loops or functions (they must have the correct syntax the first time), but most stuff is just line by line, and you can play around with it.
Debugging. You don't need to put selective print statements in code to see what variables are when you write it in the interpreter. You just have to type >>> a, and it will show what a is. Nice again to see if you constructed something correctly. The building Python debugger pdb also uses the intrepeter functionality, so you can not only see what a variable is when debugging, but you can also manipulate or even change it without halting debugging.
When people say that Python is faster to develop in, I guarantee that this is a big part of what they are talking about.
Commenters: anything I am forgetting?
REPL Loops (like Python's interactive mode) provide immediate feedback to the programmer. As such, you can rapidly write and test small pieces of code, and assemble those pieces into a larger program.
You're talking about running Python in the console by simply typing "python"? That's just for little tests and for practicing with the language. It's very useful when learning the language and testing out other modules.
Of course any real software project is written in .py files and later executed by the interpreter!
The Python interpreter is a least common denominator: you can run it on multiple platforms, and it acts the same way (modulo platform-specific modules), so it's pretty easy to get a newbie going with.
It's a lot easier to tell a newbie to launch the interpreter and "do this" than to have them open a file, type in some code, save it, make it executable, make sure python is in your PATH, or use a #! line, etc etc. Scrap all of that and just launch the interpreter. For simple examples, you can't beat it. It was never meant for long programs, so if you were using it for that, you probably missed the part of the tutorial that told you "longer scripts go in a file". :)
you use the interactive interpreter to test snippets of your code before you put them into your script.
As already mentioned, the Python interactive interpreter gives a quick and dirty way to test simple Python functions and/or code snippets.
I personally use the Python shell as a very quick way to perform simple Numerical operations (provided by the math module). I have my environment setup, so that the math module is automatically imported whenever I start a Python shell. In fact, its a good way to "market" Python to non-Pythoniasts. Show them how they can use Python as a neat scientific calculator, and for simple mathematical prototyping.
One thing I use interactive mode for that others haven't mentioned: To see if a module is installed. Just fire up Python and try to import the module; if it dies, then your PYTHONPATH is broke or the module is not installed.
This is a great first step for "Hey, it's not working on my machine" or "Which Python did that get installed in, anyway" bugs.
I find the interactive interpreter very, very good for testing quick code, or to show others the Power of Python. Sometimes I use the interpreter as a handy calculator, too. It's amazing what you can do in a very short amount of time.
Aside from the built-in console, I also have to recommend Pyshell. It has auto-completion, and a decent syntax highlighting. You can also edit multiple lines of code at once. Of course, it's not perfect, but certainly better than the default python console.
When coding in Java, you almost always will have the API open in some browser window. However with the python interpreter, you can always import any module that you are thinking about using and check what it offers. You can also test the behavior of new methods that you are unsure of, to eliminate the "Oh! so THAT's how it works" as a source of bugs.
Interactive mode makes it easy to test code snippets before incorporating them into a larger program. If you use IDLE there's syntax highlighting and argument pop-ups to help you out. It's also a quick way of checking that you've figured out how to use a module without having to write a test program.

Categories

Resources