Friday, 20 November 2015

Python importing compiled .pyc files in to the __main__ namespace

I had written a rather long piece of code in Python, which was all in one file, and all using the same namespace. I later needed to split the file and was hoping to find a method that worked by allowing me to "include" the file in a list, just like the code was in one large file.

Using import gave each module its own namespace, and so they weren't able to access the other imported modules without first importing the module again. Similarly, using any combination of from [name] import * had the same problem.

What ended up being the solution was the imp library.

The code that I have used is as follows:

import imp
imp.load_module(“__main__”, filename)

By passing "__main__", this loads the module into the workspace __main__, which is the namespace my code was using. I only used this to load .pyc files. The filename doesn't have to be a full path, and can just be the file name if located in the same folder as the script, assuming you haven't changed directories.

In hindsight, the problems were mostly from poor code structure, and each module should run as a standalone module, but due to time constraints I didn't have time to fix these issues.

Friday, 13 November 2015

Setting default search engine in Chrome to stop geographical redirection

I was somewhat frustrated when using Google Chrome when abroad, as it would always redirect to the local version of Google. The change in interface isn't the main problem, the problem is that localised versions of Google searches give localised results, which is more of a problem when you don't speak the language. There are options to change your Google account display settings, but this don't seem to always stick, and if you aren't logged in, or have multiple accounts then this setting doesn't help. Also there is the tip to use the "No Country Redirect" option, by accessing your prefered version of Google, followed by "/ncr", e.g. http://www.google.com/ncr however this only works if you set it as a bookmark or homepage, and not from the Omnibar.

Finally, I found a way to set the Omnibar to use my local Google. Go to chrome://settings/ and scroll down to the "Search" section. There, the deafault entry for Google will be as follows:

{google:baseURL}search?q=%s&{google:RLZ}... (clipped)

Here, changing the first bit of the string to the local Google isn't quite enough. I originally changed it to the following:

https://google.co.uk/search?q=%s&{google:RLZ}... (clipped)

However when closing the settings, it would revert back to {google:baseURL} which used to revert back to the local Google. By changing it to "https", this seems to have stopped it happening for now. I'm not sure why this is even a "feature", but seems to work for the moment at least. So the full search URL is as follows:

https://google.co.uk/search?q=%s&{google:RLZ}{google:originalQueryForSuggestion}{google:assistedQueryStats}{google:searchFieldtrialParameter}{google:bookmarkBarPinned}{google:searchClient}{google:sourceId}{google:instantExtendedEnabledParameter}{google:omniboxStartMarginParameter}ie={inputEncoding}

The following is a screenshot of how it looks now:



It also works to cut out all of the extra stuff, and so just make the string as follows:

https://google.co.uk/search?q=%s

If this was useful to you, I also have another post on how to correct the language reported by chrome to websites, which you can view here.

Matlab definition of functions, and function definition errors

I was trying to create a new function in Matlab, and had some issues as I am relatively unfamiliar with the format.

When writing a function, it must go in a .m file, and only the function can go in the .m file, no other code.  The typical syntax for a function

function output_variable=function_name(input_variable)

output_variable = 2* input_variable

end
The above function then should go in a file named function_name.m. Furthermore, to use the function, the code does not need to be executed first. The "current folder" must contain the .m script, and then just use the function name with variables if needed. Comments are also allowed.

If the function contains any code outside of the function, the following error will be seen:

This statement is not inside any function.
 (It follows the END that terminates the definition
 of the function "create_const_mat".)

In this case, remove any code before the first line defining the function name, or after the end line.

If the function is saved in a .m file with a different name from the function, the following error will be seen:

Error: Function definitions are not permitted in this
context.

To solve this error, rename the .m file to match the function name, or conversely rename the function name to match the .m file name.

If executing the code in the function, then there are other possible errors, which will depend on the variables in the current workspace, and whether they match the variables needed by the function. If, your function requires varaibles which are not defined, then expect the following error:

Not enough input arguments.

If they are defined incorrectly, then anything could happen.