Wednesday, March 29, 2017

There goes another one, as the Earth goes out to applause

Trump signs order dismantling Obama-era climate policies

Goodbye Earth. It was nice knowing you.

And you should know that the end started with people clapping.

Using PyInstaller

The other day, I wrote about using Cython to create an executable file. Well, as expected, it does create a smaller executable file, but it depends on the user having the required DLLs already installed on the system.

Which may not be an option for end-users who are not that computer savvy.

In my search for a good way to package software for distribution to end-users, I came across PyInstaller. It is actually one of the easiest ways to package Python code for distribution.

Simply install PyInstaller using pip.
pip install pyinstaller

Then, use PyInstaller to package the Python script into a Windows executable file. I prefer to make this a single standalone exe file for easy distribution, which is easily done with the -F option. And you can even embed an icon for your exe file, using the -i option.
pyinstaller -F -i my_icon.ico my_script.py

This creates my_script.exe which contains the required DLLs together with your Python code, and end-users can easily run the file by typing:
my_script

Oh, and if it is a non-console program, just add the -w option to get rid of the console window.
pyinstaller -F -w -i my_icon.ico my_script.py

To make use of the embedded icon, add
        if sys.platform == 'win32':
            exeName = sys.executable
            icon = wx.Icon(exeName, wx.BITMAP_TYPE_ICO)
            self.SetIcon(icon)

to the __init__() method of your wx.Frame class.

Easy, single line way to package Python code for distribution to end-users. But of course, the exe file can be quite big since it contains the DLLs (including python.dll to run your Python code). And it doesn't protect your code, people can still reverse the process to get to your code. Still, it is a simple and fast way to get code out to people who just want to use them.

Tuesday, March 28, 2017

Trump's legacy?

Just penning it down, in case it really happens. Haha.

I think eventually, Trump's legacy will be: put up many many bills, but none of what he promised will go through, and so he is unable to keep any of his election promises. The U.S. just continues to run as it has, based on existing policies (that's what the bureaucracy is for, to keep things moving). And at the end of four years, with no promise kept, he will just blame everyone else for not passing the bills he put up. It is not his fault, but the people who refused to work with him to make the changes that he is proposing.

He won't question why the people won't cooperate with him, he won't doubt his proposals, because those proposals must be great (after all, he signed them). So it has to be everyone else's fault for refusing to accept these great proposals.

Oh well, I guess the U.S. can survive the next four years without collapsing. It won't do well, but it probably won't collapse.

Meanwhile, others will be using those four years to get ahead...

Monday, March 27, 2017

My trinity of code optimization

In conservation, there is the trinity "reduce, reuse, recycle".

In code optimization, I also have a trinity: code, data, results. Basically, it is about balancing the reuse of these three.

Reuse code. Your code should be as short as possible, and that means reusing as much as you can. But this should not be at the expense of code readability, unless you really really need that small pinch of extra speed.

Reuse data. Once data has been loaded, reuse it as much as you can so that you maximize the I/O cost of loading that data in the first place. Of course, this must be balanced with the amount of data you keep in memory, with the actual amount of memory that you have. On most modern computers, for most day-to-day tasks, this should not be a problem, but for complex computing problems with large datasets, this may not be as easy as it seems.

Reuse results. There is a computing (and maybe I/O) cost to calculating results. So when possible, results should be kept in memory so that they can be reused when necessary, instead of recomputing them. Again, this needs to be balanced with the amount of memory you have.

So there you have it. My trinity of code optimization, which is basically a balance between reusing code, data, and results. But though it may sound easy, achieving a good balance is actually not an easy thing to do.

The art of negotiating

Trump thinks he is good at negotiating. After all, he thinks he is a successful businessman, and has negotiated many successful deals in the past.

But business deals are not the same as political ones.

In business deals, the main factor is usually about maximising the profits for all stakeholders. And the stakeholders are usually two companies, or in some cases, a handful of them.

But in political deals, the situation is usually a lot more varied. Money is no longer the sole consideration; there are now a thousand and one stakeholders, all having different political aims, ideals, non-monetary interests, and their own opinions on how things should be done. Throw in diplomacy and you have to consider history, cultures, traditions, economic backgrounds, and other national interests.

The things you need to consider for the most difficult business deal pale in comparison to the simplest of political ones.

So while a person may be a successful businessman with many past successes in negotiating business deals, the political arena is a different monster altogether.

The key is in how to apply one's past experience in the business world into the political one. And that really depends on how fast a person can learn and adapt. To learn about his new environment, and adapt his past experiences to that new environment. And it takes a humble person to do this, to be willing to admit that he or she needs to go back to the basics and learn from scratch this new environment.

And being humble is not easy, especially for people who have been successful to date.

So to those who wish to succeed: be humble.

Saturday, March 25, 2017

Compiling Cython output into Windows executable

 I have been playing around with wxPython and trying to work on simple applications. The problem is how to distribute these programs easily to other people. On Windows, that means creating executable exe files. After searching through StackOverflow, and trying out various things on my own, I finally found a few ways. I will talk about one of them, which produces the smallest executable file so far. By the way, PyInstaller is easy to use, but it will generate a much bigger file.

1. First, convert the icon file that the application will be using into Python code using the script img2py.
img2py -i my_icon.ico my_icon.py


2. Copy and paste the contents of my_icon.py into my main wxPython script, which I shall call my_script.py here. Then, add the following to the __init__() method of the script's main wx.Frame() class. By the way, I got this method of doing things from Michael Driscoll's post here.
ico = my_icon.GetIcon()
self.SetIcon(ico)


3. Convert the Python script into C using cython.
cython my_script.py --embed

4. Use cl.exe from Microsoft Visual Studio to compile the C file into an exe executable.
cl.exe /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\Python36\include -Ic:\Python36\PC /Tcmy_script.c /link /OUT:"test.exe" /SUBSYSTEM:WINDOWS /entry:wmainCRTStartup /MACHINE:X64 /LIBPATH:c:\Python36\libs /LIBPATH:c:\Python36\PCbuild

By the way, the options
/SUBSYSTEM:WINDOWS /entry:wmainCRTStartup
are needed to suppress the console window from showing up. The generated C code may use main() instead, which means you may need to change the options to
/SUBSYSTEM:WINDOWS /entry:mainCRTStartup 
to link it properly.

This should give you a test.exe which you can run.

If your script is a console script, you can just skip steps 1 and 2, and remove the /SUBSYSTEM and /entry options.

The problem with this method is that you will likely need to distribute required DLLs with the executable file.

Meanwhile, I will continue to test out various ways to generate an executable that is fully standalone and yet small in size.

Tuesday, March 14, 2017

My love for programming

I really love programming.

Once I start, I get very engrossed in it. I think it is because to me, writing a program is like solving a problem. And when I have a problem, I try to solve it as best as I can. So when faced with a programming problem, I try to come up with the solution, working on it, troubleshooting bugs, trying to improve the solution (aka program), and before I know it, hours have gone by.

So it will really be a dream come true if I can make programming a career. I mean, it means I will be working at solving problems all the time, in a way that is interesting to me. Because I like to try out different ways to improve my programs.

Any one needs a piece of software developed?

Tuesday, March 07, 2017

People don't like to admit their mistakes

Con Man Don: what behavioral science teaches us

Quite an interesting read on how the campaign was a big scam, and how it continues on. And I guess with all that accusing going on, it is probably true. Although Trump probably never trained as a con-man, or intended to be one, how he ran his campaign, and his presidency, is a good example of the kind of person he is.

And the worst thing is, people don't like to admit their mistakes. Most people don't have the courage to face up to themselves, to admit that they were wrong. Instead, they will defend their decisions (no matter how wrong, or nonsensical) even if they know that they had made a wrong choice.

So now we are stuck with this guy for the next four years. Four years of watching The Apprentice, except that this time, it is on every channel, and there is no way to switch to watching something else if you don't like the show.

Thoughts on "Congress will investigate Trump's wiretap claims"

Congress will investigate Trump's wiretap claims

I do hope the Congress investigation digs up all the skeletons in the closet.

And the American people should realize that every time Congress (or some other public body) goes off investigating any claims made by this person, public funds are being spent. And if it turns out to be a witch hunt, then public funds would have been wasted.