Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?

12 rats tied together posted:

the context manager is pretty easy and is a great python feature, you basically just define __enter__ and __exit__ methods

There's a simpler syntax since (I think) 3.5, using the @contextmanager decorator:

Python code:
from contextlib import contextmanager
import os

@contextmanager
def managed_tempfile(path):
    fh = open(path, 'w+')
    try:
        yield fh
    finally:
        fh.close()
        os.remove(path)

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

D34THROW posted:

Already kicking myself. I've got 4 kids and sometimes I want to randomly pick one for something, or randomize the order they do things. I typically use a d4 for this...but I just thought "hey, I'm learning Python, why not build an app for it"? Android development on Windows looks like fun! :suicide:

All seriousness though i adore a challenge so...any advice is more than welcome.

I thought Android was all Java?

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

QuarkJets posted:

I thought Android was all Java?

There are wrappers that, if I understand correctly, translate python to java bytecode for Android purposes. There's also Kotlin and another language or two supported.

Loezi
Dec 18, 2012

Never buy the cheap stuff
I'm procrastinating by sketching out an elective python course for late BSc/early MSc CS students who already have a bunch of programming experience in general and have done some basic python stuff. This would not be a course on programming in general, on specific 3rd party libraries, on DS/ML etc, but more of a "here's a bunch of more python stuff that might be useful".

I've drafted the following learning objectives, and would welcome any feedback on stuff I've completely missed. Note that there's some basic stuff (e.g. sets) to cover gaps in their introductory programming courses:

quote:

# Part 1: On good python
- You have read and know how to access the Zen of Python
- You know what PEPs are in general
- You know what PEP8 is and have skimmed through it
- You know of some of the most common critiques of PEP8
- You are aware of at least one alternative to PEP8
- You are aware of at least one automated formatting tool (`black`)
- You know how to document python code with docstrings

# Part 2: Types and type hints
- You are able to interpret and use type hints
- You are able to use the most common parts of the `typing` module: `List`, `Set`, `Dict`, `Optional`, `Any` and `Union`
- You are able to use the more complex members of the `typing` module, such as `Callable`, `TypeVar`, `Generic`, `NewType`, and `Protocol`
- You have heard of contravariance, covariance and invariance, and have a basic understanding of how these concepts relate to python generics
- You know the difference between structural and nominal typing, and know how these relate to python type hints
- You know here to learn more about the last two points

# Part 3: On Sequences and Iterability
- (Refresher on comprehensions)
- You understand unpacking, including the use of the star operator
- You can use slicing to obtain different subsequences from sequences
- You know how to make your own data structure sliceable and indexable
- You understand how the keyword `else` functions in relation to for loops
- You know how to use the most important iteration helpers `enumerate` and `zip`
- You can use `zip` together with the star operator
- You know how the magic methods `__iter__`, `__next__` and `__getitem__` relate to iterability, and can implement them for a simple data structure of your

# Part 4: Advanced functions
- You are aware of the problem with mutable default arguments and know how to address the issue
- You can use variable and key-value arguments
- You know what lambdas are, and how they relate to standard functions
- You can create your own lambdas and use them with methods such as `min` and `max`
- You know how to make functions that take functions as arguments
- You understand what a `Callable` is and know how to implement a `__call__` method
- You understand what generators are, and can create simple generators using the `yield` keyword
- You know what a closure is
- You are aware of the lambda variable binding problem, and how it can be avoided

# Part 5: Classes and other abstractions
- You can use named tuples and data classes, and know how they relate to the standard classes
- You know the difference between the `__str__` and `__repr__` methods
- (Refresher on inheritance, originally covered on a previous course)
- You know what Method Resolution Order is and how it influences subclasses
- You are familiar with the concept of an abstract class
- You know how to create an abstract class using the `ABC` module
- You are aware of the concept of mixins, and know of some common, useful, ones
- You are understand how classes relate to dictionaries and can use the `__dict__` method
- You are aware of the `__slots__` property and what it can be used for
- You can use and influence `getattr` and `setattr` methods

# Part 6: Data structures
- You know of and can use basic python data structures such `set`, `defaultdict`, `Counter`, `deque` and `OrderedDict`
- You know where in the standard library to find further useful data structure
- You know how the following magic methods can be used to construct your own data structures: `__len__`, `__getitem__`, `__setitem__`, `__delitem__`, `__missing__`, `__iter__`, `__reversed__`, `__contains__`

# Part 7: Context managers, decorators and concurrency
- You know of and can use context managers and the `with as` syntax
- You can create simple context managers with `__enter__` and `__exit__` methods
- You know of and can create simple decorators
- You understand what the Global Interpreter Lock is, and how it relates to the concurrency of python programs
- You can use the `async` keyword in simple ways
- You know of the principal differences between `pypy` and `cpython` python implementations

# Part 8: Libraries
- You know of some useful standard library modules, such as `unittest`, `argparse`, `Pathlib` and `itertools`
- You know what `pip` and `venv` are, and how they can be used to incorporate 3rd party libraries into your python projects

# Misc stuff that needs to go somewhere
- a brief discussion on "is" vs. "==", incl. "is None"
- data type quirks (bools are ints and how that relates to sums, small ints and "is")

Soylent Pudding
Jun 22, 2007

We've got people!


Loezi posted:

I'm procrastinating by sketching out an elective python course for late BSc/early MSc CS students who already have a bunch of programming experience in general and have done some basic python stuff. This would not be a course on programming in general, on specific 3rd party libraries, on DS/ML etc, but more of a "here's a bunch of more python stuff that might be useful".

I've drafted the following learning objectives, and would welcome any feedback on stuff I've completely missed. Note that there's some basic stuff (e.g. sets) to cover gaps in their introductory programming courses:

This looks like the python course I wish I could take.

duck monster
Dec 15, 2004

DoctorTristan posted:

There's a simpler syntax since (I think) 3.5, using the @contextmanager decorator:

Python code:
from contextlib import contextmanager
import os

@contextmanager
def managed_tempfile(path):
    fh = open(path, 'w+')
    try:
        yield fh
    finally:
        fh.close()
        os.remove(path)


Its *almost* at the point where they'll cut the nonsense and just implement the drat anonymous blocks from Ruby, like they should have done a decade and a half ago.

I love python, but I'm perpetually annoyed at the contortions GvR put python through to avoid having to implement anonymous function. I get the proliferation of poo poo DSLs that ruby suffered was probably a hard "hell no"", but honestly, it just feels like the one area where python is a decade behind the competition.

Hell even loving pascal (http://docwiki.appmethod.com/appmethod/1.13/topics/en/Anonymous_Methods_in_Object_Pascal) has them, and that things a *dinosaur*.

(a dinosaur I still have a soft spot for, as my original programming language)

duck monster
Dec 15, 2004

edit: deleted, I didnt read your comment properly lol. My bad.

a foolish pianist
May 6, 2007

(bi)cyclic mutation

duck monster posted:

Hell even loving pascal (http://docwiki.appmethod.com/appmethod/1.13/topics/en/Anonymous_Methods_in_Object_Pascal) has them, and that things a *dinosaur*.

(a dinosaur I still have a soft spot for, as my original programming language)

My high school AP computer science class was in Pascal, back in like 1997.

Data Graham
Dec 28, 2009

📈📊🍪😋



a foolish pianist posted:

My high school AP computer science class was in Pascal, back in like 19973.

And it was considered pretty archaic then

Precambrian Video Games
Aug 19, 2002



Loezi posted:

I'm procrastinating by sketching out an elective python course for late BSc/early MSc CS students who already have a bunch of programming experience in general and have done some basic python stuff. This would not be a course on programming in general, on specific 3rd party libraries, on DS/ML etc, but more of a "here's a bunch of more python stuff that might be useful".

I've drafted the following learning objectives, and would welcome any feedback on stuff I've completely missed. Note that there's some basic stuff (e.g. sets) to cover gaps in their introductory programming courses:

That looks like a pretty good outline and also one that I wish I'd gone through before I started having to use Python. I think basically everything in that list is covered by Fluent Python (2nd Ed.), which I found an excellent read (though I don't think it's quite finished yet, but if you have institutional access to O'Reilly online it's a great resource). The very first section is "The Python Data Model" and goes over the Python data model - particularly magic/dunder methods - in a much more digestible way than the official docs, so that approach might be worth considering.

quote:

- You have heard of contravariance, covariance and invariance, and have a basic understanding of how these concepts relate to python generics

... I have not ... or if I did, it was ~16 years ago and I've forgotten...

quote:

- You know of and can use basic python data structures such `set`, `defaultdict`, `Counter`, `deque` and `OrderedDict`

Out of curiosity, are you planning to tell them anything about OrderedDict besides that dicts are ordered by default and so it's being deprecated in 3.10? Also, for what it's worth (not much), I checked the codebase I'm working on and found no usages of deque in Python code, though there were some std::deque usages in C++ code.

12 rats tied together
Sep 7, 2006

duck monster posted:

Its *almost* at the point where they'll cut the nonsense and just implement the drat anonymous blocks from Ruby, like they should have done a decade and a half ago.

To be honest I prefer the explicit __enter__ and __exit__ definitions to the decorator that converts a try/catch into something that can be context managed, but I have a probably unreasonable hate of try/catch in general. When I write python at work, it's usually for / around people who are not python developers, so I try to stick to code patterns that are either single line comprehensions that I can explain with a comment, or "c# without curly braces".

It would be cool to work somewhere I can assume that everyone knows what a decorator is.

eXXon posted:

Also, for what it's worth (not much), I checked the codebase I'm working on and found no usages of deque in Python code, though there were some std::deque usages in C++ code.
I use deque quite heavily in a personal project (an authoritative server for an online video game), but I will admit that I don't see it a lot in production business logic. I think you could probably teach deques as a decent stand-in for something like celery/sidekiq/faktory and students would have something that they can conceptually map to when working with network queues or pubsub systems in their jobs.

It seems like in general a queue is not particularly useful without an event loop, and most python courses I've seen never really get that far.

Loezi
Dec 18, 2012

Never buy the cheap stuff

Soylent Pudding posted:

This looks like the python course I wish I could take.

eXXon posted:

That looks like a pretty good outline and also one that I wish I'd gone through before I started having to use Python.

Thanks!

eXXon posted:

I think basically everything in that list is covered by Fluent Python (2nd Ed.), which I found an excellent read (though I don't think it's quite finished yet, but if you have institutional access to O'Reilly online it's a great resource). The very first section is "The Python Data Model" and goes over the Python data model - particularly magic/dunder methods - in a much more digestible way than the official docs, so that approach might be worth considering.

This is a good suggestion! I don't seem to have access to this, but I'll talk to the library folks.

eXXon posted:

... I have not ... or if I did, it was ~16 years ago and I've forgotten...

Basic idea, as far as I've understood it: You have "Type" and "Subtype", and it's clear that you can call a function expecting a Type argument with a Subtype argument. But are you allowed to call a function expecting a Container[Type] with a Container[Subtype]? Intuition might say "yes", but what if the functions adds a new Type object to the Container? To simplify a bit, covariance basically means "yes", contravariance means "no, but the other way around is okay" and invariance (the python default) means "only the exact type I specified is allowed". PEP484 has a section on this but it's not something you'd necessarily ever run across.

eXXon posted:

Out of curiosity, are you planning to tell them anything about OrderedDict besides that dicts are ordered by default and so it's being deprecated in 3.10? Also, for what it's worth (not much), I checked the codebase I'm working on and found no usages of deque in Python code, though there were some std::deque usages in C++ code.

Completely forgot about the ordered dictionaries thing. I think I'll need to cover my bases and talk a bit about this whole mess.

12 rats tied together posted:

It seems like in general a queue is not particularly useful without an event loop, and most python courses I've seen never really get that far.

I mostly wanted to talk about deque to 1) contrast the performance of dequeue.appendleft(v) and list.insert(0, v) and 2) highlight the existence of many data structures they might not have thought of.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
I am both a hobbyist and a standards nut. Therefore, rewiring my brain to write PEP8-compliant code is a massive dick. Namely, I'm so used to using camelCase for variable names and PascalCase for function names that going to lowercase_with_underscores is loving killing me. Never mind the trailing newline on files, making sure I'm not leaving extra whitespace at the end of lines, and the most wonderful part...staying within 72 characters for comments and 100 characters for code. That's really fun when I'm 5 indents deep for the function alone, then doing something with a long string that's broken into 18 lines because the actual string doesn't start until column 76 and PEP8 wants the continuation to be at or past the original indentation.

12 rats tied together
Sep 7, 2006

thats certainly a noble goal but do note that pep8 stresses consistency (and knowing when to be inconsistent) over any of its individual recommendations. you can camelCase if you want to and you'll be pep8 compliant so long as you are consistently camelCase

i set all my poo poo to 2 spaces instead of 4

e:

Loezi posted:

I mostly wanted to talk about deque to 1) contrast the performance of dequeue.appendleft(v) and list.insert(0, v) and 2) highlight the existence of many data structures they might not have thought of.
Cool, that's a good call. Getting students into the habit of checking the time complexity table on the wiki is a fantastic idea, I wish more people were aware of it

12 rats tied together fucked around with this message at 19:00 on Aug 5, 2021

necrotic
Aug 2, 2005
I owe my brother big time for this!

D34THROW posted:

I am both a hobbyist and a standards nut. Therefore, rewiring my brain to write PEP8-compliant code is a massive dick. Namely, I'm so used to using camelCase for variable names and PascalCase for function names that going to lowercase_with_underscores is loving killing me. Never mind the trailing newline on files, making sure I'm not leaving extra whitespace at the end of lines, and the most wonderful part...staying within 72 characters for comments and 100 characters for code. That's really fun when I'm 5 indents deep for the function alone, then doing something with a long string that's broken into 18 lines because the actual string doesn't start until column 76 and PEP8 wants the continuation to be at or past the original indentation.

Use black.

CarForumPoster
Jun 26, 2013

⚡POWER⚡

D34THROW posted:

I am both a hobbyist and a standards nut. Therefore, rewiring my brain to write PEP8-compliant code is a massive dick. Namely, I'm so used to using camelCase for variable names and PascalCase for function names that going to lowercase_with_underscores is loving killing me. Never mind the trailing newline on files, making sure I'm not leaving extra whitespace at the end of lines, and the most wonderful part...staying within 72 characters for comments and 100 characters for code. That's really fun when I'm 5 indents deep for the function alone, then doing something with a long string that's broken into 18 lines because the actual string doesn't start until column 76 and PEP8 wants the continuation to be at or past the original indentation.

You're worried about things that disappear if you use PyCharm and set it up the way you want. I'd assume VScode would be true as well. I've literally never once thought about how many characters to stay within for comments. If I make a long one I see a yellow squiggly, right click, refactor.

necrotic posted:

Use black.

12 rats tied together posted:

thats certainly a noble goal but do note that pep8 stresses consistency (and knowing when to be inconsistent) over any of its individual recommendations. you can camelCase if you want to and you'll be pep8 compliant so long as you are consistently camelCase

i set all my poo poo to 2 spaces instead of 4

At least a few people who hire python peeps in this thread, me included, have said that camelCase var names for python coders is a red flag for bad code. Not a reason to not hire someone, but it will stick out like a sore thumb.

pmchem
Jan 22, 2010


necrotic posted:

Use black.

this

12 rats tied together
Sep 7, 2006

CarForumPoster posted:

At least a few people who hire python peeps in this thread, me included, have said that camelCase var names for python coders is a red flag for bad code. Not a reason to not hire someone, but it will stick out like a sore thumb.

Agree, I wouldn't have mentioned it if OP didn't indicate that they are primarily a hobbyist. Submitting python code as part of a python tech interview to a python software shop with camelCase is not a great way to communicate "I will be pleasant to work with and productive in your ecosystem".

necrotic posted:

Use black.
yeah

Presto
Nov 22, 2002

Keep calm and Harry on.

12 rats tied together posted:

i set all my poo poo to 2 spaces instead of 4
You're on the list now, pal. :colbert:

12 rats tied together
Sep 7, 2006

there was one thing ruby got right, and it was spacing :twisted:

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

CarForumPoster posted:

You're worried about things that disappear if you use PyCharm and set it up the way you want. I'd assume VScode would be true as well. I've literally never once thought about how many characters to stay within for comments. If I make a long one I see a yellow squiggly, right click, refactor.

I have VSCode set up with appropriate rulers but i had no idea refactoring code fixes long comments. Awesome!

Use black...what the hell does that mean :ohno:


EDIT: Well, that smiley has changed since last I used it.

vvv I'll check it out!

D34THROW fucked around with this message at 22:45 on Aug 5, 2021

CarForumPoster
Jun 26, 2013

⚡POWER⚡

D34THROW posted:

I have VSCode set up with appropriate rulers but i had no idea refactoring code fixes long comments. Awesome!

Use black...what the hell does that mean :ohno:

https://black.readthedocs.io/en/stable/

necrotic
Aug 2, 2005
I owe my brother big time for this!
Yeah, I should have originally linked it. Phone posting makes me lazy

Wallet
Jun 19, 2006

D34THROW posted:

I have VSCode set up with appropriate rulers but i had no idea refactoring code fixes long comments. Awesome!

Use black...what the hell does that mean :ohno:


EDIT: Well, that smiley has changed since last I used it.

vvv I'll check it out!

If you use PyCharm (use PyCharm if you don't use Pycharm) there's a plugin to let you obsessively press a hotkey and have all of your disgusting formatting fixed. Takes all of five minutes to set up, doesn't really need configuration.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
I've tried pycharm, its sluggish as hell on my 2010 Win7 work laptop and hogs way too much memory to run alongside Outlook, Excel, and our ERP. VSCode is much more work friendly I've found.

Data Graham
Dec 28, 2009

📈📊🍪😋



PyCharm has all the IntelliJ bloat and some weird Java-smelly keybindings and search behaviors (just try searching for text in a Terminal window, what the gently caress is going on) but once I've gotten used to all its conveniences I recently opened up Xcode assuming it would have all the same features and nnnnnnnope. Just a wasteland

CarForumPoster
Jun 26, 2013

⚡POWER⚡

D34THROW posted:

my 2010 Win7 work laptop

youre kidding right? throw some coffee on that poo poo and get a new laptop.

NinpoEspiritoSanto
Oct 22, 2013




Finding you've got crazy long strings wrapping madly on the end of 20 indents isn't the python complaint you think it is, time to learn to structure code better.

Data Graham
Dec 28, 2009

📈📊🍪😋



And being five indents deep in any logic is a good time to step back and think about refactoring

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

quote:

hobbyist

:shobon: I'm entirely self-taught and I tend to program how I think and modularize when I can. My proudest Python achievement at the moment is a mini-suite of tools I'm using for a CLI app - TextMenu(), ActionMenu(), and EndMenu().

TextMenu() takes a list of strings, uses them to generate a menu, validates input, and returns the input to the caller. ActionMenu() does the same thing, except with tuples instead of strings - [0] contains the menu item and [1] contains the command to eval() on selection. EndMenu() extends ActionMenu() in a way, asking if the user wants to resume, go back to the main menu, or exit. It takes the resume function name as an argument.


On the plus side, I'm learning how to document as I go and my docstrings and comments are improving. I really like docstrings and how VSCode integrates them so well.

Macichne Leainig
Jul 26, 2012

by VG
Docstrings, well-documented Python code, and PyCharm is like a dream. I love it all.

lazerwolf
Dec 22, 2009

Orange and Black
n-thing using a python formatter like black. Makes your life much easier and your code quality better. If you use some sort of VCS (you should be) you can also look into pre-commit to set up your repo to execute black flake8 and other formatters on commit

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:

lazerwolf posted:

n-thing using a python formatter like black. Makes your life much easier and your code quality better. If you use some sort of VCS (you should be) you can also look into pre-commit to set up your repo to execute black flake8 and other formatters on commit

I use GitHub integrated with VSCode - I can initialize the repository straight from there. Mainly it's because I'm paranoid about data loss, I commit every night, but I'm trying to get on a better system on a per-feature basis.

Black looks amazing. Auto-formatting your code so you don't...really have to worry about the minutiae anymore? Abso-friggin-lutely.


EDIT: I just ran Black on a venv and this happened. I guess Python's core isn't very PEP8. :dumbbravo:
code:
367 files reformatted, 168 files left unchanged, 2 files failed to reformat.

D34THROW fucked around with this message at 18:07 on Aug 6, 2021

QuarkJets
Sep 8, 2008

lazerwolf posted:

n-thing using a python formatter like black. Makes your life much easier and your code quality better. If you use some sort of VCS (you should be) you can also look into pre-commit to set up your repo to execute black flake8 and other formatters on commit

Is there a reason to run black and flake8? I haven't used black but thought people tended to use one or the other

lazerwolf
Dec 22, 2009

Orange and Black

QuarkJets posted:

Is there a reason to run black and flake8? I haven't used black but thought people tended to use one or the other

Yeah black is just a configurable automatic formatter.

Flake8 doesn’t reformat your code but catches other errors like unused imports etc.

Loezi
Dec 18, 2012

Never buy the cheap stuff
I use isort followed by black followed by flake8 as my pre-commit hook and it's great.

Hed
Mar 31, 2004

Fun Shoe

Loezi posted:

I use isort followed by black followed by flake8 as my pre-commit hook and it's great.

Cheers. I never knew about isort

dorkanoid
Dec 21, 2004

D34THROW posted:

EDIT: I just ran Black on a venv and this happened. I guess Python's core isn't very PEP8. :dumbbravo:
code:
367 files reformatted, 168 files left unchanged, 2 files failed to reformat.

To be fair, black is even more opinionated than PEP8!

I tried using black (and others) with pre-commit, but all of them add 2-3 minutes to the commit time (possibly because I'm using GitHub Desktop); any tricks to speed it up?

lazerwolf
Dec 22, 2009

Orange and Black

dorkanoid posted:

To be fair, black is even more opinionated than PEP8!

I tried using black (and others) with pre-commit, but all of them add 2-3 minutes to the commit time (possibly because I'm using GitHub Desktop); any tricks to speed it up?

Depending on which IDE you use, you can usually get black and isort plug-ins to format on save so your files should just pass through pre commit faster.

Also I guess it depends on how many files in your repo and how many pre commit hooks you’re executing.

Maybe look into GitHub actions to run a code quality verification on push

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

dorkanoid posted:

To be fair, black is even more opinionated than PEP8!

I tried using black (and others) with pre-commit, but all of them add 2-3 minutes to the commit time (possibly because I'm using GitHub Desktop); any tricks to speed it up?

Are you accidentally running against every file in the repo maybe, instead of just the committed files?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply