Code editors and style for Python 3

Need help with C, C++, perl, python, etc?

Re: Code editors and style for Python 3

Postby Head_on_a_Stick » 2020-01-02 15:03

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
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12305
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Code editors and style for Python 3

Postby neuraleskimo » 2020-01-02 16:49

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...
Black Lives Matter
User avatar
neuraleskimo
 
Posts: 186
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-02 17:12

^ 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.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby pylkko » 2020-01-03 10:05

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
pylkko
 
Posts: 1777
Joined: 2014-11-06 19:02

Re: Code editors and style for Python 3

Postby Head_on_a_Stick » 2020-01-03 10:11

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/
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12305
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-03 10:44

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

It's like a much improved nano.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby Head_on_a_Stick » 2020-01-03 11:26

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

If it's good enough for Rob Pike & Dennis Ritchie...
Black Lives Matter

Debian buster-backports ISO image: for new hardware support
User avatar
Head_on_a_Stick
 
Posts: 12305
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: Code editors and style for Python 3

Postby arzgi » 2020-01-03 13:14

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: 638
Joined: 2008-02-21 17:03
Location: Finland

Re: Code editors and style for Python 3

Postby arzgi » 2020-01-03 13:40

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.
arzgi
 
Posts: 638
Joined: 2008-02-21 17:03
Location: Finland

Re: Code editors and style for Python 3

Postby Soul Singin' » 2020-01-03 16:43

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.
User avatar
Soul Singin'
 
Posts: 1607
Joined: 2008-12-21 07:02

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-03 17:53

^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.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby pylkko » 2020-01-03 20:31

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 shit. 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.
User avatar
pylkko
 
Posts: 1777
Joined: 2014-11-06 19:02

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-03 20:50

^ 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.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

Re: Code editors and style for Python 3

Postby neuraleskimo » 2020-01-03 21:16

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?
Black Lives Matter
User avatar
neuraleskimo
 
Posts: 186
Joined: 2019-03-12 23:26
Location: Bloomington, Indiana, USA

Re: Code editors and style for Python 3

Postby Wheelerof4te » 2020-01-03 21:26

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.
Wheelerof4te
 
Posts: 1454
Joined: 2015-08-30 20:14

PreviousNext

Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable