Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Activity for Karl Knechtel‭

Type On... Excerpt Status Date
Answer A: Why does `distutils` seem to be missing or broken? Isn't it part of the standard library?
Understanding distutils and setuptools: the history The short version of "why does installing Setuptools fix the problem?" is that Setuptools provides its own `distutils` package, overriding anything the standard library might or might not have. When you `import setuptools` explicitly, it furtherm...
(more)
26 days ago
Question Why does `distutils` seem to be missing or broken? Isn't it part of the standard library?
Sometimes when I try to install a third-party library for Python, I get an error saying that either `distutils`, or some part of it like `distutils.core` or `distutils.util`, could not be found. It's shown as a `ModuleNotFoundError`, so presumably it's coming from Python when it tries to run a `setup...
(more)
26 days ago
Answer A: How and where does Python code start running? How can I control that?
Starting up Python There are several key ways to use the `python` command from the command line: `python` by itself starts a REPL that allows typing in and running Python code on the fly, one statement at a time. `python myscript.py` runs the code in `myscript.py`, assuming it's in the cur...
(more)
30 days ago
Question How and where does Python code start running? How can I control that?
Suppose I have some code in a file `myscript.py` like: ``` def myfunction(): print("Test") ``` What steps do I need to take in order to make `myfunction` run? That is to say: how do I get Python to run `myscript.py`; and from there, how do I choose `myfunction` as the starting point for ...
(more)
30 days ago
Answer A: Privilege escalation from Python like from systemd
Generallly (assuming no exploit is found) it's not possible to escalate the privileges of an already running process, except via code that itself has kernel level access (running in ring 0 - this is presumably how `systemd` is able to do it). But from your own Python program running in user-land (as ...
(more)
about 1 month ago
Answer A: How can I manage multiple consecutive strings in a buffer (and add more later)?
The fundamental problem here is that it is already ambiguous where the "end" of the data in the buffer is. Strings can be empty (have zero length as reported by `strlen`); as such, `buffer` could equally well be interpreted as containing three strings, where the last is empty. Or more than that - up ...
(more)
about 2 months ago
Question How can I manage multiple consecutive strings in a buffer (and add more later)?
This question is inspired by If I have a char array containing strings with a null byte (\0) terminating each string, how would I add another string onto the end? on Stack Overflow. Suppose I have a `char[]` buffer that I'm using to represent multiple null-terminated (ASCII) strings, one after the...
(more)
about 2 months ago
Answer A: Don't close questions for lack of detail/confusion
No. Terms like "too generic", "unclear", "too broad", "off topic" are absolutely not euphemisms for "stupid question, go away". They mean what they say; and when they are used Somewhere Else, multiple of them exist simultaneously for a reason. They are explicitly not designed to be used to judge t...
(more)
2 months ago
Answer A: Open file in script's own folder
Theory Relative paths are relative to the current working directory of the Python process. This can be checked from within Python using the `os.getcwd` function and set using the `os.chdir` function. Whenever Python loads a module, a `file` attribute may be set on the resulting module object, g...
(more)
4 months ago
Answer A: How can I provide additional information when raising an exception?
The `raise` statement in Python accepts either an Exception class or an instance of that class. When used with a class, Python will create and raise an instance by passing no arguments to the constructor. Python exception constructors, by default, accept an arbitrary number of positional arguments...
(more)
4 months ago
Answer A: Detecting balanced parentheses in Python
Command-line timing Rather than using separate code for timing, I tried running the `timeit` module as a command-line tool. I wrote initial versions of the implementations based on the OP and hkotsubo's answer, as a file `balance.py`: `balance.py` ``` def usingreplace(s): if len(...
(more)
4 months ago
Answer A: JSON log formatter
PEP 8 Four-space indentation is the community standard. There are many strategies for wrapping long lines; I have found that it's often best to just use temporary variables to avoid wrapping lines. Avoid pointless classes The `UncaughtExceptionHook` class is a textbook example of the design ...
(more)
4 months ago
Answer A: How to troubleshoot ModuleNotFoundError?
Root cause `ModuleNotFoundError` is a kind of `ImportError`. It occurs when Python code tries to use an absolute import, but can't find the named module - just as the name suggests. (Failed relative imports will only raise the base `ImportError`. This makes sense because an absolute import involve...
(more)
4 months ago
Question What does "namespace" mean?
A well-known easter egg in Python displays some ideas about writing good Python code, credited to Tim Peters (one of the core developers). This question is about the last: > Namespaces are one honking great idea -- let's do more of those! It seems that the concept of a "namespace" appears in pr...
(more)
6 months ago
Answer A: Does Python have a "ternary operator" (conditional evaluation operator like "?:" in other languages)?
How to do it Yes, Python has an equivalent operator. However, it's spelled with keywords rather than punctuation, and it uses a different order of operands. It looks like: ```python conditionvalue if somecondition() else notconditionvalue ``` This creates an expression, so the result can then ...
(more)
6 months ago
Question Does Python have a "ternary operator" (conditional evaluation operator like "?:" in other languages)?
Sometimes code needs to assign (or otherwise use) a value that depends on some condition. The naive approach is to use explicit branching, which in Python would look like: ``` if somecondition(): avariable = conditionvalue else: avariable = notconditionvalue ``` However, this seems nee...
(more)
6 months ago
Question Git-ignoring files with special characters in their names, especially newlines
My actual motivation is to understand the semantics of the .gitignore file syntax in precise detail, for a program which is expected to emulate them as accurately as possible. However, while coming up with test cases I realized an interesting problem. Suppose, on Linux, I have created a file with ...
(more)
6 months ago
Answer A: Why are list comprehensions written differently if you use `else`?
These two uses of `if` are different The `if` at the end of a list comprehension syntax: ```python [num for num in hand if num != 11] ``` is a filter; its purpose is to decide whether or not the resulting list should contain a value that corresponds to any given `num` (`for` each one found `in...
(more)
7 months ago
Answer A: Understanding mutable default arguments in Python
Possible justifications It may make sense to use a mutable default argument in the following situations: For simplicity Consider for example an argument that should be some kind of mapping, where the function will only use it for lookup without actually mutating the provided object: ``` ...
(more)
8 months ago
Answer A: Understanding mutable default arguments in Python
Workarounds Avoiding mutation Because problems are only caused by actually mutating the default argument, the simplest way to avoid problems is to... not do that. Pythonic code obeys command-query separation; a function should take its effect either by mutating one or more parameters (a "comman...
(more)
8 months ago
Answer A: Understanding mutable default arguments in Python
Terminology "Mutable default argument" means exactly what the individual words would suggest: it's an argument which is supplied as the default value for that parameter, which also is mutable. To mutate an object means to change its state - for a list, that includes adding, removing, replacing or ...
(more)
8 months ago
Question Understanding mutable default arguments in Python
Consider this code example: ``` def example(param=[]): param.append('value') print(param) ``` When `example` is repeatedly called with an existing list, it repeatedly appends to the list, as one might expect: ``` >>> mylist = [] >>> example(mylist) ['value'] >>> example(mylis...
(more)
8 months ago
Question Handling common wrong approaches and misguided motivations for basic technique questions
Background This is inspired to some extent by https://software.codidact.com/posts/289597 . I'm trying to provide a large amount of content (gradually!) that novices (mainly to Python) will find useful. The goal of the actual content is to demonstrate standard techniques and clear up common misc...
(more)
8 months ago
Answer A: Are questions about (abstract) algorithms and datastructures considered on-topic?
As the author, this is my defense. Design and modeling are on-topic The site topicality documentation explicitly includes "Software design, architecture, or modeling" as on topic. There doesn't seem to have been any objection in the original feedback process. I formulated the question with t...
(more)
8 months ago
Answer A: Optimized representation for sets?
First store the universe of potential elements as a sequence, then encode each set as an unsigned integer interpreted as follows: if the 1s bit in binary is set (1), the set contains the 0th element in the universe sequence; if the 2s bit is set, the set contains the 1st element; if the 4s bit, the 2...
(more)
8 months ago
Question Optimized representation for sets?
I need to do a lot of calculations involving sets. There are relatively few values in the "universe" of candidates that could appear in any of the sets, but potentially very many such sets (they might not initially be distinct, either). My language has a built-in (or standard library) representati...
(more)
8 months ago
Question What categories could we benefit from having?
So far, existing Meta discussion seems to have at least hinted at the possibility of using separate categories here: To shuffle closed questions out of the way (globally for Codidact) (also) For debugging help requests (discussed in passing) (also) To recommend books or other resources To p...
(more)
9 months ago
Answer A: Should asking about book recommendations directly connected to software development be on-topic?
Are books special? When I see a question like this, I naturally transform it into a more general question about resources. Printed books aren't necessarily the best way to learn about programming concepts; web pages may work much better. Some people like video tutorials; I personally have found th...
(more)
9 months ago
Question Should self-answered Q&A use separate answers for different techniques/approaches (even if there's a caveat that applies overall)?
Looking back at my own Q&A How can I build a string from smaller pieces?, the answer is incredibly long. I'm essentially showing five different ways to solve the problem - because they all exist, and well-informed developers should know about all of them. Would it make more sense to split the cont...
(more)
9 months ago
Answer A: How should I organize material about text encoding in Python into questions?
Here is my current thinking on the matter. 1. Regarding questions/facets that are "two sides of the same coin" - encoding vs decoding the data, reading vs. writing files - I think they should be addressed in one breath. 1. Regarding the Python documentation: I think it is better cited, on deman...
(more)
9 months ago
Question How should I organize material about text encoding in Python into questions?
I want to write one or more self-answered Q&As on the topic of text encoding in Python, to serve as canonicals and preempt future lower-quality questions. I can think of the following things that need to be addressed: What is an encoding? What are encoding (the process) and decoding? How do I k...
(more)
9 months ago
Answer A: How can I schedule a later task in Python?
Use `at` to schedule the command, using `subprocess` from Python to invoke `at`. It doesn't even require `shell=True`. For example: ``` import shlex, subprocess subprocess.run( # `at` command to run now shlex.split("at now +10 minutes"), # shell command that `at` will schedule, ...
(more)
9 months ago
Answer A: How can I properly type-hint methods in different files that would lead to circular imports?
Import modules rather than names first to avoid a circular reference in the `import` statements; then use forward declarations, as before, to avoid a circular reference in the type annotations - like so: ``` process.py import helpers class Process: def dosomething(self, helper: "helper...
(more)
9 months ago
Answer A: Why is git merge from rather than to?
> I struggle to think of any use cases for merging from. Why was the merge command designed this way? The model here is that many developers on the same project are using branches to develop features independently; someone has to be in charge, and that is the person responsible for the `master` (r...
(more)
10 months ago
Answer A: Do we want a wiki (or similar) alongside Q&A?
I think the framing of this question (and the prior discussion) is wrong, and I think that conditions have evolved since it was originally asked - in particular, we can now see how articles have turned out for other communities. Rather than try to define "wiki" or consider ways to implement that stra...
(more)
10 months ago
Answer A: How can I output / display multiple values, with controlled spacing, without building a string?
Using the `write` method The `write` method of a file offers a much more limited interface. It only accepts one argument - a string, for a file opened in text mode - and outputs just what it's given. Any additional desired spacing must be specified explicitly. Sometimes, this makes it practical...
(more)
10 months ago
Question How can I output / display multiple values, with controlled spacing, without building a string?
I know that I can display a single string in the terminal window like `print("example")` (or similarly with a string in a variable), and I know how to open text files and write to them. I also know that I can create a "formatted" string, that contains multiple pieces of information, in a variety o...
(more)
10 months ago
Question On self-answered questions, is it inappropriate to mark my own answer "Works for me" immediately?
Would it discourage others from posting answers, if they saw that a question had an answer with a "works for me" indication applied immediately? (More so than just seeing an immediate, comprehensive answer?) Could that ever be desirable? Or would it cause hurt feelings etc.?
(more)
10 months ago
Answer A: How can I build a string from smaller pieces?
Before attempting this, make sure it makes sense in context. In a few particular situations, it would be better to take a different approach rather than using the normal tools for composing or formatting a string. If the string is for an SQL query, use the SQL library's built-in functional...
(more)
10 months ago
Question How can I build a string from smaller pieces?
Suppose I have some variables like: ```python >>> count = 8 >>> status = 'off' ``` I want to combine them with some hard-coded text, to get a single string like `'I have 8 cans of Spam®; baked beans are off'`. Simply writing the values in sequence only works for literal strings: ```python...
(more)
10 months ago
Question What are statements and expressions?
When I have tried to read technical explanations of the syntax rules for programming languages, and when I am trying to decipher error messages, I often encounter the terms expression and statement. It comes across that these two are related to each other somehow. I understand that these terms hav...
(more)
10 months ago
Answer A: How to compress columns of dataframe by function
Conceptually, applying a function along an axis of a `DataFrame` (i.e., applying it to each row or column) inherently produces a `Series`: a two-dimensional result is collapsed to a one-dimensional result, because one-dimensional "lines" of data are fed into a function that produces a scalar value. ...
(more)
10 months ago
Answer A: Questions easily answered by studying a beginner-level book
There's still time > This scenario is not yet a problem for this site, but we will get there, since it's a huge problem for Stack Overflow This topic should be separately addressed, too. At time of writing, the main category here has 673 posts (perhaps a few more deleted ones, because I coul...
(more)
10 months ago
Answer A: Automatically install all packages needed
A compromise exists between automatically inferring package names (unreliable and potentially dangerous) and writing out an explicit separate `requirements.txt` file: script-running tools such as `pip-run` may offer the ability to parse requirements declared explicitly within the source file itself, ...
(more)
10 months ago
Answer A: How can we grow this community?
Never Too Late Due to, shall we say, recent AI-related hallucinations, pretty much everything that was possible PR-wise in 2019 is possible for this site again. People are leaving Stack Overflow and this is arguably the best existing alternative. Opportunity has knocked again, but in a sense it re...
(more)
10 months ago