|
Use while true and set the answer at the top of the loop. As written you only read the input once.
|
# ? Aug 28, 2019 14:31 |
|
|
# ? May 27, 2024 00:00 |
|
Also you can use a break statement to exit a loop https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops.
|
# ? Aug 28, 2019 17:47 |
|
The break is already in the right spot, but an input other than Y will not reach it and loop forever without requesting a new input.
|
# ? Aug 28, 2019 18:09 |
You can also do it recursively:pre:def cat_loop_recursive(input=None): if input == 'Y': return 'yay, cat!' else: print "Do you pet the cat?" user_input = get_input() return cat_loop_recursive(input=user_input)
|
|
# ? Aug 28, 2019 18:52 |
|
You can in this case but I wouldn't get into the habit of using recursion in Python because it defaults to 1k maximum iterations due to not having tail call optimization
|
# ? Aug 28, 2019 19:03 |
|
Stop being cruel to the poor newbie you guys
|
# ? Aug 28, 2019 19:10 |
That was a bit ridiculous. Really, you just want the exit condition in the loop definition:pre:cat_petted = False #boolean to keep track of whether the cat has been petted yet while not cat_petted: ask for input and set cat_petted appropriately a foolish pianist fucked around with this message at 19:36 on Aug 28, 2019 |
|
# ? Aug 28, 2019 19:32 |
|
Dancer posted:Stop being cruel to the poor newbie you guys The first reply was exactly what to do lol To be more clear, the `answer = input...` thing is outside the while loop, so it's only run once. So your `while answer` thing is always referring to the initial input as `answer` never changes. What you want is `while True:` followed by `answer = input...` which is how Python does a do-while loop.
|
# ? Aug 28, 2019 19:33 |
I really prefer a control variable set ahead of time to a while True: ... break()pre:finish_condition = False while not finish_condition: do_stuff() if should_exit(): finish_condition=True
|
|
# ? Aug 28, 2019 19:43 |
|
I’m making a toy script to check the git repo status of all the repos I have on my computer. Right now I’m just using subprocess.run and doing some regex on stdout to check things like if I’m behind or have new files or whatever. Is there an easier way to do this with a package and which one?
|
# ? Aug 29, 2019 17:24 |
|
Boris Galerkin posted:I’m making a toy script to check the git repo status of all the repos I have on my computer. Right now I’m just using subprocess.run and doing some regex on stdout to check things like if I’m behind or have new files or whatever.
|
# ? Aug 29, 2019 17:42 |
|
a foolish pianist posted:I really prefer a control variable set ahead of time to a while True: ... break() There are more or less infinite ways to do this but if you just put it in a function you can hide it somewhere and never look at your dirty while True loop: Python code:
Wallet fucked around with this message at 00:03 on Aug 30, 2019 |
# ? Aug 29, 2019 22:26 |
|
Given the "problem" requires repeating the question unless "Y" is given, and having different responses for "N" vs any other character, that doesn't seem like a great approach... edit: I'm pretty sure you just wanted to give an example of the approach but it totally fails for the "problem" presented.
|
# ? Aug 30, 2019 00:43 |
|
Here's an example of exactly the kind of thing I get stumped by and whatever decision I make I eventually regret so I'll just ask this before painting myself into some dumb corner I'll be furiously refactoring a week from now: is it weird to write a utility function to grab poo poo from the Internet (implemented with requests, of course) with logging and exception handling to reuse wherever I would do that in my code, throwing a user-defined exception instead of RequestException? I can't tell if this is straightforward DRY or overengineering that will end up with me adding parameters as I find more use cases and just unnecessarily replicating the requests API. Like, this:Python code:
(Incidentally, this is the first code I've gotten to use f-strings in and they own, feels very Pythonic -- another plus of Docker, like we were talking about earlier, I can easily use 3.7 without giving a poo poo what's in the repos for the host OS.)
|
# ? Aug 30, 2019 02:09 |
|
I have no real advice but I'm glad you're using type annotations.
|
# ? Aug 30, 2019 03:04 |
|
Wrapping requests (or anything) up can be useful if there's stuff you need to do every time that it's not already doing. Without further context, I don't see much advantage of what you've written there over enabling the logging that requests/it's dependencies already do, putting a response.raise_for_status() after your get, and catching the already thrown exceptions wherever if need be. I guess there is potentially some different behavior for 3xx status codes, but those are redirects not failures, and requests follows them by default, so I don't think you will hit that else branch in too many situations where raise_for_status wouldn't raise an exception. I guess a literal 300 response might result in some different behavior but I don't think I've ever actually seen one, I have no idea what requests does with it.
|
# ? Aug 30, 2019 03:08 |
|
Also my man bromplicated up there is already on 3.8 while yall still on 2.7, just a couple typos is all.
|
# ? Aug 30, 2019 04:04 |
|
necrotic posted:Use while true and set the answer at the top of the loop. As written you only read the input once. xtal posted:The first reply was exactly what to do lol Thank you guys. I knew it was something really simple.
|
# ? Aug 30, 2019 12:01 |
|
necrotic posted:Given the "problem" requires repeating the question unless "Y" is given, and having different responses for "N" vs any other character, that doesn't seem like a great approach... I was being lazy (and still am), but you get the idea: Python code:
Wallet fucked around with this message at 13:44 on Aug 30, 2019 |
# ? Aug 30, 2019 13:24 |
|
breaks posted:Wrapping requests (or anything) up can be useful if there's stuff you need to do every time that it's not already doing. Without further context, I don't see much advantage of what you've written there over enabling the logging that requests/it's dependencies already do, putting a response.raise_for_status() after your get, and catching the already thrown exceptions wherever if need be. I guess there is potentially some different behavior for 3xx status codes, but those are redirects not failures, and requests follows them by default, so I don't think you will hit that else branch in too many situations where raise_for_status wouldn't raise an exception. I guess a literal 300 response might result in some different behavior but I don't think I've ever actually seen one, I have no idea what requests does with it. Solumin posted:I have no real advice but I'm glad you're using type annotations.
|
# ? Aug 31, 2019 02:07 |
|
Type annotations [rich type systems in general] catch classes of errors that would otherwise show up as surprises during execution. Whether it's a compiler, checker like MyPy, or IDE like PyCharm. Unfortunately, ergonomics are awkward when using non-typed dependencies.
Dominoes fucked around with this message at 06:09 on Aug 31, 2019 |
# ? Aug 31, 2019 06:07 |
|
A random-ish question that's proving itself surprisingly hard to google. I had a class called Command, that was giving all sorts of weird errors. When I was initializing it, with a bunch of arguments, it failed because for some reason it was using __call__ (which I hadn't even written for it) instead of __init__. After improvising a __call__ to try to make it work it was raising an error that __call__ had the wrong number of arguments. This all got fixed when I just renamed it to Commandx, leading me to believe that there's some built-in python thing titled Command. But, as you might imagine, typing "python command" and other similar things isn't being helpful . Is Command a thing in Python?
|
# ? Aug 31, 2019 12:42 |
|
Dancer posted:A random-ish question that's proving itself surprisingly hard to google. I had a class called Command, that was giving all sorts of weird errors. When I was initializing it, with a bunch of arguments, it failed because for some reason it was using __call__ (which I hadn't even written for it) instead of __init__. After improvising a __call__ to try to make it work it was raising an error that __call__ had the wrong number of arguments.
|
# ? Sep 1, 2019 22:01 |
|
Another thing you can do is, before you define your Command class, do this:Python code:
|
# ? Sep 1, 2019 22:10 |
Does anyone have any recommendations for a good 2D drawing library? I'm using Pillow right now but it's kind of limited as far as shapes go. Aggdraw is pretty much exactly what I'm looking for, but it's Python 2 only.
|
|
# ? Sep 3, 2019 02:39 |
|
i vomit kittens posted:Does anyone have any recommendations for a good 2D drawing library? I'm using Pillow right now but it's kind of limited as far as shapes go. Aggdraw is pretty much exactly what I'm looking for, but it's Python 2 only. I don't really have much experience in the area, but pycairo or maybe even pygame. Both things I've used to draw stuff before...
|
# ? Sep 3, 2019 02:43 |
|
i vomit kittens posted:Does anyone have any recommendations for a good 2D drawing library? I'm using Pillow right now but it's kind of limited as far as shapes go. Aggdraw is pretty much exactly what I'm looking for, but it's Python 2 only. I know almost nothing about this but the free vector drawing software Inkscape seems to use python for extensions. There may be an interface there that allows you rather robust software with python.
|
# ? Sep 3, 2019 02:49 |
I'll take a look at those. What I'm trying to do (and having trouble with in Pillow) is make a box for text that is somewhat form fitting to the text. Also I need an arrow going from the text box to the character saying it (it's a comic) and Pillow's arc objects for some reason don't have an option to control their thickness. I'm making a comic generator similar to what the Discord bot "Sepatpus" does. It reads the last X messages in a chat and generates a comic of them with random characters/backgrounds taken from Microsoft Comic Chat. I wanted to port this functionality to GroupMe but already have a functioning bot written in Python and don't like Go much so I decided to do it myself. At first I was just trying to port the code over 1:1 but that wasn't working so well so I just started from scratch. It doesn't do the text part yet, since that's what I'm figuring out. Right now it just generates the panels, backgrounds, and characters. It also doesn't look too great when you have > 8 messages or one person who says > 2 things in a row, so those are other things I need to sort out afterwards. i vomit kittens fucked around with this message at 19:10 on Sep 30, 2019 |
|
# ? Sep 3, 2019 03:20 |
|
I use qt for gui design. It draws all kinds of shapes very well! And it has the other features you're looking for. I don't know whether it's the best tool for the job but it would certainly work
|
# ? Sep 3, 2019 03:35 |
|
Don't have a Python question at the moment (but will add type annotations to the code I'm currently working on), but had some input on Windows 10 and Docker / VirtualBox discussion on the last few pages. You can create a separate Windows 10 boot option w/ Hyper-V disabled for when you want/need to run VirtualBox. Granted, you have to reboot, but for my use case it was a pretty easy and clean solution using bcdedit: https://stackoverflow.com/a/43843318 [1] You may need to hold down [Shift] when clicking Restart to get the boot menu the first time? I seem to get the "choose boot option" screen every time I reboot now. YMMV. [1] Alternate instructions: https://blogs.technet.microsoft.com/francescovb/2016/11/22/dual-boot-with-no-hyper-v/
|
# ? Sep 3, 2019 04:00 |
|
Wallet posted:
Can anyone talk more about this? I wrote a Fridge script, where you put different categories of food in a fridge and at the end it lists them back to you. I wrote it in a way where it defines a new function for each food category. Is there a way I could write one function to handle all the different food types?
|
# ? Sep 4, 2019 02:44 |
|
code:
|
# ? Sep 4, 2019 02:54 |
|
I started writing it as a Fridge class, but got stuck doing that because I kept getting NameError where the instance of fridge I was making was calling lists instead of strings or integers. Instead I just wrote a series of functions and that was much easier. Most other people's code I see seem to just use functions. When is a good time to use Classes vs. Functions?
|
# ? Sep 4, 2019 03:08 |
|
You're going to get a number of answers, I think. Which is good -- programming is problem solving, and people solve problems in different ways. Functions are purely behavior: they take some input, do some stuff to it, and possibly produce some output. Classes combine behavior with state. They encapsulate some data and the functions that operate on that data. Your Fridge is a good example: it encapsulates the contents of the fridge (data) with functions that affect the contents of the fridge. A Fridge class might have store() and remove() methods, for example. You could do those as standalone functions, of course: code:
This works fine, but what happens if you need more than one Fridge? Well, one way is to make the functions take a Fridge as an argument: code:
code:
As our code gets more complicated, classes help us keep everything organized. It's easier to import modules that use classes, it's easier to use class objects instead of the separate data/functions, and it lets you reuse names of things. (What if we wanted a Wardrobe class that also stored a quantity of things? We'd like to have store() and remove() methods!) My personal way of thinking about this is that classes store data and functions modify data. (But I'm at heart a functional programmer.)
|
# ? Sep 4, 2019 03:44 |
|
Here's the first functional release of the package manager I posted about earlier. After feedback from last time, I think the readme does a better explaining its purpose, ie why I built it when pip/venv/poetry/pipenv and Conda exist.
|
# ? Sep 4, 2019 13:00 |
|
This question is more suited for a general "how to talk with serial peripherals" conversation than Python itself but I'll give it a shot: I'm trying to do work out a thing with an analog modem that picks up when the someone calls on a land line, but my code isn't reliable. The program runs ok until the second to last line, but then it frequently misses the RING word. Calling a second time usually goes through , and then it finds the caller number just fine. Minicom shows the modem behaving as expected so I'm don't suppose it's a hardware issue. I'd appreciate ideas or pointers to address this. e: I use pySerial unpacked robinhood fucked around with this message at 17:26 on Sep 4, 2019 |
# ? Sep 4, 2019 17:23 |
|
Dominoes posted:Here's the first functional release of the package manager I posted about earlier. After feedback from last time, I think the readme does a better explaining its purpose, ie why I built it when pip/venv/poetry/pipenv and Conda exist. quote:Multiple versions of a dependency can be installed, allowing resolution of conflicting sub-dependencies, and using the highest version allowed for each requirement. How did you achieve this?
|
# ? Sep 4, 2019 17:42 |
|
Renaming imports, both in the dependency and its parent. It's currently fragile: Packages that include relative imports in compiled code don't work, and publishing a package relying on this wouldn't work properly unless its user also used something capable of handling this. I'm looking for suggestions on how to improve this, with the default answer being it's not possible to do correctly.
|
# ? Sep 4, 2019 23:35 |
|
bromplicated posted:I wrote it in a way where it defines a new function for each food category. Is there a way I could write one function to handle all the different food types? Solumin gave a great overview of why you might want to use classes and what you might use them for. I don't know exactly what your script did, but to give you a simple version of putting different categories of food into a fridge and then listing everything that was added back at the end: Python code:
Wallet fucked around with this message at 01:03 on Sep 5, 2019 |
# ? Sep 5, 2019 00:39 |
|
|
# ? May 27, 2024 00:00 |
|
Dominoes posted:Renaming imports, both in the dependency and its parent. It's currently fragile: Packages that include relative imports in compiled code don't work, and publishing a package relying on this wouldn't work properly unless its user also used something capable of handling this. Does it handle imports done with importlib? That's pretty common, so if it doesn't you should probably make a note in the readme or something.
|
# ? Sep 5, 2019 01:10 |