Category: Uncategorized (page 1 of 2)

Making Python Datetime Timezone Aware For Django

To make a Python Datetime timezone aware for Django Datetime field. You can use the utc from django.utils.timezone for replacing the tzinfo of Python Datetime but make sure that it is in UTC format. Below is a simple example of how to retrieve the current date and time and make it timezone aware for Django Datetime model field.

from django.db import models
from django.utils.timezone import utc
import datetime 
 
class Foo( models.Model ): 
 #datetime field sample 
 date_posted = models.DateTimeField( auto_now_add=True, blank=True ) 
 def save(self, *args, **kwargs): 
 #the datetime generated by datetime module will now be timezone aware 
 self.last_date_updated = datetime.datetime.utcnow().replace(tzinfo=utc) 
 super(Foo, self).save(*args, **kwargs)

Set Default Network Interface In Ubuntu

If you have two network interface in your Ubuntu eth0 and usb0 and the default network interface is eth0 but you want to use usb0 for internet connection. To make this happen you must set usb0 as your default interface so that when you need to access something that isnt located in the eth0 interface local network, usb0 will be used. For example you have a phone and you want its data connection to be tethered to your Ubuntu desktop. Once connected to your desktop it registered as usb0. So now you have both eth0 and usb0 interface registered and you want usb0 to be used for internet connection but still want to be able to connect to a computer that resides in eth0 local network and eth0 network also have an internet connection. By default eth0 will always be used since it it will be more likely the default interface. Continue reading

Debian/Ubuntu Handy apt Commands

When I first got on the Linux boat I only know that apt-get is a command to either install or remove a package from the system and only used the Synaptic Package Manager for searching packages. After a long time using Debian/Ubuntu I got to know some handy apt commands.

Here are the list of apt commands that I know right off the top of my head.

apt-get install [package name]

I think this is the first command that all people learned when they first tried Linux using Debian or Ubuntu. This command will install the package with the package name you entered

apt-get update

This command will update your systems record/cached packages list using the locations in your sources list.

apt-get remove [package name]

If you want to remove a specific package you can use this command.

apt-get purge [package name]

This command is the same as “remove” and will also remove its configuration files.

 apt-cache search [package name]

You can use this command if you dont know the exact package name. This will search the package in your systems cached packages and return the results so running apt-get update is advisable before you use this.

apt-get upgrade

This will update all packages that are installed in your system.

apt-get dist-upgrade

This will upgrade your system distribution to the latest one if its already available so this also perform the upgrade command

Compiling OGRE3D for Android

Update: 2015-02-18 – ndk version

You can now compile OGRE3D using NDK and the OGRE branch version 1.9. Ive followed the instruction in OGRE wiki for compiling for Android but got stuck several times in several areas and almost just give up. Im new to Android specially using the NDK and all of this just overwhelmed me. So this is a step by step guide Ive decided to make on how to compile OGRE for people that are like me, new to Android.

Requirements:

Continue reading

Django 1.5 Upgrade From 1.4 Problem On DEBUG=False

Ive upgraded the Django version Im using to 1.5 from 1.4.5 several days earlier and since then I was unable to run my app in release mode. Whenever I set DEBUG=False in settings.py a “HTTP Error 500” page is always served but setting it to DEBUG=True will make the app run fine.

Ive found out that now on version 1.5, Django has a new setting in settings.py:

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []

This setting is not included in version 1.4 and is now needed in 1.5. You need to add your domain here ex: www.jamesbaltar.com. See this link for more

HTML And CSS: Padding HTML Element Without Resizing

For sometime now Ive been making my elements actual size subtracted by a certain percentage to compensate for the resizing triggered by padding and add a border that is the same color as the element to emulate a margin. This is really an ugly approach since depending on the device I might have over/under compensate the percentage but I found a css code that will make the padding grow inside the element size instead of growing its actual size. Below is the css code:

-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */

As you can see we have 3 different codes for different browser. With this when you use padding your html element will not auto re-size to accommodate the padding.

South Database Migration Tool

Update: South migration tool is now pulled into Django core as of Django 1.7. So if you are starting out on Django 1.7 then you don’t need to install South anymore because it has now it’s own implementation of a database migration tool.

For sometime now Ive been playing with Django. Making small applications that is not worth mentioning. When I first touched Django Ive read somewhere that it would help me greatly if I used South, a database migration tool but I just brushed it off too early at that time since I didnt want to learn something on top of Django. Now that Ive tried it Ive realized that Ive truly made a big mistake of not trying it earlier.

South is a tool for database migration. What it does is help you sync your database tables with your models. Youre really going to appreciate it when you have a live production setup and then you find yourself needing to change some tables, like add columns or change the default values. Imagine if you have made a lot of changes and when you need to put your project into the live setup youre going to write a bunch of SQL scripts to patch the live setup. With South you wont need to create your own SQL scripts to patch your tables.

Like all Django application you will need to add it in your settings.py. It also creates it own tables in your database. For now I dont really care what it adds to my project since it doesnt seem to get in my way. It is really a great app/tool to have specially for me since Im only learning Django and web development so I always ends up modifying my models too often.

You can get South from here. Installation and a tutorial is also in their website to help you get started quickly.

Difference of C++ operator delete and delete[]

The difference between “delete” and “delete []” is the former is used for a pointer only while the latter is used for pointers to an array. Take for example the code below:

int main( int argc, char* argv[] ) {
 //declare a pointer to an array of char named as a
 char *a = new char[64];
 //declare a pointer to a char named as b
 char *b = new char;
 
 //since a is a pointer to an array of char the use of delete[] is a must
 delete [] a;
 //for b which is only a pointer to a char use delete
 delete b;
 
 return 0;
}

if you use “delete” on a pointer to an array it will cause a memory leak, so make sure you use the correct “delete”.

Samba Client in Ubuntu Command Line

Samba Installation

Check if Samba is installed

apt-cache policy samba

You should see something like this if it is installed:

 Samba:
 Installed: 2:3.4.7~dfsg-1ubuntu3.7
 Candidate: 2:3.4.7~dfsg-1ubuntu3.10

otherwise “Installed” on the top has a blank value. To install type in:

sudo apt-get install samba

After successfully installing Samba you can now connect to a shared folder on the network.

Connecting

Suppose you want to connect to “192.168.0.114” shared folder named “Public” . You just need to type:

smbclient //192.168.0.114/Public -user [Your_User_Name]

“-user [Your_User_Name]” argument is optional. If no “-user” indicated you will be defaulted to the default user. After pressing enter with the above command, the terminal is going to ask for your password. After that the terminal should look like this:

smb: \>

You are now successfully log-in in the server computer and ready to write to or copy from it.

Writing Files

To write a file in the server using Samba client use the command:

puts [local name]
  • [local name] is the filename of the file you want to transfer on the current directory before you connect in samba client. This can also be a filepath relative to the current directory or an absolute path.
  • is optional. If no is specified the filename of [local name] will be used.

Suppose we have a file named “test.txt”. We can transfer it by:

smb: \>puts test.txt

If we want it to be named “test2.txt” on the server. Type in:

smb: \>puts test.txt test2.txt

Make sure you have write access to the folder or else youll get an error like this “NT_STATUS_ACCESS_DENIED opening remote file ”

Copying Files

You can copy files from the server with Samba client using the command:

get  [local name]

and [local name] are the same as the the “put” command above.

To exit the Samba client just type “exit”.

5 Simple Tips In C++

Always Initialize Variables
Since C++ doesnt give variables a default value, you should initialize it to avoid problems. That is, instead of just saying:

int a;
double b;

You should instead:

int a = 0;
double b = 0.0

Always Use Smart Pointers
Smart pointers were made to make your life easier, so why not use it? You can use boost or the one from TR1. Also smart pointers are now included in C++11.

Dont use “using namespace” in a global scope
Frankly speaking I dont really use “using namespace” even on a local scope. Namespace was made to avoid name conflicts so why take them out of their namespace and transfer them into the global space? If you really are feeling lazy to type lengthy namespaces like the one from Boost you should just use an alias:

namespace foo3 = foo1::foo2::foo3;

Use “const” Modifier
“const” will convey to the user that you dont have an intention to alter any data member or function arguments. This can also prevent some of your teammates to create codes that can affect data inside it in the future. Think of it like a contract that any parameters with const will not be changed.

Use assert() For Debugging
assert() is a handy macro for debugging. It is also easy to remove once your building for release, just define “NDEBUG”. Some do prefer unit testing but if youre maintaining a project that was made earlier by another person that doesnt have a unit test ready . Then adding asserts for the new added codes is not as difficult as making unit tests for it.20

« Older posts

© 2019 James Baltar

Theme by Anders NorenUp ↑