How to program in Python + Paste Code for review/help

The S&M crowd is getting naughty with the game files.

How to program in Python + Paste Code for review/help

Postby FormalMoss » Thu Jan 14, 2016 12:10 pm

Where to start learning

Free Online Book: - Learn Python The Hard Way (Thanks Cornflakes)
Free Online Book: - Automate the Boring Stuff - Practical Programming for Total Beginners
Python Wiki
Python Docs


Free Online Course: - Introduction to Computer Science and Programming Using Python
Python Coding (Thanks Jason S) (NB: Aimed at Python 3.x)
Programming in 3d - Panda 3D
A List of possible IDEs

Spyder (Thanks Dinosawer)
Pycharm (Community Edition) (Thanks Dinosawer)
CodeLite
Notepad ++
Pycharm EDU (Thanks Nikky (Achati))

Dinosawer wrote:ARISE THREAD AND REVEL IN YOUR NEW-FOUND USEFULNESS :ghost:

myself and several other people who regularly hang out on the Limit Theory IRC are Python programmers and are more than willing to help you if you have a problem, so feel free to drop by. ;)

For a long time I've used Spyder, which is not a bad IDE, having a built-in python console and debugging and so on, but is lacking in code collapsing (there is none) and auto-complete (it only completes functions you've already used in that file - I expect auto-complete to at least fill in stuff from the standard library too).

Pycharm (the community edition, which is free)
It has code folding, and does have auto-complete for all your installed python packages, support both 2.7 and 3.* at the same time.

(spyder has 2 separate versions), built-in version control (I tested github and it just works™, cloning, commiting and pushing), extensive code inspecting (errors and warnings), autoformatting, built-in console and debugger and so on.
Have only played with it for a little bit but I like it a lot so far.
Screenshot:
Spoiler:      SHOW
Image



Nikky (Achati) wrote:This is basically a simpler version of pycharm
which is tailored for people learning the language
and also has about everything you will need included
including downloading pip packages iirc
edu is even simpler
maybe a plus if you dont want to know how the build process behind it works or some stuff that needs to be external in community/ full-version isnt working
because as far as i can see its all integrated in edu
also apperently python courses are included


Jason S wrote:I should really add more to it, but it does have a neat example of starting a 2d top down game which you can use as a starting point for your creation.
Last edited by FormalMoss on Fri Apr 15, 2016 7:10 pm, edited 23 times in total.
YAY PYTHON \o/

In Josh We Trust
-=326.3827=-


User avatar
FormalMoss
Captain
 
Posts: 957
Joined: Fri Jun 06, 2014 8:06 am

Re: If you want to learn how to program in Python...

Postby FormalMoss » Mon Jan 18, 2016 8:41 am

Code Snippets:



Updating Version Info Inside a Text File

Learn Python The Hard Way - Chapter 21 - Functions can return something


Display IP Address

Thanks to Dinosawer, I am now able to print out strings properly :)
Code: Select all
print ""
Store_IP = '10.25.'

def Create_IP_Address(Store_IP, Third, Fourth):
   print "Here are 3 different ways to display the results"
   print "1: Your IP Address is %s%s.%s" % (Store_IP, Third, Fourth)
   print "2: Your IP Address is " + Store_IP + Third + "." + Fourth
   print "3: " + Store_IP + Third + "." + Fourth

def main():
   Third = raw_input("Enter the 3rd octet: ")
   Fourth = raw_input("Enter the 4th octet: ")
   print ""
   Create_IP_Address(Store_IP, Third, Fourth)
   print ""
   
main()



List directories and files

Thanks to Jason S:
http://www.pythoncoding.com/areas/files/list-directories-and-files/
Code: Select all
# Importing os will allow file accessing
import os

slash = '\\'

# The root directory to scan
directoryStart = r'C:\Users\Public'

# Use walk method to transverse a directory tree and files within it
for directoryName, directoryNames, fileNames in os.walk(directoryStart):

    # print sub directory paths
    for subDirectoryName in directoryNames:
        print(directoryName + slash + subDirectoryName)

    # print file name paths to all file names
    for fileName in fileNames:
        print(directoryName + slash + fileName)



Memory Profiler in Python

Taken from here.
Let us prove my point using memory_profiler, a Python add-on module (which depends on the python-psutil package) by Fabian Pedregosa (the module’s github page). This add-on provides the decorator @profile that allows one to monitor one specific function memory usage. It is extremely simple to use. Let us consider the following program:

Code: Select all
import copy
import memory_profiler

@profile
def function():
    x = list(range(1000000))  # allocate a big list
    y = copy.deepcopy(x)
    del x
    return y

if __name__ == "__main__":
    function()

Code: Select all
invoking

python -m memory_profiler memory-profile-me.py

prints, on a 64-bit computer

Filename: memory-profile-me.py

Line # Mem usage Increment Line Contents
================================================
4 @profile
5 9.11 MB 0.00 MB def function():
6 40.05 MB 30.94 MB x = list(range(1000000)) # allocate a big list
7 89.73 MB 49.68 MB y = copy.deepcopy(x)
8 82.10 MB -7.63 MB del x
9 82.10 MB 0.00 MB return y

This program creates a list of n=1,000,000 ints (n x 24 bytes = ~23 MB) and an additional list of references (n x 8 bytes = ~7.6 MB), which amounts to a total memory usage of ~31 MB. copy.deepcopy copies both lists, which allocates again ~50 MB (I am not sure where the additional overhead of 50 MB - 31 MB = 19 MB comes from). The interesting part is del x: it deletes x, but the memory usage only decreases by 7.63 MB! This is because del only deletes the reference list, not the actual integer values, which remain on the heap and cause a memory overhead of ~23 MB.

This example allocates in total ~73 MB, which is more than twice the amount of memory needed to store a single list of ~31 MB. You can see that memory can increase surprisingly if you are not careful!

Note that you might get different results on a different platform or with a different python version.
Last edited by FormalMoss on Thu Apr 21, 2016 3:47 pm, edited 11 times in total.
YAY PYTHON \o/

In Josh We Trust
-=326.3827=-


User avatar
FormalMoss
Captain
 
Posts: 957
Joined: Fri Jun 06, 2014 8:06 am

Re: If you want to learn how to program in Python...

Postby FormalMoss » Mon Jan 18, 2016 9:09 am

Apparently Panda3d was made to be used with Python.. also, be prepared to learn about GLSL for fancy graphics :)

And to start off with Panda3d, for beginners (read, me):
https://www.panda3d.org/manual/index.php/Starting_Panda3D

When Panda3d is up and running, open a terminal and type the following into the Python window:
Code: Select all
from direct.showbase.ShowBase import ShowBase; s = ShowBase(); s.run()


Another bit of code is:
Code: Select all
base.disableMouse(); m=loader.loadModel("models/smiley"); m.reparent_to(base.render); base.camera.set_pos(0, -5, 0); base.camera.look_at(0,0,0)





Resources



Parallel Python

http://www.parallelpython.com/
PP is a python module which provides mechanism for parallel execution of python code on SMP (systems with multiple processors or cores) and clusters (computers connected via network).

It is light, easy to install and integrate with other python software.

PP is an open source and cross-platform module written in pure python.

The most simple and common way to write parallel applications for SMP computers is to use threads. Although, it appears that if the application is computation-bound using 'thread' or 'threading' python modules will not allow to run python byte-code in parallel. The reason is that python interpreter uses GIL (Global Interpreter Lock) for internal bookkeeping. This lock allows to execute only one python byte-code instruction at a time even on an SMP computer.

PP module overcomes this limitation and provides a simple way to write parallel python applications. Internally ppsmp uses processes and IPC (Inter Process Communications) to organize parallel computations. All the details and complexity of the latter are completely taken care of, and your application just submits jobs and retrieves their results (the easiest way to write parallel applications).
Last edited by FormalMoss on Fri Apr 15, 2016 10:47 am, edited 4 times in total.
YAY PYTHON \o/

In Josh We Trust
-=326.3827=-


User avatar
FormalMoss
Captain
 
Posts: 957
Joined: Fri Jun 06, 2014 8:06 am

Re: If you want to learn how to program in Python...

Postby Talvieno » Mon Jan 18, 2016 9:12 am

I'm planning on learning Python at some point, but other things are taking priority at the moment.
User avatar
Talvieno
Community Manager
 
Posts: 8004
Joined: Fri Apr 11, 2014 6:50 pm
Location: North GA, USA

Re: If you want to learn how to program in Python...

Postby FormalMoss » Mon Jan 18, 2016 10:51 am

Kewl - in as much as I am interested in rendering a 3d engine with Python.. ultimately, I would like to know Python for modding in Limit Theory.
Maybe I can use this page for tracking my progress..

Tal; I highly recommend you sign up to edX, it is free, and the URL above will allow you to follow through with the course exercise even after it has finished.. just sign up for it, and you can have it in your "library" of courses on edX.
:ghost:
YAY PYTHON \o/

In Josh We Trust
-=326.3827=-


User avatar
FormalMoss
Captain
 
Posts: 957
Joined: Fri Jun 06, 2014 8:06 am

Re: If you want to learn how to program in Python...

Postby Silverware » Mon Jan 18, 2016 1:20 pm

Py (at least non-compiled Py) is slow as balls for anything serious.
It's perfect for short scripts that need to be quickly cross-platform, because almost all Linux flavors install Py2 by default.

Py however is a good tool to learn to program with, as non-compiled languages tend to be, so if you can't write a Hello World you really should give Python a shot. :D
ᕕ(ಠ‸ಠ)⊃━☆゚.*・。゚
Image
User avatar
Silverware
Vice Admiral
 
Posts: 2806
Joined: Sun Sep 07, 2014 3:23 pm
Location: Goattown-Three, Sigma Six, Goat Space

Re: If you want to learn how to program in Python...

Postby Dinosawer » Mon Jan 18, 2016 1:28 pm

Define serious? We often do numerical mathematical analyses in Python - those are pretty calculation intensive and work just fine :P
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Image
User avatar
Dinosawer
Admiral
 
Posts: 5665
Joined: Fri May 09, 2014 1:08 pm
Location: Belgium

Re: If you want to learn how to program in Python...

Postby Cha0zz » Mon Jan 18, 2016 1:46 pm

Python is used pretty often in academic context even :V
LT Wiki | IRC | REKT Wiki
Image
Idiots. Idiots everywhere. ~Dr. Cha0zz
User avatar
Cha0zz
Rear Admiral
 
Posts: 1833
Joined: Thu Aug 01, 2013 3:37 am
Location: Belgium

Re: If you want to learn how to program in Python...

Postby davdav » Mon Jan 18, 2016 1:47 pm

Silverware wrote:Py (at least non-compiled Py) is slow as balls for anything serious.
It's perfect for short scripts that need to be quickly cross-platform, because almost all Linux flavors install Py2 by default.

Py however is a good tool to learn to program with, as non-compiled languages tend to be, so if you can't write a Hello World you really should give Python a shot. :D


Openstack is fully written in python...
and it's not a short script but a full cloud management system.
davdav
Commander
 
Posts: 218
Joined: Fri Dec 05, 2014 6:42 am
Location: FR

Re: If you want to learn how to program in Python...

Postby Silverware » Mon Jan 18, 2016 2:56 pm

Dinosawer wrote:Define serious? We often do numerical mathematical analyses in Python - those are pretty calculation intensive and work just fine :P


Work fine, does not mean work fast :D
I can do the same numerical work in C or C++ much faster. :P
(or in JS at about the same speed, only with more work involved due to Py having the nice SciPy libs)

Python is great for the general use-case of calculating random shit. But it is not a speed demon, and is best used for one-off calculations, rather than trying to get thousands of calculations per second. :P


davdav wrote:Openstack is fully written in python...
and it's not a short script but a full cloud management system.


Yes, and I believe OpenStack is also compiled Python :P
ᕕ(ಠ‸ಠ)⊃━☆゚.*・。゚
Image
User avatar
Silverware
Vice Admiral
 
Posts: 2806
Joined: Sun Sep 07, 2014 3:23 pm
Location: Goattown-Three, Sigma Six, Goat Space

Re: If you want to learn how to program in Python...

Postby Dinosawer » Mon Jan 18, 2016 2:59 pm

Silverware wrote:Work fine, does not mean work fast :D
I can do the same numerical work in C or C++ much faster. :P

Not if you count in the time it takes you to program it. :ghost:
Silverware wrote:(or in JS at about the same speed, only with more work involved due to Py having the nice SciPy libs)

Scipy uses built in C++ functions, you aint gonna beat that with JS :P
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Image
User avatar
Dinosawer
Admiral
 
Posts: 5665
Joined: Fri May 09, 2014 1:08 pm
Location: Belgium

Re: If you want to learn how to program in Python...

Postby Talvieno » Mon Jan 18, 2016 3:02 pm

Guys, don't be silly. The only language everyone will agree is fast is C++. :lol:
User avatar
Talvieno
Community Manager
 
Posts: 8004
Joined: Fri Apr 11, 2014 6:50 pm
Location: North GA, USA

Re: If you want to learn how to program in Python...

Postby Silverware » Mon Jan 18, 2016 4:02 pm

Dinosawer wrote:
Silverware wrote:Work fine, does not mean work fast :D
I can do the same numerical work in C or C++ much faster. :P

Not if you count in the time it takes you to program it. :ghost:
Silverware wrote:(or in JS at about the same speed, only with more work involved due to Py having the nice SciPy libs)

Scipy uses built in C++ functions, you aint gonna beat that with JS :P


Yes, SciPy does use C++ modules, JS can call C++ modules also (when using node.js). :V
And depending on how often a thing needs to be run for the speed. If you need to run it lots, write it in C++, get the inherent speed boosts.
If you only need a prototype or to run it once, basically any language is fine.

Py is good for what it's good for, quickly made scripts.
C++ is good for what it's good for, fast execution of anything.
JS is good for what it's good for, cross-platform and browser scripting.
Java is good for what it's good for, string manipulation. (Yes Java only has this one useful feature)

See a pattern? :V
Learn everything and use the right language at the right time. ;)
ᕕ(ಠ‸ಠ)⊃━☆゚.*・。゚
Image
User avatar
Silverware
Vice Admiral
 
Posts: 2806
Joined: Sun Sep 07, 2014 3:23 pm
Location: Goattown-Three, Sigma Six, Goat Space

Re: If you want to learn how to program in Python...

Postby Dinosawer » Mon Jan 18, 2016 4:16 pm

I prefer Python's string handling, tbh
Java is good for Taiya. :ghost:

And Python can do way more than just scripts *coughs and points at REKT inventory manager I whipped up in a few weeks* :squirrel:
Warning: do not ask about physics unless you really want to know about physics.
The LT IRC / Alternate link || The REKT Wiki || PUDDING
Image
User avatar
Dinosawer
Admiral
 
Posts: 5665
Joined: Fri May 09, 2014 1:08 pm
Location: Belgium

Re: If you want to learn how to program in Python...

Postby Silverware » Mon Jan 18, 2016 4:29 pm

Didn't say it could only do the one thing, just that that happens to be the best use case for Python. :D

And Java is FAR faster with string manipulation than Python, not easier to use. ;)
I prefer JS's string manipulation myself.
ᕕ(ಠ‸ಠ)⊃━☆゚.*・。゚
Image
User avatar
Silverware
Vice Admiral
 
Posts: 2806
Joined: Sun Sep 07, 2014 3:23 pm
Location: Goattown-Three, Sigma Six, Goat Space

Next

Return to Scripting & Modding



Who is online

Users browsing this forum: No registered users and 2 guests