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
baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

I'm with the cops, you're all busted

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

You mean just running a bit of code to check it works or to generate some output quickly? You can do that with shift+return in VS Code too

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Arguably the loop version is the most explicit and clean - loop over an iterable until you find an element that matches the predicate, then move on. Creating a generator just to call next on it once works, but it feels hacky to me

Not the first time I've thought itertools needs a first function tbh

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Typically in other languages first (or whatever) takes a predicate and an iterable and returns the first item that matches. It works like filter only it's explicitly for a single item, and you don't need to wrangle some result sequence to grab what you actually want. So you get a very simple, neat, explicit call that either returns a result or some default/missing value, or even throws an exception (depending on the implementation obv)

I mean yeah you could just write it yourself but it's a nice thing to have, and it's very common

baka kaba fucked around with this message at 01:58 on Mar 13, 2019

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

I think you're meant to use time.monotonic for basic scheduling, that might be the easiest way of calculating elapsed real time

if I were doing this I'd use your short-sleep polling method, but with a little more logic around it. If the next beat is extremely close (like your expected sleep period might cross it) it might be better to play it early. Or maybe calculate a more accurate sleep period to aim for the exact time you need to hit. If you overshoot, make sure you play the beat - it should never actually skip one, even 600bpm is a beat every 100ms, you shouldn't be locked out of your process for that long surely?

Also make sure your scheduling is completely separate from the actual time your beats get played. If you start at time 0, and you have beats at 0.5s, 1s, 1.5s etc, and your second beat comes out late at 0.51s, make sure your next beat time is calculated at 1s, not 0.51 + 0.5, y'know? So you have a constant pulse scheduled, and if a beat is inaccurate it doesn't affect the others that follow. You're probably already doing this but it's worth mentioning, you don't want timing errors compounding

ALSO I see you're using Qt I think? Have you tried using QTimer which they recommend over the sleep methods? Or using Qt's Priority values to create a high-priority worker thread (or just bump the priority of the main thread, might be a bad idea though, you'd have to test it I don't know Qt)

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Rocko Bonaparte posted:

If we want something to play with in that regards, I recently had a list of strings that I wanted to iterate in groups of fours. Generally there's some mish-mash of syntax in itertools to do that, but they all were kind of wonky. It smelled like something with a functional solution (ie "Do this to every group of four elements of this list") but expressing it was gross.

ime functional languages tend to have a built-in function that does this, I don't think python does yet - whenever I try to use it in a functional way it always feels a little half-baked to me. Probably because it was designed with stuff like comprehensions and generator expressions that encourage pure functional behaviour (take an input and iterate over it to produce a new output), so all the other things feel a bit like an afterthought to try and expand that. (I know map and filter have been in there forever, but a lot of the time it's like... why not just use a generator or comprehension?)

look at partition in the link SurgicalOntologist posted anyhow

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Honestly when I got a new computer I just installed straight python, I used Anaconda before but the combination of having multiple library sources to worry about (conda, pip) and the fact their virtual environment stuff still didn't work with the standard windows terminal made me think it's just not worth the hassle

apparently they have some hacky script to make powershell work now, that runs a copy of python every time you open a PS terminal whether you're using conda or not :wtc:

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

mr_package posted:

How are you organizing your @dataclass objects? If each instance is essentially a row from a database, is there a good way to order/organize them? e.g. if I just shove everything into a List I need to iterate through to try and find the specific instance I'm looking for. There's probably a smarter way?
code:
@dataclass
class Language(object):
    id: int
    code: str
    name: str
Would you remove the id parameter from Languge class and use it as a dictionary key instead? e.g. languages = {1: Language('en-US', 'English (US)', 2: Language('fr-FR', 'French'} This feels a bit weird to me because I expect the id to be part of the Language object but maybe this is part of data modeling, I should write a Languages class that is a collection of Language objects and implements some friendly methods to find them?

If you think about it though, the primary key in a database row is part of the organisational system, so it makes sense to have it as the key in the dictionary. It's what you're using to identify and reference a particular data object

And sometimes that primary key is also part of the data object, like an ISBN, instead of just an internal identifier the database is using. So in that case, it makes sense to also have it in the object you're storing in the dictionary. Chances are the thing that ends up accessing it will want to make use of that attribute, but won't necessarily have access to the dictionary. And maybe you might want to change that implementation anyway - but the data should stay the same, right? Store what you need to store

So yeah you're storing the same piece of data twice, but imo that's ok, it makes sense sometimes! You'll probably want to write something that enforces that consistency - maybe a class that holds the dictionary internally, and adds stuff by automatically reading the key from your data and using that to add an item, so you never interact with the dict directly. It depends if end users care about the ID or not - I'm guessing they supply it for lookups?

(If you really care about this redundancy you could have a fetch method that adds the key to the returned data, so you store it separately but the user gets the whole thing)

Also don't forget if the IDs are in sequential order you can just index into a list instead, if that works for you

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Well by "end user" I really meant whatever's calling it (should have said client). So say your web app goes "hey I need that list of languages" and your Python code goes "here ya go, each one has an ID". If the web app is doing something like displaying all the languages, letting the user pick one, and then passing the relevant ID back to your Python code, that ID is sort of temporary - it's like getting a menu of options and going "yeah, the third one", once you get the thing you wanted you don't really need to remember what its position was anymore, right?

Whereas if you're gonna need to reference that ID again later somewhere, it makes sense to bundle it up as an attribute of the Language, since it's more of a persistent reference and the way you actually specify which object you want - it's acting more like a database, the primary key is part of the data (even if the user doesn't ever see it). It depends on exactly what you're doing, but by the sound of it the ID is probably gonna be a persistent reference, right?

If the duplication bothers you, imagine you have Languages with an ID field, and you're storing them in a list. That's basically a database table. You can search it just fine, looking at the ID field, but for speed in a database you might want to build an index, which is basically another table that holds the key (so you now have a copy in two places) and a pointer to the data in the first table. That combo of index and data table is sort of what a dictionary is, so it's not that weird to have the key present in the lookup and the data itself!

Like I said you could separate the key out for storage, and recombine it with the data when it's retrieved, but that's extra complexity that needs to be worth it

baka kaba fucked around with this message at 19:24 on May 11, 2019

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Maybe "batteries included" isn't the best idea because those batteries are usually crappy and get discarded quickly??

Make it easier for people to add their own quality batteries

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

A for loop basically iterates over a bunch of items, handing you each item in turn. So in the body of the loop, you do something with the current item, and when you run the loop it will basically do that thing with all the items.

so you want something like
Python code:
# start with an 'empty' version of the thing that's gonna hold your result
final_string = ""
# loop over all the names - each item you're handed is gonna be called 'name' so you can refer to it in your loop
for name in names:
  # grab the first letter from the name string (item 0 in the string sequence) and add it to your result variable
  final_string += name[0]

print(final_string)
you have a bunch of names, and for each one you just want to look at the first letter and stick it on the end of what you've got so far. That's the only index you need to mess with, referring to that first letter. It's a pretty simple procedure, so make sure it makes sense to you before you tackle the rest of it, or you're gonna overcomplicate things where Python should be doing the work for you

e- lookit all them replies

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Use a for loop, look at each name, build up a total

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Mycroft Holmes posted:

wait poo poo thats the next question, i'm stuck on the find a number of letters instance

nobody just wants to give you the answer but

Python code:
letter = input("Please enter a letter")
total = 0
for name in names:
  total += name.count(letter)

print(total)
you need to understand what's happening there. This is fundamental stuff, which isn't to say "duh it's simple" but it uses some basic ideas, and the later questions build on that. Like you're gonna struggle to do the vowels one if you haven't grasped how to do it for a single letter yet

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Mycroft Holmes posted:

except that answer fails

n/m i'll wait until my teacher gets back to me

You probably need to convert each name and your input letter to lowercase (or whatever, just so there's no mismatch) because 'e' won't match 'E'

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