Order is a very relative notion, it is specific for each of us. “The world is my representation” as Arthur “Chop” Schopenhauer says. At the same time, experience teaches us “Every cloud has a silver lining” so modestly I discovered that a relative “disorder” is often more efficient than an absolute “order”. Right? Ooh slow down a bit! It is not an easy-peasy Q/A!
For sure, you can ask yourself why such interrogations are part of a so called post dedicated to Python. Well, first of all, it make sense for me for several reasons:
Never bad to introduce some philosophy in programming practice. Personally, it reconciles two of my favorite hobbies (bad programming & cheap philosophy) to one : headache! More serioulsy, these thoughts come from a practical and real-world experience.
Let me tell you the story: after a long, long aging process, as a PO, I achieved to build a robust testing strategy with CodeceptJS for a backoffice! But then I discovered that the e2e sequential execution of the suite created a bias (let’s called them errors). OMG! Then I was wondering how to introduce randomization inside the testing suite execution in order to reduce this bias to the maximum. The purpose is to avoid the false positive nightmare!
Meanwhile, in a completely chaotic, confused, disordered, disorganized, topsy-turvy, helter-skelter, pell-mell, upside-down, higgledy-piggledy, hugger-mugger, harum-scarum, snafu, slipshod, unplanned, erratic, strayed, hit-and-miss, incidental, spontaneous, uncoordinated, willy-nilly, devil-may-care, unpremeditated, reckless, cockeyed, hit-and-miss, fluky, incidental… anarchic way, I am learning advanced Python techniques (NLP, facial recognition …) but also fundamentals like the difference between a “tuple” and a “dictionary”. So it was logic that Python popped up in my mind to bring this randomness, given the complexity of doing the same thing in Bash, a language that I am far from mastering!
Incidentally, this experiment proves me that Python is:
- Easy to read: Python is easy to read and most of the language makes sense at a glimpse. This makes finding issues a lot easier than more complicated languages.
- Portability: Python runs on many platforms and systems, meaning your programs can reach a wider audience. It can easily replace Bash or Ruby of instance.
Two qualities that gave Python a serious advantage in what I am looking for: to create on-demand order or disorder 🙂
- Order: when it comes to browsing, summarizing and indexing hundreds of texts in order to store them in a database itself MySQL and/or NoSQL.
- Disorder: when it comes to run tests in parallel, for example, and thereby overcome a bias linked to a sequential execution.
Oddly, sometimes, Disorder proves to be more efficient and effective than Order.
So, this post presents my quick researches on how-to handle random in Python. Having in mind that the final objective is to load randomly testing files that achieve a complete Backoffice’s assessment without increasing errors into the process.
By the way, it reveals a third and fourth Python qualities:
- Increasing productivity: how much work you can accomplish in a given time with few lines of code.
- Relieving Boring Stuff in Bullshit Jobs: Thanks to Al Sweigart and David Graeber to have enabled me to create this “tuna-mayo-sandwich” concept.
So, now, let’s move on and show some code…
The code for this POC
A mix of files that show some concepts explained above. Most of them should end to the bin but sometimes I need to refresh my own memory so I made up a post with it.
# Get to the correct directory cd /Users/brunoflaven/Documents/02_copy/_random_is_all_about/ # Shuffling array in python shuffle_array.py # Working with Tuple depot_command_python_1.py # Introducing shuffle in array depot_command_python_3.py # Output python version of the system depot_command_python_4.py # Working with Dictionaries depot_command_python_5.py # Using bash to launch python execute_python_from_bash_1.sh execute_python_from_bash_2.sh # Model for Basic Automation with Python extracted from Book Redefining a testing automation strategy for a P.O. with CodeceptJS & WordPress (The continuous learning trilogy Book 3) (English Edition) launch_scripts_with_python_3.py # Attempts in Randomization random_array_1.sh random_array_2.py random_array_2.sh random_array_3.py random_array_4.py random_array_5.py # Encoding issue utf8_shebang.py # A directory with a demonstration applied for the E2E testing suite, but not the suite itself test_platform
Which Python ?
Most of the time I am using python from Anaconda but from now on, I started to use:
- Python distributed (python2) with Mac
- but also another Python (python3) installed with the help of Homebrew
The fact that Python replace my poor Bash notions and enable me to scale up at the same time my abilities to do things and speed up my Python’s learning process. For instance, the python’s ability to randomize or shuffle elements is very easy to catch and then code. So, my idea was to launch a bash (.bat, yes I am sometime on Windows) that launch a Python script (.py) that will launch a random shuffle of CPJS scripts e2e suite (a bunch of .js files). Russian doll game for PO.
There are plenty of resources about this Python version issue. I always forget to note down precisely what was helpful and what was not. Anyway, when you’ll be handling to Python versions on your computer, you may find useful these commands.
# Some commands to determine the Python's version python --version # Output: Python 2.7.10 which python # /usr/bin/python python3 --version # Output: Python 3.7.7 which python3 # /usr/local/bin/python3 # So, you can decide to execute your Python script whether with the version 2 or 3 of Python. python3 myscript_in_python.py python myscript_in_python.py
The other thing is that I am not always writting in english so I have start to use only Python 3 as Python 3 come by default in UTF-8 and declare also the encoding. Period.
#!/usr/bin/python # -*- coding: utf-8 -*- #
- The right and wrong way to set Python 3 as default on a Mac
- Python Shuffle List: A Step-By-Step Guide
- Python | Ways to shuffle a list
- Python random.shuffle() function to shuffle list