Tuesday, May 26, 2020

Tricky Python I : Memory Management for Mutable & Immutable Objects

It’s hard not to fall in love with Python: its clean syntax and close resemblance to English makes coding in Python feel like composing an elegant prose. However, just when you think you are beginning to speak Python, things start to get a little strange:
>>> x = 8
>>> y = x
>>> x = 100
>>> y
8
Wait, what just happened? Shouldn’t y hold the value of 100, since we just assigned y the value of x?
The mystery can be solved by understanding how Python handles memory management for mutable and immutable objects. To do this, we need to first discuss the object-oriented nature of python.

OOP in Python

“Everything is an object”
What is an object? To understand this, we need to first discuss the concept of a class. Similar to a struct in C, a class is a bundle of data of different types and functions logically grouped together. An object is an instance of a particular class. We can think of class as a blueprint, a template based on which an object is created.
Guido van Rossum, who wrote Python, was very deliberate in ensuring all objects in Python were “first class.” In other words, list, string, integer, or function — “anything that could be named in the language” — is an object that belongs to the corresponding class, and should be treated indiscriminately (the discussion on this is beyond the scope of this post). For example, 3 is an integer object belonging to the integer class"I'm a string!" is a string object belonging to the string class, and so on.
As you can see, the names of these classes describe the data type of an object. We can find out what class an object belongs to using the built-in type() function:
>>> L = [1, 2, 3]
>>> type(L)
<class 'list'>
Note that another built-in function isinstance() also checks if an object belongs to a given class by returning a boolean value. The difference is that isinstance() checks subclasses in addition, while type() doesn’t.

Mutable vs. Immutable Objects

“Not all objects are created equal”
There are two kinds of objects in Python: Mutable objects and Immutable objects. The value of a mutable object can be modified in place after it’s creation, while the value of an immutable object cannot be changed.
  • Immutable Objectint, float, long, complex, string tuple, bool
  • Mutable Object: list, dict, set, byte array, user-defined classes

Is it the same object?

We can check the mutability of an object by attempting to modify it and see if it is still the same object. There are two ways to do this:
  • Using the built-in function id(): this function returns the unique identity of an object. In CPython implementation, id() returns the memory address of the object. No two objects have the same identity.
  • Using the is and is not operator: these identity operators evaluates whether or not the objects have the same identity. In other words, if they are the same object.
Let’s see what happens when we apply id() on an immutable object, an integer:
>>> a = 89
>>> id(a)
4434330504
>>> a = 89 + 1
>>> print(a)
90
>>> id(a)
4430689552  # this is different from before!
…and contrasting the result with a mutable object, a list:
>>> L = [1, 2, 3]
>>> id(L)
4430688016
>>> L += [4]
>>> print(L)
[1, 2, 3, 4]
>>> id(L)
4430688016    # this is the same as before! 
We see that when we attempt to modify an immutable object (integer in this case), Python simply gives us a different object instead. On the other hand, we are able to make changes to an mutable object (a list) and have it remain the same object throughout.
It’s important to distinguish the identity function id() and identity operator is from the comparison operator ==, which evaluates whether the values are equal. We’ll demonstrate the difference and use it to illustrate the different behaviors of mutable/immutable objects in the following section on Python Memory Management.

Python Memory Management

In C, when we assign a variable, we first declare it, thereby reserving a space in memory, and then store the value in the memory spot allocated. We can create another variable with the same value by repeating the process, ending up with two spots in memory, each with its own value that is equivalent to the other’s.
Python employs a different approach. Instead of storing values in the memory space reserved by the variable, Python has the variable refer to the value. Similar to pointers in C, variables in Python refer to values (or objects) stored somewhere in memory. In fact, all variable names in Python are said to be references to the values, some of which are front loaded by Python and therefore exist before the name references occur (more on this later). Python keeps an internal counter on how many references an object has. Once the counter goes to zero — meaning that no reference is made to the object — the garbage collector in Python removes the object , thus freeing up the memory.
One may like to take a look at this article written by Sreejith Kesavanwith that provide nice analogy and clear visuals in illustrating the differences in how C and Python approach variable assignments.

Making References to Values

Each time we create a variable that refers to an object, a new object is created.
For example:
>>> L1 = [1, 2, 3]
>>> L2 = [1, 2, 3]
>>> L1 == L2
True             # L1 and L2 have the same value
>>> L1 is L2
False            # L1 and L2 do not refer to the same object!
We can, however, have two variables refer to the same object through a process called “aliasing”: assigning one variable the value of the other variable. In other words, one variable now serves as an alias for the other, since both of them now refer to the same object.
Here is an example:
>>> L1 = [1, 2, 3]
>>> L2 = L1         # L2 now refers to the same object as L1
>>> L1 == L2
True
>>> L1 is L2
True
>>> L1.append(4)
>>> print(L2)   
[1, 2, 3, 4]
Since L1 and L2 both refer to the same object, modifying L1 results in the same change in L2.
The example at the beginning of this article should start to make sense:
>>> x = 8
>>> y = x         # y refers to the same object (number 8) as x
>>> x = 100       # x now refers to a different object (number 100), 
                    since integers are immutable
>>> y             # but y is still referring to the same object
8

Exceptions with Immutable Objects

While it is true that a new object is created each time we have a variable that makes reference to it, there are few notable exceptions:
  1. some strings
  2. Integers between -5 and 256 (inclusive)
  3. empty immutable containers (e.g. tuples)
These exceptions arise as a result of memory optimization in Python implementation. After all, if two variables refer to objects with the same value, why wasting memory creating a new object for the second variable? Why not simply have the second variable refer to the same object in memory ?
Let’s look at some examples, shall we?
  1. String Interning
>>> a = "python is cool!"
>>> b = "python is cool!"
>>> a is b
False
This should not be surprising, since it obeys the “new objects are created each time” rule.
>>> a = "python"
>>> b = "python"
>>> a is b
True   # a and b refer to the same object!
This is a result of string interning, which allows two variables to refer to the same string object. Python automatically does this, although the exact rules remain fuzzy. One can also forcibly intern strings by calling the intern() function. Guillo’s article provides an in-depth look into string interning.
2. Integer Caching
The Python implementation front loads an array of integers between -5 to 256. Hence, variables referring to an integer within the range would be pointing to the same object that already exists in memory:
>>> a = 256
>>> b = 256
>>> a is b
True
This is not the case if the object referred to is outside the range:
>>> a = 257
>>> b = 257
>>> a is b
False
3. Empty Immutable Objects
Let’s take a look at empty tuples, which are immutable:
>>> a = ()
>>> b = ()
>>> a is b
True  # a and b both refer to the same object in memory
However, for non-empty tuples, new objects are created, even though both objects have the same value:
>>> a = (1, )
>>> b = (1, )
>>> a == b
True
>>> a is b
False
One last thing before we finish…

The Tricky Case with Operators

We have seen in the earlier example that given a list L, we can modify it in place using L += [x], which is equivalent to L.append(x). But how about L = L + [x] ?
>>> L = [1, 2, 3]
>>> id(L)
4431388424
>>> L = L + [4]
>>> id(L)
4434330504     # L now refers to a different object from before!
Why does this happen?
The answer lies in the subtle difference behind the operators. The + operator calls the __add__ magic method (these are methods automatically called instead of having to be explicitly invoked), which does not modify either arguments. Hence, the expression L + [4] creates a new object with the value [1, 2, 3, 4], which L on the left hand side now refers to. On the other hand, the += operator calls __iadd__ that modifies the arguments in place.
Fewfff…that was a lot of information. In my next post, we will be discussing some surprising (and not-so-surprising) things about parameter passing in Python as a result of object mutability/immutability. Stay tuned!

Monday, May 25, 2020

Covid ml



https://devpost.com/software/covid-19-growth-modeling-forecasting?utm_campaign=winner_email&utm_content=submission_won&utm_medium=tweet&utm_source=twitter

What's next for COVID-19 Forecasting

We are planning to add a forecast correlation with the number of daily patient tests and the number of patients recovered daily.
Technical article Ihttps://bit.ly/2XHjL4P
Technical article IIhttps://bit.ly/2RMB14V

Wednesday, May 13, 2020

Opencart Useful link


Write permission fix

https://www.opencart.com/index.php?route=marketplace/extension/info&extension_id=31130


Add JSON SUPPORT for PHP
sudo yum install php-pecl-json


Permission issue:

sudo semanage fcontext -a -t httpd_sys_rw_content_t  "/var/www/html/.*?"

sudo semanage fcontext -a -t httpd_sys_rw_content_t  "/var/www/html/load/opencart/htdocs"
sudo restorecon -Rv /var/www/html/

Source:
https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory

If you are running Security-Enhanced Linux, then it might be the reason for the problem, by denying access to the file from the server.
To check whether SELinux is enabled on your system, run the sestatus command in a terminal. If the command does not exist, then SELinux is not on your system. If it does exist, then it should tell you whether it is enforced or not.
To check whether SELinux policies are the reason for the problem, you can try turning it off temporarily. However be CAREFUL, since this will disable protection entirely. Do not do this on your production server.
setenforce 0
If you no longer have the problem with SELinux turned off, then this is the root cause.
To solve it, you will have to configure SELinux accordingly.
The following context types will be necessary :
  • httpd_sys_content_t for files that you want your server to be able to read
  • httpd_sys_rw_content_t for files on which you want read and write access
  • httpd_log_t for log files
  • httpd_cache_t for the cache directory
For example, to assign the httpd_sys_content_t context type to your website root directory, run :
semanage fcontext -a -t httpd_sys_content_t "/path/to/root(/.*)?"
restorecon -Rv /path/to/root
If your file is in a home directory, you will also need to turn on the httpd_enable_homedirs boolean :
setsebool -P httpd_enable_homedirs 1
In any case, there could be a variety of reasons why SELinux would deny access to a file, depending on your policies. So you will need to enquire into that. Here is a tutorial specifically on configuring SELinux for a web server.


Host WordPress for Free on Oracle Cloud

So you’ve just signed up for Oracle’s free tier cloud and you’re wondering what you can do with it. Or maybe you’ve come looking for a free solution to host a WordPress site (version 5.2). Either way this is something the Oracle cloud can do, and something it does well.
If you’d like a TLDR, give-me-my-WordPress-now version; I’ll quickly show you how to use a bash script I wrote to automate the setup process, and this will require minimal Linux understanding. Then I’ll break it all down for those of you curious about how it works.

Where’s My WordPress?!

Let’s dive right in! If you haven’t set up up your virtual machine on the cloud yet, check out the , and take note of .
Once you have remotely accessed your virtual machine, run the following line to download the bash script:
This will download ‘wordpress_setup.sh’ to your home directory. From here you have two options to run it; either use the ‘bash’ command, or make the script executable.
To use the bash command simply run the following, ensuring you are in your home directory:
bash wordpress_setup.sh
Instead making the script executable is a two line process:
chmod +x ./wordpress_setup.sh
./wordpress_setup.sh
Both methods will result in WordPress being set up, so it’s up to you which you’d rather do.
Towards the end of the script you’ll be prompted for a few pieces of information:
  • a new password for the root user on the MySQL database
  • the name for your WordPress database
  • the name for a new user on the WordPress database
  • and a password for the new user
Once you’ve provided all of this, and the script has finished running, the virtual machine will be rebooted. Once it has finished rebooting (which could take about a minute), you can take the public IP address of the machine and navigate to it through a browser. There you’ll find the last few setup steps for your WordPress site; e.g. giving it a name, setting up an admin account, etc.
Image of admin page on WordPress
Bar using a third party service to set up a domain name for your new site, that’s it! Time to put some content into that site! Simple, no?

So What’s the Script Doing?

, rather than dumping the entire thing here. If you’re looking at doing this manually, there are quite a number of lines of it that can be skipped. So I’ll only mention the relevant line numbers.
Starting at line 11 you will see a tuple ‘prerequisites’. These are (in order):
[0] the MySQL repository info location on the web
[1] the My SQL repository file
[2] the PHP for Oracle repository.
With the following commands replace the bracketed numbers with the above prerequisites. To make use of these [0] must be downloaded with the ‘wget [0]’ command, following this install [1] using ‘sudo yum localinstall -y [1]’. Finally use a normal install for [2]; ‘sudo yum install -y [2]’.
GIF of person typing
On line 12 is another tuple; ‘packages’. For each of these use ‘sudo yum install -y [tuple item]’ to install them all. The first item is the Apache web server package. This allows the virtual machine to provide a web page a browser can interpret and display. Also see line 22 which alters the firewall to allow inbound http traffic, i.e. other devices can request web pages from the virtual machine. The last item is the MySQL server that will run our database, and the items between these two are the various PHP packages necessary to host a WordPress site.
At time of writing the following versions of the above are installed: Apache Server version 2.4, MySQL version 8.0, PHP version 7.2. While WordPress version 5.2 is not the most recent version, the PHP version we have access to is a limiting factor. There is also a slight issue with MySQL version 8.0 and onwards that we need to work around.
GIF with 1s and 0s showing silhouette of a lock
In version 8.0 the default authentication encryption was changed, yet WordPress relies on using the older version of this. Thankfully, this is easily fixed by uncommenting one line in one of the MySQL files. This is addressed in line 23 of the script. Open ‘ /etc/my.cnf’ in your preferred text editor. There should be a block of commented text, ending with the line ‘# default-authentication-plugin=mysql_native_password’ (if you’re using nano as an editor you can use ctrl+w to search for this line). Simply uncomment this line and save the file.
Next, looking at lines 17–21 we will download the WordPress files, and place them in the Apache server’s directory. Using the wget on line 17, the files for version 5.2 of WordPress will be downloaded. The downloaded file must be unzipped, using the ‘tar -xzvf …’ command. The unzipped files are then moved to the default Apache directory, and the user ‘apache’ is given ownership of them.
To start the MySQL server, run line 24. You need to do this to continue the setup process. Now run ‘sudo grep ‘temporary password’ /var/log/mysqld.log’, to get the temporary password assigned to the root user. You will need this to make use of the MySQL server.
GIF showing a more secure WordPress
Running the following; ‘sudo mysql_secure_installation’, will greet you with a prompt for the temporary password. Once you provide it you will have to set a new password, followed by answering a series of y/n questions. These all involve removing testing tables/users, and revoking remote querying. Answering ‘y’ to each will provide you with a more secure setup.
Now we need to create a new database and associated user for WordPress. This can be done purely via the command line, or by logging in to mysql. I will be showing the latter here.
$ mysql -u root -p
     [you will be prompted for the password now]
mysql> CREATE DATABASE [DATABASE NAME];
mysql> CREATE USER [USER NAME]@localhost IDENTIFIED BY '[PASSWORD]';
mysql> GRANT ALL PRIVILEGES ON [DATABASE NAME].* TO [USER NAME]@localhost;
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
Above replace [DATABASE NAME], [USER NAME], and [PASSWORD] with your chosen names and password.
We’re almost done now. First rename ‘/var/www/html/wp-config-sample.php’ to ‘/var/www/html/wp-config.php’ using line 59. Next open up ‘/var/www/html/wp-config.php’ in your chosen text editor. We need to give WordPress some information so that it can access the database. We’re going to be doing some finding and replacing now. Find the following three lines:
define( ‘DB_NAME’, ‘database_name_here’ )
define( ‘DB_USER’, ‘username_here’ )
define( ‘DB_PASSWORD’, ‘password_here’ )
Replace ‘database_name_here’ with your choice for [DATABASE NAME] above, ‘username_here’ with [USER NAME], and ‘password_here’ with [PASSWORD]. Though be sure to leave the inverted commas in place.
Finally, using lines 69–72 restart the web server and MySQL server, and enable them to start when the virtual machine boots. Reboot the virtual machine now. Going to the public IP of the virtual machine in a web browser now will display the last few steps to get your WordPress site up and running!

I hope you found this useful, and enjoy filling your new websites with wonderful content!
If you’d like to get in contact, find me over on !

Deduplicating Data on the Databricks Lakehouse: Making joins, BI, and AI queries “safe by default.”

  Imagine this: your manager asks the AI analytics tool: "What were our top-selling products last quarter?" The AI generates perfe...