GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pythonadventures.wordpress.com — Python Adventures Python AdventuresWelcome to the Jungle!
Get the value of PI without a constantProblem You are using a new programming language and you need the value of PI. However, there’s no such constant in the language. What do you do? Solution You can copy the value of PI from somewhere and create your own constant. Or, you can use a math function for that.
I must have learnt it in high school, but it was long forgotten. It was nice to re-discover it :) Fortran At the end of December 2025, I started to learn Fortran (just for fun). Actually, Modern Fortran is pretty good! I like it. However, the constant PI is missing. So I came up with this:
In a graph, find the largest complete subgraphProblem In a graph, you want to find the largest complete subgraph. A complete subgraph is a graph in which every node is connected to every other node. Real-life example: in a party it’s a group of friends where everybody knows each other. Solution It’s a well-known graph problem that can be solved with the Bron-Kerbosch algorithm. You can find more info here, where you can also find a Python implementation.
Categories: algorithms
Tags: Bron-Kerbosch, Bron-Kerbosch algorithm, complete subgraph, graph, largest complete subgraph, subgraph
How to solve a two-variable equation with SympyProblem You have a two-variable equation and you want to solve it with Python / Sympy. Solution Let’s see this example:
What is the value of ‘x’ and ‘y’? We could solve it manually too, of course, but let’s say we have hundreds of these. So let’s solve it with Python using the Sympy library. >>> import sympy as sp
>>>
>>> x = sp.symbols('x')
>>> y = sp.symbols('y')
>>>
>>> eq1 = sp.Eq(7 * x + 9 * y, 8)
>>> eq2 = sp.Eq(9 * x - 8 * y, 69)
>>>
>>> system = [eq1, eq2]
>>>
>>> sol_set = sp.linsolve(system, x, y)
>>> sol_set
{(5, -3)}
>>>
>>> 7 * 5 + 9 * (-3)
8
>>> 9 * 5 - 8 * (-3)
69
Autoformat your Python code with RuffProblem You use VS Code. When you save a Python file, you want the source code to be autoformatted. Solution Use Ruff for that. Follow these steps:
"[python]": {
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.formatOnType": true
},
3. Optionally, you can create a config file for Ruff. Under Linux, its location is # on Linux: ~/.config/ruff/ruff.toml # on Windows: C:\Users\Alice\AppData\Roaming\ruff\ruff.toml line-length = 100 [lint] # rule IDs: https://docs.astral.sh/ruff/rules/ # ignore = ["E203","F401","F841","E712","E722"] ignore = ["E722"] Now, if you save a Python file in VS Code, you’ll get the following goodies:
Links
Play a sound file from memoryProblem I wanted to play short sound files a lot of times. Calling an external program each time to play a file would have been too slow. How to read sound files in the memory and play them from there? Solution First I tried the package And that’s how I found import soundfile as sf
import sounddevice as sd
samples, samplerate = sf.read('file.wav')
sd.play(samples, samplerate)
sd.wait()
To use them, you also need to install More info here: https://stackoverflow.com/a/42387773/232485 Update: I had some .wav files that were not played correctly with Links
Handle extended file attributesProblem You want to handle extended file attributes from Python. Solution If you are not familiar with extended file attributes, you can read more about it here. I also have a blog post (see here), in which I show how to set/remove extended file attributes using the To handle the extended file attributes from Python, you can use the Its usage is very simple: >>> import xattr
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
>>> xattr.getxattr("main.py", "user.com.dropbox.attrs")
b'\n\x12...'
>>> xattr.setxattr("main.py", "user.com.dropbox.ignored", "1")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs', b'user.com.dropbox.ignored']
>>> xattr.getxattr("main.py", "user.com.dropbox.ignored")
b'1'
>>> xattr.removexattr("main.py", "user.com.dropbox.ignored")
>>> xattr.listxattr("main.py")
[b'user.com.dropbox.attrs']
A typical use case: you want Dropbox to ignore your Links
keysoundProblem I have a non-mechanical keyboard. However, when I press keys, I also want to hear some insane clickytty clicks. What to do? Solution If you don’t want to buy a new keyboard, you can try my keysound project. When you press a key, a short sound file is played. At the bottom of the README of this project’s GitHub page, I also collected some related work, thus there are several alternatives available.
Categories: python
Tags: click, keyboard, mechanical, sound
Sum the digits of a number until you get just one digitProblem Take a positive integer and sum its digits. Repeat this process until you get just one digit. Example: 1472 🠖 1+4+7+2 = 14 🠖 1+4 = 5. The answer is 5. Solution # Python code n = 1472 result = ((n-1) % 9) + 1 Credits Thanks to my friend Mocsa who told me this math trick. Merge Overlapping IntervalsProblem You have some overlapping closed intervals and you want to merge them. What is the algorithm for this? Solution Here are some examples with input and output:
And the algorithm:
Links
Categories: python
Tags: algorithms, intervals, merge
Compile Python module to C extensionI found an interesting blog post: You Should Compile Your Python And Here’s Why. In short: if you have a type-annotated Python code (mypy), then with the command Here you can find a concrete example: https://github.com/jabbalaci/SpeedTests/tree/master/python3 Steps:
#!/usr/bin/env bash python3 -c "import main; main.main()" If you do a lot of computation in a function, then with this trick you can make it 4-5 times faster. I don’t think I would use it very often, but it’s good to know that this thing exists. And all you have to do is to add type hints to your code. Links Blog Stats
Random PostRecent Postsascii
bash
beautifulsoup
book
bpython
challenge
command-line
convert
cron
crontab
date
datetime
distance
dropbox
email
eric
exercise
extract
file
firefox
github
google
hacking
heroku
html
ide
imdb
imgur
install
ipython
java
jinja2
json
linux
module
modules
MongoDB
movies
news
pdf
perl
pi
pip
print
pylint
pypi
python3
python shell
quote
readline
reddit
redirect
refactoring
regexp
requests
reverse
review
shell
sound
stdout
string
tab
text
threads
time
tip
tutorial
unicode
url
video
virtualenv
virtual environment
virtualenvwrapper
xml
zip Archives
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |