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
Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
What's a good library for drawing an array of pixels? They would change over time (I'm not animating figures, though), and I plan on eventually feeding into some LEDs, but for now I'd rather prototype without wiring up a bunch of LEDs. I've thought of just doing it with Flask + Javascript but I'm wondering if there's some other good solution.

I'm semi-willing to use some other language, but Python's the language I use daily so I'd rather just stick with that unless I can't find an adequate solution.

Adbot
ADBOT LOVES YOU

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

huhu posted:

https://python-pillow.org/

Edit: How many LEDs are you talking about by the way? Are you planning to use Arduino, Raspberry Pi, or something else?
32x32 and a Pi for the prototype. We'll see whether a Pi can do what I actually have in mind at the end of the day, but I'll get there when I get there. But on a computer I can have a much higher resolution I'm probably gonna start with that. I'm okay with this taking a lot of iteration and work.

OnceIWasAnOstrich posted:

numpy for the array and matplotlib imshow() to render?
Well that's pretty easy. I could probably write it to render at 20fps or whatever.

praise be to numpy

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

QuarkJets posted:

Wouldn't that necessitate also specifying that the step size is 1, then?

Although practicality beats purity :colbert:

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
When you run a script, either directly or by importing it, it executes all the code in it from top to bottom.

But suppose you wanted to import this as a module. If you had a runall() function defined and then called at the bottom, it would run when you import, which may not be the behavior you want. You might want to have different behavior if it's running as a script, or as a module.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

shrike82 posted:

Do most of you run Python in Windows?

Package issues like this and the difficulty getting tensorflow running on Windows drove me to a dual boot Ubuntu setup.

It's a pain in the rear end to setup but I've found switching to a Linux build helpful in mitigating stuff like this.
I have two separate Python installations on my home computer. One is a Python 3.6 installation with Anaconda for Windows, the other is Python 3.5 installation in Bash for Windows, which is where I use Tensorflow. For almost everything I can use them interchangeably.

In the brave new world of Bash for Windows, you don't really need to run VMs or dual boot if you don't want to.

Ghost of Reagan Past fucked around with this message at 12:35 on Jun 6, 2017

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Dex posted:

i'm a windows 7 holdout on my work laptop for reasons, does the windows subsystem for linux fix all those annoying things like not being able to run gunicorn because windows doesn't have fcntl and so on?
Haven't tried gunicorn, but most syscalls are implemented. Not all, though. Should probably work, and if it doesn't they're working pretty heavily on it so it'll probably be there soon.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
You can totally do this

code:
def connect_to_db():
    conn = sqlite3.connect('db.sqlite')
    cur = conn.cursor()
    return conn, cur


def insert(query, cur):
    cur.execute(query)
    cur.commit()

conn, cur = connect_to_db()
insert(query, cur)
insert(query2, cur)

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
CPython has the Global Interpreter Lock, which means that only one thread can process Python bytecode at a time. It's not that Python code can't be multi-threaded, but that the interpreter is locked if one thread is executing Python bytecode; no others can process bytecode. Apparently it helps because the CPython memory management isn't thread-safe. As far as I understand it, anyway, I'm no CPython internals guru (I've looked in there precisely once).

The GIL is acquired by a thread and released on I/O operations, if it helps you understand where bottlenecks might occur. I love Python, but the CPython implementation has some serious drawbacks and other languages should be used in certain cases.

For what it's worth, most Python modules written in C don't acquire the lock (because they're running in C and aren't Python bytecode).

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Boris Galerkin posted:

Well I just had a frustrating time figuring out what was wrong with a part of my code when it turns out it was numpy being weird.

So I have a numpy array where some values are inf and some values are nan. I create a Boolean list of where these values in the array are inf/nan, and then I use these indices to do something to another array.

Like so:

code:
import numpy as np

a = np.array([1, np.sqrt(-1), np.nan, np.inf])  # [1, nan, nan, inf]

print(a == np.inf)
# F, F, F, T, as expected

print(a == np.nan)
# F, F, F, F, which is wrong

print(np.isnan(a))
# F, T, T, F
Is there a reason it does this? Does np.nan has an actual numerical value or something? I would have thought it would be treated in the same way as None where it just "is" or "isn't."


code:
>>> np.nan == np.nan
False
NaN is equal to nothing, not even itself.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
One.

Otherwise, users of the function will have to do type-checking themselves and call the right function or pass in the right kwargs, when you could do that burdensome work for them and reduce the possibility of error/proliferation of ways of doing said checking.

Simple is better than complex.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
You might also be interested in other plotting libraries. I'm personally fond of ggplot, a port of the R library. There's also Bokeh, which has some interactivity functionality and is useful for web data visualizations.

But seaborn + matplotlib will serve you well.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Thermopyle posted:

As a followup to that thing I posted from stackoverflow about how Python is the fastest growing language, they delve into why its the fastest growing language.




its data science
Good, good, maybe soon the ecosystem will rival R :getin:

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

VikingofRock posted:

Why does this happen? (Python 3.6.3)

Python code:
letters = {'a': 1.0, 'b': 2.0, 'c': 3.0}
fns = {key: lambda x: x / val for key, val in letters.items()}
for key in fns:
    print(key, fns[key](1))

# output:
# 
# a 0.3333333333333333
# b 0.3333333333333333
# c 0.3333333333333333
Python closures are "late binding". The function isn't evaluated until it's actually called, right? Well, in the case of this lambda, what is the final value of val. It's 3.0, of course. The variable isn't in the global scope, but it's in the scope of the closure. You stumbled across one of Python's weirder behaviors, because of course that's not the expected behavior.

You can do the following:

Python code:
letters = {'a': 1.0, 'b': 2.0, 'c': 3.0}
fns = {key: lambda x, val=val: x / val for key, val in letters.items()}
for k, v in fns.items():
    print(k, v(1))
Which will get you the desired result.

You could probably use functools.partial to do this but :effort: (also I'm not super familiar with it and the 30 seconds I tried I couldn't get it to work right so)

E: f; b :smith:

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Thermopyle posted:

I'm so dependent upon formatting and syntax highlighting that I literally cannot read that post without significant effort.

I mean without indentation it isn't valid Python anyway :v:

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Boris Galerkin posted:

Anyway on an unrelated topic, does anyone use Jupyter notebooks? I feel like Jupyter notebooks are one of these things that I've always heard about and people rave about it, like Docker, but I don't really "get" it so I'm having a hard time seeing use cases for them. But I'll admit that once I actually sat down and understood Docker sometime last year I went from "meh okay" to "holy poo poo, this is awesome" real fast. I'm hoping the same thing happens with Jupyter notebooks.
They're great for manipulation of data. I can make charts, do exploratory analysis, write code that I'll refactor later, throwaway experiments, etc. Plus, include prose and other stuff to illuminate what I'm actually doing.

They're less good for most other coding things.

There are obviously holdouts but if you work with data in Python, you're going to use notebooks.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Business posted:

I don't know quite how to ask for what I'm looking for because I know very little about actual software development even if I've done some programming.

So I wrote some scripts that manipulate text in certain ways and output certain strings into a new text file. Custom, very weird use case NLP tasks. These more or less work for my purposes, but my bigger goals are to (1) collect all of these into a command line program that would just be easier for me (and ideally others) to use (2) get the whole shebang online for people to use.
Can anyone direct me towards some resources to help me think through how to do (1)? (2) is a huge stretch goal, but ideally I would be able to do (1) in a way that would set me up for success on (2). I'm looking for some explanations of how to go from a folder of .py files that do little tasks to an organized piece of software that I can work on, add features to, etc. I want to have a sense of best practices as my lil scripts get more complicated over time. There must be a name for what I'm trying to get at here, but I don't even know.
Try Click. It's very nice to work with, kind of guides you into writing good user interfaces, and has really extensive documentation that hits on some design questions. You should refactor your individual scripts into functions and classes you can import around into other files, and then write a CLI app around your various tools using click. So you might have a script that right now that tags parts of speech and prints the tree (or whatever). You might then refactor that into a function that parses the sentence and a function that prints the tree. Then write a CLI app with click that you use like nlp tagger "this is a sentence" --tree, but then you can write an option for the app nlp tagger "this is a sentence" --table that prints a table.

I have not tried it at all so I can't vouch for it, but Python Fire might be useful for your purposes?

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

NtotheTC posted:

I'm not trying to nominate it for code of the year, just pointing out a real-world example of it
We've all done code crimes with Python but this is a real mess.

EDIT: I think my greatest Python code crime was erasing exceptions deep in some library and baffling myself and others about how the gently caress it was breaking. It made sense at the time but holy gently caress it's a mess.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

QuarkJets posted:

Brown starts off saying that the stdlib doesn't have enough useful features, so users need to rely on third party stuff, which sucks

Then Brown points out that the stdlib is bloated and hard to build, with features that are simply worse than 3rd party modules, which is problematic because simply being in the stdlib means that people will sacrifice features for convenience.

I don't think you can truly fix both of these things.
I think if they buckled down and made the stdlib APIs consistent and then treated them as a great base to build better, friendlier solutions on top of (like Requests, Arrow, etc.), that'd do a lot of good. Brown is right that some features need to also be dropped -- tkinter needs to go, if someone still uses it they can have it as a separate package and leave the rest of us in peace.

The fact is there isn't a great solution to the problems because Python and the library are nearly 30 years old now. But you can make it better, and as a Python developer I think that's a good goal.

But yes just rip off the Python 2 bandaid. Now that supervisor 4 has Python 3 support there is basically zero excuse for it. Your lovely legacy application needs to get ported to Python 3, and that's all there is to it.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

the yeti posted:

Is tkinter really that bad? I ask this relatively innocently--I've seen a small example and it seemed pretty clumsy, but I have almost no experience with Python GUIs besides learning PySimpleGUI to refactor said small example.
There's nothing really awful about it but it's just not where the world is nowadays -- if you're going to build a GUI application with Python in 2019 you're probably better off just using Flask and building a web application, or some better, more modern libraries. Maintaining tkinter support for no real reason means that you add build complexities and bloat. Brown picked it out as an example, but there's a bunch down there in the stdlib. Luckily there's upcoming effort to remove and deprecate some more useless packages (macpath, holy poo poo!), but I think a stronger culling and a focus on rationalizing the low-level APIs is probably the path forward.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Empress Brosephine posted:

Hello I’m new to Python but I want to jump thirty steps ahead. I developed the code for a little program I can use for work (although perhaps Python wasn’t the best language for it, it’s a program that asks you how many tickets of each type do you want then spits out a price ) but I want to make a decent UI for it that anyone could use. Where would I work towards next?

Thanks
My standard suggestion is build it as a web app. I always recommend Flask for this. You'll have to build HTML templates, but I've generally found that HTML+CSS+Javascript is better than most other GUI solutions like Qt or (as we've been talking about) Tkinter.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Empress Brosephine posted:

I think Flask is way out of my league skills wise now...I’ve only done If statements and for loops so far

Eh you'll be fine, just dive in. Flask is a lot of magic but if you follow the instructions you can build websites quite quickly. Python's an easy language, so just build poo poo even if it's out of your league.

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

Hollow Talk posted:

I have a quasi-religious (i.e. style) question, since I briefly paused today when writing something to consider what would be the most natural way for other people. Background is how to write/compose a set of transformations, e.g. funneling a dict through a series of functions which each take a dict, operate on it, and return a dict.

code:
row = [1, 2, 3, 4]
row = list(map(dec, map(inc, map(inc, row))))
This one. But I like to make sure all my code passes the "2AM test." That is, if I'm woken up at 2AM and have to look at the code because of some dumb production incident or something, will I be able to understand it? This I would, but once you start doing more complicated things, your mileage may vary. So I think this works, but you're gonna be teetering on the edge of the 2AM test with more complex code. Readability counts :)

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun

taqueso posted:

DNG is a digital negative file from lightroom I think
Correct. It's Adobe's "universal" raw image format (as opposed to, say, Nikon's NEF and Canon's CRW). There looks to be a library https://pypi.org/project/rawpy/ that looks like it might do what you need. It's structured like a TIFF apparently.

Adbot
ADBOT LOVES YOU

Ghost of Reagan Past
Oct 7, 2003

rock and roll fun
The homebrew maintainers also made it impossible to install older packages from a git commit, which you might want for development purposes, because in the Real World we have to use non-bleeding-edge software for compatibility reasons. Basically making it useless to developers. This is recent and they've been absolutely unresponsive and unsympathetic to the mess that this causes. It's not the "intended workflow", which they just changed without warning because they clearly don't care about how people actually use brew. It's always been garbage and now it's somehow worse than it's ever been.

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