Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Code editors and style for Python 3

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Code editors and style for Python 3

#16 Post by Head_on_a_Stick »

Wheelerof4te wrote:How did you install Go?
The golang package is the one you want. The version in stable should be fine: https://golang.org/doc/go1compat
deadbang

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Code editors and style for Python 3

#17 Post by neuraleskimo »

Wheelerof4te wrote:I started writing Python about 5 months ago and I am loving it. It allows me to automate some of the tasks I do, experiment with useful CLI programs, even write a game or two.
Congratulations! Python is a great language.
Wheelerof4te wrote: So, what are your favorite editors and programming styles for writing Python?
For a great many years (and more than I would like to admit ;-)), I used Emacs and other text editors. Occasionally, I would use an IDE, but never really liked any that I used. A few years ago, I started using Atom and really liked it. Now, I use it almost exclusively.

I program in whatever style suits the problem. A lot of my Python code looks much like C: simple functions and structs. However, OOP and inheritance have its place. Aside from the textbook example of shapes, inheritance is the best tool for some real problems where you want a standard interface across a group of related things. Having said that, I strongly prefer simple, concrete structs where I want to handle related data as a single "thing". Sometimes recursion and other functional programming techniques are the best tool (or programming style). BTW, list comprehensions are cool. My advice: learn how to use the major programming styles (or paradigms) in Python and then pull out each of the styles when needed.

Again, good job! Let me know if I can help you on this journey...

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#18 Post by Wheelerof4te »

^ Thanks for the tips. I am struggling with inheritance, but luckily see no need for it right now in my programs. Currently, I'm creating a library for finance-related problems. Here's a simple class for instancing Book of Incoming Invoices entries:

Code: Select all

import csv

class BookInc:
    pattern = "kuf: 02-" # in Serbian
    entries = []

    def __init__(self, num: int, year: int, com_name: str, com_seat: str, amount: float, date=""):
        self.num = self.pattern + str(num) + "/" + str(year)
        self.com_name = com_name
        self.com_seat = com_seat
        self.amount = amount
        self.date = date
        BookInc.entries.append(self)

    @staticmethod
    def make_csv(head1: str, head2: str, head3: str, head4: str, head5: str, filename="BookInc2020.csv"): # add as many as needed
        with open(filename, 'w', newline='') as csvfile: # again, Serbian. Change to BookInc.csv
            writer = csv.writer(csvfile, delimiter=' ', quoting=csv.QUOTE_MINIMAL)
            writer.writerow([head1] + [head2] + [head3] + [head4] + [head5])
            for entry in BookInc.entries:
                writer.writerow([entry.num] + [entry.com_name] + [entry.com_seat] + [entry.amount] + [entry.date])
As you can see, there is no need to append entries in your script, it is done automatically. And yes, those are type annotations in Python :D

EDIT: I added the filename as a paremeter to the method per suggestion below.
Last edited by Wheelerof4te on 2020-01-03 18:09, edited 1 time in total.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Code editors and style for Python 3

#19 Post by pylkko »

What is a good editor depends - of course - on what you need. Simple text editors lack features that might be requirements for other people. For example, more complex editors allow you to connect to multiple kernels, even non-local ones. Many IDE's also allow you to use local or remote git to do version control and share code. You can imagine that these are requirements for many people. Linters, autocoplete, auto-indent and autostyle also make it easier to create readable consistent code. Many IDE's also have documentation, so that things can open pop-ups with documentation on their use while you are using them (as opposed to having to have another window with a browser or whatever for looking up stuff). Step-through can also seriously help debugging.

In my experience most people that say something like "vim is the only way" or "use ed, like I do". don't actually produce much code. Being mininalist is fine and nice, but once you hit the point that it hurts productivity, it's not smart any more.

Some python editors/IDE's that I have not seen mentioned yet, but which are lighter than PyCharm or Atom are:

Thonny (https://thonny.org/)
Spyder (https://pypi.org/project/spyder/)
Ninja (http://www.ninja-ide.org/)

Also, gedit is fairly simple and stable. In the add-ons there are Python syntax high-lighting, a Python terminal git highlighting and other things that can be used to make it a fairly light-weight editor that nevertheless offers more useful functionality than just a simple text editor.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Code editors and style for Python 3

#20 Post by Head_on_a_Stick »

pylkko wrote:In my experience most people that say something like "vim is the only way" or "use ed, like I do". don't actually produce much code. Being mininalist is fine and nice, but once you hit the point that it hurts productivity, it's not smart any more.
Although this is certainly true for me (I couldn't code my way out of a wet paper bag) I don't think it's true for the hardcore vim coders, once you're used to it's methods the text entry is blindingly efficient.

EDIT: https://sanctum.geek.nz/arabesque/series/unix-as-ide/
deadbang

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#21 Post by Wheelerof4te »

If you like terminal-based editors, check out Micro:
https://github.com/zyedidia/micro

It's like a much improved nano.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 133 times

Re: Code editors and style for Python 3

#22 Post by Head_on_a_Stick »

Left-field approach: http://acme.cat-v.org/

If it's good enough for Rob Pike & Dennis Ritchie...
deadbang

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Code editors and style for Python 3

#23 Post by arzgi »

pylkko wrote: In my experience most people that say something like "vim is the only way" or "use ed, like I do". don't actually produce much code. Being mininalist is fine and nice, but once you hit the point that it hurts productivity, it's not smart any more.

Also, gedit is fairly simple and stable. In the add-ons there are Python syntax high-lighting, a Python terminal git highlighting and other things that can be used to make it a fairly light-weight editor that nevertheless offers more useful functionality than just a simple text editor.

I also pointed out how many useful plugins for programmers there are for vim, certainly more than for gedit. I told what I've done, not saying it's the only way.

If you are used to vim, it hardly hurts productivity.

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Code editors and style for Python 3

#24 Post by arzgi »

Head_on_a_Stick wrote:[
Although this is certainly true for me (I couldn't code my way out of a wet paper bag) I don't think it's true for the hardcore vim coders, once you're used to it's methods the text entry is blindingly efficient.

EDIT: https://sanctum.geek.nz/arabesque/series/unix-as-ide/
My older brother is a professional coder. in one of his workplaces the lead programmer used M$ Word for coding, it was the 'editor' he knew best.

User avatar
Soul Singin'
Posts: 1605
Joined: 2008-12-21 07:02

Re: Code editors and style for Python 3

#25 Post by Soul Singin' »

Wheelerof4te wrote:

Code: Select all

        with open("KUF.csv", 'w', newline='') as csvfile: # again, Serbian. Change to BookInc.csv
Instead of specifying a file name in the class, give yourself the option to choose the file name each time you run the program.

One way you could do that is to set a default (like "KUF.csv"), but accept a different file name from a command-line argument:

Code: Select all

##  default file to write to
csv_file = 'KUF.csv'

##  option to write to a different CSV file
def argparser():
    Argparser = argparse.ArgumentParser()
    Argparser.add_argument('--csv', type=str, default = csv_file , help='CSV output file')

    args = Argparser.parse_args()
    return args

##  parse the arguments
args = argparser()
The only argument here is the output file. In practice, you should also add the input file (or files) to that function.

Then in your class:

Code: Select all

        with open(args.csv, 'w', newline='') as csvfile:
which would default to "KUF.csv", while also allowing you to specify a different file name each time you run the program.

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#26 Post by Wheelerof4te »

^Those are really good tips, I never bothered with arguments as I found the explanations bad. I might implement the way to choose a file name in the library instead. Since that class is part of a specific library, once you set the file name it should not change at least for a year.

For example "KUF2019.csv", "KUF2020.csv", depending on which year the Book of incoming invoices is meant for.

Implementation of a program would look like this:

Code: Select all

from fin.books import BookInc # fin is a package with books.py module

def main():
    # Entries:
    r1 = BookInc(1344, 19, "FEDEX", "New York", 203.34, "21.05.2019.")
    r2 = BookInc(1345, 19, "The Expose", "Alabama", 178.54,)
    r3 = BookInc(1346, 19, "Electro Cars", "Memphis", 345.43, "23.05.2019.")
    r4 = BookInc(1347, 19, "Aramco", "Riyad", 300.23, "25.05.2019.")
    r5 = BookInc(1348, 19, "Amar Trade", "Dubai", 300.00, "27.05.2019.")
    r6 = BookInc(1349, 19, "Dunav Osiguranje", "Belgrade", 304.00, "27.05.2019.")
    r7 = BookInc(1350, 19, "Brent Crude", "Washington D.C.", 250.60,)
    r8 = BookInc(1351, 19, "ShellCo", "Texas", 367.56, "29.05.2019.")

    # Make .csv file:
    BookInc.make_csv("Num./Year.", "Company", "Seat", "Amount", "Date", filename="BookInc2019.csv")

# Start the program:
if __name__ == "__main__":
    main()
Anyway, it is a good advice, and I will include a parameter to define an output filename.
Last edited by Wheelerof4te on 2020-01-03 20:40, edited 1 time in total.

User avatar
pylkko
Posts: 1802
Joined: 2014-11-06 19:02

Re: Code editors and style for Python 3

#27 Post by pylkko »

Head_on_a_Stick wrote:
pylkko wrote:In my experience most people that say something like "vim is the only way" or "use ed, like I do". don't actually produce much code. Being mininalist is fine and nice, but once you hit the point that it hurts productivity, it's not smart any more.
Although this is certainly true for me (I couldn't code my way out of a wet paper bag) I don't think it's true for the hardcore vim coders, once you're used to it's methods the text entry is blindingly efficient.

EDIT: https://sanctum.geek.nz/arabesque/series/unix-as-ide/
Sorry, I didn't mean to come off all elitist and crap. But there were two points 1) the larger the project is, the more likely you will need something more than a text editor 2) using vim and countless other programs, extensions (e.g UNIX as IDE) isn't really light weight, you have all the same functionality as a full blown IDE only spread out into pieces.

But Atom and PyCharm are not even bloated, try netbeans or android studio :o

I think the bottom line is that you should use what works best. this applies not only to editors, but also to the language used, or if you should use a higher level library or lower level basic elements. in each and every one of these situation you should use all of these options (they are not mutually exclusive) when they suite you the best. this is the problem with universal claims here. the only circumstances where perl is better than python, a library is better or not than using lower level basic elements, or a simple text editor is better than an IDE is when your coding style is very narrow and particular to you. and in this case these claims are not likely to apply to other situtations...

edit: yeah I know a few professionals that use vim also, so I don't want to totally rain down on that. nevertheless, I'd say that a lot of people make that statement out of "ideological" and non-pragmatic reasons, which I jsut wanted to point out.

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#28 Post by Wheelerof4te »

^ Sometimes, IDE's and editors such as VS Code can be limiting. For example, in the script above, VS Code would complain that my variables r1, r2, r3, etc. are unused, which is not true. They are instanced and added to the class entry list, as they should be. I had it complain before because of some other stupid things, like not recognising self.owner assigments.

That stuff only confuses you. You think your program is not behaving right, except it is. Another thing, when you have some large project, it can take a while before it's loaded in a heavy IDE such as PyCharm. In Gedit, Kate or Geany, it would load much faster.

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Code editors and style for Python 3

#29 Post by neuraleskimo »

Wheelerof4te wrote:^ Thanks for the tips. I am struggling with inheritance, but luckily see no need for it right now in my programs.
I am happy to see if I can help, but where specifically are you stuck? On the concepts? When/how to use the inheritance? Writing the code itself?

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#30 Post by Wheelerof4te »

I don't really know how to use it and how to write (better said access) it. I know that I can define a base class and use it to create other sub-classed classes (urgh..). Then, the new class shares the attributes and methods of a base class. And that is all I know.

Honestly, it is very messy for me. I see classes only as a collection of associated attributes and functions (methods). Basically as structs. I can't wrap my finger around the concept of OOP yet.

neuraleskimo
Posts: 195
Joined: 2019-03-12 23:26

Re: Code editors and style for Python 3

#31 Post by neuraleskimo »

pylkko wrote:Sorry, I didn't mean to come off all elitist and crap.
No problem. I don't listen to people who use IDE's. Just joking. ;-)

I don't IDE's them because I rarely need the features. I primarily write C or C++ code, but rarely use the compile button and then the run button. Instead, I would rather control-tab to my terminal and hit the up arrow + return. I haven't used a debugger in decades. Instead, I write lots of tests, fully instrument my code, and use liberal logging. Besides running a debugger is hard or impossible in lots of scenarios. Firefox is the defacto documentation viewer. On and on... I will say that auto-completion is pretty nice SOMETIMES: when it is correct and when there aren't 50 variations in the popup.

My experience anyway. I won't think less of you for requiring training-wheels on your development tools! Just joking. ;-) Seriously, there are some nice IDE's and I am probably just a curmudgeon.

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Code editors and style for Python 3

#32 Post by arzgi »

Wheelerof4te wrote:I don't really know how to use it and how to write (better said access) it. I know that I can define a base class and use it to create other sub-classed classes (urgh..). Then, the new class shares the attributes and methods of a base class. And that is all I know.

Honestly, it is very messy for me. I see classes only as a collection of associated attributes and functions (methods). Basically as structs. I can't wrap my finger around the concept of OOP yet.
There are many books of OOP programming, it's a wide subject. For my own projects, rarely have needed any classes (outside school projects so many years ago...), so I most program python in functional style. Blame M$ extended basic, which was the first I learned :mrgreen: Then if I write a program to some less technically oriented, some light GUI-frontend using tkinter (python3-tk package).

Again, use what you need, there is no single solution that works everywhere, and often there are more ways to achieve same result.

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#33 Post by Wheelerof4te »

arzgi wrote:Again, use what you need, there is no single solution that works everywhere, and often there are more ways to achieve same result.
I fuly agree!
Ok, it's time to share my completed game with you all. Some of you remember how I started a thread about it a few months back. Well, now it is complete, but I deleted my github account. Nevertheless, here is a copy from my Google drive:
https://drive.google.com/open?id=1gilOe ... nvQDy6WSL4

If you are on Buster, make sure you have python3-libtcod installed in order to run the game. Otherwise, install python-tcod and change the import statement to read
import tcod as tcod
Controls are in the README file. Read the TIPS too, the game isn't easy :)

Enjoy!
Last edited by Wheelerof4te on 2020-01-13 11:48, edited 1 time in total.

arzgi
Posts: 1194
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: Code editors and style for Python 3

#34 Post by arzgi »

If you are interested in game programming, pygame might be worth checking (python3-pygame package).

Wheelerof4te
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

#35 Post by Wheelerof4te »

^To be honest, no, I'm not so interested in game development. At least not with Python. I have written this game ever since I started learning Python and it's very dear project to me. It shows all my rookie mistakes (only one file, heavy usage of globals, etc.). Ever since, I have grown out of my game devel dream and started using Python for what it's good at.

Pygame is also against what I have written before in this thread: I don't want to learn some 3rd party library because it feels like learning a new language. Especially if it's just a wrapper library for some C/C++ code.

Post Reply