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

Code editors and style for Python 3

#1 Post by Wheelerof4te »

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.

My style:
I am mostly writing in procedural style, using classes for simple objects with modifiable attributes that need a shared method applied to them. I try to keep my code organized in small files, with classes and functions related to each other in one file. For the syntax style, PEP 8 rules, so it's 4 spaces indents, CamelCase for classes, lowercase for vars, _ as word separator, etc.
I wanted to learn more OOP style, but then quickly backtracked. I think it's silly to use classes outside of custom objects creation, just for the purpose of abstracting things. Python is an object oriented language, but I saw a lot of OO Python code and it is not pretty. And don't get me started on the inheritance mess. That concept needs to die. As do most of the libraries found on PyPi, using them sometimes feels like learning a new language, haha. Standard lib FTW.

Editors I tried are VS Code, Atom, Geany, PyCharm and of course IDLE. Now I am digging Gedit with a few plugins, since it's fast and light, forcing me to focus on my code. Atom and PyCharm are bloated mess, I don't even wanna start. Geany is a fine editor, but Gedit is cleaner. VS Code was nice, but it's still Electron application and it's developed by Microsoft, so no thank you. Also, VS Code complains about some things when you try to write around limitations like auto-appending to lists. Variable not used, like hell.

So, what are your favorite editors and programming styles for writing Python?
BTW, RIP Python 2. I haven't used it, but it sure was a great language.

print "Bye World!"

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

Re: Code editors and style for Python 3

#2 Post by Soul Singin' »

Wheelerof4te wrote:So, what are your favorite editors ... ?
Emacs
Wheelerof4te wrote:So, what are your favorite ... programming styles ... ?
Perl
Wheelerof4te wrote:Python?
Python has a lot of good libraries and it's very easy to write mathematical functions in the language, but for regular expressions and for organizing complex data structures, nothing beats Perl.

You can write regular expressions in Python, but first you have to import the module. Regular expressions are not an integral part of the Python language. Perl's regular expressions are much easier to use because they are an integral part of the Perl language.

Python has dictionaries, but Perl has scalars, arrays and hashes that are much easier to combine. Check out the Data Structures Cookbook by Tom Christiansen. It's like a Python dictionary on steroids.

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

Re: Code editors and style for Python 3

#3 Post by Soul Singin' »

Soul Singin' wrote:regular expressions in Python
Suppose you have the string "red socks" and you want to change it to "blue socks".

So let's start with the string. Strings are simple in both languages.

Python:

Code: Select all

clothes = 'red socks'
Perl:

Code: Select all

my $clothes = "red socks";
Now let's perform the substitution. In Python, you first have to compile the pattern. Only then can you perform the substitution. In Perl, there's only one step. And it's much shorter.

Python:

Code: Select all

pattern = re.compile('red')
clothes = pattern.sub('blue',clothes)
Perl:

Code: Select all

$clothes =~ s/red/blue/g;
Python requires you to remember the name of the variable that you saved the pattern to. That's horribly confusing.

Perl requires far less typing and the substitution is much clearer. It literally says: "replace 'red' with 'blue'."

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

Re: Code editors and style for Python 3

#4 Post by arzgi »

Good choice to start learning python! :D

I use only vim for programming, it has at least hundreds if not thousands plugins, many in Debian repo, rest in https://www.vim.org/scripts/index.php

I do most of my programming in python(3), so have installed autocompletion and syntax-checker plugins: jedi and syntastic. But these are not the only one like, vimballs are eaysy to install and purge, so I've tried many. If it does not feel right or is cumbersome to use, let if go!

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

Re: Code editors and style for Python 3

#5 Post by Head_on_a_Stick »

Vim ftw!
deadbang

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

Re: Code editors and style for Python 3

#6 Post by Wheelerof4te »

I once opened Vim. ...yeah. But I know it has a bunch of useful little plugins like above mentioned.

@Soul Singin':
Perl requires far less typing and the substitution is much clearer. It literally says: "replace 'red' with 'blue'."

I don't know, I never tried Perl, but that syntax feels confusing to me:
$clothes =~ s/red/blue/g;
What do 's' and 'g' mean? Switch, generate? So confusing, even that ~ is confusing and a PITA to write. But with Python:
clothes = pattern.sub('blue',clothes)
you know that .sub means substitute, and that it's a method of an instanced object "pattern".
It just feels more natural to write in Python to me, as I don't have to deal with syntax magic as in most other languages. Lack of syntax magic is also what drives me ever closer to Go, as it might be my next language. The only worry there is Google.

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

Re: Code editors and style for Python 3

#7 Post by Soul Singin' »

Wheelerof4te wrote:
Soul Singin' wrote:$clothes =~ s/red/blue/g;
What do 's' and 'g' mean? Switch, generate?

"s" means "substitute" and "g" means "globally" -- When you learn how to program, you'll notice that sed uses the same syntax.
Wheelerof4te wrote:So confusing, even that ~ is confusing and a PITA to write.
How is it a "PITA to write" two characters ("=~"), but not a "PITA to write" two whole lines?
Wheelerof4te wrote:But with Python:
Soul Singin' wrote:clothes = pattern.sub('blue',clothes)
you know that .sub means substitute, and that it's a method of an instanced object "pattern".
So you would seriously prefer to create an instanced object for every pattern you wish to match? Really? You don't think that's a "PITA to write"? Try writing a website scraper -- where you have to match dozens of patterns -- and I promise you that you will feel differently.
Wheelerof4te wrote:It just feels more natural to write in Python to me, as I don't have to deal with syntax magic as in most other languages.
So you would seriously prefer not to have any shortcuts to simplify your task? How is that not "PITA to write"?
Wheelerof4te wrote:Lack of syntax magic is also what drives me ever closer to Go, as it might be my next language. The only worry there is Google.
That a major technology company might devote time, money and resources to developing the language? That worries you? Seriously?

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

Re: Code editors and style for Python 3

#8 Post by Wheelerof4te »

Soul Singin' wrote:So you would seriously prefer not to have any shortcuts to simplify your task? How is that not "PITA to write"?
Same reason as C is much easier to learn and write than C++: because it's just a simpler language with far fewer features. I don't measure language's simplicity by lenght of lines written, but by amount of abstractions and syntax magic the language forces you to learn.
Soul Singin' wrote:How is it a "PITA to write" two characters ("=~")
I meant that it's PITA to write the character ~, at least on my keyboard. I usually don't reach for ` with SHIFT on my QWERTZ keys.
As for Google, I don't trust a Big Tech, monopolistic company based in USA, period.
Soul Singin' wrote:When you learn how to program
This is just rude. Let's not turn this thread into Perl vs Python flame war :D

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

Re: Code editors and style for Python 3

#9 Post by Soul Singin' »

Wheelerof4te wrote:
Soul Singin' wrote:How is it a "PITA to write" two characters ("=~")
I meant that it's PITA to write the character ~, at least on my keyboard. I usually don't reach for ` with SHIFT on my QWERTZ keys.
Seriously? You would prefer to write several lines of code than to reach once for the upper left corner of the keyboard?
Wheelerof4te wrote:Let's not turn this thread into Perl vs Python flame war :D
Of course, not. Python already lost that war.

For example, if you study Natural Language Processing, you'll notice that Python's mosestokenizer module consists of wrappers for the Perl scripts in the Moses toolkit. Why does the Python module use wrappers? because it's easier to write regular expressions in Perl.

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

Re: Code editors and style for Python 3

#10 Post by arzgi »

Perl brain washing, yea! :mrgreen: Every programming language has it's own strengths, but is it really necessary advocate your preferred language, when someone ask advice for his/her selected.

I would say python is better for general purpose programming than perl (let the flame war begin!). I'm only a hobby programmer, for me python, bash, sed and awk have been good enough. Depends what you need.

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

Re: Code editors and style for Python 3

#11 Post by Head_on_a_Stick »

POSIX sh ftw! :mrgreen:

But seriously, Go was created by Rob Pike & Ken Thompson. Don't let the big G association put you off, it's not like they can control your programs if you write them in Go.
deadbang

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

Re: Code editors and style for Python 3

#12 Post by Soul Singin' »

arzgi wrote:Perl brain washing, yea! :mrgreen: Every programming language has it's own strengths, but is it really necessary advocate your preferred language, when someone ask advice for his/her selected.
The very first sentence that I wrote in this thread was:
Soul Singin' wrote:Python has a lot of good libraries and it's very easy to write mathematical functions in the language,
In other words, you pick the tool that's right for the job. Python has its strengths. A lot of them. The war occurs because inexperienced Python users ignore its weaknesses.

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

Re: Code editors and style for Python 3

#13 Post by Wheelerof4te »

Soul Singin' wrote:. Python has its strengths. A lot of them. The war occurs because inexperienced Python users ignore its weaknesses
I didn't write about any of the Perl's weaknesess, and I'm sure it has plenty. As I said in one of my previous posts, Perl is not my forte, nor did I write a single line of Perl in my life. So stop with "muh Perl is better than Python", none asked and no one cares. What I do care is what is your prefered programming style, that was the question. Is it procedural, pure functional, or OOP?
Not the language, the language in this thread is Python, although you can talk about your experiences with other languages. I want to choose my second language, so far that choice is Go.
Head_on_a_Stick wrote:But seriously, Go was created by Rob Pike & Ken Thompson. Don't let the big G association put you off
Yeah, Ken is the big name in Unix and C. I will consider it, thanks.

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

Re: Code editors and style for Python 3

#14 Post by Soul Singin' »

Wheelerof4te wrote:
Soul Singin' wrote:. Python has its strengths. A lot of them. The war occurs because inexperienced Python users ignore its weaknesses
I didn't write about any of the Perl's weaknesess, and I'm sure it has plenty. As I said in one of my previous posts, Perl is not my forte, nor did I write a single line of Perl in my life. So stop with "muh Perl is better than Python", none asked and no one cares.
The war occurs because inexperienced Python users ignore its weaknesses.

Thank you for proving my point.

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

Re: Code editors and style for Python 3

#15 Post by Wheelerof4te »

So, I have to write Perl to be considered an experienced Python programmer?!
Yeah, no.

@HoaS:
How did you install Go? Using apt, or by downloading it from golang.org? Will Go 1.11 be enough for a beginner? I don't want to bother with modules right now.

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 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: 132 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

Post Reply