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
Jabor
Jul 16, 2010

#1 Loser at SpaceChem
Antlr.

Adbot
ADBOT LOVES YOU

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Thermopyle posted:

I always use https://regex101.com/ when building regexes. Anyone have anything else they like to use more? I've got a project coming up where I need to build a lot of regexes to parse some HTML and I'm not looking forward to it.












haha, not parsing H͓͔̏̀͆̂̎ͪ̂ͤŤ̵̷̖̟̖̜̝̜̔̓ͣ̓̒M̄̈́ͦͪ̃̆͑̑̀͏͏̙̗͈̬͔̩͙L̨͔̥̦͓̰̔̍̓̋ͬ̋́̓͝ͅ

I also have https://www.debuggex.com/ bookmarked, but I think their features overlap somewhat.

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Thermopyle posted:

I've got a project coming up where I need to build a lot of regexes to parse some HTML and I'm not looking forward to it.

Shouldn't you use an HTML parser to parse HTML? Parsing HTML is a solved problem. Maybe you need regexes to extract a specific portion of some element once the HTML has been parsed, but you shouldn't be doing the parsing itself with regexes.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

TooMuchAbstraction posted:

Shouldn't you use an HTML parser to parse HTML? Parsing HTML is a solved problem. Maybe you need regexes to extract a specific portion of some element once the HTML has been parsed, but you shouldn't be doing the parsing itself with regexes.

good idea!

Kekekela
Oct 28, 2004

Thermopyle posted:

I always use https://regex101.com/ when building regexes.

:same:

lifg
Dec 4, 2000
<this tag left blank>
Muldoon

Thermopyle posted:

I always use https://regex101.com/ when building regexes. Anyone have anything else they like to use more? I've got a project coming up where I need to build a lot of regexes to parse some HTML and I'm not looking forward to it.












haha, not parsing H͓͔̏̀͆̂̎ͪ̂ͤŤ̵̷̖̟̖̜̝̜̔̓ͣ̓̒M̄̈́ͦͪ̃̆͑̑̀͏͏̙̗͈̬͔̩͙L̨͔̥̦͓̰̔̍̓̋ͬ̋́̓͝ͅ

Jesus Christ you gave me a small heart attack for a moment there.

feedmegin
Jul 30, 2008

Thermopyle posted:

good idea!

I assume you've seen https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 yes?

No Safe Word
Feb 26, 2005


TooMuchAbstraction posted:

Shouldn't you use an HTML parser to parse HTML? Parsing HTML is a solved problem. Maybe you need regexes to extract a specific portion of some element once the HTML has been parsed, but you shouldn't be doing the parsing itself with regexes.

did you both miss this part

Thermopyle posted:

haha, not parsing H͓͔̏̀͆̂̎ͪ̂ͤŤ̵̷̖̟̖̜̝̜̔̓ͣ̓̒M̄̈́ͦͪ̃̆͑̑̀͏͏̙̗͈̬͔̩͙L̨͔̥̦͓̰̔̍̓̋ͬ̋́̓͝ͅ

Pollyanna
Mar 5, 2005

Milk's on them.


I have a regex question. I need to make a bunch of changes to many different bits of code that take this form:

code:

this.handleBlur.bind(this, 'this-string-differs-on-each-line')

and replace them with this:

code:

this.handleBlur.bind(this, {data: 'this-string-differs-on-each-line'})

I tried using regex to find-and-replace this all at once, but I can't get the syntax down right. I want to capture all of those 'this-string-differs-on-each-line' strings and wrap them in the {data: } stuff. I can't even select anything. How do I write this?

code:

\{this\.handleBlur\.bind\(this, ($1)

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Pollyanna posted:

How do I write this?

code:
\{this\.handleBlur\.bind\(this, ($1)

In vim it'd be something like
code:
%s/this.handleBlur.bind(this, \('.*'\))/this.handleBlur.bind(this, {data: \1})
But the syntax tends to be slightly different depending on your exact domain. I believe the term you want is "backreference".

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Pollyanna posted:

I have a regex question. I need to make a bunch of changes to many different bits of code that take this form:

code:
this.handleBlur.bind(this, 'this-string-differs-on-each-line')
and replace them with this:

code:
this.handleBlur.bind(this, {data: 'this-string-differs-on-each-line'})
I tried using regex to find-and-replace this all at once, but I can't get the syntax down right. I want to capture all of those 'this-string-differs-on-each-line' strings and wrap them in the {data: } stuff. I can't even select anything. How do I write this?

code:
\{this\.handleBlur\.bind\(this, ($1)


code:
s/(this\.handleBlur\.bind\(this,) ('.*?')\)/\1 {data:\2})/
https://regex101.com/r/0dgQuH/1

Parenthesis create a capture group numbered in order from 1 that you can reference with a \# in the replace, unless you make it a non-capturing group with ?:. Also the group 0 is the whole match, but I rarely find that useful. .*? is a non-greedy (or maybe backtracking?) match all, so it won't consume the rest of the candidate tokens - just the ones it can without matching on the next token in the expression, which is a single-quote in your example.

Munkeymon fucked around with this message at 19:58 on May 24, 2017

Pollyanna
Mar 5, 2005

Milk's on them.


You guys are lifesavers. :woop:

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

No Safe Word posted:

did you both miss this part

I always thought that skipping a few lines and making the text small was just a semi-funny joke because who would that actually trap?

Jewel
May 2, 2009

Thermopyle posted:

I always use https://regex101.com/ when building regexes. Anyone have anything else they like to use more?

Yes, actually. Well, partially. I use regex101 for writing them, but when I'm trying to debug or see what an existing one does quickly, or just want to see the end result of one I've written to check if it's what I actually wanted, I use http://regexper.com

For example, on a basic phone number parsing regex I just found online (don't ever do that, please,)

a dangerous thot
Dec 19, 2016
is there any programming language that allows for run time event chain hooking, object instance mutation, reflection traversing brains and linkers, etc?

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

a dangerous thot posted:

is there any programming language that allows for run time event chain hooking, object instance mutation, reflection traversing brains and linkers, etc?

Any dynamically-typed language that gives you decent access to the object "magic" allows for this kind of thing. Python for example lets you add/remove fields from objects by accessing the __dict__ field of the object.

If you want a strongly-typed language that lets you do that kind of thing, you're probably in for a lot more pain. Reflection is a lot more unpleasant when the compiler starts having opinions about what you're doing.

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:41 on Jul 27, 2018

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
I'm afraid you're using domain-specific language that I don't recognize. What problem are you trying to solve, anyway?

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.

TooMuchAbstraction posted:

I'm afraid you're using domain-specific language that I don't recognize. What problem are you trying to solve, anyway?

Come on man, you're better than this.

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:41 on Jul 27, 2018

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
I'm sure that if you recite just a few more out-of-context buzzwords, someone will appear and give you the magical answer to solve your problem with no effort on your part.

Do you actually have a problem you're trying to solve?

a dangerous thot
Dec 19, 2016

Jabor posted:

I'm sure that if you recite just a few more out-of-context buzzwords, someone will appear and give you the magical answer to solve your problem with no effort on your part.

Do you actually have a problem you're trying to solve?

is that a roundabout way to say it doesnt exist and i should make it because im definitely asking its existence

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
No-one knows what you're asking for because your description of what you want is mostly just a bag of meaningless words that doesn't actually do anything to describe what it is you're looking for.

Do you have a problem you're trying to solve?

Asymmetrikon
Oct 30, 2009

I believe you're a big dork!

a dangerous thot posted:

is that a roundabout way to say it doesnt exist and i should make it because im definitely asking its existence

I think this is what you're looking for?

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:40 on Jul 27, 2018

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


That's a solution. What's the problem you're trying to solve?

Star War Sex Parrot
Oct 2, 2003

ultrafilter posted:

That's a solution. What's the problem you're trying to solve?
How to escape its life as a Markov bot.

a dangerous thot
Dec 19, 2016

ultrafilter posted:

That's a solution. What's the problem you're trying to solve?

unnecessary work

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:40 on Jul 27, 2018

lifg
Dec 4, 2000
<this tag left blank>
Muldoon
It sounds like you're trying to insert or modify code in an object oriented application while it's running.

Yes? No? Am I close?

Good Will Hrunting
Oct 8, 2012

I changed my mind.
I'm not sorry.
You guys are all so loving stupid.

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:39 on Jul 27, 2018

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:39 on Jul 27, 2018

lifg
Dec 4, 2000
<this tag left blank>
Muldoon
I think you want Smalltalk. The virtual machine, not just the language.

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:39 on Jul 27, 2018

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.
Check out http://joearms.github.io/2013/11/21/My-favorite-erlang-program.html

a dangerous thot
Dec 19, 2016
.

a dangerous thot fucked around with this message at 07:39 on Jul 27, 2018

TheMostFrench
Jul 12, 2009

Stop for me, it's the claw!



So I'm learning Python 3, and I'm new to coding in general. I'm trying to make a program to sort votes for preferential counting, but I'm having some trouble working with the data I have. Sorry if my question doesn't make much sense:

I have two files - one with candidate names, and a file with a series of ballots/votes in the format 1, 2, 3, 4, 5 (the preferences could be in any order) per line. My intention was to read through each ballot then apply votes to the candidates by performing rounds of vote counting. For example: in the first round I scan the ballot for "1" - if "1" is in index 0 of the string, the candidate in index 0 gets a vote. This happens for all ballots until the first round is finished, then happens again for round 2 (except it scans the ballots for "2" being the second preference), and again for however many rounds it takes to determine a clear winner. Currently each ballot is a separate string in a single list i.e ['13452', '42531', etc]. To track total votes, I have a dictionary which contains the names of each candidate and the votes they have received during the count, e.g voteTotal = {candidate[0] : 0, candidate[1] : 0 , etc : etc}

The trouble I'm having is scanning each ballot string for the required preference, depending on round - I can't figure out how to scan through more than one ballot at a time.

Is there a way to update the dictionary by scanning through every ballot string in the list? Here's what I tried

for j in ballot:
if "1" in ballot[ : ][ : ]:
voteTotal.update((x, candidate) for x, candidate in voteTotal.items())
#I'm really unsure about this last line. I just read it about it in a stackoverflow question and it seemed relevant to what I needed.

When I try to do the above with something like if "1" in ballot[0], it does find "1" in the first string, but it doesn't change the dictionary.

With what I understand so far about indexing and slicing, I thought ballot[ : ][ : ] would enable me to scan through every index of every string in the list, but it just evaluates false for every entry.

So, my question: what are some ways to scan through a large number of list indexes for specific items in each string?

Currently the code doesn't give me an error, it just doesn't seem to change anything and I don't know why.

TheMostFrench fucked around with this message at 12:30 on May 25, 2017

Eela6
May 25, 2007
Shredded Hen

TheMostFrench posted:

So I'm learning Python 3

...

So, my question: what are some ways to scan through a large number of list indexes for specific items in each string?

In my opinion, you are asking the wrong question. The correct question is question is, 'how do I get each voter's preferred candidate in a given round?'

Also, you're using the wrong syntax to update a dictionary. The easiest and best way to update a dictionary is like this:
IN
Python code:
some_dict = {1: 'a',  2: 'b', 3: 'c'}
print(some_dict)
some_dict[1] = 'new_value'
print(some_dict)
OUT
Python code:
{1: 'a', 2: 'b', 3: 'c'}
{1: 'new_value', 2: 'b', 3: 'c'}
It's easiest not to hold the preferences, but simply to track the remaining- candidates, iterate through the ballots each time, and get the first remaining candidate listed on each ballot.

Here's a solution of your problem.

Python code:

def preference_voting(candidates_file, ballots_file):
    with open(candidates_file) as file:
        candidates = {str(i): candidate for i, candidate in enumerate(file, 1)}
    
    remaining_candidates = set(candidates.values())
    
    def read_ballots():
        with open(ballots_file) as ballots:
            yield from ballots

    def preferred_remaining_candidate(ballot):
        for preference in ballot:
            if candidates[preference] in remaining_candidates:
                return candidates[preference]

    while len(remaining_candidates) > 1:
        votes = {candidate: 0 for candidate in remaining_candidates}
        for ballot in read_ballots():
            candidate = preferred_remaining_candidate(ballot)
            votes[candidate] += 1
        loser = min(votes, key=votes.get()) 
        # note: this will arbitrarially choose the first candidate in case of a tie!
        # this is left as a problem for you to solve ;)
        remaining_candidates -= {loser}
    (winner, ) = remaining_candidates     
    # this is the cleanest way to extract the only member of a single-element set
    return winner
Note that we have a Python thread. Beginners are welcome!

I'm using a lot of special python syntax here for cleanliness, so there might be things here you don't understand. If there's anything you don't get ask us about it in the thread and we'll either explain it to you or give you the right terms so you can look it up yourself.

Eela6 fucked around with this message at 18:57 on May 25, 2017

Adbot
ADBOT LOVES YOU

TheMostFrench
Jul 12, 2009

Stop for me, it's the claw!



I had to look up some things like Yield, which seem interesting.

I get an error at this line:
loser = min(votes, key=votes.get())

code:
loser = min(votes, key=votes.get()) 
TypeError: get expected at least 1 arguments, got 0
I'm not familiar with .get() so I'll look that up. My understanding of that line is that assigns the candidate with the least votes to 'loser', but if it's not getting any information does that mean nothing is being passed to it from the dictionary?

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