Answer by David Z for How to efficiently calculate the fraction (valid UTF8...
Honestly I couldn't quite follow this question all the way through, but here's the idea I think you want.Judging by your last program, you figured out, more or less, that you can express the number of...
View ArticleComment by David Z on How to grow a list to fit a given capacity in Python
Ah I must have made a mistake, I'll fix it.
View ArticleAnswer by David Z for How to execute shell script from LaTeX?
I would do something like the following (partially motivated by what Roman suggested): make your LaTeX file be\documentclass{article}\begin{document}\input{scriptoutput.tex}\end{document}and generate...
View ArticleComment by David Z on distutils: How to pass a user defined parameter to...
Since distutils was removed from Python 3.12, that link is dead, but the Python 3.11 version of the page is still up, and it also exists (for now) in the setuptools documentation. The sdist source code...
View ArticleComment by David Z on Using the open() system call
Thanks! I must have missed these comments, my bad. (although it's a little surprising it took this long for someone to fix a simple typo)
View ArticleComment by David Z on jq pass argument as a key to new field
@somenxavier In general I would suggest asking followup questions as their own separate posts, not as comments. But since this one is quick, I'll tell you that it's because $md is part of a string, and...
View ArticleAnswer by David Z for How can I put an actual backslash in a string literal...
To answer your question directly, put r in front of the string.final= path + r'\xulrunner.exe '+ path + r'\application.ini'More on Python's site hereBoth string and bytes literals may optionally be...
View ArticleAnswer by David Z for mod_python publisher and pretty URLs
You would have to have an object bar.a1.a2.a3.an defined within your foo.py module. Basically, the publisher handler replaces the slashes in the URL with dots, and tries to find some Python object with...
View ArticleComment by David Z on Finding process count in Linux via command line
@krzemian Sorry I didn't see this at the time! In case it's still relevant: the difference is probably coming from the wc process, which is listed in the output of ps when you have them running in...
View ArticleComment by David Z on Unable to use pipenv to install: ImportError: cannot...
This just runs a different version of pipenv (and/or uses a different version of Python). It seems that whatever version of pipenv you get when running python3 -m pipenv has a fix for this error,...
View ArticleHow can I get a human-readable timezone name in Python?
In a Python project I'm working on, I'd like to be able to get a "human-readable" timezone name of the form America/New_York, corresponding to the system local timezone, to display to the user. Every...
View ArticleSelecting distinct column values in SQLAlchemy/Elixir
In a little script I'm writing using SQLAlchemy and Elixir, I need to get all the distinct values for a particular column. In ordinary SQL it'd be a simple matter ofSELECT DISTINCT `column` FROM...
View ArticleAnswer by David Z for In Python script, how do I set PYTHONPATH?
You don't set PYTHONPATH, you add entries to sys.path. It's a list of directories that should be searched for Python packages, so you can just append your directories to that...
View ArticleAnswer by David Z for How to calculate the cosine similarity betwen list to...
You need to change a couple things: first, when using dot(), the last dimension of the first array needs to match the second-to-last dimension of the second array, so in your case what you probably...
View ArticleAnswer by David Z for Pythonic way to combine (interleave, interlace,...
There's a recipe for this in the itertools documentation (note: for Python 3):from itertools import cycle, islicedef roundrobin(*iterables):"roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe...
View ArticleAnswer by David Z for How are lambdas useful?
Are you talking about lambda expressions? Likelambda x: x**2 + 2*x - 5Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass...
View ArticleAnswer by David Z for Why am I getting only 800 hashes per second?
The crypt module, like the crypt() system call that underlies it, is meant for hashing passwords, and good password hashing algorithms are designed to be slow, precisely because they want to make it...
View ArticleAnswer by David Z for Python: No csv.close()?
The reader is really just a parser. When you ask it for a line of data, it delegates the reading action to the underlying file object and just converts the result into a set of fields. The reader...
View ArticleCreating Python modules from an extension with extra C data structures
I'm working on a custom Python loader that creates Python modules from a particular kind of non-Python file, let's call it a "cheese file". I'm writing my project as a C extension module because these...
View ArticleAnswer by David Z for Grouping imports together from subdirectories
Nope. The package that contains a module will not necessarily know anything about the contents of the module, so in your case accounts doesn't know anything about the User class in accounts.user. There...
View Article