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.
Related
If you search over the internet about python project structures, you will find some articles about python package structure. Based on it, What I want to know is if there is any kind of instructions for creating structure for python projects that isn't packages, that is, projects that the code is the end code it self?
For example, I created a package that handles some requests of some specific endpoints. This package will serve the main code that will handle the data fetched by this package. The main code is not a package, that is, it don't have classes and __init__ files, because in this software layer, there will be no necessity of code reuse. Instead, the main code relate straight to the end it self.
Is there any instructions for it?
It would be good to see the structure itself instead of reading the description of it - it can help visualize the problem and answer properly to your case 😉
projects that isn't packages, that is, projects that the code is the end code it self
In general, I would say you should always structure your code! And by telling that, I mean exactly the work with the modules/packages. It is needed mostly to sperate the responsibilities and to introduce things that can be reused. It also gives the possibility to find things easier/faster instead of going through the unstructured tones of the code.
Of course, as I said, it is a general thought and as far as you are experienced you can experiment with the structure to find the best one for the project which you are working on. But without any structure, you won't survive in a bigger project (or the life will be harder than you want).
Is there any existing python code obfuscating tool for Python3?
Please do not try to teach me that Python isn't the right choice if I want to hide/obfuscate my code. Or that correct licenses should protect the code instead of obfuscation...
Update: This question does not duplicate issue How do I protect Python code?: I simply ask if there is a tool to obfuscate Python. Nothing more and nothing less. (If there is none I wonder why I get so much feedback...)
No matter what you do, at some point the Python interpreter is going to be reading in unobfuscated Python byte-code. From that it is dead easy to get back to your source code (minus comments and non-obvious layout). This is why everybody says it's pretty much impossible to obfuscate Python. The fact that it's pretty much impossible to obfuscate Python implies that there are no good tools for doing so. I'm afraid it's just wishful thinking to say "I know this can't be done very effectively, but are there any tools for doing it?"
Probably the best you can do will be to encrypt your code with standard encryption tools, and write a little wrapper program in some other language that just decrypts your Python and runs your program, then deletes the unencrypted code when it's done. If you want to put way too much effort in you could probably do something with the C API and embedding the Python interpreter in a C program to feed your unencrypted Python to the interpreter only in memory, rather than files on disk.
Note that these schemes will still be relatively easy to get around, and don't work at all if what you want is to provide importable Python modules (rather than whole programs). Which is why I wouldn't expect to find anyone's already written a tool for you to do it.
I wouldn't go the obfuscating approach if I were you and rather investigate alternative ways to ship executable binary files instead of (byte)-code.
Tools that are known to me (there are probably a few others):
http://www.ohloh.net/p/py2c (converts Python into C code which you can then compile)
http://www.pyinstaller.org/
http://cx-freeze.sourceforge.net/
http://www.py2exe.org/ (Windows only)
http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html (Mac only)
I don't know how hack-proof any of those tools are, but I think it's worth taking a look.
Edit: Damnit, missed the Python 3 part. It's a little hard to help because you don't write anything about the product itself (OS, GUI, etc). If it can be also Python 2 code but you have written all your code in Python 3 already, I suggest 3to2.
I'd recommend using pyarmor.
It converts code to binary form. Only drawback would be, you need to obfuscate code for every OS separately.
There is no way to obfuscate Python code in any useful manner, and no reason why you would want to. You seem to want to obfuscate the code to protect it. That is completely pointless, as you can instead ship only the .pyc files, meaning you don't ship the source code at all. Not that shipping only .pyc files will help you, as there are uncompilers for .pyc-files.
If your program is reasonably simple and well-coded, creating executables with cx-freeze, py2exe et al, means that the .pyc files end up inside the executable file, and hence are marginally harder to find, and it's also less obvious that you use Python, so that might be help. But more importantly, it might make installation simpler for your users. They like that.
If you really want to obfuscate your code in a useful way, convert all of it to use Cython, which will create C-files you can compile. This will also speed up the program. Cython is however not fully Python compatible, so you will probably have to make changes.
And I know you don't want to hear this, but I'll say it for the benefit of others:
All of this is of course stupid and misguided. Open source is good for you. Really. You shouldn't protect your code, you should get as many eyes and hands on it as possible.
Trust me on this: Your main worry should be about getting more users, not less pirates. And you get more users by making your software better, not worse. And open source will help in that.
Pyminifier is a Python code minifier, obfuscator, and compressor.
This tool works on Python 3
Update: This project has been discontinued.
The best way to hide your code is to not release it.
Advertise a service - you receive their data then return the processed data. Transmissions can be via the web, email, DHL, pigeon, telephone, graviton pulse, ...
I'm tired of books and tutorials who walk me through how to print things before I can do anything fun. I want to build simple apps or programs. Any suggestions for where to start so I can make and learn at the same time?
If you're just looking for something to do that will challenge you to start actually using Python rather than reading about it, try the Ruby Quiz. You don't have to use Ruby to create solutions.
Each quiz is a problem that can be scripted (using any language, really). Ideally it'll force you to apply the concepts you've been reading about to "real life" problems.
If you are tired of tutorials, then just start building something. Anything. If you get stuck, glance back over the tutorials, or consult the documentation. I'm a big fan of learning by doing.
A short list of options from off the top of my head:
If you're into web development, Django is a popular python web framework that is very well documented. The Blog app is a popular starting point.
Python itself is pretty well documented. If you're a complete beginner to python AND programming in general, you may want to try something less complex. Pick a random task and try to do it using python:
Read and print the ID3 tags from all your mp3 files using mutagen.
List or download your email using the python imaplib or poplib modules.
Write a zip/unzip utility using zipfile.
Don't be in too much of a hurry, and be realistic. Unless you've got a pile of programming under your belt, you won't be able to jump into a complex project after reading a few tutorials. Patience and practice will get you to the place where you can tackle really interesting projects. Impatience will merely lead to frustration.
Might i suggest pygame http://www.pygame.org/news.html ? If you want to do things with visuals, your not going to get anywhere with the default python modules unless, of course you know how to implement SDL. As stated above in the comments, the tutorials are there for a reason; although they are simple, they are meant to teach you the basics and perhaps, have you think about ideas of implementing such given tools to larger projects. Give pygame I try. You can create a window with lines and shapes in a little as ~10 lines. From there you can expand your knowledge to Object-Oriented programming(which a must for UI) and be on your way to larger projects such as AI, graphics, etc.
P.S. Check this book out http://apress.com/book/view/1590598725. Although you might not want to get into game development, it will teach you some rather useful techniques which may help your research in application development.
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.
I'm a pretty inexperienced programmer (can make tk apps, text processing, sort of understand oop), but Python is so awesome that I would like to help the community. What's the best way for a beginner to contribute?
Add to the docs. it is downright crappy
Help out other users on the dev and user mailing lists.
TEST PYTHON. bugs in programming languages are real bad. And I have seen someone discover atleast 1 bug in python
Frequent the #python channel on irc.freenode.net
Build something cool in Python and share it with others. Small values of cool are still cool. Not everyone gets to write epic, world-changing software.
Every problem solved well using Python is a way of showing how cool Python is.
I guess one way would be to help with documentation (translation, updating), until you are aware enough about the language. Also following the devs and users mail groups would give you a pretty good idea of what is being done and needs to be done by the community.
I see two ways of going about it: working on Python directly or working on something that utilizes Python
Since you're a beginner, you're probably hesitant to work on the core Python language or feel that you can't contribute in a meaningful way, which is understandable. However, as a beginner, you're in a good position to help improve documentation and other items that are essential to learning Python.
For example, the Python tutorial is less of a tutorial (in the standard sense) and more of a feature listing, at least in my opinion. When I tried to learn from it, I never got the feeling that I was building up my knowledge, like creating an application. It felt more like I was being shown all the parts that make up Python but not how to put them together into a cohesive structure.
Once I became more comfortable with the language (mostly through books and lots of practice), I eventually wrote my own tutorial, trying to provide not only the technical information but also lessons learned and "newbie gotchas".
Alternatively, you can contribute to the Python world by using Python in programs. You can contribute to projects already established, e.g. Django, PyGame, etc., or you can make your own program to "scratch an itch". Either way, you not only build your knowledge of Python but you are giving back to the community.
Finally, you can become an advocate of Python, encouraging others to learn the language. I kept suggesting to my supervisor at my last job to use Python rather than Java when a considering what to use for a new project. I tell everyone I know about the joys of Python and encourage them to give it a try. I convinced the administrator of a computer forum I frequent to create a section for Python. And, as I already said, I wrote a tutorial for Python and I'm working on a new one for wxPython.
There are many ways you can contribute to Python that aren't necessarily programming related. As your programming skills grow, you may want to move further into code contributions. But you may gain more satisfaction by helping others find the same joy you found in Python.
If you aren't up to actually working on the Python core, there are still many ways to contribute.. 2 that immediately come to mind is:
work on documentation.. it can ALWAYS be improved. Take your favorite modules and check out the documentation and add where you can.
Reporting descriptive bugs is very helpful to the development process.
Get involved with the community: http://www.python.org/dev/
Start by contributing to a Python project that you use and enjoy. This can be as simple as answering questions on the mailing list or IRC channel, offering to help with documentation and test writing or fixing bugs.