Archive for category Programming
Intro to Python Multithreading
Posted by delightlylinux in Programming on February 23, 2024
📅 February 23, 2024
Multithreading allows your Python script to perform more than one thing at the same time. This is called concurrency, and it can be used to help make your programs more efficient and speed up their apparent execution…somewhat.
Python provides the threading module, so you can run multiple functions simultaneously. It is pretty cool stuff, but the world of concurrency opens an entirely new set of issues to deal with.
Let’s write a simple Python script that runs the same function multiple times — simultaneously — to show how to perform multithreading in Python.
Python3 – Generate Random Items with Random Enchantments for Skyrim
Posted by delightlylinux in Programming on February 25, 2022
📅 February 25, 2022
“How about starting Skyrim with a random item possessing random enchantments?”
A fun twist to playing Skyrim is to begin the game with a random item entered from the console command. This starting item can even contain an enchantment or two for a little extra punch in our pseudo-Nordic fantasy world.
This adds an extra element of surprise and can help shape the hero’s direction in his fantasy life regarding the path to take. After all, if you have a ring of waterbreathing, would you not want to spend more time exploring rivers and lakes?
So, rather than look up codes to enter, let’s create a script that will generate these console codes for us.
This is not a mod. This is a simple — very simple — Python3 script that picks an item (ring or amulet) at random from a dictionary and assigns two random enchantments to it. The script outputs a console command that can then be entered manually during gameplay from the Skyrim console to add the item to the player’s inventory.
This is mainly practice in picking and assembling random dictionary elements, but rather than showing another standard Python example, why not show how this can be applied to a real-world problem in a fun way?
Python: Generate a Ridiculously Complex Password
Posted by delightlylinux in Programming on September 7, 2020
📅 September 7, 2020
What? You say you are running out of password ideas? Let’s fix that!
Here is a Python 3 script that generates a (supposedly) very secure password certain to thwart brute-force password cracking attempts. In fact, it is so complex that the primary challenge is remembering it.
But no worries! The script will save it to a text file where it can be easily copied and pasted into a password box…assuming it is not used for initial Linux GUI logins where copy and paste would not work.
Ready to explore some interesting Python 3 scrambling?
Python: Passphrase Hashing for Increased Security
Posted by delightlylinux in Programming on July 21, 2020
📅 July 21, 2020
Concerned that your generated passphrase cool.bait.build might be guessed by brute force password cracking means? Then, hash it!
Passphrases based upon parts of speech (rabbits.burn.oily.paper) are easier to remember than something like %5gN&31+=?, but this might cause concern for those alarmed by password crackers attempting to try every word combination word in the dictionary.
Well, here is an added approach for you!
Just hash the passphrase using your favorite hash algorithm, and then use the hash as the passphrase. This way, you do not need to memorize the hash, only the passphrase that generates it.
Python: Create Passphrases Based on Parts of Speech
Posted by delightlylinux in Programming on July 15, 2020
📅 July 15, 2020
What? You can’t remember e7$t=ehQr8+ as your password?
The repeated password advice given over the years tends to encourage lazy password choices that end up being tricky for people to remember but easy for computers to crack.
As an example, the password above might be considered secure, but it is not convenient. As a result, people will shy away from it and pick something, like Pa$$w0rd123, that they think is secure.
Techniques such as Diceware attempt to resolve this situation and do a good job of creating passphrases, but the result might be a random selection of words that make little sense and be tricky to remember.
What if we tried creating passphrases that resemble English sentences by assembling words based upon parts of speech?
Here is a Python script that attempts to make resulting passphrases easier to remember by combining nouns, verbs, and adjectives in a more memorable way. We even use a pattern system to give us flexibility in the formation of phrases.
Python: Generate Random Sequence from Weighted Values
Posted by delightlylinux in Programming on July 14, 2020
📅 July 14, 2020
It’s a battle between two factions. The enemy attacks! Whew! That was close! You barely survived the onslaught of enemy attacks against your fortress in the post-apocalyptic fantasy world of What-Cha-Ma-Call-It. You are mankind’s last hope. Will you survive another round?
After replenishing your supplies, the enemy attacks again! However, it uses the same attack pattern, making this a predictable battle. You win, but it was rather lackluster.
The enemy attacks a third time using the same strategy. Yawn. This is getting boring. You barely needed to think in order to survive. Is there any way to make the enemy choose more random events to seem more intelligent and unpredictable?
Python 3 provides a handy function, called choices(), that selects items from a list at random based upon weighted values. We can use this function to write a Python script for our game to increases the enemy strategy and fun.
Python 3: Process Command Line Arguments with argparse
Posted by delightlylinux in Programming on May 13, 2020
📅 May 13, 2020
Python 3 provides a handy module, argparse, that makes it easy to get and parse command line arguments passed to your script.
If you ever found argument parsing to be a tedious extra, then argparse might be just what you were looking for to simplify the process and focus on your code.
JavaScript: Get a random number within a range
Posted by delightlylinux in Programming on April 26, 2017
📅 April 26, 2017
The Math object in JavaScript provides the random() function that returns a floating point value from 0 to up to but not including 1.
What if we want to grab an integer, say, from 1 to 25 inclusive?
We can do this easily by modifying the random result to our liking.
Tidbit: Discovering Xubuntu Distribution from PHP
Posted by delightlylinux in linux, Programming on September 24, 2016
📅 September 24, 2016
In PHP, we can use php_uname to grab information about the operating system that the server is running on.
echo php_uname(‘s’) will give the name of the OS, but this is a general name. When executed from a server running Xubuntu, it returns the string “Linux.” Is this Ubuntu, Kubuntu, Xubuntu, Linux Mint, or…what?
What if we want to get the specific Linux distribution? Is this possible from PHP without performing host OS system calls or executing Bash scripts? Yes.
php: Get Random Image
Posted by delightlylinux in Programming on February 4, 2016
📅 February 4, 2016
Using a similar technique as the random text PHP code, here is a simple code snippet that chooses an image file at random and displays it in a browser.
This is useful for randomizing a banner image on a web page. Each time the browser refreshes, a different banner image (should) appear. While the same effect can be performed using JavaScript, what if JavaScript is disabled in the browser?
By moving the code to the server side, a random banner image (or any kind of image) will appear in order to add freshness to a page.