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
teen phone cutie
Jun 18, 2012

last year i rewrote something awful from scratch because i hate myself
huh interesting. even more interesting that the sqlalchemy docs don't even mention this:

https://docs.sqlalchemy.org/en/14/orm/mapping_columns.html

So if you want your column name to be "id," this is preferred?

Python code:
user_id = Column('id', Integer, primary_key=True)

Adbot
ADBOT LOVES YOU

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

teen phone cutie posted:

huh interesting. even more interesting that the sqlalchemy docs don't even mention this:

https://docs.sqlalchemy.org/en/14/orm/mapping_columns.html

So if you want your column name to be "id," this is preferred?

Python code:
user_id = Column('id', Integer, primary_key=True)

Yeah, in general you want to avoid reusing any already used variable names from the standard library. IDEs should warn you about this if you're using one, so naming something list, id, type, etc should all give a warning because you're overwriting it. In theory you can do it, but it can cause weird behavior or straight up bigger problems so it's a good idea to avoid it whenever possible. It's kind of wild that they just don't mention it in their docs, but welp.

Macichne Leainig
Jul 26, 2012

by VG
It's probably fine on a class, if not a bit of a code smell - PyCharm correctly flags a global variable shadowing the builtin, but doesn't care at all in a class.

I think the idea being that you'd always access it as object.id instead of just plain id.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
It's perfectly normal (even documented as such) in Django models to just name the field `id`, for what its worth. It also automatically adds a field called id to any model that doesn't otherwise specify a pkey.

The March Hare fucked around with this message at 07:22 on Mar 10, 2022

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

Protocol7 posted:

It's probably fine on a class, if not a bit of a code smell - PyCharm correctly flags a global variable shadowing the builtin, but doesn't care at all in a class.

I think the idea being that you'd always access it as object.id instead of just plain id.

Oh, hey that's a good point actually, I hadn't considered that.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
VSCode doesn't flag using builtins as variable names. I have to catch it myself based on different syntax highlighting.

The reason I don't suffix _id to the FK fields is because I'm using a backref on the parent tables with the name <field>_id, which sticks that field on the child table. Now, taking another look at the SQLA docs, I'm not even sure why I'm doing that. Realistically, I don't think I'm ever gonna be querying backwards, from a child to its parent; I'll only be determining children from the parent.

Sleepy Robot
Mar 24, 2006
instant constitutional scholar, just add astonomist
I'm trying to piece together a script to automatically adjust screen brightness settings based on a user's location. So far I have it working with hardcoded lat/long values, but I'd like the user to input a city / state and it accurately determines the intended location (lat/long is required for sunset module) or at least offers a set of possible matching cities before converting to coordinates. Is there an API or package that can do this? I'm thinking Google Maps could probably handle it but maybe there's a better approach.

Sleepy Robot fucked around with this message at 23:39 on Mar 10, 2022

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

Sleepy Robot posted:

I'm trying to piece together a script to automatically adjust screen brightness settings based on a user's location. So far I have it working with hardcoded lat/long values, but I'd like the user to input a city / state and it accurately determines the intended location (lat/long is required for sunset module) or at least offers a set of possible matching cities before converting to coordinates. Is there an API or package that can do this? I'm thinking Google Maps could probably handle it but maybe there's a better approach.

https://openweathermap.org/price looks like it has a free tier, which depending on how much calls you're making should be fine.

Ninja Edit: https://openweathermap.org/api/geocoding-api it has a geocoding api, it's not just for weather.

Ninja Edit 2: https://geopy.readthedocs.io/en/stable/ It looks like there's a python library for interacting with geocoding apis too, which might save you a little work.

duck monster
Dec 15, 2004

Geodjango can be a world of suffering sometimes.

Trying to install an app on a Centos 7 machine, and dear god are the dependencies killing me. For various reasons involving people in suits who dont understand technology I cant dockerize it, and this archaic shitful distribution lacks pretty much everything. Worst off, the part that I need most, GEOS, is just a recursive wall of suffering in terms of C/C++ dependencies. Hell, even the version of CMAKE is too old.

Boss better like this poo poo or I'll start flipping cop cars and going berk.

QuarkJets
Sep 8, 2008

Can you use singularity? IIRC it's part of the EPEL and should be much easier to sell cybersecurity people on than Docker. Singularity is unlike Docker in that it's made for environments where users don't have any special permissions outside of their home areas, e.g. multi-user HPC systems where a group may get very mad if you let some other group have access to their data. You can create a singularity container from a docker container, or it's very easy to write your own singularity build script that uses a docker container as its base image.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
I want my users to have the option to create a job by the following process:

Export NAV job card to Excel > Send exported workbook from Excel to jobcreate@company.com > Automatically import that job card with pandas.read_excel() to the database

I can't find anything relevant on actually receiving mail with Flask-Mail and doing something with attachments. Anyone have any idea?

Macichne Leainig
Jul 26, 2012

by VG
Flask-mail's documentation seems to imply its use is only for sending emails, you'd probably have to parse the email some other way.

Fortunately the email builtin package looks like it might handle that.

https://docs.python.org/3/library/email.examples.html

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
I suppose that's something I'll have to look at in the future, thank you :) As of now, the base plan is to have the ability to upload that saved .XLSB file on a page and parse it that way. I took care of the ability to read in a job card today, pandas makes that loving easy once you're familiar with the structure of the exported data.

Ytlaya
Nov 13, 2005

I'm trying to rewrite this old work script (it wasn't working because it was written for an earlier version of Python) and want to complain about this:

code:
x = y = 0
x1 = -1
GeneList2 = []
while x < len(GeneList) and y < len(Names):
    if GeneList[x] == Names[y]:
        x += 1
        y += 1
    elif GeneList[x] < Names[y]:
        if x != x1:
            GeneList2.append(GeneList[x])
            x1 = x
        x += 1
    elif GeneList[x] > Names[y]:
        y += 1
I'm pretty sure this is just comparing the two lists and trying to get the items in the first list that aren't in the second list (because it's followed by writing GeneList2's contents into a file containing the gene IDs that aren't in the DB and then inserting sample data for the ones that do). Just what the heck lol.

CarForumPoster
Jun 26, 2013

⚡POWER⚡

Ytlaya posted:

I'm trying to rewrite this old work script (it wasn't working because it was written for an earlier version of Python) and want to complain about this:

code:
x = y = 0
x1 = -1
GeneList2 = []
while x < len(GeneList) and y < len(Names):
    if GeneList[x] == Names[y]:
        x += 1
        y += 1
    elif GeneList[x] < Names[y]:
        if x != x1:
            GeneList2.append(GeneList[x])
            x1 = x
        x += 1
    elif GeneList[x] > Names[y]:
        y += 1
I'm pretty sure this is just comparing the two lists and trying to get the items in the first list that aren't in the second list (because it's followed by writing GeneList2's contents into a file containing the gene IDs that aren't in the DB and then inserting sample data for the ones that do). Just what the heck lol.

This is academics writing code.

Dr Subterfuge
Aug 31, 2005

TIME TO ROC N' ROLL
Now I’m curious. I would solve that problem by turning the lists into sets and subtracting the second from the first. Is there a way that’s more simple than that?

12 rats tied together
Sep 7, 2006

If you have unique ids that would scream set difference to me, which is what you said yeah

edit: there's of course like 2 dozen other things that are wrong with it, but, i guess one of the nice things about python is that you can fall back to list index nonsense if you didnt bother to read any of the documentation

12 rats tied together fucked around with this message at 22:03 on Mar 11, 2022

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

12 rats tied together posted:

If you have unique ids that would scream set difference to me, which is what you said yeah

edit: there's of course like 2 dozen other things that are wrong with it, but, i guess one of the nice things about python is that you can fall back to list index nonsense if you didnt bother to read any of the documentation

Agreed on set difference - sets are great for when you're trying to do unique IDs of things, and you can use them pretty lazily to just dedupe a list. (as long as comparison/etc is possible between the list objects, etc etc. There are edge cases.)

code:
de_duped_list = list(set(list_of_stuff_with_duplicates))
So really you can probably do that code with this, assuming I didn't miss some use case there.

code:
set_one = set(GeneList)
set_two = set(GeneList2)
return list(set_one.Difference(set_two))

12 rats tied together
Sep 7, 2006

given that the only (presented) desire is to produce the resulting set, I'd probably use a set comprehension which reduces it to one line:
Python code:
{gene for gene in GeneList.difference(Names)}
the control flow is still confusing to me though:
Python code:
    if GeneList[x] == Names[y]:

    elif GeneList[x] < Names[y]:
            GeneList2.append(GeneList[x])

    elif GeneList[x] > Names[y]:
Which is like:

- only if the items are not equal,
- and the GeneList item is smaller than(?) the Names item
- save it and return it

What possible application could this have? The first time we hit an item pair that fails the equality test we start either only walking GeneList or only walking Names, it seems like that goes out of sync instantly and then does absolutely nothing until it catches back up? why not just always walk both lists?

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man
If those are strings I think it means lexically-previous not shorter, like “a” < “b” == True and “b” < “a” == False

CarForumPoster
Jun 26, 2013

⚡POWER⚡
Question: How to replace all regex reserved characters with . in a list of strings? For example replace ( with .
Goal: Filter a DataFrame to a list of matching company names, even when regex characters appear in the names.

Reproduce Error:
Python code:
df = pd.DataFrame(["Bob Smith (DECEASEDAND HEIRSet al.","Walmart, Inc.","Goons, inc."], columns=["company_name"])

good_names = ["Walmart", "Bob Smith (DECEASEDAND HEIRSet al."]

# fails
df = df[df['company_name'].str.contains(
	"|".join(good_names)
)]
Produces error: sre_constants.error: missing ), unterminated subpattern at position 18

I thought this would work:
code:
import re
df = df[df['company_name'].str.contains(
	"|".join([re.sub("[!@#$%^&*()[]{};:,./<>?\|`~-=_+]", ".", name) for name in good_names])
)]
but it doesn't make the sub of (, instead returning: ['Walmart', 'Bob Smith (DECEASEDAND HEIRSet al.']


EDIT: Annnnd 10 seconds after posting I thought of just escaping them which I should've been doing to begin with. I am dumb.

Solution: [re.escape(name) for name in good_names]

CarForumPoster fucked around with this message at 15:48 on Mar 16, 2022

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
When you realize that you can use regex to extract information that's always been a pain in the rear end to parse from a shittily-configured string that serves as some sort of inscrutable machine-friendly code. :hawaaaafap:

Like dealing with 53.125X63.X.,35,STD,1/2" FL,W,EQUAL,G/G 5/16,CL,NO GRID,SS PACKAGE,DBL,SWEEP,1816K-BOXED,LSILL sucks rear end. But I can at least use regex to find things like the glass strength, colors, etc. since they're all predictable values.

Asleep Style
Oct 20, 2010

stupid python question: we have a script where we want to add retry limits on errors for a particular action. there's a bunch of different exceptions that could be causing an error at this point. I'm looking for a way to increment the retry count on any exception, then continue to handle specific exceptions differently, rather than having to add the retry count increment to each specific except statement

original
code:
try:
    foo()
except Ex1 as e:
    log()
    bar()
except Ex2 as e:
    log()
    baz()
the ugly way I thought of doing this
code:
try:
    foo()
except Exception as e:
    increment_database_retry_count()
    try:
        raise e
    except Ex1 as e:
        log()
        bar()
    except Ex2 as e:
        log()
        baz()
any suggestions for better ways of handling this?

boofhead
Feb 18, 2021

this might be ugly or have weird edge cases (something to do with modules and period-separated namespaced exceptions? cant remember, might be confusing it with something else), but i'd probably either use isinstance(e, SomeException) (if you want to catch subclasses as well) or type(e).__name__ == 'SomeException' if you need to be precise

exceptions:

code:
class SightException(Exception):
	pass

class SmellException(Exception):
	pass

class HalitosisException(SmellException):
	# subclass
	pass
with isinstance

code:
try:
	try_to_flirt()
except Exception as e:
	log_to_db('failed to flirt')
	if isinstance(e, SightException):
		comb_hair()
	elif isinstance(e, SmellException):  # will also catch subclasses, e.g. HalitosisException !
		shower()
		brush_teeth()
	elif isinstance(e, HalitosisException):  # will never trigger due to above !
		brush_teeth()
with type().__name__

code:
try:
	try_to_flirt()
except Exception as e:
	log_to_db('failed to flirt')
	if type(e).__name__ == 'SightException':
		comb_hair()
	elif type(e).__name__ == 'SmellException':  # will NOT catch subclasses
		shower()
		brush_teeth()
	elif type(e).__name__ == 'HalitosisException':  # will trigger
		brush_teeth()

boofhead fucked around with this message at 19:14 on Mar 16, 2022

ExcessBLarg!
Sep 1, 2001

CarForumPoster posted:

Goal: Filter a DataFrame to a list of matching company names, even when regex characters appear in the names.
Why not make a set of matching company names and test for membership?

Escaping the list and building the regexp at runtime is code smell at best, and exploitable at worst.

CarForumPoster
Jun 26, 2013

⚡POWER⚡

ExcessBLarg! posted:

Why not make a set of matching company names and test for membership?

Escaping the list and building the regexp at runtime is code smell at best, and exploitable at worst.

Its old code tech debt for an infrequent issue. This bit is for a web scraper that runs once a day on a win10 machine, so refactoring = time I could be poo poo posting.

Asleep Style
Oct 20, 2010

boofhead posted:

this might be ugly or have weird edge cases (something to do with modules and period-separated namespaced exceptions? cant remember, might be confusing it with something else), but i'd probably either use isinstance(e, SomeException) (if you want to catch subclasses as well) or type(e).__name__ == 'SomeException' if you need to be precise

exceptions:

code:
class SightException(Exception):
	pass

class SmellException(Exception):
	pass

class HalitosisException(SmellException):
	# subclass
	pass
with isinstance

code:
try:
	try_to_flirt()
except Exception as e:
	log_to_db('failed to flirt')
	if isinstance(e, SightException):
		comb_hair()
	elif isinstance(e, SmellException):  # will also catch subclasses, e.g. HalitosisException !
		shower()
		brush_teeth()
	elif isinstance(e, HalitosisException):  # will never trigger due to above !
		brush_teeth()
with type().__name__

code:
try:
	try_to_flirt()
except Exception as e:
	log_to_db('failed to flirt')
	if type(e).__name__ == 'SightException':
		comb_hair()
	elif type(e).__name__ == 'SmellException':  # will NOT catch subclasses
		shower()
		brush_teeth()
	elif type(e).__name__ == 'HalitosisException':  # will trigger
		brush_teeth()

this will work great, thank you

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
I...did it. After a full day of trying to figure out just how the gently caress I wanted to handle it, two days in the REPL testing concepts and in actual coding and I can parse both CSV and XML data from our primary window supplier. The relevant class determines the file type, extracts relevant data, and places it in a nice dictionary for the actual calculators to do the work.

I'm kinda glad I decided to refactor from the ground up. :gbsmith: I don't think I'm even going to touch the Flask end of things until I have all my back-end math classes in place. Somehow loving around with regex and pandas is less painful than HTML and CSS to make things look loving nice.


EDIT: "Success" in this case is that aside from the two expected differences between the CSV and XML output files, they are otherwise identical. The only things that should be and indeed are different are the path to the file and the actual data (in one case, the dataframe containing the CSV data and in the other the XML text itself).

D34THROW fucked around with this message at 20:45 on Mar 16, 2022

punk rebel ecks
Dec 11, 2010

A shitty post? This calls for a dance of deduction.
Wait so I read somewhere that Troika Games coded their titles in Python?

So "Vampire: The Masquerade - Bloodlines", "Arcanum", and "Temple of Elemental Evil" were all coded in Python and not like some form of C like almost every other game?

QuarkJets
Sep 8, 2008

iirc Eve Online is coded in some hosed up 1.X fork of Python

Twerk from Home
Jan 17, 2009

This avatar brought to you by the 'save our dead gay forums' foundation.

punk rebel ecks posted:

Wait so I read somewhere that Troika Games coded their titles in Python?

So "Vampire: The Masquerade - Bloodlines", "Arcanum", and "Temple of Elemental Evil" were all coded in Python and not like some form of C like almost every other game?

I would bet they're using Python for game logic, nobody is insane enough to do a 3d engine in Python.

Games tend to have a more comfortable language for logic, whether something custom like UnrealScript in unreal engine 1-3 or a more widely used language like Lua.

EVE online used stackless python for their game logic. I don't know if they still do, but I wouldn't be surprised if it's some horrifying variant.

Data Graham
Dec 28, 2009

📈📊🍪😋



Twerk from Home posted:

I would bet they're using Python for game logic, nobody is insane enough to do a 3d engine in Python.

Games tend to have a more comfortable language for logic, whether something custom like UnrealScript in unreal engine 1-3 or a more widely used language like Lua.

EVE online used stackless python for their game logic. I don't know if they still do, but I wouldn't be surprised if it's some horrifying variant.

For some reason I got the mental image of Tom Scott going :byodood: AUTOHOTKEY

punk rebel ecks
Dec 11, 2010

A shitty post? This calls for a dance of deduction.
Why would Python be bad for building a 3D game engine?

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
Developing a whole engine in Python could be difficult because it's an interpreted language rather than compiled. For the scripting, it's one thing. A whole-rear end engine is another.

Given, once you run it, it does compile to CPython and get cached, but it still has to get interpreted and poo poo. It's also hella slow compared to lower-level languages. This Quora topic had some good answers.

12 rats tied together
Sep 7, 2006

It's a fine language to use for a 3d engine but like most other things that are python but need to be performant, you have to put a lot of work into having as little of it execute in the interpreter as possible. Since Python is simultaneously so slow and so popular, a lot of stuff exists for this that you generally don't have to think about. For example, you can write Panda3D in python but most of your hot path runs as C++ or GLSL/Cg anyway.

punk rebel ecks
Dec 11, 2010

A shitty post? This calls for a dance of deduction.

D34THROW posted:

Developing a whole engine in Python could be difficult because it's an interpreted language rather than compiled. For the scripting, it's one thing. A whole-rear end engine is another.

Given, once you run it, it does compile to CPython and get cached, but it still has to get interpreted and poo poo. It's also hella slow compared to lower-level languages. This Quora topic had some good answers.

I’ve just started learning Java and was wondering why I have to do things like put “int” in front of a number to state it’s a number rather than simply not putting quotes around it. Or why I need curly brackets everywhere.

I thought it was just obtuse but I realize after reading that Quora post it’s likely to increase speed as the language has to do “less checks”.

death cob for cutie
Dec 30, 2006

dwarves won't delve no more
too much splatting down on Zot:4

QuarkJets posted:

iirc Eve Online is coded in some hosed up 1.X fork of Python

the backend is IIRC, I would be surprised if the frontend was or still is after all these years, given that that's a relatively easy overhaul (emphasis on "relative")

12 rats tied together
Sep 7, 2006

IMHO the best resource for learning more about that concept in <15 minutes is to read the section on the stack vs the heap in the rust book's ownership chapter.

After that you can read the python documentation on memory management for some insight into at least one dimension of Python's slowness.

D34THROW
Jan 29, 2012

RETAIL RETAIL LISTEN TO ME BITCH ABOUT RETAIL
:rant:
Ever have those moments when you look back at your old code, or how you used to do things, and have a :smugbert: look on your face?

Last time I did this dance, I input a shitton of data manually into the DB from flask shell.

Now, I'm putting everything in an Excel file, sheet names matching the class names and column names matching the field names for the ORM interfaces, and I'm just gonna use dataframes to read it all into the DB. That way, when I inevitably gently caress something up and have to DROP TABLE *, I don't have to spend an hour retyping poo poo.

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

punk rebel ecks posted:

I’ve just started learning Java and was wondering why I have to do things like put “int” in front of a number to state it’s a number rather than simply not putting quotes around it. Or why I need curly brackets everywhere.

I thought it was just obtuse but I realize after reading that Quora post it’s likely to increase speed as the language has to do “less checks”.

fwiw compiled languages like C++ later added an "auto" variable type that automatically assigns some other type that can be inferred during compilation, and templates exist specifically because it's dumb to have to redefine a function for every valid type that it can use.

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