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
Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Eela6 posted:

This is syntatically equivalent to the following:

Just a small correction, you actually meant this:
Python code:
try:
    #threading
except Exception as err:
    try:
        logf = open('log.txt', 'a')
        logf.write('Error: {}\n'.format(str(e)))
    finally:
        logf.close()

Adbot
ADBOT LOVES YOU

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
but you're actually using that variable! (this is the dumbest tangent)

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

baka kaba posted:

do I need to screw around parsing numbers from filenames here

Most likely yes. If you make it explicit that the number part is an integer you'll get the sorting you want:

>>> ('file', 2) < ('file', 10)
True
>>> ('file', '2') < ('file', '10')
False

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Malcolm XML posted:

:psyduck:

Did u think that 64 bits was enough to represent all of the reals?

Malcolm XML is a smart guy but this post is dumb as gently caress.

I mean, people start programming and with some experience assume that "float" is just a fancy name for numbers that intuitively behave like fixed-point numbers. This reasoning deflects the fact that real numbers are infinite.

I don't think many introductory materials go into teaching the details of floating points, it's a somewhat low level thing. But it's too bad, because imo every introductory material should tackle a bit into the classic 0.1 + 0.2 = 0.30000000000000004 to clear out people's minds from intuitively assuming they're fixed-point.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

QuarkJets posted:

Back when bitcoin was still newer a bunch of people learned first-hand that floating-point arithmetic has precision issues.

oh man, I vaguely remember hearing about this fiasco, do you have a link?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Philip Rivers posted:

I've tried but a lot of it is over my head, I don't have much grounding in algorithms that I can readily parse the info out there.

I'm pretty sure a basic line sweep algorithm would help you on this: https://courses.csail.mit.edu/6.006/spring11/lectures/lec24.pdf

I'll try to find an online class with a video lecture on this later. If you're already on the level of drawing all these lines on the screen then implementing line sweep should not be tricky for you.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Frank Viola posted:

Hi guys,

I'm just now diving into python for scripting, and so far I am really enjoying the language. However, I am running into an issue where I am trying to run a specific query and email each line of the results tog a specific address.

Python code:
AlertResult = cursor.fetchall()
    for result in AlertResult:
        sender = 'Sender Email
        mailto = 'Recipient group'
        header = 'To:' + mailto + '\n' + 'From: ' + sender + '\n' + 'Device Alerts for' + [Some variable] + '\n'
        msg = (header + (str(result)))
        mailServer.sendmail(sender, mailto, msg.as_string())
        time.sleep(1)
        print ("Sent Email")
Cursor declared as cnxn.cursor from the pyodbc library

and I am using the smtplib library for email sending.

Is there a different SMTP library that works better for this type of operation? Alternatively, am I missing something with how I am preparing the information to be sent?

Are you using the MIMEText object from email.mime.text? Because the way you're sending msg, which is a concatenation of 2 strings, instead of a MIMEText looks odd to me.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Sad Panda posted:

Could someone point out the glaring mistake in my code?

This:

Sad Panda posted:

Python code:
    unsorted_list  = list

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
pathlib is nice if you need to navigate around files and directories a lot, but if you're dealing with a fixed path then you don't really have much use for these Path objects. It also abstracts away a lot of OS specific stuff.

os.path can do much of the same things but pathlib has a much nicer API imo

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Alternatively it seems like a very turtle thing:

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

unpacked robinhood posted:

I'm trying to apply a function f(x,y) to each possible combination of two strings in a list.

Python code:
    names = ['joel','barney','georges','amsterdam']
    # 4 is a random placeholder value
    d = pd.DataFrame(4,names,names)
code:
           joel  barney  georges  amsterdam
joel          4       4        4          4
barney        4       4        4          4
georges       4       4        4          4
amsterdam     4       4        4          4
I'd like to compute a value for each cell (in my case the Levenshtein distance) using the row index and column index as value.
Googling gets me vague and complicated answers so I'm probably not going in the right direction ?

Using your d, maybe something like this?

Python code:
for col_key, row in d.iterrows():
    for row_key in row.keys():
        d[col_key][row_key] = levenshtein_distance(col_key, row_key)

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Python is changing dramatically, it always been kinda vague but now in 2018 it's clear that "pythonic" has no meaning whatsoever.

(yes, I'm looking at assignment expressions here)

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Installing Anaconda seems to be the best solution for this in my experience.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Jose Cuervo posted:

What does this process look like? Get everyone in the class to download Anaconda on day one of the class, then...?

Yep, this is what I do.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Mister Fister posted:

I'm learning about regular expressions. With the below expression, i understand the stuff inside the brackets will look to match any upper case/lowercase a-z character, periods, and whitespace while the * is a wildcard. What do the ^ and $ characters indicate? I'm having trouble finding documentation on this. Thanks!

code:
pattern = '^[A-Za-z\.\s]*$'

Tangential but... To avoid escaping certain characters use raw strings for regex like this:

code:
pattern = r'^[A-Za-z\.\s]*$'

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Boris Galerkin posted:

Why does it seem like there’s suddenly a drive to static type everything now? I thought not having to do that was one of the core things with python.

My impression was that the main motivation for the push on static typing is to provide better hints for bytecode optimization.

And yeah, I feel like this push is not really playing with python's strengths but that's like just my opinion. Language design is an ever changing thing.

Adbot
ADBOT LOVES YOU

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

cinci zoo sniper posted:

Cruious - what do you guys think of PEP 572?

I feel like it's a terrible idea but I'll surely use it sometimes, it's a feature I abuse a lot in C anyway so...

It'll be just like how I use for-else sometimes. It's terrible and dumb but sometimes while typing code I just don't give a poo poo. If the python devs wish to enable my inner perl coder then gently caress the police.

edit: https://twitter.com/symbolicbutt/status/1024783816186060800

Symbolic Butt fucked around with this message at 09:43 on Mar 27, 2019

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