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
Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
I am setting up a python dev environment on Windows 11, only ever used Linux or Mac. On those I just `conda install python3` and let conda manage all my dependencies cause I typically need the scientific poo poo.

Just do the same thing in Windows?

Adbot
ADBOT LOVES YOU

Twerk from Home
Jan 17, 2009

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

Boris Galerkin posted:

I am setting up a python dev environment on Windows 11, only ever used Linux or Mac. On those I just `conda install python3` and let conda manage all my dependencies cause I typically need the scientific poo poo.

Just do the same thing in Windows?

If you've used Anaconda and it works for you, keep using conda.

I hope you don't need any packages from bioconda, because they do not support Windows.

QuarkJets
Sep 8, 2008

Boris Galerkin posted:

I am setting up a python dev environment on Windows 11, only ever used Linux or Mac. On those I just `conda install python3` and let conda manage all my dependencies cause I typically need the scientific poo poo.

Just do the same thing in Windows?

That's pretty much it, yeah

Oysters Autobio
Mar 13, 2017
Yeah if you're doing science / analysis stuff and already familiar with it I've yet to hear about any killer feature to justify moving from conda to something dev oriented like poetry or whatever.

Slimchandi
May 13, 2005
That finger on your temple is the barrel of my raygun

Boris Galerkin posted:

I am setting up a python dev environment on Windows 11, only ever used Linux or Mac. On those I just `conda install python3` and let conda manage all my dependencies cause I typically need the scientific poo poo.

Just do the same thing in Windows?

Have you considered running WSL2 instead? I use it for dev with poetry and works great.

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

Slimchandi posted:

Have you considered running WSL2 instead? I use it for dev with poetry and works great.

Why would you setup a whole OS layer for python installs? Like WSL is great and all but there's nothing about WSL you can't do with windows as far as python installs and stuff goes.

Edit: Forgot the person you were replying to specifically said they were familiar with Linux. Ignore me.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
Setting up a windows dev environment serves two purposes for me:

1. I’ve never done any programming in a non-*nix based environment. I’m genuinely curious.

2. At my job we use some various command line tools ran via Cygwin, some being python scripts, and let’s just say that these tools are “statically” (i mean, the paths are hard coded, everything is hard coded) linked to a random python.exe file inside a completely and entirely unrelated software folder in c:/program files. These python scripts are like mission critical. Like seriously one of the default things they do is download and install an old version of a program that we don’t use just because all the python scripts we do use point to a python.exe inside that folder.

I’d like to change that but now that I typed this all out I feel like I should just leave it alone.

E: So setting up python in windows for my own personal use was to get python running. I’m (obviously) not going to copy company code onto my personal computer or work for free at home to figure this out, but I figure I could develop a simple concept for my own learning purposes, and then from there see if I can propose to get some support/funding to at the very least set up a dedicated and reproducible python environment for work poo poo.

Boris Galerkin fucked around with this message at 13:47 on Feb 13, 2024

Seventh Arrow
Jan 26, 2005

I have never had good experiences setting up python in windows, so I just use a VM with linux on it.

The Fool
Oct 16, 2003


simple python envs work fine in windows

depending on what you're doing some packages may not work cleanly

that being said, my daily driver is a windows machine but I do 90% of my work in WSL or a docker container.

boofhead
Feb 18, 2021

Yeah I am bad at windows development and setting up environments, if it's for a project I know I'll eventually want/need to dockerise it anyway so I just start with that and then use python from within the container

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
Well I'll weigh in then. On my home computer I use exclusively Windows for dev. I've used both poetry and venv without any trouble and also have used pyenv to manage my various python base installs too. That one took some extra setup but still worked pretty well.

I dev on Linux at work so I guess I could have used WSL, but it just seemed unnecessary. All the confusing parts for me were getting vscode to find the install which isnt an os specific problem as far as I can tell

StumblyWumbly
Sep 12, 2007

Batmanticore!
Is using docker to do builds with pyinstaller a dumb idea or a great idea?
It seems like a venv could work just as well but there's always something weird going on

CarForumPoster
Jun 26, 2013

⚡POWER⚡

StumblyWumbly posted:

Is using docker to do builds with pyinstaller a dumb idea or a great idea?
It seems like a venv could work just as well but there's always something weird going on

Ive only ever pip installed my requirements in the container's dockerfile but is always for AWS Lambda.

StumblyWumbly
Sep 12, 2007

Batmanticore!
What I mean is, we use pyinstaller to make executables for our stuff. It always seems like this is a very delicate process because we have dlls and special files to include, so I've been encouraging folks to set up a docker that can make the build.
I feel like a venv should fix these problems but it also sounds like it hasn't in the past, and I'm not sure if I'm remembering the ancient past or if there are things that just work better outside a virtual environment

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug
I dunno, sounds like a container might be perfect if you have weird dlls and stuff. No experience with building that into venvs etc.

If a solution works it's probably not that stupid.

The Fool
Oct 16, 2003


Doing your build in docker is not a terrible idea except for the fact that windows containers are horrible.

xzzy
Mar 5, 2009

Anyone got a technique or tips for having a globally available script run from a venv without making a user source the activate script? It also needs to support tab completion (using the argcomplete module).

I've got a script that needs some newer modules than the base OS provides, and I'm not able to mess with OS packages. I've done some googling and found some suggestions for making a wrapper script or setting up an alias setting with environment variables, but nothing really works all that well. The tab completion is the biggest hangup.

I know I'm going to have to drop a script in profile.d to do some setup and that's fine, but it would be nice if it could work transparently without interfering with other python scripts that aren't in the venv.

QuarkJets
Sep 8, 2008

StumblyWumbly posted:

What I mean is, we use pyinstaller to make executables for our stuff. It always seems like this is a very delicate process because we have dlls and special files to include, so I've been encouraging folks to set up a docker that can make the build.
I feel like a venv should fix these problems but it also sounds like it hasn't in the past, and I'm not sure if I'm remembering the ancient past or if there are things that just work better outside a virtual environment

A build container is the ideal usecase for this kind of thing, you're right. With venv you get a partial sandbox, pyinstaller can be dependent on more than what venv alone provides.

QuarkJets
Sep 8, 2008

xzzy posted:

Anyone got a technique or tips for having a globally available script run from a venv without making a user source the activate script? It also needs to support tab completion (using the argcomplete module).

I've got a script that needs some newer modules than the base OS provides, and I'm not able to mess with OS packages. I've done some googling and found some suggestions for making a wrapper script or setting up an alias setting with environment variables, but nothing really works all that well. The tab completion is the biggest hangup.

I know I'm going to have to drop a script in profile.d to do some setup and that's fine, but it would be nice if it could work transparently without interfering with other python scripts that aren't in the venv.

Write a wrapper script that activates the environment and then runs the script. Place that wrapper script in a common PATH location.

Twerk from Home
Jan 17, 2009

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

xzzy posted:

Anyone got a technique or tips for having a globally available script run from a venv without making a user source the activate script? It also needs to support tab completion (using the argcomplete module).

I've got a script that needs some newer modules than the base OS provides, and I'm not able to mess with OS packages. I've done some googling and found some suggestions for making a wrapper script or setting up an alias setting with environment variables, but nothing really works all that well. The tab completion is the biggest hangup.

I know I'm going to have to drop a script in profile.d to do some setup and that's fine, but it would be nice if it could work transparently without interfering with other python scripts that aren't in the venv.

Look at how pipx works, or just use it directly: https://github.com/pypa/pipx

A virtual environment for each script, and an entrypoint script put somewhere on the users path that launches the right python.

WHERE MY HAT IS AT
Jan 7, 2011
There's also Pex: https://pex.readthedocs.io/en/v2.1.163/

It requires that an interpreter matching your constraints be present on the system already, but otherwise, it includes all runtime dependencies and acts as a hermetic environment like a venv does.

12 rats tied together
Sep 7, 2006

I used pex for a thing and I liked it. We ended up shelving it and writing a rust version of the tool instead, and if we need to do more stuff in this area later, I have "try nuitka" on my todo list.

Seventh Arrow
Jan 26, 2005

I'm doing some projects and coding challenges to try and whip myself back into shape, and I encountered this:

quote:

Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;

front_times('Chocolate', 2) → 'ChoCho'
front_times('Chocolate', 3) → 'ChoChoCho'
front_times('Abc', 3) → 'AbcAbcAbc'

My solution was as follows:

code:
def front_times(str, n):
  # in case some wiseguy tries to enter a negative number
  if n < 0:
    return "Please enter a non-negative number"
  # return n copies of the first 3 characters
  else:
    return str[:3] * n
The official solution is thus:

code:
def front_times(str, n):
  front_len = 3
  if front_len > len(str):
    front_len = len(str)
  front = str[:front_len]
  
  result = ""
  for i in range(n):
    result = result + front
  return result
My simpler code passed all of the tests. Is the official solution needlessly complex? Or am I missing something by dumbing it down?

Seventh Arrow
Jan 26, 2005

Actually, I think I can probably even do everything in a single line:

code:
def front_times(str, n):
    return str[:3] * n if n > 0 else "Please enter a non-negative number"

Zugzwang
Jan 2, 2005

You have a kind of sick desperation in your laugh.


Ramrod XTreme
The official solution is really verbose, non-Pythonic code, and the verbosity doesn't add anything useful like error checking. I don't think you're missing anything.

E: It's possible the official solution is written that way for teaching purposes, since each step in the logic is explicit. Like, by taking 4 lines of code to get "front," that avoids needing to know that if you slice a string to a higher end index than the string's length, you just get whatever's in the string.

Zugzwang fucked around with this message at 06:36 on Feb 15, 2024

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!
I think that if you’re learning to code, it’s better to be more explicit like their solution. If you’re just learning Python do what you’re doing.

Kinda like math, how you’re very much encouraged to "show your work" so to speak. I wouldn’t expect students in like physics 400+ to show every single step of a derivative … but the intro to calculus 101 students would be expected to.

Boris Galerkin fucked around with this message at 12:39 on Feb 15, 2024

StumblyWumbly
Sep 12, 2007

Batmanticore!

Seventh Arrow posted:

Actually, I think I can probably even do everything in a single line:

code:
def front_times(str, n):
    return str[:3] * n if n > 0 else "Please enter a non-negative number"


Your code is correct and generally better, but simple is not always shorter. Using that type of if statement is generally just unnecessarily dense. In some places like list comprehension I think it can make things more efficient, but if you can use a standard if statement instead you probably should

boofhead
Feb 18, 2021

yeah i'd just omit it and add a comment

Python code:
def repeat_text_start(text: str, num: int) -> str:
    """Returns the first 3 chars of a 'text' string repeated 'num' times."""
    # TODO! validate args, handle exceptions
    return text[:3] * num
so that they know i know it's not production-ready, but validation and exception handling is beyond the scope of the task (or ticket, if you're dealing with lazy product managers)

as far as im aware the above code wont break for any value of text or num, so long as they're strings and integers respectively. 0, -1, or an empty string should all work and just return '' (i.e. an empty string). and value[:3] will just return whatever it has if the length is less than 3

+1 for not being a fan of conditionals coming after the action, feels like a minor code smell to me

e: also you should keep an eye on your variable names, calling a string variable "str" is a bad habit to get into

boofhead fucked around with this message at 14:12 on Feb 15, 2024

BAD AT STUFF
May 10, 2012

We choose to go to the moon in this decade and do the other things, not because they are easy, but because fuck you.

Seventh Arrow posted:

Actually, I think I can probably even do everything in a single line:

code:
def front_times(str, n):
    return str[:3] * n if n > 0 else "Please enter a non-negative number"


I'd prefer your first conditional approach, both for clarity and because it would be more Pythonic to raise a ValueError rather than saying what's wrong in the return value.

Seventh Arrow
Jan 26, 2005

Yes I was thinking of a better way to handle that, so ValueError seems ideal. Thanks!

boofhead
Feb 18, 2021

e: ah jeez i misread the post lmao, ignore

QuarkJets
Sep 8, 2008

Seventh Arrow posted:

I'm doing some projects and coding challenges to try and whip myself back into shape, and I encountered this:

My solution was as follows:

code:
def front_times(str, n):
  # in case some wiseguy tries to enter a negative number
  if n < 0:
    return "Please enter a non-negative number"
  # return n copies of the first 3 characters
  else:
    return str[:3] * n
The official solution is thus:

code:
def front_times(str, n):
  front_len = 3
  if front_len > len(str):
    front_len = len(str)
  front = str[:front_len]
  
  result = ""
  for i in range(n):
    result = result + front
  return result
My simpler code passed all of the tests. Is the official solution needlessly complex? Or am I missing something by dumbing it down?

imo the actual solution is outright bad code, these things need to go or be changed:
- Argument name "str"
- String concatenation in a for loop
- Bullshit function name
- No docstring

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

QuarkJets posted:

imo the actual solution is outright bad code, these things need to go or be changed:
- Argument name "str"
- String concatenation in a for loop
- Bullshit function name
- No docstring
Other than the input variable shadowing a builtin, which has the potential to create gnarly bugs later on, these aren't the kinds of things new devs, or arguably any devs, really need to spend cognitive energy on. (IMO, all internal utility function names are bullshit no matter how much you pretend they aren't. Beyond a small handful of developers, none of them will ever see actual reuse.)

Beyond the function name, these are all things that would get caught by a linter like flake8 or pylint, and it's a good idea to get in the habit of running code through these sorts of tools to see where they deviate from accepted style or hit things that, at scale, would become obvious performance torpedoes.

Vulture Culture fucked around with this message at 13:43 on Feb 16, 2024

Armitag3
Mar 15, 2020

Forget it Jake, it's cybertown.


Vulture Culture posted:

Other than the input variable shadowing a builtin, which has the potential to create gnarly bugs later on, these aren't the kinds of things new devs, or arguably any devs, really need to spend cognitive energy on. (IMO, all internal utility function names are bullshit no matter how much you pretend they aren't. Beyond a small handful of developers, none of them will ever see actual reuse.)

I still avoid using id as an attribute of a model because of that best practice, but yeah I don't think I've ever seen id() being used in the wild.

QuarkJets
Sep 8, 2008

Vulture Culture posted:

Other than the input variable shadowing a builtin, which has the potential to create gnarly bugs later on, these aren't the kinds of things new devs, or arguably any devs, really need to spend cognitive energy on. (IMO, all internal utility function names are bullshit no matter how much you pretend they aren't. Beyond a small handful of developers, none of them will ever see actual reuse.)

Beyond the function name, these are all things that would get caught by a linter like flake8 or pylint, and it's a good idea to get in the habit of running code through these sorts of tools to see where they deviate from accepted style or hit things that, at scale, would become obvious performance torpedoes.

Doesn't saying "flake8 will see some these and then spit in your face" dispute the notion that new developers don't need to think about them?

The missing docstring and bad function name are probably the two most important items on that list, new developers should definitely be thinking about these things when writing a new function. Every time.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

QuarkJets posted:

Doesn't saying "flake8 will see some these and then spit in your face" dispute the notion that new developers don't need to think about them?
Someone's job with new devs, whether an engineering manager or a computer science teacher or an effective mentor, is to get their brains into something resembling a flow state, not to avoid triggering senior engineers' pet peeves. Clean it up later (code review, fast follow, maybe never) if it matters. There's a reason these contrived problems have so many sub-optimal solutions being published, and it's not that everyone involved is dumb.

QuarkJets posted:

The missing docstring and bad function name are probably the two most important items on that list, new developers should definitely be thinking about these things when writing a new function. Every time.
There's value in building high-quality libraries, but I also seriously question the time management of anyone who overinvests in these things for irrelevant little internal functions that only exist to reduce LOC in some actually-important function or another. If you have a real problem with comments to the extent that you need to measure percentages of functions with docstrings and ensure number only ever go up, sure, do what you've got to do to make that happen. Pretending everyone has this problem doesn't make a piss function missing a docstring "outright bad code".

Vulture Culture fucked around with this message at 13:40 on Feb 20, 2024

QuarkJets
Sep 8, 2008

It's easy to say that anything is bad if you "overinvest" in it, that's not a real argument. Writing concise function names and concise docstrings are fundamental coding skills, they're as important as knowing how to define a function in the first place, and you get better at those skills with practice and review. You're not doing a new developer any favors by letting them skimp

Falcon2001
Oct 10, 2004

Eat your hamburgers, Apollo.
Pillbug

QuarkJets posted:

It's easy to say that anything is bad if you "overinvest" in it, that's not a real argument. Writing concise function names and concise docstrings are fundamental coding skills, they're as important as knowing how to define a function in the first place, and you get better at those skills with practice and review. You're not doing a new developer any favors by letting them skimp

I agree with this. I also agree that it's fine for them to backfill that during code review. I think it's challenging for noobs to really realize how frustrating debugging or improving code without docstrings etc can be, especially in Python where type hinting is optional.

QuarkJets
Sep 8, 2008

If we're saying that pull requests with badly named and undocumented functions should be blocked until those issues are fixed then we're all on the same page. If you're just messing around in a notebook then go nuts

Adbot
ADBOT LOVES YOU

Oysters Autobio
Mar 13, 2017
Any pandas experts here?

I've got an adhoc spreadsheet I need to clean for ETL (why this is my job as a data analyst, I don't know) but I'm having trouble even articulating the problem decently enough to leverage ChatGPT or Google.

Spreadsheet (.XLSX file) is structured like this


code:

+----------+-------+------------+
| category | names | dates      |
+----------+-------+------------+
| Western  |       |            |
| 1        | Jim   | 2023.02.01 |
| 2        | Greg  | 2013.12.11 |
| 3        | Bob   | 2003.07.11 |
| Eastern  |       |            |
| 1        | Jess  | 2002.02.01 |
| 2        | Bill  | 2001.10.11 |
|          |       |            |
|          |       |            |
+----------+-------+------------+


Repeat for many more categories (i.e. "Southern", "Southwestwern") each with different ranges of numbers following them with matching names/dates.

Reading it in as a data frame with defaults has pandas assigning the "category" column as the index, but despite reading docs and googling I still can't really wrap my head around multi - indexes. Lots of tutorials on creating them, or analysing them, but can't find anything for the reverse (taking a multi-level index and transforming it into a repeating column value). The target model should be something like this:

code:

+---+----------+-------+------------+
|   | category | names | dates      |
+---+----------+-------+------------+
| 1 | Western  | Jim   | 2023.02.01 |
| 2 | Western  | Greg  | 2013.12.11 |
| 3 | Western  | Bob   | 2003.07.11 |
| 1 | Eastern  | Jess  | 2002.02.01 |
| 2 | Eastern  | Bill  | 2001.10.11 |
|   |          |       |            |
+---+----------+-------+------------+


edit: should specify that we'll like receive these types of spreadsheets with different labels and values. So I'd like to write it to be somewhat parameterized and reusable, which is why I'm not just hacking through this for a one-time ETL

Oysters Autobio fucked around with this message at 02:32 on Feb 21, 2024

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